Add support for XDP in egress path
Subsystems (8)
cover Add support for XDP in egress path
From: David Ahern <[email protected]>
This series adds support for XDP in the egress path by introducing a new XDP attachment type, BPF_XDP_EGRESS, and adding an if_link API for attaching the program to a netdevice and reporting the program. The intent is to emulate the current RX path for XDP as much as possible to maintain consistency in the 2 paths.
This set adds initial support for XDP in the egress path to the tun driver, mostly for XDP_DROP (e.g., ACL on the ingress path to the VM). XDP_REDIRECT will be handled later because we need to come up with proper way to handle it in tx path. veth should be an easy follow on for use with containers once this initial API change is in.
Some of the patches in this set were originally part of Jason Wang's work "XDP offload with virtio-net" [1]. The work overlaps work done by me, so we decided to consolidate the work into the egress path first with the offload option expected to be a follow on.
With respect to comments on RFC v2:
The kernel side implementation requires 2 UAPI attributes:
1. attach type - BPF_XDP_EGRESS, and
2. attach to a device and report such - IFLA_XDP_EGRESS.
The attach type uapi is needed as a way for the verifier to restrict access to Rx entries in the xdp context for example and is consistent with other programs and the concept of attach type. The if_link changes should mirror the current XDP API for Rx (i.e., IFLA_XDP and its nested attributes).
The expectation is that the current mode levels (skb, driver and hardware) will also apply to the egress path. The current patch set focuses on the driver mode with tun. Attempting to tag the EGRESS path as yet another mode is inconsistent on a number of levels - from the current usage of XDP_FLAGS to options passed to the verifier for restricting xdp_md accesses. Using the API as listed above maintains consistency with all existing code.
As for libbpf, we believe that an API that mirrors and is consistent with the existing xdp functions for the ingress path is the right way to go for the egress path. Meaning, the libbpf API is replicated with _egress in the name, but the libbpf implementation shares common code to the existing xdp to the degree possible.
v3:
- reworked the patches - splitting patch 1 from RFC v2 into 3, combining patch 2 from RFC v3 into the first 3, combining patches 6 and 7 from RFC v2 into 1 since both did a trivial rename and export. Reordered the patches such that kernel changes are first followed by libbpf and an enhancement to a sample.
- moved small xdp related helper functions from tun.c to tun.h to make tun_ptr_free usable from the tap code. This is needed to handle the case of tap builtin and tun built as a module.
- pkt_ptrs added to struct tun_file and passed to tun_consume_packets rather than declaring pkts as an array on the stack.
v2:
- New XDP attachment type: Jesper, Toke and Alexei discussed whether to introduce a new program type. Since this set adds a way to attach regular XDP program to the tx path, as per Alexei's suggestion, a new attachment type BPF_XDP_EGRESS is introduced.
- libbpf API changes:
Alexei had suggested _opts() style of API extension. Considering it two new libbpf APIs are introduced which are equivalent to existing APIs. New ones can be extended easily. Please see individual patches for details. xdp1 sample program is modified to use new APIs.
- tun: Some patches from previous set are removed as they are irrelevant in this series. They will in introduced later.
[1]: https://netdevconf.info/0x13/session.html?xdp-offload-with-virtio-net
David Ahern (4):
net: Add new XDP setup and query commands
net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
tun: set egress XDP program
libbpf: Add egress XDP support
Jason Wang (1):
net: core: rename netif_receive_generic_xdp() to do_generic_xdp_core()
Prashant Bhole (7):
net: Add BPF_XDP_EGRESS as a bpf_attach_type
tuntap: check tun_msg_ctl type at necessary places
tun: move shared functions to if_tun.h
vhost_net: user tap recvmsg api to access ptr ring
tuntap: remove usage of ptr ring in vhost_net
tun: run XDP program in tx path samples/bpf: xdp1, add egress XDP support
drivers/net/tap.c | 42 +++-- drivers/net/tun.c | 257 ++++++++++++++++++++++------- drivers/vhost/net.c | 77 ++++----- include/linux/if_tap.h | 5 - include/linux/if_tun.h | 57 ++++++- include/linux/netdevice.h | 6 +- include/uapi/linux/bpf.h | 1 + include/uapi/linux/if_link.h | 1 + net/core/dev.c | 44 +++-- net/core/filter.c | 8 + net/core/rtnetlink.c | 114 ++++++++++++- samples/bpf/xdp1_user.c | 30 +++- tools/include/uapi/linux/bpf.h | 1 + tools/include/uapi/linux/if_link.h | 1 + tools/lib/bpf/libbpf.c | 2 + tools/lib/bpf/libbpf.h | 6 + tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/netlink.c | 52 +++++- 18 files changed, 555 insertions(+), 152 deletions(-)
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 1/12 net: Add new XDP setup and query commands
From: David Ahern <[email protected]>
Add new netdev commands, XDP_SETUP_PROG_EGRESS and XDP_QUERY_PROG_EGRESS.
Update dev_change_xdp_fd and dev_xdp_install to take an egress argument. If egress bool is set, then use XDP_SETUP_PROG_EGRESS in dev_xdp_install and XDP_QUERY_PROG_EGRESS in dev_change_xdp_fd.
Update dev_xdp_uninstall to query for XDP_QUERY_PROG_EGRESS and if a program is installed call dev_xdp_install with the egress argument set to true.
This enables existing infrastructure to be used for XDP programs in the egress path.
--- include/linux/netdevice.h | 4 +++- net/core/dev.c | 30 +++++++++++++++++++++--------- net/core/rtnetlink.c | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-)
include/linux/netdevice.h +3 -1
@@ -865,8 +865,10 @@ enum bpf_netdev_command {*/ XDP_SETUP_PROG ,XDP_SETUP_PROG_HW , +XDP_SETUP_PROG_EGRESS ,XDP_QUERY_PROG ,XDP_QUERY_PROG_HW , +XDP_QUERY_PROG_EGRESS ,/* BPF program for offload callbacks, invoked at program load time. */ BPF_OFFLOAD_MAP_ALLOC , BPF_OFFLOAD_MAP_FREE,
@@ -3729,7 +3731,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf);int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack, -int fd,u32 flags); +int fd,u32 flags,bool egress);u32 __dev_xdp_query (struct net_device *dev,bpf_op_t xdp_op,enum bpf_netdev_command cmd);int xdp_umem_query (struct net_device *dev,u16 queue_id);
net/core/dev.c +21 -9
@@ -8538,7 +8538,7 @@ u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,static int dev_xdp_install (struct net_device *dev,bpf_op_t bpf_op,struct netlink_ext_ack *extack,u32 flags, -struct bpf_prog *prog) +struct bpf_prog *prog,bool egress) {bool non_hw = !(flags &XDP_FLAGS_HW_MODE );struct bpf_prog *prev_prog =NULL ;
@@ -8556,7 +8556,7 @@ static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,if (flags &XDP_FLAGS_HW_MODE ) xdp.command =XDP_SETUP_PROG_HW ;else - xdp.command =XDP_SETUP_PROG ; + xdp.command = egress ?XDP_SETUP_PROG_EGRESS :XDP_SETUP_PROG ; xdp.extack = extack; xdp.flags = flags; xdp.prog = prog;
@@ -8577,7 +8577,8 @@ static void dev_xdp_uninstall(struct net_device *dev)bpf_op_t ndo_bpf;/* Remove generic XDP */ -WARN_ON (dev_xdp_install (dev, generic_xdp_install,NULL ,0 ,NULL )); +WARN_ON (dev_xdp_install (dev, generic_xdp_install,NULL ,0 ,NULL , + false));/* Remove from the driver */ ndo_bpf = dev->netdev_ops->ndo_bpf;
@@ -8589,14 +8590,20 @@ static void dev_xdp_uninstall(struct net_device *dev)WARN_ON (ndo_bpf (dev, &xdp));if (xdp.prog_id)WARN_ON (dev_xdp_install (dev, ndo_bpf,NULL , xdp.prog_flags, -NULL )); +NULL , false));/* Remove HW offload */ memset (&xdp,0 ,sizeof (xdp)); xdp.command =XDP_QUERY_PROG_HW ;if (!ndo_bpf (dev, &xdp) && xdp.prog_id)WARN_ON (dev_xdp_install (dev, ndo_bpf,NULL , xdp.prog_flags, -NULL )); +NULL , false)); +/* Remove egress program */ +memset (&xdp,0 ,sizeof (xdp)); + xdp.command =XDP_QUERY_PROG_EGRESS ; +if (!ndo_bpf (dev, &xdp) && xdp.prog_id) +WARN_ON (dev_xdp_install (dev, ndo_bpf,NULL , xdp.prog_flags, +NULL , true)); }/**
@@ -8609,7 +8616,7 @@ static void dev_xdp_uninstall(struct net_device *dev)* Set or clear a bpf program for a device */ int dev_change_xdp_fd (struct net_device *dev,struct netlink_ext_ack *extack, -int fd,u32 flags) +int fd,u32 flags,bool egress) {const struct net_device_ops *ops = dev->netdev_ops;enum bpf_netdev_command query;
@@ -8621,7 +8628,11 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,ASSERT_RTNL (); offload = flags &XDP_FLAGS_HW_MODE ; - query = offload ?XDP_QUERY_PROG_HW :XDP_QUERY_PROG ; +if (egress) + query =XDP_QUERY_PROG_EGRESS ; +else + query = offload ?XDP_QUERY_PROG_HW :XDP_QUERY_PROG ; + bpf_op = bpf_chk = ops->ndo_bpf;if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE |XDP_FLAGS_HW_MODE ))) {
@@ -8636,7 +8647,8 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,if (fd >=0 ) {u32 prog_id; -if (!offload &&__dev_xdp_query (dev, bpf_chk,XDP_QUERY_PROG )) { +if (!offload && !egress && +__dev_xdp_query (dev, bpf_chk,XDP_QUERY_PROG )) {NL_SET_ERR_MSG (extack,"native and generic XDP can't be active at the same time" );return -EEXIST ; }
@@ -8668,7 +8680,7 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,return 0 ; } - err =dev_xdp_install (dev, bpf_op, extack, flags, prog); + err =dev_xdp_install (dev, bpf_op, extack, flags, prog, egress);if (err <0 && prog)bpf_prog_put (prog);
net/core/rtnetlink.c +1 -1
@@ -2801,7 +2801,7 @@ static int do_setlink(const struct sk_buff *skb,if (xdp[IFLA_XDP_FD ]) { err =dev_change_xdp_fd (dev, extack,nla_get_s32 (xdp[IFLA_XDP_FD ]), - xdp_flags); + xdp_flags, false);if (err) goto errout; status |=DO_SETLINK_NOTIFY ;
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 2/12 net: Add BPF_XDP_EGRESS as a bpf_attach_type
In reply to David AhernFrom: Prashant Bhole <[email protected]>
Add new bpf_attach_type, BPF_XDP_EGRESS, for BPF programs attached at the XDP layer, but the egress path.
Since egress path does not have rx_queue_index and ingress_ifindex set, update xdp_is_valid_access to block access to these entries in the xdp context when a program is attached to egress path.
--- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 8 ++++++++ tools/include/uapi/linux/bpf.h | 1 + 3 files changed, 10 insertions(+)
include/uapi/linux/bpf.h +1 -0
@@ -209,6 +209,7 @@ enum bpf_attach_type {BPF_TRACE_RAW_TP ,BPF_TRACE_FENTRY ,BPF_TRACE_FEXIT , +BPF_XDP_EGRESS , __MAX_BPF_ATTACH_TYPE };
net/core/filter.c +8 -0
@@ -6803,6 +6803,14 @@ static bool xdp_is_valid_access(int off, int size,return false; } +if (prog->expected_attach_type ==BPF_XDP_EGRESS ) { +switch (off) { +case offsetof(struct xdp_md , rx_queue_index): +case offsetof(struct xdp_md , ingress_ifindex): +return false; + } + } +switch (off) {case offsetof(struct xdp_md , data): info->reg_type =PTR_TO_PACKET ;
tools/include/uapi/linux/bpf.h +1 -0
@@ -209,6 +209,7 @@ enum bpf_attach_type {BPF_TRACE_RAW_TP ,BPF_TRACE_FENTRY ,BPF_TRACE_FEXIT , +BPF_XDP_EGRESS , __MAX_BPF_ATTACH_TYPE };
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 3/12 net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernFrom: David Ahern <[email protected]>
Add IFLA_XDP_EGRESS to if_link.h uapi to handle an XDP program attached to the egress path of a device. Add rtnl_xdp_egress_fill and helpers as the egress counterpart to the existing rtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.
--- include/uapi/linux/if_link.h | 1 + net/core/dev.c | 6 ++ net/core/rtnetlink.c | 112 ++++++++++++++++++++++++++++- tools/include/uapi/linux/if_link.h | 1 + 4 files changed, 119 insertions(+), 1 deletion(-)
include/uapi/linux/if_link.h +1 -0
@@ -170,6 +170,7 @@ enum {IFLA_PROP_LIST ,IFLA_ALT_IFNAME ,/* Alternative ifname */ IFLA_PERM_ADDRESS , +IFLA_XDP_EGRESS ,/* nested attribute with 1 or more IFLA_XDP_ attrs */ __IFLA_MAX };
net/core/dev.c +6 -0
@@ -8664,6 +8664,12 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,if (IS_ERR (prog))return PTR_ERR (prog); +if (egress && prog->expected_attach_type !=BPF_XDP_EGRESS ) { +NL_SET_ERR_MSG (extack,"XDP program in egress path must use BPF_XDP_EGRESS attach type" ); +bpf_prog_put (prog); +return -EINVAL ; + } +if (!offload &&bpf_prog_is_dev_bound (prog->aux)) {NL_SET_ERR_MSG (extack,"using device-bound program without HW_MODE flag is not supported" );bpf_prog_put (prog);
net/core/rtnetlink.c +111 -1
148 lines not on this page · raw message
tools/include/uapi/linux/if_link.h +1 -0
@@ -170,6 +170,7 @@ enum {IFLA_PROP_LIST ,IFLA_ALT_IFNAME ,/* Alternative ifname */ IFLA_PERM_ADDRESS , +IFLA_XDP_EGRESS ,/* nested attribute with 1 or more IFLA_XDP_ attrs */ __IFLA_MAX };
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 5/12 tuntap: check tun_msg_ctl type at necessary places
In reply to David AhernFrom: Prashant Bhole <[email protected]>
tun_msg_ctl is used by vhost_net to communicate with tuntap. We will introduce another type in soon. As a preparation this patch adds conditions to check tun_msg_ctl type at necessary places.
--- drivers/net/tap.c | 7 +++++-- drivers/net/tun.c | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/net/tap.c +5 -2
@@ -1203,6 +1203,7 @@ static int tap_sendmsg(struct socket *sock, struct msghdr *m, struct tap_queue *q = container_of(sock, struct tap_queue, sock);struct tun_msg_ctl *ctl = m->msg_control;struct xdp_buff *xdp; +void *ptr =NULL ;int i;if (ctl && (ctl->type ==TUN_MSG_PTR )) {
@@ -1213,8 +1214,10 @@ static int tap_sendmsg(struct socket *sock, struct msghdr *m,return 0 ; } -return tap_get_user (q, ctl ? ctl->ptr :NULL , &m->msg_iter, - m->msg_flags &MSG_DONTWAIT ); +if (ctl && ctl->type ==TUN_MSG_UBUF ) + ptr = ctl->ptr; + +return tap_get_user (q, ptr, &m->msg_iter, m->msg_flags &MSG_DONTWAIT ); } static int tap_recvmsg(struct socket *sock, struct msghdr *m,
drivers/net/tun.c +5 -1
@@ -2529,6 +2529,7 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)struct tun_struct *tun =tun_get (tfile);struct tun_msg_ctl *ctl = m->msg_control;struct xdp_buff *xdp; +void *ptr =NULL ;if (!tun)return -EBADFD ;
@@ -2560,7 +2561,10 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) goto out; } - ret =tun_get_user (tun, tfile, ctl ? ctl->ptr :NULL , &m->msg_iter, +if (ctl && ctl->type ==TUN_MSG_UBUF ) + ptr = ctl->ptr; + + ret =tun_get_user (tun, tfile, ptr, &m->msg_iter, m->msg_flags &MSG_DONTWAIT , m->msg_flags &MSG_MORE ); out:
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 7/12 vhost_net: user tap recvmsg api to access ptr ring
In reply to David AhernFrom: Prashant Bhole <[email protected]>
Currently vhost_net directly accesses ptr ring of tap driver to fetch Rx packet pointers. In order to avoid it this patch modifies tap driver's recvmsg api to do additional task of fetching Rx packet pointers.
A special struct tun_msg_ctl is already being passed via msg_control for tun Rx XDP batching. This patch extends tun_msg_ctl usage to send sub commands to recvmsg api. Now tun_recvmsg will handle commands to consume and unconsume packet pointers from ptr ring.
This will be useful in implementation of tx path XDP in tun driver, where XDP program will process the packet before it is passed to vhost_net.
--- drivers/net/tap.c | 22 ++++++++++++++++++- drivers/net/tun.c | 24 ++++++++++++++++++++- drivers/vhost/net.c | 48 +++++++++++++++++++++++++++++++----------- include/linux/if_tun.h | 18 ++++++++++++++++ 4 files changed, 98 insertions(+), 14 deletions(-)
drivers/net/tap.c +21 -1
@@ -1224,8 +1224,28 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, int flags) { struct tap_queue *q = container_of(sock, struct tap_queue, sock); -struct sk_buff *skb = m->msg_control; +struct tun_msg_ctl *ctl = m->msg_control; +struct sk_buff *skb =NULL ;int ret; + +if (ctl) { +switch (ctl->type) { +case TUN_MSG_PKT : + skb = ctl->ptr; +break ; +case TUN_MSG_CONSUME_PKTS : +return ptr_ring_consume_batched (&q->ring, + ctl->ptr, + ctl->num); +case TUN_MSG_UNCONSUME_PKTS : +ptr_ring_unconsume (&q->ring, ctl->ptr, ctl->num, + tun_ptr_free); +return 0 ; +default : +return -EINVAL ; + } + } +if (flags & ~(MSG_DONTWAIT |MSG_TRUNC )) {kfree_skb (skb);return -EINVAL ;
drivers/net/tun.c +23 -1
@@ -2544,7 +2544,8 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, { struct tun_file *tfile = container_of(sock, struct tun_file, socket);struct tun_struct *tun =tun_get (tfile); -void *ptr = m->msg_control; +struct tun_msg_ctl *ctl = m->msg_control; +void *ptr =NULL ;int ret;if (!tun) {
@@ -2552,6 +2553,27 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, goto out_free; } +if (ctl) { +switch (ctl->type) { +case TUN_MSG_PKT : + ptr = ctl->ptr; +break ; +case TUN_MSG_CONSUME_PKTS : + ret =ptr_ring_consume_batched (&tfile->tx_ring, + ctl->ptr, + ctl->num); + goto out; +case TUN_MSG_UNCONSUME_PKTS : +ptr_ring_unconsume (&tfile->tx_ring, ctl->ptr, + ctl->num, tun_ptr_free); + ret =0 ; + goto out; +default : + ret = -EINVAL ; + goto out_put_tun; + } + } +if (flags & ~(MSG_DONTWAIT |MSG_TRUNC |MSG_ERRQUEUE )) { ret = -EINVAL ; goto out_put_tun;
drivers/vhost/net.c +36 -12
90 lines not on this page · raw message
include/linux/if_tun.h +18 -0
@@ -12,8 +12,26 @@#define TUN_XDP_FLAG 0x1UL +/* +* tun_msg_ctl types +*/ +#define TUN_MSG_UBUF 1#define TUN_MSG_PTR 2 +/* +* Used for passing a packet pointer from vhost to tun +*/ +#define TUN_MSG_PKT 3 +/* +* Used for passing an array of pointer from vhost to tun. +* tun consumes packets from ptr ring and stores in pointer array. +*/ +#define TUN_MSG_CONSUME_PKTS 4 +/* +* Used for passing an array of pointer from vhost to tun. +* tun consumes get pointer from array and puts back into ptr ring. +*/ +#define TUN_MSG_UNCONSUME_PKTS 5struct tun_msg_ctl {unsigned short type;unsigned short num;
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 6/12 tun: move shared functions to if_tun.h
In reply to David AhernFrom: Prashant Bhole <[email protected]>
This patch moves tun_ptr_free and related functions to if_tun.h. We will use this function in tap.c in further patches. This was need to avoid a scenario when tap is built into kernel image, tun is module but not loaded. Below functions are moved to if_tun.h
- tun_ptr_free
- tun_xdp_to_ptr
- tun_ptr_to_xdp
- tun_ptr_free
--- drivers/net/tun.c | 33 --------------------------------- include/linux/if_tun.h | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 37 deletions(-)
drivers/net/tun.c +0 -33
@@ -64,7 +64,6 @@#include <net/xdp.h> #include <linux/seq_file.h> #include <linux/uio.h> -#include <linux/skb_array.h> #include <linux/bpf.h> #include <linux/bpf_trace.h> #include <linux/mutex.h>
@@ -249,24 +248,6 @@ struct veth {__be16 h_vlan_TCI; }; -bool tun_is_xdp_frame (void *ptr) -{ -return (unsigned long )ptr &TUN_XDP_FLAG ; -} -EXPORT_SYMBOL (tun_is_xdp_frame); - -void *tun_xdp_to_ptr (void *ptr) -{ -return (void *)((unsigned long )ptr |TUN_XDP_FLAG ); -} -EXPORT_SYMBOL (tun_xdp_to_ptr); - -void *tun_ptr_to_xdp (void *ptr) -{ -return (void *)((unsigned long )ptr & ~TUN_XDP_FLAG ); -} -EXPORT_SYMBOL (tun_ptr_to_xdp); -static int tun_napi_receive (struct napi_struct *napi,int budget) { struct tun_file *tfile = container_of(napi, struct tun_file, napi);
@@ -649,20 +630,6 @@ static struct tun_struct *tun_enable_queue(struct tun_file *tfile)return tun; } -void tun_ptr_free (void *ptr) -{ -if (!ptr) -return ; -if (tun_is_xdp_frame (ptr)) { -struct xdp_frame *xdpf =tun_ptr_to_xdp (ptr); - -xdp_return_frame (xdpf); - }else { -__skb_array_destroy_skb (ptr); - } -} -EXPORT_SYMBOL_GPL (tun_ptr_free); -static void tun_queue_purge (struct tun_file *tfile) {void *ptr;
include/linux/if_tun.h +30 -4
@@ -6,6 +6,7 @@#ifndef __IF_TUN_H#define __IF_TUN_H +#include <linux/skb_array.h> #include <uapi/linux/if_tun.h> #include <uapi/linux/virtio_net.h>
@@ -27,10 +28,35 @@ struct tun_xdp_hdr {#if defined(CONFIG_TUN ) || defined(CONFIG_TUN_MODULE )struct socket *tun_get_socket (struct file *);struct ptr_ring *tun_get_tx_ring (struct file *file); -bool tun_is_xdp_frame (void *ptr); -void *tun_xdp_to_ptr (void *ptr); -void *tun_ptr_to_xdp (void *ptr); -void tun_ptr_free (void *ptr); + +static inline bool tun_is_xdp_frame (void *ptr) +{ +return (unsigned long )ptr &TUN_XDP_FLAG ; +} + +static inline void *tun_xdp_to_ptr (void *ptr) +{ +return (void *)((unsigned long )ptr |TUN_XDP_FLAG ); +} + +static inline void *tun_ptr_to_xdp (void *ptr) +{ +return (void *)((unsigned long )ptr & ~TUN_XDP_FLAG ); +} + +static inline void tun_ptr_free (void *ptr) +{ +if (!ptr) +return ; +if (tun_is_xdp_frame (ptr)) { +struct xdp_frame *xdpf =tun_ptr_to_xdp (ptr); + +xdp_return_frame (xdpf); + }else { +__skb_array_destroy_skb (ptr); + } +} +#else #include <linux/err.h> #include <linux/errno.h>
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 9/12 tun: set egress XDP program
In reply to David AhernFrom: David Ahern <[email protected]>
This patch adds a way to set tx path XDP program in tun driver by handling XDP_SETUP_PROG_EGRESS and XDP_QUERY_PROG_EGRESS in ndo_bpf handler.
--- drivers/net/tun.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
drivers/net/tun.c +26 -8
@@ -238,6 +238,7 @@ struct tun_struct {u32 rx_batched;struct tun_pcpu_stats __percpu *pcpu_stats;struct bpf_prog __rcu *xdp_prog; +struct bpf_prog __rcu *xdp_egress_prog;struct tun_prog __rcu *steering_prog;struct tun_prog __rcu *filter_prog;struct ethtool_link_ksettings link_ksettings;
@@ -1156,15 +1157,21 @@ tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) }static int tun_xdp_set (struct net_device *dev,struct bpf_prog *prog, -struct netlink_ext_ack *extack) +bool egress,struct netlink_ext_ack *extack) {struct tun_struct *tun =netdev_priv (dev);struct tun_file *tfile;struct bpf_prog *old_prog;int i; - old_prog =rtnl_dereference (tun->xdp_prog); -rcu_assign_pointer (tun->xdp_prog, prog); +if (egress) { + old_prog =rtnl_dereference (tun->xdp_egress_prog); +rcu_assign_pointer (tun->xdp_egress_prog, prog); + }else { + old_prog =rtnl_dereference (tun->xdp_prog); +rcu_assign_pointer (tun->xdp_prog, prog); + } +if (old_prog)bpf_prog_put (old_prog);
@@ -1185,12 +1192,16 @@ static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,return 0 ; } -static u32 tun_xdp_query (struct net_device *dev) +static u32 tun_xdp_query (struct net_device *dev,bool egress) {struct tun_struct *tun =netdev_priv (dev);const struct bpf_prog *xdp_prog; - xdp_prog =rtnl_dereference (tun->xdp_prog); +if (egress) + xdp_prog =rtnl_dereference (tun->xdp_egress_prog); +else + xdp_prog =rtnl_dereference (tun->xdp_prog); +if (xdp_prog)return xdp_prog->aux->id;
@@ -1201,13 +1212,20 @@ static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) {switch (xdp->command) {case XDP_SETUP_PROG : -return tun_xdp_set (dev, xdp->prog, xdp->extack); +return tun_xdp_set (dev, xdp->prog, false, xdp->extack); +case XDP_SETUP_PROG_EGRESS : +return tun_xdp_set (dev, xdp->prog, true, xdp->extack);case XDP_QUERY_PROG : - xdp->prog_id =tun_xdp_query (dev); -return 0 ; + xdp->prog_id =tun_xdp_query (dev, false); +break ; +case XDP_QUERY_PROG_EGRESS : + xdp->prog_id =tun_xdp_query (dev, true); +break ;default :return -EINVAL ; } + +return 0 ; }static int tun_net_change_carrier (struct net_device *dev,bool new_carrier)
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 8/12 tuntap: remove usage of ptr ring in vhost_net
In reply to David AhernFrom: Prashant Bhole <[email protected]>
Remove usage of ptr ring of tuntap in vhost_net and remove the functions exported from tuntap drivers to get ptr ring.
--- drivers/net/tap.c | 13 ------------- drivers/net/tun.c | 13 ------------- drivers/vhost/net.c | 31 ++++--------------------------- include/linux/if_tap.h | 5 ----- include/linux/if_tun.h | 5 ----- 5 files changed, 4 insertions(+), 63 deletions(-)
drivers/net/tap.c +0 -13
@@ -1288,19 +1288,6 @@ struct socket *tap_get_socket(struct file *file) }EXPORT_SYMBOL_GPL (tap_get_socket); -struct ptr_ring *tap_get_ptr_ring (struct file *file) -{ -struct tap_queue *q; - -if (file->f_op != &tap_fops) -return ERR_PTR (-EINVAL ); - q = file->private_data; -if (!q) -return ERR_PTR (-EBADFD ); -return &q->ring; -} -EXPORT_SYMBOL_GPL (tap_get_ptr_ring); -int tap_queue_resize (struct tap_dev *tap) {struct net_device *dev = tap->dev;
drivers/net/tun.c +0 -13
@@ -3717,19 +3717,6 @@ struct socket *tun_get_socket(struct file *file) }EXPORT_SYMBOL_GPL (tun_get_socket); -struct ptr_ring *tun_get_tx_ring (struct file *file) -{ -struct tun_file *tfile; - -if (file->f_op != &tun_fops) -return ERR_PTR (-EINVAL ); - tfile = file->private_data; -if (!tfile) -return ERR_PTR (-EBADFD ); -return &tfile->tx_ring; -} -EXPORT_SYMBOL_GPL (tun_get_tx_ring); -module_init (tun_init);module_exit (tun_cleanup);MODULE_DESCRIPTION (DRV_DESCRIPTION );
drivers/vhost/net.c +4 -27
@@ -122,7 +122,6 @@ struct vhost_net_virtqueue {/* Reference counting for outstanding ubufs. * Protected by vq mutex. Writers must also take device mutex. */ struct vhost_net_ubuf_ref *ubufs; -struct ptr_ring *rx_ring;struct vhost_net_buf rxq;/* Batched XDP buffs */ struct xdp_buff *xdp;
@@ -997,8 +996,9 @@ static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)int len =0 ;unsigned long flags; -if (rvq->rx_ring) -return vhost_net_buf_peek (rvq); + len =vhost_net_buf_peek (rvq); +if (len) +return len;spin_lock_irqsave (&sk->sk_receive_queue.lock, flags); head =skb_peek (&sk->sk_receive_queue);
@@ -1187,7 +1187,7 @@ static void handle_rx(struct vhost_net *net) goto out; } busyloop_intr = false; -if (nvq->rx_ring) { +if (!vhost_net_buf_is_empty (&nvq->rxq)) { ctl.type =TUN_MSG_PKT ; ctl.ptr =vhost_net_buf_consume (&nvq->rxq); msg.msg_control = &ctl;
@@ -1343,7 +1343,6 @@ static int vhost_net_open(struct inode *inode, struct file *f) n->vqs[i].batched_xdp =0 ; n->vqs[i].vhost_hlen =0 ; n->vqs[i].sock_hlen =0 ; - n->vqs[i].rx_ring =NULL ;vhost_net_buf_init (&n->vqs[i].rxq); } vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
@@ -1372,7 +1371,6 @@ static struct socket *vhost_net_stop_vq(struct vhost_net *n,vhost_net_disable_vq (n, vq);vhost_net_buf_unproduce (nvq); vq->private_data =NULL ; - nvq->rx_ring =NULL ;mutex_unlock (&vq->mutex);return sock; }
@@ -1468,25 +1466,6 @@ static struct socket *get_raw_socket(int fd)return ERR_PTR (r); } -static struct ptr_ring *get_tap_ptr_ring (int fd) -{ -struct ptr_ring *ring; -struct file *file =fget (fd); - -if (!file) -return NULL ; - ring =tun_get_tx_ring (file); -if (!IS_ERR (ring)) - goto out; - ring =tap_get_ptr_ring (file); -if (!IS_ERR (ring)) - goto out; - ring =NULL ; -out: -fput (file); -return ring; -} -static struct socket *get_tap_socket (int fd) {struct file *file =fget (fd);
@@ -1570,8 +1549,6 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) r =vhost_net_enable_vq (n, vq);if (r) goto err_used; -if (index ==VHOST_NET_VQ_RX ) - nvq->rx_ring =get_tap_ptr_ring (fd); oldubufs = nvq->ubufs; nvq->ubufs = ubufs;
include/linux/if_tap.h +0 -5
@@ -4,7 +4,6 @@#if IS_ENABLED (CONFIG_TAP )struct socket *tap_get_socket (struct file *); -struct ptr_ring *tap_get_ptr_ring (struct file *file);#else #include <linux/err.h> #include <linux/errno.h>
@@ -14,10 +13,6 @@ static inline struct socket *tap_get_socket(struct file *f) {return ERR_PTR (-EINVAL ); } -static inline struct ptr_ring *tap_get_ptr_ring (struct file *f) -{ -return ERR_PTR (-EINVAL ); -}#endif /* CONFIG_TAP */ #include <net/sock.h>
include/linux/if_tun.h +0 -5
@@ -45,7 +45,6 @@ struct tun_xdp_hdr {#if defined(CONFIG_TUN ) || defined(CONFIG_TUN_MODULE )struct socket *tun_get_socket (struct file *); -struct ptr_ring *tun_get_tx_ring (struct file *file);static inline bool tun_is_xdp_frame (void *ptr) {
@@ -84,10 +83,6 @@ static inline struct socket *tun_get_socket(struct file *f) {return ERR_PTR (-EINVAL ); } -static inline struct ptr_ring *tun_get_tx_ring (struct file *f) -{ -return ERR_PTR (-EINVAL ); -}static inline bool tun_is_xdp_frame (void *ptr) {return false;
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 10/12 tun: run XDP program in tx path
In reply to David AhernFrom: Prashant Bhole <[email protected]>
Run the XDP program as soon as packet is removed from the ptr ring. Since this is XDP in tx path, the traditional handling of XDP actions XDP_TX/REDIRECT isn't valid. For this reason we call do_xdp_generic_core instead of do_xdp_generic. do_xdp_generic_core just runs the program and leaves the action handling to us.
--- drivers/net/tun.c | 153 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-)
drivers/net/tun.c +150 -3
@@ -130,6 +130,7 @@ struct tap_filter {/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal * to max number of VCPUs in guest. */ #define MAX_TAP_QUEUES 256 +#define MAX_TAP_BATCH 64#define MAX_TAP_FLOWS 4096#define TUN_FLOW_EXPIRE (3 * HZ)
@@ -175,6 +176,7 @@ struct tun_file {struct tun_struct *detached;struct ptr_ring tx_ring;struct xdp_rxq_info xdp_rxq; +void *pkt_ptrs[MAX_TAP_BATCH ]; };struct tun_page {
@@ -2140,6 +2142,107 @@ static ssize_t tun_put_user(struct tun_struct *tun,return total; } +static struct sk_buff *tun_prepare_xdp_skb (struct sk_buff *skb) +{ +struct sk_buff *nskb; + +if (skb_shared (skb) ||skb_cloned (skb)) { + nskb =skb_copy (skb,GFP_ATOMIC ); +consume_skb (skb); +return nskb; + } + +return skb; +} + +static u32 tun_do_xdp_tx_generic (struct tun_struct *tun, +struct sk_buff *skb) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + skb =tun_prepare_xdp_skb (skb); +if (!skb) { + act =XDP_DROP ; +kfree_skb (skb); + goto drop; + } + + act =do_xdp_generic_core (skb, &xdp, xdp_prog); +switch (act) { +case XDP_TX : +/* Rx path generic XDP will be called in this path +*/ +local_bh_disable (); +netif_receive_skb (skb); +local_bh_enable (); +break ; +case XDP_PASS : +break ; +case XDP_REDIRECT : +/* Since we are not handling this case yet, let's free +* skb here. In case of XDP_DROP/XDP_ABORTED, the skb +* was already freed in do_xdp_generic_core() +*/ +kfree_skb (skb); +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : + goto drop; + } + } + +return act; +drop: +this_cpu_inc (tun->pcpu_stats->tx_dropped); +return act; +} + +static u32 tun_do_xdp_tx (struct tun_struct *tun,struct tun_file *tfile, +struct xdp_frame *frame) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + xdp.data_hard_start = frame->data - frame->headroom; + xdp.data = frame->data; + xdp.data_end = xdp.data + frame->len; + xdp.data_meta = xdp.data - frame->metasize; + + act =bpf_prog_run_xdp (xdp_prog, &xdp); +switch (act) { +case XDP_PASS : +break ; +case XDP_TX : +/* fall through */ +case XDP_REDIRECT : +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : +xdp_return_frame_rx_napi (frame); +break ; + } + } + +return act; +} +static void *tun_ring_recv (struct tun_file *tfile,int noblock,int *err) {DECLARE_WAITQUEUE (wait, current);
@@ -2557,6 +2660,52 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)return ret; } +static int tun_consume_packets (struct tun_file *tfile,void **ptr_array,int n) +{ +void **pkts = tfile->pkt_ptrs; +struct xdp_frame *frame; +struct tun_struct *tun; +int i, num_ptrs; +int pkt_cnt =0 ; +void *ptr; +u32 act; +int batchsz; + +if (unlikely (!tfile)) +return 0 ; + +rcu_read_lock (); + tun =rcu_dereference (tfile->tun); +if (unlikely (!tun)) { +rcu_read_unlock (); +return 0 ; + } + +while (n) { + batchsz = (n >MAX_TAP_BATCH ) ?MAX_TAP_BATCH : n; + n -= batchsz; + num_ptrs =ptr_ring_consume_batched (&tfile->tx_ring, pkts, + batchsz); +if (!num_ptrs) +break ; +for (i =0 ; i < num_ptrs; i++) { + ptr = pkts[i]; +if (tun_is_xdp_frame (ptr)) { + frame =tun_ptr_to_xdp (ptr); + act =tun_do_xdp_tx (tun, tfile, frame); + }else { + act =tun_do_xdp_tx_generic (tun, ptr); + } + +if (act ==XDP_PASS ) + ptr_array[pkt_cnt++] = ptr; + } + } + +rcu_read_unlock (); +return pkt_cnt; +} +static int tun_recvmsg (struct socket *sock,struct msghdr *m,size_t total_len,int flags) {
@@ -2577,9 +2726,7 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, ptr = ctl->ptr;break ;case TUN_MSG_CONSUME_PKTS : - ret =ptr_ring_consume_batched (&tfile->tx_ring, - ctl->ptr, - ctl->num); + ret =tun_consume_packets (tfile, ctl->ptr, ctl->num); goto out;case TUN_MSG_UNCONSUME_PKTS : ptr_ring_unconsume(&tfile->tx_ring, ctl->ptr,
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 11/12 libbpf: Add egress XDP support
In reply to David AhernFrom: David Ahern <[email protected]>
Patch adds egress XDP support in libbpf.
First, new section name hint, xdp_egress, is added to set expected attach type at program load. Programs can use xdp_egress as the prefix in the SEC statement to load the program with the BPF_XDP_EGRESS attach type set.
Second, new APIs are added that parallel the existing xdp ones which can be changed:
bpf_set_link_xdp_egress_fd - attach program at fd to device as
xdp egress
bpf_get_link_xdp_egress_id - get id for xdp egress program bpf_get_link_xdp_egress_info - get info for xdp egress program
Internally, the libbpf code is re-factored to be common for both XDP use cases with a new egress argument to specify which netlink attribute to use.
--- tools/lib/bpf/libbpf.c | 2 ++ tools/lib/bpf/libbpf.h | 6 +++++ tools/lib/bpf/libbpf.map | 3 +++ tools/lib/bpf/netlink.c | 52 ++++++++++++++++++++++++++++++++++------ 4 files changed, 56 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +2 -0
@@ -6265,6 +6265,8 @@ static const struct bpf_sec_def section_defs[] = { .expected_attach_type = BPF_TRACE_FEXIT, .is_attach_btf = true, .attach_fn = attach_trace), +BPF_EAPROG_SEC ("xdp_egress" ,BPF_PROG_TYPE_XDP , +BPF_XDP_EGRESS ),BPF_PROG_SEC ("xdp" ,BPF_PROG_TYPE_XDP ),BPF_PROG_SEC ("perf_event" ,BPF_PROG_TYPE_PERF_EVENT ), BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
tools/lib/bpf/libbpf.h +6 -0
@@ -433,6 +433,12 @@ LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,size_t info_size,__u32 flags); +LIBBPF_API int bpf_set_link_xdp_egress_fd(int ifindex, int fd, __u32 flags); +LIBBPF_API int bpf_get_link_xdp_egress_id(int ifindex, __u32 *prog_id, +__u32 flags); +LIBBPF_API int bpf_get_link_xdp_egress_info(int ifindex, +struct xdp_link_info *info, +size_t info_size,__u32 flags);struct perf_buffer ;
tools/lib/bpf/libbpf.map +3 -0
@@ -232,4 +232,7 @@ LIBBPF_0.0.7 { bpf_program__set_struct_ops; btf__align_of; libbpf_find_kernel_btf; + bpf_set_link_xdp_egress_fd; + bpf_get_link_xdp_egress_id; + bpf_get_link_xdp_egress_info; } LIBBPF_0.0.6;
tools/lib/bpf/netlink.c +45 -7
114 lines not on this page · raw message
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 12/12 samples/bpf: xdp1, add egress XDP support
In reply to David AhernFrom: Prashant Bhole <[email protected]>
xdp1 and xdp2 now accept -E flag to set XDP program in the egress path.
--- samples/bpf/xdp1_user.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)
samples/bpf/xdp1_user.c +25 -5
@@ -21,17 +21,27 @@static int ifindex;static __u32 xdp_flags =XDP_FLAGS_UPDATE_IF_NOEXIST ;static __u32 prog_id; +static bool egress;static void int_exit (int sig) {__u32 curr_prog_id =0 ; +int err; -if (bpf_get_link_xdp_id (ifindex, &curr_prog_id, xdp_flags)) { +if (egress) + err =bpf_get_link_xdp_egress_id (ifindex, &curr_prog_id, + xdp_flags); +else + err =bpf_get_link_xdp_id (ifindex, &curr_prog_id, xdp_flags); +if (err) {printf ("bpf_get_link_xdp_id failed\n" );exit (1 ); }if (prog_id == curr_prog_id) -bpf_set_link_xdp_fd (ifindex,-1 , xdp_flags); +if (egress) +bpf_set_link_xdp_egress_fd (ifindex,-1 , xdp_flags); +else +bpf_set_link_xdp_fd (ifindex,-1 , xdp_flags);else if (!curr_prog_id)printf ("couldn't find a prog id on a given interface\n" );else
@@ -73,7 +83,8 @@ static void usage(const char *prog)"OPTS:\n" " -S use skb-mode\n" " -N enforce native mode\n" -" -F force loading prog\n" , +" -F force loading prog\n" +" -E egress path program\n" , prog); }
@@ -85,7 +96,7 @@ int main(int argc, char **argv) };struct bpf_prog_info info = {};__u32 info_len =sizeof (info); -const char *optstr ="FSN" ; +const char *optstr ="FSNE" ;int prog_fd, map_fd, opt;struct bpf_object *obj;struct bpf_map *map;
@@ -103,6 +114,9 @@ int main(int argc, char **argv)case 'F' : xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST ;break ; +case 'E' : + egress = true; +break ;default :usage (basename (argv[0 ]));return 1 ;
@@ -130,6 +144,8 @@ int main(int argc, char **argv)snprintf (filename,sizeof (filename),"%s_kern.o" , argv[0 ]); prog_load_attr.file = filename; +if (egress) + prog_load_attr.expected_attach_type =BPF_XDP_EGRESS ;if (bpf_prog_load_xattr (&prog_load_attr, &obj, &prog_fd))return 1 ;
@@ -149,7 +165,11 @@ int main(int argc, char **argv)signal (SIGINT , int_exit);signal (SIGTERM , int_exit); -if (bpf_set_link_xdp_fd (ifindex, prog_fd, xdp_flags) <0 ) { +if (egress) + err =bpf_set_link_xdp_egress_fd (ifindex, prog_fd, xdp_flags); +else + err =bpf_set_link_xdp_fd (ifindex, prog_fd, xdp_flags); +if (err <0 ) {printf ("link set xdp fd failed\n" );return 1 ; }
Signature · 1 line
2.21.1 (Apple Git-122.3)
PATCH 4/12 net: core: rename netif_receive_generic_xdp() to do_generic_xdp_core()
In reply to David AhernFrom: Jason Wang <[email protected]>
In skb generic path, we need a way to run XDP program on skb but to have customized handling of XDP actions. netif_receive_generic_xdp will be more helpful in such cases than do_xdp_generic.
This patch prepares netif_receive_generic_xdp() to be used as general purpose function by renaming it and exporting as a general purpose function. It will just run XDP program on skb but will not handle XDP actions.
--- include/linux/netdevice.h | 2 ++ net/core/dev.c | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-)
include/linux/netdevice.h +2 -0
@@ -3665,6 +3665,8 @@ static inline void dev_consume_skb_any(struct sk_buff *skb)void generic_xdp_tx (struct sk_buff *skb,struct bpf_prog *xdp_prog);int do_xdp_generic (struct bpf_prog *xdp_prog,struct sk_buff *skb); +u32 do_xdp_generic_core (struct sk_buff *skb,struct xdp_buff *xdp, +struct bpf_prog *xdp_prog);int netif_rx (struct sk_buff *skb);int netif_rx_ni (struct sk_buff *skb);int netif_receive_skb (struct sk_buff *skb);
net/core/dev.c +4 -4
@@ -4461,9 +4461,8 @@ static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)return rxqueue; } -static u32 netif_receive_generic_xdp (struct sk_buff *skb, -struct xdp_buff *xdp, -struct bpf_prog *xdp_prog) +u32 do_xdp_generic_core (struct sk_buff *skb,struct xdp_buff *xdp, +struct bpf_prog *xdp_prog) {struct netdev_rx_queue *rxqueue;void *orig_data, *orig_data_end;
@@ -4574,6 +4573,7 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,return act; } +EXPORT_SYMBOL_GPL (do_xdp_generic_core);/* When doing generic XDP we have to bypass the qdisc layer and the * network taps in order to match in-driver-XDP behavior.
@@ -4610,7 +4610,7 @@ int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)u32 act;int err; - act =netif_receive_generic_xdp (skb, &xdp, xdp_prog); + act =do_xdp_generic_core (skb, &xdp, xdp_prog);if (act !=XDP_PASS ) {switch (act) {case XDP_REDIRECT :
Signature · 1 line
2.21.1 (Apple Git-122.3)
Michael S. Tsirkin
maintainerRe: [PATCH bpf-next 10/12] tun: run XDP program in tx path
In reply to David AhernQuoted 163 lines · David Ahern
From: Prashant Bhole <[email protected]>
Run the XDP program as soon as packet is removed from the ptr ring. Since this is XDP in tx path, the traditional handling of XDP actions XDP_TX/REDIRECT isn't valid. For this reason we call
do_xdp_generic_coreinstead ofdo_xdp_generic.do_xdp_generic_corejust runs the program and leaves the action handling to us.Signed-off-by Prashant Bhole <[email protected]>--- drivers/net/tun.c | 153 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-)drivers/net/tun.c +121
-0@@ -130,6 +130,7 @@ struct tap_filter {/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal * to max number of VCPUs in guest. */ #define MAX_TAP_QUEUES 256 +#define MAX_TAP_BATCH 64#define MAX_TAP_FLOWS 4096#define TUN_FLOW_EXPIRE (3 * HZ)@@ -175,6 +176,7 @@ struct tun_file {struct tun_struct *detached;struct ptr_ring tx_ring;struct xdp_rxq_info xdp_rxq; +void *pkt_ptrs[MAX_TAP_BATCH ]; };struct tun_page {@@ -2140,6 +2142,107 @@ static ssize_t tun_put_user(struct tun_struct *tun,return total; } +static struct sk_buff *tun_prepare_xdp_skb (struct sk_buff *skb) +{ +struct sk_buff *nskb; + +if (skb_shared (skb) ||skb_cloned (skb)) { + nskb =skb_copy (skb,GFP_ATOMIC ); +consume_skb (skb); +return nskb; + } + +return skb; +} + +static u32 tun_do_xdp_tx_generic (struct tun_struct *tun, +struct sk_buff *skb) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + skb =tun_prepare_xdp_skb (skb); +if (!skb) { + act =XDP_DROP ; +kfree_skb (skb); + goto drop; + } + + act =do_xdp_generic_core (skb, &xdp, xdp_prog); +switch (act) { +case XDP_TX : +/* Rx path generic XDP will be called in this path +*/ +local_bh_disable (); +netif_receive_skb (skb); +local_bh_enable (); +break ; +case XDP_PASS : +break ; +case XDP_REDIRECT : +/* Since we are not handling this case yet, let's free +* skb here. In case of XDP_DROP/XDP_ABORTED, the skb +* was already freed in do_xdp_generic_core() +*/ +kfree_skb (skb); +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : + goto drop; + } + } + +return act; +drop: +this_cpu_inc (tun->pcpu_stats->tx_dropped); +return act; +} + +static u32 tun_do_xdp_tx (struct tun_struct *tun,struct tun_file *tfile, +struct xdp_frame *frame) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + xdp.data_hard_start = frame->data - frame->headroom; + xdp.data = frame->data; + xdp.data_end = xdp.data + frame->len; + xdp.data_meta = xdp.data - frame->metasize; + + act =bpf_prog_run_xdp (xdp_prog, &xdp); +switch (act) { +case XDP_PASS : +break ; +case XDP_TX : +/* fall through */ +case XDP_REDIRECT : +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : +xdp_return_frame_rx_napi (frame); +break ; + } + } + +return act; +} +static void *tun_ring_recv (struct tun_file *tfile,int noblock,int *err) {DECLARE_WAITQUEUE (wait, current);@@ -2557,6 +2660,52 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)return ret; } +static int tun_consume_packets (struct tun_file *tfile,void **ptr_array,int n) +{ +void **pkts = tfile->pkt_ptrs; +struct xdp_frame *frame; +struct tun_struct *tun; +int i, num_ptrs; +int pkt_cnt =0 ; +void *ptr; +u32 act; +int batchsz; + +if (unlikely (!tfile)) +return 0 ; + +rcu_read_lock (); + tun =rcu_dereference (tfile->tun); +if (unlikely (!tun)) { +rcu_read_unlock ();
drivers/net/tun.c static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) · line 2690
Quoted 10 lines
drivers/net/tun.c +10
-0+return 0 ; + } + +while (n) { + batchsz = (n >MAX_TAP_BATCH ) ?MAX_TAP_BATCH : n; + n -= batchsz; + num_ptrs =ptr_ring_consume_batched (&tfile->tx_ring, pkts, + batchsz); +if (!num_ptrs) +break ;
Can't we avoid looping over the packets in the current case where there are no xdp programs at all?
Quoted 34 lines
drivers/net/tun.c +19
-3+for (i =0 ; i < num_ptrs; i++) { + ptr = pkts[i]; +if (tun_is_xdp_frame (ptr)) { + frame =tun_ptr_to_xdp (ptr); + act =tun_do_xdp_tx (tun, tfile, frame); + }else { + act =tun_do_xdp_tx_generic (tun, ptr); + } + +if (act ==XDP_PASS ) + ptr_array[pkt_cnt++] = ptr; + } + } + +rcu_read_unlock (); +return pkt_cnt; +} +static int tun_recvmsg (struct socket *sock,struct msghdr *m,size_t total_len,int flags) {@@ -2577,9 +2726,7 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, ptr = ctl->ptr;break ;case TUN_MSG_CONSUME_PKTS : - ret =ptr_ring_consume_batched (&tfile->tx_ring, - ctl->ptr, - ctl->num); + ret =tun_consume_packets (tfile, ctl->ptr, ctl->num); goto out;case TUN_MSG_UNCONSUME_PKTS : ptr_ring_unconsume(&tfile->tx_ring, ctl->ptr,Signature · 1 line
2.21.1 (Apple Git-122.3)
Michael S. Tsirkin
maintainerRe: [PATCH bpf-next 07/12] vhost_net: user tap recvmsg api to access ptr ring
In reply to David Aherndrivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Quoted 63 lines · David Ahern
From: Prashant Bhole <[email protected]>
Currently
vhost_netdirectly accesses ptr ring of tap driver to fetch Rx packet pointers. In order to avoid it this patch modifies tap driver's recvmsg api to do additional task of fetching Rx packet pointers.A special struct
tun_msg_ctlis already being passed viamsg_controlfor tun Rx XDP batching. This patch extendstun_msg_ctlusage to send sub commands to recvmsg api. Nowtun_recvmsgwill handle commands to consume and unconsume packet pointers from ptr ring.This will be useful in implementation of tx path XDP in tun driver, where XDP program will process the packet before it is passed to
vhost_net.Signed-off-by Prashant Bhole <[email protected]>--- drivers/net/tap.c | 22 ++++++++++++++++++- drivers/net/tun.c | 24 ++++++++++++++++++++- drivers/vhost/net.c | 48 +++++++++++++++++++++++++++++++----------- include/linux/if_tun.h | 18 ++++++++++++++++ 4 files changed, 98 insertions(+), 14 deletions(-)drivers/net/tap.c +21
-1@@ -1224,8 +1224,28 @@ static int tap_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, int flags) { struct tap_queue *q = container_of(sock, struct tap_queue, sock); -struct sk_buff *skb = m->msg_control; +struct tun_msg_ctl *ctl = m->msg_control; +struct sk_buff *skb =NULL ;int ret; + +if (ctl) { +switch (ctl->type) { +case TUN_MSG_PKT : + skb = ctl->ptr; +break ; +case TUN_MSG_CONSUME_PKTS : +return ptr_ring_consume_batched (&q->ring, + ctl->ptr, + ctl->num); +case TUN_MSG_UNCONSUME_PKTS : +ptr_ring_unconsume (&q->ring, ctl->ptr, ctl->num, + tun_ptr_free); +return 0 ; +default : +return -EINVAL ; + } + } +if (flags & ~(MSG_DONTWAIT |MSG_TRUNC )) {kfree_skb (skb);return -EINVAL ;drivers/net/tun.c +0
-0@@ -2544,7 +2544,8 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
Hmm what about tap_recvmsg then?
drivers/net/tun.c static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, · line 2555
Quoted 13 lines
drivers/net/tun.c +2
-1{ struct tun_file *tfile = container_of(sock, struct tun_file, socket);struct tun_struct *tun =tun_get (tfile); -void *ptr = m->msg_control; +struct tun_msg_ctl *ctl = m->msg_control; +void *ptr =NULL ;int ret;if (!tun) {@@ -2552,6 +2553,27 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, goto out_free; }
there's an extra tun_get/tun_put above.
Do we need them?
And if yes isn't tun_ptr_free(ptr) on error a problem?
Quoted 43 lines
drivers/net/tun.c +21
-0+if (ctl) { +switch (ctl->type) { +case TUN_MSG_PKT : + ptr = ctl->ptr; +break ; +case TUN_MSG_CONSUME_PKTS : + ret =ptr_ring_consume_batched (&tfile->tx_ring, + ctl->ptr, + ctl->num); + goto out; +case TUN_MSG_UNCONSUME_PKTS : +ptr_ring_unconsume (&tfile->tx_ring, ctl->ptr, + ctl->num, tun_ptr_free); + ret =0 ; + goto out; +default : + ret = -EINVAL ; + goto out_put_tun; + } + } +if (flags & ~(MSG_DONTWAIT |MSG_TRUNC |MSG_ERRQUEUE )) { ret = -EINVAL ; goto out_put_tun;drivers/vhost/net.c +10
-0@@ -175,24 +175,44 @@ static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)static int vhost_net_buf_produce (struct vhost_net_virtqueue *nvq) { +struct vhost_virtqueue *vq = &nvq->vq; +struct socket *sock = vq->private_data;struct vhost_net_buf *rxq = &nvq->rxq; +struct tun_msg_ctl ctl = { + .type =TUN_MSG_CONSUME_PKTS , + .ptr = (void *) rxq->queue, + .num =VHOST_NET_BATCH , + }; +struct msghdr msg = { + .msg_control = &ctl, + };
drivers/vhost/net.c static void *vhost_net_buf_consume(struct vhost_net_buf *rxq) · line 196
Quoted 10 lines
drivers/vhost/net.c +4
-2rxq->head =0 ; - rxq->tail =ptr_ring_consume_batched (nvq->rx_ring, rxq->queue, -VHOST_NET_BATCH ); + rxq->tail = sock->ops->recvmsg (sock, &msg,0 ,0 ); +if (WARN_ON_ONCE (rxq->tail <0 )) + rxq->tail =0 ; +return rxq->tail; }
Hmm isn't there a way to avoid an indirect call here on data path? ptr ring is a tun/tap thing anyway ...
drivers/vhost/net.c static void *vhost_net_buf_consume(struct vhost_net_buf *rxq) · line 205
Quoted 9 lines
drivers/vhost/net.c +5
-0static void vhost_net_buf_unproduce (struct vhost_net_virtqueue *nvq) { +struct vhost_virtqueue *vq = &nvq->vq; +struct socket *sock = vq->private_data;struct vhost_net_buf *rxq = &nvq->rxq; +struct tun_msg_ctl ctl = { + .type =TUN_MSG_UNCONSUME_PKTS , + .ptr = (void *) (rxq->queue + rxq->head),
You don't really need to cast to void. An assignment will do.
Quoted 42 lines
drivers/vhost/net.c +15
-8+ .num = vhost_net_buf_get_size(rxq), + }; +struct msghdr msg = { + .msg_control = &ctl, + }; -if (nvq->rx_ring && !vhost_net_buf_is_empty (rxq)) { -ptr_ring_unconsume (nvq->rx_ring, rxq->queue + rxq->head, -vhost_net_buf_get_size (rxq), - tun_ptr_free); - rxq->head = rxq->tail =0 ; - } +if (!vhost_net_buf_is_empty (rxq)) + sock->ops->recvmsg (sock, &msg,0 ,0 ); + + rxq->head = rxq->tail =0 ; }static int vhost_net_buf_peek_len (void *ptr)@@ -1109,6 +1129,7 @@ static void handle_rx(struct vhost_net *net) .flags = 0, .gso_type = VIRTIO_NET_HDR_GSO_NONE }; +struct tun_msg_ctl ctl;size_t total_len =0 ;int err, mergeable;s16 headcount;@@ -1166,8 +1187,11 @@ static void handle_rx(struct vhost_net *net) goto out; } busyloop_intr = false; -if (nvq->rx_ring) - msg.msg_control =vhost_net_buf_consume (&nvq->rxq); +if (nvq->rx_ring) { + ctl.type =TUN_MSG_PKT ; + ctl.ptr =vhost_net_buf_consume (&nvq->rxq); + msg.msg_control = &ctl; + }/* On overrun, truncate and discard */ if (unlikely (headcount >UIO_MAXIOV )) {iov_iter_init (&msg.msg_iter,READ , vq->iov,1 ,1 );
drivers/vhost/net.c static struct socket *vhost_net_stop_vq(struct vhost_net *n, · line 1375
Quoted 7 lines
drivers/vhost/net.c +1
-1@@ -1346,8 +1370,8 @@ static struct socket *vhost_net_stop_vq(struct vhost_net *n,mutex_lock (&vq->mutex); sock = vq->private_data;vhost_net_disable_vq (n, vq); - vq->private_data =NULL ;vhost_net_buf_unproduce (nvq); + vq->private_data =NULL ; nvq->rx_ring =NULL ;
So is rx_ring still in use?
Quoted 45 lines
drivers/vhost/net.c +1
-1mutex_unlock (&vq->mutex);return sock;@@ -1538,8 +1562,8 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) }vhost_net_disable_vq (n, vq); - vq->private_data = sock;vhost_net_buf_unproduce (nvq); + vq->private_data = sock; r =vhost_vq_init_access (vq);if (r) goto err_used;include/linux/if_tun.h +18
-0@@ -12,8 +12,26 @@#define TUN_XDP_FLAG 0x1UL +/* +* tun_msg_ctl types +*/ +#define TUN_MSG_UBUF 1#define TUN_MSG_PTR 2 +/* +* Used for passing a packet pointer from vhost to tun +*/ +#define TUN_MSG_PKT 3 +/* +* Used for passing an array of pointer from vhost to tun. +* tun consumes packets from ptr ring and stores in pointer array. +*/ +#define TUN_MSG_CONSUME_PKTS 4 +/* +* Used for passing an array of pointer from vhost to tun. +* tun consumes get pointer from array and puts back into ptr ring. +*/ +#define TUN_MSG_UNCONSUME_PKTS 5struct tun_msg_ctl {unsigned short type;unsigned short num;Signature · 1 line
2.21.1 (Apple Git-122.3)
Re: [PATCH bpf-next 02/12] net: Add BPF_XDP_EGRESS as a bpf_attach_type
In reply to David AhernQuoted 8 lines · David Ahern
From: Prashant Bhole <[email protected]>
Add new
bpf_attach_type,BPF_XDP_EGRESS, for BPF programs attached at the XDP layer, but the egress path.Since egress path does not have
rx_queue_indexandingress_ifindexset, updatexdp_is_valid_accessto block access to these entries in the xdp context when a program is attached to egress path.
Isn't the whole point of this to be able to use unchanged XDP programs? But now you're introducing a semantic difference. Since supposedly only point-to-point links are going to be using this attach type, don't they know enough about their peer device to be able to populate those fields with meaningful values, instead of restricting access to them?
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.
How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
-Toke
Re: [PATCH bpf-next 02/12] net: Add BPF_XDP_EGRESS as a bpf_attach_type
In reply to Toke Høiland-JørgensenQuoted 4 lines · Toke Høiland-Jørgensen
Quoted 2 lines · David Ahern
From: Prashant Bhole <[email protected]>
Quoted 8 lines
Quoted 6 lines
Add new
bpf_attach_type,BPF_XDP_EGRESS, for BPF programs attached at the XDP layer, but the egress path.Since egress path does not have
rx_queue_indexandingress_ifindexset, updatexdp_is_valid_accessto block access to these entries in the xdp context when a program is attached to egress path.Isn't the whole point of this to be able to use unchanged XDP programs?
See patch 12. Only the userspace code was changed to load the same program with the egress attach type set.
The verifier needs to check the egress program does not access Rx only entries in xdp_md context. The attach type allows that check.
Quoted 5 lines
But now you're introducing a semantic difference. Since supposedly only point-to-point links are going to be using this attach type, don't they know enough about their peer device to be able to populate those fields with meaningful values, instead of restricting access to them?
You are conflating use cases. Don't assume point to point or peer devices.
This could be a REDIRECT from eth0 to eth1 and then an EGRESS program on eth1 to do something. In the current test scenario it is REDIRECT from eth0 to tapN and then on tapN run an egress program (Tx for a tap is ingress to the VM).
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Toke Høiland-JørgensenQuoted 10 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.
Quoted 7 lines
How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
-Toke
Nothing about this patch set is limited to point-to-point links.
Re: [PATCH bpf-next 02/12] net: Add BPF_XDP_EGRESS as a bpf_attach_type
In reply to David AhernQuoted 34 lines · David Ahern
From: Prashant Bhole <[email protected]>
Add new
bpf_attach_type,BPF_XDP_EGRESS, for BPF programs attached at the XDP layer, but the egress path.Since egress path does not have
rx_queue_indexandingress_ifindexset, updatexdp_is_valid_accessto block access to these entries in the xdp context when a program is attached to egress path.Signed-off-by Prashant Bhole <[email protected]>Signed-off-by David Ahern <[email protected]>--- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 8 ++++++++ tools/include/uapi/linux/bpf.h | 1 + 3 files changed, 10 insertions(+)include/uapi/linux/bpf.h +1
-0@@ -209,6 +209,7 @@ enum bpf_attach_type {BPF_TRACE_RAW_TP ,BPF_TRACE_FENTRY ,BPF_TRACE_FEXIT , +BPF_XDP_EGRESS , __MAX_BPF_ATTACH_TYPE };net/core/filter.c +0
-0
net/core/filter.c static bool xdp_is_valid_access(int off, int size, · line 6806
Quoted 4 lines
net/core/filter.c +1
-0@@ -6803,6 +6803,14 @@ static bool xdp_is_valid_access(int off, int size,return false; } +if (prog->expected_attach_type ==BPF_XDP_EGRESS ) {
For BPF_PROG_TYPE_XDP, the expected_attach_type is currently not enforced to be 0 in bpf_prog_load_check_attach(). Not sure if it is ok to test it here and also return false in some of the following switch cases.
Quoted 10 lines
net/core/filter.c +7
-0+switch (off) { +case offsetof(struct xdp_md , rx_queue_index): +case offsetof(struct xdp_md , ingress_ifindex): +return false; + } + } +switch (off) {case offsetof(struct xdp_md , data): info->reg_type =PTR_TO_PACKET ;
Re: [PATCH bpf-next 02/12] net: Add BPF_XDP_EGRESS as a bpf_attach_type
In reply to David AhernQuoted 26 lines · David Ahern
Quoted 4 lines · Toke Høiland-Jørgensen
Quoted 2 lines · David Ahern
From: Prashant Bhole <[email protected]>
Quoted 8 lines
Quoted 6 lines
Add new
bpf_attach_type,BPF_XDP_EGRESS, for BPF programs attached at the XDP layer, but the egress path.Since egress path does not have
rx_queue_indexandingress_ifindexset, updatexdp_is_valid_accessto block access to these entries in the xdp context when a program is attached to egress path.Isn't the whole point of this to be able to use unchanged XDP programs?
See patch 12. Only the userspace code was changed to load the same program with the egress attach type set.
The verifier needs to check the egress program does not access Rx only entries in
xdp_mdcontext. The attach type allows that check.Quoted 5 lines
But now you're introducing a semantic difference. Since supposedly only point-to-point links are going to be using this attach type, don't they know enough about their peer device to be able to populate those fields with meaningful values, instead of restricting access to them?
Quoted 6 lines
You are conflating use cases. Don't assume point to point or peer devices.
This could be a REDIRECT from eth0 to eth1 and then an EGRESS program on eth1 to do something. In the current test scenario it is REDIRECT from eth0 to tapN and then on tapN run an egress program (Tx for a tap is ingress to the VM).
But why would any hardware driver implement this program type, instead of the proper TX hook? I thought the whole idea of this "third" not-quite-TX program type was the virtualisation offload case. If you just want to be able to run eBPF code in the TX path, why not implement the proper TX hook straight away?
-Toke
Re: [PATCH bpf-next 10/12] tun: run XDP program in tx path
In reply to Michael S. TsirkinQuoted 169 lines · Michael S. Tsirkin
Quoted 168 lines · David Ahern
From: Prashant Bhole <[email protected]>
Run the XDP program as soon as packet is removed from the ptr ring. Since this is XDP in tx path, the traditional handling of XDP actions XDP_TX/REDIRECT isn't valid. For this reason we call
do_xdp_generic_coreinstead ofdo_xdp_generic.do_xdp_generic_corejust runs the program and leaves the action handling to us.Signed-off-by Prashant Bhole <[email protected]>--- drivers/net/tun.c | 153 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-)drivers/net/tun.c +126
-0@@ -130,6 +130,7 @@ struct tap_filter {/* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal * to max number of VCPUs in guest. */ #define MAX_TAP_QUEUES 256 +#define MAX_TAP_BATCH 64#define MAX_TAP_FLOWS 4096#define TUN_FLOW_EXPIRE (3 * HZ)@@ -175,6 +176,7 @@ struct tun_file {struct tun_struct *detached;struct ptr_ring tx_ring;struct xdp_rxq_info xdp_rxq; +void *pkt_ptrs[MAX_TAP_BATCH ]; };struct tun_page {@@ -2140,6 +2142,107 @@ static ssize_t tun_put_user(struct tun_struct *tun,return total; } +static struct sk_buff *tun_prepare_xdp_skb (struct sk_buff *skb) +{ +struct sk_buff *nskb; + +if (skb_shared (skb) ||skb_cloned (skb)) { + nskb =skb_copy (skb,GFP_ATOMIC ); +consume_skb (skb); +return nskb; + } + +return skb; +} + +static u32 tun_do_xdp_tx_generic (struct tun_struct *tun, +struct sk_buff *skb) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + skb =tun_prepare_xdp_skb (skb); +if (!skb) { + act =XDP_DROP ; +kfree_skb (skb); + goto drop; + } + + act =do_xdp_generic_core (skb, &xdp, xdp_prog); +switch (act) { +case XDP_TX : +/* Rx path generic XDP will be called in this path +*/ +local_bh_disable (); +netif_receive_skb (skb); +local_bh_enable (); +break ; +case XDP_PASS : +break ; +case XDP_REDIRECT : +/* Since we are not handling this case yet, let's free +* skb here. In case of XDP_DROP/XDP_ABORTED, the skb +* was already freed in do_xdp_generic_core() +*/ +kfree_skb (skb); +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : + goto drop; + } + } + +return act; +drop: +this_cpu_inc (tun->pcpu_stats->tx_dropped); +return act; +} + +static u32 tun_do_xdp_tx (struct tun_struct *tun,struct tun_file *tfile, +struct xdp_frame *frame) +{ +struct bpf_prog *xdp_prog; +struct xdp_buff xdp; +u32 act =XDP_PASS ; + + xdp_prog =rcu_dereference (tun->xdp_egress_prog); +if (xdp_prog) { + xdp.data_hard_start = frame->data - frame->headroom; + xdp.data = frame->data; + xdp.data_end = xdp.data + frame->len; + xdp.data_meta = xdp.data - frame->metasize; + + act =bpf_prog_run_xdp (xdp_prog, &xdp); +switch (act) { +case XDP_PASS : +break ; +case XDP_TX : +/* fall through */ +case XDP_REDIRECT : +/* fall through */ +default : +bpf_warn_invalid_xdp_action (act); +/* fall through */ +case XDP_ABORTED : +trace_xdp_exception (tun->dev, xdp_prog, act); +/* fall through */ +case XDP_DROP : +xdp_return_frame_rx_napi (frame); +break ; + } + } + +return act; +} +static void *tun_ring_recv (struct tun_file *tfile,int noblock,int *err) {DECLARE_WAITQUEUE (wait, current);@@ -2557,6 +2660,52 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)return ret; } +static int tun_consume_packets (struct tun_file *tfile,void **ptr_array,int n) +{ +void **pkts = tfile->pkt_ptrs; +struct xdp_frame *frame; +struct tun_struct *tun; +int i, num_ptrs; +int pkt_cnt =0 ; +void *ptr; +u32 act; +int batchsz; + +if (unlikely (!tfile)) +return 0 ; + +rcu_read_lock (); + tun =rcu_dereference (tfile->tun); +if (unlikely (!tun)) { +rcu_read_unlock (); +return 0 ; + } + +while (n) { + batchsz = (n >MAX_TAP_BATCH ) ?MAX_TAP_BATCH : n;
Quoted 8 lines
drivers/net/tun.c static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) · line 2690
Quoted 5 lines
drivers/net/tun.c +5
-0+ n -= batchsz; + num_ptrs =ptr_ring_consume_batched (&tfile->tx_ring, pkts, + batchsz); +if (!num_ptrs) +break ;Can't we avoid looping over the packets in the current case where there are no xdp programs at all?
That's doable. Thanks.
Prashant
Quoted 37 lines
Quoted 36 lines
drivers/net/tun.c +19
-3+for (i =0 ; i < num_ptrs; i++) { + ptr = pkts[i]; +if (tun_is_xdp_frame (ptr)) { + frame =tun_ptr_to_xdp (ptr); + act =tun_do_xdp_tx (tun, tfile, frame); + }else { + act =tun_do_xdp_tx_generic (tun, ptr); + } + +if (act ==XDP_PASS ) + ptr_array[pkt_cnt++] = ptr; + } + } + +rcu_read_unlock (); +return pkt_cnt; +} +static int tun_recvmsg (struct socket *sock,struct msghdr *m,size_t total_len,int flags) {@@ -2577,9 +2726,7 @@ static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, ptr = ctl->ptr;break ;case TUN_MSG_CONSUME_PKTS : - ret =ptr_ring_consume_batched (&tfile->tx_ring, - ctl->ptr, - ctl->num); + ret =tun_consume_packets (tfile, ctl->ptr, ctl->num); goto out;case TUN_MSG_UNCONSUME_PKTS : ptr_ring_unconsume(&tfile->tx_ring, ctl->ptr,Signature · 1 line
2.21.1 (Apple Git-122.3)
Re: [PATCH bpf-next 10/12] tun: run XDP program in tx path
In reply to Michael S. TsirkinQuoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · David Ahern
From: Prashant Bhole <[email protected]>
Run the XDP program as soon as packet is removed from the ptr ring. Since this is XDP in tx path, the traditional handling of XDP actions XDP_TX/REDIRECT isn't valid. For this reason we call
do_xdp_generic_coreinstead ofdo_xdp_generic.do_xdp_generic_corejust runs the program and leaves the action handling to us.Signed-off-by Prashant Bhole <[email protected]>--- drivers/net/tun.c | 153 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 3 deletions(-)drivers/net/tun.c +0
-0
[...]
Quoted 23 lines
Quoted 23 lines
drivers/net/tun.c +23
-0+static int tun_consume_packets (struct tun_file *tfile,void **ptr_array,int n) +{ +void **pkts = tfile->pkt_ptrs; +struct xdp_frame *frame; +struct tun_struct *tun; +int i, num_ptrs; +int pkt_cnt =0 ; +void *ptr; +u32 act; +int batchsz; + +if (unlikely (!tfile)) +return 0 ; + +rcu_read_lock (); + tun =rcu_dereference (tfile->tun); +if (unlikely (!tun)) { +rcu_read_unlock (); +return 0 ; + } + +while (n) { + batchsz = (n >MAX_TAP_BATCH ) ?MAX_TAP_BATCH : n;
Quoted 8 lines
drivers/net/tun.c static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) · line 2690
Quoted 5 lines
drivers/net/tun.c +5
-0+ n -= batchsz; + num_ptrs =ptr_ring_consume_batched (&tfile->tx_ring, pkts, + batchsz); +if (!num_ptrs) +break ;Can't we avoid looping over the packets in the current case where there are no xdp programs at all?
That's doable. Thanks.
Prashant
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 10 lines · David Ahern
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.
Quoted 6 lines
Quoted 4 lines
How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from XDP_REDIRECT?
A little hard to figure out the semantics when we start from a funky device like tun :S
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · David Ahern
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Quoted 8 lines
Quoted 1 line
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from
XDP_REDIRECT?A little hard to figure out the semantics when we start from a funky device like tun :S
Yes, that is also why I found this a bit weird. We have discussed plans for an XDP TX hook before:
https://github.com/xdp-project/xdp-project/blob/master/xdp-project.org#xdp-hook-at-tx
That TX hook would run for everything at TX, but it would be a separate program type with its own metadata access. Whereas the idea with this series (seemed to me) to be just to be able to "emulate" run a regular RX-side XDP program on egress for devices where this makes sense.
If this series is not meant to implement that "emulation", but rather be usable for all devices, I really think we should go straight for the full TX hook as discussed earlier...
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 31 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · David Ahern
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Quoted 8 lines
Quoted 1 line
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from
XDP_REDIRECT?A little hard to figure out the semantics when we start from a funky device like tun :S
Yes, that is also why I found this a bit weird. We have discussed plans for an XDP TX hook before:
https://github.com/xdp-project/xdp-project/blob/master/xdp-project.org#xdp-hook-at-tx
Quoted 9 lines
That TX hook would run for everything at TX, but it would be a separate program type with its own metadata access. Whereas the idea with this series (seemed to me) to be just to be able to "emulate" run a regular RX-side XDP program on egress for devices where this makes sense.
If this series is not meant to implement that "emulation", but rather be usable for all devices, I really think we should go straight for the full TX hook as discussed earlier...
The first patch set from Jason and Prashant started from the perspective of offloading XDP programs for a guest. Independently, I was looking at XDP in the TX path (now referred to as egress to avoid confusion with the XDP_TX return type). Jason and Prashant were touching some of the same code paths in the tun driver that I needed for XDP in the Tx path, so we decided to consolidate and have XDP egress done first and then offload of VMs as a followup. Offload in virtio_net can be done very similar to how it is done in nfp -- the program is passed to the host as a hardware level attach mode, and the driver verifies the program can be offloaded (e.g., does not contain helpers that expose host specific data like the fib lookup helper).
At this point, you need to stop thinking solely from the perspective of tun or tap and VM offload; think about this from the ability to run an XDP program on egress path at an appropriate place in the NIC driver that covers both skbs and xdp_frames (e.g., on a REDIRECT). This has been discussed before as a need (e.g, Toke's reference above), and I am trying to get this initial support done.
I very much wanted to avoid copy-paste-modify for the entire XDP API for this. For the most part XDP means ebpf at the NIC driver / hardware level (obviously with the exception of generic mode). The goal is tempered with the need for the verifier to reject rx entries in the xdp_md context. Hence the reason for use of an attach_type - existing infrastructure to test and reject the accesses.
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce expected_attach_type then that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 6 lines · David Ahern
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce
expected_attach_typethen that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.
This is acceptable risk. We did such thing in the past. The chances of user space breakage are extremely low.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 54 lines · On Sat, 25 Jan 2020 18:43:36 -0700 David Ahern <[email protected]> wrote:
Quoted 31 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · David Ahern
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Quoted 8 lines
Quoted 1 line
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from
XDP_REDIRECT?A little hard to figure out the semantics when we start from a funky device like tun :S
Yes, that is also why I found this a bit weird. We have discussed plans for an XDP TX hook before:
https://github.com/xdp-project/xdp-project/blob/master/xdp-project.org#xdp-hook-at-txQuoted 9 lines
That TX hook would run for everything at TX, but it would be a separate program type with its own metadata access. Whereas the idea with this series (seemed to me) to be just to be able to "emulate" run a regular RX-side XDP program on egress for devices where this makes sense.
If this series is not meant to implement that "emulation", but rather be usable for all devices, I really think we should go straight for the full TX hook as discussed earlier...
The first patch set from Jason and Prashant started from the perspective of offloading XDP programs for a guest. Independently, I was looking at XDP in the TX path (now referred to as egress to avoid confusion with the
XDP_TXreturn type). Jason and Prashant were touching some of the same code paths in the tun driver that I needed for XDP in the Tx path, so we decided to consolidate and have XDP egress done first and then offload of VMs as a followup. Offload invirtio_netcan be done very similar to how it is done in nfp -- the program is passed to the host as a hardware level attach mode, and the driver verifies the program can be offloaded (e.g., does not contain helpers that expose host specific data like the fib lookup helper).
Quoted 4 lines
At this point, you need to stop thinking solely from the perspective of tun or tap and VM offload; think about this from the ability to run an XDP program on egress path at an appropriate place in the NIC driver that covers both skbs and
xdp_frames(e.g., on a REDIRECT).
Yes, please. I want this NIC TX hook to see both SKBs and xdp_frames.
Quoted 11 lines
This has
been discussed before as a need (e.g, Toke's reference above), and I am trying to get this initial support done.I very much wanted to avoid copy-paste-modify for the entire XDP API for this. For the most part XDP means ebpf at the NIC driver / hardware level (obviously with the exception of generic mode). The goal is tempered with the need for the verifier to reject rx entries in the
xdp_mdcontext. Hence the reason for use of anattach_type- existing infrastructure to test and reject the accesses.
Quoted 5 lines
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce
expected_attach_typethen that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.
Taking about return codes. Does XDP the return codes make sense for this EGRESS hook? (if thinking about this being egress on the real NIC).
E.g. XDP_REDIRECT would have to be supported, which is interesting, but also have implications (like looping packets).
E.g. what is the semantics/action of XDP_TX return code?
E.g. I'm considering adding a XDP_CONGESTED return code that can cause backpressure towards qdisc layer.
Also think about that if this EGRESS hook uses standard prog type for XDP (BPF_PROG_TYPE_XDP), then we need to convert xdp_frame to xdp_buff (and also convert SKBs to xdp_buff).
Are we sure that reusing the same bpf prog type is the right choice?
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 17 lines · Jesper Dangaard Brouer
Quoted 11 lines
This has
been discussed before as a need (e.g, Toke's reference above), and I am trying to get this initial support done.I very much wanted to avoid copy-paste-modify for the entire XDP API for this. For the most part XDP means ebpf at the NIC driver / hardware level (obviously with the exception of generic mode). The goal is tempered with the need for the verifier to reject rx entries in the
xdp_mdcontext. Hence the reason for use of anattach_type- existing infrastructure to test and reject the accesses.Quoted 5 lines
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce
expected_attach_typethen that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.
Quoted 7 lines
Taking about return codes. Does XDP the return codes make sense for this EGRESS hook? (if thinking about this being egress on the real NIC).
E.g.
XDP_REDIRECTwould have to be supported, which is interesting, but also have implications (like looping packets).E.g. what is the semantics/action of
XDP_TXreturn code?
This has been discussed. XDP_TX in the EGRESS path could arguably be equal to XDP_PASS.
Quoted 7 lines
E.g. I'm considering adding a
XDP_CONGESTEDreturn code that can cause backpressure towards qdisc layer.Also think about that if this EGRESS hook uses standard prog type for XDP (
BPF_PROG_TYPE_XDP), then we need to convertxdp_frametoxdp_buff(and also convert SKBs toxdp_buff).
Why? What about the patch set requires that change to be done to have support for EGRESS path?
Quoted 3 lines
Are we sure that reusing the same bpf prog type is the right choice?
Martin's comment about existing checking on the expected attach type is the only reason I have seen so far to not have the same program type.
Looking at the helpers for use in XDP programs do you believe any of those should not be allowed with EGRESS programs? Do you have any reason to think that existing XDP capabilities should be prohibited or different for EGRESS? As mentioned earlier the attach type can be used to have the verifier handle small context differences (and restrict helpers if needed).
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 36 lines · David Ahern
Quoted 35 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · David Ahern
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Quoted 8 lines
Quoted 1 line
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from
XDP_REDIRECT?A little hard to figure out the semantics when we start from a funky device like tun :S
Yes, that is also why I found this a bit weird. We have discussed plans for an XDP TX hook before:
https://github.com/xdp-project/xdp-project/blob/master/xdp-project.org#xdp-hook-at-txThat TX hook would run for everything at TX, but it would be a separate program type with its own metadata access. Whereas the idea with this series (seemed to me) to be just to be able to "emulate" run a regular RX-side XDP program on egress for devices where this makes sense.
Quoted 8 lines
Quoted 3 lines
If this series is not meant to implement that "emulation", but rather be usable for all devices, I really think we should go straight for the full TX hook as discussed earlier...
The first patch set from Jason and Prashant started from the perspective of offloading XDP programs for a guest. Independently, I was looking at XDP in the TX path (now referred to as egress to avoid confusion with the
XDP_TXreturn type).
I looked through the commit message and the cover letter again, and you never explain why you need the egress hook. Could you please clarify your needs? If it's container-related maybe what Daniel talked about at last netconf could be a better solution?
I can't quite square the concept of XDP which started as close to the metal BPF hook for HW drivers, and this heavily SW-focused addition.
Quoted 8 lines
Jason and Prashant were touching some of the
same code paths in the tun driver that I needed for XDP in the Tx path, so we decided to consolidate and have XDP egress done first and then offload of VMs as a followup. Offload invirtio_netcan be done very similar to how it is done in nfp -- the program is passed to the host as a hardware level attach mode, and the driver verifies the program can be offloaded (e.g., does not contain helpers that expose host specific data like the fib lookup helper).
<rant>
I'd ask to please never compare this work to the nfp offload. Netronome was able to open up their NIC down to the instruction set level, with the JIT in tree and rest of the FW open source:
https://github.com/Netronome/nic-firmware/
and that work is now used as precedent for something that risks turning the kernel into a damn control plane for proprietary clouds?
I can see how they may seem similar in operational terms, but for people who care about open source they couldn't be more different.
</rant>
Quoted 6 lines
At this point, you need to stop thinking solely from the perspective of tun or tap and VM offload; think about this from the ability to run an XDP program on egress path at an appropriate place in the NIC driver that covers both skbs and
xdp_frames(e.g., on a REDIRECT). This has been discussed before as a need (e.g, Toke's reference above), and I am trying to get this initial support done.
TX hook related to queuing is a very different beast than just a RX hook flipped. The queuing is a problem that indeed needs work, but just adding a mirror RX hook does not solve that, and establishes semantics which may be counter productive. That's why I was asking for clear semantics.
Quoted 6 lines
I very much wanted to avoid copy-paste-modify for the entire XDP API for this. For the most part XDP means ebpf at the NIC driver / hardware level (obviously with the exception of generic mode). The goal is tempered with the need for the verifier to reject rx entries in the
xdp_mdcontext. Hence the reason for use of anattach_type- existing infrastructure to test and reject the accesses.
For the offload host rx queue == dev PCI tx queue and vice versa. So other than the name the rejection makes no sense. Just add a union to xdp_md so both tx and rx names can be used.
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jesper Dangaard BrouerQuoted 1 line · Jesper Dangaard Brouer
Yes, please. I want this NIC TX hook to see both SKBs and
xdp_frames.
Any pointers on what for? Unless we see actual use cases there's a justifiable concern of the entire thing just being an application of "We can solve any problem by introducing an extra level of indirection."
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 4 lines · Jakub Kicinski
I looked through the commit message and the cover letter again, and you never explain why you need the egress hook. Could you please clarify your needs?
XDP is about efficient network processing - ie., bypassing the Linux stack when it does not make sense for the person deploying some solution. XDP right now is Rx centric.
I want to run an ebpf program in the Tx path of the NIC regardless of how the packet arrived at the device -- as an skb or an xdp_frame. There are options for running programs on skb-based packets (e.g., tc). There are *zero* options for manipulating/controlling/denying xdp_frames - e.g., one REDIRECTED from an ingress device.
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 8 lines · David Ahern
Quoted 3 lines · Jakub Kicinski
I looked through the commit message and the cover letter again, and you never explain why you need the egress hook. Could you please clarify your needs?
XDP is about efficient network processing - ie., bypassing the Linux stack when it does not make sense for the person deploying some solution. XDP right now is Rx centric.
Network hardware is also "Rx centric" and somehow it works..
Quoted 5 lines
I want to run an ebpf program in the Tx path of the NIC regardless of how the packet arrived at the device -- as an skb or an
xdp_frame. There are options for running programs on skb-based packets (e.g., tc). There are *zero* options for manipulating/controlling/denyingxdp_frames- e.g., one REDIRECTED from an ingress device.
Okay - so no precise use case. You can run the same program at the end of whatever is doing the redirect (especially with Alexei's work on linking) and from cls_bpf 🤷♂️
I'm sure all driver authors can't wait to plough through their TX paths :/
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 7 lines · Jakub Kicinski
Quoted 5 lines
I want to run an ebpf program in the Tx path of the NIC regardless of how the packet arrived at the device -- as an skb or an
xdp_frame. There are options for running programs on skb-based packets (e.g., tc). There are *zero* options for manipulating/controlling/denyingxdp_frames- e.g., one REDIRECTED from an ingress device.Okay - so no precise use case. You can run the same program at the
For the sake of this discussion, consider a per-VM, per-tap device ebpf program for firewall / ACL / traffic verification (packet manipulation is also a possibility). Small, singly focused ebpf programs - attached at startup, driven by maps, cleaned up when the tap device is destroyed. (Remember: Tx for tap device is ingress to a VM.)
Small, singly focused programs only run for traffic to be delivered to the VM. Setup is easy; cleanup automatic. How the traffic got there could vary - from a bridge (L2 forwarding), the host stack (L3 routing), or an XDP program on the host's NICs. It could have arrived at the host with an encap header which is removed and the inner packet forwarded to the VM.
Quoted 1 line
end of whatever is doing the redirect (especially with Alexei's work
There are use cases where they may make sense, but this is not one.
Quoted 2 lines
on linking) and from
cls_bpf🤷♂️
cls_bpf is tc based == skb, no? I want to handle any packet, regardless of how it arrived at the device's xmit function.
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 8 lines · David Ahern
Quoted 1 line
end of whatever is doing the redirect (especially with Alexei's work
There are use cases where they may make sense, but this is not one.
Quoted 1 line
on linking) and from
cls_bpf🤷♂️
cls_bpfis tc based == skb, no? I want to handle any packet, regardless of how it arrived at the device's xmit function.
Yes, that's why I said you need the same rules in XDP before REDIRECT and cls_bpf. Sure it's more complex, but (1) it's faster to drop in the ingress prog before going though the entire redirect code and without parsing the packet twice and (2) no extra kernel code necessary.
Even the VM "offload" work doesn't need this. Translating an XDP prog into a cls_bpf one should be trivial. Slap on some prologue to linearize the skb, move ctx offsets around, slap on an epilogue to convert exit codes, anything else?
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs. And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 6 lines · On Sun, 26 Jan 2020 14:17:01 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 1 line · Jesper Dangaard Brouer
Yes, please. I want this NIC TX hook to see both SKBs and
xdp_frames.Any pointers on what for? Unless we see actual use cases there's a justifiable concern of the entire thing just being an application of "We can solve any problem by introducing an extra level of indirection."
I have two use-cases:
(1) For XDP easier handling of interface specific setting on egress, e.g. pushing a VLAN-id, instead of having to figure this out in RX hook. (I think this is also David Ahern's use-case)
(2) I want this egress XDP hook to have the ability to signal backpressure. Today we have BQL in most drivers (which is essential to avoid bufferbloat). For XDP_REDIRECT we don't, which we must solve.
For use-case(2), we likely need a BPF-helper calling netif_tx_stop_queue(), or a return code that can stop the queue towards the higher layers.
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 10 lines · Jesper Dangaard Brouer
Quoted 6 lines · On Sun, 26 Jan 2020 14:17:01 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 1 line · Jesper Dangaard Brouer
Yes, please. I want this NIC TX hook to see both SKBs and
xdp_frames.Any pointers on what for? Unless we see actual use cases there's a justifiable concern of the entire thing just being an application of "We can solve any problem by introducing an extra level of indirection."
Quoted 5 lines
I have two use-cases:
(1) For XDP easier handling of interface specific setting on egress, e.g. pushing a VLAN-id, instead of having to figure this out in RX hook. (I think this is also David Ahern's use-case)
Is it really useful to have a hook before multi-buffer frames are possible and perhaps TSO? The local TCP performance is going to tank with XDP enabled otherwise.
Quoted 6 lines
(2) I want this egress XDP hook to have the ability to signal backpressure. Today we have BQL in most drivers (which is essential to avoid bufferbloat). For
XDP_REDIRECTwe don't, which we must solve.For use-case(2), we likely need a BPF-helper calling
netif_tx_stop_queue(), or a return code that can stop the queue towards the higher layers.
Agreed, although for that use case, I'm not sure if non-XDP frames have to pass trough the hook. Hard to tell as the current patches don't touch on this use case.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 54 lines · David Ahern
Quoted 31 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Jakub Kicinski
Quoted 15 lines · David Ahern
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 7 lines · David Ahern
From: David Ahern <[email protected]>
Add
IFLA_XDP_EGRESStoif_link.huapi to handle an XDP program attached to the egress path of a device. Addrtnl_xdp_egress_filland helpers as the egress counterpart to the existingrtnl_xdp_fill. The expectation is that going forward egress path will acquire the various levels of attach - generic, driver and hardware.How would a 'hardware' attach work for this? As I said in my reply to the previous patch, isn't this explicitly for emulating XDP on the other end of a point-to-point link? How would that work with offloaded programs?
Quoted 8 lines
Quoted 1 line
Nothing about this patch set is limited to point-to-point links.
I struggle to understand of what the expected semantics of this new hook are. Is this going to be run on all frames sent to the device from the stack? All frames from the stack and from
XDP_REDIRECT?A little hard to figure out the semantics when we start from a funky device like tun :S
Yes, that is also why I found this a bit weird. We have discussed plans for an XDP TX hook before:
https://github.com/xdp-project/xdp-project/blob/master/xdp-project.org#xdp-hook-at-txQuoted 9 lines
That TX hook would run for everything at TX, but it would be a separate program type with its own metadata access. Whereas the idea with this series (seemed to me) to be just to be able to "emulate" run a regular RX-side XDP program on egress for devices where this makes sense.
If this series is not meant to implement that "emulation", but rather be usable for all devices, I really think we should go straight for the full TX hook as discussed earlier...
The first patch set from Jason and Prashant started from the perspective of offloading XDP programs for a guest. Independently, I was looking at XDP in the TX path (now referred to as egress to avoid confusion with the
XDP_TXreturn type). Jason and Prashant were touching some of the same code paths in the tun driver that I needed for XDP in the Tx path, so we decided to consolidate and have XDP egress done first and then offload of VMs as a followup. Offload invirtio_netcan be done very similar to how it is done in nfp -- the program is passed to the host as a hardware level attach mode, and the driver verifies the program can be offloaded (e.g., does not contain helpers that expose host specific data like the fib lookup helper).
Quoted 6 lines
At this point, you need to stop thinking solely from the perspective of tun or tap and VM offload; think about this from the ability to run an XDP program on egress path at an appropriate place in the NIC driver that covers both skbs and
xdp_frames(e.g., on a REDIRECT). This has been discussed before as a need (e.g, Toke's reference above), and I am trying to get this initial support done.
Right, so what we're discussing here *is* the "full" egress hook we've discussed earlier. I thought this was still the other thing, which is why I was confused.
Quoted 7 lines
I very much wanted to avoid copy-paste-modify for the entire XDP API for this. For the most part XDP means ebpf at the NIC driver / hardware level (obviously with the exception of generic mode). The goal is tempered with the need for the verifier to reject rx entries in the
xdp_mdcontext. Hence the reason for use of anattach_type- existing infrastructure to test and reject the accesses.
Quoted 5 lines
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce
expected_attach_typethen that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.
In any case an egress program will differ in:
- The context object (the RX-related fields will be invalid on egress, and we'll probably want to add new TX-related ones, such as HW TX-queue occupancy).
- The return code semantics (even if XDP_TX becomes equivalent to XDP_PASS, that is still a semantic difference from the RX side; and it's not necessarily clear whether we'll want to support REDIRECT on the egress side either, is it?)
So we'll have to disambiguate between the two different types of programs. Which means that what we're discussing is really whether that disambiguation should be encoded in the program type, or in the attach type. IMO, making it a separate program type is a clearer and more explicit UAPI. The verifier could still treat the two program types as basically equivalent except for those cases where there has to be a difference anyway. So it seems to me that all you are saving by using attach_type instead of program type is the need for a new enum value and a bunch of additions to switch statements? Or am I wildly underestimating the effort to add a new program type?
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 13 lines · Jakub Kicinski
Quoted 12 lines · Jesper Dangaard Brouer
Quoted 6 lines · On Sun, 26 Jan 2020 14:17:01 -0800 Jakub Kicinski <[email protected]> wrote:
Quoted 1 line · Jesper Dangaard Brouer
Yes, please. I want this NIC TX hook to see both SKBs and
xdp_frames.Any pointers on what for? Unless we see actual use cases there's a justifiable concern of the entire thing just being an application of "We can solve any problem by introducing an extra level of indirection."
I have two use-cases:
Quoted 7 lines
Quoted 3 lines
(1) For XDP easier handling of interface specific setting on egress, e.g. pushing a VLAN-id, instead of having to figure this out in RX hook. (I think this is also David Ahern's use-case)
Is it really useful to have a hook before multi-buffer frames are possible and perhaps TSO? The local TCP performance is going to tank with XDP enabled otherwise.
For a software router (i.e., something that mostly forwards packets) it can still be useful without multi-buffer. But yeah, that is definitely something we need to solve, regardless of where this goes.
Quoted 10 lines
Quoted 6 lines
(2) I want this egress XDP hook to have the ability to signal backpressure. Today we have BQL in most drivers (which is essential to avoid bufferbloat). For
XDP_REDIRECTwe don't, which we must solve.For use-case(2), we likely need a BPF-helper calling
netif_tx_stop_queue(), or a return code that can stop the queue towards the higher layers.Agreed, although for that use case, I'm not sure if non-XDP frames have to pass trough the hook. Hard to tell as the current patches don't touch on this use case.
I think it would be weird and surprising if it *doesn't* see packets from the stack. On RX, XDP sees everything; the natural expectation would be that this was also the case on TX, no?
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub Kicinski[ skipping the first part to just comment on the below: ]
Quoted 2 lines
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs.
I kinda agree with this; but on the other hand, if we have to wait for all drivers to catch up, that would mean we couldn't add *anything* new that requires driver changes, which is not ideal either :/
Quoted 4 lines
And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
I do share your concern that this will end up being incompatible with whatever solution we end up with for queueing. However, I don't necessarily think it will: I view the XDP egress hook as something that in any case will run *after* packets are dequeued from whichever intermediate queueing it has been through (if any). I think such a hook is missing in any case; for instance, it's currently impossible to implement something like CoDel (which needs to know how long a packet spent in the queue) in eBPF.
-Toke
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 2 lines
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs.
I kinda agree with this; but on the other hand, if we have to wait for all drivers to catch up, that would mean we couldn't add *anything* new that requires driver changes, which is not ideal either :/
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to XDP_TX handling in the drivers), making drivers smaller and XDP feature velocity higher.
I think loading the drivers with complexity is hurting us in so many ways..
Quoted 5 lines
Quoted 4 lines
And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
Quoted 8 lines
I do share your concern that this will end up being incompatible with whatever solution we end up with for queueing. However, I don't necessarily think it will: I view the XDP egress hook as something that in any case will run *after* packets are dequeued from whichever intermediate queueing it has been through (if any). I think such a hook is missing in any case; for instance, it's currently impossible to implement something like CoDel (which needs to know how long a packet spent in the queue) in eBPF.
Possibly 🤔 I don't have a good mental image of how the XDP queuing would work.
Maybe once the queuing primitives are defined they can easily be hooked into the Qdisc layer. With Martin's recent work all we need is a fifo that can store skb pointers, really...
It'd be good if the BPF queuing could replace TC Qdiscs, rather than layer underneath.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 11 lines · Jakub Kicinski
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 2 lines
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs.
I kinda agree with this; but on the other hand, if we have to wait for all drivers to catch up, that would mean we couldn't add *anything* new that requires driver changes, which is not ideal either :/
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to
XDP_TXhandling in the drivers), making drivers smaller and XDP feature velocity higher.
But if it's only for XDP frames that are REDIRECTed, then one might as well perform whatever action the TX hook was doing before REDIRECTing (as you yourself argued)... :)
Quoted 2 lines
I think loading the drivers with complexity is hurting us in so many ways..
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
Ultimately, I think Jesper's idea of having drivers operate exclusively on XDP frames and have the skb handling entirely in the core is an intriguing way to resolve this problem. Though this is obviously a long-term thing, and one might reasonably doubt we'll ever get there for existing drivers...
Quoted 17 lines
Quoted 5 lines
Quoted 4 lines
And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
Quoted 8 lines
I do share your concern that this will end up being incompatible with whatever solution we end up with for queueing. However, I don't necessarily think it will: I view the XDP egress hook as something that in any case will run *after* packets are dequeued from whichever intermediate queueing it has been through (if any). I think such a hook is missing in any case; for instance, it's currently impossible to implement something like CoDel (which needs to know how long a packet spent in the queue) in eBPF.
Possibly 🤔 I don't have a good mental image of how the XDP queuing would work.
Quoted 6 lines
Maybe once the queuing primitives are defined they can easily be hooked into the Qdisc layer. With Martin's recent work all we need is a fifo that can store skb pointers, really...
It'd be good if the BPF queuing could replace TC Qdiscs, rather than layer underneath.
Hmm, hooking into the existing qdisc layer is an interesting idea. Ultimately, I fear it won't be feasible for performance reasons; but it's certainly something to consider. Maybe at least as an option?
-Toke
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 9 lines · Toke Høiland-Jørgensen
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 2 lines
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs.
I kinda agree with this; but on the other hand, if we have to wait for all drivers to catch up, that would mean we couldn't add *anything* new that requires driver changes, which is not ideal either :/
Quoted 7 lines
Quoted 3 lines
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to
XDP_TXhandling in the drivers), making drivers smaller and XDP feature velocity higher.But if it's only for XDP frames that are REDIRECTed, then one might as well perform whatever action the TX hook was doing before REDIRECTing (as you yourself argued)... :)
Right, that's why I think the design needs to start from queuing which can't be done today, and has to be done in context of the destination. Solving queuing justifies the added complexity if you will :)
Quoted 6 lines
Quoted 2 lines
I think loading the drivers with complexity is hurting us in so many ways..
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?
I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 27 lines
Ultimately, I think Jesper's idea of having drivers operate exclusively on XDP frames and have the skb handling entirely in the core is an intriguing way to resolve this problem. Though this is obviously a long-term thing, and one might reasonably doubt we'll ever get there for existing drivers...
Quoted 21 lines
Quoted 5 lines
Quoted 4 lines
And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
Quoted 8 lines
I do share your concern that this will end up being incompatible with whatever solution we end up with for queueing. However, I don't necessarily think it will: I view the XDP egress hook as something that in any case will run *after* packets are dequeued from whichever intermediate queueing it has been through (if any). I think such a hook is missing in any case; for instance, it's currently impossible to implement something like CoDel (which needs to know how long a packet spent in the queue) in eBPF.
Possibly 🤔 I don't have a good mental image of how the XDP queuing would work.
Maybe once the queuing primitives are defined they can easily be hooked into the Qdisc layer. With Martin's recent work all we need is a fifo that can store skb pointers, really...
Quoted 6 lines
Quoted 2 lines
It'd be good if the BPF queuing could replace TC Qdiscs, rather than layer underneath.
Hmm, hooking into the existing qdisc layer is an interesting idea. Ultimately, I fear it won't be feasible for performance reasons; but it's certainly something to consider. Maybe at least as an option?
For forwarding sure, but for locally generated traffic.. 🤷♂️
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 7 lines · Jakub Kicinski
Quoted 6 lines · David Ahern
Quoted 1 line
end of whatever is doing the redirect (especially with Alexei's work
There are use cases where they may make sense, but this is not one.
Quoted 1 line
on linking) and from
cls_bpf🤷♂️
Quoted 7 lines
Quoted 2 lines
cls_bpfis tc based == skb, no? I want to handle any packet, regardless of how it arrived at the device's xmit function.Yes, that's why I said you need the same rules in XDP before REDIRECT and
cls_bpf. Sure it's more complex, but (1) it's faster to drop in the ingress prog before going though the entire redirect code and without parsing the packet twice and (2) no extra kernel code necessary.
you are making a lot of assumptions and frankly it's the 'c' word (complex) that I want to avoid. I do not believe in the OVS style of packet processing - one gigantic program with a bunch of logic and data driven flow lookups that affect every packet. I prefer simple, singly focused programs logically concatenated to make decisions. Simpler management, simpler lifecycle. The scope, scale and lifecycle management of VMs/containers is just as important as minimizing the cycles spent per packet. XDP in the Tx path is a missing primitive to make life simple.
As an example, the program on the ingress NICs to the host can be nothing more than a demux'er - use the ethernet header and vlan (preferably with vlan offload enabled) to do a <vlan,mac> lookup. No packet parsing at all at this level. The lookup returns an index of where to redirect the packet (e.g., the tap device of a VM). At the same time, packets can hit the "slow path" - processing support from the full stack when it is needed and still packets end up at the tap device. *IF* there is an ingress ACL for that tap device managed by the host, it can exist in 1 place - a program and map attached to the tap device limited to that tap device's networking function (VMs can have multiple connections to different networks with different needs at this point), and the program gets run for both XDP fast path and skb slow path.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 2 lines · Jakub Kicinski
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to
XDP_TXhandling in the drivers),
It is not. I have said multiple times it is to work on ALL packets that hit the xmit function, both skbs and xdp_frames.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Toke Høiland-JørgensenQuoted 4 lines · Toke Høiland-Jørgensen
I think it would be weird and surprising if it *doesn't* see packets from the stack. On RX, XDP sees everything; the natural expectation would be that this was also the case on TX, no?
Yes. XDP_EGRESS should be symmetrical with XDP on the Rx side.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Toke Høiland-JørgensenQuoted 6 lines · Toke Høiland-Jørgensen
In any case an egress program will differ in:
- The context object (the RX-related fields will be invalid on egress, and we'll probably want to add new TX-related ones, such as HW TX-queue occupancy).
Jakub has suggested that rx_queue_index can be a union with tx_queue_index; the former for the Rx path and the latter for the egress.
The rest of the fields in xdp_md are legit for either direction.
Quoted 5 lines
- The return code semantics (even if
XDP_TXbecomes equivalent toXDP_PASS, that is still a semantic difference from the RX side; and it's not necessarily clear whether we'll want to support REDIRECT on the egress side either, is it?)
Why should REDIRECT not be allowed in the egress path? e.g., service chaining or capturing suspicious packets (e.g., encap with a header and redirect somewhere for analysis).
Quoted 12 lines
So we'll have to disambiguate between the two different types of programs. Which means that what we're discussing is really whether that disambiguation should be encoded in the program type, or in the attach type. IMO, making it a separate program type is a clearer and more explicit UAPI. The verifier could still treat the two program types as basically equivalent except for those cases where there has to be a difference anyway. So it seems to me that all you are saving by using
attach_typeinstead of program type is the need for a new enum value and a bunch of additions to switch statements? Or am I wildly underestimating the effort to add a new program type?
IMHO that is duplicating code and APIs for no real reason. XDP refers to fast path processing, the only difference is where the program is attached - Rx side or Tx side.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Alexei StarovoitovQuoted 11 lines · Alexei Starovoitov
Quoted 6 lines · David Ahern
That said, Martin's comment throws a wrench in the goal: if the existing code does not enforce
expected_attach_typethen that option can not be used in which case I guess I have to go with a new program type (BPF_PROG_TYPE_XDP_EGRESS) which takes a new context (xdp_egress_md), has different return codes, etc.This is acceptable risk. We did such thing in the past. The chances of user space breakage are extremely low.
Ultimately that is a decision for the maintainers. Code wise both iproute2 and libbpf always initialize bpf_attr to 0 and given the many uses of that union it seems odd that someone would initialize one field at a time.
Unless someone comes back with a strong 'hell, no' I am planning to send the next RFC version with the current API.
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 6 lines · David Ahern
Quoted 2 lines · Jakub Kicinski
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to
XDP_TXhandling in the drivers),It is not. I have said multiple times it is to work on ALL packets that hit the xmit function, both skbs and
xdp_frames.
Okay, I should have said "was to be", I guess?
If EGRESS _was_to_be_ only for XDP frames we could try to hide the handling in the core (with slight changes to XDP_TX handling in the drivers), making drivers smaller and XDP feature velocity higher.
I understand you'd like a hook for all packets, that is clear. I'm just trying to highlight the cost and consequences.
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 9 lines · David Ahern
you are making a lot of assumptions and frankly it's the 'c' word (complex) that I want to avoid. I do not believe in the OVS style of packet processing - one gigantic program with a bunch of logic and data driven flow lookups that affect every packet. I prefer simple, singly focused programs logically concatenated to make decisions. Simpler management, simpler lifecycle. The scope, scale and lifecycle management of VMs/containers is just as important as minimizing the cycles spent per packet. XDP in the Tx path is a missing primitive to make life simple.
Quoted 12 lines
As an example, the program on the ingress NICs to the host can be nothing more than a demux'er - use the ethernet header and vlan (preferably with vlan offload enabled) to do a <vlan,mac> lookup. No packet parsing at all at this level. The lookup returns an index of where to redirect the packet (e.g., the tap device of a VM). At the same time, packets can hit the "slow path" - processing support from the full stack when it is needed and still packets end up at the tap device. *IF* there is an ingress ACL for that tap device managed by the host, it can exist in 1 place - a program and map attached to the tap device limited to that tap device's networking function (VMs can have multiple connections to different networks with different needs at this point), and the program gets run for both XDP fast path and skb slow path.
I think our perspectives will be difficult to reconcile.
My reading is that you look at this from slow software device perspective. Of which there are few (and already loaded with hacks). And you want the control plane to be simple rather than performance.
I look at this from HW driver perspective of which there are many. Saying that it's fine to load TX paths of all drivers with extra code, and that it's fine to effectively disable SG in the entire system just doesn't compute.
Is that a fair summary of the arguments?
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 7 lines · Jakub Kicinski
I think our perspectives will be difficult to reconcile.
My reading is that you look at this from slow software device perspective. Of which there are few (and already loaded with hacks). And you want the control plane to be simple rather than performance.
Quoted 7 lines
I look at this from HW driver perspective of which there are many. Saying that it's fine to load TX paths of all drivers with extra code, and that it's fine to effectively disable SG in the entire system just doesn't compute.
Is that a fair summary of the arguments?
I do not believe so.
My argument is about allowing XDP and full stack to work
synergistically: Fast path for known unicast traffic, and slow path for BUM traffic without duplicating config such as ACLs and forcing a specific design (such as forcing all programs in the XDP Rx path which does not work for all traffic patterns). For example, if I am willing to trade a few cycles of over head (redirect processing) for a simpler overall solution then I should be to do that. Fast path with XDP is still faster than the full host stack for known unicast traffic.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiQuoted 14 lines · Jakub Kicinski
Quoted 13 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Jakub Kicinski
Quoted 6 lines · Toke Høiland-Jørgensen
Quoted 2 lines
I'm weary of partially implemented XDP features, EGRESS prog does us no good when most drivers didn't yet catch up with the REDIRECTs.
I kinda agree with this; but on the other hand, if we have to wait for all drivers to catch up, that would mean we couldn't add *anything* new that requires driver changes, which is not ideal either :/
If EGRESS is only for XDP frames we could try to hide the handling in the core (with slight changes to
XDP_TXhandling in the drivers), making drivers smaller and XDP feature velocity higher.
Quoted 7 lines
Quoted 3 lines
But if it's only for XDP frames that are REDIRECTed, then one might as well perform whatever action the TX hook was doing before REDIRECTing (as you yourself argued)... :)
Right, that's why I think the design needs to start from queuing which can't be done today, and has to be done in context of the destination. Solving queuing justifies the added complexity if you will :)
Right, that makes sense. Hmm, I wonder if a TX driver hook is enough? I.e., a driver callback in the TX path could also just queue that packet (returning XDP_QUEUED?), without the driver needing any more handling than it will already have? I'm spitballing a little bit here, but it may be quite straight-forward? :)
Quoted 41 lines
Quoted 6 lines
Quoted 2 lines
I think loading the drivers with complexity is hurting us in so many ways..
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 27 lines
Ultimately, I think Jesper's idea of having drivers operate exclusively on XDP frames and have the skb handling entirely in the core is an intriguing way to resolve this problem. Though this is obviously a long-term thing, and one might reasonably doubt we'll ever get there for existing drivers...
Quoted 21 lines
Quoted 5 lines
Quoted 4 lines
And we're adding this before we considered the queuing problem.
But if I'm alone in thinking this, and I'm not convincing anyone we can move on :)
Quoted 8 lines
I do share your concern that this will end up being incompatible with whatever solution we end up with for queueing. However, I don't necessarily think it will: I view the XDP egress hook as something that in any case will run *after* packets are dequeued from whichever intermediate queueing it has been through (if any). I think such a hook is missing in any case; for instance, it's currently impossible to implement something like CoDel (which needs to know how long a packet spent in the queue) in eBPF.
Possibly 🤔 I don't have a good mental image of how the XDP queuing would work.
Maybe once the queuing primitives are defined they can easily be hooked into the Qdisc layer. With Martin's recent work all we need is a fifo that can store skb pointers, really...
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
It'd be good if the BPF queuing could replace TC Qdiscs, rather than layer underneath.
Hmm, hooking into the existing qdisc layer is an interesting idea. Ultimately, I fear it won't be feasible for performance reasons; but it's certainly something to consider. Maybe at least as an option?
For forwarding sure, but for locally generated traffic.. 🤷♂️
Right, well, for locally generated traffic we already have the qdisc? This was kinda the reason why my original thought was to add the queueing for XDP only at REDIRECT time. I.e., we already have the xdp_dev_bulk_queue (which now even lives in struct net_device), so we could "just" extend that and make it into a proper queueing structure, and call it a day? :)
But from your comments it sounds like that when you're saying "BPF queueing" you mean that the queueing itself should be programmable using BPF? To me, the most urgent thing has been to figure out a way to do *any* kind of queueing with XDP-forwarded frames, so haven't given much thought to what a "fully programmable" queue would look like... Do you have any thoughts?
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to David AhernQuoted 4 lines · David Ahern
Quoted 3 lines · Toke Høiland-Jørgensen
In any case an egress program will differ in:
Quoted 8 lines
Quoted 3 lines
- The context object (the RX-related fields will be invalid on egress, and we'll probably want to add new TX-related ones, such as HW TX-queue occupancy).
Jakub has suggested that
rx_queue_indexcan be a union withtx_queue_index; the former for the Rx path and the latter for the egress.The rest of the fields in
xdp_mdare legit for either direction.
Right, okay, ifindex and queue index could make sense in both directions. But it would still mean that we would limit ourselves to only adding new fields that would work on both RX and TX. Maybe that is OK, though.
Quoted 8 lines
Quoted 4 lines
- The return code semantics (even if
XDP_TXbecomes equivalent toXDP_PASS, that is still a semantic difference from the RX side; and it's not necessarily clear whether we'll want to support REDIRECT on the egress side either, is it?)Why should REDIRECT not be allowed in the egress path? e.g., service chaining or capturing suspicious packets (e.g., encap with a header and redirect somewhere for analysis).
Implementation and deployment complexity, mostly (loops!)? I do agree that it would be nice to allow it, I'm just not entirely convinced that it's feasible...
Quoted 12 lines
Quoted 11 lines
So we'll have to disambiguate between the two different types of programs. Which means that what we're discussing is really whether that disambiguation should be encoded in the program type, or in the attach type. IMO, making it a separate program type is a clearer and more explicit UAPI. The verifier could still treat the two program types as basically equivalent except for those cases where there has to be a difference anyway. So it seems to me that all you are saving by using
attach_typeinstead of program type is the need for a new enum value and a bunch of additions to switch statements? Or am I wildly underestimating the effort to add a new program type?
Quoted 3 lines
IMHO that is duplicating code and APIs for no real reason. XDP refers to fast path processing, the only difference is where the program is attached - Rx side or Tx side.
Sure, if we can guarantee API and semantic equivalence. I.e., if any XDP program really *can* be attached as both an RX and TX program, I have no objections to re-using the program type. But if it turns out that there *is* a difference, we're making implicit subtypes anyway, in which case I think it's better to be explicit about the difference.
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
In reply to Jakub KicinskiOops, I see I forgot to reply to this bit:
Quoted 10 lines
Quoted 3 lines
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
This was Jesper's idea originally, so maybe he can explain better; but as I understood it, he basically wanted to expose the same information that BQL has to eBPF. Making it possible for an eBPF program to either (re-)implement BQL with its own custom policy, or react to HWQ pressure in some other way, such as by load balancing to another interface.
-Toke
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 13 lines · On Mon, 03 Feb 2020 21:13:24 +0100 Toke Høiland-Jørgensen <[email protected]>…
Oops, I see I forgot to reply to this bit:
Quoted 10 lines
Quoted 3 lines
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 5 lines
This was Jesper's idea originally, so maybe he can explain better; but as I understood it, he basically wanted to expose the same information that BQL has to eBPF. Making it possible for an eBPF program to either (re-)implement BQL with its own custom policy, or react to HWQ pressure in some other way, such as by load balancing to another interface.
Yes, and I also have plans that goes beyond BQL. But let me start with explaining the BQL part, and answer Toke's question below.
Quoted 1 line · On Mon, 03 Feb 2020 20:56:03 +0100 Toke wrote:
[...] Hmm, I wonder if a TX driver hook is enough?
Short answer is no, a TX driver hook is not enough. The queue state info the TX driver hook have access to, needs to be updated once the hardware have "confirmed" the TX-DMA operation have completed. For BQL/DQL this update happens during TX-DMA completion/cleanup (code see call sites for netdev_tx_completed_queue()). (As Jakub wisely point out we cannot query the device directly due to performance implications). It doesn't need to be a new BPF hook, just something that update the queue state info (we could piggy back on the netdev_tx_completed_queue() call or give TX hook access to dev_queue->dql).
Regarding "where is the queue": For me the XDP-TX queue is the NIC hardware queue, that this BPF hook have some visibility into and can do filtering on. (Imagine that my TX queue is bandwidth limited, then I can shrink the packet size and still send a "congestion" packet to my receiver).
The bigger picture is that I envision the XDP-TX/egress hook can open-up for taking advantage of NIC hardware TX queue features. This also ties into the queue abstraction work by Björn+Magnus. Today NIC hardware can do a million TX-queues, and hardware can also do rate limiting per queue. Thus, I also envision that the XDP-TX/egress hook can choose/change the TX queue the packet is queue/sent on (we can likely just overload the XDP_REDIRECT and have a new bpf map type for this).
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 13 lines · On Mon, 03 Feb 2020 21:13:24 +0100 Toke Høiland-Jørgensen <[email protected]>…
Oops, I see I forgot to reply to this bit:
Quoted 10 lines
Quoted 3 lines
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 5 lines
This was Jesper's idea originally, so maybe he can explain better; but as I understood it, he basically wanted to expose the same information that BQL has to eBPF. Making it possible for an eBPF program to either (re-)implement BQL with its own custom policy, or react to HWQ pressure in some other way, such as by load balancing to another interface.
Yes, and I also have plans that goes beyond BQL. But let me start with explaining the BQL part, and answer Toke's question below.
Quoted 1 line · On Mon, 03 Feb 2020 20:56:03 +0100 Toke wrote:
[...] Hmm, I wonder if a TX driver hook is enough?
Quoted 10 lines
Short answer is no, a TX driver hook is not enough. The queue state info the TX driver hook have access to, needs to be updated once the hardware have "confirmed" the TX-DMA operation have completed. For BQL/DQL this update happens during TX-DMA completion/cleanup (code see call sites for
netdev_tx_completed_queue()). (As Jakub wisely point out we cannot query the device directly due to performance implications). It doesn't need to be a new BPF hook, just something that update the queue state info (we could piggy back on thenetdev_tx_completed_queue()call or give TX hook access todev_queue->dql).
The question is whether this can't simply be done through bpf helpers? bpf_get_txq_occupancy(ifindex, txqno)?
Quoted 5 lines
Regarding "where is the queue": For me the XDP-TX queue is the NIC hardware queue, that this BPF hook have some visibility into and can do filtering on. (Imagine that my TX queue is bandwidth limited, then I can shrink the packet size and still send a "congestion" packet to my receiver).
I'm not sure the hardware queues will be enough, though. Unless I'm misunderstanding something, hardware queues are (1) fairly short and (2) FIFO. So, say we wanted to implement fq_codel for XDP forwarding: we'd still need a software queueing layer on top of the hardware queue.
If the hardware is EDT-aware this may change, I suppose, but I'm not sure if we can design the XDP queueing primitives with this assumption? :)
Quoted 8 lines
The bigger picture is that I envision the XDP-TX/egress hook can open-up for taking advantage of NIC hardware TX queue features. This also ties into the queue abstraction work by Björn+Magnus. Today NIC hardware can do a million TX-queues, and hardware can also do rate limiting per queue. Thus, I also envision that the XDP-TX/egress hook can choose/change the TX queue the packet is queue/sent on (we can likely just overload the
XDP_REDIRECTand have a new bpf map type for this).
Yes, I think I mentioned in another email that putting all the queueing smarts into the redirect map was also something I'd considered (well, I do think we've discussed this in the past, so maybe not so surprising if we're thinking along the same lines) :)
But the implication of this is also that an actual TX hook in the driver need not necessarily incorporate a lot of new functionality, as it can control the queueing through a combination of BPF helpers and map updates?
-Toke
Jakub Kicinski
maintainerRe: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 29 lines · Toke Høiland-Jørgensen
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 13 lines · On Mon, 03 Feb 2020 21:13:24 +0100 Toke Høiland-Jørgensen <[email protected]>…
Oops, I see I forgot to reply to this bit:
Quoted 10 lines
Quoted 3 lines
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 5 lines
This was Jesper's idea originally, so maybe he can explain better; but as I understood it, he basically wanted to expose the same information that BQL has to eBPF. Making it possible for an eBPF program to either (re-)implement BQL with its own custom policy, or react to HWQ pressure in some other way, such as by load balancing to another interface.
Yes, and I also have plans that goes beyond BQL. But let me start with explaining the BQL part, and answer Toke's question below.
Quoted 1 line · On Mon, 03 Feb 2020 20:56:03 +0100 Toke wrote:
[...] Hmm, I wonder if a TX driver hook is enough?
Quoted 10 lines
Quoted 10 lines
Short answer is no, a TX driver hook is not enough. The queue state info the TX driver hook have access to, needs to be updated once the hardware have "confirmed" the TX-DMA operation have completed. For BQL/DQL this update happens during TX-DMA completion/cleanup (code see call sites for
netdev_tx_completed_queue()). (As Jakub wisely point out we cannot query the device directly due to performance implications). It doesn't need to be a new BPF hook, just something that update the queue state info (we could piggy back on thenetdev_tx_completed_queue()call or give TX hook access todev_queue->dql).
Interesting, that model does make sense to me.
Quoted 2 lines
The question is whether this can't simply be done through bpf helpers?
bpf_get_txq_occupancy(ifindex, txqno)?
Helper vs dev_queue->dql field access seems like a technicality. The usual flexibility of implementation vs performance and simplicity consideration applies.. I guess?
Quoted 10 lines
Quoted 5 lines
Regarding "where is the queue": For me the XDP-TX queue is the NIC hardware queue, that this BPF hook have some visibility into and can do filtering on. (Imagine that my TX queue is bandwidth limited, then I can shrink the packet size and still send a "congestion" packet to my receiver).
I'm not sure the hardware queues will be enough, though. Unless I'm misunderstanding something, hardware queues are (1) fairly short and (2) FIFO. So, say we wanted to implement
fq_codelfor XDP forwarding: we'd still need a software queueing layer on top of the hardware queue.
Jesper makes a very interesting point tho. If all the implementation wants is FIFO queues which are services in some simple manner (that is something that can be offloaded) we should support that.
That means REDIRECT can target multiple TX queues, and we need an API to control the queue allocation..
Quoted 2 lines
If the hardware is EDT-aware this may change, I suppose, but I'm not sure if we can design the XDP queueing primitives with this assumption? :)
But I agree with you as well. I think both HW and SW feeding needs to be supported. The HW implementations are always necessarily behind ideas people implemented and tested in SW..
Quoted 8 lines
Quoted 8 lines
The bigger picture is that I envision the XDP-TX/egress hook can open-up for taking advantage of NIC hardware TX queue features. This also ties into the queue abstraction work by Björn+Magnus. Today NIC hardware can do a million TX-queues, and hardware can also do rate limiting per queue. Thus, I also envision that the XDP-TX/egress hook can choose/change the TX queue the packet is queue/sent on (we can likely just overload the
XDP_REDIRECTand have a new bpf map type for this).
I wonder what that does to our HW offload model which is based on TC Qdisc offload today :S Do we use TC API to control configuration of XDP queues? :S
Quoted 9 lines
Yes, I think I mentioned in another email that putting all the queueing smarts into the redirect map was also something I'd considered (well, I do think we've discussed this in the past, so maybe not so surprising if we're thinking along the same lines) :)
But the implication of this is also that an actual TX hook in the driver need not necessarily incorporate a lot of new functionality, as it can control the queueing through a combination of BPF helpers and map updates?
True, it's the dequeuing that's on the TX side, so we could go as far as putting all the enqueuing logic in the RX prog..
To answer your question from the other email Toke, my basic model was kind of similar to TC Qdiscs. XDP redirect selects a device, then that device has an enqueue and dequeue programs. Enqueue program can be run in the XDP_REDIRECT context, dequeue is run every time NAPI cleaned up some space on the TX descriptor ring. There is a "queue state" but the FIFOs etc are sort of internal detail that the enqueue and dequeue programs only share between each other. To be clear this is not a suggestion of how things should be, it's what sprung to my mind without thinking.
Re: [PATCH bpf-next 03/12] net: Add IFLA_XDP_EGRESS for XDP programs in the egress path
Quoted 41 lines · Jakub Kicinski
Quoted 29 lines · Toke Høiland-Jørgensen
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 13 lines · On Mon, 03 Feb 2020 21:13:24 +0100 Toke Høiland-Jørgensen <[email protected]>…
Oops, I see I forgot to reply to this bit:
Quoted 10 lines
Quoted 3 lines
Yeah, but having the low-level details available to the XDP program (such as HW queue occupancy for the egress hook) is one of the benefits of XDP, isn't it?
I think I glossed over the hope for having access to HW queue occupancy
- what exactly are you after?I don't think one can get anything beyond a BQL type granularity. Reading over PCIe is out of question, device write back on high granularity would burn through way too much bus throughput.
Quoted 5 lines
This was Jesper's idea originally, so maybe he can explain better; but as I understood it, he basically wanted to expose the same information that BQL has to eBPF. Making it possible for an eBPF program to either (re-)implement BQL with its own custom policy, or react to HWQ pressure in some other way, such as by load balancing to another interface.
Yes, and I also have plans that goes beyond BQL. But let me start with explaining the BQL part, and answer Toke's question below.
Quoted 1 line · On Mon, 03 Feb 2020 20:56:03 +0100 Toke wrote:
[...] Hmm, I wonder if a TX driver hook is enough?
Quoted 10 lines
Quoted 10 lines
Short answer is no, a TX driver hook is not enough. The queue state info the TX driver hook have access to, needs to be updated once the hardware have "confirmed" the TX-DMA operation have completed. For BQL/DQL this update happens during TX-DMA completion/cleanup (code see call sites for
netdev_tx_completed_queue()). (As Jakub wisely point out we cannot query the device directly due to performance implications). It doesn't need to be a new BPF hook, just something that update the queue state info (we could piggy back on thenetdev_tx_completed_queue()call or give TX hook access todev_queue->dql).
Quoted 8 lines
Interesting, that model does make sense to me.
Quoted 2 lines
The question is whether this can't simply be done through bpf helpers?
bpf_get_txq_occupancy(ifindex, txqno)?Helper vs
dev_queue->dqlfield access seems like a technicality. The usual flexibility of implementation vs performance and simplicity consideration applies.. I guess?
Yeah, that was my point: We can do whatever makes the most sense for that; it doesn't necessarily have to be something that's baked into the TX hook point.
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
Regarding "where is the queue": For me the XDP-TX queue is the NIC hardware queue, that this BPF hook have some visibility into and can do filtering on. (Imagine that my TX queue is bandwidth limited, then I can shrink the packet size and still send a "congestion" packet to my receiver).
I'm not sure the hardware queues will be enough, though. Unless I'm misunderstanding something, hardware queues are (1) fairly short and (2) FIFO. So, say we wanted to implement
fq_codelfor XDP forwarding: we'd still need a software queueing layer on top of the hardware queue.
Quoted 6 lines
Jesper makes a very interesting point tho. If all the implementation wants is FIFO queues which are services in some simple manner (that is something that can be offloaded) we should support that.
That means REDIRECT can target multiple TX queues, and we need an API to control the queue allocation..
Yes. I think that should be part of the "hwq API" that we presented at LPC last year[0].
Quoted 6 lines
Quoted 2 lines
If the hardware is EDT-aware this may change, I suppose, but I'm not sure if we can design the XDP queueing primitives with this assumption? :)
But I agree with you as well. I think both HW and SW feeding needs to be supported. The HW implementations are always necessarily behind ideas people implemented and tested in SW..
Exactly. And even though we mostly think about 10-100G network interfaces, I believe there is also potential for this to be useful at the low end (think small embedded routers that are very CPU constrained). In these sorts of environments software queueing is more feasible.
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
The bigger picture is that I envision the XDP-TX/egress hook can open-up for taking advantage of NIC hardware TX queue features. This also ties into the queue abstraction work by Björn+Magnus. Today NIC hardware can do a million TX-queues, and hardware can also do rate limiting per queue. Thus, I also envision that the XDP-TX/egress hook can choose/change the TX queue the packet is queue/sent on (we can likely just overload the
XDP_REDIRECTand have a new bpf map type for this).
Quoted 3 lines
I wonder what that does to our HW offload model which is based on TC Qdisc offload today :S Do we use TC API to control configuration of XDP queues? :S
My thought was that it could be baked into the same API[0]. I.e., add a way to say not just "I want 3 hardware queues for this", but also "I want 3 hardware queues, with fq_codel (or whatever) on top".
Quoted 13 lines
Quoted 9 lines
Yes, I think I mentioned in another email that putting all the queueing smarts into the redirect map was also something I'd considered (well, I do think we've discussed this in the past, so maybe not so surprising if we're thinking along the same lines) :)
But the implication of this is also that an actual TX hook in the driver need not necessarily incorporate a lot of new functionality, as it can control the queueing through a combination of BPF helpers and map updates?
True, it's the dequeuing that's on the TX side, so we could go as far as putting all the enqueuing logic in the RX prog..
Quoted 9 lines
To answer your question from the other email Toke, my basic model was kind of similar to TC Qdiscs. XDP redirect selects a device, then that device has an enqueue and dequeue programs. Enqueue program can be run in the
XDP_REDIRECTcontext, dequeue is run every time NAPI cleaned up some space on the TX descriptor ring. There is a "queue state" but the FIFOs etc are sort of internal detail that the enqueue and dequeue programs only share between each other. To be clear this is not a suggestion of how things should be, it's what sprung to my mind without thinking.
Ah, that's interesting. I do agree that it may be better to bake this into the existing hooks rather than have an additional set of (en/de)queue hooks.
-Toke
[0] https://linuxplumbersconf.org/event/4/contributions/462/