2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "command-line.h"
29 #include "openvswitch/dynamic-string.h"
30 #include "fatal-signal.h"
34 #include "openvswitch/json.h"
36 #include "ovsdb-data.h"
37 #include "ovsdb-error.h"
38 #include "socket-util.h"
42 #include "openvswitch/vlog.h"
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity
;
47 /* --role: RBAC role to use for "transact" and "query" commands. */
48 static const char *rbac_role
;
50 static const struct ovs_cmdl_command
*get_all_commands(void);
52 OVS_NO_RETURN
static void usage(void);
53 static void parse_options(int argc
, char *argv
[]);
55 static const char *default_db(void);
56 static const char *default_schema(void);
59 main(int argc
, char *argv
[])
61 struct ovs_cmdl_context ctx
= { .argc
= 0, };
62 set_program_name(argv
[0]);
63 parse_options(argc
, argv
);
64 fatal_ignore_sigpipe();
65 ctx
.argc
= argc
- optind
;
66 ctx
.argv
= argv
+ optind
;
67 ovs_cmdl_run_command(&ctx
, get_all_commands());
72 parse_options(int argc
, char *argv
[])
75 OPT_RBAC_ROLE
= UCHAR_MAX
+ 1
77 static const struct option long_options
[] = {
78 {"more", no_argument
, NULL
, 'm'},
79 {"rbac-role", required_argument
, NULL
, OPT_RBAC_ROLE
},
80 {"verbose", optional_argument
, NULL
, 'v'},
81 {"help", no_argument
, NULL
, 'h'},
82 {"option", no_argument
, NULL
, 'o'},
83 {"version", no_argument
, NULL
, 'V'},
86 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
91 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
109 ovs_cmdl_print_options(long_options
);
113 ovs_print_version(0, 0);
117 vlog_set_verbosity(optarg
);
133 printf("%s: Open vSwitch database management utility\n"
134 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
135 " create [DB [SCHEMA]] create DB with the given SCHEMA\n"
136 " compact [DB [DST]] compact DB in-place (or to DST)\n"
137 " convert [DB [SCHEMA [DST]]] convert DB to SCHEMA (to DST)\n"
138 " db-version [DB] report version of schema used by DB\n"
139 " db-cksum [DB] report checksum of schema used by DB\n"
140 " schema-version [SCHEMA] report SCHEMA's schema version\n"
141 " schema-cksum [SCHEMA] report SCHEMA's checksum\n"
142 " query [DB] TRNS execute read-only transaction on DB\n"
143 " transact [DB] TRNS execute read/write transaction on DB\n"
144 " [-m]... show-log [DB] print DB's log entries\n"
145 "The default DB is %s.\n"
146 "The default SCHEMA is %s.\n",
147 program_name
, program_name
, default_db(), default_schema());
151 -m, --more increase show-log verbosity\n\
152 --rbac-role=ROLE RBAC role for transact and query commands\n\
153 -h, --help display this help message\n\
154 -V, --version display version information\n");
163 db
= xasprintf("%s/conf.db", ovs_dbdir());
173 schema
= xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
179 parse_json(const char *s
)
181 struct json
*json
= json_from_string(s
);
182 if (json
->type
== JSON_STRING
) {
183 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
189 print_and_free_json(struct json
*json
)
191 char *string
= json_to_string(json
, JSSF_SORT
);
198 check_ovsdb_error(struct ovsdb_error
*error
)
201 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
206 do_create(struct ovs_cmdl_context
*ctx
)
208 const char *db_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
209 const char *schema_file_name
= ctx
->argc
>= 3 ? ctx
->argv
[2] : default_schema();
210 struct ovsdb_schema
*schema
;
211 struct ovsdb_log
*log
;
214 /* Read schema from file and convert to JSON. */
215 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
216 json
= ovsdb_schema_to_json(schema
);
217 ovsdb_schema_destroy(schema
);
219 /* Create database file. */
220 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_CREATE
,
222 check_ovsdb_error(ovsdb_log_write(log
, json
));
223 check_ovsdb_error(ovsdb_log_commit(log
));
224 ovsdb_log_close(log
);
230 compact_or_convert(const char *src_name_
, const char *dst_name_
,
231 const struct ovsdb_schema
*new_schema
,
234 char *src_name
, *dst_name
;
235 struct lockfile
*src_lock
;
236 struct lockfile
*dst_lock
;
237 bool in_place
= dst_name_
== NULL
;
241 /* Dereference symlinks for source and destination names. In the in-place
242 * case this ensures that, if the source name is a symlink, we replace its
243 * target instead of replacing the symlink by a regular file. In the
244 * non-in-place, this has the same effect for the destination name. */
245 src_name
= follow_symlinks(src_name_
);
247 ? xasprintf("%s.tmp", src_name
)
248 : follow_symlinks(dst_name_
));
250 /* Lock the source, if we will be replacing it. */
252 retval
= lockfile_lock(src_name
, &src_lock
);
254 ovs_fatal(retval
, "%s: failed to lock lockfile", src_name
);
258 /* Get (temporary) destination and lock it. */
259 retval
= lockfile_lock(dst_name
, &dst_lock
);
261 ovs_fatal(retval
, "%s: failed to lock lockfile", dst_name
);
265 check_ovsdb_error(new_schema
266 ? ovsdb_file_open_as_schema(src_name
, new_schema
, &db
)
267 : ovsdb_file_open(src_name
, true, &db
, NULL
));
268 check_ovsdb_error(ovsdb_file_save_copy(dst_name
, false, comment
, db
));
271 /* Replace source. */
276 if (rename(dst_name
, src_name
)) {
277 ovs_fatal(errno
, "failed to rename \"%s\" to \"%s\"",
280 fsync_parent_dir(dst_name
);
281 lockfile_unlock(src_lock
);
284 lockfile_unlock(dst_lock
);
291 do_compact(struct ovs_cmdl_context
*ctx
)
293 const char *db
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
294 const char *target
= ctx
->argc
>= 3 ? ctx
->argv
[2] : NULL
;
296 compact_or_convert(db
, target
, NULL
, "compacted by ovsdb-tool "VERSION
);
300 do_convert(struct ovs_cmdl_context
*ctx
)
302 const char *db
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
303 const char *schema
= ctx
->argc
>= 3 ? ctx
->argv
[2] : default_schema();
304 const char *target
= ctx
->argc
>= 4 ? ctx
->argv
[3] : NULL
;
305 struct ovsdb_schema
*new_schema
;
307 check_ovsdb_error(ovsdb_schema_from_file(schema
, &new_schema
));
308 compact_or_convert(db
, target
, new_schema
,
309 "converted by ovsdb-tool "VERSION
);
310 ovsdb_schema_destroy(new_schema
);
314 do_needs_conversion(struct ovs_cmdl_context
*ctx
)
316 const char *db_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
317 const char *schema_file_name
= ctx
->argc
>= 3 ? ctx
->argv
[2] : default_schema();
318 struct ovsdb_schema
*schema1
, *schema2
;
320 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema1
));
321 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema2
));
322 puts(ovsdb_schema_equal(schema1
, schema2
) ? "no" : "yes");
323 ovsdb_schema_destroy(schema1
);
324 ovsdb_schema_destroy(schema2
);
328 do_db_version(struct ovs_cmdl_context
*ctx
)
330 const char *db_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
331 struct ovsdb_schema
*schema
;
333 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
334 puts(schema
->version
);
335 ovsdb_schema_destroy(schema
);
339 do_db_cksum(struct ovs_cmdl_context
*ctx
)
341 const char *db_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
342 struct ovsdb_schema
*schema
;
344 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
346 ovsdb_schema_destroy(schema
);
350 do_schema_version(struct ovs_cmdl_context
*ctx
)
352 const char *schema_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_schema();
353 struct ovsdb_schema
*schema
;
355 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
356 puts(schema
->version
);
357 ovsdb_schema_destroy(schema
);
361 do_schema_cksum(struct ovs_cmdl_context
*ctx
)
363 const char *schema_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_schema();
364 struct ovsdb_schema
*schema
;
366 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
368 ovsdb_schema_destroy(schema
);
372 transact(bool read_only
, int argc
, char *argv
[])
374 const char *db_file_name
= argc
>= 3 ? argv
[1] : default_db();
375 const char *transaction
= argv
[argc
- 1];
376 struct json
*request
, *result
;
379 check_ovsdb_error(ovsdb_file_open(db_file_name
, read_only
, &db
, NULL
));
381 request
= parse_json(transaction
);
382 result
= ovsdb_execute(db
, NULL
, request
, false, rbac_role
, NULL
, 0, NULL
);
383 json_destroy(request
);
385 print_and_free_json(result
);
390 do_query(struct ovs_cmdl_context
*ctx
)
392 transact(true, ctx
->argc
, ctx
->argv
);
396 do_transact(struct ovs_cmdl_context
*ctx
)
398 transact(false, ctx
->argc
, ctx
->argv
);
402 print_db_changes(struct shash
*tables
, struct shash
*names
,
403 const struct ovsdb_schema
*schema
)
405 struct shash_node
*n1
;
407 SHASH_FOR_EACH (n1
, tables
) {
408 const char *table
= n1
->name
;
409 struct ovsdb_table_schema
*table_schema
;
410 struct json
*rows
= n1
->data
;
411 struct shash_node
*n2
;
413 if (n1
->name
[0] == '_' || rows
->type
!= JSON_OBJECT
) {
417 table_schema
= shash_find_data(&schema
->tables
, table
);
418 SHASH_FOR_EACH (n2
, json_object(rows
)) {
419 const char *row_uuid
= n2
->name
;
420 struct json
*columns
= n2
->data
;
421 struct shash_node
*n3
;
422 char *old_name
, *new_name
;
423 bool free_new_name
= false;
425 old_name
= new_name
= shash_find_data(names
, row_uuid
);
426 if (columns
->type
== JSON_OBJECT
) {
427 struct json
*new_name_json
;
429 new_name_json
= shash_find_data(json_object(columns
), "name");
431 new_name
= json_to_string(new_name_json
, JSSF_SORT
);
432 free_new_name
= true;
436 printf("\ttable %s", table
);
440 printf(" insert row %s (%.8s):\n", new_name
, row_uuid
);
442 printf(" insert row %.8s:\n", row_uuid
);
445 printf(" row %s (%.8s):\n", old_name
, row_uuid
);
448 if (columns
->type
== JSON_OBJECT
) {
449 if (show_log_verbosity
> 1) {
450 SHASH_FOR_EACH (n3
, json_object(columns
)) {
451 const char *column
= n3
->name
;
452 const struct ovsdb_column
*column_schema
;
453 struct json
*value
= n3
->data
;
454 char *value_string
= NULL
;
458 ? shash_find_data(&table_schema
->columns
, column
)
461 const struct ovsdb_type
*type
;
462 struct ovsdb_error
*error
;
463 struct ovsdb_datum datum
;
465 type
= &column_schema
->type
;
466 error
= ovsdb_datum_from_json(&datum
, type
,
472 ovsdb_datum_to_string(&datum
, type
, &s
);
473 value_string
= ds_steal_cstr(&s
);
475 ovsdb_error_destroy(error
);
479 value_string
= json_to_string(value
, JSSF_SORT
);
481 printf("\t\t%s=%s\n", column
, value_string
);
486 || (new_name
!= old_name
&& strcmp(old_name
, new_name
))) {
488 shash_delete(names
, shash_find(names
, row_uuid
));
491 shash_add(names
, row_uuid
, (new_name
493 : xmemdup0(row_uuid
, 8)));
495 } else if (columns
->type
== JSON_NULL
) {
496 struct shash_node
*node
;
498 printf("\t\tdelete row\n");
499 node
= shash_find(names
, row_uuid
);
501 shash_delete(names
, node
);
514 do_show_log(struct ovs_cmdl_context
*ctx
)
516 const char *db_file_name
= ctx
->argc
>= 2 ? ctx
->argv
[1] : default_db();
518 struct ovsdb_log
*log
;
519 struct ovsdb_schema
*schema
;
522 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_READ_ONLY
,
529 check_ovsdb_error(ovsdb_log_read(log
, &json
));
534 printf("record %u:", i
);
536 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
537 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
538 schema
->name
, schema
->version
, schema
->cksum
);
539 } else if (json
->type
== JSON_OBJECT
) {
540 struct json
*date
, *comment
;
542 date
= shash_find_data(json_object(json
), "_date");
543 if (date
&& date
->type
== JSON_INTEGER
) {
544 long long int t
= json_integer(date
);
548 /* Older versions of ovsdb wrote timestamps in seconds. */
552 s
= xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t
, true);
557 comment
= shash_find_data(json_object(json
), "_comment");
558 if (comment
&& comment
->type
== JSON_STRING
) {
559 printf(" \"%s\"", json_string(comment
));
562 if (i
> 0 && show_log_verbosity
> 0) {
564 print_db_changes(json_object(json
), &names
, schema
);
571 ovsdb_log_close(log
);
572 ovsdb_schema_destroy(schema
);
573 /* XXX free 'names'. */
577 do_help(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
583 do_list_commands(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
585 ovs_cmdl_print_commands(get_all_commands());
588 static const struct ovs_cmdl_command all_commands
[] = {
589 { "create", "[db [schema]]", 0, 2, do_create
, OVS_RW
},
590 { "compact", "[db [dst]]", 0, 2, do_compact
, OVS_RW
},
591 { "convert", "[db [schema [dst]]]", 0, 3, do_convert
, OVS_RW
},
592 { "needs-conversion", NULL
, 0, 2, do_needs_conversion
, OVS_RO
},
593 { "db-version", "[db]", 0, 1, do_db_version
, OVS_RO
},
594 { "db-cksum", "[db]", 0, 1, do_db_cksum
, OVS_RO
},
595 { "schema-version", "[schema]", 0, 1, do_schema_version
, OVS_RO
},
596 { "schema-cksum", "[schema]", 0, 1, do_schema_cksum
, OVS_RO
},
597 { "query", "[db] trns", 1, 2, do_query
, OVS_RO
},
598 { "transact", "[db] trns", 1, 2, do_transact
, OVS_RO
},
599 { "show-log", "[db]", 0, 1, do_show_log
, OVS_RO
},
600 { "help", NULL
, 0, INT_MAX
, do_help
, OVS_RO
},
601 { "list-commands", NULL
, 0, INT_MAX
, do_list_commands
, OVS_RO
},
602 { NULL
, NULL
, 0, 0, NULL
, OVS_RO
},
605 static const struct ovs_cmdl_command
*get_all_commands(void)