/
physical.c
1790 lines (1583 loc) · 68.9 KB
/
physical.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 "binding.h"
#include "coverage.h"
#include "byte-order.h"
#include "encaps.h"
#include "flow.h"
#include "ha-chassis.h"
#include "lflow.h"
#include "local_data.h"
#include "lport.h"
#include "chassis.h"
#include "lib/bundle.h"
#include "openvswitch/poll-loop.h"
#include "lib/uuid.h"
#include "ofctrl.h"
#include "openvswitch/list.h"
#include "openvswitch/hmap.h"
#include "openvswitch/match.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "openvswitch/ofp-parse.h"
#include "ovn-controller.h"
#include "lib/chassis-index.h"
#include "lib/ovn-sb-idl.h"
#include "lib/ovn-util.h"
#include "physical.h"
#include "openvswitch/shash.h"
#include "simap.h"
#include "smap.h"
#include "sset.h"
#include "util.h"
#include "vswitch-idl.h"
#include "hmapx.h"
VLOG_DEFINE_THIS_MODULE(physical);
COVERAGE_DEFINE(physical_run);
/* Datapath zone IDs for connection tracking and NAT */
struct zone_ids {
int ct; /* MFF_LOG_CT_ZONE. */
int dnat; /* MFF_LOG_DNAT_ZONE. */
int snat; /* MFF_LOG_SNAT_ZONE. */
};
static void
load_logical_ingress_metadata(const struct sbrec_port_binding *binding,
const struct zone_ids *zone_ids,
struct ofpbuf *ofpacts_p);
/* UUID to identify OF flows not associated with ovsdb rows. */
static struct uuid *hc_uuid = NULL;
#define CHASSIS_MAC_TO_ROUTER_MAC_CONJID 100
void
physical_register_ovs_idl(struct ovsdb_idl *ovs_idl)
{
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_name);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_interfaces);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_port_col_external_ids);
ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_name);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_ofport);
ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
}
static void
put_load(uint64_t value, enum mf_field_id dst, int ofs, int n_bits,
struct ofpbuf *ofpacts)
{
struct ofpact_set_field *sf = ofpact_put_set_field(ofpacts,
mf_from_id(dst), NULL,
NULL);
ovs_be64 n_value = htonll(value);
bitwise_copy(&n_value, 8, 0, sf->value, sf->field->n_bytes, ofs, n_bits);
bitwise_one(ofpact_set_field_mask(sf), sf->field->n_bytes, ofs, n_bits);
}
static void
put_move(enum mf_field_id src, int src_ofs,
enum mf_field_id dst, int dst_ofs,
int n_bits,
struct ofpbuf *ofpacts)
{
struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
move->src.field = mf_from_id(src);
move->src.ofs = src_ofs;
move->src.n_bits = n_bits;
move->dst.field = mf_from_id(dst);
move->dst.ofs = dst_ofs;
move->dst.n_bits = n_bits;
}
static void
put_resubmit(uint8_t table_id, struct ofpbuf *ofpacts)
{
struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ofpacts);
resubmit->in_port = OFPP_IN_PORT;
resubmit->table_id = table_id;
}
/*
* For a port binding, get the corresponding ovn-chassis-id tunnel port
* from the associated encap.
*/
static struct chassis_tunnel *
get_port_binding_tun(const struct sbrec_port_binding *binding,
const struct hmap *chassis_tunnels)
{
struct sbrec_encap *encap = binding->encap;
struct sbrec_chassis *chassis = binding->chassis;
struct chassis_tunnel *tun = NULL;
if (encap) {
tun = chassis_tunnel_find(chassis_tunnels, chassis->name, encap->ip);
}
if (!tun) {
tun = chassis_tunnel_find(chassis_tunnels, chassis->name, NULL);
}
return tun;
}
static void
put_encapsulation(enum mf_field_id mff_ovn_geneve,
const struct chassis_tunnel *tun,
const struct sbrec_datapath_binding *datapath,
uint16_t outport, bool is_ramp_switch,
struct ofpbuf *ofpacts)
{
if (tun->type == GENEVE) {
put_load(datapath->tunnel_key, MFF_TUN_ID, 0, 24, ofpacts);
put_load(outport, mff_ovn_geneve, 0, 32, ofpacts);
put_move(MFF_LOG_INPORT, 0, mff_ovn_geneve, 16, 15, ofpacts);
} else if (tun->type == STT) {
put_load(datapath->tunnel_key | ((uint64_t) outport << 24),
MFF_TUN_ID, 0, 64, ofpacts);
put_move(MFF_LOG_INPORT, 0, MFF_TUN_ID, 40, 15, ofpacts);
} else if (tun->type == VXLAN) {
uint64_t vni = datapath->tunnel_key;
if (!is_ramp_switch) {
/* Only some bits are used for regular tunnels. */
vni |= (uint64_t) outport << 12;
}
put_load(vni, MFF_TUN_ID, 0, 24, ofpacts);
} else {
OVS_NOT_REACHED();
}
}
static void
put_stack(enum mf_field_id field, struct ofpact_stack *stack)
{
stack->subfield.field = mf_from_id(field);
stack->subfield.ofs = 0;
stack->subfield.n_bits = stack->subfield.field->n_bits;
}
static const struct sbrec_port_binding *
get_localnet_port(const struct hmap *local_datapaths, int64_t tunnel_key)
{
const struct local_datapath *ld = get_local_datapath(local_datapaths,
tunnel_key);
return ld ? ld->localnet_port : NULL;
}
static struct zone_ids
get_zone_ids(const struct sbrec_port_binding *binding,
const struct simap *ct_zones)
{
struct zone_ids zone_ids;
zone_ids.ct = simap_get(ct_zones, binding->logical_port);
const struct uuid *key = &binding->datapath->header_.uuid;
char *dnat = alloc_nat_zone_key(key, "dnat");
zone_ids.dnat = simap_get(ct_zones, dnat);
free(dnat);
char *snat = alloc_nat_zone_key(key, "snat");
zone_ids.snat = simap_get(ct_zones, snat);
free(snat);
return zone_ids;
}
static void
put_remote_port_redirect_bridged(const struct
sbrec_port_binding *binding,
const struct hmap *local_datapaths,
struct local_datapath *ld,
struct match *match,
struct ofpbuf *ofpacts_p,
struct ovn_desired_flow_table *flow_table)
{
if (strcmp(binding->type, "chassisredirect")) {
/* bridged based redirect is only supported for chassisredirect
* type remote ports. */
return;
}
struct eth_addr binding_mac;
bool is_valid_mac = extract_sbrec_binding_first_mac(binding,
&binding_mac);
if (!is_valid_mac) {
return;
}
uint32_t ls_dp_key = 0;
for (int i = 0; i < ld->n_peer_ports; i++) {
const struct sbrec_port_binding *sport_binding =
ld->peer_ports[i].remote;
const char *sport_peer_name =
smap_get(&sport_binding->options, "peer");
const char *distributed_port =
smap_get(&binding->options, "distributed-port");
if (!strcmp(sport_peer_name, distributed_port)) {
ls_dp_key = sport_binding->datapath->tunnel_key;
break;
}
}
if (!ls_dp_key) {
return;
}
union mf_value value;
struct ofpact_mac *src_mac;
const struct sbrec_port_binding *ls_localnet_port;
ls_localnet_port = get_localnet_port(local_datapaths, ls_dp_key);
if (!ls_localnet_port) {
return;
}
src_mac = ofpact_put_SET_ETH_SRC(ofpacts_p);
src_mac->mac = binding_mac;
value.be64 = htonll(ls_dp_key);
ofpact_put_set_field(ofpacts_p, mf_from_id(MFF_METADATA),
&value, NULL);
value.be32 = htonl(ls_localnet_port->tunnel_key);
ofpact_put_set_field(ofpacts_p, mf_from_id(MFF_REG15),
&value, NULL);
put_resubmit(OFTABLE_LOG_TO_PHY, ofpacts_p);
ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100,
binding->header_.uuid.parts[0],
match, ofpacts_p, &binding->header_.uuid);
}
static void
put_remote_port_redirect_overlay(const struct
sbrec_port_binding *binding,
bool is_ha_remote,
struct ha_chassis_ordered *ha_ch_ordered,
enum mf_field_id mff_ovn_geneve,
const struct chassis_tunnel *tun,
uint32_t port_key,
struct match *match,
struct ofpbuf *ofpacts_p,
const struct hmap *chassis_tunnels,
struct ovn_desired_flow_table *flow_table)
{
if (!is_ha_remote) {
/* Setup encapsulation */
const struct chassis_tunnel *rem_tun =
get_port_binding_tun(binding, chassis_tunnels);
if (!rem_tun) {
return;
}
put_encapsulation(mff_ovn_geneve, tun, binding->datapath, port_key,
!strcmp(binding->type, "vtep"),
ofpacts_p);
/* Output to tunnel. */
ofpact_put_OUTPUT(ofpacts_p)->port = rem_tun->ofport;
} else {
/* Make sure all tunnel endpoints use the same encapsulation,
* and set it up */
for (size_t i = 0; i < ha_ch_ordered->n_ha_ch; i++) {
const struct sbrec_chassis *ch = ha_ch_ordered->ha_ch[i].chassis;
if (!ch) {
continue;
}
if (!tun) {
tun = chassis_tunnel_find(chassis_tunnels, ch->name, NULL);
} else {
struct chassis_tunnel *chassis_tunnel =
chassis_tunnel_find(chassis_tunnels, ch->name, NULL);
if (chassis_tunnel &&
tun->type != chassis_tunnel->type) {
static struct vlog_rate_limit rl =
VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_ERR_RL(&rl, "Port %s has Gateway_Chassis "
"with mixed encapsulations, only "
"uniform encapsulations are "
"supported.", binding->logical_port);
return;
}
}
}
if (!tun) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_ERR_RL(&rl, "No tunnel endpoint found for HA chassis in "
"HA chassis group of port %s",
binding->logical_port);
return;
}
put_encapsulation(mff_ovn_geneve, tun, binding->datapath, port_key,
!strcmp(binding->type, "vtep"),
ofpacts_p);
/* Output to tunnels with active/backup */
struct ofpact_bundle *bundle = ofpact_put_BUNDLE(ofpacts_p);
for (size_t i = 0; i < ha_ch_ordered->n_ha_ch; i++) {
const struct sbrec_chassis *ch =
ha_ch_ordered->ha_ch[i].chassis;
if (!ch) {
continue;
}
tun = chassis_tunnel_find(chassis_tunnels, ch->name, NULL);
if (!tun) {
continue;
}
if (bundle->n_members >= BUNDLE_MAX_MEMBERS) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "Remote endpoints for port beyond "
"BUNDLE_MAX_MEMBERS");
break;
}
ofpbuf_put(ofpacts_p, &tun->ofport, sizeof tun->ofport);
bundle = ofpacts_p->header;
bundle->n_members++;
}
bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
/* Although ACTIVE_BACKUP bundle algorithm seems to ignore
* the next two fields, those are always set */
bundle->basis = 0;
bundle->fields = NX_HASH_FIELDS_ETH_SRC;
ofpact_finish_BUNDLE(ofpacts_p, &bundle);
}
ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
binding->header_.uuid.parts[0],
match, ofpacts_p, &binding->header_.uuid);
}
static struct hmap remote_chassis_macs =
HMAP_INITIALIZER(&remote_chassis_macs);
/* Maps from a physical network name to the chassis macs of remote chassis. */
struct remote_chassis_mac {
struct hmap_node hmap_node;
char *chassis_mac;
char *chassis_id;
uint32_t chassis_sb_cookie;
};
static void
populate_remote_chassis_macs(const struct sbrec_chassis *my_chassis,
const struct sbrec_chassis_table *chassis_table)
{
const struct sbrec_chassis *chassis;
SBREC_CHASSIS_TABLE_FOR_EACH (chassis, chassis_table) {
/* We want only remote chassis macs. */
if (!strcmp(my_chassis->name, chassis->name)) {
continue;
}
const char *tokens
= get_chassis_mac_mappings(&chassis->other_config);
if (!strlen(tokens)) {
continue;
}
char *save_ptr = NULL;
char *token;
char *tokstr = xstrdup(tokens);
/* Format for a chassis mac configuration is:
* ovn-chassis-mac-mappings="bridge-name1:MAC1,bridge-name2:MAC2"
*/
for (token = strtok_r(tokstr, ",", &save_ptr);
token != NULL;
token = strtok_r(NULL, ",", &save_ptr)) {
char *save_ptr2 = NULL;
char *chassis_mac_bridge = strtok_r(token, ":", &save_ptr2);
char *chassis_mac_str = strtok_r(NULL, "", &save_ptr2);
struct remote_chassis_mac *remote_chassis_mac = NULL;
remote_chassis_mac = xmalloc(sizeof *remote_chassis_mac);
hmap_insert(&remote_chassis_macs, &remote_chassis_mac->hmap_node,
hash_string(chassis_mac_bridge, 0));
remote_chassis_mac->chassis_mac = xstrdup(chassis_mac_str);
remote_chassis_mac->chassis_id = xstrdup(chassis->name);
remote_chassis_mac->chassis_sb_cookie =
chassis->header_.uuid.parts[0];
}
free(tokstr);
}
}
static void
free_remote_chassis_macs(void)
{
struct remote_chassis_mac *mac, *next_mac;
HMAP_FOR_EACH_SAFE (mac, next_mac, hmap_node, &remote_chassis_macs) {
hmap_remove(&remote_chassis_macs, &mac->hmap_node);
free(mac->chassis_mac);
free(mac->chassis_id);
free(mac);
}
}
static void
put_chassis_mac_conj_id_flow(const struct sbrec_chassis_table *chassis_table,
const struct sbrec_chassis *chassis,
struct ofpbuf *ofpacts_p,
struct ovn_desired_flow_table *flow_table)
{
struct match match;
struct remote_chassis_mac *mac;
populate_remote_chassis_macs(chassis, chassis_table);
HMAP_FOR_EACH (mac, hmap_node, &remote_chassis_macs) {
struct eth_addr chassis_mac;
char *err_str = NULL;
struct ofpact_conjunction *conj;
if ((err_str = str_to_mac(mac->chassis_mac, &chassis_mac))) {
free(err_str);
free_remote_chassis_macs();
return;
}
ofpbuf_clear(ofpacts_p);
match_init_catchall(&match);
match_set_dl_src(&match, chassis_mac);
conj = ofpact_put_CONJUNCTION(ofpacts_p);
conj->id = CHASSIS_MAC_TO_ROUTER_MAC_CONJID;
conj->n_clauses = 2;
conj->clause = 0;
ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 180,
mac->chassis_sb_cookie,
&match, ofpacts_p, hc_uuid);
}
free_remote_chassis_macs();
}
static void
put_replace_chassis_mac_flows(const struct simap *ct_zones,
const struct
sbrec_port_binding *localnet_port,
const struct hmap *local_datapaths,
struct ofpbuf *ofpacts_p,
ofp_port_t ofport,
struct ovn_desired_flow_table *flow_table)
{
/* Packets arriving on localnet port, could have been routed on
* source chassis and hence will have a chassis mac.
* conj_match will match source mac with chassis macs conjunction
* and replace it with corresponding router port mac.
*/
struct local_datapath *ld = get_local_datapath(local_datapaths,
localnet_port->datapath->
tunnel_key);
ovs_assert(ld);
int tag = localnet_port->tag ? *localnet_port->tag : 0;
struct zone_ids zone_ids = get_zone_ids(localnet_port, ct_zones);
for (int i = 0; i < ld->n_peer_ports; i++) {
const struct sbrec_port_binding *rport_binding =
ld->peer_ports[i].remote;
struct eth_addr router_port_mac;
char *err_str = NULL;
struct match match;
struct ofpact_mac *replace_mac;
ovs_assert(rport_binding->n_mac == 1);
if ((err_str = str_to_mac(rport_binding->mac[0], &router_port_mac))) {
/* Parsing of mac failed. */
VLOG_WARN("Parsing or router port mac failed for router port: %s, "
"with error: %s", rport_binding->logical_port, err_str);
free(err_str);
return;
}
ofpbuf_clear(ofpacts_p);
match_init_catchall(&match);
/* Add flow, which will match on conjunction id and will
* replace source with router port mac */
/* Match on ingress port, vlan_id and conjunction id */
match_set_in_port(&match, ofport);
match_set_conj_id(&match, CHASSIS_MAC_TO_ROUTER_MAC_CONJID);
if (tag) {
match_set_dl_vlan(&match, htons(tag), 0);
} else {
match_set_dl_tci_masked(&match, 0, htons(VLAN_CFI));
}
/* Actions */
if (tag) {
ofpact_put_STRIP_VLAN(ofpacts_p);
}
load_logical_ingress_metadata(localnet_port, &zone_ids, ofpacts_p);
replace_mac = ofpact_put_SET_ETH_SRC(ofpacts_p);
replace_mac->mac = router_port_mac;
/* Resubmit to first logical ingress pipeline table. */
put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, ofpacts_p);
ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 180,
rport_binding->header_.uuid.parts[0],
&match, ofpacts_p, hc_uuid);
/* Provide second search criteria, i.e localnet port's
* vlan ID for conjunction flow */
struct ofpact_conjunction *conj;
ofpbuf_clear(ofpacts_p);
match_init_catchall(&match);
if (tag) {
match_set_dl_vlan(&match, htons(tag), 0);
} else {
match_set_dl_tci_masked(&match, 0, htons(VLAN_CFI));
}
conj = ofpact_put_CONJUNCTION(ofpacts_p);
conj->id = CHASSIS_MAC_TO_ROUTER_MAC_CONJID;
conj->n_clauses = 2;
conj->clause = 1;
ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 180,
rport_binding->header_.uuid.parts[0],
&match, ofpacts_p, hc_uuid);
}
}
#define VLAN_80211AD_ETHTYPE 0x88a8
#define VLAN_80211Q_ETHTYPE 0x8100
static void
ofpact_put_push_vlan(struct ofpbuf *ofpacts, const struct smap *options, int tag)
{
const char *ethtype_opt = options ? smap_get(options, "ethtype") : NULL;
int ethtype = VLAN_80211Q_ETHTYPE;
if (ethtype_opt) {
if (!strcasecmp(ethtype_opt, "802.11ad")) {
ethtype = VLAN_80211AD_ETHTYPE;
} else if (strcasecmp(ethtype_opt, "802.11q")) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "Unknown port ethtype: %s", ethtype_opt);
}
}
struct ofpact_push_vlan *push_vlan;
push_vlan = ofpact_put_PUSH_VLAN(ofpacts);
push_vlan->ethertype = htons(ethtype);
struct ofpact_vlan_vid *vlan_vid;
vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
vlan_vid->vlan_vid = tag;
vlan_vid->push_vlan_if_needed = false;
}
static void
put_replace_router_port_mac_flows(struct ovsdb_idl_index
*sbrec_port_binding_by_name,
const struct
sbrec_port_binding *localnet_port,
const struct sbrec_chassis *chassis,
const struct sset *active_tunnels,
const struct hmap *local_datapaths,
struct ofpbuf *ofpacts_p,
ofp_port_t ofport,
struct ovn_desired_flow_table *flow_table)
{
struct local_datapath *ld = get_local_datapath(local_datapaths,
localnet_port->datapath->
tunnel_key);
ovs_assert(ld);
uint32_t dp_key = localnet_port->datapath->tunnel_key;
uint32_t port_key = localnet_port->tunnel_key;
int tag = localnet_port->tag ? *localnet_port->tag : 0;
const char *network = smap_get(&localnet_port->options, "network_name");
struct eth_addr chassis_mac;
if (!network) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "Physical network not configured for datapath:"
"%"PRId64" with localnet port",
localnet_port->datapath->tunnel_key);
return;
}
/* Get chassis mac */
if (!chassis_get_mac(chassis, network, &chassis_mac)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
/* Keeping the log level low for backward compatibility.
* Chassis mac is a new configuration.
*/
VLOG_DBG_RL(&rl, "Could not get chassis mac for network: %s", network);
return;
}
for (int i = 0; i < ld->n_peer_ports; i++) {
const struct sbrec_port_binding *rport_binding =
ld->peer_ports[i].remote;
struct eth_addr router_port_mac;
struct match match;
struct ofpact_mac *replace_mac;
char *cr_peer_name = xasprintf("cr-%s", rport_binding->logical_port);
if (lport_is_chassis_resident(sbrec_port_binding_by_name,
chassis, active_tunnels,
cr_peer_name)) {
/* If a router port's chassisredirect port is
* resident on this chassis, then we need not do mac replace. */
free(cr_peer_name);
continue;
}
free(cr_peer_name);
/* Table 65, priority 150.
* =======================
*
* Implements output to localnet port.
* a. Flow replaces ingress router port mac with a chassis mac.
* b. Flow appends the vlan id localnet port is configured with.
*/
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
ovs_assert(rport_binding->n_mac == 1);
char *err_str = str_to_mac(rport_binding->mac[0], &router_port_mac);
if (err_str) {
/* Parsing of mac failed. */
VLOG_WARN("Parsing or router port mac failed for router port: %s, "
"with error: %s", rport_binding->logical_port, err_str);
free(err_str);
return;
}
/* Replace Router mac flow */
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
match_set_dl_src(&match, router_port_mac);
replace_mac = ofpact_put_SET_ETH_SRC(ofpacts_p);
replace_mac->mac = chassis_mac;
if (tag) {
ofpact_put_push_vlan(ofpacts_p, &localnet_port->options, tag);
}
ofpact_put_OUTPUT(ofpacts_p)->port = ofport;
ofctrl_add_flow(flow_table, OFTABLE_LOG_TO_PHY, 150,
localnet_port->header_.uuid.parts[0],
&match, ofpacts_p, &localnet_port->header_.uuid);
}
}
static void
put_local_common_flows(uint32_t dp_key,
const struct sbrec_port_binding *pb,
const struct sbrec_port_binding *parent_pb,
const struct zone_ids *zone_ids,
struct ofpbuf *ofpacts_p,
struct ovn_desired_flow_table *flow_table)
{
struct match match;
uint32_t port_key = pb->tunnel_key;
/* Table 38, priority 100.
* =======================
*
* Implements output to local hypervisor. Each flow matches a
* logical output port on the local hypervisor, and resubmits to
* table 39.
*/
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
/* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
if (zone_ids) {
if (zone_ids->ct) {
put_load(zone_ids->ct, MFF_LOG_CT_ZONE, 0, 32, ofpacts_p);
}
if (zone_ids->dnat) {
put_load(zone_ids->dnat, MFF_LOG_DNAT_ZONE, 0, 32, ofpacts_p);
}
if (zone_ids->snat) {
put_load(zone_ids->snat, MFF_LOG_SNAT_ZONE, 0, 32, ofpacts_p);
}
}
/* Resubmit to table 39. */
put_resubmit(OFTABLE_CHECK_LOOPBACK, ofpacts_p);
ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100,
pb->header_.uuid.parts[0], &match, ofpacts_p,
&pb->header_.uuid);
/* Table 39, Priority 100.
* =======================
*
* Drop packets whose logical inport and outport are the same
* and the MLF_ALLOW_LOOPBACK flag is not set. */
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
match_set_metadata(&match, htonll(dp_key));
match_set_reg_masked(&match, MFF_LOG_FLAGS - MFF_REG0,
0, MLF_ALLOW_LOOPBACK);
match_set_reg(&match, MFF_LOG_INPORT - MFF_REG0, port_key);
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
ofctrl_add_flow(flow_table, OFTABLE_CHECK_LOOPBACK, 100,
pb->header_.uuid.parts[0], &match, ofpacts_p,
&pb->header_.uuid);
/* Table 64, Priority 100.
* =======================
*
* If the packet is supposed to hair-pin because the
* - "loopback" flag is set
* - or if the destination is a nested container
* - or if "nested_container" flag is set and the destination is the
* parent port,
* temporarily set the in_port to OFPP_NONE, resubmit to
* table 65 for logical-to-physical translation, then restore
* the port number.
*
* If 'parent_pb' is not NULL, then the 'pb' represents a nested
* container.
*
* Note:We can set in_port to 0 too. But if recirculation happens
* later (eg. clone action to enter peer pipeline and a subsequent
* ct action), ovs-vswitchd will drop the packet if the frozen metadata
* in_port is 0.
* */
bool nested_container = parent_pb ? true: false;
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
if (!nested_container) {
match_set_reg_masked(&match, MFF_LOG_FLAGS - MFF_REG0,
MLF_ALLOW_LOOPBACK, MLF_ALLOW_LOOPBACK);
}
put_stack(MFF_IN_PORT, ofpact_put_STACK_PUSH(ofpacts_p));
put_load(ofp_to_u16(OFPP_NONE), MFF_IN_PORT, 0, 16, ofpacts_p);
put_resubmit(OFTABLE_LOG_TO_PHY, ofpacts_p);
put_stack(MFF_IN_PORT, ofpact_put_STACK_POP(ofpacts_p));
ofctrl_add_flow(flow_table, OFTABLE_SAVE_INPORT, 100,
pb->header_.uuid.parts[0], &match, ofpacts_p,
&pb->header_.uuid);
if (nested_container) {
/* It's a nested container and when the packet from the nested
* container is to be sent to the parent port, "nested_container"
* flag will be set. We need to temporarily set the in_port to
* OFPP_NONE as mentioned in the comment above.
*
* If a parent port has multiple child ports, then this if condition
* will be hit multiple times, but we want to add only one flow.
* ofctrl_add_flow() logs a warning message for duplicate flows.
* So use the function 'ofctrl_check_and_add_flow_metered' which
* doesn't log a warning.
*
* Other option is to add this flow for all the ports which are not
* nested containers. In which case we will add this flow for all the
* ports even if they don't have any child ports which is
* unnecessary.
*/
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
parent_pb->tunnel_key);
match_set_reg_masked(&match, MFF_LOG_FLAGS - MFF_REG0,
MLF_NESTED_CONTAINER, MLF_NESTED_CONTAINER);
put_stack(MFF_IN_PORT, ofpact_put_STACK_PUSH(ofpacts_p));
put_load(ofp_to_u16(OFPP_NONE), MFF_IN_PORT, 0, 16, ofpacts_p);
put_resubmit(OFTABLE_LOG_TO_PHY, ofpacts_p);
put_stack(MFF_IN_PORT, ofpact_put_STACK_POP(ofpacts_p));
ofctrl_check_and_add_flow_metered(flow_table, OFTABLE_SAVE_INPORT, 100,
0, &match, ofpacts_p, hc_uuid,
NX_CTLR_NO_METER, false);
}
}
static void
load_logical_ingress_metadata(const struct sbrec_port_binding *binding,
const struct zone_ids *zone_ids,
struct ofpbuf *ofpacts_p)
{
if (zone_ids) {
if (zone_ids->ct) {
put_load(zone_ids->ct, MFF_LOG_CT_ZONE, 0, 32, ofpacts_p);
}
if (zone_ids->dnat) {
put_load(zone_ids->dnat, MFF_LOG_DNAT_ZONE, 0, 32, ofpacts_p);
}
if (zone_ids->snat) {
put_load(zone_ids->snat, MFF_LOG_SNAT_ZONE, 0, 32, ofpacts_p);
}
}
/* Set MFF_LOG_DATAPATH and MFF_LOG_INPORT. */
uint32_t dp_key = binding->datapath->tunnel_key;
uint32_t port_key = binding->tunnel_key;
put_load(dp_key, MFF_LOG_DATAPATH, 0, 64, ofpacts_p);
put_load(port_key, MFF_LOG_INPORT, 0, 32, ofpacts_p);
}
static const struct sbrec_port_binding *
get_binding_peer(struct ovsdb_idl_index *sbrec_port_binding_by_name,
const struct sbrec_port_binding *binding)
{
const char *peer_name = smap_get(&binding->options, "peer");
if (!peer_name) {
return NULL;
}
const struct sbrec_port_binding *peer = lport_lookup_by_name(
sbrec_port_binding_by_name, peer_name);
if (!peer || strcmp(peer->type, binding->type)) {
return NULL;
}
const char *peer_peer_name = smap_get(&peer->options, "peer");
if (!peer_peer_name || strcmp(peer_peer_name, binding->logical_port)) {
return NULL;
}
return peer;
}
static void
consider_port_binding(struct ovsdb_idl_index *sbrec_port_binding_by_name,
enum mf_field_id mff_ovn_geneve,
const struct simap *ct_zones,
const struct sset *active_tunnels,
const struct hmap *local_datapaths,
const struct shash *local_bindings,
const struct simap *patch_ofports,
const struct hmap *chassis_tunnels,
const struct sbrec_port_binding *binding,
const struct sbrec_chassis *chassis,
struct ovn_desired_flow_table *flow_table,
struct ofpbuf *ofpacts_p)
{
uint32_t dp_key = binding->datapath->tunnel_key;
uint32_t port_key = binding->tunnel_key;
struct local_datapath *ld;
if (!(ld = get_local_datapath(local_datapaths, dp_key))) {
return;
}
struct match match;
if (!strcmp(binding->type, "patch")
|| (!strcmp(binding->type, "l3gateway")
&& binding->chassis == chassis)) {
const struct sbrec_port_binding *peer = get_binding_peer(
sbrec_port_binding_by_name, binding);
if (!peer) {
return;
}
struct zone_ids binding_zones = get_zone_ids(binding, ct_zones);
put_local_common_flows(dp_key, binding, NULL, &binding_zones,
ofpacts_p, flow_table);
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
size_t clone_ofs = ofpacts_p->size;
struct ofpact_nest *clone = ofpact_put_CLONE(ofpacts_p);
ofpact_put_CT_CLEAR(ofpacts_p);
put_load(0, MFF_LOG_DNAT_ZONE, 0, 32, ofpacts_p);
put_load(0, MFF_LOG_SNAT_ZONE, 0, 32, ofpacts_p);
put_load(0, MFF_LOG_CT_ZONE, 0, 32, ofpacts_p);
struct zone_ids peer_zones = get_zone_ids(peer, ct_zones);
load_logical_ingress_metadata(peer, &peer_zones, ofpacts_p);
put_load(0, MFF_LOG_FLAGS, 0, 32, ofpacts_p);
put_load(0, MFF_LOG_OUTPORT, 0, 32, ofpacts_p);
for (int i = 0; i < MFF_N_LOG_REGS; i++) {
put_load(0, MFF_LOG_REG0 + i, 0, 32, ofpacts_p);
}
put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, ofpacts_p);
clone = ofpbuf_at_assert(ofpacts_p, clone_ofs, sizeof *clone);
ofpacts_p->header = clone;
ofpact_finish_CLONE(ofpacts_p, &clone);
ofctrl_add_flow(flow_table, OFTABLE_LOG_TO_PHY, 100,
binding->header_.uuid.parts[0],
&match, ofpacts_p, &binding->header_.uuid);
return;
}
struct ha_chassis_ordered *ha_ch_ordered
= ha_chassis_get_ordered(binding->ha_chassis_group);
if (!strcmp(binding->type, "chassisredirect")
&& (binding->chassis == chassis
|| ha_chassis_group_is_active(binding->ha_chassis_group,
active_tunnels, chassis))) {
/* Table 33, priority 100.
* =======================
*
* Implements output to local hypervisor. Each flow matches a
* logical output port on the local hypervisor, and resubmits to
* table 34. For ports of type "chassisredirect", the logical
* output port is changed from the "chassisredirect" port to the
* underlying distributed port. */
match_init_catchall(&match);
ofpbuf_clear(ofpacts_p);
match_set_metadata(&match, htonll(dp_key));
match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
const char *distributed_port = smap_get_def(&binding->options,
"distributed-port", "");
const struct sbrec_port_binding *distributed_binding
= lport_lookup_by_name(sbrec_port_binding_by_name,
distributed_port);
if (!distributed_binding) {
/* Packet will be dropped. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl, "No port binding record for distributed "
"port %s referred by chassisredirect port %s",
distributed_port,
binding->logical_port);
} else if (binding->datapath !=
distributed_binding->datapath) {
/* Packet will be dropped. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
VLOG_WARN_RL(&rl,
"chassisredirect port %s refers to "
"distributed port %s in wrong datapath",
binding->logical_port,
distributed_port);
} else {
put_load(distributed_binding->tunnel_key,
MFF_LOG_OUTPORT, 0, 32, ofpacts_p);
struct zone_ids zone_ids = get_zone_ids(distributed_binding,
ct_zones);
if (zone_ids.ct) {