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 	struct bpf_program *prog;
9 	struct bpf_object *obj;
10 	int err, prog_fd;
11 	__u32 *offset;
12 	__u8 *buf;
13 	LIBBPF_OPTS(bpf_test_run_opts, topts);
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 	topts.data_in = buf;
36 	topts.data_out = buf;
37 	topts.data_size_in = 128;
38 	topts.data_size_out = 128;
39 
40 	err = bpf_prog_test_run_opts(prog_fd, &topts);
41 
42 	/* test_xdp_update_frags: buf[16,31]: 0xaa -> 0xbb */
43 	ASSERT_OK(err, "xdp_update_frag");
44 	ASSERT_EQ(topts.retval, XDP_PASS, "xdp_update_frag retval");
45 	ASSERT_EQ(buf[16], 0xbb, "xdp_update_frag buf[16]");
46 	ASSERT_EQ(buf[31], 0xbb, "xdp_update_frag buf[31]");
47 
48 	free(buf);
49 
50 	buf = malloc(9000);
51 	if (!ASSERT_OK_PTR(buf, "alloc buf 9Kb"))
52 		goto out;
53 
54 	memset(buf, 0, 9000);
55 	offset = (__u32 *)buf;
56 	*offset = 5000;
57 	buf[*offset] = 0xaa;		/* marker at offset 5000 (frag0) */
58 	buf[*offset + 15] = 0xaa;	/* marker at offset 5015 (frag0) */
59 
60 	topts.data_in = buf;
61 	topts.data_out = buf;
62 	topts.data_size_in = 9000;
63 	topts.data_size_out = 9000;
64 
65 	err = bpf_prog_test_run_opts(prog_fd, &topts);
66 
67 	/* test_xdp_update_frags: buf[5000,5015]: 0xaa -> 0xbb */
68 	ASSERT_OK(err, "xdp_update_frag");
69 	ASSERT_EQ(topts.retval, XDP_PASS, "xdp_update_frag retval");
70 	ASSERT_EQ(buf[5000], 0xbb, "xdp_update_frag buf[5000]");
71 	ASSERT_EQ(buf[5015], 0xbb, "xdp_update_frag buf[5015]");
72 
73 	memset(buf, 0, 9000);
74 	offset = (__u32 *)buf;
75 	*offset = 3510;
76 	buf[*offset] = 0xaa;		/* marker at offset 3510 (head) */
77 	buf[*offset + 15] = 0xaa;	/* marker at offset 3525 (frag0) */
78 
79 	err = bpf_prog_test_run_opts(prog_fd, &topts);
80 
81 	/* test_xdp_update_frags: buf[3510,3525]: 0xaa -> 0xbb */
82 	ASSERT_OK(err, "xdp_update_frag");
83 	ASSERT_EQ(topts.retval, XDP_PASS, "xdp_update_frag retval");
84 	ASSERT_EQ(buf[3510], 0xbb, "xdp_update_frag buf[3510]");
85 	ASSERT_EQ(buf[3525], 0xbb, "xdp_update_frag buf[3525]");
86 
87 	memset(buf, 0, 9000);
88 	offset = (__u32 *)buf;
89 	*offset = 7606;
90 	buf[*offset] = 0xaa;		/* marker at offset 7606 (frag0) */
91 	buf[*offset + 15] = 0xaa;	/* marker at offset 7621 (frag1) */
92 
93 	err = bpf_prog_test_run_opts(prog_fd, &topts);
94 
95 	/* test_xdp_update_frags: buf[7606,7621]: 0xaa -> 0xbb */
96 	ASSERT_OK(err, "xdp_update_frag");
97 	ASSERT_EQ(topts.retval, XDP_PASS, "xdp_update_frag retval");
98 	ASSERT_EQ(buf[7606], 0xbb, "xdp_update_frag buf[7606]");
99 	ASSERT_EQ(buf[7621], 0xbb, "xdp_update_frag buf[7621]");
100 
101 	free(buf);
102 out:
103 	bpf_object__close(obj);
104 }
105 
106 void test_xdp_adjust_frags(void)
107 {
108 	if (test__start_subtest("xdp_adjust_frags"))
109 		test_xdp_update_frags();
110 }
111