forked from ovn-org/ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovn-sbctl.c
1628 lines (1397 loc) · 51.2 KB
/
ovn-sbctl.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
/*
* Copyright (c) 2015, 2016, 2017 Nicira, Inc.
*
* 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 <ctype.h>
#include <errno.h>
#include <float.h>
#include <getopt.h>
#include <inttypes.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "colors.h"
#include "command-line.h"
#include "compiler.h"
#include "db-ctl-base.h"
#include "daemon.h"
#include "dirs.h"
#include "fatal-signal.h"
#include "jsonrpc.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/json.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-flow.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/shash.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "lib/ovn-sb-idl.h"
#include "lib/ovn-util.h"
#include "memory.h"
#include "ovn-dbctl.h"
#include "ovsdb-data.h"
#include "ovsdb-idl.h"
#include "openvswitch/poll-loop.h"
#include "process.h"
#include "simap.h"
#include "sset.h"
#include "stream-ssl.h"
#include "stream.h"
#include "table.h"
#include "timer.h"
#include "timeval.h"
#include "unixctl.h"
#include "util.h"
#include "svec.h"
VLOG_DEFINE_THIS_MODULE(sbctl);
static void
sbctl_add_base_prerequisites(struct ovsdb_idl *idl,
enum nbctl_wait_type wait_type OVS_UNUSED)
{
ovsdb_idl_add_table(idl, &sbrec_table_sb_global);
}
static void
sbctl_pre_execute(struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
enum nbctl_wait_type wait_type OVS_UNUSED)
{
const struct sbrec_sb_global *sb = sbrec_sb_global_first(idl);
if (!sb) {
/* XXX add verification that table is empty */
sb = sbrec_sb_global_insert(txn);
}
}
static void
sbctl_usage(void)
{
printf("\
%s: OVN southbound DB management utility\n\
\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
General commands:\n\
show print overview of database contents\n\
\n\
Chassis commands:\n\
chassis-add CHASSIS ENCAP-TYPE ENCAP-IP create a new chassis named\n\
CHASSIS with ENCAP-TYPE tunnels\n\
and ENCAP-IP\n\
chassis-del CHASSIS delete CHASSIS and all of its encaps\n\
and gateway_ports\n\
\n\
Port binding commands:\n\
lsp-bind PORT CHASSIS bind logical port PORT to CHASSIS\n\
lsp-unbind PORT reset the port binding of logical port PORT\n\
\n\
Logical flow commands:\n\
lflow-list [DATAPATH] [LFLOW...] list logical flows for DATAPATH\n\
dump-flows [DATAPATH] [LFLOW...] alias for lflow-list\n\
count-flows [DATAPATH] count logical flows for DATAPATH\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\
\n\
%s\
%s\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--no-leader-only accept any cluster member, not just the leader\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_sb_db());
table_usage();
vlog_usage();
printf("\
--no-syslog equivalent to --verbose=sbctl: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
static int
get_inactivity_probe(struct ovsdb_idl *idl)
{
const struct sbrec_sb_global *sb = sbrec_sb_global_first(idl);
int interval = DEFAULT_UTILS_PROBE_INTERVAL_MSEC;
if (sb) {
interval = smap_get_int(&sb->options, "sbctl_probe_interval",
interval);
}
return interval;
}
/* ovs-sbctl specific context. Inherits the 'struct ctl_context' as base. */
struct sbctl_context {
struct ctl_context base;
/* A cache of the contents of the database.
*
* A command that needs to use any of this information must first call
* sbctl_context_populate_cache(). A command that changes anything that
* could invalidate the cache must either call
* sbctl_context_invalidate_cache() or manually update the cache to
* maintain its correctness. */
bool cache_valid;
/* Maps from chassis name to struct sbctl_chassis. */
struct shash chassis;
/* Maps from chassis name to struct sbctl_chassis_private. */
struct shash chassis_private;
/* Maps from lport name to struct sbctl_port_binding. */
struct shash port_bindings;
};
/* Casts 'base' into 'struct sbctl_context'. */
static struct sbctl_context *
sbctl_context_cast(struct ctl_context *base)
{
return CONTAINER_OF(base, struct sbctl_context, base);
}
struct sbctl_chassis {
const struct sbrec_chassis *ch_cfg;
};
struct sbctl_chassis_private {
const struct sbrec_chassis_private *ch_priv;
};
struct sbctl_port_binding {
const struct sbrec_port_binding *bd_cfg;
};
static void
sbctl_context_invalidate_cache(struct ctl_context *ctx)
{
struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
if (!sbctl_ctx->cache_valid) {
return;
}
sbctl_ctx->cache_valid = false;
shash_destroy_free_data(&sbctl_ctx->chassis);
shash_destroy_free_data(&sbctl_ctx->chassis_private);
shash_destroy_free_data(&sbctl_ctx->port_bindings);
}
/* Casts 'base' into 'struct sbctl_context' and initializes it if needed. */
static struct sbctl_context *
sbctl_context_get(struct ctl_context *ctx)
{
struct sbctl_context *sbctl_ctx
= CONTAINER_OF(ctx, struct sbctl_context, base);
if (sbctl_ctx->cache_valid) {
return sbctl_ctx;
}
const struct sbrec_chassis *chassis_rec;
const struct sbrec_chassis_private *chassis_private_rec;
const struct sbrec_port_binding *port_binding_rec;
struct sset chassis, port_bindings;
sbctl_ctx->cache_valid = true;
shash_init(&sbctl_ctx->chassis);
shash_init(&sbctl_ctx->chassis_private);
shash_init(&sbctl_ctx->port_bindings);
sset_init(&chassis);
SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->idl) {
struct sbctl_chassis *ch;
if (!sset_add(&chassis, chassis_rec->name)) {
VLOG_WARN("database contains duplicate chassis name (%s)",
chassis_rec->name);
continue;
}
ch = xmalloc(sizeof *ch);
ch->ch_cfg = chassis_rec;
shash_add(&sbctl_ctx->chassis, chassis_rec->name, ch);
}
sset_destroy(&chassis);
sset_init(&chassis);
SBREC_CHASSIS_PRIVATE_FOR_EACH (chassis_private_rec, ctx->idl) {
struct sbctl_chassis_private *ch_priv;
if (!sset_add(&chassis, chassis_private_rec->name)) {
VLOG_WARN("database contains duplicate private record for "
"chassis named (%s)",
chassis_rec->name);
continue;
}
ch_priv = xmalloc(sizeof *ch_priv);
ch_priv->ch_priv = chassis_private_rec;
shash_add(&sbctl_ctx->chassis_private,
chassis_private_rec->name,
ch_priv);
}
sset_destroy(&chassis);
sset_init(&port_bindings);
SBREC_PORT_BINDING_FOR_EACH(port_binding_rec, ctx->idl) {
struct sbctl_port_binding *bd;
if (!sset_add(&port_bindings, port_binding_rec->logical_port)) {
VLOG_WARN("database contains duplicate port binding for logical "
"port (%s)",
port_binding_rec->logical_port);
continue;
}
bd = xmalloc(sizeof *bd);
bd->bd_cfg = port_binding_rec;
shash_add(&sbctl_ctx->port_bindings, port_binding_rec->logical_port,
bd);
}
sset_destroy(&port_bindings);
return sbctl_ctx;
}
static struct ctl_context *
sbctl_ctx_create(void)
{
struct sbctl_context *sbctx = xmalloc(sizeof *sbctx);
*sbctx = (struct sbctl_context) {
.cache_valid = false,
};
return &sbctx->base;
}
static void
sbctl_ctx_destroy(struct ctl_context *ctx)
{
sbctl_context_invalidate_cache(ctx);
free(ctx);
}
static struct sbctl_chassis *
find_chassis(struct ctl_context *ctx, const char *name, bool must_exist)
{
struct sbctl_context *sbctl_ctx = sbctl_context_get(ctx);
struct sbctl_chassis *sbctl_ch = shash_find_data(&sbctl_ctx->chassis,
name);
if (must_exist && !sbctl_ch) {
ctl_error(ctx, "no chassis named %s", name);
}
return sbctl_ch;
}
static struct sbctl_chassis_private *
find_chassis_private(struct ctl_context *ctx,
const char *name,
bool must_exist)
{
struct sbctl_context *sbctl_ctx = sbctl_context_get(ctx);
struct sbctl_chassis_private *sbctl_ch_priv = shash_find_data(
&sbctl_ctx->chassis_private, name);
if (must_exist && !sbctl_ch_priv) {
ctl_error(ctx, "no private record for chassis named %s", name);
}
return sbctl_ch_priv;
}
static struct sbctl_port_binding *
find_port_binding(struct ctl_context *ctx, const char *name, bool must_exist)
{
struct sbctl_context *sbctl_ctx = sbctl_context_get(ctx);
struct sbctl_port_binding *bd = shash_find_data(&sbctl_ctx->port_bindings,
name);
if (must_exist && !bd) {
ctl_error(&sbctl_ctx->base, "no port named %s", name);
}
return bd;
}
static void
pre_get_info(struct ctl_context *ctx)
{
ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_name);
ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_col_encaps);
ovsdb_idl_add_column(ctx->idl, &sbrec_chassis_private_col_name);
ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_type);
ovsdb_idl_add_column(ctx->idl, &sbrec_encap_col_ip);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_logical_port);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_tunnel_key);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_chassis);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_datapath);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_up);
ovsdb_idl_add_column(ctx->idl, &sbrec_port_binding_col_mirror_rules);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_logical_datapath);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_logical_dp_group);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_pipeline);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_actions);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_priority);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_table_id);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_match);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_flow_col_external_ids);
ovsdb_idl_add_column(ctx->idl, &sbrec_logical_dp_group_col_datapaths);
ovsdb_idl_add_column(ctx->idl, &sbrec_datapath_binding_col_external_ids);
ovsdb_idl_add_column(ctx->idl, &sbrec_ip_multicast_col_datapath);
ovsdb_idl_add_column(ctx->idl, &sbrec_ip_multicast_col_seq_no);
ovsdb_idl_add_column(ctx->idl, &sbrec_multicast_group_col_name);
ovsdb_idl_add_column(ctx->idl, &sbrec_multicast_group_col_datapath);
ovsdb_idl_add_column(ctx->idl, &sbrec_multicast_group_col_tunnel_key);
ovsdb_idl_add_column(ctx->idl, &sbrec_multicast_group_col_ports);
ovsdb_idl_add_column(ctx->idl, &sbrec_mac_binding_col_datapath);
ovsdb_idl_add_column(ctx->idl, &sbrec_mac_binding_col_logical_port);
ovsdb_idl_add_column(ctx->idl, &sbrec_mac_binding_col_ip);
ovsdb_idl_add_column(ctx->idl, &sbrec_mac_binding_col_mac);
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_datapaths);
/* datapath_group column is deprecated. */
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_datapath_group);
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_ls_datapath_group);
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_vips);
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_name);
ovsdb_idl_add_column(ctx->idl, &sbrec_load_balancer_col_protocol);
}
static struct cmd_show_table cmd_show_tables[] = {
{&sbrec_table_chassis,
&sbrec_chassis_col_name,
{&sbrec_chassis_col_hostname,
&sbrec_chassis_col_encaps,
NULL},
{&sbrec_table_port_binding,
&sbrec_port_binding_col_logical_port,
&sbrec_port_binding_col_chassis}},
{&sbrec_table_encap,
&sbrec_encap_col_type,
{&sbrec_encap_col_ip,
&sbrec_encap_col_options,
NULL},
{NULL, NULL, NULL}},
{NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}},
};
static void
sbctl_init(struct ctl_context *ctx OVS_UNUSED)
{
}
static void
cmd_chassis_add(struct ctl_context *ctx)
{
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
const char *ch_name, *encap_types, *encap_ip;
ch_name = ctx->argv[1];
encap_types = ctx->argv[2];
encap_ip = ctx->argv[3];
if (find_chassis(ctx, ch_name, false)) {
if (may_exist) {
return;
}
}
struct sbctl_context *sbctl_ctx = sbctl_context_get(ctx);
if (shash_find(&sbctl_ctx->chassis, ch_name)) {
if (!may_exist) {
ctl_error(ctx, "cannot create a chassis named %s because a "
"chassis named %s already exists", ch_name, ch_name);
}
return;
}
struct sset encap_set;
sset_from_delimited_string(&encap_set, encap_types, ",");
size_t n_encaps = sset_count(&encap_set);
struct sbrec_encap **encaps = xmalloc(n_encaps * sizeof *encaps);
const struct smap options = SMAP_CONST1(&options, "csum", "true");
const char *encap_type;
int i = 0;
SSET_FOR_EACH (encap_type, &encap_set){
encaps[i] = sbrec_encap_insert(ctx->txn);
sbrec_encap_set_type(encaps[i], encap_type);
sbrec_encap_set_ip(encaps[i], encap_ip);
sbrec_encap_set_options(encaps[i], &options);
sbrec_encap_set_chassis_name(encaps[i], ch_name);
i++;
}
sset_destroy(&encap_set);
struct sbrec_chassis *ch = sbrec_chassis_insert(ctx->txn);
sbrec_chassis_set_name(ch, ch_name);
sbrec_chassis_set_encaps(ch, encaps, n_encaps);
free(encaps);
sbctl_context_invalidate_cache(ctx);
}
static void
cmd_chassis_del(struct ctl_context *ctx)
{
struct sbctl_context *sbctl_ctx = sbctl_context_cast(ctx);
bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct sbctl_chassis *sbctl_ch;
sbctl_ch = find_chassis(ctx, ctx->argv[1], must_exist);
if (sbctl_ch) {
if (sbctl_ch->ch_cfg) {
size_t i;
for (i = 0; i < sbctl_ch->ch_cfg->n_encaps; i++) {
sbrec_encap_delete(sbctl_ch->ch_cfg->encaps[i]);
}
struct sbctl_chassis_private *sbctl_ch_priv;
sbctl_ch_priv = find_chassis_private(ctx, ctx->argv[1], false);
if (sbctl_ch_priv) {
if (sbctl_ch_priv->ch_priv) {
sbrec_chassis_private_delete(sbctl_ch_priv->ch_priv);
}
shash_find_and_delete(&sbctl_ctx->chassis_private,
ctx->argv[1]);
free(sbctl_ch_priv);
}
sbrec_chassis_delete(sbctl_ch->ch_cfg);
}
shash_find_and_delete(&sbctl_ctx->chassis, ctx->argv[1]);
free(sbctl_ch);
}
}
static void
cmd_lsp_bind(struct ctl_context *ctx)
{
bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
struct sbctl_chassis *sbctl_ch;
struct sbctl_port_binding *sbctl_bd;
char *lport_name, *ch_name;
bool up = true;
/* port_binding must exist, chassis must exist! */
lport_name = ctx->argv[1];
ch_name = ctx->argv[2];
sbctl_bd = find_port_binding(ctx, lport_name, true);
if (!sbctl_bd) {
return;
}
sbctl_ch = find_chassis(ctx, ch_name, true);
if (!sbctl_ch) {
return;
}
if (sbctl_bd->bd_cfg->chassis) {
if (!may_exist || sbctl_bd->bd_cfg->chassis != sbctl_ch->ch_cfg) {
ctl_error(ctx, "lport (%s) has already been binded to chassis (%s)",
lport_name, sbctl_bd->bd_cfg->chassis->name);
}
return;
}
sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, sbctl_ch->ch_cfg);
sbrec_port_binding_set_up(sbctl_bd->bd_cfg, &up, 1);
sbctl_context_invalidate_cache(ctx);
}
static void
cmd_lsp_unbind(struct ctl_context *ctx)
{
bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct sbctl_port_binding *sbctl_bd;
char *lport_name;
lport_name = ctx->argv[1];
sbctl_bd = find_port_binding(ctx, lport_name, must_exist);
if (sbctl_bd) {
sbrec_port_binding_set_chassis(sbctl_bd->bd_cfg, NULL);
sbrec_port_binding_set_up(sbctl_bd->bd_cfg, NULL, 0);
}
}
enum {
PL_INGRESS,
PL_EGRESS,
};
/* Help ensure we catch any future pipeline values */
static int
pipeline_encode(const char *pl)
{
if (!strcmp(pl, "ingress")) {
return PL_INGRESS;
} else if (!strcmp(pl, "egress")) {
return PL_EGRESS;
}
OVS_NOT_REACHED();
}
struct sbctl_lflow {
const struct sbrec_logical_flow *lflow;
const struct sbrec_datapath_binding *dp;
};
static int
sbctl_lflow_cmp(const void *a_, const void *b_)
{
const struct sbctl_lflow *a_ctl_lflow = a_;
const struct sbctl_lflow *b_ctl_lflow = b_;
const struct sbrec_logical_flow *a = a_ctl_lflow->lflow;
const struct sbrec_logical_flow *b = b_ctl_lflow->lflow;
const struct sbrec_datapath_binding *adb = a_ctl_lflow->dp;
const struct sbrec_datapath_binding *bdb = b_ctl_lflow->dp;
const char *a_name = smap_get_def(&adb->external_ids, "name", "");
const char *b_name = smap_get_def(&bdb->external_ids, "name", "");
int cmp = strcmp(a_name, b_name);
if (cmp) {
return cmp;
}
cmp = uuid_compare_3way(&adb->header_.uuid, &bdb->header_.uuid);
if (cmp) {
return cmp;
}
int a_pipeline = pipeline_encode(a->pipeline);
int b_pipeline = pipeline_encode(b->pipeline);
cmp = (a_pipeline > b_pipeline ? 1
: a_pipeline < b_pipeline ? -1
: a->table_id > b->table_id ? 1
: a->table_id < b->table_id ? -1
: a->priority > b->priority ? -1
: a->priority < b->priority ? 1
: strcmp(a->match, b->match));
return cmp ? cmp : strcmp(a->actions, b->actions);
}
static bool
is_uuid_with_prefix(const char *uuid)
{
return uuid[0] == '0' && (uuid[1] == 'x' || uuid[1] == 'X');
}
static bool
parse_partial_uuid(char *s)
{
/* Accept a full or partial UUID. */
if (uuid_is_partial_string(s)) {
return true;
}
/* Accept a full or partial UUID prefixed by 0x, since "ovs-ofctl
* dump-flows" prints cookies prefixed by 0x. */
if (is_uuid_with_prefix(s) && uuid_is_partial_string(s + 2)) {
return true;
}
/* Not a (partial) UUID. */
return false;
}
static const char *
strip_leading_zero(const char *s)
{
return s + strspn(s, "0");
}
static bool
is_partial_uuid_match(const struct uuid *uuid, const char *match)
{
char uuid_s[UUID_LEN + 1];
snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
/* We strip leading zeros because we want to accept cookie values derived
* from UUIDs, and cookie values are printed without leading zeros because
* they're just numbers. */
const char *s1 = strip_leading_zero(uuid_s);
const char *s2 = match;
if (is_uuid_with_prefix(s2)) {
s2 = s2 + 2;
}
s2 = strip_leading_zero(s2);
return !strncmp(s1, s2, strlen(s2));
}
static char *
default_ovs(void)
{
return xasprintf("unix:%s/br-int.mgmt", ovs_rundir());
}
static struct vconn *
sbctl_open_vconn(struct shash *options)
{
struct shash_node *ovs = shash_find(options, "--ovs");
if (!ovs) {
return NULL;
}
char *remote = ovs->data ? xstrdup(ovs->data) : default_ovs();
struct vconn *vconn;
int retval = vconn_open_block(remote, 1 << OFP15_VERSION, 0, -1, &vconn);
if (retval) {
VLOG_WARN("%s: connection failed (%s)", remote, ovs_strerror(retval));
}
free(remote);
return vconn;
}
static void
sbctl_dump_openflow(struct vconn *vconn, const struct uuid *uuid, bool stats,
struct ds *s)
{
struct ofputil_flow_stats_request fsr = {
.cookie = htonll(uuid->parts[0]),
.cookie_mask = OVS_BE64_MAX,
.out_port = OFPP_ANY,
.out_group = OFPG_ANY,
.table_id = OFPTT_ALL,
};
struct ofputil_flow_stats *fses;
size_t n_fses;
int error = vconn_dump_flows(vconn, &fsr, OFPUTIL_P_OF15_OXM,
&fses, &n_fses);
if (error) {
VLOG_WARN("%s: error obtaining flow stats (%s)",
vconn_get_name(vconn), ovs_strerror(error));
return;
}
if (n_fses) {
for (size_t i = 0; i < n_fses; i++) {
const struct ofputil_flow_stats *fs = &fses[i];
ds_put_cstr(s, " ");
if (stats) {
ofputil_flow_stats_format(s, fs, NULL, NULL, true);
} else {
ds_put_format(s, "%stable=%s%"PRIu8" ",
colors.special, colors.end, fs->table_id);
match_format(&fs->match, NULL, s, OFP_DEFAULT_PRIORITY);
if (ds_last(s) != ' ') {
ds_put_char(s, ' ');
}
ds_put_format(s, "%sactions=%s", colors.actions, colors.end);
struct ofpact_format_params fp = { .s = s };
ofpacts_format(fs->ofpacts, fs->ofpacts_len, &fp);
}
ds_put_char(s, '\n');
}
}
for (size_t i = 0; i < n_fses; i++) {
free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
}
free(fses);
}
static void
print_datapath_name(const struct sbrec_datapath_binding *dp, struct ds *s)
{
const struct smap *ids = &dp->external_ids;
const char *name = smap_get(ids, "name");
const char *name2 = smap_get(ids, "name2");
if (name && name2) {
ds_put_format(s, "\"%s\" aka \"%s\"", name, name2);
} else if (name || name2) {
ds_put_format(s, "\"%s\"", name ? name : name2);
}
}
static void
print_vflow_datapath_name(const struct sbrec_datapath_binding *dp,
bool do_print, struct ds *s)
{
if (!do_print) {
return;
}
ds_put_cstr(s, "datapath=");
print_datapath_name(dp, s);
ds_put_cstr(s, ", ");
}
static void
print_uuid_part(const struct uuid *uuid, bool do_print, struct ds *s)
{
if (!do_print) {
return;
}
ds_put_format(s, "uuid=0x%08"PRIx32", ", uuid->parts[0]);
}
static void
cmd_lflow_list_port_bindings(struct ctl_context *ctx, struct vconn *vconn,
const struct sbrec_datapath_binding *datapath,
bool stats, bool print_uuid)
{
const struct sbrec_port_binding *pb;
const struct sbrec_port_binding *pb_prev = NULL;
SBREC_PORT_BINDING_FOR_EACH (pb, ctx->idl) {
if (datapath && pb->datapath != datapath) {
continue;
}
if (!pb_prev) {
ds_put_cstr(&ctx->output, "\nPort Bindings:\n");
}
ds_put_cstr(&ctx->output, " ");
print_uuid_part(&pb->header_.uuid, print_uuid, &ctx->output);
print_vflow_datapath_name(pb->datapath, !datapath, &ctx->output);
ds_put_format(&ctx->output,
"logical_port=%s, tunnel_key=%-5"PRId64"\n",
pb->logical_port, pb->tunnel_key);
if (vconn) {
sbctl_dump_openflow(vconn, &pb->header_.uuid, stats, &ctx->output);
}
pb_prev = pb;
}
}
static void
cmd_lflow_list_mac_bindings(struct ctl_context *ctx, struct vconn *vconn,
const struct sbrec_datapath_binding *datapath,
bool stats, bool print_uuid)
{
const struct sbrec_mac_binding *mb;
const struct sbrec_mac_binding *mb_prev = NULL;
SBREC_MAC_BINDING_FOR_EACH (mb, ctx->idl) {
if (datapath && mb->datapath != datapath) {
continue;
}
if (!mb_prev) {
ds_put_cstr(&ctx->output, "\nMAC Bindings:\n");
}
ds_put_cstr(&ctx->output, " ");
print_uuid_part(&mb->header_.uuid, print_uuid, &ctx->output);
print_vflow_datapath_name(mb->datapath, !datapath, &ctx->output);
ds_put_format(&ctx->output, "logical_port=%s, ip=%s, mac=%s\n",
mb->logical_port, mb->ip, mb->mac);
if (vconn) {
sbctl_dump_openflow(vconn, &mb->header_.uuid, stats, &ctx->output);
}
mb_prev = mb;
}
}
static void
cmd_lflow_list_mc_groups(struct ctl_context *ctx, struct vconn *vconn,
const struct sbrec_datapath_binding *datapath,
bool stats, bool print_uuid)
{
const struct sbrec_multicast_group *mc;
const struct sbrec_multicast_group *mc_prev = NULL;
SBREC_MULTICAST_GROUP_FOR_EACH (mc, ctx->idl) {
if (datapath && mc->datapath != datapath) {
continue;
}
if (!mc_prev) {
ds_put_cstr(&ctx->output, "\nMC Groups:\n");
}
ds_put_cstr(&ctx->output, " ");
print_uuid_part(&mc->header_.uuid, print_uuid, &ctx->output);
print_vflow_datapath_name(mc->datapath, !datapath, &ctx->output);
ds_put_format(&ctx->output, "name=%s, tunnel_key=%-5"PRId64", ports=(",
mc->name, mc->tunnel_key);
for (size_t i = 0; i < mc->n_ports; i++) {
ds_put_cstr(&ctx->output, mc->ports[i]->logical_port);
if (i != mc->n_ports - 1) {
ds_put_cstr(&ctx->output, ", ");
}
}
ds_put_cstr(&ctx->output, ")\n");
if (vconn) {
sbctl_dump_openflow(vconn, &mc->header_.uuid, stats, &ctx->output);
}
mc_prev = mc;
}
}
static void
cmd_lflow_list_chassis(struct ctl_context *ctx, struct vconn *vconn,
bool stats, bool print_uuid)
{
const struct sbrec_chassis *chassis;
const struct sbrec_chassis *chassis_prev = NULL;
SBREC_CHASSIS_FOR_EACH (chassis, ctx->idl) {
if (!chassis_prev) {
ds_put_cstr(&ctx->output, "\nChassis:\n");
}
ds_put_cstr(&ctx->output, " ");
print_uuid_part(&chassis->header_.uuid, print_uuid, &ctx->output);
ds_put_format(&ctx->output, "name=%s\n", chassis->name);
if (vconn) {
sbctl_dump_openflow(vconn, &chassis->header_.uuid, stats,
&ctx->output);
}
chassis_prev = chassis;
}
}
static bool
datapath_group_contains_datapath(const struct sbrec_logical_dp_group *g,
const struct sbrec_datapath_binding *dp)
{
if (!g || !dp) {
return false;
}
for (size_t i = 0; i < g->n_datapaths; i++) {
if (g->datapaths[i] == dp) {
return true;
}
}
return false;
}
static void
cmd_lflow_list_load_balancers(struct ctl_context *ctx, struct vconn *vconn,
const struct sbrec_datapath_binding *datapath,
bool stats, bool print_uuid)
{
const struct sbrec_load_balancer *lb;
const struct sbrec_load_balancer *lb_prev = NULL;
SBREC_LOAD_BALANCER_FOR_EACH (lb, ctx->idl) {
bool dp_found = false;
if (datapath) {
size_t i;
for (i = 0; i < lb->n_datapaths; i++) {
if (datapath == lb->datapaths[i]) {
dp_found = true;
break;
}
}
/* datapath_group column is deprecated. */
if (lb->datapath_group && !dp_found) {
dp_found = datapath_group_contains_datapath(lb->datapath_group,
datapath);
}
if (lb->ls_datapath_group && !dp_found) {
dp_found = datapath_group_contains_datapath(
lb->ls_datapath_group, datapath);
}
if (!dp_found) {
continue;
}
}
if (!lb_prev) {
ds_put_cstr(&ctx->output, "\nLoad Balancers:\n");
}
ds_put_cstr(&ctx->output, " ");
print_uuid_part(&lb->header_.uuid, print_uuid, &ctx->output);
ds_put_format(&ctx->output, "name=\"%s\", protocol=\"%s\", ",
lb->name, lb->protocol);
if (!dp_found) {
for (size_t i = 0; i < lb->n_datapaths; i++) {
print_vflow_datapath_name(lb->datapaths[i], true,
&ctx->output);
}
/* datapath_group column is deprecated. */
for (size_t i = 0; lb->datapath_group
&& i < lb->datapath_group->n_datapaths; i++) {
print_vflow_datapath_name(lb->datapath_group->datapaths[i],
true, &ctx->output);
}
for (size_t i = 0; lb->ls_datapath_group
&& i < lb->ls_datapath_group->n_datapaths; i++) {
print_vflow_datapath_name(lb->ls_datapath_group->datapaths[i],
true, &ctx->output);
}
}
ds_put_cstr(&ctx->output, "\n vips:\n");
struct smap_node *node;
SMAP_FOR_EACH (node, &lb->vips) {
ds_put_format(&ctx->output, " %s = %s\n",
node->key, node->value);
}
ds_put_cstr(&ctx->output, "\n");
if (vconn) {
sbctl_dump_openflow(vconn, &lb->header_.uuid, stats, &ctx->output);
}
lb_prev = lb;
}
}
static void
sbctl_lflow_add(struct sbctl_lflow **lflows,
size_t *n_flows, size_t *n_capacity,
const struct sbrec_logical_flow *lflow,
const struct sbrec_datapath_binding *dp)
{
if (*n_flows == *n_capacity) {
*lflows = x2nrealloc(*lflows, n_capacity, sizeof **lflows);