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 "dynamic-string.h"
30 #include "fatal-signal.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 static const struct ovs_cmdl_command
*get_all_commands(void);
49 OVS_NO_RETURN
static void usage(void);
50 static void parse_options(int argc
, char *argv
[]);
52 static const char *default_db(void);
53 static const char *default_schema(void);
56 main(int argc
, char *argv
[])
58 set_program_name(argv
[0]);
59 parse_options(argc
, argv
);
60 fatal_ignore_sigpipe();
61 ovs_cmdl_run_command(argc
- optind
, argv
+ optind
, get_all_commands());
66 parse_options(int argc
, char *argv
[])
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 {"option", no_argument
, NULL
, 'o'},
73 {"version", no_argument
, NULL
, 'V'},
76 char *short_options
= ovs_cmdl_long_options_to_short_options(long_options
);
81 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
95 ovs_cmdl_print_options(long_options
);
99 ovs_print_version(0, 0);
103 vlog_set_verbosity(optarg
);
119 printf("%s: Open vSwitch database management utility\n"
120 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
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());
135 printf("\nOther options:\n"
136 " -m, --more increase show-log verbosity\n"
137 " -h, --help display this help message\n"
138 " -V, --version display version information\n");
147 db
= xasprintf("%s/conf.db", ovs_dbdir());
157 schema
= xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
163 parse_json(const char *s
)
165 struct json
*json
= json_from_string(s
);
166 if (json
->type
== JSON_STRING
) {
167 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
173 print_and_free_json(struct json
*json
)
175 char *string
= json_to_string(json
, JSSF_SORT
);
182 check_ovsdb_error(struct ovsdb_error
*error
)
185 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
190 do_create(int argc
, char *argv
[])
192 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
193 const char *schema_file_name
= argc
>= 3 ? argv
[2] : default_schema();
194 struct ovsdb_schema
*schema
;
195 struct ovsdb_log
*log
;
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
);
201 ovsdb_schema_destroy(schema
);
203 /* Create database file. */
204 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_CREATE
,
206 check_ovsdb_error(ovsdb_log_write(log
, json
));
207 check_ovsdb_error(ovsdb_log_commit(log
));
208 ovsdb_log_close(log
);
214 compact_or_convert(const char *src_name_
, const char *dst_name_
,
215 const struct ovsdb_schema
*new_schema
,
218 char *src_name
, *dst_name
;
219 struct lockfile
*src_lock
;
220 struct lockfile
*dst_lock
;
221 bool in_place
= dst_name_
== NULL
;
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_
);
231 ? xasprintf("%s.tmp", src_name
)
232 : follow_symlinks(dst_name_
));
234 /* Lock the source, if we will be replacing it. */
236 retval
= lockfile_lock(src_name
, &src_lock
);
238 ovs_fatal(retval
, "%s: failed to lock lockfile", src_name
);
242 /* Get (temporary) destination and lock it. */
243 retval
= lockfile_lock(dst_name
, &dst_lock
);
245 ovs_fatal(retval
, "%s: failed to lock lockfile", dst_name
);
249 check_ovsdb_error(new_schema
250 ? ovsdb_file_open_as_schema(src_name
, new_schema
, &db
)
251 : ovsdb_file_open(src_name
, true, &db
, NULL
));
252 check_ovsdb_error(ovsdb_file_save_copy(dst_name
, false, comment
, db
));
255 /* Replace source. */
260 if (rename(dst_name
, src_name
)) {
261 ovs_fatal(errno
, "failed to rename \"%s\" to \"%s\"",
264 fsync_parent_dir(dst_name
);
265 lockfile_unlock(src_lock
);
268 lockfile_unlock(dst_lock
);
275 do_compact(int argc
, char *argv
[])
277 const char *db
= argc
>= 2 ? argv
[1] : default_db();
278 const char *target
= argc
>= 3 ? argv
[2] : NULL
;
280 compact_or_convert(db
, target
, NULL
, "compacted by ovsdb-tool "VERSION
);
284 do_convert(int argc
, char *argv
[])
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
;
289 struct ovsdb_schema
*new_schema
;
291 check_ovsdb_error(ovsdb_schema_from_file(schema
, &new_schema
));
292 compact_or_convert(db
, target
, new_schema
,
293 "converted by ovsdb-tool "VERSION
);
294 ovsdb_schema_destroy(new_schema
);
298 do_needs_conversion(int argc
, char *argv
[])
300 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
301 const char *schema_file_name
= argc
>= 3 ? argv
[2] : default_schema();
302 struct ovsdb_schema
*schema1
, *schema2
;
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
);
312 do_db_version(int argc
, char *argv
[])
314 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
315 struct ovsdb_schema
*schema
;
317 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
318 puts(schema
->version
);
319 ovsdb_schema_destroy(schema
);
323 do_db_cksum(int argc OVS_UNUSED
, char *argv
[])
325 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
326 struct ovsdb_schema
*schema
;
328 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
330 ovsdb_schema_destroy(schema
);
334 do_schema_version(int argc
, char *argv
[])
336 const char *schema_file_name
= argc
>= 2 ? argv
[1] : default_schema();
337 struct ovsdb_schema
*schema
;
339 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
340 puts(schema
->version
);
341 ovsdb_schema_destroy(schema
);
345 do_schema_cksum(int argc
, char *argv
[])
347 const char *schema_file_name
= argc
>= 2 ? argv
[1] : default_schema();
348 struct ovsdb_schema
*schema
;
350 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
352 ovsdb_schema_destroy(schema
);
356 transact(bool read_only
, int argc
, char *argv
[])
358 const char *db_file_name
= argc
>= 3 ? argv
[1] : default_db();
359 const char *transaction
= argv
[argc
- 1];
360 struct json
*request
, *result
;
363 check_ovsdb_error(ovsdb_file_open(db_file_name
, read_only
, &db
, NULL
));
365 request
= parse_json(transaction
);
366 result
= ovsdb_execute(db
, NULL
, request
, 0, NULL
);
367 json_destroy(request
);
369 print_and_free_json(result
);
374 do_query(int argc
, char *argv
[])
376 transact(true, argc
, argv
);
380 do_transact(int argc
, char *argv
[])
382 transact(false, argc
, argv
);
386 print_db_changes(struct shash
*tables
, struct shash
*names
,
387 const struct ovsdb_schema
*schema
)
389 struct shash_node
*n1
;
391 SHASH_FOR_EACH (n1
, tables
) {
392 const char *table
= n1
->name
;
393 struct ovsdb_table_schema
*table_schema
;
394 struct json
*rows
= n1
->data
;
395 struct shash_node
*n2
;
397 if (n1
->name
[0] == '_' || rows
->type
!= JSON_OBJECT
) {
401 table_schema
= shash_find_data(&schema
->tables
, table
);
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;
409 old_name
= new_name
= shash_find_data(names
, row_uuid
);
410 if (columns
->type
== JSON_OBJECT
) {
411 struct json
*new_name_json
;
413 new_name_json
= shash_find_data(json_object(columns
), "name");
415 new_name
= json_to_string(new_name_json
, JSSF_SORT
);
416 free_new_name
= true;
420 printf("\ttable %s", table
);
424 printf(" insert row %s (%.8s):\n", new_name
, row_uuid
);
426 printf(" insert row %.8s:\n", row_uuid
);
429 printf(" row %s (%.8s):\n", old_name
, row_uuid
);
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
;
436 const struct ovsdb_column
*column_schema
;
437 struct json
*value
= n3
->data
;
438 char *value_string
= NULL
;
442 ? shash_find_data(&table_schema
->columns
, column
)
445 const struct ovsdb_type
*type
;
446 struct ovsdb_error
*error
;
447 struct ovsdb_datum datum
;
449 type
= &column_schema
->type
;
450 error
= ovsdb_datum_from_json(&datum
, type
,
456 ovsdb_datum_to_string(&datum
, type
, &s
);
457 value_string
= ds_steal_cstr(&s
);
459 ovsdb_error_destroy(error
);
463 value_string
= json_to_string(value
, JSSF_SORT
);
465 printf("\t\t%s=%s\n", column
, value_string
);
470 || (new_name
!= old_name
&& strcmp(old_name
, new_name
))) {
472 shash_delete(names
, shash_find(names
, row_uuid
));
475 shash_add(names
, row_uuid
, (new_name
477 : xmemdup0(row_uuid
, 8)));
479 } else if (columns
->type
== JSON_NULL
) {
480 struct shash_node
*node
;
482 printf("\t\tdelete row\n");
483 node
= shash_find(names
, row_uuid
);
485 shash_delete(names
, node
);
498 do_show_log(int argc
, char *argv
[])
500 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
502 struct ovsdb_log
*log
;
503 struct ovsdb_schema
*schema
;
506 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_READ_ONLY
,
513 check_ovsdb_error(ovsdb_log_read(log
, &json
));
518 printf("record %u:", i
);
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
) {
524 struct json
*date
, *comment
;
526 date
= shash_find_data(json_object(json
), "_date");
527 if (date
&& date
->type
== JSON_INTEGER
) {
528 long long int t
= json_integer(date
);
532 /* Older versions of ovsdb wrote timestamps in seconds. */
536 s
= xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t
, true);
541 comment
= shash_find_data(json_object(json
), "_comment");
542 if (comment
&& comment
->type
== JSON_STRING
) {
543 printf(" \"%s\"", json_string(comment
));
546 if (i
> 0 && show_log_verbosity
> 0) {
548 print_db_changes(json_object(json
), &names
, schema
);
555 ovsdb_log_close(log
);
556 ovsdb_schema_destroy(schema
);
557 /* XXX free 'names'. */
561 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
567 do_list_commands(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
569 ovs_cmdl_print_commands(get_all_commands());
572 static const struct ovs_cmdl_command all_commands
[] = {
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
},
589 static const struct ovs_cmdl_command
*get_all_commands(void)