Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16137b0

Browse files
Cong WangAlexei Starovoitov
authored andcommitted
bpf: Compute data_end dynamically with JIT code
Currently, we compute ->data_end with a compile-time constant offset of skb. But as Jakub pointed out, we can actually compute it in eBPF JIT code at run-time, so that we can competely get rid of ->data_end. This is similar to skb_shinfo(skb) computation in bpf_convert_shinfo_access(). Suggested-by: Jakub Sitnicki <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5a685cd commit 16137b0

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

include/net/tcp.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -886,18 +886,12 @@ struct tcp_skb_cb {
886886
struct {
887887
__u32 flags;
888888
struct sock *sk_redir;
889-
void *data_end;
890889
} bpf;
891890
};
892891
};
893892

894893
#define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
895894

896-
static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
897-
{
898-
TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
899-
}
900-
901895
static inline bool tcp_skb_bpf_ingress(const struct sk_buff *skb)
902896
{
903897
return TCP_SKB_CB(skb)->bpf.flags & BPF_F_INGRESS;

net/core/filter.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,10 +1863,7 @@ static const struct bpf_func_proto bpf_sk_fullsock_proto = {
18631863
static inline int sk_skb_try_make_writable(struct sk_buff *skb,
18641864
unsigned int write_len)
18651865
{
1866-
int err = __bpf_try_make_writable(skb, write_len);
1867-
1868-
bpf_compute_data_end_sk_skb(skb);
1869-
return err;
1866+
return __bpf_try_make_writable(skb, write_len);
18701867
}
18711868

18721869
BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
@@ -3577,7 +3574,6 @@ BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
35773574
return -ENOMEM;
35783575
__skb_pull(skb, len_diff_abs);
35793576
}
3580-
bpf_compute_data_end_sk_skb(skb);
35813577
if (tls_sw_has_ctx_rx(skb->sk)) {
35823578
struct strp_msg *rxm = strp_msg(skb);
35833579

@@ -3742,10 +3738,7 @@ static const struct bpf_func_proto bpf_skb_change_tail_proto = {
37423738
BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
37433739
u64, flags)
37443740
{
3745-
int ret = __bpf_skb_change_tail(skb, new_len, flags);
3746-
3747-
bpf_compute_data_end_sk_skb(skb);
3748-
return ret;
3741+
return __bpf_skb_change_tail(skb, new_len, flags);
37493742
}
37503743

37513744
static const struct bpf_func_proto sk_skb_change_tail_proto = {
@@ -3808,10 +3801,7 @@ static const struct bpf_func_proto bpf_skb_change_head_proto = {
38083801
BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
38093802
u64, flags)
38103803
{
3811-
int ret = __bpf_skb_change_head(skb, head_room, flags);
3812-
3813-
bpf_compute_data_end_sk_skb(skb);
3814-
return ret;
3804+
return __bpf_skb_change_head(skb, head_room, flags);
38153805
}
38163806

38173807
static const struct bpf_func_proto sk_skb_change_head_proto = {
@@ -9655,22 +9645,40 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
96559645
return insn - insn_buf;
96569646
}
96579647

9648+
/* data_end = skb->data + skb_headlen() */
9649+
static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
9650+
struct bpf_insn *insn)
9651+
{
9652+
/* si->dst_reg = skb->data */
9653+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9654+
si->dst_reg, si->src_reg,
9655+
offsetof(struct sk_buff, data));
9656+
/* AX = skb->len */
9657+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9658+
BPF_REG_AX, si->src_reg,
9659+
offsetof(struct sk_buff, len));
9660+
/* si->dst_reg = skb->data + skb->len */
9661+
*insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
9662+
/* AX = skb->data_len */
9663+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
9664+
BPF_REG_AX, si->src_reg,
9665+
offsetof(struct sk_buff, data_len));
9666+
/* si->dst_reg = skb->data + skb->len - skb->data_len */
9667+
*insn++ = BPF_ALU64_REG(BPF_SUB, si->dst_reg, BPF_REG_AX);
9668+
9669+
return insn;
9670+
}
9671+
96589672
static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
96599673
const struct bpf_insn *si,
96609674
struct bpf_insn *insn_buf,
96619675
struct bpf_prog *prog, u32 *target_size)
96629676
{
96639677
struct bpf_insn *insn = insn_buf;
9664-
int off;
96659678

96669679
switch (si->off) {
96679680
case offsetof(struct __sk_buff, data_end):
9668-
off = si->off;
9669-
off -= offsetof(struct __sk_buff, data_end);
9670-
off += offsetof(struct sk_buff, cb);
9671-
off += offsetof(struct tcp_skb_cb, bpf.data_end);
9672-
*insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9673-
si->src_reg, off);
9681+
insn = bpf_convert_data_end_access(si, insn);
96749682
break;
96759683
default:
96769684
return bpf_convert_ctx_access(type, si, insn_buf, prog,

net/core/skmsg.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,6 @@ EXPORT_SYMBOL_GPL(sk_psock_msg_verdict);
746746
static int sk_psock_bpf_run(struct sk_psock *psock, struct bpf_prog *prog,
747747
struct sk_buff *skb)
748748
{
749-
bpf_compute_data_end_sk_skb(skb);
750749
return bpf_prog_run_pin_on_cpu(prog, skb);
751750
}
752751

0 commit comments

Comments
 (0)