Skip to content

Commit 84fbfe0

Browse files
jrfastabborkmann
authored andcommitted
bpf: test_sockmap add options to use msg_push_data
Add options to run msg_push_data, this patch creates two more flags in test_sockmap that can be used to specify the offset and length of bytes to be added. The new options are --txmsg_start_push to specify where bytes should be inserted and --txmsg_end_push to specify how many bytes. This is analagous to the options that are used to pull data, --txmsg_start and --txmsg_end. In addition to adding the options tests are added to the test suit to run the tests similar to what was done for msg_pull_data. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent f908d26 commit 84fbfe0

File tree

2 files changed

+129
-26
lines changed

2 files changed

+129
-26
lines changed

tools/testing/selftests/bpf/test_sockmap.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ int txmsg_apply;
7777
int txmsg_cork;
7878
int txmsg_start;
7979
int txmsg_end;
80+
int txmsg_start_push;
81+
int txmsg_end_push;
8082
int txmsg_ingress;
8183
int txmsg_skb;
8284
int ktls;
@@ -100,6 +102,8 @@ static const struct option long_options[] = {
100102
{"txmsg_cork", required_argument, NULL, 'k'},
101103
{"txmsg_start", required_argument, NULL, 's'},
102104
{"txmsg_end", required_argument, NULL, 'e'},
105+
{"txmsg_start_push", required_argument, NULL, 'p'},
106+
{"txmsg_end_push", required_argument, NULL, 'q'},
103107
{"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
104108
{"txmsg_skb", no_argument, &txmsg_skb, 1 },
105109
{"ktls", no_argument, &ktls, 1 },
@@ -903,6 +907,30 @@ static int run_options(struct sockmap_options *options, int cg_fd, int test)
903907
}
904908
}
905909

910+
if (txmsg_start_push) {
911+
i = 2;
912+
err = bpf_map_update_elem(map_fd[5],
913+
&i, &txmsg_start_push, BPF_ANY);
914+
if (err) {
915+
fprintf(stderr,
916+
"ERROR: bpf_map_update_elem (txmsg_start_push): %d (%s)\n",
917+
err, strerror(errno));
918+
goto out;
919+
}
920+
}
921+
922+
if (txmsg_end_push) {
923+
i = 3;
924+
err = bpf_map_update_elem(map_fd[5],
925+
&i, &txmsg_end_push, BPF_ANY);
926+
if (err) {
927+
fprintf(stderr,
928+
"ERROR: bpf_map_update_elem %i@%i (txmsg_end_push): %d (%s)\n",
929+
txmsg_end_push, i, err, strerror(errno));
930+
goto out;
931+
}
932+
}
933+
906934
if (txmsg_ingress) {
907935
int in = BPF_F_INGRESS;
908936

@@ -1235,6 +1263,8 @@ static int test_mixed(int cgrp)
12351263
txmsg_pass = txmsg_noisy = txmsg_redir_noisy = txmsg_drop = 0;
12361264
txmsg_apply = txmsg_cork = 0;
12371265
txmsg_start = txmsg_end = 0;
1266+
txmsg_start_push = txmsg_end_push = 0;
1267+
12381268
/* Test small and large iov_count values with pass/redir/apply/cork */
12391269
txmsg_pass = 1;
12401270
txmsg_redir = 0;
@@ -1351,6 +1381,8 @@ static int test_start_end(int cgrp)
13511381
/* Test basic start/end with lots of iov_count and iov_lengths */
13521382
txmsg_start = 1;
13531383
txmsg_end = 2;
1384+
txmsg_start_push = 1;
1385+
txmsg_end_push = 2;
13541386
err = test_txmsg(cgrp);
13551387
if (err)
13561388
goto out;
@@ -1364,6 +1396,8 @@ static int test_start_end(int cgrp)
13641396
for (i = 99; i <= 1600; i += 500) {
13651397
txmsg_start = 0;
13661398
txmsg_end = i;
1399+
txmsg_start_push = 0;
1400+
txmsg_end_push = i;
13671401
err = test_exec(cgrp, &opt);
13681402
if (err)
13691403
goto out;
@@ -1373,6 +1407,8 @@ static int test_start_end(int cgrp)
13731407
for (i = 199; i <= 1600; i += 500) {
13741408
txmsg_start = 100;
13751409
txmsg_end = i;
1410+
txmsg_start_push = 100;
1411+
txmsg_end_push = i;
13761412
err = test_exec(cgrp, &opt);
13771413
if (err)
13781414
goto out;
@@ -1381,34 +1417,44 @@ static int test_start_end(int cgrp)
13811417
/* Test start/end with cork pulling last sg entry */
13821418
txmsg_start = 1500;
13831419
txmsg_end = 1600;
1420+
txmsg_start_push = 1500;
1421+
txmsg_end_push = 1600;
13841422
err = test_exec(cgrp, &opt);
13851423
if (err)
13861424
goto out;
13871425

13881426
/* Test start/end pull of single byte in last page */
13891427
txmsg_start = 1111;
13901428
txmsg_end = 1112;
1429+
txmsg_start_push = 1111;
1430+
txmsg_end_push = 1112;
13911431
err = test_exec(cgrp, &opt);
13921432
if (err)
13931433
goto out;
13941434

13951435
/* Test start/end with end < start */
13961436
txmsg_start = 1111;
13971437
txmsg_end = 0;
1438+
txmsg_start_push = 1111;
1439+
txmsg_end_push = 0;
13981440
err = test_exec(cgrp, &opt);
13991441
if (err)
14001442
goto out;
14011443

14021444
/* Test start/end with end > data */
14031445
txmsg_start = 0;
14041446
txmsg_end = 1601;
1447+
txmsg_start_push = 0;
1448+
txmsg_end_push = 1601;
14051449
err = test_exec(cgrp, &opt);
14061450
if (err)
14071451
goto out;
14081452

14091453
/* Test start/end with start > data */
14101454
txmsg_start = 1601;
14111455
txmsg_end = 1600;
1456+
txmsg_start_push = 1601;
1457+
txmsg_end_push = 1600;
14121458
err = test_exec(cgrp, &opt);
14131459

14141460
out:
@@ -1424,7 +1470,7 @@ char *map_names[] = {
14241470
"sock_map_redir",
14251471
"sock_apply_bytes",
14261472
"sock_cork_bytes",
1427-
"sock_pull_bytes",
1473+
"sock_bytes",
14281474
"sock_redir_flags",
14291475
"sock_skb_opts",
14301476
};
@@ -1531,7 +1577,7 @@ static int __test_suite(int cg_fd, char *bpf_file)
15311577
}
15321578

15331579
/* Tests basic commands and APIs with range of iov values */
1534-
txmsg_start = txmsg_end = 0;
1580+
txmsg_start = txmsg_end = txmsg_start_push = txmsg_end_push = 0;
15351581
err = test_txmsg(cg_fd);
15361582
if (err)
15371583
goto out;
@@ -1580,7 +1626,7 @@ int main(int argc, char **argv)
15801626
if (argc < 2)
15811627
return test_suite(-1);
15821628

1583-
while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:",
1629+
while ((opt = getopt_long(argc, argv, ":dhvc:r:i:l:t:p:q:",
15841630
long_options, &longindex)) != -1) {
15851631
switch (opt) {
15861632
case 's':
@@ -1589,6 +1635,12 @@ int main(int argc, char **argv)
15891635
case 'e':
15901636
txmsg_end = atoi(optarg);
15911637
break;
1638+
case 'p':
1639+
txmsg_start_push = atoi(optarg);
1640+
break;
1641+
case 'q':
1642+
txmsg_end_push = atoi(optarg);
1643+
break;
15921644
case 'a':
15931645
txmsg_apply = atoi(optarg);
15941646
break;

tools/testing/selftests/bpf/test_sockmap_kern.h

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ struct bpf_map_def SEC("maps") sock_cork_bytes = {
7070
.max_entries = 1
7171
};
7272

73-
struct bpf_map_def SEC("maps") sock_pull_bytes = {
73+
struct bpf_map_def SEC("maps") sock_bytes = {
7474
.type = BPF_MAP_TYPE_ARRAY,
7575
.key_size = sizeof(int),
7676
.value_size = sizeof(int),
77-
.max_entries = 2
77+
.max_entries = 4
7878
};
7979

8080
struct bpf_map_def SEC("maps") sock_redir_flags = {
@@ -181,27 +181,33 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
181181
SEC("sk_msg1")
182182
int bpf_prog4(struct sk_msg_md *msg)
183183
{
184-
int *bytes, zero = 0, one = 1;
185-
int *start, *end;
184+
int *bytes, zero = 0, one = 1, two = 2, three = 3;
185+
int *start, *end, *start_push, *end_push;
186186

187187
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
188188
if (bytes)
189189
bpf_msg_apply_bytes(msg, *bytes);
190190
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
191191
if (bytes)
192192
bpf_msg_cork_bytes(msg, *bytes);
193-
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
194-
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
193+
start = bpf_map_lookup_elem(&sock_bytes, &zero);
194+
end = bpf_map_lookup_elem(&sock_bytes, &one);
195195
if (start && end)
196196
bpf_msg_pull_data(msg, *start, *end, 0);
197+
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
198+
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
199+
if (start_push && end_push)
200+
bpf_msg_push_data(msg, *start_push, *end_push, 0);
197201
return SK_PASS;
198202
}
199203

200204
SEC("sk_msg2")
201205
int bpf_prog5(struct sk_msg_md *msg)
202206
{
203-
int err1 = -1, err2 = -1, zero = 0, one = 1;
204-
int *bytes, *start, *end, len1, len2;
207+
int zero = 0, one = 1, two = 2, three = 3;
208+
int *start, *end, *start_push, *end_push;
209+
int *bytes, len1, len2 = 0, len3;
210+
int err1 = -1, err2 = -1;
205211

206212
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
207213
if (bytes)
@@ -210,8 +216,8 @@ int bpf_prog5(struct sk_msg_md *msg)
210216
if (bytes)
211217
err2 = bpf_msg_cork_bytes(msg, *bytes);
212218
len1 = (__u64)msg->data_end - (__u64)msg->data;
213-
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
214-
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
219+
start = bpf_map_lookup_elem(&sock_bytes, &zero);
220+
end = bpf_map_lookup_elem(&sock_bytes, &one);
215221
if (start && end) {
216222
int err;
217223

@@ -225,6 +231,23 @@ int bpf_prog5(struct sk_msg_md *msg)
225231
bpf_printk("sk_msg2: length update %i->%i\n",
226232
len1, len2);
227233
}
234+
235+
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
236+
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
237+
if (start_push && end_push) {
238+
int err;
239+
240+
bpf_printk("sk_msg2: push(%i:%i)\n",
241+
start_push ? *start_push : 0,
242+
end_push ? *end_push : 0);
243+
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
244+
if (err)
245+
bpf_printk("sk_msg2: push_data err %i\n", err);
246+
len3 = (__u64)msg->data_end - (__u64)msg->data;
247+
bpf_printk("sk_msg2: length push_update %i->%i\n",
248+
len2 ? len2 : len1, len3);
249+
}
250+
228251
bpf_printk("sk_msg2: data length %i err1 %i err2 %i\n",
229252
len1, err1, err2);
230253
return SK_PASS;
@@ -233,8 +256,8 @@ int bpf_prog5(struct sk_msg_md *msg)
233256
SEC("sk_msg3")
234257
int bpf_prog6(struct sk_msg_md *msg)
235258
{
236-
int *bytes, zero = 0, one = 1, key = 0;
237-
int *start, *end, *f;
259+
int *bytes, *start, *end, *start_push, *end_push, *f;
260+
int zero = 0, one = 1, two = 2, three = 3, key = 0;
238261
__u64 flags = 0;
239262

240263
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
@@ -243,10 +266,17 @@ int bpf_prog6(struct sk_msg_md *msg)
243266
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
244267
if (bytes)
245268
bpf_msg_cork_bytes(msg, *bytes);
246-
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
247-
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
269+
270+
start = bpf_map_lookup_elem(&sock_bytes, &zero);
271+
end = bpf_map_lookup_elem(&sock_bytes, &one);
248272
if (start && end)
249273
bpf_msg_pull_data(msg, *start, *end, 0);
274+
275+
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
276+
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
277+
if (start_push && end_push)
278+
bpf_msg_push_data(msg, *start_push, *end_push, 0);
279+
250280
f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
251281
if (f && *f) {
252282
key = 2;
@@ -262,8 +292,9 @@ int bpf_prog6(struct sk_msg_md *msg)
262292
SEC("sk_msg4")
263293
int bpf_prog7(struct sk_msg_md *msg)
264294
{
265-
int err1 = 0, err2 = 0, zero = 0, one = 1, key = 0;
266-
int *f, *bytes, *start, *end, len1, len2;
295+
int zero = 0, one = 1, two = 2, three = 3, len1, len2 = 0, len3;
296+
int *bytes, *start, *end, *start_push, *end_push, *f;
297+
int err1 = 0, err2 = 0, key = 0;
267298
__u64 flags = 0;
268299

269300
int err;
@@ -274,10 +305,10 @@ int bpf_prog7(struct sk_msg_md *msg)
274305
if (bytes)
275306
err2 = bpf_msg_cork_bytes(msg, *bytes);
276307
len1 = (__u64)msg->data_end - (__u64)msg->data;
277-
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
278-
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
279-
if (start && end) {
280308

309+
start = bpf_map_lookup_elem(&sock_bytes, &zero);
310+
end = bpf_map_lookup_elem(&sock_bytes, &one);
311+
if (start && end) {
281312
bpf_printk("sk_msg2: pull(%i:%i)\n",
282313
start ? *start : 0, end ? *end : 0);
283314
err = bpf_msg_pull_data(msg, *start, *end, 0);
@@ -288,6 +319,22 @@ int bpf_prog7(struct sk_msg_md *msg)
288319
bpf_printk("sk_msg2: length update %i->%i\n",
289320
len1, len2);
290321
}
322+
323+
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
324+
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
325+
if (start_push && end_push) {
326+
bpf_printk("sk_msg4: push(%i:%i)\n",
327+
start_push ? *start_push : 0,
328+
end_push ? *end_push : 0);
329+
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
330+
if (err)
331+
bpf_printk("sk_msg4: push_data err %i\n",
332+
err);
333+
len3 = (__u64)msg->data_end - (__u64)msg->data;
334+
bpf_printk("sk_msg4: length push_update %i->%i\n",
335+
len2 ? len2 : len1, len3);
336+
}
337+
291338
f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
292339
if (f && *f) {
293340
key = 2;
@@ -342,19 +389,23 @@ int bpf_prog9(struct sk_msg_md *msg)
342389
SEC("sk_msg7")
343390
int bpf_prog10(struct sk_msg_md *msg)
344391
{
345-
int *bytes, zero = 0, one = 1;
346-
int *start, *end;
392+
int *bytes, *start, *end, *start_push, *end_push;
393+
int zero = 0, one = 1, two = 2, three = 3;
347394

348395
bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
349396
if (bytes)
350397
bpf_msg_apply_bytes(msg, *bytes);
351398
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
352399
if (bytes)
353400
bpf_msg_cork_bytes(msg, *bytes);
354-
start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
355-
end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
401+
start = bpf_map_lookup_elem(&sock_bytes, &zero);
402+
end = bpf_map_lookup_elem(&sock_bytes, &one);
356403
if (start && end)
357404
bpf_msg_pull_data(msg, *start, *end, 0);
405+
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
406+
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
407+
if (start_push && end_push)
408+
bpf_msg_push_data(msg, *start_push, *end_push, 0);
358409

359410
return SK_DROP;
360411
}

0 commit comments

Comments
 (0)