AF_XDP support for veth.
Subsystems (4)
cover AF_XDP support for veth.
The patch series adds AF_XDP async xmit support for veth device. First patch add a new API for supporting non-physical NIC device to get packet's virtual address. The second patch implements the async xmit, and last patch adds example use cases.
I tested with 2 namespaces, one as sender, the other as receiver. The packet rate is measure at the receiver side.
ip netns add at_ns0
ip link add p0 type veth peer name p1
ip link set p0 netns at_ns0
ip link set dev p1 up
ip netns exec at_ns0 ip link set dev p0 up
# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP
# sender with AF_XDP xdpsock -i p1 -t -N -z
# or sender without AF_XDP xdpsock -i p1 -t -S
Without AF_XDP: 724 Kpps
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index 0:1 724339 0rx_queue_index 0:sum 724339
With AF_XDP: 1.1 Mpps (with ksoftirqd 100% cpu)
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index 0:3 1188181 0rx_queue_index 0:sum 1188181
William Tu (3):
xsk: add xsk_umem_consume_tx_virtual.
veth: support AF_XDP.
samples: bpf: add veth AF_XDP example.
drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++- include/net/xdp_sock.h | 7 ++ net/xdp/xsk.c | 24 ++++ samples/bpf/test_veth_afxdp.sh | 67 +++++++++++ 4 files changed, 343 insertions(+), 2 deletions(-) create mode 100755 samples/bpf/test_veth_afxdp.sh
Signature · 1 line
2.7.4
RFC 1/3 xsk: add xsk_umem_consume_tx_virtual.
Currently the xsk_umem_consume_tx expects only the physical NICs so the api returns a dma address. This patch introduce the new function to return the virtual address, when XSK is used by a virtual device.
--- include/net/xdp_sock.h | 7 +++++++ net/xdp/xsk.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/net/xdp_sock.h +7 -0
@@ -81,6 +81,7 @@ u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);void xsk_umem_discard_addr (struct xdp_umem *umem);void xsk_umem_complete_tx (struct xdp_umem *umem,u32 nb_entries);bool xsk_umem_consume_tx (struct xdp_umem *umem,dma_addr_t *dma,u32 *len); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len);void xsk_umem_consume_tx_done (struct xdp_umem *umem);struct xdp_umem_fq_reuse *xsk_reuseq_prepare (u32 nentries); struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,
@@ -165,6 +166,12 @@ static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma,return false; } +static inline bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem, +char **dma,u32 *len) +{ +return false; +} +static inline void xsk_umem_consume_tx_done (struct xdp_umem *umem) { }
net/xdp/xsk.c +24 -0
@@ -197,6 +197,30 @@ bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) }EXPORT_SYMBOL (xsk_umem_consume_tx); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len) +{ +struct xdp_desc desc; +struct xdp_sock *xs; + +rcu_read_lock (); +list_for_each_entry_rcu (xs, &umem->xsk_list, list) { +if (!xskq_peek_desc (xs->tx, &desc)) +continue ; +if (xskq_produce_addr_lazy (umem->cq, desc.addr)) + goto out; + + *addr =xdp_umem_get_data (umem, desc.addr); + *len = desc.len; +xskq_discard_desc (xs->tx); +rcu_read_unlock (); +return true; + } +out: +rcu_read_unlock (); +return false; +} +EXPORT_SYMBOL (xsk_umem_consume_tx_virtual); +static int xsk_zc_xmit (struct sock *sk) {struct xdp_sock *xs =xdp_sk (sk);
Signature · 1 line
2.7.4
Re: [bpf-next RFC 1/3] xsk: add xsk_umem_consume_tx_virtual.
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 24 lines
Currently the
xsk_umem_consume_txexpects only the physical NICs so the api returns a dma address. This patch introduce the new function to return the virtual address, when XSK is used by a virtual device.Signed-off-by William Tu <[email protected]>--- include/net/xdp_sock.h | 7 +++++++ net/xdp/xsk.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+)include/net/xdp_sock.h +1
-0@@ -81,6 +81,7 @@ u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);void xsk_umem_discard_addr (struct xdp_umem *umem);void xsk_umem_complete_tx (struct xdp_umem *umem,u32 nb_entries);bool xsk_umem_consume_tx (struct xdp_umem *umem,dma_addr_t *dma,u32 *len); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len);void xsk_umem_consume_tx_done (struct xdp_umem *umem);struct xdp_umem_fq_reuse *xsk_reuseq_prepare (u32 nentries); struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,
include/net/xdp_sock.h static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, · line 170
Quoted 5 lines
include/net/xdp_sock.h +2
-0@@ -165,6 +166,12 @@ static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma,return false; } +static inline bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem, +char **dma,u32 *len)
Nit: dma->addr
Quoted 12 lines
include/net/xdp_sock.h +4
-0+{ +return false; +} +static inline void xsk_umem_consume_tx_done (struct xdp_umem *umem) { }net/xdp/xsk.c +0
-0
net/xdp/xsk.c bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) · line 200
Quoted 4 lines
net/xdp/xsk.c +1
-0@@ -197,6 +197,30 @@ bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) }EXPORT_SYMBOL (xsk_umem_consume_tx); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len)
Prefer void ** in favor of char**?
Quoted 12 lines
net/xdp/xsk.c +12
-0+{ +struct xdp_desc desc; +struct xdp_sock *xs; + +rcu_read_lock (); +list_for_each_entry_rcu (xs, &umem->xsk_list, list) { +if (!xskq_peek_desc (xs->tx, &desc)) +continue ; +if (xskq_produce_addr_lazy (umem->cq, desc.addr)) + goto out; + + *addr =xdp_umem_get_data (umem, desc.addr);
net/xdp/xsk.c bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) · line 222
Quoted 10 lines
net/xdp/xsk.c +10
-0+ *len = desc.len; +xskq_discard_desc (xs->tx); +rcu_read_unlock (); +return true; + } +out: +rcu_read_unlock (); +return false; +} +EXPORT_SYMBOL (xsk_umem_consume_tx_virtual);
Is possible to make xsk_umem_consume_tx_virtual and xsk_umem_consume_tx share a common implementation. They are very similar.
Quoted 7 lines
net/xdp/xsk.c +1
-1+static int xsk_zc_xmit (struct sock *sk) {struct xdp_sock *xs =xdp_sk (sk); --2.7.4
Re: [bpf-next RFC 1/3] xsk: add xsk_umem_consume_tx_virtual.
Quoted 71 lines · Björn Töpel
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 24 lines
Currently the
xsk_umem_consume_txexpects only the physical NICs so the api returns a dma address. This patch introduce the new function to return the virtual address, when XSK is used by a virtual device.Signed-off-by William Tu <[email protected]>--- include/net/xdp_sock.h | 7 +++++++ net/xdp/xsk.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+)include/net/xdp_sock.h +1
-0@@ -81,6 +81,7 @@ u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);void xsk_umem_discard_addr (struct xdp_umem *umem);void xsk_umem_complete_tx (struct xdp_umem *umem,u32 nb_entries);bool xsk_umem_consume_tx (struct xdp_umem *umem,dma_addr_t *dma,u32 *len); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len);void xsk_umem_consume_tx_done (struct xdp_umem *umem);struct xdp_umem_fq_reuse *xsk_reuseq_prepare (u32 nentries); struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,include/net/xdp_sock.h static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, · line 170
Quoted 5 lines
include/net/xdp_sock.h +2
-0@@ -165,6 +166,12 @@ static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma,return false; } +static inline bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem, +char **dma,u32 *len)Nit:
dma->addrQuoted 12 lines
include/net/xdp_sock.h +4
-0+{ +return false; +} +static inline void xsk_umem_consume_tx_done (struct xdp_umem *umem) { }net/xdp/xsk.c +0
-0net/xdp/xsk.c bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) · line 200
Quoted 4 lines
net/xdp/xsk.c +1
-0@@ -197,6 +197,30 @@ bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) }EXPORT_SYMBOL (xsk_umem_consume_tx); +bool xsk_umem_consume_tx_virtual (struct xdp_umem *umem,char **addr,u32 *len)Prefer void ** in favor of char**?
Quoted 18 lines
net/xdp/xsk.c +18
-0+{ +struct xdp_desc desc; +struct xdp_sock *xs; + +rcu_read_lock (); +list_for_each_entry_rcu (xs, &umem->xsk_list, list) { +if (!xskq_peek_desc (xs->tx, &desc)) +continue ; +if (xskq_produce_addr_lazy (umem->cq, desc.addr)) + goto out; + + *addr =xdp_umem_get_data (umem, desc.addr); + *len = desc.len; +xskq_discard_desc (xs->tx); +rcu_read_unlock (); +return true; + } +out:
Quoted 8 lines
net/xdp/xsk.c bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len) · line 222
Quoted 4 lines
net/xdp/xsk.c +4
-0+rcu_read_unlock (); +return false; +} +EXPORT_SYMBOL (xsk_umem_consume_tx_virtual);Is possible to make
xsk_umem_consume_tx_virtualandxsk_umem_consume_txshare a common implementation. They are very similar.
Sure, will do it in next version.
Thanks
William
Quoted 8 lines
Quoted 8 lines
net/xdp/xsk.c +1
-1+static int xsk_zc_xmit (struct sock *sk) {struct xdp_sock *xs =xdp_sk (sk); --2.7.4
RFC 2/3 veth: support AF_XDP.
In reply to William TuThe patch adds support for AF_XDP async xmit. Users can use AF_XDP on both side of the veth and get better performance, with the cost of ksoftirqd doing the xmit. The veth_xsk_async_xmit simply kicks the napi function, veth_poll, to run, and the transmit logic is implemented there.
Tested using two namespaces, one runs xdpsock and the other runs xdp_rxq_info. A simple script comparing the performance with/without AF_XDP shows improvement from 724Kpps to 1.1Mpps.
ip netns add at_ns0
ip link add p0 type veth peer name p1
ip link set p0 netns at_ns0
ip link set dev p1 up
ip netns exec at_ns0 ip link set dev p0 up
# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP
# sender xdpsock -i p1 -t -N -z or xdpsock -i p1 -t -S
--- drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 245 insertions(+), 2 deletions(-)
drivers/net/veth.c +245 -2
@@ -25,6 +25,10 @@#include <linux/ptr_ring.h> #include <linux/bpf_trace.h> #include <linux/net_tstamp.h> +#include <net/xdp_sock.h> +#include <linux/mm.h> +#include <linux/slab.h> +#include <net/page_pool.h> #define DRV_NAME "veth"#define DRV_VERSION "1.0"
@@ -53,6 +57,7 @@ struct veth_rq {bool rx_notify_masked;struct ptr_ring xdp_ring;struct xdp_rxq_info xdp_rxq; +struct xdp_umem *xsk_umem; };struct veth_priv {
@@ -61,6 +66,11 @@ struct veth_priv {struct bpf_prog *_xdp_prog;struct veth_rq *rq;unsigned int requested_headroom; + +/* AF_XDP zero-copy */ +struct xdp_umem **xsk_umems; +u16 num_xsk_umems_used; +u16 num_xsk_umems; };/*
@@ -742,10 +752,87 @@ static int veth_poll(struct napi_struct *napi, int budget)struct veth_rq *rq = container_of(napi, struct veth_rq, xdp_napi);unsigned int xdp_xmit =0 ; -int done; +int tx_budget = budget; +int done =0 ; + +/* tx: use netif_tx_napi_add or here? */ +while (rq->xsk_umem && tx_budget--) { +struct veth_priv *priv, *peer_priv; +struct net_device *dev, *peer_dev; +unsigned int inner_xdp_xmit =0 ; +unsigned int metasize =0 ; +struct veth_rq *peer_rq; +struct xdp_frame *xdpf; +bool dropped = false; +struct sk_buff *skb; +struct page *page; +char *vaddr; +void *addr; +u32 len; + +if (!xsk_umem_consume_tx_virtual (rq->xsk_umem, &vaddr, &len)) +break ; + + page =dev_alloc_page (); +if (!page) +return -ENOMEM ; + + addr =page_to_virt (page); + xdpf = addr; +memset (xdpf,0 ,sizeof (*xdpf)); + + addr +=sizeof (*xdpf); +memcpy (addr, vaddr, len); + + xdpf->data = addr + metasize; + xdpf->len = len; + xdpf->headroom =0 ; + xdpf->metasize = metasize; + xdpf->mem.type =MEM_TYPE_PAGE_SHARED ; + +/* Invoke peer rq to rcv */ + dev = rq->dev; + priv =netdev_priv (dev); + peer_dev = priv->peer; + peer_priv =netdev_priv (peer_dev); + peer_rq = peer_priv->rq; + +/* put into peer rq */ + skb =veth_xdp_rcv_one (peer_rq, xdpf, &inner_xdp_xmit); +if (!skb) { +/* Peer side has XDP program attached */ +if (inner_xdp_xmit &VETH_XDP_TX ) { +/* Not supported */ +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem); +xdp_return_frame (xdpf); + goto skip_tx; + }else if (inner_xdp_xmit &VETH_XDP_REDIR ) { +xdp_do_flush_map (); + }else { + dropped = true; + } + }else { +/* Peer side has no XDP attached */ +napi_gro_receive (&peer_rq->xdp_napi, skb); + } +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem); + +/* update peer stats */ +u64_stats_update_begin (&peer_rq->stats.syncp); + peer_rq->stats.xdp_packets++; + peer_rq->stats.xdp_bytes += len; +if (dropped) + rq->stats.xdp_drops++; +u64_stats_update_end (&peer_rq->stats.syncp); + done++; + } +skip_tx: +/* rx */ xdp_set_return_frame_no_direct (); - done =veth_xdp_rcv (rq, budget, &xdp_xmit); + done +=veth_xdp_rcv (rq, budget, &xdp_xmit);if (done < budget &&napi_complete_done (napi, done)) {/* Write rx_notify_masked before reading ptr_ring */
@@ -1115,6 +1202,141 @@ static u32 veth_xdp_query(struct net_device *dev)return 0 ; } +static int veth_alloc_xsk_umems (struct net_device *dev) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (priv->xsk_umems) +return 0 ; + + priv->num_xsk_umems_used =0 ; + priv->num_xsk_umems = dev->real_num_rx_queues; + priv->xsk_umems =kcalloc (priv->num_xsk_umems, +sizeof (*priv->xsk_umems), +GFP_KERNEL ); +return 0 ; +} + +static int veth_add_xsk_umem (struct net_device *dev, +struct xdp_umem *umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +int err; + + err =veth_alloc_xsk_umems (dev); +if (err) +return err; + + priv->xsk_umems[qid] = umem; + priv->num_xsk_umems_used++; + priv->rq[qid].xsk_umem = umem; + +return 0 ; +} + +static void veth_remove_xsk_umem (struct net_device *dev,u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + + priv->xsk_umems[qid] =NULL ; + priv->num_xsk_umems_used--; + +if (priv->num_xsk_umems ==0 ) { +kfree (priv->xsk_umems); + priv->xsk_umems =NULL ; + priv->num_xsk_umems =0 ; + } +} + +int veth_xsk_umem_query (struct net_device *dev,struct xdp_umem **umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (qid >= dev->real_num_rx_queues) +return -EINVAL ; + +if (priv->xsk_umems) { +if (qid >= priv->num_xsk_umems) +return -EINVAL ; + *umem = priv->xsk_umems[qid]; +return 0 ; + } + + *umem =NULL ; +return 0 ; +} + +static int veth_xsk_umem_enable (struct net_device *dev, +struct xdp_umem *umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +struct xdp_umem_fq_reuse *reuseq; +int err =0 ; + +if (qid >= dev->real_num_rx_queues) +return -EINVAL ; + +if (priv->xsk_umems) { +if (qid >= priv->num_xsk_umems) +return -EINVAL ; +if (priv->xsk_umems[qid]) +return -EBUSY ; + } + + reuseq =xsk_reuseq_prepare (priv->rq[0 ].xdp_ring.size); +if (!reuseq) +return -ENOMEM ; + +xsk_reuseq_free (xsk_reuseq_swap (umem, reuseq)); + +/* Check if_running and disable/enable? */ + err =veth_add_xsk_umem (dev, umem, qid); + +return err; +} + +static int veth_xsk_umem_disable (struct net_device *dev, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (!priv->xsk_umems || qid >= priv->num_xsk_umems || + !priv->xsk_umems[qid]) +return -EINVAL ; + +veth_remove_xsk_umem (dev, qid); +return 0 ; +} + +int veth_xsk_umem_setup (struct net_device *dev,struct xdp_umem *umem, +u16 qid) +{ +return umem ?veth_xsk_umem_enable (dev, umem, qid) : +veth_xsk_umem_disable (dev, qid); +} + +int veth_xsk_async_xmit (struct net_device *dev,u32 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +struct veth_rq *rq; + + rq = &priv->rq[qid]; + +if (qid >= dev->real_num_rx_queues) +return -ENXIO ; + +if (!priv->xsk_umems) +return -ENXIO ; + +if (!napi_if_scheduled_mark_missed (&rq->xdp_napi)) +napi_schedule (&rq->xdp_napi); + +return 0 ; +} +static int veth_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {
@@ -1123,6 +1345,26 @@ static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)case XDP_QUERY_PROG : xdp->prog_id =veth_xdp_query (dev);return 0 ; +case XDP_QUERY_XSK_UMEM : +return veth_xsk_umem_query (dev, &xdp->xsk.umem, + xdp->xsk.queue_id); +case XDP_SETUP_XSK_UMEM : { +struct veth_priv *priv; +int err; + +/* Enable XDP on both sides */ + err =veth_enable_xdp (dev); +if (err) +return err; + + priv =netdev_priv (dev); + err =veth_enable_xdp (priv->peer); +if (err) +return err; + +return veth_xsk_umem_setup (dev, xdp->xsk.umem, + xdp->xsk.queue_id); + }default :return -EINVAL ; }
@@ -1145,6 +1387,7 @@ static const struct net_device_ops veth_netdev_ops = { .ndo_set_rx_headroom = veth_set_rx_headroom, .ndo_bpf = veth_xdp, .ndo_xdp_xmit = veth_xdp_xmit, + .ndo_xsk_async_xmit = veth_xsk_async_xmit, };#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Signature · 1 line
2.7.4
Re: [bpf-next RFC 2/3] veth: support AF_XDP.
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 54 lines
The patch adds support for
AF_XDPasync xmit. Users can useAF_XDPon both side of the veth and get better performance, with the cost of ksoftirqd doing the xmit. Theveth_xsk_async_xmitsimply kicks the napi function,veth_poll, to run, and the transmit logic is implemented there.Tested using two namespaces, one runs xdpsock and the other runs
xdp_rxq_info. A simple script comparing the performance with/withoutAF_XDPshows improvement from 724Kpps to 1.1Mpps.ip netns add
at_ns0
ip link add p0 type veth peer name p1
ip link set p0 netnsat_ns0
ip link set dev p1 up
ip netns execat_ns0ip link set dev p0 up# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP# sender xdpsock -i p1 -t -N -z or xdpsock -i p1 -t -SSigned-off-by William Tu <[email protected]>--- drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 245 insertions(+), 2 deletions(-)drivers/net/veth.c +5
-0@@ -25,6 +25,10 @@#include <linux/ptr_ring.h> #include <linux/bpf_trace.h> #include <linux/net_tstamp.h> +#include <net/xdp_sock.h> +#include <linux/mm.h> +#include <linux/slab.h> +#include <net/page_pool.h> #define DRV_NAME "veth"#define DRV_VERSION "1.0"@@ -53,6 +57,7 @@ struct veth_rq {bool rx_notify_masked;struct ptr_ring xdp_ring;struct xdp_rxq_info xdp_rxq; +struct xdp_umem *xsk_umem; };struct veth_priv {
drivers/net/veth.c struct veth_priv { · line 75
Quoted 10 lines
drivers/net/veth.c +5
-0@@ -61,6 +66,11 @@ struct veth_priv {struct bpf_prog *_xdp_prog;struct veth_rq *rq;unsigned int requested_headroom; + +/* AF_XDP zero-copy */ +struct xdp_umem **xsk_umems; +u16 num_xsk_umems_used; +u16 num_xsk_umems; };
The umems are since commit 661b8d1b0e3a ("net: add umem reference in netdev{_rx}_queue") part of the netdev structure, so instead of storing the umem in the priv area, you can pull it from the netdev device. Doing so means that you can remove all the *_umem() functions below.
Jan (added as Cc:) just did that work on the i40e driver (patches posted on intel-wired-lan list).
Quoted 72 lines
drivers/net/veth.c +66
-1/* @@ -742,10 +752,87 @@ static int veth_poll(struct napi_struct *napi, int budget)struct veth_rq *rq = container_of(napi, struct veth_rq, xdp_napi);unsigned int xdp_xmit =0 ; -int done; +int tx_budget = budget; +int done =0 ; + +/* tx: use netif_tx_napi_add or here? */ +while (rq->xsk_umem && tx_budget--) { +struct veth_priv *priv, *peer_priv; +struct net_device *dev, *peer_dev; +unsigned int inner_xdp_xmit =0 ; +unsigned int metasize =0 ; +struct veth_rq *peer_rq; +struct xdp_frame *xdpf; +bool dropped = false; +struct sk_buff *skb; +struct page *page; +char *vaddr; +void *addr; +u32 len; + +if (!xsk_umem_consume_tx_virtual (rq->xsk_umem, &vaddr, &len)) +break ; + + page =dev_alloc_page (); +if (!page) +return -ENOMEM ; + + addr =page_to_virt (page); + xdpf = addr; +memset (xdpf,0 ,sizeof (*xdpf)); + + addr +=sizeof (*xdpf); +memcpy (addr, vaddr, len); + + xdpf->data = addr + metasize; + xdpf->len = len; + xdpf->headroom =0 ; + xdpf->metasize = metasize; + xdpf->mem.type =MEM_TYPE_PAGE_SHARED ; + +/* Invoke peer rq to rcv */ + dev = rq->dev; + priv =netdev_priv (dev); + peer_dev = priv->peer; + peer_priv =netdev_priv (peer_dev); + peer_rq = peer_priv->rq; + +/* put into peer rq */ + skb =veth_xdp_rcv_one (peer_rq, xdpf, &inner_xdp_xmit); +if (!skb) { +/* Peer side has XDP program attached */ +if (inner_xdp_xmit &VETH_XDP_TX ) { +/* Not supported */ +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem); +xdp_return_frame (xdpf); + goto skip_tx; + }else if (inner_xdp_xmit &VETH_XDP_REDIR ) { +xdp_do_flush_map (); + }else { + dropped = true; + } + }else { +/* Peer side has no XDP attached */ +napi_gro_receive (&peer_rq->xdp_napi, skb); + } +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem);
drivers/net/veth.c static int veth_poll(struct napi_struct *napi, int budget) · line 830
Quoted 10 lines
drivers/net/veth.c +10
-0+ +/* update peer stats */ +u64_stats_update_begin (&peer_rq->stats.syncp); + peer_rq->stats.xdp_packets++; + peer_rq->stats.xdp_bytes += len; +if (dropped) + rq->stats.xdp_drops++; +u64_stats_update_end (&peer_rq->stats.syncp); + done++; + }
Refactor to a function?
Quoted 189 lines
drivers/net/veth.c +159
-2+skip_tx: +/* rx */ xdp_set_return_frame_no_direct (); - done =veth_xdp_rcv (rq, budget, &xdp_xmit); + done +=veth_xdp_rcv (rq, budget, &xdp_xmit);if (done < budget &&napi_complete_done (napi, done)) {/* Write rx_notify_masked before reading ptr_ring */ @@ -1115,6 +1202,141 @@ static u32 veth_xdp_query(struct net_device *dev)return 0 ; } +static int veth_alloc_xsk_umems (struct net_device *dev) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (priv->xsk_umems) +return 0 ; + + priv->num_xsk_umems_used =0 ; + priv->num_xsk_umems = dev->real_num_rx_queues; + priv->xsk_umems =kcalloc (priv->num_xsk_umems, +sizeof (*priv->xsk_umems), +GFP_KERNEL ); +return 0 ; +} + +static int veth_add_xsk_umem (struct net_device *dev, +struct xdp_umem *umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +int err; + + err =veth_alloc_xsk_umems (dev); +if (err) +return err; + + priv->xsk_umems[qid] = umem; + priv->num_xsk_umems_used++; + priv->rq[qid].xsk_umem = umem; + +return 0 ; +} + +static void veth_remove_xsk_umem (struct net_device *dev,u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + + priv->xsk_umems[qid] =NULL ; + priv->num_xsk_umems_used--; + +if (priv->num_xsk_umems ==0 ) { +kfree (priv->xsk_umems); + priv->xsk_umems =NULL ; + priv->num_xsk_umems =0 ; + } +} + +int veth_xsk_umem_query (struct net_device *dev,struct xdp_umem **umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (qid >= dev->real_num_rx_queues) +return -EINVAL ; + +if (priv->xsk_umems) { +if (qid >= priv->num_xsk_umems) +return -EINVAL ; + *umem = priv->xsk_umems[qid]; +return 0 ; + } + + *umem =NULL ; +return 0 ; +} + +static int veth_xsk_umem_enable (struct net_device *dev, +struct xdp_umem *umem, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +struct xdp_umem_fq_reuse *reuseq; +int err =0 ; + +if (qid >= dev->real_num_rx_queues) +return -EINVAL ; + +if (priv->xsk_umems) { +if (qid >= priv->num_xsk_umems) +return -EINVAL ; +if (priv->xsk_umems[qid]) +return -EBUSY ; + } + + reuseq =xsk_reuseq_prepare (priv->rq[0 ].xdp_ring.size); +if (!reuseq) +return -ENOMEM ; + +xsk_reuseq_free (xsk_reuseq_swap (umem, reuseq)); + +/* Check if_running and disable/enable? */ + err =veth_add_xsk_umem (dev, umem, qid); + +return err; +} + +static int veth_xsk_umem_disable (struct net_device *dev, +u16 qid) +{ +struct veth_priv *priv =netdev_priv (dev); + +if (!priv->xsk_umems || qid >= priv->num_xsk_umems || + !priv->xsk_umems[qid]) +return -EINVAL ; + +veth_remove_xsk_umem (dev, qid); +return 0 ; +} + +int veth_xsk_umem_setup (struct net_device *dev,struct xdp_umem *umem, +u16 qid) +{ +return umem ?veth_xsk_umem_enable (dev, umem, qid) : +veth_xsk_umem_disable (dev, qid); +} + +int veth_xsk_async_xmit (struct net_device *dev,u32 qid) +{ +struct veth_priv *priv =netdev_priv (dev); +struct veth_rq *rq; + + rq = &priv->rq[qid]; + +if (qid >= dev->real_num_rx_queues) +return -ENXIO ; + +if (!priv->xsk_umems) +return -ENXIO ; + +if (!napi_if_scheduled_mark_missed (&rq->xdp_napi)) +napi_schedule (&rq->xdp_napi); + +return 0 ; +} +static int veth_xdp (struct net_device *dev,struct netdev_bpf *xdp) {switch (xdp->command) {@@ -1123,6 +1345,26 @@ static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)case XDP_QUERY_PROG : xdp->prog_id =veth_xdp_query (dev);return 0 ; +case XDP_QUERY_XSK_UMEM : +return veth_xsk_umem_query (dev, &xdp->xsk.umem, + xdp->xsk.queue_id); +case XDP_SETUP_XSK_UMEM : { +struct veth_priv *priv; +int err; + +/* Enable XDP on both sides */ + err =veth_enable_xdp (dev); +if (err) +return err; + + priv =netdev_priv (dev); + err =veth_enable_xdp (priv->peer); +if (err) +return err; + +return veth_xsk_umem_setup (dev, xdp->xsk.umem, + xdp->xsk.queue_id); + }default :return -EINVAL ; }@@ -1145,6 +1387,7 @@ static const struct net_device_ops veth_netdev_ops = { .ndo_set_rx_headroom = veth_set_rx_headroom, .ndo_bpf = veth_xdp, .ndo_xdp_xmit = veth_xdp_xmit, + .ndo_xsk_async_xmit = veth_xsk_async_xmit, };#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \ --2.7.4
Re: [bpf-next RFC 2/3] veth: support AF_XDP.
Quoted 64 lines · Björn Töpel
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 62 lines
The patch adds support for
AF_XDPasync xmit. Users can useAF_XDPon both side of the veth and get better performance, with the cost of ksoftirqd doing the xmit. Theveth_xsk_async_xmitsimply kicks the napi function,veth_poll, to run, and the transmit logic is implemented there.Tested using two namespaces, one runs xdpsock and the other runs
xdp_rxq_info. A simple script comparing the performance with/withoutAF_XDPshows improvement from 724Kpps to 1.1Mpps.ip netns add
at_ns0
ip link add p0 type veth peer name p1
ip link set p0 netnsat_ns0
ip link set dev p1 up
ip netns execat_ns0ip link set dev p0 up# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP# sender xdpsock -i p1 -t -N -z or xdpsock -i p1 -t -SSigned-off-by William Tu <[email protected]>--- drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 245 insertions(+), 2 deletions(-)drivers/net/veth.c +10
-0@@ -25,6 +25,10 @@#include <linux/ptr_ring.h> #include <linux/bpf_trace.h> #include <linux/net_tstamp.h> +#include <net/xdp_sock.h> +#include <linux/mm.h> +#include <linux/slab.h> +#include <net/page_pool.h> #define DRV_NAME "veth"#define DRV_VERSION "1.0"@@ -53,6 +57,7 @@ struct veth_rq {bool rx_notify_masked;struct ptr_ring xdp_ring;struct xdp_rxq_info xdp_rxq; +struct xdp_umem *xsk_umem; };struct veth_priv {@@ -61,6 +66,11 @@ struct veth_priv {struct bpf_prog *_xdp_prog;struct veth_rq *rq;unsigned int requested_headroom; + +/* AF_XDP zero-copy */ +struct xdp_umem **xsk_umems; +u16 num_xsk_umems_used; +u16 num_xsk_umems;
Quoted 9 lines
drivers/net/veth.c struct veth_priv { · line 75
Quoted 2 lines
drivers/net/veth.c +0
-0};The umems are since commit
661b8d1b0e3a("net: add umem reference in netdev{_rx}_queue") part of the netdev structure, so instead of storing the umem in the priv area, you can pull it from the netdev device. Doing so means that you can remove all the*_umem()functions below.
Thanks, will use it in next version.
Quoted 45 lines
Jan (added as Cc:) just did that work on the i40e driver (patches posted on intel-wired-lan list).
Quoted 42 lines
drivers/net/veth.c +36
-1/* @@ -742,10 +752,87 @@ static int veth_poll(struct napi_struct *napi, int budget)struct veth_rq *rq = container_of(napi, struct veth_rq, xdp_napi);unsigned int xdp_xmit =0 ; -int done; +int tx_budget = budget; +int done =0 ; + +/* tx: use netif_tx_napi_add or here? */ +while (rq->xsk_umem && tx_budget--) { +struct veth_priv *priv, *peer_priv; +struct net_device *dev, *peer_dev; +unsigned int inner_xdp_xmit =0 ; +unsigned int metasize =0 ; +struct veth_rq *peer_rq; +struct xdp_frame *xdpf; +bool dropped = false; +struct sk_buff *skb; +struct page *page; +char *vaddr; +void *addr; +u32 len; + +if (!xsk_umem_consume_tx_virtual (rq->xsk_umem, &vaddr, &len)) +break ; + + page =dev_alloc_page (); +if (!page) +return -ENOMEM ; + + addr =page_to_virt (page); + xdpf = addr; +memset (xdpf,0 ,sizeof (*xdpf)); + + addr +=sizeof (*xdpf); +memcpy (addr, vaddr, len); + + xdpf->data = addr + metasize; + xdpf->len = len; + xdpf->headroom =0 ; + xdpf->metasize = metasize;
Quoted 8 lines
drivers/net/veth.c static int veth_poll(struct napi_struct *napi, int budget) · line 798
Quoted 8 lines
drivers/net/veth.c +8
-0+ xdpf->mem.type =MEM_TYPE_PAGE_SHARED ; + +/* Invoke peer rq to rcv */ + dev = rq->dev; + priv =netdev_priv (dev); + peer_dev = priv->peer; + peer_priv =netdev_priv (peer_dev); + peer_rq = peer_priv->rq;
There is a bug here, where in this case we'll always use peer_rq[0] Instead, we should do peer_priv->rq[qid];
Quoted 26 lines
Quoted 26 lines
drivers/net/veth.c +26
-0+ +/* put into peer rq */ + skb =veth_xdp_rcv_one (peer_rq, xdpf, &inner_xdp_xmit); +if (!skb) { +/* Peer side has XDP program attached */ +if (inner_xdp_xmit &VETH_XDP_TX ) { +/* Not supported */ +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem); +xdp_return_frame (xdpf); + goto skip_tx; + }else if (inner_xdp_xmit &VETH_XDP_REDIR ) { +xdp_do_flush_map (); + }else { + dropped = true; + } + }else { +/* Peer side has no XDP attached */ +napi_gro_receive (&peer_rq->xdp_napi, skb); + } +xsk_umem_complete_tx (rq->xsk_umem,1 ); +xsk_umem_consume_tx_done (rq->xsk_umem); + +/* update peer stats */ +u64_stats_update_begin (&peer_rq->stats.syncp); + peer_rq->stats.xdp_packets++;
Quoted 8 lines
drivers/net/veth.c static int veth_poll(struct napi_struct *napi, int budget) · line 830
Quoted 6 lines
drivers/net/veth.c +6
-0+ peer_rq->stats.xdp_bytes += len; +if (dropped) + rq->stats.xdp_drops++; +u64_stats_update_end (&peer_rq->stats.syncp); + done++; + }Refactor to a function?
Sure, will do it.
Regards,
William
RFC 3/3 samples: bpf: add veth AF_XDP example.
In reply to William TuAdd example use cases for AF_XDP socket on two namespaces. The script runs sender at the root namespace, and receiver at the at_ns0 namespace with different XDP actions.
--- samples/bpf/test_veth_afxdp.sh | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 samples/bpf/test_veth_afxdp.sh
samples/bpf/test_veth_afxdp.sh new file +67 -0
@@ -0,0 +1,67 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +# The script runs the sender at the root namespace, and +# the receiver at the namespace at_ns0, with different mode +# 1. XDP_DROP +# 2. XDP_TX +# 3. XDP_PASS +# 4. XDP_REDIRECT +# 5. Generic XDP + +XDPSOCK=./xdpsock +XDP_RXQ_INFO=./xdp_rxq_info + +ip netns add at_ns0 +ip link add p0 type veth peer name p1 +ip link set p0 netns at_ns0 +ip link set dev p1 up +ip netns exec at_ns0 ip link set dev p0 up + +# send at root namespace +test_xdp_drop() +{ + echo "[Peer] XDP_DROP" + ip netns exec at_ns0 $XDP_RXQ_INFO --dev p0 --action XDP_DROP & + $XDPSOCK -i p1 -t -N -z &> /tmp/t &> /dev/null & +} + +test_xdp_pass() +{ + echo "[Peer] XDP_PASS" + ip netns exec at_ns0 $XDP_RXQ_INFO --dev p0 --action XDP_PASS & + $XDPSOCK -i p1 -t -N -z &> /tmp/t &> /dev/null & +} + +test_xdp_tx() +{ + echo "[Peer] XDP_TX" + ip netns exec at_ns0 $XDP_RXQ_INFO --dev p0 --action XDP_TX & + $XDPSOCK -i p1 -t -N -z &> /tmp/t &> /dev/null & +} + +test_generic_xdp() +{ + echo "[Peer] Generic XDP" + ip netns exec at_ns0 $XDPSOCK -i p0 -r -S & + $XDPSOCK -i p1 -t -N -z &> /tmp/t &> /dev/null & +} + +test_xdp_redirect() +{ + echo "[Peer] XDP_REDIRECT" + ip netns exec at_ns0 $XDPSOCK -i p0 -r -N & + $XDPSOCK -i p1 -t -N -z &> /tmp/t &> /dev/null & +} + +cleanup() { + killall xdpsock + killall xdp_rxq_info + ip netns del at_ns0 + ip link del p1 +} + +trap cleanup 0 3 6 + +test_xdp_drop +cleanup
Signature · 1 line
2.7.4
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 6 lines
The patch series adds
AF_XDPasync xmit support for veth device. First patch add a new API for supporting non-physical NIC device to get packet's virtual address. The second patch implements the async xmit, and last patch adds example use cases.
The first virtual device with AF_XDP support! Yay!
This is only the zero-copy on the Tx side -- it's still allocations plus copy on the ingress side? That's a bit different from the i40e/ixgbe implementation, where zero-copy means both Tx and Rx. For veth I don't see that we need to support Rx right away, especially for Tx only sockets. Still, when the netdev has accepted the umem via ndo_bpf, the zero-copy for both Tx and Rx is assumed. We might want to change the ndo_bpf at some point to support zero-copy for Tx, Rx, Tx *and* Rx.
Are you planning to add zero-copy to the ingress side, i.e. pulling frames from the fill ring, instead of allocating via dev_alloc_page? (The term *zero-copy* for veth is a bit weird, since we're still doing copies, but eliding the page allocation. :-))
It would be interesting to hear a bit about what use-case veth/AF_XDP has, if you can share that.
Cheers,
Björn
Quoted 42 lines
I tested with 2 namespaces, one as sender, the other as receiver. The packet rate is measure at the receiver side.
ip netns addat_ns0
ip link add p0 type veth peer name p1
ip link set p0 netnsat_ns0
ip link set dev p1 up
ip netns execat_ns0ip link set dev p0 up# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP# sender with AF_XDP xdpsock -i p1 -t -N -z# or sender without AF_XDP xdpsock -i p1 -t -SWithout
AF_XDP: 724 Kpps
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index0:1 724339 0rx_queue_index0:sum 724339With
AF_XDP: 1.1 Mpps (with ksoftirqd 100% cpu)
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index0:3 1188181 0rx_queue_index0:sum 1188181William Tu (3):
xsk: addxsk_umem_consume_tx_virtual.
veth: supportAF_XDP.
samples: bpf: add vethAF_XDPexample.drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++- include/net/xdp_sock.h | 7 ++ net/xdp/xsk.c | 24 ++++ samples/bpf/test_veth_afxdp.sh | 67 +++++++++++ 4 files changed, 343 insertions(+), 2 deletions(-) create mode 100755 samples/bpf/test_veth_afxdp.sh--
2.7.4
Thanks for the feedback.
Quoted 9 lines · Björn Töpel
Den mån 17 dec. 2018 kl 20:40 skrev William Tu <[email protected]>:
Quoted 6 lines
The patch series adds
AF_XDPasync xmit support for veth device. First patch add a new API for supporting non-physical NIC device to get packet's virtual address. The second patch implements the async xmit, and last patch adds example use cases.
Quoted 5 lines
The first virtual device with
AF_XDPsupport! Yay!This is only the zero-copy on the Tx side -- it's still allocations plus copy on the ingress side? That's a bit different from the i40e/ixgbe implementation, where zero-copy means both Tx and Rx. For
Right, it's a little different than i40e/ixgbe, which is physical nic. For veth, the xmit is just placing the packet into peer device's rx queue. Here, the veth af_xdp implementation is doing an extra copy from the umem, create the packet, and triggers the receive code at peer device.
Quoted 10 lines
veth I don't see that we need to support Rx right away, especially for Tx only sockets. Still, when the netdev has accepted the umem via
ndo_bpf, the zero-copy for both Tx and Rx is assumed. We might want to change thendo_bpfat some point to support zero-copy for Tx, Rx, Tx *and* Rx.Are you planning to add zero-copy to the ingress side, i.e. pulling frames from the fill ring, instead of allocating via
dev_alloc_page? (The term *zero-copy* for veth is a bit weird, since we're still doing copies, but eliding the page allocation. :-))
Yes, I'm trying to remove this dev_alloc_page, still not successful. Do you think we should go directly with the zero-copy version for next patch?
Quoted 4 lines
It would be interesting to hear a bit about what use-case veth/AF_XDP has, if you can share that.
Yes, so we've been working on OVS + AF_XDP netdev support. See OVS conference: Fast Userspace OVS with AF_XDP
http://www.openvswitch.org/support/ovscon2018/
AF_XDP from OVS's perspective is just a netdev doing packet I/O, that is, a faster way to send and receive packets.
With i40e/ixgbe AF_XDP support, OVS can forward packets at very high packet rate. However, users also attach virtual port to the OVS bridge, for example, tap device connected to VM, or veth peer device connected to container. So packets flow from:
Physical NIC (with AF_XDP) --> OVS --> virtual port (no AF_XDP) --> VM/container
Since there is no AF_XDP support for virtual devices yet, the performance drops significantly. That's kind of the motivation for this patch series, to add
virtual device support for AF_XDP.
Ultimately with I hope with AF_XDP support, a packet comes from
a physical nic, dma directly to umem, processed by OVS or others processing software, zero-copy to tap/veth peer devices, then received by VM/container app.
Thanks.
William
Quoted 46 lines
Cheers,
BjörnQuoted 42 lines
I tested with 2 namespaces, one as sender, the other as receiver. The packet rate is measure at the receiver side.
ip netns addat_ns0
ip link add p0 type veth peer name p1
ip link set p0 netnsat_ns0
ip link set dev p1 up
ip netns execat_ns0ip link set dev p0 up# receiver ip netns exec at_ns0 xdp_rxq_info --dev p0 --action XDP_DROP# sender with AF_XDP xdpsock -i p1 -t -N -z# or sender without AF_XDP xdpsock -i p1 -t -SWithout
AF_XDP: 724 Kpps
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index0:1 724339 0rx_queue_index0:sum 724339With
AF_XDP: 1.1 Mpps (with ksoftirqd 100% cpu)
RXQ stats RXQ:CPU pps issue-ppsrx_queue_index0:3 1188181 0rx_queue_index0:sum 1188181William Tu (3):
xsk: addxsk_umem_consume_tx_virtual.
veth: supportAF_XDP.
samples: bpf: add vethAF_XDPexample.drivers/net/veth.c | 247 ++++++++++++++++++++++++++++++++++++++++- include/net/xdp_sock.h | 7 ++ net/xdp/xsk.c | 24 ++++ samples/bpf/test_veth_afxdp.sh | 67 +++++++++++ 4 files changed, 343 insertions(+), 2 deletions(-) create mode 100755 samples/bpf/test_veth_afxdp.sh--
2.7.4