Skip to content

Commit b1d9fc4

Browse files
borkmanndavem330
authored andcommitted
bpf: add napi_id read access to __sk_buff
Add napi_id access to __sk_buff for socket filter program types, tc program types and other bpf_convert_ctx_access() users. Having access to skb->napi_id is useful for per RX queue listener siloing, f.e. in combination with SO_ATTACH_REUSEPORT_EBPF and when busy polling is used, meaning SO_REUSEPORT enabled listeners can then select the corresponding socket at SYN time already [1]. The skb is marked via skb_mark_napi_id() early in the receive path (e.g., napi_gro_receive()). Currently, sockets can only use SO_INCOMING_NAPI_ID from 6d43390 ("net: Introduce SO_INCOMING_NAPI_ID") as a socket option to look up the NAPI ID associated with the queue for steering, which requires a prior sk_mark_napi_id() after the socket was looked up. Semantics for the __sk_buff napi_id access are similar, meaning if skb->napi_id is < MIN_NAPI_ID (e.g. outgoing packets using sender_cpu), then an invalid napi_id of 0 is returned to the program, otherwise a valid non-zero napi_id. [1] http://netdevconf.org/2.1/slides/apr6/dumazet-BUSY-POLLING-Netdev-2.1.pdf Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 73e64fa commit b1d9fc4

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ struct __sk_buff {
603603
__u32 tc_classid;
604604
__u32 data;
605605
__u32 data_end;
606+
__u32 napi_id;
606607
};
607608

608609
struct bpf_tunnel_key {

net/core/filter.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <net/dst_metadata.h>
5454
#include <net/dst.h>
5555
#include <net/sock_reuseport.h>
56+
#include <net/busy_poll.h>
5657

5758
/**
5859
* sk_filter_trim_cap - run a packet through a socket filter
@@ -3201,6 +3202,19 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
32013202
*insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
32023203
else
32033204
*insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3205+
#endif
3206+
break;
3207+
3208+
case offsetof(struct __sk_buff, napi_id):
3209+
#if defined(CONFIG_NET_RX_BUSY_POLL)
3210+
BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, napi_id) != 4);
3211+
3212+
*insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3213+
offsetof(struct sk_buff, napi_id));
3214+
*insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3215+
*insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3216+
#else
3217+
*insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
32043218
#endif
32053219
break;
32063220
}

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ struct __sk_buff {
603603
__u32 tc_classid;
604604
__u32 data;
605605
__u32 data_end;
606+
__u32 napi_id;
606607
};
607608

608609
struct bpf_tunnel_key {

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,9 @@ static struct bpf_test tests[] = {
772772
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
773773
offsetof(struct __sk_buff, vlan_tci)),
774774
BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
775+
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
776+
offsetof(struct __sk_buff, napi_id)),
777+
BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 0, 0),
775778
BPF_EXIT_INSN(),
776779
},
777780
.result = ACCEPT,

0 commit comments

Comments
 (0)