Skip to content

Commit 0367d0a

Browse files
tehnerdborkmann
authored andcommitted
bpf: adding tests for bpf_xdp_adjust_tail
adding selftests for bpf_xdp_adjust_tail helper. in this synthetic test we are testing that 1) if data_end < data helper will return EINVAL 2) for normal use case packet's length would be reduced. Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 587b80c commit 0367d0a

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

tools/include/uapi/linux/bpf.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,13 @@ union bpf_attr {
755755
* @addr: pointer to struct sockaddr to bind socket to
756756
* @addr_len: length of sockaddr structure
757757
* Return: 0 on success or negative error code
758+
*
759+
* int bpf_xdp_adjust_tail(xdp_md, delta)
760+
* Adjust the xdp_md.data_end by delta. Only shrinking of packet's
761+
* size is supported.
762+
* @xdp_md: pointer to xdp_md
763+
* @delta: A negative integer to be added to xdp_md.data_end
764+
* Return: 0 on success or negative on error
758765
*/
759766
#define __BPF_FUNC_MAPPER(FN) \
760767
FN(unspec), \
@@ -821,7 +828,8 @@ union bpf_attr {
821828
FN(msg_apply_bytes), \
822829
FN(msg_cork_bytes), \
823830
FN(msg_pull_data), \
824-
FN(bind),
831+
FN(bind), \
832+
FN(xdp_adjust_tail),
825833

826834
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
827835
* function eBPF program intends to call

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
3131
sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
3232
test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
3333
sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
34-
sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o
34+
sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o
3535

3636
# Order correspond to 'make run_tests' order
3737
TEST_PROGS := test_kmod.sh \

tools/testing/selftests/bpf/bpf_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
9696
(void *) BPF_FUNC_msg_pull_data;
9797
static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
9898
(void *) BPF_FUNC_bind;
99+
static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
100+
(void *) BPF_FUNC_xdp_adjust_tail;
101+
99102

100103
/* llvm builtin functions that eBPF C program may use to
101104
* emit BPF_LD_ABS and BPF_LD_IND instructions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
* Copyright (c) 2018 Facebook
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of version 2 of the GNU General Public
6+
* License as published by the Free Software Foundation.
7+
*/
8+
#include <linux/bpf.h>
9+
#include <linux/if_ether.h>
10+
#include "bpf_helpers.h"
11+
12+
int _version SEC("version") = 1;
13+
14+
SEC("xdp_adjust_tail")
15+
int _xdp_adjust_tail(struct xdp_md *xdp)
16+
{
17+
void *data_end = (void *)(long)xdp->data_end;
18+
void *data = (void *)(long)xdp->data;
19+
int offset = 0;
20+
21+
if (data_end - data == 54)
22+
offset = 256;
23+
else
24+
offset = 20;
25+
if (bpf_xdp_adjust_tail(xdp, 0 - offset))
26+
return XDP_DROP;
27+
return XDP_TX;
28+
}
29+
30+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_progs.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,37 @@ static void test_xdp(void)
166166
bpf_object__close(obj);
167167
}
168168

169+
static void test_xdp_adjust_tail(void)
170+
{
171+
const char *file = "./test_adjust_tail.o";
172+
struct bpf_object *obj;
173+
char buf[128];
174+
__u32 duration, retval, size;
175+
int err, prog_fd;
176+
177+
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
178+
if (err) {
179+
error_cnt++;
180+
return;
181+
}
182+
183+
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
184+
buf, &size, &retval, &duration);
185+
186+
CHECK(err || errno || retval != XDP_DROP,
187+
"ipv4", "err %d errno %d retval %d size %d\n",
188+
err, errno, retval, size);
189+
190+
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
191+
buf, &size, &retval, &duration);
192+
CHECK(err || errno || retval != XDP_TX || size != 54,
193+
"ipv6", "err %d errno %d retval %d size %d\n",
194+
err, errno, retval, size);
195+
bpf_object__close(obj);
196+
}
197+
198+
199+
169200
#define MAGIC_VAL 0x1234
170201
#define NUM_ITER 100000
171202
#define VIP_NUM 5
@@ -1177,6 +1208,7 @@ int main(void)
11771208
{
11781209
test_pkt_access();
11791210
test_xdp();
1211+
test_xdp_adjust_tail();
11801212
test_l4lb_all();
11811213
test_xdp_noinline();
11821214
test_tcp_estats();

0 commit comments

Comments
 (0)