1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Intel Corporation.
9 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_xdp.h>
15 #include <linux/if_link.h>
16 #include <linux/ethtool.h>
17 #include <linux/sockios.h>
18 #include "af_xdp_deps.h"
21 #include <rte_ethdev.h>
22 #include <ethdev_driver.h>
23 #include <ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_bus_vdev.h>
26 #include <rte_string_fns.h>
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
31 #include <rte_ether.h>
32 #include <rte_lcore.h>
34 #include <rte_memory.h>
35 #include <rte_memzone.h>
36 #include <rte_mempool.h>
38 #include <rte_malloc.h>
40 #include <rte_spinlock.h>
44 #ifndef SO_PREFER_BUSY_POLL
45 #define SO_PREFER_BUSY_POLL 69
47 #ifndef SO_BUSY_POLL_BUDGET
48 #define SO_BUSY_POLL_BUDGET 70
64 RTE_LOG_REGISTER(af_xdp_logtype, pmd.net.af_xdp, NOTICE);
66 #define AF_XDP_LOG(level, fmt, args...) \
67 rte_log(RTE_LOG_ ## level, af_xdp_logtype, \
68 "%s(): " fmt, __func__, ##args)
70 #define ETH_AF_XDP_FRAME_SIZE 2048
71 #define ETH_AF_XDP_NUM_BUFFERS 4096
72 #define ETH_AF_XDP_DFLT_NUM_DESCS XSK_RING_CONS__DEFAULT_NUM_DESCS
73 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
74 #define ETH_AF_XDP_DFLT_QUEUE_COUNT 1
75 #define ETH_AF_XDP_DFLT_BUSY_BUDGET 64
76 #define ETH_AF_XDP_DFLT_BUSY_TIMEOUT 20
78 #define ETH_AF_XDP_RX_BATCH_SIZE XSK_RING_CONS__DEFAULT_NUM_DESCS
79 #define ETH_AF_XDP_TX_BATCH_SIZE XSK_RING_CONS__DEFAULT_NUM_DESCS
82 struct xsk_umem_info {
83 struct xsk_umem *umem;
84 struct rte_ring *buf_ring;
85 const struct rte_memzone *mz;
86 struct rte_mempool *mb_pool;
99 struct xsk_ring_cons rx;
100 struct xsk_umem_info *umem;
101 struct xsk_socket *xsk;
102 struct rte_mempool *mb_pool;
104 struct rx_stats stats;
106 struct xsk_ring_prod fq;
107 struct xsk_ring_cons cq;
109 struct pkt_tx_queue *pair;
110 struct pollfd fds[1];
121 struct pkt_tx_queue {
122 struct xsk_ring_prod tx;
123 struct xsk_umem_info *umem;
125 struct tx_stats stats;
127 struct pkt_rx_queue *pair;
131 struct pmd_internals {
133 char if_name[IFNAMSIZ];
137 int combined_queue_cnt;
139 char prog_path[PATH_MAX];
140 bool custom_prog_configured;
142 struct rte_ether_addr eth_addr;
144 struct pkt_rx_queue *rx_queues;
145 struct pkt_tx_queue *tx_queues;
148 #define ETH_AF_XDP_IFACE_ARG "iface"
149 #define ETH_AF_XDP_START_QUEUE_ARG "start_queue"
150 #define ETH_AF_XDP_QUEUE_COUNT_ARG "queue_count"
151 #define ETH_AF_XDP_SHARED_UMEM_ARG "shared_umem"
152 #define ETH_AF_XDP_PROG_ARG "xdp_prog"
153 #define ETH_AF_XDP_BUDGET_ARG "busy_budget"
155 static const char * const valid_arguments[] = {
156 ETH_AF_XDP_IFACE_ARG,
157 ETH_AF_XDP_START_QUEUE_ARG,
158 ETH_AF_XDP_QUEUE_COUNT_ARG,
159 ETH_AF_XDP_SHARED_UMEM_ARG,
161 ETH_AF_XDP_BUDGET_ARG,
165 static const struct rte_eth_link pmd_link = {
166 .link_speed = ETH_SPEED_NUM_10G,
167 .link_duplex = ETH_LINK_FULL_DUPLEX,
168 .link_status = ETH_LINK_DOWN,
169 .link_autoneg = ETH_LINK_AUTONEG
172 /* List which tracks PMDs to facilitate sharing UMEMs across them. */
173 struct internal_list {
174 TAILQ_ENTRY(internal_list) next;
175 struct rte_eth_dev *eth_dev;
178 TAILQ_HEAD(internal_list_head, internal_list);
179 static struct internal_list_head internal_list =
180 TAILQ_HEAD_INITIALIZER(internal_list);
182 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
184 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
186 reserve_fill_queue_zc(struct xsk_umem_info *umem, uint16_t reserve_size,
187 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
192 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
193 for (i = 0; i < reserve_size; i++)
194 rte_pktmbuf_free(bufs[i]);
195 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
199 for (i = 0; i < reserve_size; i++) {
203 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
204 addr = (uint64_t)bufs[i] - (uint64_t)umem->buffer -
205 umem->mb_pool->header_size;
209 xsk_ring_prod__submit(fq, reserve_size);
215 reserve_fill_queue_cp(struct xsk_umem_info *umem, uint16_t reserve_size,
216 struct rte_mbuf **bufs __rte_unused,
217 struct xsk_ring_prod *fq)
219 void *addrs[reserve_size];
223 if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
225 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
229 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
230 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
231 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
236 for (i = 0; i < reserve_size; i++) {
239 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
240 *fq_addr = (uint64_t)addrs[i];
243 xsk_ring_prod__submit(fq, reserve_size);
250 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size,
251 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
253 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
254 return reserve_fill_queue_zc(umem, reserve_size, bufs, fq);
256 return reserve_fill_queue_cp(umem, reserve_size, bufs, fq);
260 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
262 af_xdp_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
264 struct pkt_rx_queue *rxq = queue;
265 struct xsk_ring_cons *rx = &rxq->rx;
266 struct xsk_ring_prod *fq = &rxq->fq;
267 struct xsk_umem_info *umem = rxq->umem;
269 unsigned long rx_bytes = 0;
271 struct rte_mbuf *fq_bufs[ETH_AF_XDP_RX_BATCH_SIZE];
273 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
276 if (rx_syscall_needed(&rxq->fq, rxq->busy_budget))
277 (void)recvfrom(xsk_socket__fd(rxq->xsk), NULL, 0,
278 MSG_DONTWAIT, NULL, NULL);
283 /* allocate bufs for fill queue replenishment after rx */
284 if (rte_pktmbuf_alloc_bulk(umem->mb_pool, fq_bufs, nb_pkts)) {
286 "Failed to get enough buffers for fq.\n");
287 /* rollback cached_cons which is added by
288 * xsk_ring_cons__peek
290 rx->cached_cons -= nb_pkts;
294 for (i = 0; i < nb_pkts; i++) {
295 const struct xdp_desc *desc;
300 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
304 offset = xsk_umem__extract_offset(addr);
305 addr = xsk_umem__extract_addr(addr);
307 bufs[i] = (struct rte_mbuf *)
308 xsk_umem__get_data(umem->buffer, addr +
309 umem->mb_pool->header_size);
310 bufs[i]->data_off = offset - sizeof(struct rte_mbuf) -
311 rte_pktmbuf_priv_size(umem->mb_pool) -
312 umem->mb_pool->header_size;
314 rte_pktmbuf_pkt_len(bufs[i]) = len;
315 rte_pktmbuf_data_len(bufs[i]) = len;
319 xsk_ring_cons__release(rx, nb_pkts);
320 (void)reserve_fill_queue(umem, nb_pkts, fq_bufs, fq);
323 rxq->stats.rx_pkts += nb_pkts;
324 rxq->stats.rx_bytes += rx_bytes;
330 af_xdp_rx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
332 struct pkt_rx_queue *rxq = queue;
333 struct xsk_ring_cons *rx = &rxq->rx;
334 struct xsk_umem_info *umem = rxq->umem;
335 struct xsk_ring_prod *fq = &rxq->fq;
337 unsigned long rx_bytes = 0;
339 uint32_t free_thresh = fq->size >> 1;
340 struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
342 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
343 (void)reserve_fill_queue(umem, nb_pkts, NULL, fq);
345 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
347 #if defined(XDP_USE_NEED_WAKEUP)
348 if (xsk_ring_prod__needs_wakeup(fq))
349 (void)recvfrom(xsk_socket__fd(rxq->xsk), NULL, 0,
350 MSG_DONTWAIT, NULL, NULL);
355 if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts))) {
356 /* rollback cached_cons which is added by
357 * xsk_ring_cons__peek
359 rx->cached_cons -= nb_pkts;
363 for (i = 0; i < nb_pkts; i++) {
364 const struct xdp_desc *desc;
369 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
372 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
374 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
375 rte_ring_enqueue(umem->buf_ring, (void *)addr);
376 rte_pktmbuf_pkt_len(mbufs[i]) = len;
377 rte_pktmbuf_data_len(mbufs[i]) = len;
382 xsk_ring_cons__release(rx, nb_pkts);
385 rxq->stats.rx_pkts += nb_pkts;
386 rxq->stats.rx_bytes += rx_bytes;
393 af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
395 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
396 return af_xdp_rx_zc(queue, bufs, nb_pkts);
398 return af_xdp_rx_cp(queue, bufs, nb_pkts);
403 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
407 if (likely(nb_pkts <= ETH_AF_XDP_RX_BATCH_SIZE))
408 return af_xdp_rx(queue, bufs, nb_pkts);
410 /* Split larger batch into smaller batches of size
411 * ETH_AF_XDP_RX_BATCH_SIZE or less.
417 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
418 ret = af_xdp_rx(queue, &bufs[nb_rx], n);
419 nb_rx = (uint16_t)(nb_rx + ret);
420 nb_pkts = (uint16_t)(nb_pkts - ret);
429 pull_umem_cq(struct xsk_umem_info *umem, int size, struct xsk_ring_cons *cq)
434 n = xsk_ring_cons__peek(cq, size, &idx_cq);
436 for (i = 0; i < n; i++) {
438 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
439 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
440 addr = xsk_umem__extract_addr(addr);
441 rte_pktmbuf_free((struct rte_mbuf *)
442 xsk_umem__get_data(umem->buffer,
443 addr + umem->mb_pool->header_size));
445 rte_ring_enqueue(umem->buf_ring, (void *)addr);
449 xsk_ring_cons__release(cq, n);
453 kick_tx(struct pkt_tx_queue *txq, struct xsk_ring_cons *cq)
455 struct xsk_umem_info *umem = txq->umem;
457 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
459 if (tx_syscall_needed(&txq->tx))
460 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
461 0, MSG_DONTWAIT) < 0) {
462 /* some thing unexpected */
463 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
466 /* pull from completion queue to leave more space */
469 XSK_RING_CONS__DEFAULT_NUM_DESCS,
474 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
476 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
478 struct pkt_tx_queue *txq = queue;
479 struct xsk_umem_info *umem = txq->umem;
480 struct rte_mbuf *mbuf;
481 unsigned long tx_bytes = 0;
485 struct xdp_desc *desc;
486 uint64_t addr, offset;
487 struct xsk_ring_cons *cq = &txq->pair->cq;
488 uint32_t free_thresh = cq->size >> 1;
490 if (xsk_cons_nb_avail(cq, free_thresh) >= free_thresh)
491 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
493 for (i = 0; i < nb_pkts; i++) {
496 if (mbuf->pool == umem->mb_pool) {
497 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
499 if (!xsk_ring_prod__reserve(&txq->tx, 1,
503 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
504 desc->len = mbuf->pkt_len;
505 addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
506 umem->mb_pool->header_size;
507 offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
509 umem->mb_pool->header_size;
510 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
511 desc->addr = addr | offset;
514 struct rte_mbuf *local_mbuf =
515 rte_pktmbuf_alloc(umem->mb_pool);
518 if (local_mbuf == NULL)
521 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
522 rte_pktmbuf_free(local_mbuf);
527 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
528 desc->len = mbuf->pkt_len;
530 addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
531 umem->mb_pool->header_size;
532 offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
533 (uint64_t)local_mbuf +
534 umem->mb_pool->header_size;
535 pkt = xsk_umem__get_data(umem->buffer, addr + offset);
536 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
537 desc->addr = addr | offset;
538 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
540 rte_pktmbuf_free(mbuf);
544 tx_bytes += mbuf->pkt_len;
550 xsk_ring_prod__submit(&txq->tx, count);
552 txq->stats.tx_pkts += count;
553 txq->stats.tx_bytes += tx_bytes;
554 txq->stats.tx_dropped += nb_pkts - count;
560 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
562 struct pkt_tx_queue *txq = queue;
563 struct xsk_umem_info *umem = txq->umem;
564 struct rte_mbuf *mbuf;
565 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
566 unsigned long tx_bytes = 0;
569 struct xsk_ring_cons *cq = &txq->pair->cq;
571 pull_umem_cq(umem, nb_pkts, cq);
573 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
578 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
580 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
584 for (i = 0; i < nb_pkts; i++) {
585 struct xdp_desc *desc;
588 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
590 desc->len = mbuf->pkt_len;
592 desc->addr = (uint64_t)addrs[i];
593 pkt = xsk_umem__get_data(umem->mz->addr,
595 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
596 tx_bytes += mbuf->pkt_len;
597 rte_pktmbuf_free(mbuf);
600 xsk_ring_prod__submit(&txq->tx, nb_pkts);
604 txq->stats.tx_pkts += nb_pkts;
605 txq->stats.tx_bytes += tx_bytes;
611 af_xdp_tx_cp_batch(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
615 if (likely(nb_pkts <= ETH_AF_XDP_TX_BATCH_SIZE))
616 return af_xdp_tx_cp(queue, bufs, nb_pkts);
622 /* Split larger batch into smaller batches of size
623 * ETH_AF_XDP_TX_BATCH_SIZE or less.
625 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
626 ret = af_xdp_tx_cp(queue, &bufs[nb_tx], n);
627 nb_tx = (uint16_t)(nb_tx + ret);
628 nb_pkts = (uint16_t)(nb_pkts - ret);
638 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
640 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
641 return af_xdp_tx_zc(queue, bufs, nb_pkts);
643 return af_xdp_tx_cp_batch(queue, bufs, nb_pkts);
648 eth_dev_start(struct rte_eth_dev *dev)
650 dev->data->dev_link.link_status = ETH_LINK_UP;
655 /* This function gets called when the current port gets stopped. */
657 eth_dev_stop(struct rte_eth_dev *dev)
659 dev->data->dev_link.link_status = ETH_LINK_DOWN;
663 /* Find ethdev in list */
664 static inline struct internal_list *
665 find_internal_resource(struct pmd_internals *port_int)
668 struct internal_list *list = NULL;
670 if (port_int == NULL)
673 pthread_mutex_lock(&internal_list_lock);
675 TAILQ_FOREACH(list, &internal_list, next) {
676 struct pmd_internals *list_int =
677 list->eth_dev->data->dev_private;
678 if (list_int == port_int) {
684 pthread_mutex_unlock(&internal_list_lock);
692 /* Check if the netdev,qid context already exists */
694 ctx_exists(struct pkt_rx_queue *rxq, const char *ifname,
695 struct pkt_rx_queue *list_rxq, const char *list_ifname)
699 if (rxq->xsk_queue_idx == list_rxq->xsk_queue_idx &&
700 !strncmp(ifname, list_ifname, IFNAMSIZ)) {
701 AF_XDP_LOG(ERR, "ctx %s,%i already exists, cannot share umem\n",
702 ifname, rxq->xsk_queue_idx);
709 /* Get a pointer to an existing UMEM which overlays the rxq's mb_pool */
711 get_shared_umem(struct pkt_rx_queue *rxq, const char *ifname,
712 struct xsk_umem_info **umem)
714 struct internal_list *list;
715 struct pmd_internals *internals;
717 struct rte_mempool *mb_pool = rxq->mb_pool;
722 pthread_mutex_lock(&internal_list_lock);
724 TAILQ_FOREACH(list, &internal_list, next) {
725 internals = list->eth_dev->data->dev_private;
726 for (i = 0; i < internals->queue_cnt; i++) {
727 struct pkt_rx_queue *list_rxq =
728 &internals->rx_queues[i];
731 if (mb_pool == internals->rx_queues[i].mb_pool) {
732 if (ctx_exists(rxq, ifname, list_rxq,
733 internals->if_name)) {
738 &internals->rx_queues[i].umem->refcnt,
740 *umem = internals->rx_queues[i].umem;
748 pthread_mutex_unlock(&internal_list_lock);
754 eth_dev_configure(struct rte_eth_dev *dev)
756 struct pmd_internals *internal = dev->data->dev_private;
758 /* rx/tx must be paired */
759 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
762 if (internal->shared_umem) {
763 struct internal_list *list = NULL;
764 const char *name = dev->device->name;
766 /* Ensure PMD is not already inserted into the list */
767 list = find_internal_resource(internal);
771 list = rte_zmalloc_socket(name, sizeof(*list), 0,
772 dev->device->numa_node);
777 pthread_mutex_lock(&internal_list_lock);
778 TAILQ_INSERT_TAIL(&internal_list, list, next);
779 pthread_mutex_unlock(&internal_list_lock);
786 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
788 struct pmd_internals *internals = dev->data->dev_private;
790 dev_info->if_index = internals->if_index;
791 dev_info->max_mac_addrs = 1;
792 dev_info->max_rx_pktlen = ETH_FRAME_LEN;
793 dev_info->max_rx_queues = internals->queue_cnt;
794 dev_info->max_tx_queues = internals->queue_cnt;
796 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
797 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
798 dev_info->max_mtu = getpagesize() -
799 sizeof(struct rte_mempool_objhdr) -
800 sizeof(struct rte_mbuf) -
801 RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
803 dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
806 dev_info->default_rxportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
807 dev_info->default_txportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
808 dev_info->default_rxportconf.nb_queues = 1;
809 dev_info->default_txportconf.nb_queues = 1;
810 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
811 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
817 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
819 struct pmd_internals *internals = dev->data->dev_private;
820 struct xdp_statistics xdp_stats;
821 struct pkt_rx_queue *rxq;
822 struct pkt_tx_queue *txq;
826 for (i = 0; i < dev->data->nb_rx_queues; i++) {
827 optlen = sizeof(struct xdp_statistics);
828 rxq = &internals->rx_queues[i];
830 stats->q_ipackets[i] = rxq->stats.rx_pkts;
831 stats->q_ibytes[i] = rxq->stats.rx_bytes;
833 stats->q_opackets[i] = txq->stats.tx_pkts;
834 stats->q_obytes[i] = txq->stats.tx_bytes;
836 stats->ipackets += stats->q_ipackets[i];
837 stats->ibytes += stats->q_ibytes[i];
838 stats->imissed += rxq->stats.rx_dropped;
839 stats->oerrors += txq->stats.tx_dropped;
840 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
841 XDP_STATISTICS, &xdp_stats, &optlen);
843 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
846 stats->imissed += xdp_stats.rx_dropped;
848 stats->opackets += stats->q_opackets[i];
849 stats->obytes += stats->q_obytes[i];
856 eth_stats_reset(struct rte_eth_dev *dev)
858 struct pmd_internals *internals = dev->data->dev_private;
861 for (i = 0; i < internals->queue_cnt; i++) {
862 memset(&internals->rx_queues[i].stats, 0,
863 sizeof(struct rx_stats));
864 memset(&internals->tx_queues[i].stats, 0,
865 sizeof(struct tx_stats));
872 remove_xdp_program(struct pmd_internals *internals)
874 uint32_t curr_prog_id = 0;
876 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
877 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
878 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
881 bpf_set_link_xdp_fd(internals->if_index, -1,
882 XDP_FLAGS_UPDATE_IF_NOEXIST);
886 xdp_umem_destroy(struct xsk_umem_info *umem)
888 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
889 umem->mb_pool = NULL;
891 rte_memzone_free(umem->mz);
894 rte_ring_free(umem->buf_ring);
895 umem->buf_ring = NULL;
902 eth_dev_close(struct rte_eth_dev *dev)
904 struct pmd_internals *internals = dev->data->dev_private;
905 struct pkt_rx_queue *rxq;
908 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
911 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
914 for (i = 0; i < internals->queue_cnt; i++) {
915 rxq = &internals->rx_queues[i];
916 if (rxq->umem == NULL)
918 xsk_socket__delete(rxq->xsk);
920 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE)
922 (void)xsk_umem__delete(rxq->umem->umem);
923 xdp_umem_destroy(rxq->umem);
926 /* free pkt_tx_queue */
932 * MAC is not allocated dynamically, setting it to NULL would prevent
933 * from releasing it in rte_eth_dev_release_port.
935 dev->data->mac_addrs = NULL;
937 remove_xdp_program(internals);
939 if (internals->shared_umem) {
940 struct internal_list *list;
942 /* Remove ethdev from list used to track and share UMEMs */
943 list = find_internal_resource(internals);
945 pthread_mutex_lock(&internal_list_lock);
946 TAILQ_REMOVE(&internal_list, list, next);
947 pthread_mutex_unlock(&internal_list_lock);
956 eth_queue_release(void *q __rte_unused)
961 eth_link_update(struct rte_eth_dev *dev __rte_unused,
962 int wait_to_complete __rte_unused)
967 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
968 static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
970 struct rte_mempool_memhdr *memhdr;
971 uintptr_t memhdr_addr, aligned_addr;
973 memhdr = STAILQ_FIRST(&mp->mem_list);
974 memhdr_addr = (uintptr_t)memhdr->addr;
975 aligned_addr = memhdr_addr & ~(getpagesize() - 1);
976 *align = memhdr_addr - aligned_addr;
982 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
983 struct pkt_rx_queue *rxq)
985 struct xsk_umem_info *umem = NULL;
987 struct xsk_umem_config usr_config = {
988 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
989 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
990 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
991 void *base_addr = NULL;
992 struct rte_mempool *mb_pool = rxq->mb_pool;
993 uint64_t umem_size, align = 0;
995 if (internals->shared_umem) {
996 if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
1000 __atomic_load_n(&umem->refcnt, __ATOMIC_ACQUIRE) <
1002 AF_XDP_LOG(INFO, "%s,qid%i sharing UMEM\n",
1003 internals->if_name, rxq->xsk_queue_idx);
1004 __atomic_fetch_add(&umem->refcnt, 1, __ATOMIC_ACQUIRE);
1009 usr_config.frame_size =
1010 rte_mempool_calc_obj_size(mb_pool->elt_size,
1011 mb_pool->flags, NULL);
1012 usr_config.frame_headroom = mb_pool->header_size +
1013 sizeof(struct rte_mbuf) +
1014 rte_pktmbuf_priv_size(mb_pool) +
1015 RTE_PKTMBUF_HEADROOM;
1017 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0,
1020 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1024 umem->mb_pool = mb_pool;
1025 base_addr = (void *)get_base_addr(mb_pool, &align);
1026 umem_size = (uint64_t)mb_pool->populated_size *
1027 (uint64_t)usr_config.frame_size +
1030 ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
1031 &rxq->fq, &rxq->cq, &usr_config);
1033 AF_XDP_LOG(ERR, "Failed to create umem");
1036 umem->buffer = base_addr;
1038 if (internals->shared_umem) {
1039 umem->max_xsks = mb_pool->populated_size /
1040 ETH_AF_XDP_NUM_BUFFERS;
1041 AF_XDP_LOG(INFO, "Max xsks for UMEM %s: %u\n",
1042 mb_pool->name, umem->max_xsks);
1045 __atomic_store_n(&umem->refcnt, 1, __ATOMIC_RELEASE);
1050 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1051 struct pkt_rx_queue *rxq)
1053 struct xsk_umem_info *umem;
1054 const struct rte_memzone *mz;
1055 struct xsk_umem_config usr_config = {
1056 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1057 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1058 .frame_size = ETH_AF_XDP_FRAME_SIZE,
1059 .frame_headroom = 0 };
1060 char ring_name[RTE_RING_NAMESIZE];
1061 char mz_name[RTE_MEMZONE_NAMESIZE];
1065 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
1067 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1071 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
1072 internals->if_name, rxq->xsk_queue_idx);
1073 umem->buf_ring = rte_ring_create(ring_name,
1074 ETH_AF_XDP_NUM_BUFFERS,
1077 if (umem->buf_ring == NULL) {
1078 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
1082 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
1083 rte_ring_enqueue(umem->buf_ring,
1084 (void *)(i * ETH_AF_XDP_FRAME_SIZE));
1086 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1087 internals->if_name, rxq->xsk_queue_idx);
1088 mz = rte_memzone_reserve_aligned(mz_name,
1089 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1090 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1093 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1097 ret = xsk_umem__create(&umem->umem, mz->addr,
1098 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1103 AF_XDP_LOG(ERR, "Failed to create umem");
1112 xdp_umem_destroy(umem);
1117 load_custom_xdp_prog(const char *prog_path, int if_index)
1119 int ret, prog_fd = -1;
1120 struct bpf_object *obj;
1121 struct bpf_map *map;
1123 ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
1125 AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
1130 * The loaded program must provision for a map of xsks, such that some
1131 * traffic can be redirected to userspace. When the xsk is created,
1132 * libbpf inserts it into the map.
1134 map = bpf_object__find_map_by_name(obj, "xsks_map");
1136 AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
1140 /* Link the program with the given network device */
1141 ret = bpf_set_link_xdp_fd(if_index, prog_fd,
1142 XDP_FLAGS_UPDATE_IF_NOEXIST);
1144 AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
1149 AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
1150 prog_path, prog_fd);
1155 /* Detect support for busy polling through setsockopt(). */
1157 configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
1160 int fd = xsk_socket__fd(rxq->xsk);
1163 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1164 (void *)&sock_opt, sizeof(sock_opt));
1166 AF_XDP_LOG(DEBUG, "Failed to set SO_PREFER_BUSY_POLL\n");
1170 sock_opt = ETH_AF_XDP_DFLT_BUSY_TIMEOUT;
1171 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1174 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL\n");
1178 sock_opt = rxq->busy_budget;
1179 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1180 (void *)&sock_opt, sizeof(sock_opt));
1182 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL_BUDGET\n");
1184 AF_XDP_LOG(INFO, "Busy polling budget set to: %u\n",
1189 /* setsockopt failure - attempt to restore xsk to default state and
1190 * proceed without busy polling support.
1193 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1196 AF_XDP_LOG(ERR, "Failed to unset SO_BUSY_POLL\n");
1202 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1203 (void *)&sock_opt, sizeof(sock_opt));
1205 AF_XDP_LOG(ERR, "Failed to unset SO_PREFER_BUSY_POLL\n");
1210 rxq->busy_budget = 0;
1215 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1218 struct xsk_socket_config cfg;
1219 struct pkt_tx_queue *txq = rxq->pair;
1221 int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1222 struct rte_mbuf *fq_bufs[reserve_size];
1224 rxq->umem = xdp_umem_configure(internals, rxq);
1225 if (rxq->umem == NULL)
1227 txq->umem = rxq->umem;
1229 cfg.rx_size = ring_size;
1230 cfg.tx_size = ring_size;
1231 cfg.libbpf_flags = 0;
1232 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1235 #if defined(XDP_USE_NEED_WAKEUP)
1236 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1239 if (strnlen(internals->prog_path, PATH_MAX) &&
1240 !internals->custom_prog_configured) {
1241 ret = load_custom_xdp_prog(internals->prog_path,
1242 internals->if_index);
1244 AF_XDP_LOG(ERR, "Failed to load custom XDP program %s\n",
1245 internals->prog_path);
1248 internals->custom_prog_configured = 1;
1251 if (internals->shared_umem)
1252 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1253 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1254 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1256 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1257 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1261 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1265 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1266 ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size);
1268 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1273 if (rxq->busy_budget) {
1274 ret = configure_preferred_busy_poll(rxq);
1276 AF_XDP_LOG(ERR, "Failed configure busy polling.\n");
1281 ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1283 xsk_socket__delete(rxq->xsk);
1284 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1291 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1292 xdp_umem_destroy(rxq->umem);
1298 eth_rx_queue_setup(struct rte_eth_dev *dev,
1299 uint16_t rx_queue_id,
1300 uint16_t nb_rx_desc,
1301 unsigned int socket_id __rte_unused,
1302 const struct rte_eth_rxconf *rx_conf __rte_unused,
1303 struct rte_mempool *mb_pool)
1305 struct pmd_internals *internals = dev->data->dev_private;
1306 struct pkt_rx_queue *rxq;
1309 rxq = &internals->rx_queues[rx_queue_id];
1311 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1312 rx_queue_id, rxq->xsk_queue_idx);
1314 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1315 uint32_t buf_size, data_size;
1317 /* Now get the space available for data in the mbuf */
1318 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1319 RTE_PKTMBUF_HEADROOM;
1320 data_size = ETH_AF_XDP_FRAME_SIZE;
1322 if (data_size > buf_size) {
1323 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1324 dev->device->name, data_size, buf_size);
1330 rxq->mb_pool = mb_pool;
1332 if (xsk_configure(internals, rxq, nb_rx_desc)) {
1333 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1338 if (!rxq->busy_budget)
1339 AF_XDP_LOG(DEBUG, "Preferred busy polling not enabled\n");
1341 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1342 rxq->fds[0].events = POLLIN;
1344 dev->data->rx_queues[rx_queue_id] = rxq;
1352 eth_tx_queue_setup(struct rte_eth_dev *dev,
1353 uint16_t tx_queue_id,
1354 uint16_t nb_tx_desc __rte_unused,
1355 unsigned int socket_id __rte_unused,
1356 const struct rte_eth_txconf *tx_conf __rte_unused)
1358 struct pmd_internals *internals = dev->data->dev_private;
1359 struct pkt_tx_queue *txq;
1361 txq = &internals->tx_queues[tx_queue_id];
1363 dev->data->tx_queues[tx_queue_id] = txq;
1368 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1370 struct pmd_internals *internals = dev->data->dev_private;
1371 struct ifreq ifr = { .ifr_mtu = mtu };
1375 s = socket(PF_INET, SOCK_DGRAM, 0);
1379 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1380 ret = ioctl(s, SIOCSIFMTU, &ifr);
1383 return (ret < 0) ? -errno : 0;
1387 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1393 s = socket(PF_INET, SOCK_DGRAM, 0);
1397 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1398 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1402 ifr.ifr_flags &= mask;
1403 ifr.ifr_flags |= flags;
1404 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1414 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1416 struct pmd_internals *internals = dev->data->dev_private;
1418 return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1422 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1424 struct pmd_internals *internals = dev->data->dev_private;
1426 return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1429 static const struct eth_dev_ops ops = {
1430 .dev_start = eth_dev_start,
1431 .dev_stop = eth_dev_stop,
1432 .dev_close = eth_dev_close,
1433 .dev_configure = eth_dev_configure,
1434 .dev_infos_get = eth_dev_info,
1435 .mtu_set = eth_dev_mtu_set,
1436 .promiscuous_enable = eth_dev_promiscuous_enable,
1437 .promiscuous_disable = eth_dev_promiscuous_disable,
1438 .rx_queue_setup = eth_rx_queue_setup,
1439 .tx_queue_setup = eth_tx_queue_setup,
1440 .rx_queue_release = eth_queue_release,
1441 .tx_queue_release = eth_queue_release,
1442 .link_update = eth_link_update,
1443 .stats_get = eth_stats_get,
1444 .stats_reset = eth_stats_reset,
1447 /** parse busy_budget argument */
1449 parse_budget_arg(const char *key __rte_unused,
1450 const char *value, void *extra_args)
1452 int *i = (int *)extra_args;
1455 *i = strtol(value, &end, 10);
1456 if (*i < 0 || *i > UINT16_MAX) {
1457 AF_XDP_LOG(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u\n",
1465 /** parse integer from integer argument */
1467 parse_integer_arg(const char *key __rte_unused,
1468 const char *value, void *extra_args)
1470 int *i = (int *)extra_args;
1473 *i = strtol(value, &end, 10);
1475 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1482 /** parse name argument */
1484 parse_name_arg(const char *key __rte_unused,
1485 const char *value, void *extra_args)
1487 char *name = extra_args;
1489 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1490 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1495 strlcpy(name, value, IFNAMSIZ);
1500 /** parse xdp prog argument */
1502 parse_prog_arg(const char *key __rte_unused,
1503 const char *value, void *extra_args)
1505 char *path = extra_args;
1507 if (strnlen(value, PATH_MAX) == PATH_MAX) {
1508 AF_XDP_LOG(ERR, "Invalid path %s, should be less than %u bytes.\n",
1513 if (access(value, F_OK) != 0) {
1514 AF_XDP_LOG(ERR, "Error accessing %s: %s\n",
1515 value, strerror(errno));
1519 strlcpy(path, value, PATH_MAX);
1525 xdp_get_channels_info(const char *if_name, int *max_queues,
1526 int *combined_queues)
1528 struct ethtool_channels channels;
1532 fd = socket(AF_INET, SOCK_DGRAM, 0);
1536 channels.cmd = ETHTOOL_GCHANNELS;
1537 ifr.ifr_data = (void *)&channels;
1538 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1539 ret = ioctl(fd, SIOCETHTOOL, &ifr);
1541 if (errno == EOPNOTSUPP) {
1549 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1550 /* If the device says it has no channels, then all traffic
1551 * is sent to a single stream, so max queues = 1.
1554 *combined_queues = 1;
1556 *max_queues = channels.max_combined;
1557 *combined_queues = channels.combined_count;
1566 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1567 int *queue_cnt, int *shared_umem, char *prog_path,
1572 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1573 &parse_name_arg, if_name);
1577 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1578 &parse_integer_arg, start_queue);
1582 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1583 &parse_integer_arg, queue_cnt);
1584 if (ret < 0 || *queue_cnt <= 0) {
1589 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1590 &parse_integer_arg, shared_umem);
1594 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
1595 &parse_prog_arg, prog_path);
1599 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
1600 &parse_budget_arg, busy_budget);
1605 rte_kvargs_free(kvlist);
1610 get_iface_info(const char *if_name,
1611 struct rte_ether_addr *eth_addr,
1615 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1620 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1621 if (ioctl(sock, SIOCGIFINDEX, &ifr))
1624 *if_index = ifr.ifr_ifindex;
1626 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1629 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1639 static struct rte_eth_dev *
1640 init_internals(struct rte_vdev_device *dev, const char *if_name,
1641 int start_queue_idx, int queue_cnt, int shared_umem,
1642 const char *prog_path, int busy_budget)
1644 const char *name = rte_vdev_device_name(dev);
1645 const unsigned int numa_node = dev->device.numa_node;
1646 struct pmd_internals *internals;
1647 struct rte_eth_dev *eth_dev;
1651 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1652 if (internals == NULL)
1655 internals->start_queue_idx = start_queue_idx;
1656 internals->queue_cnt = queue_cnt;
1657 strlcpy(internals->if_name, if_name, IFNAMSIZ);
1658 strlcpy(internals->prog_path, prog_path, PATH_MAX);
1659 internals->custom_prog_configured = 0;
1661 #ifndef ETH_AF_XDP_SHARED_UMEM
1663 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1664 "Check kernel and libbpf version\n");
1665 goto err_free_internals;
1668 internals->shared_umem = shared_umem;
1670 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1671 &internals->combined_queue_cnt)) {
1672 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1674 goto err_free_internals;
1677 if (queue_cnt > internals->combined_queue_cnt) {
1678 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1679 queue_cnt, internals->combined_queue_cnt);
1680 goto err_free_internals;
1683 internals->rx_queues = rte_zmalloc_socket(NULL,
1684 sizeof(struct pkt_rx_queue) * queue_cnt,
1686 if (internals->rx_queues == NULL) {
1687 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1688 goto err_free_internals;
1691 internals->tx_queues = rte_zmalloc_socket(NULL,
1692 sizeof(struct pkt_tx_queue) * queue_cnt,
1694 if (internals->tx_queues == NULL) {
1695 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1698 for (i = 0; i < queue_cnt; i++) {
1699 internals->tx_queues[i].pair = &internals->rx_queues[i];
1700 internals->rx_queues[i].pair = &internals->tx_queues[i];
1701 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1702 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1703 internals->rx_queues[i].busy_budget = busy_budget;
1706 ret = get_iface_info(if_name, &internals->eth_addr,
1707 &internals->if_index);
1711 eth_dev = rte_eth_vdev_allocate(dev, 0);
1712 if (eth_dev == NULL)
1715 eth_dev->data->dev_private = internals;
1716 eth_dev->data->dev_link = pmd_link;
1717 eth_dev->data->mac_addrs = &internals->eth_addr;
1718 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1719 eth_dev->dev_ops = &ops;
1720 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1721 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1723 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1724 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1730 rte_free(internals->tx_queues);
1732 rte_free(internals->rx_queues);
1734 rte_free(internals);
1739 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1741 struct rte_kvargs *kvlist;
1742 char if_name[IFNAMSIZ] = {'\0'};
1743 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1744 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1745 int shared_umem = 0;
1746 char prog_path[PATH_MAX] = {'\0'};
1747 int busy_budget = -1;
1748 struct rte_eth_dev *eth_dev = NULL;
1751 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1752 rte_vdev_device_name(dev));
1754 name = rte_vdev_device_name(dev);
1755 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1756 strlen(rte_vdev_device_args(dev)) == 0) {
1757 eth_dev = rte_eth_dev_attach_secondary(name);
1758 if (eth_dev == NULL) {
1759 AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1762 eth_dev->dev_ops = &ops;
1763 rte_eth_dev_probing_finish(eth_dev);
1767 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1768 if (kvlist == NULL) {
1769 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1773 if (dev->device.numa_node == SOCKET_ID_ANY)
1774 dev->device.numa_node = rte_socket_id();
1776 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1777 &xsk_queue_cnt, &shared_umem, prog_path,
1778 &busy_budget) < 0) {
1779 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1783 if (strlen(if_name) == 0) {
1784 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1788 busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
1791 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1792 xsk_queue_cnt, shared_umem, prog_path,
1794 if (eth_dev == NULL) {
1795 AF_XDP_LOG(ERR, "Failed to init internals\n");
1799 rte_eth_dev_probing_finish(eth_dev);
1805 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1807 struct rte_eth_dev *eth_dev = NULL;
1809 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1815 /* find the ethdev entry */
1816 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1817 if (eth_dev == NULL)
1820 eth_dev_close(eth_dev);
1821 rte_eth_dev_release_port(eth_dev);
1827 static struct rte_vdev_driver pmd_af_xdp_drv = {
1828 .probe = rte_pmd_af_xdp_probe,
1829 .remove = rte_pmd_af_xdp_remove,
1832 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1833 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1835 "start_queue=<int> "
1836 "queue_count=<int> "
1837 "shared_umem=<int> "
1838 "xdp_prog=<string> "
1839 "busy_budget=<int>");