Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / ovsdb / ovsdb-tool.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "compiler.h"
28 #include "dirs.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "file.h"
32 #include "lockfile.h"
33 #include "log.h"
34 #include "json.h"
35 #include "ovsdb.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-error.h"
38 #include "socket-util.h"
39 #include "table.h"
40 #include "timeval.h"
41 #include "util.h"
42 #include "vlog.h"
43
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity;
46
47 static const struct command *get_all_commands(void);
48
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
51
52 static const char *default_db(void);
53 static const char *default_schema(void);
54
55 int
56 main(int argc, char *argv[])
57 {
58     set_program_name(argv[0]);
59     parse_options(argc, argv);
60     fatal_ignore_sigpipe();
61     run_command(argc - optind, argv + optind, get_all_commands());
62     return 0;
63 }
64
65 static void
66 parse_options(int argc, char *argv[])
67 {
68     static const struct option long_options[] = {
69         {"more", no_argument, NULL, 'm'},
70         {"verbose", optional_argument, NULL, 'v'},
71         {"help", no_argument, NULL, 'h'},
72         {"version", no_argument, NULL, 'V'},
73         {NULL, 0, NULL, 0},
74     };
75     char *short_options = long_options_to_short_options(long_options);
76
77     for (;;) {
78         int c;
79
80         c = getopt_long(argc, argv, short_options, long_options, NULL);
81         if (c == -1) {
82             break;
83         }
84
85         switch (c) {
86         case 'm':
87             show_log_verbosity++;
88             break;
89
90         case 'h':
91             usage();
92
93         case 'V':
94             ovs_print_version(0, 0);
95             exit(EXIT_SUCCESS);
96
97         case 'v':
98             vlog_set_verbosity(optarg);
99             break;
100
101         case '?':
102             exit(EXIT_FAILURE);
103
104         default:
105             abort();
106         }
107     }
108     free(short_options);
109 }
110
111 static void
112 usage(void)
113 {
114     printf("%s: Open vSwitch database management utility\n"
115            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
116            "  create [DB [SCHEMA]]    create DB with the given SCHEMA\n"
117            "  compact [DB [DST]]      compact DB in-place (or to DST)\n"
118            "  convert [DB [SCHEMA [DST]]]   convert DB to SCHEMA (to DST)\n"
119            "  db-version [DB]         report version of schema used by DB\n"
120            "  db-cksum [DB]           report checksum of schema used by DB\n"
121            "  schema-version [SCHEMA] report SCHEMA's schema version\n"
122            "  schema-cksum [SCHEMA]   report SCHEMA's checksum\n"
123            "  query [DB] TRNS         execute read-only transaction on DB\n"
124            "  transact [DB] TRNS      execute read/write transaction on DB\n"
125            "  [-m]... show-log [DB]   print DB's log entries\n"
126            "The default DB is %s.\n"
127            "The default SCHEMA is %s.\n",
128            program_name, program_name, default_db(), default_schema());
129     vlog_usage();
130     printf("\nOther options:\n"
131            "  -m, --more                  increase show-log verbosity\n"
132            "  -h, --help                  display this help message\n"
133            "  -V, --version               display version information\n");
134     exit(EXIT_SUCCESS);
135 }
136
137 static const char *
138 default_db(void)
139 {
140     static char *db;
141     if (!db) {
142         db = xasprintf("%s/conf.db", ovs_dbdir());
143     }
144     return db;
145 }
146
147 static const char *
148 default_schema(void)
149 {
150     static char *schema;
151     if (!schema) {
152         schema = xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
153     }
154     return schema;
155 }
156 \f
157 static struct json *
158 parse_json(const char *s)
159 {
160     struct json *json = json_from_string(s);
161     if (json->type == JSON_STRING) {
162         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
163     }
164     return json;
165 }
166
167 static void
168 print_and_free_json(struct json *json)
169 {
170     char *string = json_to_string(json, JSSF_SORT);
171     json_destroy(json);
172     puts(string);
173     free(string);
174 }
175
176 static void
177 check_ovsdb_error(struct ovsdb_error *error)
178 {
179     if (error) {
180         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
181     }
182 }
183 \f
184 static void
185 do_create(int argc, char *argv[])
186 {
187     const char *db_file_name = argc >= 2 ? argv[1] : default_db();
188     const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
189     struct ovsdb_schema *schema;
190     struct ovsdb_log *log;
191     struct json *json;
192
193     /* Read schema from file and convert to JSON. */
194     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
195     json = ovsdb_schema_to_json(schema);
196     ovsdb_schema_destroy(schema);
197
198     /* Create database file. */
199     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
200                                      -1, &log));
201     check_ovsdb_error(ovsdb_log_write(log, json));
202     check_ovsdb_error(ovsdb_log_commit(log));
203     ovsdb_log_close(log);
204
205     json_destroy(json);
206 }
207
208 static void
209 compact_or_convert(const char *src_name_, const char *dst_name_,
210                    const struct ovsdb_schema *new_schema,
211                    const char *comment)
212 {
213     char *src_name, *dst_name;
214     struct lockfile *src_lock;
215     struct lockfile *dst_lock;
216     bool in_place = dst_name_ == NULL;
217     struct ovsdb *db;
218     int retval;
219
220     /* Dereference symlinks for source and destination names.  In the in-place
221      * case this ensures that, if the source name is a symlink, we replace its
222      * target instead of replacing the symlink by a regular file.  In the
223      * non-in-place, this has the same effect for the destination name. */
224     src_name = follow_symlinks(src_name_);
225     dst_name = (in_place
226                 ? xasprintf("%s.tmp", src_name)
227                 : follow_symlinks(dst_name_));
228
229     /* Lock the source, if we will be replacing it. */
230     if (in_place) {
231         retval = lockfile_lock(src_name, &src_lock);
232         if (retval) {
233             ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
234         }
235     }
236
237     /* Get (temporary) destination and lock it. */
238     retval = lockfile_lock(dst_name, &dst_lock);
239     if (retval) {
240         ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
241     }
242
243     /* Save a copy. */
244     check_ovsdb_error(new_schema
245                       ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
246                       : ovsdb_file_open(src_name, true, &db, NULL));
247     check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
248     ovsdb_destroy(db);
249
250     /* Replace source. */
251     if (in_place) {
252         if (rename(dst_name, src_name)) {
253             ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
254                       dst_name, src_name);
255         }
256         fsync_parent_dir(dst_name);
257         lockfile_unlock(src_lock);
258     }
259
260     lockfile_unlock(dst_lock);
261
262     free(src_name);
263     free(dst_name);
264 }
265
266 static void
267 do_compact(int argc, char *argv[])
268 {
269     const char *db = argc >= 2 ? argv[1] : default_db();
270     const char *target = argc >= 3 ? argv[2] : NULL;
271
272     compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
273 }
274
275 static void
276 do_convert(int argc, char *argv[])
277 {
278     const char *db = argc >= 2 ? argv[1] : default_db();
279     const char *schema = argc >= 3 ? argv[2] : default_schema();
280     const char *target = argc >= 4 ? argv[3] : NULL;
281     struct ovsdb_schema *new_schema;
282
283     check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
284     compact_or_convert(db, target, new_schema,
285                        "converted by ovsdb-tool "VERSION);
286     ovsdb_schema_destroy(new_schema);
287 }
288
289 static void
290 do_needs_conversion(int argc, char *argv[])
291 {
292     const char *db_file_name = argc >= 2 ? argv[1] : default_db();
293     const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
294     struct ovsdb_schema *schema1, *schema2;
295
296     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
297     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
298     puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
299     ovsdb_schema_destroy(schema1);
300     ovsdb_schema_destroy(schema2);
301 }
302
303 static void
304 do_db_version(int argc, char *argv[])
305 {
306     const char *db_file_name = argc >= 2 ? argv[1] : default_db();
307     struct ovsdb_schema *schema;
308
309     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
310     puts(schema->version);
311     ovsdb_schema_destroy(schema);
312 }
313
314 static void
315 do_db_cksum(int argc OVS_UNUSED, char *argv[])
316 {
317     const char *db_file_name = argc >= 2 ? argv[1] : default_db();
318     struct ovsdb_schema *schema;
319
320     check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
321     puts(schema->cksum);
322     ovsdb_schema_destroy(schema);
323 }
324
325 static void
326 do_schema_version(int argc, char *argv[])
327 {
328     const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
329     struct ovsdb_schema *schema;
330
331     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
332     puts(schema->version);
333     ovsdb_schema_destroy(schema);
334 }
335
336 static void
337 do_schema_cksum(int argc, char *argv[])
338 {
339     const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
340     struct ovsdb_schema *schema;
341
342     check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
343     puts(schema->cksum);
344     ovsdb_schema_destroy(schema);
345 }
346
347 static void
348 transact(bool read_only, int argc, char *argv[])
349 {
350     const char *db_file_name = argc >= 3 ? argv[1] : default_db();
351     const char *transaction = argv[argc - 1];
352     struct json *request, *result;
353     struct ovsdb *db;
354
355     check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
356
357     request = parse_json(transaction);
358     result = ovsdb_execute(db, NULL, request, 0, NULL);
359     json_destroy(request);
360
361     print_and_free_json(result);
362     ovsdb_destroy(db);
363 }
364
365 static void
366 do_query(int argc, char *argv[])
367 {
368     transact(true, argc, argv);
369 }
370
371 static void
372 do_transact(int argc, char *argv[])
373 {
374     transact(false, argc, argv);
375 }
376
377 static void
378 print_db_changes(struct shash *tables, struct shash *names,
379                  const struct ovsdb_schema *schema)
380 {
381     struct shash_node *n1;
382
383     SHASH_FOR_EACH (n1, tables) {
384         const char *table = n1->name;
385         struct ovsdb_table_schema *table_schema;
386         struct json *rows = n1->data;
387         struct shash_node *n2;
388
389         if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
390             continue;
391         }
392
393         table_schema = shash_find_data(&schema->tables, table);
394         SHASH_FOR_EACH (n2, json_object(rows)) {
395             const char *row_uuid = n2->name;
396             struct json *columns = n2->data;
397             struct shash_node *n3;
398             char *old_name, *new_name;
399             bool free_new_name = false;
400
401             old_name = new_name = shash_find_data(names, row_uuid);
402             if (columns->type == JSON_OBJECT) {
403                 struct json *new_name_json;
404
405                 new_name_json = shash_find_data(json_object(columns), "name");
406                 if (new_name_json) {
407                     new_name = json_to_string(new_name_json, JSSF_SORT);
408                     free_new_name = true;
409                 }
410             }
411
412             printf("\ttable %s", table);
413
414             if (!old_name) {
415                 if (new_name) {
416                     printf(" insert row %s (%.8s):\n", new_name, row_uuid);
417                 } else {
418                     printf(" insert row %.8s:\n", row_uuid);
419                 }
420             } else {
421                 printf(" row %s (%.8s):\n", old_name, row_uuid);
422             }
423
424             if (columns->type == JSON_OBJECT) {
425                 if (show_log_verbosity > 1) {
426                     SHASH_FOR_EACH (n3, json_object(columns)) {
427                         const char *column = n3->name;
428                         const struct ovsdb_column *column_schema;
429                         struct json *value = n3->data;
430                         char *value_string = NULL;
431
432                         column_schema =
433                             (table_schema
434                              ? shash_find_data(&table_schema->columns, column)
435                              : NULL);
436                         if (column_schema) {
437                             const struct ovsdb_type *type;
438                             struct ovsdb_error *error;
439                             struct ovsdb_datum datum;
440
441                             type = &column_schema->type;
442                             error = ovsdb_datum_from_json(&datum, type,
443                                                           value, NULL);
444                             if (!error) {
445                                 struct ds s;
446
447                                 ds_init(&s);
448                                 ovsdb_datum_to_string(&datum, type, &s);
449                                 value_string = ds_steal_cstr(&s);
450                             } else {
451                                 ovsdb_error_destroy(error);
452                             }
453                         }
454                         if (!value_string) {
455                             value_string = json_to_string(value, JSSF_SORT);
456                         }
457                         printf("\t\t%s=%s\n", column, value_string);
458                         free(value_string);
459                     }
460                 }
461                 if (!old_name
462                     || (new_name != old_name && strcmp(old_name, new_name))) {
463                     if (old_name) {
464                         shash_delete(names, shash_find(names, row_uuid));
465                         free(old_name);
466                     }
467                     shash_add(names, row_uuid, (new_name
468                                                 ? xstrdup(new_name)
469                                                 : xmemdup0(row_uuid, 8)));
470                 }
471             } else if (columns->type == JSON_NULL) {
472                 struct shash_node *node;
473
474                 printf("\t\tdelete row\n");
475                 node = shash_find(names, row_uuid);
476                 if (node) {
477                     shash_delete(names, node);
478                 }
479                 free(old_name);
480             }
481
482             if (free_new_name) {
483                 free(new_name);
484             }
485         }
486     }
487 }
488
489 static void
490 do_show_log(int argc, char *argv[])
491 {
492     const char *db_file_name = argc >= 2 ? argv[1] : default_db();
493     struct shash names;
494     struct ovsdb_log *log;
495     struct ovsdb_schema *schema;
496     unsigned int i;
497
498     check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
499                                      -1, &log));
500     shash_init(&names);
501     schema = NULL;
502     for (i = 0; ; i++) {
503         struct json *json;
504
505         check_ovsdb_error(ovsdb_log_read(log, &json));
506         if (!json) {
507             break;
508         }
509
510         printf("record %u:", i);
511         if (i == 0) {
512             check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
513             printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
514                    schema->name, schema->version, schema->cksum);
515         } else if (json->type == JSON_OBJECT) {
516             struct json *date, *comment;
517
518             date = shash_find_data(json_object(json), "_date");
519             if (date && date->type == JSON_INTEGER) {
520                 long long int t = json_integer(date);
521                 char *s;
522
523                 if (t < INT32_MAX) {
524                     /* Older versions of ovsdb wrote timestamps in seconds. */
525                     t *= 1000;
526                 }
527
528                 s = xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t, true);
529                 fputs(s, stdout);
530                 free(s);
531             }
532
533             comment = shash_find_data(json_object(json), "_comment");
534             if (comment && comment->type == JSON_STRING) {
535                 printf(" \"%s\"", json_string(comment));
536             }
537
538             if (i > 0 && show_log_verbosity > 0) {
539                 putchar('\n');
540                 print_db_changes(json_object(json), &names, schema);
541             }
542         }
543         json_destroy(json);
544         putchar('\n');
545     }
546
547     ovsdb_log_close(log);
548     ovsdb_schema_destroy(schema);
549     /* XXX free 'names'. */
550 }
551
552 static void
553 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
554 {
555     usage();
556 }
557
558 static const struct command all_commands[] = {
559     { "create", 0, 2, do_create },
560     { "compact", 0, 2, do_compact },
561     { "convert", 0, 3, do_convert },
562     { "needs-conversion", 0, 2, do_needs_conversion },
563     { "db-version", 0, 1, do_db_version },
564     { "db-cksum", 0, 1, do_db_cksum },
565     { "schema-version", 0, 1, do_schema_version },
566     { "schema-cksum", 0, 1, do_schema_cksum },
567     { "query", 1, 2, do_query },
568     { "transact", 1, 2, do_transact },
569     { "show-log", 0, 1, do_show_log },
570     { "help", 0, INT_MAX, do_help },
571     { NULL, 0, 0, NULL },
572 };
573
574 static const struct command *get_all_commands(void)
575 {
576     return all_commands;
577 }