forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovn-trace.c
2062 lines (1794 loc) · 64.1 KB
/
ovn-trace.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) 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 <getopt.h>
#include "command-line.h"
#include "compiler.h"
#include "daemon.h"
#include "dirs.h"
#include "fatal-signal.h"
#include "flow.h"
#include "nx-match.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/json.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/vconn.h"
#include "openvswitch/vlog.h"
#include "ovn/actions.h"
#include "ovn/expr.h"
#include "ovn/lex.h"
#include "ovn/lib/acl-log.h"
#include "ovn/lib/logical-fields.h"
#include "ovn/lib/ovn-sb-idl.h"
#include "ovn/lib/ovn-dhcp.h"
#include "ovn/lib/ovn-util.h"
#include "ovsdb-idl.h"
#include "poll-loop.h"
#include "stream-ssl.h"
#include "stream.h"
#include "unixctl.h"
#include "util.h"
VLOG_DEFINE_THIS_MODULE(ovntrace);
/* --db: The database server to contact. */
static const char *db;
/* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
commands. */
static char *unixctl_path;
/* The southbound database. */
static struct ovsdb_idl *ovnsb_idl;
/* --detailed: Show a detailed, table-by-table trace. */
static bool detailed;
/* --summary: Show a trace that omits table information. */
static bool summary;
/* --minimal: Show a trace with only minimal information. */
static bool minimal;
/* --ovs: OVS instance to contact to get OpenFlow flows. */
static const char *ovs;
static struct vconn *vconn;
/* --ct: Connection tracking state to use for ct_next() actions. */
static uint32_t *ct_states;
static size_t n_ct_states;
static size_t ct_state_idx;
/* --friendly-names, --no-friendly-names: Whether to substitute human-friendly
* port and datapath names for the awkward UUIDs typically used in the actual
* logical flows. */
static bool use_friendly_names = true;
OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[]);
static char *trace(const char *datapath, const char *flow);
static void read_db(void);
static unixctl_cb_func ovntrace_exit;
static unixctl_cb_func ovntrace_trace;
int
main(int argc, char *argv[])
{
set_program_name(argv[0]);
service_start(&argc, &argv);
fatal_ignore_sigpipe();
vlog_set_levels_from_string_assert("reconnect:warn");
/* Parse command line. */
parse_options(argc, argv);
argc -= optind;
argv += optind;
if (get_detach()) {
if (argc != 0) {
ovs_fatal(0, "non-option arguments not supported with --detach "
"(use --help for help)");
}
} else {
if (argc != 2) {
ovs_fatal(0, "exactly two non-option arguments are required "
"(use --help for help)");
}
}
struct unixctl_server *server = NULL;
bool exiting = false;
if (get_detach()) {
daemonize_start(false);
int error = unixctl_server_create(unixctl_path, &server);
if (error) {
ovs_fatal(error, "failed to create unixctl server");
}
unixctl_command_register("exit", "", 0, 0, ovntrace_exit, &exiting);
unixctl_command_register("trace", "[OPTIONS] DATAPATH MICROFLOW",
2, INT_MAX, ovntrace_trace, NULL);
}
ovnsb_idl = ovsdb_idl_create(db, &sbrec_idl_class, true, false);
bool already_read = false;
for (;;) {
ovsdb_idl_run(ovnsb_idl);
unixctl_server_run(server);
if (!ovsdb_idl_is_alive(ovnsb_idl)) {
int retval = ovsdb_idl_get_last_error(ovnsb_idl);
ovs_fatal(0, "%s: database connection failed (%s)",
db, ovs_retval_to_string(retval));
}
if (ovsdb_idl_has_ever_connected(ovnsb_idl)) {
if (!already_read) {
already_read = true;
read_db();
}
daemonize_complete();
if (!get_detach()) {
char *output = trace(argv[0], argv[1]);
fputs(output, stdout);
free(output);
return 0;
}
}
if (exiting) {
break;
}
ovsdb_idl_wait(ovnsb_idl);
unixctl_server_wait(server);
poll_block();
}
}
static char *
default_ovs(void)
{
return xasprintf("unix:%s/br-int.mgmt", ovs_rundir());
}
static void
parse_ct_option(const char *state_s_)
{
uint32_t state;
struct ds ds = DS_EMPTY_INITIALIZER;
if (!parse_ct_state(state_s_, CS_TRACKED, &state, &ds)) {
ovs_fatal(0, "%s", ds_cstr(&ds));
}
if (!validate_ct_state(state, &ds)) {
VLOG_WARN("%s", ds_cstr(&ds));
}
ds_destroy(&ds);
ct_states = xrealloc(ct_states, (n_ct_states + 1) * sizeof *ct_states);
ct_states[n_ct_states++] = state;
}
static void
parse_options(int argc, char *argv[])
{
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_UNIXCTL,
OPT_DETAILED,
OPT_SUMMARY,
OPT_MINIMAL,
OPT_ALL,
OPT_OVS,
OPT_CT,
OPT_FRIENDLY_NAMES,
OPT_NO_FRIENDLY_NAMES,
DAEMON_OPTION_ENUMS,
SSL_OPTION_ENUMS,
VLOG_OPTION_ENUMS
};
static const struct option long_options[] = {
{"db", required_argument, NULL, OPT_DB},
{"unixctl", required_argument, NULL, OPT_UNIXCTL},
{"detailed", no_argument, NULL, OPT_DETAILED},
{"summary", no_argument, NULL, OPT_SUMMARY},
{"minimal", no_argument, NULL, OPT_MINIMAL},
{"all", no_argument, NULL, OPT_ALL},
{"ovs", optional_argument, NULL, OPT_OVS},
{"ct", required_argument, NULL, OPT_CT},
{"friendly-names", no_argument, NULL, OPT_FRIENDLY_NAMES},
{"no-friendly-names", no_argument, NULL, OPT_NO_FRIENDLY_NAMES},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
DAEMON_LONG_OPTIONS,
VLOG_LONG_OPTIONS,
STREAM_SSL_LONG_OPTIONS,
{NULL, 0, NULL, 0},
};
char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
for (;;) {
int idx;
int c;
c = getopt_long(argc, argv, short_options, long_options, &idx);
if (c == -1) {
break;
}
switch (c) {
case OPT_DB:
db = optarg;
break;
case OPT_UNIXCTL:
unixctl_path = optarg;
break;
case OPT_DETAILED:
detailed = true;
break;
case OPT_SUMMARY:
summary = true;
break;
case OPT_MINIMAL:
minimal = true;
break;
case OPT_ALL:
detailed = summary = minimal = true;
break;
case OPT_OVS:
ovs = optarg ? optarg : default_ovs();
break;
case OPT_CT:
parse_ct_option(optarg);
break;
case OPT_FRIENDLY_NAMES:
use_friendly_names = true;
break;
case OPT_NO_FRIENDLY_NAMES:
use_friendly_names = false;
break;
case 'h':
usage();
case 'V':
ovs_print_version(0, 0);
printf("DB Schema %s\n", sbrec_get_db_version());
exit(EXIT_SUCCESS);
DAEMON_OPTION_HANDLERS
VLOG_OPTION_HANDLERS
STREAM_SSL_OPTION_HANDLERS
case '?':
exit(EXIT_FAILURE);
default:
abort();
}
}
free(short_options);
if (!db) {
db = default_sb_db();
}
if (!detailed && !summary && !minimal) {
detailed = true;
}
}
static void
usage(void)
{
printf("\
%s: OVN trace utility\n\
usage: %s [OPTIONS] DATAPATH MICROFLOW\n\
%s [OPTIONS] --detach\n\
\n\
Output format options:\n\
--detailed table-by-table \"backtrace\" (default)\n\
--summary less detailed, more parseable\n\
--minimal minimum to explain externally visible behavior\n\
--all provide all forms of output\n\
Output style options:\n\
--no-friendly-names do not substitute human friendly names for UUIDs\n",
program_name, program_name, program_name);
daemon_usage();
vlog_usage();
printf("\n\
Other options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--ovs[=REMOTE] obtain corresponding OpenFlow flows from REMOTE\n\
(default: %s)\n\
--unixctl=SOCKET set control socket name\n\
-h, --help display this help message\n\
-V, --version display version information\n",
default_sb_db(), default_ovs());
stream_usage("database", true, true, false);
vconn_usage(true, false, false);
exit(EXIT_SUCCESS);
}
struct ovntrace_datapath {
struct hmap_node sb_uuid_node;
struct uuid sb_uuid;
struct uuid nb_uuid;
char *name;
char *name2;
char *friendly_name;
uint32_t tunnel_key;
struct ovs_list mcgroups; /* Contains "struct ovntrace_mcgroup"s. */
struct ovntrace_flow **flows;
size_t n_flows, allocated_flows;
struct hmap mac_bindings; /* Contains "struct ovntrace_mac_binding"s. */
bool has_local_l3gateway;
};
struct ovntrace_port {
struct ovntrace_datapath *dp;
struct uuid uuid;
char *name;
char *name2;
const char *friendly_name;
char *type;
uint16_t tunnel_key;
struct ovntrace_port *peer; /* Patch ports only. */
struct ovntrace_port *distributed_port; /* chassisredirect ports only. */
};
struct ovntrace_mcgroup {
struct ovs_list list_node; /* In struct ovntrace_datapath's 'mcgroups'. */
struct ovntrace_datapath *dp;
char *name;
uint16_t tunnel_key;
struct ovntrace_port **ports;
size_t n_ports;
};
struct ovntrace_flow {
struct uuid uuid;
enum ovnact_pipeline pipeline;
int table_id;
char *stage_name;
char *source;
int priority;
char *match_s;
struct expr *match;
struct ovnact *ovnacts;
size_t ovnacts_len;
};
struct ovntrace_mac_binding {
struct hmap_node node;
uint16_t port_key;
struct in6_addr ip;
struct eth_addr mac;
};
static inline uint32_t
hash_mac_binding(uint16_t port_key, const struct in6_addr *ip)
{
return hash_bytes(ip, sizeof *ip, port_key);
}
/* Every ovntrace_datapath, by southbound Datapath_Binding record UUID. */
static struct hmap datapaths;
/* Every ovntrace_port, by name. */
static struct shash ports;
/* Symbol table for expressions and actions. */
static struct shash symtab;
/* Address sets. */
static struct shash address_sets;
/* DHCP options. */
static struct hmap dhcp_opts; /* Contains "struct dhcp_opts_map"s. */
static struct hmap dhcpv6_opts; /* Contains "struct dhcp_opts_map"s. */
static struct ovntrace_datapath *
ovntrace_datapath_find_by_sb_uuid(const struct uuid *sb_uuid)
{
struct ovntrace_datapath *dp;
HMAP_FOR_EACH_WITH_HASH (dp, sb_uuid_node, uuid_hash(sb_uuid),
&datapaths) {
if (uuid_equals(&dp->sb_uuid, sb_uuid)) {
return dp;
}
}
return NULL;
}
static const struct ovntrace_datapath *
ovntrace_datapath_find_by_name(const char *name)
{
struct ovntrace_datapath *dp;
HMAP_FOR_EACH (dp, sb_uuid_node, &datapaths) {
if (!strcmp(name, dp->name)
|| (dp->name2 && !strcmp(name, dp->name2))) {
return dp;
}
}
struct ovntrace_datapath *match = NULL;
HMAP_FOR_EACH (dp, sb_uuid_node, &datapaths) {
if (uuid_is_partial_match(&dp->sb_uuid, name) >= 4 ||
uuid_is_partial_match(&dp->nb_uuid, name) >= 4) {
if (match) {
VLOG_WARN("name \"%s\" matches multiple datapaths", name);
return NULL;
}
match = dp;
}
}
return match;
}
static const struct ovntrace_port *
ovntrace_port_find_by_key(const struct ovntrace_datapath *dp,
uint16_t tunnel_key)
{
const struct shash_node *node;
SHASH_FOR_EACH (node, &ports) {
const struct ovntrace_port *port = node->data;
if (port->dp == dp && port->tunnel_key == tunnel_key) {
return port;
}
}
return NULL;
}
static const char *
ovntrace_port_key_to_name(const struct ovntrace_datapath *dp,
uint16_t key)
{
const struct ovntrace_port *port = ovntrace_port_find_by_key(dp, key);
return (port ? port->friendly_name
: !key ? ""
: "(unnamed)");
}
static const struct ovntrace_mcgroup *
ovntrace_mcgroup_find_by_key(const struct ovntrace_datapath *dp,
uint16_t tunnel_key)
{
const struct ovntrace_mcgroup *mcgroup;
LIST_FOR_EACH (mcgroup, list_node, &dp->mcgroups) {
if (mcgroup->tunnel_key == tunnel_key) {
return mcgroup;
}
}
return NULL;
}
static const struct ovntrace_mcgroup *
ovntrace_mcgroup_find_by_name(const struct ovntrace_datapath *dp,
const char *name)
{
const struct ovntrace_mcgroup *mcgroup;
LIST_FOR_EACH (mcgroup, list_node, &dp->mcgroups) {
if (!strcmp(mcgroup->name, name)) {
return mcgroup;
}
}
return NULL;
}
static const struct ovntrace_mac_binding *
ovntrace_mac_binding_find(const struct ovntrace_datapath *dp,
uint16_t port_key, const struct in6_addr *ip)
{
const struct ovntrace_mac_binding *bind;
HMAP_FOR_EACH_WITH_HASH (bind, node, hash_mac_binding(port_key, ip),
&dp->mac_bindings) {
if (bind->port_key == port_key && ipv6_addr_equals(ip, &bind->ip)) {
return bind;
}
}
return NULL;
}
/* If 's' ends with a UUID, returns a copy of it with the UUID truncated to
* just the first 6 characters; otherwise, returns a copy of 's'. */
static char *
shorten_uuid(const char *s)
{
size_t len = strlen(s);
return (len >= UUID_LEN && uuid_is_partial_string(s + (len - UUID_LEN))
? xmemdup0(s, len - (UUID_LEN - 6))
: xstrdup(s));
}
static void
read_datapaths(void)
{
hmap_init(&datapaths);
const struct sbrec_datapath_binding *sbdb;
SBREC_DATAPATH_BINDING_FOR_EACH (sbdb, ovnsb_idl) {
struct ovntrace_datapath *dp = xzalloc(sizeof *dp);
const struct smap *ids = &sbdb->external_ids;
dp->sb_uuid = sbdb->header_.uuid;
if (!smap_get_uuid(ids, "logical-switch", &dp->nb_uuid) &&
!smap_get_uuid(ids, "logical-router", &dp->nb_uuid)) {
dp->nb_uuid = dp->sb_uuid;
}
const char *name = smap_get(ids, "name");
dp->name = (name
? xstrdup(name)
: xasprintf(UUID_FMT, UUID_ARGS(&dp->nb_uuid)));
dp->name2 = nullable_xstrdup(smap_get(ids, "name2"));
dp->friendly_name = (!use_friendly_names
? xasprintf(UUID_FMT, UUID_ARGS(&dp->nb_uuid))
: shorten_uuid(dp->name2 ? dp->name2 : dp->name));
dp->tunnel_key = sbdb->tunnel_key;
ovs_list_init(&dp->mcgroups);
hmap_init(&dp->mac_bindings);
hmap_insert(&datapaths, &dp->sb_uuid_node, uuid_hash(&dp->sb_uuid));
}
}
static void
read_ports(void)
{
shash_init(&ports);
const struct sbrec_port_binding *sbpb;
SBREC_PORT_BINDING_FOR_EACH (sbpb, ovnsb_idl) {
const char *port_name = sbpb->logical_port;
struct ovntrace_datapath *dp
= ovntrace_datapath_find_by_sb_uuid(&sbpb->datapath->header_.uuid);
if (!dp) {
VLOG_WARN("logical port %s missing datapath", port_name);
continue;
}
struct ovntrace_port *port = xzalloc(sizeof *port);
if (!shash_add_once(&ports, port_name, port)) {
VLOG_WARN("duplicate logical port name %s", port_name);
free(port);
continue;
}
port->dp = dp;
port->uuid = sbpb->header_.uuid;
port->name = xstrdup(port_name);
port->type = xstrdup(sbpb->type);
port->tunnel_key = sbpb->tunnel_key;
port->name2 = nullable_xstrdup(smap_get(&sbpb->external_ids, "name"));
port->friendly_name = (!use_friendly_names ? xstrdup(port->name)
: shorten_uuid(port->name2
? port->name2 : port->name));
if (!strcmp(sbpb->type, "patch")) {
const char *peer_name = smap_get(&sbpb->options, "peer");
if (peer_name) {
struct ovntrace_port *peer
= shash_find_data(&ports, peer_name);
if (peer) {
port->peer = peer;
port->peer->peer = port;
}
}
} else if (!strcmp(sbpb->type, "l3gateway")) {
/* Treat all gateways as local for our purposes. */
dp->has_local_l3gateway = true;
}
}
SBREC_PORT_BINDING_FOR_EACH (sbpb, ovnsb_idl) {
if (!strcmp(sbpb->type, "chassisredirect")) {
struct ovntrace_port *port
= shash_find_data(&ports, sbpb->logical_port);
if (port) {
const char *distributed_name = smap_get(&sbpb->options,
"distributed-port");
if (distributed_name) {
struct ovntrace_port *distributed_port
= shash_find_data(&ports, distributed_name);
if (distributed_port && distributed_port->dp == port->dp) {
port->distributed_port = distributed_port;
}
}
}
}
}
}
static int
compare_port(const void *a_, const void *b_)
{
struct ovntrace_port *const *ap = a_;
struct ovntrace_port *const *bp = b_;
const struct ovntrace_port *a = *ap;
const struct ovntrace_port *b = *bp;
return strcmp(a->name, b->name);
}
static void
read_mcgroups(void)
{
const struct sbrec_multicast_group *sbmg;
SBREC_MULTICAST_GROUP_FOR_EACH (sbmg, ovnsb_idl) {
struct ovntrace_datapath *dp
= ovntrace_datapath_find_by_sb_uuid(&sbmg->datapath->header_.uuid);
if (!dp) {
VLOG_WARN("logical multicast group %s missing datapath",
sbmg->name);
continue;
}
struct ovntrace_mcgroup *mcgroup = xzalloc(sizeof *mcgroup);
ovs_list_push_back(&dp->mcgroups, &mcgroup->list_node);
mcgroup->dp = dp;
mcgroup->tunnel_key = sbmg->tunnel_key;
mcgroup->name = xstrdup(sbmg->name);
mcgroup->ports = xmalloc(sbmg->n_ports * sizeof *mcgroup->ports);
for (size_t i = 0; i < sbmg->n_ports; i++) {
const char *port_name = sbmg->ports[i]->logical_port;
struct ovntrace_port *p = shash_find_data(&ports, port_name);
if (!p) {
VLOG_WARN("missing port %s", port_name);
continue;
}
if (!uuid_equals(&sbmg->ports[i]->datapath->header_.uuid,
&p->dp->sb_uuid)) {
VLOG_WARN("multicast group %s in datapath %s contains "
"port %s outside that datapath",
mcgroup->name, mcgroup->dp->name, port_name);
continue;
}
mcgroup->ports[mcgroup->n_ports++] = p;
}
/* Sort the ports in alphabetical order to make output more
* predictable. */
qsort(mcgroup->ports, mcgroup->n_ports, sizeof *mcgroup->ports,
compare_port);
}
}
static void
read_address_sets(void)
{
shash_init(&address_sets);
const struct sbrec_address_set *sbas;
SBREC_ADDRESS_SET_FOR_EACH (sbas, ovnsb_idl) {
expr_addr_sets_add(&address_sets, sbas->name,
(const char *const *) sbas->addresses,
sbas->n_addresses);
}
}
static bool
ovntrace_is_chassis_resident(const void *aux OVS_UNUSED,
const char *port_name)
{
if (port_name[0] == '\0') {
return true;
}
const struct ovntrace_port *port = shash_find_data(&ports, port_name);
if (port) {
/* Since ovntrace is not chassis specific, assume any port
* that exists is resident. */
return true;
}
VLOG_WARN("%s: unknown logical port\n", port_name);
return false;
}
static int
compare_flow(const void *a_, const void *b_)
{
struct ovntrace_flow *const *ap = a_;
struct ovntrace_flow *const *bp = b_;
const struct ovntrace_flow *a = *ap;
const struct ovntrace_flow *b = *bp;
if (a->pipeline != b->pipeline) {
/* Sort OVNACT_P_INGRESS before OVNACT_P_EGRESS. */
return a->pipeline == OVNACT_P_EGRESS ? 1 : -1;
} else if (a->table_id != b->table_id) {
/* Sort in increasing order of table_id. */
return a->table_id > b->table_id ? 1 : -1;
} else if (a->priority != b->priority) {
/* Sort in decreasing order of priority. */
return a->priority > b->priority ? -1 : 1;
} else {
/* Otherwise who cares. */
return 0;
}
}
static char *
ovntrace_make_names_friendly(const char *in)
{
if (!use_friendly_names) {
return xstrdup(in);
}
struct ds out = DS_EMPTY_INITIALIZER;
while (*in) {
struct lex_token token;
const char *start;
const char *next;
next = lex_token_parse(&token, in, &start);
if (token.type == LEX_T_STRING) {
const struct ovntrace_port *port = shash_find_data(&ports,
token.s);
if (port) {
ds_put_buffer(&out, in, start - in);
json_string_escape(port->friendly_name, &out);
} else {
ds_put_buffer(&out, in, next - in);
}
} else if (token.type != LEX_T_END) {
ds_put_buffer(&out, in, next - in);
} else {
break;
}
lex_token_destroy(&token);
in = next;
}
return ds_steal_cstr(&out);
}
static void
read_flows(void)
{
ovn_init_symtab(&symtab);
const struct sbrec_logical_flow *sblf;
SBREC_LOGICAL_FLOW_FOR_EACH (sblf, ovnsb_idl) {
const struct sbrec_datapath_binding *sbdb = sblf->logical_datapath;
struct ovntrace_datapath *dp
= ovntrace_datapath_find_by_sb_uuid(&sbdb->header_.uuid);
if (!dp) {
VLOG_WARN("logical flow missing datapath");
continue;
}
char *error;
struct expr *match;
match = expr_parse_string(sblf->match, &symtab, &address_sets, &error);
if (error) {
VLOG_WARN("%s: parsing expression failed (%s)",
sblf->match, error);
free(error);
continue;
}
struct ovnact_parse_params pp = {
.symtab = &symtab,
.dhcp_opts = &dhcp_opts,
.dhcpv6_opts = &dhcpv6_opts,
.pipeline = (!strcmp(sblf->pipeline, "ingress")
? OVNACT_P_INGRESS
: OVNACT_P_EGRESS),
.n_tables = 24,
.cur_ltable = sblf->table_id,
};
uint64_t stub[1024 / 8];
struct ofpbuf ovnacts = OFPBUF_STUB_INITIALIZER(stub);
struct expr *prereqs;
error = ovnacts_parse_string(sblf->actions, &pp, &ovnacts, &prereqs);
if (error) {
VLOG_WARN("%s: parsing actions failed (%s)", sblf->actions, error);
free(error);
expr_destroy(match);
continue;
}
match = expr_combine(EXPR_T_AND, match, prereqs);
match = expr_annotate(match, &symtab, &error);
if (error) {
VLOG_WARN("match annotation failed (%s)", error);
free(error);
expr_destroy(match);
ovnacts_free(ovnacts.data, ovnacts.size);
ofpbuf_uninit(&ovnacts);
continue;
}
if (match) {
match = expr_simplify(match, ovntrace_is_chassis_resident, NULL);
}
struct ovntrace_flow *flow = xzalloc(sizeof *flow);
flow->uuid = sblf->header_.uuid;
flow->pipeline = (!strcmp(sblf->pipeline, "ingress")
? OVNACT_P_INGRESS
: OVNACT_P_EGRESS);
flow->table_id = sblf->table_id;
flow->stage_name = nullable_xstrdup(smap_get(&sblf->external_ids,
"stage-name"));
flow->source = nullable_xstrdup(smap_get(&sblf->external_ids,
"source"));
flow->priority = sblf->priority;
flow->match_s = ovntrace_make_names_friendly(sblf->match);
flow->match = match;
flow->ovnacts_len = ovnacts.size;
flow->ovnacts = ofpbuf_steal_data(&ovnacts);
if (dp->n_flows >= dp->allocated_flows) {
dp->flows = x2nrealloc(dp->flows, &dp->allocated_flows,
sizeof *dp->flows);
}
dp->flows[dp->n_flows++] = flow;
}
const struct ovntrace_datapath *dp;
HMAP_FOR_EACH (dp, sb_uuid_node, &datapaths) {
qsort(dp->flows, dp->n_flows, sizeof *dp->flows, compare_flow);
}
}
static void
read_dhcp_opts(void)
{
hmap_init(&dhcp_opts);
const struct sbrec_dhcp_options *sdo;
SBREC_DHCP_OPTIONS_FOR_EACH (sdo, ovnsb_idl) {
dhcp_opt_add(&dhcp_opts, sdo->name, sdo->code, sdo->type);
}
hmap_init(&dhcpv6_opts);
const struct sbrec_dhcpv6_options *sdo6;
SBREC_DHCPV6_OPTIONS_FOR_EACH(sdo6, ovnsb_idl) {
dhcp_opt_add(&dhcpv6_opts, sdo6->name, sdo6->code, sdo6->type);
}
}
static void
read_mac_bindings(void)
{
const struct sbrec_mac_binding *sbmb;
SBREC_MAC_BINDING_FOR_EACH (sbmb, ovnsb_idl) {
const struct ovntrace_port *port = shash_find_data(
&ports, sbmb->logical_port);
if (!port) {
VLOG_WARN("missing port %s", sbmb->logical_port);
continue;
}
if (!uuid_equals(&port->dp->sb_uuid, &sbmb->datapath->header_.uuid)) {
VLOG_WARN("port %s is in wrong datapath", sbmb->logical_port);
continue;
}
struct in6_addr ip6;
ovs_be32 ip4;
if (ip_parse(sbmb->ip, &ip4)) {
ip6 = in6_addr_mapped_ipv4(ip4);
} else if (!ipv6_parse(sbmb->ip, &ip6)) {
VLOG_WARN("%s: bad IP address", sbmb->ip);
continue;
}
struct eth_addr mac;
if (!eth_addr_from_string(sbmb->mac, &mac)) {
VLOG_WARN("%s: bad Ethernet address", sbmb->mac);
continue;
}
struct ovntrace_mac_binding *binding = xmalloc(sizeof *binding);
binding->port_key = port->tunnel_key;
binding->ip = ip6;
binding->mac = mac;
hmap_insert(&port->dp->mac_bindings, &binding->node,
hash_mac_binding(binding->port_key, &ip6));
}
}
static void
read_db(void)
{
read_datapaths();
read_ports();
read_mcgroups();
read_address_sets();
read_dhcp_opts();
read_flows();
read_mac_bindings();
}
static const struct ovntrace_port *
ovntrace_port_lookup_by_name(const char *name)
{
const struct ovntrace_port *port = shash_find_data(&ports, name);
if (port) {
return port;
}
const struct ovntrace_port *match = NULL;
struct shash_node *node;
SHASH_FOR_EACH (node, &ports) {
port = node->data;
if (port->name2 && !strcmp(port->name2, name)) {
if (match) {
VLOG_WARN("name \"%s\" matches multiple ports", name);
return NULL;
}
match = port;
}
}
if (uuid_is_partial_string(name) >= 4) {
SHASH_FOR_EACH (node, &ports) {
port = node->data;
struct uuid name_uuid;
if (uuid_is_partial_match(&port->uuid, name)
|| (uuid_from_string(&name_uuid, port->name)
&& uuid_is_partial_match(&name_uuid, name))) {
if (match && match != port) {
VLOG_WARN("name \"%s\" matches multiple ports", name);
return NULL;
}
match = port;
}
}
}
return match;
}
static bool
ovntrace_lookup_port(const void *dp_, const char *port_name,
unsigned int *portp)
{
const struct ovntrace_datapath *dp = dp_;
if (port_name[0] == '\0') {
*portp = 0;
return true;
}
const struct ovntrace_port *port = ovntrace_port_lookup_by_name(port_name);
if (port) {
if (port->dp == dp) {
*portp = port->tunnel_key;
return true;
}
VLOG_WARN("%s: not in datapath %s", port_name, dp->name);
}