util: New function nullable_xstrdup().
[cascardo/ovs.git] / ovn / utilities / ovn-nbctl.c
1 /*
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:
5  *
6  *     http://www.apache.org/licenses/LICENSE-2.0
7  *
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.
13  */
14
15 #include <config.h>
16
17 #include <getopt.h>
18 #include <inttypes.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "command-line.h"
23 #include "db-ctl-base.h"
24 #include "dirs.h"
25 #include "fatal-signal.h"
26 #include "json.h"
27 #include "ovn/lib/ovn-nb-idl.h"
28 #include "packets.h"
29 #include "poll-loop.h"
30 #include "process.h"
31 #include "smap.h"
32 #include "stream.h"
33 #include "stream-ssl.h"
34 #include "svec.h"
35 #include "table.h"
36 #include "timeval.h"
37 #include "util.h"
38 #include "openvswitch/vlog.h"
39
40 VLOG_DEFINE_THIS_MODULE(nbctl);
41
42 /* --db: The database server to contact. */
43 static const char *db;
44
45 /* --oneline: Write each command's output as a single line? */
46 static bool oneline;
47
48 /* --dry-run: Do not commit any changes. */
49 static bool dry_run;
50
51 /* --timeout: Time to wait for a connection to 'db'. */
52 static int timeout;
53
54 /* Format for table output. */
55 static struct table_style table_style = TABLE_STYLE_DEFAULT;
56
57 /* The IDL we're using and the current transaction, if any.
58  * This is for use by nbctl_exit() only, to allow it to clean up.
59  * Other code should use its context arguments. */
60 static struct ovsdb_idl *the_idl;
61 static struct ovsdb_idl_txn *the_idl_txn;
62 OVS_NO_RETURN static void nbctl_exit(int status);
63
64 static void nbctl_cmd_init(void);
65 OVS_NO_RETURN static void usage(void);
66 static void parse_options(int argc, char *argv[], struct shash *local_options);
67 static const char *nbctl_default_db(void);
68 static void run_prerequisites(struct ctl_command[], size_t n_commands,
69                               struct ovsdb_idl *);
70 static bool do_nbctl(const char *args, struct ctl_command *, size_t n,
71                      struct ovsdb_idl *);
72
73 int
74 main(int argc, char *argv[])
75 {
76     struct ovsdb_idl *idl;
77     struct ctl_command *commands;
78     struct shash local_options;
79     unsigned int seqno;
80     size_t n_commands;
81     char *args;
82
83     set_program_name(argv[0]);
84     fatal_ignore_sigpipe();
85     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
86     vlog_set_levels_from_string_assert("reconnect:warn");
87     nbrec_init();
88
89     nbctl_cmd_init();
90
91     /* Log our arguments.  This is often valuable for debugging systems. */
92     args = process_escape_args(argv);
93     VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG,
94          "Called as %s", args);
95
96     /* Parse command line. */
97     shash_init(&local_options);
98     parse_options(argc, argv, &local_options);
99     commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
100                                   &n_commands);
101
102     if (timeout) {
103         time_alarm(timeout);
104     }
105
106     /* Initialize IDL. */
107     idl = the_idl = ovsdb_idl_create(db, &nbrec_idl_class, true, false);
108     run_prerequisites(commands, n_commands, idl);
109
110     /* Execute the commands.
111      *
112      * 'seqno' is the database sequence number for which we last tried to
113      * execute our transaction.  There's no point in trying to commit more than
114      * once for any given sequence number, because if the transaction fails
115      * it's because the database changed and we need to obtain an up-to-date
116      * view of the database before we try the transaction again. */
117     seqno = ovsdb_idl_get_seqno(idl);
118     for (;;) {
119         ovsdb_idl_run(idl);
120         if (!ovsdb_idl_is_alive(idl)) {
121             int retval = ovsdb_idl_get_last_error(idl);
122             ctl_fatal("%s: database connection failed (%s)",
123                         db, ovs_retval_to_string(retval));
124         }
125
126         if (seqno != ovsdb_idl_get_seqno(idl)) {
127             seqno = ovsdb_idl_get_seqno(idl);
128             if (do_nbctl(args, commands, n_commands, idl)) {
129                 free(args);
130                 exit(EXIT_SUCCESS);
131             }
132         }
133
134         if (seqno == ovsdb_idl_get_seqno(idl)) {
135             ovsdb_idl_wait(idl);
136             poll_block();
137         }
138     }
139 }
140
141 static const char *
142 nbctl_default_db(void)
143 {
144     static char *def;
145     if (!def) {
146         def = getenv("OVN_NB_DB");
147         if (!def) {
148             def = xasprintf("unix:%s/ovnnb_db.sock", ovs_rundir());
149         }
150     }
151     return def;
152 }
153
154 static void
155 parse_options(int argc, char *argv[], struct shash *local_options)
156 {
157     enum {
158         OPT_DB = UCHAR_MAX + 1,
159         OPT_NO_SYSLOG,
160         OPT_DRY_RUN,
161         OPT_ONELINE,
162         OPT_LOCAL,
163         OPT_COMMANDS,
164         OPT_OPTIONS,
165         VLOG_OPTION_ENUMS,
166         TABLE_OPTION_ENUMS
167     };
168     static const struct option global_long_options[] = {
169         {"db", required_argument, NULL, OPT_DB},
170         {"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
171         {"dry-run", no_argument, NULL, OPT_DRY_RUN},
172         {"oneline", no_argument, NULL, OPT_ONELINE},
173         {"timeout", required_argument, NULL, 't'},
174         {"help", no_argument, NULL, 'h'},
175         {"commands", no_argument, NULL, OPT_COMMANDS},
176         {"options", no_argument, NULL, OPT_OPTIONS},
177         {"version", no_argument, NULL, 'V'},
178         VLOG_LONG_OPTIONS,
179         STREAM_SSL_LONG_OPTIONS,
180         TABLE_LONG_OPTIONS,
181         {NULL, 0, NULL, 0},
182     };
183     const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
184     char *tmp, *short_options;
185
186     struct option *options;
187     size_t allocated_options;
188     size_t n_options;
189     size_t i;
190
191     tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
192     short_options = xasprintf("+%s", tmp);
193     free(tmp);
194
195     /* We want to parse both global and command-specific options here, but
196      * getopt_long() isn't too convenient for the job.  We copy our global
197      * options into a dynamic array, then append all of the command-specific
198      * options. */
199     options = xmemdup(global_long_options, sizeof global_long_options);
200     allocated_options = ARRAY_SIZE(global_long_options);
201     n_options = n_global_long_options;
202     ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
203     table_style.format = TF_LIST;
204
205     for (;;) {
206         int idx;
207         int c;
208
209         c = getopt_long(argc, argv, short_options, options, &idx);
210         if (c == -1) {
211             break;
212         }
213
214         switch (c) {
215         case OPT_DB:
216             db = optarg;
217             break;
218
219         case OPT_ONELINE:
220             oneline = true;
221             break;
222
223         case OPT_NO_SYSLOG:
224             vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
225             break;
226
227         case OPT_DRY_RUN:
228             dry_run = true;
229             break;
230
231         case OPT_LOCAL:
232             if (shash_find(local_options, options[idx].name)) {
233                 ctl_fatal("'%s' option specified multiple times",
234                             options[idx].name);
235             }
236             shash_add_nocopy(local_options,
237                              xasprintf("--%s", options[idx].name),
238                              nullable_xstrdup(optarg));
239             break;
240
241         case 'h':
242             usage();
243             exit(EXIT_SUCCESS);
244
245         case OPT_COMMANDS:
246             ctl_print_commands();
247
248         case OPT_OPTIONS:
249             ctl_print_options(global_long_options);
250
251         case 'V':
252             ovs_print_version(0, 0);
253             printf("DB Schema %s\n", nbrec_get_db_version());
254             exit(EXIT_SUCCESS);
255
256         case 't':
257             timeout = strtoul(optarg, NULL, 10);
258             if (timeout < 0) {
259                 ctl_fatal("value %s on -t or --timeout is invalid", optarg);
260             }
261             break;
262
263         VLOG_OPTION_HANDLERS
264         TABLE_OPTION_HANDLERS(&table_style)
265         STREAM_SSL_OPTION_HANDLERS
266
267         case '?':
268             exit(EXIT_FAILURE);
269
270         default:
271             abort();
272         }
273     }
274     free(short_options);
275
276     if (!db) {
277         db = nbctl_default_db();
278     }
279
280     for (i = n_global_long_options; options[i].name; i++) {
281         free(CONST_CAST(char *, options[i].name));
282     }
283     free(options);
284 }
285
286 static void
287 usage(void)
288 {
289     printf("\
290 %s: OVN northbound DB management utility\n\
291 usage: %s [OPTIONS] COMMAND [ARG...]\n\
292 \n\
293 General commands:\n\
294   show                      print overview of database contents\n\
295   show SWITCH               print overview of database contents for SWITCH\n\
296   show ROUTER               print overview of database contents for ROUTER\n\
297 \n\
298 Logical switch commands:\n\
299   ls-add [SWITCH]           create a logical switch named SWITCH\n\
300   ls-del SWITCH             delete SWITCH and all its ports\n\
301   ls-list                   print the names of all logical switches\n\
302 \n\
303 ACL commands:\n\
304   acl-add SWITCH DIRECTION PRIORITY MATCH ACTION [log]\n\
305                             add an ACL to SWITCH\n\
306   acl-del SWITCH [DIRECTION [PRIORITY MATCH]]\n\
307                             remove ACLs from SWITCH\n\
308   acl-list SWITCH           print ACLs for SWITCH\n\
309 \n\
310 Logical switch port commands:\n\
311   lsp-add SWITCH PORT       add logical port PORT on SWITCH\n\
312   lsp-add SWITCH PORT PARENT TAG\n\
313                             add logical port PORT on SWITCH with PARENT\n\
314                             on TAG\n\
315   lsp-del PORT              delete PORT from its attached switch\n\
316   lsp-list SWITCH           print the names of all logical ports on SWITCH\n\
317   lsp-get-parent PORT       get the parent of PORT if set\n\
318   lsp-get-tag PORT          get the PORT's tag if set\n\
319   lsp-set-addresses PORT [ADDRESS]...\n\
320                             set MAC or MAC+IP addresses for PORT.\n\
321   lsp-get-addresses PORT    get a list of MAC addresses on PORT\n\
322   lsp-set-port-security PORT [ADDRS]...\n\
323                             set port security addresses for PORT.\n\
324   lsp-get-port-security PORT    get PORT's port security addresses\n\
325   lsp-get-up PORT           get state of PORT ('up' or 'down')\n\
326   lsp-set-enabled PORT STATE\n\
327                             set administrative state PORT\n\
328                             ('enabled' or 'disabled')\n\
329   lsp-get-enabled PORT      get administrative state PORT\n\
330                             ('enabled' or 'disabled')\n\
331   lsp-set-type PORT TYPE    set the type for PORT\n\
332   lsp-get-type PORT         get the type for PORT\n\
333   lsp-set-options PORT KEY=VALUE [KEY=VALUE]...\n\
334                             set options related to the type of PORT\n\
335   lsp-get-options PORT      get the type specific options for PORT\n\
336 \n\
337 Logical router commands:\n\
338   lr-add [ROUTER]           create a logical router named ROUTER\n\
339   lr-del ROUTER             delete ROUTER and all its ports\n\
340   lr-list                   print the names of all logical routers\n\
341 \n\
342 Logical router port commands:\n\
343   lrp-add ROUTER PORT MAC NETWORK [PEER]\n\
344                             add logical port PORT on ROUTER\n\
345   lrp-del PORT              delete PORT from its attached router\n\
346   lrp-list ROUTER           print the names of all ports on ROUTER\n\
347   lrp-set-enabled PORT STATE\n\
348                             set administrative state PORT\n\
349                             ('enabled' or 'disabled')\n\
350   lrp-get-enabled PORT      get administrative state PORT\n\
351                             ('enabled' or 'disabled')\n\
352 \n\
353 Route commands:\n\
354   lr-route-add ROUTER PREFIX NEXTHOP [PORT]\n\
355                             add a route to ROUTER\n\
356   lr-route-del ROUTER [PREFIX]\n\
357                             remove routes from ROUTER\n\
358   lr-route-list ROUTER      print routes for ROUTER\n\
359 \n\
360 %s\
361 \n\
362 Options:\n\
363   --db=DATABASE               connect to DATABASE\n\
364                               (default: %s)\n\
365   -t, --timeout=SECS          wait at most SECS seconds\n\
366   --dry-run                   do not commit changes to database\n\
367   --oneline                   print exactly one line of output per command\n",
368            program_name, program_name, ctl_get_db_cmd_usage(), nbctl_default_db());
369     vlog_usage();
370     printf("\
371   --no-syslog             equivalent to --verbose=nbctl:syslog:warn\n");
372     printf("\n\
373 Other options:\n\
374   -h, --help                  display this help message\n\
375   -V, --version               display version information\n");
376     exit(EXIT_SUCCESS);
377 }
378 \f
379
380 /* Find a logical router given its id. */
381 static const struct nbrec_logical_router *
382 lr_by_name_or_uuid(struct ctl_context *ctx, const char *id,
383                         bool must_exist)
384 {
385     const struct nbrec_logical_router *lr = NULL;
386     bool is_uuid = false;
387     struct uuid lr_uuid;
388
389     if (uuid_from_string(&lr_uuid, id)) {
390         is_uuid = true;
391         lr = nbrec_logical_router_get_for_uuid(ctx->idl, &lr_uuid);
392     }
393
394     if (!lr) {
395         const struct nbrec_logical_router *iter;
396
397         NBREC_LOGICAL_ROUTER_FOR_EACH(iter, ctx->idl) {
398             if (strcmp(iter->name, id)) {
399                 continue;
400             }
401             if (lr) {
402                 ctl_fatal("Multiple logical routers named '%s'.  "
403                           "Use a UUID.", id);
404             }
405             lr = iter;
406         }
407     }
408
409     if (!lr && must_exist) {
410         ctl_fatal("%s: router %s not found", id, is_uuid ? "UUID" : "name");
411     }
412
413     return lr;
414 }
415
416 static const struct nbrec_logical_switch *
417 ls_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
418 {
419     const struct nbrec_logical_switch *ls = NULL;
420
421     struct uuid ls_uuid;
422     bool is_uuid = uuid_from_string(&ls_uuid, id);
423     if (is_uuid) {
424         ls = nbrec_logical_switch_get_for_uuid(ctx->idl, &ls_uuid);
425     }
426
427     if (!ls) {
428         const struct nbrec_logical_switch *iter;
429
430         NBREC_LOGICAL_SWITCH_FOR_EACH(iter, ctx->idl) {
431             if (strcmp(iter->name, id)) {
432                 continue;
433             }
434             if (ls) {
435                 ctl_fatal("Multiple logical switches named '%s'.  "
436                           "Use a UUID.", id);
437             }
438             ls = iter;
439         }
440     }
441
442     if (!ls && must_exist) {
443         ctl_fatal("%s: switch %s not found", id, is_uuid ? "UUID" : "name");
444     }
445
446     return ls;
447 }
448
449 /* Given pointer to logical router, this routine prints the router
450  * information.  */
451 static void
452 print_lr(const struct nbrec_logical_router *lr, struct ds *s)
453 {
454     ds_put_format(s, "    router "UUID_FMT" (%s)\n",
455                   UUID_ARGS(&lr->header_.uuid), lr->name);
456
457     for (size_t i = 0; i < lr->n_ports; i++) {
458         const struct nbrec_logical_router_port *lrp = lr->ports[i];
459         ds_put_format(s, "        port %s\n", lrp->name);
460         if (lrp->mac) {
461             ds_put_cstr(s, "            mac: ");
462             ds_put_format(s, "\"%s\"", lrp->mac);
463         }
464         ds_put_format(s, "\n");
465     }
466 }
467
468 static void
469 print_ls(const struct nbrec_logical_switch *ls, struct ds *s)
470 {
471     ds_put_format(s, "    switch "UUID_FMT" (%s)\n",
472                   UUID_ARGS(&ls->header_.uuid), ls->name);
473
474     for (size_t i = 0; i < ls->n_ports; i++) {
475         const struct nbrec_logical_switch_port *lsp = ls->ports[i];
476
477         ds_put_format(s, "        port %s\n", lsp->name);
478         if (lsp->parent_name) {
479             ds_put_format(s, "            parent: %s\n", lsp->parent_name);
480         }
481         if (lsp->n_tag) {
482             ds_put_format(s, "            tag: %"PRIu64"\n", lsp->tag[0]);
483         }
484         if (lsp->n_addresses) {
485             ds_put_cstr(s, "            addresses: [");
486             for (size_t j = 0; j < lsp->n_addresses; j++) {
487                 ds_put_format(s, "%s\"%s\"",
488                         j == 0 ? "" : ", ",
489                         lsp->addresses[j]);
490             }
491             ds_put_cstr(s, "]\n");
492         }
493     }
494 }
495
496 static void
497 nbctl_show(struct ctl_context *ctx)
498 {
499     const struct nbrec_logical_switch *ls;
500
501     if (ctx->argc == 2) {
502         ls = ls_by_name_or_uuid(ctx, ctx->argv[1], false);
503         if (ls) {
504             print_ls(ls, &ctx->output);
505         }
506     } else {
507         NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
508             print_ls(ls, &ctx->output);
509         }
510     }
511     const struct nbrec_logical_router *lr;
512
513     if (ctx->argc == 2) {
514         lr = lr_by_name_or_uuid(ctx, ctx->argv[1], false);
515         if (lr) {
516             print_lr(lr, &ctx->output);
517         }
518     } else {
519         NBREC_LOGICAL_ROUTER_FOR_EACH(lr, ctx->idl) {
520             print_lr(lr, &ctx->output);
521         }
522     }
523 }
524
525 static void
526 nbctl_ls_add(struct ctl_context *ctx)
527 {
528     const char *ls_name = ctx->argc == 2 ? ctx->argv[1] : NULL;
529
530     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
531     bool add_duplicate = shash_find(&ctx->options, "--add-duplicate") != NULL;
532     if (may_exist && add_duplicate) {
533         ctl_fatal("--may-exist and --add-duplicate may not be used together");
534     }
535
536     if (ls_name) {
537         if (!add_duplicate) {
538             const struct nbrec_logical_switch *ls;
539             NBREC_LOGICAL_SWITCH_FOR_EACH (ls, ctx->idl) {
540                 if (!strcmp(ls->name, ls_name)) {
541                     if (may_exist) {
542                         return;
543                     }
544                     ctl_fatal("%s: a switch with this name already exists",
545                               ls_name);
546                 }
547             }
548         }
549     } else if (may_exist) {
550         ctl_fatal("--may-exist requires specifying a name");
551     } else if (add_duplicate) {
552         ctl_fatal("--add-duplicate requires specifying a name");
553     }
554
555     struct nbrec_logical_switch *ls;
556     ls = nbrec_logical_switch_insert(ctx->txn);
557     if (ls_name) {
558         nbrec_logical_switch_set_name(ls, ls_name);
559     }
560 }
561
562 static void
563 nbctl_ls_del(struct ctl_context *ctx)
564 {
565     bool must_exist = !shash_find(&ctx->options, "--if-exists");
566     const char *id = ctx->argv[1];
567     const struct nbrec_logical_switch *ls;
568
569     ls = ls_by_name_or_uuid(ctx, id, must_exist);
570     if (!ls) {
571         return;
572     }
573
574     nbrec_logical_switch_delete(ls);
575 }
576
577 static void
578 nbctl_ls_list(struct ctl_context *ctx)
579 {
580     const struct nbrec_logical_switch *ls;
581     struct smap lswitches;
582
583     smap_init(&lswitches);
584     NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
585         smap_add_format(&lswitches, ls->name, UUID_FMT " (%s)",
586                         UUID_ARGS(&ls->header_.uuid), ls->name);
587     }
588     const struct smap_node **nodes = smap_sort(&lswitches);
589     for (size_t i = 0; i < smap_count(&lswitches); i++) {
590         const struct smap_node *node = nodes[i];
591         ds_put_format(&ctx->output, "%s\n", node->value);
592     }
593     smap_destroy(&lswitches);
594     free(nodes);
595 }
596 \f
597 static const struct nbrec_logical_switch_port *
598 lsp_by_name_or_uuid(struct ctl_context *ctx, const char *id,
599                     bool must_exist)
600 {
601     const struct nbrec_logical_switch_port *lsp = NULL;
602
603     struct uuid lsp_uuid;
604     bool is_uuid = uuid_from_string(&lsp_uuid, id);
605     if (is_uuid) {
606         lsp = nbrec_logical_switch_port_get_for_uuid(ctx->idl, &lsp_uuid);
607     }
608
609     if (!lsp) {
610         NBREC_LOGICAL_SWITCH_PORT_FOR_EACH(lsp, ctx->idl) {
611             if (!strcmp(lsp->name, id)) {
612                 break;
613             }
614         }
615     }
616
617     if (!lsp && must_exist) {
618         ctl_fatal("%s: port %s not found", id, is_uuid ? "UUID" : "name");
619     }
620
621     return lsp;
622 }
623
624 /* Returns the logical switch that contains 'lsp'. */
625 static const struct nbrec_logical_switch *
626 lsp_to_ls(const struct ovsdb_idl *idl,
627                const struct nbrec_logical_switch_port *lsp)
628 {
629     const struct nbrec_logical_switch *ls;
630     NBREC_LOGICAL_SWITCH_FOR_EACH (ls, idl) {
631         for (size_t i = 0; i < ls->n_ports; i++) {
632             if (ls->ports[i] == lsp) {
633                 return ls;
634             }
635         }
636     }
637
638     /* Can't happen because of the database schema */
639     ctl_fatal("logical port %s is not part of any logical switch",
640               lsp->name);
641 }
642
643 static const char *
644 ls_get_name(const struct nbrec_logical_switch *ls,
645                  char uuid_s[UUID_LEN + 1], size_t uuid_s_size)
646 {
647     if (ls->name[0]) {
648         return ls->name;
649     }
650     snprintf(uuid_s, uuid_s_size, UUID_FMT, UUID_ARGS(&ls->header_.uuid));
651     return uuid_s;
652 }
653
654 static void
655 nbctl_lsp_add(struct ctl_context *ctx)
656 {
657     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
658
659     const struct nbrec_logical_switch *ls;
660     ls = ls_by_name_or_uuid(ctx, ctx->argv[1], true);
661
662     const char *parent_name;
663     int64_t tag;
664     if (ctx->argc == 3) {
665         parent_name = NULL;
666         tag = -1;
667     } else if (ctx->argc == 5) {
668         /* Validate tag. */
669         parent_name = ctx->argv[3];
670         if (!ovs_scan(ctx->argv[4], "%"SCNd64, &tag)
671             || tag < 0 || tag > 4095) {
672             ctl_fatal("%s: invalid tag", ctx->argv[4]);
673         }
674     } else {
675         ctl_fatal("lsp-add with parent must also specify a tag");
676     }
677
678     const char *lsp_name = ctx->argv[2];
679     const struct nbrec_logical_switch_port *lsp;
680     lsp = lsp_by_name_or_uuid(ctx, lsp_name, false);
681     if (lsp) {
682         if (!may_exist) {
683             ctl_fatal("%s: a port with this name already exists",
684                       lsp_name);
685         }
686
687         const struct nbrec_logical_switch *lsw;
688         lsw = lsp_to_ls(ctx->idl, lsp);
689         if (lsw != ls) {
690             char uuid_s[UUID_LEN + 1];
691             ctl_fatal("%s: port already exists but in switch %s", lsp_name,
692                       ls_get_name(lsw, uuid_s, sizeof uuid_s));
693         }
694
695         if (parent_name) {
696             if (!lsp->parent_name) {
697                 ctl_fatal("%s: port already exists but has no parent",
698                           lsp_name);
699             } else if (strcmp(parent_name, lsp->parent_name)) {
700                 ctl_fatal("%s: port already exists with different parent %s",
701                           lsp_name, lsp->parent_name);
702             }
703
704             if (!lsp->n_tag) {
705                 ctl_fatal("%s: port already exists but has no tag",
706                           lsp_name);
707             } else if (lsp->tag[0] != tag) {
708                 ctl_fatal("%s: port already exists with different "
709                           "tag %"PRId64, lsp_name, lsp->tag[0]);
710             }
711         } else {
712             if (lsp->parent_name) {
713                 ctl_fatal("%s: port already exists but has parent %s",
714                           lsp_name, lsp->parent_name);
715             }
716         }
717
718         return;
719     }
720
721     /* Create the logical port. */
722     lsp = nbrec_logical_switch_port_insert(ctx->txn);
723     nbrec_logical_switch_port_set_name(lsp, lsp_name);
724     if (tag >= 0) {
725         nbrec_logical_switch_port_set_parent_name(lsp, parent_name);
726         nbrec_logical_switch_port_set_tag(lsp, &tag, 1);
727     }
728
729     /* Insert the logical port into the logical switch. */
730     nbrec_logical_switch_verify_ports(ls);
731     struct nbrec_logical_switch_port **new_ports = xmalloc(sizeof *new_ports *
732                                                     (ls->n_ports + 1));
733     memcpy(new_ports, ls->ports, sizeof *new_ports * ls->n_ports);
734     new_ports[ls->n_ports] = CONST_CAST(struct nbrec_logical_switch_port *,
735                                              lsp);
736     nbrec_logical_switch_set_ports(ls, new_ports, ls->n_ports + 1);
737     free(new_ports);
738 }
739
740 /* Removes logical switch port 'ls->ports[idx]'. */
741 static void
742 remove_lsp(const struct nbrec_logical_switch *ls, size_t idx)
743 {
744     const struct nbrec_logical_switch_port *lsp = ls->ports[idx];
745
746     /* First remove 'lsp' from the array of ports.  This is what will
747      * actually cause the logical port to be deleted when the transaction is
748      * sent to the database server (due to garbage collection). */
749     struct nbrec_logical_switch_port **new_ports
750         = xmemdup(ls->ports, sizeof *new_ports * ls->n_ports);
751     new_ports[idx] = new_ports[ls->n_ports - 1];
752     nbrec_logical_switch_verify_ports(ls);
753     nbrec_logical_switch_set_ports(ls, new_ports, ls->n_ports - 1);
754     free(new_ports);
755
756     /* Delete 'lsp' from the IDL.  This won't have a real effect on the
757      * database server (the IDL will suppress it in fact) but it means that it
758      * won't show up when we iterate with NBREC_LOGICAL_SWITCH_PORT_FOR_EACH
759      * later. */
760     nbrec_logical_switch_port_delete(lsp);
761 }
762
763 static void
764 nbctl_lsp_del(struct ctl_context *ctx)
765 {
766     bool must_exist = !shash_find(&ctx->options, "--if-exists");
767     const struct nbrec_logical_switch_port *lsp;
768
769     lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], must_exist);
770     if (!lsp) {
771         return;
772     }
773
774     /* Find the switch that contains 'lsp', then delete it. */
775     const struct nbrec_logical_switch *ls;
776     NBREC_LOGICAL_SWITCH_FOR_EACH (ls, ctx->idl) {
777         for (size_t i = 0; i < ls->n_ports; i++) {
778             if (ls->ports[i] == lsp) {
779                 remove_lsp(ls, i);
780                 return;
781             }
782         }
783     }
784
785     /* Can't happen because of the database schema. */
786     ctl_fatal("logical port %s is not part of any logical switch",
787               ctx->argv[1]);
788 }
789
790 static void
791 nbctl_lsp_list(struct ctl_context *ctx)
792 {
793     const char *id = ctx->argv[1];
794     const struct nbrec_logical_switch *ls;
795     struct smap lsps;
796     size_t i;
797
798     ls = ls_by_name_or_uuid(ctx, id, true);
799
800     smap_init(&lsps);
801     for (i = 0; i < ls->n_ports; i++) {
802         const struct nbrec_logical_switch_port *lsp = ls->ports[i];
803         smap_add_format(&lsps, lsp->name, UUID_FMT " (%s)",
804                         UUID_ARGS(&lsp->header_.uuid), lsp->name);
805     }
806     const struct smap_node **nodes = smap_sort(&lsps);
807     for (i = 0; i < smap_count(&lsps); i++) {
808         const struct smap_node *node = nodes[i];
809         ds_put_format(&ctx->output, "%s\n", node->value);
810     }
811     smap_destroy(&lsps);
812     free(nodes);
813 }
814
815 static void
816 nbctl_lsp_get_parent(struct ctl_context *ctx)
817 {
818     const struct nbrec_logical_switch_port *lsp;
819
820     lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], true);
821     if (lsp->parent_name) {
822         ds_put_format(&ctx->output, "%s\n", lsp->parent_name);
823     }
824 }
825
826 static void
827 nbctl_lsp_get_tag(struct ctl_context *ctx)
828 {
829     const struct nbrec_logical_switch_port *lsp;
830
831     lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], true);
832     if (lsp->n_tag > 0) {
833         ds_put_format(&ctx->output, "%"PRId64"\n", lsp->tag[0]);
834     }
835 }
836
837 static void
838 nbctl_lsp_set_addresses(struct ctl_context *ctx)
839 {
840     const char *id = ctx->argv[1];
841     const struct nbrec_logical_switch_port *lsp;
842
843     lsp = lsp_by_name_or_uuid(ctx, id, true);
844
845     int i;
846     for (i = 2; i < ctx->argc; i++) {
847         struct eth_addr ea;
848
849         if (strcmp(ctx->argv[i], "unknown")
850             && !ovs_scan(ctx->argv[i], ETH_ADDR_SCAN_FMT,
851                          ETH_ADDR_SCAN_ARGS(ea))) {
852             ctl_fatal("%s: Invalid address format. See ovn-nb(5). "
853                       "Hint: An Ethernet address must be "
854                       "listed before an IP address, together as a single "
855                       "argument.", ctx->argv[i]);
856         }
857     }
858
859     nbrec_logical_switch_port_set_addresses(lsp,
860             (const char **) ctx->argv + 2, ctx->argc - 2);
861 }
862
863 static void
864 nbctl_lsp_get_addresses(struct ctl_context *ctx)
865 {
866     const char *id = ctx->argv[1];
867     const struct nbrec_logical_switch_port *lsp;
868     struct svec addresses;
869     const char *mac;
870     size_t i;
871
872     lsp = lsp_by_name_or_uuid(ctx, id, true);
873
874     svec_init(&addresses);
875     for (i = 0; i < lsp->n_addresses; i++) {
876         svec_add(&addresses, lsp->addresses[i]);
877     }
878     svec_sort(&addresses);
879     SVEC_FOR_EACH(i, mac, &addresses) {
880         ds_put_format(&ctx->output, "%s\n", mac);
881     }
882     svec_destroy(&addresses);
883 }
884
885 static void
886 nbctl_lsp_set_port_security(struct ctl_context *ctx)
887 {
888     const char *id = ctx->argv[1];
889     const struct nbrec_logical_switch_port *lsp;
890
891     lsp = lsp_by_name_or_uuid(ctx, id, true);
892     nbrec_logical_switch_port_set_port_security(lsp,
893             (const char **) ctx->argv + 2, ctx->argc - 2);
894 }
895
896 static void
897 nbctl_lsp_get_port_security(struct ctl_context *ctx)
898 {
899     const char *id = ctx->argv[1];
900     const struct nbrec_logical_switch_port *lsp;
901     struct svec addrs;
902     const char *addr;
903     size_t i;
904
905     lsp = lsp_by_name_or_uuid(ctx, id, true);
906     svec_init(&addrs);
907     for (i = 0; i < lsp->n_port_security; i++) {
908         svec_add(&addrs, lsp->port_security[i]);
909     }
910     svec_sort(&addrs);
911     SVEC_FOR_EACH(i, addr, &addrs) {
912         ds_put_format(&ctx->output, "%s\n", addr);
913     }
914     svec_destroy(&addrs);
915 }
916
917 static void
918 nbctl_lsp_get_up(struct ctl_context *ctx)
919 {
920     const char *id = ctx->argv[1];
921     const struct nbrec_logical_switch_port *lsp;
922
923     lsp = lsp_by_name_or_uuid(ctx, id, true);
924     ds_put_format(&ctx->output,
925                   "%s\n", (lsp->up && *lsp->up) ? "up" : "down");
926 }
927
928 static bool
929 parse_enabled(const char *state)
930 {
931     if (!strcasecmp(state, "enabled")) {
932         return true;
933     } else if (!strcasecmp(state, "disabled")) {
934         return false;
935     } else {
936         ctl_fatal("%s: state must be \"enabled\" or \"disabled\"", state);
937     }
938 }
939
940 static void
941 nbctl_lsp_set_enabled(struct ctl_context *ctx)
942 {
943     const char *id = ctx->argv[1];
944     const char *state = ctx->argv[2];
945     const struct nbrec_logical_switch_port *lsp;
946
947     lsp = lsp_by_name_or_uuid(ctx, id, true);
948     bool enabled = parse_enabled(state);
949     nbrec_logical_switch_port_set_enabled(lsp, &enabled, 1);
950 }
951
952 static void
953 nbctl_lsp_get_enabled(struct ctl_context *ctx)
954 {
955     const char *id = ctx->argv[1];
956     const struct nbrec_logical_switch_port *lsp;
957
958     lsp = lsp_by_name_or_uuid(ctx, id, true);
959     ds_put_format(&ctx->output, "%s\n",
960                   !lsp->enabled || *lsp->enabled ? "enabled" : "disabled");
961 }
962
963 static void
964 nbctl_lsp_set_type(struct ctl_context *ctx)
965 {
966     const char *id = ctx->argv[1];
967     const char *type = ctx->argv[2];
968     const struct nbrec_logical_switch_port *lsp;
969
970     lsp = lsp_by_name_or_uuid(ctx, id, true);
971     nbrec_logical_switch_port_set_type(lsp, type);
972 }
973
974 static void
975 nbctl_lsp_get_type(struct ctl_context *ctx)
976 {
977     const char *id = ctx->argv[1];
978     const struct nbrec_logical_switch_port *lsp;
979
980     lsp = lsp_by_name_or_uuid(ctx, id, true);
981     ds_put_format(&ctx->output, "%s\n", lsp->type);
982 }
983
984 static void
985 nbctl_lsp_set_options(struct ctl_context *ctx)
986 {
987     const char *id = ctx->argv[1];
988     const struct nbrec_logical_switch_port *lsp;
989     size_t i;
990     struct smap options = SMAP_INITIALIZER(&options);
991
992     lsp = lsp_by_name_or_uuid(ctx, id, true);
993     for (i = 2; i < ctx->argc; i++) {
994         char *key, *value;
995         value = xstrdup(ctx->argv[i]);
996         key = strsep(&value, "=");
997         if (value) {
998             smap_add(&options, key, value);
999         }
1000         free(key);
1001     }
1002
1003     nbrec_logical_switch_port_set_options(lsp, &options);
1004
1005     smap_destroy(&options);
1006 }
1007
1008 static void
1009 nbctl_lsp_get_options(struct ctl_context *ctx)
1010 {
1011     const char *id = ctx->argv[1];
1012     const struct nbrec_logical_switch_port *lsp;
1013     struct smap_node *node;
1014
1015     lsp = lsp_by_name_or_uuid(ctx, id, true);
1016     SMAP_FOR_EACH(node, &lsp->options) {
1017         ds_put_format(&ctx->output, "%s=%s\n", node->key, node->value);
1018     }
1019 }
1020
1021 enum {
1022     DIR_FROM_LPORT,
1023     DIR_TO_LPORT
1024 };
1025
1026 static int
1027 dir_encode(const char *dir)
1028 {
1029     if (!strcmp(dir, "from-lport")) {
1030         return DIR_FROM_LPORT;
1031     } else if (!strcmp(dir, "to-lport")) {
1032         return DIR_TO_LPORT;
1033     }
1034
1035     OVS_NOT_REACHED();
1036 }
1037
1038 static int
1039 acl_cmp(const void *acl1_, const void *acl2_)
1040 {
1041     const struct nbrec_acl *const *acl1p = acl1_;
1042     const struct nbrec_acl *const *acl2p = acl2_;
1043     const struct nbrec_acl *acl1 = *acl1p;
1044     const struct nbrec_acl *acl2 = *acl2p;
1045
1046     int dir1 = dir_encode(acl1->direction);
1047     int dir2 = dir_encode(acl2->direction);
1048
1049     if (dir1 != dir2) {
1050         return dir1 < dir2 ? -1 : 1;
1051     } else if (acl1->priority != acl2->priority) {
1052         return acl1->priority > acl2->priority ? -1 : 1;
1053     } else {
1054         return strcmp(acl1->match, acl2->match);
1055     }
1056 }
1057
1058 static void
1059 nbctl_acl_list(struct ctl_context *ctx)
1060 {
1061     const struct nbrec_logical_switch *ls;
1062     const struct nbrec_acl **acls;
1063     size_t i;
1064
1065     ls = ls_by_name_or_uuid(ctx, ctx->argv[1], true);
1066
1067     acls = xmalloc(sizeof *acls * ls->n_acls);
1068     for (i = 0; i < ls->n_acls; i++) {
1069         acls[i] = ls->acls[i];
1070     }
1071
1072     qsort(acls, ls->n_acls, sizeof *acls, acl_cmp);
1073
1074     for (i = 0; i < ls->n_acls; i++) {
1075         const struct nbrec_acl *acl = acls[i];
1076         ds_put_format(&ctx->output, "%10s %5"PRId64" (%s) %s%s\n",
1077                       acl->direction, acl->priority,
1078                       acl->match, acl->action, acl->log ? " log" : "");
1079     }
1080
1081     free(acls);
1082 }
1083
1084 static const char *
1085 parse_direction(const char *arg)
1086 {
1087     /* Validate direction.  Only require the first letter. */
1088     if (arg[0] == 't') {
1089         return "to-lport";
1090     } else if (arg[0] == 'f') {
1091         return "from-lport";
1092     } else {
1093         ctl_fatal("%s: direction must be \"to-lport\" or \"from-lport\"", arg);
1094     }
1095 }
1096
1097 static int
1098 parse_priority(const char *arg)
1099 {
1100     /* Validate priority. */
1101     int64_t priority;
1102     if (!ovs_scan(arg, "%"SCNd64, &priority)
1103         || priority < 0 || priority > 32767) {
1104         ctl_fatal("%s: priority must in range 0...32767", arg);
1105     }
1106     return priority;
1107 }
1108
1109 static void
1110 nbctl_acl_add(struct ctl_context *ctx)
1111 {
1112     const struct nbrec_logical_switch *ls;
1113     const char *action = ctx->argv[5];
1114
1115     ls = ls_by_name_or_uuid(ctx, ctx->argv[1], true);
1116
1117     const char *direction = parse_direction(ctx->argv[2]);
1118     int64_t priority = parse_priority(ctx->argv[3]);
1119
1120     /* Validate action. */
1121     if (strcmp(action, "allow") && strcmp(action, "allow-related")
1122         && strcmp(action, "drop") && strcmp(action, "reject")) {
1123         ctl_fatal("%s: action must be one of \"allow\", \"allow-related\", "
1124                   "\"drop\", and \"reject\"", action);
1125         return;
1126     }
1127
1128     /* Create the acl. */
1129     struct nbrec_acl *acl = nbrec_acl_insert(ctx->txn);
1130     nbrec_acl_set_priority(acl, priority);
1131     nbrec_acl_set_direction(acl, direction);
1132     nbrec_acl_set_match(acl, ctx->argv[4]);
1133     nbrec_acl_set_action(acl, action);
1134     if (shash_find(&ctx->options, "--log") != NULL) {
1135         nbrec_acl_set_log(acl, true);
1136     }
1137
1138     /* Insert the acl into the logical switch. */
1139     nbrec_logical_switch_verify_acls(ls);
1140     struct nbrec_acl **new_acls = xmalloc(sizeof *new_acls * (ls->n_acls + 1));
1141     memcpy(new_acls, ls->acls, sizeof *new_acls * ls->n_acls);
1142     new_acls[ls->n_acls] = acl;
1143     nbrec_logical_switch_set_acls(ls, new_acls, ls->n_acls + 1);
1144     free(new_acls);
1145 }
1146
1147 static void
1148 nbctl_acl_del(struct ctl_context *ctx)
1149 {
1150     const struct nbrec_logical_switch *ls;
1151     ls = ls_by_name_or_uuid(ctx, ctx->argv[1], true);
1152
1153     if (ctx->argc != 2 && ctx->argc != 3 && ctx->argc != 5) {
1154         ctl_fatal("cannot specify priority without match");
1155     }
1156
1157     if (ctx->argc == 2) {
1158         /* If direction, priority, and match are not specified, delete
1159          * all ACLs. */
1160         nbrec_logical_switch_verify_acls(ls);
1161         nbrec_logical_switch_set_acls(ls, NULL, 0);
1162         return;
1163     }
1164
1165     const char *direction = parse_direction(ctx->argv[2]);
1166
1167     /* If priority and match are not specified, delete all ACLs with the
1168      * specified direction. */
1169     if (ctx->argc == 3) {
1170         struct nbrec_acl **new_acls = xmalloc(sizeof *new_acls * ls->n_acls);
1171
1172         int n_acls = 0;
1173         for (size_t i = 0; i < ls->n_acls; i++) {
1174             if (strcmp(direction, ls->acls[i]->direction)) {
1175                 new_acls[n_acls++] = ls->acls[i];
1176             }
1177         }
1178
1179         nbrec_logical_switch_verify_acls(ls);
1180         nbrec_logical_switch_set_acls(ls, new_acls, n_acls);
1181         free(new_acls);
1182         return;
1183     }
1184
1185     int64_t priority = parse_priority(ctx->argv[3]);
1186
1187     /* Remove the matching rule. */
1188     for (size_t i = 0; i < ls->n_acls; i++) {
1189         struct nbrec_acl *acl = ls->acls[i];
1190
1191         if (priority == acl->priority && !strcmp(ctx->argv[4], acl->match) &&
1192              !strcmp(direction, acl->direction)) {
1193             struct nbrec_acl **new_acls
1194                 = xmemdup(ls->acls, sizeof *new_acls * ls->n_acls);
1195             new_acls[i] = ls->acls[ls->n_acls - 1];
1196             nbrec_logical_switch_verify_acls(ls);
1197             nbrec_logical_switch_set_acls(ls, new_acls,
1198                                           ls->n_acls - 1);
1199             free(new_acls);
1200             return;
1201         }
1202     }
1203 }
1204 \f
1205 static void
1206 nbctl_lr_add(struct ctl_context *ctx)
1207 {
1208     const char *lr_name = ctx->argc == 2 ? ctx->argv[1] : NULL;
1209
1210     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1211     bool add_duplicate = shash_find(&ctx->options, "--add-duplicate") != NULL;
1212     if (may_exist && add_duplicate) {
1213         ctl_fatal("--may-exist and --add-duplicate may not be used together");
1214     }
1215
1216     if (lr_name) {
1217         if (!add_duplicate) {
1218             const struct nbrec_logical_router *lr;
1219             NBREC_LOGICAL_ROUTER_FOR_EACH (lr, ctx->idl) {
1220                 if (!strcmp(lr->name, lr_name)) {
1221                     if (may_exist) {
1222                         return;
1223                     }
1224                     ctl_fatal("%s: a router with this name already exists",
1225                               lr_name);
1226                 }
1227             }
1228         }
1229     } else if (may_exist) {
1230         ctl_fatal("--may-exist requires specifying a name");
1231     } else if (add_duplicate) {
1232         ctl_fatal("--add-duplicate requires specifying a name");
1233     }
1234
1235     struct nbrec_logical_router *lr;
1236     lr = nbrec_logical_router_insert(ctx->txn);
1237     if (lr_name) {
1238         nbrec_logical_router_set_name(lr, lr_name);
1239     }
1240 }
1241
1242 static void
1243 nbctl_lr_del(struct ctl_context *ctx)
1244 {
1245     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1246     const char *id = ctx->argv[1];
1247     const struct nbrec_logical_router *lr;
1248
1249     lr = lr_by_name_or_uuid(ctx, id, must_exist);
1250     if (!lr) {
1251         return;
1252     }
1253
1254     nbrec_logical_router_delete(lr);
1255 }
1256
1257 static void
1258 nbctl_lr_list(struct ctl_context *ctx)
1259 {
1260     const struct nbrec_logical_router *lr;
1261     struct smap lrs;
1262
1263     smap_init(&lrs);
1264     NBREC_LOGICAL_ROUTER_FOR_EACH(lr, ctx->idl) {
1265         smap_add_format(&lrs, lr->name, UUID_FMT " (%s)",
1266                         UUID_ARGS(&lr->header_.uuid), lr->name);
1267     }
1268     const struct smap_node **nodes = smap_sort(&lrs);
1269     for (size_t i = 0; i < smap_count(&lrs); i++) {
1270         const struct smap_node *node = nodes[i];
1271         ds_put_format(&ctx->output, "%s\n", node->value);
1272     }
1273     smap_destroy(&lrs);
1274     free(nodes);
1275 }
1276
1277 /* The caller must free the returned string. */
1278 static char *
1279 normalize_ipv4_prefix(ovs_be32 ipv4, unsigned int plen)
1280 {
1281     ovs_be32 network = ipv4 & be32_prefix_mask(plen);
1282     if (plen == 32) {
1283         return xasprintf(IP_FMT, IP_ARGS(network));
1284     } else {
1285         return xasprintf(IP_FMT"/%d", IP_ARGS(network), plen);
1286     }
1287 }
1288
1289 /* The caller must free the returned string. */
1290 static char *
1291 normalize_ipv6_prefix(struct in6_addr ipv6, unsigned int plen)
1292 {
1293     char network_s[INET6_ADDRSTRLEN];
1294
1295     struct in6_addr mask = ipv6_create_mask(plen);
1296     struct in6_addr network = ipv6_addr_bitand(&ipv6, &mask);
1297
1298     inet_ntop(AF_INET6, &network, network_s, INET6_ADDRSTRLEN);
1299     if (plen == 128) {
1300         return xasprintf("%s", network_s);
1301     } else {
1302         return xasprintf("%s/%d", network_s, plen);
1303     }
1304 }
1305
1306 /* The caller must free the returned string. */
1307 static char *
1308 normalize_prefix_str(const char *orig_prefix)
1309 {
1310     unsigned int plen;
1311     ovs_be32 ipv4;
1312     char *error;
1313
1314     error = ip_parse_cidr(orig_prefix, &ipv4, &plen);
1315     if (!error) {
1316         return normalize_ipv4_prefix(ipv4, plen);
1317     } else {
1318         struct in6_addr ipv6;
1319         free(error);
1320
1321         error = ipv6_parse_cidr(orig_prefix, &ipv6, &plen);
1322         if (error) {
1323             free(error);
1324             return NULL;
1325         }
1326         return normalize_ipv6_prefix(ipv6, plen);
1327     }
1328 }
1329 \f
1330 static void
1331 nbctl_lr_route_add(struct ctl_context *ctx)
1332 {
1333     const struct nbrec_logical_router *lr;
1334     lr = lr_by_name_or_uuid(ctx, ctx->argv[1], true);
1335     char *prefix, *next_hop;
1336
1337     prefix = normalize_prefix_str(ctx->argv[2]);
1338     if (!prefix) {
1339         ctl_fatal("bad prefix argument: %s", ctx->argv[2]);
1340     }
1341
1342     next_hop = normalize_prefix_str(ctx->argv[3]);
1343     if (!next_hop) {
1344         ctl_fatal("bad next hop argument: %s", ctx->argv[3]);
1345     }
1346
1347     if (strchr(prefix, '.')) {
1348         ovs_be32 hop_ipv4;
1349         if (!ip_parse(ctx->argv[3], &hop_ipv4)) {
1350             ctl_fatal("bad IPv4 nexthop argument: %s", ctx->argv[3]);
1351         }
1352     } else {
1353         struct in6_addr hop_ipv6;
1354         if (!ipv6_parse(ctx->argv[3], &hop_ipv6)) {
1355             ctl_fatal("bad IPv6 nexthop argument: %s", ctx->argv[3]);
1356         }
1357     }
1358
1359     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1360     for (int i = 0; i < lr->n_static_routes; i++) {
1361         const struct nbrec_logical_router_static_route *route
1362             = lr->static_routes[i];
1363         char *rt_prefix;
1364
1365         rt_prefix = normalize_prefix_str(lr->static_routes[i]->ip_prefix);
1366         if (!rt_prefix) {
1367             /* Ignore existing prefix we couldn't parse. */
1368             continue;
1369         }
1370
1371         if (strcmp(rt_prefix, prefix)) {
1372             free(rt_prefix);
1373             continue;
1374         }
1375
1376         if (!may_exist) {
1377             ctl_fatal("duplicate prefix: %s", prefix);
1378         }
1379
1380         /* Update the next hop for an existing route. */
1381         nbrec_logical_router_verify_static_routes(lr);
1382         nbrec_logical_router_static_route_verify_ip_prefix(route);
1383         nbrec_logical_router_static_route_verify_nexthop(route);
1384         nbrec_logical_router_static_route_set_ip_prefix(route, prefix);
1385         nbrec_logical_router_static_route_set_nexthop(route, next_hop);
1386         free(rt_prefix);
1387         free(next_hop);
1388         free(prefix);
1389         return;
1390     }
1391
1392     struct nbrec_logical_router_static_route *route;
1393     route = nbrec_logical_router_static_route_insert(ctx->txn);
1394     nbrec_logical_router_static_route_set_ip_prefix(route, prefix);
1395     nbrec_logical_router_static_route_set_nexthop(route, next_hop);
1396     if (ctx->argc == 5) {
1397         nbrec_logical_router_static_route_set_output_port(route, ctx->argv[4]);
1398     }
1399
1400     nbrec_logical_router_verify_static_routes(lr);
1401     struct nbrec_logical_router_static_route **new_routes
1402         = xmalloc(sizeof *new_routes * (lr->n_static_routes + 1));
1403     memcpy(new_routes, lr->static_routes,
1404            sizeof *new_routes * lr->n_static_routes);
1405     new_routes[lr->n_static_routes] = route;
1406     nbrec_logical_router_set_static_routes(lr, new_routes,
1407                                            lr->n_static_routes + 1);
1408     free(new_routes);
1409     free(next_hop);
1410     free(prefix);
1411 }
1412
1413 static void
1414 nbctl_lr_route_del(struct ctl_context *ctx)
1415 {
1416     const struct nbrec_logical_router *lr;
1417     lr = lr_by_name_or_uuid(ctx, ctx->argv[1], true);
1418
1419     if (ctx->argc == 2) {
1420         /* If a prefix is not specified, delete all routes. */
1421         nbrec_logical_router_set_static_routes(lr, NULL, 0);
1422         return;
1423     }
1424
1425     char *prefix = normalize_prefix_str(ctx->argv[2]);
1426     if (!prefix) {
1427         ctl_fatal("bad prefix argument: %s", ctx->argv[2]);
1428     }
1429
1430     for (int i = 0; i < lr->n_static_routes; i++) {
1431         char *rt_prefix = normalize_prefix_str(lr->static_routes[i]->ip_prefix);
1432         if (!rt_prefix) {
1433             /* Ignore existing prefix we couldn't parse. */
1434             continue;
1435         }
1436
1437         if (!strcmp(prefix, rt_prefix)) {
1438             struct nbrec_logical_router_static_route **new_routes
1439                 = xmemdup(lr->static_routes,
1440                           sizeof *new_routes * lr->n_static_routes);
1441
1442             new_routes[i] = lr->static_routes[lr->n_static_routes - 1];
1443             nbrec_logical_router_verify_static_routes(lr);
1444             nbrec_logical_router_set_static_routes(lr, new_routes,
1445                                                  lr->n_static_routes - 1);
1446             free(new_routes);
1447             free(rt_prefix);
1448             free(prefix);
1449             return;
1450         }
1451         free(rt_prefix);
1452     }
1453
1454     if (!shash_find(&ctx->options, "--if-exists")) {
1455         ctl_fatal("no matching prefix: %s", prefix);
1456     }
1457     free(prefix);
1458 }
1459 \f
1460 static const struct nbrec_logical_router_port *
1461 lrp_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
1462 {
1463     const struct nbrec_logical_router_port *lrp = NULL;
1464
1465     struct uuid lrp_uuid;
1466     bool is_uuid = uuid_from_string(&lrp_uuid, id);
1467     if (is_uuid) {
1468         lrp = nbrec_logical_router_port_get_for_uuid(ctx->idl, &lrp_uuid);
1469     }
1470
1471     if (!lrp) {
1472         NBREC_LOGICAL_ROUTER_PORT_FOR_EACH(lrp, ctx->idl) {
1473             if (!strcmp(lrp->name, id)) {
1474                 break;
1475             }
1476         }
1477     }
1478
1479     if (!lrp && must_exist) {
1480         ctl_fatal("%s: port %s not found", id, is_uuid ? "UUID" : "name");
1481     }
1482
1483     return lrp;
1484 }
1485
1486 /* Returns the logical router that contains 'lrp'. */
1487 static const struct nbrec_logical_router *
1488 lrp_to_lr(const struct ovsdb_idl *idl,
1489                const struct nbrec_logical_router_port *lrp)
1490 {
1491     const struct nbrec_logical_router *lr;
1492     NBREC_LOGICAL_ROUTER_FOR_EACH (lr, idl) {
1493         for (size_t i = 0; i < lr->n_ports; i++) {
1494             if (lr->ports[i] == lrp) {
1495                 return lr;
1496             }
1497         }
1498     }
1499
1500     /* Can't happen because of the database schema */
1501     ctl_fatal("port %s is not part of any logical router",
1502               lrp->name);
1503 }
1504
1505 static const char *
1506 lr_get_name(const struct nbrec_logical_router *lr, char uuid_s[UUID_LEN + 1],
1507             size_t uuid_s_size)
1508 {
1509     if (lr->name[0]) {
1510         return lr->name;
1511     }
1512     snprintf(uuid_s, uuid_s_size, UUID_FMT, UUID_ARGS(&lr->header_.uuid));
1513     return uuid_s;
1514 }
1515
1516 static void
1517 nbctl_lrp_add(struct ctl_context *ctx)
1518 {
1519     bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
1520
1521     const struct nbrec_logical_router *lr;
1522     lr = lr_by_name_or_uuid(ctx, ctx->argv[1], true);
1523
1524     const char *lrp_name = ctx->argv[2];
1525     const char *mac = ctx->argv[3];
1526     const char *network = ctx->argv[4];
1527     const char *peer = (ctx->argc == 6) ? ctx->argv[5] : NULL;
1528
1529     const struct nbrec_logical_router_port *lrp;
1530     lrp = lrp_by_name_or_uuid(ctx, lrp_name, false);
1531     if (lrp) {
1532         if (!may_exist) {
1533             ctl_fatal("%s: a port with this name already exists",
1534                       lrp_name);
1535         }
1536
1537         const struct nbrec_logical_router *bound_lr;
1538         bound_lr = lrp_to_lr(ctx->idl, lrp);
1539         if (bound_lr != lr) {
1540             char uuid_s[UUID_LEN + 1];
1541             ctl_fatal("%s: port already exists but in router %s", lrp_name,
1542                       lr_get_name(bound_lr, uuid_s, sizeof uuid_s));
1543         }
1544
1545         if (strcmp(mac, lrp->mac)) {
1546             ctl_fatal("%s: port already exists with mac %s", lrp_name,
1547                       lrp->mac);
1548         }
1549
1550         if (strcmp(network, lrp->network)) {
1551             ctl_fatal("%s: port already exists with network %s", lrp_name,
1552                       lrp->network);
1553         }
1554
1555         if ((!peer != !lrp->peer) ||
1556                 (lrp->peer && strcmp(peer, lrp->peer))) {
1557             ctl_fatal("%s: port already exists with mismatching peer",
1558                       lrp_name);
1559         }
1560
1561         return;
1562     }
1563
1564     struct eth_addr ea;
1565     if (!ovs_scan(mac, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(ea))) {
1566         ctl_fatal("%s: invalid mac address.", mac);
1567     }
1568
1569     ovs_be32 ipv4;
1570     unsigned int plen;
1571     char *error = ip_parse_cidr(network, &ipv4, &plen);
1572     if (error) {
1573         free(error);
1574         struct in6_addr ipv6;
1575         error = ipv6_parse_cidr(network, &ipv6, &plen);
1576         if (error) {
1577             free(error);
1578             ctl_fatal("%s: invalid network address.", network);
1579         }
1580     }
1581
1582     /* Create the logical port. */
1583     lrp = nbrec_logical_router_port_insert(ctx->txn);
1584     nbrec_logical_router_port_set_name(lrp, lrp_name);
1585     nbrec_logical_router_port_set_mac(lrp, mac);
1586     nbrec_logical_router_port_set_network(lrp, network);
1587     if (peer) {
1588         nbrec_logical_router_port_set_peer(lrp, peer);
1589     }
1590
1591     /* Insert the logical port into the logical router. */
1592     nbrec_logical_router_verify_ports(lr);
1593     struct nbrec_logical_router_port **new_ports = xmalloc(sizeof *new_ports *
1594                                                         (lr->n_ports + 1));
1595     memcpy(new_ports, lr->ports, sizeof *new_ports * lr->n_ports);
1596     new_ports[lr->n_ports] = CONST_CAST(struct nbrec_logical_router_port *,
1597                                              lrp);
1598     nbrec_logical_router_set_ports(lr, new_ports, lr->n_ports + 1);
1599     free(new_ports);
1600 }
1601
1602 /* Removes logical router port 'lr->ports[idx]'. */
1603 static void
1604 remove_lrp(const struct nbrec_logical_router *lr, size_t idx)
1605 {
1606     const struct nbrec_logical_router_port *lrp = lr->ports[idx];
1607
1608     /* First remove 'lrp' from the array of ports.  This is what will
1609      * actually cause the logical port to be deleted when the transaction is
1610      * sent to the database server (due to garbage collection). */
1611     struct nbrec_logical_router_port **new_ports
1612         = xmemdup(lr->ports, sizeof *new_ports * lr->n_ports);
1613     new_ports[idx] = new_ports[lr->n_ports - 1];
1614     nbrec_logical_router_verify_ports(lr);
1615     nbrec_logical_router_set_ports(lr, new_ports, lr->n_ports - 1);
1616     free(new_ports);
1617
1618     /* Delete 'lrp' from the IDL.  This won't have a real effect on
1619      * the database server (the IDL will suppress it in fact) but it
1620      * means that it won't show up when we iterate with
1621      * NBREC_LOGICAL_ROUTER_PORT_FOR_EACH later. */
1622     nbrec_logical_router_port_delete(lrp);
1623 }
1624
1625 static void
1626 nbctl_lrp_del(struct ctl_context *ctx)
1627 {
1628     bool must_exist = !shash_find(&ctx->options, "--if-exists");
1629     const struct nbrec_logical_router_port *lrp;
1630
1631     lrp = lrp_by_name_or_uuid(ctx, ctx->argv[1], must_exist);
1632     if (!lrp) {
1633         return;
1634     }
1635
1636     /* Find the router that contains 'lrp', then delete it. */
1637     const struct nbrec_logical_router *lr;
1638     NBREC_LOGICAL_ROUTER_FOR_EACH (lr, ctx->idl) {
1639         for (size_t i = 0; i < lr->n_ports; i++) {
1640             if (lr->ports[i] == lrp) {
1641                 remove_lrp(lr, i);
1642                 return;
1643             }
1644         }
1645     }
1646
1647     /* Can't happen because of the database schema. */
1648     ctl_fatal("logical port %s is not part of any logical router",
1649               ctx->argv[1]);
1650 }
1651
1652 /* Print a list of logical router ports. */
1653 static void
1654 nbctl_lrp_list(struct ctl_context *ctx)
1655 {
1656     const char *id = ctx->argv[1];
1657     const struct nbrec_logical_router *lr;
1658     struct smap lrps;
1659     size_t i;
1660
1661     lr = lr_by_name_or_uuid(ctx, id, true);
1662
1663     smap_init(&lrps);
1664     for (i = 0; i < lr->n_ports; i++) {
1665         const struct nbrec_logical_router_port *lrp = lr->ports[i];
1666         smap_add_format(&lrps, lrp->name, UUID_FMT " (%s)",
1667                         UUID_ARGS(&lrp->header_.uuid), lrp->name);
1668     }
1669     const struct smap_node **nodes = smap_sort(&lrps);
1670     for (i = 0; i < smap_count(&lrps); i++) {
1671         const struct smap_node *node = nodes[i];
1672         ds_put_format(&ctx->output, "%s\n", node->value);
1673     }
1674     smap_destroy(&lrps);
1675     free(nodes);
1676 }
1677
1678 /* Set the logical router port admin-enabled state. */
1679 static void
1680 nbctl_lrp_set_enabled(struct ctl_context *ctx)
1681 {
1682     const char *id = ctx->argv[1];
1683     const char *state = ctx->argv[2];
1684     const struct nbrec_logical_router_port *lrp;
1685
1686     lrp = lrp_by_name_or_uuid(ctx, id, true);
1687     if (!lrp) {
1688         return;
1689     }
1690
1691     bool enabled = parse_enabled(state);
1692     nbrec_logical_router_port_set_enabled(lrp, &enabled, 1);
1693 }
1694
1695 /* Print admin-enabled state for logical router port. */
1696 static void
1697 nbctl_lrp_get_enabled(struct ctl_context *ctx)
1698 {
1699     const char *id = ctx->argv[1];
1700     const struct nbrec_logical_router_port *lrp;
1701
1702     lrp = lrp_by_name_or_uuid(ctx, id, true);
1703     if (!lrp) {
1704         return;
1705     }
1706
1707     ds_put_format(&ctx->output, "%s\n",
1708                   !lrp->enabled ||
1709                   *lrp->enabled ? "enabled" : "disabled");
1710 }
1711 \f
1712 struct ipv4_route {
1713     int plen;
1714     ovs_be32 addr;
1715     const struct nbrec_logical_router_static_route *route;
1716 };
1717
1718 static int
1719 ipv4_route_cmp(const void *route1_, const void *route2_)
1720 {
1721     const struct ipv4_route *route1p = route1_;
1722     const struct ipv4_route *route2p = route2_;
1723
1724     if (route1p->plen != route2p->plen) {
1725         return route1p->plen > route2p->plen ? -1 : 1;
1726     } else if (route1p->addr != route2p->addr) {
1727         return ntohl(route1p->addr) < ntohl(route2p->addr) ? -1 : 1;
1728     } else {
1729         return 0;
1730     }
1731 }
1732
1733 struct ipv6_route {
1734     int plen;
1735     struct in6_addr addr;
1736     const struct nbrec_logical_router_static_route *route;
1737 };
1738
1739 static int
1740 ipv6_route_cmp(const void *route1_, const void *route2_)
1741 {
1742     const struct ipv6_route *route1p = route1_;
1743     const struct ipv6_route *route2p = route2_;
1744
1745     if (route1p->plen != route2p->plen) {
1746         return route1p->plen > route2p->plen ? -1 : 1;
1747     }
1748     return memcmp(&route1p->addr, &route2p->addr, sizeof(route1p->addr));
1749 }
1750
1751 static void
1752 print_route(const struct nbrec_logical_router_static_route *route, struct ds *s)
1753 {
1754
1755     char *prefix = normalize_prefix_str(route->ip_prefix);
1756     char *next_hop = normalize_prefix_str(route->nexthop);
1757     ds_put_format(s, "%25s %25s", prefix, next_hop);
1758     free(prefix);
1759     free(next_hop);
1760
1761     if (route->output_port) {
1762         ds_put_format(s, " %s", route->output_port);
1763     }
1764     ds_put_char(s, '\n');
1765 }
1766
1767 static void
1768 nbctl_lr_route_list(struct ctl_context *ctx)
1769 {
1770     const struct nbrec_logical_router *lr;
1771     struct ipv4_route *ipv4_routes;
1772     struct ipv6_route *ipv6_routes;
1773     size_t n_ipv4_routes = 0;
1774     size_t n_ipv6_routes = 0;
1775
1776     lr = lr_by_name_or_uuid(ctx, ctx->argv[1], true);
1777
1778     ipv4_routes = xmalloc(sizeof *ipv4_routes * lr->n_static_routes);
1779     ipv6_routes = xmalloc(sizeof *ipv6_routes * lr->n_static_routes);
1780
1781     for (int i = 0; i < lr->n_static_routes; i++) {
1782         const struct nbrec_logical_router_static_route *route
1783             = lr->static_routes[i];
1784         unsigned int plen;
1785         ovs_be32 ipv4;
1786         char *error;
1787
1788         error = ip_parse_cidr(route->ip_prefix, &ipv4, &plen);
1789         if (!error) {
1790             ipv4_routes[n_ipv4_routes].plen = plen;
1791             ipv4_routes[n_ipv4_routes].addr = ipv4;
1792             ipv4_routes[n_ipv4_routes].route = route;
1793             n_ipv4_routes++;
1794         } else {
1795             free(error);
1796
1797             struct in6_addr ipv6;
1798             if (!ipv6_parse_cidr(route->ip_prefix, &ipv6, &plen)) {
1799                 ipv6_routes[n_ipv6_routes].plen = plen;
1800                 ipv6_routes[n_ipv6_routes].addr = ipv6;
1801                 ipv6_routes[n_ipv6_routes].route = route;
1802                 n_ipv6_routes++;
1803             } else {
1804                 /* Invalid prefix. */
1805                 VLOG_WARN("router "UUID_FMT" (%s) has invalid prefix: %s",
1806                           UUID_ARGS(&lr->header_.uuid), lr->name,
1807                           route->ip_prefix);
1808                 free(error);
1809                 continue;
1810             }
1811         }
1812     }
1813
1814     qsort(ipv4_routes, n_ipv4_routes, sizeof *ipv4_routes, ipv4_route_cmp);
1815     qsort(ipv6_routes, n_ipv6_routes, sizeof *ipv6_routes, ipv6_route_cmp);
1816
1817     if (n_ipv4_routes) {
1818         ds_put_cstr(&ctx->output, "IPv4 Routes\n");
1819     }
1820     for (int i = 0; i < n_ipv4_routes; i++) {
1821         print_route(ipv4_routes[i].route, &ctx->output);
1822     }
1823
1824     if (n_ipv6_routes) {
1825         ds_put_format(&ctx->output, "%sIPv6 Routes\n",
1826                       n_ipv4_routes ?  "\n" : "");
1827     }
1828     for (int i = 0; i < n_ipv6_routes; i++) {
1829         print_route(ipv6_routes[i].route, &ctx->output);
1830     }
1831
1832     free(ipv4_routes);
1833     free(ipv6_routes);
1834 }
1835
1836 static const struct ctl_table_class tables[] = {
1837     {&nbrec_table_logical_switch,
1838      {{&nbrec_table_logical_switch, &nbrec_logical_switch_col_name, NULL},
1839       {NULL, NULL, NULL}}},
1840
1841     {&nbrec_table_logical_switch_port,
1842      {{&nbrec_table_logical_switch_port, &nbrec_logical_switch_port_col_name,
1843        NULL},
1844       {NULL, NULL, NULL}}},
1845
1846     {&nbrec_table_acl,
1847      {{NULL, NULL, NULL},
1848       {NULL, NULL, NULL}}},
1849
1850     {&nbrec_table_logical_router,
1851      {{&nbrec_table_logical_router, &nbrec_logical_router_col_name, NULL},
1852       {NULL, NULL, NULL}}},
1853
1854     {&nbrec_table_logical_router_port,
1855      {{&nbrec_table_logical_router_port, &nbrec_logical_router_port_col_name,
1856        NULL},
1857       {NULL, NULL, NULL}}},
1858
1859     {&nbrec_table_logical_router_static_route,
1860      {{&nbrec_table_logical_router_static_route, NULL,
1861        NULL},
1862       {NULL, NULL, NULL}}},
1863
1864     {&nbrec_table_nat,
1865      {{&nbrec_table_nat, NULL,
1866        NULL},
1867       {NULL, NULL, NULL}}},
1868
1869     {NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
1870 };
1871 \f
1872 static void
1873 run_prerequisites(struct ctl_command *commands, size_t n_commands,
1874                   struct ovsdb_idl *idl)
1875 {
1876     struct ctl_command *c;
1877
1878     for (c = commands; c < &commands[n_commands]; c++) {
1879         if (c->syntax->prerequisites) {
1880             struct ctl_context ctx;
1881
1882             ds_init(&c->output);
1883             c->table = NULL;
1884
1885             ctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
1886             (c->syntax->prerequisites)(&ctx);
1887             ctl_context_done(&ctx, c);
1888
1889             ovs_assert(!c->output.string);
1890             ovs_assert(!c->table);
1891         }
1892     }
1893 }
1894
1895 static bool
1896 do_nbctl(const char *args, struct ctl_command *commands, size_t n_commands,
1897          struct ovsdb_idl *idl)
1898 {
1899     struct ovsdb_idl_txn *txn;
1900     enum ovsdb_idl_txn_status status;
1901     struct ovsdb_symbol_table *symtab;
1902     struct ctl_context ctx;
1903     struct ctl_command *c;
1904     struct shash_node *node;
1905     char *error = NULL;
1906
1907     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
1908     if (dry_run) {
1909         ovsdb_idl_txn_set_dry_run(txn);
1910     }
1911
1912     ovsdb_idl_txn_add_comment(txn, "ovs-nbctl: %s", args);
1913
1914     symtab = ovsdb_symbol_table_create();
1915     for (c = commands; c < &commands[n_commands]; c++) {
1916         ds_init(&c->output);
1917         c->table = NULL;
1918     }
1919     ctl_context_init(&ctx, NULL, idl, txn, symtab, NULL);
1920     for (c = commands; c < &commands[n_commands]; c++) {
1921         ctl_context_init_command(&ctx, c);
1922         if (c->syntax->run) {
1923             (c->syntax->run)(&ctx);
1924         }
1925         ctl_context_done_command(&ctx, c);
1926
1927         if (ctx.try_again) {
1928             ctl_context_done(&ctx, NULL);
1929             goto try_again;
1930         }
1931     }
1932     ctl_context_done(&ctx, NULL);
1933
1934     SHASH_FOR_EACH (node, &symtab->sh) {
1935         struct ovsdb_symbol *symbol = node->data;
1936         if (!symbol->created) {
1937             ctl_fatal("row id \"%s\" is referenced but never created (e.g. "
1938                       "with \"-- --id=%s create ...\")",
1939                       node->name, node->name);
1940         }
1941         if (!symbol->strong_ref) {
1942             if (!symbol->weak_ref) {
1943                 VLOG_WARN("row id \"%s\" was created but no reference to it "
1944                           "was inserted, so it will not actually appear in "
1945                           "the database", node->name);
1946             } else {
1947                 VLOG_WARN("row id \"%s\" was created but only a weak "
1948                           "reference to it was inserted, so it will not "
1949                           "actually appear in the database", node->name);
1950             }
1951         }
1952     }
1953
1954     status = ovsdb_idl_txn_commit_block(txn);
1955     if (status == TXN_UNCHANGED || status == TXN_SUCCESS) {
1956         for (c = commands; c < &commands[n_commands]; c++) {
1957             if (c->syntax->postprocess) {
1958                 ctl_context_init(&ctx, c, idl, txn, symtab, NULL);
1959                 (c->syntax->postprocess)(&ctx);
1960                 ctl_context_done(&ctx, c);
1961             }
1962         }
1963     }
1964     error = xstrdup(ovsdb_idl_txn_get_error(txn));
1965
1966     switch (status) {
1967     case TXN_UNCOMMITTED:
1968     case TXN_INCOMPLETE:
1969         OVS_NOT_REACHED();
1970
1971     case TXN_ABORTED:
1972         /* Should not happen--we never call ovsdb_idl_txn_abort(). */
1973         ctl_fatal("transaction aborted");
1974
1975     case TXN_UNCHANGED:
1976     case TXN_SUCCESS:
1977         break;
1978
1979     case TXN_TRY_AGAIN:
1980         goto try_again;
1981
1982     case TXN_ERROR:
1983         ctl_fatal("transaction error: %s", error);
1984
1985     case TXN_NOT_LOCKED:
1986         /* Should not happen--we never call ovsdb_idl_set_lock(). */
1987         ctl_fatal("database not locked");
1988
1989     default:
1990         OVS_NOT_REACHED();
1991     }
1992     free(error);
1993
1994     ovsdb_symbol_table_destroy(symtab);
1995
1996     for (c = commands; c < &commands[n_commands]; c++) {
1997         struct ds *ds = &c->output;
1998
1999         if (c->table) {
2000             table_print(c->table, &table_style);
2001         } else if (oneline) {
2002             size_t j;
2003
2004             ds_chomp(ds, '\n');
2005             for (j = 0; j < ds->length; j++) {
2006                 int ch = ds->string[j];
2007                 switch (ch) {
2008                 case '\n':
2009                     fputs("\\n", stdout);
2010                     break;
2011
2012                 case '\\':
2013                     fputs("\\\\", stdout);
2014                     break;
2015
2016                 default:
2017                     putchar(ch);
2018                 }
2019             }
2020             putchar('\n');
2021         } else {
2022             fputs(ds_cstr(ds), stdout);
2023         }
2024         ds_destroy(&c->output);
2025         table_destroy(c->table);
2026         free(c->table);
2027
2028         shash_destroy_free_data(&c->options);
2029     }
2030     free(commands);
2031     ovsdb_idl_txn_destroy(txn);
2032     ovsdb_idl_destroy(idl);
2033
2034     return true;
2035
2036 try_again:
2037     /* Our transaction needs to be rerun, or a prerequisite was not met.  Free
2038      * resources and return so that the caller can try again. */
2039     if (txn) {
2040         ovsdb_idl_txn_abort(txn);
2041         ovsdb_idl_txn_destroy(txn);
2042         the_idl_txn = NULL;
2043     }
2044     ovsdb_symbol_table_destroy(symtab);
2045     for (c = commands; c < &commands[n_commands]; c++) {
2046         ds_destroy(&c->output);
2047         table_destroy(c->table);
2048         free(c->table);
2049     }
2050     free(error);
2051     return false;
2052 }
2053
2054 /* Frees the current transaction and the underlying IDL and then calls
2055  * exit(status).
2056  *
2057  * Freeing the transaction and the IDL is not strictly necessary, but it makes
2058  * for a clean memory leak report from valgrind in the normal case.  That makes
2059  * it easier to notice real memory leaks. */
2060 static void
2061 nbctl_exit(int status)
2062 {
2063     if (the_idl_txn) {
2064         ovsdb_idl_txn_abort(the_idl_txn);
2065         ovsdb_idl_txn_destroy(the_idl_txn);
2066     }
2067     ovsdb_idl_destroy(the_idl);
2068     exit(status);
2069 }
2070
2071 static const struct ctl_command_syntax nbctl_commands[] = {
2072     { "show", 0, 1, "[SWITCH]", NULL, nbctl_show, NULL, "", RO },
2073
2074     /* logical switch commands. */
2075     { "ls-add", 0, 1, "[SWITCH]", NULL, nbctl_ls_add, NULL,
2076       "--may-exist,--add-duplicate", RW },
2077     { "ls-del", 1, 1, "SWITCH", NULL, nbctl_ls_del, NULL, "--if-exists", RW },
2078     { "ls-list", 0, 0, "", NULL, nbctl_ls_list, NULL, "", RO },
2079
2080     /* acl commands. */
2081     { "acl-add", 5, 5, "SWITCH DIRECTION PRIORITY MATCH ACTION", NULL,
2082       nbctl_acl_add, NULL, "--log", RW },
2083     { "acl-del", 1, 4, "SWITCH [DIRECTION [PRIORITY MATCH]]", NULL,
2084       nbctl_acl_del, NULL, "", RW },
2085     { "acl-list", 1, 1, "SWITCH", NULL, nbctl_acl_list, NULL, "", RO },
2086
2087     /* logical switch port commands. */
2088     { "lsp-add", 2, 4, "SWITCH PORT [PARENT] [TAG]", NULL, nbctl_lsp_add,
2089       NULL, "--may-exist", RW },
2090     { "lsp-del", 1, 1, "PORT", NULL, nbctl_lsp_del, NULL, "--if-exists", RW },
2091     { "lsp-list", 1, 1, "SWITCH", NULL, nbctl_lsp_list, NULL, "", RO },
2092     { "lsp-get-parent", 1, 1, "PORT", NULL, nbctl_lsp_get_parent, NULL,
2093       "", RO },
2094     { "lsp-get-tag", 1, 1, "PORT", NULL, nbctl_lsp_get_tag, NULL, "", RO },
2095     { "lsp-set-addresses", 1, INT_MAX, "PORT [ADDRESS]...", NULL,
2096       nbctl_lsp_set_addresses, NULL, "", RW },
2097     { "lsp-get-addresses", 1, 1, "PORT", NULL, nbctl_lsp_get_addresses, NULL,
2098       "", RO },
2099     { "lsp-set-port-security", 0, INT_MAX, "PORT [ADDRS]...", NULL,
2100       nbctl_lsp_set_port_security, NULL, "", RW },
2101     { "lsp-get-port-security", 1, 1, "PORT", NULL,
2102       nbctl_lsp_get_port_security, NULL, "", RO },
2103     { "lsp-get-up", 1, 1, "PORT", NULL, nbctl_lsp_get_up, NULL, "", RO },
2104     { "lsp-set-enabled", 2, 2, "PORT STATE", NULL, nbctl_lsp_set_enabled,
2105       NULL, "", RW },
2106     { "lsp-get-enabled", 1, 1, "PORT", NULL, nbctl_lsp_get_enabled, NULL,
2107       "", RO },
2108     { "lsp-set-type", 2, 2, "PORT TYPE", NULL, nbctl_lsp_set_type, NULL,
2109       "", RW },
2110     { "lsp-get-type", 1, 1, "PORT", NULL, nbctl_lsp_get_type, NULL, "", RO },
2111     { "lsp-set-options", 1, INT_MAX, "PORT KEY=VALUE [KEY=VALUE]...", NULL,
2112       nbctl_lsp_set_options, NULL, "", RW },
2113     { "lsp-get-options", 1, 1, "PORT", NULL, nbctl_lsp_get_options, NULL,
2114       "", RO },
2115
2116     /* logical router commands. */
2117     { "lr-add", 0, 1, "[ROUTER]", NULL, nbctl_lr_add, NULL,
2118       "--may-exist,--add-duplicate", RW },
2119     { "lr-del", 1, 1, "ROUTER", NULL, nbctl_lr_del, NULL, "--if-exists", RW },
2120     { "lr-list", 0, 0, "", NULL, nbctl_lr_list, NULL, "", RO },
2121
2122     /* logical router port commands. */
2123     { "lrp-add", 4, 5, "ROUTER PORT MAC NETWORK [PEER]", NULL, nbctl_lrp_add,
2124       NULL, "--may-exist", RW },
2125     { "lrp-del", 1, 1, "LPORT", NULL, nbctl_lrp_del, NULL, "--if-exists", RW },
2126     { "lrp-list", 1, 1, "ROUTER", NULL, nbctl_lrp_list, NULL, "", RO },
2127     { "lrp-set-enabled", 2, 2, "PORT STATE", NULL, nbctl_lrp_set_enabled,
2128       NULL, "", RW },
2129     { "lrp-get-enabled", 1, 1, "PORT", NULL, nbctl_lrp_get_enabled,
2130       NULL, "", RO },
2131
2132     /* logical router route commands. */
2133     { "lr-route-add", 3, 4, "ROUTER PREFIX NEXTHOP [PORT]", NULL,
2134       nbctl_lr_route_add, NULL, "--may-exist", RW },
2135     { "lr-route-del", 1, 2, "ROUTER [PREFIX]", NULL, nbctl_lr_route_del,
2136       NULL, "--if-exists", RW },
2137     { "lr-route-list", 1, 1, "ROUTER", NULL, nbctl_lr_route_list, NULL,
2138       "", RO },
2139
2140     {NULL, 0, 0, NULL, NULL, NULL, NULL, "", RO},
2141 };
2142
2143 /* Registers nbctl and common db commands. */
2144 static void
2145 nbctl_cmd_init(void)
2146 {
2147     ctl_init(tables, NULL, nbctl_exit);
2148     ctl_register_commands(nbctl_commands);
2149 }