2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at:
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
22 #include "command-line.h"
24 #include "fatal-signal.h"
25 #include "ovn/ovn-nb-idl.h"
26 #include "poll-loop.h"
29 #include "stream-ssl.h"
31 #include "openvswitch/vlog.h"
33 VLOG_DEFINE_THIS_MODULE(ovn_nbctl);
35 struct nbctl_context {
36 struct ovsdb_idl *idl;
37 struct ovsdb_idl_txn *txn;
40 static const char *db;
42 static const char *default_db(void);
48 %s: OVN northbound DB management utility\n\
49 usage: %s [OPTIONS] COMMAND [ARG...]\n\
51 Logical switch commands:\n\
52 lswitch-add [LSWITCH] create a logical switch named LSWITCH\n\
53 lswitch-del LSWITCH delete LSWITCH and all its ports\n\
54 lswitch-list print the names of all logical switches\n\
55 lswitch-set-external-id LSWITCH KEY [VALUE]\n\
56 set or delete an external-id on LSWITCH\n\
57 lswitch-get-external-id LSWITCH [KEY]\n\
58 list one or all external-ids on LSWITCH\n\
60 Logical port commands:\n\
61 lport-add LSWITCH LPORT add logical port LPORT on LSWITCH\n\
62 lport-add LSWITCH LPORT PARENT TAG\n\
63 add logical port LPORT on LSWITCH with PARENT\n\
65 lport-del LPORT delete LPORT from its attached switch\n\
66 lport-list LSWITCH print the names of all logical ports on LSWITCH\n\
67 lport-get-parent LPORT get the parent of LPORT if set\n\
68 lport-get-tag LPORT get the LPORT's tag if set\n\
69 lport-set-external-id LPORT KEY [VALUE]\n\
70 set or delete an external-id on LPORT\n\
71 lport-get-external-id LPORT [KEY]\n\
72 list one or all external-ids on LPORT\n\
73 lport-set-macs LPORT [MAC]...\n\
74 set MAC addresses for LPORT.\n\
75 lport-get-macs LPORT get a list of MAC addresses on LPORT\n\
76 lport-get-up LPORT get state of LPORT ('up' or 'down')\n\
79 --db=DATABASE connect to DATABASE\n\
81 -h, --help display this help message\n\
82 -o, --options list available options\n\
83 -V, --version display version information\n\
84 ", program_name, program_name, default_db());
86 stream_usage("database", true, true, false);
89 static const struct nbrec_logical_switch *
90 lswitch_by_name_or_uuid(struct nbctl_context *nb_ctx, const char *id)
92 const struct nbrec_logical_switch *lswitch = NULL;
94 bool duplicate = false;
95 struct uuid lswitch_uuid;
97 if (uuid_from_string(&lswitch_uuid, id)) {
99 lswitch = nbrec_logical_switch_get_for_uuid(nb_ctx->idl,
104 const struct nbrec_logical_switch *iter;
106 NBREC_LOGICAL_SWITCH_FOR_EACH(iter, nb_ctx->idl) {
107 if (strcmp(iter->name, id)) {
111 VLOG_WARN("There is more than one logical switch named '%s'. "
121 if (!lswitch && !duplicate) {
122 VLOG_WARN("lswitch not found for %s: '%s'",
123 is_uuid ? "UUID" : "name", id);
130 do_lswitch_add(struct ovs_cmdl_context *ctx)
132 struct nbctl_context *nb_ctx = ctx->pvt;
133 struct nbrec_logical_switch *lswitch;
135 lswitch = nbrec_logical_switch_insert(nb_ctx->txn);
136 if (ctx->argc == 2) {
137 nbrec_logical_switch_set_name(lswitch, ctx->argv[1]);
142 do_lswitch_del(struct ovs_cmdl_context *ctx)
144 struct nbctl_context *nb_ctx = ctx->pvt;
145 const char *id = ctx->argv[1];
146 const struct nbrec_logical_switch *lswitch;
148 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
153 nbrec_logical_switch_delete(lswitch);
157 do_lswitch_list(struct ovs_cmdl_context *ctx)
159 struct nbctl_context *nb_ctx = ctx->pvt;
160 const struct nbrec_logical_switch *lswitch;
162 NBREC_LOGICAL_SWITCH_FOR_EACH(lswitch, nb_ctx->idl) {
163 printf(UUID_FMT " (%s)\n",
164 UUID_ARGS(&lswitch->header_.uuid), lswitch->name);
169 do_lswitch_set_external_id(struct ovs_cmdl_context *ctx)
171 struct nbctl_context *nb_ctx = ctx->pvt;
172 const char *id = ctx->argv[1];
173 const struct nbrec_logical_switch *lswitch;
174 struct smap new_external_ids;
176 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
181 smap_init(&new_external_ids);
182 smap_clone(&new_external_ids, &lswitch->external_ids);
183 if (ctx->argc == 4) {
184 smap_replace(&new_external_ids, ctx->argv[2], ctx->argv[3]);
186 smap_remove(&new_external_ids, ctx->argv[2]);
188 nbrec_logical_switch_set_external_ids(lswitch, &new_external_ids);
189 smap_destroy(&new_external_ids);
193 do_lswitch_get_external_id(struct ovs_cmdl_context *ctx)
195 struct nbctl_context *nb_ctx = ctx->pvt;
196 const char *id = ctx->argv[1];
197 const struct nbrec_logical_switch *lswitch;
199 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
204 if (ctx->argc == 3) {
205 const char *key = ctx->argv[2];
208 /* List one external ID */
210 value = smap_get(&lswitch->external_ids, key);
212 printf("%s\n", value);
215 struct smap_node *node;
217 /* List all external IDs */
219 SMAP_FOR_EACH(node, &lswitch->external_ids) {
220 printf("%s=%s\n", node->key, node->value);
225 static const struct nbrec_logical_port *
226 lport_by_name_or_uuid(struct nbctl_context *nb_ctx, const char *id)
228 const struct nbrec_logical_port *lport = NULL;
229 bool is_uuid = false;
230 struct uuid lport_uuid;
232 if (uuid_from_string(&lport_uuid, id)) {
234 lport = nbrec_logical_port_get_for_uuid(nb_ctx->idl, &lport_uuid);
238 NBREC_LOGICAL_PORT_FOR_EACH(lport, nb_ctx->idl) {
239 if (!strcmp(lport->name, id)) {
246 VLOG_WARN("lport not found for %s: '%s'",
247 is_uuid ? "UUID" : "name", id);
254 do_lport_add(struct ovs_cmdl_context *ctx)
256 struct nbctl_context *nb_ctx = ctx->pvt;
257 struct nbrec_logical_port *lport;
258 const struct nbrec_logical_switch *lswitch;
261 lswitch = lswitch_by_name_or_uuid(nb_ctx, ctx->argv[1]);
266 if (ctx->argc != 3 && ctx->argc != 5) {
267 /* If a parent_name is specififed, a tag must be specified as well. */
268 VLOG_WARN("Invalid arguments to lport-add.");
272 if (ctx->argc == 5) {
274 if (!ovs_scan(ctx->argv[4], "%"SCNd64, &tag) || tag < 0 || tag > 4095) {
275 VLOG_WARN("Invalid tag '%s'", ctx->argv[4]);
280 /* Finally, create the transaction. */
281 lport = nbrec_logical_port_insert(nb_ctx->txn);
282 nbrec_logical_port_set_name(lport, ctx->argv[2]);
283 nbrec_logical_port_set_lswitch(lport, lswitch);
284 if (ctx->argc == 5) {
285 nbrec_logical_port_set_parent_name(lport, ctx->argv[3]);
286 nbrec_logical_port_set_tag(lport, &tag, 1);
291 do_lport_del(struct ovs_cmdl_context *ctx)
293 struct nbctl_context *nb_ctx = ctx->pvt;
294 const struct nbrec_logical_port *lport;
296 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
301 nbrec_logical_port_delete(lport);
305 is_lswitch(const struct nbrec_logical_switch *lswitch,
306 struct uuid *lswitch_uuid, const char *name)
309 return uuid_equals(lswitch_uuid, &lswitch->header_.uuid);
311 return !strcmp(lswitch->name, name);
317 do_lport_list(struct ovs_cmdl_context *ctx)
319 struct nbctl_context *nb_ctx = ctx->pvt;
320 const char *id = ctx->argv[1];
321 const struct nbrec_logical_port *lport;
322 bool is_uuid = false;
323 struct uuid lswitch_uuid;
325 if (uuid_from_string(&lswitch_uuid, id)) {
329 NBREC_LOGICAL_PORT_FOR_EACH(lport, nb_ctx->idl) {
332 match = is_lswitch(lport->lswitch, &lswitch_uuid, NULL);
334 match = is_lswitch(lport->lswitch, NULL, id);
339 printf(UUID_FMT " (%s)\n",
340 UUID_ARGS(&lport->header_.uuid), lport->name);
345 do_lport_get_parent(struct ovs_cmdl_context *ctx)
347 struct nbctl_context *nb_ctx = ctx->pvt;
348 const struct nbrec_logical_port *lport;
350 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
355 if (lport->parent_name) {
356 printf("%s\n", lport->parent_name);
361 do_lport_get_tag(struct ovs_cmdl_context *ctx)
363 struct nbctl_context *nb_ctx = ctx->pvt;
364 const struct nbrec_logical_port *lport;
366 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
371 if (lport->n_tag > 0) {
372 printf("%"PRId64"\n", lport->tag[0]);
377 do_lport_set_external_id(struct ovs_cmdl_context *ctx)
379 struct nbctl_context *nb_ctx = ctx->pvt;
380 const char *id = ctx->argv[1];
381 const struct nbrec_logical_port *lport;
382 struct smap new_external_ids;
384 lport = lport_by_name_or_uuid(nb_ctx, id);
389 smap_init(&new_external_ids);
390 smap_clone(&new_external_ids, &lport->external_ids);
391 if (ctx->argc == 4) {
392 smap_replace(&new_external_ids, ctx->argv[2], ctx->argv[3]);
394 smap_remove(&new_external_ids, ctx->argv[2]);
396 nbrec_logical_port_set_external_ids(lport, &new_external_ids);
397 smap_destroy(&new_external_ids);
401 do_lport_get_external_id(struct ovs_cmdl_context *ctx)
403 struct nbctl_context *nb_ctx = ctx->pvt;
404 const char *id = ctx->argv[1];
405 const struct nbrec_logical_port *lport;
407 lport = lport_by_name_or_uuid(nb_ctx, id);
412 if (ctx->argc == 3) {
413 const char *key = ctx->argv[2];
416 /* List one external ID */
418 value = smap_get(&lport->external_ids, key);
420 printf("%s\n", value);
423 struct smap_node *node;
425 /* List all external IDs */
427 SMAP_FOR_EACH(node, &lport->external_ids) {
428 printf("%s=%s\n", node->key, node->value);
434 do_lport_set_macs(struct ovs_cmdl_context *ctx)
436 struct nbctl_context *nb_ctx = ctx->pvt;
437 const char *id = ctx->argv[1];
438 const struct nbrec_logical_port *lport;
440 lport = lport_by_name_or_uuid(nb_ctx, id);
445 nbrec_logical_port_set_macs(lport,
446 (const char **) ctx->argv + 2, ctx->argc - 2);
450 do_lport_get_macs(struct ovs_cmdl_context *ctx)
452 struct nbctl_context *nb_ctx = ctx->pvt;
453 const char *id = ctx->argv[1];
454 const struct nbrec_logical_port *lport;
457 lport = lport_by_name_or_uuid(nb_ctx, id);
462 for (i = 0; i < lport->n_macs; i++) {
463 printf("%s\n", lport->macs[i]);
468 do_lport_get_up(struct ovs_cmdl_context *ctx)
470 struct nbctl_context *nb_ctx = ctx->pvt;
471 const char *id = ctx->argv[1];
472 const struct nbrec_logical_port *lport;
474 lport = lport_by_name_or_uuid(nb_ctx, id);
479 printf("%s\n", (lport->up && *lport->up) ? "up" : "down");
483 parse_options(int argc, char *argv[])
488 static const struct option long_options[] = {
489 {"db", required_argument, NULL, 'd'},
490 {"help", no_argument, NULL, 'h'},
491 {"options", no_argument, NULL, 'o'},
492 {"version", no_argument, NULL, 'V'},
494 STREAM_SSL_LONG_OPTIONS,
497 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
502 c = getopt_long(argc, argv, short_options, long_options, NULL);
508 VLOG_OPTION_HANDLERS;
509 STREAM_SSL_OPTION_HANDLERS;
520 ovs_cmdl_print_options(long_options);
524 ovs_print_version(0, 0);
539 static const struct ovs_cmdl_command all_commands[] = {
541 .name = "lswitch-add",
542 .usage = "[LSWITCH]",
545 .handler = do_lswitch_add,
548 .name = "lswitch-del",
552 .handler = do_lswitch_del,
555 .name = "lswitch-list",
559 .handler = do_lswitch_list,
562 .name = "lswitch-set-external-id",
563 .usage = "LSWITCH KEY [VALUE]",
566 .handler = do_lswitch_set_external_id,
569 .name = "lswitch-get-external-id",
570 .usage = "LSWITCH [KEY]",
573 .handler = do_lswitch_get_external_id,
577 .usage = "LSWITCH LPORT [PARENT] [TAG]",
580 .handler = do_lport_add,
587 .handler = do_lport_del,
590 .name = "lport-list",
594 .handler = do_lport_list,
597 .name = "lport-get-parent",
601 .handler = do_lport_get_parent,
604 .name = "lport-get-tag",
608 .handler = do_lport_get_tag,
611 .name = "lport-set-external-id",
612 .usage = "LPORT KEY [VALUE]",
615 .handler = do_lport_set_external_id,
618 .name = "lport-get-external-id",
619 .usage = "LPORT [KEY]",
622 .handler = do_lport_get_external_id,
625 .name = "lport-set-macs",
626 .usage = "LPORT [MAC]...",
628 /* Accept however many arguments the system will allow. */
630 .handler = do_lport_set_macs,
633 .name = "lport-get-macs",
637 .handler = do_lport_get_macs,
640 .name = "lport-get-up",
644 .handler = do_lport_get_up,
653 static const struct ovs_cmdl_command *
654 get_all_commands(void)
664 def = xasprintf("unix:%s/db.sock", ovs_rundir());
670 main(int argc, char *argv[])
672 extern struct vlog_module VLM_reconnect;
673 struct ovs_cmdl_context ctx;
674 struct nbctl_context nb_ctx = { .idl = NULL, };
675 enum ovsdb_idl_txn_status txn_status;
680 fatal_ignore_sigpipe();
681 set_program_name(argv[0]);
682 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
683 vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
684 parse_options(argc, argv);
687 args = process_escape_args(argv);
689 nb_ctx.idl = ovsdb_idl_create(db, &nbrec_idl_class, true, false);
691 ctx.argc = argc - optind;
692 ctx.argv = argv + optind;
694 seqno = ovsdb_idl_get_seqno(nb_ctx.idl);
696 ovsdb_idl_run(nb_ctx.idl);
698 if (!ovsdb_idl_is_alive(nb_ctx.idl)) {
699 int retval = ovsdb_idl_get_last_error(nb_ctx.idl);
700 VLOG_ERR("%s: database connection failed (%s)",
701 db, ovs_retval_to_string(retval));
706 if (seqno != ovsdb_idl_get_seqno(nb_ctx.idl)) {
707 nb_ctx.txn = ovsdb_idl_txn_create(nb_ctx.idl);
708 ovsdb_idl_txn_add_comment(nb_ctx.txn, "ovn-nbctl: %s", args);
709 ovs_cmdl_run_command(&ctx, get_all_commands());
710 txn_status = ovsdb_idl_txn_commit_block(nb_ctx.txn);
711 if (txn_status == TXN_TRY_AGAIN) {
712 ovsdb_idl_txn_destroy(nb_ctx.txn);
720 if (seqno == ovsdb_idl_get_seqno(nb_ctx.idl)) {
721 ovsdb_idl_wait(nb_ctx.idl);
727 ovsdb_idl_txn_destroy(nb_ctx.txn);
729 ovsdb_idl_destroy(nb_ctx.idl);