1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 void test_xdp_update_frags(void)
6 {
7 	const char *file = "./test_xdp_update_frags.o";
8 	__u32 duration, retval, size;
9 	struct bpf_program *prog;
10 	struct bpf_object *obj;
11 	int err, prog_fd;
12 	__u32 *offset;
13 	__u8 *buf;
14 
15 	obj = bpf_object__open(file);
16 	if (libbpf_get_error(obj))
17 		return;
18 
19 	prog = bpf_object__next_program(obj, NULL);
20 	if (bpf_object__load(obj))
21 		return;
22 
23 	prog_fd = bpf_program__fd(prog);
24 
25 	buf = malloc(128);
26 	if (!ASSERT_OK_PTR(buf, "alloc buf 128b"))
27 		goto out;
28 
29 	memset(buf, 0, 128);
30 	offset = (__u32 *)buf;
31 	*offset = 16;
32 	buf[*offset] = 0xaa;		/* marker at offset 16 (head) */
33 	buf[*offset + 15] = 0xaa;	/* marker at offset 31 (head) */
34 
35 	err = bpf_prog_test_run(prog_fd, 1, buf, 128,
36 				buf, &size, &retval, &duration);
37 
38 	/* test_xdp_update_frags: buf[16,31]: 0xaa -> 0xbb */
39 	ASSERT_OK(err, "xdp_update_frag");
40 	ASSERT_EQ(retval, XDP_PASS, "xdp_update_frag retval");
41 	ASSERT_EQ(buf[16], 0xbb, "xdp_update_frag buf[16]");
42 	ASSERT_EQ(buf[31], 0xbb, "xdp_update_frag buf[31]");
43 
44 	free(buf);
45 
46 	buf = malloc(9000);
47 	if (!ASSERT_OK_PTR(buf, "alloc buf 9Kb"))
48 		goto out;
49 
50 	memset(buf, 0, 9000);
51 	offset = (__u32 *)buf;
52 	*offset = 5000;
53 	buf[*offset] = 0xaa;		/* marker at offset 5000 (frag0) */
54 	buf[*offset + 15] = 0xaa;	/* marker at offset 5015 (frag0) */
55 
56 	err = bpf_prog_test_run(prog_fd, 1, buf, 9000,
57 				buf, &size, &retval, &duration);
58 
59 	/* test_xdp_update_frags: buf[5000,5015]: 0xaa -> 0xbb */
60 	ASSERT_OK(err, "xdp_update_frag");
61 	ASSERT_EQ(retval, XDP_PASS, "xdp_update_frag retval");
62 	ASSERT_EQ(buf[5000], 0xbb, "xdp_update_frag buf[5000]");
63 	ASSERT_EQ(buf[5015], 0xbb, "xdp_update_frag buf[5015]");
64 
65 	memset(buf, 0, 9000);
66 	offset = (__u32 *)buf;
67 	*offset = 3510;
68 	buf[*offset] = 0xaa;		/* marker at offset 3510 (head) */
69 	buf[*offset + 15] = 0xaa;	/* marker at offset 3525 (frag0) */
70 
71 	err = bpf_prog_test_run(prog_fd, 1, buf, 9000,
72 				buf, &size, &retval, &duration);
73 
74 	/* test_xdp_update_frags: buf[3510,3525]: 0xaa -> 0xbb */
75 	ASSERT_OK(err, "xdp_update_frag");
76 	ASSERT_EQ(retval, XDP_PASS, "xdp_update_frag retval");
77 	ASSERT_EQ(buf[3510], 0xbb, "xdp_update_frag buf[3510]");
78 	ASSERT_EQ(buf[3525], 0xbb, "xdp_update_frag buf[3525]");
79 
80 	memset(buf, 0, 9000);
81 	offset = (__u32 *)buf;
82 	*offset = 7606;
83 	buf[*offset] = 0xaa;		/* marker at offset 7606 (frag0) */
84 	buf[*offset + 15] = 0xaa;	/* marker at offset 7621 (frag1) */
85 
86 	err = bpf_prog_test_run(prog_fd, 1, buf, 9000,
87 				buf, &size, &retval, &duration);
88 
89 	/* test_xdp_update_frags: buf[7606,7621]: 0xaa -> 0xbb */
90 	ASSERT_OK(err, "xdp_update_frag");
91 	ASSERT_EQ(retval, XDP_PASS, "xdp_update_frag retval");
92 	ASSERT_EQ(buf[7606], 0xbb, "xdp_update_frag buf[7606]");
93 	ASSERT_EQ(buf[7621], 0xbb, "xdp_update_frag buf[7621]");
94 
95 	free(buf);
96 out:
97 	bpf_object__close(obj);
98 }
99 
100 void test_xdp_adjust_frags(void)
101 {
102 	if (test__start_subtest("xdp_adjust_frags"))
103 		test_xdp_update_frags();
104 }
105