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