Skip to content

Commit cf62089

Browse files
wdebruijAlexei Starovoitov
authored andcommitted
bpf: Add gso_size to __sk_buff
BPF programs may want to know whether an skb is gso. The canonical answer is skb_is_gso(skb), which tests that gso_size != 0. Expose this field in the same manner as gso_segs. That field itself is not a sufficient signal, as the comment in skb_shared_info makes clear: gso_segs may be zero, e.g., from dodgy sources. Also prepare net/bpf/test_run for upcoming BPF_PROG_TEST_RUN tests of the feature. Signed-off-by: Willem de Bruijn <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent abbc61a commit cf62089

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,6 +3176,7 @@ struct __sk_buff {
31763176
__u32 wire_len;
31773177
__u32 gso_segs;
31783178
__bpf_md_ptr(struct bpf_sock *, sk);
3179+
__u32 gso_size;
31793180
};
31803181

31813182
struct bpf_tunnel_key {

net/bpf/test_run.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
277277
/* gso_segs is allowed */
278278

279279
if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
280+
offsetof(struct __sk_buff, gso_size)))
281+
return -EINVAL;
282+
283+
/* gso_size is allowed */
284+
285+
if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_size),
280286
sizeof(struct __sk_buff)))
281287
return -EINVAL;
282288

@@ -297,6 +303,7 @@ static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
297303
if (__skb->gso_segs > GSO_MAX_SEGS)
298304
return -EINVAL;
299305
skb_shinfo(skb)->gso_segs = __skb->gso_segs;
306+
skb_shinfo(skb)->gso_size = __skb->gso_size;
300307

301308
return 0;
302309
}

net/core/filter.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7139,6 +7139,27 @@ static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
71397139
return insn - insn_buf;
71407140
}
71417141

7142+
static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
7143+
struct bpf_insn *insn)
7144+
{
7145+
/* si->dst_reg = skb_shinfo(SKB); */
7146+
#ifdef NET_SKBUFF_DATA_USES_OFFSET
7147+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7148+
BPF_REG_AX, si->src_reg,
7149+
offsetof(struct sk_buff, end));
7150+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7151+
si->dst_reg, si->src_reg,
7152+
offsetof(struct sk_buff, head));
7153+
*insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7154+
#else
7155+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7156+
si->dst_reg, si->src_reg,
7157+
offsetof(struct sk_buff, end));
7158+
#endif
7159+
7160+
return insn;
7161+
}
7162+
71427163
static u32 bpf_convert_ctx_access(enum bpf_access_type type,
71437164
const struct bpf_insn *si,
71447165
struct bpf_insn *insn_buf,
@@ -7461,26 +7482,21 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
74617482
break;
74627483

74637484
case offsetof(struct __sk_buff, gso_segs):
7464-
/* si->dst_reg = skb_shinfo(SKB); */
7465-
#ifdef NET_SKBUFF_DATA_USES_OFFSET
7466-
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7467-
BPF_REG_AX, si->src_reg,
7468-
offsetof(struct sk_buff, end));
7469-
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7470-
si->dst_reg, si->src_reg,
7471-
offsetof(struct sk_buff, head));
7472-
*insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7473-
#else
7474-
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7475-
si->dst_reg, si->src_reg,
7476-
offsetof(struct sk_buff, end));
7477-
#endif
7485+
insn = bpf_convert_shinfo_access(si, insn);
74787486
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
74797487
si->dst_reg, si->dst_reg,
74807488
bpf_target_off(struct skb_shared_info,
74817489
gso_segs, 2,
74827490
target_size));
74837491
break;
7492+
case offsetof(struct __sk_buff, gso_size):
7493+
insn = bpf_convert_shinfo_access(si, insn);
7494+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
7495+
si->dst_reg, si->dst_reg,
7496+
bpf_target_off(struct skb_shared_info,
7497+
gso_size, 2,
7498+
target_size));
7499+
break;
74847500
case offsetof(struct __sk_buff, wire_len):
74857501
BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
74867502

0 commit comments

Comments
 (0)