/
vr_nexthop.c
3123 lines (2571 loc) · 86.5 KB
/
vr_nexthop.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
/*
* vr_nexthop.c -- data path nexthop management
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <vr_os.h>
#include <vr_types.h>
#include <vr_packet.h>
#include <vr_interface.h>
#include <vr_nexthop.h>
#include <vr_vxlan.h>
#include "vr_message.h"
#include "vr_sandesh.h"
#include "vr_bridge.h"
#include "vr_btable.h"
#include "vr_datapath.h"
#include "vr_route.h"
#include "vr_hash.h"
extern bool vr_has_to_fragment(struct vr_interface *, struct vr_packet *,
unsigned int);
extern struct vr_vrf_stats *(*vr_inet_vrf_stats)(unsigned short, unsigned int);
extern struct vr_nexthop *vr_inet6_ip_lookup(unsigned short, uint8_t *);
extern struct vr_nexthop *vr_inet_ip_lookup(unsigned short, uint32_t);
extern struct vr_nexthop *vr_inet_src_lookup(unsigned short,
struct vr_packet *);
extern l4_pkt_type_t vr_ip6_well_known_packet(struct vr_packet *);
extern l4_pkt_type_t vr_ip_well_known_packet(struct vr_packet *);
struct vr_nexthop *ip4_default_nh;
struct vr_nexthop *ip6_default_nh;
unsigned int vr_nexthops = VR_DEF_NEXTHOPS;
struct vr_nexthop *
__vrouter_get_nexthop(struct vrouter *router, unsigned int index)
{
if (!router || index >= router->vr_max_nexthops)
return NULL;
return *(struct vr_nexthop **)vr_btable_get(router->vr_nexthops, index);
}
struct vr_nexthop *
vrouter_get_nexthop(unsigned int rid, unsigned int index)
{
struct vr_nexthop *nh;
struct vrouter *router;
router = vrouter_get(rid);
nh = __vrouter_get_nexthop(router, index);
if (nh)
nh->nh_users++;
return nh;
}
static int
__vrouter_set_nexthop(struct vrouter *router, unsigned int index,
struct vr_nexthop *nh)
{
struct vr_nexthop **nh_p;
nh_p = (struct vr_nexthop **)vr_btable_get(router->vr_nexthops, index);
if (!nh_p)
return -EINVAL;
*nh_p = nh;
return 0;
}
static void
vrouter_free_nexthop(struct vr_nexthop *nh)
{
if (nh->nh_type == NH_COMPOSITE) {
if (nh->nh_component_nh) {
vr_free(nh->nh_component_nh, VR_NEXTHOP_COMPONENT_OBJECT);
nh->nh_component_nh = NULL;
}
if (nh->nh_component_ecmp) {
nh->nh_component_ecmp_cnt = 0;
vr_free(nh->nh_component_ecmp, VR_NEXTHOP_COMPONENT_OBJECT);
nh->nh_component_ecmp = NULL;
}
} else if ((nh->nh_type == NH_TUNNEL) &&
(nh->nh_flags & NH_FLAG_TUNNEL_UDP) &&
(nh->nh_family == AF_INET6)) {
if (nh->nh_udp_tun6_sip) {
vr_free(nh->nh_udp_tun6_sip, VR_NETWORK_ADDRESS_OBJECT);
nh->nh_udp_tun6_sip = NULL;
}
if (nh->nh_udp_tun6_dip) {
vr_free(nh->nh_udp_tun6_dip, VR_NETWORK_ADDRESS_OBJECT);
nh->nh_udp_tun6_dip = NULL;
}
}
if (nh->nh_dev) {
vrouter_put_interface(nh->nh_dev);
}
vr_free(nh, VR_NEXTHOP_OBJECT);
return;
}
static void
vrouter_free_nexthop_cb(struct vrouter *router, void *data)
{
struct vr_defer_data *vdd = (struct vr_defer_data *)data;
if (!vdd)
return;
vrouter_free_nexthop((struct vr_nexthop *)vdd->vdd_data);
return;
}
static int
vrouter_free_nexthop_defer(struct vr_nexthop *nh)
{
struct vr_defer_data *defer;
defer = vr_get_defer_data(sizeof(*defer));
if (!defer)
return -ENOMEM;
defer->vdd_data = nh;
vr_defer(nh->nh_router, vrouter_free_nexthop_cb, (void *)defer);
return 0;
}
void
vrouter_put_nexthop(struct vr_nexthop *nh)
{
int i, component_cnt;
struct vr_nexthop *cnh;
unsigned int ref_cnt;
if (!nh)
return;
/* This function might get invoked with zero ref_cnt */
ref_cnt = nh->nh_users;
if (ref_cnt) {
ref_cnt = __sync_sub_and_fetch(&nh->nh_users, 1);
}
if (!ref_cnt ) {
/* If composite de-ref the internal nexthops */
if (nh->nh_type == NH_COMPOSITE) {
component_cnt = nh->nh_component_cnt;
nh->nh_component_cnt = 0;
for (i = 0; i < component_cnt; i++) {
if (nh->nh_component_nh[i].cnh) {
cnh = nh->nh_component_nh[i].cnh;
nh->nh_component_nh[i].cnh = NULL;
vrouter_put_nexthop(cnh);
}
}
}
if (vr_not_ready) {
vrouter_free_nexthop(nh);
} else {
if (vrouter_free_nexthop_defer(nh)) {
vr_delay_op();
vrouter_free_nexthop(nh);
}
}
}
return;
}
static int
vrouter_add_nexthop(struct vr_nexthop *nh)
{
struct vrouter *router = vrouter_get(nh->nh_rid);
if (!router || nh->nh_id >= router->vr_max_nexthops)
return -EINVAL;
/*
* NH change just copies the field
* over to nexthop, incase of change
* just return
*/
if (__vrouter_get_nexthop(router, nh->nh_id))
return 0;
nh->nh_users++;
return __vrouter_set_nexthop(router, nh->nh_id, nh);
}
static void
nh_del(struct vr_nexthop *nh)
{
struct vrouter *router = vrouter_get(nh->nh_rid);
if (!router || nh->nh_id >= router->vr_max_nexthops)
return;
__vrouter_set_nexthop(router, nh->nh_id, NULL);
vrouter_put_nexthop(nh);
return;
}
bool
vr_gateway_nexthop(struct vr_nexthop *nh)
{
if (nh) {
if (!nh->nh_dev)
return false;
if ((nh->nh_type == NH_ENCAP) &&
(nh->nh_dev->vif_type == VIF_TYPE_AGENT))
return true;
}
return false;
}
static int
nh_resolve(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
struct vr_vrf_stats *stats;
struct vr_packet *pkt_clone;
stats = vr_inet_vrf_stats(fmd->fmd_dvrf, pkt->vp_cpu);
if (stats)
stats->vrf_resolves++;
if (pkt->vp_if->vif_bridge) {
/*
* bridge is set only for vhost/physical interface, and this
* path will be hit only for packets from vhost, in which case
* we already know everything that has to be known i.e. we know
* the outgoing device and the mac address (which was already
* resolved as part of the arp request from host
*/
pkt_clone = vr_pclone(pkt);
if (pkt_clone) {
vr_preset(pkt_clone);
vif_xconnect(pkt->vp_if, pkt_clone, fmd);
}
}
/* will trap the packet to agent to create a route */
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_RESOLVE, NULL);
return 0;
}
static int
nh_vrf_translate(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
struct vr_vrf_stats *stats;
stats = vr_inet_vrf_stats(nh->nh_vrf, pkt->vp_cpu);
if (stats)
stats->vrf_vrf_translates++;
fmd->fmd_dvrf = nh->nh_vrf;
if (!(nh->nh_flags & NH_FLAG_VNID))
return vr_forward(nh->nh_router, pkt, fmd);
return vr_bridge_input(nh->nh_router, pkt, fmd);
}
static int
nh_l2_rcv(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
struct vr_vrf_stats *stats;
int pull_len, handled = 0;
stats = vr_inet_vrf_stats(fmd->fmd_dvrf, pkt->vp_cpu);
if (stats)
stats->vrf_l2_receives++;
fmd->fmd_to_me = 1;
pull_len = pkt_get_network_header_off(pkt) - pkt_head_space(pkt);
if (pkt_pull(pkt, pull_len) < 0) {
vr_pfree(pkt, VP_DROP_PULL);
return 0;
}
/*
* there should not be any unicast ARP requests destined to "my mac".
* The ARP response destined to "my mac" incase of ARP request being
* generated by Agent for some features
*/
if (pkt->vp_type == VP_TYPE_IP6) {
handled = vr_neighbor_input(pkt, fmd);
if (!handled)
handled = vr_l3_input(pkt, fmd);
} else if (pkt->vp_type == VP_TYPE_IP) {
handled = vr_l3_input(pkt, fmd);
} else if (pkt->vp_type == VP_TYPE_ARP) {
handled = vr_arp_input(pkt, fmd);
}
if (!handled)
vr_pfree(pkt, VP_DROP_INVALID_PROTOCOL);
return 0;
}
static int
nh_l3_rcv(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
struct vr_vrf_stats *stats;
stats = vr_inet_vrf_stats(fmd->fmd_dvrf, pkt->vp_cpu);
if (stats)
stats->vrf_receives++;
if (nh->nh_family == AF_INET)
return vr_ip_rcv(nh->nh_router, pkt, fmd);
else
vr_pfree(pkt, VP_DROP_INVALID_PROTOCOL);
return 0;
}
static int
nh_push_mpls_header(struct vr_packet *pkt, unsigned int label,
struct vr_forwarding_class_qos *qos)
{
uint32_t exp_qos = 0;
unsigned int *lbl;
unsigned int ttl;
lbl = (unsigned int *)pkt_push(pkt, sizeof(unsigned int));
if (!lbl)
return -ENOSPC;
/* Use the ttl from packet. If not ttl,
* initialise to some arbitrary value */
ttl = pkt->vp_ttl;
if (!ttl) {
ttl = 64;
}
if (qos) {
exp_qos = qos->vfcq_mpls_qos;
exp_qos <<= VR_MPLS_EXP_QOS_SHIFT;
}
*lbl = htonl((label << VR_MPLS_LABEL_SHIFT) | exp_qos |
VR_MPLS_STACK_BIT | ttl);
return 0;
}
/*
* nh_udp_tunnel_helper - helper function to use for UDP tunneling. Used
* by mirroring and MPLS over UDP. Returns true on success, false otherwise.
*/
static bool
nh_udp_tunnel_helper(struct vr_packet *pkt, unsigned short sport,
unsigned short dport, unsigned int sip,
unsigned int dip, struct vr_forwarding_class_qos *qos)
{
struct vr_ip *ip;
struct vr_udp *udp;
/* Udp Header */
udp = (struct vr_udp *)pkt_push(pkt, sizeof(struct vr_udp));
if (!udp) {
return false;
}
udp->udp_sport = sport;
udp->udp_dport = dport;
udp->udp_length = htons(pkt_len(pkt));
udp->udp_csum = 0;
/* And now the IP header */
ip = (struct vr_ip *)pkt_push(pkt, sizeof(struct vr_ip));
if (!ip) {
return false;
}
ip->ip_version = 4;
ip->ip_hl = 5;
if (qos) {
ip->ip_tos = VR_IP_DSCP(qos->vfcq_dscp);
pkt->vp_queue = qos->vfcq_queue_id + 1;
pkt->vp_priority = qos->vfcq_dotonep_qos;
} else {
ip->ip_tos = 0;
}
ip->ip_id = htons(vr_generate_unique_ip_id());
ip->ip_frag_off = 0;
if (vr_pkt_is_diag(pkt)) {
ip->ip_ttl = pkt->vp_ttl;
} else {
ip->ip_ttl = 64;
}
ip->ip_proto = VR_IP_PROTO_UDP;
ip->ip_saddr = sip;
ip->ip_daddr = dip;
ip->ip_len = htons(pkt_len(pkt));
/*
* header checksum
*/
ip->ip_csum = 0;
ip->ip_csum = vr_ip_csum(ip);
return true;
}
static bool
nh_udp_tunnel6_helper(struct vr_packet *pkt, struct vr_nexthop *nh)
{
unsigned int v4_ip;
uint8_t *sip = NULL;
uint8_t sip6[VR_IP6_ADDRESS_LEN];
struct vr_ip6 *ip6;
struct vr_ip *ip;
struct vr_udp *udp;
if (nh->nh_flags & NH_FLAG_TUNNEL_SIP_COPY) {
if (pkt->vp_type == VP_TYPE_IP6) {
ip6 = (struct vr_ip6 *)pkt_network_header(pkt);
if (pkt->vp_if->vif_type == VIF_TYPE_PHYSICAL)
sip = ip6->ip6_dst;
sip = ip6->ip6_src;
} else if (pkt->vp_type == VP_TYPE_IP) {
ip = (struct vr_ip *)pkt_network_header(pkt);
v4_ip = ip->ip_saddr;
if (pkt->vp_if->vif_type == VIF_TYPE_PHYSICAL)
v4_ip = ip->ip_daddr;
vr_inet6_generate_ip6(sip6, v4_ip);
sip = sip6;
}
}
/* udp Header */
udp = (struct vr_udp *)pkt_push(pkt, sizeof(struct vr_udp));
if (!udp) {
return false;
}
udp->udp_sport = nh->nh_udp_tun6_sport;
udp->udp_dport = nh->nh_udp_tun6_dport;
udp->udp_length = htons(pkt_len(pkt));
udp->udp_csum = 0;
/* And now the IP6 header */
ip6 = (struct vr_ip6 *)pkt_push(pkt, sizeof(struct vr_ip6));
if (!ip6) {
return false;
}
ip6->ip6_version = 6;
ip6->ip6_priority_l = 0;
ip6->ip6_priority_h = 0;
ip6->ip6_flow_l = 0;
ip6->ip6_flow_h = 0;
ip6->ip6_plen = htons(pkt_len(pkt) - sizeof(struct vr_ip6));
ip6->ip6_nxt = VR_IP_PROTO_UDP;
ip6->ip6_hlim = 64;
if (!sip)
sip = nh->nh_udp_tun6_sip;
memcpy(ip6->ip6_src, sip, VR_IP6_ADDRESS_LEN);
memcpy(ip6->ip6_dst, nh->nh_udp_tun6_dip, VR_IP6_ADDRESS_LEN);
return true;
}
static bool
nh_vxlan_tunnel_helper(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd, unsigned int sip, unsigned int dip)
{
unsigned short udp_src_port = VR_VXLAN_UDP_SRC_PORT;
struct vr_vxlan *vxlanh;
struct vr_packet *tmp_pkt;
struct vr_forwarding_class_qos *qos;
if (pkt_head_space(pkt) < VR_VXLAN_HDR_LEN) {
tmp_pkt = vr_pexpand_head(pkt, VR_VXLAN_HDR_LEN - pkt_head_space(pkt));
if (!tmp_pkt) {
return false;
}
pkt = tmp_pkt;
}
if (fmd->fmd_udp_src_port)
udp_src_port = fmd->fmd_udp_src_port;
/*
* The UDP source port is a hash of the inner headers
*/
if ((!fmd->fmd_udp_src_port) && vr_get_udp_src_port) {
udp_src_port = vr_get_udp_src_port(pkt, fmd, fmd->fmd_dvrf);
if (udp_src_port == 0) {
return false;
}
}
vr_forwarding_md_update_label_type(fmd, VR_LABEL_TYPE_VXLAN_ID);
/* Add the vxlan header */
vxlanh = (struct vr_vxlan *)pkt_push(pkt, sizeof(struct vr_vxlan));
vxlanh->vxlan_vnid = htonl(fmd->fmd_label << VR_VXLAN_VNID_SHIFT);
vxlanh->vxlan_flags = htonl(VR_VXLAN_IBIT);
qos = vr_qos_get_forwarding_class(router, pkt, fmd);
return nh_udp_tunnel_helper(pkt, htons(udp_src_port),
htons(VR_VXLAN_UDP_DST_PORT), sip, dip, qos);
}
static struct vr_packet *
nh_mcast_clone(struct vr_packet *pkt, unsigned short head_room)
{
struct vr_packet *clone_pkt;
/* Clone the packet */
clone_pkt = vr_pclone(pkt);
if (!clone_pkt) {
return NULL;
}
/* Increase the head space by the head_room */
if (vr_pcow(clone_pkt, head_room)) {
vr_pfree(clone_pkt, VP_DROP_PCOW_FAIL);
return NULL;
}
/* Copy the ttl from old packet */
clone_pkt->vp_ttl = pkt->vp_ttl;
return clone_pkt;
}
static int
nh_composite_ecmp_validate_src(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd, void *ret_data)
{
int i;
struct vr_nexthop *cnh;
/* the first few checks are straight forward */
if (!fmd || (uint8_t)fmd->fmd_ecmp_src_nh_index >= nh->nh_component_cnt)
return NH_SOURCE_INVALID;
cnh = nh->nh_component_nh[fmd->fmd_ecmp_src_nh_index].cnh;
if (cnh && !cnh->nh_validate_src)
return NH_SOURCE_INVALID;
/*
* when the 'supposed' source goes down, cnh is null, in which
* case validate the source against other present nexthops. follow
* the same logic if the component validate source returns invalid
* source, which could mean that source has moved
*/
if (!cnh || (NH_SOURCE_INVALID ==
cnh->nh_validate_src(pkt, cnh, fmd, NULL))) {
for (i = 0; i < nh->nh_component_cnt; i++) {
if (i == fmd->fmd_ecmp_src_nh_index)
continue;
cnh = nh->nh_component_nh[i].cnh;
/* If direct nexthop is not valid, dont process it */
if (!cnh || !(cnh->nh_flags & NH_FLAG_VALID) ||
!cnh->nh_validate_src)
continue;
/*
* if the source has moved to a present and valid source,
* return mismatch
*/
if ((NH_SOURCE_VALID ==
cnh->nh_validate_src(pkt, cnh, fmd, NULL)))
return NH_SOURCE_MISMATCH;
}
/* if everything else fails, source is indeed invalid */
return NH_SOURCE_INVALID;
}
/* source is validated by validate_src */
return NH_SOURCE_VALID;
}
static struct vr_nexthop *
nh_composite_ecmp_select_nh(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
int ret;
unsigned int hash, hash_ecmp, count;
struct vr_flow flow;
struct vr_ip6 *ip6;
struct vr_nexthop *cnh = NULL;
struct vr_component_nh *cnhp = nh->nh_component_nh;
struct vr_component_nh *cnhp_ecmp = nh->nh_component_ecmp;
if (!(count = nh->nh_component_cnt))
return NULL;
if (pkt->vp_type == VP_TYPE_IP) {
ret = vr_inet_get_flow_key(nh->nh_router, pkt, fmd, &flow);
if (ret < 0)
return NULL;
} else if (pkt->vp_type == VP_TYPE_IP6) {
ip6 = (struct vr_ip6 *)pkt_network_header(pkt);
ret = vr_inet6_form_flow(nh->nh_router, fmd->fmd_dvrf, pkt,
fmd->fmd_vlan, ip6, &flow);
if (ret < 0)
return NULL;
} else {
return NULL;
}
hash = hash_ecmp = vr_hash(&flow, flow.flow_key_len, 0);
hash %= count;
cnh = cnhp[hash].cnh;
if (!cnh && nh->nh_component_ecmp_cnt) {
hash_ecmp %= nh->nh_component_ecmp_cnt;
cnh = cnhp_ecmp[hash_ecmp].cnh;
}
return cnh;
}
static int
nh_composite_ecmp(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
int ret = 0;
struct vr_nexthop *member_nh = NULL;
struct vr_vrf_stats *stats;
pkt->vp_type = VP_TYPE_IP;
stats = vr_inet_vrf_stats(fmd->fmd_dvrf, pkt->vp_cpu);
if (stats)
stats->vrf_ecmp_composites++;
if (!fmd || fmd->fmd_ecmp_nh_index >= (short)nh->nh_component_cnt)
goto drop;
if (fmd->fmd_ecmp_nh_index >= 0) {
member_nh = nh->nh_component_nh[fmd->fmd_ecmp_nh_index].cnh;
} else if (fmd->fmd_flow_index < 0) {
member_nh = nh_composite_ecmp_select_nh(pkt, nh, fmd);
}
if (!member_nh) {
if (fmd->fmd_flow_index < 0) {
vr_pfree(pkt, VP_DROP_INVALID_NH);
return 0;
} else {
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_ECMP_RESOLVE, &fmd->fmd_flow_index);
return 0;
}
}
vr_forwarding_md_set_label(fmd,
nh->nh_component_nh[fmd->fmd_ecmp_nh_index].cnh_label,
VR_LABEL_TYPE_UNKNOWN);
return nh_output(pkt, member_nh, fmd);
drop:
vr_pfree(pkt, VP_DROP_NO_FMD);
return ret;
}
/*
* This function validate the source of the tunnel incase of L2
* multicast
*/
static int
nh_composite_mcast_validate_src(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd, void *ret_flags)
{
int i, j;
struct vr_nexthop *dir_nh, *fabric_nh;
unsigned int tun_dip;
/*
* If multicast packet is received on fabric interface, we need to
* validate whether the source of this packet is in the distribution
* tree. If source is not in the list, packet needs to be dropped.
*/
/* Valid if source is VM */
if (pkt->vp_if->vif_type != VIF_TYPE_PHYSICAL)
return NH_SOURCE_VALID;
/* If there is no source IP to compare treat it as invalid source */
if (!fmd->fmd_outer_src_ip)
return NH_SOURCE_INVALID;
for(j = 0; j < nh->nh_component_cnt; j++) {
fabric_nh = nh->nh_component_nh[j].cnh;
if (!fabric_nh || !(fabric_nh->nh_flags & NH_FLAG_VALID))
continue;
if (fabric_nh->nh_type != NH_COMPOSITE ||
!(fabric_nh->nh_flags & (NH_FLAG_COMPOSITE_FABRIC |
NH_FLAG_COMPOSITE_EVPN | NH_FLAG_COMPOSITE_TOR)))
continue;
for (i = 0; i < fabric_nh->nh_component_cnt; i++) {
dir_nh = fabric_nh->nh_component_nh[i].cnh;
/* If direct nexthop is not valid, dont process it */
if ((!dir_nh) || !(dir_nh->nh_flags & NH_FLAG_VALID))
continue;
if (dir_nh->nh_type != NH_TUNNEL)
continue;
tun_dip = 0;
if (dir_nh->nh_flags & NH_FLAG_TUNNEL_GRE)
tun_dip = dir_nh->nh_gre_tun_dip;
else if (dir_nh->nh_flags &
(NH_FLAG_TUNNEL_UDP_MPLS | NH_FLAG_TUNNEL_VXLAN))
tun_dip = dir_nh->nh_udp_tun_dip;
/* If source is in districution tree, it is valid */
if (tun_dip && fmd->fmd_outer_src_ip &&
fmd->fmd_outer_src_ip == tun_dip) {
if (ret_flags)
*((unsigned int *)ret_flags) = fabric_nh->nh_flags;
return NH_SOURCE_VALID;
}
}
}
return NH_SOURCE_INVALID;
}
static int
nh_handle_mcast_control_pkt(struct vr_packet *pkt, struct vr_forwarding_md *fmd,
unsigned int pkt_src, bool *flood_to_vms)
{
int handled = 1;
unsigned short trap, rt_flags, drop_reason, pull_len = 0;
l4_pkt_type_t l4_type = L4_TYPE_UNKNOWN;
struct vr_eth *eth;
struct vr_arp *sarp;
struct vr_nexthop *src_nh;
struct vr_ip6 *ip6;
/*
* The vlan tagged packets are meant to be handled only by VM's
*/
if (fmd->fmd_vlan != VLAN_ID_INVALID)
return !handled;
eth = (struct vr_eth *)pkt_data(pkt);
pull_len = pkt_get_network_header_off(pkt) - pkt_head_space(pkt);
if (pkt_pull(pkt, pull_len) < 0) {
drop_reason = VP_DROP_PULL;
goto drop;
}
if (pkt->vp_type == VP_TYPE_ARP) {
handled = vr_arp_input(pkt, fmd);
if (handled)
return handled;
/*
* If not handled, packet needs to be flooded. If the ARP is
* from MX, VM's should not see this ARP as VM's always need to
* Agentas gateway
*/
if (pkt_src) {
sarp = (struct vr_arp *)pkt_data(pkt);
src_nh = vr_inet_ip_lookup(fmd->fmd_dvrf, sarp->arp_spa);
if (vr_gateway_nexthop(src_nh)) {
*flood_to_vms = false;
}
}
goto unhandled;
}
if ((pkt->vp_type != VP_TYPE_IP) && (pkt->vp_type != VP_TYPE_IP6))
goto unhandled;
/*
* V6 Ndisc, router solictation packets are ICMP packets. So we need
* to parse to identify the type, unlike V4 ARP
*/
if (pkt->vp_type == VP_TYPE_IP6)
l4_type = vr_ip6_well_known_packet(pkt);
else if (pkt_src == PKT_SRC_TOR_REPL_TREE)
l4_type = vr_ip_well_known_packet(pkt);
/*
* Special control packets need to be handled only if BMS
*/
if ((pkt_src == PKT_SRC_TOR_REPL_TREE) || !pkt_src) {
/*
* If packet is identified as known packet, we always trap
* it to Agentm with the exception of DHCP. DHCP can be flooded
* depending on the configuration on VMI or L2 route flags
*/
if (l4_type != L4_TYPE_UNKNOWN) {
trap = true;
if (l4_type == L4_TYPE_DHCP_REQUEST) {
rt_flags = vr_bridge_route_flags(fmd->fmd_dvrf, eth->eth_smac);
if (rt_flags & VR_BE_FLOOD_DHCP_FLAG)
trap = false;
} else if (l4_type == L4_TYPE_NEIGHBOUR_SOLICITATION) {
trap = false;
}
if (trap) {
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_L3_PROTOCOLS, NULL);
return handled;
}
}
}
if (l4_type == L4_TYPE_NEIGHBOUR_SOLICITATION) {
handled = vr_neighbor_input(pkt, fmd);
if (handled)
return handled;
if (pkt_src) {
ip6 = (struct vr_ip6 *)pkt_data(pkt);
src_nh = vr_inet6_ip_lookup(fmd->fmd_dvrf, ip6->ip6_src);
if (vr_gateway_nexthop(src_nh))
*flood_to_vms = false;
}
}
unhandled:
if (pull_len)
pkt_push(pkt, pull_len);
return 0;
drop:
vr_pfree(pkt, drop_reason);
return 1;
}
static int
nh_composite_mcast_l2(struct vr_packet *pkt, struct vr_nexthop *nh,
struct vr_forwarding_md *fmd)
{
int i, clone_size;
bool flood_to_vms = true;
unsigned short drop_reason, label, pkt_vrf, pull_len;
unsigned int tun_src, pkt_src, hashval, port_range, handled;
struct vr_eth *eth = NULL;
struct vr_nexthop *dir_nh;
struct vr_packet *new_pkt;
struct vr_vrf_stats *stats;
stats = vr_inet_vrf_stats(fmd->fmd_dvrf, pkt->vp_cpu);
if (stats)
stats->vrf_l2_mcast_composites++;
pkt_vrf = fmd->fmd_dvrf;
drop_reason = VP_DROP_CLONED_ORIGINAL;
if (!fmd) {
drop_reason = VP_DROP_NO_FMD;
goto drop;
}
tun_src = pkt_src = 0;
if (nh->nh_validate_src) {
if (nh->nh_validate_src(pkt, nh, fmd, &tun_src) == NH_SOURCE_INVALID) {
drop_reason = VP_DROP_INVALID_MCAST_SOURCE;
goto drop;
}
if (tun_src & NH_FLAG_COMPOSITE_EVPN)
pkt_src = PKT_SRC_INGRESS_REPL_TREE;
if (tun_src & NH_FLAG_COMPOSITE_TOR) {
pkt_src = PKT_SRC_TOR_REPL_TREE;
fmd->fmd_src = TOR_SOURCE;
}
if ((pkt->vp_if->vif_type == VIF_TYPE_PHYSICAL) &&
((pkt_src != PKT_SRC_INGRESS_REPL_TREE) &&
(pkt_src != PKT_SRC_TOR_REPL_TREE))) {
if (*(unsigned int *)pkt_data(pkt) != VR_L2_MCAST_CTRL_DATA) {
drop_reason = VP_DROP_INVALID_PACKET;
goto drop;
}
pkt_src = PKT_SRC_EDGE_REPL_TREE;
}
}
if (pkt_src == PKT_SRC_EDGE_REPL_TREE) {
eth = (struct vr_eth *)pkt_data_at_offset(pkt, pkt->vp_data +
VR_L2_MCAST_CTRL_DATA_LEN + VR_VXLAN_HDR_LEN);
} else {
eth = (struct vr_eth *)pkt_data(pkt);
}
if (!eth) {
drop_reason = VP_DROP_INVALID_PACKET;
goto drop;
}
handled = nh_handle_mcast_control_pkt(pkt, fmd, pkt_src, &flood_to_vms);
if (handled)
return 0;
/*
* The packet can come to this nexthp either from Fabric or from VM.
* Incase of Fabric, the packet would contain the Vxlan header and
* control information. From VM, it contains neither of them
*/
if (!fmd->fmd_udp_src_port) {
if (hashrnd_inited == 0) {
get_random_bytes(&vr_hashrnd, sizeof(vr_hashrnd));
hashrnd_inited = 1;
}
hashval = vr_hash(pkt_data(pkt), sizeof(struct vr_eth), vr_hashrnd);
/* Include the VRF to calculate the hash */
hashval = vr_hash_2words(hashval, fmd->fmd_dvrf, vr_hashrnd);
/*
* Convert the hash value to a value in the port range that we want
* for dynamic UDP ports
*/
port_range = VR_MUDP_PORT_RANGE_END - VR_MUDP_PORT_RANGE_START;
fmd->fmd_udp_src_port = (uint16_t) (((uint64_t) hashval * port_range) >> 32);
if (fmd->fmd_udp_src_port > port_range) {
/*
* Shouldn't happen...
*/
fmd->fmd_udp_src_port = 0;
}
fmd->fmd_udp_src_port += VR_MUDP_PORT_RANGE_START;
}
label = fmd->fmd_label;
for (i = 0; i < nh->nh_component_cnt; i++) {
clone_size = 0;
dir_nh = nh->nh_component_nh[i].cnh;
/* We need to copy back the original label from Bridge lookaup
* as previous iteration would have manipulated that
*/
vr_forwarding_md_set_label(fmd, label, VR_LABEL_TYPE_UNKNOWN);
fmd->fmd_dvrf = pkt_vrf;
/* If direct nexthop is not valid, dont process it */
if ((!dir_nh) || !(dir_nh->nh_flags & NH_FLAG_VALID) ||
(dir_nh->nh_type != NH_COMPOSITE))
continue;
if (dir_nh->nh_flags & NH_FLAG_COMPOSITE_ENCAP) {
if (!flood_to_vms)
continue;
if (!(new_pkt = nh_mcast_clone(pkt, 0))) {