net: bpf: support direct packet access in tracing program
Subsystems (7)
PATCH 1/1 net: bpf: support direct packet access in tracing program
From: Menglong Dong <[email protected]>
For now, eBPF program of type TRACING is able to access the arguments of the function or raw_tracepoint directly, which makes data access easier and efficient. And we can also output the raw data in skb to user space in tracing program by bpf_skb_output() helper.
However, we still can't access the packet data in 'struct sk_buff' directly and have to use the helper bpf_probe_read_kernel() to analyse packet data.
Network tools, which based on eBPF TRACING, often do packet analyse works in tracing program for filtering, statistics, etc. For example, we want to trace abnormal skb free through 'kfree_skb' tracepoint with special ip address or tcp port.
In this patch, 2 helpers are introduced: bpf_skb_get_header() and bpf_skb_get_end(). The pointer returned by bpf_skb_get_header() has the same effect with the 'data' in 'struct __sk_buff', and bpf_skb_get_end() has the same effect with the 'data_end'.
Therefore, we can now access packet data in tracing program in this way:
SEC("fentry/icmp_rcv")
int BPF_PROG(tracing_open, struct sk_buff* skb)
{
void *data, *data_end;
struct ethhdr *eth;
data = bpf_skb_get_header(skb, BPF_SKB_HEADER_MAC);
data_end = bpf_skb_get_end(skb);
if (!data || !data_end)
return 0; if (data + sizeof(*eth) > data_end)
return 0;
eth = data;
bpf_printk("proto:%d\n", bpf_ntohs(eth->h_proto));
return 0;
}With any positive reply, I'll complete the selftests programs.
--- include/linux/bpf.h | 4 +++ include/uapi/linux/bpf.h | 29 ++++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++ kernel/trace/bpf_trace.c | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+)
include/linux/bpf.h +4 -0
@@ -419,6 +419,8 @@ enum bpf_return_type {RET_PTR_TO_ALLOC_MEM ,/* returns a pointer to dynamically allocated memory */ RET_PTR_TO_MEM_OR_BTF_ID ,/* returns a pointer to a valid memory or a btf_id */ RET_PTR_TO_BTF_ID ,/* returns a pointer to a btf_id */ +RET_PTR_TO_PACKET ,/* returns a pointer to packet */ +RET_PTR_TO_PACKET_END ,/* returns a pointer to skb->data + headlen */ __BPF_RET_TYPE_MAX,/* Extended ret_types. */
@@ -428,6 +430,8 @@ enum bpf_return_type {RET_PTR_TO_SOCK_COMMON_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_SOCK_COMMON ,RET_PTR_TO_ALLOC_MEM_OR_NULL =PTR_MAYBE_NULL |MEM_ALLOC |RET_PTR_TO_ALLOC_MEM ,RET_PTR_TO_BTF_ID_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_BTF_ID , +RET_PTR_TO_PACKET_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_PACKET , +RET_PTR_TO_PACKET_END_OR_NULL =PTR_MAYBE_NULL |RET_PTR_TO_PACKET_END ,/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag.
include/uapi/linux/bpf.h +29 -0
@@ -5143,6 +5143,27 @@ union bpf_attr {* The **hash_algo** is returned on success, * **-EOPNOTSUP** if the hash calculation failed or **-EINVAL** if * invalid arguments are passed. +* +* void *bpf_skb_get_header(struct sk_buff *skb, u32 flags) +* Description +* Get packet header from skb in tracing program, which can +* be access (read) directly. According to the *flags*, +* different packet header is returned: +* +* **BPF_SKB_HEADER_MAC**: get mac (L2) header +* **BPF_SKB_HEADER_NETWORK**: get network (L3) header +* **BPF_SKB_HEADER_TRANSPORT**: +* get transport (L4) header +* Return +* The pointer to packet header on success, NULL on fail. +* +* void *bpf_skb_get_end(struct sk_buff *skb) +* Description +* Get packet head end pointer from skb in tracing program, +* which is equal to *data_end* in *struct __sk_buff*. +* Return +* The pointer to packet head end on success, and NULL on +* failing. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5339,6 +5360,8 @@ union bpf_attr {FN (copy_from_user_task), \FN (skb_set_tstamp), \FN (ima_file_hash), \ +FN (skb_get_header), \ +FN (skb_get_end), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -5537,6 +5560,12 @@ enum {*/ }; +enum { +BPF_SKB_HEADER_MAC , +BPF_SKB_HEADER_NETWORK , +BPF_SKB_HEADER_TRANSPORT , +}; +/* user accessible mirror of in-kernel sk_buff. * new fields can only be added to the end of this structure */
kernel/bpf/verifier.c +6 -0
@@ -6877,6 +6877,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn*/ regs[BPF_REG_0 ].btf = btf_vmlinux; regs[BPF_REG_0 ].btf_id = ret_btf_id; + }else if (base_type (ret_type) ==RET_PTR_TO_PACKET ) { +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_PACKET | ret_flag; + }else if (base_type (ret_type) ==RET_PTR_TO_PACKET_END ) { +mark_reg_known_zero (env, regs,BPF_REG_0 ); + regs[BPF_REG_0 ].type =PTR_TO_PACKET_END | ret_flag; }else {verbose (env,"unknown return type %u of func %s#%d\n" ,base_type (ret_type),func_id_name (func_id), func_id);
kernel/trace/bpf_trace.c +58 -0
70 lines not on this page · raw message
Signature · 1 line
2.36.0
Quoted 44 lines · On Wed, Apr 27, 2022 at 12:08 AM <[email protected]> wrote:
From: Menglong Dong <[email protected]>
For now, eBPF program of type TRACING is able to access the arguments of the function or
raw_tracepointdirectly, which makes data access easier and efficient. And we can also output the raw data in skb to user space in tracing program bybpf_skb_output()helper.However, we still can't access the packet data in 'struct
sk_buff' directly and have to use the helperbpf_probe_read_kernel()to analyse packet data.Network tools, which based on eBPF TRACING, often do packet analyse works in tracing program for filtering, statistics, etc. For example, we want to trace abnormal skb free through '
kfree_skb' tracepoint with special ip address or tcp port.In this patch, 2 helpers are introduced:
bpf_skb_get_header()andbpf_skb_get_end(). The pointer returned bybpf_skb_get_header()has the same effect with the 'data' in 'struct__sk_buff', andbpf_skb_get_end()has the same effect with the 'data_end'.Therefore, we can now access packet data in tracing program in this way:
SEC("fentry/icmp_rcv") int BPF_PROG(tracing_open, struct sk_buff* skb) { void *data, *data_end; struct ethhdr *eth;data = bpf_skb_get_header(skb, BPF_SKB_HEADER_MAC); data_end = bpf_skb_get_end(skb); if (!data || !data_end) return 0; if (data + sizeof(*eth) > data_end) return 0; eth = data; bpf_printk("proto:%d\n", bpf_ntohs(eth->h_proto)); return 0;
Quoted 3 lines
}
With any positive reply, I'll complete the selftests programs.
See bpf_dynptr patches that Joanne is working on. That's an alternative mechanism for data/data_end and is going to be easier and more flexible to work with. It is the plan that once basic bpf_dynptr functionality lands, we'll have dynptr "constructors" for xdp_buff and sk_buff. I think it's a better path forward.
Quoted 11 lines
Reviewed-by Jiang Biao <[email protected]>Reviewed-by Hao Peng <[email protected]>Signed-off-by Menglong Dong <[email protected]>--- include/linux/bpf.h | 4 +++ include/uapi/linux/bpf.h | 29 ++++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++ kernel/trace/bpf_trace.c | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+)
[...]
Quoted 48 lines · Andrii Nakryiko
Quoted 46 lines · On Wed, Apr 27, 2022 at 12:08 AM <[email protected]> wrote:
From: Menglong Dong <[email protected]>
For now, eBPF program of type TRACING is able to access the arguments of the function or
raw_tracepointdirectly, which makes data access easier and efficient. And we can also output the raw data in skb to user space in tracing program bybpf_skb_output()helper.However, we still can't access the packet data in 'struct
sk_buff' directly and have to use the helperbpf_probe_read_kernel()to analyse packet data.Network tools, which based on eBPF TRACING, often do packet analyse works in tracing program for filtering, statistics, etc. For example, we want to trace abnormal skb free through '
kfree_skb' tracepoint with special ip address or tcp port.In this patch, 2 helpers are introduced:
bpf_skb_get_header()andbpf_skb_get_end(). The pointer returned bybpf_skb_get_header()has the same effect with the 'data' in 'struct__sk_buff', andbpf_skb_get_end()has the same effect with the 'data_end'.Therefore, we can now access packet data in tracing program in this way:
SEC("fentry/icmp_rcv") int BPF_PROG(tracing_open, struct sk_buff* skb) { void *data, *data_end; struct ethhdr *eth;data = bpf_skb_get_header(skb, BPF_SKB_HEADER_MAC); data_end = bpf_skb_get_end(skb); if (!data || !data_end) return 0; if (data + sizeof(*eth) > data_end) return 0; eth = data; bpf_printk("proto:%d\n", bpf_ntohs(eth->h_proto)); return 0;}
Quoted 8 lines
Quoted 1 line
With any positive reply, I'll complete the selftests programs.
See
bpf_dynptrpatches that Joanne is working on. That's an alternative mechanism for data/data_end and is going to be easier and more flexible to work with. It is the plan that once basicbpf_dynptrfunctionality lands, we'll have dynptr "constructors" forxdp_buffandsk_buff. I think it's a better path forward.
Yeah, bpf_dynptr seems to be a nice feature, which I didn't notice :/ Thanks for your explanation.
Menglong Dong
Quoted 13 lines
Quoted 11 lines
Reviewed-by Jiang Biao <[email protected]>Reviewed-by Hao Peng <[email protected]>Signed-off-by Menglong Dong <[email protected]>--- include/linux/bpf.h | 4 +++ include/uapi/linux/bpf.h | 29 ++++++++++++++++++++ kernel/bpf/verifier.c | 6 +++++ kernel/trace/bpf_trace.c | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+)[...]