Skip to content

Commit a5a358a

Browse files
LorenzoBianconiAlexei Starovoitov
authored andcommitted
selftest/bpf: Check invalid length in test_xdp_update_frags
Update test_xdp_update_frags adding a test for a buffer size set to (MAX_SKB_FRAGS + 2) * PAGE_SIZE. The kernel is supposed to return -ENOMEM. Signed-off-by: Lorenzo Bianconi <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/3e4afa0ee4976854b2f0296998fe6754a80b62e5.1644366736.git.lorenzo@kernel.org
1 parent 85fbd23 commit a5a358a

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

tools/testing/selftests/bpf/prog_tests/xdp_adjust_frags.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
static void test_xdp_update_frags(void)
66
{
77
const char *file = "./test_xdp_update_frags.o";
8+
int err, prog_fd, max_skb_frags, buf_size, num;
89
struct bpf_program *prog;
910
struct bpf_object *obj;
10-
int err, prog_fd;
1111
__u32 *offset;
1212
__u8 *buf;
13+
FILE *f;
1314
LIBBPF_OPTS(bpf_test_run_opts, topts);
1415

1516
obj = bpf_object__open(file);
@@ -99,6 +100,41 @@ static void test_xdp_update_frags(void)
99100
ASSERT_EQ(buf[7621], 0xbb, "xdp_update_frag buf[7621]");
100101

101102
free(buf);
103+
104+
/* test_xdp_update_frags: unsupported buffer size */
105+
f = fopen("/proc/sys/net/core/max_skb_frags", "r");
106+
if (!ASSERT_OK_PTR(f, "max_skb_frag file pointer"))
107+
goto out;
108+
109+
num = fscanf(f, "%d", &max_skb_frags);
110+
fclose(f);
111+
112+
if (!ASSERT_EQ(num, 1, "max_skb_frags read failed"))
113+
goto out;
114+
115+
/* xdp_buff linear area size is always set to 4096 in the
116+
* bpf_prog_test_run_xdp routine.
117+
*/
118+
buf_size = 4096 + (max_skb_frags + 1) * sysconf(_SC_PAGE_SIZE);
119+
buf = malloc(buf_size);
120+
if (!ASSERT_OK_PTR(buf, "alloc buf"))
121+
goto out;
122+
123+
memset(buf, 0, buf_size);
124+
offset = (__u32 *)buf;
125+
*offset = 16;
126+
buf[*offset] = 0xaa;
127+
buf[*offset + 15] = 0xaa;
128+
129+
topts.data_in = buf;
130+
topts.data_out = buf;
131+
topts.data_size_in = buf_size;
132+
topts.data_size_out = buf_size;
133+
134+
err = bpf_prog_test_run_opts(prog_fd, &topts);
135+
ASSERT_EQ(err, -ENOMEM,
136+
"unsupported buf size, possible non-default /proc/sys/net/core/max_skb_flags?");
137+
free(buf);
102138
out:
103139
bpf_object__close(obj);
104140
}

0 commit comments

Comments
 (0)