bpf: Add rx_queue_mapping to bpf_sock
authorAmritha Nambiar <amritha.nambiar@intel.com>
Wed, 27 May 2020 00:34:36 +0000 (17:34 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 1 Jun 2020 21:38:23 +0000 (14:38 -0700)
Add "rx_queue_mapping" to bpf_sock. This gives read access for the
existing field (sk_rx_queue_mapping) of struct sock from bpf_sock.
Semantics for the bpf_sock rx_queue_mapping access are similar to
sk_rx_queue_get(), i.e the value NO_QUEUE_MAPPING is not allowed
and -1 is returned in that case. This is useful for transmit queue
selection based on the received queue index which is cached in the
socket in the receive path.

v3: Addressed review comments to add usecase in patch description,
    and fixed default value for rx_queue_mapping.
v2: fixed build error for CONFIG_XPS wrapping, reported by
    kbuild test robot <lkp@intel.com>

Signed-off-by: Amritha Nambiar <amritha.nambiar@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/uapi/linux/bpf.h
net/core/filter.c

index 974ca6e948e38e3689b827820ab1ce0eeec0990b..630432c5c292600aa79644be944d3b3dcb160f8f 100644 (file)
@@ -3612,6 +3612,7 @@ struct bpf_sock {
        __u32 dst_ip4;
        __u32 dst_ip6[4];
        __u32 state;
+       __s32 rx_queue_mapping;
 };
 
 struct bpf_tcp_sock {
index a6fc23447f12ed59abd6c6e5df894c39f28cc0a4..0008b029d6446c35ab1cf9a4e23542147adabb99 100644 (file)
@@ -6849,6 +6849,7 @@ bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
        case offsetof(struct bpf_sock, protocol):
        case offsetof(struct bpf_sock, dst_port):
        case offsetof(struct bpf_sock, src_port):
+       case offsetof(struct bpf_sock, rx_queue_mapping):
        case bpf_ctx_range(struct bpf_sock, src_ip4):
        case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
        case bpf_ctx_range(struct bpf_sock, dst_ip4):
@@ -7897,6 +7898,23 @@ u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
                                                    skc_state),
                                       target_size));
                break;
+       case offsetof(struct bpf_sock, rx_queue_mapping):
+#ifdef CONFIG_XPS
+               *insn++ = BPF_LDX_MEM(
+                       BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
+                       si->dst_reg, si->src_reg,
+                       bpf_target_off(struct sock, sk_rx_queue_mapping,
+                                      sizeof_field(struct sock,
+                                                   sk_rx_queue_mapping),
+                                      target_size));
+               *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
+                                     1);
+               *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
+#else
+               *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
+               *target_size = 2;
+#endif
+               break;
        }
 
        return insn - insn_buf;