hulk inclusion category: featrue bugzilla: https://atomgit.com/openeuler/kernel/issues/7116 -------------------------------- Add bpf_xdp_set_ingress_dev kfunc. Signed-off-by: Pu Lehui <pulehui@huawei.com> --- net/core/filter.c | 20 ++++++++++++++++++++ samples/bpf/hisock/bpf.c | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/net/core/filter.c b/net/core/filter.c index abf0c7abe20a..3c3c4bf63f2c 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -12135,6 +12135,25 @@ __bpf_kfunc int bpf_xdp_early_demux(struct xdp_md *xdp_ctx, unsigned long _sk) return 0; } +__bpf_kfunc int +bpf_xdp_set_ingress_dev(struct xdp_md *xdp_ctx, unsigned long _dev) +{ + struct net_device *dev = (struct net_device *)_dev; + struct xdp_buff *xdp = (struct xdp_buff *)xdp_ctx; + struct hisock_xdp_buff *hxdp = (void *)xdp; + struct sk_buff *skb = hxdp->skb; + + if (!skb) + return -EOPNOTSUPP; + + if (!dev || !virt_addr_valid(dev)) + return -EFAULT; + + skb->dev = dev; + skb->skb_iif = dev->ifindex; + return 0; +} + __bpf_kfunc int bpf_xdp_change_dev(struct xdp_md *xdp_ctx, u32 ifindex) { struct xdp_buff *xdp = (struct xdp_buff *)xdp_ctx; @@ -12194,6 +12213,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) #ifdef CONFIG_HISOCK BTF_ID_FLAGS(func, bpf_xdp_set_ingress_dst) BTF_ID_FLAGS(func, bpf_xdp_early_demux) +BTF_ID_FLAGS(func, bpf_xdp_set_ingress_dev) BTF_ID_FLAGS(func, bpf_xdp_change_dev) #endif BTF_SET8_END(bpf_kfunc_check_set_xdp) diff --git a/samples/bpf/hisock/bpf.c b/samples/bpf/hisock/bpf.c index 9c1d5bd9b570..fab5ea7ed0db 100644 --- a/samples/bpf/hisock/bpf.c +++ b/samples/bpf/hisock/bpf.c @@ -4,7 +4,9 @@ * * Description: End-to-End HiSock Redirect sample. */ +#define KBUILD_MODNAME "foo" #include <linux/if_vlan.h> +#include <linux/filter.h> #include <net/dst.h> #include <uapi/linux/in.h> @@ -15,6 +17,7 @@ #include <bpf/bpf_endian.h> #include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> #define IP_MF 0x2000 #define IP_OFFSET 0x1FFF @@ -35,6 +38,7 @@ struct sock_tuple { struct sock_value { unsigned long sk; + unsigned long ingress_dev; struct ethhdr ingress_eth; bool eth_updated; u32 ingress_ifindex; @@ -62,6 +66,7 @@ struct { } target_comm SEC(".maps"); int bpf_xdp_early_demux(struct xdp_md *xdp_ctx, unsigned long sk) __ksym; +int bpf_xdp_set_ingress_dev(struct xdp_md *xdp_ctx, unsigned long dev) __ksym; int bpf_skb_change_dev(struct __sk_buff *skb, u32 ifindex) __ksym; static inline bool is_speed_flow(u16 port) @@ -75,6 +80,17 @@ static inline bool is_speed_flow(u16 port) return false; } +static inline unsigned long parse_ingress_dev(struct bpf_sock_ops *skops) +{ + struct sk_buff *skb; + struct net_device *dev; + + skb = BPF_CORE_READ((struct bpf_sock_ops_kern *)skops, skb); + dev = BPF_CORE_READ(skb, dev); + + return (unsigned long)dev; +} + static void handle_listen_cb(struct bpf_sock_ops *skops) { char comm[TASK_COMM_LEN] = { 0 }; @@ -113,6 +129,7 @@ int hisock_sockops_prog(struct bpf_sock_ops *skops) key.dport = skops->local_port; val.sk = (unsigned long)skops->sk; + val.ingress_dev = parse_ingress_dev(skops); bpf_map_update_elem(&connmap, &key, &val, BPF_ANY); bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG); @@ -194,6 +211,7 @@ int hisock_ingress_prog(struct xdp_md *ctx) if (unlikely(!val->ingress_ifindex)) val->ingress_ifindex = ctx->ingress_ifindex; + bpf_xdp_set_ingress_dev(ctx, val->ingress_dev); bpf_xdp_early_demux(ctx, val->sk); return XDP_HISOCK_REDIRECT; -- 2.34.1