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\
52 show print overview of database contents\n\
54 Logical switch commands:\n\
55 lswitch-add [LSWITCH] create a logical switch named LSWITCH\n\
56 lswitch-del LSWITCH delete LSWITCH and all its ports\n\
57 lswitch-list print the names of all logical switches\n\
58 lswitch-set-external-id LSWITCH KEY [VALUE]\n\
59 set or delete an external-id on LSWITCH\n\
60 lswitch-get-external-id LSWITCH [KEY]\n\
61 list one or all external-ids on LSWITCH\n\
63 Logical port commands:\n\
64 lport-add LSWITCH LPORT add logical port LPORT on LSWITCH\n\
65 lport-add LSWITCH LPORT PARENT TAG\n\
66 add logical port LPORT on LSWITCH with PARENT\n\
68 lport-del LPORT delete LPORT from its attached switch\n\
69 lport-list LSWITCH print the names of all logical ports on LSWITCH\n\
70 lport-get-parent LPORT get the parent of LPORT if set\n\
71 lport-get-tag LPORT get the LPORT's tag if set\n\
72 lport-set-external-id LPORT KEY [VALUE]\n\
73 set or delete an external-id on LPORT\n\
74 lport-get-external-id LPORT [KEY]\n\
75 list one or all external-ids on LPORT\n\
76 lport-set-macs LPORT [MAC]...\n\
77 set MAC addresses for LPORT.\n\
78 lport-get-macs LPORT get a list of MAC addresses on LPORT\n\
79 lport-set-port-security LPORT [ADDRS]...\n\
80 set port security addresses for LPORT.\n\
81 lport-get-port-security LPORT get LPORT's port security addresses\n\
82 lport-get-up LPORT get state of LPORT ('up' or 'down')\n\
85 --db=DATABASE connect to DATABASE\n\
87 -h, --help display this help message\n\
88 -o, --options list available options\n\
89 -V, --version display version information\n\
90 ", program_name, program_name, default_db());
92 stream_usage("database", true, true, false);
95 static const struct nbrec_logical_switch *
96 lswitch_by_name_or_uuid(struct nbctl_context *nb_ctx, const char *id)
98 const struct nbrec_logical_switch *lswitch = NULL;
100 bool duplicate = false;
101 struct uuid lswitch_uuid;
103 if (uuid_from_string(&lswitch_uuid, id)) {
105 lswitch = nbrec_logical_switch_get_for_uuid(nb_ctx->idl,
110 const struct nbrec_logical_switch *iter;
112 NBREC_LOGICAL_SWITCH_FOR_EACH(iter, nb_ctx->idl) {
113 if (strcmp(iter->name, id)) {
117 VLOG_WARN("There is more than one logical switch named '%s'. "
127 if (!lswitch && !duplicate) {
128 VLOG_WARN("lswitch not found for %s: '%s'",
129 is_uuid ? "UUID" : "name", id);
136 print_lswitch(const struct nbctl_context *nb_ctx,
137 const struct nbrec_logical_switch *lswitch)
139 const struct nbrec_logical_port *lport;
141 printf(" lswitch "UUID_FMT" (%s)\n",
142 UUID_ARGS(&lswitch->header_.uuid), lswitch->name);
144 NBREC_LOGICAL_PORT_FOR_EACH(lport, nb_ctx->idl) {
147 if (lport->lswitch == lswitch) {
148 printf(" lport %s\n", lport->name);
149 if (lport->parent_name && lport->n_tag) {
150 printf(" parent: %s, tag:%"PRIu64"\n",
151 lport->parent_name, lport->tag[0]);
155 for (i=0; i < lport->n_macs; i++) {
156 printf(" %s", lport->macs[i]);
165 do_show(struct ovs_cmdl_context *ctx)
167 struct nbctl_context *nb_ctx = ctx->pvt;
168 const struct nbrec_logical_switch *lswitch;
170 if (ctx->argc == 2) {
171 lswitch = lswitch_by_name_or_uuid(nb_ctx, ctx->argv[1]);
173 print_lswitch(nb_ctx, lswitch);
176 NBREC_LOGICAL_SWITCH_FOR_EACH(lswitch, nb_ctx->idl) {
177 print_lswitch(nb_ctx, lswitch);
183 do_lswitch_add(struct ovs_cmdl_context *ctx)
185 struct nbctl_context *nb_ctx = ctx->pvt;
186 struct nbrec_logical_switch *lswitch;
188 lswitch = nbrec_logical_switch_insert(nb_ctx->txn);
189 if (ctx->argc == 2) {
190 nbrec_logical_switch_set_name(lswitch, ctx->argv[1]);
195 do_lswitch_del(struct ovs_cmdl_context *ctx)
197 struct nbctl_context *nb_ctx = ctx->pvt;
198 const char *id = ctx->argv[1];
199 const struct nbrec_logical_switch *lswitch;
201 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
206 nbrec_logical_switch_delete(lswitch);
210 do_lswitch_list(struct ovs_cmdl_context *ctx)
212 struct nbctl_context *nb_ctx = ctx->pvt;
213 const struct nbrec_logical_switch *lswitch;
215 NBREC_LOGICAL_SWITCH_FOR_EACH(lswitch, nb_ctx->idl) {
216 printf(UUID_FMT " (%s)\n",
217 UUID_ARGS(&lswitch->header_.uuid), lswitch->name);
222 do_lswitch_set_external_id(struct ovs_cmdl_context *ctx)
224 struct nbctl_context *nb_ctx = ctx->pvt;
225 const char *id = ctx->argv[1];
226 const struct nbrec_logical_switch *lswitch;
227 struct smap new_external_ids;
229 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
234 smap_init(&new_external_ids);
235 smap_clone(&new_external_ids, &lswitch->external_ids);
236 if (ctx->argc == 4) {
237 smap_replace(&new_external_ids, ctx->argv[2], ctx->argv[3]);
239 smap_remove(&new_external_ids, ctx->argv[2]);
241 nbrec_logical_switch_set_external_ids(lswitch, &new_external_ids);
242 smap_destroy(&new_external_ids);
246 do_lswitch_get_external_id(struct ovs_cmdl_context *ctx)
248 struct nbctl_context *nb_ctx = ctx->pvt;
249 const char *id = ctx->argv[1];
250 const struct nbrec_logical_switch *lswitch;
252 lswitch = lswitch_by_name_or_uuid(nb_ctx, id);
257 if (ctx->argc == 3) {
258 const char *key = ctx->argv[2];
261 /* List one external ID */
263 value = smap_get(&lswitch->external_ids, key);
265 printf("%s\n", value);
268 struct smap_node *node;
270 /* List all external IDs */
272 SMAP_FOR_EACH(node, &lswitch->external_ids) {
273 printf("%s=%s\n", node->key, node->value);
278 static const struct nbrec_logical_port *
279 lport_by_name_or_uuid(struct nbctl_context *nb_ctx, const char *id)
281 const struct nbrec_logical_port *lport = NULL;
282 bool is_uuid = false;
283 struct uuid lport_uuid;
285 if (uuid_from_string(&lport_uuid, id)) {
287 lport = nbrec_logical_port_get_for_uuid(nb_ctx->idl, &lport_uuid);
291 NBREC_LOGICAL_PORT_FOR_EACH(lport, nb_ctx->idl) {
292 if (!strcmp(lport->name, id)) {
299 VLOG_WARN("lport not found for %s: '%s'",
300 is_uuid ? "UUID" : "name", id);
307 do_lport_add(struct ovs_cmdl_context *ctx)
309 struct nbctl_context *nb_ctx = ctx->pvt;
310 struct nbrec_logical_port *lport;
311 const struct nbrec_logical_switch *lswitch;
314 lswitch = lswitch_by_name_or_uuid(nb_ctx, ctx->argv[1]);
319 if (ctx->argc != 3 && ctx->argc != 5) {
320 /* If a parent_name is specififed, a tag must be specified as well. */
321 VLOG_WARN("Invalid arguments to lport-add.");
325 if (ctx->argc == 5) {
327 if (!ovs_scan(ctx->argv[4], "%"SCNd64, &tag) || tag < 0 || tag > 4095) {
328 VLOG_WARN("Invalid tag '%s'", ctx->argv[4]);
333 /* Finally, create the transaction. */
334 lport = nbrec_logical_port_insert(nb_ctx->txn);
335 nbrec_logical_port_set_name(lport, ctx->argv[2]);
336 nbrec_logical_port_set_lswitch(lport, lswitch);
337 if (ctx->argc == 5) {
338 nbrec_logical_port_set_parent_name(lport, ctx->argv[3]);
339 nbrec_logical_port_set_tag(lport, &tag, 1);
344 do_lport_del(struct ovs_cmdl_context *ctx)
346 struct nbctl_context *nb_ctx = ctx->pvt;
347 const struct nbrec_logical_port *lport;
349 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
354 nbrec_logical_port_delete(lport);
358 is_lswitch(const struct nbrec_logical_switch *lswitch,
359 struct uuid *lswitch_uuid, const char *name)
362 return uuid_equals(lswitch_uuid, &lswitch->header_.uuid);
364 return !strcmp(lswitch->name, name);
370 do_lport_list(struct ovs_cmdl_context *ctx)
372 struct nbctl_context *nb_ctx = ctx->pvt;
373 const char *id = ctx->argv[1];
374 const struct nbrec_logical_port *lport;
375 bool is_uuid = false;
376 struct uuid lswitch_uuid;
378 if (uuid_from_string(&lswitch_uuid, id)) {
382 NBREC_LOGICAL_PORT_FOR_EACH(lport, nb_ctx->idl) {
385 match = is_lswitch(lport->lswitch, &lswitch_uuid, NULL);
387 match = is_lswitch(lport->lswitch, NULL, id);
392 printf(UUID_FMT " (%s)\n",
393 UUID_ARGS(&lport->header_.uuid), lport->name);
398 do_lport_get_parent(struct ovs_cmdl_context *ctx)
400 struct nbctl_context *nb_ctx = ctx->pvt;
401 const struct nbrec_logical_port *lport;
403 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
408 if (lport->parent_name) {
409 printf("%s\n", lport->parent_name);
414 do_lport_get_tag(struct ovs_cmdl_context *ctx)
416 struct nbctl_context *nb_ctx = ctx->pvt;
417 const struct nbrec_logical_port *lport;
419 lport = lport_by_name_or_uuid(nb_ctx, ctx->argv[1]);
424 if (lport->n_tag > 0) {
425 printf("%"PRId64"\n", lport->tag[0]);
430 do_lport_set_external_id(struct ovs_cmdl_context *ctx)
432 struct nbctl_context *nb_ctx = ctx->pvt;
433 const char *id = ctx->argv[1];
434 const struct nbrec_logical_port *lport;
435 struct smap new_external_ids;
437 lport = lport_by_name_or_uuid(nb_ctx, id);
442 smap_init(&new_external_ids);
443 smap_clone(&new_external_ids, &lport->external_ids);
444 if (ctx->argc == 4) {
445 smap_replace(&new_external_ids, ctx->argv[2], ctx->argv[3]);
447 smap_remove(&new_external_ids, ctx->argv[2]);
449 nbrec_logical_port_set_external_ids(lport, &new_external_ids);
450 smap_destroy(&new_external_ids);
454 do_lport_get_external_id(struct ovs_cmdl_context *ctx)
456 struct nbctl_context *nb_ctx = ctx->pvt;
457 const char *id = ctx->argv[1];
458 const struct nbrec_logical_port *lport;
460 lport = lport_by_name_or_uuid(nb_ctx, id);
465 if (ctx->argc == 3) {
466 const char *key = ctx->argv[2];
469 /* List one external ID */
471 value = smap_get(&lport->external_ids, key);
473 printf("%s\n", value);
476 struct smap_node *node;
478 /* List all external IDs */
480 SMAP_FOR_EACH(node, &lport->external_ids) {
481 printf("%s=%s\n", node->key, node->value);
487 do_lport_set_macs(struct ovs_cmdl_context *ctx)
489 struct nbctl_context *nb_ctx = ctx->pvt;
490 const char *id = ctx->argv[1];
491 const struct nbrec_logical_port *lport;
493 lport = lport_by_name_or_uuid(nb_ctx, id);
498 nbrec_logical_port_set_macs(lport,
499 (const char **) ctx->argv + 2, ctx->argc - 2);
503 do_lport_get_macs(struct ovs_cmdl_context *ctx)
505 struct nbctl_context *nb_ctx = ctx->pvt;
506 const char *id = ctx->argv[1];
507 const struct nbrec_logical_port *lport;
510 lport = lport_by_name_or_uuid(nb_ctx, id);
515 for (i = 0; i < lport->n_macs; i++) {
516 printf("%s\n", lport->macs[i]);
521 do_lport_set_port_security(struct ovs_cmdl_context *ctx)
523 struct nbctl_context *nb_ctx = ctx->pvt;
524 const char *id = ctx->argv[1];
525 const struct nbrec_logical_port *lport;
527 lport = lport_by_name_or_uuid(nb_ctx, id);
532 nbrec_logical_port_set_port_security(lport,
533 (const char **) ctx->argv + 2, ctx->argc - 2);
537 do_lport_get_port_security(struct ovs_cmdl_context *ctx)
539 struct nbctl_context *nb_ctx = ctx->pvt;
540 const char *id = ctx->argv[1];
541 const struct nbrec_logical_port *lport;
544 lport = lport_by_name_or_uuid(nb_ctx, id);
549 for (i = 0; i < lport->n_port_security; i++) {
550 printf("%s\n", lport->port_security[i]);
555 do_lport_get_up(struct ovs_cmdl_context *ctx)
557 struct nbctl_context *nb_ctx = ctx->pvt;
558 const char *id = ctx->argv[1];
559 const struct nbrec_logical_port *lport;
561 lport = lport_by_name_or_uuid(nb_ctx, id);
566 printf("%s\n", (lport->up && *lport->up) ? "up" : "down");
570 parse_options(int argc, char *argv[])
575 static const struct option long_options[] = {
576 {"db", required_argument, NULL, 'd'},
577 {"help", no_argument, NULL, 'h'},
578 {"options", no_argument, NULL, 'o'},
579 {"version", no_argument, NULL, 'V'},
581 STREAM_SSL_LONG_OPTIONS,
584 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
589 c = getopt_long(argc, argv, short_options, long_options, NULL);
595 VLOG_OPTION_HANDLERS;
596 STREAM_SSL_OPTION_HANDLERS;
607 ovs_cmdl_print_options(long_options);
611 ovs_print_version(0, 0);
626 static const struct ovs_cmdl_command all_commands[] = {
629 .usage = "[LSWITCH]",
635 .name = "lswitch-add",
636 .usage = "[LSWITCH]",
639 .handler = do_lswitch_add,
642 .name = "lswitch-del",
646 .handler = do_lswitch_del,
649 .name = "lswitch-list",
653 .handler = do_lswitch_list,
656 .name = "lswitch-set-external-id",
657 .usage = "LSWITCH KEY [VALUE]",
660 .handler = do_lswitch_set_external_id,
663 .name = "lswitch-get-external-id",
664 .usage = "LSWITCH [KEY]",
667 .handler = do_lswitch_get_external_id,
671 .usage = "LSWITCH LPORT [PARENT] [TAG]",
674 .handler = do_lport_add,
681 .handler = do_lport_del,
684 .name = "lport-list",
688 .handler = do_lport_list,
691 .name = "lport-get-parent",
695 .handler = do_lport_get_parent,
698 .name = "lport-get-tag",
702 .handler = do_lport_get_tag,
705 .name = "lport-set-external-id",
706 .usage = "LPORT KEY [VALUE]",
709 .handler = do_lport_set_external_id,
712 .name = "lport-get-external-id",
713 .usage = "LPORT [KEY]",
716 .handler = do_lport_get_external_id,
719 .name = "lport-set-macs",
720 .usage = "LPORT [MAC]...",
722 /* Accept however many arguments the system will allow. */
724 .handler = do_lport_set_macs,
727 .name = "lport-get-macs",
731 .handler = do_lport_get_macs,
734 .name = "lport-set-port-security",
735 .usage = "LPORT [ADDRS]...",
737 /* Accept however many arguments the system will allow. */
739 .handler = do_lport_set_port_security,
742 .name = "lport-get-port-security",
746 .handler = do_lport_get_port_security,
749 .name = "lport-get-up",
753 .handler = do_lport_get_up,
762 static const struct ovs_cmdl_command *
763 get_all_commands(void)
773 def = xasprintf("unix:%s/db.sock", ovs_rundir());
779 main(int argc, char *argv[])
781 extern struct vlog_module VLM_reconnect;
782 struct ovs_cmdl_context ctx;
783 struct nbctl_context nb_ctx = { .idl = NULL, };
784 enum ovsdb_idl_txn_status txn_status;
789 fatal_ignore_sigpipe();
790 set_program_name(argv[0]);
791 vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
792 vlog_set_levels(&VLM_reconnect, VLF_ANY_DESTINATION, VLL_WARN);
793 parse_options(argc, argv);
796 args = process_escape_args(argv);
798 nb_ctx.idl = ovsdb_idl_create(db, &nbrec_idl_class, true, false);
800 ctx.argc = argc - optind;
801 ctx.argv = argv + optind;
803 seqno = ovsdb_idl_get_seqno(nb_ctx.idl);
805 ovsdb_idl_run(nb_ctx.idl);
807 if (!ovsdb_idl_is_alive(nb_ctx.idl)) {
808 int retval = ovsdb_idl_get_last_error(nb_ctx.idl);
809 VLOG_ERR("%s: database connection failed (%s)",
810 db, ovs_retval_to_string(retval));
815 if (seqno != ovsdb_idl_get_seqno(nb_ctx.idl)) {
816 nb_ctx.txn = ovsdb_idl_txn_create(nb_ctx.idl);
817 ovsdb_idl_txn_add_comment(nb_ctx.txn, "ovn-nbctl: %s", args);
818 ovs_cmdl_run_command(&ctx, get_all_commands());
819 txn_status = ovsdb_idl_txn_commit_block(nb_ctx.txn);
820 if (txn_status == TXN_TRY_AGAIN) {
821 ovsdb_idl_txn_destroy(nb_ctx.txn);
829 if (seqno == ovsdb_idl_get_seqno(nb_ctx.idl)) {
830 ovsdb_idl_wait(nb_ctx.idl);
836 ovsdb_idl_txn_destroy(nb_ctx.txn);
838 ovsdb_idl_destroy(nb_ctx.idl);