]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/ovsdb-tool.c
Debian: Update change log for 1.1.0~pre2.g2.ea763e0e-1 upload
[mirror_ovs.git] / ovsdb / ovsdb-tool.c
CommitLineData
f85f8ebb 1/*
c6782bb0 2 * Copyright (c) 2009, 2010 Nicira Networks.
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
25#include "command-line.h"
26#include "compiler.h"
bd06962a 27#include "file.h"
1e19e50e 28#include "lockfile.h"
41709ccc 29#include "log.h"
f85f8ebb
BP
30#include "json.h"
31#include "ovsdb.h"
32#include "ovsdb-error.h"
1e19e50e 33#include "socket-util.h"
f85f8ebb
BP
34#include "table.h"
35#include "timeval.h"
36#include "util.h"
f85f8ebb 37#include "vlog.h"
5136ce49 38
d98e6007 39VLOG_DEFINE_THIS_MODULE(ovsdb_tool);
f85f8ebb 40
c6782bb0
BP
41/* -m, --more: Verbosity level for "show-log" command output. */
42static int show_log_verbosity;
43
f85f8ebb
BP
44static const struct command all_commands[];
45
46static void usage(void) NO_RETURN;
47static void parse_options(int argc, char *argv[]);
48
49int
50main(int argc, char *argv[])
51{
52 set_program_name(argv[0]);
f85f8ebb
BP
53 parse_options(argc, argv);
54 signal(SIGPIPE, SIG_IGN);
55 run_command(argc - optind, argv + optind, all_commands);
56 return 0;
57}
58
59static void
60parse_options(int argc, char *argv[])
61{
62 static struct option long_options[] = {
c6782bb0 63 {"more", no_argument, 0, 'm'},
f85f8ebb
BP
64 {"verbose", optional_argument, 0, 'v'},
65 {"help", no_argument, 0, 'h'},
66 {"version", no_argument, 0, 'V'},
67 {0, 0, 0, 0},
68 };
69 char *short_options = long_options_to_short_options(long_options);
70
71 for (;;) {
72 int c;
73
74 c = getopt_long(argc, argv, short_options, long_options, NULL);
75 if (c == -1) {
76 break;
77 }
78
79 switch (c) {
c6782bb0
BP
80 case 'm':
81 show_log_verbosity++;
82 break;
83
f85f8ebb
BP
84 case 'h':
85 usage();
86
87 case 'V':
88 OVS_PRINT_VERSION(0, 0);
89 exit(EXIT_SUCCESS);
90
91 case 'v':
92 vlog_set_verbosity(optarg);
93 break;
94
95 case '?':
96 exit(EXIT_FAILURE);
97
98 default:
99 abort();
100 }
101 }
102 free(short_options);
103}
104
105static void
106usage(void)
107{
108 printf("%s: Open vSwitch database management utility\n"
109 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110 " create DB SCHEMA create DB with the given SCHEMA\n"
111 " compact DB [DST] compact DB in-place (or to DST)\n"
1e19e50e 112 " convert DB SCHEMA [DST] convert DB to SCHEMA (to DST)\n"
8159b984
BP
113 " db-version DB report version of schema used by DB\n"
114 " schema-version SCHEMA report SCHEMA's schema version\n"
f85f8ebb 115 " query DB TRNS execute read-only transaction on DB\n"
722f6301
BP
116 " transact DB TRNS execute read/write transaction on DB\n"
117 " show-log DB prints information about DB's log entries\n",
f85f8ebb
BP
118 program_name, program_name);
119 vlog_usage();
120 printf("\nOther options:\n"
c6782bb0 121 " -m, --more increase show-log verbosity\n"
f85f8ebb
BP
122 " -h, --help display this help message\n"
123 " -V, --version display version information\n");
124 exit(EXIT_SUCCESS);
125}
126\f
127static struct json *
128parse_json(const char *s)
129{
130 struct json *json = json_from_string(s);
131 if (json->type == JSON_STRING) {
132 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
133 }
134 return json;
135}
136
137static void
138print_and_free_json(struct json *json)
139{
140 char *string = json_to_string(json, JSSF_SORT);
141 json_destroy(json);
142 puts(string);
143 free(string);
144}
145
146static void
147check_ovsdb_error(struct ovsdb_error *error)
148{
149 if (error) {
150 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
151 }
152}
153\f
154static void
c69ee87c 155do_create(int argc OVS_UNUSED, char *argv[])
f85f8ebb
BP
156{
157 const char *db_file_name = argv[1];
158 const char *schema_file_name = argv[2];
159 struct ovsdb_schema *schema;
41709ccc 160 struct ovsdb_log *log;
f85f8ebb
BP
161 struct json *json;
162
163 /* Read schema from file and convert to JSON. */
164 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
165 json = ovsdb_schema_to_json(schema);
90cc4071 166 ovsdb_schema_destroy(schema);
f85f8ebb
BP
167
168 /* Create database file. */
7446f148
BP
169 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
170 -1, &log));
41709ccc
BP
171 check_ovsdb_error(ovsdb_log_write(log, json));
172 check_ovsdb_error(ovsdb_log_commit(log));
173 ovsdb_log_close(log);
f85f8ebb
BP
174
175 json_destroy(json);
176}
177
1e19e50e
BP
178static void
179compact_or_convert(const char *src_name, const char *dst_name,
180 const struct ovsdb_schema *new_schema,
181 const char *comment)
182{
183 struct lockfile *src_lock;
184 struct lockfile *dst_lock;
185 bool in_place = dst_name == NULL;
186 struct ovsdb *db;
187 int retval;
188
aebfc869 189 /* Lock the source, if we will be replacing it. */
1e19e50e 190 if (in_place) {
7a6ec19d 191 retval = lockfile_lock(src_name, 0, &src_lock);
aebfc869
BP
192 if (retval) {
193 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
194 }
1e19e50e
BP
195 }
196
aebfc869
BP
197 /* Get (temporary) destination and lock it. */
198 if (in_place) {
199 dst_name = xasprintf("%s.tmp", src_name);
1e19e50e 200 }
7a6ec19d 201 retval = lockfile_lock(dst_name, 0, &dst_lock);
1e19e50e
BP
202 if (retval) {
203 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
204 }
205
206 /* Save a copy. */
207 check_ovsdb_error(new_schema
208 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
ada496b5 209 : ovsdb_file_open(src_name, true, &db, NULL));
1e19e50e
BP
210 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
211 ovsdb_destroy(db);
212
213 /* Replace source. */
214 if (in_place) {
215 if (rename(dst_name, src_name)) {
216 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
217 dst_name, src_name);
218 }
219 fsync_parent_dir(dst_name);
1e19e50e
BP
220 lockfile_unlock(src_lock);
221 }
222
223 lockfile_unlock(dst_lock);
224}
225
226static void
227do_compact(int argc OVS_UNUSED, char *argv[])
228{
229 compact_or_convert(argv[1], argv[2], NULL, "compacted by ovsdb-tool");
230}
231
232static void
233do_convert(int argc OVS_UNUSED, char *argv[])
234{
235 const char *schema_file_name = argv[2];
236 struct ovsdb_schema *new_schema;
237
238 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
239 compact_or_convert(argv[1], argv[3], new_schema,
240 "converted by ovsdb-tool");
241 ovsdb_schema_destroy(new_schema);
242}
243
8159b984
BP
244static void
245do_db_version(int argc OVS_UNUSED, char *argv[])
246{
247 const char *db_file_name = argv[1];
248 struct ovsdb *db;
249
250 check_ovsdb_error(ovsdb_file_open(db_file_name, true, &db, NULL));
251 puts(db->schema->version);
252 ovsdb_destroy(db);
253}
254
255static void
256do_schema_version(int argc OVS_UNUSED, char *argv[])
257{
258 const char *schema_file_name = argv[1];
259 struct ovsdb_schema *schema;
260
261 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
262 puts(schema->version);
263 ovsdb_schema_destroy(schema);
264}
265
f85f8ebb 266static void
8b681e6f 267transact(bool read_only, const char *db_file_name, const char *transaction)
f85f8ebb
BP
268{
269 struct json *request, *result;
270 struct ovsdb *db;
271
ada496b5 272 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
f85f8ebb
BP
273
274 request = parse_json(transaction);
275 result = ovsdb_execute(db, request, 0, NULL);
276 json_destroy(request);
277
278 print_and_free_json(result);
279 ovsdb_destroy(db);
280}
281
282static void
c69ee87c 283do_query(int argc OVS_UNUSED, char *argv[])
f85f8ebb 284{
8b681e6f 285 transact(true, argv[1], argv[2]);
f85f8ebb
BP
286}
287
288static void
c69ee87c 289do_transact(int argc OVS_UNUSED, char *argv[])
f85f8ebb 290{
8b681e6f 291 transact(false, argv[1], argv[2]);
f85f8ebb
BP
292}
293
c6782bb0
BP
294static void
295print_db_changes(struct shash *tables, struct shash *names)
296{
297 struct shash_node *n1;
298
299 SHASH_FOR_EACH (n1, tables) {
300 const char *table = n1->name;
301 struct json *rows = n1->data;
302 struct shash_node *n2;
303
304 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
305 continue;
306 }
307
308 SHASH_FOR_EACH (n2, json_object(rows)) {
309 const char *row_uuid = n2->name;
310 struct json *columns = n2->data;
311 struct shash_node *n3;
312 char *old_name, *new_name;
313 bool free_new_name = false;
314
315 old_name = new_name = shash_find_data(names, row_uuid);
316 if (columns->type == JSON_OBJECT) {
317 struct json *new_name_json;
318
319 new_name_json = shash_find_data(json_object(columns), "name");
320 if (new_name_json) {
321 new_name = json_to_string(new_name_json, JSSF_SORT);
322 free_new_name = true;
323 }
324 }
325
326 printf("\ttable %s", table);
327
328 if (!old_name) {
329 if (new_name) {
330 printf(" insert row %s:\n", new_name);
331 } else {
332 printf(" insert row %.8s:\n", row_uuid);
333 }
334 } else {
335 printf(" row %s:\n", old_name);
336 }
337
338 if (columns->type == JSON_OBJECT) {
339 if (show_log_verbosity > 1) {
340 SHASH_FOR_EACH (n3, json_object(columns)) {
341 const char *column = n3->name;
342 struct json *value = n3->data;
343 char *value_string;
344
345 value_string = json_to_string(value, JSSF_SORT);
346 printf("\t\t%s=%s\n", column, value_string);
347 free(value_string);
348 }
349 }
350 if (!old_name
351 || (new_name != old_name && strcmp(old_name, new_name))) {
352 if (old_name) {
353 shash_delete(names, shash_find(names, row_uuid));
354 free(old_name);
355 }
356 shash_add(names, row_uuid, (new_name
357 ? xstrdup(new_name)
358 : xmemdup0(row_uuid, 8)));
359 }
360 } else if (columns->type == JSON_NULL) {
b932d88b
BP
361 struct shash_node *node;
362
c6782bb0 363 printf("\t\tdelete row\n");
b932d88b
BP
364 node = shash_find(names, row_uuid);
365 if (node) {
366 shash_delete(names, node);
367 }
c6782bb0
BP
368 free(old_name);
369 }
370
371 if (free_new_name) {
372 free(new_name);
373 }
374 }
375 }
376}
377
722f6301 378static void
c69ee87c 379do_show_log(int argc OVS_UNUSED, char *argv[])
722f6301
BP
380{
381 const char *db_file_name = argv[1];
c6782bb0 382 struct shash names;
722f6301
BP
383 struct ovsdb_log *log;
384 unsigned int i;
385
7446f148
BP
386 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
387 -1, &log));
c6782bb0 388 shash_init(&names);
722f6301
BP
389 for (i = 0; ; i++) {
390 struct json *json;
391
392 check_ovsdb_error(ovsdb_log_read(log, &json));
393 if (!json) {
394 break;
395 }
396
397 printf("record %u:", i);
398 if (json->type == JSON_OBJECT) {
399 struct json *date, *comment;
400
401 date = shash_find_data(json_object(json), "_date");
402 if (date && date->type == JSON_INTEGER) {
403 time_t t = json_integer(date);
404 char s[128];
405
406 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
407 printf(" %s", s);
408 }
409
410 comment = shash_find_data(json_object(json), "_comment");
411 if (comment && comment->type == JSON_STRING) {
412 printf(" \"%s\"", json_string(comment));
413 }
c6782bb0
BP
414
415 if (i > 0 && show_log_verbosity > 0) {
416 putchar('\n');
417 print_db_changes(json_object(json), &names);
418 }
722f6301
BP
419 }
420 json_destroy(json);
421 putchar('\n');
422 }
c6782bb0
BP
423
424 /* XXX free 'names'. */
722f6301
BP
425}
426
f85f8ebb 427static void
c69ee87c 428do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
429{
430 usage();
431}
432
433static const struct command all_commands[] = {
434 { "create", 2, 2, do_create },
1e19e50e
BP
435 { "compact", 1, 2, do_compact },
436 { "convert", 2, 3, do_convert },
8159b984
BP
437 { "db-version", 1, 1, do_db_version },
438 { "schema-version", 1, 1, do_schema_version },
f85f8ebb
BP
439 { "query", 2, 2, do_query },
440 { "transact", 2, 2, do_transact },
722f6301 441 { "show-log", 1, 1, do_show_log },
f85f8ebb
BP
442 { "help", 0, INT_MAX, do_help },
443 { NULL, 0, 0, NULL },
444};