-
Notifications
You must be signed in to change notification settings - Fork 9
/
xdp_acl.c
1109 lines (937 loc) · 29.7 KB
/
xdp_acl.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
// +build ignore
#include <linux/types.h>
#include <linux/byteorder.h>
// #include <linux/rcupdate.h>
// <linux/rcupdate.h>
#include <stddef.h>
#include <linux/in.h> // proto type
#include <linux/if_ether.h> // l2
#include <linux/ip.h> // l3
#include <linux/tcp.h> // l4 struct tcphdr
#include <linux/udp.h> // l4 struct udphdr
#include <linux/icmp.h> // l4 备用
// #include <linux/if_packet.h>
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
// #define XDPACL_DEBUG
#define BITMAP_ARRAY_SIZE 160
/*
about BITMAP_ARRAY_SIZE
240 (15360) also ok; 288(18432; unroll 最大 36) is ok;
320 (20480) ok (需要全部展开)
必须是 8 的整数倍
*/
#ifndef IPPROTO_OSPF
#define IPPROTO_OSPF 89
#endif
// cacheline alignment
#ifndef L1_CACHE_BYTES
#define L1_CACHE_BYTES 64
#endif
#ifndef SMP_CACHE_BYTES
#define SMP_CACHE_BYTES L1_CACHE_BYTES
#endif
#ifndef ____cacheline_aligned
#define ____cacheline_aligned __attribute__((__aligned__(SMP_CACHE_BYTES)))
#endif
// likely optimization
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
// FIXED value
// #define ETH_HDR_SIZE 14
// #define IP_HDR_SIZE 20
// #define TCP_HDR_SIZE 20
// #define UDP_HDR_SIZE 8
// 支持的最多规则条数, 必须是 64 的整数倍; 比如: 64 * 16 == 1024
#define RULE_NUM_MAX_ENTRIES_V4 64 * BITMAP_ARRAY_SIZE
#define IP_MAX_ENTRIES_V4 RULE_NUM_MAX_ENTRIES_V4
// 支持的最多端口个数 1~65535; 65536 == 2^16
#define PORT_MAX_ENTRIES_V4 65536
// 支持的最多协议类型数 tcp udp icmp
#define PROTO_MAX_ENTRIES_V4 4
// 支持的规则数 编号
#define RULE_ACTION_MAX_ENTRIES_V4 RULE_NUM_MAX_ENTRIES_V4
#define LPM_PREFIXLEN_IPv4 32
struct lpm_key_ipv4
{
__u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
__u8 data[4]; /* Arbitrary size */
} __attribute__((aligned(4)));
// v4 v6 可共用
__u64 bitmap[BITMAP_ARRAY_SIZE];
// v4 v6 可共用此结构体
struct rule_action
{
__u64 action;
__u64 count;
};
// v4 v6 可共用此结构体
struct rule_action_key
{
__u64 bitmap_ffs;
__u64 bitmap_array_index;
};
struct bpf_map_def SEC("maps") src_v4 = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.key_size = sizeof(struct lpm_key_ipv4), // 8 byte: mask len; 8 byte: host byte oreder
.value_size = sizeof(bitmap),
.max_entries = IP_MAX_ENTRIES_V4,
.map_flags = BPF_F_NO_PREALLOC,
};
struct bpf_map_def SEC("maps") sport_v4 = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(__u16), // 2 byte; net byte oreder
.value_size = sizeof(bitmap),
.max_entries = PORT_MAX_ENTRIES_V4,
};
struct bpf_map_def SEC("maps") dst_v4 = {
.type = BPF_MAP_TYPE_LPM_TRIE,
.key_size = sizeof(struct lpm_key_ipv4), // 8 byte: mask len; 8 byte; host byte oreder
.value_size = sizeof(bitmap),
.max_entries = IP_MAX_ENTRIES_V4,
.map_flags = BPF_F_NO_PREALLOC,
};
struct bpf_map_def SEC("maps") dport_v4 = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(__u16), // 2 byte; net byte oreder
.value_size = sizeof(bitmap),
.max_entries = PORT_MAX_ENTRIES_V4,
};
struct bpf_map_def SEC("maps") proto_v4 = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(__u32), // 4 byte; host byte oreder
.value_size = sizeof(bitmap),
.max_entries = PROTO_MAX_ENTRIES_V4,
};
struct bpf_map_def SEC("maps") rule_action_v4 = {
.type = BPF_MAP_TYPE_PERCPU_HASH,
.key_size = sizeof(struct rule_action_key),
.value_size = sizeof(struct rule_action),
.max_entries = RULE_ACTION_MAX_ENTRIES_V4,
};
struct hdr_cursor
{
void *pos;
};
static __always_inline int parse_ethhdr(struct hdr_cursor *nh, void *data_end,
struct ethhdr **ethhdr_l2)
{
*ethhdr_l2 = nh->pos;
// Byte-count bounds check; check if current pointer + size of header is after data_end.
if ((void *)((*ethhdr_l2) + 1) > data_end)
{
return -1;
}
nh->pos += sizeof(struct ethhdr);
return (*ethhdr_l2)->h_proto; // network-byte-order
}
static __always_inline int parse_iphdr(struct hdr_cursor *nh,
void *data_end,
struct iphdr **iphdr_l3)
{
*iphdr_l3 = nh->pos;
if ((void *)((*iphdr_l3) + 1) > data_end)
{
return -1;
}
int hdrsize = ((*iphdr_l3)->ihl) << 2; // * 4
// Sanity check packet field is valid
if (hdrsize < sizeof(struct iphdr))
{
return -1;
}
// Variable-length IPv4 header, need to use byte-based arithmetic
nh->pos += hdrsize;
if (nh->pos > data_end)
{
return -1;
}
return (*iphdr_l3)->protocol;
}
// parse and return the length of the tcp header
static __always_inline int parse_tcphdr(struct hdr_cursor *nh,
void *data_end,
struct tcphdr **tcphdr_l4)
{
*tcphdr_l4 = nh->pos;
if ((void *)((*tcphdr_l4) + 1) > data_end)
{
return -1;
}
int len = ((*tcphdr_l4)->doff) << 2; // * 4
// Sanity check packet field is valid
if (len < sizeof(struct tcphdr))
{
return -1;
}
// Variable-length TCP header, need to use byte-based arithmetic
nh->pos += len;
if (nh->pos > data_end)
{
return -1;
}
return len;
}
// parse the udp header and return the length of the udp payload
static __always_inline int parse_udphdr(struct hdr_cursor *nh,
void *data_end,
struct udphdr **udphdr_l4)
{
*udphdr_l4 = nh->pos;
if ((void *)((*udphdr_l4) + 1) > data_end)
{
return -1;
}
nh->pos += sizeof(struct udphdr);
int len = bpf_ntohs((*udphdr_l4)->len) - sizeof(struct udphdr);
if (len < 0)
{
return -1;
}
return len;
}
static __always_inline void get_lpm_prefix_data_v4(__u32 addr, struct lpm_key_ipv4 *lpm_key_v4)
{
lpm_key_v4->data[0] = addr & 0xff; // bpf_ntohl(iphdr_l3->saddr);
lpm_key_v4->data[1] = (addr >> 8) & 0xff;
lpm_key_v4->data[2] = (addr >> 16) & 0xff;
lpm_key_v4->data[3] = (addr >> 24) & 0xff;
}
static __always_inline void get_bitmap_array_for_tcp_v4(__u64 *rule_array[], __u32 *rule_array_len, int *proto_type, struct iphdr *iphdr_l3, struct tcphdr *tcphdr_l4)
{
struct lpm_key_ipv4 lpm_key_v4;
__u64 *bitmap = NULL;
// addr src
lpm_key_v4.prefixlen = LPM_PREFIXLEN_IPv4;
get_lpm_prefix_data_v4(iphdr_l3->saddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&src_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr src: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->saddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// addr dst
get_lpm_prefix_data_v4(iphdr_l3->daddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&dst_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr dst: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->daddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// port src
__u16 port = tcphdr_l4->source; // bpf_ntohs(tcphdr_l4->source);
bitmap = bpf_map_lookup_elem(&sport_v4, &port);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit port src: %u; bitmap[0]: %llu\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohs(tcphdr_l4->source), bitmap[0]);
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// port dst bpf_ntohs(tcphdr_l4->dest);
port = tcphdr_l4->dest;
#ifdef XDPACL_DEBUG
char msg1[] = "port dst h-order: %u; n-order: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohs(tcphdr_l4->dest), tcphdr_l4->dest);
#endif
bitmap = bpf_map_lookup_elem(&dport_v4, &port);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit port dst: %u; bitmap[7]: %llu\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohs(tcphdr_l4->dest), bitmap[7]);
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// proto
bitmap = bpf_map_lookup_elem(&proto_v4, proto_type);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit tcp\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
}
static __always_inline void get_bitmap_array_for_udp_v4(__u64 *rule_array[], __u32 *rule_array_len, int *proto_type, struct iphdr *iphdr_l3, struct udphdr *udphdr_l4)
{
struct lpm_key_ipv4 lpm_key_v4;
__u64 *bitmap;
// addr src
lpm_key_v4.prefixlen = LPM_PREFIXLEN_IPv4;
get_lpm_prefix_data_v4(iphdr_l3->saddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&src_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr src: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->saddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// addr dst
get_lpm_prefix_data_v4(iphdr_l3->daddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&dst_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr dst: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->daddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// port src
__u16 port = udphdr_l4->source; // bpf_ntohs(udphdr_l4->source);
bitmap = bpf_map_lookup_elem(&sport_v4, &port);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit port src: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohs(udphdr_l4->source));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// port dst
port = udphdr_l4->dest; // bpf_ntohs(udphdr_l4->dest);
bitmap = bpf_map_lookup_elem(&dport_v4, &port);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit port dst: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohs(udphdr_l4->dest));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// proto
bitmap = bpf_map_lookup_elem(&proto_v4, proto_type);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit udp\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
}
static __always_inline void get_bitmap_array_for_icmp_v4(__u64 *rule_array[], __u32 *rule_array_len, int *proto_type, struct iphdr *iphdr_l3)
{
struct lpm_key_ipv4 lpm_key_v4;
__u64 *bitmap;
// addr src
lpm_key_v4.prefixlen = LPM_PREFIXLEN_IPv4;
get_lpm_prefix_data_v4(iphdr_l3->saddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&src_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr src: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->saddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// addr dst
get_lpm_prefix_data_v4(iphdr_l3->daddr, &lpm_key_v4);
bitmap = bpf_map_lookup_elem(&dst_v4, &lpm_key_v4);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit addr dst: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), bpf_ntohl(iphdr_l3->daddr));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
// proto
bitmap = bpf_map_lookup_elem(&proto_v4, proto_type);
if (NULL != bitmap)
{
#ifdef XDPACL_DEBUG
char msg1[] = "hit icmp\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
rule_array[(*rule_array_len)++] = bitmap;
}
}
static __always_inline void get_hit_rules_optimize(__u64 *rule_array[], __u32 *rule_array_len_ptr, __u64 *rule_array_index_ptr, __u64 *hit_rules_ptr)
{
if (5 == *rule_array_len_ptr)
{
// 5
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]) & (rule_array[4][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
}
else
{
// 3
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
}
return;
}
static __always_inline int get_rule_action_v4(__u64 *rule_array[], __u32 *rule_array_len_ptr)
{
/*
三种特殊情况:
未匹配到规则:
1 *rule_array_len == 0 => (bitmap[0] == 0); 或者 *rule_array_len == 1 2 4 返回 XDP_PASS;
2 *rule_array_len != 0; 但 bitmap 按位与时,都为 0 => (bitmap[BITMAP_ARRAY_SIZE - 1] == 0); 返回 XDP_PASS
匹配到规则:
3 未找到 action
*/
struct rule_action_key key;
struct rule_action *value;
if (unlikely(3 != *rule_array_len_ptr && 5 != *rule_array_len_ptr))
{
// 特殊情况 1
// key.bitmap_ffs = 0;
// key.bitmap_array_index = 0;
#ifdef XDPACL_DEBUG
char msg3[] = "result => rule action: specified 1\n";
bpf_trace_printk(msg3, sizeof(msg3));
#endif
// value = bpf_map_lookup_elem(&rule_action_v4, &key);
// if (NULL != value)
// {
// (value->count)++;
// return value->action;
// }
return XDP_PASS;
}
__u64 rule_array_index ____cacheline_aligned = 0;
__u64 hit_rules ____cacheline_aligned = 0;
// #pragma unroll
// for (; rule_array_index < BITMAP_ARRAY_SIZE; rule_array_index++)
// {
// get_hit_rules(rule_array, rule_array_len_ptr, &rule_array_index, &hit_rules);
// // #ifdef XDPACL_DEBUG
// // char msg3[] = "hit_rules: %llu; rule_array_inx: %llu\n";
// // bpf_trace_printk(msg3, sizeof(msg3), hit_rules, rule_array_index);
// // #endif
// if (hit_rules > 0)
// {
// break;
// }
// }
__u64 rule_array_outer_index ____cacheline_aligned = 0;
#pragma unroll
for (; rule_array_outer_index < BITMAP_ARRAY_SIZE; rule_array_outer_index += 8)
{
get_hit_rules_optimize(rule_array, rule_array_len_ptr, &rule_array_index, &hit_rules);
if (hit_rules > 0)
{
break;
}
rule_array_index++;
}
#ifdef XDPACL_DEBUG
char msg13[] = "result => rule_array.size = %lu; rule_array_index: %llu; hit_rules: %llu;\n";
bpf_trace_printk(msg13, sizeof(msg13), *rule_array_len_ptr, rule_array_index, hit_rules);
#endif
if (rule_array_index >= BITMAP_ARRAY_SIZE)
{
// 特殊情况 2
#ifdef XDPACL_DEBUG
char msg3[] = "result => hit bitmap[%d]: %llu; specified 2; \n";
bpf_trace_printk(msg3, sizeof(msg3), rule_array_index, hit_rules);
#endif
return XDP_PASS;
}
key.bitmap_ffs = hit_rules & (-hit_rules);
key.bitmap_array_index = rule_array_index;
value = bpf_map_lookup_elem(&rule_action_v4, &key);
if (NULL != value)
{
#ifdef XDPACL_DEBUG
char msg3[] = "result => hit bitmap[%d]: %llu; ffs: %llu; normal\n";
bpf_trace_printk(msg3, sizeof(msg3), rule_array_index, hit_rules, key.bitmap_ffs);
char msg4[] = "hit bitmap[%d]: ffs: %llu; action: %d\n";
bpf_trace_printk(msg4, sizeof(msg4), rule_array_index, key.bitmap_ffs, value->action);
#endif
(value->count)++;
return value->action;
}
// 特殊情况 3,现在默认为 XDP_PASS
#ifdef XDPACL_DEBUG
char msg2[] = "result => hit bitmap[%d]: %llu; ffs: %llu; rule action: specified 3\n";
bpf_trace_printk(msg2, sizeof(msg2), rule_array_index, hit_rules, key.bitmap_ffs);
#endif
return XDP_PASS;
}
SEC("xdp_acl")
int xdp_acl_func(struct xdp_md *ctx)
{
#ifdef XDPACL_DEBUG
char msg1[] = "\n\nreceive new frame, ingress_ifindex: %u; rx_queue_index: %u\n";
bpf_trace_printk(msg1, sizeof(msg1), ctx->ingress_ifindex, ctx->rx_queue_index);
#endif
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct hdr_cursor nh = {.pos = data};
int proto_type;
struct ethhdr *ethhdr_l2;
proto_type = parse_ethhdr(&nh, data_end, ðhdr_l2);
if (bpf_htons(ETH_P_IP) == proto_type)
{
// IPv4
__u64 *rule_array[5];
__u32 rule_array_len = 0;
struct iphdr *iphdr_l3;
proto_type = parse_iphdr(&nh, data_end, &iphdr_l3);
if (likely(IPPROTO_TCP == proto_type))
{
#ifdef XDPACL_DEBUG
char msg1[] = "receive TCP pkt\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
struct tcphdr *tcphdr_l4;
if (parse_tcphdr(&nh, data_end, &tcphdr_l4) < 0)
{
#ifdef XDPACL_DEBUG
char msg1[] = "\ndrop TCP pkt\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
return XDP_DROP;
}
get_bitmap_array_for_tcp_v4(rule_array, &rule_array_len, &proto_type, iphdr_l3, tcphdr_l4);
}
else if (IPPROTO_UDP == proto_type)
{
#ifdef XDPACL_DEBUG
char msg1[] = "receive UDP pkt\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
struct udphdr *udphdr_l4;
if (parse_udphdr(&nh, data_end, &udphdr_l4) < 0)
{
return XDP_DROP;
}
get_bitmap_array_for_udp_v4(rule_array, &rule_array_len, &proto_type, iphdr_l3, udphdr_l4);
}
else if (IPPROTO_ICMP == proto_type)
{
#ifdef XDPACL_DEBUG
char msg1[] = "receive ICMP pkt\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
get_bitmap_array_for_icmp_v4(rule_array, &rule_array_len, &proto_type, iphdr_l3);
}
else
{
// IPPROTO_GRE IPPROTO_OSPF...
#ifdef XDPACL_DEBUG
char msg1[] = "\n\nreceive Unknown IP pkt\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
return XDP_PASS;
}
return get_rule_action_v4(rule_array, &rule_array_len);
}
else if (proto_type > 0)
{
#ifdef XDPACL_DEBUG
char msg1[] = "\nreceive Not IP frame\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
// contain ETH_P_IPV6 ETH_P_ARP
return XDP_PASS;
}
else
{
#ifdef XDPACL_DEBUG
char msg1[] = "\ndrop eth frame\n";
bpf_trace_printk(msg1, sizeof(msg1));
#endif
return XDP_DROP;
}
}
char _license[] SEC("license") = "GPL";
// with optimization max len 5
/*
static __always_inline void get_hit_rules_optimize_max_len_5(__u64 *rule_array[], __u32 *rule_array_len_ptr, __u64 *rule_array_index_ptr, __u64 *hit_rules_ptr)
{
switch (*rule_array_len_ptr)
{
case 1:
{
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][*rule_array_index_ptr];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][*rule_array_index_ptr];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
(*rule_array_index_ptr)++;
if (rule_array[0][*rule_array_index_ptr] > 0)
{
*hit_rules_ptr = rule_array[0][(*rule_array_index_ptr)];
return;
}
return;
}
case 2:
{
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
return;
}
case 3:
{
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
return;
}
case 4:
{
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}
(*rule_array_index_ptr)++;
*hit_rules_ptr = (rule_array[0][*rule_array_index_ptr]) & (rule_array[1][*rule_array_index_ptr]) & (rule_array[2][*rule_array_index_ptr]) & (rule_array[3][*rule_array_index_ptr]);
if (*hit_rules_ptr > 0)
{
return;
}