/
vr_flow.c
1966 lines (1618 loc) · 54.7 KB
/
vr_flow.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_flow.c -- flow handling
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <vr_os.h>
#include <vr_types.h>
#include <vrouter.h>
#include <vr_packet.h>
#include <vr_flow.h>
#include <vr_mirror.h>
#include "vr_sandesh.h"
#include "vr_message.h"
#include "vr_mcast.h"
#include "vr_btable.h"
#include "vr_fragment.h"
#include "vr_datapath.h"
#include "vr_hash.h"
#include "vr_ip_mtrie.h"
#define VR_NUM_FLOW_TABLES 1
#define VR_DEF_FLOW_ENTRIES (512 * 1024)
#define VR_FLOW_TABLE_SIZE (vr_flow_entries * \
sizeof(struct vr_flow_entry))
#define VR_NUM_OFLOW_TABLES 1
#define VR_DEF_OFLOW_ENTRIES (8 * 1024)
#define VR_OFLOW_TABLE_SIZE (vr_oflow_entries *\
sizeof(struct vr_flow_entry))
#define VR_FLOW_ENTRIES_PER_BUCKET 4U
#define VR_MAX_FLOW_TABLE_HOLD_COUNT \
4096
unsigned int vr_flow_entries = VR_DEF_FLOW_ENTRIES;
unsigned int vr_oflow_entries = VR_DEF_OFLOW_ENTRIES;
unsigned int vr_flow_hold_limit = 1;
#if defined(__linux__) && defined(__KERNEL__)
extern unsigned short vr_flow_major;
#endif
uint32_t vr_hashrnd = 0;
int hashrnd_inited = 0;
extern int vr_ip_input(struct vrouter *, unsigned short,
struct vr_packet *, struct vr_forwarding_md *);
extern void vr_ip_update_csum(struct vr_packet *, unsigned int,
unsigned int);
extern uint16_t vr_icmp6_checksum(void * buffer, int bytes);
static void vr_flush_entry(struct vrouter *, struct vr_flow_entry *,
struct vr_flow_md *, struct vr_forwarding_md *);
static void vr_flush_flow_queue(struct vrouter *, struct vr_flow_entry *,
struct vr_forwarding_md *, struct vr_flow_queue *);
struct vr_flow_entry *vr_find_flow(struct vrouter *, struct vr_flow_key *,
unsigned int *);
unsigned int vr_trap_flow(struct vrouter *, struct vr_flow_entry *,
struct vr_packet *, unsigned int);
void get_random_bytes(void *buf, int nbytes);
#ifdef __FreeBSD__
uint32_t
jhash(void *key, uint32_t length, uint32_t interval);
#endif
#ifdef __FreeBSD__
uint32_t
jhash(void *key, uint32_t length, uint32_t interval)
{
uint32_t ret = 0;
int i;
unsigned char *data = (unsigned char *)key;
for (i = 0; i < length; i ++)
ret += data[i];
return ret;
}
#endif
bool
vr_valid_link_local_port(struct vrouter *router, int family,
int proto, int port)
{
unsigned char data;
unsigned int tmp;
if (!router->vr_link_local_ports)
return false;
if ((family != AF_INET) ||
((proto != VR_IP_PROTO_TCP) && (proto != VR_IP_PROTO_UDP)))
return false;
if ((port < VR_DYNAMIC_PORT_START) || (port > VR_DYNAMIC_PORT_END))
return false;
tmp = port - VR_DYNAMIC_PORT_START;
if (proto == VR_IP_PROTO_UDP)
tmp += (router->vr_link_local_ports_size * 8 / 2);
data = router->vr_link_local_ports[(tmp /8)];
if (data & (1 << (tmp % 8)))
return true;
return false;
}
static void
vr_clear_link_local_port(struct vrouter *router, int family,
int proto, int port)
{
unsigned char *data;
unsigned int tmp;
if (!router->vr_link_local_ports)
return;
if ((family != AF_INET) ||
((proto != VR_IP_PROTO_TCP) && (proto != VR_IP_PROTO_UDP)))
return;
if ((port < VR_DYNAMIC_PORT_START) || (port > VR_DYNAMIC_PORT_END))
return;
tmp = port - VR_DYNAMIC_PORT_START;
if (proto == VR_IP_PROTO_UDP)
tmp += (router->vr_link_local_ports_size * 8 / 2);
data = &router->vr_link_local_ports[(tmp /8)];
*data &= (~(1 << (tmp % 8)));
}
static void
vr_set_link_local_port(struct vrouter *router, int family,
int proto, int port)
{
unsigned char *data;
unsigned int tmp;
if (!router->vr_link_local_ports)
return;
if ((family != AF_INET) ||
((proto != VR_IP_PROTO_TCP) && (proto != VR_IP_PROTO_UDP)))
return;
if ((port < VR_DYNAMIC_PORT_START) || (port > VR_DYNAMIC_PORT_END))
return;
tmp = port - VR_DYNAMIC_PORT_START;
if (proto == VR_IP_PROTO_UDP)
tmp += (router->vr_link_local_ports_size * 8 / 2);
data = &router->vr_link_local_ports[tmp /8];
*data |= (1 << (tmp % 8));
}
static void
vr_flow_reset_mirror(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index)
{
if (fe->fe_flags & VR_FLOW_FLAG_MIRROR) {
vrouter_put_mirror(router, fe->fe_mirror_id);
fe->fe_mirror_id = VR_MAX_MIRROR_INDICES;
vrouter_put_mirror(router, fe->fe_sec_mirror_id);
fe->fe_sec_mirror_id = VR_MAX_MIRROR_INDICES;
vr_mirror_meta_entry_del(router, index);
}
fe->fe_flags &= ~VR_FLOW_FLAG_MIRROR;
fe->fe_mirror_id = VR_MAX_MIRROR_INDICES;
fe->fe_sec_mirror_id = VR_MAX_MIRROR_INDICES;
return;
}
static void
vr_init_flow_entry(struct vr_flow_entry *fe)
{
fe->fe_rflow = -1;
fe->fe_mirror_id = VR_MAX_MIRROR_INDICES;
fe->fe_sec_mirror_id = VR_MAX_MIRROR_INDICES;
fe->fe_ecmp_nh_index = -1;
return;
}
static void
vr_reset_flow_entry(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index)
{
memset(&fe->fe_stats, 0, sizeof(fe->fe_stats));
memset(&fe->fe_hold_list, 0, sizeof(fe->fe_hold_list));;
memset(&fe->fe_key, 0, sizeof(fe->fe_key));
vr_flow_reset_mirror(router, fe, index);
fe->fe_ecmp_nh_index = -1;
fe->fe_src_nh_index = NH_DISCARD_ID;
fe->fe_rflow = -1;
fe->fe_action = VR_FLOW_ACTION_DROP;
fe->fe_flags = 0;
return;
}
static inline bool
vr_set_flow_active(struct vr_flow_entry *fe)
{
return __sync_bool_compare_and_swap(&fe->fe_flags,
fe->fe_flags & ~VR_FLOW_FLAG_ACTIVE, VR_FLOW_FLAG_ACTIVE);
}
static inline struct vr_flow_entry *
vr_flow_table_entry_get(struct vrouter *router, unsigned int i)
{
return (struct vr_flow_entry *)vr_btable_get(router->vr_flow_table, i);
}
static inline struct vr_flow_entry *
vr_oflow_table_entry_get(struct vrouter *router, unsigned int i)
{
return (struct vr_flow_entry *)vr_btable_get(router->vr_oflow_table, i);
}
unsigned int
vr_flow_table_size(struct vrouter *router)
{
return vr_btable_size(router->vr_flow_table);
}
unsigned int
vr_oflow_table_size(struct vrouter *router)
{
return vr_btable_size(router->vr_oflow_table);
}
/*
* this is used by the mmap code. mmap sees the whole flow table
* (including the overflow table) as one large table. so, given
* an offset into that large memory, we should return the correct
* virtual address
*/
void *
vr_flow_get_va(struct vrouter *router, uint64_t offset)
{
struct vr_btable *table = router->vr_flow_table;
unsigned int size = vr_flow_table_size(router);
if (offset >= vr_flow_table_size(router)) {
table = router->vr_oflow_table;
offset -= size;
}
return vr_btable_get_address(table, offset);
}
struct vr_flow_entry *
vr_get_flow_entry(struct vrouter *router, int index)
{
struct vr_btable *table;
if (index < 0)
return NULL;
if ((unsigned int)index < vr_flow_entries)
table = router->vr_flow_table;
else {
table = router->vr_oflow_table;
index -= vr_flow_entries;
if ((unsigned int)index >= vr_oflow_entries)
return NULL;
}
return (struct vr_flow_entry *)vr_btable_get(table, index);
}
static inline void
vr_get_flow_key(struct vr_flow_key *key, uint16_t vlan, struct vr_packet *pkt,
unsigned int sip, unsigned int dip, unsigned char proto,
unsigned short sport, unsigned short dport)
{
unsigned short nh_id;
/* copy both source and destinations */
key->key_src_ip = sip;
key->key_dest_ip = dip;
key->key_proto = proto;
key->key_zero = 0;
if (vif_is_fabric(pkt->vp_if) && pkt->vp_nh) {
nh_id = pkt->vp_nh->nh_id;
} else if (vif_is_service(pkt->vp_if)) {
nh_id = vif_vrf_table_get_nh(pkt->vp_if, vlan);
} else {
nh_id = pkt->vp_if->vif_nh_id;
}
key->key_nh_id = nh_id;
switch (proto) {
case VR_IP_PROTO_TCP:
case VR_IP_PROTO_UDP:
case VR_IP_PROTO_ICMP:
case VR_IP_PROTO_ICMP6:
key->key_src_port = sport;
key->key_dst_port = dport;
break;
default:
key->key_src_port = key->key_dst_port = 0;
break;
}
return;
}
static void
vr_flow_queue_free(struct vrouter *router, void *arg)
{
struct vr_forwarding_md fmd;
struct vr_defer_data *defer;
struct vr_flow_entry *fe;
struct vr_flow_queue *vfq;
defer = (struct vr_defer_data *)arg;
if (!defer)
return;
vr_init_forwarding_md(&fmd);
vfq = (struct vr_flow_queue *)defer->vdd_data;
fe = vr_get_flow_entry(router, vfq->vfq_index);
vr_flush_flow_queue(router, fe, &fmd, vfq);
vr_free(vfq);
return;
}
static void
vr_flow_queue_free_defer(struct vr_flow_md *flmd, struct vr_flow_queue *vfq)
{
struct vr_defer_data *vdd = flmd->flmd_defer_data;
if (!vdd) {
vr_free(vfq);
return;
}
vdd->vdd_data = (void *)vfq;
vr_defer(flmd->flmd_router, vr_flow_queue_free, (void *)vdd);
return;
}
static struct vr_flow_entry *
vr_find_free_entry(struct vrouter *router, struct vr_flow_key *key,
bool need_hold, unsigned int *fe_index)
{
unsigned int i, index, hash;
struct vr_flow_entry *tmp_fe, *fe = NULL;
*fe_index = 0;
hash = vr_hash(key, sizeof(*key), 0);
index = (hash % vr_flow_entries) & ~(VR_FLOW_ENTRIES_PER_BUCKET - 1);
for (i = 0; i < VR_FLOW_ENTRIES_PER_BUCKET; i++) {
tmp_fe = vr_flow_table_entry_get(router, index);
if (tmp_fe && !(tmp_fe->fe_flags & VR_FLOW_FLAG_ACTIVE)) {
if (vr_set_flow_active(tmp_fe)) {
vr_init_flow_entry(tmp_fe);
fe = tmp_fe;
break;
}
}
index++;
}
if (!fe) {
index = hash % vr_oflow_entries;
for (i = 0; i < vr_oflow_entries; i++) {
tmp_fe = vr_oflow_table_entry_get(router, index);
if (tmp_fe && !(tmp_fe->fe_flags & VR_FLOW_FLAG_ACTIVE)) {
if (vr_set_flow_active(tmp_fe)) {
vr_init_flow_entry(tmp_fe);
fe = tmp_fe;
break;
}
}
index = (index + 1) % vr_oflow_entries;
}
if (fe)
*fe_index += vr_flow_entries;
}
if (fe) {
*fe_index += index;
if (need_hold) {
fe->fe_hold_list = vr_zalloc(sizeof(struct vr_flow_queue));
if (!fe->fe_hold_list) {
vr_reset_flow_entry(router, fe, *fe_index);
fe = NULL;
} else {
fe->fe_hold_list->vfq_index = *fe_index;
}
}
if (fe)
memcpy(&fe->fe_key, key, sizeof(*key));
}
return fe;
}
static inline struct vr_flow_entry *
vr_flow_table_lookup(struct vr_flow_key *key, struct vr_btable *table,
unsigned int table_size, unsigned int bucket_size,
unsigned int hash, unsigned int *fe_index)
{
unsigned int i;
struct vr_flow_entry *flow_e;
hash %= table_size;
if (!bucket_size) {
bucket_size = table_size;
} else {
hash &= ~(bucket_size - 1);
}
for (i = 0; i < bucket_size; i++) {
flow_e = (struct vr_flow_entry *)vr_btable_get(table,
(hash + i) % table_size);
if (flow_e && flow_e->fe_flags & VR_FLOW_FLAG_ACTIVE) {
if (!memcmp(&flow_e->fe_key, key, sizeof(*key))) {
*fe_index = (hash + i) % table_size;
return flow_e;
}
}
}
return NULL;
}
struct vr_flow_entry *
vr_find_flow(struct vrouter *router, struct vr_flow_key *key,
unsigned int *fe_index)
{
unsigned int hash;
struct vr_flow_entry *flow_e;
hash = vr_hash(key, sizeof(*key), 0);
/* first look in the regular flow table */
flow_e = vr_flow_table_lookup(key, router->vr_flow_table, vr_flow_entries,
VR_FLOW_ENTRIES_PER_BUCKET, hash, fe_index);
/* if not in the regular flow table, lookup in the overflow flow table */
if (!flow_e) {
flow_e = vr_flow_table_lookup(key, router->vr_oflow_table, vr_oflow_entries,
0, hash, fe_index);
*fe_index += vr_flow_entries;
}
return flow_e;
}
static int
vr_enqueue_flow(struct vrouter *router, struct vr_flow_entry *fe,
struct vr_packet *pkt, unsigned short proto,
unsigned int index, struct vr_forwarding_md *fmd)
{
unsigned int i;
unsigned short drop_reason = 0;
struct vr_flow_queue *vfq = fe->fe_hold_list;
struct vr_packet_node *pnode;
if (!vfq) {
drop_reason = VP_DROP_FLOW_UNUSABLE;
goto drop;
}
i = __sync_fetch_and_add(&vfq->vfq_entries, 1);
if (i >= VR_MAX_FLOW_QUEUE_ENTRIES) {
drop_reason = VP_DROP_FLOW_QUEUE_LIMIT_EXCEEDED;
goto drop;
}
pnode = &vfq->vfq_pnodes[i];
/*
* we cannot cache nexthop here. to cache, we need to hold reference
* to the nexthop. to hold a reference, we will have to hold a lock,
* which we cannot. the only known case of misbehavior if we do not
* cache is ECMP. when the packet comes from the fabric, the nexthop
* actually points to a local composite, whereas a route lookup actually
* returns a different nexthop, in which case the ecmp index will return
* a bad nexthop. to avoid that, we will cache the label, and reuse it
*/
pkt->vp_nh = NULL;
pnode->pl_proto = proto;
pnode->pl_vif_idx = pkt->vp_if->vif_idx;
if (fmd) {
pnode->pl_outer_src_ip = fmd->fmd_outer_src_ip;
pnode->pl_label = fmd->fmd_label;
}
__sync_synchronize();
pnode->pl_packet = pkt;
if (!i)
vr_trap_flow(router, fe, pkt, index);
return 0;
drop:
vr_pfree(pkt, drop_reason);
return 0;
}
int
vr_flow_forward(unsigned short vrf, struct vr_packet *pkt,
unsigned short proto, struct vr_forwarding_md *fmd)
{
struct vr_interface *vif = pkt->vp_if;
struct vrouter *router = vif->vif_router;
if ((proto != VR_ETH_PROTO_IP) && (proto != VR_ETH_PROTO_IP6)) {
vr_pfree(pkt, VP_DROP_FLOW_INVALID_PROTOCOL);
return 0;
}
if (pkt->vp_nh)
return nh_output(vrf, pkt, pkt->vp_nh, fmd);
pkt_set_data(pkt, pkt->vp_network_h);
return vr_ip_input(router, vrf, pkt, fmd);
}
static int
vr_flow_nat(unsigned short vrf, struct vr_flow_entry *fe, struct vr_packet *pkt,
unsigned short proto, struct vr_forwarding_md *fmd)
{
unsigned int ip_inc, inc = 0;
unsigned short *t_sport, *t_dport;
struct vrouter *router = pkt->vp_if->vif_router;
struct vr_flow_entry *rfe;
struct vr_ip *ip, *icmp_pl_ip;
struct vr_icmp *icmph;
bool hdr_update = false;
if (fe->fe_rflow < 0)
goto drop;
ip = (struct vr_ip *)pkt_data(pkt);
if (vr_ip_is_ip6(ip)) {
/* No NAT support for IPv6 yet */
vr_pfree(pkt, VP_DROP_FLOW_ACTION_INVALID);
return 0;
}
rfe = vr_get_flow_entry(router, fe->fe_rflow);
if (!rfe)
goto drop;
if (ip->ip_proto == VR_IP_PROTO_ICMP) {
icmph = (struct vr_icmp *)((unsigned char *)ip + (ip->ip_hl * 4));
if (vr_icmp_error(icmph)) {
icmp_pl_ip = (struct vr_ip *)(icmph + 1);
if (fe->fe_flags & VR_FLOW_FLAG_SNAT) {
icmp_pl_ip->ip_daddr = rfe->fe_key.key_dest_ip;
hdr_update = true;
}
if (fe->fe_flags & VR_FLOW_FLAG_DNAT) {
icmp_pl_ip->ip_saddr = rfe->fe_key.key_src_ip;
hdr_update = true;
}
if (hdr_update)
icmp_pl_ip->ip_csum = vr_ip_csum(icmp_pl_ip);
t_sport = (unsigned short *)((unsigned char *)icmp_pl_ip +
(icmp_pl_ip->ip_hl * 4));
t_dport = t_sport + 1;
if (fe->fe_flags & VR_FLOW_FLAG_SPAT)
*t_dport = rfe->fe_key.key_dst_port;
if (fe->fe_flags & VR_FLOW_FLAG_DPAT)
*t_sport = rfe->fe_key.key_src_port;
}
}
if ((fe->fe_flags & VR_FLOW_FLAG_SNAT) &&
(ip->ip_saddr == fe->fe_key.key_src_ip)) {
vr_incremental_diff(ip->ip_saddr, rfe->fe_key.key_dest_ip, &inc);
ip->ip_saddr = rfe->fe_key.key_dest_ip;
}
if (fe->fe_flags & VR_FLOW_FLAG_DNAT) {
vr_incremental_diff(ip->ip_daddr, rfe->fe_key.key_src_ip, &inc);
ip->ip_daddr = rfe->fe_key.key_src_ip;
}
ip_inc = inc;
if (vr_ip_transport_header_valid(ip)) {
t_sport = (unsigned short *)((unsigned char *)ip +
(ip->ip_hl * 4));
t_dport = t_sport + 1;
if (fe->fe_flags & VR_FLOW_FLAG_SPAT) {
vr_incremental_diff(*t_sport, rfe->fe_key.key_dst_port, &inc);
*t_sport = rfe->fe_key.key_dst_port;
}
if (fe->fe_flags & VR_FLOW_FLAG_DPAT) {
vr_incremental_diff(*t_dport, rfe->fe_key.key_src_port, &inc);
*t_dport = rfe->fe_key.key_src_port;
}
}
#ifdef VROUTER_CONFIG_DIAG
if (ip->ip_csum != VR_DIAG_IP_CSUM)
vr_ip_update_csum(pkt, ip_inc, inc);
#else
vr_ip_update_csum(pkt, ip_inc, inc);
#endif
/*
* If VRF is translated lets chose a new nexthop
*/
if ((fe->fe_flags & VR_FLOW_FLAG_VRFT) &&
pkt->vp_nh && pkt->vp_nh->nh_vrf != vrf)
pkt->vp_nh = NULL;
return vr_flow_forward(vrf, pkt, proto, fmd);
drop:
vr_pfree(pkt, VP_DROP_FLOW_NAT_NO_RFLOW);
return 0;
}
static void
vr_flow_set_forwarding_md(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index, struct vr_forwarding_md *md)
{
struct vr_flow_entry *rfe;
md->fmd_flow_index = index;
md->fmd_ecmp_nh_index = fe->fe_ecmp_nh_index;
md->fmd_udp_src_port = fe->fe_udp_src_port;
if (fe->fe_flags & VR_RFLOW_VALID) {
rfe = vr_get_flow_entry(router, fe->fe_rflow);
if (rfe)
md->fmd_ecmp_src_nh_index = rfe->fe_ecmp_nh_index;
}
return;
}
static int
vr_flow_action(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index, struct vr_packet *pkt,
unsigned short proto, struct vr_forwarding_md *fmd)
{
int ret = 0, valid_src;
unsigned short vrf;
struct vr_forwarding_md mirror_fmd;
struct vr_nexthop *src_nh;
struct vr_packet *pkt_clone;
vrf = fe->fe_vrf;
/*
* for now, we will not use dvrf if VRFT is set, because the RPF
* check needs to happen in the source vrf
*/
vr_flow_set_forwarding_md(router, fe, index, fmd);
src_nh = __vrouter_get_nexthop(router, fe->fe_src_nh_index);
if (!src_nh) {
vr_pfree(pkt, VP_DROP_INVALID_NH);
return 0;
}
if (src_nh->nh_validate_src) {
valid_src = src_nh->nh_validate_src(vrf, pkt, src_nh, fmd, NULL);
if (valid_src == NH_SOURCE_INVALID) {
vr_pfree(pkt, VP_DROP_INVALID_SOURCE);
return 0;
}
if (valid_src == NH_SOURCE_MISMATCH) {
pkt_clone = vr_pclone(pkt);
if (pkt_clone) {
vr_preset(pkt_clone);
if (vr_pcow(pkt_clone, sizeof(struct vr_eth) +
sizeof(struct agent_hdr))) {
vr_pfree(pkt_clone, VP_DROP_PCOW_FAIL);
} else {
vr_trap(pkt_clone, vrf,
AGENT_TRAP_ECMP_RESOLVE, &fmd->fmd_flow_index);
}
}
}
}
if (fe->fe_flags & VR_FLOW_FLAG_VRFT)
vrf = fe->fe_dvrf;
if (fe->fe_flags & VR_FLOW_FLAG_MIRROR) {
if (fe->fe_mirror_id < VR_MAX_MIRROR_INDICES) {
mirror_fmd = *fmd;
mirror_fmd.fmd_ecmp_nh_index = -1;
vr_mirror(router, fe->fe_mirror_id, pkt, &mirror_fmd);
}
if (fe->fe_sec_mirror_id < VR_MAX_MIRROR_INDICES) {
mirror_fmd = *fmd;
mirror_fmd.fmd_ecmp_nh_index = -1;
vr_mirror(router, fe->fe_sec_mirror_id, pkt, &mirror_fmd);
}
}
switch (fe->fe_action) {
case VR_FLOW_ACTION_DROP:
vr_pfree(pkt, VP_DROP_FLOW_ACTION_DROP);
break;
case VR_FLOW_ACTION_FORWARD:
ret = vr_flow_forward(vrf, pkt, proto, fmd);
break;
case VR_FLOW_ACTION_NAT:
ret = vr_flow_nat(vrf, fe, pkt, proto, fmd);
break;
default:
vr_pfree(pkt, VP_DROP_FLOW_ACTION_INVALID);
break;
}
return ret;
}
unsigned int
vr_trap_flow(struct vrouter *router, struct vr_flow_entry *fe,
struct vr_packet *pkt, unsigned int index)
{
unsigned int trap_reason;
struct vr_packet *npkt;
struct vr_flow_trap_arg ta;
npkt = vr_pclone(pkt);
if (!npkt)
return -ENOMEM;
vr_preset(npkt);
switch (fe->fe_flags & VR_FLOW_FLAG_TRAP_MASK) {
default:
trap_reason = AGENT_TRAP_FLOW_MISS;
ta.vfta_index = index;
ta.vfta_nh_index = fe->fe_key.key_nh_id;
break;
}
return vr_trap(npkt, fe->fe_vrf, trap_reason, &ta);
}
static int
vr_do_flow_action(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index, struct vr_packet *pkt,
unsigned short proto, struct vr_forwarding_md *fmd)
{
uint32_t new_stats;
new_stats = __sync_add_and_fetch(&fe->fe_stats.flow_bytes, pkt_len(pkt));
if (new_stats < pkt_len(pkt))
fe->fe_stats.flow_bytes_oflow++;
new_stats = __sync_add_and_fetch(&fe->fe_stats.flow_packets, 1);
if (!new_stats)
fe->fe_stats.flow_packets_oflow++;
if (fe->fe_action == VR_FLOW_ACTION_HOLD) {
vr_enqueue_flow(router, fe, pkt, proto, index, fmd);
return 0;
}
return vr_flow_action(router, fe, index, pkt, proto, fmd);
}
static unsigned int
vr_flow_table_hold_count(struct vrouter *router)
{
unsigned int i, num_cpus;
uint64_t hcount = 0, act_count;
struct vr_flow_table_info *infop = router->vr_flow_table_info;
num_cpus = vr_num_cpus;
for (i = 0; i < num_cpus; i++)
hcount += infop->vfti_hold_count[i];
act_count = infop->vfti_action_count;
if (hcount >= act_count)
return hcount - act_count;
return 0;
}
static void
vr_flow_entry_set_hold(struct vrouter *router, struct vr_flow_entry *flow_e)
{
unsigned int cpu;
uint64_t act_count;
struct vr_flow_table_info *infop = router->vr_flow_table_info;
cpu = vr_get_cpu();
if (cpu >= vr_num_cpus) {
vr_printf("vrouter: Set HOLD failed (cpu %u num_cpus %u)\n",
cpu, vr_num_cpus);
return;
}
flow_e->fe_action = VR_FLOW_ACTION_HOLD;
if (infop->vfti_hold_count[cpu] + 1 < infop->vfti_hold_count[cpu]) {
(void)__sync_add_and_fetch(&infop->vfti_oflows, 1);
act_count = infop->vfti_action_count;
if (act_count > infop->vfti_hold_count[cpu]) {
(void)__sync_sub_and_fetch(&infop->vfti_action_count,
infop->vfti_hold_count[cpu]);
infop->vfti_hold_count[cpu] = 0;
} else {
infop->vfti_hold_count[cpu] -= act_count;
(void)__sync_sub_and_fetch(&infop->vfti_action_count,
act_count);
}
}
infop->vfti_hold_count[cpu]++;
return;
}
static int
vr_flow_lookup(struct vrouter *router, unsigned short vrf,
struct vr_flow_key *key, struct vr_packet *pkt, unsigned short proto,
struct vr_forwarding_md *fmd)
{
unsigned int fe_index;
struct vr_flow_entry *flow_e;
pkt->vp_flags |= VP_FLAG_FLOW_SET;
flow_e = vr_find_flow(router, key, &fe_index);
if (!flow_e) {
if (pkt->vp_nh &&
(pkt->vp_nh->nh_flags & NH_FLAG_RELAXED_POLICY))
return vr_flow_forward(vrf, pkt, proto, fmd);
if ((vr_flow_hold_limit) &&
(vr_flow_table_hold_count(router) >
VR_MAX_FLOW_TABLE_HOLD_COUNT)) {
vr_pfree(pkt, VP_DROP_FLOW_UNUSABLE);
return 0;
}
flow_e = vr_find_free_entry(router, key, true, &fe_index);
if (!flow_e) {
vr_pfree(pkt, VP_DROP_FLOW_TABLE_FULL);
return 0;
}
flow_e->fe_vrf = vrf;
/* mark as hold */
vr_flow_entry_set_hold(router, flow_e);
vr_do_flow_action(router, flow_e, fe_index, pkt, proto, fmd);
return 0;
}
return vr_do_flow_action(router, flow_e, fe_index, pkt, proto, fmd);
}
/*
* This inline function decides whether to trap the packet, or bypass
* flow table or not.
*/
inline unsigned int
vr_flow_parse(struct vrouter *router, struct vr_flow_key *key,
struct vr_packet *pkt, unsigned int *trap_res)
{
unsigned int proto_port;
/* without any data, the result has to be BYPASS, right? */
unsigned int res = VR_FLOW_BYPASS;
/*
* if the packet has already done one round of flow lookup, there
* is no point in doing it again, eh?
*/
if (pkt->vp_flags & VP_FLAG_FLOW_SET)
return res;
/*
* if the interface is policy enabled, or if somebody else (eg:nexthop)
* has requested for a policy lookup, packet has to go through a lookup
*/
if ((pkt->vp_if->vif_flags & VIF_FLAG_POLICY_ENABLED) ||
(pkt->vp_flags & VP_FLAG_FLOW_GET))
res = VR_FLOW_LOOKUP;
/*
* ..., but then there are some exceptions, as checked below.
* please note that these conditions also need to work when policy is
* really not enabled
*/
if (key) {
if (IS_BMCAST_IP(key->key_dest_ip)) {
/* no flow lookup for multicast or broadcast ip */
res = VR_FLOW_BYPASS;
pkt->vp_flags |= VP_FLAG_FLOW_SET;
/*
* dhcp packet handling:
*
* for now we handle dhcp requests from only VMs and that too only
* for VMs that are not in the fabric VRF. dhcp refresh packets will
* anyway hit the route entry and get trapped from there.
*/
if (vif_is_virtual(pkt->vp_if) && vif_dhcp_enabled(pkt->vp_if)) {
proto_port = (key->key_proto << VR_FLOW_PROTO_SHIFT) |
key->key_src_port;
if (proto_port == VR_UDP_DHCP_CPORT) {
res = VR_FLOW_TRAP;
pkt->vp_flags |= VP_FLAG_FLOW_SET;
if (trap_res)
*trap_res = AGENT_TRAP_L3_PROTOCOLS;
}
}
}
}
return res;
}
extern struct vr_nexthop *(*vr_inet_route_lookup)(unsigned int,
struct vr_route_req *, struct vr_packet *);
unsigned int
vr_flow_inet6_input(struct vrouter *router, unsigned short vrf,
struct vr_packet *pkt, unsigned short proto,
struct vr_forwarding_md *fmd)
{
struct vr_ip6 *ip6;
struct vr_eth *eth;
unsigned int trap_res = 0, tag_size = 0;
unsigned short *t_hdr, sport, dport, eth_off, *eth_proto;
struct vr_icmp *icmph;
unsigned char *icmp_opt_ptr;
int proxy = 0;
struct vr_route_req rt;
struct vr_nexthop *nh;
struct vr_interface *vif = pkt->vp_if;
uint32_t rt_prefix[4];
pkt->vp_type = VP_TYPE_IP6;
ip6 = (struct vr_ip6 *)pkt_network_header(pkt);
/* TODO: Handle options headers */
t_hdr = (unsigned short *)((char *)ip6 + sizeof(struct vr_ip6));
switch (ip6->ip6_nxt) {
case VR_IP_PROTO_ICMP6:
/* First word on ICMP and ICMPv6 are same */
icmph = (struct vr_icmp *)t_hdr;
switch (icmph->icmp_type) {
case VR_ICMP6_TYPE_ECHO_REQ:
case VR_ICMP6_TYPE_ECHO_REPLY:
/* ICMPv6 Echo format is same as ICMP */
sport = icmph->icmp_eid;
dport = VR_ICMP6_TYPE_ECHO_REPLY;
break;
case VR_ICMP6_TYPE_NEIGH_SOL: //Neighbor Solicit, respond with VRRP MAC
/* For L2-only networks, bridge the packets */