forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovn-nbctl.c
3426 lines (2965 loc) · 107 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 "db-ctl-base.h"
#include "dirs.h"
#include "fatal-signal.h"
#include "openvswitch/json.h"
#include "ovn/lib/ovn-nb-idl.h"
#include "ovn/lib/ovn-util.h"
#include "packets.h"
#include "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 "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 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);
static void nbctl_cmd_init(void);
OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[], struct shash *local_options);
static void run_prerequisites(struct ctl_command[], size_t n_commands,
struct ovsdb_idl *);
static bool do_nbctl(const char *args, struct ctl_command *, size_t n,
struct ovsdb_idl *);
static const struct nbrec_dhcp_options *dhcp_options_get(
struct ctl_context *ctx, const char *id, bool must_exist);
int
main(int argc, char *argv[])
{
struct ovsdb_idl *idl;
struct ctl_command *commands;
struct shash local_options;
unsigned int seqno;
size_t n_commands;
char *args;
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();
/* Log our arguments. This is often valuable for debugging systems. */
args = process_escape_args(argv);
VLOG(ctl_might_write_to_db(argv) ? VLL_INFO : VLL_DBG,
"Called as %s", args);
/* Parse command line. */
shash_init(&local_options);
parse_options(argc, argv, &local_options);
commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
&n_commands);
if (timeout) {
time_alarm(timeout);
}
/* Initialize IDL. */
idl = the_idl = ovsdb_idl_create(db, &nbrec_idl_class, true, false);
run_prerequisites(commands, n_commands, idl);
/* 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);
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 (seqno != ovsdb_idl_get_seqno(idl)) {
seqno = ovsdb_idl_get_seqno(idl);
if (do_nbctl(args, commands, n_commands, idl)) {
free(args);
exit(EXIT_SUCCESS);
}
}
if (seqno == ovsdb_idl_get_seqno(idl)) {
ovsdb_idl_wait(idl);
poll_block();
}
}
}
static void
parse_options(int argc, char *argv[], struct shash *local_options)
{
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_NO_SYSLOG,
OPT_NO_WAIT,
OPT_WAIT,
OPT_DRY_RUN,
OPT_ONELINE,
OPT_LOCAL,
OPT_COMMANDS,
OPT_OPTIONS,
OPT_BOOTSTRAP_CA_CERT,
VLOG_OPTION_ENUMS,
TABLE_OPTION_ENUMS,
SSL_OPTION_ENUMS,
};
static const struct option global_long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"no-syslog", no_argument, NULL, OPT_NO_SYSLOG},
{"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'},
{"help", no_argument, NULL, 'h'},
{"commands", no_argument, NULL, OPT_COMMANDS},
{"options", no_argument, NULL, OPT_OPTIONS},
{"version", no_argument, NULL, 'V'},
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
TABLE_LONG_OPTIONS,
{NULL, 0, NULL, 0},
};
const int n_global_long_options = ARRAY_SIZE(global_long_options) - 1;
char *tmp, *short_options;
struct option *options;
size_t allocated_options;
size_t n_options;
size_t i;
tmp = ovs_cmdl_long_options_to_short_options(global_long_options);
short_options = xasprintf("+%s", tmp);
free(tmp);
/* 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. */
options = xmemdup(global_long_options, sizeof global_long_options);
allocated_options = ARRAY_SIZE(global_long_options);
n_options = n_global_long_options;
ctl_add_cmd_options(&options, &n_options, &allocated_options, OPT_LOCAL);
for (;;) {
int idx;
int c;
c = getopt_long(argc, argv, short_options, options, &idx);
if (c == -1) {
break;
}
switch (c) {
case OPT_DB:
db = optarg;
break;
case OPT_ONELINE:
oneline = true;
break;
case OPT_NO_SYSLOG:
vlog_set_levels(&this_module, VLF_SYSLOG, VLL_WARN);
break;
case OPT_NO_WAIT:
wait_type = NBCTL_WAIT_NONE;
break;
case OPT_WAIT:
if (!strcmp(optarg, "none")) {
wait_type = NBCTL_WAIT_NONE;
} else if (!strcmp(optarg, "sb")) {
wait_type = NBCTL_WAIT_SB;
} else if (!strcmp(optarg, "hv")) {
wait_type = NBCTL_WAIT_HV;
} else {
ctl_fatal("argument to --wait must be "
"\"none\", \"sb\", or \"hv\"");
}
break;
case OPT_DRY_RUN:
dry_run = true;
break;
case OPT_LOCAL:
if (shash_find(local_options, options[idx].name)) {
ctl_fatal("'%s' option specified multiple times",
options[idx].name);
}
shash_add_nocopy(local_options,
xasprintf("--%s", options[idx].name),
nullable_xstrdup(optarg));
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
case OPT_COMMANDS:
ctl_print_commands();
case OPT_OPTIONS:
ctl_print_options(global_long_options);
case 'V':
ovs_print_version(0, 0);
printf("DB Schema %s\n", nbrec_get_db_version());
exit(EXIT_SUCCESS);
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout < 0) {
ctl_fatal("value %s on -t or --timeout is invalid", optarg);
}
break;
VLOG_OPTION_HANDLERS
TABLE_OPTION_HANDLERS(&table_style)
STREAM_SSL_OPTION_HANDLERS
case OPT_BOOTSTRAP_CA_CERT:
stream_ssl_set_ca_cert_file(optarg, true);
break;
case '?':
exit(EXIT_FAILURE);
default:
abort();
}
}
free(short_options);
if (!db) {
db = default_nb_db();
}
for (i = n_global_long_options; options[i].name; i++) {
free(CONST_CAST(char *, options[i].name));
}
free(options);
}
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\
acl-add SWITCH DIRECTION PRIORITY MATCH ACTION [log]\n\
add an ACL to SWITCH\n\
acl-del SWITCH [DIRECTION [PRIORITY MATCH]]\n\
remove ACLs from SWITCH\n\
acl-list SWITCH print ACLs for SWITCH\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\
\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-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\
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 set the SSL configuration\n\
\n\
%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\
--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(),
default_nb_db());
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");
exit(EXIT_SUCCESS);
}
/* Find a logical router given its id. */
static const struct nbrec_logical_router *
lr_by_name_or_uuid(struct ctl_context *ctx, const char *id,
bool must_exist)
{
const struct nbrec_logical_router *lr = NULL;
bool is_uuid = false;
struct uuid lr_uuid;
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) {
ctl_fatal("Multiple logical routers named '%s'. "
"Use a UUID.", id);
}
lr = iter;
}
}
if (!lr && must_exist) {
ctl_fatal("%s: router %s not found", id, is_uuid ? "UUID" : "name");
}
return lr;
}
static const struct nbrec_logical_switch *
ls_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
{
const struct nbrec_logical_switch *ls = 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) {
ctl_fatal("Multiple logical switches named '%s'. "
"Use a UUID.", id);
}
ls = iter;
}
}
if (!ls && must_exist) {
ctl_fatal("%s: switch %s not found", id, is_uuid ? "UUID" : "name");
}
return ls;
}
static const struct nbrec_load_balancer *
lb_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
{
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) {
ctl_fatal("Multiple load balancers named '%s'. "
"Use a UUID.", id);
}
lb = iter;
}
}
if (!lb && must_exist) {
ctl_fatal("%s: load balancer %s not found", id,
is_uuid ? "UUID" : "name");
}
return lb;
}
/* 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)\n",
UUID_ARGS(&lr->header_.uuid), lr->name);
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");
}
}
}
static void
print_ls(const struct nbrec_logical_switch *ls, struct ds *s)
{
ds_put_format(s, " switch "UUID_FMT" (%s)\n",
UUID_ARGS(&ls->header_.uuid), ls->name);
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\n", lsp->name);
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]);
}
if (lsp->n_addresses) {
ds_put_cstr(s, " addresses: [");
for (size_t j = 0; j < lsp->n_addresses; j++) {
ds_put_format(s, "%s\"%s\"",
j == 0 ? "" : ", ",
lsp->addresses[j]);
}
ds_put_cstr(s, "]\n");
}
}
}
static void
nbctl_init(struct ctl_context *ctx OVS_UNUSED)
{
}
static void
nbctl_pre_sync(struct ctl_context *ctx OVS_UNUSED)
{
if (wait_type != NBCTL_WAIT_NONE) {
force_wait = true;
} else {
VLOG_INFO("\"sync\" command has no effect without --wait");
}
}
static void
nbctl_sync(struct ctl_context *ctx OVS_UNUSED)
{
}
static void
nbctl_show(struct ctl_context *ctx)
{
const struct nbrec_logical_switch *ls;
if (ctx->argc == 2) {
ls = ls_by_name_or_uuid(ctx, ctx->argv[1], false);
if (ls) {
print_ls(ls, &ctx->output);
}
} else {
NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
print_ls(ls, &ctx->output);
}
}
const struct nbrec_logical_router *lr;
if (ctx->argc == 2) {
lr = lr_by_name_or_uuid(ctx, ctx->argv[1], false);
if (lr) {
print_lr(lr, &ctx->output);
}
} else {
NBREC_LOGICAL_ROUTER_FOR_EACH(lr, ctx->idl) {
print_lr(lr, &ctx->output);
}
}
}
static void
nbctl_ls_add(struct ctl_context *ctx)
{
const char *ls_name = ctx->argc == 2 ? ctx->argv[1] : NULL;
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
bool add_duplicate = shash_find(&ctx->options, "--add-duplicate") != NULL;
if (may_exist && add_duplicate) {
ctl_fatal("--may-exist and --add-duplicate may not be used together");
}
if (ls_name) {
if (!add_duplicate) {
const struct nbrec_logical_switch *ls;
NBREC_LOGICAL_SWITCH_FOR_EACH (ls, ctx->idl) {
if (!strcmp(ls->name, ls_name)) {
if (may_exist) {
return;
}
ctl_fatal("%s: a switch with this name already exists",
ls_name);
}
}
}
} else if (may_exist) {
ctl_fatal("--may-exist requires specifying a name");
} else if (add_duplicate) {
ctl_fatal("--add-duplicate requires specifying a name");
}
struct nbrec_logical_switch *ls;
ls = nbrec_logical_switch_insert(ctx->txn);
if (ls_name) {
nbrec_logical_switch_set_name(ls, ls_name);
}
}
static void
nbctl_ls_del(struct ctl_context *ctx)
{
bool must_exist = !shash_find(&ctx->options, "--if-exists");
const char *id = ctx->argv[1];
const struct nbrec_logical_switch *ls;
ls = ls_by_name_or_uuid(ctx, id, must_exist);
if (!ls) {
return;
}
nbrec_logical_switch_delete(ls);
}
static void
nbctl_ls_list(struct ctl_context *ctx)
{
const struct nbrec_logical_switch *ls;
struct smap lswitches;
smap_init(&lswitches);
NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
smap_add_format(&lswitches, ls->name, UUID_FMT " (%s)",
UUID_ARGS(&ls->header_.uuid), ls->name);
}
const struct smap_node **nodes = smap_sort(&lswitches);
for (size_t i = 0; i < smap_count(&lswitches); i++) {
const struct smap_node *node = nodes[i];
ds_put_format(&ctx->output, "%s\n", node->value);
}
smap_destroy(&lswitches);
free(nodes);
}
static const struct nbrec_logical_switch_port *
lsp_by_name_or_uuid(struct ctl_context *ctx, const char *id,
bool must_exist)
{
const struct nbrec_logical_switch_port *lsp = NULL;
struct uuid lsp_uuid;
bool is_uuid = uuid_from_string(&lsp_uuid, id);
if (is_uuid) {
lsp = nbrec_logical_switch_port_get_for_uuid(ctx->idl, &lsp_uuid);
}
if (!lsp) {
NBREC_LOGICAL_SWITCH_PORT_FOR_EACH(lsp, ctx->idl) {
if (!strcmp(lsp->name, id)) {
break;
}
}
}
if (!lsp && must_exist) {
ctl_fatal("%s: port %s not found", id, is_uuid ? "UUID" : "name");
}
return lsp;
}
/* Returns the logical switch that contains 'lsp'. */
static const struct nbrec_logical_switch *
lsp_to_ls(const struct ovsdb_idl *idl,
const struct nbrec_logical_switch_port *lsp)
{
const struct nbrec_logical_switch *ls;
NBREC_LOGICAL_SWITCH_FOR_EACH (ls, idl) {
for (size_t i = 0; i < ls->n_ports; i++) {
if (ls->ports[i] == lsp) {
return ls;
}
}
}
/* Can't happen because of the database schema */
ctl_fatal("logical port %s is not part of any logical switch",
lsp->name);
}
static const char *
ls_get_name(const struct nbrec_logical_switch *ls,
char uuid_s[UUID_LEN + 1], size_t uuid_s_size)
{
if (ls->name[0]) {
return ls->name;
}
snprintf(uuid_s, uuid_s_size, UUID_FMT, UUID_ARGS(&ls->header_.uuid));
return uuid_s;
}
static void
nbctl_lsp_add(struct ctl_context *ctx)
{
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
const struct nbrec_logical_switch *ls;
ls = ls_by_name_or_uuid(ctx, ctx->argv[1], true);
const char *parent_name;
int64_t tag;
if (ctx->argc == 3) {
parent_name = NULL;
tag = -1;
} else if (ctx->argc == 5) {
/* Validate tag. */
parent_name = ctx->argv[3];
if (!ovs_scan(ctx->argv[4], "%"SCNd64, &tag)
|| tag < 0 || tag > 4095) {
ctl_fatal("%s: invalid tag (must be in range 0 to 4095)",
ctx->argv[4]);
}
} else {
ctl_fatal("lsp-add with parent must also specify a tag");
}
const char *lsp_name = ctx->argv[2];
const struct nbrec_logical_switch_port *lsp;
lsp = lsp_by_name_or_uuid(ctx, lsp_name, false);
if (lsp) {
if (!may_exist) {
ctl_fatal("%s: a port with this name already exists",
lsp_name);
}
const struct nbrec_logical_switch *lsw;
lsw = lsp_to_ls(ctx->idl, lsp);
if (lsw != ls) {
char uuid_s[UUID_LEN + 1];
ctl_fatal("%s: port already exists but in switch %s", lsp_name,
ls_get_name(lsw, uuid_s, sizeof uuid_s));
}
if (parent_name) {
if (!lsp->parent_name) {
ctl_fatal("%s: port already exists but has no parent",
lsp_name);
} else if (strcmp(parent_name, lsp->parent_name)) {
ctl_fatal("%s: port already exists with different parent %s",
lsp_name, lsp->parent_name);
}
if (!lsp->n_tag_request) {
ctl_fatal("%s: port already exists but has no tag_request",
lsp_name);
} else if (lsp->tag_request[0] != tag) {
ctl_fatal("%s: port already exists with different "
"tag_request %"PRId64, lsp_name,
lsp->tag_request[0]);
}
} else {
if (lsp->parent_name) {
ctl_fatal("%s: port already exists but has parent %s",
lsp_name, lsp->parent_name);
}
}
return;
}
/* Create the logical port. */
lsp = nbrec_logical_switch_port_insert(ctx->txn);
nbrec_logical_switch_port_set_name(lsp, lsp_name);
if (tag >= 0) {
nbrec_logical_switch_port_set_parent_name(lsp, parent_name);
nbrec_logical_switch_port_set_tag_request(lsp, &tag, 1);
}
/* Insert the logical port into the logical switch. */
nbrec_logical_switch_verify_ports(ls);
struct nbrec_logical_switch_port **new_ports = xmalloc(sizeof *new_ports *
(ls->n_ports + 1));
memcpy(new_ports, ls->ports, sizeof *new_ports * ls->n_ports);
new_ports[ls->n_ports] = CONST_CAST(struct nbrec_logical_switch_port *,
lsp);
nbrec_logical_switch_set_ports(ls, new_ports, ls->n_ports + 1);
free(new_ports);
}
/* Removes logical switch port 'ls->ports[idx]'. */
static void
remove_lsp(const struct nbrec_logical_switch *ls, size_t idx)
{
const struct nbrec_logical_switch_port *lsp = ls->ports[idx];
/* First remove 'lsp' from the array of ports. This is what will
* actually cause the logical port to be deleted when the transaction is
* sent to the database server (due to garbage collection). */
struct nbrec_logical_switch_port **new_ports
= xmemdup(ls->ports, sizeof *new_ports * ls->n_ports);
new_ports[idx] = new_ports[ls->n_ports - 1];
nbrec_logical_switch_verify_ports(ls);
nbrec_logical_switch_set_ports(ls, new_ports, ls->n_ports - 1);
free(new_ports);
/* Delete 'lsp' from the IDL. This won't have a real effect on the
* database server (the IDL will suppress it in fact) but it means that it
* won't show up when we iterate with NBREC_LOGICAL_SWITCH_PORT_FOR_EACH
* later. */
nbrec_logical_switch_port_delete(lsp);
}
static void
nbctl_lsp_del(struct ctl_context *ctx)
{
bool must_exist = !shash_find(&ctx->options, "--if-exists");
const struct nbrec_logical_switch_port *lsp;
lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], must_exist);
if (!lsp) {
return;
}
/* Find the switch that contains 'lsp', then delete it. */
const struct nbrec_logical_switch *ls;
NBREC_LOGICAL_SWITCH_FOR_EACH (ls, ctx->idl) {
for (size_t i = 0; i < ls->n_ports; i++) {
if (ls->ports[i] == lsp) {
remove_lsp(ls, i);
return;
}
}
}
/* Can't happen because of the database schema. */
ctl_fatal("logical port %s is not part of any logical switch",
ctx->argv[1]);
}
static void
nbctl_lsp_list(struct ctl_context *ctx)
{
const char *id = ctx->argv[1];
const struct nbrec_logical_switch *ls;
struct smap lsps;
size_t i;
ls = ls_by_name_or_uuid(ctx, id, true);
smap_init(&lsps);
for (i = 0; i < ls->n_ports; i++) {
const struct nbrec_logical_switch_port *lsp = ls->ports[i];
smap_add_format(&lsps, lsp->name, UUID_FMT " (%s)",
UUID_ARGS(&lsp->header_.uuid), lsp->name);
}
const struct smap_node **nodes = smap_sort(&lsps);
for (i = 0; i < smap_count(&lsps); i++) {
const struct smap_node *node = nodes[i];
ds_put_format(&ctx->output, "%s\n", node->value);
}
smap_destroy(&lsps);
free(nodes);
}
static void
nbctl_lsp_get_parent(struct ctl_context *ctx)
{
const struct nbrec_logical_switch_port *lsp;
lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], true);
if (lsp->parent_name) {
ds_put_format(&ctx->output, "%s\n", lsp->parent_name);
}
}
static void
nbctl_lsp_get_tag(struct ctl_context *ctx)
{
const struct nbrec_logical_switch_port *lsp;
lsp = lsp_by_name_or_uuid(ctx, ctx->argv[1], true);
if (lsp->n_tag > 0) {
ds_put_format(&ctx->output, "%"PRId64"\n", lsp->tag[0]);
}
}
static void
nbctl_lsp_set_addresses(struct ctl_context *ctx)
{
const char *id = ctx->argv[1];
const struct nbrec_logical_switch_port *lsp;
lsp = lsp_by_name_or_uuid(ctx, id, true);
int i;
for (i = 2; i < ctx->argc; i++) {
struct eth_addr ea;
if (strcmp(ctx->argv[i], "unknown") && strcmp(ctx->argv[i], "dynamic")
&& strcmp(ctx->argv[i], "router")
&& !ovs_scan(ctx->argv[i], ETH_ADDR_SCAN_FMT,
ETH_ADDR_SCAN_ARGS(ea))) {
ctl_fatal("%s: Invalid address format. See ovn-nb(5). "
"Hint: An Ethernet address must be "