Skip to content

Commit d991730

Browse files
chaudronAlexei Starovoitov
authored andcommitted
bpf: add frags support to xdp copy helpers
This patch adds support for frags for the following helpers: - bpf_xdp_output() - bpf_perf_event_output() Acked-by: Toke Hoiland-Jorgensen <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: Eelco Chaudron <[email protected]> Signed-off-by: Lorenzo Bianconi <[email protected]> Link: https://lore.kernel.org/r/340b4a99cdc24337b40eaf8bb597f9f9e7b0373e.1642758637.git.lorenzo@kernel.org Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent bf25146 commit d991730

File tree

4 files changed

+137
-36
lines changed

4 files changed

+137
-36
lines changed

kernel/trace/bpf_trace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
15621562

15631563
extern const struct bpf_func_proto bpf_skb_output_proto;
15641564
extern const struct bpf_func_proto bpf_xdp_output_proto;
1565+
extern const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto;
15651566

15661567
BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
15671568
struct bpf_map *, map, u64, flags)
@@ -1661,6 +1662,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16611662
return &bpf_sock_from_file_proto;
16621663
case BPF_FUNC_get_socket_cookie:
16631664
return &bpf_get_socket_ptr_cookie_proto;
1665+
case BPF_FUNC_xdp_get_buff_len:
1666+
return &bpf_xdp_get_buff_len_trace_proto;
16641667
#endif
16651668
case BPF_FUNC_seq_printf:
16661669
return prog->expected_attach_type == BPF_TRACE_ITER ?

net/core/filter.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,6 +3796,15 @@ static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
37963796
.arg1_type = ARG_PTR_TO_CTX,
37973797
};
37983798

3799+
BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3800+
3801+
const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3802+
.func = bpf_xdp_get_buff_len,
3803+
.gpl_only = false,
3804+
.arg1_type = ARG_PTR_TO_BTF_ID,
3805+
.arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3806+
};
3807+
37993808
static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
38003809
{
38013810
return xdp_data_meta_unsupported(xdp) ? 0 :
@@ -4668,10 +4677,48 @@ static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
46684677
};
46694678
#endif
46704679

4671-
static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4680+
static unsigned long bpf_xdp_copy(void *dst_buff, const void *ctx,
46724681
unsigned long off, unsigned long len)
46734682
{
4674-
memcpy(dst_buff, src_buff + off, len);
4683+
struct xdp_buff *xdp = (struct xdp_buff *)ctx;
4684+
unsigned long ptr_len, ptr_off = 0;
4685+
skb_frag_t *next_frag, *end_frag;
4686+
struct skb_shared_info *sinfo;
4687+
u8 *ptr_buf;
4688+
4689+
if (likely(xdp->data_end - xdp->data >= off + len)) {
4690+
memcpy(dst_buff, xdp->data + off, len);
4691+
return 0;
4692+
}
4693+
4694+
sinfo = xdp_get_shared_info_from_buff(xdp);
4695+
end_frag = &sinfo->frags[sinfo->nr_frags];
4696+
next_frag = &sinfo->frags[0];
4697+
4698+
ptr_len = xdp->data_end - xdp->data;
4699+
ptr_buf = xdp->data;
4700+
4701+
while (true) {
4702+
if (off < ptr_off + ptr_len) {
4703+
unsigned long copy_off = off - ptr_off;
4704+
unsigned long copy_len = min(len, ptr_len - copy_off);
4705+
4706+
memcpy(dst_buff, ptr_buf + copy_off, copy_len);
4707+
4708+
off += copy_len;
4709+
len -= copy_len;
4710+
dst_buff += copy_len;
4711+
}
4712+
4713+
if (!len || next_frag == end_frag)
4714+
break;
4715+
4716+
ptr_off += ptr_len;
4717+
ptr_buf = skb_frag_address(next_frag);
4718+
ptr_len = skb_frag_size(next_frag);
4719+
next_frag++;
4720+
}
4721+
46754722
return 0;
46764723
}
46774724

@@ -4682,11 +4729,11 @@ BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
46824729

46834730
if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
46844731
return -EINVAL;
4685-
if (unlikely(!xdp ||
4686-
xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4732+
4733+
if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
46874734
return -EFAULT;
46884735

4689-
return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4736+
return bpf_event_output(map, flags, meta, meta_size, xdp,
46904737
xdp_size, bpf_xdp_copy);
46914738
}
46924739

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

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,97 @@ struct meta {
1010
int pkt_len;
1111
};
1212

13+
struct test_ctx_s {
14+
bool passed;
15+
int pkt_size;
16+
};
17+
18+
struct test_ctx_s test_ctx;
19+
1320
static void on_sample(void *ctx, int cpu, void *data, __u32 size)
1421
{
1522
struct meta *meta = (struct meta *)data;
1623
struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta);
24+
unsigned char *raw_pkt = data + sizeof(*meta);
25+
struct test_ctx_s *tst_ctx = ctx;
1726

1827
ASSERT_GE(size, sizeof(pkt_v4) + sizeof(*meta), "check_size");
1928
ASSERT_EQ(meta->ifindex, if_nametoindex("lo"), "check_meta_ifindex");
20-
ASSERT_EQ(meta->pkt_len, sizeof(pkt_v4), "check_meta_pkt_len");
29+
ASSERT_EQ(meta->pkt_len, tst_ctx->pkt_size, "check_meta_pkt_len");
2130
ASSERT_EQ(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)), 0,
2231
"check_packet_content");
2332

24-
*(bool *)ctx = true;
33+
if (meta->pkt_len > sizeof(pkt_v4)) {
34+
for (int i = 0; i < meta->pkt_len - sizeof(pkt_v4); i++)
35+
ASSERT_EQ(raw_pkt[i + sizeof(pkt_v4)], (unsigned char)i,
36+
"check_packet_content");
37+
}
38+
39+
tst_ctx->passed = true;
2540
}
2641

27-
void test_xdp_bpf2bpf(void)
42+
#define BUF_SZ 9000
43+
44+
static void run_xdp_bpf2bpf_pkt_size(int pkt_fd, struct perf_buffer *pb,
45+
struct test_xdp_bpf2bpf *ftrace_skel,
46+
int pkt_size)
2847
{
2948
__u32 duration = 0, retval, size;
30-
char buf[128];
49+
__u8 *buf, *buf_in;
50+
int err;
51+
52+
if (!ASSERT_LE(pkt_size, BUF_SZ, "pkt_size") ||
53+
!ASSERT_GE(pkt_size, sizeof(pkt_v4), "pkt_size"))
54+
return;
55+
56+
buf_in = malloc(BUF_SZ);
57+
if (!ASSERT_OK_PTR(buf_in, "buf_in malloc()"))
58+
return;
59+
60+
buf = malloc(BUF_SZ);
61+
if (!ASSERT_OK_PTR(buf, "buf malloc()")) {
62+
free(buf_in);
63+
return;
64+
}
65+
66+
test_ctx.passed = false;
67+
test_ctx.pkt_size = pkt_size;
68+
69+
memcpy(buf_in, &pkt_v4, sizeof(pkt_v4));
70+
if (pkt_size > sizeof(pkt_v4)) {
71+
for (int i = 0; i < (pkt_size - sizeof(pkt_v4)); i++)
72+
buf_in[i + sizeof(pkt_v4)] = i;
73+
}
74+
75+
/* Run test program */
76+
err = bpf_prog_test_run(pkt_fd, 1, buf_in, pkt_size,
77+
buf, &size, &retval, &duration);
78+
79+
ASSERT_OK(err, "ipv4");
80+
ASSERT_EQ(retval, XDP_PASS, "ipv4 retval");
81+
ASSERT_EQ(size, pkt_size, "ipv4 size");
82+
83+
/* Make sure bpf_xdp_output() was triggered and it sent the expected
84+
* data to the perf ring buffer.
85+
*/
86+
err = perf_buffer__poll(pb, 100);
87+
88+
ASSERT_GE(err, 0, "perf_buffer__poll");
89+
ASSERT_TRUE(test_ctx.passed, "test passed");
90+
/* Verify test results */
91+
ASSERT_EQ(ftrace_skel->bss->test_result_fentry, if_nametoindex("lo"),
92+
"fentry result");
93+
ASSERT_EQ(ftrace_skel->bss->test_result_fexit, XDP_PASS, "fexit result");
94+
95+
free(buf);
96+
free(buf_in);
97+
}
98+
99+
void test_xdp_bpf2bpf(void)
100+
{
31101
int err, pkt_fd, map_fd;
32-
bool passed = false;
33-
struct iphdr iph;
34-
struct iptnl_info value4 = {.family = AF_INET};
102+
int pkt_sizes[] = {sizeof(pkt_v4), 1024, 4100, 8200};
103+
struct iptnl_info value4 = {.family = AF_INET6};
35104
struct test_xdp *pkt_skel = NULL;
36105
struct test_xdp_bpf2bpf *ftrace_skel = NULL;
37106
struct vip key4 = {.protocol = 6, .family = AF_INET};
@@ -73,32 +142,14 @@ void test_xdp_bpf2bpf(void)
73142
goto out;
74143

75144
/* Set up perf buffer */
76-
pb = perf_buffer__new(bpf_map__fd(ftrace_skel->maps.perf_buf_map), 1,
77-
on_sample, NULL, &passed, NULL);
145+
pb = perf_buffer__new(bpf_map__fd(ftrace_skel->maps.perf_buf_map), 8,
146+
on_sample, NULL, &test_ctx, NULL);
78147
if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
79148
goto out;
80149

81-
/* Run test program */
82-
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
83-
buf, &size, &retval, &duration);
84-
memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
85-
86-
ASSERT_OK(err, "ipv4");
87-
ASSERT_EQ(retval, XDP_TX, "ipv4 retval");
88-
ASSERT_EQ(size, 74, "ipv4 size");
89-
ASSERT_EQ(iph.protocol, IPPROTO_IPIP, "ipv4 proto");
90-
91-
/* Make sure bpf_xdp_output() was triggered and it sent the expected
92-
* data to the perf ring buffer.
93-
*/
94-
err = perf_buffer__poll(pb, 100);
95-
96-
ASSERT_GE(err, 0, "perf_buffer__poll");
97-
ASSERT_TRUE(passed, "test passed");
98-
/* Verify test results */
99-
ASSERT_EQ(ftrace_skel->bss->test_result_fentry, if_nametoindex("lo"),
100-
"fentry result");
101-
ASSERT_EQ(ftrace_skel->bss->test_result_fexit, XDP_TX, "fexit result");
150+
for (int i = 0; i < ARRAY_SIZE(pkt_sizes); i++)
151+
run_xdp_bpf2bpf_pkt_size(pkt_fd, pb, ftrace_skel,
152+
pkt_sizes[i]);
102153
out:
103154
perf_buffer__free(pb);
104155
test_xdp__destroy(pkt_skel);

tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
4949
void *data = (void *)(long)xdp->data;
5050

5151
meta.ifindex = xdp->rxq->dev->ifindex;
52-
meta.pkt_len = data_end - data;
52+
meta.pkt_len = bpf_xdp_get_buff_len((struct xdp_md *)xdp);
5353
bpf_xdp_output(xdp, &perf_buf_map,
5454
((__u64) meta.pkt_len << 32) |
5555
BPF_F_CURRENT_CPU,

0 commit comments

Comments
 (0)