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