Add bpf programmable net device
Subsystems (9)
Daniel Borkmann
maintainerv4 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!
v3 -> v4:
- Moved netkit_release_all() into ndo_uninit (Stan)
- Two small commit msg corrections (Toke)
- Added Acked/Reviewed-by
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 | 940 ++++++++++++++++++ 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, 2433 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 v4 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 'pass' 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 | 940 +++++++++++++++++++++++++++++++++ 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, 1074 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 +940 -0
940 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 v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 15 lines · Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
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 2 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
Single device mode should work how?
Quoted 5 lines
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 'pass' or 'drop' for the case when no BPF program is attached.
It looks to me that you only need the host (primary) netdevice to be used as a handle to attach the bpf programs. Because the bpf program can (and probably in real use case will) redirect to uplink/another pod netdevice skipping the host (primary) netdevice, correct?
If so, why can't you do just single device mode from start finding a different sort of bpf attach handle? (not sure which)
Quoted 21 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.
Quoted 3 lines
An extensive test suite for checking device operations and the BPF program and link management API comes as BPF selftests in this series.
Couple of nitpicks below:
[..]
drivers/net/netkit.c line 257
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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 :
Isn't this job for netlink policy?
Quoted 4 lines
drivers/net/netkit.c new file +4
-0+NL_SET_ERR_MSG_ATTR (extack, tb, +"Provided default xmit policy not supported" ); +return -EINVAL ; + }
drivers/net/netkit.c line 271
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+} + +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 :
Isn't this job for netlink policy?
Quoted 94 lines
drivers/net/netkit.c new file +94
-0+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);
drivers/net/netkit.c line 375
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+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 );
Aren't these already 0?
Quoted 46 lines
drivers/net/netkit.c new file +46
-0+ + 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; +} +
[..]
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 1 line · Jiri Pirko
Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
[...]
Quoted 6 lines
Quoted 5 lines
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 'pass' or 'drop' for the case when no BPF program is attached.
Quoted 7 lines
It looks to me that you only need the host (primary) netdevice to be used as a handle to attach the bpf programs. Because the bpf program can (and probably in real use case will) redirect to uplink/another pod netdevice skipping the host (primary) netdevice, correct?
If so, why can't you do just single device mode from start finding a different sort of bpf attach handle? (not sure which)
The first point where we switch netns from a K8s Pod is out of the netdevice. For the CNI case the vast majority has one but there could also be multi- homing for Pods where there may be two or more, and from a troubleshooting PoV aka tcpdump et al, it is the most natural point. Other attach handle inside the Pod doesn't really fit given from policy PoV it also must be unreachable for apps inside the Pod itself.
Quoted 20 lines
Quoted 20 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.
Quoted 4 lines
Quoted 2 lines
An extensive test suite for checking device operations and the BPF program and link management API comes as BPF selftests in this series.
Couple of nitpicks below:
Thanks for looking into those, I'll look into it and send a small cleanup on the two.
Quoted 187 lines
drivers/net/netkit.c line 257
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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 :Isn't this job for netlink policy?
Quoted 4 lines
drivers/net/netkit.c new file +4
-0+NL_SET_ERR_MSG_ATTR (extack, tb, +"Provided default xmit policy not supported" ); +return -EINVAL ; + }drivers/net/netkit.c line 271
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+} + +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 :Isn't this job for netlink policy?
Quoted 94 lines
drivers/net/netkit.c new file +94
-0+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);drivers/net/netkit.c line 375
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+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 );Aren't these already 0?
Quoted 46 lines
drivers/net/netkit.c new file +46
-0+ + 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; +} +[..]
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 17 lines · Wed, Oct 25, 2023 at 07:20:01PM CEST, [email protected] wrote:
Quoted 1 line · Jiri Pirko
Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
[...]
Quoted 6 lines
Quoted 5 lines
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 'pass' or 'drop' for the case when no BPF program is attached.
Quoted 7 lines
It looks to me that you only need the host (primary) netdevice to be used as a handle to attach the bpf programs. Because the bpf program can (and probably in real use case will) redirect to uplink/another pod netdevice skipping the host (primary) netdevice, correct?
If so, why can't you do just single device mode from start finding a different sort of bpf attach handle? (not sure which)
Quoted 6 lines
The first point where we switch netns from a K8s Pod is out of the netdevice. For the CNI case the vast majority has one but there could also be multi- homing for Pods where there may be two or more, and from a troubleshooting PoV aka tcpdump et al, it is the most natural point. Other attach handle inside the Pod doesn't really fit given from policy PoV it also must be unreachable for apps inside the Pod itself.
Okay. What is the usecase for the single device model then?
[..]
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 18 lines · Jiri Pirko
Quoted 17 lines · Wed, Oct 25, 2023 at 07:20:01PM CEST, [email protected] wrote:
Quoted 1 line · Jiri Pirko
Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
[...]
Quoted 13 lines
Quoted 5 lines
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 'pass' or 'drop' for the case when no BPF program is attached.
It looks to me that you only need the host (primary) netdevice to be used as a handle to attach the bpf programs. Because the bpf program can (and probably in real use case will) redirect to uplink/another pod netdevice skipping the host (primary) netdevice, correct?
If so, why can't you do just single device mode from start finding a different sort of bpf attach handle? (not sure which)
Quoted 8 lines
Quoted 6 lines
The first point where we switch netns from a K8s Pod is out of the netdevice. For the CNI case the vast majority has one but there could also be multi- homing for Pods where there may be two or more, and from a troubleshooting PoV aka tcpdump et al, it is the most natural point. Other attach handle inside the Pod doesn't really fit given from policy PoV it also must be unreachable for apps inside the Pod itself.
Okay. What is the usecase for the single device model then?
One immediate use case in the context of Cilium itself is to replace and simplify our cilium_host/cilium_net pair which is in the hostns and depending on the routing mode that Cilium is configured in handling traffic/policy from host into Pods respectively from host or Pods into collect_md encaps in order to route traffic E/W to other cluster nodes. Going further, we were thinking also to have single dev and move it into target netns with policy being configured from host.
Best,
Daniel
Nikolay Aleksandrov
maintainerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
In reply to Jiri PirkoQuoted 2 lines · Jiri Pirko
Quoted 1 line · Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
This work adds a new, minimal BPF-programmable device called "netkit"
[snip]
Quoted 6 lines
Couple of nitpicks below:
[..]
Hi,
Thanks for the review. I know about the nits below but decided against changing them, more below each...
Quoted 12 lines
drivers/net/netkit.c line 257
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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 :Isn't this job for netlink policy?
This cannot be handled by policies AFAIK, because only 2 sparse values from more are allowed. We could potentially do it through validate() but it's the same minus the explicit policy type info. IMO this approach is good.
Quoted 8 lines
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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)
Quoted 10 lines
drivers/net/netkit.c line 271
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+{ +switch (mode) { +case NETKIT_L2 : +case NETKIT_L3 : +return 0 ; +default :Isn't this job for netlink policy?
This one can be handled by policy indeed, but then we lose the nice user error. Again can be done through validate(), but it's the same and we lose explicit policy type information.
Quoted 98 lines
Quoted 98 lines
drivers/net/netkit.c new file +98
-0+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);
Quoted 10 lines
drivers/net/netkit.c line 375
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+ 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 );Aren't these already 0?
Yep, they are. Here decided in favor of explicit show of values, although it's minor and I'm fine either way.
Quoted 40 lines
Quoted 40 lines
drivers/net/netkit.c new file +40
-0+ + 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);
Quoted 8 lines
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+return err; +err_register_peer: +free_netdev (peer); +return err; +} +[..]
Cheers,
Nik
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 28 lines · Wed, Oct 25, 2023 at 09:21:01PM CEST, [email protected] wrote:
Quoted 2 lines · Jiri Pirko
Quoted 1 line · Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
This work adds a new, minimal BPF-programmable device called "netkit"
[snip]
Quoted 6 lines
Couple of nitpicks below:
[..]
Hi,
Thanks for the review. I know about the nits below but decided against changing them, more below each...Quoted 12 lines
drivers/net/netkit.c line 257
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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 :Isn't this job for netlink policy?
Quoted 2 lines
This cannot be handled by policies AFAIK, because only 2 sparse values from more are allowed. We could potentially do it through
validate()but
Perhaps good time to extend the netlink validation for list of values allowed?
Quoted 21 lines
it's the same minus the explicit policy type info. IMO this approach is good.
Quoted 8 lines
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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)Quoted 10 lines
drivers/net/netkit.c line 271
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+{ +switch (mode) { +case NETKIT_L2 : +case NETKIT_L3 : +return 0 ; +default :Isn't this job for netlink policy?
Quoted 3 lines
This one can be handled by policy indeed, but then we lose the nice user error. Again can be done through
validate(), but it's the same and we lose explicit policy type information.
But the netlink validator setups the extack properly. What's wrong with that?
Quoted 110 lines
Quoted 99 lines
Quoted 99 lines
drivers/net/netkit.c new file +98
-0+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);Quoted 10 lines
drivers/net/netkit.c line 375
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+ 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 );Aren't these already 0?
Quoted 2 lines
Yep, they are. Here decided in favor of explicit show of values, although it's minor and I'm fine either way.
It is always confusing to see this. Reader might think this is necessary as if the mem was not previuosly cleared. The general approach is to rely on mem being zeroed, not sure why this is different.
Quoted 53 lines
Quoted 41 lines
Quoted 41 lines
drivers/net/netkit.c new file +40
-0+ + 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);Quoted 8 lines
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+return err; +err_register_peer: +free_netdev (peer); +return err; +} +[..]
Cheers,
Nik
Nikolay Aleksandrov
maintainerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 58 lines · Jiri Pirko
Quoted 28 lines · Wed, Oct 25, 2023 at 09:21:01PM CEST, [email protected] wrote:
Quoted 2 lines · Jiri Pirko
Quoted 1 line · Tue, Oct 24, 2023 at 11:48:58PM CEST, [email protected] wrote:
This work adds a new, minimal BPF-programmable device called "netkit"
[snip]
Quoted 6 lines
Couple of nitpicks below:
[..]
Hi,
Thanks for the review. I know about the nits below but decided against changing them, more below each...Quoted 12 lines
drivers/net/netkit.c line 257
Quoted 8 lines
drivers/net/netkit.c new file +8
-0+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 :Isn't this job for netlink policy?
Quoted 2 lines
This cannot be handled by policies AFAIK, because only 2 sparse values from more are allowed. We could potentially do it through
validate()butPerhaps good time to extend the netlink validation for list of values allowed?
Quoted 21 lines
it's the same minus the explicit policy type info. IMO this approach is good.
Quoted 18 lines
Quoted 4 lines
drivers/net/netkit.c new file +4
-0+NL_SET_ERR_MSG_ATTR (extack, tb, +"Provided default xmit policy not supported" ); +return -EINVAL ; + }drivers/net/netkit.c line 271
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+} + +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 :Isn't this job for netlink policy?
Quoted 8 lines
Quoted 3 lines
This one can be handled by policy indeed, but then we lose the nice user error. Again can be done through
validate(), but it's the same and we lose explicit policy type information.But the netlink validator setups the extack properly. What's wrong with that?
"integer out of range" vs "Provided device mode can only be L2 or L3" ?
I like the second one better and it's more informative. The way to get it is to use NLA_POLICY_VALIDATE_FN() as type and provide custom validate() callback, which is identical to the current solution, I see no added value in changing it.
Quoted 111 lines
Quoted 111 lines
Quoted 110 lines
Quoted 96 lines
drivers/net/netkit.c new file +94
-0+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);drivers/net/netkit.c line 375
Quoted 10 lines
drivers/net/netkit.c new file +10
-0+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 );Aren't these already 0?
Quoted 8 lines
Quoted 2 lines
Yep, they are. Here decided in favor of explicit show of values, although it's minor and I'm fine either way.
It is always confusing to see this. Reader might think this is necessary as if the mem was not previuosly cleared. The general approach is to rely on mem being zeroed, not sure why this is different.
Oh come on :) you really believe someone reading this code would start inferring about netdev_alloc mem zeroing instead of getting a mental picture of the expected state? Anyway, as I said I think this is way too minor and it's fine either way, we can remove the explicit initialization and rely on the implicit one.
Quoted 53 lines
Quoted 53 lines
Quoted 41 lines
Quoted 41 lines
drivers/net/netkit.c new file +40
-0+ + 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);Quoted 8 lines
Quoted 6 lines
drivers/net/netkit.c new file +6
-0+return err; +err_register_peer: +free_netdev (peer); +return err; +} +[..]
Cheers,
Nik
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
In reply to Daniel BorkmannQuoted 6 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.
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new struct_ops type.
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 10 lines · Kui-Feng Lee
Quoted 6 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.
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.
Could your elaborate more about what does this struct_ops type do and how is it different from the SCHED_CLS bpf prog that the netkit is running?
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 11 lines · Martin KaFai Lau
Quoted 10 lines · Kui-Feng Lee
Quoted 6 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.
Quoted 7 lines
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.Could your elaborate more about what does this
struct_opstype do and how is it different from theSCHED_CLSbpf prog that the netkit is running?
I found the code has been landed.
Basing on the landed code and
the patchset of registering bpf struct_ops from modules that I am working on, it will looks like what is done in following patch. No changes on syscall, uapi and libbpf are required.
drivers/net/netkit.c +105 -2
@@ -20,6 +20,7 @@ struct netkit {struct bpf_mprog_entry __rcu *active;enum netkit_action policy;struct bpf_mprog_bundle bundle; +struct hlist_head ops_list;/* Needed in slow-path */ enum netkit_mode mode;
@@ -27,6 +28,13 @@ struct netkit {u32 headroom; }; +struct netkit_ops { +struct hlist_node node; +int ifindex; + +int (*xmit)(struct sk_buff *skb); +}; +struct netkit_link {struct bpf_link link;struct net_device *dev;
@@ -46,6 +54,22 @@ netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,if (ret !=NETKIT_NEXT )break ; } + +return ret; +} + +static __always_inline int +netkit_run_st_ops (const struct netkit *nk,struct sk_buff *skb, +enum netkit_action ret) +{ +struct netkit_ops *ops; + +hlist_for_each_entry_rcu (ops, &nk->ops_list, node) { + ret = ops->xmit (skb); +if (ret !=NETKIT_NEXT ) +break ; + } +return ret; }
@@ -80,6 +104,8 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) entry =rcu_dereference (nk->active);if (entry) ret =netkit_run (entry, skb, ret); +if (ret ==NETKIT_NEXT ) + ret =netkit_run_st_ops (nk, skb, ret);switch (ret) {case NETKIT_NEXT :case NETKIT_PASS :
@@ -900,6 +926,78 @@ static const struct nla_policy netkit_policy[IFLA_NETKIT_MAX + 1] = { .reject_message = "Primary attribute is read-only" }, }; +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + +static bool bpf_netkit_ops_is_valid_access (int off,int size, +enum bpf_access_type type, +const struct bpf_prog *prog, +struct bpf_insn_access_aux *info) +{ +return bpf_tracing_btf_ctx_access (off, size, type, prog, info); +} + +static const struct bpf_verifier_ops bpf_netkit_verifier_ops = { + .is_valid_access = bpf_netkit_ops_is_valid_access, +}; + +static int bpf_netkit_ops_reg (void *kdata) +{ +struct netkit_ops *ops = kdata; +struct netkit_link *nkl; +struct net_device *dev; + +BTF_STRUCT_OPS_TYPE_EMIT (netkit_ops); + dev =netkit_dev_fetch (current->nsproxy->net_ns, + ops->ifindex, +BPF_NETKIT_PRIMARY ); + nkl =netkit_link (dev); +hlist_add_tail_rcu (&ops->node, &nkl->ops_list); + +return 0 ; +} + +static int bpf_netkit_ops_init (struct btf *btf) +{ +return 0 ; +} + +static int bpf_netkit_ops_init_member (const struct btf_type *t, +const struct btf_member *member, +void *kdata,const void *udata) +{ +struct netkit_ops *kops = kdata; +struct netkit_ops *uops = kdata; + +u32 moff =__btf_member_bit_offset (t, member) /8 ; +if (moff == offsetof(struct netkit_ops , ifindex)) { + kops->ifindex = uops->ifindex; +return 1 ; + } +if (mod < offsetof(struct netkit_ops , ifindex)) +return 1 ; + +return 0 ; +} + +static void bpf_netkit_ops_unreg (void *kdata) +{ +struct netkit_ops *ops = kdata; + +hlist_del_rcu (&ops->node); +} + +struct bpf_struct_ops bpf_netkit_ops = { + .verifier_ops = &bpf_netkit_verifier_ops, + .init = bpf_netkit_ops_init, + .init_member = bpf_netkit_ops_init_member, + .reg = bpf_netkit_ops_reg, + .unreg = bpf_netki_ops_unreg, + .name ="netkit_ops" , + .owner =THIS_MODULE , +}; + +#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ +static struct rtnl_link_ops netkit_link_ops = { .kind =DRV_NAME , .priv_size =sizeof (struct netkit ),
@@ -917,17 +1015,22 @@ static struct rtnl_link_ops netkit_link_ops = {static __init int netkit_init (void ) { +int ret; +BUILD_BUG_ON ((int )NETKIT_NEXT != (int )TCX_NEXT || (int )NETKIT_PASS != (int )TCX_PASS || (int )NETKIT_DROP != (int )TCX_DROP || (int )NETKIT_REDIRECT != (int )TCX_REDIRECT ); -return rtnl_link_register (&netkit_link_ops); + ret =rtnl_link_register (&netkit_link_ops); +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + ret = ret ?:register_bpf_struct_ops (&bpf_netkit_ops); +#endif } static __exit void netkit_exit(void) { -rtnl_link_unregister (&netkit_link_ops); +rtnl_link_unregister (&bpf_netkit_ops); }module_init (netkit_init);
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 182 lines · Kui-Feng Lee
Quoted 11 lines · Martin KaFai Lau
Quoted 10 lines · Kui-Feng Lee
Quoted 6 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.
Quoted 7 lines
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.Could your elaborate more about what does this
struct_opstype do and how is it different from theSCHED_CLSbpf prog that the netkit is running?I found the code has been landed.
Basing on the landed code and
the patchset of registering bpfstruct_opsfrom modules that I am working on, it will looks like what is done in following patch. No changes on syscall, uapi and libbpf are required.drivers/net/netkit.c +26
-0@@ -20,6 +20,7 @@ struct netkit { struct bpf_mprog_entry __rcu *active; enum netkit_action policy; struct bpf_mprog_bundle bundle; + struct hlist_head ops_list; /* Needed in slow-path */ enum netkit_mode mode;@@ -27,6 +28,13 @@ struct netkit { u32 headroom; }; +struct netkit_ops { + struct hlist_node node; + int ifindex; + + int (*xmit)(struct sk_buff *skb); +}; + struct netkit_link { struct bpf_link link; struct net_device *dev;@@ -46,6 +54,22 @@ netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, if (ret != NETKIT_NEXT) break; } + + return ret; +} + +static __always_inline int +netkit_run_st_ops (const struct netkit *nk,struct sk_buff *skb, + enum netkit_action ret) +{ + struct netkit_ops *ops; + + hlist_for_each_entry_rcu(ops, &nk->ops_list, node) { + ret = ops->xmit(skb); + if (ret != NETKIT_NEXT) + break; + } + return ret; }@@ -80,6 +104,8 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) entry = rcu_dereference(nk->active); if (entry) ret = netkit_run(entry, skb, ret); + if (ret == NETKIT_NEXT) + ret = netkit_run_st_ops(nk, skb, ret); switch (ret) { case NETKIT_NEXT:case
NETKIT_PASS:drivers/net/netkit.c +72
-0@@ -900,6 +926,78 @@ static const struct nla_policy netkit_policy[IFLA_NETKIT_MAX + 1] = { .reject_message = "Primary attribute is read-only" }, }; +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + +static bool bpf_netkit_ops_is_valid_access (int off,int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + return bpf_tracing_btf_ctx_access(off, size, type, prog, info); +} + +static const struct bpf_verifier_ops bpf_netkit_verifier_ops = { + .is_valid_access = bpf_netkit_ops_is_valid_access, +}; + +static int bpf_netkit_ops_reg (void *kdata) +{ + struct netkit_ops *ops = kdata; + struct netkit_link *nkl; + struct net_device *dev; + + BTF_STRUCT_OPS_TYPE_EMIT(netkit_ops); + dev = netkit_dev_fetch(current->nsproxy->net_ns, + ops->ifindex, + BPF_NETKIT_PRIMARY); + nkl = netkit_link(dev); + hlist_add_tail_rcu(&ops->node, &nkl->ops_list); + + return 0; +} + +static int bpf_netkit_ops_init (struct btf *btf) +{ + return 0; +} + +static int bpf_netkit_ops_init_member (const struct btf_type *t, + const struct btf_member *member, + void *kdata, const void *udata) +{ + struct netkit_ops *kops = kdata; + struct netkit_ops *uops = kdata; + + u32 moff = __btf_member_bit_offset(t, member) / 8; + if (moff == offsetof(struct netkit_ops, ifindex)) { + kops->ifindex = uops->ifindex; + return 1; + } + if (mod < offsetof(struct netkit_ops, ifindex)) + return 1; + + return 0; +} + +static void bpf_netkit_ops_unreg (void *kdata) +{ + struct netkit_ops *ops = kdata; + + hlist_del_rcu(&ops->node); +} + +struct bpf_struct_ops bpf_netkit_ops = { + .verifier_ops = &bpf_netkit_verifier_ops, + .init = bpf_netkit_ops_init, + .init_member = bpf_netkit_ops_init_member, + .reg = bpf_netkit_ops_reg, + .unreg = bpf_netki_ops_unreg, + .name = "netkit_ops", + .owner = THIS_MODULE, +}; + +#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ + static struct rtnl_link_ops netkit_link_ops = {.kind =
DRV_NAME,
.priv_size = sizeof(struct netkit),drivers/net/netkit.c +2
-1@@ -917,17 +1015,22 @@ static struct rtnl_link_ops netkit_link_ops = { static __init int netkit_init(void) { + int ret; + BUILD_BUG_ON((int)NETKIT_NEXT != (int)TCX_NEXT || (int)NETKIT_PASS != (int)TCX_PASS || (int)NETKIT_DROP != (int)TCX_DROP || (int)NETKIT_REDIRECT != (int)TCX_REDIRECT); - return rtnl_link_register(&netkit_link_ops);
drivers/net/netkit.c static struct rtnl_link_ops netkit_link_ops = { · line 1033
Quoted 10 lines
drivers/net/netkit.c +5
-1+ ret = rtnl_link_register(&netkit_link_ops); +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + ret = ret ?: register_bpf_struct_ops(&bpf_netkit_ops); +#endif } static __exit void netkit_exit(void) { - rtnl_link_unregister(&netkit_link_ops); + rtnl_link_unregister(&bpf_netkit_ops);
This change should be removed.
Quoted 3 lines
drivers/net/netkit.c +0
-0} module_init(netkit_init);
Daniel Borkmann
maintainerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Hi Kui-Feng,
Quoted 73 lines · Kui-Feng Lee
Quoted 72 lines · Kui-Feng Lee
Quoted 8 lines · Martin KaFai Lau
Quoted 7 lines · Kui-Feng Lee
Quoted 5 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.
Quoted 7 lines
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.Could your elaborate more about what does this
struct_opstype do and how is it different from theSCHED_CLSbpf prog that the netkit is running?I found the code has been landed.
Basing on the landed code and
the patchset of registering bpfstruct_opsfrom modules that I am working on, it will looks like what is done in following patch. No changes on syscall, uapi and libbpf are required.drivers/net/netkit.c +24
-0@@ -20,6 +20,7 @@ struct netkit { struct bpf_mprog_entry __rcu *active; enum netkit_action policy; struct bpf_mprog_bundle bundle; + struct hlist_head ops_list; /* Needed in slow-path */ enum netkit_mode mode;@@ -27,6 +28,13 @@ struct netkit { u32 headroom; }; +struct netkit_ops { + struct hlist_node node; + int ifindex; + + int (*xmit)(struct sk_buff *skb); +}; + struct netkit_link { struct bpf_link link; struct net_device *dev;@@ -46,6 +54,22 @@ netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, if (ret != NETKIT_NEXT) break; } + + return ret; +} + +static __always_inline int +netkit_run_st_ops (const struct netkit *nk,struct sk_buff *skb, + enum netkit_action ret) +{ + struct netkit_ops *ops; + + hlist_for_each_entry_rcu(ops, &nk->ops_list, node) { + ret = ops->xmit(skb); + if (ret != NETKIT_NEXT) + break; + } + return ret; }
Quoted 9 lines
Quoted 9 lines
drivers/net/netkit.c +2
-0@@ -80,6 +104,8 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) entry = rcu_dereference(nk->active); if (entry) ret = netkit_run(entry, skb, ret); + if (ret == NETKIT_NEXT) + ret = netkit_run_st_ops(nk, skb, ret); switch (ret) {case
NETKIT_NEXT:
caseNETKIT_PASS:
I don't think it makes sense to cramp struct ops in here for what has been solved already with the bpf_mprog interface in a more efficient way and with control dependencies for the insertion (before/after relative programs/links). The latter is in particular crucial for a multi-user interface when dealing with network traffic (think for example: policy, forwarder, observability prog, etc).
Thanks,
Daniel
Re: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 86 lines · Daniel Borkmann
Hi Kui-Feng,
Quoted 83 lines · Kui-Feng Lee
Quoted 82 lines · Kui-Feng Lee
Quoted 8 lines · Martin KaFai Lau
Quoted 7 lines · Kui-Feng Lee
Quoted 5 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.
Quoted 7 lines
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.Could your elaborate more about what does this
struct_opstype do and how is it different from theSCHED_CLSbpf prog that the netkit is running?I found the code has been landed.
Basing on the landed code and
the patchset of registering bpfstruct_opsfrom modules that I am working on, it will looks like what is done in following patch. No changes on syscall, uapi and libbpf are required.drivers/net/netkit.c +26
-0@@ -20,6 +20,7 @@ struct netkit { struct bpf_mprog_entry __rcu *active; enum netkit_action policy; struct bpf_mprog_bundle bundle; + struct hlist_head ops_list; /* Needed in slow-path */ enum netkit_mode mode;@@ -27,6 +28,13 @@ struct netkit { u32 headroom; }; +struct netkit_ops { + struct hlist_node node; + int ifindex; + + int (*xmit)(struct sk_buff *skb); +}; + struct netkit_link { struct bpf_link link; struct net_device *dev;@@ -46,6 +54,22 @@ netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, if (ret != NETKIT_NEXT) break; } + + return ret; +} + +static __always_inline int +netkit_run_st_ops (const struct netkit *nk,struct sk_buff *skb, + enum netkit_action ret) +{ + struct netkit_ops *ops; + + hlist_for_each_entry_rcu(ops, &nk->ops_list, node) { + ret = ops->xmit(skb); + if (ret != NETKIT_NEXT) + break; + } + return ret; }@@ -80,6 +104,8 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) entry = rcu_dereference(nk->active); if (entry) ret = netkit_run(entry, skb, ret); + if (ret == NETKIT_NEXT) + ret = netkit_run_st_ops(nk, skb, ret); switch (ret) {case
NETKIT_NEXT:
Quoted 9 lines
Quoted 1 line
Quoted 1 line
case
NETKIT_PASS:I don't think it makes sense to cramp struct ops in here for what has been solved already with the
bpf_mproginterface in a more efficient way and with control dependencies for the insertion (before/after relative programs/links). The latter is in particular crucial for a multi-user interface when dealing with network traffic (think for example: policy, forwarder, observability prog, etc).
I don't mean to cramp two implementations together
and don't notice this patchset is already landed at beginning. This patch is just for explanation of how it likes if it is implemented with just struct_ops (without bpf_mprog).
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v4 1/7] netkit, bpf: Add bpf programmable net device
Quoted 99 lines · Kui-Feng Lee
Quoted 86 lines · Daniel Borkmann
Hi Kui-Feng,
Quoted 83 lines · Kui-Feng Lee
Quoted 82 lines · Kui-Feng Lee
Quoted 8 lines · Martin KaFai Lau
Quoted 7 lines · Kui-Feng Lee
Quoted 5 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.
Quoted 7 lines
Quoted 5 lines
Sorry for intruding into this discussion! Although it is too late to mentioned this since this patchset have been v4 already.
I notice netkit has introduced a new attach type. I wonder if it possible to implement it as a new
struct_opstype.Could your elaborate more about what does this
struct_opstype do and how is it different from theSCHED_CLSbpf prog that the netkit is running?I found the code has been landed.
Basing on the landed code and
the patchset of registering bpfstruct_opsfrom modules that I am working on, it will looks like what is done in following patch. No changes on syscall, uapi and libbpf are required.drivers/net/netkit.c +26
-0@@ -20,6 +20,7 @@ struct netkit { struct bpf_mprog_entry __rcu *active; enum netkit_action policy; struct bpf_mprog_bundle bundle; + struct hlist_head ops_list; /* Needed in slow-path */ enum netkit_mode mode;@@ -27,6 +28,13 @@ struct netkit { u32 headroom; }; +struct netkit_ops { + struct hlist_node node; + int ifindex; + + int (*xmit)(struct sk_buff *skb); +}; + struct netkit_link { struct bpf_link link; struct net_device *dev;@@ -46,6 +54,22 @@ netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, if (ret != NETKIT_NEXT) break; } + + return ret; +} + +static __always_inline int +netkit_run_st_ops (const struct netkit *nk,struct sk_buff *skb, + enum netkit_action ret) +{ + struct netkit_ops *ops; + + hlist_for_each_entry_rcu(ops, &nk->ops_list, node) { + ret = ops->xmit(skb); + if (ret != NETKIT_NEXT) + break; + } + return ret; }@@ -80,6 +104,8 @@ static netdev_tx_t netkit_xmit(struct sk_buff *skb, struct net_device *dev) entry = rcu_dereference(nk->active); if (entry) ret = netkit_run(entry, skb, ret); + if (ret == NETKIT_NEXT) + ret = netkit_run_st_ops(nk, skb, ret); switch (ret) {case
NETKIT_NEXT:Quoted 9 lines
Quoted 1 line
Quoted 1 line
case
NETKIT_PASS:I don't think it makes sense to cramp struct ops in here for what has been solved already with the
bpf_mproginterface in a more efficient way and with control dependencies for the insertion (before/after relative programs/links). The latter is in particular crucial for a multi-user interface when dealing with network traffic (think for example: policy, forwarder, observability prog, etc).
Quoted 2 lines
I don't mean to cramp two implementations together and don't notice this patchset is already landed at beginning.
There are a few ways to track this. patchwork bot will send a landing message to the list. There is a few mins lag time but I don't think this lags matter here. You may want to check your inbox and ensure it gets through.
git always has the source of true also.
Quoted 2 lines
This patch is just for explanation of how it likes if it is implemented with just
struct_ops(withoutbpf_mprog).
Thanks for sharing a struct_ops code snippet. It is an interesting idea to embed ifindex and other details in the struct.
Leaving it still needs verifier changes to make the PTR_TO_BTF_ID skb in struct_ops to work like tc __sk_buff such that all existing tc-bpf prog will work as is. Daniel has already mentioned the ordering API (bpf_mprog) that has been discussed for a year and has already been used in tc-link which I hope it will be extended to solve the xdp ordering also. I am also not convinced saving two attach types (note the prog type is the same here) deserve to re-create something in-parallel to tc-link and then require the same "skb" bpf dataplane program to be administrated (attach/introspect...etc) differently.
Daniel Borkmann
maintainerPATCH v4 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 v4 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 flags; +__u32 relative_fd; +__u32 relative_id; +__u64 expected_revision; + size_t :0; +}; +#define bpf_netkit_opts__last_field expected_revision + +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 v4 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 and the attach_type.
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
Daniel Borkmann
maintainerPATCH v4 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 v4 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 v4 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
Quoted 4 lines · Daniel Borkmann
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_mprogand it comes with BPF link support. For details see patch 1 and following. Thanks!
Thanks for the work on this. We have been testing the earlier version in a production service and we see at least 3% cpu win when comparing with veth.
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 29 lines · On Tue, 24 Oct 2023 23:48:57 +0200 you wrote:
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_mprogand it comes with BPF link support. For details see patch 1 and following. Thanks!v3 -> v4:
- Movednetkit_release_all()intondo_uninit(Stan)
- Two small commit msg corrections (Toke)
- Added Acked/Reviewed-by
v2 -> v3:
- Remove settingdev->min_mtutoETH_MIN_MTU(Andrew)
- Do not populate ethtoolinfo->version(Andrew)
- Populate netdev private data beforeregister_netdevice(Andrew)
- Use strscpy for ifname template (Jakub)
- UseGFP_KERNEL_ACCOUNTfor link kzalloc (Jakub)
- Carry and dump link attach type for bpftool (Toke)
v1 -> v2:
- Rename from meta (Toke, Andrii, Alexei)
- Reuseskb_scrub_packet(Stan)
- RemoveIFF_METAand usenetdev_ops(Toke)
- Add comment to multicast handler (Toke)
- Remove silly version info (Toke)
- Fixattach_type_name(Quentin)
- Rework libbpf link attach api to be similar
as tcx (Andrii)
- Move flags last forbpf_netkit_opts(Andrii)
- Rebased tobpf_mprogquery api changes
- Folded link support patch into main one
Quoted 1 line
[...]
Here is the summary with links:
- [bpf-next,v4,1/7] netkit, bpf: Add bpf programmable net device
https://git.kernel.org/bpf/bpf-next/c/35dfaad7188c - [bpf-next,v4,2/7] tools: Sync if_link uapi header https://git.kernel.org/bpf/bpf-next/c/5c1b994de4be - [bpf-next,v4,3/7] libbpf: Add link-based API for netkit https://git.kernel.org/bpf/bpf-next/c/05c31b4ab205 - [bpf-next,v4,4/7] bpftool: Implement link show support for netkit https://git.kernel.org/bpf/bpf-next/c/92a85e18ad47 - [bpf-next,v4,5/7] bpftool: Extend net dump with netkit progs https://git.kernel.org/bpf/bpf-next/c/bec981a4add6 - [bpf-next,v4,6/7] selftests/bpf: Add netlink helper library https://git.kernel.org/bpf/bpf-next/c/51f1892b5289 - [bpf-next,v4,7/7] selftests/bpf: Add selftests for netkit https://git.kernel.org/bpf/bpf-next/c/ace15f91e569
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 4 lines · Wed, Oct 25, 2023 at 01:50:25AM CEST, [email protected] wrote:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Interesting, applied within 2 hours after send. You bpf people don't care about some 24h timeout?
[..]
Martin KaFai Lau
reviewerQuoted 8 lines · Jiri Pirko
Quoted 4 lines · Wed, Oct 25, 2023 at 01:50:25AM CEST, [email protected] wrote:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Interesting, applied within 2 hours after send. You bpf people don't care about some 24h timeout?
24hr? The v1 was posted to both netdev and bpf list on 9/25. It was 10/24 yesterday. The part you commented in patch 1 had not been changed much since v1, so there was a month of time. netdev is always on the cc list. Multiple people (Andrew, Jakub...etc) had already helped to review and Daniel had addressed the comments. The change history had been diminishing from v1 to v4 and v4 changes was mostly nit-picking already.
Quoted 10 lines · Wed, Oct 25, 2023 at 06:54:27PM CEST, [email protected] wrote:
Quoted 8 lines · Jiri Pirko
Quoted 4 lines · Wed, Oct 25, 2023 at 01:50:25AM CEST, [email protected] wrote:
Hello:
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Interesting, applied within 2 hours after send. You bpf people don't care about some 24h timeout?
Quoted 6 lines
24hr? The v1 was posted to both netdev and bpf list on 9/25. It was 10/24 yesterday. The part you commented in patch 1 had not been changed much since v1, so there was a month of time. netdev is always on the cc list. Multiple people (Andrew, Jakub...etc) had already helped to review and Daniel had addressed the comments. The change history had been diminishing from v1 to v4 and v4 changes was mostly nit-picking already.
AFAIK netdev maintainer have a policy (which I thoink I saw written down somewhere but cannot find) that the patch stays on the list one day before it is getting applied. It actually makes a lot of sense. Anyway, I may be wrong.