Skip to content

Commit d831ee8

Browse files
chaudronAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add bpf_xdp_output() helper
Introduce new helper that reuses existing xdp perf_event output implementation, but can be called from raw_tracepoint programs that receive 'struct xdp_buff *' as a tracepoint argument. Signed-off-by: Eelco Chaudron <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Toke Høiland-Jørgensen <[email protected]> Link: https://lore.kernel.org/bpf/158348514556.2239.11050972434793741444.stgit@xdp-tutorial
1 parent 4823b72 commit d831ee8

File tree

7 files changed

+148
-4
lines changed

7 files changed

+148
-4
lines changed

include/uapi/linux/bpf.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,29 @@ union bpf_attr {
29272927
*
29282928
* **-ENOENT** if pidns does not exists for the current task.
29292929
*
2930+
* int bpf_xdp_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
2931+
* Description
2932+
* Write raw *data* blob into a special BPF perf event held by
2933+
* *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf
2934+
* event must have the following attributes: **PERF_SAMPLE_RAW**
2935+
* as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and
2936+
* **PERF_COUNT_SW_BPF_OUTPUT** as **config**.
2937+
*
2938+
* The *flags* are used to indicate the index in *map* for which
2939+
* the value must be put, masked with **BPF_F_INDEX_MASK**.
2940+
* Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU**
2941+
* to indicate that the index of the current CPU core should be
2942+
* used.
2943+
*
2944+
* The value to write, of *size*, is passed through eBPF stack and
2945+
* pointed by *data*.
2946+
*
2947+
* *ctx* is a pointer to in-kernel struct xdp_buff.
2948+
*
2949+
* This helper is similar to **bpf_perf_eventoutput**\ () but
2950+
* restricted to raw_tracepoint bpf programs.
2951+
* Return
2952+
* 0 on success, or a negative error in case of failure.
29302953
*/
29312954
#define __BPF_FUNC_MAPPER(FN) \
29322955
FN(unspec), \
@@ -3049,7 +3072,8 @@ union bpf_attr {
30493072
FN(send_signal_thread), \
30503073
FN(jiffies64), \
30513074
FN(read_branch_records), \
3052-
FN(get_ns_current_pid_tgid),
3075+
FN(get_ns_current_pid_tgid), \
3076+
FN(xdp_output),
30533077

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

kernel/bpf/verifier.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -3650,7 +3650,8 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
36503650
if (func_id != BPF_FUNC_perf_event_read &&
36513651
func_id != BPF_FUNC_perf_event_output &&
36523652
func_id != BPF_FUNC_skb_output &&
3653-
func_id != BPF_FUNC_perf_event_read_value)
3653+
func_id != BPF_FUNC_perf_event_read_value &&
3654+
func_id != BPF_FUNC_xdp_output)
36543655
goto error;
36553656
break;
36563657
case BPF_MAP_TYPE_STACK_TRACE:
@@ -3740,6 +3741,7 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
37403741
case BPF_FUNC_perf_event_output:
37413742
case BPF_FUNC_perf_event_read_value:
37423743
case BPF_FUNC_skb_output:
3744+
case BPF_FUNC_xdp_output:
37433745
if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
37443746
goto error;
37453747
break;

kernel/trace/bpf_trace.c

+3
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = {
11451145
};
11461146

11471147
extern const struct bpf_func_proto bpf_skb_output_proto;
1148+
extern const struct bpf_func_proto bpf_xdp_output_proto;
11481149

11491150
BPF_CALL_3(bpf_get_stackid_raw_tp, struct bpf_raw_tracepoint_args *, args,
11501151
struct bpf_map *, map, u64, flags)
@@ -1220,6 +1221,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12201221
#ifdef CONFIG_NET
12211222
case BPF_FUNC_skb_output:
12221223
return &bpf_skb_output_proto;
1224+
case BPF_FUNC_xdp_output:
1225+
return &bpf_xdp_output_proto;
12231226
#endif
12241227
default:
12251228
return raw_tp_prog_func_proto(func_id, prog);

net/core/filter.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -4061,7 +4061,8 @@ BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
40614061

40624062
if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
40634063
return -EINVAL;
4064-
if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4064+
if (unlikely(!xdp ||
4065+
xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
40654066
return -EFAULT;
40664067

40674068
return bpf_event_output(map, flags, meta, meta_size, xdp->data,
@@ -4079,6 +4080,19 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = {
40794080
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
40804081
};
40814082

4083+
static int bpf_xdp_output_btf_ids[5];
4084+
const struct bpf_func_proto bpf_xdp_output_proto = {
4085+
.func = bpf_xdp_event_output,
4086+
.gpl_only = true,
4087+
.ret_type = RET_INTEGER,
4088+
.arg1_type = ARG_PTR_TO_BTF_ID,
4089+
.arg2_type = ARG_CONST_MAP_PTR,
4090+
.arg3_type = ARG_ANYTHING,
4091+
.arg4_type = ARG_PTR_TO_MEM,
4092+
.arg5_type = ARG_CONST_SIZE_OR_ZERO,
4093+
.btf_id = bpf_xdp_output_btf_ids,
4094+
};
4095+
40824096
BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
40834097
{
40844098
return skb->sk ? sock_gen_cookie(skb->sk) : 0;

tools/include/uapi/linux/bpf.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,29 @@ union bpf_attr {
29272927
*
29282928
* **-ENOENT** if pidns does not exists for the current task.
29292929
*
2930+
* int bpf_xdp_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
2931+
* Description
2932+
* Write raw *data* blob into a special BPF perf event held by
2933+
* *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf
2934+
* event must have the following attributes: **PERF_SAMPLE_RAW**
2935+
* as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and
2936+
* **PERF_COUNT_SW_BPF_OUTPUT** as **config**.
2937+
*
2938+
* The *flags* are used to indicate the index in *map* for which
2939+
* the value must be put, masked with **BPF_F_INDEX_MASK**.
2940+
* Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU**
2941+
* to indicate that the index of the current CPU core should be
2942+
* used.
2943+
*
2944+
* The value to write, of *size*, is passed through eBPF stack and
2945+
* pointed by *data*.
2946+
*
2947+
* *ctx* is a pointer to in-kernel struct xdp_buff.
2948+
*
2949+
* This helper is similar to **bpf_perf_eventoutput**\ () but
2950+
* restricted to raw_tracepoint bpf programs.
2951+
* Return
2952+
* 0 on success, or a negative error in case of failure.
29302953
*/
29312954
#define __BPF_FUNC_MAPPER(FN) \
29322955
FN(unspec), \
@@ -3049,7 +3072,8 @@ union bpf_attr {
30493072
FN(send_signal_thread), \
30503073
FN(jiffies64), \
30513074
FN(read_branch_records), \
3052-
FN(get_ns_current_pid_tgid),
3075+
FN(get_ns_current_pid_tgid), \
3076+
FN(xdp_output),
30533077

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

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

+53
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,51 @@
44
#include "test_xdp.skel.h"
55
#include "test_xdp_bpf2bpf.skel.h"
66

7+
struct meta {
8+
int ifindex;
9+
int pkt_len;
10+
};
11+
12+
static void on_sample(void *ctx, int cpu, void *data, __u32 size)
13+
{
14+
int duration = 0;
15+
struct meta *meta = (struct meta *)data;
16+
struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta);
17+
18+
if (CHECK(size < sizeof(pkt_v4) + sizeof(*meta),
19+
"check_size", "size %u < %zu\n",
20+
size, sizeof(pkt_v4) + sizeof(*meta)))
21+
return;
22+
23+
if (CHECK(meta->ifindex != if_nametoindex("lo"), "check_meta_ifindex",
24+
"meta->ifindex = %d\n", meta->ifindex))
25+
return;
26+
27+
if (CHECK(meta->pkt_len != sizeof(pkt_v4), "check_meta_pkt_len",
28+
"meta->pkt_len = %zd\n", sizeof(pkt_v4)))
29+
return;
30+
31+
if (CHECK(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)),
32+
"check_packet_content", "content not the same\n"))
33+
return;
34+
35+
*(bool *)ctx = true;
36+
}
37+
738
void test_xdp_bpf2bpf(void)
839
{
940
__u32 duration = 0, retval, size;
1041
char buf[128];
1142
int err, pkt_fd, map_fd;
43+
bool passed = false;
1244
struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
1345
struct iptnl_info value4 = {.family = AF_INET};
1446
struct test_xdp *pkt_skel = NULL;
1547
struct test_xdp_bpf2bpf *ftrace_skel = NULL;
1648
struct vip key4 = {.protocol = 6, .family = AF_INET};
1749
struct bpf_program *prog;
50+
struct perf_buffer *pb = NULL;
51+
struct perf_buffer_opts pb_opts = {};
1852

1953
/* Load XDP program to introspect */
2054
pkt_skel = test_xdp__open_and_load();
@@ -50,6 +84,14 @@ void test_xdp_bpf2bpf(void)
5084
if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
5185
goto out;
5286

87+
/* Set up perf buffer */
88+
pb_opts.sample_cb = on_sample;
89+
pb_opts.ctx = &passed;
90+
pb = perf_buffer__new(bpf_map__fd(ftrace_skel->maps.perf_buf_map),
91+
1, &pb_opts);
92+
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb)))
93+
goto out;
94+
5395
/* Run test program */
5496
err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
5597
buf, &size, &retval, &duration);
@@ -60,6 +102,15 @@ void test_xdp_bpf2bpf(void)
60102
err, errno, retval, size))
61103
goto out;
62104

105+
/* Make sure bpf_xdp_output() was triggered and it sent the expected
106+
* data to the perf ring buffer.
107+
*/
108+
err = perf_buffer__poll(pb, 100);
109+
if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
110+
goto out;
111+
112+
CHECK_FAIL(!passed);
113+
63114
/* Verify test results */
64115
if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
65116
"result", "fentry failed err %llu\n",
@@ -70,6 +121,8 @@ void test_xdp_bpf2bpf(void)
70121
"fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
71122

72123
out:
124+
if (pb)
125+
perf_buffer__free(pb);
73126
test_xdp__destroy(pkt_skel);
74127
test_xdp_bpf2bpf__destroy(ftrace_skel);
75128
}

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

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <bpf/bpf_tracing.h>
44
#include <bpf/bpf_helpers.h>
55

6+
char _license[] SEC("license") = "GPL";
7+
68
struct net_device {
79
/* Structure does not need to contain all entries,
810
* as "preserve_access_index" will use BTF to fix this...
@@ -27,10 +29,32 @@ struct xdp_buff {
2729
struct xdp_rxq_info *rxq;
2830
} __attribute__((preserve_access_index));
2931

32+
struct meta {
33+
int ifindex;
34+
int pkt_len;
35+
};
36+
37+
struct {
38+
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
39+
__uint(key_size, sizeof(int));
40+
__uint(value_size, sizeof(int));
41+
} perf_buf_map SEC(".maps");
42+
3043
__u64 test_result_fentry = 0;
3144
SEC("fentry/FUNC")
3245
int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
3346
{
47+
struct meta meta;
48+
void *data_end = (void *)(long)xdp->data_end;
49+
void *data = (void *)(long)xdp->data;
50+
51+
meta.ifindex = xdp->rxq->dev->ifindex;
52+
meta.pkt_len = data_end - data;
53+
bpf_xdp_output(xdp, &perf_buf_map,
54+
((__u64) meta.pkt_len << 32) |
55+
BPF_F_CURRENT_CPU,
56+
&meta, sizeof(meta));
57+
3458
test_result_fentry = xdp->rxq->dev->ifindex;
3559
return 0;
3660
}

0 commit comments

Comments
 (0)