BPF XDP link
Subsystems (29)
v3 cover BPF XDP link
Following cgroup and netns examples, implement bpf_link support for XDP.
The semantics is described in patch #2. Program and link attachments are mutually exclusive, in the sense that neither link can replace attached program nor program can replace attached link. Link can't replace attached link as well, as is the case for any other bpf_link implementation.
Patch #1 refactors existing BPF program-based attachment API and centralizes high-level query/attach decisions in generic kernel code, while drivers are kept simple and are instructed with low-level decisions about attaching and detaching specific bpf_prog. This also makes QUERY command unnecessary, and patch #8 removes support for it from all kernel drivers. If that's a bad idea, we can drop that patch altogether.
With refactoring in patch #1, adding bpf_xdp_link is completely transparent to drivers, they are still functioning at the level of "effective" bpf_prog, that should be called in XDP data path.
Corresponding libbpf support for BPF XDP link is added in patch #5.
v2->v3:
- fix build when CONFIG_BPF_SYSCALL=n (kernel test robot);
v1->v2:
- fix prog refcounting bug (David);
- split dev_change_xdp_fd() changes into 2 patches (David);
- add extack messages to all user-induced errors (David).
Andrii Nakryiko (9):
bpf: make bpf_link API available indepently of CONFIG_BPF_SYSCALL bpf, xdp: maintain info on attached XDP BPF programs in net_device bpf, xdp: extract commong XDP program attachment logic bpf, xdp: add bpf_link-based XDP attachment API
bpf, xdp: implement LINK_UPDATE for BPF XDP link
bpf: implement BPF XDP link-specific introspection APIs
libbpf: add support for BPF XDP link
selftests/bpf: add BPF XDP link selftests
bpf, xdp: remove XDP_QUERY_PROG and XDP_QUERY_PROG_HW XDP commands
drivers/net/ethernet/amazon/ena/ena_netdev.c | 6 - drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 4 - .../net/ethernet/cavium/thunder/nicvf_main.c | 3 - .../net/ethernet/freescale/dpaa2/dpaa2-eth.c | 3 - drivers/net/ethernet/intel/i40e/i40e_main.c | 3 - drivers/net/ethernet/intel/ice/ice_main.c | 3 - drivers/net/ethernet/intel/ixgbe/ixgbe_main.c | 4 - .../net/ethernet/intel/ixgbevf/ixgbevf_main.c | 6 - drivers/net/ethernet/marvell/mvneta.c | 5 - .../net/ethernet/marvell/mvpp2/mvpp2_main.c | 3 - .../net/ethernet/mellanox/mlx4/en_netdev.c | 24 - .../net/ethernet/mellanox/mlx5/core/en_main.c | 18 - .../ethernet/netronome/nfp/nfp_net_common.c | 4 - .../net/ethernet/qlogic/qede/qede_filter.c | 3 - drivers/net/ethernet/sfc/efx.c | 5 - drivers/net/ethernet/socionext/netsec.c | 3 - drivers/net/ethernet/ti/cpsw_priv.c | 3 - drivers/net/hyperv/netvsc_bpf.c | 21 +- drivers/net/netdevsim/bpf.c | 4 - drivers/net/netdevsim/netdevsim.h | 2 +- drivers/net/tun.c | 15 - drivers/net/veth.c | 15 - drivers/net/virtio_net.c | 17 - drivers/net/xen-netfront.c | 21 - include/linux/bpf.h | 81 ++- include/linux/netdevice.h | 29 +- include/net/xdp.h | 2 - include/uapi/linux/bpf.h | 10 +- kernel/bpf/syscall.c | 5 + net/core/dev.c | 523 +++++++++++++----- net/core/rtnetlink.c | 5 +- net/core/xdp.c | 9 - tools/include/uapi/linux/bpf.h | 10 +- tools/lib/bpf/libbpf.c | 9 +- tools/lib/bpf/libbpf.h | 2 + tools/lib/bpf/libbpf.map | 1 + .../selftests/bpf/prog_tests/section_names.c | 2 +- .../selftests/bpf/prog_tests/xdp_link.c | 137 +++++ .../selftests/bpf/progs/test_xdp_link.c | 12 + 39 files changed, 656 insertions(+), 376 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_link.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_link.c
Signature · 1 line
2.24.1
PATCH v3 1/9 bpf: make bpf_link API available indepently of CONFIG_BPF_SYSCALL
Similarly to bpf_prog, make bpf_link and related generic API available unconditionally to make it easier to have bpf_link support in various parts of the kernel. Stub out init/prime/settle/cleanup and inc/put APIs.
--- include/linux/bpf.h | 81 ++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 26 deletions(-)
include/linux/bpf.h +55 -26
@@ -759,6 +759,32 @@ struct bpf_array_aux {struct work_struct work; }; +struct bpf_link { +atomic64_t refcnt; +u32 id; +enum bpf_link_type type; +const struct bpf_link_ops *ops; +struct bpf_prog *prog; +struct work_struct work; +}; + +struct bpf_link_ops { +void (*release)(struct bpf_link *link); +void (*dealloc)(struct bpf_link *link); +int (*update_prog)(struct bpf_link *link,struct bpf_prog *new_prog, +struct bpf_prog *old_prog); +void (*show_fdinfo)(const struct bpf_link *link,struct seq_file *seq); +int (*fill_link_info)(const struct bpf_link *link, +struct bpf_link_info *info); +}; + +struct bpf_link_primer { +struct bpf_link *link; +struct file *file; +int fd; +u32 id; +}; +struct bpf_struct_ops_value ;struct btf_type ;struct btf_member ;
@@ -1138,32 +1164,6 @@ static inline bool bpf_bypass_spec_v4(void)int bpf_map_new_fd (struct bpf_map *map,int flags);int bpf_prog_new_fd (struct bpf_prog *prog); -struct bpf_link { -atomic64_t refcnt; -u32 id; -enum bpf_link_type type; -const struct bpf_link_ops *ops; -struct bpf_prog *prog; -struct work_struct work; -}; - -struct bpf_link_primer { -struct bpf_link *link; -struct file *file; -int fd; -u32 id; -}; - -struct bpf_link_ops { -void (*release)(struct bpf_link *link); -void (*dealloc)(struct bpf_link *link); -int (*update_prog)(struct bpf_link *link,struct bpf_prog *new_prog, -struct bpf_prog *old_prog); -void (*show_fdinfo)(const struct bpf_link *link,struct seq_file *seq); -int (*fill_link_info)(const struct bpf_link *link, -struct bpf_link_info *info); -}; -void bpf_link_init (struct bpf_link *link,enum bpf_link_type type,const struct bpf_link_ops *ops,struct bpf_prog *prog);int bpf_link_prime (struct bpf_link *link,struct bpf_link_primer *primer);
@@ -1364,6 +1364,35 @@ static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages) { } +static inline void bpf_link_init (struct bpf_link *link,enum bpf_link_type type, +const struct bpf_link_ops *ops, +struct bpf_prog *prog) +{ +} + +static inline int bpf_link_prime (struct bpf_link *link, +struct bpf_link_primer *primer) +{ +return -EOPNOTSUPP ; +} + +static inline int bpf_link_settle (struct bpf_link_primer *primer) +{ +return -EOPNOTSUPP ; +} + +static inline void bpf_link_cleanup (struct bpf_link_primer *primer) +{ +} + +static inline void bpf_link_inc (struct bpf_link *link) +{ +} + +static inline void bpf_link_put (struct bpf_link *link) +{ +} +static inline int bpf_obj_get_user (const char __user *pathname,int flags) {return -EOPNOTSUPP ;
Signature · 1 line
2.24.1
PATCH v3 2/9 bpf, xdp: maintain info on attached XDP BPF programs in net_device
In reply to Andrii NakryikoInstead of delegating to drivers, maintain information about which BPF programs are attached in which XDP modes (generic/skb, driver, or hardware) locally in net_device. This effectively obsoletes XDP_QUERY_PROG command.
Such re-organization simplifies existing code already. But it also allows to further add bpf_link-based XDP attachments without drivers having to know about any of this at all, which seems like a good setup. XDP_SETUP_PROG/XDP_SETUP_PROG_HW are just low-level commands to driver to install/uninstall active BPF program. All the higher-level concerns about prog/link interaction will be contained within generic driver-agnostic logic.
All the XDP_QUERY_PROG calls to driver in dev_xdp_uninstall() were removed. It's not clear for me why dev_xdp_uninstall() were passing previous prog_flags when resetting installed programs. That seems unnecessary, plus most drivers don't populate prog_flags anyways. Having XDP_SETUP_PROG vs XDP_SETUP_PROG_HW should be enough of an indicator of what is required of driver to correctly reset active BPF program. dev_xdp_uninstall() is also generalized as an iteration over all three supported mode.
--- include/linux/netdevice.h | 17 +++- net/core/dev.c | 158 +++++++++++++++++++++----------------- net/core/rtnetlink.c | 5 +- 3 files changed, 105 insertions(+), 75 deletions(-)
include/linux/netdevice.h +15 -2
@@ -889,6 +889,17 @@ struct netlink_ext_ack;struct xdp_umem ;struct xdp_dev_bulk_queue ; +enum bpf_xdp_mode { +XDP_MODE_SKB =0 , +XDP_MODE_DRV =1 , +XDP_MODE_HW =2 , + __MAX_XDP_MODE +}; + +struct bpf_xdp_entity { +struct bpf_prog *prog; +}; +struct netdev_bpf {enum bpf_netdev_command command;union {
@@ -2142,6 +2153,9 @@ struct net_device {#endif const struct udp_tunnel_nic_info *udp_tunnel_nic_info;struct udp_tunnel_nic *udp_tunnel_nic; + +/* protected by rtnl_lock */ +struct bpf_xdp_entity xdp_state[__MAX_XDP_MODE]; };#define to_net_dev (d) container_of(d, struct net_device, dev)
@@ -3817,8 +3831,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf);int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack,int fd,int expected_fd,u32 flags); -u32 __dev_xdp_query (struct net_device *dev,bpf_op_t xdp_op, -enum bpf_netdev_command cmd); +u32 dev_xdp_prog_id (struct net_device *dev,enum bpf_xdp_mode mode);int xdp_umem_query (struct net_device *dev,u16 queue_id);int __dev_forward_skb (struct net_device *dev,struct sk_buff *skb);
net/core/dev.c +88 -70
227 lines not on this page · raw message
net/core/rtnetlink.c +2 -3
@@ -1416,13 +1416,12 @@ static u32 rtnl_xdp_prog_skb(struct net_device *dev)static u32 rtnl_xdp_prog_drv (struct net_device *dev) { -return __dev_xdp_query (dev, dev->netdev_ops->ndo_bpf,XDP_QUERY_PROG ); +return dev_xdp_prog_id (dev,XDP_MODE_DRV ); }static u32 rtnl_xdp_prog_hw (struct net_device *dev) { -return __dev_xdp_query (dev, dev->netdev_ops->ndo_bpf, -XDP_QUERY_PROG_HW ); +return dev_xdp_prog_id (dev,XDP_MODE_HW ); } static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
Signature · 1 line
2.24.1
Re: [PATCH v3 bpf-next 2/9] bpf, xdp: maintain info on attached XDP BPF programs in net_device
Quoted 19 lines · Andrii Nakryiko
Instead of delegating to drivers, maintain information about which BPF programs are attached in which XDP modes (generic/skb, driver, or hardware) locally in
net_device. This effectively obsoletesXDP_QUERY_PROGcommand.Such re-organization simplifies existing code already. But it also allows to further add bpf_link-based XDP attachments without drivers having to know about any of this at all, which seems like a good setup. XDP_SETUP_PROG/XDP_SETUP_PROG_HW are just low-level commands to driver to install/uninstall active BPF program. All the higher-level concerns about prog/link interaction will be contained within generic driver-agnostic logic.
All the
XDP_QUERY_PROGcalls to driver indev_xdp_uninstall()were removed. It's not clear for me whydev_xdp_uninstall()were passing previousprog_flagswhen resetting installed programs. That seems unnecessary, plus most drivers don't populateprog_flagsanyways. HavingXDP_SETUP_PROGvsXDP_SETUP_PROG_HWshould be enough of an indicator of what is required of driver to correctly reset active BPF program.dev_xdp_uninstall()is also generalized as an iteration over all three supported mode.
Quoted 4 lines
Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/netdevice.h | 17 +++- net/core/dev.c | 158 +++++++++++++++++++++-----------------
Similar to my comment on a v1 patch, this change is doing multiple things that really should be split into 2 patches - one moving code around and the second making the change you want. As is the patch is difficult to properly review.
Given that you need a v4 anyways, can you split this patch into 2?
Re: [PATCH v3 bpf-next 2/9] bpf, xdp: maintain info on attached XDP BPF programs in net_device
Quoted 26 lines · David Ahern
Quoted 19 lines · Andrii Nakryiko
Instead of delegating to drivers, maintain information about which BPF programs are attached in which XDP modes (generic/skb, driver, or hardware) locally in
net_device. This effectively obsoletesXDP_QUERY_PROGcommand.Such re-organization simplifies existing code already. But it also allows to further add bpf_link-based XDP attachments without drivers having to know about any of this at all, which seems like a good setup. XDP_SETUP_PROG/XDP_SETUP_PROG_HW are just low-level commands to driver to install/uninstall active BPF program. All the higher-level concerns about prog/link interaction will be contained within generic driver-agnostic logic.
All the
XDP_QUERY_PROGcalls to driver indev_xdp_uninstall()were removed. It's not clear for me whydev_xdp_uninstall()were passing previousprog_flagswhen resetting installed programs. That seems unnecessary, plus most drivers don't populateprog_flagsanyways. HavingXDP_SETUP_PROGvsXDP_SETUP_PROG_HWshould be enough of an indicator of what is required of driver to correctly reset active BPF program.dev_xdp_uninstall()is also generalized as an iteration over all three supported mode.Quoted 4 lines
Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/netdevice.h | 17 +++- net/core/dev.c | 158 +++++++++++++++++++++-----------------
Quoted 5 lines
Similar to my comment on a v1 patch, this change is doing multiple things that really should be split into 2 patches - one moving code around and the second making the change you want. As is the patch is difficult to properly review.
You mean xdp_uninstall? In patch 1 leave it as three separate sections, but switch to different querying. And then in a separate patch do a loop?
Alright, I'll split that up as well. But otherwise I don't really see much more opportunities to split it.
Quoted 1 line
Given that you need a v4 anyways, can you split this patch into 2?
Re: [PATCH v3 bpf-next 2/9] bpf, xdp: maintain info on attached XDP BPF programs in net_device
Quoted 34 lines · Andrii Nakryiko
Quoted 26 lines · David Ahern
Quoted 19 lines · Andrii Nakryiko
Instead of delegating to drivers, maintain information about which BPF programs are attached in which XDP modes (generic/skb, driver, or hardware) locally in
net_device. This effectively obsoletesXDP_QUERY_PROGcommand.Such re-organization simplifies existing code already. But it also allows to further add bpf_link-based XDP attachments without drivers having to know about any of this at all, which seems like a good setup. XDP_SETUP_PROG/XDP_SETUP_PROG_HW are just low-level commands to driver to install/uninstall active BPF program. All the higher-level concerns about prog/link interaction will be contained within generic driver-agnostic logic.
All the
XDP_QUERY_PROGcalls to driver indev_xdp_uninstall()were removed. It's not clear for me whydev_xdp_uninstall()were passing previousprog_flagswhen resetting installed programs. That seems unnecessary, plus most drivers don't populateprog_flagsanyways. HavingXDP_SETUP_PROGvsXDP_SETUP_PROG_HWshould be enough of an indicator of what is required of driver to correctly reset active BPF program.dev_xdp_uninstall()is also generalized as an iteration over all three supported mode.Quoted 4 lines
Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/netdevice.h | 17 +++- net/core/dev.c | 158 +++++++++++++++++++++-----------------Quoted 5 lines
Similar to my comment on a v1 patch, this change is doing multiple things that really should be split into 2 patches - one moving code around and the second making the change you want. As is the patch is difficult to properly review.
Quoted 6 lines
You mean
xdp_uninstall? In patch 1 leave it as three separate sections, but switch to different querying. And then in a separate patch do a loop?Alright, I'll split that up as well. But otherwise I don't really see much more opportunities to split it.
So I ended up not doing that. Given dev_xdp_uninstall() is just 15 lines of code, half of which are trivial, it just doesn't make sense to split dev_xdp_uninstall() refactor into two phases.
Quoted 2 lines
Quoted 2 lines
Given that you need a v4 anyways, can you split this patch into 2?
PATCH v3 3/9 bpf, xdp: extract commong XDP program attachment logic
In reply to Andrii NakryikoFurther refactor XDP attachment code. dev_change_xdp_fd() is split into two
parts: getting bpf_progs from FDs and attachment logic, working with bpf_progs. This makes attachment logic a bit more straightforward and prepares code for bpf_xdp_link inclusion, which will share the common logic.
--- net/core/dev.c | 158 ++++++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 69 deletions(-)
net/core/dev.c +89 -69
@@ -8813,104 +8813,124 @@ static void dev_xdp_uninstall(struct net_device *dev) } } -/** -* dev_change_xdp_fd - set or clear a bpf program for a device rx path -* @dev: device -* @extack: netlink extended ack -* @fd: new program fd or negative value to clear -* @expected_fd: old program fd that userspace expects to replace or clear -* @flags: xdp-related flags -* -* Set or clear a bpf program for a device -*/ -int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack, -int fd,int expected_fd,u32 flags) +static int dev_xdp_attach (struct net_device *dev,struct netlink_ext_ack *extack, +struct bpf_prog *new_prog,struct bpf_prog *old_prog, +u32 flags) { -const struct net_device_ops *ops = dev->netdev_ops; -enum bpf_xdp_mode mode =dev_xdp_mode (flags); -bool offload = mode ==XDP_MODE_HW ; -u32 prog_id, expected_id =0 ; -struct bpf_prog *prog; +struct bpf_prog *cur_prog; +enum bpf_xdp_mode mode;bpf_op_t bpf_op;int err;ASSERT_RTNL (); - bpf_op =dev_xdp_bpf_op (dev, mode); -if (!bpf_op) { -NL_SET_ERR_MSG (extack,"underlying driver does not support XDP in native mode" ); -return -EOPNOTSUPP ; +/* just one XDP mode bit should be set, zero defaults to SKB mode */ +if (hweight32 (flags &XDP_FLAGS_MODES ) >1 ) { +NL_SET_ERR_MSG (extack,"Only one XDP mode flag can be set" ); +return -EINVAL ; + } +/* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ +if (old_prog && !(flags &XDP_FLAGS_REPLACE )) { +NL_SET_ERR_MSG (extack,"XDP_FLAGS_REPLACE is not specified" ); +return -EINVAL ; } - prog_id =dev_xdp_prog_id (dev, mode); -if (flags &XDP_FLAGS_REPLACE ) { -if (expected_fd >=0 ) { - prog =bpf_prog_get_type_dev (expected_fd, -BPF_PROG_TYPE_XDP , - bpf_op == ops->ndo_bpf); -if (IS_ERR (prog)) -return PTR_ERR (prog); - expected_id = prog->aux->id; -bpf_prog_put (prog); - } - -if (prog_id != expected_id) { -NL_SET_ERR_MSG (extack,"Active program does not match expected" ); -return -EEXIST ; - } + mode =dev_xdp_mode (flags); + cur_prog =dev_xdp_prog (dev, mode); +if ((flags &XDP_FLAGS_REPLACE ) && cur_prog != old_prog) { +NL_SET_ERR_MSG (extack,"Active program does not match expected" ); +return -EEXIST ; } -if (fd >=0 ) { +if ((flags &XDP_FLAGS_UPDATE_IF_NOEXIST ) && cur_prog) { +NL_SET_ERR_MSG (extack,"XDP program already attached" ); +return -EBUSY ; + } + +if (new_prog) { +bool offload = mode ==XDP_MODE_HW ;enum bpf_xdp_mode other_mode = mode ==XDP_MODE_SKB ?XDP_MODE_DRV :XDP_MODE_SKB ; -if (!offload &&dev_xdp_prog_id (dev, other_mode)) { +if (!offload &&dev_xdp_prog (dev, other_mode)) {NL_SET_ERR_MSG (extack,"Native and generic XDP can't be active at the same time" );return -EEXIST ; } - -if ((flags &XDP_FLAGS_UPDATE_IF_NOEXIST ) && prog_id) { -NL_SET_ERR_MSG (extack,"XDP program already attached" ); -return -EBUSY ; - } - - prog =bpf_prog_get_type_dev (fd,BPF_PROG_TYPE_XDP , - bpf_op == ops->ndo_bpf); -if (IS_ERR (prog)) -return PTR_ERR (prog); - -if (!offload &&bpf_prog_is_dev_bound (prog->aux)) { +if (!offload &&bpf_prog_is_dev_bound (new_prog->aux)) {NL_SET_ERR_MSG (extack,"Using device-bound program without HW_MODE flag is not supported" ); -bpf_prog_put (prog);return -EINVAL ; } - -if (prog->expected_attach_type ==BPF_XDP_DEVMAP ) { +if (new_prog->expected_attach_type ==BPF_XDP_DEVMAP ) {NL_SET_ERR_MSG (extack,"BPF_XDP_DEVMAP programs can not be attached to a device" ); -bpf_prog_put (prog);return -EINVAL ; } + } -/* prog->aux->id may be 0 for orphaned device-bound progs */ -if (prog->aux->id && prog->aux->id == prog_id) { -bpf_prog_put (prog); -return 0 ; +/* don't call drivers if the effective program didn't change */ +if (new_prog != cur_prog) { + bpf_op =dev_xdp_bpf_op (dev, mode); +if (!bpf_op) { +NL_SET_ERR_MSG (extack,"Underlying driver does not support XDP in native mode" ); +return -EOPNOTSUPP ; } - }else { -if (!prog_id) -return 0 ; - prog =NULL ; - } - err =dev_xdp_install (dev, mode, bpf_op, extack, flags, prog); -if (err <0 && prog) { -bpf_prog_put (prog); -return err; + err =dev_xdp_install (dev, mode, bpf_op, extack, flags, new_prog); +if (err) +return err; } -dev_xdp_set_prog (dev, mode, prog); + +dev_xdp_set_prog (dev, mode, new_prog); +if (cur_prog) +bpf_prog_put (cur_prog);return 0 ; } +/** +* dev_change_xdp_fd - set or clear a bpf program for a device rx path +* @dev: device +* @extack: netlink extended ack +* @fd: new program fd or negative value to clear +* @expected_fd: old program fd that userspace expects to replace or clear +* @flags: xdp-related flags +* +* Set or clear a bpf program for a device +*/ +int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack, +int fd,int expected_fd,u32 flags) +{ +enum bpf_xdp_mode mode =dev_xdp_mode (flags); +struct bpf_prog *new_prog =NULL , *old_prog =NULL ; +int err; + +ASSERT_RTNL (); + +if (fd >=0 ) { + new_prog =bpf_prog_get_type_dev (fd,BPF_PROG_TYPE_XDP , + mode !=XDP_MODE_SKB ); +if (IS_ERR (new_prog)) +return PTR_ERR (new_prog); + } + +if (expected_fd >=0 ) { + old_prog =bpf_prog_get_type_dev (expected_fd,BPF_PROG_TYPE_XDP , + mode !=XDP_MODE_SKB ); +if (IS_ERR (old_prog)) { + err =PTR_ERR (old_prog); + old_prog =NULL ; + goto err_out; + } + } + + err =dev_xdp_attach (dev, extack, new_prog, old_prog, flags); + +err_out: +if (err && new_prog) +bpf_prog_put (new_prog); +if (old_prog) +bpf_prog_put (old_prog); +return err; +} +/** * dev_new_index - allocate an ifindex * @net: the applicable net namespace
Signature · 1 line
2.24.1
PATCH v3 4/9 bpf, xdp: add bpf_link-based XDP attachment API
In reply to Andrii NakryikoAdd bpf_link-based API (bpf_xdp_link) to attach BPF XDP program through BPF_LINK_CREATE command.
bpf_xdp_link is mutually exclusive with direct BPF program attachment, previous BPF program should be detached prior to attempting to create a new bpf_xdp_link attachment (for a given XDP mode). Once BPF link is attached, it can't be replaced by other BPF program attachment or link attachment. It will be detached only when the last BPF link FD is closed.
bpf_xdp_link will be auto-detached when net_device is shutdown, similarly to how other BPF links behave (cgroup, flow_dissector). At that point bpf_link will become defunct, but won't be destroyed until last FD is closed.
--- include/linux/netdevice.h | 4 + include/uapi/linux/bpf.h | 7 +- kernel/bpf/syscall.c | 5 ++ net/core/dev.c | 169 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 178 insertions(+), 7 deletions(-)
include/linux/netdevice.h +4 -0
@@ -888,6 +888,7 @@ struct bpf_prog_offload_ops;struct netlink_ext_ack ;struct xdp_umem ;struct xdp_dev_bulk_queue ; +struct bpf_xdp_link ;enum bpf_xdp_mode {XDP_MODE_SKB =0 ,
@@ -898,6 +899,7 @@ enum bpf_xdp_mode {struct bpf_xdp_entity {struct bpf_prog *prog; +struct bpf_xdp_link *link; };struct netdev_bpf {
@@ -3831,7 +3833,9 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf);int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack,int fd,int expected_fd,u32 flags); +int bpf_xdp_link_attach (const union bpf_attr *attr,struct bpf_prog *prog);u32 dev_xdp_prog_id (struct net_device *dev,enum bpf_xdp_mode mode); +int xdp_umem_query (struct net_device *dev,u16 queue_id);int __dev_forward_skb (struct net_device *dev,struct sk_buff *skb);
include/uapi/linux/bpf.h +6 -1
@@ -227,6 +227,7 @@ enum bpf_attach_type {BPF_CGROUP_INET6_GETSOCKNAME ,BPF_XDP_DEVMAP ,BPF_CGROUP_INET_SOCK_RELEASE , +BPF_XDP , __MAX_BPF_ATTACH_TYPE };
@@ -239,6 +240,7 @@ enum bpf_link_type {BPF_LINK_TYPE_CGROUP =3 ,BPF_LINK_TYPE_ITER =4 ,BPF_LINK_TYPE_NETNS =5 , +BPF_LINK_TYPE_XDP =6 , MAX_BPF_LINK_TYPE, };
@@ -604,7 +606,10 @@ union bpf_attr {struct {/* struct used by BPF_LINK_CREATE command */ __u32 prog_fd;/* eBPF program to attach */ -__u32 target_fd;/* object to attach to */ +union { +__u32 target_fd;/* object to attach to */ +__u32 target_ifindex;/* target ifindex */ + };__u32 attach_type;/* attach type */ __u32 flags;/* extra flags */ } link_create;
kernel/bpf/syscall.c +5 -0
@@ -2817,6 +2817,8 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)return BPF_PROG_TYPE_CGROUP_SOCKOPT ;case BPF_TRACE_ITER :return BPF_PROG_TYPE_TRACING ; +case BPF_XDP : +return BPF_PROG_TYPE_XDP ;default :return BPF_PROG_TYPE_UNSPEC ; }
@@ -3893,6 +3895,9 @@ static int link_create(union bpf_attr *attr)case BPF_PROG_TYPE_FLOW_DISSECTOR : ret =netns_bpf_link_create (attr, prog);break ; +case BPF_PROG_TYPE_XDP : + ret =bpf_xdp_link_attach (attr, prog); +break ;default : ret = -EINVAL ; }
net/core/dev.c +163 -6
246 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v3 5/9 bpf, xdp: implement LINK_UPDATE for BPF XDP link
In reply to Andrii NakryikoAdd support for LINK_UPDATE command for BPF XDP link to enable reliable replacement of underlying BPF program.
--- net/core/dev.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
net/core/dev.c +43 -0
@@ -8990,9 +8990,52 @@ static void bpf_xdp_link_dealloc(struct bpf_link *link)kfree (xdp_link); } +static int bpf_xdp_link_update (struct bpf_link *link,struct bpf_prog *new_prog, +struct bpf_prog *old_prog) +{ + struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); +enum bpf_xdp_mode mode; +bpf_op_t bpf_op; +int err =0 ; + +rtnl_lock (); + +/* link might have been auto-released already, so fail */ +if (!xdp_link->dev) { + err = -ENOLINK ; + goto out_unlock; + } + +if (old_prog && link->prog != old_prog) { + err = -EPERM ; + goto out_unlock; + } + old_prog = link->prog; +if (old_prog == new_prog) { +/* no-op, don't disturb drivers */ +bpf_prog_put (new_prog); + goto out_unlock; + } + + mode =dev_xdp_mode (xdp_link->flags); + bpf_op =dev_xdp_bpf_op (xdp_link->dev, mode); + err =dev_xdp_install (xdp_link->dev, mode, bpf_op,NULL , + xdp_link->flags, new_prog); +if (err) + goto out_unlock; + + old_prog =xchg (&link->prog, new_prog); +bpf_prog_put (old_prog); + +out_unlock: +rtnl_unlock (); +return err; +} +static const struct bpf_link_ops bpf_xdp_link_lops = { .release = bpf_xdp_link_release, .dealloc = bpf_xdp_link_dealloc, + .update_prog = bpf_xdp_link_update, };int bpf_xdp_link_attach (const union bpf_attr *attr,struct bpf_prog *prog)
Signature · 1 line
2.24.1
PATCH v3 6/9 bpf: implement BPF XDP link-specific introspection APIs
In reply to Andrii NakryikoImplement XDP link-specific show_fdinfo and link_info to emit ifindex.
--- include/uapi/linux/bpf.h | 3 +++ net/core/dev.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
include/uapi/linux/bpf.h +3 -0
@@ -3986,6 +3986,9 @@ struct bpf_link_info {__u32 netns_ino;__u32 attach_type; } netns; +struct { +__u32 ifindex; + } xdp; }; } __attribute__((aligned(8)));
net/core/dev.c +31 -0
@@ -8990,6 +8990,35 @@ static void bpf_xdp_link_dealloc(struct bpf_link *link)kfree (xdp_link); } +static void bpf_xdp_link_show_fdinfo (const struct bpf_link *link, +struct seq_file *seq) +{ + struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); +u32 ifindex =0 ; + +rtnl_lock (); +if (xdp_link->dev) + ifindex = xdp_link->dev->ifindex; +rtnl_unlock (); + +seq_printf (seq,"ifindex:\t%u\n" , ifindex); +} + +static int bpf_xdp_link_fill_link_info (const struct bpf_link *link, +struct bpf_link_info *info) +{ + struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); +u32 ifindex =0 ; + +rtnl_lock (); +if (xdp_link->dev) + ifindex = xdp_link->dev->ifindex; +rtnl_unlock (); + + info->xdp.ifindex = ifindex; +return 0 ; +} +static int bpf_xdp_link_update (struct bpf_link *link,struct bpf_prog *new_prog,struct bpf_prog *old_prog) {
@@ -9035,6 +9064,8 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,static const struct bpf_link_ops bpf_xdp_link_lops = { .release = bpf_xdp_link_release, .dealloc = bpf_xdp_link_dealloc, + .show_fdinfo = bpf_xdp_link_show_fdinfo, + .fill_link_info = bpf_xdp_link_fill_link_info, .update_prog = bpf_xdp_link_update, };
Signature · 1 line
2.24.1
PATCH v3 7/9 libbpf: add support for BPF XDP link
In reply to Andrii NakryikoSync UAPI header and add support for using bpf_link-based XDP attachment. Make xdp/ prog type set expected attach type. Kernel didn't enforce attach_type for XDP programs before, so there is no backwards compatiblity issues there.
Also fix section_names selftest to recognize that xdp prog types now have expected attach type.
--- tools/include/uapi/linux/bpf.h | 10 +++++++++- tools/lib/bpf/libbpf.c | 9 ++++++++- tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + tools/testing/selftests/bpf/prog_tests/section_names.c | 2 +- 5 files changed, 21 insertions(+), 3 deletions(-)
tools/include/uapi/linux/bpf.h +9 -1
@@ -227,6 +227,7 @@ enum bpf_attach_type {BPF_CGROUP_INET6_GETSOCKNAME ,BPF_XDP_DEVMAP ,BPF_CGROUP_INET_SOCK_RELEASE , +BPF_XDP , __MAX_BPF_ATTACH_TYPE };
@@ -239,6 +240,7 @@ enum bpf_link_type {BPF_LINK_TYPE_CGROUP =3 ,BPF_LINK_TYPE_ITER =4 ,BPF_LINK_TYPE_NETNS =5 , +BPF_LINK_TYPE_XDP =6 , MAX_BPF_LINK_TYPE, };
@@ -604,7 +606,10 @@ union bpf_attr {struct {/* struct used by BPF_LINK_CREATE command */ __u32 prog_fd;/* eBPF program to attach */ -__u32 target_fd;/* object to attach to */ +union { +__u32 target_fd;/* object to attach to */ +__u32 target_ifindex;/* target ifindex */ + };__u32 attach_type;/* attach type */ __u32 flags;/* extra flags */ } link_create;
@@ -3981,6 +3986,9 @@ struct bpf_link_info {__u32 netns_ino;__u32 attach_type; } netns; +struct { +__u32 ifindex; + } xdp; }; } __attribute__((aligned(8)));
tools/lib/bpf/libbpf.c +8 -1
@@ -6912,7 +6912,8 @@ static const struct bpf_sec_def section_defs[] = { .attach_fn = attach_iter),BPF_EAPROG_SEC ("xdp_devmap/" ,BPF_PROG_TYPE_XDP ,BPF_XDP_DEVMAP ), -BPF_PROG_SEC ("xdp" ,BPF_PROG_TYPE_XDP ), +BPF_EAPROG_SEC ("xdp" ,BPF_PROG_TYPE_XDP , +BPF_XDP ),BPF_PROG_SEC ("perf_event" ,BPF_PROG_TYPE_PERF_EVENT ),BPF_PROG_SEC ("lwt_in" ,BPF_PROG_TYPE_LWT_IN ), BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
@@ -8273,6 +8274,12 @@ bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)return bpf_program__attach_fd (prog, netns_fd,"netns" ); } +struct bpf_link *bpf_program__attach_xdp (struct bpf_program *prog,int ifindex) +{ +/* target_fd/target_ifindex use the same field in LINK_CREATE */ +return bpf_program__attach_fd (prog, ifindex,"xdp" ); +} +struct bpf_link *bpf_program__attach_iter (struct bpf_program *prog,const struct bpf_iter_attach_opts *opts)
tools/lib/bpf/libbpf.h +2 -0
@@ -257,6 +257,8 @@ LIBBPF_API struct bpf_link * bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd); LIBBPF_API struct bpf_link *bpf_program__attach_netns (struct bpf_program *prog,int netns_fd); +LIBBPF_API struct bpf_link * +bpf_program__attach_xdp (struct bpf_program *prog,int ifindex);struct bpf_map ;
tools/lib/bpf/libbpf.map +1 -0
@@ -286,6 +286,7 @@ LIBBPF_0.1.0 { bpf_map__set_value_size; bpf_map__type; bpf_map__value_size; + bpf_program__attach_xdp; bpf_program__autoload; bpf_program__set_autoload; btf__set_fd;
tools/testing/selftests/bpf/prog_tests/section_names.c +1 -1
@@ -35,7 +35,7 @@ static struct sec_name_test tests[] = { {-EINVAL, 0}, }, {"raw_tp/", {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0}, {-EINVAL, 0} }, - {"xdp", {0, BPF_PROG_TYPE_XDP, 0}, {-EINVAL, 0} }, + {"xdp", {0, BPF_PROG_TYPE_XDP, BPF_XDP}, {0, BPF_XDP} }, {"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT, 0}, {-EINVAL, 0} }, {"lwt_in", {0, BPF_PROG_TYPE_LWT_IN, 0}, {-EINVAL, 0} }, {"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT, 0}, {-EINVAL, 0} },
Signature · 1 line
2.24.1
PATCH v3 8/9 selftests/bpf: add BPF XDP link selftests
In reply to Andrii NakryikoAdd selftest validating all the attachment logic around BPF XDP link. Test also link updates and get_obj_info() APIs.
--- .../selftests/bpf/prog_tests/xdp_link.c | 137 ++++++++++++++++++ .../selftests/bpf/progs/test_xdp_link.c | 12 ++ 2 files changed, 149 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_link.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_link.c
tools/testing/selftests/bpf/prog_tests/xdp_link.c new file +137 -0
@@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#include <uapi/linux/if_link.h> +#include <test_progs.h> +#include "test_xdp_link.skel.h" + +#define IFINDEX_LO 1 + +void test_xdp_link (void ) +{ +__u32 duration =0 , id1, id2, id0 =0 , prog_fd1, prog_fd2, err; + DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, .old_fd = -1); +struct test_xdp_link *skel1 =NULL , *skel2 =NULL ; +struct bpf_link_info link_info; +struct bpf_prog_info prog_info; +struct bpf_link *link; +__u32 link_info_len =sizeof (link_info); +__u32 prog_info_len =sizeof (prog_info); + + skel1 =test_xdp_link__open_and_load (); +if (CHECK (!skel1,"skel_load" ,"skeleton open and load failed\n" )) + goto cleanup; + prog_fd1 =bpf_program__fd (skel1->progs.xdp_handler); + + skel2 =test_xdp_link__open_and_load (); +if (CHECK (!skel2,"skel_load" ,"skeleton open and load failed\n" )) + goto cleanup; + prog_fd2 =bpf_program__fd (skel2->progs.xdp_handler); + +memset (&prog_info,0 ,sizeof (prog_info)); + err =bpf_obj_get_info_by_fd (prog_fd1, &prog_info, &prog_info_len); +if (CHECK (err,"fd_info1" ,"failed %d\n" , -errno)) + goto cleanup; + id1 = prog_info.id; + +memset (&prog_info,0 ,sizeof (prog_info)); + err =bpf_obj_get_info_by_fd (prog_fd2, &prog_info, &prog_info_len); +if (CHECK (err,"fd_info2" ,"failed %d\n" , -errno)) + goto cleanup; + id2 = prog_info.id; + +/* set initial prog attachment */ + err =bpf_set_link_xdp_fd_opts (IFINDEX_LO , prog_fd1,XDP_FLAGS_REPLACE , &opts); +if (CHECK (err,"fd_attach" ,"initial prog attach failed: %d\n" , err)) + goto cleanup; + +/* validate prog ID */ + err =bpf_get_link_xdp_id (IFINDEX_LO , &id0,0 ); +CHECK (err || id0 != id1,"id1_check" , +"loaded prog id %u != id1 %u, err %d" , id0, id1, err); + +/* BPF link is not allowed to replace prog attachment */ + link =bpf_program__attach_xdp (skel1->progs.xdp_handler,IFINDEX_LO ); +if (CHECK (!IS_ERR (link),"link_attach_fail" ,"unexpected success\n" )) { +bpf_link__destroy (link); +/* best-effort detach prog */ + opts.old_fd = prog_fd1; +bpf_set_link_xdp_fd_opts (IFINDEX_LO ,-1 ,XDP_FLAGS_REPLACE , &opts); + goto cleanup; + } + +/* detach BPF program */ + opts.old_fd = prog_fd1; + err =bpf_set_link_xdp_fd_opts (IFINDEX_LO ,-1 ,XDP_FLAGS_REPLACE , &opts); +if (CHECK (err,"prog_detach" ,"failed %d\n" , err)) + goto cleanup; + +/* now BPF link should attach successfully */ + link =bpf_program__attach_xdp (skel1->progs.xdp_handler,IFINDEX_LO ); +if (CHECK (IS_ERR (link),"link_attach" ,"failed: %ld\n" ,PTR_ERR (link))) + goto cleanup; + skel1->links.xdp_handler = link; + +/* validate prog ID */ + err =bpf_get_link_xdp_id (IFINDEX_LO , &id0,0 ); +if (CHECK (err || id0 != id1,"id1_check" , +"loaded prog id %u != id1 %u, err %d" , id0, id1, err)) + goto cleanup; + +/* BPF prog attach is not allowed to replace BPF link */ + opts.old_fd = prog_fd1; + err =bpf_set_link_xdp_fd_opts (IFINDEX_LO , prog_fd2,XDP_FLAGS_REPLACE , &opts); +if (CHECK (!err,"prog_attach_fail" ,"unexpected success\n" )) + goto cleanup; + +/* Can't force-update when BPF link is active */ + err =bpf_set_link_xdp_fd (IFINDEX_LO , prog_fd2,0 ); +if (CHECK (!err,"prog_update_fail" ,"unexpected success\n" )) + goto cleanup; + +/* Can't force-detach when BPF link is active */ + err =bpf_set_link_xdp_fd (IFINDEX_LO ,-1 ,0 ); +if (CHECK (!err,"prog_detach_fail" ,"unexpected success\n" )) + goto cleanup; + +/* BPF link is not allowed to replace another BPF link */ + link =bpf_program__attach_xdp (skel2->progs.xdp_handler,IFINDEX_LO ); +if (CHECK (!IS_ERR (link),"link_attach_fail" ,"unexpected success\n" )) { +bpf_link__destroy (link); + goto cleanup; + } + +bpf_link__destroy (skel1->links.xdp_handler); + skel1->links.xdp_handler =NULL ; + +/* new link attach should succeed */ + link =bpf_program__attach_xdp (skel2->progs.xdp_handler,IFINDEX_LO ); +if (CHECK (IS_ERR (link),"link_attach" ,"failed: %ld\n" ,PTR_ERR (link))) + goto cleanup; + skel2->links.xdp_handler = link; + + err =bpf_get_link_xdp_id (IFINDEX_LO , &id0,0 ); +if (CHECK (err || id0 != id2,"id2_check" , +"loaded prog id %u != id2 %u, err %d" , id0, id1, err)) + goto cleanup; + +/* updating program under active BPF link works as expected */ + err =bpf_link__update_program (link, skel1->progs.xdp_handler); +if (CHECK (err,"link_upd" ,"failed: %d\n" , err)) + goto cleanup; + +memset (&link_info,0 ,sizeof (link_info)); + err =bpf_obj_get_info_by_fd (bpf_link__fd (link), &link_info, &link_info_len); +if (CHECK (err,"link_info" ,"failed: %d\n" , err)) + goto cleanup; + +CHECK (link_info.type !=BPF_LINK_TYPE_XDP ,"link_type" , +"got %u != exp %u\n" , link_info.type,BPF_LINK_TYPE_XDP ); +CHECK (link_info.prog_id != id1,"link_prog_id" , +"got %u != exp %u\n" , link_info.prog_id, id1); +CHECK (link_info.xdp.ifindex !=IFINDEX_LO ,"link_ifindex" , +"got %u != exp %u\n" , link_info.xdp.ifindex,IFINDEX_LO ); + +cleanup: +test_xdp_link__destroy (skel1); +test_xdp_link__destroy (skel2); +}
tools/testing/selftests/bpf/progs/test_xdp_link.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +char LICENSE[] SEC("license") = "GPL"; + +SEC("xdp/handler") +int xdp_handler (struct xdp_md *xdp) +{ +return 0 ; +}
Signature · 1 line
2.24.1
PATCH v3 9/9 bpf, xdp: remove XDP_QUERY_PROG and XDP_QUERY_PROG_HW XDP commands
In reply to Andrii NakryikoNow that BPF program/link management is centralized in generic net_device code, kernel code never queries program id from drivers, so XDP_QUERY_PROG/XDP_QUERY_PROG_HW commands are unnecessary.
This patch removes all the implementations of those commands in kernel, along the xdp_attachment_query().
This patch was compile-tested on allyesconfig.
--- drivers/net/ethernet/amazon/ena/ena_netdev.c | 6 ----- drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 4 ---- .../net/ethernet/cavium/thunder/nicvf_main.c | 3 --- .../net/ethernet/freescale/dpaa2/dpaa2-eth.c | 3 --- drivers/net/ethernet/intel/i40e/i40e_main.c | 3 --- drivers/net/ethernet/intel/ice/ice_main.c | 3 --- drivers/net/ethernet/intel/ixgbe/ixgbe_main.c | 4 ---- .../net/ethernet/intel/ixgbevf/ixgbevf_main.c | 6 ----- drivers/net/ethernet/marvell/mvneta.c | 5 ---- .../net/ethernet/marvell/mvpp2/mvpp2_main.c | 3 --- .../net/ethernet/mellanox/mlx4/en_netdev.c | 24 ------------------- .../net/ethernet/mellanox/mlx5/core/en_main.c | 18 -------------- .../ethernet/netronome/nfp/nfp_net_common.c | 4 ---- .../net/ethernet/qlogic/qede/qede_filter.c | 3 --- drivers/net/ethernet/sfc/efx.c | 5 ---- drivers/net/ethernet/socionext/netsec.c | 3 --- drivers/net/ethernet/ti/cpsw_priv.c | 3 --- drivers/net/hyperv/netvsc_bpf.c | 21 +--------------- drivers/net/netdevsim/bpf.c | 4 ---- drivers/net/netdevsim/netdevsim.h | 2 +- drivers/net/tun.c | 15 ------------ drivers/net/veth.c | 15 ------------ drivers/net/virtio_net.c | 17 ------------- drivers/net/xen-netfront.c | 21 ---------------- include/linux/netdevice.h | 8 ------- include/net/xdp.h | 2 -- net/core/dev.c | 4 ---- net/core/xdp.c | 9 ------- 28 files changed, 2 insertions(+), 216 deletions(-)
drivers/net/ethernet/amazon/ena/ena_netdev.c +0 -6
@@ -576,15 +576,9 @@ static int ena_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf)*/ static int ena_xdp (struct net_device *netdev,struct netdev_bpf *bpf) { -struct ena_adapter *adapter =netdev_priv (netdev); -switch (bpf->command) {case XDP_SETUP_PROG :return ena_xdp_set (netdev, bpf); -case XDP_QUERY_PROG : - bpf->prog_id = adapter->xdp_bpf_prog ? - adapter->xdp_bpf_prog->aux->id :0 ; -break ;default :return -EINVAL ; }
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +0 -4
@@ -330,10 +330,6 @@ int bnxt_xdp(struct net_device *dev, struct netdev_bpf *xdp)case XDP_SETUP_PROG : rc =bnxt_xdp_set (bp, xdp->prog);break ; -case XDP_QUERY_PROG : - xdp->prog_id = bp->xdp_prog ? bp->xdp_prog->aux->id :0 ; - rc =0 ; -break ;default : rc = -EINVAL ;break ;
drivers/net/ethernet/cavium/thunder/nicvf_main.c +0 -3
@@ -1906,9 +1906,6 @@ static int nicvf_xdp(struct net_device *netdev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return nicvf_xdp_setup (nic, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = nic->xdp_prog ? nic->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c +0 -3
@@ -2081,9 +2081,6 @@ static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return setup_xdp (dev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id :0 ; -break ;default :return -EINVAL ; }
drivers/net/ethernet/intel/i40e/i40e_main.c +0 -3
@@ -12923,9 +12923,6 @@ static int i40e_xdp(struct net_device *dev,switch (xdp->command) {case XDP_SETUP_PROG :return i40e_xdp_setup (vsi, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id :0 ; -return 0 ;case XDP_SETUP_XSK_UMEM :return i40e_xsk_umem_setup (vsi, xdp->xsk.umem, xdp->xsk.queue_id);
drivers/net/ethernet/intel/ice/ice_main.c +0 -3
@@ -1974,9 +1974,6 @@ static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return ice_xdp_setup_prog (vsi, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id :0 ; -return 0 ;case XDP_SETUP_XSK_UMEM :return ice_xsk_umem_setup (vsi, xdp->xsk.umem, xdp->xsk.queue_id);
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +0 -4
@@ -10190,10 +10190,6 @@ static int ixgbe_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return ixgbe_xdp_setup (dev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = adapter->xdp_prog ? - adapter->xdp_prog->aux->id :0 ; -return 0 ;case XDP_SETUP_XSK_UMEM :return ixgbe_xsk_umem_setup (adapter, xdp->xsk.umem, xdp->xsk.queue_id);
drivers/net/ethernet/intel/ixgbevf/ixgbevf_main.c +0 -6
@@ -4502,15 +4502,9 @@ static int ixgbevf_xdp_setup(struct net_device *dev, struct bpf_prog *prog)static int ixgbevf_xdp (struct net_device *dev,struct netdev_bpf *xdp) { -struct ixgbevf_adapter *adapter =netdev_priv (dev); -switch (xdp->command) {case XDP_SETUP_PROG :return ixgbevf_xdp_setup (dev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = adapter->xdp_prog ? - adapter->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/marvell/mvneta.c +0 -5
@@ -4417,14 +4417,9 @@ static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,static int mvneta_xdp (struct net_device *dev,struct netdev_bpf *xdp) { -struct mvneta_port *pp =netdev_priv (dev); -switch (xdp->command) {case XDP_SETUP_PROG :return mvneta_xdp_setup (dev, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id = pp->xdp_prog ? pp->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -3
@@ -4656,9 +4656,6 @@ static int mvpp2_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return mvpp2_xdp_setup (port, xdp); -case XDP_QUERY_PROG : - xdp->prog_id = port->xdp_prog ? port->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +0 -24
@@ -2802,35 +2802,11 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)return err; } -static u32 mlx4_xdp_query (struct net_device *dev) -{ -struct mlx4_en_priv *priv =netdev_priv (dev); -struct mlx4_en_dev *mdev = priv->mdev; -const struct bpf_prog *xdp_prog; -u32 prog_id =0 ; - -if (!priv->tx_ring_num[TX_XDP ]) -return prog_id; - -mutex_lock (&mdev->state_lock); - xdp_prog =rcu_dereference_protected ( - priv->rx_ring[0 ]->xdp_prog, -lockdep_is_held (&mdev->state_lock)); -if (xdp_prog) - prog_id = xdp_prog->aux->id; -mutex_unlock (&mdev->state_lock); - -return prog_id; -} -static int mlx4_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return mlx4_xdp_set (dev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id =mlx4_xdp_query (dev); -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +0 -18
@@ -4517,29 +4517,11 @@ static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)return err; } -static u32 mlx5e_xdp_query (struct net_device *dev) -{ -struct mlx5e_priv *priv =netdev_priv (dev); -const struct bpf_prog *xdp_prog; -u32 prog_id =0 ; - -mutex_lock (&priv->state_lock); - xdp_prog = priv->channels.params.xdp_prog; -if (xdp_prog) - prog_id = xdp_prog->aux->id; -mutex_unlock (&priv->state_lock); - -return prog_id; -} -static int mlx5e_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return mlx5e_xdp_set (dev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id =mlx5e_xdp_query (dev); -return 0 ;case XDP_SETUP_XSK_UMEM :return mlx5e_xsk_setup_umem (dev, xdp->xsk.umem, xdp->xsk.queue_id);
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +0 -4
@@ -3614,10 +3614,6 @@ static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)return nfp_net_xdp_setup_drv (nn, xdp);case XDP_SETUP_PROG_HW :return nfp_net_xdp_setup_hw (nn, xdp); -case XDP_QUERY_PROG : -return xdp_attachment_query (&nn->xdp, xdp); -case XDP_QUERY_PROG_HW : -return xdp_attachment_query (&nn->xdp_hw, xdp);default :return nfp_app_bpf (nn->app, nn, xdp); }
drivers/net/ethernet/qlogic/qede/qede_filter.c +0 -3
@@ -1045,9 +1045,6 @@ int qede_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return qede_xdp_set (edev, xdp->prog); -case XDP_QUERY_PROG : - xdp->prog_id = edev->xdp_prog ? edev->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/sfc/efx.c +0 -5
@@ -721,15 +721,10 @@ static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)static int efx_xdp (struct net_device *dev,struct netdev_bpf *xdp) {struct efx_nic *efx =netdev_priv (dev); -struct bpf_prog *xdp_prog;switch (xdp->command) {case XDP_SETUP_PROG :return efx_xdp_setup_prog (efx, xdp->prog); -case XDP_QUERY_PROG : - xdp_prog =rtnl_dereference (efx->xdp_prog); - xdp->prog_id = xdp_prog ? xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/socionext/netsec.c +0 -3
@@ -1811,9 +1811,6 @@ static int netsec_xdp(struct net_device *ndev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return netsec_xdp_setup (priv, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id :0 ; -return 0 ;default :return -EINVAL ; }
drivers/net/ethernet/ti/cpsw_priv.c +0 -3
@@ -1286,9 +1286,6 @@ int cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf)case XDP_SETUP_PROG :return cpsw_xdp_prog_setup (priv, bpf); -case XDP_QUERY_PROG : -return xdp_attachment_query (&priv->xdpi, bpf); -default :return -EINVAL ; }
drivers/net/hyperv/netvsc_bpf.c +1 -20
@@ -163,16 +163,6 @@ int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog)return ret; } -static u32 netvsc_xdp_query (struct netvsc_device *nvdev) -{ -struct bpf_prog *prog =netvsc_xdp_get (nvdev); - -if (prog) -return prog->aux->id; - -return 0 ; -} -int netvsc_bpf (struct net_device *dev,struct netdev_bpf *bpf) {struct net_device_context *ndevctx =netdev_priv (dev);
@@ -182,12 +172,7 @@ int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf)int ret;if (!nvdev || nvdev->destroy) { -if (bpf->command ==XDP_QUERY_PROG ) { - bpf->prog_id =0 ; -return 0 ;/* Query must always succeed */ - }else { -return -ENODEV ; - } +return -ENODEV ; }switch (bpf->command) {
@@ -208,10 +193,6 @@ int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf)return ret; -case XDP_QUERY_PROG : - bpf->prog_id =netvsc_xdp_query (nvdev); -return 0 ; -default :return -EINVAL ; }
drivers/net/netdevsim/bpf.c +0 -4
@@ -551,10 +551,6 @@ int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)ASSERT_RTNL ();switch (bpf->command) { -case XDP_QUERY_PROG : -return xdp_attachment_query (&ns->xdp, bpf); -case XDP_QUERY_PROG_HW : -return xdp_attachment_query (&ns->xdp_hw, bpf);case XDP_SETUP_PROG : err =nsim_setup_prog_checks (ns, bpf);if (err)
drivers/net/netdevsim/netdevsim.h +1 -1
@@ -121,7 +121,7 @@ static inline void nsim_bpf_uninit(struct netdevsim *ns)static inline int nsim_bpf (struct net_device *dev,struct netdev_bpf *bpf) { -return bpf->command ==XDP_QUERY_PROG ?0 : -EOPNOTSUPP ; +return -EOPNOTSUPP ; }static inline int nsim_bpf_disable_tc (struct netdevsim *ns)
drivers/net/tun.c +0 -15
@@ -1184,26 +1184,11 @@ static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,return 0 ; } -static u32 tun_xdp_query (struct net_device *dev) -{ -struct tun_struct *tun =netdev_priv (dev); -const struct bpf_prog *xdp_prog; - - xdp_prog =rtnl_dereference (tun->xdp_prog); -if (xdp_prog) -return xdp_prog->aux->id; - -return 0 ; -} -static int tun_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return tun_xdp_set (dev, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id =tun_xdp_query (dev); -return 0 ;default :return -EINVAL ; }
drivers/net/veth.c +0 -15
@@ -1198,26 +1198,11 @@ static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,return err; } -static u32 veth_xdp_query (struct net_device *dev) -{ -struct veth_priv *priv =netdev_priv (dev); -const struct bpf_prog *xdp_prog; - - xdp_prog = priv->_xdp_prog; -if (xdp_prog) -return xdp_prog->aux->id; - -return 0 ; -} -static int veth_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return veth_xdp_set (dev, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id =veth_xdp_query (dev); -return 0 ;default :return -EINVAL ; }
drivers/net/virtio_net.c +0 -17
@@ -2490,28 +2490,11 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,return err; } -static u32 virtnet_xdp_query (struct net_device *dev) -{ -struct virtnet_info *vi =netdev_priv (dev); -const struct bpf_prog *xdp_prog; -int i; - -for (i =0 ; i < vi->max_queue_pairs; i++) { - xdp_prog =rtnl_dereference (vi->rq[i].xdp_prog); -if (xdp_prog) -return xdp_prog->aux->id; - } -return 0 ; -} -static int virtnet_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return virtnet_xdp_set (dev, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id =virtnet_xdp_query (dev); -return 0 ;default :return -EINVAL ; }
drivers/net/xen-netfront.c +0 -21
@@ -1478,32 +1478,11 @@ static int xennet_xdp_set(struct net_device *dev, struct bpf_prog *prog,return 0 ; } -static u32 xennet_xdp_query (struct net_device *dev) -{ -unsigned int num_queues = dev->real_num_tx_queues; -struct netfront_info *np =netdev_priv (dev); -const struct bpf_prog *xdp_prog; -struct netfront_queue *queue; -unsigned int i; - -for (i =0 ; i < num_queues; ++i) { - queue = &np->queues[i]; - xdp_prog =rtnl_dereference (queue->xdp_prog); -if (xdp_prog) -return xdp_prog->aux->id; - } - -return 0 ; -} -static int xennet_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG :return xennet_xdp_set (dev, xdp->prog, xdp->extack); -case XDP_QUERY_PROG : - xdp->prog_id =xennet_xdp_query (dev); -return 0 ;default :return -EINVAL ; }
include/linux/netdevice.h +0 -8
@@ -876,8 +876,6 @@ enum bpf_netdev_command {*/ XDP_SETUP_PROG ,XDP_SETUP_PROG_HW , -XDP_QUERY_PROG , -XDP_QUERY_PROG_HW ,/* BPF program for offload callbacks, invoked at program load time. */ BPF_OFFLOAD_MAP_ALLOC , BPF_OFFLOAD_MAP_FREE,
@@ -911,12 +909,6 @@ struct netdev_bpf {struct bpf_prog *prog;struct netlink_ext_ack *extack; }; -/* XDP_QUERY_PROG, XDP_QUERY_PROG_HW */ -struct { -u32 prog_id; -/* flags with which program was installed */ -u32 prog_flags; - };/* BPF_OFFLOAD_MAP_ALLOC, BPF_OFFLOAD_MAP_FREE */ struct {struct bpf_offloaded_map *offmap;
include/net/xdp.h +0 -2
@@ -209,8 +209,6 @@ struct xdp_attachment_info { };struct netdev_bpf ; -int xdp_attachment_query (struct xdp_attachment_info *info, -struct netdev_bpf *bpf);bool xdp_attachment_flags_ok (struct xdp_attachment_info *info,struct netdev_bpf *bpf); void xdp_attachment_setup(struct xdp_attachment_info *info,
net/core/dev.c +0 -4
@@ -5466,10 +5466,6 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) }break ; -case XDP_QUERY_PROG : - xdp->prog_id = old ? old->aux->id :0 ; -break ; -default : ret = -EINVAL ;break ;
net/core/xdp.c +0 -9
@@ -400,15 +400,6 @@ void __xdp_release_frame(void *data, struct xdp_mem_info *mem) }EXPORT_SYMBOL_GPL (__xdp_release_frame); -int xdp_attachment_query (struct xdp_attachment_info *info, -struct netdev_bpf *bpf) -{ - bpf->prog_id = info->prog ? info->prog->aux->id :0 ; - bpf->prog_flags = info->prog ? info->flags :0 ; -return 0 ; -} -EXPORT_SYMBOL_GPL (xdp_attachment_query); -bool xdp_attachment_flags_ok (struct xdp_attachment_info *info,struct netdev_bpf *bpf) {
Signature · 1 line
2.24.1
Jakub Kicinski
maintainerRe: [PATCH v3 bpf-next 9/9] bpf, xdp: remove XDP_QUERY_PROG and XDP_QUERY_PROG_HW XDP commands
Quoted 10 lines · On Wed, 15 Jul 2020 21:56:01 -0700 Andrii Nakryiko wrote:
Now that BPF program/link management is centralized in generic
net_devicecode, kernel code never queries program id from drivers, so XDP_QUERY_PROG/XDP_QUERY_PROG_HW commands are unnecessary.This patch removes all the implementations of those commands in kernel, along the
xdp_attachment_query().This patch was compile-tested on allyesconfig.
Signed-off-by Andrii Nakryiko <[email protected]>
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c: In function ‘dpaa2_eth_xdp’:drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c:2079:25: warning: unused variable ‘priv’ [-Wunused-variable]
2079 | struct dpaa2_eth_priv *priv = netdev_priv(dev);
| ^~~~
Re: [PATCH v3 bpf-next 9/9] bpf, xdp: remove XDP_QUERY_PROG and XDP_QUERY_PROG_HW XDP commands
Quoted 9 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 15 Jul 2020 21:56:01 -0700 Andrii Nakryiko wrote:
Now that BPF program/link management is centralized in generic
net_devicecode, kernel code never queries program id from drivers, so XDP_QUERY_PROG/XDP_QUERY_PROG_HW commands are unnecessary.This patch removes all the implementations of those commands in kernel, along the
xdp_attachment_query().
Quoted 9 lines
Quoted 3 lines
This patch was compile-tested on allyesconfig.
Signed-off-by Andrii Nakryiko <[email protected]>
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c: In function ‘dpaa2_eth_xdp’:drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c:2079:25: warning: unused variable ‘priv’ [-Wunused-variable]
2079 | structdpaa2_eth_priv*priv =netdev_priv(dev);
| ^~~~
Oh, I've fixed a few such warnings already, but apparently missed the last one. It's hard to notice those in allyesconfig build. I've double-checked the build log with grep now, it seems like there are no more warnings anymore, thanks!