/
ovn-nbctl.c
5572 lines (4890 loc) · 169 KB
/
ovn-nbctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include "command-line.h"
#include "daemon.h"
#include "db-ctl-base.h"
#include "dirs.h"
#include "fatal-signal.h"
#include "jsonrpc.h"
#include "openvswitch/json.h"
#include "ovn/lib/acl-log.h"
#include "ovn/lib/ovn-nb-idl.h"
#include "ovn/lib/ovn-util.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "process.h"
#include "smap.h"
#include "sset.h"
#include "stream.h"
#include "stream-ssl.h"
#include "svec.h"
#include "table.h"
#include "timeval.h"
#include "timer.h"
#include "unixctl.h"
#include "util.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(nbctl);
/* --db: The database server to contact. */
static const char *db;
/* --oneline: Write each command's output as a single line? */
static bool oneline;
/* --dry-run: Do not commit any changes. */
static bool dry_run;
/* --wait=TYPE: Wait for configuration change to take effect? */
enum nbctl_wait_type {
NBCTL_WAIT_NONE, /* Do not wait. */
NBCTL_WAIT_SB, /* Wait for southbound database updates. */
NBCTL_WAIT_HV /* Wait for hypervisors to catch up. */
};
static enum nbctl_wait_type wait_type = NBCTL_WAIT_NONE;
/* Should we wait (if specified by 'wait_type') even if the commands don't
* change the database at all? */
static bool force_wait = false;
/* --timeout: Time to wait for a connection to 'db'. */
static unsigned int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
/* The IDL we're using and the current transaction, if any.
* This is for use by nbctl_exit() only, to allow it to clean up.
* Other code should use its context arguments. */
static struct ovsdb_idl *the_idl;
static struct ovsdb_idl_txn *the_idl_txn;
OVS_NO_RETURN static void nbctl_exit(int status);
/* --leader-only, --no-leader-only: Only accept the leader in a cluster. */
static int leader_only = true;
/* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
commands. */
static char *unixctl_path;
static unixctl_cb_func server_cmd_exit;
static unixctl_cb_func server_cmd_run;
static void nbctl_cmd_init(void);
OVS_NO_RETURN static void usage(void);
static struct option *get_all_options(void);
static bool has_option(const struct ovs_cmdl_parsed_option *, size_t n,
int option);
static void nbctl_client(const char *socket_name,
const struct ovs_cmdl_parsed_option *, size_t n,
int argc, char *argv[]);
static bool will_detach(const struct ovs_cmdl_parsed_option *, size_t n);
static void apply_options_direct(const struct ovs_cmdl_parsed_option *,
size_t n, struct shash *local_options);
static char * OVS_WARN_UNUSED_RESULT run_prerequisites(struct ctl_command[],
size_t n_commands,
struct ovsdb_idl *);
static char * OVS_WARN_UNUSED_RESULT do_nbctl(const char *args,
struct ctl_command *, size_t n,
struct ovsdb_idl *,
const struct timer *,
bool *retry);
static char * OVS_WARN_UNUSED_RESULT dhcp_options_get(
struct ctl_context *ctx, const char *id, bool must_exist,
const struct nbrec_dhcp_options **);
static char * OVS_WARN_UNUSED_RESULT main_loop(const char *args,
struct ctl_command *commands,
size_t n_commands,
struct ovsdb_idl *idl,
const struct timer *);
static void server_loop(struct ovsdb_idl *idl);
int
main(int argc, char *argv[])
{
struct ovsdb_idl *idl;
struct shash local_options;
set_program_name(argv[0]);
fatal_ignore_sigpipe();
vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
vlog_set_levels_from_string_assert("reconnect:warn");
nbctl_cmd_init();
/* ovn-nbctl has three operation modes:
*
* - Direct: Executes commands by contacting ovsdb-server directly.
*
* - Server: Runs in the background as a daemon waiting for requests
* from ovn-nbctl running in client mode.
*
* - Client: Executes commands by passing them to an ovn-nbctl running
* in the server mode.
*
* At this point we don't know what mode we're running in. The mode partly
* depends on the command line. So, for now we transform the command line
* into a parsed form, and figure out what to do with it later.
*/
char *args = process_escape_args(argv);
struct ovs_cmdl_parsed_option *parsed_options;
size_t n_parsed_options;
char *error_s = ovs_cmdl_parse_all(argc, argv, get_all_options(),
&parsed_options, &n_parsed_options);
if (error_s) {
ctl_fatal("%s", error_s);
}
/* Now figure out the operation mode:
*
* - A --detach option implies server mode.
*
* - An OVN_NB_DAEMON environment variable implies client mode.
*
* - Otherwise, we're in direct mode. */
char *socket_name = getenv("OVN_NB_DAEMON");
if (socket_name && socket_name[0]
&& !will_detach(parsed_options, n_parsed_options)) {
nbctl_client(socket_name, parsed_options, n_parsed_options,
argc, argv);
}
/* Parse command line. */
shash_init(&local_options);
apply_options_direct(parsed_options, n_parsed_options, &local_options);
free(parsed_options);
argc -= optind;
argv += optind;
/* Initialize IDL. */
idl = the_idl = ovsdb_idl_create(db, &nbrec_idl_class, true, false);
ovsdb_idl_set_leader_only(idl, leader_only);
if (get_detach()) {
if (argc != 0) {
ctl_fatal("non-option arguments not supported with --detach "
"(use --help for help)");
}
server_loop(idl);
} else {
struct ctl_command *commands;
size_t n_commands;
char *error;
error = ctl_parse_commands(argc, argv, &local_options, &commands,
&n_commands);
if (error) {
ctl_fatal("%s", error);
}
VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
"Called as %s", args);
ctl_timeout_setup(timeout);
error = run_prerequisites(commands, n_commands, idl);
if (error) {
ctl_fatal("%s", error);
}
error = main_loop(args, commands, n_commands, idl, NULL);
if (error) {
ctl_fatal("%s", error);
}
struct ctl_command *c;
for (c = commands; c < &commands[n_commands]; c++) {
ds_destroy(&c->output);
table_destroy(c->table);
free(c->table);
shash_destroy_free_data(&c->options);
}
free(commands);
}
ovsdb_idl_destroy(idl);
idl = the_idl = NULL;
free(args);
exit(EXIT_SUCCESS);
}
static char *
main_loop(const char *args, struct ctl_command *commands, size_t n_commands,
struct ovsdb_idl *idl, const struct timer *wait_timeout)
{
unsigned int seqno;
bool idl_ready;
/* Execute the commands.
*
* 'seqno' is the database sequence number for which we last tried to
* execute our transaction. There's no point in trying to commit more than
* once for any given sequence number, because if the transaction fails
* it's because the database changed and we need to obtain an up-to-date
* view of the database before we try the transaction again. */
seqno = ovsdb_idl_get_seqno(idl);
/* IDL might have already obtained the database copy during previous
* invocation. If so, we can't expect the sequence number to change before
* we issue any new requests. */
idl_ready = ovsdb_idl_has_ever_connected(idl);
for (;;) {
ovsdb_idl_run(idl);
if (!ovsdb_idl_is_alive(idl)) {
int retval = ovsdb_idl_get_last_error(idl);
ctl_fatal("%s: database connection failed (%s)",
db, ovs_retval_to_string(retval));
}
if (idl_ready || seqno != ovsdb_idl_get_seqno(idl)) {
idl_ready = false;
seqno = ovsdb_idl_get_seqno(idl);
bool retry;
char *error = do_nbctl(args, commands, n_commands, idl,
wait_timeout, &retry);
if (error) {
return error;
}
if (!retry) {
return NULL;
}
}
if (seqno == ovsdb_idl_get_seqno(idl)) {
ovsdb_idl_wait(idl);
poll_block();
}
}
return NULL;
}
/* All options that affect the main loop and are not external. */
#define MAIN_LOOP_OPTION_ENUMS \
OPT_NO_WAIT, \
OPT_WAIT, \
OPT_DRY_RUN, \
OPT_ONELINE
#define MAIN_LOOP_LONG_OPTIONS \
{"no-wait", no_argument, NULL, OPT_NO_WAIT}, \
{"wait", required_argument, NULL, OPT_WAIT}, \
{"dry-run", no_argument, NULL, OPT_DRY_RUN}, \
{"oneline", no_argument, NULL, OPT_ONELINE}, \
{"timeout", required_argument, NULL, 't'}
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_NO_SYSLOG,
OPT_LOCAL,
OPT_COMMANDS,
OPT_OPTIONS,
OPT_LEADER_ONLY,
OPT_NO_LEADER_ONLY,
OPT_BOOTSTRAP_CA_CERT,
MAIN_LOOP_OPTION_ENUMS,
DAEMON_OPTION_ENUMS,
VLOG_OPTION_ENUMS,
TABLE_OPTION_ENUMS,
SSL_OPTION_ENUMS,
};
static char * OVS_WARN_UNUSED_RESULT
handle_main_loop_option(int opt, const char *arg, bool *handled)
{
ovs_assert(handled);
*handled = true;
switch (opt) {
case OPT_ONELINE:
oneline = true;
break;
case OPT_NO_WAIT:
wait_type = NBCTL_WAIT_NONE;
break;
case OPT_WAIT:
if (!strcmp(arg, "none")) {
wait_type = NBCTL_WAIT_NONE;
} else if (!strcmp(arg, "sb")) {
wait_type = NBCTL_WAIT_SB;
} else if (!strcmp(arg, "hv")) {
wait_type = NBCTL_WAIT_HV;
} else {
return xstrdup("argument to --wait must be "
"\"none\", \"sb\", or \"hv\"");
}
break;
case OPT_DRY_RUN:
dry_run = true;
break;
case 't':
if (!str_to_uint(arg, 10, &timeout) || !timeout) {
return xasprintf("value %s on -t or --timeout is invalid", arg);
}
break;
default:
*handled = false;
break;
}
return NULL;
}
static char * OVS_WARN_UNUSED_RESULT
build_short_options(const struct option *long_options, bool print_errors)
{
char *tmp, *short_options;
tmp = ovs_cmdl_long_options_to_short_options(long_options);
short_options = xasprintf("+%s%s", print_errors ? "" : ":", tmp);
free(tmp);
return short_options;
}
static struct option * OVS_WARN_UNUSED_RESULT
append_command_options(const struct option *options, int opt_val)
{
struct option *o;
size_t n_allocated;
size_t n_existing;
int i;
for (i = 0; options[i].name; i++) {
;
}
n_allocated = i + 1;
n_existing = i;
/* We want to parse both global and command-specific options here, but
* getopt_long() isn't too convenient for the job. We copy our global
* options into a dynamic array, then append all of the command-specific
* options. */
o = xmemdup(options, n_allocated * sizeof *options);
ctl_add_cmd_options(&o, &n_existing, &n_allocated, opt_val);
return o;
}
static struct option *
get_all_options(void)
{
static const struct option global_long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
{"help", no_argument, NULL, 'h'},
{"commands", no_argument, NULL, OPT_COMMANDS},
{"options", no_argument, NULL, OPT_OPTIONS},
{"leader-only", no_argument, NULL, OPT_LEADER_ONLY},
{"no-leader-only", no_argument, NULL, OPT_NO_LEADER_ONLY},
{"version", no_argument, NULL, 'V'},
MAIN_LOOP_LONG_OPTIONS,
DAEMON_LONG_OPTIONS,
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
TABLE_LONG_OPTIONS,
{NULL, 0, NULL, 0},
};
static struct option *options;
if (!options) {
options = append_command_options(global_long_options, OPT_LOCAL);
}
return options;
}
static bool
has_option(const struct ovs_cmdl_parsed_option *parsed_options, size_t n,
int option)
{
for (const struct ovs_cmdl_parsed_option *po = parsed_options;
po < &parsed_options[n]; po++) {
if (po->o->val == option) {
return true;
}
}
return false;
}
static bool
will_detach(const struct ovs_cmdl_parsed_option *parsed_options, size_t n)
{
return has_option(parsed_options, n, OPT_DETACH);
}
static char * OVS_WARN_UNUSED_RESULT
add_local_option(const char *name, const char *arg,
struct shash *local_options)
{
char *full_name = xasprintf("--%s", name);
if (shash_find(local_options, full_name)) {
char *error = xasprintf("'%s' option specified multiple times",
full_name);
free(full_name);
return error;
}
shash_add_nocopy(local_options, full_name, nullable_xstrdup(arg));
return NULL;
}
static void
apply_options_direct(const struct ovs_cmdl_parsed_option *parsed_options,
size_t n, struct shash *local_options)
{
for (const struct ovs_cmdl_parsed_option *po = parsed_options;
po < &parsed_options[n]; po++) {
bool handled;
char *error = handle_main_loop_option(po->o->val, po->arg, &handled);
if (error) {
ctl_fatal("%s", error);
}
if (handled) {
continue;
}
optarg = po->arg;
switch (po->o->val) {
case OPT_DB:
db = po->arg;
break;
case OPT_NO_SYSLOG:
vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
break;
case OPT_LOCAL:
error = add_local_option(po->o->name, po->arg, local_options);
if (error) {
ctl_fatal("%s", error);
}
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
case OPT_COMMANDS:
ctl_print_commands();
/* fall through */
case OPT_OPTIONS:
ctl_print_options(get_all_options());
/* fall through */
case OPT_LEADER_ONLY:
leader_only = true;
break;
case OPT_NO_LEADER_ONLY:
leader_only = false;
break;
case 'V':
ovs_print_version(0, 0);
printf("DB Schema %s\n", nbrec_get_db_version());
exit(EXIT_SUCCESS);
DAEMON_OPTION_HANDLERS
VLOG_OPTION_HANDLERS
TABLE_OPTION_HANDLERS(&table_style)
STREAM_SSL_OPTION_HANDLERS
case OPT_BOOTSTRAP_CA_CERT:
stream_ssl_set_ca_cert_file(po->arg, true);
break;
case '?':
exit(EXIT_FAILURE);
default:
abort();
case 0:
break;
}
}
if (!db) {
db = default_nb_db();
}
}
static void
usage(void)
{
printf("\
%s: OVN northbound DB management utility\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
General commands:\n\
init initialize the database\n\
show print overview of database contents\n\
show SWITCH print overview of database contents for SWITCH\n\
show ROUTER print overview of database contents for ROUTER\n\
\n\
Logical switch commands:\n\
ls-add [SWITCH] create a logical switch named SWITCH\n\
ls-del SWITCH delete SWITCH and all its ports\n\
ls-list print the names of all logical switches\n\
\n\
ACL commands:\n\
[--type={switch | port-group}] [--log] [--severity=SEVERITY] [--name=NAME] [--may-exist]\n\
acl-add {SWITCH | PORTGROUP} DIRECTION PRIORITY MATCH ACTION\n\
add an ACL to SWITCH/PORTGROUP\n\
[--type={switch | port-group}]\n\
acl-del {SWITCH | PORTGROUP} [DIRECTION [PRIORITY MATCH]]\n\
remove ACLs from SWITCH/PORTGROUP\n\
[--type={switch | port-group}]\n\
acl-list {SWITCH | PORTGROUP}\n\
print ACLs for SWITCH\n\
\n\
QoS commands:\n\
qos-add SWITCH DIRECTION PRIORITY MATCH [rate=RATE [burst=BURST]] [dscp=DSCP]\n\
add an QoS rule to SWITCH\n\
qos-del SWITCH [DIRECTION [PRIORITY MATCH]]\n\
remove QoS rules from SWITCH\n\
qos-list SWITCH print QoS rules for SWITCH\n\
\n\
Meter commands:\n\
meter-add NAME ACTION RATE UNIT [BURST]\n\
add a meter\n\
meter-del [NAME] remove meters\n\
meter-list print meters\n\
\n\
Logical switch port commands:\n\
lsp-add SWITCH PORT add logical port PORT on SWITCH\n\
lsp-add SWITCH PORT PARENT TAG\n\
add logical port PORT on SWITCH with PARENT\n\
on TAG\n\
lsp-del PORT delete PORT from its attached switch\n\
lsp-list SWITCH print the names of all logical ports on SWITCH\n\
lsp-get-parent PORT get the parent of PORT if set\n\
lsp-get-tag PORT get the PORT's tag if set\n\
lsp-set-addresses PORT [ADDRESS]...\n\
set MAC or MAC+IP addresses for PORT.\n\
lsp-get-addresses PORT get a list of MAC or MAC+IP addresses on PORT\n\
lsp-set-port-security PORT [ADDRS]...\n\
set port security addresses for PORT.\n\
lsp-get-port-security PORT get PORT's port security addresses\n\
lsp-get-up PORT get state of PORT ('up' or 'down')\n\
lsp-set-enabled PORT STATE\n\
set administrative state PORT\n\
('enabled' or 'disabled')\n\
lsp-get-enabled PORT get administrative state PORT\n\
('enabled' or 'disabled')\n\
lsp-set-type PORT TYPE set the type for PORT\n\
lsp-get-type PORT get the type for PORT\n\
lsp-set-options PORT KEY=VALUE [KEY=VALUE]...\n\
set options related to the type of PORT\n\
lsp-get-options PORT get the type specific options for PORT\n\
lsp-set-dhcpv4-options PORT [DHCP_OPTIONS_UUID]\n\
set dhcpv4 options for PORT\n\
lsp-get-dhcpv4-options PORT get the dhcpv4 options for PORT\n\
lsp-set-dhcpv6-options PORT [DHCP_OPTIONS_UUID]\n\
set dhcpv6 options for PORT\n\
lsp-get-dhcpv6-options PORT get the dhcpv6 options for PORT\n\
\n\
Logical router commands:\n\
lr-add [ROUTER] create a logical router named ROUTER\n\
lr-del ROUTER delete ROUTER and all its ports\n\
lr-list print the names of all logical routers\n\
\n\
Logical router port commands:\n\
lrp-add ROUTER PORT MAC NETWORK... [peer=PEER]\n\
add logical port PORT on ROUTER\n\
lrp-set-gateway-chassis PORT CHASSIS [PRIORITY]\n\
set gateway chassis for port PORT\n\
lrp-del-gateway-chassis PORT CHASSIS\n\
delete gateway chassis from port PORT\n\
lrp-get-gateway-chassis PORT\n\
print the names of all gateway chassis on PORT\n\
with PRIORITY\n\
lrp-del PORT delete PORT from its attached router\n\
lrp-list ROUTER print the names of all ports on ROUTER\n\
lrp-set-enabled PORT STATE\n\
set administrative state PORT\n\
('enabled' or 'disabled')\n\
lrp-get-enabled PORT get administrative state PORT\n\
('enabled' or 'disabled')\n\
\n\
Route commands:\n\
[--policy=POLICY] lr-route-add ROUTER PREFIX NEXTHOP [PORT]\n\
add a route to ROUTER\n\
lr-route-del ROUTER [PREFIX]\n\
remove routes from ROUTER\n\
lr-route-list ROUTER print routes for ROUTER\n\
\n\
NAT commands:\n\
lr-nat-add ROUTER TYPE EXTERNAL_IP LOGICAL_IP [LOGICAL_PORT EXTERNAL_MAC]\n\
add a NAT to ROUTER\n\
lr-nat-del ROUTER [TYPE [IP]]\n\
remove NATs from ROUTER\n\
lr-nat-list ROUTER print NATs for ROUTER\n\
\n\
LB commands:\n\
lb-add LB VIP[:PORT] IP[:PORT]... [PROTOCOL]\n\
create a load-balancer or add a VIP to an\n\
existing load balancer\n\
lb-del LB [VIP] remove a load-balancer or just the VIP from\n\
the load balancer\n\
lb-list [LB] print load-balancers\n\
lr-lb-add ROUTER LB add a load-balancer to ROUTER\n\
lr-lb-del ROUTER [LB] remove load-balancers from ROUTER\n\
lr-lb-list ROUTER print load-balancers\n\
ls-lb-add SWITCH LB add a load-balancer to SWITCH\n\
ls-lb-del SWITCH [LB] remove load-balancers from SWITCH\n\
ls-lb-list SWITCH print load-balancers\n\
\n\
DHCP Options commands:\n\
dhcp-options-create CIDR [EXTERNAL_IDS]\n\
create a DHCP options row with CIDR\n\
dhcp-options-del DHCP_OPTIONS_UUID\n\
delete DHCP_OPTIONS_UUID\n\
dhcp-options-list \n\
lists the DHCP_Options rows\n\
dhcp-options-set-options DHCP_OPTIONS_UUID KEY=VALUE [KEY=VALUE]...\n\
set DHCP options for DHCP_OPTIONS_UUID\n\
dhcp-options-get-options DHCO_OPTIONS_UUID \n\
displays the DHCP options for DHCP_OPTIONS_UUID\n\
\n\
Connection commands:\n\
get-connection print the connections\n\
del-connection delete the connections\n\
[--inactivity-probe=MSECS]\n\
set-connection TARGET... set the list of connections to TARGET...\n\
\n\
SSL commands:\n\
get-ssl print the SSL configuration\n\
del-ssl delete the SSL configuration\n\
set-ssl PRIV-KEY CERT CA-CERT [SSL-PROTOS [SSL-CIPHERS]] \
set the SSL configuration\n\
Port group commands:\n\
pg-add PG [PORTS] Create port group PG with optional PORTS\n\
pg-set-ports PG PORTS Set PORTS on port group PG\n\
pg-del PG Delete port group PG\n\
\n\
%s\
%s\
\n\
Synchronization command (use with --wait=sb|hv):\n\
sync wait even for earlier changes to take effect\n\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--no-wait, --wait=none do not wait for OVN reconfiguration (default)\n\
--no-leader-only accept any cluster member, not just the leader\n\
--wait=sb wait for southbound database update\n\
--wait=hv wait for all chassis to catch up\n\
-t, --timeout=SECS wait at most SECS seconds\n\
--dry-run do not commit changes to database\n\
--oneline print exactly one line of output per command\n",
program_name, program_name, ctl_get_db_cmd_usage(),
ctl_list_db_tables_usage(), default_nb_db());
table_usage();
daemon_usage();
vlog_usage();
printf("\
--no-syslog equivalent to --verbose=nbctl:syslog:warn\n");
printf("\n\
Other options:\n\
-h, --help display this help message\n\
-V, --version display version information\n");
stream_usage("database", true, true, true);
exit(EXIT_SUCCESS);
}
/* One should not use ctl_fatal() within commands because it will kill the
* daemon if we're in daemon mode. Use ctl_error() instead and return
* gracefully. */
#define ctl_fatal dont_use_ctl_fatal_use_ctl_error_and_return
/* Find a logical router given its id. */
static char * OVS_WARN_UNUSED_RESULT
lr_by_name_or_uuid(struct ctl_context *ctx, const char *id,
bool must_exist, const struct nbrec_logical_router **lr_p)
{
const struct nbrec_logical_router *lr = NULL;
bool is_uuid = false;
struct uuid lr_uuid;
*lr_p = NULL;
if (uuid_from_string(&lr_uuid, id)) {
is_uuid = true;
lr = nbrec_logical_router_get_for_uuid(ctx->idl, &lr_uuid);
}
if (!lr) {
const struct nbrec_logical_router *iter;
NBREC_LOGICAL_ROUTER_FOR_EACH(iter, ctx->idl) {
if (strcmp(iter->name, id)) {
continue;
}
if (lr) {
return xasprintf("Multiple logical routers named '%s'. "
"Use a UUID.", id);
}
lr = iter;
}
}
if (!lr && must_exist) {
return xasprintf("%s: router %s not found",
id, is_uuid ? "UUID" : "name");
}
*lr_p = lr;
return NULL;
}
static char * OVS_WARN_UNUSED_RESULT
ls_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
const struct nbrec_logical_switch **ls_p)
{
const struct nbrec_logical_switch *ls = NULL;
*ls_p = NULL;
struct uuid ls_uuid;
bool is_uuid = uuid_from_string(&ls_uuid, id);
if (is_uuid) {
ls = nbrec_logical_switch_get_for_uuid(ctx->idl, &ls_uuid);
}
if (!ls) {
const struct nbrec_logical_switch *iter;
NBREC_LOGICAL_SWITCH_FOR_EACH(iter, ctx->idl) {
if (strcmp(iter->name, id)) {
continue;
}
if (ls) {
return xasprintf("Multiple logical switches named '%s'. "
"Use a UUID.", id);
}
ls = iter;
}
}
if (!ls && must_exist) {
return xasprintf("%s: switch %s not found",
id, is_uuid ? "UUID" : "name");
}
*ls_p = ls;
return NULL;
}
static char * OVS_WARN_UNUSED_RESULT
lb_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
const struct nbrec_load_balancer **lb_p)
{
const struct nbrec_load_balancer *lb = NULL;
struct uuid lb_uuid;
bool is_uuid = uuid_from_string(&lb_uuid, id);
if (is_uuid) {
lb = nbrec_load_balancer_get_for_uuid(ctx->idl, &lb_uuid);
}
if (!lb) {
const struct nbrec_load_balancer *iter;
NBREC_LOAD_BALANCER_FOR_EACH(iter, ctx->idl) {
if (strcmp(iter->name, id)) {
continue;
}
if (lb) {
return xasprintf("Multiple load balancers named '%s'. "
"Use a UUID.", id);
}
lb = iter;
}
}
if (!lb && must_exist) {
return xasprintf("%s: load balancer %s not found", id,
is_uuid ? "UUID" : "name");
}
if (lb_p) {
*lb_p = lb;
}
return NULL;
}
static char * OVS_WARN_UNUSED_RESULT
pg_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
const struct nbrec_port_group **pg_p)
{
const struct nbrec_port_group *pg = NULL;
*pg_p = NULL;
struct uuid pg_uuid;
bool is_uuid = uuid_from_string(&pg_uuid, id);
if (is_uuid) {
pg = nbrec_port_group_get_for_uuid(ctx->idl, &pg_uuid);
}
if (!pg) {
const struct nbrec_port_group *iter;
NBREC_PORT_GROUP_FOR_EACH (iter, ctx->idl) {
if (!strcmp(iter->name, id)) {
pg = iter;
break;
}
}
}
if (!pg && must_exist) {
return xasprintf("%s: port group %s not found", id,
is_uuid ? "UUID" : "name");
}
*pg_p = pg;
return NULL;
}
static void
print_alias(const struct smap *external_ids, const char *key, struct ds *s)
{
const char *alias = smap_get(external_ids, key);
if (alias && alias[0]) {
ds_put_format(s, " (aka %s)", alias);
}
}
/* gateway_chassis ordering
* */
static int
compare_chassis_prio_(const void *gc1_, const void *gc2_)
{
const struct nbrec_gateway_chassis *const *gc1p = gc1_;
const struct nbrec_gateway_chassis *const *gc2p = gc2_;
const struct nbrec_gateway_chassis *gc1 = *gc1p;
const struct nbrec_gateway_chassis *gc2 = *gc2p;
int prio_diff = gc2->priority - gc1->priority;
if (!prio_diff) {
return strcmp(gc2->name, gc1->name);
}
return prio_diff;
}
static const struct nbrec_gateway_chassis **
get_ordered_gw_chassis_prio_list(const struct nbrec_logical_router_port *lrp)
{
const struct nbrec_gateway_chassis **gcs;
int i;
gcs = xmalloc(sizeof *gcs * lrp->n_gateway_chassis);
for (i = 0; i < lrp->n_gateway_chassis; i++) {
gcs[i] = lrp->gateway_chassis[i];
}
qsort(gcs, lrp->n_gateway_chassis, sizeof *gcs, compare_chassis_prio_);
return gcs;
}
/* Given pointer to logical router, this routine prints the router
* information. */
static void
print_lr(const struct nbrec_logical_router *lr, struct ds *s)
{
ds_put_format(s, "router "UUID_FMT" (%s)",
UUID_ARGS(&lr->header_.uuid), lr->name);
print_alias(&lr->external_ids, "neutron:router_name", s);
ds_put_char(s, '\n');
for (size_t i = 0; i < lr->n_ports; i++) {
const struct nbrec_logical_router_port *lrp = lr->ports[i];
ds_put_format(s, " port %s\n", lrp->name);
if (lrp->mac) {
ds_put_cstr(s, " mac: ");
ds_put_format(s, "\"%s\"\n", lrp->mac);
}
if (lrp->n_networks) {
ds_put_cstr(s, " networks: [");
for (size_t j = 0; j < lrp->n_networks; j++) {
ds_put_format(s, "%s\"%s\"",
j == 0 ? "" : ", ",
lrp->networks[j]);
}
ds_put_cstr(s, "]\n");
}
if (lrp->n_gateway_chassis) {
const struct nbrec_gateway_chassis **gcs;
gcs = get_ordered_gw_chassis_prio_list(lrp);
ds_put_cstr(s, " gateway chassis: [");
for (size_t j = 0; j < lrp->n_gateway_chassis; j++) {
const struct nbrec_gateway_chassis *gc = gcs[j];
ds_put_format(s, "%s ", gc->chassis_name);
}
ds_chomp(s, ' ');
ds_put_cstr(s, "]\n");
free(gcs);
}
}
for (size_t i = 0; i < lr->n_nat; i++) {
const struct nbrec_nat *nat = lr->nat[i];
ds_put_format(s, " nat "UUID_FMT"\n",
UUID_ARGS(&nat->header_.uuid));
ds_put_cstr(s, " external ip: ");
ds_put_format(s, "\"%s\"\n", nat->external_ip);
ds_put_cstr(s, " logical ip: ");
ds_put_format(s, "\"%s\"\n", nat->logical_ip);
ds_put_cstr(s, " type: ");
ds_put_format(s, "\"%s\"\n", nat->type);
}
}
static void
print_ls(const struct nbrec_logical_switch *ls, struct ds *s)
{
ds_put_format(s, "switch "UUID_FMT" (%s)",
UUID_ARGS(&ls->header_.uuid), ls->name);
print_alias(&ls->external_ids, "neutron:network_name", s);
ds_put_char(s, '\n');
for (size_t i = 0; i < ls->n_ports; i++) {
const struct nbrec_logical_switch_port *lsp = ls->ports[i];
ds_put_format(s, " port %s", lsp->name);
print_alias(&lsp->external_ids, "neutron:port_name", s);
ds_put_char(s, '\n');
if (lsp->type[0]) {
ds_put_format(s, " type: %s\n", lsp->type);
}
if (lsp->parent_name) {
ds_put_format(s, " parent: %s\n", lsp->parent_name);
}
if (lsp->n_tag) {
ds_put_format(s, " tag: %"PRIu64"\n", lsp->tag[0]);
}
/* Print the addresses, but not if there's just a single "router"
* address because that's just clutter. */
if (lsp->n_addresses