Skip to content

Commit b530e9e

Browse files
tohojoAlexei Starovoitov
authored andcommitted
bpf: Add "live packet" mode for XDP in BPF_PROG_RUN
This adds support for running XDP programs through BPF_PROG_RUN in a mode that enables live packet processing of the resulting frames. Previous uses of BPF_PROG_RUN for XDP returned the XDP program return code and the modified packet data to userspace, which is useful for unit testing of XDP programs. The existing BPF_PROG_RUN for XDP allows userspace to set the ingress ifindex and RXQ number as part of the context object being passed to the kernel. This patch reuses that code, but adds a new mode with different semantics, which can be selected with the new BPF_F_TEST_XDP_LIVE_FRAMES flag. When running BPF_PROG_RUN in this mode, the XDP program return codes will be honoured: returning XDP_PASS will result in the frame being injected into the networking stack as if it came from the selected networking interface, while returning XDP_TX and XDP_REDIRECT will result in the frame being transmitted out that interface. XDP_TX is translated into an XDP_REDIRECT operation to the same interface, since the real XDP_TX action is only possible from within the network drivers themselves, not from the process context where BPF_PROG_RUN is executed. Internally, this new mode of operation creates a page pool instance while setting up the test run, and feeds pages from that into the XDP program. The setup cost of this is amortised over the number of repetitions specified by userspace. To support the performance testing use case, we further optimise the setup step so that all pages in the pool are pre-initialised with the packet data, and pre-computed context and xdp_frame objects stored at the start of each page. This makes it possible to entirely avoid touching the page content on each XDP program invocation, and enables sending up to 9 Mpps/core on my test box. Because the data pages are recycled by the page pool, and the test runner doesn't re-initialise them for each run, subsequent invocations of the XDP program will see the packet data in the state it was after the last time it ran on that particular page. This means that an XDP program that modifies the packet before redirecting it has to be careful about which assumptions it makes about the packet content, but that is only an issue for the most naively written programs. Enabling the new flag is only allowed when not setting ctx_out and data_out in the test specification, since using it means frames will be redirected somewhere else, so they can't be returned. Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 3399dd9 commit b530e9e

File tree

5 files changed

+328
-15
lines changed

5 files changed

+328
-15
lines changed

include/uapi/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ enum {
12321232

12331233
/* If set, run the test on the cpu specified by bpf_attr.test.cpu */
12341234
#define BPF_F_TEST_RUN_ON_CPU (1U << 0)
1235+
/* If set, XDP frames will be transmitted after processing */
1236+
#define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1)
12351237

12361238
/* type for BPF_ENABLE_STATS */
12371239
enum bpf_stats_type {
@@ -1393,6 +1395,7 @@ union bpf_attr {
13931395
__aligned_u64 ctx_out;
13941396
__u32 flags;
13951397
__u32 cpu;
1398+
__u32 batch_size;
13961399
} test;
13971400

13981401
struct { /* anonymous struct used by BPF_*_GET_*_ID */

kernel/bpf/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config BPF_SYSCALL
3030
select TASKS_TRACE_RCU
3131
select BINARY_PRINTF
3232
select NET_SOCK_MSG if NET
33+
select PAGE_POOL if NET
3334
default n
3435
help
3536
Enable the bpf() system call that allows to manipulate BPF programs

kernel/bpf/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3336,7 +3336,7 @@ static int bpf_prog_query(const union bpf_attr *attr,
33363336
}
33373337
}
33383338

3339-
#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3339+
#define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
33403340

33413341
static int bpf_prog_test_run(const union bpf_attr *attr,
33423342
union bpf_attr __user *uattr)

0 commit comments

Comments
 (0)