aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorJohn Fastabend <john.fastabend@gmail.com>2018-03-18 15:58:12 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-03-19 16:14:41 -0400
commit0dcbbf6785799ba05b44df2ba6bcc1195f4f945c (patch)
treefc6bb4f05e72ef992a80d015fa963fa08d2cbc6d /samples
parente6373ce70a01292424a118d9457d927349dad51d (diff)
bpf: sockmap sample test for bpf_msg_pull_data
This adds an option to test the msg_pull_data helper. This uses two options txmsg_start and txmsg_end to let the user specify start and end bytes to pull. The options can be used with txmsg_apply, txmsg_cork options as well as with any of the basic tests, txmsg, txmsg_redir and txmsg_drop (plus noisy variants) to run pull_data inline with those tests. By giving user direct control over the variables we can easily do negative testing as well as positive tests. Signed-off-by: John Fastabend <john.fastabend@gmail.com> Acked-by: David S. Miller <davem@davemloft.net> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/sockmap/sockmap_kern.c79
-rw-r--r--samples/sockmap/sockmap_user.c32
2 files changed, 97 insertions, 14 deletions
diff --git a/samples/sockmap/sockmap_kern.c b/samples/sockmap/sockmap_kern.c
index 8b6c34cd5f46..9ad5ba79c85a 100644
--- a/samples/sockmap/sockmap_kern.c
+++ b/samples/sockmap/sockmap_kern.c
@@ -71,6 +71,14 @@ struct bpf_map_def SEC("maps") sock_cork_bytes = {
71 .max_entries = 1 71 .max_entries = 1
72}; 72};
73 73
74struct bpf_map_def SEC("maps") sock_pull_bytes = {
75 .type = BPF_MAP_TYPE_ARRAY,
76 .key_size = sizeof(int),
77 .value_size = sizeof(int),
78 .max_entries = 2
79};
80
81
74SEC("sk_skb1") 82SEC("sk_skb1")
75int bpf_prog1(struct __sk_buff *skb) 83int bpf_prog1(struct __sk_buff *skb)
76{ 84{
@@ -137,7 +145,8 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
137SEC("sk_msg1") 145SEC("sk_msg1")
138int bpf_prog4(struct sk_msg_md *msg) 146int bpf_prog4(struct sk_msg_md *msg)
139{ 147{
140 int *bytes, zero = 0; 148 int *bytes, zero = 0, one = 1;
149 int *start, *end;
141 150
142 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); 151 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
143 if (bytes) 152 if (bytes)
@@ -145,15 +154,18 @@ int bpf_prog4(struct sk_msg_md *msg)
145 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero); 154 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
146 if (bytes) 155 if (bytes)
147 bpf_msg_cork_bytes(msg, *bytes); 156 bpf_msg_cork_bytes(msg, *bytes);
157 start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
158 end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
159 if (start && end)
160 bpf_msg_pull_data(msg, *start, *end, 0);
148 return SK_PASS; 161 return SK_PASS;
149} 162}
150 163
151SEC("sk_msg2") 164SEC("sk_msg2")
152int bpf_prog5(struct sk_msg_md *msg) 165int bpf_prog5(struct sk_msg_md *msg)
153{ 166{
154 void *data_end = (void *)(long) msg->data_end; 167 int err1 = -1, err2 = -1, zero = 0, one = 1;
155 void *data = (void *)(long) msg->data; 168 int *bytes, *start, *end, len1, len2;
156 int *bytes, err1 = -1, err2 = -1, zero = 0;
157 169
158 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); 170 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
159 if (bytes) 171 if (bytes)
@@ -161,17 +173,32 @@ int bpf_prog5(struct sk_msg_md *msg)
161 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero); 173 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
162 if (bytes) 174 if (bytes)
163 err2 = bpf_msg_cork_bytes(msg, *bytes); 175 err2 = bpf_msg_cork_bytes(msg, *bytes);
176 len1 = (__u64)msg->data_end - (__u64)msg->data;
177 start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
178 end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
179 if (start && end) {
180 int err;
181
182 bpf_printk("sk_msg2: pull(%i:%i)\n",
183 start ? *start : 0, end ? *end : 0);
184 err = bpf_msg_pull_data(msg, *start, *end, 0);
185 if (err)
186 bpf_printk("sk_msg2: pull_data err %i\n",
187 err);
188 len2 = (__u64)msg->data_end - (__u64)msg->data;
189 bpf_printk("sk_msg2: length update %i->%i\n",
190 len1, len2);
191 }
164 bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n", 192 bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n",
165 (__u64)data_end - (__u64)data, err1, err2); 193 len1, err1, err2);
166 return SK_PASS; 194 return SK_PASS;
167} 195}
168 196
169SEC("sk_msg3") 197SEC("sk_msg3")
170int bpf_prog6(struct sk_msg_md *msg) 198int bpf_prog6(struct sk_msg_md *msg)
171{ 199{
172 void *data_end = (void *)(long) msg->data_end; 200 int *bytes, zero = 0, one = 1;
173 void *data = (void *)(long) msg->data; 201 int *start, *end;
174 int *bytes, zero = 0;
175 202
176 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); 203 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
177 if (bytes) 204 if (bytes)
@@ -179,15 +206,18 @@ int bpf_prog6(struct sk_msg_md *msg)
179 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero); 206 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
180 if (bytes) 207 if (bytes)
181 bpf_msg_cork_bytes(msg, *bytes); 208 bpf_msg_cork_bytes(msg, *bytes);
209 start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
210 end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
211 if (start && end)
212 bpf_msg_pull_data(msg, *start, *end, 0);
182 return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0); 213 return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
183} 214}
184 215
185SEC("sk_msg4") 216SEC("sk_msg4")
186int bpf_prog7(struct sk_msg_md *msg) 217int bpf_prog7(struct sk_msg_md *msg)
187{ 218{
188 void *data_end = (void *)(long) msg->data_end; 219 int err1 = 0, err2 = 0, zero = 0, one = 1;
189 void *data = (void *)(long) msg->data; 220 int *bytes, *start, *end, len1, len2;
190 int *bytes, err1 = 0, err2 = 0, zero = 0;
191 221
192 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); 222 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
193 if (bytes) 223 if (bytes)
@@ -195,9 +225,24 @@ int bpf_prog7(struct sk_msg_md *msg)
195 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero); 225 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
196 if (bytes) 226 if (bytes)
197 err2 = bpf_msg_cork_bytes(msg, *bytes); 227 err2 = bpf_msg_cork_bytes(msg, *bytes);
198 228 len1 = (__u64)msg->data_end - (__u64)msg->data;
229 start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
230 end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
231 if (start && end) {
232 int err;
233
234 bpf_printk("sk_msg2: pull(%i:%i)\n",
235 start ? *start : 0, end ? *end : 0);
236 err = bpf_msg_pull_data(msg, *start, *end, 0);
237 if (err)
238 bpf_printk("sk_msg2: pull_data err %i\n",
239 err);
240 len2 = (__u64)msg->data_end - (__u64)msg->data;
241 bpf_printk("sk_msg2: length update %i->%i\n",
242 len1, len2);
243 }
199 bpf_printk("sk_msg3: redirect(%iB) err1=%i err2=%i\n", 244 bpf_printk("sk_msg3: redirect(%iB) err1=%i err2=%i\n",
200 (__u64)data_end - (__u64)data, err1, err2); 245 len1, err1, err2);
201 return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0); 246 return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
202} 247}
203 248
@@ -239,7 +284,8 @@ int bpf_prog9(struct sk_msg_md *msg)
239SEC("sk_msg7") 284SEC("sk_msg7")
240int bpf_prog10(struct sk_msg_md *msg) 285int bpf_prog10(struct sk_msg_md *msg)
241{ 286{
242 int *bytes, zero = 0; 287 int *bytes, zero = 0, one = 1;
288 int *start, *end;
243 289
244 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero); 290 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
245 if (bytes) 291 if (bytes)
@@ -247,6 +293,11 @@ int bpf_prog10(struct sk_msg_md *msg)
247 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero); 293 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
248 if (bytes) 294 if (bytes)
249 bpf_msg_cork_bytes(msg, *bytes); 295 bpf_msg_cork_bytes(msg, *bytes);
296 start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
297 end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
298 if (start && end)
299 bpf_msg_pull_data(msg, *start, *end, 0);
300
250 return SK_DROP; 301 return SK_DROP;
251} 302}
252 303
diff --git a/samples/sockmap/sockmap_user.c b/samples/sockmap/sockmap_user.c
index 52c4ed7774d4..07aa237221d1 100644
--- a/samples/sockmap/sockmap_user.c
+++ b/samples/sockmap/sockmap_user.c
@@ -62,6 +62,8 @@ int txmsg_redir_noisy;
62int txmsg_drop; 62int txmsg_drop;
63int txmsg_apply; 63int txmsg_apply;
64int txmsg_cork; 64int txmsg_cork;
65int txmsg_start;
66int txmsg_end;
65 67
66static const struct option long_options[] = { 68static const struct option long_options[] = {
67 {"help", no_argument, NULL, 'h' }, 69 {"help", no_argument, NULL, 'h' },
@@ -79,6 +81,8 @@ static const struct option long_options[] = {
79 {"txmsg_drop", no_argument, &txmsg_drop, 1 }, 81 {"txmsg_drop", no_argument, &txmsg_drop, 1 },