Skip to contentSkip to conversation

PMD driver for AF_XDP

PATCH In review Qi Zhang 3 participants 11 messages Aug 16, 2018 – Aug 28, 2018

Subsystems (5)
lore
Share & tools
lore

Qi Zhang

author
#0 lore raw

v3 cover PMD driver for AF_XDP

Overview
========

The patch set add a new PMD driver for AF_XDP which is a proposed faster version of AF_PACKET interface in Linux, see below link for detail AF_XDP introduction:
https://lwn.net/Articles/750845/
https://fosdem.org/2018/schedule/event/af_xdp/

AF_XDP roadmap
==============
- The kernel 4.18 is out and af_xdp is included.
https://kernelnewbies.org/Linux_4.18
- So far there is no zero copy supported driver be merged, but some are on the way.

Change logs
===========

v3:
- Re-work base on AF_XDP's interface changes.
- Support multi-queues, each dpdk queue has its own xdp socket. An xdp socket is always bound to a netdev queue.
We assume all xdp socket from the same ethdev are bound to the same netdev queue, though a netdev queue still can be bound by xdp sockets from different ethdev instances.
Below is an example of the mapping.
------------------------------------------------------
| dpdk q0 | dpdk q1 | dpdk q0 | dpdk q0 | dpdk q1 |
------------------------------------------------------

  | xsk A   | xsk B   | xsk C      | xsk D   | xsk E   |<---|
  ------------------------------------------------------    |
  |  ETHDEV 0         | ETHDEV 1   |  ETHDEV 2         |    |  DPDK
  ------------------------------------------------------------------ 
  |  netdev queue 0                |   netdev queue 1  |    |  KERNEL
  ------------------------------------------------------    |
  |                  NETDEV eth0                       |    |
  ------------------------------------------------------    |
  |                    key   xsk                       |    |
  |  ----------       --------------                   |    |
  |  |         |      | 0  | xsk A |                   |    |
  |  |         |      --------------                   |    |
  |  |         |      | 2  | xsk B |                   |    |
  |  | ebpf    |      ---------------------------------------
  |  |         |      | 3  | xsk C |                   |
  |  |     redirect ->|--------------                  |
  |  |         |      | 4  | xsk D |                   |
  |  |         |      --------------                   |
  |  |---------|      | 5  | xsk E |                   |
  |                   --------------                   |
  |-----------------------------------------------------

- It is an open question that how to load ebpf to kernel and link to specific netdev in DPDK, should it be part of PMD, or it should be handled by an independent tool? In this patchset, it takes the second option, there will be a "bind" stage before we start AF_XDP PMD, this includes below steps:
a) load ebpf program to the kernel, (the ebpf program must contain the logic to redirect packet to a xdp socket base on a redirect map).
b) link ebpf program to specific network interface.
c) expose the xdp socket redirect map id and entries number to user, so this will be parsed to PMD, and PMD will create xdp socket
for each queue and update the redirect map correctly.
(example: --vdev,iface=eth0,xsk_map_id=53,xsk_map_key_base=0,xsk_map_key_count=4)

v2:
- fix lisence header
- clean up bpf dependency, bpf program is embedded, no "xdpsock_kern.o" required
- clean up make file, only linux_header is required
- fix all the compile warning.
- fix packet number return in Tx.

How to try
==========

1. Take the kernel v4.18.
make sure you turn on XDP sockets when compiling
Networking support -->
Networking options -->
[ * ] XDP sockets
2. in the kernel source code, apply below patch and compile the bpf sample code. #make samples/bpf/
so the sample xdpsock can be used as a bind/unbind tool for af_xdp PMD, sorry for this ugly, but in future, there could be a dedicated tool in DPDK, if we agree with the idea that bpf configure in the kernel should be separated from PMD.

~~~~~~~~~~~~~~~~~~~~~~~PATCH START~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

samples/bpf/xdpsock_user.c +13 -1
index d69c8d78d3fd..44a6318043e7 100644
@@ -76,6 +76,8 @@ static int opt_poll;
 static int opt_shared_packet_buffer;
 static int opt_interval = 1;
 static u32 opt_xdp_bind_flags;
+static int opt_bind;
+static int opt_unbind;
 
 struct xdp_umem_uqueue {
 	u32 cached_prod;
@@ -662,6 +664,8 @@ static void usage(const char *prog)
 		"  -S, --xdp-skb=n	Use XDP skb-mod\n"
 		"  -N, --xdp-native=n	Enfore XDP native mode\n"
 		"  -n, --interval=n	Specify statistics update interval (default 1 sec).\n"
+		"  -b, --bind		Bind only.\n"
+		"  -u, --unbind		Unbind only.\n"
 		"\n";
 	fprintf(stderr, str, prog);
 	exit(EXIT_FAILURE);
@@ -674,7 +678,7 @@ static void parse_command_line(int argc, char **argv)
 	opterr = 0;
 
 	for (;;) {
-		c = getopt_long(argc, argv, "rtli:q:psSNn:", long_options,
+		c = getopt_long(argc, argv, "rtli:q:psSNn:bu", long_options,
 				&option_index);
 		if (c == -1)
 			break;
@@ -711,6 +715,12 @@ static void parse_command_line(int argc, char **argv)
 		case 'n':
 			opt_interval = atoi(optarg);
 			break;
+		case 'b':
+			opt_bind = 1;
+			break;
+		case 'u':
+			opt_unbind = 1;
+			break;
 		default:
 			usage(basename(argv[0]));
 		}
@@ -898,6 +908,12 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if (opt_unbind) {
+		bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
 

~~~~~~~~~~~~~~~~~~~~~~~PATCH END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3. bind
#./samples/bpf/xdpsock -i eth0 -b

in this step, an ebpf binary xdpsock_kern.o is be loaded into the kernel and linked to eth0, the ebpf source code is /samples/bpf/xdpsock_kern.c you can modify it and re-compile for a different test.

4. dump xdp socket map information. #./tools/bpf/bpftool/bpftool map -p, you will see something like below.

  },{
       "id": 56,
       "type": "xskmap",
       "name": "xsks_map",
       "flags": 0,
       "bytes_key": 4,
       "bytes_value": 4,
       "max_entries": 4,
       "bytes_memlock": 4096
   }

in this case 56 is the map id and it has 4 entries

5. start testpmd

   ./build/app/testpmd -c 0xc -n 4 --vdev eth_af_xdp,iface=enp59s0f0,xsk_map_id=56,xsk_map_key_start=2xsk_map_key_count=2 -- -i --rxq=2 --txq=2

in this case, we reserved 2 entries (2,3) in the map, and they will be mapped to queue 0 and queue 1.

6. unbind after test
./sample/bpf/xdpsock -i eth0 -u.

Performance
===========
Since no zero copy driver is ready yet.
So far only tested with DRV and SKB mode on i40e 25G the result show identical with kernel sample "xdpsock"

Qi Zhang (6):
net/af_xdp: new PMD driver
lib/mbuf: enable parse flags when create mempool lib/mempool: allow page size aligned mempool net/af_xdp: use mbuf mempool for buffer management net/af_xdp: enable zero copy
app/testpmd: add mempool flags parameter

 app/test-pmd/parameters.c                     |   12 +
 app/test-pmd/testpmd.c                        |   15 +-
 app/test-pmd/testpmd.h                        |    1 +
 config/common_base                            |    5 +
 config/common_linuxapp                        |    1 +
 drivers/net/Makefile                          |    1 +
 drivers/net/af_xdp/Makefile                   |   30 +
 drivers/net/af_xdp/meson.build                |    7 +
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 1345 +++++++++++++++++++++++++
 drivers/net/af_xdp/rte_pmd_af_xdp_version.map |    4 +
 lib/librte_mbuf/rte_mbuf.c                    |   15 +-
 lib/librte_mbuf/rte_mbuf.h                    |    8 +-
 lib/librte_mempool/rte_mempool.c              |    3 +
 lib/librte_mempool/rte_mempool.h              |    1 +
 mk/rte.app.mk                                 |    1 +
 15 files changed, 1439 insertions(+), 10 deletions(-)
 create mode 100644 drivers/net/af_xdp/Makefile
 create mode 100644 drivers/net/af_xdp/meson.build
 create mode 100644 drivers/net/af_xdp/rte_eth_af_xdp.c
 create mode 100644 drivers/net/af_xdp/rte_pmd_af_xdp_version.map
Signature · 1 line
2.13.6
author
#1 lore raw

RFC v3 1/6 net/af_xdp: new PMD driver

Add a new PMD driver for AF_XDP which is a proposed faster version of AF_PACKET interface in Linux.
https://fosdem.org/2018/schedule/event/af_xdp/
https://lwn.net/Articles/745934/

This patch enable the vanilla version.
Packet data will copy between xdp socket's memory buffer and rx queue's mbuf mempool, also memory allocation of xdp socket's memory buffer is simply managed by a fifo ring.
Further improvement will be covered in following patches.

---
 config/common_base                            |    5 +
 config/common_linuxapp                        |    1 +
 drivers/net/Makefile                          |    1 +
 drivers/net/af_xdp/Makefile                   |   30 +
 drivers/net/af_xdp/meson.build                |    7 +
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 1247 +++++++++++++++++++++++++
 drivers/net/af_xdp/rte_pmd_af_xdp_version.map |    4 +
 mk/rte.app.mk                                 |    1 +
 8 files changed, 1296 insertions(+)
 create mode 100644 drivers/net/af_xdp/Makefile
 create mode 100644 drivers/net/af_xdp/meson.build
 create mode 100644 drivers/net/af_xdp/rte_eth_af_xdp.c
 create mode 100644 drivers/net/af_xdp/rte_pmd_af_xdp_version.map
config/common_base +5 -0
index 4bcbaf923..81aa81754 100644
@@ -383,6 +383,11 @@ CONFIG_RTE_LIBRTE_VMXNET3_DEBUG_TX_FREE=n
 CONFIG_RTE_LIBRTE_PMD_AF_PACKET=n
 
 #
+# Compile software PMD backed by AF_XDP sockets (Linux only)
+#
+CONFIG_RTE_LIBRTE_PMD_AF_XDP=n
+
+#
 # Compile link bonding PMD library
 #
 CONFIG_RTE_LIBRTE_PMD_BOND=y
config/common_linuxapp +1 -0
index 9c5ea9d89..5fa1cfb87 100644
@@ -18,6 +18,7 @@ CONFIG_RTE_LIBRTE_PMD_VHOST=y
 CONFIG_RTE_LIBRTE_IFC_PMD=y
 CONFIG_RTE_LIBRTE_PMD_AF_PACKET=y
 CONFIG_RTE_LIBRTE_PMD_SOFTNIC=y
+CONFIG_RTE_LIBRTE_PMD_AF_XDP=y
 CONFIG_RTE_LIBRTE_PMD_TAP=y
 CONFIG_RTE_LIBRTE_AVP_PMD=y
 CONFIG_RTE_LIBRTE_VDEV_NETVSC_PMD=y
drivers/net/Makefile +1 -0
index 664398de9..7cff65c45 100644
@@ -9,6 +9,7 @@ ifeq ($(CONFIG_RTE_LIBRTE_THUNDERX_NICVF_PMD),d)
 endif
 
 DIRS-$(CONFIG_RTE_LIBRTE_PMD_AF_PACKET) += af_packet
+DIRS-$(CONFIG_RTE_LIBRTE_PMD_AF_XDP) += af_xdp
 DIRS-$(CONFIG_RTE_LIBRTE_ARK_PMD) += ark
 DIRS-$(CONFIG_RTE_LIBRTE_AVF_PMD) += avf
 DIRS-$(CONFIG_RTE_LIBRTE_AVP_PMD) += avp
drivers/net/af_xdp/Makefile new file +30 -0
new file mode 100644
index 000000000..8dee0144a
@@ -0,0 +1,30 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2018 Intel Corporation
+
+include $(RTE_SDK)/mk/rte.vars.mk
+
+#
+# library name
+#
+LIB = librte_pmd_af_xdp.a
+
+EXPORT_MAP := rte_pmd_af_xdp_version.map
+
+LIBABIVER := 1
+
+
+CFLAGS += -O3
+# below line should be removed
+CFLAGS += -I/home/qzhan15/bpf/usr/include
+
+CFLAGS += $(WERROR_FLAGS)
+LDLIBS += -lrte_eal -lrte_mbuf -lrte_mempool -lrte_ring
+LDLIBS += -lrte_ethdev -lrte_net -lrte_kvargs
+LDLIBS += -lrte_bus_vdev
+
+#
+# all source are stored in SRCS-y
+#
+SRCS-$(CONFIG_RTE_LIBRTE_PMD_AF_XDP) += rte_eth_af_xdp.c
+
+include $(RTE_SDK)/mk/rte.lib.mk
drivers/net/af_xdp/meson.build new file +7 -0
new file mode 100644
index 000000000..4b6652685
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2018 Intel Corporation
+
+if host_machine.system() != 'linux'
+	build = false
+endif
+sources = files('rte_eth_af_xdp.c')
drivers/net/af_xdp/rte_eth_af_xdp.c new file +1247 -0

1,247 lines not on this page · raw message

drivers/net/af_xdp/rte_pmd_af_xdp_version.map new file +4 -0
new file mode 100644
index 000000000..ef3539840
@@ -0,0 +1,4 @@
+DPDK_2.0 {
+
+	local: *;
+};
mk/rte.app.mk +1 -0
index de33883be..428ad8ab0 100644
@@ -118,6 +118,7 @@ _LDLIBS-$(CONFIG_RTE_LIBRTE_DPAA2_MEMPOOL)  += -lrte_mempool_dpaa2
 endif
 
 _LDLIBS-$(CONFIG_RTE_LIBRTE_PMD_AF_PACKET)  += -lrte_pmd_af_packet
+_LDLIBS-$(CONFIG_RTE_LIBRTE_PMD_AF_XDP)     += -lrte_pmd_af_xdp -lelf -lbpf
 _LDLIBS-$(CONFIG_RTE_LIBRTE_ARK_PMD)        += -lrte_pmd_ark
 _LDLIBS-$(CONFIG_RTE_LIBRTE_AVF_PMD)        += -lrte_pmd_avf
 _LDLIBS-$(CONFIG_RTE_LIBRTE_AVP_PMD)        += -lrte_pmd_avp
Signature · 1 line
2.13.6
author
#2 lore raw

RFC v3 2/6 lib/mbuf: enable parse flags when create mempool

In reply to Qi Zhang

This give the option that applicaiton can configure each memory chunk's size precisely. (by MEMPOOL_F_NO_SPREAD).

---
 lib/librte_mbuf/rte_mbuf.c | 15 ++++++++++++---
 lib/librte_mbuf/rte_mbuf.h |  8 +++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)
lib/librte_mbuf/rte_mbuf.c +12 -3
index e714c5a59..dd119f5ac 100644
@@ -110,7 +110,7 @@ rte_pktmbuf_init(struct rte_mempool *mp,
 struct rte_mempool *
 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
 	unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
-	int socket_id, const char *ops_name)
+	unsigned int flags, int socket_id, const char *ops_name)
 {
 	struct rte_mempool *mp;
 	struct rte_pktmbuf_pool_private mbp_priv;
@@ -130,7 +130,7 @@ rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
 	mbp_priv.mbuf_priv_size = priv_size;
 
 	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
-		 sizeof(struct rte_pktmbuf_pool_private), socket_id, 0);
+		 sizeof(struct rte_pktmbuf_pool_private), socket_id, flags);
 	if (mp == NULL)
 		return NULL;
 
@@ -164,9 +164,18 @@ rte_pktmbuf_pool_create(const char *name, unsigned int n,
 	int socket_id)
 {
 	return rte_pktmbuf_pool_create_by_ops(name, n, cache_size, priv_size,
-			data_room_size, socket_id, NULL);
+			data_room_size, 0, socket_id, NULL);
 }
 
+/* helper to create a mbuf pool with NO_SPREAD */
+struct rte_mempool *
+rte_pktmbuf_pool_create_with_flags(const char *name, unsigned int n,
+	unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
+	unsigned int flags, int socket_id)
+{
+	return rte_pktmbuf_pool_create_by_ops(name, n, cache_size, priv_size,
+			data_room_size, flags, socket_id, NULL);
+}
 /* do some sanity checks on a mbuf: panic if it fails */
 void
 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
lib/librte_mbuf/rte_mbuf.h +7 -1
index 9ce5d76d7..d83d17b79 100644
@@ -1127,6 +1127,12 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
 	int socket_id);
 
+struct rte_mempool *
+rte_pktmbuf_pool_create_with_flags(const char *name, unsigned int n,
+	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
+	unsigned flags, int socket_id);
+
+
 /**
  * Create a mbuf pool with a given mempool ops name
  *
@@ -1167,7 +1173,7 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 struct rte_mempool *
 rte_pktmbuf_pool_create_by_ops(const char *name, unsigned int n,
 	unsigned int cache_size, uint16_t priv_size, uint16_t data_room_size,
-	int socket_id, const char *ops_name);
+	unsigned int flags, int socket_id, const char *ops_name);
 
 /**
  * Get the data room size of mbufs stored in a pktmbuf_pool
Signature · 1 line
2.13.6
author
#3 lore raw

RFC v3 3/6 lib/mempool: allow page size aligned mempool

In reply to Qi Zhang

Allow create a mempool with page size aligned base address.

---
 lib/librte_mempool/rte_mempool.c | 3 +++
 lib/librte_mempool/rte_mempool.h | 1 +
 2 files changed, 4 insertions(+)
lib/librte_mempool/rte_mempool.c +3 -0
index 03e6b5f73..61f7764c5 100644
@@ -508,6 +508,9 @@ rte_mempool_populate_default(struct rte_mempool *mp)
 		if (try_contig)
 			flags |= RTE_MEMZONE_IOVA_CONTIG;
 
+		if (mp->flags & MEMPOOL_F_PAGE_ALIGN)
+			align = getpagesize();
+
 		mz = rte_memzone_reserve_aligned(mz_name, mem_size,
 				mp->socket_id, flags, align);
 
lib/librte_mempool/rte_mempool.h +1 -0
index 7c9cd9a2f..75553b36f 100644
@@ -264,6 +264,7 @@ struct rte_mempool {
 #define MEMPOOL_F_POOL_CREATED   0x0010 /**< Internal: pool is created. */
 #define MEMPOOL_F_NO_IOVA_CONTIG 0x0020 /**< Don't need IOVA contiguous objs. */
 #define MEMPOOL_F_NO_PHYS_CONTIG MEMPOOL_F_NO_IOVA_CONTIG /* deprecated */
+#define MEMPOOL_F_PAGE_ALIGN     0x0040 /**< Chunk's base address is page aligned */
 
 /**
  * @internal When debug is enabled, store some statistics.
Signature · 1 line
2.13.6
#4 lore raw

Re: [RFC v3 3/6] lib/mempool: allow page size aligned mempool

-----Original Message-----

Quoted 11 lines

Date: Thu, 16 Aug 2018 22:43:18 +0800
From: Qi Zhang <[email protected]>
To: [email protected]
CC: [email protected], [email protected],
[email protected], [email protected], [email protected], Qi Zhang <[email protected]>
Subject: [dpdk-dev] [RFC v3 3/6] lib/mempool: allow page size aligned
mempool
X-Mailer: git-send-email 2.13.6

Quoted 1 line

Allow create a mempool with page size aligned base address.

If I understand it correctly, you are doing this enable AF_XDP PMD which has some constraints. If so, I think more transparent way to handle this from application perspective would be to
1) add new mempool ops which based this new flags
and
2) and call rte_mbuf_set_platform_mempool_ops("new mempool op for af_xdp"); on af_xdp device probe()

That will avoid the need for new API and testpmd changes for flag selection.

The proposed RFC has problem where, end user needs to know what flags are relevant for certain PMD.

Quoted 36 lines
---
 lib/librte_mempool/rte_mempool.c | 3 +++
 lib/librte_mempool/rte_mempool.h | 1 +
 2 files changed, 4 insertions(+)
lib/librte_mempool/rte_mempool.c +3 -0
index 03e6b5f73..61f7764c5 100644
@@ -508,6 +508,9 @@ rte_mempool_populate_default(struct rte_mempool *mp)
                if (try_contig)
                        flags |= RTE_MEMZONE_IOVA_CONTIG;
 
+               if (mp->flags & MEMPOOL_F_PAGE_ALIGN)
+                       align = getpagesize();
+
                mz = rte_memzone_reserve_aligned(mz_name, mem_size,
                                mp->socket_id, flags, align);
 
lib/librte_mempool/rte_mempool.h +1 -1
index 7c9cd9a2f..75553b36f 100644
@@ -264,6 +264,7 @@ struct rte_mempool {
 #define MEMPOOL_F_POOL_CREATED   0x0010 /**< Internal: pool is created. */
 #define MEMPOOL_F_NO_IOVA_CONTIG 0x0020 /**< Don't need IOVA contiguous objs. */
 #define MEMPOOL_F_NO_PHYS_CONTIG MEMPOOL_F_NO_IOVA_CONTIG /* deprecated */
+#define MEMPOOL_F_PAGE_ALIGN     0x0040 /**< Chunk's base address is page aligned */
 
 /**
  * @internal When debug is enabled, store some statistics.
--

2.13.6

author
#5 lore raw

RFC v3 4/6 net/af_xdp: use mbuf mempool for buffer management

In reply to Qi Zhang

Now, af_xdp registered memory buffer is managed by rte_mempool. mbuf be allocated from rte_mempool can be convert to xdp_desc's address and vice versa.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 184 +++++++++++++++++++++---------------
 1 file changed, 108 insertions(+), 76 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +108 -76
index 12252014d..69bc38536 100644
@@ -42,7 +42,11 @@
 
 #define ETH_AF_XDP_FRAME_SIZE		2048
 #define ETH_AF_XDP_NUM_BUFFERS		4096
-#define ETH_AF_XDP_DATA_HEADROOM	0
+/* mempool hdrobj size (64 bytes) + sizeof(struct rte_mbuf) (128 bytes) */
+#define ETH_AF_XDP_MBUF_OVERHEAD	192
+/* data start from offset 320 (192 + 128) bytes */
+#define ETH_AF_XDP_DATA_HEADROOM \
+	(ETH_AF_XDP_MBUF_OVERHEAD + RTE_PKTMBUF_HEADROOM)
 #define ETH_AF_XDP_DFLT_NUM_DESCS	1024
 #define ETH_AF_XDP_FQ_NUM_DESCS		1024
 #define ETH_AF_XDP_CQ_NUM_DESCS		1024
@@ -68,7 +72,7 @@ struct xdp_umem {
 	char *frames;
 	struct xdp_umem_uqueue fq;
 	struct xdp_umem_uqueue cq;
-	struct rte_ring *buf_ring; /* be used to manage the buffer */
+	struct rte_mempool *mb_pool; /* be used to manage the buffer */
 	int fd;
 };
 
@@ -304,11 +308,25 @@ static char *get_pkt_data(struct xdp_umem *umem, uint64_t addr)
 	return &umem->frames[addr];
 }
 
+static inline struct rte_mbuf *
+addr_to_mbuf(struct xdp_umem *umem, uint64_t addr)
+{
+	return (struct rte_mbuf *)((uint64_t)umem->frames + addr - 0x100);
+}
+
+static inline uint64_t
+mbuf_to_addr(struct xdp_umem *umem, struct rte_mbuf *mbuf)
+{
+	return (uint64_t)mbuf->buf_addr + mbuf->data_off -
+		(uint64_t)umem->frames;
+}
+
 static uint16_t
 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
 	struct xdp_desc descs[ETH_AF_XDP_RX_BATCH_SIZE];
-	void *addrs[ETH_AF_XDP_RX_BATCH_SIZE];
+	struct rte_mbuf *bufs_to_fill[ETH_AF_XDP_RX_BATCH_SIZE];
+	uint64_t addrs[ETH_AF_XDP_RX_BATCH_SIZE];
 	struct pkt_rx_queue *rxq = queue;
 	struct xdp_uqueue *uq = &rxq->rx;
 	struct xdp_umem_uqueue *fq = &rxq->umem->fq;
@@ -317,25 +335,25 @@ eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	unsigned long dropped = 0;
 	unsigned long rx_bytes = 0;
 	uint16_t count = 0;
-	int rcvd, i;
+	int rcvd, i, ret;
 
 	nb_pkts = nb_pkts < ETH_AF_XDP_RX_BATCH_SIZE ?
 		  nb_pkts : ETH_AF_XDP_RX_BATCH_SIZE;
 
 	if (umem_nb_free(fq, free_thresh) >= free_thresh) {
-		int n = rte_ring_dequeue_bulk(rxq->umem->buf_ring,
-					      addrs,
-					      ETH_AF_XDP_RX_BATCH_SIZE,
-					      NULL);
-		if (n == 0)
+		ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool,
+					bufs_to_fill,
+					ETH_AF_XDP_RX_BATCH_SIZE);
+		if (ret)
 			return -ENOMEM;
 
-		if (umem_fill_to_kernel(fq, (uint64_t *)&addrs[0],
-					ETH_AF_XDP_RX_BATCH_SIZE)) {
-			rte_ring_enqueue_bulk(rxq->umem->buf_ring,
-					      addrs,
-					      ETH_AF_XDP_RX_BATCH_SIZE,
-					      NULL);
+		for (i = 0; i < ETH_AF_XDP_RX_BATCH_SIZE; i++)
+			addrs[i] = mbuf_to_addr(rxq->umem, bufs_to_fill[i]);
+
+		if (umem_fill_to_kernel(fq, addrs,
+			ETH_AF_XDP_RX_BATCH_SIZE)) {
+			for (i = 0; i < ETH_AF_XDP_RX_BATCH_SIZE; i++)
+				rte_pktmbuf_free(bufs_to_fill[i]);
 		}
 	}
 
@@ -361,11 +379,9 @@ eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		} else {
 			dropped++;
 		}
-		addrs[i] = (void *)addr;
+		rte_pktmbuf_free(addr_to_mbuf(rxq->umem, addr));
 	}
 
-	rte_ring_enqueue_bulk(rxq->umem->buf_ring, addrs, rcvd, NULL);
-
 	rxq->rx_pkts += (rcvd - dropped);
 	rxq->rx_bytes += rx_bytes;
 	rxq->rx_dropped += dropped;
@@ -375,11 +391,10 @@ eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 
 static void kick_tx(struct pkt_tx_queue *txq)
 {
-	void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
-	struct rte_ring *buf_ring = txq->pair->umem->buf_ring;
 	struct xdp_umem_uqueue *cq = &txq->pair->umem->cq;
+	uint64_t addrs[ETH_AF_XDP_TX_BATCH_SIZE];
 	int fd = txq->pair->xsk_fd;
-	int ret, n;
+	int ret, n, i;
 
 	while (1) {
 
@@ -398,9 +413,10 @@ static void kick_tx(struct pkt_tx_queue *txq)
 			n = umem_complete_from_kernel(cq,
 					(uint64_t *)&addrs[0],
 					ETH_AF_XDP_TX_BATCH_SIZE);
-			if (n > 0)
-				rte_ring_enqueue_bulk(buf_ring,
-					addrs, n, NULL);
+			for (i = 0; i < n; i++)
+				rte_pktmbuf_free(
+					addr_to_mbuf(txq->pair->umem,
+						addrs[i]));
 		}
 	}
 }
@@ -413,23 +429,21 @@ eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	struct xdp_umem_uqueue *cq = &txq->pair->umem->cq;
 	struct rte_mbuf *mbuf;
 	struct xdp_desc descs[ETH_AF_XDP_TX_BATCH_SIZE];
-	void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
-	uint16_t i, valid;
+	uint64_t addrs[ETH_AF_XDP_TX_BATCH_SIZE];
+	struct rte_mbuf *bufs_to_fill[ETH_AF_XDP_TX_BATCH_SIZE];
 	unsigned long tx_bytes = 0;
+	int i, valid, n;
 
 	nb_pkts = nb_pkts < ETH_AF_XDP_TX_BATCH_SIZE ?
 		  nb_pkts : ETH_AF_XDP_TX_BATCH_SIZE;
 
-	int n = umem_complete_from_kernel(cq, (uint64_t *)&addrs[0],
-					  ETH_AF_XDP_TX_BATCH_SIZE);
-	if (n > 0)
-		rte_ring_enqueue_bulk(txq->pair->umem->buf_ring,
-				addrs, n, NULL);
-
-	nb_pkts = rte_ring_dequeue_bulk(txq->pair->umem->buf_ring, addrs,
-					nb_pkts, NULL);
-	if (!nb_pkts)
-		return 0;
+	n = umem_complete_from_kernel(cq, addrs,
+					ETH_AF_XDP_TX_BATCH_SIZE);
+	if (n > 0) {
+		for (i = 0; i < n; i++)
+			rte_pktmbuf_free(addr_to_mbuf(txq->pair->umem,
+							addrs[i]));
+	}
 
 	valid = 0;
 	for (i = 0; i < nb_pkts; i++) {
@@ -438,7 +452,13 @@ eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
 		mbuf = bufs[i];
 		if (mbuf->pkt_len <= buf_len) {
-			descs[valid].addr = (uint64_t)addrs[valid];
+			bufs_to_fill[valid] =
+				rte_pktmbuf_alloc(txq->pair->umem->mb_pool);
+			if (!bufs_to_fill[valid])
+				break;
+			descs[valid].addr =
+				mbuf_to_addr(txq->pair->umem,
+						bufs_to_fill[valid]);
 			descs[valid].len = mbuf->pkt_len;
 			descs[valid].options = 0;
 			pkt = get_pkt_data(txq->pair->umem, descs[valid].addr);
@@ -447,20 +467,20 @@ eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			valid++;
 			tx_bytes += mbuf->pkt_len;
 		}
-		rte_pktmbuf_free(mbuf);
 	}
 
 	if (xq_enq(uq, descs, valid)) {
+		for (i = 0; i < valid; i++)
+			rte_pktmbuf_free(bufs_to_fill[i]);
+		nb_pkts = 0;
 		valid = 0;
 		tx_bytes = 0;
 	} else {
 		kick_tx(txq);
+		for (i = 0; i < nb_pkts; i++)
+			rte_pktmbuf_free(bufs[i]);
 	}
 
-	if (valid < nb_pkts)
-		rte_ring_enqueue_bulk(txq->pair->umem->buf_ring, &addrs[valid],
-				      nb_pkts - valid, NULL);
-
 	txq->err_pkts += (nb_pkts - valid);
 	txq->tx_pkts += valid;
 	txq->tx_bytes += tx_bytes;
@@ -472,13 +492,15 @@ static void
 fill_rx_desc(struct xdp_umem *umem)
 {
 	struct xdp_umem_uqueue *fq = &umem->fq;
-	void *p = NULL;
+	struct rte_mbuf *mbuf;
+	uint64_t addr;
 	uint32_t i;
 
 	for (i = 0; i < fq->size / 2; i++) {
-		rte_ring_dequeue(umem->buf_ring, &p);
-		if (umem_fill_to_kernel(fq, (uint64_t *)&p, 1)) {
-			rte_ring_enqueue(umem->buf_ring, p);
+		mbuf = rte_pktmbuf_alloc(umem->mb_pool);
+		addr = mbuf_to_addr(umem, mbuf);
+		if (umem_fill_to_kernel(fq, &addr, 1)) {
+			rte_pktmbuf_free(mbuf);
 			break;
 		}
 	}
@@ -597,14 +619,28 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
 
 static void xdp_umem_destroy(struct xdp_umem *umem)
 {
-	if (umem->frames)
-		free(umem->frames);
-	if (umem->buf_ring)
-		rte_ring_free(umem->buf_ring);
+	if (umem->mb_pool)
+		rte_mempool_free(umem->mb_pool);
 
 	free(umem);
 }
 
+static inline uint64_t get_base_addr(struct rte_mempool *mp)
+{
+	struct rte_mempool_memhdr *memhdr;
+
+	memhdr = STAILQ_FIRST(&mp->mem_list);
+	return (uint64_t)(memhdr->addr);
+}
+
+static inline uint64_t get_len(struct rte_mempool *mp)
+{
+	struct rte_mempool_memhdr *memhdr;
+
+	memhdr = STAILQ_FIRST(&mp->mem_list);
+	return (uint64_t)(memhdr->len);
+}
+
 static struct xdp_umem *xdp_umem_configure(int sfd)
 {
 	int fq_size = ETH_AF_XDP_FQ_NUM_DESCS;
@@ -612,40 +648,29 @@ static struct xdp_umem *xdp_umem_configure(int sfd)
 	struct xdp_mmap_offsets off;
 	struct xdp_umem_reg mr;
 	struct xdp_umem *umem;
-	char ring_name[0x100];
+	char pool_name[0x100];
 	socklen_t optlen;
-	void *bufs = NULL;
-	uint64_t i;
 
 	umem = calloc(1, sizeof(*umem));
 	if (!umem)
 		return NULL;
 
-	snprintf(ring_name, 0x100, "%s_%d", "af_xdp_ring", sfd);
-	umem->buf_ring = rte_ring_create(ring_name,
-					 ETH_AF_XDP_NUM_BUFFERS,
-					 SOCKET_ID_ANY,
-					 0x0);
-	if (!umem->buf_ring) {
-		RTE_LOG(ERR, PMD,
-			"Failed to create rte_ring\n");
-		goto err;
-	}
+	snprintf(pool_name, 0x100, "%s_%d", "af_xdp_ring", sfd);
+	umem->mb_pool = rte_pktmbuf_pool_create_with_flags(
+			pool_name, ETH_AF_XDP_NUM_BUFFERS,
+			250, 0,
+			ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_MBUF_OVERHEAD,
+			MEMPOOL_F_NO_SPREAD | MEMPOOL_F_PAGE_ALIGN,
+			SOCKET_ID_ANY);
 
-	for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
-		rte_ring_enqueue(umem->buf_ring,
-				 (void *)(i * ETH_AF_XDP_FRAME_SIZE +
-					  ETH_AF_XDP_DATA_HEADROOM));
-
-	if (posix_memalign(&bufs, getpagesize(), /* PAGE_SIZE aligned */
-		ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE)) {
+	if (!umem->mb_pool || umem->mb_pool->nb_mem_chunks != 1) {
 		RTE_LOG(ERR, PMD,
-			"Failed to allocate memory pool.\n");
+			"Failed to create rte_mempool\n");
 		goto err;
 	}
 
-	mr.addr = (uint64_t)bufs;
-	mr.len = ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE;
+	mr.addr = get_base_addr(umem->mb_pool);
+	mr.len = get_len(umem->mb_pool);
 	mr.chunk_size = ETH_AF_XDP_FRAME_SIZE;
 	mr.headroom = ETH_AF_XDP_DATA_HEADROOM;
 
@@ -717,7 +742,7 @@ static struct xdp_umem *xdp_umem_configure(int sfd)
 		(uint32_t *)((uint64_t)umem->cq.map + off.cr.consumer);
 	umem->cq.ring = (uint64_t *)((uint64_t)umem->cq.map + off.cr.desc);
 
-	umem->frames = bufs;
+	umem->frames = (void *)get_base_addr(umem->mb_pool);
 	umem->fd = sfd;
 
 	return umem;
@@ -729,7 +754,8 @@ static struct xdp_umem *xdp_umem_configure(int sfd)
 }
 
 static int
-xsk_configure(struct pkt_rx_queue *rxq, int ring_size, struct xdp_umem *umem)
+xsk_configure(struct pkt_rx_queue *rxq, int ring_size,
+		struct xdp_umem *umem)
 {
 	struct pkt_tx_queue *txq = rxq->pair;
 	struct xdp_mmap_offsets off;
@@ -863,6 +889,12 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
 	int xsk_key;
 	int map_fd;
 
+	if (mb_pool == NULL) {
+		RTE_LOG(ERR, PMD,
+			"Invalid mb_pool\n");
+		return -EINVAL;
+	}
+
 	if (dev->data->nb_rx_queues <= rx_queue_id) {
 		RTE_LOG(ERR, PMD,
 			"Invalid rx queue id: %d\n", rx_queue_id);
@@ -1222,7 +1254,7 @@ rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
 	for (i = 0; i < internals->xsk_map_key_count; i++)
 		queue_reset(internals, i);
 
-	rte_ring_free(internals->umem_share->buf_ring);
+	rte_mempool_free(internals->umem_share->mb_pool);
 	rte_free(internals->umem_share->frames);
 	rte_free(internals->umem_share);
 	rte_free(internals);
Signature · 1 line
2.13.6
author
#6 lore raw

RFC v3 5/6 net/af_xdp: enable zero copy

In reply to Qi Zhang

Try to check if external mempool (from rx_queue_setup) is fit for af_xdp, if it is, it will be registered to af_xdp socket directly and there will be no packet data copy on Rx and Tx.

---
 drivers/net/af_xdp/rte_eth_af_xdp.c | 158 +++++++++++++++++++++++++-----------
 1 file changed, 112 insertions(+), 46 deletions(-)
drivers/net/af_xdp/rte_eth_af_xdp.c +112 -46
index 69bc38536..c78c66a8c 100644
@@ -73,6 +73,7 @@ struct xdp_umem {
 	struct xdp_umem_uqueue fq;
 	struct xdp_umem_uqueue cq;
 	struct rte_mempool *mb_pool; /* be used to manage the buffer */
+	uint8_t zc;
 	int fd;
 };
 
@@ -258,6 +259,7 @@ struct pkt_rx_queue {
 	unsigned long rx_dropped;
 
 	struct pkt_tx_queue *pair;
+	uint8_t zc;
 };
 
 struct pkt_tx_queue {
@@ -366,20 +368,24 @@ eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		char *pkt;
 		uint64_t addr = descs[i].addr;
 
-		mbuf = rte_pktmbuf_alloc(rxq->mb_pool);
-		rte_pktmbuf_pkt_len(mbuf) =
-			rte_pktmbuf_data_len(mbuf) =
-			descs[i].len;
-		if (mbuf) {
-			pkt = get_pkt_data(rxq->umem, addr);
-			memcpy(rte_pktmbuf_mtod(mbuf, void *),
-			       pkt, descs[i].len);
-			rx_bytes += descs[i].len;
-			bufs[count++] = mbuf;
+		if (!rxq->zc) {
+			mbuf = rte_pktmbuf_alloc(rxq->mb_pool);
+			rte_pktmbuf_pkt_len(mbuf) =
+				rte_pktmbuf_data_len(mbuf) =
+				descs[i].len;
+			if (mbuf) {
+				pkt = get_pkt_data(rxq->umem, addr);
+				memcpy(rte_pktmbuf_mtod(mbuf, void *),
+					pkt, descs[i].len);
+					rx_bytes += descs[i].len;
+					bufs[count++] = mbuf;
+			} else {
+				dropped++;
+			}
+			rte_pktmbuf_free(addr_to_mbuf(rxq->umem, addr));
 		} else {
-			dropped++;
+			bufs[count++] = addr_to_mbuf(rxq->umem, addr);
 		}
-		rte_pktmbuf_free(addr_to_mbuf(rxq->umem, addr));
 	}
 
 	rxq->rx_pkts += (rcvd - dropped);
@@ -425,14 +431,17 @@ static uint16_t
 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
 	struct pkt_tx_queue *txq = queue;
+	struct xdp_umem *umem = txq->pair->umem;
 	struct xdp_uqueue *uq = &txq->tx;
 	struct xdp_umem_uqueue *cq = &txq->pair->umem->cq;
+	struct rte_mempool *mp = umem->mb_pool;
 	struct rte_mbuf *mbuf;
 	struct xdp_desc descs[ETH_AF_XDP_TX_BATCH_SIZE];
 	uint64_t addrs[ETH_AF_XDP_TX_BATCH_SIZE];
 	struct rte_mbuf *bufs_to_fill[ETH_AF_XDP_TX_BATCH_SIZE];
+	struct rte_mbuf *bufs_to_free[ETH_AF_XDP_TX_BATCH_SIZE];
 	unsigned long tx_bytes = 0;
-	int i, valid, n;
+	int i, valid, n, free, fill;
 
 	nb_pkts = nb_pkts < ETH_AF_XDP_TX_BATCH_SIZE ?
 		  nb_pkts : ETH_AF_XDP_TX_BATCH_SIZE;
@@ -446,39 +455,57 @@ eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	}
 
 	valid = 0;
+	free = 0;
+	fill = 0;
 	for (i = 0; i < nb_pkts; i++) {
-		char *pkt;
-		unsigned int buf_len =
-			ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
 		mbuf = bufs[i];
-		if (mbuf->pkt_len <= buf_len) {
-			bufs_to_fill[valid] =
-				rte_pktmbuf_alloc(txq->pair->umem->mb_pool);
-			if (!bufs_to_fill[valid])
-				break;
-			descs[valid].addr =
-				mbuf_to_addr(txq->pair->umem,
-						bufs_to_fill[valid]);
+		/* mbuf is in shared mempool, zero copy */
+		if (txq->pair->zc && bufs[i]->pool == mp) {
+			descs[valid].addr = mbuf_to_addr(umem, mbuf);
 			descs[valid].len = mbuf->pkt_len;
 			descs[valid].options = 0;
-			pkt = get_pkt_data(txq->pair->umem, descs[valid].addr);
-			memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
-			       descs[i].len);
 			valid++;
 			tx_bytes += mbuf->pkt_len;
+		} else {
+			char *pkt;
+			unsigned int buf_len =
+				ETH_AF_XDP_FRAME_SIZE -
+				ETH_AF_XDP_DATA_HEADROOM;
+			if (mbuf->pkt_len <= buf_len) {
+
+				bufs_to_fill[fill] = rte_pktmbuf_alloc(mp);
+				if (bufs_to_fill[fill] == NULL) {
+					bufs_to_free[free++] = mbuf;
+					continue;
+				}
+
+				descs[valid].addr =
+					mbuf_to_addr(umem, bufs_to_fill[fill]);
+				fill++;
+				descs[valid].len = mbuf->pkt_len;
+				descs[valid].options = 0;
+				pkt = get_pkt_data(umem, descs[valid].addr);
+				memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
+					descs[i].len);
+				valid++;
+				tx_bytes += mbuf->pkt_len;
+			}
+			bufs_to_free[free++] = mbuf;
 		}
 	}
 
 	if (xq_enq(uq, descs, valid)) {
-		for (i = 0; i < valid; i++)
+		/* if failed, all tmp mbufs need to be free */
+		for (i = 0; i < fill; i++)
 			rte_pktmbuf_free(bufs_to_fill[i]);
 		nb_pkts = 0;
 		valid = 0;
 		tx_bytes = 0;
 	} else {
+		/* if passed, original mbuf need to be free */
+		for (i = 0; i < free; i++)
+			rte_pktmbuf_free(bufs_to_free[i]);
 		kick_tx(txq);
-		for (i = 0; i < nb_pkts; i++)
-			rte_pktmbuf_free(bufs[i]);
 	}
 
 	txq->err_pkts += (nb_pkts - valid);
@@ -641,7 +668,7 @@ static inline uint64_t get_len(struct rte_mempool *mp)
 	return (uint64_t)(memhdr->len);
 }
 
-static struct xdp_umem *xdp_umem_configure(int sfd)
+static struct xdp_umem *xdp_umem_configure(int sfd, struct rte_mempool *mb_pool)
 {
 	int fq_size = ETH_AF_XDP_FQ_NUM_DESCS;
 	int cq_size = ETH_AF_XDP_CQ_NUM_DESCS;
@@ -655,18 +682,24 @@ static struct xdp_umem *xdp_umem_configure(int sfd)
 	if (!umem)
 		return NULL;
 
-	snprintf(pool_name, 0x100, "%s_%d", "af_xdp_ring", sfd);
-	umem->mb_pool = rte_pktmbuf_pool_create_with_flags(
-			pool_name, ETH_AF_XDP_NUM_BUFFERS,
-			250, 0,
-			ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_MBUF_OVERHEAD,
-			MEMPOOL_F_NO_SPREAD | MEMPOOL_F_PAGE_ALIGN,
-			SOCKET_ID_ANY);
-
-	if (!umem->mb_pool || umem->mb_pool->nb_mem_chunks != 1) {
-		RTE_LOG(ERR, PMD,
-			"Failed to create rte_mempool\n");
-		goto err;
+	if (!mb_pool) {
+		snprintf(pool_name, 0x100, "%s_%d", "af_xdp_ring", sfd);
+		umem->mb_pool = rte_pktmbuf_pool_create_with_flags(
+				pool_name, ETH_AF_XDP_NUM_BUFFERS,
+				250, 0,
+				ETH_AF_XDP_FRAME_SIZE -
+				ETH_AF_XDP_MBUF_OVERHEAD,
+				MEMPOOL_F_NO_SPREAD | MEMPOOL_F_PAGE_ALIGN,
+				SOCKET_ID_ANY);
+
+		if (!umem->mb_pool || umem->mb_pool->nb_mem_chunks != 1) {
+			RTE_LOG(ERR, PMD,
+				"Failed to create rte_mempool\n");
+			goto err;
+		}
+	} else {
+		umem->mb_pool = mb_pool;
+		umem->zc = 1;
 	}
 
 	mr.addr = get_base_addr(umem->mb_pool);
@@ -753,9 +786,34 @@ static struct xdp_umem *xdp_umem_configure(int sfd)
 
 }
 
+static uint8_t
+check_mempool_zc(struct rte_mempool *mp)
+{
+	RTE_ASSERT(mp);
+
+	/* must continues */
+	if (mp->nb_mem_chunks > 1)
+		return 0;
+
+	/* check header size */
+	if (mp->header_size != RTE_CACHE_LINE_SIZE)
+		return 0;
+
+	/* check base address */
+	if ((uint64_t)get_base_addr(mp) % getpagesize() != 0)
+		return 0;
+
+	/* check chunk size */
+	if ((mp->elt_size + mp->header_size + mp->trailer_size) %
+		ETH_AF_XDP_FRAME_SIZE != 0)
+		return 0;
+
+	return 1;
+}
+
 static int
 xsk_configure(struct pkt_rx_queue *rxq, int ring_size,
-		struct xdp_umem *umem)
+		struct xdp_umem *umem, struct rte_mempool *mb_pool)
 {
 	struct pkt_tx_queue *txq = rxq->pair;
 	struct xdp_mmap_offsets off;
@@ -767,7 +825,8 @@ xsk_configure(struct pkt_rx_queue *rxq, int ring_size,
 		return -1;
 
 	if (!umem) {
-		rxq->umem = xdp_umem_configure(rxq->xsk_fd);
+		mb_pool = check_mempool_zc(mb_pool) ? mb_pool : NULL;
+		rxq->umem = xdp_umem_configure(rxq->xsk_fd, mb_pool);
 		if (!rxq->umem)
 			goto err;
 		new_umem = 1;
@@ -918,7 +977,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
 
 	rxq->mb_pool = mb_pool;
 
-	if (xsk_configure(rxq, nb_rx_desc, internals->umem_share)) {
+	if (xsk_configure(rxq, nb_rx_desc, internals->umem_share, mb_pool)) {
 		RTE_LOG(ERR, PMD,
 			"Failed to configure xdp socket\n");
 		return -EINVAL;
@@ -945,6 +1004,13 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
 	if (!internals->umem_share)
 		internals->umem_share = rxq->umem;
 
+	if (mb_pool == internals->umem_share->mb_pool)
+		rxq->zc = internals->umem_share->zc;
+
+	if (rxq->zc)
+		RTE_LOG(INFO, PMD,
+			"zero copy enabled on rx queue %d\n", rx_queue_id);
+
 	internals->umem_share_count++;
 	map_fd = bpf_map_get_fd_by_id(internals->xsk_map_id);
 
Signature · 1 line
2.13.6
author
#7 lore raw

RFC v3 6/6 app/testpmd: add mempool flags parameter

In reply to Qi Zhang

When create rte_mempool, flags can be parsed from command line. Now, it is possible for testpmd to create a af_xdp friendly mempool (which enable zero copy).

---
 app/test-pmd/parameters.c | 12 ++++++++++++
 app/test-pmd/testpmd.c    | 15 +++++++++------
 app/test-pmd/testpmd.h    |  1 +
 3 files changed, 22 insertions(+), 6 deletions(-)
app/test-pmd/parameters.c +12 -0
index 962fad789..a5778e1a2 100644
@@ -61,6 +61,7 @@ usage(char* progname)
 	       "--tx-first | --stats-period=PERIOD | "
 	       "--coremask=COREMASK --portmask=PORTMASK --numa "
 	       "--mbuf-size= | --total-num-mbufs= | "
+	       "--mp-flags= | "
 	       "--nb-cores= | --nb-ports= | "
 #ifdef RTE_LIBRTE_CMDLINE
 	       "--eth-peers-configfile= | "
@@ -105,6 +106,7 @@ usage(char* progname)
 	printf("  --socket-num=N: set socket from which all memory is allocated "
 	       "in NUMA mode.\n");
 	printf("  --mbuf-size=N: set the data size of mbuf to N bytes.\n");
+	printf("  --mp-flags=N: set the flags when create mbuf memory pool.\n");
 	printf("  --total-num-mbufs=N: set the number of mbufs to be allocated "
 	       "in mbuf pools.\n");
 	printf("  --max-pkt-len=N: set the maximum size of packet to N bytes.\n");
@@ -568,6 +570,7 @@ launch_args_parse(int argc, char** argv)
 		{ "ring-numa-config",           1, 0, 0 },
 		{ "socket-num",			1, 0, 0 },
 		{ "mbuf-size",			1, 0, 0 },
+		{ "mp-flags",			1, 0, 0 },
 		{ "total-num-mbufs",		1, 0, 0 },
 		{ "max-pkt-len",		1, 0, 0 },
 		{ "pkt-filter-mode",            1, 0, 0 },
@@ -772,6 +775,15 @@ launch_args_parse(int argc, char** argv)
 					rte_exit(EXIT_FAILURE,
 						 "mbuf-size should be > 0 and < 65536\n");
 			}
+			if (!strcmp(lgopts[opt_idx].name, "mp-flags")) {
+				n = atoi(optarg);
+				if (n > 0 && n <= 0xFFFF)
+					mp_flags = (uint16_t)n;
+				else
+					rte_exit(EXIT_FAILURE,
+						 "mp-flags should be > 0 and < 65536\n");
+			}
+
 			if (!strcmp(lgopts[opt_idx].name, "total-num-mbufs")) {
 				n = atoi(optarg);
 				if (n > 1024)
app/test-pmd/testpmd.c +9 -6
index ee48db2a3..0567cc5dd 100644
@@ -173,6 +173,7 @@ uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
 
 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
+uint16_t mp_flags = 0; /**< flags parsed when create mempool */
 uint32_t param_total_num_mbufs = 0;  /**< number of mbufs in all pools - if
                                       * specified on command-line. */
 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
@@ -533,6 +534,7 @@ set_def_fwd_config(void)
  */
 static void
 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
+		 unsigned int flags,
 		 unsigned int socket_id)
 {
 	char pool_name[RTE_MEMPOOL_NAMESIZE];
@@ -550,7 +552,7 @@ mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
 		rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
 			mb_size, (unsigned) mb_mempool_cache,
 			sizeof(struct rte_pktmbuf_pool_private),
-			socket_id, 0);
+			socket_id, flags);
 		if (rte_mp == NULL)
 			goto err;
 
@@ -565,8 +567,8 @@ mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
 		/* wrapper to rte_mempool_create() */
 		TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
 				rte_mbuf_best_mempool_ops());
-		rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
-			mb_mempool_cache, 0, mbuf_seg_size, socket_id);
+		rte_mp = rte_pktmbuf_pool_create_with_flags(pool_name, nb_mbuf,
+			mb_mempool_cache, 0, mbuf_seg_size, flags, socket_id);
 	}
 
 err:
@@ -797,13 +799,14 @@ init_config(void)
 
 		for (i = 0; i < num_sockets; i++)
 			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
-					 socket_ids[i]);
+					 mp_flags, socket_ids[i]);
 	} else {
 		if (socket_num == UMA_NO_CONFIG)
-			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
+			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
+					 mp_flags, 0);
 		else
 			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
-						 socket_num);
+					 mp_flags, socket_num);
 	}
 
 	init_port_config();
app/test-pmd/testpmd.h +1 -0
index a1f661472..f5f8692ea 100644
@@ -379,6 +379,7 @@ extern uint8_t dcb_config;
 extern uint8_t dcb_test;
 
 extern uint16_t mbuf_data_size; /**< Mbuf data space size. */
+extern uint16_t mp_flags;  /**< flags for mempool creation. */
 extern uint32_t param_total_num_mbufs;
 
 extern uint16_t stats_period;
Signature · 1 line
2.13.6
#8 lore raw
In reply to Qi Zhang

Hi Zhang Qi,

I'm not familiar with DPDK code, but I'm curious about the benefits of using AF_XDP pmd, specifically I have a couple questions:

1) With zero-copy driver support, is AF_XDP pmd expects to have similar performance than other pmd? Since AF_XDP is still using native device driver, isn't the interrupt still there and not "poll-mode" anymore?

2) does the patch expect user to customize the ebpf/xdp code so that this becomes another way to extend dpdk datapath?

Thank you
William

Quoted 213 lines · Qi Zhang

Overview
========

The patch set add a new PMD driver for AF_XDP which is a proposed faster version of AF_PACKET interface in Linux, see below link for detail AF_XDP introduction:
https://lwn.net/Articles/750845/
https://fosdem.org/2018/schedule/event/af_xdp/

AF_XDP roadmap
==============
- The kernel 4.18 is out and af_xdp is included.
https://kernelnewbies.org/Linux_4.18
- So far there is no zero copy supported driver be merged, but some are on the way.

Change logs
===========

v3:
- Re-work base on AF_XDP's interface changes.
- Support multi-queues, each dpdk queue has its own xdp socket. An xdp socket is always bound to a netdev queue.
We assume all xdp socket from the same ethdev are bound to the same netdev queue, though a netdev queue still can be bound by xdp sockets from different ethdev instances.
Below is an example of the mapping.
------------------------------------------------------
| dpdk q0 | dpdk q1 | dpdk q0 | dpdk q0 | dpdk q1 |
------------------------------------------------------

  | xsk A   | xsk B   | xsk C      | xsk D   | xsk E   |<---|
  ------------------------------------------------------    |
  |  ETHDEV 0         | ETHDEV 1   |  ETHDEV 2         |    |  DPDK
  ------------------------------------------------------------------
  |  netdev queue 0                |   netdev queue 1  |    |  KERNEL
  ------------------------------------------------------    |
  |                  NETDEV eth0                       |    |
  ------------------------------------------------------    |
  |                    key   xsk                       |    |
  |  ----------       --------------                   |    |
  |  |         |      | 0  | xsk A |                   |    |
  |  |         |      --------------                   |    |
  |  |         |      | 2  | xsk B |                   |    |
  |  | ebpf    |      ---------------------------------------
  |  |         |      | 3  | xsk C |                   |
  |  |     redirect ->|--------------                  |
  |  |         |      | 4  | xsk D |                   |
  |  |         |      --------------                   |
  |  |---------|      | 5  | xsk E |                   |
  |                   --------------                   |
  |-----------------------------------------------------

- It is an open question that how to load ebpf to kernel and link to specific netdev in DPDK, should it be part of PMD, or it should be handled by an independent tool? In this patchset, it takes the second option, there will be a "bind" stage before we start AF_XDP PMD, this includes below steps:
a) load ebpf program to the kernel, (the ebpf program must contain the logic to redirect packet to a xdp socket base on a redirect map).
b) link ebpf program to specific network interface.
c) expose the xdp socket redirect map id and entries number to user, so this will be parsed to PMD, and PMD will create xdp socket
for each queue and update the redirect map correctly.
(example: --vdev,iface=eth0,xsk_map_id=53,xsk_map_key_base=0,xsk_map_key_count=4)

v2:
- fix lisence header
- clean up bpf dependency, bpf program is embedded, no "xdpsock_kern.o" required
- clean up make file, only linux_header is required
- fix all the compile warning.
- fix packet number return in Tx.

How to try
==========

1. Take the kernel v4.18.
make sure you turn on XDP sockets when compiling
Networking support -->
Networking options -->
[ * ] XDP sockets
2. in the kernel source code, apply below patch and compile the bpf sample code. #make samples/bpf/
so the sample xdpsock can be used as a bind/unbind tool for af_xdp PMD, sorry for this ugly, but in future, there could be a dedicated tool in DPDK, if we agree with the idea that bpf configure in the kernel should be separated from PMD.

~~~~~~~~~~~~~~~~~~~~~~~PATCH START~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

samples/bpf/xdpsock_user.c +13 -1
index d69c8d78d3fd..44a6318043e7 100644
@@ -76,6 +76,8 @@ static int opt_poll;
 static int opt_shared_packet_buffer;
 static int opt_interval = 1;
 static u32 opt_xdp_bind_flags;
+static int opt_bind;
+static int opt_unbind;
 
 struct xdp_umem_uqueue {
        u32 cached_prod;
@@ -662,6 +664,8 @@ static void usage(const char *prog)
                "  -S, --xdp-skb=n      Use XDP skb-mod\n"
                "  -N, --xdp-native=n   Enfore XDP native mode\n"
                "  -n, --interval=n     Specify statistics update interval (default 1 sec).\n"
+               "  -b, --bind           Bind only.\n"
+               "  -u, --unbind         Unbind only.\n"
                "\n";
        fprintf(stderr, str, prog);
        exit(EXIT_FAILURE);
@@ -674,7 +678,7 @@ static void parse_command_line(int argc, char **argv)
        opterr = 0;
 
        for (;;) {
-               c = getopt_long(argc, argv, "rtli:q:psSNn:", long_options,
+               c = getopt_long(argc, argv, "rtli:q:psSNn:bu", long_options,
                                &option_index);
                if (c == -1)
                        break;
@@ -711,6 +715,12 @@ static void parse_command_line(int argc, char **argv)
                case 'n':
                        opt_interval = atoi(optarg);
                        break;
+               case 'b':
+                       opt_bind = 1;
+                       break;
+               case 'u':
+                       opt_unbind = 1;
+                       break;
                default:
                        usage(basename(argv[0]));
                }
@@ -898,6 +908,12 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
+       if (opt_unbind) {
+               bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
 

~~~~~~~~~~~~~~~~~~~~~~~PATCH END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3. bind
#./samples/bpf/xdpsock -i eth0 -b

in this step, an ebpf binary xdpsock_kern.o is be loaded into the kernel and linked to eth0, the ebpf source code is /samples/bpf/xdpsock_kern.c you can modify it and re-compile for a different test.

4. dump xdp socket map information. #./tools/bpf/bpftool/bpftool map -p, you will see something like below.

  },{
       "id": 56,
       "type": "xskmap",
       "name": "xsks_map",
       "flags": 0,
       "bytes_key": 4,
       "bytes_value": 4,
       "max_entries": 4,
       "bytes_memlock": 4096
   }

in this case 56 is the map id and it has 4 entries

5. start testpmd

   ./build/app/testpmd -c 0xc -n 4 --vdev eth_af_xdp,iface=enp59s0f0,xsk_map_id=56,xsk_map_key_start=2xsk_map_key_count=2 -- -i --rxq=2 --txq=2

in this case, we reserved 2 entries (2,3) in the map, and they will be mapped to queue 0 and queue 1.

6. unbind after test
./sample/bpf/xdpsock -i eth0 -u.

Performance
===========
Since no zero copy driver is ready yet.
So far only tested with DRV and SKB mode on i40e 25G the result show identical with kernel sample "xdpsock"

Qi Zhang (6):
net/af_xdp: new PMD driver
lib/mbuf: enable parse flags when create mempool lib/mempool: allow page size aligned mempool net/af_xdp: use mbuf mempool for buffer management net/af_xdp: enable zero copy
app/testpmd: add mempool flags parameter

 app/test-pmd/parameters.c                     |   12 +
 app/test-pmd/testpmd.c                        |   15 +-
 app/test-pmd/testpmd.h                        |    1 +
 config/common_base                            |    5 +
 config/common_linuxapp                        |    1 +
 drivers/net/Makefile                          |    1 +
 drivers/net/af_xdp/Makefile                   |   30 +
 drivers/net/af_xdp/meson.build                |    7 +
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 1345 +++++++++++++++++++++++++
 drivers/net/af_xdp/rte_pmd_af_xdp_version.map |    4 +
 lib/librte_mbuf/rte_mbuf.c                    |   15 +-
 lib/librte_mbuf/rte_mbuf.h                    |    8 +-
 lib/librte_mempool/rte_mempool.c              |    3 +
 lib/librte_mempool/rte_mempool.h              |    1 +
 mk/rte.app.mk                                 |    1 +
 15 files changed, 1439 insertions(+), 10 deletions(-)
 create mode 100644 drivers/net/af_xdp/Makefile
 create mode 100644 drivers/net/af_xdp/meson.build
 create mode 100644 drivers/net/af_xdp/rte_eth_af_xdp.c
 create mode 100644 drivers/net/af_xdp/rte_pmd_af_xdp_version.map

--
2.13.6

#9 lore raw

Hi William:

Quoted 9 lines

-----Original Message-----
From: William Tu [mailto:[email protected]]
Sent: Friday, August 24, 2018 12:25 AM
To: Zhang, Qi Z <[email protected]>
Cc: [email protected]; Karlsson, Magnus <[email protected]>; Topel, Bjorn <[email protected]>; Wu, Jingjing <[email protected]>; Li, Xiaoyun <[email protected]>; Yigit, Ferruh <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v3 0/6] PMD driver for AF_XDP

Quoted 7 lines

Hi Zhang Qi,

I'm not familiar with DPDK code, but I'm curious about the benefits of using AF_XDP pmd, specifically I have a couple questions:

1) With zero-copy driver support, is AF_XDP pmd expects to have similar performance than other pmd?

Zero-copy will improve performance a lot, but it still have gap with native DPDK PMD. basically it's kind of less performance but more flexible solution.

BTW, Patches to enable zero copy for i40e just be published by Bjorn, there is some performance data for your reference.
http://lists.openwall.net/netdev/2018/08/28/62

Quoted 3 lines

Since AF_XDP is still using native device driver, isn't the interrupt still there and not "poll-mode" anymore?

Yes, it's still napi->poll triggered by interrupt.

Quoted 3 lines

2) does the patch expect user to customize the ebpf/xdp code so that this becomes another way to extend dpdk datapath?

Yes, this provides another option to use kernel's eBPF eco-system for packet filtering,. And it will be easy for us to develop some tool to load/link/expose ebpf as part of DPDK I think.

According to AF_XDP PMD, my view is since DPDK is very popular, it is becoming some standard way to develop network applications. So a DPDK PMD is going to be a bridge for developers to take advantage of the AF_XDP technology if compared to deal with the XDP Socket and libc directly.

Regards
Qi

Quoted 241 lines

Thank you
William

Quoted 31 lines · Qi Zhang

Overview
========

The patch set add a new PMD driver for AF_XDP which is a proposed faster version of AF_PACKET interface in Linux, see below link for detail AF_XDP introduction:
https://lwn.net/Articles/750845/
https://fosdem.org/2018/schedule/event/af_xdp/

AF_XDP roadmap
==============
- The kernel 4.18 is out and af_xdp is included.
https://kernelnewbies.org/Linux_4.18
- So far there is no zero copy supported driver be merged, but some are on the way.

Change logs
===========

v3:
- Re-work base on AF_XDP's interface changes.
- Support multi-queues, each dpdk queue has its own xdp socket. An xdp socket is always bound to a netdev queue.
We assume all xdp socket from the same ethdev are bound to the same netdev queue, though a netdev queue still can be bound by xdp sockets from different ethdev instances.
Below is an example of the mapping.
------------------------------------------------------
| dpdk q0 | dpdk q1 | dpdk q0 | dpdk q0 | dpdk q1 |
------------------------------------------------------

Quoted 3 lines
  | xsk A   | xsk B   | xsk C      | xsk D   | xsk E   |<---|
  ------------------------------------------------------    |
  |  ETHDEV 0         | ETHDEV 1   |  ETHDEV 2         |    |

DPDK

Quoted 2 lines

------------------------------------------------------------------
| netdev queue 0 | netdev queue 1 | |

KERNEL

Quoted 17 lines
  ------------------------------------------------------    |
  |                  NETDEV eth0                       |    |
  ------------------------------------------------------    |
  |                    key   xsk                       |    |
  |  ----------       --------------                   |    |
  |  |         |      | 0  | xsk A |                   |    |
  |  |         |      --------------                   |    |
  |  |         |      | 2  | xsk B |                   |    |
  |  | ebpf    |      ---------------------------------------
  |  |         |      | 3  | xsk C |                   |
  |  |     redirect ->|--------------                  |
  |  |         |      | 4  | xsk D |                   |
  |  |         |      --------------                   |
  |  |---------|      | 5  | xsk E |                   |
  |                   --------------                   |
  |-----------------------------------------------------
Quoted 2 lines

- It is an open question that how to load ebpf to kernel and link to specific netdev in DPDK, should it be part of PMD, or it should be

handled by

Quoted 1 line

an independent tool? In this patchset, it takes the second option, there

will

Quoted 1 line

be a "bind" stage before we start AF_XDP PMD, this includes below

steps:

Quoted 8 lines

a) load ebpf program to the kernel, (the ebpf program must contain the logic to redirect packet to a xdp socket base on a redirect map).
b) link ebpf program to specific network interface.
c) expose the xdp socket redirect map id and entries number to user, so this will be parsed to PMD, and PMD will create xdp socket for each queue and update the redirect map correctly. (example:

--vdev,iface=eth0,xsk_map_id=53,xsk_map_key_base=0,xsk_map_key_count =4

Quoted 5 lines

)

v2:
- fix lisence header
- clean up bpf dependency, bpf program is embedded, no

"xdpsock_kern.o"

Quoted 8 lines

required
- clean up make file, only linux_header is required
- fix all the compile warning.
- fix packet number return in Tx.

How to try
==========

Quoted 6 lines

1. Take the kernel v4.18.
make sure you turn on XDP sockets when compiling
Networking support -->
Networking options -->
[ * ] XDP sockets
2. in the kernel source code, apply below patch and compile the bpf sample

code.

Quoted 22 lines

#make samples/bpf/
so the sample xdpsock can be used as a bind/unbind tool for af_xdp PMD, sorry for this ugly, but in future, there could be a dedicated tool in DPDK, if we agree with the idea that bpf configure in the kernel should be separated from PMD.

~~~~~~~~~~~~~~~~~~~~~~~PATCH
START~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

samples/bpf/xdpsock_user.c +0 -0
index d69c8d78d3fd..44a6318043e7 100644
@@ -76,6 +76,8 @@ static int opt_poll;  static int

opt_shared_packet_buffer; static int opt_interval = 1; static u32 opt_xdp_bind_flags;

samples/bpf/xdpsock_user.c +2 -0
+static int opt_bind;
+static int opt_unbind;
 
 struct xdp_umem_uqueue {
        u32 cached_prod;

samples/bpf/xdpsock_user.c static void usage(const char *prog) · line 666

Quoted 3 lines
samples/bpf/xdpsock_user.c +0 -0
@@ -662,6 +664,8 @@ static void usage(const char *prog)
                "  -S, --xdp-skb=n      Use XDP skb-mod\n"
                "  -N, --xdp-native=n   Enfore XDP native mode\n"
                "  -n, --interval=n     Specify statistics update

interval (default 1 sec).\n"

samples/bpf/xdpsock_user.c static void parse_command_line(int argc, char

Quoted 6 lines
samples/bpf/xdpsock_user.c +2 -0
+               "  -b, --bind           Bind only.\n"
+               "  -u, --unbind         Unbind only.\n"
                "\n";
        fprintf(stderr, str, prog);
        exit(EXIT_FAILURE);
@@ -674,7 +678,7 @@ static void parse_command_line(int argc, char

**argv)

samples/bpf/xdpsock_user.c static void parse_command_line(int argc, char

Quoted 10 lines
samples/bpf/xdpsock_user.c +2 -1
        opterr = 0;
 
        for (;;) {
-               c = getopt_long(argc, argv, "rtli:q:psSNn:", long_options,
+               c = getopt_long(argc, argv, "rtli:q:psSNn:bu",
+ long_options,
                                &option_index);
                if (c == -1)
                        break;
@@ -711,6 +715,12 @@ static void parse_command_line(int argc, char

**argv)

Quoted 22 lines
samples/bpf/xdpsock_user.c +8 -0
                case 'n':
                        opt_interval = atoi(optarg);
                        break;
+               case 'b':
+                       opt_bind = 1;
+                       break;
+               case 'u':
+                       opt_unbind = 1;
+                       break;
                default:
                        usage(basename(argv[0]));
                }
@@ -898,6 +908,12 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
+       if (opt_unbind) {
+               bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
 

~~~~~~~~~~~~~~~~~~~~~~~PATCH
END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 5 lines

3. bind
#./samples/bpf/xdpsock -i eth0 -b

in this step, an ebpf binary xdpsock_kern.o is be loaded into the kernel and linked to eth0, the ebpf source code is

/samples/bpf/xdpsock_kern.c

Quoted 16 lines

you can modify it and re-compile for a different test.

4. dump xdp socket map information. #./tools/bpf/bpftool/bpftool map -p, you will see something like below.

  },{
       "id": 56,
       "type": "xskmap",
       "name": "xsks_map",
       "flags": 0,
       "bytes_key": 4,
       "bytes_value": 4,
       "max_entries": 4,
       "bytes_memlock": 4096
   }
Quoted 6 lines

in this case 56 is the map id and it has 4 entries

5. start testpmd

   ./build/app/testpmd -c 0xc -n 4 --vdev

eth_af_xdp,iface=enp59s0f0,xsk_map_id=56,xsk_map_key_start=2xsk_map_ ke

Quoted 3 lines

y_count=2 -- -i --rxq=2 --txq=2

in this case, we reserved 2 entries (2,3) in the map, and they will be

mapped to queue 0 and queue 1.

Quoted 18 lines

6. unbind after test
./sample/bpf/xdpsock -i eth0 -u.

Performance
===========
Since no zero copy driver is ready yet.
So far only tested with DRV and SKB mode on i40e 25G the result show identical with kernel sample "xdpsock"

Qi Zhang (6):
net/af_xdp: new PMD driver
lib/mbuf: enable parse flags when create mempool lib/mempool: allow page size aligned mempool net/af_xdp: use mbuf mempool for buffer management net/af_xdp: enable zero copy
app/testpmd: add mempool flags parameter

Quoted 9 lines
 app/test-pmd/parameters.c                     |   12 +
 app/test-pmd/testpmd.c                        |   15 +-
 app/test-pmd/testpmd.h                        |    1 +
 config/common_base                            |    5 +
 config/common_linuxapp                        |    1 +
 drivers/net/Makefile                          |    1 +
 drivers/net/af_xdp/Makefile                   |   30 +
 drivers/net/af_xdp/meson.build                |    7 +
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 1345

+++++++++++++++++++++++++

Quoted 15 lines
 drivers/net/af_xdp/rte_pmd_af_xdp_version.map |    4 +
 lib/librte_mbuf/rte_mbuf.c                    |   15 +-
 lib/librte_mbuf/rte_mbuf.h                    |    8 +-
 lib/librte_mempool/rte_mempool.c              |    3 +
 lib/librte_mempool/rte_mempool.h              |    1 +
 mk/rte.app.mk                                 |    1 +
 15 files changed, 1439 insertions(+), 10 deletions(-)  create mode
100644 drivers/net/af_xdp/Makefile  create mode 100644
drivers/net/af_xdp/meson.build  create mode 100644
drivers/net/af_xdp/rte_eth_af_xdp.c
 create mode 100644 drivers/net/af_xdp/rte_pmd_af_xdp_version.map

--
2.13.6

#10 lore raw
In reply to Qi Zhang

Sorry, the patch for kernel sample code is not complete. It should be as below

~~~~~~~~~~~~~~~~~~~~~~~PATCH START ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

samples/bpf/xdpsock_user.c +22 -1
index d69c8d78d3fd..44a6318043e7 100644
@@ -76,6 +76,8 @@ static int opt_poll;
 static int opt_shared_packet_buffer;
 static int opt_interval = 1;
 static u32 opt_xdp_bind_flags;
+static int opt_bind;
+static int opt_unbind;
 
 struct xdp_umem_uqueue {
 	u32 cached_prod;
@@ -662,6 +664,8 @@ static void usage(const char *prog)
 		"  -S, --xdp-skb=n	Use XDP skb-mod\n"
 		"  -N, --xdp-native=n	Enfore XDP native mode\n"
 		"  -n, --interval=n	Specify statistics update interval (default 1 sec).\n"
+		"  -b, --bind		Bind only.\n"
+		"  -u, --unbind		Unbind only.\n"
 		"\n";
 	fprintf(stderr, str, prog);
 	exit(EXIT_FAILURE);
@@ -674,7 +678,7 @@ static void parse_command_line(int argc, char **argv)
 	opterr = 0;
 
 	for (;;) {
-		c = getopt_long(argc, argv, "rtli:q:psSNn:", long_options,
+		c = getopt_long(argc, argv, "rtli:q:psSNn:bu", long_options,
 				&option_index);
 		if (c == -1)
 			break;
@@ -711,6 +715,12 @@ static void parse_command_line(int argc, char **argv)
 		case 'n':
 			opt_interval = atoi(optarg);
 			break;
+		case 'b':
+			opt_bind = 1;
+			break;
+		case 'u':
+			opt_unbind = 1;
+			break;
 		default:
 			usage(basename(argv[0]));
 		}
@@ -898,6 +908,12 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if (opt_unbind) {
+		bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
+		printf("unbind.\n");
+		return 0;
+	}
+
 	snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
 
 	if (load_bpf_file(xdp_filename)) {
@@ -922,6 +938,11 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if (opt_bind) {
+		printf("bind.\n");
+		return 0;
+	}
+
 	/* Create sockets... */
 	xsks[num_socks++] = xsk_configure(NULL);
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PATCH END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BTW, there is bug in kernel bpftool on 4.18, it will cause segment fault when you try to dump bpf map with #./tools/bpf/bpftool/bpftool map -p

So, please also apply below patch

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PATCH START ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tools/bpf/bpftool/map.c +1 -0
index 097b1a5e046b..0c661de58976 100644
@@ -67,6 +67,7 @@ static const char * const map_type_name[] = {
 	[BPF_MAP_TYPE_SOCKMAP]		= "sockmap",
 	[BPF_MAP_TYPE_CPUMAP]		= "cpumap",
 	[BPF_MAP_TYPE_SOCKHASH]		= "sockhash",
+	[BPF_MAP_TYPE_XSKMAP]		= "xskmap"
 };
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PATCH END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 237 lines

-----Original Message-----
From: Zhang, Qi Z
Sent: Thursday, August 16, 2018 10:43 PM
To: [email protected]
Cc: Karlsson, Magnus <[email protected]>; Topel, Bjorn <[email protected]>; Wu, Jingjing <[email protected]>; Li, Xiaoyun <[email protected]>; Yigit, Ferruh <[email protected]>; Zhang, Qi Z <[email protected]>
Subject: [PATCH v3 0/6] PMD driver for AF_XDP

Overview
========

The patch set add a new PMD driver for AF_XDP which is a proposed faster version of AF_PACKET interface in Linux, see below link for detail AF_XDP introduction:
https://lwn.net/Articles/750845/
https://fosdem.org/2018/schedule/event/af_xdp/

AF_XDP roadmap
==============
- The kernel 4.18 is out and af_xdp is included.
https://kernelnewbies.org/Linux_4.18
- So far there is no zero copy supported driver be merged, but some are on the way.

Change logs
===========

v3:
- Re-work base on AF_XDP's interface changes.
- Support multi-queues, each dpdk queue has its own xdp socket. An xdp socket is always bound to a netdev queue.
We assume all xdp socket from the same ethdev are bound to the same netdev queue, though a netdev queue still can be bound by xdp sockets from different ethdev instances.
Below is an example of the mapping.
------------------------------------------------------
| dpdk q0 | dpdk q1 | dpdk q0 | dpdk q0 | dpdk q1 |
------------------------------------------------------

  | xsk A   | xsk B   | xsk C      | xsk D   | xsk E   |<---|
  ------------------------------------------------------    |
  |  ETHDEV 0         | ETHDEV 1   |  ETHDEV 2         |    |

DPDK
------------------------------------------------------------------
| netdev queue 0 | netdev queue 1 | |
KERNEL

  ------------------------------------------------------    |
  |                  NETDEV eth0                       |    |
  ------------------------------------------------------    |
  |                    key   xsk                       |    |
  |  ----------       --------------                   |    |
  |  |         |      | 0  | xsk A |                   |    |
  |  |         |      --------------                   |    |
  |  |         |      | 2  | xsk B |                   |    |
  |  | ebpf    |      ---------------------------------------
  |  |         |      | 3  | xsk C |                   |
  |  |     redirect ->|--------------                  |
  |  |         |      | 4  | xsk D |                   |
  |  |         |      --------------                   |
  |  |---------|      | 5  | xsk E |                   |
  |                   --------------                   |
  |-----------------------------------------------------

- It is an open question that how to load ebpf to kernel and link to specific netdev in DPDK, should it be part of PMD, or it should be handled
by
an independent tool? In this patchset, it takes the second option, there will be a "bind" stage before we start AF_XDP PMD, this includes below steps:
a) load ebpf program to the kernel, (the ebpf program must contain the logic to redirect packet to a xdp socket base on a redirect map).
b) link ebpf program to specific network interface.
c) expose the xdp socket redirect map id and entries number to user, so this will be parsed to PMD, and PMD will create xdp socket for each queue and update the redirect map correctly. (example:
--vdev,iface=eth0,xsk_map_id=53,xsk_map_key_base=0,xsk_map_key_count =4)

v2:
- fix lisence header
- clean up bpf dependency, bpf program is embedded, no "xdpsock_kern.o"
required
- clean up make file, only linux_header is required
- fix all the compile warning.
- fix packet number return in Tx.

How to try
==========

1. Take the kernel v4.18.
make sure you turn on XDP sockets when compiling
Networking support -->
Networking options -->
[ * ] XDP sockets
2. in the kernel source code, apply below patch and compile the bpf sample code.
#make samples/bpf/
so the sample xdpsock can be used as a bind/unbind tool for af_xdp PMD, sorry for this ugly, but in future, there could be a dedicated tool in DPDK, if we agree with the idea that bpf configure in the kernel should be separated from PMD.

~~~~~~~~~~~~~~~~~~~~~~~PATCH
START~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

samples/bpf/xdpsock_user.c index renamed from samples/bpf/xdpsock_user.c +0 -0

d69c8d78d3fd..44a6318043e7 100644

samples/bpf/xdpsock_user.c +2 -0
@@ -76,6 +76,8 @@ static int opt_poll;
 static int opt_shared_packet_buffer;
 static int opt_interval = 1;
 static u32 opt_xdp_bind_flags;
+static int opt_bind;
+static int opt_unbind;
 
 struct xdp_umem_uqueue {
 	u32 cached_prod;
@@ -662,6 +664,8 @@ static void usage(const char *prog)
 		"  -S, --xdp-skb=n	Use XDP skb-mod\n"
 		"  -N, --xdp-native=n	Enfore XDP native mode\n"
 		"  -n, --interval=n	Specify statistics update interval (default 1

sec).\n"

samples/bpf/xdpsock_user.c +3 -1
+		"  -b, --bind		Bind only.\n"
+		"  -u, --unbind		Unbind only.\n"
 		"\n";
 	fprintf(stderr, str, prog);
 	exit(EXIT_FAILURE);
@@ -674,7 +678,7 @@ static void parse_command_line(int argc, char
 **argv)
 	opterr = 0;
 
 	for (;;) {
-		c = getopt_long(argc, argv, "rtli:q:psSNn:", long_options,
+		c = getopt_long(argc, argv, "rtli:q:psSNn:bu", long_options,
 				&option_index);
 		if (c == -1)
samples/bpf/xdpsock_user.c +6 -0
 			break;
@@ -711,6 +715,12 @@ static void parse_command_line(int argc, char
 **argv)
 		case 'n':
 			opt_interval = atoi(optarg);
 			break;
+		case 'b':
+			opt_bind = 1;
+			break;
+		case 'u':
+			opt_unbind = 1;
+			break;
 		default:
 			usage(basename(argv[0]));
samples/bpf/xdpsock_user.c +2 -0
 		}
@@ -898,6 +908,12 @@ int main(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if (opt_unbind) {
+		bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
 

~~~~~~~~~~~~~~~~~~~~~~~PATCH
END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3. bind
#./samples/bpf/xdpsock -i eth0 -b

in this step, an ebpf binary xdpsock_kern.o is be loaded into the kernel and linked to eth0, the ebpf source code is /samples/bpf/xdpsock_kern.c you can modify it and re-compile for a different test.

4. dump xdp socket map information. #./tools/bpf/bpftool/bpftool map -p, you will see something like below.

  },{
       "id": 56,
       "type": "xskmap",
       "name": "xsks_map",
       "flags": 0,
       "bytes_key": 4,
       "bytes_value": 4,
       "max_entries": 4,
       "bytes_memlock": 4096
   }

in this case 56 is the map id and it has 4 entries

5. start testpmd

   ./build/app/testpmd -c 0xc -n 4 --vdev

eth_af_xdp,iface=enp59s0f0,xsk_map_id=56,xsk_map_key_start=2xsk_map_ key_count=2 -- -i --rxq=2 --txq=2

in this case, we reserved 2 entries (2,3) in the map, and they will be
mapped to queue 0 and queue 1.

6. unbind after test
./sample/bpf/xdpsock -i eth0 -u.

Performance
===========
Since no zero copy driver is ready yet.
So far only tested with DRV and SKB mode on i40e 25G the result show identical with kernel sample "xdpsock"

Qi Zhang (6):
net/af_xdp: new PMD driver
lib/mbuf: enable parse flags when create mempool lib/mempool: allow page size aligned mempool net/af_xdp: use mbuf mempool for buffer management net/af_xdp: enable zero copy
app/testpmd: add mempool flags parameter

 app/test-pmd/parameters.c                     |   12 +
 app/test-pmd/testpmd.c                        |   15 +-
 app/test-pmd/testpmd.h                        |    1 +
 config/common_base                            |    5 +
 config/common_linuxapp                        |    1 +
 drivers/net/Makefile                          |    1 +
 drivers/net/af_xdp/Makefile                   |   30 +
 drivers/net/af_xdp/meson.build                |    7 +
 drivers/net/af_xdp/rte_eth_af_xdp.c           | 1345
+++++++++++++++++++++++++
 drivers/net/af_xdp/rte_pmd_af_xdp_version.map |    4 +
 lib/librte_mbuf/rte_mbuf.c                    |   15 +-
 lib/librte_mbuf/rte_mbuf.h                    |    8 +-
 lib/librte_mempool/rte_mempool.c              |    3 +
 lib/librte_mempool/rte_mempool.h              |    1 +
 mk/rte.app.mk                                 |    1 +
 15 files changed, 1439 insertions(+), 10 deletions(-)  create mode 100644
drivers/net/af_xdp/Makefile  create mode 100644
drivers/net/af_xdp/meson.build  create mode 100644
drivers/net/af_xdp/rte_eth_af_xdp.c
 create mode 100644 drivers/net/af_xdp/rte_pmd_af_xdp_version.map

--
2.13.6