Add bpf programmable net device
Subsystems (9)
Daniel Borkmann
maintainerv3 cover Add bpf programmable net device
This work adds a BPF programmable device which can operate in L3 or L2 mode where the BPF program is part of the xmit routine. It's program management is done via bpf_mprog and it comes with BPF link support. For details see patch 1 and following. Thanks!
v2 -> v3:
- Remove setting dev->min_mtu to ETH_MIN_MTU (Andrew)
- Do not populate ethtool info->version (Andrew)
- Populate netdev private data before register_netdevice (Andrew)
- Use strscpy for ifname template (Jakub)
- Use GFP_KERNEL_ACCOUNT for link kzalloc (Jakub)
- Carry and dump link attach type for bpftool (Toke)
v1 -> v2:
- Rename from meta (Toke, Andrii, Alexei)
- Reuse skb_scrub_packet (Stan)
- Remove IFF_META and use netdev_ops (Toke)
- Add comment to multicast handler (Toke)
- Remove silly version info (Toke)
- Fix attach_type_name (Quentin)
- Rework libbpf link attach api to be similar
as tcx (Andrii)
- Move flags last for bpf_netkit_opts (Andrii)
- Rebased to bpf_mprog query api changes
- Folded link support patch into main one
Daniel Borkmann (7):
netkit, bpf: Add bpf programmable net device
tools: Sync if_link uapi header
libbpf: Add link-based API for netkit
bpftool: Implement link show support for netkit
bpftool: Extend net dump with netkit progs
selftests/bpf: Add netlink helper library
selftests/bpf: Add selftests for netkit
MAINTAINERS | 9 + drivers/net/Kconfig | 9 + drivers/net/Makefile | 1 + drivers/net/netkit.c | 934 ++++++++++++++++++ include/net/netkit.h | 38 + include/uapi/linux/bpf.h | 14 + include/uapi/linux/if_link.h | 24 + kernel/bpf/syscall.c | 30 +- .../bpf/bpftool/Documentation/bpftool-net.rst | 8 +- tools/bpf/bpftool/link.c | 9 + tools/bpf/bpftool/net.c | 7 +- tools/include/uapi/linux/bpf.h | 14 + tools/include/uapi/linux/if_link.h | 141 +++ tools/lib/bpf/bpf.c | 16 + tools/lib/bpf/bpf.h | 5 + tools/lib/bpf/libbpf.c | 39 + tools/lib/bpf/libbpf.h | 15 + tools/lib/bpf/libbpf.map | 1 + tools/testing/selftests/bpf/Makefile | 19 +- tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/netlink_helpers.c | 358 +++++++ tools/testing/selftests/bpf/netlink_helpers.h | 46 + .../selftests/bpf/prog_tests/tc_helpers.h | 4 + .../selftests/bpf/prog_tests/tc_netkit.c | 687 +++++++++++++ .../selftests/bpf/progs/test_tc_link.c | 13 + 25 files changed, 2427 insertions(+), 15 deletions(-) create mode 100644 drivers/net/netkit.c create mode 100644 include/net/netkit.h create mode 100644 tools/testing/selftests/bpf/netlink_helpers.c create mode 100644 tools/testing/selftests/bpf/netlink_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_netkit.c
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v3 1/7 netkit, bpf: Add bpf programmable net device
This work adds a new, minimal BPF-programmable device called "netkit" (former PoC code-name "meta") we recently presented at LSF/MM/BPF. The core idea is that BPF programs are executed within the drivers xmit routine and therefore e.g. in case of containers/Pods moving BPF processing closer to the source.
One of the goals was that in case of Pod egress traffic, this allows to move BPF programs from hostns tcx ingress into the device itself, providing earlier drop or forward mechanisms, for example, if the BPF program determines that the skb must be sent out of the node, then a redirect to the physical device can take place directly without going through per-CPU backlog queue. This helps to shift processing for such traffic from softirq to process context, leading to better scheduling decisions/performance (see measurements in the slides).
In this initial version, the netkit device ships as a pair, but we plan to extend this further so it can also operate in single device mode. The pair comes with a primary and a peer device. Only the primary device, typically residing in hostns, can manage BPF programs for itself and its peer. The peer device is designated for containers/Pods and cannot attach/detach BPF programs. Upon the device creation, the user can set the default policy to 'forward' or 'drop' for the case when no BPF program is attached.
Additionally, the device can be operated in L3 (default) or L2 mode. The management of BPF programs is done via bpf_mprog, so that multi-attach is supported right from the beginning with similar API and dependency controls as tcx. For details on the latter see commit 053c8e1f235d ("bpf: Add generic attach/detach/query API for multi-progs"). tc BPF compatibility is provided, so that existing programs can be easily migrated.
Going forward, we plan to use netkit devices in Cilium as the main device type for connecting Pods. They will be operated in L3 mode in order to simplify a Pod's neighbor management and the peer will operate in default drop mode, so that no traffic is leaving between the time when a Pod is brought up by the CNI plugin and programs attached by the agent. Additionally, the programs we attach via tcx on the physical devices are using bpf_redirect_peer() for inbound traffic into netkit device, hence the latter is also supporting the ndo_get_peer_dev callback. Similarly, we use bpf_redirect_neigh() for the way out, pushing from netkit peer to phys device directly. Also, BIG TCP is supported on netkit device. For the follow-up work in single device mode, we plan to convert Cilium's cilium_host/_net devices into a single one.
An extensive test suite for checking device operations and the BPF program and link management API comes as BPF selftests in this series.
--- MAINTAINERS | 9 + drivers/net/Kconfig | 9 + drivers/net/Makefile | 1 + drivers/net/netkit.c | 934 +++++++++++++++++++++++++++++++++ include/net/netkit.h | 38 ++ include/uapi/linux/bpf.h | 14 + include/uapi/linux/if_link.h | 24 + kernel/bpf/syscall.c | 30 +- tools/include/uapi/linux/bpf.h | 14 + 9 files changed, 1068 insertions(+), 5 deletions(-) create mode 100644 drivers/net/netkit.c create mode 100644 include/net/netkit.h
MAINTAINERS +9 -0
@@ -3795,6 +3795,15 @@ L: [email protected] S: Odd Fixes K: (?:\b|_)bpf(?:\b|_) +BPF [NETKIT] (BPF-programmable network device) +M: Daniel Borkmann <[email protected]> +M: Nikolay Aleksandrov <[email protected]> +L: [email protected] +L: [email protected] +S: Supported +F: drivers/net/netkit.c +F: include/net/netkit.h + BPF [NETWORKING] (struct_ops, reuseport) M: Martin KaFai Lau <[email protected]> L: [email protected]
drivers/net/Kconfig +9 -0
@@ -448,6 +448,15 @@ config NLMON diagnostics, etc. This is mostly intended for developers or support to debug netlink issues. If unsure, say N. +config NETKIT + bool "BPF-programmable network device" + depends on BPF_SYSCALL + help + The netkit device is a virtual networking device where BPF programs + can be attached to the device(s) transmission routine in order to + implement the driver's internal logic. The device can be configured + to operate in L3 or L2 mode. If unsure, say N. + config NET_VRF tristate "Virtual Routing and Forwarding (Lite)" depends on IP_MULTIPLE_TABLES
drivers/net/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_MDIO) += mdio.o obj-$(CONFIG_NET) += loopback.o obj-$(CONFIG_NETDEV_LEGACY_INIT) += Space.o obj-$(CONFIG_NETCONSOLE) += netconsole.o +obj-$(CONFIG_NETKIT) += netkit.o obj-y += phy/ obj-y += pse-pd/ obj-y += mdio/
drivers/net/netkit.c new file +934 -0
934 lines not on this page · raw message
include/net/netkit.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2023 Isovalent */ +#ifndef __NET_NETKIT_H +#define __NET_NETKIT_H + +#include <linux/bpf.h> + +#ifdef CONFIG_NETKIT +int netkit_prog_attach (const union bpf_attr *attr,struct bpf_prog *prog); +int netkit_link_attach (const union bpf_attr *attr,struct bpf_prog *prog); +int netkit_prog_detach (const union bpf_attr *attr,struct bpf_prog *prog); +int netkit_prog_query (const union bpf_attr *attr,union bpf_attr __user *uattr); +#else +static inline int netkit_prog_attach (const union bpf_attr *attr, +struct bpf_prog *prog) +{ +return -EINVAL ; +} + +static inline int netkit_link_attach (const union bpf_attr *attr, +struct bpf_prog *prog) +{ +return -EINVAL ; +} + +static inline int netkit_prog_detach (const union bpf_attr *attr, +struct bpf_prog *prog) +{ +return -EINVAL ; +} + +static inline int netkit_prog_query (const union bpf_attr *attr, +union bpf_attr __user *uattr) +{ +return -EINVAL ; +} +#endif /* CONFIG_NETKIT */ +#endif /* __NET_NETKIT_H */
include/uapi/linux/bpf.h +14 -0
@@ -1052,6 +1052,8 @@ enum bpf_attach_type {BPF_CGROUP_UNIX_RECVMSG ,BPF_CGROUP_UNIX_GETPEERNAME ,BPF_CGROUP_UNIX_GETSOCKNAME , +BPF_NETKIT_PRIMARY , +BPF_NETKIT_PEER , __MAX_BPF_ATTACH_TYPE };
@@ -1071,6 +1073,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETFILTER =10 ,BPF_LINK_TYPE_TCX =11 ,BPF_LINK_TYPE_UPROBE_MULTI =12 , +BPF_LINK_TYPE_NETKIT =13 , MAX_BPF_LINK_TYPE, };
@@ -1656,6 +1659,13 @@ union bpf_attr {__u32 flags;__u32 pid; } uprobe_multi; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } netkit; }; } link_create;
@@ -6576,6 +6586,10 @@ struct bpf_link_info {__u32 ifindex;__u32 attach_type; } tcx; +struct { +__u32 ifindex; +__u32 attach_type; + } netkit; }; } __attribute__((aligned(8)));
include/uapi/linux/if_link.h +24 -0
@@ -756,6 +756,30 @@ struct tunnel_msg {__u32 ifindex; }; +/* netkit section */ +enum netkit_action { +NETKIT_NEXT =-1 , +NETKIT_PASS =0 , +NETKIT_DROP =2 , +NETKIT_REDIRECT =7 , +}; + +enum netkit_mode { +NETKIT_L2 , +NETKIT_L3 , +}; + +enum { +IFLA_NETKIT_UNSPEC , +IFLA_NETKIT_PEER_INFO , +IFLA_NETKIT_PRIMARY , +IFLA_NETKIT_POLICY , +IFLA_NETKIT_PEER_POLICY , +IFLA_NETKIT_MODE , + __IFLA_NETKIT_MAX, +}; +#define IFLA_NETKIT_MAX (__IFLA_NETKIT_MAX - 1) +/* VXLAN section */ /* include statistics in the dump */
kernel/bpf/syscall.c +25 -5
@@ -35,8 +35,9 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/trace_events.h> -#include <net/netfilter/nf_bpf_link.h> +#include <net/netfilter/nf_bpf_link.h> +#include <net/netkit.h> #include <net/tcx.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
@@ -3730,6 +3731,8 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)return BPF_PROG_TYPE_LSM ;case BPF_TCX_INGRESS :case BPF_TCX_EGRESS : +case BPF_NETKIT_PRIMARY : +case BPF_NETKIT_PEER :return BPF_PROG_TYPE_SCHED_CLS ;default :return BPF_PROG_TYPE_UNSPEC ;
@@ -3781,7 +3784,9 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,return 0 ;case BPF_PROG_TYPE_SCHED_CLS :if (attach_type !=BPF_TCX_INGRESS && - attach_type !=BPF_TCX_EGRESS ) + attach_type !=BPF_TCX_EGRESS && + attach_type !=BPF_NETKIT_PRIMARY && + attach_type !=BPF_NETKIT_PEER )return -EINVAL ;return 0 ;default :
@@ -3864,7 +3869,11 @@ static int bpf_prog_attach(const union bpf_attr *attr) ret =cgroup_bpf_prog_attach (attr, ptype, prog);break ;case BPF_PROG_TYPE_SCHED_CLS : - ret =tcx_prog_attach (attr, prog); +if (attr->attach_type ==BPF_TCX_INGRESS || + attr->attach_type ==BPF_TCX_EGRESS ) + ret =tcx_prog_attach (attr, prog); +else + ret =netkit_prog_attach (attr, prog);break ;default : ret = -EINVAL ;
@@ -3925,7 +3934,11 @@ static int bpf_prog_detach(const union bpf_attr *attr) ret =cgroup_bpf_prog_detach (attr, ptype);break ;case BPF_PROG_TYPE_SCHED_CLS : - ret =tcx_prog_detach (attr, prog); +if (attr->attach_type ==BPF_TCX_INGRESS || + attr->attach_type ==BPF_TCX_EGRESS ) + ret =tcx_prog_detach (attr, prog); +else + ret =netkit_prog_detach (attr, prog);break ;default : ret = -EINVAL ;
@@ -3992,6 +4005,9 @@ static int bpf_prog_query(const union bpf_attr *attr,case BPF_TCX_INGRESS :case BPF_TCX_EGRESS :return tcx_prog_query (attr, uattr); +case BPF_NETKIT_PRIMARY : +case BPF_NETKIT_PEER : +return netkit_prog_query (attr, uattr);default :return -EINVAL ; }
@@ -4973,7 +4989,11 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr) ret =bpf_xdp_link_attach (attr, prog);break ;case BPF_PROG_TYPE_SCHED_CLS : - ret =tcx_link_attach (attr, prog); +if (attr->link_create.attach_type ==BPF_TCX_INGRESS || + attr->link_create.attach_type ==BPF_TCX_EGRESS ) + ret =tcx_link_attach (attr, prog); +else + ret =netkit_link_attach (attr, prog);break ;case BPF_PROG_TYPE_NETFILTER : ret =bpf_nf_link_attach (attr, prog);
tools/include/uapi/linux/bpf.h +14 -0
@@ -1052,6 +1052,8 @@ enum bpf_attach_type {BPF_CGROUP_UNIX_RECVMSG ,BPF_CGROUP_UNIX_GETPEERNAME ,BPF_CGROUP_UNIX_GETSOCKNAME , +BPF_NETKIT_PRIMARY , +BPF_NETKIT_PEER , __MAX_BPF_ATTACH_TYPE };
@@ -1071,6 +1073,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETFILTER =10 ,BPF_LINK_TYPE_TCX =11 ,BPF_LINK_TYPE_UPROBE_MULTI =12 , +BPF_LINK_TYPE_NETKIT =13 , MAX_BPF_LINK_TYPE, };
@@ -1656,6 +1659,13 @@ union bpf_attr {__u32 flags;__u32 pid; } uprobe_multi; +struct { +union { +__u32 relative_fd; +__u32 relative_id; + }; +__u64 expected_revision; + } netkit; }; } link_create;
@@ -6576,6 +6586,10 @@ struct bpf_link_info {__u32 ifindex;__u32 attach_type; } tcx; +struct { +__u32 ifindex; +__u32 attach_type; + } netkit; }; } __attribute__((aligned(8)));
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
Quoted 15 lines · Daniel Borkmann
This work adds a new, minimal BPF-programmable device called "netkit" (former PoC code-name "meta") we recently presented at LSF/MM/BPF. The core idea is that BPF programs are executed within the drivers xmit routine and therefore e.g. in case of containers/Pods moving BPF processing closer to the source.
One of the goals was that in case of Pod egress traffic, this allows to move BPF programs from hostns tcx ingress into the device itself, providing earlier drop or forward mechanisms, for example, if the BPF program determines that the skb must be sent out of the node, then a redirect to the physical device can take place directly without going through per-CPU backlog queue. This helps to shift processing for such traffic from softirq to process context, leading to better scheduling decisions/performance (see measurements in the slides).
Quoted 7 lines
In this initial version, the netkit device ships as a pair, but we plan to extend this further so it can also operate in single device mode. The pair comes with a primary and a peer device. Only the primary device, typically residing in hostns, can manage BPF programs for itself and its peer. The peer device is designated for containers/Pods and cannot attach/detach BPF programs. Upon the device creation, the user can set the default policy to 'forward' or 'drop' for the case when no BPF program is attached.
Nit: according to the code the policies are 'pass' and 'drop'? :)
Quoted 23 lines
Additionally, the device can be operated in L3 (default) or L2 mode. The management of BPF programs is done via
bpf_mprog, so that multi-attach is supported right from the beginning with similar API and dependency controls as tcx. For details on the latter see commit053c8e1f235d("bpf: Add generic attach/detach/query API for multi-progs"). tc BPF compatibility is provided, so that existing programs can be easily migrated.Going forward, we plan to use netkit devices in Cilium as the main device type for connecting Pods. They will be operated in L3 mode in order to simplify a Pod's neighbor management and the peer will operate in default drop mode, so that no traffic is leaving between the time when a Pod is brought up by the CNI plugin and programs attached by the agent. Additionally, the programs we attach via tcx on the physical devices are using
bpf_redirect_peer()for inbound traffic into netkit device, hence the latter is also supporting thendo_get_peer_devcallback. Similarly, we usebpf_redirect_neigh()for the way out, pushing from netkit peer to phys device directly. Also, BIG TCP is supported on netkit device. For the follow-up work in single device mode, we plan to convert Cilium's cilium_host/_net devices into a single one.An extensive test suite for checking device operations and the BPF program and link management API comes as BPF selftests in this series.
Quoted 7 lines
Co-developed-by Nikolay Aleksandrov <[email protected]>Signed-off-by Nikolay Aleksandrov <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Link:
http://vger.kernel.org/bpfconf2023_material/tcx_meta_netdev_borkmann.pdf
(24ff.)
I like the new name - thank you for changing it! :)
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
On 10/24/23 6:07 PM, Toke Høiland-Jørgensen wrote:
[...]
Quoted 1 line
I like the new name - thank you for changing it! :)
I'm glad :) and thanks for the review!
Quoted 2 lines
Reviewed-by Toke Høiland-Jørgensen <[email protected]>
Re: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
In reply to Daniel BorkmannQuoted 873 lines · Daniel Borkmann
This work adds a new, minimal BPF-programmable device called "netkit" (former PoC code-name "meta") we recently presented at LSF/MM/BPF. The core idea is that BPF programs are executed within the drivers xmit routine and therefore e.g. in case of containers/Pods moving BPF processing closer to the source.
One of the goals was that in case of Pod egress traffic, this allows to move BPF programs from hostns tcx ingress into the device itself, providing earlier drop or forward mechanisms, for example, if the BPF program determines that the skb must be sent out of the node, then a redirect to the physical device can take place directly without going through per-CPU backlog queue. This helps to shift processing for such traffic from softirq to process context, leading to better scheduling decisions/performance (see measurements in the slides).
In this initial version, the netkit device ships as a pair, but we plan to extend this further so it can also operate in single device mode. The pair comes with a primary and a peer device. Only the primary device, typically residing in hostns, can manage BPF programs for itself and its peer. The peer device is designated for containers/Pods and cannot attach/detach BPF programs. Upon the device creation, the user can set the default policy to 'forward' or 'drop' for the case when no BPF program is attached.
Additionally, the device can be operated in L3 (default) or L2 mode. The management of BPF programs is done via
bpf_mprog, so that multi-attach is supported right from the beginning with similar API and dependency controls as tcx. For details on the latter see commit053c8e1f235d("bpf: Add generic attach/detach/query API for multi-progs"). tc BPF compatibility is provided, so that existing programs can be easily migrated.Going forward, we plan to use netkit devices in Cilium as the main device type for connecting Pods. They will be operated in L3 mode in order to simplify a Pod's neighbor management and the peer will operate in default drop mode, so that no traffic is leaving between the time when a Pod is brought up by the CNI plugin and programs attached by the agent. Additionally, the programs we attach via tcx on the physical devices are using
bpf_redirect_peer()for inbound traffic into netkit device, hence the latter is also supporting thendo_get_peer_devcallback. Similarly, we usebpf_redirect_neigh()for the way out, pushing from netkit peer to phys device directly. Also, BIG TCP is supported on netkit device. For the follow-up work in single device mode, we plan to convert Cilium's cilium_host/_net devices into a single one.An extensive test suite for checking device operations and the BPF program and link management API comes as BPF selftests in this series.
Co-developed-by Nikolay Aleksandrov <[email protected]>Signed-off-by Nikolay Aleksandrov <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- MAINTAINERS | 9 + drivers/net/Kconfig | 9 + drivers/net/Makefile | 1 + drivers/net/netkit.c | 934 +++++++++++++++++++++++++++++++++ include/net/netkit.h | 38 ++ include/uapi/linux/bpf.h | 14 + include/uapi/linux/if_link.h | 24 + kernel/bpf/syscall.c | 30 +- tools/include/uapi/linux/bpf.h | 14 + 9 files changed, 1068 insertions(+), 5 deletions(-) create mode 100644 drivers/net/netkit.c create mode 100644 include/net/netkit.hMAINTAINERS +9
-0@@ -3795,6 +3795,15 @@ L: [email protected] S: Odd Fixes K: (?:\b|_)bpf(?:\b|_) +BPF [NETKIT] (BPF-programmable network device) +M: Daniel Borkmann <[email protected]> +M: Nikolay Aleksandrov <[email protected]> +L: [email protected] +L: [email protected] +S: Supported +F: drivers/net/netkit.c +F: include/net/netkit.h + BPF [NETWORKING] (struct_ops, reuseport) M: Martin KaFai Lau <[email protected]> L: [email protected]drivers/net/Kconfig +9
-0@@ -448,6 +448,15 @@ config NLMON diagnostics, etc. This is mostly intended for developers or support to debug netlink issues. If unsure, say N. +config NETKIT + bool "BPF-programmable network device" + depends on BPF_SYSCALL + help + The netkit device is a virtual networking device where BPF programs + can be attached to the device(s) transmission routine in order to + implement the driver's internal logic. The device can be configured + to operate in L3 or L2 mode. If unsure, say N. + config NET_VRF tristate "Virtual Routing and Forwarding (Lite)" depends on IP_MULTIPLE_TABLESdrivers/net/Makefile +1
-0@@ -22,6 +22,7 @@ obj-$(CONFIG_MDIO) += mdio.o obj-$(CONFIG_NET) += loopback.o obj-$(CONFIG_NETDEV_LEGACY_INIT) += Space.o obj-$(CONFIG_NETCONSOLE) += netconsole.o +obj-$(CONFIG_NETKIT) += netkit.o obj-y += phy/ obj-y += pse-pd/ obj-y += mdio/drivers/net/netkit.c new file +750
-0@@ -0,0 +1,934 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2023 Isovalent */ + +#include <linux/netdevice.h> +#include <linux/ethtool.h> +#include <linux/etherdevice.h> +#include <linux/filter.h> +#include <linux/netfilter_netdev.h> +#include <linux/bpf_mprog.h> + +#include <net/netkit.h> +#include <net/dst.h> +#include <net/tcx.h> + +#define DRV_NAME "netkit" + +struct netkit { +/* Needed in fast-path */ +struct net_device __rcu *peer; +struct bpf_mprog_entry __rcu *active; +enum netkit_action policy; +struct bpf_mprog_bundle bundle; + +/* Needed in slow-path */ +enum netkit_mode mode; +bool primary; +u32 headroom; +}; + +struct netkit_link { +struct bpf_link link; +struct net_device *dev; +u32 location; +}; + +static __always_inline int +netkit_run (const struct bpf_mprog_entry *entry,struct sk_buff *skb, +enum netkit_action ret) +{ +const struct bpf_mprog_fp *fp; +const struct bpf_prog *prog; + + bpf_mprog_foreach_prog(entry, fp, prog) { +bpf_compute_data_pointers (skb); + ret =bpf_prog_run (prog, skb); +if (ret !=NETKIT_NEXT ) +break ; + } +return ret; +} + +static void netkit_prep_forward (struct sk_buff *skb,bool xnet) +{ +skb_scrub_packet (skb, xnet); + skb->priority =0 ; +nf_skip_egress (skb, true); +} + +static struct netkit *netkit_priv (const struct net_device *dev) +{ +return netdev_priv (dev); +} + +static netdev_tx_t netkit_xmit (struct sk_buff *skb,struct net_device *dev) +{ +struct netkit *nk =netkit_priv (dev); +enum netkit_action ret =READ_ONCE (nk->policy); +netdev_tx_t ret_dev =NET_XMIT_SUCCESS ; +const struct bpf_mprog_entry *entry; +struct net_device *peer; + +rcu_read_lock (); + peer =rcu_dereference (nk->peer); +if (unlikely (!peer || !(peer->flags &IFF_UP ) || + !pskb_may_pull (skb,ETH_HLEN ) || +skb_orphan_frags (skb,GFP_ATOMIC ))) + goto drop; +netkit_prep_forward (skb, !net_eq (dev_net (dev),dev_net (peer))); + skb->dev = peer; + entry =rcu_dereference (nk->active); +if (entry) + ret =netkit_run (entry, skb, ret); +switch (ret) { +case NETKIT_NEXT : +case NETKIT_PASS : + skb->protocol =eth_type_trans (skb, skb->dev); +skb_postpull_rcsum (skb,eth_hdr (skb),ETH_HLEN ); +__netif_rx (skb); +break ; +case NETKIT_REDIRECT : +skb_do_redirect (skb); +break ; +case NETKIT_DROP : +default : +drop: +kfree_skb (skb); +dev_core_stats_tx_dropped_inc (dev); + ret_dev =NET_XMIT_DROP ; +break ; + } +rcu_read_unlock (); +return ret_dev; +} + +static int netkit_open (struct net_device *dev) +{ +struct netkit *nk =netkit_priv (dev); +struct net_device *peer =rtnl_dereference (nk->peer); + +if (!peer) +return -ENOTCONN ; +if (peer->flags &IFF_UP ) { +netif_carrier_on (dev); +netif_carrier_on (peer); + } +return 0 ; +} + +static int netkit_close (struct net_device *dev) +{ +struct netkit *nk =netkit_priv (dev); +struct net_device *peer =rtnl_dereference (nk->peer); + +netif_carrier_off (dev); +if (peer) +netif_carrier_off (peer); +return 0 ; +} + +static int netkit_get_iflink (const struct net_device *dev) +{ +struct netkit *nk =netkit_priv (dev); +struct net_device *peer; +int iflink =0 ; + +rcu_read_lock (); + peer =rcu_dereference (nk->peer); +if (peer) + iflink = peer->ifindex; +rcu_read_unlock (); +return iflink; +} + +static void netkit_set_multicast (struct net_device *dev) +{ +/* Nothing to do, we receive whatever gets pushed to us! */ +} + +static void netkit_set_headroom (struct net_device *dev,int headroom) +{ +struct netkit *nk =netkit_priv (dev), *nk2; +struct net_device *peer; + +if (headroom <0 ) + headroom =NET_SKB_PAD ; + +rcu_read_lock (); + peer =rcu_dereference (nk->peer); +if (unlikely (!peer)) + goto out; + + nk2 =netkit_priv (peer); + nk->headroom = headroom; + headroom =max (nk->headroom, nk2->headroom); + + peer->needed_headroom = headroom; + dev->needed_headroom = headroom; +out: +rcu_read_unlock (); +} + +static struct net_device *netkit_peer_dev (struct net_device *dev) +{ +return rcu_dereference (netkit_priv (dev)->peer); +} + +static const struct net_device_ops netkit_netdev_ops = { + .ndo_open = netkit_open, + .ndo_stop = netkit_close, + .ndo_start_xmit = netkit_xmit, + .ndo_set_rx_mode = netkit_set_multicast, + .ndo_set_rx_headroom = netkit_set_headroom, + .ndo_get_iflink = netkit_get_iflink, + .ndo_get_peer_dev = netkit_peer_dev, + .ndo_features_check = passthru_features_check, +}; + +static void netkit_get_drvinfo (struct net_device *dev, +struct ethtool_drvinfo *info) +{ +strscpy (info->driver,DRV_NAME ,sizeof (info->driver)); +} + +static const struct ethtool_ops netkit_ethtool_ops = { + .get_drvinfo = netkit_get_drvinfo, +}; + +static void netkit_setup (struct net_device *dev) +{ +static const netdev_features_t netkit_features_hw_vlan = +NETIF_F_HW_VLAN_CTAG_TX | +NETIF_F_HW_VLAN_CTAG_RX | +NETIF_F_HW_VLAN_STAG_TX | +NETIF_F_HW_VLAN_STAG_RX ; +static const netdev_features_t netkit_features = + netkit_features_hw_vlan | +NETIF_F_SG | +NETIF_F_FRAGLIST | +NETIF_F_HW_CSUM | +NETIF_F_RXCSUM | +NETIF_F_SCTP_CRC | +NETIF_F_HIGHDMA | +NETIF_F_GSO_SOFTWARE | +NETIF_F_GSO_ENCAP_ALL ; + +ether_setup (dev); + dev->max_mtu =ETH_MAX_MTU ; + + dev->flags |=IFF_NOARP ; + dev->priv_flags &= ~IFF_TX_SKB_SHARING ; + dev->priv_flags |=IFF_LIVE_ADDR_CHANGE ; + dev->priv_flags |=IFF_PHONY_HEADROOM ; + dev->priv_flags |=IFF_NO_QUEUE ; + + dev->ethtool_ops = &netkit_ethtool_ops; + dev->netdev_ops = &netkit_netdev_ops; + + dev->features |= netkit_features |NETIF_F_LLTX ; + dev->hw_features = netkit_features; + dev->hw_enc_features = netkit_features; + dev->mpls_features =NETIF_F_HW_CSUM |NETIF_F_GSO_SOFTWARE ; + dev->vlan_features = dev->features & ~netkit_features_hw_vlan; + + dev->needs_free_netdev = true; + +netif_set_tso_max_size (dev,GSO_MAX_SIZE ); +} + +static struct net *netkit_get_link_net (const struct net_device *dev) +{ +struct netkit *nk =netkit_priv (dev); +struct net_device *peer =rtnl_dereference (nk->peer); + +return peer ?dev_net (peer) :dev_net (dev); +} + +static int netkit_check_policy (int policy,struct nlattr *tb, +struct netlink_ext_ack *extack) +{ +switch (policy) { +case NETKIT_PASS : +case NETKIT_DROP : +return 0 ; +default : +NL_SET_ERR_MSG_ATTR (extack, tb, +"Provided default xmit policy not supported" ); +return -EINVAL ; + } +} + +static int netkit_check_mode (int mode,struct nlattr *tb, +struct netlink_ext_ack *extack) +{ +switch (mode) { +case NETKIT_L2 : +case NETKIT_L3 : +return 0 ; +default : +NL_SET_ERR_MSG_ATTR (extack, tb, +"Provided device mode can only be L2 or L3" ); +return -EINVAL ; + } +} + +static int netkit_validate (struct nlattr *tb[],struct nlattr *data[], +struct netlink_ext_ack *extack) +{ +struct nlattr *attr = tb[IFLA_ADDRESS ]; + +if (!attr) +return 0 ; +NL_SET_ERR_MSG_ATTR (extack, attr, +"Setting Ethernet address is not supported" ); +return -EOPNOTSUPP ; +} + +static struct rtnl_link_ops netkit_link_ops; + +static int netkit_new_link (struct net *src_net,struct net_device *dev, +struct nlattr *tb[],struct nlattr *data[], +struct netlink_ext_ack *extack) +{ +struct nlattr *peer_tb[IFLA_MAX +1 ], **tbp = tb, *attr; +enum netkit_action default_prim =NETKIT_PASS ; +enum netkit_action default_peer =NETKIT_PASS ; +enum netkit_mode mode =NETKIT_L3 ; +unsigned char ifname_assign_type; +struct ifinfomsg *ifmp =NULL ; +struct net_device *peer; +char ifname[IFNAMSIZ ]; +struct netkit *nk; +struct net *net; +int err; + +if (data) { +if (data[IFLA_NETKIT_MODE ]) { + attr = data[IFLA_NETKIT_MODE ]; + mode =nla_get_u32 (attr); + err =netkit_check_mode (mode, attr, extack); +if (err <0 ) +return err; + } +if (data[IFLA_NETKIT_PEER_INFO ]) { + attr = data[IFLA_NETKIT_PEER_INFO ]; + ifmp =nla_data (attr); + err =rtnl_nla_parse_ifinfomsg (peer_tb, attr, extack); +if (err <0 ) +return err; + err =netkit_validate (peer_tb,NULL , extack); +if (err <0 ) +return err; + tbp = peer_tb; + } +if (data[IFLA_NETKIT_POLICY ]) { + attr = data[IFLA_NETKIT_POLICY ]; + default_prim =nla_get_u32 (attr); + err =netkit_check_policy (default_prim, attr, extack); +if (err <0 ) +return err; + } +if (data[IFLA_NETKIT_PEER_POLICY ]) { + attr = data[IFLA_NETKIT_PEER_POLICY ]; + default_peer =nla_get_u32 (attr); + err =netkit_check_policy (default_peer, attr, extack); +if (err <0 ) +return err; + } + } + +if (ifmp && tbp[IFLA_IFNAME ]) { +nla_strscpy (ifname, tbp[IFLA_IFNAME ],IFNAMSIZ ); + ifname_assign_type =NET_NAME_USER ; + }else { +strscpy (ifname,"nk%d" ,IFNAMSIZ ); + ifname_assign_type =NET_NAME_ENUM ; + } + + net =rtnl_link_get_net (src_net, tbp); +if (IS_ERR (net)) +return PTR_ERR (net); + + peer =rtnl_create_link (net, ifname, ifname_assign_type, + &netkit_link_ops, tbp, extack); +if (IS_ERR (peer)) { +put_net (net); +return PTR_ERR (peer); + } + +netif_inherit_tso_max (peer, dev); + +if (mode ==NETKIT_L2 ) +eth_hw_addr_random (peer); +if (ifmp && dev->ifindex) + peer->ifindex = ifmp->ifi_index; + + nk =netkit_priv (peer); + nk->primary = false; + nk->policy = default_peer; + nk->mode = mode; +bpf_mprog_bundle_init (&nk->bundle); +RCU_INIT_POINTER (nk->active,NULL ); +RCU_INIT_POINTER (nk->peer,NULL ); + + err =register_netdevice (peer); +put_net (net); +if (err <0 ) + goto err_register_peer; +netif_carrier_off (peer); +if (mode ==NETKIT_L2 ) +dev_change_flags (peer, peer->flags & ~IFF_NOARP ,NULL ); + + err =rtnl_configure_link (peer,NULL ,0 ,NULL ); +if (err <0 ) + goto err_configure_peer; + +if (mode ==NETKIT_L2 ) +eth_hw_addr_random (dev); +if (tb[IFLA_IFNAME ]) +nla_strscpy (dev->name, tb[IFLA_IFNAME ],IFNAMSIZ ); +else +strscpy (dev->name,"nk%d" ,IFNAMSIZ ); + + nk =netkit_priv (dev); + nk->primary = true; + nk->policy = default_prim; + nk->mode = mode; +bpf_mprog_bundle_init (&nk->bundle); +RCU_INIT_POINTER (nk->active,NULL ); +RCU_INIT_POINTER (nk->peer,NULL ); + + err =register_netdevice (dev); +if (err <0 ) + goto err_configure_peer; +netif_carrier_off (dev); +if (mode ==NETKIT_L2 ) +dev_change_flags (dev, dev->flags & ~IFF_NOARP ,NULL ); + +rcu_assign_pointer (netkit_priv (dev)->peer, peer); +rcu_assign_pointer (netkit_priv (peer)->peer, dev); +return 0 ; +err_configure_peer: +unregister_netdevice (peer); +return err; +err_register_peer: +free_netdev (peer); +return err; +} + +static struct bpf_mprog_entry *netkit_entry_fetch (struct net_device *dev, +bool bundle_fallback) +{ +struct netkit *nk =netkit_priv (dev); +struct bpf_mprog_entry *entry; + +ASSERT_RTNL (); + entry =rcu_dereference_rtnl (nk->active); +if (entry) +return entry; +if (bundle_fallback) +return &nk->bundle.a; +return NULL ; +} + +static void netkit_entry_update (struct net_device *dev, +struct bpf_mprog_entry *entry) +{ +struct netkit *nk =netkit_priv (dev); + +ASSERT_RTNL (); +rcu_assign_pointer (nk->active, entry); +} + +static void netkit_entry_sync (void ) +{ +synchronize_rcu (); +} + +static struct net_device *netkit_dev_fetch (struct net *net,u32 ifindex,u32 which) +{ +struct net_device *dev; +struct netkit *nk; + +ASSERT_RTNL (); + +switch (which) { +case BPF_NETKIT_PRIMARY : +case BPF_NETKIT_PEER : +break ; +default : +return ERR_PTR (-EINVAL ); + } + + dev =__dev_get_by_index (net, ifindex); +if (!dev) +return ERR_PTR (-ENODEV ); +if (dev->netdev_ops != &netkit_netdev_ops) +return ERR_PTR (-ENXIO ); + + nk =netkit_priv (dev); +if (!nk->primary) +return ERR_PTR (-EACCES ); +if (which ==BPF_NETKIT_PEER ) { + dev =rcu_dereference_rtnl (nk->peer); +if (!dev) +return ERR_PTR (-ENODEV ); + } +return dev; +} + +int netkit_prog_attach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +struct bpf_mprog_entry *entry, *entry_new; +struct bpf_prog *replace_prog =NULL ; +struct net_device *dev; +int ret; + +rtnl_lock (); + dev =netkit_dev_fetch (current->nsproxy->net_ns, attr->target_ifindex, + attr->attach_type); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); + goto out; + } + entry =netkit_entry_fetch (dev, true); +if (attr->attach_flags &BPF_F_REPLACE ) { + replace_prog =bpf_prog_get_type (attr->replace_bpf_fd, + prog->type); +if (IS_ERR (replace_prog)) { + ret =PTR_ERR (replace_prog); + replace_prog =NULL ; + goto out; + } + } + ret =bpf_mprog_attach (entry, &entry_new, prog,NULL , replace_prog, + attr->attach_flags, attr->relative_fd, + attr->expected_revision); +if (!ret) { +if (entry != entry_new) { +netkit_entry_update (dev, entry_new); +netkit_entry_sync (); + } +bpf_mprog_commit (entry); + } +out: +if (replace_prog) +bpf_prog_put (replace_prog); +rtnl_unlock (); +return ret; +} + +int netkit_prog_detach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +struct bpf_mprog_entry *entry, *entry_new; +struct net_device *dev; +int ret; + +rtnl_lock (); + dev =netkit_dev_fetch (current->nsproxy->net_ns, attr->target_ifindex, + attr->attach_type); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); + goto out; + } + entry =netkit_entry_fetch (dev, false); +if (!entry) { + ret = -ENOENT ; + goto out; + } + ret =bpf_mprog_detach (entry, &entry_new, prog,NULL , attr->attach_flags, + attr->relative_fd, attr->expected_revision); +if (!ret) { +if (!bpf_mprog_total (entry_new)) + entry_new =NULL ; +netkit_entry_update (dev, entry_new); +netkit_entry_sync (); +bpf_mprog_commit (entry); + } +out: +rtnl_unlock (); +return ret; +} + +int netkit_prog_query (const union bpf_attr *attr,union bpf_attr __user *uattr) +{ +struct net_device *dev; +int ret; + +rtnl_lock (); + dev =netkit_dev_fetch (current->nsproxy->net_ns, + attr->query.target_ifindex, + attr->query.attach_type); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); + goto out; + } + ret =bpf_mprog_query (attr, uattr,netkit_entry_fetch (dev, false)); +out: +rtnl_unlock (); +return ret; +} + +static struct netkit_link *netkit_link (const struct bpf_link *link) +{ + return container_of(link, struct netkit_link, link); +} + +static int netkit_link_prog_attach (struct bpf_link *link,u32 flags, +u32 id_or_fd,u64 revision) +{ +struct netkit_link *nkl =netkit_link (link); +struct bpf_mprog_entry *entry, *entry_new; +struct net_device *dev = nkl->dev; +int ret; + +ASSERT_RTNL (); + entry =netkit_entry_fetch (dev, true); + ret =bpf_mprog_attach (entry, &entry_new, link->prog, link,NULL , flags, + id_or_fd, revision); +if (!ret) { +if (entry != entry_new) { +netkit_entry_update (dev, entry_new); +netkit_entry_sync (); + } +bpf_mprog_commit (entry); + } +return ret; +} + +static void netkit_link_release (struct bpf_link *link) +{ +struct netkit_link *nkl =netkit_link (link); +struct bpf_mprog_entry *entry, *entry_new; +struct net_device *dev; +int ret =0 ; + +rtnl_lock (); + dev = nkl->dev; +if (!dev) + goto out; + entry =netkit_entry_fetch (dev, false); +if (!entry) { + ret = -ENOENT ; + goto out; + } + ret =bpf_mprog_detach (entry, &entry_new, link->prog, link,0 ,0 ,0 ); +if (!ret) { +if (!bpf_mprog_total (entry_new)) + entry_new =NULL ; +netkit_entry_update (dev, entry_new); +netkit_entry_sync (); +bpf_mprog_commit (entry); + nkl->dev =NULL ; + } +out: +WARN_ON_ONCE (ret); +rtnl_unlock (); +} + +static int netkit_link_update (struct bpf_link *link,struct bpf_prog *nprog, +struct bpf_prog *oprog) +{ +struct netkit_link *nkl =netkit_link (link); +struct bpf_mprog_entry *entry, *entry_new; +struct net_device *dev; +int ret =0 ; + +rtnl_lock (); + dev = nkl->dev; +if (!dev) { + ret = -ENOLINK ; + goto out; + } +if (oprog && link->prog != oprog) { + ret = -EPERM ; + goto out; + } + oprog = link->prog; +if (oprog == nprog) { +bpf_prog_put (nprog); + goto out; + } + entry =netkit_entry_fetch (dev, false); +if (!entry) { + ret = -ENOENT ; + goto out; + } + ret =bpf_mprog_attach (entry, &entry_new, nprog, link, oprog, +BPF_F_REPLACE |BPF_F_ID , + link->prog->aux->id,0 ); +if (!ret) { +WARN_ON_ONCE (entry != entry_new); + oprog =xchg (&link->prog, nprog); +bpf_prog_put (oprog); +bpf_mprog_commit (entry); + } +out: +rtnl_unlock (); +return ret; +} + +static void netkit_link_dealloc (struct bpf_link *link) +{ +kfree (netkit_link (link)); +} + +static void netkit_link_fdinfo (const struct bpf_link *link,struct seq_file *seq) +{ +const struct netkit_link *nkl =netkit_link (link); +u32 ifindex =0 ; + +rtnl_lock (); +if (nkl->dev) + ifindex = nkl->dev->ifindex; +rtnl_unlock (); + +seq_printf (seq,"ifindex:\t%u\n" , ifindex); +seq_printf (seq,"attach_type:\t%u (%s)\n" , + nkl->location, + nkl->location ==BPF_NETKIT_PRIMARY ?"primary" :"peer" ); +} + +static int netkit_link_fill_info (const struct bpf_link *link, +struct bpf_link_info *info) +{ +const struct netkit_link *nkl =netkit_link (link); +u32 ifindex =0 ; + +rtnl_lock (); +if (nkl->dev) + ifindex = nkl->dev->ifindex; +rtnl_unlock (); + + info->netkit.ifindex = ifindex; + info->netkit.attach_type = nkl->location; +return 0 ; +} + +static int netkit_link_detach (struct bpf_link *link) +{ +netkit_link_release (link); +return 0 ; +} + +static const struct bpf_link_ops netkit_link_lops = { + .release = netkit_link_release, + .detach = netkit_link_detach, + .dealloc = netkit_link_dealloc, + .update_prog = netkit_link_update, + .show_fdinfo = netkit_link_fdinfo, + .fill_link_info = netkit_link_fill_info, +}; + +static int netkit_link_init (struct netkit_link *nkl, +struct bpf_link_primer *link_primer, +const union bpf_attr *attr, +struct net_device *dev, +struct bpf_prog *prog) +{ +bpf_link_init (&nkl->link,BPF_LINK_TYPE_NETKIT , + &netkit_link_lops, prog); + nkl->location = attr->link_create.attach_type; + nkl->dev = dev; +return bpf_link_prime (&nkl->link, link_primer); +} + +int netkit_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +struct bpf_link_primer link_primer; +struct netkit_link *nkl; +struct net_device *dev; +int ret; + +rtnl_lock (); + dev =netkit_dev_fetch (current->nsproxy->net_ns, + attr->link_create.target_ifindex, + attr->link_create.attach_type); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); + goto out; + }
drivers/net/netkit.c line 760
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+ nkl =kzalloc (sizeof (*nkl),GFP_KERNEL_ACCOUNT ); +if (!nkl) { + ret = -ENOMEM ; + goto out; + } + ret =netkit_link_init (nkl, &link_primer, attr, dev, prog); +if (ret) { +kfree (nkl); + goto out; + }
The series looks great! FWIW:
One small question I have is:
We now (and after introduction of tcx) seem to store non-refcounted dev pointers in the bpf_link(s). Is it guaranteed that the dev will outlive the link?
drivers/net/netkit.c line 768
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+ ret =netkit_link_prog_attach (&nkl->link, + attr->link_create.flags, + attr->link_create.netkit.relative_fd, + attr->link_create.netkit.expected_revision); +if (ret) { + nkl->dev =NULL ; +bpf_link_cleanup (&link_primer); + goto out;
What happens to nkl here? Do we leak it?
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+ } + ret =bpf_link_settle (&link_primer); +out: +rtnl_unlock (); +return ret; +}
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
Quoted 1 line · Stanislav Fomichev
On 10/23, Daniel Borkmann wrote:
[...]
Quoted 2 lines
The series looks great! FWIW:
Thanks for review!
Quoted 4 lines
One small question I have is:
We now (and after introduction of tcx) seem to store non-refcounted dev pointers in thebpf_link(s). Is it guaranteed that the dev will outlive the link?
The semantics are the same as it was done in XDP, meaning, the link is in detached state so link->dev is NULL when dev goes away, see also the dev_xdp_uninstall(). We cannot hold a refcount on the dev as otherwise if the link outlives it we get the infamous "unregister_netdev...waiting for <dev>... refcnt = 1" bug.
Quoted 10 lines
drivers/net/netkit.c line 768
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+ ret =netkit_link_prog_attach (&nkl->link, + attr->link_create.flags, + attr->link_create.netkit.relative_fd, + attr->link_create.netkit.expected_revision); +if (ret) { + nkl->dev =NULL ; +bpf_link_cleanup (&link_primer); + goto out;What happens to nkl here? Do we leak it?
No, this is done similarly as in XDP and tcx, that is, bpf_link_cleanup() will trigger eventual release of nlk here :
/* Clean up bpf_link and corresponding anon_inode file and FD. After
* anon_inode is created, bpf_link can't be just kfree()'d due to deferred
* anon_inode's release() call. This helper marks bpf_link as
* defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
* is not decremented, it's the responsibility of a calling code that failed
* to complete bpf_link initialization.
* This helper eventually calls link's dealloc callback, but does not call
* link's release callback.
*/
Thanks,
Daniel
Re: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
Quoted 13 lines · Daniel Borkmann
Quoted 1 line · Stanislav Fomichev
On 10/23, Daniel Borkmann wrote:
[...]
Quoted 2 lines
The series looks great! FWIW:
Thanks for review!
Quoted 4 lines
One small question I have is:
We now (and after introduction of tcx) seem to store non-refcounted dev pointers in thebpf_link(s). Is it guaranteed that the dev will outlive the link?
Quoted 5 lines
The semantics are the same as it was done in XDP, meaning, the link is in detached state so
link->devis NULL when dev goes away, see also thedev_xdp_uninstall(). We cannot hold a refcount on the dev as otherwise if the link outlives it we get the infamous "unregister_netdev...waiting for <dev>... refcnt = 1" bug.
Yeah, I remember I've had a similar issue with holding netdev when adding dev-bound programs, so I was wondering what are we doing here. Thanks for the pointers!
And here, I guess the assumption that the device shutdown goes via dellink (netkit_del_link) and there is no special path that reaches unregister_netdevice_many_notify otherwise, right?
What about that ndo_uninit btw? Would it be more safe/clear to make netkit_release_all be ndo_uninit? Looks like it's being triggered in a place similar to dev_xdp_uninstall/dev_tcx_uninstall.
Quoted 24 lines
Quoted 10 lines
drivers/net/netkit.c line 768
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+ ret =netkit_link_prog_attach (&nkl->link, + attr->link_create.flags, + attr->link_create.netkit.relative_fd, + attr->link_create.netkit.expected_revision); +if (ret) { + nkl->dev =NULL ; +bpf_link_cleanup (&link_primer); + goto out;What happens to nkl here? Do we leak it?
No, this is done similarly as in XDP and tcx, that is,
bpf_link_cleanup()will trigger eventual release of nlk here :/* Clean up
bpf_linkand correspondinganon_inodefile and FD. After
*anon_inodeis created,bpf_linkcan't be just kfree()'d due to deferred
* anon_inode'srelease()call. This helper marksbpf_linkas
* defunct, releasesanon_inodefile and puts reserved FD. bpf_prog's refcnt
* is not decremented, it's the responsibility of a calling code that failed
* to completebpf_linkinitialization.
* This helper eventually calls link's dealloc callback, but does not call
* link's release callback.
*/
Quoted 2 lines
Thanks,
Daniel
👍
Daniel Borkmann
maintainerRe: [PATCH bpf-next v3 1/7] netkit, bpf: Add bpf programmable net device
Quoted 20 lines · Stanislav Fomichev
Quoted 13 lines · Daniel Borkmann
Quoted 1 line · Stanislav Fomichev
On 10/23, Daniel Borkmann wrote:
[...]
Quoted 2 lines
The series looks great! FWIW:
Thanks for review!
Quoted 4 lines
One small question I have is:
We now (and after introduction of tcx) seem to store non-refcounted dev pointers in thebpf_link(s). Is it guaranteed that the dev will outlive the link?Quoted 5 lines
The semantics are the same as it was done in XDP, meaning, the link is in detached state so
link->devis NULL when dev goes away, see also thedev_xdp_uninstall(). We cannot hold a refcount on the dev as otherwise if the link outlives it we get the infamous "unregister_netdev...waiting for <dev>... refcnt = 1" bug.
Quoted 7 lines
Yeah, I remember I've had a similar issue with holding netdev when adding dev-bound programs, so I was wondering what are we doing here. Thanks for the pointers!
And here, I guess the assumption that the device shutdown goes via dellink (
netkit_del_link) and there is no special path that reachesunregister_netdevice_many_notifyotherwise, right?
Correct, this is where various netdevices do their internal cleanup.
Quoted 3 lines
What about that
ndo_uninitbtw? Would it be more safe/clear to makenetkit_release_allbendo_uninit? Looks like it's being triggered in a place similar to dev_xdp_uninstall/dev_tcx_uninstall.
Looking into it, that is a better location for netkit_release_all() indeed, and ndo_uninit is under rtnl. I'll spin a v4 with this and the commit message fixups that Toke suggested. Thanks for the pointer!
Thanks,
Daniel
Daniel Borkmann
maintainerPATCH v3 2/7 tools: Sync if_link uapi header
In reply to Daniel BorkmannSync if_link uapi header to the latest version as we need the refresher in tooling for netkit device. Given it's been a while since the last sync and the diff is fairly big, it has been done as its own commit.
--- tools/include/uapi/linux/if_link.h | 141 +++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+)
tools/include/uapi/linux/if_link.h +141 -0
@@ -211,6 +211,9 @@ struct rtnl_link_stats {* @rx_nohandler: Number of packets received on the interface * but dropped by the networking stack because the device is * not designated to receive packets (e.g. backup link in a bond). +* +* @rx_otherhost_dropped: Number of packets dropped due to mismatch +* in destination MAC address. */ struct rtnl_link_stats64 {__u64 rx_packets;
@@ -243,6 +246,23 @@ struct rtnl_link_stats64 {__u64 rx_compressed;__u64 tx_compressed;__u64 rx_nohandler; + +__u64 rx_otherhost_dropped; +}; + +/* Subset of link stats useful for in-HW collection. Meaning of the fields is as +* for struct rtnl_link_stats64. +*/ +struct rtnl_hw_stats64 { +__u64 rx_packets; +__u64 tx_packets; +__u64 rx_bytes; +__u64 tx_bytes; +__u64 rx_errors; +__u64 tx_errors; +__u64 rx_dropped; +__u64 tx_dropped; +__u64 multicast; };/* The struct should be in sync with struct ifmap */
@@ -350,7 +370,13 @@ enum {IFLA_GRO_MAX_SIZE ,IFLA_TSO_MAX_SIZE ,IFLA_TSO_MAX_SEGS , +IFLA_ALLMULTI ,/* Allmulti count: > 0 means acts ALLMULTI */ + +IFLA_DEVLINK_PORT , +IFLA_GSO_IPV4_MAX_SIZE , +IFLA_GRO_IPV4_MAX_SIZE , +IFLA_DPLL_PIN , __IFLA_MAX };
@@ -539,6 +565,12 @@ enum {IFLA_BRPORT_MRP_IN_OPEN ,IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT ,IFLA_BRPORT_MCAST_EHT_HOSTS_CNT , +IFLA_BRPORT_LOCKED , +IFLA_BRPORT_MAB , +IFLA_BRPORT_MCAST_N_GROUPS , +IFLA_BRPORT_MCAST_MAX_GROUPS , +IFLA_BRPORT_NEIGH_VLAN_SUPPRESS , +IFLA_BRPORT_BACKUP_NHID , __IFLA_BRPORT_MAX };#define IFLA_BRPORT_MAX (__IFLA_BRPORT_MAX - 1)
@@ -716,7 +748,79 @@ enum ipvlan_mode {#define IPVLAN_F_PRIVATE 0x01#define IPVLAN_F_VEPA 0x02 +/* Tunnel RTM header */ +struct tunnel_msg { +__u8 family; +__u8 flags; +__u16 reserved2; +__u32 ifindex; +}; + +/* netkit section */ +enum netkit_action { +NETKIT_NEXT =-1 , +NETKIT_PASS =0 , +NETKIT_DROP =2 , +NETKIT_REDIRECT =7 , +}; + +enum netkit_mode { +NETKIT_L2 , +NETKIT_L3 , +}; + +enum { +IFLA_NETKIT_UNSPEC , +IFLA_NETKIT_PEER_INFO , +IFLA_NETKIT_PRIMARY , +IFLA_NETKIT_POLICY , +IFLA_NETKIT_PEER_POLICY , +IFLA_NETKIT_MODE , + __IFLA_NETKIT_MAX, +}; +#define IFLA_NETKIT_MAX (__IFLA_NETKIT_MAX - 1) +/* VXLAN section */ + +/* include statistics in the dump */ +#define TUNNEL_MSG_FLAG_STATS 0x01 + +#define TUNNEL_MSG_VALID_USER_FLAGS TUNNEL_MSG_FLAG_STATS + +/* Embedded inside VXLAN_VNIFILTER_ENTRY_STATS */ +enum { +VNIFILTER_ENTRY_STATS_UNSPEC , +VNIFILTER_ENTRY_STATS_RX_BYTES , +VNIFILTER_ENTRY_STATS_RX_PKTS , +VNIFILTER_ENTRY_STATS_RX_DROPS , +VNIFILTER_ENTRY_STATS_RX_ERRORS , +VNIFILTER_ENTRY_STATS_TX_BYTES , +VNIFILTER_ENTRY_STATS_TX_PKTS , +VNIFILTER_ENTRY_STATS_TX_DROPS , +VNIFILTER_ENTRY_STATS_TX_ERRORS , +VNIFILTER_ENTRY_STATS_PAD , + __VNIFILTER_ENTRY_STATS_MAX +}; +#define VNIFILTER_ENTRY_STATS_MAX (__VNIFILTER_ENTRY_STATS_MAX - 1) + +enum { +VXLAN_VNIFILTER_ENTRY_UNSPEC , +VXLAN_VNIFILTER_ENTRY_START , +VXLAN_VNIFILTER_ENTRY_END , +VXLAN_VNIFILTER_ENTRY_GROUP , +VXLAN_VNIFILTER_ENTRY_GROUP6 , +VXLAN_VNIFILTER_ENTRY_STATS , + __VXLAN_VNIFILTER_ENTRY_MAX +}; +#define VXLAN_VNIFILTER_ENTRY_MAX (__VXLAN_VNIFILTER_ENTRY_MAX - 1) + +enum { +VXLAN_VNIFILTER_UNSPEC , +VXLAN_VNIFILTER_ENTRY , + __VXLAN_VNIFILTER_MAX +}; +#define VXLAN_VNIFILTER_MAX (__VXLAN_VNIFILTER_MAX - 1) +enum {IFLA_VXLAN_UNSPEC ,IFLA_VXLAN_ID ,
@@ -748,6 +852,8 @@ enum {IFLA_VXLAN_GPE ,IFLA_VXLAN_TTL_INHERIT ,IFLA_VXLAN_DF , +IFLA_VXLAN_VNIFILTER ,/* only applicable with COLLECT_METADATA mode */ +IFLA_VXLAN_LOCALBYPASS , __IFLA_VXLAN_MAX };#define IFLA_VXLAN_MAX (__IFLA_VXLAN_MAX - 1)
@@ -781,6 +887,7 @@ enum {IFLA_GENEVE_LABEL ,IFLA_GENEVE_TTL_INHERIT ,IFLA_GENEVE_DF , +IFLA_GENEVE_INNER_PROTO_INHERIT , __IFLA_GENEVE_MAX };#define IFLA_GENEVE_MAX (__IFLA_GENEVE_MAX - 1)
@@ -826,6 +933,8 @@ enum {IFLA_GTP_FD1 ,IFLA_GTP_PDP_HASHSIZE ,IFLA_GTP_ROLE , +IFLA_GTP_CREATE_SOCKETS , +IFLA_GTP_RESTART_COUNT , __IFLA_GTP_MAX, };#define IFLA_GTP_MAX (__IFLA_GTP_MAX - 1)
@@ -1162,6 +1271,17 @@ enum {#define IFLA_STATS_FILTER_BIT (ATTR ) (1 << (ATTR - 1)) +enum { +IFLA_STATS_GETSET_UNSPEC , +IFLA_STATS_GET_FILTERS ,/* Nest of IFLA_STATS_LINK_xxx, each a u32 with +* a filter mask for the corresponding group. +*/ +IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS ,/* 0 or 1 as u8 */ + __IFLA_STATS_GETSET_MAX, +}; + +#define IFLA_STATS_GETSET_MAX (__IFLA_STATS_GETSET_MAX - 1) +/* These are embedded into IFLA_STATS_LINK_XSTATS: * [IFLA_STATS_LINK_XSTATS] * -> [LINK_XSTATS_TYPE_xxx]
@@ -1179,10 +1299,21 @@ enum {enum {IFLA_OFFLOAD_XSTATS_UNSPEC ,IFLA_OFFLOAD_XSTATS_CPU_HIT ,/* struct rtnl_link_stats64 */ +IFLA_OFFLOAD_XSTATS_HW_S_INFO ,/* HW stats info. A nest */ +IFLA_OFFLOAD_XSTATS_L3_STATS ,/* struct rtnl_hw_stats64 */ __IFLA_OFFLOAD_XSTATS_MAX };#define IFLA_OFFLOAD_XSTATS_MAX (__IFLA_OFFLOAD_XSTATS_MAX - 1) +enum { +IFLA_OFFLOAD_XSTATS_HW_S_INFO_UNSPEC , +IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST ,/* u8 */ +IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED ,/* u8 */ + __IFLA_OFFLOAD_XSTATS_HW_S_INFO_MAX, +}; +#define IFLA_OFFLOAD_XSTATS_HW_S_INFO_MAX \ + (__IFLA_OFFLOAD_XSTATS_HW_S_INFO_MAX - 1) +/* XDP section */ #define XDP_FLAGS_UPDATE_IF_NOEXIST (1U << 0)
@@ -1281,4 +1412,14 @@ enum {#define IFLA_MCTP_MAX (__IFLA_MCTP_MAX - 1) +/* DSA section */ + +enum { +IFLA_DSA_UNSPEC , +IFLA_DSA_MASTER , + __IFLA_DSA_MAX, +}; + +#define IFLA_DSA_MAX (__IFLA_DSA_MAX - 1) +#endif /* _UAPI_LINUX_IF_LINK_H */
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v3 3/7 libbpf: Add link-based API for netkit
In reply to Daniel BorkmannThis adds bpf_program__attach_netkit() API to libbpf. Overall it is very similar to tcx. The API looks as following:
LIBBPF_API struct bpf_link * bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
const struct bpf_netkit_opts *opts);
The struct bpf_netkit_opts is done in similar way as struct bpf_tcx_opts for supporting bpf_mprog control parameters. The attach location for the primary and peer device is derived from the program section "netkit/primary" and "netkit/peer", respectively.
--- tools/lib/bpf/bpf.c | 16 ++++++++++++++++ tools/lib/bpf/bpf.h | 5 +++++ tools/lib/bpf/libbpf.c | 39 +++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 76 insertions(+)
tools/lib/bpf/bpf.c +16 -0
@@ -810,6 +810,22 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, tcx))return libbpf_err (-EINVAL );break ; +case BPF_NETKIT_PRIMARY : +case BPF_NETKIT_PEER : + relative_fd =OPTS_GET (opts, netkit.relative_fd,0 ); + relative_id =OPTS_GET (opts, netkit.relative_id,0 ); +if (relative_fd && relative_id) +return libbpf_err (-EINVAL ); +if (relative_id) { + attr.link_create.netkit.relative_id = relative_id; + attr.link_create.flags |=BPF_F_ID ; + }else { + attr.link_create.netkit.relative_fd = relative_fd; + } + attr.link_create.netkit.expected_revision =OPTS_GET (opts, netkit.expected_revision,0 ); +if (!OPTS_ZEROED (opts, netkit)) +return libbpf_err (-EINVAL ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +5 -0
@@ -415,6 +415,11 @@ struct bpf_link_create_opts {__u32 relative_id;__u64 expected_revision; } tcx; +struct { +__u32 relative_fd; +__u32 relative_id; +__u64 expected_revision; + } netkit; }; size_t :0; };
tools/lib/bpf/libbpf.c +39 -0
@@ -126,6 +126,8 @@ static const char * const attach_type_name[] = { [BPF_TCX_INGRESS] = "tcx_ingress", [BPF_TCX_EGRESS] = "tcx_egress", [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi", + [BPF_NETKIT_PRIMARY] = "netkit_primary", + [BPF_NETKIT_PEER] = "netkit_peer", };static const char *const link_type_name[] = {
@@ -142,6 +144,7 @@ static const char * const link_type_name[] = { [BPF_LINK_TYPE_NETFILTER] = "netfilter", [BPF_LINK_TYPE_TCX] = "tcx", [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi", + [BPF_LINK_TYPE_NETKIT] = "netkit", };static const char *const map_type_name[] = {
@@ -8915,6 +8918,8 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("tc" ,SCHED_CLS ,0 ,SEC_NONE ),/* deprecated / legacy, use tcx */ SEC_DEF ("classifier" ,SCHED_CLS ,0 ,SEC_NONE ),/* deprecated / legacy, use tcx */ SEC_DEF ("action" ,SCHED_ACT ,0 ,SEC_NONE ),/* deprecated / legacy, use tcx */ +SEC_DEF ("netkit/primary" ,SCHED_CLS ,BPF_NETKIT_PRIMARY ,SEC_NONE ), +SEC_DEF ("netkit/peer" ,SCHED_CLS ,BPF_NETKIT_PEER ,SEC_NONE ),SEC_DEF ("tracepoint+" ,TRACEPOINT ,0 ,SEC_NONE , attach_tp),SEC_DEF ("tp+" ,TRACEPOINT ,0 ,SEC_NONE , attach_tp), SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
@@ -12126,6 +12131,40 @@ bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,return bpf_program_attach_fd (prog, ifindex,"tcx" , &link_create_opts); } +struct bpf_link * +bpf_program__attach_netkit (const struct bpf_program *prog,int ifindex, +const struct bpf_netkit_opts *opts) +{ +LIBBPF_OPTS (bpf_link_create_opts, link_create_opts); +__u32 relative_id; +int relative_fd; + +if (!OPTS_VALID (opts, bpf_netkit_opts)) +return libbpf_err_ptr (-EINVAL ); + + relative_id =OPTS_GET (opts, relative_id,0 ); + relative_fd =OPTS_GET (opts, relative_fd,0 ); + +/* validate we don't have unexpected combinations of non-zero fields */ +if (!ifindex) { +pr_warn ("prog '%s': target netdevice ifindex cannot be zero\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } +if (relative_fd && relative_id) { +pr_warn ("prog '%s': relative_fd and relative_id cannot be set at the same time\n" , + prog->name); +return libbpf_err_ptr (-EINVAL ); + } + + link_create_opts.netkit.expected_revision =OPTS_GET (opts, expected_revision,0 ); + link_create_opts.netkit.relative_fd = relative_fd; + link_create_opts.netkit.relative_id = relative_id; + link_create_opts.flags =OPTS_GET (opts, flags,0 ); + +return bpf_program_attach_fd (prog, ifindex,"netkit" , &link_create_opts); +} +struct bpf_link *bpf_program__attach_freplace (const struct bpf_program *prog,int target_fd,const char *attach_func_name)
tools/lib/bpf/libbpf.h +15 -0
@@ -800,6 +800,21 @@ LIBBPF_API struct bpf_link * bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex, const struct bpf_tcx_opts *opts); +struct bpf_netkit_opts { +/* size of this struct, for forward/backward compatibility */ +size_t sz; +__u32 relative_fd; +__u32 relative_id; +__u64 expected_revision; +__u32 flags; + size_t :0; +}; +#define bpf_netkit_opts__last_field flags + +LIBBPF_API struct bpf_link * +bpf_program__attach_netkit (const struct bpf_program *prog,int ifindex, +const struct bpf_netkit_opts *opts); +struct bpf_map ; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
tools/lib/bpf/libbpf.map +1 -0
@@ -398,6 +398,7 @@ LIBBPF_1.3.0 { bpf_object__unpin; bpf_prog_detach_opts; bpf_program__attach_netfilter; + bpf_program__attach_netkit; bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; ring__avail_data_size;
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v3 4/7 bpftool: Implement link show support for netkit
In reply to Daniel BorkmannAdd support to dump netkit link information to bpftool in similar way as we have for XDP. The netkit link info only exposes the ifindex.
Below shows an example link dump output, and a cgroup link is included for comparison, too:
# bpftool link
[...]
10: cgroup prog 2466
cgroup_id 1 attach_type cgroup_inet6_post_bind
[...]
8: netkit prog 35
ifindex nk1(18) attach_type netkit_primary
[...]Equivalent json output:
# bpftool link --json
[...]
{
"id": 10,
"type": "cgroup",
"prog_id": 2466,
"cgroup_id": 1,
"attach_type": "cgroup_inet6_post_bind"
},
[...]
{
"id": 12,
"type": "netkit",
"prog_id": 61,
"devname": "nk1",
"ifindex": 21,
"attach_type": "netkit_primary"
}
[...]--- tools/bpf/bpftool/link.c | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/bpf/bpftool/link.c +9 -0
@@ -451,6 +451,10 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)show_link_ifindex_json (info->tcx.ifindex, json_wtr);show_link_attach_type_json (info->tcx.attach_type, json_wtr);break ; +case BPF_LINK_TYPE_NETKIT : +show_link_ifindex_json (info->netkit.ifindex, json_wtr); +show_link_attach_type_json (info->netkit.attach_type, json_wtr); +break ;case BPF_LINK_TYPE_XDP :show_link_ifindex_json (info->xdp.ifindex, json_wtr);break ;
@@ -791,6 +795,11 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)show_link_ifindex_plain (info->tcx.ifindex);show_link_attach_type_plain (info->tcx.attach_type);break ; +case BPF_LINK_TYPE_NETKIT : +printf ("\n\t" ); +show_link_ifindex_plain (info->netkit.ifindex); +show_link_attach_type_plain (info->netkit.attach_type); +break ;case BPF_LINK_TYPE_XDP :printf ("\n\t" );show_link_ifindex_plain (info->xdp.ifindex);
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 4/7] bpftool: Implement link show support for netkit
Quoted 2 lines · Daniel Borkmann
Add support to dump netkit link information to bpftool in similar way as we have for XDP. The netkit link info only exposes the ifindex.
Nit: Looks like you forgot to update this bit of the commit message? :)
-Toke
Daniel Borkmann
maintainerPATCH v3 5/7 bpftool: Extend net dump with netkit progs
In reply to Daniel BorkmannAdd support to dump BPF programs on netkit via bpftool. This includes both the BPF link and attach ops programs. Dumped information contain the attach location, function entry name, program ID and link ID when applicable.
Example with tc BPF link:
# ./bpftool net xdp:
tc:
nk1(22) netkit/peer tc1 prog_id 43 link_id 12
[...]
Example with json dump:
# ./bpftool net --json | jq
[
{
"xdp": [],
"tc": [
{
"devname": "nk1",
"ifindex": 18,
"kind": "netkit/primary",
"name": "tc1",
"prog_id": 29,
"prog_flags": [],
"link_id": 8,
"link_flags": []
}
],
"flow_dissector": [],
"netfilter": []
}
]--- tools/bpf/bpftool/Documentation/bpftool-net.rst | 8 ++++---- tools/bpf/bpftool/net.c | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-net.rst +4 -4
@@ -37,7 +37,7 @@ DESCRIPTION **bpftool net { show | list }** [ **dev** *NAME* ] List bpf program attachments in the kernel networking subsystem. - Currently, device driver xdp attachments, tcx and old-style tc + Currently, device driver xdp attachments, tcx, netkit and old-style tc classifier/action attachments, flow_dissector as well as netfilter attachments are implemented, i.e., for program types **BPF_PROG_TYPE_XDP**, **BPF_PROG_TYPE_SCHED_CLS**,
@@ -52,11 +52,11 @@ DESCRIPTION bpf programs, users should consult other tools, e.g., iproute2. The current output will start with all xdp program attachments, followed by - all tcx, then tc class/qdisc bpf program attachments, then flow_dissector - and finally netfilter programs. Both xdp programs and tcx/tc programs are + all tcx, netkit, then tc class/qdisc bpf program attachments, then flow_dissector + and finally netfilter programs. Both xdp programs and tcx/netkit/tc programs are ordered based on ifindex number. If multiple bpf programs attached to the same networking device through **tc**, the order will be first - all bpf programs attached to tcx, then tc classes, then all bpf programs + all bpf programs attached to tcx, netkit, then tc classes, then all bpf programs attached to non clsact qdiscs, and finally all bpf programs attached to root and clsact qdisc.
tools/bpf/bpftool/net.c +6 -1
@@ -79,6 +79,8 @@ static const char * const attach_type_strings[] = {static const char *const attach_loc_strings[] = { [BPF_TCX_INGRESS ] ="tcx/ingress" , [BPF_TCX_EGRESS ] ="tcx/egress" , + [BPF_NETKIT_PRIMARY ] ="netkit/primary" , + [BPF_NETKIT_PEER ] ="netkit/peer" , };const size_t net_attach_type_size =ARRAY_SIZE (attach_type_strings);
@@ -506,6 +508,9 @@ static void show_dev_tc_bpf(struct ip_devname_ifindex *dev) {__show_dev_tc_bpf (dev,BPF_TCX_INGRESS );__show_dev_tc_bpf (dev,BPF_TCX_EGRESS ); + +__show_dev_tc_bpf (dev,BPF_NETKIT_PRIMARY ); +__show_dev_tc_bpf (dev,BPF_NETKIT_PEER ); } static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
@@ -926,7 +931,7 @@ static int do_help(int argc, char **argv)" ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n" " " HELP_SPEC_OPTIONS " }\n" "\n" -"Note: Only xdp, tcx, tc, flow_dissector and netfilter attachments\n" +"Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n" " are currently supported.\n" " For progs attached to cgroups, use \"bpftool cgroup\"\n" " to dump program attachments. For program types\n"
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v3 6/7 selftests/bpf: Add netlink helper library
In reply to Daniel BorkmannAdd a minimal netlink helper library for the BPF selftests. This has been taken and cut down and cleaned up from iproute2. This covers basics such as netdevice creation which we need for BPF selftests / BPF CI given iproute2 package cannot cover it yet.
Stanislav Fomichev suggested that this could be replaced in future by ynl tool generated C code once it has RTNL support to create devices. Once we get to this point the BPF CI would also need to add libmnl. If no further extensions are needed, a second option could be that we remove this code again once iproute2 package has support.
--- tools/testing/selftests/bpf/Makefile | 19 +- tools/testing/selftests/bpf/netlink_helpers.c | 358 ++++++++++++++++++ tools/testing/selftests/bpf/netlink_helpers.h | 46 +++ 3 files changed, 418 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/bpf/netlink_helpers.c create mode 100644 tools/testing/selftests/bpf/netlink_helpers.h
tools/testing/selftests/bpf/Makefile +14 -5
@@ -585,11 +585,20 @@ endef # Define test_progs test runner. TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs -TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ - network_helpers.c testing_helpers.c \ - btf_helpers.c flow_dissector_load.h \ - cap_helpers.c test_loader.c xsk.c disasm.c \ - json_writer.c unpriv_helpers.c \ +TRUNNER_EXTRA_SOURCES := test_progs.c \ + cgroup_helpers.c \ + trace_helpers.c \ + network_helpers.c \ + testing_helpers.c \ + btf_helpers.c \ + cap_helpers.c \ + unpriv_helpers.c \ + netlink_helpers.c \ + test_loader.c \ + xsk.c \ + disasm.c \ + json_writer.c \ + flow_dissector_load.h \ ip_check_defrag_frags.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/liburandom_read.so \
tools/testing/selftests/bpf/netlink_helpers.c new file +358 -0
358 lines not on this page · raw message
tools/testing/selftests/bpf/netlink_helpers.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerPATCH v3 7/7 selftests/bpf: Add selftests for netkit
In reply to Daniel BorkmannAdd a bigger batch of test coverage to assert correct operation of netkit devices and their BPF program management:
# ./test_progs -t tc_netkit [...] [ 1.166267] bpf_testmod: loading out-of-tree module taints kernel. [ 1.166831] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel [ 1.270957] tsc: Refined TSC clocksource calibration: 3407.988 MHz [ 1.272579] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fc932722, max_idle_ns: 440795381586 ns [ 1.275336] clocksource: Switched to clocksource tsc #257 tc_netkit_basic:OK #258 tc_netkit_device:OK #259 tc_netkit_multi_links:OK #260 tc_netkit_multi_opts:OK #261 tc_netkit_neigh_links:OK Summary: 5/0 PASSED, 0 SKIPPED, 0 FAILED [...]
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/tc_helpers.h | 4 + .../selftests/bpf/prog_tests/tc_netkit.c | 687 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_link.c | 13 + 4 files changed, 705 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_netkit.c
tools/testing/selftests/bpf/config +1 -0
@@ -71,6 +71,7 @@ CONFIG_NETFILTER_SYNPROXY=y CONFIG_NETFILTER_XT_CONNMARK=y CONFIG_NETFILTER_XT_MATCH_STATE=y CONFIG_NETFILTER_XT_TARGET_CT=y +CONFIG_NETKIT=y CONFIG_NF_CONNTRACK=y CONFIG_NF_CONNTRACK_MARK=y CONFIG_NF_DEFRAG_IPV4=y
tools/testing/selftests/bpf/prog_tests/tc_helpers.h +4 -0
@@ -4,6 +4,10 @@#define TC_HELPERS #include <test_progs.h> +#ifndef loopback +# define loopback 1 +#endif +static inline __u32 id_from_prog_fd (int fd) {struct bpf_prog_info prog_info = {};
tools/testing/selftests/bpf/prog_tests/tc_netkit.c new file +687 -0
687 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_tc_link.c +13 -0
@@ -1,7 +1,11 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2023 Isovalent */ #include <stdbool.h> +#include <linux/bpf.h> +#include <linux/if_ether.h> + +#include <bpf/bpf_endian.h> #include <bpf/bpf_helpers.h> char LICENSE[] SEC("license") = "GPL";
@@ -12,10 +16,19 @@ bool seen_tc3;bool seen_tc4;bool seen_tc5;bool seen_tc6; +bool seen_eth; SEC("tc/ingress")int tc1 (struct __sk_buff *skb) { +struct ethhdr eth = {}; + +if (skb->protocol !=__bpf_constant_htons (ETH_P_IP )) + goto out; +if (bpf_skb_load_bytes (skb,0 , ð,sizeof (eth))) + goto out; + seen_eth = eth.h_proto ==bpf_htons (ETH_P_IP ); +out: seen_tc1 = true;return TCX_NEXT ; }
Signature · 1 line
2.34.1