1
1
/* SPDX-License-Identifier: GPL-2.0 */
2
2
#include <linux/bpf.h>
3
- #include <linux/in.h>
4
3
#include <bpf/bpf_helpers.h>
5
- #include <bpf/bpf_endian.h>
6
-
7
- // The parsing helper functions from the packet01 lesson have moved here
8
- #include "../common/parsing_helpers.h"
9
- #include "../common/rewrite_helpers.h"
10
-
11
- /* Defines xdp_stats_map */
12
- #include "../common/xdp_stats_kern_user.h"
13
- #include "../common/xdp_stats_kern.h"
14
4
15
5
#define MTU 1536
16
- #define MIN_LEN 64
17
-
18
- SEC ("xdp_test1b" )
19
- int _xdp_test1b (struct xdp_md * ctx )
6
+ #define MIN_LEN 14
7
+
8
+ /*
9
+ * This example show howto access packet last byte in XDP packet,
10
+ * without parsing packet contents.
11
+ *
12
+ * It is not very effecient, as it advance the data pointer one-byte in a
13
+ * loop until reaching data_end. This is needed as the verifier only allows
14
+ * accessing data via advancing the position of the data pointer. The bounded
15
+ * loop with a max number of iterations allows the verifier to see the bound.
16
+ */
17
+
18
+ SEC ("xdp_end_loop" )
19
+ int _xdp_end_loop (struct xdp_md * ctx )
20
20
{
21
21
void * data_end = (void * )(long )ctx -> data_end ;
22
22
void * data = (void * )(long )ctx -> data ;
23
23
unsigned char * ptr ;
24
24
unsigned int i ;
25
25
void * pos ;
26
26
27
+ /* Assume minimum length to reduce loops needed a bit */
27
28
unsigned int offset = MIN_LEN ;
28
29
29
30
pos = data ;
30
31
31
- /* Verifier can handle this bounded basic-loop construct */
32
+ /* Verifier can handle this bounded ' basic-loop' construct */
32
33
for (i = 0 ; i < (MTU - MIN_LEN ); i ++ ) {
33
34
if (pos + offset > data_end ) {
34
35
/* Promise verifier no access beyond data_end */
@@ -54,80 +55,9 @@ int _xdp_test1b(struct xdp_md *ctx)
54
55
goto out ;
55
56
56
57
read :
57
- // ptr = pos + (offset - );
58
- // ptr = pos + (offset - sizeof(*ptr) - 1);
59
- ptr = pos + (offset - sizeof (* ptr ));
58
+ ptr = pos + (offset - sizeof (* ptr )); /* Parentheses needed */
60
59
if (* ptr == 0xFF )
61
60
return XDP_ABORTED ;
62
61
out :
63
62
return XDP_PASS ;
64
63
}
65
-
66
-
67
- SEC ("xdp_test1" )
68
- int _xdp_test1 (struct xdp_md * ctx )
69
- {
70
- void * data_end = (void * )(long )ctx -> data_end ;
71
- void * data = (void * )(long )ctx -> data ;
72
- unsigned char * ptr ;
73
- unsigned int i ;
74
- void * pos ;
75
-
76
- unsigned int offset = 64 ;
77
-
78
- pos = data ;
79
-
80
- if (pos + 64 > data_end )
81
- goto out ;
82
-
83
- for (i = 0 ; i < (1536 - 64 ); i ++ ) {
84
- if (pos + offset > data_end ) {
85
- goto out ;
86
- }
87
- if (pos + offset == data_end ) {
88
- goto read ;
89
- }
90
- offset ++ ;
91
- }
92
- goto out ;
93
-
94
- read :
95
- // ptr = pos + (offset - );
96
- // ptr = pos + (offset - sizeof(*ptr) - 1);
97
- ptr = pos + (offset - sizeof (* ptr ));
98
- if (* ptr == 0xFF )
99
- return XDP_ABORTED ;
100
- out :
101
- return XDP_PASS ;
102
- }
103
-
104
-
105
- SEC ("xdp_test2" )
106
- int _xdp_test2 (struct xdp_md * ctx )
107
- {
108
- void * data_end = (void * )(long )ctx -> data_end ;
109
- void * data = (void * )(long )ctx -> data ;
110
- struct hdr_cursor nh ;
111
- unsigned char * ptr ;
112
-
113
- unsigned int offset = 64 ;
114
-
115
- nh .pos = data ;
116
-
117
- if (nh .pos + offset > data_end )
118
- goto out ;
119
-
120
- // ptr = nh.pos + (offset - 1);
121
- ptr = nh .pos + (offset - sizeof (* ptr ));
122
- if (* ptr == 0xFF )
123
- return XDP_ABORTED ;
124
- out :
125
- return xdp_stats_record_action (ctx , XDP_PASS );
126
- }
127
-
128
- SEC ("xdp_pass" )
129
- int xdp_pass_f1 (struct xdp_md * ctx )
130
- {
131
- return xdp_stats_record_action (ctx , XDP_PASS );
132
- }
133
-
0 commit comments