2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
30 #include "command-line.h"
33 #include "dynamic-string.h"
34 #include "fatal-signal.h"
37 #include "ovsdb-data.h"
38 #include "ovsdb-idl.h"
39 #include "poll-loop.h"
42 #include "stream-ssl.h"
46 #include "lib/vtep-idl.h"
53 VLOG_DEFINE_THIS_MODULE(vtep_ctl
);
55 /* vtep_ctl_fatal() also logs the error, so it is preferred in this file. */
56 #define ovs_fatal please_use_vtep_ctl_fatal_instead_of_ovs_fatal
58 struct vtep_ctl_context
;
60 /* A command supported by vtep-ctl. */
61 struct vtep_ctl_command_syntax
{
62 const char *name
; /* e.g. "add-ps" */
63 int min_args
; /* Min number of arguments following name. */
64 int max_args
; /* Max number of arguments following name. */
66 /* If nonnull, calls ovsdb_idl_add_column() or ovsdb_idl_add_table() for
67 * each column or table in ctx->idl that it uses. */
68 void (*prerequisites
)(struct vtep_ctl_context
*ctx
);
70 /* Does the actual work of the command and puts the command's output, if
71 * any, in ctx->output or ctx->table.
73 * Alternatively, if some prerequisite of the command is not met and the
74 * caller should wait for something to change and then retry, it may set
75 * ctx->try_again to true. (Only the "wait-until" command currently does
77 void (*run
)(struct vtep_ctl_context
*ctx
);
79 /* If nonnull, called after the transaction has been successfully
80 * committed. ctx->output is the output from the "run" function, which
81 * this function may modify and otherwise postprocess as needed. (Only the
82 * "create" command currently does any postprocessing.) */
83 void (*postprocess
)(struct vtep_ctl_context
*ctx
);
85 /* A comma-separated list of supported options, e.g. "--a,--b", or the
86 * empty string if the command does not support any options. */
88 enum { RO
, RW
} mode
; /* Does this command modify the database? */
91 struct vtep_ctl_command
{
92 /* Data that remains constant after initialization. */
93 const struct vtep_ctl_command_syntax
*syntax
;
98 /* Data modified by commands. */
103 /* --db: The database server to contact. */
104 static const char *db
;
106 /* --oneline: Write each command's output as a single line? */
109 /* --dry-run: Do not commit any changes. */
112 /* --timeout: Time to wait for a connection to 'db'. */
115 /* Format for table output. */
116 static struct table_style table_style
= TABLE_STYLE_DEFAULT
;
118 /* All supported commands. */
119 static const struct vtep_ctl_command_syntax all_commands
[];
121 /* The IDL we're using and the current transaction, if any.
122 * This is for use by vtep_ctl_exit() only, to allow it to clean up.
123 * Other code should use its context arguments. */
124 static struct ovsdb_idl
*the_idl
;
125 static struct ovsdb_idl_txn
*the_idl_txn
;
127 static void vtep_ctl_exit(int status
) NO_RETURN
;
128 static void vtep_ctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN
;
129 static char *default_db(void);
130 static void usage(void) NO_RETURN
;
131 static void parse_options(int argc
, char *argv
[], struct shash
*local_options
);
132 static bool might_write_to_db(char **argv
);
134 static struct vtep_ctl_command
*parse_commands(int argc
, char *argv
[],
135 struct shash
*local_options
,
136 size_t *n_commandsp
);
137 static void parse_command(int argc
, char *argv
[], struct shash
*local_options
,
138 struct vtep_ctl_command
*);
139 static const struct vtep_ctl_command_syntax
*find_command(const char *name
);
140 static void run_prerequisites(struct vtep_ctl_command
[], size_t n_commands
,
142 static void do_vtep_ctl(const char *args
, struct vtep_ctl_command
*, size_t n
,
145 static const struct vtep_ctl_table_class
*get_table(const char *table_name
);
146 static void set_column(const struct vtep_ctl_table_class
*,
147 const struct ovsdb_idl_row
*, const char *arg
,
148 struct ovsdb_symbol_table
*);
150 static bool is_condition_satisfied(const struct vtep_ctl_table_class
*,
151 const struct ovsdb_idl_row
*,
153 struct ovsdb_symbol_table
*);
155 static struct vtep_ctl_lswitch
*find_lswitch(struct vtep_ctl_context
*,
160 main(int argc
, char *argv
[])
162 extern struct vlog_module VLM_reconnect
;
163 struct ovsdb_idl
*idl
;
164 struct vtep_ctl_command
*commands
;
165 struct shash local_options
;
170 set_program_name(argv
[0]);
171 fatal_ignore_sigpipe();
172 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_WARN
);
173 vlog_set_levels(&VLM_reconnect
, VLF_ANY_FACILITY
, VLL_WARN
);
176 /* Log our arguments. This is often valuable for debugging systems. */
177 args
= process_escape_args(argv
);
178 VLOG(might_write_to_db(argv
) ? VLL_INFO
: VLL_DBG
, "Called as %s", args
);
180 /* Parse command line. */
181 shash_init(&local_options
);
182 parse_options(argc
, argv
, &local_options
);
183 commands
= parse_commands(argc
- optind
, argv
+ optind
, &local_options
,
190 /* Initialize IDL. */
191 idl
= the_idl
= ovsdb_idl_create(db
, &vteprec_idl_class
, false, false);
192 run_prerequisites(commands
, n_commands
, idl
);
194 /* Execute the commands.
196 * 'seqno' is the database sequence number for which we last tried to
197 * execute our transaction. There's no point in trying to commit more than
198 * once for any given sequence number, because if the transaction fails
199 * it's because the database changed and we need to obtain an up-to-date
200 * view of the database before we try the transaction again. */
201 seqno
= ovsdb_idl_get_seqno(idl
);
205 if (seqno
!= ovsdb_idl_get_seqno(idl
)) {
206 seqno
= ovsdb_idl_get_seqno(idl
);
207 do_vtep_ctl(args
, commands
, n_commands
, idl
);
210 if (seqno
== ovsdb_idl_get_seqno(idl
)) {
217 static struct option
*
218 find_option(const char *name
, struct option
*options
, size_t n_options
)
222 for (i
= 0; i
< n_options
; i
++) {
223 if (!strcmp(options
[i
].name
, name
)) {
230 static struct option
*
231 add_option(struct option
**optionsp
, size_t *n_optionsp
,
232 size_t *allocated_optionsp
)
234 if (*n_optionsp
>= *allocated_optionsp
) {
235 *optionsp
= x2nrealloc(*optionsp
, allocated_optionsp
,
238 return &(*optionsp
)[(*n_optionsp
)++];
242 parse_options(int argc
, char *argv
[], struct shash
*local_options
)
245 OPT_DB
= UCHAR_MAX
+ 1,
254 static const struct option global_long_options
[] = {
255 {"db", required_argument
, NULL
, OPT_DB
},
256 {"no-syslog", no_argument
, NULL
, OPT_NO_SYSLOG
},
257 {"dry-run", no_argument
, NULL
, OPT_DRY_RUN
},
258 {"oneline", no_argument
, NULL
, OPT_ONELINE
},
259 {"timeout", required_argument
, NULL
, 't'},
260 {"help", no_argument
, NULL
, 'h'},
261 {"version", no_argument
, NULL
, 'V'},
264 STREAM_SSL_LONG_OPTIONS
,
265 {"peer-ca-cert", required_argument
, NULL
, OPT_PEER_CA_CERT
},
268 const int n_global_long_options
= ARRAY_SIZE(global_long_options
) - 1;
269 char *tmp
, *short_options
;
271 const struct vtep_ctl_command_syntax
*p
;
272 struct option
*options
, *o
;
273 size_t allocated_options
;
277 tmp
= long_options_to_short_options(global_long_options
);
278 short_options
= xasprintf("+%s", tmp
);
281 /* We want to parse both global and command-specific options here, but
282 * getopt_long() isn't too convenient for the job. We copy our global
283 * options into a dynamic array, then append all of the command-specific
285 options
= xmemdup(global_long_options
, sizeof global_long_options
);
286 allocated_options
= ARRAY_SIZE(global_long_options
);
287 n_options
= n_global_long_options
;
288 for (p
= all_commands
; p
->name
; p
++) {
290 char *save_ptr
= NULL
;
294 s
= xstrdup(p
->options
);
295 for (name
= strtok_r(s
, ",", &save_ptr
); name
!= NULL
;
296 name
= strtok_r(NULL
, ",", &save_ptr
)) {
300 ovs_assert(name
[0] == '-' && name
[1] == '-' && name
[2]);
303 equals
= strchr(name
, '=');
305 has_arg
= required_argument
;
308 has_arg
= no_argument
;
311 o
= find_option(name
, options
, n_options
);
313 ovs_assert(o
- options
>= n_global_long_options
);
314 ovs_assert(o
->has_arg
== has_arg
);
316 o
= add_option(&options
, &n_options
, &allocated_options
);
317 o
->name
= xstrdup(name
);
318 o
->has_arg
= has_arg
;
327 o
= add_option(&options
, &n_options
, &allocated_options
);
328 memset(o
, 0, sizeof *o
);
330 table_style
.format
= TF_LIST
;
336 c
= getopt_long(argc
, argv
, short_options
, options
, &idx
);
351 vlog_set_levels(&VLM_vtep_ctl
, VLF_SYSLOG
, VLL_WARN
);
359 if (shash_find(local_options
, options
[idx
].name
)) {
360 vtep_ctl_fatal("'%s' option specified multiple times",
363 shash_add_nocopy(local_options
,
364 xasprintf("--%s", options
[idx
].name
),
365 optarg
? xstrdup(optarg
) : NULL
);
372 ovs_print_version(0, 0);
373 printf("DB Schema %s\n", vteprec_get_db_version());
377 timeout
= strtoul(optarg
, NULL
, 10);
379 vtep_ctl_fatal("value %s on -t or --timeout is invalid",
385 TABLE_OPTION_HANDLERS(&table_style
)
387 STREAM_SSL_OPTION_HANDLERS
389 case OPT_PEER_CA_CERT
:
390 stream_ssl_set_peer_ca_cert_file(optarg
);
406 for (i
= n_global_long_options
; options
[i
].name
; i
++) {
407 free(CONST_CAST(char *, options
[i
].name
));
412 static struct vtep_ctl_command
*
413 parse_commands(int argc
, char *argv
[], struct shash
*local_options
,
416 struct vtep_ctl_command
*commands
;
417 size_t n_commands
, allocated_commands
;
421 n_commands
= allocated_commands
= 0;
423 for (start
= i
= 0; i
<= argc
; i
++) {
424 if (i
== argc
|| !strcmp(argv
[i
], "--")) {
426 if (n_commands
>= allocated_commands
) {
427 struct vtep_ctl_command
*c
;
429 commands
= x2nrealloc(commands
, &allocated_commands
,
431 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
432 shash_moved(&c
->options
);
435 parse_command(i
- start
, &argv
[start
], local_options
,
436 &commands
[n_commands
++]);
437 } else if (!shash_is_empty(local_options
)) {
438 vtep_ctl_fatal("missing command name (use --help for help)");
444 vtep_ctl_fatal("missing command name (use --help for help)");
446 *n_commandsp
= n_commands
;
451 parse_command(int argc
, char *argv
[], struct shash
*local_options
,
452 struct vtep_ctl_command
*command
)
454 const struct vtep_ctl_command_syntax
*p
;
455 struct shash_node
*node
;
459 shash_init(&command
->options
);
460 shash_swap(local_options
, &command
->options
);
461 for (i
= 0; i
< argc
; i
++) {
462 const char *option
= argv
[i
];
466 if (option
[0] != '-') {
470 equals
= strchr(option
, '=');
472 key
= xmemdup0(option
, equals
- option
);
473 value
= xstrdup(equals
+ 1);
475 key
= xstrdup(option
);
479 if (shash_find(&command
->options
, key
)) {
480 vtep_ctl_fatal("'%s' option specified multiple times", argv
[i
]);
482 shash_add_nocopy(&command
->options
, key
, value
);
485 vtep_ctl_fatal("missing command name (use --help for help)");
488 p
= find_command(argv
[i
]);
490 vtep_ctl_fatal("unknown command '%s'; use --help for help", argv
[i
]);
493 SHASH_FOR_EACH (node
, &command
->options
) {
494 const char *s
= strstr(p
->options
, node
->name
);
495 int end
= s
? s
[strlen(node
->name
)] : EOF
;
497 if (end
!= '=' && end
!= ',' && end
!= ' ' && end
!= '\0') {
498 vtep_ctl_fatal("'%s' command has no '%s' option",
499 argv
[i
], node
->name
);
501 if ((end
== '=') != (node
->data
!= NULL
)) {
503 vtep_ctl_fatal("missing argument to '%s' option on '%s' "
504 "command", node
->name
, argv
[i
]);
506 vtep_ctl_fatal("'%s' option on '%s' does not accept an "
507 "argument", node
->name
, argv
[i
]);
512 n_arg
= argc
- i
- 1;
513 if (n_arg
< p
->min_args
) {
514 vtep_ctl_fatal("'%s' command requires at least %d arguments",
515 p
->name
, p
->min_args
);
516 } else if (n_arg
> p
->max_args
) {
519 for (j
= i
+ 1; j
< argc
; j
++) {
520 if (argv
[j
][0] == '-') {
521 vtep_ctl_fatal("'%s' command takes at most %d arguments "
522 "(note that options must precede command "
523 "names and follow a \"--\" argument)",
524 p
->name
, p
->max_args
);
528 vtep_ctl_fatal("'%s' command takes at most %d arguments",
529 p
->name
, p
->max_args
);
533 command
->argc
= n_arg
+ 1;
534 command
->argv
= &argv
[i
];
537 /* Returns the "struct vtep_ctl_command_syntax" for a given command 'name', or a
538 * null pointer if there is none. */
539 static const struct vtep_ctl_command_syntax
*
540 find_command(const char *name
)
542 static struct shash commands
= SHASH_INITIALIZER(&commands
);
544 if (shash_is_empty(&commands
)) {
545 const struct vtep_ctl_command_syntax
*p
;
547 for (p
= all_commands
; p
->name
; p
++) {
548 shash_add_assert(&commands
, p
->name
, p
);
552 return shash_find_data(&commands
, name
);
556 vtep_ctl_fatal(const char *format
, ...)
561 va_start(args
, format
);
562 message
= xvasprintf(format
, args
);
565 vlog_set_levels(&VLM_vtep_ctl
, VLF_CONSOLE
, VLL_OFF
);
566 VLOG_ERR("%s", message
);
567 ovs_error(0, "%s", message
);
568 vtep_ctl_exit(EXIT_FAILURE
);
571 /* Frees the current transaction and the underlying IDL and then calls
574 * Freeing the transaction and the IDL is not strictly necessary, but it makes
575 * for a clean memory leak report from valgrind in the normal case. That makes
576 * it easier to notice real memory leaks. */
578 vtep_ctl_exit(int status
)
581 ovsdb_idl_txn_abort(the_idl_txn
);
582 ovsdb_idl_txn_destroy(the_idl_txn
);
584 ovsdb_idl_destroy(the_idl
);
592 %s: VTEP configuration utility\n\
593 usage: %s [OPTIONS] COMMAND [ARG...]\n\
596 get-manager print the managers\n\
597 del-manager delete the managers\n\
598 set-manager TARGET... set the list of managers to TARGET...\n\
600 Physical Switch commands:\n\
601 add-ps PS create a new physical switch named PS\n\
602 del-ps PS delete PS and all of its ports\n\
603 list-ps print the names of all the physical switches\n\
604 ps-exists PS exit 2 if PS does not exist\n\
607 list-ports PS print the names of all the ports on PS\n\
608 add-port PS PORT add network device PORT to PS\n\
609 del-port PS PORT delete PORT from PS\n\
611 Logical Switch commands:\n\
612 add-ls LS create a new logical switch named LS\n\
613 del-ls LS delete LS and all of its ports\n\
614 list-ls print the names of all the logical switches\n\
615 ls-exists LS exit 2 if LS does not exist\n\
616 bind-ls PS PORT VLAN LS bind LS to VLAN on PORT\n\
617 unbind-ls PS PORT VLAN unbind logical switch on VLAN from PORT\n\
618 list-bindings PS PORT list bindings for PORT on PS\n\
620 MAC binding commands:\n\
621 add-ucast-local LS MAC [ENCAP] IP add ucast local entry in LS\n\
622 del-ucast-local LS MAC del ucast local entry from LS\n\
623 add-mcast-local LS MAC [ENCAP] IP add mcast local entry in LS\n\
624 del-mcast-local LS MAC [ENCAP] IP del mcast local entry from LS\n\
625 clear-local-macs LS clear local mac entries\n\
626 list-local-macs LS list local mac entries\n\
627 add-ucast-remote LS MAC [ENCAP] IP add ucast remote entry in LS\n\
628 del-ucast-remote LS MAC del ucast remote entry from LS\n\
629 add-mcast-remote LS MAC [ENCAP] IP add mcast remote entry in LS\n\
630 del-mcast-remote LS MAC [ENCAP] IP del mcast remote entry from LS\n\
631 clear-remote-macs LS clear remote mac entries\n\
632 list-remote-macs LS list remote mac entries\n\
634 Database commands:\n\
635 list TBL [REC] list RECord (or all records) in TBL\n\
636 find TBL CONDITION... list records satisfying CONDITION in TBL\n\
637 get TBL REC COL[:KEY] print values of COLumns in RECord in TBL\n\
638 set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
639 add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
640 remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
641 clear TBL REC COL clear values from COLumn in RECord in TBL\n\
642 create TBL COL[:KEY]=VALUE create and initialize new record\n\
643 destroy TBL REC delete RECord from TBL\n\
644 wait-until TBL REC [COL[:KEY]=VALUE] wait until condition is true\n\
645 Potentially unsafe database commands require --force option.\n\
648 --db=DATABASE connect to DATABASE\n\
650 -t, --timeout=SECS wait at most SECS seconds\n\
651 --dry-run do not commit changes to database\n\
652 --oneline print exactly one line of output per command\n",
653 program_name
, program_name
, default_db());
656 --no-syslog equivalent to --verbose=vtep_ctl:syslog:warn\n");
657 stream_usage("database", true, true, false);
660 -h, --help display this help message\n\
661 -V, --version display version information\n");
670 def
= xasprintf("unix:%s/db.sock", ovs_rundir());
675 /* Returns true if it looks like this set of arguments might modify the
676 * database, otherwise false. (Not very smart, so it's prone to false
679 might_write_to_db(char **argv
)
681 for (; *argv
; argv
++) {
682 const struct vtep_ctl_command_syntax
*p
= find_command(*argv
);
683 if (p
&& p
->mode
== RW
) {
690 struct vtep_ctl_context
{
694 struct shash options
;
696 /* Modifiable state. */
699 struct ovsdb_idl
*idl
;
700 struct ovsdb_idl_txn
*txn
;
701 struct ovsdb_symbol_table
*symtab
;
702 const struct vteprec_global
*vtep_global
;
705 /* A cache of the contents of the database.
707 * A command that needs to use any of this information must first
708 * call vtep_ctl_context_populate_cache(). A command that changes
709 * anything that could invalidate the cache must either call
710 * vtep_ctl_context_invalidate_cache() or manually update the cache
711 * to maintain its correctness. */
713 struct shash pswitches
; /* Maps from physical switch name to
714 * struct vtep_ctl_pswitch. */
715 struct shash ports
; /* Maps from port name to struct vtep_ctl_port. */
717 struct shash lswitches
; /* Maps from logical switch name to
718 * struct vtep_ctl_lswitch. */
719 struct shash plocs
; /* Maps from "<encap>+<dst_ip>" to
720 * struct vteprec_physical_locator. */
722 /* A command may set this member to true if some prerequisite is not met
723 * and the caller should wait for something to change and then retry. */
727 struct vtep_ctl_pswitch
{
728 const struct vteprec_physical_switch
*ps_cfg
;
730 struct list ports
; /* Contains "struct vteprec_physical_port"s. */
733 struct vtep_ctl_port
{
734 struct list ports_node
; /* In struct vtep_ctl_pswitch's 'ports' list. */
735 const struct vteprec_physical_port
*port_cfg
;
736 struct vtep_ctl_pswitch
*ps
;
737 struct shash bindings
; /* Maps from vlan to vtep_ctl_lswitch. */
740 struct vtep_ctl_lswitch
{
741 const struct vteprec_logical_switch
*ls_cfg
;
743 struct shash ucast_local
; /* Maps from mac to vteprec_ucast_macs_local. */
744 struct shash ucast_remote
; /* Maps from mac to vteprec_ucast_macs_remote.*/
745 struct shash mcast_local
; /* Maps from mac to vtep_ctl_mcast_mac. */
746 struct shash mcast_remote
; /* Maps from mac to vtep_ctl_mcast_mac. */
749 struct vtep_ctl_mcast_mac
{
750 const struct vteprec_mcast_macs_local
*local_cfg
;
751 const struct vteprec_mcast_macs_remote
*remote_cfg
;
753 const struct vteprec_physical_locator_set
*ploc_set_cfg
;
754 struct list locators
; /* Contains 'vtep_ctl_ploc's. */
757 struct vtep_ctl_ploc
{
758 struct list locators_node
; /* In struct vtep_ctl_ploc_set's 'locators'
760 const struct vteprec_physical_locator
*ploc_cfg
;
764 verify_ports(struct vtep_ctl_context
*ctx
)
766 if (!ctx
->verified_ports
) {
767 const struct vteprec_physical_switch
*ps
;
769 vteprec_global_verify_switches(ctx
->vtep_global
);
770 VTEPREC_PHYSICAL_SWITCH_FOR_EACH (ps
, ctx
->idl
) {
771 vteprec_physical_switch_verify_ports(ps
);
774 ctx
->verified_ports
= true;
778 static struct vtep_ctl_port
*
779 add_port_to_cache(struct vtep_ctl_context
*ctx
,
780 struct vtep_ctl_pswitch
*ps
,
781 struct vteprec_physical_port
*port_cfg
)
783 char *cache_name
= xasprintf("%s+%s", ps
->name
, port_cfg
->name
);
784 struct vtep_ctl_port
*port
;
786 port
= xmalloc(sizeof *port
);
787 list_push_back(&ps
->ports
, &port
->ports_node
);
788 port
->port_cfg
= port_cfg
;
790 shash_add(&ctx
->ports
, cache_name
, port
);
792 shash_init(&port
->bindings
);
798 del_cached_port(struct vtep_ctl_context
*ctx
, struct vtep_ctl_port
*port
)
800 char *cache_name
= xasprintf("%s+%s", port
->ps
->name
, port
->port_cfg
->name
);
802 list_remove(&port
->ports_node
);
803 shash_find_and_delete(&ctx
->ports
, port
->port_cfg
->name
);
804 vteprec_physical_port_delete(port
->port_cfg
);
809 static struct vtep_ctl_pswitch
*
810 add_pswitch_to_cache(struct vtep_ctl_context
*ctx
,
811 struct vteprec_physical_switch
*ps_cfg
)
813 struct vtep_ctl_pswitch
*ps
= xmalloc(sizeof *ps
);
815 ps
->name
= xstrdup(ps_cfg
->name
);
816 list_init(&ps
->ports
);
817 shash_add(&ctx
->pswitches
, ps
->name
, ps
);
822 vtep_delete_pswitch(const struct vteprec_global
*vtep_global
,
823 const struct vteprec_physical_switch
*ps
)
825 struct vteprec_physical_switch
**pswitches
;
828 pswitches
= xmalloc(sizeof *vtep_global
->switches
829 * vtep_global
->n_switches
);
830 for (i
= n
= 0; i
< vtep_global
->n_switches
; i
++) {
831 if (vtep_global
->switches
[i
] != ps
) {
832 pswitches
[n
++] = vtep_global
->switches
[i
];
835 vteprec_global_set_switches(vtep_global
, pswitches
, n
);
840 del_cached_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
842 ovs_assert(list_is_empty(&ps
->ports
));
844 vteprec_physical_switch_delete(ps
->ps_cfg
);
845 vtep_delete_pswitch(ctx
->vtep_global
, ps
->ps_cfg
);
847 shash_find_and_delete(&ctx
->pswitches
, ps
->name
);
852 static struct vtep_ctl_lswitch
*
853 add_lswitch_to_cache(struct vtep_ctl_context
*ctx
,
854 const struct vteprec_logical_switch
*ls_cfg
)
856 struct vtep_ctl_lswitch
*ls
= xmalloc(sizeof *ls
);
858 ls
->name
= xstrdup(ls_cfg
->name
);
859 shash_add(&ctx
->lswitches
, ls
->name
, ls
);
860 shash_init(&ls
->ucast_local
);
861 shash_init(&ls
->ucast_remote
);
862 shash_init(&ls
->mcast_local
);
863 shash_init(&ls
->mcast_remote
);
868 del_cached_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
871 vteprec_logical_switch_delete(ls
->ls_cfg
);
873 shash_find_and_delete(&ctx
->lswitches
, ls
->name
);
879 commit_ls_bindings(struct vtep_ctl_port
*port
)
881 struct vteprec_logical_switch
**binding_values
;
882 int64_t *binding_keys
;
884 struct shash_node
*node
;
887 n_bindings
= shash_count(&port
->bindings
);
888 binding_keys
= xmalloc(n_bindings
* sizeof *binding_keys
);
889 binding_values
= xmalloc(n_bindings
* sizeof *binding_values
);
892 SHASH_FOR_EACH(node
, &port
->bindings
) {
893 struct vtep_ctl_lswitch
*ls_entry
= node
->data
;
895 binding_keys
[i
] = strtoll(node
->name
, NULL
, 0);
896 binding_values
[i
] = (struct vteprec_logical_switch
*)ls_entry
->ls_cfg
;
900 vteprec_physical_port_set_vlan_bindings(port
->port_cfg
,
901 binding_keys
, binding_values
,
903 free(binding_values
);
908 add_ls_binding_to_cache(struct vtep_ctl_port
*port
,
910 struct vtep_ctl_lswitch
*ls
)
912 if (shash_find(&port
->bindings
, vlan
)) {
913 vtep_ctl_fatal("multiple bindings for vlan %s", vlan
);
916 shash_add(&port
->bindings
, vlan
, ls
);
920 del_cached_ls_binding(struct vtep_ctl_port
*port
, const char *vlan
)
922 if (!shash_find(&port
->bindings
, vlan
)) {
923 vtep_ctl_fatal("no binding for vlan %s", vlan
);
926 shash_find_and_delete(&port
->bindings
, vlan
);
929 static struct vteprec_physical_locator
*
930 find_ploc(struct vtep_ctl_context
*ctx
, const char *encap
,
933 struct vteprec_physical_locator
*ploc
;
934 char *name
= xasprintf("%s+%s", encap
, dst_ip
);
936 ovs_assert(ctx
->cache_valid
);
938 ploc
= shash_find_data(&ctx
->plocs
, name
);
945 add_ploc_to_cache(struct vtep_ctl_context
*ctx
,
946 struct vteprec_physical_locator
*ploc
)
948 char *name
= xasprintf("%s+%s", ploc
->encapsulation_type
, ploc
->dst_ip
);
949 struct vteprec_physical_locator
*orig_ploc
;
951 orig_ploc
= find_ploc(ctx
, ploc
->encapsulation_type
, ploc
->dst_ip
);
953 shash_add(&ctx
->plocs
, name
, ploc
);
960 add_ploc_to_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
961 struct vteprec_physical_locator
*ploc_cfg
)
963 struct vtep_ctl_ploc
*ploc
;
965 ploc
= xmalloc(sizeof *ploc
);
966 ploc
->ploc_cfg
= ploc_cfg
;
967 list_push_back(&mcast_mac
->locators
, &ploc
->locators_node
);
971 del_ploc_from_mcast_mac(struct vtep_ctl_mcast_mac
*mcast_mac
,
972 struct vteprec_physical_locator
*ploc_cfg
)
974 struct vtep_ctl_ploc
*ploc
;
976 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
977 if (ploc
->ploc_cfg
== ploc_cfg
) {
978 list_remove(&ploc
->locators_node
);
985 static struct vtep_ctl_mcast_mac
*
986 add_mcast_mac_to_cache(struct vtep_ctl_context
*ctx
,
987 struct vtep_ctl_lswitch
*ls
, const char *mac
,
988 struct vteprec_physical_locator_set
*ploc_set_cfg
,
991 struct vtep_ctl_mcast_mac
*mcast_mac
;
992 struct shash
*mcast_shash
;
995 mcast_mac
= xmalloc(sizeof *mcast_mac
);
996 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
998 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
999 list_init(&mcast_mac
->locators
);
1000 shash_add(mcast_shash
, mac
, mcast_mac
);
1002 for (i
= 0; i
< ploc_set_cfg
->n_locators
; i
++) {
1003 struct vteprec_physical_locator
*ploc_cfg
;
1005 ploc_cfg
= ploc_set_cfg
->locators
[i
];
1006 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1007 add_ploc_to_cache(ctx
, ploc_cfg
);
1014 vtep_ctl_context_invalidate_cache(struct vtep_ctl_context
*ctx
)
1016 struct shash_node
*node
;
1018 if (!ctx
->cache_valid
) {
1021 ctx
->cache_valid
= false;
1023 SHASH_FOR_EACH (node
, &ctx
->pswitches
) {
1024 struct vtep_ctl_pswitch
*ps
= node
->data
;
1028 shash_destroy(&ctx
->pswitches
);
1030 SHASH_FOR_EACH (node
, &ctx
->ports
) {
1031 struct vtep_ctl_port
*port
= node
->data
;
1032 shash_destroy(&port
->bindings
);
1034 shash_destroy_free_data(&ctx
->ports
);
1036 SHASH_FOR_EACH (node
, &ctx
->lswitches
) {
1037 struct vtep_ctl_lswitch
*ls
= node
->data
;
1038 struct shash_node
*node2
, *next_node2
;
1040 shash_destroy(&ls
->ucast_local
);
1041 shash_destroy(&ls
->ucast_remote
);
1043 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_local
) {
1044 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
1045 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
1047 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
1048 &mcast_mac
->locators
) {
1053 shash_destroy(&ls
->mcast_local
);
1055 SHASH_FOR_EACH_SAFE (node2
, next_node2
, &ls
->mcast_remote
) {
1056 struct vtep_ctl_mcast_mac
*mcast_mac
= node2
->data
;
1057 struct vtep_ctl_ploc
*ploc
, *next_ploc
;
1059 LIST_FOR_EACH_SAFE (ploc
, next_ploc
, locators_node
,
1060 &mcast_mac
->locators
) {
1065 shash_destroy(&ls
->mcast_remote
);
1070 shash_destroy(&ctx
->lswitches
);
1071 shash_destroy(&ctx
->plocs
);
1075 pre_get_info(struct vtep_ctl_context
*ctx
)
1077 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_switches
);
1079 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_name
);
1080 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_switch_col_ports
);
1082 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_name
);
1083 ovsdb_idl_add_column(ctx
->idl
, &vteprec_physical_port_col_vlan_bindings
);
1085 ovsdb_idl_add_column(ctx
->idl
, &vteprec_logical_switch_col_name
);
1087 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_MAC
);
1088 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_local_col_locator
);
1089 ovsdb_idl_add_column(ctx
->idl
,
1090 &vteprec_ucast_macs_local_col_logical_switch
);
1092 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_MAC
);
1093 ovsdb_idl_add_column(ctx
->idl
, &vteprec_ucast_macs_remote_col_locator
);
1094 ovsdb_idl_add_column(ctx
->idl
,
1095 &vteprec_ucast_macs_remote_col_logical_switch
);
1097 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_local_col_MAC
);
1098 ovsdb_idl_add_column(ctx
->idl
,
1099 &vteprec_mcast_macs_local_col_locator_set
);
1100 ovsdb_idl_add_column(ctx
->idl
,
1101 &vteprec_mcast_macs_local_col_logical_switch
);
1103 ovsdb_idl_add_column(ctx
->idl
, &vteprec_mcast_macs_remote_col_MAC
);
1104 ovsdb_idl_add_column(ctx
->idl
,
1105 &vteprec_mcast_macs_remote_col_locator_set
);
1106 ovsdb_idl_add_column(ctx
->idl
,
1107 &vteprec_mcast_macs_remote_col_logical_switch
);
1109 ovsdb_idl_add_column(ctx
->idl
,
1110 &vteprec_physical_locator_set_col_locators
);
1112 ovsdb_idl_add_column(ctx
->idl
,
1113 &vteprec_physical_locator_col_dst_ip
);
1114 ovsdb_idl_add_column(ctx
->idl
,
1115 &vteprec_physical_locator_col_encapsulation_type
);
1119 vtep_ctl_context_populate_cache(struct vtep_ctl_context
*ctx
)
1121 const struct vteprec_global
*vtep_global
= ctx
->vtep_global
;
1122 const struct vteprec_logical_switch
*ls_cfg
;
1123 const struct vteprec_ucast_macs_local
*ucast_local_cfg
;
1124 const struct vteprec_ucast_macs_remote
*ucast_remote_cfg
;
1125 const struct vteprec_mcast_macs_local
*mcast_local_cfg
;
1126 const struct vteprec_mcast_macs_remote
*mcast_remote_cfg
;
1127 struct sset pswitches
, ports
, lswitches
;
1130 if (ctx
->cache_valid
) {
1131 /* Cache is already populated. */
1134 ctx
->cache_valid
= true;
1135 shash_init(&ctx
->pswitches
);
1136 shash_init(&ctx
->ports
);
1137 shash_init(&ctx
->lswitches
);
1138 shash_init(&ctx
->plocs
);
1140 sset_init(&pswitches
);
1142 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1143 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1144 struct vtep_ctl_pswitch
*ps
;
1147 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1148 VLOG_WARN("%s: database contains duplicate physical switch name",
1152 ps
= add_pswitch_to_cache(ctx
, ps_cfg
);
1157 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1158 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1160 if (!sset_add(&ports
, port_cfg
->name
)) {
1161 /* Duplicate port name. (We will warn about that later.) */
1166 sset_destroy(&pswitches
);
1167 sset_destroy(&ports
);
1169 sset_init(&lswitches
);
1170 VTEPREC_LOGICAL_SWITCH_FOR_EACH (ls_cfg
, ctx
->idl
) {
1171 if (!sset_add(&lswitches
, ls_cfg
->name
)) {
1172 VLOG_WARN("%s: database contains duplicate logical switch name",
1176 add_lswitch_to_cache(ctx
, ls_cfg
);
1178 sset_destroy(&lswitches
);
1180 VTEPREC_UCAST_MACS_LOCAL_FOR_EACH (ucast_local_cfg
, ctx
->idl
) {
1181 struct vtep_ctl_lswitch
*ls
;
1183 if (!ucast_local_cfg
->logical_switch
) {
1186 ls
= find_lswitch(ctx
, ucast_local_cfg
->logical_switch
->name
, false);
1191 if (ucast_local_cfg
->locator
) {
1192 add_ploc_to_cache(ctx
, ucast_local_cfg
->locator
);
1195 shash_add(&ls
->ucast_local
, ucast_local_cfg
->MAC
, ucast_local_cfg
);
1198 VTEPREC_UCAST_MACS_REMOTE_FOR_EACH (ucast_remote_cfg
, ctx
->idl
) {
1199 struct vtep_ctl_lswitch
*ls
;
1201 if (!ucast_remote_cfg
->logical_switch
) {
1204 ls
= find_lswitch(ctx
, ucast_remote_cfg
->logical_switch
->name
, false);
1209 if (ucast_remote_cfg
->locator
) {
1210 add_ploc_to_cache(ctx
, ucast_remote_cfg
->locator
);
1213 shash_add(&ls
->ucast_remote
, ucast_remote_cfg
->MAC
, ucast_remote_cfg
);
1216 VTEPREC_MCAST_MACS_LOCAL_FOR_EACH (mcast_local_cfg
, ctx
->idl
) {
1217 struct vtep_ctl_mcast_mac
*mcast_mac
;
1218 struct vtep_ctl_lswitch
*ls
;
1220 if (!mcast_local_cfg
->logical_switch
) {
1223 ls
= find_lswitch(ctx
, mcast_local_cfg
->logical_switch
->name
, false);
1228 mcast_mac
= add_mcast_mac_to_cache(ctx
, ls
, mcast_local_cfg
->MAC
,
1229 mcast_local_cfg
->locator_set
,
1231 mcast_mac
->local_cfg
= mcast_local_cfg
;
1234 VTEPREC_MCAST_MACS_REMOTE_FOR_EACH (mcast_remote_cfg
, ctx
->idl
) {
1235 struct vtep_ctl_mcast_mac
*mcast_mac
;
1236 struct vtep_ctl_lswitch
*ls
;
1238 if (!mcast_remote_cfg
->logical_switch
) {
1241 ls
= find_lswitch(ctx
, mcast_remote_cfg
->logical_switch
->name
, false);
1246 mcast_mac
= add_mcast_mac_to_cache(ctx
, ls
, mcast_remote_cfg
->MAC
,
1247 mcast_remote_cfg
->locator_set
,
1249 mcast_mac
->remote_cfg
= mcast_remote_cfg
;
1252 sset_init(&pswitches
);
1253 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1254 struct vteprec_physical_switch
*ps_cfg
= vtep_global
->switches
[i
];
1255 struct vtep_ctl_pswitch
*ps
;
1258 if (!sset_add(&pswitches
, ps_cfg
->name
)) {
1261 ps
= shash_find_data(&ctx
->pswitches
, ps_cfg
->name
);
1262 for (j
= 0; j
< ps_cfg
->n_ports
; j
++) {
1263 struct vteprec_physical_port
*port_cfg
= ps_cfg
->ports
[j
];
1264 struct vtep_ctl_port
*port
;
1267 port
= shash_find_data(&ctx
->ports
, port_cfg
->name
);
1269 if (port_cfg
== port
->port_cfg
) {
1270 VLOG_WARN("%s: port is in multiple physical switches "
1272 port_cfg
->name
, ps
->name
, port
->ps
->name
);
1274 /* Log as an error because this violates the database's
1275 * uniqueness constraints, so the database server shouldn't
1276 * have allowed it. */
1277 VLOG_ERR("%s: database contains duplicate port name",
1283 port
= add_port_to_cache(ctx
, ps
, port_cfg
);
1285 for (k
= 0; k
< port_cfg
->n_vlan_bindings
; k
++) {
1286 struct vteprec_logical_switch
*ls_cfg
;
1287 struct vtep_ctl_lswitch
*ls
;
1290 vlan
= xasprintf("%"PRId64
, port_cfg
->key_vlan_bindings
[k
]);
1291 if (shash_find(&port
->bindings
, vlan
)) {
1292 vtep_ctl_fatal("multiple bindings for vlan %s", vlan
);
1295 ls_cfg
= port_cfg
->value_vlan_bindings
[k
];
1296 ls
= find_lswitch(ctx
, ls_cfg
->name
, true);
1298 shash_add_nocopy(&port
->bindings
, vlan
, ls
);
1302 sset_destroy(&pswitches
);
1305 static struct vtep_ctl_pswitch
*
1306 find_pswitch(struct vtep_ctl_context
*ctx
, const char *name
, bool must_exist
)
1308 struct vtep_ctl_pswitch
*ps
;
1310 ovs_assert(ctx
->cache_valid
);
1312 ps
= shash_find_data(&ctx
->pswitches
, name
);
1313 if (must_exist
&& !ps
) {
1314 vtep_ctl_fatal("no physical switch named %s", name
);
1316 vteprec_global_verify_switches(ctx
->vtep_global
);
1320 static struct vtep_ctl_port
*
1321 find_port(struct vtep_ctl_context
*ctx
, const char *ps_name
,
1322 const char *port_name
, bool must_exist
)
1324 char *cache_name
= xasprintf("%s+%s", ps_name
, port_name
);
1325 struct vtep_ctl_port
*port
;
1327 ovs_assert(ctx
->cache_valid
);
1329 port
= shash_find_data(&ctx
->ports
, cache_name
);
1330 if (port
&& !strcmp(port_name
, port
->ps
->name
)) {
1334 if (must_exist
&& !port
) {
1335 vtep_ctl_fatal("no port named %s", port_name
);
1342 pswitch_insert_port(const struct vteprec_physical_switch
*ps
,
1343 struct vteprec_physical_port
*port
)
1345 struct vteprec_physical_port
**ports
;
1348 ports
= xmalloc(sizeof *ps
->ports
* (ps
->n_ports
+ 1));
1349 for (i
= 0; i
< ps
->n_ports
; i
++) {
1350 ports
[i
] = ps
->ports
[i
];
1352 ports
[ps
->n_ports
] = port
;
1353 vteprec_physical_switch_set_ports(ps
, ports
, ps
->n_ports
+ 1);
1358 pswitch_delete_port(const struct vteprec_physical_switch
*ps
,
1359 const struct vteprec_physical_port
*port
)
1361 struct vteprec_physical_port
**ports
;
1364 ports
= xmalloc(sizeof *ps
->ports
* ps
->n_ports
);
1365 for (i
= n
= 0; i
< ps
->n_ports
; i
++) {
1366 if (ps
->ports
[i
] != port
) {
1367 ports
[n
++] = ps
->ports
[i
];
1370 vteprec_physical_switch_set_ports(ps
, ports
, n
);
1375 vtep_insert_pswitch(const struct vteprec_global
*vtep_global
,
1376 struct vteprec_physical_switch
*ps
)
1378 struct vteprec_physical_switch
**pswitches
;
1381 pswitches
= xmalloc(sizeof *vtep_global
->switches
1382 * (vtep_global
->n_switches
+ 1));
1383 for (i
= 0; i
< vtep_global
->n_switches
; i
++) {
1384 pswitches
[i
] = vtep_global
->switches
[i
];
1386 pswitches
[vtep_global
->n_switches
] = ps
;
1387 vteprec_global_set_switches(vtep_global
, pswitches
,
1388 vtep_global
->n_switches
+ 1);
1393 cmd_add_ps(struct vtep_ctl_context
*ctx
)
1395 const char *ps_name
= ctx
->argv
[1];
1396 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1397 struct vteprec_physical_switch
*ps
;
1399 vtep_ctl_context_populate_cache(ctx
);
1400 if (find_pswitch(ctx
, ps_name
, false)) {
1402 vtep_ctl_fatal("cannot create physical switch %s because it "
1403 "already exists", ps_name
);
1408 ps
= vteprec_physical_switch_insert(ctx
->txn
);
1409 vteprec_physical_switch_set_name(ps
, ps_name
);
1411 vtep_insert_pswitch(ctx
->vtep_global
, ps
);
1413 vtep_ctl_context_invalidate_cache(ctx
);
1417 del_port(struct vtep_ctl_context
*ctx
, struct vtep_ctl_port
*port
)
1419 pswitch_delete_port(port
->ps
->ps_cfg
, port
->port_cfg
);
1420 del_cached_port(ctx
, port
);
1424 del_pswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_pswitch
*ps
)
1426 struct vtep_ctl_port
*port
, *next_port
;
1428 LIST_FOR_EACH_SAFE (port
, next_port
, ports_node
, &ps
->ports
) {
1429 del_port(ctx
, port
);
1432 del_cached_pswitch(ctx
, ps
);
1436 cmd_del_ps(struct vtep_ctl_context
*ctx
)
1438 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1439 struct vtep_ctl_pswitch
*ps
;
1441 vtep_ctl_context_populate_cache(ctx
);
1442 ps
= find_pswitch(ctx
, ctx
->argv
[1], must_exist
);
1444 del_pswitch(ctx
, ps
);
1449 output_sorted(struct svec
*svec
, struct ds
*output
)
1455 SVEC_FOR_EACH (i
, name
, svec
) {
1456 ds_put_format(output
, "%s\n", name
);
1461 cmd_list_ps(struct vtep_ctl_context
*ctx
)
1463 struct shash_node
*node
;
1464 struct svec pswitches
;
1466 vtep_ctl_context_populate_cache(ctx
);
1468 svec_init(&pswitches
);
1469 SHASH_FOR_EACH (node
, &ctx
->pswitches
) {
1470 struct vtep_ctl_pswitch
*ps
= node
->data
;
1472 svec_add(&pswitches
, ps
->name
);
1474 output_sorted(&pswitches
, &ctx
->output
);
1475 svec_destroy(&pswitches
);
1479 cmd_ps_exists(struct vtep_ctl_context
*ctx
)
1481 vtep_ctl_context_populate_cache(ctx
);
1482 if (!find_pswitch(ctx
, ctx
->argv
[1], false)) {
1488 cmd_list_ports(struct vtep_ctl_context
*ctx
)
1490 struct vtep_ctl_pswitch
*ps
;
1491 struct vtep_ctl_port
*port
;
1494 vtep_ctl_context_populate_cache(ctx
);
1495 ps
= find_pswitch(ctx
, ctx
->argv
[1], true);
1496 vteprec_physical_switch_verify_ports(ps
->ps_cfg
);
1499 LIST_FOR_EACH (port
, ports_node
, &ps
->ports
) {
1500 if (strcmp(port
->port_cfg
->name
, ps
->name
)) {
1501 svec_add(&ports
, port
->port_cfg
->name
);
1504 output_sorted(&ports
, &ctx
->output
);
1505 svec_destroy(&ports
);
1509 add_port(struct vtep_ctl_context
*ctx
, const char *ps_name
,
1510 const char *port_name
, bool may_exist
)
1512 struct vtep_ctl_port
*vtep_ctl_port
;
1513 struct vtep_ctl_pswitch
*ps
;
1514 struct vteprec_physical_port
*port
;
1516 vtep_ctl_context_populate_cache(ctx
);
1518 vtep_ctl_port
= find_port(ctx
, ps_name
, port_name
, false);
1519 if (vtep_ctl_port
) {
1521 vtep_ctl_fatal("cannot create a port named %s on %s because a "
1522 "port with that name already exists",
1523 port_name
, ps_name
);
1528 ps
= find_pswitch(ctx
, ps_name
, true);
1530 port
= vteprec_physical_port_insert(ctx
->txn
);
1531 vteprec_physical_port_set_name(port
, port_name
);
1533 pswitch_insert_port(ps
->ps_cfg
, port
);
1535 add_port_to_cache(ctx
, ps
, port
);
1539 cmd_add_port(struct vtep_ctl_context
*ctx
)
1541 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1542 add_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], may_exist
);
1546 cmd_del_port(struct vtep_ctl_context
*ctx
)
1548 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1549 struct vtep_ctl_port
*port
;
1551 vtep_ctl_context_populate_cache(ctx
);
1553 port
= find_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], must_exist
);
1555 if (ctx
->argc
== 3) {
1556 struct vtep_ctl_pswitch
*ps
;
1558 ps
= find_pswitch(ctx
, ctx
->argv
[1], true);
1559 if (port
->ps
!= ps
) {
1560 vtep_ctl_fatal("physical switch %s does not have a port %s",
1561 ctx
->argv
[1], ctx
->argv
[2]);
1565 del_port(ctx
, port
);
1569 static struct vtep_ctl_lswitch
*
1570 find_lswitch(struct vtep_ctl_context
*ctx
, const char *name
, bool must_exist
)
1572 struct vtep_ctl_lswitch
*ls
;
1574 ovs_assert(ctx
->cache_valid
);
1576 ls
= shash_find_data(&ctx
->lswitches
, name
);
1577 if (must_exist
&& !ls
) {
1578 vtep_ctl_fatal("no logical switch named %s", name
);
1584 cmd_add_ls(struct vtep_ctl_context
*ctx
)
1586 const char *ls_name
= ctx
->argv
[1];
1587 bool may_exist
= shash_find(&ctx
->options
, "--may-exist") != NULL
;
1588 struct vteprec_logical_switch
*ls
;
1590 vtep_ctl_context_populate_cache(ctx
);
1591 if (find_lswitch(ctx
, ls_name
, false)) {
1593 vtep_ctl_fatal("cannot create logical switch %s because it "
1594 "already exists", ls_name
);
1599 ls
= vteprec_logical_switch_insert(ctx
->txn
);
1600 vteprec_logical_switch_set_name(ls
, ls_name
);
1602 vtep_ctl_context_invalidate_cache(ctx
);
1606 del_lswitch(struct vtep_ctl_context
*ctx
, struct vtep_ctl_lswitch
*ls
)
1608 del_cached_lswitch(ctx
, ls
);
1612 cmd_del_ls(struct vtep_ctl_context
*ctx
)
1614 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
1615 struct vtep_ctl_lswitch
*ls
;
1617 vtep_ctl_context_populate_cache(ctx
);
1618 ls
= find_lswitch(ctx
, ctx
->argv
[1], must_exist
);
1620 del_lswitch(ctx
, ls
);
1625 cmd_list_ls(struct vtep_ctl_context
*ctx
)
1627 struct shash_node
*node
;
1628 struct svec lswitches
;
1630 vtep_ctl_context_populate_cache(ctx
);
1632 svec_init(&lswitches
);
1633 SHASH_FOR_EACH (node
, &ctx
->lswitches
) {
1634 struct vtep_ctl_lswitch
*ls
= node
->data
;
1636 svec_add(&lswitches
, ls
->name
);
1638 output_sorted(&lswitches
, &ctx
->output
);
1639 svec_destroy(&lswitches
);
1643 cmd_ls_exists(struct vtep_ctl_context
*ctx
)
1645 vtep_ctl_context_populate_cache(ctx
);
1646 if (!find_lswitch(ctx
, ctx
->argv
[1], false)) {
1652 cmd_list_bindings(struct vtep_ctl_context
*ctx
)
1654 const struct shash_node
*node
;
1655 struct vtep_ctl_port
*port
;
1656 struct svec bindings
;
1658 vtep_ctl_context_populate_cache(ctx
);
1659 port
= find_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1661 svec_init(&bindings
);
1662 SHASH_FOR_EACH (node
, &port
->bindings
) {
1663 struct vtep_ctl_lswitch
*lswitch
= node
->data
;
1666 binding
= xasprintf("%04lld %s", strtoll(node
->name
, NULL
, 0),
1668 svec_add_nocopy(&bindings
, binding
);
1670 output_sorted(&bindings
, &ctx
->output
);
1671 svec_destroy(&bindings
);
1675 cmd_bind_ls(struct vtep_ctl_context
*ctx
)
1677 struct vtep_ctl_lswitch
*ls
;
1678 struct vtep_ctl_port
*port
;
1681 vtep_ctl_context_populate_cache(ctx
);
1683 port
= find_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1684 vlan
= ctx
->argv
[3];
1685 ls
= find_lswitch(ctx
, ctx
->argv
[4], true);
1687 add_ls_binding_to_cache(port
, vlan
, ls
);
1688 commit_ls_bindings(port
);
1690 vtep_ctl_context_invalidate_cache(ctx
);
1694 cmd_unbind_ls(struct vtep_ctl_context
*ctx
)
1696 struct vtep_ctl_port
*port
;
1699 vtep_ctl_context_populate_cache(ctx
);
1701 port
= find_port(ctx
, ctx
->argv
[1], ctx
->argv
[2], true);
1702 vlan
= ctx
->argv
[3];
1704 del_cached_ls_binding(port
, vlan
);
1705 commit_ls_bindings(port
);
1707 vtep_ctl_context_invalidate_cache(ctx
);
1711 add_ucast_entry(struct vtep_ctl_context
*ctx
, bool local
)
1713 struct vtep_ctl_lswitch
*ls
;
1717 struct vteprec_physical_locator
*ploc_cfg
;
1719 vtep_ctl_context_populate_cache(ctx
);
1721 ls
= find_lswitch(ctx
, ctx
->argv
[1], true);
1724 if (ctx
->argc
== 4) {
1725 encap
= "vxlan_over_ipv4";
1726 dst_ip
= ctx
->argv
[3];
1728 encap
= ctx
->argv
[3];
1729 dst_ip
= ctx
->argv
[4];
1732 ploc_cfg
= find_ploc(ctx
, encap
, dst_ip
);
1734 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1735 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1736 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1738 add_ploc_to_cache(ctx
, ploc_cfg
);
1742 struct vteprec_ucast_macs_local
*ucast_cfg
;
1744 ucast_cfg
= shash_find_data(&ls
->ucast_local
, mac
);
1746 ucast_cfg
= vteprec_ucast_macs_local_insert(ctx
->txn
);
1747 vteprec_ucast_macs_local_set_MAC(ucast_cfg
, mac
);
1748 vteprec_ucast_macs_local_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1749 shash_add(&ls
->ucast_local
, mac
, ucast_cfg
);
1751 vteprec_ucast_macs_local_set_locator(ucast_cfg
, ploc_cfg
);
1753 struct vteprec_ucast_macs_remote
*ucast_cfg
;
1755 ucast_cfg
= shash_find_data(&ls
->ucast_remote
, mac
);
1757 ucast_cfg
= vteprec_ucast_macs_remote_insert(ctx
->txn
);
1758 vteprec_ucast_macs_remote_set_MAC(ucast_cfg
, mac
);
1759 vteprec_ucast_macs_remote_set_logical_switch(ucast_cfg
, ls
->ls_cfg
);
1760 shash_add(&ls
->ucast_remote
, mac
, ucast_cfg
);
1762 vteprec_ucast_macs_remote_set_locator(ucast_cfg
, ploc_cfg
);
1765 vtep_ctl_context_invalidate_cache(ctx
);
1769 cmd_add_ucast_local(struct vtep_ctl_context
*ctx
)
1771 add_ucast_entry(ctx
, true);
1775 cmd_add_ucast_remote(struct vtep_ctl_context
*ctx
)
1777 add_ucast_entry(ctx
, false);
1781 del_ucast_entry(struct vtep_ctl_context
*ctx
, bool local
)
1783 struct vtep_ctl_lswitch
*ls
;
1784 struct shash
*ucast_shash
;
1785 struct shash_node
*node
;
1787 vtep_ctl_context_populate_cache(ctx
);
1789 ls
= find_lswitch(ctx
, ctx
->argv
[1], true);
1790 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
1792 node
= shash_find(ucast_shash
, ctx
->argv
[2]);
1798 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
1799 vteprec_ucast_macs_local_delete(ucast_cfg
);
1801 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
1802 vteprec_ucast_macs_remote_delete(ucast_cfg
);
1804 shash_delete(ucast_shash
, node
);
1806 vtep_ctl_context_invalidate_cache(ctx
);
1810 cmd_del_ucast_local(struct vtep_ctl_context
*ctx
)
1812 del_ucast_entry(ctx
, true);
1816 cmd_del_ucast_remote(struct vtep_ctl_context
*ctx
)
1818 del_ucast_entry(ctx
, false);
1822 commit_mcast_entries(struct vtep_ctl_mcast_mac
*mcast_mac
)
1824 struct vtep_ctl_ploc
*ploc
;
1825 struct vteprec_physical_locator
**locators
= NULL
;
1829 n_locators
= list_size(&mcast_mac
->locators
);
1830 ovs_assert(n_locators
);
1832 locators
= xmalloc(n_locators
* sizeof *locators
);
1835 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
1836 locators
[i
] = (struct vteprec_physical_locator
*)ploc
->ploc_cfg
;
1840 vteprec_physical_locator_set_set_locators(mcast_mac
->ploc_set_cfg
,
1848 add_mcast_entry(struct vtep_ctl_context
*ctx
,
1849 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1850 const char *encap
, const char *dst_ip
, bool local
)
1852 struct shash
*mcast_shash
;
1853 struct vtep_ctl_mcast_mac
*mcast_mac
;
1854 struct vteprec_physical_locator
*ploc_cfg
;
1855 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1857 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1859 /* Physical locator sets are immutable, so allocate a new one. */
1860 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1862 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1864 mcast_mac
= add_mcast_mac_to_cache(ctx
, ls
, mac
, ploc_set_cfg
, local
);
1867 mcast_mac
->local_cfg
= vteprec_mcast_macs_local_insert(ctx
->txn
);
1868 vteprec_mcast_macs_local_set_MAC(mcast_mac
->local_cfg
, mac
);
1869 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1871 vteprec_mcast_macs_local_set_logical_switch(mcast_mac
->local_cfg
,
1873 mcast_mac
->remote_cfg
= NULL
;
1875 mcast_mac
->remote_cfg
= vteprec_mcast_macs_remote_insert(ctx
->txn
);
1876 vteprec_mcast_macs_remote_set_MAC(mcast_mac
->remote_cfg
, mac
);
1877 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1879 vteprec_mcast_macs_remote_set_logical_switch(mcast_mac
->remote_cfg
,
1881 mcast_mac
->local_cfg
= NULL
;
1884 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1886 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1889 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1894 ploc_cfg
= find_ploc(ctx
, encap
, dst_ip
);
1896 ploc_cfg
= vteprec_physical_locator_insert(ctx
->txn
);
1897 vteprec_physical_locator_set_dst_ip(ploc_cfg
, dst_ip
);
1898 vteprec_physical_locator_set_encapsulation_type(ploc_cfg
, encap
);
1900 add_ploc_to_cache(ctx
, ploc_cfg
);
1903 add_ploc_to_mcast_mac(mcast_mac
, ploc_cfg
);
1904 commit_mcast_entries(mcast_mac
);
1908 del_mcast_entry(struct vtep_ctl_context
*ctx
,
1909 struct vtep_ctl_lswitch
*ls
, const char *mac
,
1910 const char *encap
, const char *dst_ip
, bool local
)
1912 struct vtep_ctl_mcast_mac
*mcast_mac
;
1913 struct shash
*mcast_shash
;
1914 struct vteprec_physical_locator
*ploc_cfg
;
1915 struct vteprec_physical_locator_set
*ploc_set_cfg
;
1917 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
1919 mcast_mac
= shash_find_data(mcast_shash
, mac
);
1924 ploc_cfg
= find_ploc(ctx
, encap
, dst_ip
);
1926 /* Couldn't find the physical locator, so just ignore. */
1930 /* Physical locator sets are immutable, so allocate a new one. */
1931 ploc_set_cfg
= vteprec_physical_locator_set_insert(ctx
->txn
);
1932 mcast_mac
->ploc_set_cfg
= ploc_set_cfg
;
1934 del_ploc_from_mcast_mac(mcast_mac
, ploc_cfg
);
1935 if (list_is_empty(&mcast_mac
->locators
)) {
1936 struct shash_node
*node
= shash_find(mcast_shash
, mac
);
1938 vteprec_physical_locator_set_delete(ploc_set_cfg
);
1941 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
1943 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
1947 shash_delete(mcast_shash
, node
);
1950 vteprec_mcast_macs_local_set_locator_set(mcast_mac
->local_cfg
,
1953 vteprec_mcast_macs_remote_set_locator_set(mcast_mac
->remote_cfg
,
1956 commit_mcast_entries(mcast_mac
);
1961 add_del_mcast_entry(struct vtep_ctl_context
*ctx
, bool add
, bool local
)
1963 struct vtep_ctl_lswitch
*ls
;
1968 vtep_ctl_context_populate_cache(ctx
);
1970 ls
= find_lswitch(ctx
, ctx
->argv
[1], true);
1973 if (ctx
->argc
== 4) {
1974 encap
= "vxlan_over_ipv4";
1975 dst_ip
= ctx
->argv
[3];
1977 encap
= ctx
->argv
[3];
1978 dst_ip
= ctx
->argv
[4];
1982 add_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1984 del_mcast_entry(ctx
, ls
, mac
, encap
, dst_ip
, local
);
1987 vtep_ctl_context_invalidate_cache(ctx
);
1991 cmd_add_mcast_local(struct vtep_ctl_context
*ctx
)
1993 add_del_mcast_entry(ctx
, true, true);
1997 cmd_add_mcast_remote(struct vtep_ctl_context
*ctx
)
1999 add_del_mcast_entry(ctx
, true, false);
2003 cmd_del_mcast_local(struct vtep_ctl_context
*ctx
)
2005 add_del_mcast_entry(ctx
, false, true);
2009 cmd_del_mcast_remote(struct vtep_ctl_context
*ctx
)
2011 add_del_mcast_entry(ctx
, false, false);
2015 clear_macs(struct vtep_ctl_context
*ctx
, bool local
)
2017 struct vtep_ctl_lswitch
*ls
;
2018 const struct shash_node
*node
;
2019 struct shash
*ucast_shash
;
2020 struct shash
*mcast_shash
;
2022 vtep_ctl_context_populate_cache(ctx
);
2023 ls
= find_lswitch(ctx
, ctx
->argv
[1], true);
2025 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
2026 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
2028 SHASH_FOR_EACH (node
, ucast_shash
) {
2030 struct vteprec_ucast_macs_local
*ucast_cfg
= node
->data
;
2031 vteprec_ucast_macs_local_delete(ucast_cfg
);
2033 struct vteprec_ucast_macs_remote
*ucast_cfg
= node
->data
;
2034 vteprec_ucast_macs_remote_delete(ucast_cfg
);
2038 SHASH_FOR_EACH (node
, mcast_shash
) {
2039 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2041 vteprec_mcast_macs_local_delete(mcast_mac
->local_cfg
);
2043 vteprec_mcast_macs_remote_delete(mcast_mac
->remote_cfg
);
2047 vtep_ctl_context_invalidate_cache(ctx
);
2051 cmd_clear_local_macs(struct vtep_ctl_context
*ctx
)
2053 clear_macs(ctx
, true);
2057 cmd_clear_remote_macs(struct vtep_ctl_context
*ctx
)
2059 clear_macs(ctx
, false);
2063 list_macs(struct vtep_ctl_context
*ctx
, bool local
)
2065 struct vtep_ctl_lswitch
*ls
;
2066 const struct shash_node
*node
;
2067 struct shash
*ucast_shash
;
2068 struct svec ucast_macs
;
2069 struct shash
*mcast_shash
;
2070 struct svec mcast_macs
;
2072 vtep_ctl_context_populate_cache(ctx
);
2073 ls
= find_lswitch(ctx
, ctx
->argv
[1], true);
2075 ucast_shash
= local
? &ls
->ucast_local
: &ls
->ucast_remote
;
2076 mcast_shash
= local
? &ls
->mcast_local
: &ls
->mcast_remote
;
2078 svec_init(&ucast_macs
);
2079 SHASH_FOR_EACH (node
, ucast_shash
) {
2080 struct vteprec_ucast_macs_local
*ucast_local
= node
->data
;
2081 struct vteprec_ucast_macs_remote
*ucast_remote
= node
->data
;
2082 struct vteprec_physical_locator
*ploc_cfg
;
2085 ploc_cfg
= local
? ucast_local
->locator
: ucast_remote
->locator
;
2087 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2088 ploc_cfg
->encapsulation_type
, ploc_cfg
->dst_ip
);
2089 svec_add_nocopy(&ucast_macs
, entry
);
2091 ds_put_format(&ctx
->output
, "ucast-mac-%s\n", local
? "local" : "remote");
2092 output_sorted(&ucast_macs
, &ctx
->output
);
2093 ds_put_char(&ctx
->output
, '\n');
2094 svec_destroy(&ucast_macs
);
2096 svec_init(&mcast_macs
);
2097 SHASH_FOR_EACH (node
, mcast_shash
) {
2098 struct vtep_ctl_mcast_mac
*mcast_mac
= node
->data
;
2099 struct vtep_ctl_ploc
*ploc
;
2102 LIST_FOR_EACH (ploc
, locators_node
, &mcast_mac
->locators
) {
2103 entry
= xasprintf(" %s -> %s/%s", node
->name
,
2104 ploc
->ploc_cfg
->encapsulation_type
,
2105 ploc
->ploc_cfg
->dst_ip
);
2106 svec_add_nocopy(&mcast_macs
, entry
);
2109 ds_put_format(&ctx
->output
, "mcast-mac-%s\n", local
? "local" : "remote");
2110 output_sorted(&mcast_macs
, &ctx
->output
);
2111 ds_put_char(&ctx
->output
, '\n');
2112 svec_destroy(&mcast_macs
);
2116 cmd_list_local_macs(struct vtep_ctl_context
*ctx
)
2118 list_macs(ctx
, true);
2122 cmd_list_remote_macs(struct vtep_ctl_context
*ctx
)
2124 list_macs(ctx
, false);
2128 verify_managers(const struct vteprec_global
*vtep_global
)
2132 vteprec_global_verify_managers(vtep_global
);
2134 for (i
= 0; i
< vtep_global
->n_managers
; ++i
) {
2135 const struct vteprec_manager
*mgr
= vtep_global
->managers
[i
];
2137 vteprec_manager_verify_target(mgr
);
2142 pre_manager(struct vtep_ctl_context
*ctx
)
2144 ovsdb_idl_add_column(ctx
->idl
, &vteprec_global_col_managers
);
2145 ovsdb_idl_add_column(ctx
->idl
, &vteprec_manager_col_target
);
2149 cmd_get_manager(struct vtep_ctl_context
*ctx
)
2151 const struct vteprec_global
*vtep_global
= ctx
->vtep_global
;
2152 struct svec targets
;
2155 verify_managers(vtep_global
);
2157 /* Print the targets in sorted order for reproducibility. */
2158 svec_init(&targets
);
2160 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2161 svec_add(&targets
, vtep_global
->managers
[i
]->target
);
2164 svec_sort_unique(&targets
);
2165 for (i
= 0; i
< targets
.n
; i
++) {
2166 ds_put_format(&ctx
->output
, "%s\n", targets
.names
[i
]);
2168 svec_destroy(&targets
);
2172 delete_managers(const struct vtep_ctl_context
*ctx
)
2174 const struct vteprec_global
*vtep_global
= ctx
->vtep_global
;
2177 /* Delete Manager rows pointed to by 'managers' column. */
2178 for (i
= 0; i
< vtep_global
->n_managers
; i
++) {
2179 vteprec_manager_delete(vtep_global
->managers
[i
]);
2182 /* Delete 'Manager' row refs in 'managers' column. */
2183 vteprec_global_set_managers(vtep_global
, NULL
, 0);
2187 cmd_del_manager(struct vtep_ctl_context
*ctx
)
2189 const struct vteprec_global
*vtep_global
= ctx
->vtep_global
;
2191 verify_managers(vtep_global
);
2192 delete_managers(ctx
);
2196 insert_managers(struct vtep_ctl_context
*ctx
, char *targets
[], size_t n
)
2198 struct vteprec_manager
**managers
;
2201 /* Insert each manager in a new row in Manager table. */
2202 managers
= xmalloc(n
* sizeof *managers
);
2203 for (i
= 0; i
< n
; i
++) {
2204 if (stream_verify_name(targets
[i
]) && pstream_verify_name(targets
[i
])) {
2205 VLOG_WARN("target type \"%s\" is possibly erroneous", targets
[i
]);
2207 managers
[i
] = vteprec_manager_insert(ctx
->txn
);
2208 vteprec_manager_set_target(managers
[i
], targets
[i
]);
2211 /* Store uuids of new Manager rows in 'managers' column. */
2212 vteprec_global_set_managers(ctx
->vtep_global
, managers
, n
);
2217 cmd_set_manager(struct vtep_ctl_context
*ctx
)
2219 const size_t n
= ctx
->argc
- 1;
2221 verify_managers(ctx
->vtep_global
);
2222 delete_managers(ctx
);
2223 insert_managers(ctx
, &ctx
->argv
[1], n
);
2226 /* Parameter commands. */
2228 struct vtep_ctl_row_id
{
2229 const struct ovsdb_idl_table_class
*table
;
2230 const struct ovsdb_idl_column
*name_column
;
2231 const struct ovsdb_idl_column
*uuid_column
;
2234 struct vtep_ctl_table_class
{
2235 struct ovsdb_idl_table_class
*class;
2236 struct vtep_ctl_row_id row_ids
[2];
2239 static const struct vtep_ctl_table_class tables
[] = {
2240 {&vteprec_table_global
,
2241 {{&vteprec_table_global
, NULL
, NULL
},
2242 {NULL
, NULL
, NULL
}}},
2244 {&vteprec_table_logical_binding_stats
,
2245 {{NULL
, NULL
, NULL
},
2246 {NULL
, NULL
, NULL
}}},
2248 {&vteprec_table_logical_switch
,
2249 {{&vteprec_table_logical_switch
, &vteprec_logical_switch_col_name
, NULL
},
2250 {NULL
, NULL
, NULL
}}},
2252 {&vteprec_table_ucast_macs_local
,
2253 {{NULL
, NULL
, NULL
},
2254 {NULL
, NULL
, NULL
}}},
2256 {&vteprec_table_ucast_macs_remote
,
2257 {{NULL
, NULL
, NULL
},
2258 {NULL
, NULL
, NULL
}}},
2260 {&vteprec_table_mcast_macs_local
,
2261 {{NULL
, NULL
, NULL
},
2262 {NULL
, NULL
, NULL
}}},
2264 {&vteprec_table_mcast_macs_remote
,
2265 {{NULL
, NULL
, NULL
},
2266 {NULL
, NULL
, NULL
}}},
2268 {&vteprec_table_manager
,
2269 {{&vteprec_table_manager
, &vteprec_manager_col_target
, NULL
},
2270 {NULL
, NULL
, NULL
}}},
2272 {&vteprec_table_physical_locator
,
2273 {{NULL
, NULL
, NULL
},
2274 {NULL
, NULL
, NULL
}}},
2276 {&vteprec_table_physical_locator_set
,
2277 {{NULL
, NULL
, NULL
},
2278 {NULL
, NULL
, NULL
}}},
2280 {&vteprec_table_physical_port
,
2281 {{&vteprec_table_physical_port
, &vteprec_physical_port_col_name
, NULL
},
2282 {NULL
, NULL
, NULL
}}},
2284 {&vteprec_table_physical_switch
,
2285 {{&vteprec_table_physical_switch
, &vteprec_physical_switch_col_name
, NULL
},
2286 {NULL
, NULL
, NULL
}}},
2288 {NULL
, {{NULL
, NULL
, NULL
}, {NULL
, NULL
, NULL
}}}
2292 die_if_error(char *error
)
2295 vtep_ctl_fatal("%s", error
);
2300 to_lower_and_underscores(unsigned c
)
2302 return c
== '-' ? '_' : tolower(c
);
2306 score_partial_match(const char *name
, const char *s
)
2310 if (!strcmp(name
, s
)) {
2313 for (score
= 0; ; score
++, name
++, s
++) {
2314 if (to_lower_and_underscores(*name
) != to_lower_and_underscores(*s
)) {
2316 } else if (*name
== '\0') {
2317 return UINT_MAX
- 1;
2320 return *s
== '\0' ? score
: 0;
2323 static const struct vtep_ctl_table_class
*
2324 get_table(const char *table_name
)
2326 const struct vtep_ctl_table_class
*table
;
2327 const struct vtep_ctl_table_class
*best_match
= NULL
;
2328 unsigned int best_score
= 0;
2330 for (table
= tables
; table
->class; table
++) {
2331 unsigned int score
= score_partial_match(table
->class->name
,
2333 if (score
> best_score
) {
2336 } else if (score
== best_score
) {
2342 } else if (best_score
) {
2343 vtep_ctl_fatal("multiple table names match \"%s\"", table_name
);
2345 vtep_ctl_fatal("unknown table \"%s\"", table_name
);
2349 static const struct vtep_ctl_table_class
*
2350 pre_get_table(struct vtep_ctl_context
*ctx
, const char *table_name
)
2352 const struct vtep_ctl_table_class
*table_class
;
2355 table_class
= get_table(table_name
);
2356 ovsdb_idl_add_table(ctx
->idl
, table_class
->class);
2358 for (i
= 0; i
< ARRAY_SIZE(table_class
->row_ids
); i
++) {
2359 const struct vtep_ctl_row_id
*id
= &table_class
->row_ids
[i
];
2361 ovsdb_idl_add_table(ctx
->idl
, id
->table
);
2363 if (id
->name_column
) {
2364 ovsdb_idl_add_column(ctx
->idl
, id
->name_column
);
2366 if (id
->uuid_column
) {
2367 ovsdb_idl_add_column(ctx
->idl
, id
->uuid_column
);
2374 static const struct ovsdb_idl_row
*
2375 get_row_by_id(struct vtep_ctl_context
*ctx
, const struct vtep_ctl_table_class
*table
,
2376 const struct vtep_ctl_row_id
*id
, const char *record_id
)
2378 const struct ovsdb_idl_row
*referrer
, *final
;
2384 if (!id
->name_column
) {
2385 if (strcmp(record_id
, ".")) {
2388 referrer
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2389 if (!referrer
|| ovsdb_idl_next_row(referrer
)) {
2393 const struct ovsdb_idl_row
*row
;
2396 for (row
= ovsdb_idl_first_row(ctx
->idl
, id
->table
);
2398 row
= ovsdb_idl_next_row(row
))
2400 const struct ovsdb_datum
*name
;
2402 name
= ovsdb_idl_get(row
, id
->name_column
,
2403 OVSDB_TYPE_STRING
, OVSDB_TYPE_VOID
);
2404 if (name
->n
== 1 && !strcmp(name
->keys
[0].string
, record_id
)) {
2406 vtep_ctl_fatal("multiple rows in %s match \"%s\"",
2407 table
->class->name
, record_id
);
2418 if (id
->uuid_column
) {
2419 const struct ovsdb_datum
*uuid
;
2421 ovsdb_idl_txn_verify(referrer
, id
->uuid_column
);
2422 uuid
= ovsdb_idl_get(referrer
, id
->uuid_column
,
2423 OVSDB_TYPE_UUID
, OVSDB_TYPE_VOID
);
2425 final
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class,
2426 &uuid
->keys
[0].uuid
);
2435 static const struct ovsdb_idl_row
*
2436 get_row (struct vtep_ctl_context
*ctx
,
2437 const struct vtep_ctl_table_class
*table
, const char *record_id
)
2439 const struct ovsdb_idl_row
*row
;
2442 if (uuid_from_string(&uuid
, record_id
)) {
2443 row
= ovsdb_idl_get_row_for_uuid(ctx
->idl
, table
->class, &uuid
);
2447 for (i
= 0; i
< ARRAY_SIZE(table
->row_ids
); i
++) {
2448 row
= get_row_by_id(ctx
, table
, &table
->row_ids
[i
], record_id
);
2457 static const struct ovsdb_idl_row
*
2458 must_get_row(struct vtep_ctl_context
*ctx
,
2459 const struct vtep_ctl_table_class
*table
, const char *record_id
)
2461 const struct ovsdb_idl_row
*row
= get_row(ctx
, table
, record_id
);
2463 vtep_ctl_fatal("no row \"%s\" in table %s",
2464 record_id
, table
->class->name
);
2470 get_column(const struct vtep_ctl_table_class
*table
, const char *column_name
,
2471 const struct ovsdb_idl_column
**columnp
)
2473 const struct ovsdb_idl_column
*best_match
= NULL
;
2474 unsigned int best_score
= 0;
2477 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2478 const struct ovsdb_idl_column
*column
= &table
->class->columns
[i
];
2479 unsigned int score
= score_partial_match(column
->name
, column_name
);
2480 if (score
> best_score
) {
2481 best_match
= column
;
2483 } else if (score
== best_score
) {
2488 *columnp
= best_match
;
2491 } else if (best_score
) {
2492 return xasprintf("%s contains more than one column whose name "
2493 "matches \"%s\"", table
->class->name
, column_name
);
2495 return xasprintf("%s does not contain a column whose name matches "
2496 "\"%s\"", table
->class->name
, column_name
);
2500 static struct ovsdb_symbol
*
2501 create_symbol(struct ovsdb_symbol_table
*symtab
, const char *id
, bool *newp
)
2503 struct ovsdb_symbol
*symbol
;
2506 vtep_ctl_fatal("row id \"%s\" does not begin with \"@\"", id
);
2510 *newp
= ovsdb_symbol_table_get(symtab
, id
) == NULL
;
2513 symbol
= ovsdb_symbol_table_insert(symtab
, id
);
2514 if (symbol
->created
) {
2515 vtep_ctl_fatal("row id \"%s\" may only be specified on one --id option",
2518 symbol
->created
= true;
2523 pre_get_column(struct vtep_ctl_context
*ctx
,
2524 const struct vtep_ctl_table_class
*table
, const char *column_name
,
2525 const struct ovsdb_idl_column
**columnp
)
2527 die_if_error(get_column(table
, column_name
, columnp
));
2528 ovsdb_idl_add_column(ctx
->idl
, *columnp
);
2532 missing_operator_error(const char *arg
, const char **allowed_operators
,
2538 ds_put_format(&s
, "%s: argument does not end in ", arg
);
2539 ds_put_format(&s
, "\"%s\"", allowed_operators
[0]);
2540 if (n_allowed
== 2) {
2541 ds_put_format(&s
, " or \"%s\"", allowed_operators
[1]);
2542 } else if (n_allowed
> 2) {
2545 for (i
= 1; i
< n_allowed
- 1; i
++) {
2546 ds_put_format(&s
, ", \"%s\"", allowed_operators
[i
]);
2548 ds_put_format(&s
, ", or \"%s\"", allowed_operators
[i
]);
2550 ds_put_format(&s
, " followed by a value.");
2552 return ds_steal_cstr(&s
);
2555 /* Breaks 'arg' apart into a number of fields in the following order:
2557 * - The name of a column in 'table', stored into '*columnp'. The column
2558 * name may be abbreviated.
2560 * - Optionally ':' followed by a key string. The key is stored as a
2561 * malloc()'d string into '*keyp', or NULL if no key is present in
2564 * - If 'valuep' is nonnull, an operator followed by a value string. The
2565 * allowed operators are the 'n_allowed' string in 'allowed_operators',
2566 * or just "=" if 'n_allowed' is 0. If 'operatorp' is nonnull, then the
2567 * index of the operator within 'allowed_operators' is stored into
2568 * '*operatorp'. The value is stored as a malloc()'d string into
2569 * '*valuep', or NULL if no value is present in 'arg'.
2571 * On success, returns NULL. On failure, returned a malloc()'d string error
2572 * message and stores NULL into all of the nonnull output arguments. */
2573 static char * WARN_UNUSED_RESULT
2574 parse_column_key_value(const char *arg
,
2575 const struct vtep_ctl_table_class
*table
,
2576 const struct ovsdb_idl_column
**columnp
, char **keyp
,
2578 const char **allowed_operators
, size_t n_allowed
,
2581 const char *p
= arg
;
2585 ovs_assert(!(operatorp
&& !valuep
));
2591 /* Parse column name. */
2592 error
= ovsdb_token_parse(&p
, &column_name
);
2596 if (column_name
[0] == '\0') {
2598 error
= xasprintf("%s: missing column name", arg
);
2601 error
= get_column(table
, column_name
, columnp
);
2607 /* Parse key string. */
2610 error
= ovsdb_token_parse(&p
, keyp
);
2616 /* Parse value string. */
2622 if (!allowed_operators
) {
2623 static const char *equals
= "=";
2624 allowed_operators
= &equals
;
2630 for (i
= 0; i
< n_allowed
; i
++) {
2631 const char *op
= allowed_operators
[i
];
2632 size_t op_len
= strlen(op
);
2634 if (op_len
> best_len
&& !strncmp(op
, p
, op_len
) && p
[op_len
]) {
2640 error
= missing_operator_error(arg
, allowed_operators
, n_allowed
);
2647 *valuep
= xstrdup(p
+ best_len
);
2650 error
= xasprintf("%s: trailing garbage \"%s\" in argument",
2671 static const struct ovsdb_idl_column
*
2672 pre_parse_column_key_value(struct vtep_ctl_context
*ctx
,
2674 const struct vtep_ctl_table_class
*table
)
2676 const struct ovsdb_idl_column
*column
;
2681 die_if_error(ovsdb_token_parse(&p
, &column_name
));
2682 if (column_name
[0] == '\0') {
2683 vtep_ctl_fatal("%s: missing column name", arg
);
2686 pre_get_column(ctx
, table
, column_name
, &column
);
2693 check_mutable(const struct vtep_ctl_table_class
*table
,
2694 const struct ovsdb_idl_column
*column
)
2696 if (!column
->mutable) {
2697 vtep_ctl_fatal("cannot modify read-only column %s in table %s",
2698 column
->name
, table
->class->name
);
2703 pre_cmd_get(struct vtep_ctl_context
*ctx
)
2705 const char *id
= shash_find_data(&ctx
->options
, "--id");
2706 const char *table_name
= ctx
->argv
[1];
2707 const struct vtep_ctl_table_class
*table
;
2710 /* Using "get" without --id or a column name could possibly make sense.
2711 * Maybe, for example, a vtep-ctl run wants to assert that a row exists.
2712 * But it is unlikely that an interactive user would want to do that, so
2713 * issue a warning if we're running on a terminal. */
2714 if (!id
&& ctx
->argc
<= 3 && isatty(STDOUT_FILENO
)) {
2715 VLOG_WARN("\"get\" command without row arguments or \"--id\" is "
2716 "possibly erroneous");
2719 table
= pre_get_table(ctx
, table_name
);
2720 for (i
= 3; i
< ctx
->argc
; i
++) {
2721 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2722 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2726 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2731 cmd_get(struct vtep_ctl_context
*ctx
)
2733 const char *id
= shash_find_data(&ctx
->options
, "--id");
2734 bool if_exists
= shash_find(&ctx
->options
, "--if-exists");
2735 const char *table_name
= ctx
->argv
[1];
2736 const char *record_id
= ctx
->argv
[2];
2737 const struct vtep_ctl_table_class
*table
;
2738 const struct ovsdb_idl_row
*row
;
2739 struct ds
*out
= &ctx
->output
;
2742 table
= get_table(table_name
);
2743 row
= must_get_row(ctx
, table
, record_id
);
2745 struct ovsdb_symbol
*symbol
;
2748 symbol
= create_symbol(ctx
->symtab
, id
, &new);
2750 vtep_ctl_fatal("row id \"%s\" specified on \"get\" command was used "
2751 "before it was defined", id
);
2753 symbol
->uuid
= row
->uuid
;
2755 /* This symbol refers to a row that already exists, so disable warnings
2756 * about it being unreferenced. */
2757 symbol
->strong_ref
= true;
2759 for (i
= 3; i
< ctx
->argc
; i
++) {
2760 const struct ovsdb_idl_column
*column
;
2761 const struct ovsdb_datum
*datum
;
2764 /* Special case for obtaining the UUID of a row. We can't just do this
2765 * through parse_column_key_value() below since it returns a "struct
2766 * ovsdb_idl_column" and the UUID column doesn't have one. */
2767 if (!strcasecmp(ctx
->argv
[i
], "_uuid")
2768 || !strcasecmp(ctx
->argv
[i
], "-uuid")) {
2769 ds_put_format(out
, UUID_FMT
"\n", UUID_ARGS(&row
->uuid
));
2773 die_if_error(parse_column_key_value(ctx
->argv
[i
], table
,
2774 &column
, &key_string
,
2775 NULL
, NULL
, 0, NULL
));
2777 ovsdb_idl_txn_verify(row
, column
);
2778 datum
= ovsdb_idl_read(row
, column
);
2780 union ovsdb_atom key
;
2783 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
2784 vtep_ctl_fatal("cannot specify key to get for non-map column %s",
2788 die_if_error(ovsdb_atom_from_string(&key
,
2790 key_string
, ctx
->symtab
));
2792 idx
= ovsdb_datum_find_key(datum
, &key
,
2793 column
->type
.key
.type
);
2794 if (idx
== UINT_MAX
) {
2796 vtep_ctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
2797 key_string
, table
->class->name
, record_id
,
2801 ovsdb_atom_to_string(&datum
->values
[idx
],
2802 column
->type
.value
.type
, out
);
2804 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
2806 ovsdb_datum_to_string(datum
, &column
->type
, out
);
2808 ds_put_char(out
, '\n');
2815 parse_column_names(const char *column_names
,
2816 const struct vtep_ctl_table_class
*table
,
2817 const struct ovsdb_idl_column
***columnsp
,
2820 const struct ovsdb_idl_column
**columns
;
2823 if (!column_names
) {
2826 n_columns
= table
->class->n_columns
+ 1;
2827 columns
= xmalloc(n_columns
* sizeof *columns
);
2829 for (i
= 0; i
< table
->class->n_columns
; i
++) {
2830 columns
[i
+ 1] = &table
->class->columns
[i
];
2833 char *s
= xstrdup(column_names
);
2834 size_t allocated_columns
;
2835 char *save_ptr
= NULL
;
2839 allocated_columns
= n_columns
= 0;
2840 for (column_name
= strtok_r(s
, ", ", &save_ptr
); column_name
;
2841 column_name
= strtok_r(NULL
, ", ", &save_ptr
)) {
2842 const struct ovsdb_idl_column
*column
;
2844 if (!strcasecmp(column_name
, "_uuid")) {
2847 die_if_error(get_column(table
, column_name
, &column
));
2849 if (n_columns
>= allocated_columns
) {
2850 columns
= x2nrealloc(columns
, &allocated_columns
,
2853 columns
[n_columns
++] = column
;
2858 vtep_ctl_fatal("must specify at least one column name");
2861 *columnsp
= columns
;
2862 *n_columnsp
= n_columns
;
2867 pre_list_columns(struct vtep_ctl_context
*ctx
,
2868 const struct vtep_ctl_table_class
*table
,
2869 const char *column_names
)
2871 const struct ovsdb_idl_column
**columns
;
2875 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2876 for (i
= 0; i
< n_columns
; i
++) {
2878 ovsdb_idl_add_column(ctx
->idl
, columns
[i
]);
2885 pre_cmd_list(struct vtep_ctl_context
*ctx
)
2887 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2888 const char *table_name
= ctx
->argv
[1];
2889 const struct vtep_ctl_table_class
*table
;
2891 table
= pre_get_table(ctx
, table_name
);
2892 pre_list_columns(ctx
, table
, column_names
);
2895 static struct table
*
2896 list_make_table(const struct ovsdb_idl_column
**columns
, size_t n_columns
)
2901 out
= xmalloc(sizeof *out
);
2904 for (i
= 0; i
< n_columns
; i
++) {
2905 const struct ovsdb_idl_column
*column
= columns
[i
];
2906 const char *column_name
= column
? column
->name
: "_uuid";
2908 table_add_column(out
, "%s", column_name
);
2915 list_record(const struct ovsdb_idl_row
*row
,
2916 const struct ovsdb_idl_column
**columns
, size_t n_columns
,
2922 for (i
= 0; i
< n_columns
; i
++) {
2923 const struct ovsdb_idl_column
*column
= columns
[i
];
2924 struct cell
*cell
= table_add_cell(out
);
2927 struct ovsdb_datum datum
;
2928 union ovsdb_atom atom
;
2930 atom
.uuid
= row
->uuid
;
2933 datum
.values
= NULL
;
2936 cell
->json
= ovsdb_datum_to_json(&datum
, &ovsdb_type_uuid
);
2937 cell
->type
= &ovsdb_type_uuid
;
2939 const struct ovsdb_datum
*datum
= ovsdb_idl_read(row
, column
);
2941 cell
->json
= ovsdb_datum_to_json(datum
, &column
->type
);
2942 cell
->type
= &column
->type
;
2948 cmd_list(struct vtep_ctl_context
*ctx
)
2950 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2951 const struct ovsdb_idl_column
**columns
;
2952 const char *table_name
= ctx
->argv
[1];
2953 const struct vtep_ctl_table_class
*table
;
2958 table
= get_table(table_name
);
2959 parse_column_names(column_names
, table
, &columns
, &n_columns
);
2960 out
= ctx
->table
= list_make_table(columns
, n_columns
);
2961 if (ctx
->argc
> 2) {
2962 for (i
= 2; i
< ctx
->argc
; i
++) {
2963 list_record(must_get_row(ctx
, table
, ctx
->argv
[i
]),
2964 columns
, n_columns
, out
);
2967 const struct ovsdb_idl_row
*row
;
2969 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
!= NULL
;
2970 row
= ovsdb_idl_next_row(row
)) {
2971 list_record(row
, columns
, n_columns
, out
);
2978 pre_cmd_find(struct vtep_ctl_context
*ctx
)
2980 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2981 const char *table_name
= ctx
->argv
[1];
2982 const struct vtep_ctl_table_class
*table
;
2985 table
= pre_get_table(ctx
, table_name
);
2986 pre_list_columns(ctx
, table
, column_names
);
2987 for (i
= 2; i
< ctx
->argc
; i
++) {
2988 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
2993 cmd_find(struct vtep_ctl_context
*ctx
)
2995 const char *column_names
= shash_find_data(&ctx
->options
, "--columns");
2996 const struct ovsdb_idl_column
**columns
;
2997 const char *table_name
= ctx
->argv
[1];
2998 const struct vtep_ctl_table_class
*table
;
2999 const struct ovsdb_idl_row
*row
;
3003 table
= get_table(table_name
);
3004 parse_column_names(column_names
, table
, &columns
, &n_columns
);
3005 out
= ctx
->table
= list_make_table(columns
, n_columns
);
3006 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class); row
;
3007 row
= ovsdb_idl_next_row(row
)) {
3010 for (i
= 2; i
< ctx
->argc
; i
++) {
3011 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
],
3016 list_record(row
, columns
, n_columns
, out
);
3024 pre_cmd_set(struct vtep_ctl_context
*ctx
)
3026 const char *table_name
= ctx
->argv
[1];
3027 const struct vtep_ctl_table_class
*table
;
3030 table
= pre_get_table(ctx
, table_name
);
3031 for (i
= 3; i
< ctx
->argc
; i
++) {
3032 const struct ovsdb_idl_column
*column
;
3034 column
= pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
3035 check_mutable(table
, column
);
3040 set_column(const struct vtep_ctl_table_class
*table
,
3041 const struct ovsdb_idl_row
*row
, const char *arg
,
3042 struct ovsdb_symbol_table
*symtab
)
3044 const struct ovsdb_idl_column
*column
;
3045 char *key_string
, *value_string
;
3048 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
3049 NULL
, NULL
, 0, &value_string
);
3050 die_if_error(error
);
3051 if (!value_string
) {
3052 vtep_ctl_fatal("%s: missing value", arg
);
3056 union ovsdb_atom key
, value
;
3057 struct ovsdb_datum datum
;
3059 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
3060 vtep_ctl_fatal("cannot specify key to set for non-map column %s",
3064 die_if_error(ovsdb_atom_from_string(&key
, &column
->type
.key
,
3065 key_string
, symtab
));
3066 die_if_error(ovsdb_atom_from_string(&value
, &column
->type
.value
,
3067 value_string
, symtab
));
3069 ovsdb_datum_init_empty(&datum
);
3070 ovsdb_datum_add_unsafe(&datum
, &key
, &value
, &column
->type
);
3072 ovsdb_atom_destroy(&key
, column
->type
.key
.type
);
3073 ovsdb_atom_destroy(&value
, column
->type
.value
.type
);
3075 ovsdb_datum_union(&datum
, ovsdb_idl_read(row
, column
),
3076 &column
->type
, false);
3077 ovsdb_idl_txn_write(row
, column
, &datum
);
3079 struct ovsdb_datum datum
;
3081 die_if_error(ovsdb_datum_from_string(&datum
, &column
->type
,
3082 value_string
, symtab
));
3083 ovsdb_idl_txn_write(row
, column
, &datum
);
3091 cmd_set(struct vtep_ctl_context
*ctx
)
3093 const char *table_name
= ctx
->argv
[1];
3094 const char *record_id
= ctx
->argv
[2];
3095 const struct vtep_ctl_table_class
*table
;
3096 const struct ovsdb_idl_row
*row
;
3099 table
= get_table(table_name
);
3100 row
= must_get_row(ctx
, table
, record_id
);
3101 for (i
= 3; i
< ctx
->argc
; i
++) {
3102 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
3107 pre_cmd_add(struct vtep_ctl_context
*ctx
)
3109 const char *table_name
= ctx
->argv
[1];
3110 const char *column_name
= ctx
->argv
[3];
3111 const struct vtep_ctl_table_class
*table
;
3112 const struct ovsdb_idl_column
*column
;
3114 table
= pre_get_table(ctx
, table_name
);
3115 pre_get_column(ctx
, table
, column_name
, &column
);
3116 check_mutable(table
, column
);
3120 cmd_add(struct vtep_ctl_context
*ctx
)
3122 const char *table_name
= ctx
->argv
[1];
3123 const char *record_id
= ctx
->argv
[2];
3124 const char *column_name
= ctx
->argv
[3];
3125 const struct vtep_ctl_table_class
*table
;
3126 const struct ovsdb_idl_column
*column
;
3127 const struct ovsdb_idl_row
*row
;
3128 const struct ovsdb_type
*type
;
3129 struct ovsdb_datum old
;
3132 table
= get_table(table_name
);
3133 row
= must_get_row(ctx
, table
, record_id
);
3134 die_if_error(get_column(table
, column_name
, &column
));
3136 type
= &column
->type
;
3137 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
3138 for (i
= 4; i
< ctx
->argc
; i
++) {
3139 struct ovsdb_type add_type
;
3140 struct ovsdb_datum add
;
3144 add_type
.n_max
= UINT_MAX
;
3145 die_if_error(ovsdb_datum_from_string(&add
, &add_type
, ctx
->argv
[i
],
3147 ovsdb_datum_union(&old
, &add
, type
, false);
3148 ovsdb_datum_destroy(&add
, type
);
3150 if (old
.n
> type
->n_max
) {
3151 vtep_ctl_fatal("\"add\" operation would put %u %s in column %s of "
3152 "table %s but the maximum number is %u",
3154 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
3155 column
->name
, table
->class->name
, type
->n_max
);
3157 ovsdb_idl_txn_verify(row
, column
);
3158 ovsdb_idl_txn_write(row
, column
, &old
);
3162 pre_cmd_remove(struct vtep_ctl_context
*ctx
)
3164 const char *table_name
= ctx
->argv
[1];
3165 const char *column_name
= ctx
->argv
[3];
3166 const struct vtep_ctl_table_class
*table
;
3167 const struct ovsdb_idl_column
*column
;
3169 table
= pre_get_table(ctx
, table_name
);
3170 pre_get_column(ctx
, table
, column_name
, &column
);
3171 check_mutable(table
, column
);
3175 cmd_remove(struct vtep_ctl_context
*ctx
)
3177 const char *table_name
= ctx
->argv
[1];
3178 const char *record_id
= ctx
->argv
[2];
3179 const char *column_name
= ctx
->argv
[3];
3180 const struct vtep_ctl_table_class
*table
;
3181 const struct ovsdb_idl_column
*column
;
3182 const struct ovsdb_idl_row
*row
;
3183 const struct ovsdb_type
*type
;
3184 struct ovsdb_datum old
;
3187 table
= get_table(table_name
);
3188 row
= must_get_row(ctx
, table
, record_id
);
3189 die_if_error(get_column(table
, column_name
, &column
));
3191 type
= &column
->type
;
3192 ovsdb_datum_clone(&old
, ovsdb_idl_read(row
, column
), &column
->type
);
3193 for (i
= 4; i
< ctx
->argc
; i
++) {
3194 struct ovsdb_type rm_type
;
3195 struct ovsdb_datum rm
;
3200 rm_type
.n_max
= UINT_MAX
;
3201 error
= ovsdb_datum_from_string(&rm
, &rm_type
,
3202 ctx
->argv
[i
], ctx
->symtab
);
3203 if (error
&& ovsdb_type_is_map(&rm_type
)) {
3205 rm_type
.value
.type
= OVSDB_TYPE_VOID
;
3206 die_if_error(ovsdb_datum_from_string(&rm
, &rm_type
,
3207 ctx
->argv
[i
], ctx
->symtab
));
3209 ovsdb_datum_subtract(&old
, type
, &rm
, &rm_type
);
3210 ovsdb_datum_destroy(&rm
, &rm_type
);
3212 if (old
.n
< type
->n_min
) {
3213 vtep_ctl_fatal("\"remove\" operation would put %u %s in column %s of "
3214 "table %s but the minimum number is %u",
3216 type
->value
.type
== OVSDB_TYPE_VOID
? "values" : "pairs",
3217 column
->name
, table
->class->name
, type
->n_min
);
3219 ovsdb_idl_txn_verify(row
, column
);
3220 ovsdb_idl_txn_write(row
, column
, &old
);
3224 pre_cmd_clear(struct vtep_ctl_context
*ctx
)
3226 const char *table_name
= ctx
->argv
[1];
3227 const struct vtep_ctl_table_class
*table
;
3230 table
= pre_get_table(ctx
, table_name
);
3231 for (i
= 3; i
< ctx
->argc
; i
++) {
3232 const struct ovsdb_idl_column
*column
;
3234 pre_get_column(ctx
, table
, ctx
->argv
[i
], &column
);
3235 check_mutable(table
, column
);
3240 cmd_clear(struct vtep_ctl_context
*ctx
)
3242 const char *table_name
= ctx
->argv
[1];
3243 const char *record_id
= ctx
->argv
[2];
3244 const struct vtep_ctl_table_class
*table
;
3245 const struct ovsdb_idl_row
*row
;
3248 table
= get_table(table_name
);
3249 row
= must_get_row(ctx
, table
, record_id
);
3250 for (i
= 3; i
< ctx
->argc
; i
++) {
3251 const struct ovsdb_idl_column
*column
;
3252 const struct ovsdb_type
*type
;
3253 struct ovsdb_datum datum
;
3255 die_if_error(get_column(table
, ctx
->argv
[i
], &column
));
3257 type
= &column
->type
;
3258 if (type
->n_min
> 0) {
3259 vtep_ctl_fatal("\"clear\" operation cannot be applied to column %s "
3260 "of table %s, which is not allowed to be empty",
3261 column
->name
, table
->class->name
);
3264 ovsdb_datum_init_empty(&datum
);
3265 ovsdb_idl_txn_write(row
, column
, &datum
);
3270 pre_create(struct vtep_ctl_context
*ctx
)
3272 const char *id
= shash_find_data(&ctx
->options
, "--id");
3273 const char *table_name
= ctx
->argv
[1];
3274 const struct vtep_ctl_table_class
*table
;
3276 table
= get_table(table_name
);
3277 if (!id
&& !table
->class->is_root
) {
3278 VLOG_WARN("applying \"create\" command to table %s without --id "
3279 "option will have no effect", table
->class->name
);
3284 cmd_create(struct vtep_ctl_context
*ctx
)
3286 const char *id
= shash_find_data(&ctx
->options
, "--id");
3287 const char *table_name
= ctx
->argv
[1];
3288 const struct vtep_ctl_table_class
*table
= get_table(table_name
);
3289 const struct ovsdb_idl_row
*row
;
3290 const struct uuid
*uuid
;
3294 struct ovsdb_symbol
*symbol
= create_symbol(ctx
->symtab
, id
, NULL
);
3295 if (table
->class->is_root
) {
3296 /* This table is in the root set, meaning that rows created in it
3297 * won't disappear even if they are unreferenced, so disable
3298 * warnings about that by pretending that there is a reference. */
3299 symbol
->strong_ref
= true;
3301 uuid
= &symbol
->uuid
;
3306 row
= ovsdb_idl_txn_insert(ctx
->txn
, table
->class, uuid
);
3307 for (i
= 2; i
< ctx
->argc
; i
++) {
3308 set_column(table
, row
, ctx
->argv
[i
], ctx
->symtab
);
3310 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(&row
->uuid
));
3313 /* This function may be used as the 'postprocess' function for commands that
3314 * insert new rows into the database. It expects that the command's 'run'
3315 * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
3316 * sole output. It replaces that output by the row's permanent UUID assigned
3317 * by the database server and appends a new-line.
3319 * Currently we use this only for "create", because the higher-level commands
3320 * are supposed to be independent of the actual structure of the VTEP
3323 post_create(struct vtep_ctl_context
*ctx
)
3325 const struct uuid
*real
;
3328 if (!uuid_from_string(&dummy
, ds_cstr(&ctx
->output
))) {
3331 real
= ovsdb_idl_txn_get_insert_uuid(ctx
->txn
, &dummy
);
3333 ds_clear(&ctx
->output
);
3334 ds_put_format(&ctx
->output
, UUID_FMT
, UUID_ARGS(real
));
3336 ds_put_char(&ctx
->output
, '\n');
3340 pre_cmd_destroy(struct vtep_ctl_context
*ctx
)
3342 const char *table_name
= ctx
->argv
[1];
3344 pre_get_table(ctx
, table_name
);
3348 cmd_destroy(struct vtep_ctl_context
*ctx
)
3350 bool must_exist
= !shash_find(&ctx
->options
, "--if-exists");
3351 bool delete_all
= shash_find(&ctx
->options
, "--all");
3352 const char *table_name
= ctx
->argv
[1];
3353 const struct vtep_ctl_table_class
*table
;
3356 table
= get_table(table_name
);
3358 if (delete_all
&& ctx
->argc
> 2) {
3359 vtep_ctl_fatal("--all and records argument should not be specified together");
3362 if (delete_all
&& !must_exist
) {
3363 vtep_ctl_fatal("--all and --if-exists should not be specified together");
3367 const struct ovsdb_idl_row
*row
;
3368 const struct ovsdb_idl_row
*next_row
;
3370 for (row
= ovsdb_idl_first_row(ctx
->idl
, table
->class);
3372 next_row
= ovsdb_idl_next_row(row
);
3373 ovsdb_idl_txn_delete(row
);
3377 for (i
= 2; i
< ctx
->argc
; i
++) {
3378 const struct ovsdb_idl_row
*row
;
3380 row
= (must_exist
? must_get_row
: get_row
)(ctx
, table
, ctx
->argv
[i
]);
3382 ovsdb_idl_txn_delete(row
);
3389 RELOP(RELOP_EQ, "=") \
3390 RELOP(RELOP_NE, "!=") \
3391 RELOP(RELOP_LT, "<") \
3392 RELOP(RELOP_GT, ">") \
3393 RELOP(RELOP_LE, "<=") \
3394 RELOP(RELOP_GE, ">=") \
3395 RELOP(RELOP_SET_EQ, "{=}") \
3396 RELOP(RELOP_SET_NE, "{!=}") \
3397 RELOP(RELOP_SET_LT, "{<}") \
3398 RELOP(RELOP_SET_GT, "{>}") \
3399 RELOP(RELOP_SET_LE, "{<=}") \
3400 RELOP(RELOP_SET_GE, "{>=}")
3403 #define RELOP(ENUM, STRING) ENUM,
3409 is_set_operator(enum relop op
)
3411 return (op
== RELOP_SET_EQ
|| op
== RELOP_SET_NE
||
3412 op
== RELOP_SET_LT
|| op
== RELOP_SET_GT
||
3413 op
== RELOP_SET_LE
|| op
== RELOP_SET_GE
);
3417 evaluate_relop(const struct ovsdb_datum
*a
, const struct ovsdb_datum
*b
,
3418 const struct ovsdb_type
*type
, enum relop op
)
3423 return ovsdb_datum_compare_3way(a
, b
, type
) == 0;
3426 return ovsdb_datum_compare_3way(a
, b
, type
) != 0;
3428 return ovsdb_datum_compare_3way(a
, b
, type
) < 0;
3430 return ovsdb_datum_compare_3way(a
, b
, type
) > 0;
3432 return ovsdb_datum_compare_3way(a
, b
, type
) <= 0;
3434 return ovsdb_datum_compare_3way(a
, b
, type
) >= 0;
3437 return b
->n
> a
->n
&& ovsdb_datum_includes_all(a
, b
, type
);
3439 return a
->n
> b
->n
&& ovsdb_datum_includes_all(b
, a
, type
);
3441 return ovsdb_datum_includes_all(a
, b
, type
);
3443 return ovsdb_datum_includes_all(b
, a
, type
);
3451 is_condition_satisfied(const struct vtep_ctl_table_class
*table
,
3452 const struct ovsdb_idl_row
*row
, const char *arg
,
3453 struct ovsdb_symbol_table
*symtab
)
3455 static const char *operators
[] = {
3456 #define RELOP(ENUM, STRING) STRING,
3461 const struct ovsdb_idl_column
*column
;
3462 const struct ovsdb_datum
*have_datum
;
3463 char *key_string
, *value_string
;
3464 struct ovsdb_type type
;
3469 error
= parse_column_key_value(arg
, table
, &column
, &key_string
,
3470 &operator, operators
, ARRAY_SIZE(operators
),
3472 die_if_error(error
);
3473 if (!value_string
) {
3474 vtep_ctl_fatal("%s: missing value", arg
);
3477 type
= column
->type
;
3478 type
.n_max
= UINT_MAX
;
3480 have_datum
= ovsdb_idl_read(row
, column
);
3482 union ovsdb_atom want_key
;
3483 struct ovsdb_datum b
;
3486 if (column
->type
.value
.type
== OVSDB_TYPE_VOID
) {
3487 vtep_ctl_fatal("cannot specify key to check for non-map column %s",
3491 die_if_error(ovsdb_atom_from_string(&want_key
, &column
->type
.key
,
3492 key_string
, symtab
));
3494 type
.key
= type
.value
;
3495 type
.value
.type
= OVSDB_TYPE_VOID
;
3496 die_if_error(ovsdb_datum_from_string(&b
, &type
, value_string
, symtab
));
3498 idx
= ovsdb_datum_find_key(have_datum
,
3499 &want_key
, column
->type
.key
.type
);
3500 if (idx
== UINT_MAX
&& !is_set_operator(operator)) {
3503 struct ovsdb_datum a
;
3505 if (idx
!= UINT_MAX
) {
3507 a
.keys
= &have_datum
->values
[idx
];
3515 retval
= evaluate_relop(&a
, &b
, &type
, operator);
3518 ovsdb_atom_destroy(&want_key
, column
->type
.key
.type
);
3519 ovsdb_datum_destroy(&b
, &type
);
3521 struct ovsdb_datum want_datum
;
3523 die_if_error(ovsdb_datum_from_string(&want_datum
, &column
->type
,
3524 value_string
, symtab
));
3525 retval
= evaluate_relop(have_datum
, &want_datum
, &type
, operator);
3526 ovsdb_datum_destroy(&want_datum
, &column
->type
);
3536 pre_cmd_wait_until(struct vtep_ctl_context
*ctx
)
3538 const char *table_name
= ctx
->argv
[1];
3539 const struct vtep_ctl_table_class
*table
;
3542 table
= pre_get_table(ctx
, table_name
);
3544 for (i
= 3; i
< ctx
->argc
; i
++) {
3545 pre_parse_column_key_value(ctx
, ctx
->argv
[i
], table
);
3550 cmd_wait_until(struct vtep_ctl_context
*ctx
)
3552 const char *table_name
= ctx
->argv
[1];
3553 const char *record_id
= ctx
->argv
[2];
3554 const struct vtep_ctl_table_class
*table
;
3555 const struct ovsdb_idl_row
*row
;
3558 table
= get_table(table_name
);
3560 row
= get_row(ctx
, table
, record_id
);
3562 ctx
->try_again
= true;
3566 for (i
= 3; i
< ctx
->argc
; i
++) {
3567 if (!is_condition_satisfied(table
, row
, ctx
->argv
[i
], ctx
->symtab
)) {
3568 ctx
->try_again
= true;
3574 /* Prepares 'ctx', which has already been initialized with
3575 * vtep_ctl_context_init(), for processing 'command'. */
3577 vtep_ctl_context_init_command(struct vtep_ctl_context
*ctx
,
3578 struct vtep_ctl_command
*command
)
3580 ctx
->argc
= command
->argc
;
3581 ctx
->argv
= command
->argv
;
3582 ctx
->options
= command
->options
;
3584 ds_swap(&ctx
->output
, &command
->output
);
3585 ctx
->table
= command
->table
;
3587 ctx
->verified_ports
= false;
3589 ctx
->try_again
= false;
3592 /* Prepares 'ctx' for processing commands, initializing its members with the
3593 * values passed in as arguments.
3595 * If 'command' is nonnull, calls vtep_ctl_context_init_command() to prepare for
3596 * that particular command. */
3598 vtep_ctl_context_init(struct vtep_ctl_context
*ctx
,
3599 struct vtep_ctl_command
*command
,
3600 struct ovsdb_idl
*idl
, struct ovsdb_idl_txn
*txn
,
3601 const struct vteprec_global
*vtep_global
,
3602 struct ovsdb_symbol_table
*symtab
)
3605 vtep_ctl_context_init_command(ctx
, command
);
3609 ctx
->vtep_global
= vtep_global
;
3610 ctx
->symtab
= symtab
;
3611 ctx
->cache_valid
= false;
3614 /* Completes processing of 'command' within 'ctx'. */
3616 vtep_ctl_context_done_command(struct vtep_ctl_context
*ctx
,
3617 struct vtep_ctl_command
*command
)
3619 ds_swap(&ctx
->output
, &command
->output
);
3620 command
->table
= ctx
->table
;
3623 /* Finishes up with 'ctx'.
3625 * If command is nonnull, first calls vtep_ctl_context_done_command() to complete
3626 * processing that command within 'ctx'. */
3628 vtep_ctl_context_done(struct vtep_ctl_context
*ctx
, struct vtep_ctl_command
*command
)
3631 vtep_ctl_context_done_command(ctx
, command
);
3636 run_prerequisites(struct vtep_ctl_command
*commands
, size_t n_commands
,
3637 struct ovsdb_idl
*idl
)
3639 struct vtep_ctl_command
*c
;
3641 ovsdb_idl_add_table(idl
, &vteprec_table_global
);
3642 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3643 if (c
->syntax
->prerequisites
) {
3644 struct vtep_ctl_context ctx
;
3646 ds_init(&c
->output
);
3649 vtep_ctl_context_init(&ctx
, c
, idl
, NULL
, NULL
, NULL
);
3650 (c
->syntax
->prerequisites
)(&ctx
);
3651 vtep_ctl_context_done(&ctx
, c
);
3653 ovs_assert(!c
->output
.string
);
3654 ovs_assert(!c
->table
);
3660 do_vtep_ctl(const char *args
, struct vtep_ctl_command
*commands
,
3661 size_t n_commands
, struct ovsdb_idl
*idl
)
3663 struct ovsdb_idl_txn
*txn
;
3664 const struct vteprec_global
*vtep_global
;
3665 enum ovsdb_idl_txn_status status
;
3666 struct ovsdb_symbol_table
*symtab
;
3667 struct vtep_ctl_context ctx
;
3668 struct vtep_ctl_command
*c
;
3669 struct shash_node
*node
;
3672 txn
= the_idl_txn
= ovsdb_idl_txn_create(idl
);
3674 ovsdb_idl_txn_set_dry_run(txn
);
3677 ovsdb_idl_txn_add_comment(txn
, "vtep-ctl: %s", args
);
3679 vtep_global
= vteprec_global_first(idl
);
3681 /* XXX add verification that table is empty */
3682 vtep_global
= vteprec_global_insert(txn
);
3685 symtab
= ovsdb_symbol_table_create();
3686 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3687 ds_init(&c
->output
);
3690 vtep_ctl_context_init(&ctx
, NULL
, idl
, txn
, vtep_global
, symtab
);
3691 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3692 vtep_ctl_context_init_command(&ctx
, c
);
3693 if (c
->syntax
->run
) {
3694 (c
->syntax
->run
)(&ctx
);
3696 vtep_ctl_context_done_command(&ctx
, c
);
3698 if (ctx
.try_again
) {
3699 vtep_ctl_context_done(&ctx
, NULL
);
3703 vtep_ctl_context_done(&ctx
, NULL
);
3705 SHASH_FOR_EACH (node
, &symtab
->sh
) {
3706 struct ovsdb_symbol
*symbol
= node
->data
;
3707 if (!symbol
->created
) {
3708 vtep_ctl_fatal("row id \"%s\" is referenced but never created "
3709 "(e.g. with \"-- --id=%s create ...\")",
3710 node
->name
, node
->name
);
3712 if (!symbol
->strong_ref
) {
3713 if (!symbol
->weak_ref
) {
3714 VLOG_WARN("row id \"%s\" was created but no reference to it "
3715 "was inserted, so it will not actually appear in "
3716 "the database", node
->name
);
3718 VLOG_WARN("row id \"%s\" was created but only a weak "
3719 "reference to it was inserted, so it will not "
3720 "actually appear in the database", node
->name
);
3725 status
= ovsdb_idl_txn_commit_block(txn
);
3726 if (status
== TXN_UNCHANGED
|| status
== TXN_SUCCESS
) {
3727 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3728 if (c
->syntax
->postprocess
) {
3729 struct vtep_ctl_context ctx
;
3731 vtep_ctl_context_init(&ctx
, c
, idl
, txn
, vtep_global
, symtab
);
3732 (c
->syntax
->postprocess
)(&ctx
);
3733 vtep_ctl_context_done(&ctx
, c
);
3737 error
= xstrdup(ovsdb_idl_txn_get_error(txn
));
3738 ovsdb_idl_txn_destroy(txn
);
3739 txn
= the_idl_txn
= NULL
;
3742 case TXN_UNCOMMITTED
:
3743 case TXN_INCOMPLETE
:
3747 /* Should not happen--we never call ovsdb_idl_txn_abort(). */
3748 vtep_ctl_fatal("transaction aborted");
3758 vtep_ctl_fatal("transaction error: %s", error
);
3760 case TXN_NOT_LOCKED
:
3761 /* Should not happen--we never call ovsdb_idl_set_lock(). */
3762 vtep_ctl_fatal("database not locked");
3769 ovsdb_symbol_table_destroy(symtab
);
3771 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3772 struct ds
*ds
= &c
->output
;
3775 table_print(c
->table
, &table_style
);
3776 } else if (oneline
) {
3780 for (j
= 0; j
< ds
->length
; j
++) {
3781 int ch
= ds
->string
[j
];
3784 fputs("\\n", stdout
);
3788 fputs("\\\\", stdout
);
3797 fputs(ds_cstr(ds
), stdout
);
3799 ds_destroy(&c
->output
);
3800 table_destroy(c
->table
);
3803 shash_destroy_free_data(&c
->options
);
3807 ovsdb_idl_destroy(idl
);
3812 /* Our transaction needs to be rerun, or a prerequisite was not met. Free
3813 * resources and return so that the caller can try again. */
3815 ovsdb_idl_txn_abort(txn
);
3816 ovsdb_idl_txn_destroy(txn
);
3818 ovsdb_symbol_table_destroy(symtab
);
3819 for (c
= commands
; c
< &commands
[n_commands
]; c
++) {
3820 ds_destroy(&c
->output
);
3821 table_destroy(c
->table
);
3827 static const struct vtep_ctl_command_syntax all_commands
[] = {
3828 /* Physical Switch commands. */
3829 {"add-ps", 1, 1, pre_get_info
, cmd_add_ps
, NULL
, "--may-exist", RW
},
3830 {"del-ps", 1, 1, pre_get_info
, cmd_del_ps
, NULL
, "--if-exists", RW
},
3831 {"list-ps", 0, 0, pre_get_info
, cmd_list_ps
, NULL
, "", RO
},
3832 {"ps-exists", 1, 1, pre_get_info
, cmd_ps_exists
, NULL
, "", RO
},
3834 /* Port commands. */
3835 {"list-ports", 1, 1, pre_get_info
, cmd_list_ports
, NULL
, "", RO
},
3836 {"add-port", 2, 2, pre_get_info
, cmd_add_port
, NULL
, "--may-exist",
3838 {"del-port", 2, 2, pre_get_info
, cmd_del_port
, NULL
, "--if-exists", RW
},
3840 /* Logical Switch commands. */
3841 {"add-ls", 1, 1, pre_get_info
, cmd_add_ls
, NULL
, "--may-exist", RW
},
3842 {"del-ls", 1, 1, pre_get_info
, cmd_del_ls
, NULL
, "--if-exists", RW
},
3843 {"list-ls", 0, 0, pre_get_info
, cmd_list_ls
, NULL
, "", RO
},
3844 {"ls-exists", 1, 1, pre_get_info
, cmd_ls_exists
, NULL
, "", RO
},
3845 {"list-bindings", 2, 2, pre_get_info
, cmd_list_bindings
, NULL
, "", RO
},
3846 {"bind-ls", 4, 4, pre_get_info
, cmd_bind_ls
, NULL
, "", RO
},
3847 {"unbind-ls", 3, 3, pre_get_info
, cmd_unbind_ls
, NULL
, "", RO
},
3849 /* MAC binding commands. */
3850 {"add-ucast-local", 3, 4, pre_get_info
, cmd_add_ucast_local
, NULL
, "", RW
},
3851 {"del-ucast-local", 2, 2, pre_get_info
, cmd_del_ucast_local
, NULL
, "", RW
},
3852 {"add-mcast-local", 3, 4, pre_get_info
, cmd_add_mcast_local
, NULL
, "", RW
},
3853 {"del-mcast-local", 3, 4, pre_get_info
, cmd_del_mcast_local
, NULL
, "", RW
},
3854 {"clear-local-macs", 1, 1, pre_get_info
, cmd_clear_local_macs
, NULL
, "",
3856 {"list-local-macs", 1, 1, pre_get_info
, cmd_list_local_macs
, NULL
, "", RO
},
3857 {"add-ucast-remote", 3, 4, pre_get_info
, cmd_add_ucast_remote
, NULL
, "",
3859 {"del-ucast-remote", 2, 2, pre_get_info
, cmd_del_ucast_remote
, NULL
, "",
3861 {"add-mcast-remote", 3, 4, pre_get_info
, cmd_add_mcast_remote
, NULL
, "",
3863 {"del-mcast-remote", 3, 4, pre_get_info
, cmd_del_mcast_remote
, NULL
, "",
3865 {"clear-remote-macs", 1, 1, pre_get_info
, cmd_clear_remote_macs
, NULL
, "",
3867 {"list-remote-macs", 1, 1, pre_get_info
, cmd_list_remote_macs
, NULL
, "",
3870 /* Manager commands. */
3871 {"get-manager", 0, 0, pre_manager
, cmd_get_manager
, NULL
, "", RO
},
3872 {"del-manager", 0, 0, pre_manager
, cmd_del_manager
, NULL
, "", RW
},
3873 {"set-manager", 1, INT_MAX
, pre_manager
, cmd_set_manager
, NULL
, "", RW
},
3875 /* Database commands. */
3876 {"comment", 0, INT_MAX
, NULL
, NULL
, NULL
, "", RO
},
3877 {"get", 2, INT_MAX
, pre_cmd_get
, cmd_get
, NULL
, "--if-exists,--id=", RO
},
3878 {"list", 1, INT_MAX
, pre_cmd_list
, cmd_list
, NULL
, "--columns=", RO
},
3879 {"find", 1, INT_MAX
, pre_cmd_find
, cmd_find
, NULL
, "--columns=", RO
},
3880 {"set", 3, INT_MAX
, pre_cmd_set
, cmd_set
, NULL
, "", RW
},
3881 {"add", 4, INT_MAX
, pre_cmd_add
, cmd_add
, NULL
, "", RW
},
3882 {"remove", 4, INT_MAX
, pre_cmd_remove
, cmd_remove
, NULL
, "", RW
},
3883 {"clear", 3, INT_MAX
, pre_cmd_clear
, cmd_clear
, NULL
, "", RW
},
3884 {"create", 2, INT_MAX
, pre_create
, cmd_create
, post_create
, "--id=", RW
},
3885 {"destroy", 1, INT_MAX
, pre_cmd_destroy
, cmd_destroy
, NULL
,
3886 "--if-exists,--all", RW
},
3887 {"wait-until", 2, INT_MAX
, pre_cmd_wait_until
, cmd_wait_until
, NULL
, "",
3890 {NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
, RO
},