1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Intel Corporation.
8 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <linux/if_ether.h>
13 #include <linux/if_xdp.h>
14 #include <linux/if_link.h>
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
17 #include "af_xdp_deps.h"
20 #include <rte_ethdev.h>
21 #include <ethdev_driver.h>
22 #include <ethdev_vdev.h>
23 #include <rte_kvargs.h>
24 #include <rte_bus_vdev.h>
25 #include <rte_string_fns.h>
26 #include <rte_branch_prediction.h>
27 #include <rte_common.h>
30 #include <rte_ether.h>
31 #include <rte_lcore.h>
33 #include <rte_memory.h>
34 #include <rte_memzone.h>
35 #include <rte_mempool.h>
37 #include <rte_malloc.h>
39 #include <rte_spinlock.h>
43 #ifndef SO_PREFER_BUSY_POLL
44 #define SO_PREFER_BUSY_POLL 69
46 #ifndef SO_BUSY_POLL_BUDGET
47 #define SO_BUSY_POLL_BUDGET 70
63 RTE_LOG_REGISTER(af_xdp_logtype, pmd.net.af_xdp, NOTICE);
65 #define AF_XDP_LOG(level, fmt, args...) \
66 rte_log(RTE_LOG_ ## level, af_xdp_logtype, \
67 "%s(): " fmt, __func__, ##args)
69 #define ETH_AF_XDP_FRAME_SIZE 2048
70 #define ETH_AF_XDP_NUM_BUFFERS 4096
71 #define ETH_AF_XDP_DFLT_NUM_DESCS XSK_RING_CONS__DEFAULT_NUM_DESCS
72 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
73 #define ETH_AF_XDP_DFLT_QUEUE_COUNT 1
74 #define ETH_AF_XDP_DFLT_BUSY_BUDGET 64
75 #define ETH_AF_XDP_DFLT_BUSY_TIMEOUT 20
77 #define ETH_AF_XDP_RX_BATCH_SIZE XSK_RING_CONS__DEFAULT_NUM_DESCS
78 #define ETH_AF_XDP_TX_BATCH_SIZE XSK_RING_CONS__DEFAULT_NUM_DESCS
81 struct xsk_umem_info {
82 struct xsk_umem *umem;
83 struct rte_ring *buf_ring;
84 const struct rte_memzone *mz;
85 struct rte_mempool *mb_pool;
98 struct xsk_ring_cons rx;
99 struct xsk_umem_info *umem;
100 struct xsk_socket *xsk;
101 struct rte_mempool *mb_pool;
103 struct rx_stats stats;
105 struct xsk_ring_prod fq;
106 struct xsk_ring_cons cq;
108 struct pkt_tx_queue *pair;
109 struct pollfd fds[1];
120 struct pkt_tx_queue {
121 struct xsk_ring_prod tx;
122 struct xsk_umem_info *umem;
124 struct tx_stats stats;
126 struct pkt_rx_queue *pair;
130 struct pmd_internals {
132 char if_name[IFNAMSIZ];
136 int combined_queue_cnt;
138 char prog_path[PATH_MAX];
139 bool custom_prog_configured;
141 struct rte_ether_addr eth_addr;
143 struct pkt_rx_queue *rx_queues;
144 struct pkt_tx_queue *tx_queues;
147 #define ETH_AF_XDP_IFACE_ARG "iface"
148 #define ETH_AF_XDP_START_QUEUE_ARG "start_queue"
149 #define ETH_AF_XDP_QUEUE_COUNT_ARG "queue_count"
150 #define ETH_AF_XDP_SHARED_UMEM_ARG "shared_umem"
151 #define ETH_AF_XDP_PROG_ARG "xdp_prog"
152 #define ETH_AF_XDP_BUDGET_ARG "busy_budget"
154 static const char * const valid_arguments[] = {
155 ETH_AF_XDP_IFACE_ARG,
156 ETH_AF_XDP_START_QUEUE_ARG,
157 ETH_AF_XDP_QUEUE_COUNT_ARG,
158 ETH_AF_XDP_SHARED_UMEM_ARG,
160 ETH_AF_XDP_BUDGET_ARG,
164 static const struct rte_eth_link pmd_link = {
165 .link_speed = ETH_SPEED_NUM_10G,
166 .link_duplex = ETH_LINK_FULL_DUPLEX,
167 .link_status = ETH_LINK_DOWN,
168 .link_autoneg = ETH_LINK_AUTONEG
171 /* List which tracks PMDs to facilitate sharing UMEMs across them. */
172 struct internal_list {
173 TAILQ_ENTRY(internal_list) next;
174 struct rte_eth_dev *eth_dev;
177 TAILQ_HEAD(internal_list_head, internal_list);
178 static struct internal_list_head internal_list =
179 TAILQ_HEAD_INITIALIZER(internal_list);
181 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
183 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
185 reserve_fill_queue_zc(struct xsk_umem_info *umem, uint16_t reserve_size,
186 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
191 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
192 for (i = 0; i < reserve_size; i++)
193 rte_pktmbuf_free(bufs[i]);
194 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
198 for (i = 0; i < reserve_size; i++) {
202 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
203 addr = (uint64_t)bufs[i] - (uint64_t)umem->buffer -
204 umem->mb_pool->header_size;
208 xsk_ring_prod__submit(fq, reserve_size);
214 reserve_fill_queue_cp(struct xsk_umem_info *umem, uint16_t reserve_size,
215 struct rte_mbuf **bufs __rte_unused,
216 struct xsk_ring_prod *fq)
218 void *addrs[reserve_size];
222 if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
224 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
228 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
229 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
230 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
235 for (i = 0; i < reserve_size; i++) {
238 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
239 *fq_addr = (uint64_t)addrs[i];
242 xsk_ring_prod__submit(fq, reserve_size);
249 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size,
250 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
252 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
253 return reserve_fill_queue_zc(umem, reserve_size, bufs, fq);
255 return reserve_fill_queue_cp(umem, reserve_size, bufs, fq);
259 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
261 af_xdp_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
263 struct pkt_rx_queue *rxq = queue;
264 struct xsk_ring_cons *rx = &rxq->rx;
265 struct xsk_ring_prod *fq = &rxq->fq;
266 struct xsk_umem_info *umem = rxq->umem;
268 unsigned long rx_bytes = 0;
270 struct rte_mbuf *fq_bufs[ETH_AF_XDP_RX_BATCH_SIZE];
272 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
275 rx_syscall_handler(&rxq->fq, rxq->busy_budget, &rxq->fds[0],
280 /* allocate bufs for fill queue replenishment after rx */
281 if (rte_pktmbuf_alloc_bulk(umem->mb_pool, fq_bufs, nb_pkts)) {
283 "Failed to get enough buffers for fq.\n");
284 /* rollback cached_cons which is added by
285 * xsk_ring_cons__peek
287 rx->cached_cons -= nb_pkts;
291 for (i = 0; i < nb_pkts; i++) {
292 const struct xdp_desc *desc;
297 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
301 offset = xsk_umem__extract_offset(addr);
302 addr = xsk_umem__extract_addr(addr);
304 bufs[i] = (struct rte_mbuf *)
305 xsk_umem__get_data(umem->buffer, addr +
306 umem->mb_pool->header_size);
307 bufs[i]->data_off = offset - sizeof(struct rte_mbuf) -
308 rte_pktmbuf_priv_size(umem->mb_pool) -
309 umem->mb_pool->header_size;
311 rte_pktmbuf_pkt_len(bufs[i]) = len;
312 rte_pktmbuf_data_len(bufs[i]) = len;
316 xsk_ring_cons__release(rx, nb_pkts);
317 (void)reserve_fill_queue(umem, nb_pkts, fq_bufs, fq);
320 rxq->stats.rx_pkts += nb_pkts;
321 rxq->stats.rx_bytes += rx_bytes;
327 af_xdp_rx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
329 struct pkt_rx_queue *rxq = queue;
330 struct xsk_ring_cons *rx = &rxq->rx;
331 struct xsk_umem_info *umem = rxq->umem;
332 struct xsk_ring_prod *fq = &rxq->fq;
334 unsigned long rx_bytes = 0;
336 uint32_t free_thresh = fq->size >> 1;
337 struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
339 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
340 (void)reserve_fill_queue(umem, nb_pkts, NULL, fq);
342 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
344 #if defined(XDP_USE_NEED_WAKEUP)
345 if (xsk_ring_prod__needs_wakeup(fq))
346 (void)poll(rxq->fds, 1, 1000);
351 if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts))) {
352 /* rollback cached_cons which is added by
353 * xsk_ring_cons__peek
355 rx->cached_cons -= nb_pkts;
359 for (i = 0; i < nb_pkts; i++) {
360 const struct xdp_desc *desc;
365 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
368 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
370 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
371 rte_ring_enqueue(umem->buf_ring, (void *)addr);
372 rte_pktmbuf_pkt_len(mbufs[i]) = len;
373 rte_pktmbuf_data_len(mbufs[i]) = len;
378 xsk_ring_cons__release(rx, nb_pkts);
381 rxq->stats.rx_pkts += nb_pkts;
382 rxq->stats.rx_bytes += rx_bytes;
389 af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
391 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
392 return af_xdp_rx_zc(queue, bufs, nb_pkts);
394 return af_xdp_rx_cp(queue, bufs, nb_pkts);
399 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
403 if (likely(nb_pkts <= ETH_AF_XDP_RX_BATCH_SIZE))
404 return af_xdp_rx(queue, bufs, nb_pkts);
406 /* Split larger batch into smaller batches of size
407 * ETH_AF_XDP_RX_BATCH_SIZE or less.
413 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
414 ret = af_xdp_rx(queue, &bufs[nb_rx], n);
415 nb_rx = (uint16_t)(nb_rx + ret);
416 nb_pkts = (uint16_t)(nb_pkts - ret);
425 pull_umem_cq(struct xsk_umem_info *umem, int size, struct xsk_ring_cons *cq)
430 n = xsk_ring_cons__peek(cq, size, &idx_cq);
432 for (i = 0; i < n; i++) {
434 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
435 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
436 addr = xsk_umem__extract_addr(addr);
437 rte_pktmbuf_free((struct rte_mbuf *)
438 xsk_umem__get_data(umem->buffer,
439 addr + umem->mb_pool->header_size));
441 rte_ring_enqueue(umem->buf_ring, (void *)addr);
445 xsk_ring_cons__release(cq, n);
449 kick_tx(struct pkt_tx_queue *txq, struct xsk_ring_cons *cq)
451 struct xsk_umem_info *umem = txq->umem;
453 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
455 if (tx_syscall_needed(&txq->tx))
456 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
457 0, MSG_DONTWAIT) < 0) {
458 /* some thing unexpected */
459 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
462 /* pull from completion queue to leave more space */
465 XSK_RING_CONS__DEFAULT_NUM_DESCS,
470 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
472 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
474 struct pkt_tx_queue *txq = queue;
475 struct xsk_umem_info *umem = txq->umem;
476 struct rte_mbuf *mbuf;
477 unsigned long tx_bytes = 0;
481 struct xdp_desc *desc;
482 uint64_t addr, offset;
483 struct xsk_ring_cons *cq = &txq->pair->cq;
484 uint32_t free_thresh = cq->size >> 1;
486 if (xsk_cons_nb_avail(cq, free_thresh) >= free_thresh)
487 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
489 for (i = 0; i < nb_pkts; i++) {
492 if (mbuf->pool == umem->mb_pool) {
493 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
495 if (!xsk_ring_prod__reserve(&txq->tx, 1,
499 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
500 desc->len = mbuf->pkt_len;
501 addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
502 umem->mb_pool->header_size;
503 offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
505 umem->mb_pool->header_size;
506 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
507 desc->addr = addr | offset;
510 struct rte_mbuf *local_mbuf =
511 rte_pktmbuf_alloc(umem->mb_pool);
514 if (local_mbuf == NULL)
517 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
518 rte_pktmbuf_free(local_mbuf);
523 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
524 desc->len = mbuf->pkt_len;
526 addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
527 umem->mb_pool->header_size;
528 offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
529 (uint64_t)local_mbuf +
530 umem->mb_pool->header_size;
531 pkt = xsk_umem__get_data(umem->buffer, addr + offset);
532 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
533 desc->addr = addr | offset;
534 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
536 rte_pktmbuf_free(mbuf);
540 tx_bytes += mbuf->pkt_len;
546 xsk_ring_prod__submit(&txq->tx, count);
548 txq->stats.tx_pkts += count;
549 txq->stats.tx_bytes += tx_bytes;
550 txq->stats.tx_dropped += nb_pkts - count;
556 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
558 struct pkt_tx_queue *txq = queue;
559 struct xsk_umem_info *umem = txq->umem;
560 struct rte_mbuf *mbuf;
561 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
562 unsigned long tx_bytes = 0;
565 struct xsk_ring_cons *cq = &txq->pair->cq;
567 pull_umem_cq(umem, nb_pkts, cq);
569 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
574 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
576 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
580 for (i = 0; i < nb_pkts; i++) {
581 struct xdp_desc *desc;
584 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
586 desc->len = mbuf->pkt_len;
588 desc->addr = (uint64_t)addrs[i];
589 pkt = xsk_umem__get_data(umem->mz->addr,
591 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
592 tx_bytes += mbuf->pkt_len;
593 rte_pktmbuf_free(mbuf);
596 xsk_ring_prod__submit(&txq->tx, nb_pkts);
600 txq->stats.tx_pkts += nb_pkts;
601 txq->stats.tx_bytes += tx_bytes;
607 af_xdp_tx_cp_batch(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
611 if (likely(nb_pkts <= ETH_AF_XDP_TX_BATCH_SIZE))
612 return af_xdp_tx_cp(queue, bufs, nb_pkts);
618 /* Split larger batch into smaller batches of size
619 * ETH_AF_XDP_TX_BATCH_SIZE or less.
621 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
622 ret = af_xdp_tx_cp(queue, &bufs[nb_tx], n);
623 nb_tx = (uint16_t)(nb_tx + ret);
624 nb_pkts = (uint16_t)(nb_pkts - ret);
634 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
636 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
637 return af_xdp_tx_zc(queue, bufs, nb_pkts);
639 return af_xdp_tx_cp_batch(queue, bufs, nb_pkts);
644 eth_dev_start(struct rte_eth_dev *dev)
646 dev->data->dev_link.link_status = ETH_LINK_UP;
651 /* This function gets called when the current port gets stopped. */
653 eth_dev_stop(struct rte_eth_dev *dev)
655 dev->data->dev_link.link_status = ETH_LINK_DOWN;
659 /* Find ethdev in list */
660 static inline struct internal_list *
661 find_internal_resource(struct pmd_internals *port_int)
664 struct internal_list *list = NULL;
666 if (port_int == NULL)
669 pthread_mutex_lock(&internal_list_lock);
671 TAILQ_FOREACH(list, &internal_list, next) {
672 struct pmd_internals *list_int =
673 list->eth_dev->data->dev_private;
674 if (list_int == port_int) {
680 pthread_mutex_unlock(&internal_list_lock);
688 /* Check if the netdev,qid context already exists */
690 ctx_exists(struct pkt_rx_queue *rxq, const char *ifname,
691 struct pkt_rx_queue *list_rxq, const char *list_ifname)
695 if (rxq->xsk_queue_idx == list_rxq->xsk_queue_idx &&
696 !strncmp(ifname, list_ifname, IFNAMSIZ)) {
697 AF_XDP_LOG(ERR, "ctx %s,%i already exists, cannot share umem\n",
698 ifname, rxq->xsk_queue_idx);
705 /* Get a pointer to an existing UMEM which overlays the rxq's mb_pool */
707 get_shared_umem(struct pkt_rx_queue *rxq, const char *ifname,
708 struct xsk_umem_info **umem)
710 struct internal_list *list;
711 struct pmd_internals *internals;
713 struct rte_mempool *mb_pool = rxq->mb_pool;
718 pthread_mutex_lock(&internal_list_lock);
720 TAILQ_FOREACH(list, &internal_list, next) {
721 internals = list->eth_dev->data->dev_private;
722 for (i = 0; i < internals->queue_cnt; i++) {
723 struct pkt_rx_queue *list_rxq =
724 &internals->rx_queues[i];
727 if (mb_pool == internals->rx_queues[i].mb_pool) {
728 if (ctx_exists(rxq, ifname, list_rxq,
729 internals->if_name)) {
734 &internals->rx_queues[i].umem->refcnt,
736 *umem = internals->rx_queues[i].umem;
744 pthread_mutex_unlock(&internal_list_lock);
750 eth_dev_configure(struct rte_eth_dev *dev)
752 struct pmd_internals *internal = dev->data->dev_private;
754 /* rx/tx must be paired */
755 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
758 if (internal->shared_umem) {
759 struct internal_list *list = NULL;
760 const char *name = dev->device->name;
762 /* Ensure PMD is not already inserted into the list */
763 list = find_internal_resource(internal);
767 list = rte_zmalloc_socket(name, sizeof(*list), 0,
768 dev->device->numa_node);
773 pthread_mutex_lock(&internal_list_lock);
774 TAILQ_INSERT_TAIL(&internal_list, list, next);
775 pthread_mutex_unlock(&internal_list_lock);
782 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
784 struct pmd_internals *internals = dev->data->dev_private;
786 dev_info->if_index = internals->if_index;
787 dev_info->max_mac_addrs = 1;
788 dev_info->max_rx_pktlen = ETH_FRAME_LEN;
789 dev_info->max_rx_queues = internals->queue_cnt;
790 dev_info->max_tx_queues = internals->queue_cnt;
792 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
793 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
794 dev_info->max_mtu = getpagesize() -
795 sizeof(struct rte_mempool_objhdr) -
796 sizeof(struct rte_mbuf) -
797 RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
799 dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
802 dev_info->default_rxportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
803 dev_info->default_txportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
804 dev_info->default_rxportconf.nb_queues = 1;
805 dev_info->default_txportconf.nb_queues = 1;
806 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
807 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
813 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
815 struct pmd_internals *internals = dev->data->dev_private;
816 struct xdp_statistics xdp_stats;
817 struct pkt_rx_queue *rxq;
818 struct pkt_tx_queue *txq;
822 for (i = 0; i < dev->data->nb_rx_queues; i++) {
823 optlen = sizeof(struct xdp_statistics);
824 rxq = &internals->rx_queues[i];
826 stats->q_ipackets[i] = rxq->stats.rx_pkts;
827 stats->q_ibytes[i] = rxq->stats.rx_bytes;
829 stats->q_opackets[i] = txq->stats.tx_pkts;
830 stats->q_obytes[i] = txq->stats.tx_bytes;
832 stats->ipackets += stats->q_ipackets[i];
833 stats->ibytes += stats->q_ibytes[i];
834 stats->imissed += rxq->stats.rx_dropped;
835 stats->oerrors += txq->stats.tx_dropped;
836 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
837 XDP_STATISTICS, &xdp_stats, &optlen);
839 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
842 stats->imissed += xdp_stats.rx_dropped;
844 stats->opackets += stats->q_opackets[i];
845 stats->obytes += stats->q_obytes[i];
852 eth_stats_reset(struct rte_eth_dev *dev)
854 struct pmd_internals *internals = dev->data->dev_private;
857 for (i = 0; i < internals->queue_cnt; i++) {
858 memset(&internals->rx_queues[i].stats, 0,
859 sizeof(struct rx_stats));
860 memset(&internals->tx_queues[i].stats, 0,
861 sizeof(struct tx_stats));
868 remove_xdp_program(struct pmd_internals *internals)
870 uint32_t curr_prog_id = 0;
872 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
873 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
874 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
877 bpf_set_link_xdp_fd(internals->if_index, -1,
878 XDP_FLAGS_UPDATE_IF_NOEXIST);
882 xdp_umem_destroy(struct xsk_umem_info *umem)
884 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
885 umem->mb_pool = NULL;
887 rte_memzone_free(umem->mz);
890 rte_ring_free(umem->buf_ring);
891 umem->buf_ring = NULL;
898 eth_dev_close(struct rte_eth_dev *dev)
900 struct pmd_internals *internals = dev->data->dev_private;
901 struct pkt_rx_queue *rxq;
904 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
907 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
910 for (i = 0; i < internals->queue_cnt; i++) {
911 rxq = &internals->rx_queues[i];
912 if (rxq->umem == NULL)
914 xsk_socket__delete(rxq->xsk);
916 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE)
918 (void)xsk_umem__delete(rxq->umem->umem);
919 xdp_umem_destroy(rxq->umem);
922 /* free pkt_tx_queue */
928 * MAC is not allocated dynamically, setting it to NULL would prevent
929 * from releasing it in rte_eth_dev_release_port.
931 dev->data->mac_addrs = NULL;
933 remove_xdp_program(internals);
935 if (internals->shared_umem) {
936 struct internal_list *list;
938 /* Remove ethdev from list used to track and share UMEMs */
939 list = find_internal_resource(internals);
941 pthread_mutex_lock(&internal_list_lock);
942 TAILQ_REMOVE(&internal_list, list, next);
943 pthread_mutex_unlock(&internal_list_lock);
952 eth_queue_release(void *q __rte_unused)
957 eth_link_update(struct rte_eth_dev *dev __rte_unused,
958 int wait_to_complete __rte_unused)
963 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
964 static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
966 struct rte_mempool_memhdr *memhdr;
967 uintptr_t memhdr_addr, aligned_addr;
969 memhdr = STAILQ_FIRST(&mp->mem_list);
970 memhdr_addr = (uintptr_t)memhdr->addr;
971 aligned_addr = memhdr_addr & ~(getpagesize() - 1);
972 *align = memhdr_addr - aligned_addr;
978 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
979 struct pkt_rx_queue *rxq)
981 struct xsk_umem_info *umem = NULL;
983 struct xsk_umem_config usr_config = {
984 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
985 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
986 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
987 void *base_addr = NULL;
988 struct rte_mempool *mb_pool = rxq->mb_pool;
989 uint64_t umem_size, align = 0;
991 if (internals->shared_umem) {
992 if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
996 __atomic_load_n(&umem->refcnt, __ATOMIC_ACQUIRE) <
998 AF_XDP_LOG(INFO, "%s,qid%i sharing UMEM\n",
999 internals->if_name, rxq->xsk_queue_idx);
1000 __atomic_fetch_add(&umem->refcnt, 1, __ATOMIC_ACQUIRE);
1005 usr_config.frame_size =
1006 rte_mempool_calc_obj_size(mb_pool->elt_size,
1007 mb_pool->flags, NULL);
1008 usr_config.frame_headroom = mb_pool->header_size +
1009 sizeof(struct rte_mbuf) +
1010 rte_pktmbuf_priv_size(mb_pool) +
1011 RTE_PKTMBUF_HEADROOM;
1013 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0,
1016 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1020 umem->mb_pool = mb_pool;
1021 base_addr = (void *)get_base_addr(mb_pool, &align);
1022 umem_size = (uint64_t)mb_pool->populated_size *
1023 (uint64_t)usr_config.frame_size +
1026 ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
1027 &rxq->fq, &rxq->cq, &usr_config);
1029 AF_XDP_LOG(ERR, "Failed to create umem");
1032 umem->buffer = base_addr;
1034 if (internals->shared_umem) {
1035 umem->max_xsks = mb_pool->populated_size /
1036 ETH_AF_XDP_NUM_BUFFERS;
1037 AF_XDP_LOG(INFO, "Max xsks for UMEM %s: %u\n",
1038 mb_pool->name, umem->max_xsks);
1041 __atomic_store_n(&umem->refcnt, 1, __ATOMIC_RELEASE);
1046 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1047 struct pkt_rx_queue *rxq)
1049 struct xsk_umem_info *umem;
1050 const struct rte_memzone *mz;
1051 struct xsk_umem_config usr_config = {
1052 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1053 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1054 .frame_size = ETH_AF_XDP_FRAME_SIZE,
1055 .frame_headroom = 0 };
1056 char ring_name[RTE_RING_NAMESIZE];
1057 char mz_name[RTE_MEMZONE_NAMESIZE];
1061 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
1063 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1067 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
1068 internals->if_name, rxq->xsk_queue_idx);
1069 umem->buf_ring = rte_ring_create(ring_name,
1070 ETH_AF_XDP_NUM_BUFFERS,
1073 if (umem->buf_ring == NULL) {
1074 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
1078 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
1079 rte_ring_enqueue(umem->buf_ring,
1080 (void *)(i * ETH_AF_XDP_FRAME_SIZE));
1082 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1083 internals->if_name, rxq->xsk_queue_idx);
1084 mz = rte_memzone_reserve_aligned(mz_name,
1085 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1086 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1089 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1093 ret = xsk_umem__create(&umem->umem, mz->addr,
1094 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1099 AF_XDP_LOG(ERR, "Failed to create umem");
1108 xdp_umem_destroy(umem);
1113 load_custom_xdp_prog(const char *prog_path, int if_index)
1115 int ret, prog_fd = -1;
1116 struct bpf_object *obj;
1117 struct bpf_map *map;
1119 ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
1121 AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
1126 * The loaded program must provision for a map of xsks, such that some
1127 * traffic can be redirected to userspace. When the xsk is created,
1128 * libbpf inserts it into the map.
1130 map = bpf_object__find_map_by_name(obj, "xsks_map");
1132 AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
1136 /* Link the program with the given network device */
1137 ret = bpf_set_link_xdp_fd(if_index, prog_fd,
1138 XDP_FLAGS_UPDATE_IF_NOEXIST);
1140 AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
1145 AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
1146 prog_path, prog_fd);
1151 /* Detect support for busy polling through setsockopt(). */
1153 configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
1156 int fd = xsk_socket__fd(rxq->xsk);
1159 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1160 (void *)&sock_opt, sizeof(sock_opt));
1162 AF_XDP_LOG(DEBUG, "Failed to set SO_PREFER_BUSY_POLL\n");
1166 sock_opt = ETH_AF_XDP_DFLT_BUSY_TIMEOUT;
1167 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1170 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL\n");
1174 sock_opt = rxq->busy_budget;
1175 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1176 (void *)&sock_opt, sizeof(sock_opt));
1178 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL_BUDGET\n");
1180 AF_XDP_LOG(INFO, "Busy polling budget set to: %u\n",
1185 /* setsockopt failure - attempt to restore xsk to default state and
1186 * proceed without busy polling support.
1189 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1192 AF_XDP_LOG(ERR, "Failed to unset SO_BUSY_POLL\n");
1198 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1199 (void *)&sock_opt, sizeof(sock_opt));
1201 AF_XDP_LOG(ERR, "Failed to unset SO_PREFER_BUSY_POLL\n");
1206 rxq->busy_budget = 0;
1211 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1214 struct xsk_socket_config cfg;
1215 struct pkt_tx_queue *txq = rxq->pair;
1217 int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1218 struct rte_mbuf *fq_bufs[reserve_size];
1220 rxq->umem = xdp_umem_configure(internals, rxq);
1221 if (rxq->umem == NULL)
1223 txq->umem = rxq->umem;
1225 cfg.rx_size = ring_size;
1226 cfg.tx_size = ring_size;
1227 cfg.libbpf_flags = 0;
1228 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1231 #if defined(XDP_USE_NEED_WAKEUP)
1232 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1235 if (strnlen(internals->prog_path, PATH_MAX) &&
1236 !internals->custom_prog_configured) {
1237 ret = load_custom_xdp_prog(internals->prog_path,
1238 internals->if_index);
1240 AF_XDP_LOG(ERR, "Failed to load custom XDP program %s\n",
1241 internals->prog_path);
1244 internals->custom_prog_configured = 1;
1247 if (internals->shared_umem)
1248 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1249 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1250 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1252 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1253 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1257 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1261 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1262 ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size);
1264 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1269 if (rxq->busy_budget) {
1270 ret = configure_preferred_busy_poll(rxq);
1272 AF_XDP_LOG(ERR, "Failed configure busy polling.\n");
1277 ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1279 xsk_socket__delete(rxq->xsk);
1280 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1287 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1288 xdp_umem_destroy(rxq->umem);
1294 eth_rx_queue_setup(struct rte_eth_dev *dev,
1295 uint16_t rx_queue_id,
1296 uint16_t nb_rx_desc,
1297 unsigned int socket_id __rte_unused,
1298 const struct rte_eth_rxconf *rx_conf __rte_unused,
1299 struct rte_mempool *mb_pool)
1301 struct pmd_internals *internals = dev->data->dev_private;
1302 struct pkt_rx_queue *rxq;
1305 rxq = &internals->rx_queues[rx_queue_id];
1307 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1308 rx_queue_id, rxq->xsk_queue_idx);
1310 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1311 uint32_t buf_size, data_size;
1313 /* Now get the space available for data in the mbuf */
1314 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1315 RTE_PKTMBUF_HEADROOM;
1316 data_size = ETH_AF_XDP_FRAME_SIZE;
1318 if (data_size > buf_size) {
1319 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1320 dev->device->name, data_size, buf_size);
1326 rxq->mb_pool = mb_pool;
1328 if (xsk_configure(internals, rxq, nb_rx_desc)) {
1329 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1334 if (!rxq->busy_budget)
1335 AF_XDP_LOG(DEBUG, "Preferred busy polling not enabled\n");
1337 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1338 rxq->fds[0].events = POLLIN;
1340 dev->data->rx_queues[rx_queue_id] = rxq;
1348 eth_tx_queue_setup(struct rte_eth_dev *dev,
1349 uint16_t tx_queue_id,
1350 uint16_t nb_tx_desc __rte_unused,
1351 unsigned int socket_id __rte_unused,
1352 const struct rte_eth_txconf *tx_conf __rte_unused)
1354 struct pmd_internals *internals = dev->data->dev_private;
1355 struct pkt_tx_queue *txq;
1357 txq = &internals->tx_queues[tx_queue_id];
1359 dev->data->tx_queues[tx_queue_id] = txq;
1364 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1366 struct pmd_internals *internals = dev->data->dev_private;
1367 struct ifreq ifr = { .ifr_mtu = mtu };
1371 s = socket(PF_INET, SOCK_DGRAM, 0);
1375 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1376 ret = ioctl(s, SIOCSIFMTU, &ifr);
1379 return (ret < 0) ? -errno : 0;
1383 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1389 s = socket(PF_INET, SOCK_DGRAM, 0);
1393 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1394 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1398 ifr.ifr_flags &= mask;
1399 ifr.ifr_flags |= flags;
1400 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1410 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1412 struct pmd_internals *internals = dev->data->dev_private;
1414 return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1418 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1420 struct pmd_internals *internals = dev->data->dev_private;
1422 return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1425 static const struct eth_dev_ops ops = {
1426 .dev_start = eth_dev_start,
1427 .dev_stop = eth_dev_stop,
1428 .dev_close = eth_dev_close,
1429 .dev_configure = eth_dev_configure,
1430 .dev_infos_get = eth_dev_info,
1431 .mtu_set = eth_dev_mtu_set,
1432 .promiscuous_enable = eth_dev_promiscuous_enable,
1433 .promiscuous_disable = eth_dev_promiscuous_disable,
1434 .rx_queue_setup = eth_rx_queue_setup,
1435 .tx_queue_setup = eth_tx_queue_setup,
1436 .rx_queue_release = eth_queue_release,
1437 .tx_queue_release = eth_queue_release,
1438 .link_update = eth_link_update,
1439 .stats_get = eth_stats_get,
1440 .stats_reset = eth_stats_reset,
1443 /** parse busy_budget argument */
1445 parse_budget_arg(const char *key __rte_unused,
1446 const char *value, void *extra_args)
1448 int *i = (int *)extra_args;
1451 *i = strtol(value, &end, 10);
1452 if (*i < 0 || *i > UINT16_MAX) {
1453 AF_XDP_LOG(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u\n",
1461 /** parse integer from integer argument */
1463 parse_integer_arg(const char *key __rte_unused,
1464 const char *value, void *extra_args)
1466 int *i = (int *)extra_args;
1469 *i = strtol(value, &end, 10);
1471 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1478 /** parse name argument */
1480 parse_name_arg(const char *key __rte_unused,
1481 const char *value, void *extra_args)
1483 char *name = extra_args;
1485 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1486 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1491 strlcpy(name, value, IFNAMSIZ);
1496 /** parse xdp prog argument */
1498 parse_prog_arg(const char *key __rte_unused,
1499 const char *value, void *extra_args)
1501 char *path = extra_args;
1503 if (strnlen(value, PATH_MAX) == PATH_MAX) {
1504 AF_XDP_LOG(ERR, "Invalid path %s, should be less than %u bytes.\n",
1509 if (access(value, F_OK) != 0) {
1510 AF_XDP_LOG(ERR, "Error accessing %s: %s\n",
1511 value, strerror(errno));
1515 strlcpy(path, value, PATH_MAX);
1521 xdp_get_channels_info(const char *if_name, int *max_queues,
1522 int *combined_queues)
1524 struct ethtool_channels channels;
1528 fd = socket(AF_INET, SOCK_DGRAM, 0);
1532 channels.cmd = ETHTOOL_GCHANNELS;
1533 ifr.ifr_data = (void *)&channels;
1534 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1535 ret = ioctl(fd, SIOCETHTOOL, &ifr);
1537 if (errno == EOPNOTSUPP) {
1545 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1546 /* If the device says it has no channels, then all traffic
1547 * is sent to a single stream, so max queues = 1.
1550 *combined_queues = 1;
1552 *max_queues = channels.max_combined;
1553 *combined_queues = channels.combined_count;
1562 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1563 int *queue_cnt, int *shared_umem, char *prog_path,
1568 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1569 &parse_name_arg, if_name);
1573 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1574 &parse_integer_arg, start_queue);
1578 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1579 &parse_integer_arg, queue_cnt);
1580 if (ret < 0 || *queue_cnt <= 0) {
1585 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1586 &parse_integer_arg, shared_umem);
1590 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
1591 &parse_prog_arg, prog_path);
1595 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
1596 &parse_budget_arg, busy_budget);
1601 rte_kvargs_free(kvlist);
1606 get_iface_info(const char *if_name,
1607 struct rte_ether_addr *eth_addr,
1611 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1616 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1617 if (ioctl(sock, SIOCGIFINDEX, &ifr))
1620 *if_index = ifr.ifr_ifindex;
1622 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1625 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1635 static struct rte_eth_dev *
1636 init_internals(struct rte_vdev_device *dev, const char *if_name,
1637 int start_queue_idx, int queue_cnt, int shared_umem,
1638 const char *prog_path, int busy_budget)
1640 const char *name = rte_vdev_device_name(dev);
1641 const unsigned int numa_node = dev->device.numa_node;
1642 struct pmd_internals *internals;
1643 struct rte_eth_dev *eth_dev;
1647 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1648 if (internals == NULL)
1651 internals->start_queue_idx = start_queue_idx;
1652 internals->queue_cnt = queue_cnt;
1653 strlcpy(internals->if_name, if_name, IFNAMSIZ);
1654 strlcpy(internals->prog_path, prog_path, PATH_MAX);
1655 internals->custom_prog_configured = 0;
1657 #ifndef ETH_AF_XDP_SHARED_UMEM
1659 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1660 "Check kernel and libbpf version\n");
1661 goto err_free_internals;
1664 internals->shared_umem = shared_umem;
1666 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1667 &internals->combined_queue_cnt)) {
1668 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1670 goto err_free_internals;
1673 if (queue_cnt > internals->combined_queue_cnt) {
1674 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1675 queue_cnt, internals->combined_queue_cnt);
1676 goto err_free_internals;
1679 internals->rx_queues = rte_zmalloc_socket(NULL,
1680 sizeof(struct pkt_rx_queue) * queue_cnt,
1682 if (internals->rx_queues == NULL) {
1683 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1684 goto err_free_internals;
1687 internals->tx_queues = rte_zmalloc_socket(NULL,
1688 sizeof(struct pkt_tx_queue) * queue_cnt,
1690 if (internals->tx_queues == NULL) {
1691 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1694 for (i = 0; i < queue_cnt; i++) {
1695 internals->tx_queues[i].pair = &internals->rx_queues[i];
1696 internals->rx_queues[i].pair = &internals->tx_queues[i];
1697 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1698 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1699 internals->rx_queues[i].busy_budget = busy_budget;
1702 ret = get_iface_info(if_name, &internals->eth_addr,
1703 &internals->if_index);
1707 eth_dev = rte_eth_vdev_allocate(dev, 0);
1708 if (eth_dev == NULL)
1711 eth_dev->data->dev_private = internals;
1712 eth_dev->data->dev_link = pmd_link;
1713 eth_dev->data->mac_addrs = &internals->eth_addr;
1714 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1715 eth_dev->dev_ops = &ops;
1716 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1717 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1719 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1720 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1726 rte_free(internals->tx_queues);
1728 rte_free(internals->rx_queues);
1730 rte_free(internals);
1735 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1737 struct rte_kvargs *kvlist;
1738 char if_name[IFNAMSIZ] = {'\0'};
1739 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1740 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1741 int shared_umem = 0;
1742 char prog_path[PATH_MAX] = {'\0'};
1743 int busy_budget = -1;
1744 struct rte_eth_dev *eth_dev = NULL;
1747 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1748 rte_vdev_device_name(dev));
1750 name = rte_vdev_device_name(dev);
1751 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1752 strlen(rte_vdev_device_args(dev)) == 0) {
1753 eth_dev = rte_eth_dev_attach_secondary(name);
1754 if (eth_dev == NULL) {
1755 AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1758 eth_dev->dev_ops = &ops;
1759 rte_eth_dev_probing_finish(eth_dev);
1763 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1764 if (kvlist == NULL) {
1765 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1769 if (dev->device.numa_node == SOCKET_ID_ANY)
1770 dev->device.numa_node = rte_socket_id();
1772 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1773 &xsk_queue_cnt, &shared_umem, prog_path,
1774 &busy_budget) < 0) {
1775 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1779 if (strlen(if_name) == 0) {
1780 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1784 busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
1787 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1788 xsk_queue_cnt, shared_umem, prog_path,
1790 if (eth_dev == NULL) {
1791 AF_XDP_LOG(ERR, "Failed to init internals\n");
1795 rte_eth_dev_probing_finish(eth_dev);
1801 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1803 struct rte_eth_dev *eth_dev = NULL;
1805 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1811 /* find the ethdev entry */
1812 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1813 if (eth_dev == NULL)
1816 eth_dev_close(eth_dev);
1817 rte_eth_dev_release_port(eth_dev);
1823 static struct rte_vdev_driver pmd_af_xdp_drv = {
1824 .probe = rte_pmd_af_xdp_probe,
1825 .remove = rte_pmd_af_xdp_remove,
1828 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1829 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1831 "start_queue=<int> "
1832 "queue_count=<int> "
1833 "shared_umem=<int> "
1834 "xdp_prog=<string> "
1835 "busy_budget=<int>");