/
vr_flow.c
1546 lines (1255 loc) · 40.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_interface.h"
#include "vr_sandesh.h"
#include "vr_message.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;
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 *,
uint8_t, 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));;
fe->fe_key.key_len = 0;
fe->fe_type = VP_TYPE_NULL;
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;
fe->fe_udp_src_port = 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 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, uint8_t type,
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, key->key_len, 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) {
fe->fe_type = type;
fe->fe_key.key_len = key->key_len;
memcpy(&fe->fe_key, key, key->key_len);
}
}
return fe;
}
static inline struct vr_flow_entry *
vr_flow_table_lookup(struct vr_flow *key, uint16_t type,
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) &&
(flow_e->fe_type == type)) {
if (!memcmp(&flow_e->fe_key, key, key->key_len)) {
*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,
uint8_t type, unsigned int *fe_index)
{
unsigned int hash;
struct vr_flow_entry *flow_e;
hash = vr_hash(key, key->key_len, 0);
/* first look in the regular flow table */
flow_e = vr_flow_table_lookup(key, type, 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, type, 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 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
*/
if (pkt->vp_nh &&
(pkt->vp_nh->nh_type == NH_VRF_TRANSLATE) &&
(pkt->vp_nh->nh_flags & NH_FLAG_VNID))
pnode->pl_flags |= PN_FLAG_LABEL_IS_VNID;
pkt->vp_nh = NULL;
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;
if (fmd->fmd_to_me)
pnode->pl_flags |= PN_FLAG_TO_ME;
}
__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;
}
static flow_result_t
vr_flow_nat(struct vr_flow_entry *fe,
struct vr_packet *pkt, struct vr_forwarding_md *fmd)
{
if (pkt->vp_type == VP_TYPE_IP)
return vr_inet_flow_nat(fe, pkt, fmd);
vr_pfree(pkt, VP_DROP_FLOW_ACTION_INVALID);
return FLOW_CONSUMED;
}
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 flow_result_t
vr_flow_action(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int valid_src;
flow_result_t result;
struct vr_forwarding_md mirror_fmd;
struct vr_nexthop *src_nh;
struct vr_packet *pkt_clone;
fmd->fmd_dvrf = 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 FLOW_CONSUMED;
}
if (src_nh->nh_validate_src) {
valid_src = src_nh->nh_validate_src(pkt, src_nh, fmd, NULL);
if (valid_src == NH_SOURCE_INVALID) {
vr_pfree(pkt, VP_DROP_INVALID_SOURCE);
return FLOW_CONSUMED;
}
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, fmd->fmd_dvrf,
AGENT_TRAP_ECMP_RESOLVE, &fmd->fmd_flow_index);
}
}
}
}
if (fe->fe_flags & VR_FLOW_FLAG_VRFT) {
if (fmd->fmd_dvrf != fe->fe_dvrf) {
fmd->fmd_dvrf = fe->fe_dvrf;
fmd->fmd_to_me = 1;
}
}
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);
result = FLOW_CONSUMED;
break;
case VR_FLOW_ACTION_FORWARD:
result = FLOW_FORWARD;
break;
case VR_FLOW_ACTION_NAT:
result = vr_flow_nat(fe, pkt, fmd);
break;
default:
vr_pfree(pkt, VP_DROP_FLOW_ACTION_INVALID);
result = FLOW_CONSUMED;
break;
}
return result;
}
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;
if (fe->fe_type == VP_TYPE_IP)
ta.vfta_nh_index = fe->fe_key.flow4_nh_id;
break;
}
return vr_trap(npkt, fe->fe_vrf, trap_reason, &ta);
}
static flow_result_t
vr_do_flow_action(struct vrouter *router, struct vr_flow_entry *fe,
unsigned int index, struct vr_packet *pkt,
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, index, fmd);
return FLOW_HELD;
}
return vr_flow_action(router, fe, index, pkt, 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;
}
flow_result_t
vr_flow_lookup(struct vrouter *router, struct vr_flow *key,
struct vr_packet *pkt, 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, pkt->vp_type, &fe_index);
if (!flow_e) {
if (pkt->vp_nh &&
(pkt->vp_nh->nh_flags & NH_FLAG_RELAXED_POLICY))
return FLOW_FORWARD;
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 FLOW_CONSUMED;
}
flow_e = vr_find_free_entry(router, key, pkt->vp_type,
true, &fe_index);
if (!flow_e) {
vr_pfree(pkt, VP_DROP_FLOW_TABLE_FULL);
return FLOW_CONSUMED;
}
flow_e->fe_vrf = fmd->fmd_dvrf;
/* mark as hold */
vr_flow_entry_set_hold(router, flow_e);
}
return vr_do_flow_action(router, flow_e, fe_index, pkt, fmd);
}
static bool
__vr_flow_forward(flow_result_t result, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
bool forward = false;
switch (result) {
case FLOW_FORWARD:
forward = true;
break;
case FLOW_TRAP:
vr_trap(pkt, fmd->fmd_dvrf, AGENT_TRAP_L3_PROTOCOLS, NULL);
break;
case FLOW_HELD:
case FLOW_CONSUMED:
break;
case FLOW_DROP:
default:
vr_pfree(pkt, VP_DROP_FLOW_UNUSABLE);
break;
}
return forward;
}
bool
vr_flow_forward(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
flow_result_t result;
/* Flow processig is only for untagged unicast IP packets */
if ((pkt->vp_type == VP_TYPE_IP) && (!(pkt->vp_flags & VP_FLAG_MULTICAST))
&& ((fmd->fmd_vlan == VLAN_ID_INVALID) || vif_is_service(pkt->vp_if)))
result = vr_inet_flow_lookup(router, pkt, fmd);
else
result = FLOW_FORWARD;
return __vr_flow_forward(result, pkt, fmd);
}
static void
vr_flush_flow_queue(struct vrouter *router, struct vr_flow_entry *fe,
struct vr_forwarding_md *fmd, struct vr_flow_queue *vfq)
{
unsigned int i;
bool forward;
struct vr_interface *vif;
struct vr_packet *pkt;
struct vr_packet_node *pnode;
flow_result_t result;
for (i = 0; i < VR_MAX_FLOW_QUEUE_ENTRIES; i++) {
pnode = &vfq->vfq_pnodes[i];
if (fmd) {
memset(fmd, 0, sizeof(*fmd));
fmd->fmd_outer_src_ip = pnode->pl_outer_src_ip;
fmd->fmd_label = pnode->pl_label;
if (pnode->pl_flags & PN_FLAG_TO_ME)
fmd->fmd_to_me = 1;
}
pkt = pnode->pl_packet;
if (!pkt)
continue;
pnode->pl_packet = NULL;
/*
* this is only a security check and not a catch all check. one note
* of caution. please do not access pkt->vp_if till the if block is
* succesfully bypassed
*/
vif = __vrouter_get_interface(router, pnode->pl_vif_idx);
if (!vif || (pkt->vp_if != vif)) {
vr_pfree(pkt, VP_DROP_INVALID_IF);
continue;
}
if (!pkt->vp_nh) {
if (vif_is_fabric(pkt->vp_if) && fmd &&
(fmd->fmd_label >= 0)) {
if (!(pnode->pl_flags & PN_FLAG_LABEL_IS_VNID))
pkt->vp_nh = __vrouter_get_label(router, fmd->fmd_label);
}
}
result = vr_flow_action(router, fe, vfq->vfq_index, pkt, fmd);
forward = __vr_flow_forward(result, pkt, fmd);
if (forward)
vr_reinject_packet(pkt, fmd);
pnode->pl_packet = NULL;
}
return;
}
static void
vr_flush_entry(struct vrouter *router, struct vr_flow_entry *fe,
struct vr_flow_md *flmd, struct vr_forwarding_md *fmd)
{
struct vr_flow_queue *vfq;
if (fe->fe_action == VR_FLOW_ACTION_HOLD)
return;
vfq = fe->fe_hold_list;
if (!vfq)
return;
fe->fe_hold_list = NULL;
vr_flush_flow_queue(router, fe, fmd, vfq);
vr_flow_queue_free_defer(flmd, vfq);
return;
}
static void
vr_flow_flush(void *arg)
{
struct vrouter *router;
struct vr_flow_entry *fe;
struct vr_forwarding_md fmd;
struct vr_flow_md *flmd =
(struct vr_flow_md *)arg;
router = flmd->flmd_router;
if (!router)
goto exit_flush;
fe = vr_get_flow_entry(router, flmd->flmd_index);
if (!fe)
goto exit_flush;
vr_init_forwarding_md(&fmd);
vr_flow_set_forwarding_md(router, fe, flmd->flmd_index, &fmd);
vr_flush_entry(router, fe, flmd, &fmd);
if (!(flmd->flmd_flags & VR_FLOW_FLAG_ACTIVE)) {
vr_reset_flow_entry(router, fe, flmd->flmd_index);
}
exit_flush:
vr_free(flmd);
return;
}
static void
vr_flow_set_mirror(struct vrouter *router, vr_flow_req *req,
struct vr_flow_entry *fe)
{
struct vr_mirror_entry *mirror = NULL, *sec_mirror = NULL;
if (!(req->fr_flags & VR_FLOW_FLAG_MIRROR) &&
(fe->fe_flags & VR_FLOW_FLAG_MIRROR)) {
vr_flow_reset_mirror(router, fe, req->fr_index);
return;
}
if (!(req->fr_flags & VR_FLOW_FLAG_MIRROR))
return;
if (fe->fe_mirror_id != req->fr_mir_id) {
if (fe->fe_mirror_id < router->vr_max_mirror_indices) {
vrouter_put_mirror(router, fe->fe_mirror_id);
fe->fe_mirror_id = router->vr_max_mirror_indices;
}
if ((unsigned int)req->fr_mir_id < router->vr_max_mirror_indices) {
mirror = vrouter_get_mirror(req->fr_rid, req->fr_mir_id);
if (mirror)
fe->fe_mirror_id = req->fr_mir_id;
/* when we reached this point, we had already done all the
* sanity checks we could do. failing here will add only
* complexity to code here. so !mirror case, we will not
* handle
*/
}
}
if (fe->fe_sec_mirror_id != req->fr_sec_mir_id) {
if (fe->fe_sec_mirror_id < router->vr_max_mirror_indices) {
vrouter_put_mirror(router, fe->fe_sec_mirror_id);
fe->fe_sec_mirror_id = router->vr_max_mirror_indices;
}
if ((unsigned int)req->fr_sec_mir_id < router->vr_max_mirror_indices) {
sec_mirror = vrouter_get_mirror(req->fr_rid, req->fr_sec_mir_id);
if (sec_mirror)
fe->fe_sec_mirror_id = req->fr_sec_mir_id;
}
}
if (req->fr_pcap_meta_data_size && req->fr_pcap_meta_data)
vr_mirror_meta_entry_set(router, req->fr_index,
req->fr_mir_sip, req->fr_mir_sport,
req->fr_pcap_meta_data, req->fr_pcap_meta_data_size,
req->fr_mir_vrf);
return;
}
static struct vr_flow_entry *
vr_add_flow(unsigned int rid, struct vr_flow *key, uint8_t type,
bool need_hold_queue, unsigned int *fe_index)
{
struct vr_flow_entry *flow_e;
struct vrouter *router = vrouter_get(rid);
flow_e = vr_find_flow(router, key, type, fe_index);
if (flow_e) {
/* a race between agent and dp. allow agent to handle this error */
return NULL;
} else {
flow_e = vr_find_free_entry(router, key, type,
need_hold_queue, fe_index);
}
return flow_e;
}
static struct vr_flow_entry *
vr_add_flow_req(vr_flow_req *req, unsigned int *fe_index)
{
uint8_t type;
bool need_hold_queue = false;
struct vr_flow key;
struct vr_flow_entry *fe;
vr_inet_fill_flow(&key, req->fr_flow_nh_id, req->fr_flow_sip,
req->fr_flow_dip, req->fr_flow_proto,
req->fr_flow_sport, req->fr_flow_dport);
type = VP_TYPE_IP;