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>
40 #include <rte_power_intrinsics.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_DEFAULT(af_xdp_logtype, 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
81 #define ETH_AF_XDP_ETH_OVERHEAD (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
83 struct xsk_umem_info {
84 struct xsk_umem *umem;
85 struct rte_ring *buf_ring;
86 const struct rte_memzone *mz;
87 struct rte_mempool *mb_pool;
100 struct xsk_ring_cons rx;
101 struct xsk_umem_info *umem;
102 struct xsk_socket *xsk;
103 struct rte_mempool *mb_pool;
105 struct rx_stats stats;
107 struct xsk_ring_prod fq;
108 struct xsk_ring_cons cq;
110 struct pkt_tx_queue *pair;
111 struct pollfd fds[1];
122 struct pkt_tx_queue {
123 struct xsk_ring_prod tx;
124 struct xsk_umem_info *umem;
126 struct tx_stats stats;
128 struct pkt_rx_queue *pair;
132 struct pmd_internals {
134 char if_name[IFNAMSIZ];
138 int combined_queue_cnt;
140 char prog_path[PATH_MAX];
141 bool custom_prog_configured;
144 struct rte_ether_addr eth_addr;
146 struct pkt_rx_queue *rx_queues;
147 struct pkt_tx_queue *tx_queues;
150 #define ETH_AF_XDP_IFACE_ARG "iface"
151 #define ETH_AF_XDP_START_QUEUE_ARG "start_queue"
152 #define ETH_AF_XDP_QUEUE_COUNT_ARG "queue_count"
153 #define ETH_AF_XDP_SHARED_UMEM_ARG "shared_umem"
154 #define ETH_AF_XDP_PROG_ARG "xdp_prog"
155 #define ETH_AF_XDP_BUDGET_ARG "busy_budget"
157 static const char * const valid_arguments[] = {
158 ETH_AF_XDP_IFACE_ARG,
159 ETH_AF_XDP_START_QUEUE_ARG,
160 ETH_AF_XDP_QUEUE_COUNT_ARG,
161 ETH_AF_XDP_SHARED_UMEM_ARG,
163 ETH_AF_XDP_BUDGET_ARG,
167 static const struct rte_eth_link pmd_link = {
168 .link_speed = RTE_ETH_SPEED_NUM_10G,
169 .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
170 .link_status = RTE_ETH_LINK_DOWN,
171 .link_autoneg = RTE_ETH_LINK_AUTONEG
174 /* List which tracks PMDs to facilitate sharing UMEMs across them. */
175 struct internal_list {
176 TAILQ_ENTRY(internal_list) next;
177 struct rte_eth_dev *eth_dev;
180 TAILQ_HEAD(internal_list_head, internal_list);
181 static struct internal_list_head internal_list =
182 TAILQ_HEAD_INITIALIZER(internal_list);
184 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
186 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
188 reserve_fill_queue_zc(struct xsk_umem_info *umem, uint16_t reserve_size,
189 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
194 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
195 for (i = 0; i < reserve_size; i++)
196 rte_pktmbuf_free(bufs[i]);
197 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
201 for (i = 0; i < reserve_size; i++) {
205 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
206 addr = (uint64_t)bufs[i] - (uint64_t)umem->buffer -
207 umem->mb_pool->header_size;
211 xsk_ring_prod__submit(fq, reserve_size);
217 reserve_fill_queue_cp(struct xsk_umem_info *umem, uint16_t reserve_size,
218 struct rte_mbuf **bufs __rte_unused,
219 struct xsk_ring_prod *fq)
221 void *addrs[reserve_size];
225 if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
227 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
231 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
232 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
233 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
238 for (i = 0; i < reserve_size; i++) {
241 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
242 *fq_addr = (uint64_t)addrs[i];
245 xsk_ring_prod__submit(fq, reserve_size);
252 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size,
253 struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
255 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
256 return reserve_fill_queue_zc(umem, reserve_size, bufs, fq);
258 return reserve_fill_queue_cp(umem, reserve_size, bufs, fq);
262 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
264 af_xdp_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
266 struct pkt_rx_queue *rxq = queue;
267 struct xsk_ring_cons *rx = &rxq->rx;
268 struct xsk_ring_prod *fq = &rxq->fq;
269 struct xsk_umem_info *umem = rxq->umem;
271 unsigned long rx_bytes = 0;
273 struct rte_mbuf *fq_bufs[ETH_AF_XDP_RX_BATCH_SIZE];
275 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
278 /* we can assume a kernel >= 5.11 is in use if busy polling is
279 * enabled and thus we can safely use the recvfrom() syscall
280 * which is only supported for AF_XDP sockets in kernels >=
283 if (rxq->busy_budget) {
284 (void)recvfrom(xsk_socket__fd(rxq->xsk), NULL, 0,
285 MSG_DONTWAIT, NULL, NULL);
286 } else if (xsk_ring_prod__needs_wakeup(fq)) {
287 (void)poll(&rxq->fds[0], 1, 1000);
293 /* allocate bufs for fill queue replenishment after rx */
294 if (rte_pktmbuf_alloc_bulk(umem->mb_pool, fq_bufs, nb_pkts)) {
296 "Failed to get enough buffers for fq.\n");
297 /* rollback cached_cons which is added by
298 * xsk_ring_cons__peek
300 rx->cached_cons -= nb_pkts;
304 for (i = 0; i < nb_pkts; i++) {
305 const struct xdp_desc *desc;
310 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
314 offset = xsk_umem__extract_offset(addr);
315 addr = xsk_umem__extract_addr(addr);
317 bufs[i] = (struct rte_mbuf *)
318 xsk_umem__get_data(umem->buffer, addr +
319 umem->mb_pool->header_size);
320 bufs[i]->data_off = offset - sizeof(struct rte_mbuf) -
321 rte_pktmbuf_priv_size(umem->mb_pool) -
322 umem->mb_pool->header_size;
324 rte_pktmbuf_pkt_len(bufs[i]) = len;
325 rte_pktmbuf_data_len(bufs[i]) = len;
329 xsk_ring_cons__release(rx, nb_pkts);
330 (void)reserve_fill_queue(umem, nb_pkts, fq_bufs, fq);
333 rxq->stats.rx_pkts += nb_pkts;
334 rxq->stats.rx_bytes += rx_bytes;
340 af_xdp_rx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
342 struct pkt_rx_queue *rxq = queue;
343 struct xsk_ring_cons *rx = &rxq->rx;
344 struct xsk_umem_info *umem = rxq->umem;
345 struct xsk_ring_prod *fq = &rxq->fq;
347 unsigned long rx_bytes = 0;
349 uint32_t free_thresh = fq->size >> 1;
350 struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
352 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
353 (void)reserve_fill_queue(umem, nb_pkts, NULL, fq);
355 nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
357 #if defined(XDP_USE_NEED_WAKEUP)
358 if (xsk_ring_prod__needs_wakeup(fq))
359 (void)poll(rxq->fds, 1, 1000);
364 if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts))) {
365 /* rollback cached_cons which is added by
366 * xsk_ring_cons__peek
368 rx->cached_cons -= nb_pkts;
372 for (i = 0; i < nb_pkts; i++) {
373 const struct xdp_desc *desc;
378 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
381 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
383 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
384 rte_ring_enqueue(umem->buf_ring, (void *)addr);
385 rte_pktmbuf_pkt_len(mbufs[i]) = len;
386 rte_pktmbuf_data_len(mbufs[i]) = len;
391 xsk_ring_cons__release(rx, nb_pkts);
394 rxq->stats.rx_pkts += nb_pkts;
395 rxq->stats.rx_bytes += rx_bytes;
402 af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
404 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
405 return af_xdp_rx_zc(queue, bufs, nb_pkts);
407 return af_xdp_rx_cp(queue, bufs, nb_pkts);
412 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
416 if (likely(nb_pkts <= ETH_AF_XDP_RX_BATCH_SIZE))
417 return af_xdp_rx(queue, bufs, nb_pkts);
419 /* Split larger batch into smaller batches of size
420 * ETH_AF_XDP_RX_BATCH_SIZE or less.
426 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
427 ret = af_xdp_rx(queue, &bufs[nb_rx], n);
428 nb_rx = (uint16_t)(nb_rx + ret);
429 nb_pkts = (uint16_t)(nb_pkts - ret);
438 pull_umem_cq(struct xsk_umem_info *umem, int size, struct xsk_ring_cons *cq)
443 n = xsk_ring_cons__peek(cq, size, &idx_cq);
445 for (i = 0; i < n; i++) {
447 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
448 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
449 addr = xsk_umem__extract_addr(addr);
450 rte_pktmbuf_free((struct rte_mbuf *)
451 xsk_umem__get_data(umem->buffer,
452 addr + umem->mb_pool->header_size));
454 rte_ring_enqueue(umem->buf_ring, (void *)addr);
458 xsk_ring_cons__release(cq, n);
462 kick_tx(struct pkt_tx_queue *txq, struct xsk_ring_cons *cq)
464 struct xsk_umem_info *umem = txq->umem;
466 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
468 if (tx_syscall_needed(&txq->tx))
469 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
470 0, MSG_DONTWAIT) < 0) {
471 /* some thing unexpected */
472 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
475 /* pull from completion queue to leave more space */
478 XSK_RING_CONS__DEFAULT_NUM_DESCS,
483 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
485 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
487 struct pkt_tx_queue *txq = queue;
488 struct xsk_umem_info *umem = txq->umem;
489 struct rte_mbuf *mbuf;
490 unsigned long tx_bytes = 0;
494 struct xdp_desc *desc;
495 uint64_t addr, offset;
496 struct xsk_ring_cons *cq = &txq->pair->cq;
497 uint32_t free_thresh = cq->size >> 1;
499 if (xsk_cons_nb_avail(cq, free_thresh) >= free_thresh)
500 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
502 for (i = 0; i < nb_pkts; i++) {
505 if (mbuf->pool == umem->mb_pool) {
506 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
508 if (!xsk_ring_prod__reserve(&txq->tx, 1,
512 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
513 desc->len = mbuf->pkt_len;
514 addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
515 umem->mb_pool->header_size;
516 offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
518 umem->mb_pool->header_size;
519 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
520 desc->addr = addr | offset;
523 struct rte_mbuf *local_mbuf =
524 rte_pktmbuf_alloc(umem->mb_pool);
527 if (local_mbuf == NULL)
530 if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
531 rte_pktmbuf_free(local_mbuf);
535 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
536 desc->len = mbuf->pkt_len;
538 addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
539 umem->mb_pool->header_size;
540 offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
541 (uint64_t)local_mbuf +
542 umem->mb_pool->header_size;
543 pkt = xsk_umem__get_data(umem->buffer, addr + offset);
544 offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
545 desc->addr = addr | offset;
546 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
548 rte_pktmbuf_free(mbuf);
552 tx_bytes += mbuf->pkt_len;
556 xsk_ring_prod__submit(&txq->tx, count);
559 txq->stats.tx_pkts += count;
560 txq->stats.tx_bytes += tx_bytes;
561 txq->stats.tx_dropped += nb_pkts - count;
567 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
569 struct pkt_tx_queue *txq = queue;
570 struct xsk_umem_info *umem = txq->umem;
571 struct rte_mbuf *mbuf;
572 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
573 unsigned long tx_bytes = 0;
576 struct xsk_ring_cons *cq = &txq->pair->cq;
578 pull_umem_cq(umem, nb_pkts, cq);
580 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
585 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
587 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
591 for (i = 0; i < nb_pkts; i++) {
592 struct xdp_desc *desc;
595 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
597 desc->len = mbuf->pkt_len;
599 desc->addr = (uint64_t)addrs[i];
600 pkt = xsk_umem__get_data(umem->mz->addr,
602 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
603 tx_bytes += mbuf->pkt_len;
604 rte_pktmbuf_free(mbuf);
607 xsk_ring_prod__submit(&txq->tx, nb_pkts);
611 txq->stats.tx_pkts += nb_pkts;
612 txq->stats.tx_bytes += tx_bytes;
618 af_xdp_tx_cp_batch(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
622 if (likely(nb_pkts <= ETH_AF_XDP_TX_BATCH_SIZE))
623 return af_xdp_tx_cp(queue, bufs, nb_pkts);
629 /* Split larger batch into smaller batches of size
630 * ETH_AF_XDP_TX_BATCH_SIZE or less.
632 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
633 ret = af_xdp_tx_cp(queue, &bufs[nb_tx], n);
634 nb_tx = (uint16_t)(nb_tx + ret);
635 nb_pkts = (uint16_t)(nb_pkts - ret);
645 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
647 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
648 return af_xdp_tx_zc(queue, bufs, nb_pkts);
650 return af_xdp_tx_cp_batch(queue, bufs, nb_pkts);
655 eth_dev_start(struct rte_eth_dev *dev)
657 dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
662 /* This function gets called when the current port gets stopped. */
664 eth_dev_stop(struct rte_eth_dev *dev)
666 dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
670 /* Find ethdev in list */
671 static inline struct internal_list *
672 find_internal_resource(struct pmd_internals *port_int)
675 struct internal_list *list = NULL;
677 if (port_int == NULL)
680 pthread_mutex_lock(&internal_list_lock);
682 TAILQ_FOREACH(list, &internal_list, next) {
683 struct pmd_internals *list_int =
684 list->eth_dev->data->dev_private;
685 if (list_int == port_int) {
691 pthread_mutex_unlock(&internal_list_lock);
700 eth_dev_configure(struct rte_eth_dev *dev)
702 struct pmd_internals *internal = dev->data->dev_private;
704 /* rx/tx must be paired */
705 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
708 if (internal->shared_umem) {
709 struct internal_list *list = NULL;
710 const char *name = dev->device->name;
712 /* Ensure PMD is not already inserted into the list */
713 list = find_internal_resource(internal);
717 list = rte_zmalloc_socket(name, sizeof(*list), 0,
718 dev->device->numa_node);
723 pthread_mutex_lock(&internal_list_lock);
724 TAILQ_INSERT_TAIL(&internal_list, list, next);
725 pthread_mutex_unlock(&internal_list_lock);
731 #define CLB_VAL_IDX 0
733 eth_monitor_callback(const uint64_t value,
734 const uint64_t opaque[RTE_POWER_MONITOR_OPAQUE_SZ])
736 const uint64_t v = opaque[CLB_VAL_IDX];
737 const uint64_t m = (uint32_t)~0;
739 /* if the value has changed, abort entering power optimized state */
740 return (value & m) == v ? 0 : -1;
744 eth_get_monitor_addr(void *rx_queue, struct rte_power_monitor_cond *pmc)
746 struct pkt_rx_queue *rxq = rx_queue;
747 unsigned int *prod = rxq->rx.producer;
748 const uint32_t cur_val = rxq->rx.cached_prod; /* use cached value */
750 /* watch for changes in producer ring */
751 pmc->addr = (void *)prod;
753 /* store current value */
754 pmc->opaque[CLB_VAL_IDX] = cur_val;
755 pmc->fn = eth_monitor_callback;
757 /* AF_XDP producer ring index is 32-bit */
758 pmc->size = sizeof(uint32_t);
764 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
766 struct pmd_internals *internals = dev->data->dev_private;
768 dev_info->if_index = internals->if_index;
769 dev_info->max_mac_addrs = 1;
770 dev_info->max_rx_queues = internals->queue_cnt;
771 dev_info->max_tx_queues = internals->queue_cnt;
773 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
774 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
775 dev_info->max_rx_pktlen = getpagesize() -
776 sizeof(struct rte_mempool_objhdr) -
777 sizeof(struct rte_mbuf) -
778 RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
780 dev_info->max_rx_pktlen = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
782 dev_info->max_mtu = dev_info->max_rx_pktlen - ETH_AF_XDP_ETH_OVERHEAD;
784 dev_info->default_rxportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
785 dev_info->default_txportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
786 dev_info->default_rxportconf.nb_queues = 1;
787 dev_info->default_txportconf.nb_queues = 1;
788 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
789 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
795 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
797 struct pmd_internals *internals = dev->data->dev_private;
798 struct xdp_statistics xdp_stats;
799 struct pkt_rx_queue *rxq;
800 struct pkt_tx_queue *txq;
804 for (i = 0; i < dev->data->nb_rx_queues; i++) {
805 optlen = sizeof(struct xdp_statistics);
806 rxq = &internals->rx_queues[i];
808 stats->q_ipackets[i] = rxq->stats.rx_pkts;
809 stats->q_ibytes[i] = rxq->stats.rx_bytes;
811 stats->q_opackets[i] = txq->stats.tx_pkts;
812 stats->q_obytes[i] = txq->stats.tx_bytes;
814 stats->ipackets += stats->q_ipackets[i];
815 stats->ibytes += stats->q_ibytes[i];
816 stats->imissed += rxq->stats.rx_dropped;
817 stats->oerrors += txq->stats.tx_dropped;
818 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
819 XDP_STATISTICS, &xdp_stats, &optlen);
821 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
824 stats->imissed += xdp_stats.rx_dropped;
826 stats->opackets += stats->q_opackets[i];
827 stats->obytes += stats->q_obytes[i];
834 eth_stats_reset(struct rte_eth_dev *dev)
836 struct pmd_internals *internals = dev->data->dev_private;
839 for (i = 0; i < internals->queue_cnt; i++) {
840 memset(&internals->rx_queues[i].stats, 0,
841 sizeof(struct rx_stats));
842 memset(&internals->tx_queues[i].stats, 0,
843 sizeof(struct tx_stats));
850 remove_xdp_program(struct pmd_internals *internals)
852 uint32_t curr_prog_id = 0;
854 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
855 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
856 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
859 bpf_set_link_xdp_fd(internals->if_index, -1,
860 XDP_FLAGS_UPDATE_IF_NOEXIST);
864 xdp_umem_destroy(struct xsk_umem_info *umem)
866 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
867 umem->mb_pool = NULL;
869 rte_memzone_free(umem->mz);
872 rte_ring_free(umem->buf_ring);
873 umem->buf_ring = NULL;
880 eth_dev_close(struct rte_eth_dev *dev)
882 struct pmd_internals *internals = dev->data->dev_private;
883 struct pkt_rx_queue *rxq;
886 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
889 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
892 for (i = 0; i < internals->queue_cnt; i++) {
893 rxq = &internals->rx_queues[i];
894 if (rxq->umem == NULL)
896 xsk_socket__delete(rxq->xsk);
898 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE)
900 (void)xsk_umem__delete(rxq->umem->umem);
901 xdp_umem_destroy(rxq->umem);
904 /* free pkt_tx_queue */
910 * MAC is not allocated dynamically, setting it to NULL would prevent
911 * from releasing it in rte_eth_dev_release_port.
913 dev->data->mac_addrs = NULL;
915 remove_xdp_program(internals);
917 if (internals->shared_umem) {
918 struct internal_list *list;
920 /* Remove ethdev from list used to track and share UMEMs */
921 list = find_internal_resource(internals);
923 pthread_mutex_lock(&internal_list_lock);
924 TAILQ_REMOVE(&internal_list, list, next);
925 pthread_mutex_unlock(&internal_list_lock);
934 eth_link_update(struct rte_eth_dev *dev __rte_unused,
935 int wait_to_complete __rte_unused)
940 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
941 static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
943 struct rte_mempool_memhdr *memhdr;
944 uintptr_t memhdr_addr, aligned_addr;
946 memhdr = STAILQ_FIRST(&mp->mem_list);
947 memhdr_addr = (uintptr_t)memhdr->addr;
948 aligned_addr = memhdr_addr & ~(getpagesize() - 1);
949 *align = memhdr_addr - aligned_addr;
954 /* Check if the netdev,qid context already exists */
956 ctx_exists(struct pkt_rx_queue *rxq, const char *ifname,
957 struct pkt_rx_queue *list_rxq, const char *list_ifname)
961 if (rxq->xsk_queue_idx == list_rxq->xsk_queue_idx &&
962 !strncmp(ifname, list_ifname, IFNAMSIZ)) {
963 AF_XDP_LOG(ERR, "ctx %s,%i already exists, cannot share umem\n",
964 ifname, rxq->xsk_queue_idx);
971 /* Get a pointer to an existing UMEM which overlays the rxq's mb_pool */
973 get_shared_umem(struct pkt_rx_queue *rxq, const char *ifname,
974 struct xsk_umem_info **umem)
976 struct internal_list *list;
977 struct pmd_internals *internals;
979 struct rte_mempool *mb_pool = rxq->mb_pool;
984 pthread_mutex_lock(&internal_list_lock);
986 TAILQ_FOREACH(list, &internal_list, next) {
987 internals = list->eth_dev->data->dev_private;
988 for (i = 0; i < internals->queue_cnt; i++) {
989 struct pkt_rx_queue *list_rxq =
990 &internals->rx_queues[i];
993 if (mb_pool == internals->rx_queues[i].mb_pool) {
994 if (ctx_exists(rxq, ifname, list_rxq,
995 internals->if_name)) {
999 if (__atomic_load_n(&internals->rx_queues[i].umem->refcnt,
1000 __ATOMIC_ACQUIRE)) {
1001 *umem = internals->rx_queues[i].umem;
1009 pthread_mutex_unlock(&internal_list_lock);
1015 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1016 struct pkt_rx_queue *rxq)
1018 struct xsk_umem_info *umem = NULL;
1020 struct xsk_umem_config usr_config = {
1021 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
1022 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1023 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
1024 void *base_addr = NULL;
1025 struct rte_mempool *mb_pool = rxq->mb_pool;
1026 uint64_t umem_size, align = 0;
1028 if (internals->shared_umem) {
1029 if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
1033 __atomic_load_n(&umem->refcnt, __ATOMIC_ACQUIRE) <
1035 AF_XDP_LOG(INFO, "%s,qid%i sharing UMEM\n",
1036 internals->if_name, rxq->xsk_queue_idx);
1037 __atomic_fetch_add(&umem->refcnt, 1, __ATOMIC_ACQUIRE);
1042 usr_config.frame_size =
1043 rte_mempool_calc_obj_size(mb_pool->elt_size,
1044 mb_pool->flags, NULL);
1045 usr_config.frame_headroom = mb_pool->header_size +
1046 sizeof(struct rte_mbuf) +
1047 rte_pktmbuf_priv_size(mb_pool) +
1048 RTE_PKTMBUF_HEADROOM;
1050 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0,
1053 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1057 umem->mb_pool = mb_pool;
1058 base_addr = (void *)get_base_addr(mb_pool, &align);
1059 umem_size = (uint64_t)mb_pool->populated_size *
1060 (uint64_t)usr_config.frame_size +
1063 ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
1064 &rxq->fq, &rxq->cq, &usr_config);
1066 AF_XDP_LOG(ERR, "Failed to create umem");
1069 umem->buffer = base_addr;
1071 if (internals->shared_umem) {
1072 umem->max_xsks = mb_pool->populated_size /
1073 ETH_AF_XDP_NUM_BUFFERS;
1074 AF_XDP_LOG(INFO, "Max xsks for UMEM %s: %u\n",
1075 mb_pool->name, umem->max_xsks);
1078 __atomic_store_n(&umem->refcnt, 1, __ATOMIC_RELEASE);
1084 xdp_umem_destroy(umem);
1089 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1090 struct pkt_rx_queue *rxq)
1092 struct xsk_umem_info *umem;
1093 const struct rte_memzone *mz;
1094 struct xsk_umem_config usr_config = {
1095 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1096 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1097 .frame_size = ETH_AF_XDP_FRAME_SIZE,
1098 .frame_headroom = 0 };
1099 char ring_name[RTE_RING_NAMESIZE];
1100 char mz_name[RTE_MEMZONE_NAMESIZE];
1104 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
1106 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1110 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
1111 internals->if_name, rxq->xsk_queue_idx);
1112 umem->buf_ring = rte_ring_create(ring_name,
1113 ETH_AF_XDP_NUM_BUFFERS,
1116 if (umem->buf_ring == NULL) {
1117 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
1121 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
1122 rte_ring_enqueue(umem->buf_ring,
1123 (void *)(i * ETH_AF_XDP_FRAME_SIZE));
1125 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1126 internals->if_name, rxq->xsk_queue_idx);
1127 mz = rte_memzone_reserve_aligned(mz_name,
1128 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1129 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1132 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1136 ret = xsk_umem__create(&umem->umem, mz->addr,
1137 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1142 AF_XDP_LOG(ERR, "Failed to create umem");
1150 xdp_umem_destroy(umem);
1156 load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
1158 int ret, prog_fd = -1;
1159 struct bpf_object *obj;
1161 ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
1163 AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
1168 * The loaded program must provision for a map of xsks, such that some
1169 * traffic can be redirected to userspace.
1171 *map = bpf_object__find_map_by_name(obj, "xsks_map");
1173 AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
1177 /* Link the program with the given network device */
1178 ret = bpf_set_link_xdp_fd(if_index, prog_fd,
1179 XDP_FLAGS_UPDATE_IF_NOEXIST);
1181 AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
1186 AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
1187 prog_path, prog_fd);
1192 /* Detect support for busy polling through setsockopt(). */
1194 configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
1197 int fd = xsk_socket__fd(rxq->xsk);
1200 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1201 (void *)&sock_opt, sizeof(sock_opt));
1203 AF_XDP_LOG(DEBUG, "Failed to set SO_PREFER_BUSY_POLL\n");
1207 sock_opt = ETH_AF_XDP_DFLT_BUSY_TIMEOUT;
1208 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1211 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL\n");
1215 sock_opt = rxq->busy_budget;
1216 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1217 (void *)&sock_opt, sizeof(sock_opt));
1219 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL_BUDGET\n");
1221 AF_XDP_LOG(INFO, "Busy polling budget set to: %u\n",
1226 /* setsockopt failure - attempt to restore xsk to default state and
1227 * proceed without busy polling support.
1230 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1233 AF_XDP_LOG(ERR, "Failed to unset SO_BUSY_POLL\n");
1239 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1240 (void *)&sock_opt, sizeof(sock_opt));
1242 AF_XDP_LOG(ERR, "Failed to unset SO_PREFER_BUSY_POLL\n");
1247 rxq->busy_budget = 0;
1252 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1255 struct xsk_socket_config cfg;
1256 struct pkt_tx_queue *txq = rxq->pair;
1258 int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1259 struct rte_mbuf *fq_bufs[reserve_size];
1261 rxq->umem = xdp_umem_configure(internals, rxq);
1262 if (rxq->umem == NULL)
1264 txq->umem = rxq->umem;
1266 cfg.rx_size = ring_size;
1267 cfg.tx_size = ring_size;
1268 cfg.libbpf_flags = 0;
1269 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1272 #if defined(XDP_USE_NEED_WAKEUP)
1273 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1276 if (strnlen(internals->prog_path, PATH_MAX) &&
1277 !internals->custom_prog_configured) {
1278 ret = load_custom_xdp_prog(internals->prog_path,
1279 internals->if_index,
1282 AF_XDP_LOG(ERR, "Failed to load custom XDP program %s\n",
1283 internals->prog_path);
1286 internals->custom_prog_configured = 1;
1287 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
1290 if (internals->shared_umem)
1291 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1292 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1293 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1295 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1296 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1300 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1304 /* insert the xsk into the xsks_map */
1305 if (internals->custom_prog_configured) {
1308 fd = xsk_socket__fd(rxq->xsk);
1309 err = bpf_map_update_elem(bpf_map__fd(internals->map),
1310 &rxq->xsk_queue_idx, &fd, 0);
1312 AF_XDP_LOG(ERR, "Failed to insert xsk in map.\n");
1317 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1318 ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size);
1320 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1325 if (rxq->busy_budget) {
1326 ret = configure_preferred_busy_poll(rxq);
1328 AF_XDP_LOG(ERR, "Failed configure busy polling.\n");
1333 ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1335 xsk_socket__delete(rxq->xsk);
1336 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1343 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1344 xdp_umem_destroy(rxq->umem);
1350 eth_rx_queue_setup(struct rte_eth_dev *dev,
1351 uint16_t rx_queue_id,
1352 uint16_t nb_rx_desc,
1353 unsigned int socket_id __rte_unused,
1354 const struct rte_eth_rxconf *rx_conf __rte_unused,
1355 struct rte_mempool *mb_pool)
1357 struct pmd_internals *internals = dev->data->dev_private;
1358 struct pkt_rx_queue *rxq;
1361 rxq = &internals->rx_queues[rx_queue_id];
1363 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1364 rx_queue_id, rxq->xsk_queue_idx);
1366 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1367 uint32_t buf_size, data_size;
1369 /* Now get the space available for data in the mbuf */
1370 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1371 RTE_PKTMBUF_HEADROOM;
1372 data_size = ETH_AF_XDP_FRAME_SIZE;
1374 if (data_size > buf_size) {
1375 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1376 dev->device->name, data_size, buf_size);
1382 rxq->mb_pool = mb_pool;
1384 if (xsk_configure(internals, rxq, nb_rx_desc)) {
1385 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1390 if (!rxq->busy_budget)
1391 AF_XDP_LOG(DEBUG, "Preferred busy polling not enabled\n");
1393 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1394 rxq->fds[0].events = POLLIN;
1396 dev->data->rx_queues[rx_queue_id] = rxq;
1404 eth_tx_queue_setup(struct rte_eth_dev *dev,
1405 uint16_t tx_queue_id,
1406 uint16_t nb_tx_desc __rte_unused,
1407 unsigned int socket_id __rte_unused,
1408 const struct rte_eth_txconf *tx_conf __rte_unused)
1410 struct pmd_internals *internals = dev->data->dev_private;
1411 struct pkt_tx_queue *txq;
1413 txq = &internals->tx_queues[tx_queue_id];
1415 dev->data->tx_queues[tx_queue_id] = txq;
1420 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1422 struct pmd_internals *internals = dev->data->dev_private;
1423 struct ifreq ifr = { .ifr_mtu = mtu };
1427 s = socket(PF_INET, SOCK_DGRAM, 0);
1431 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1432 ret = ioctl(s, SIOCSIFMTU, &ifr);
1435 return (ret < 0) ? -errno : 0;
1439 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1445 s = socket(PF_INET, SOCK_DGRAM, 0);
1449 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1450 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1454 ifr.ifr_flags &= mask;
1455 ifr.ifr_flags |= flags;
1456 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1466 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1468 struct pmd_internals *internals = dev->data->dev_private;
1470 return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1474 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1476 struct pmd_internals *internals = dev->data->dev_private;
1478 return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1481 static const struct eth_dev_ops ops = {
1482 .dev_start = eth_dev_start,
1483 .dev_stop = eth_dev_stop,
1484 .dev_close = eth_dev_close,
1485 .dev_configure = eth_dev_configure,
1486 .dev_infos_get = eth_dev_info,
1487 .mtu_set = eth_dev_mtu_set,
1488 .promiscuous_enable = eth_dev_promiscuous_enable,
1489 .promiscuous_disable = eth_dev_promiscuous_disable,
1490 .rx_queue_setup = eth_rx_queue_setup,
1491 .tx_queue_setup = eth_tx_queue_setup,
1492 .link_update = eth_link_update,
1493 .stats_get = eth_stats_get,
1494 .stats_reset = eth_stats_reset,
1495 .get_monitor_addr = eth_get_monitor_addr,
1498 /** parse busy_budget argument */
1500 parse_budget_arg(const char *key __rte_unused,
1501 const char *value, void *extra_args)
1503 int *i = (int *)extra_args;
1506 *i = strtol(value, &end, 10);
1507 if (*i < 0 || *i > UINT16_MAX) {
1508 AF_XDP_LOG(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u\n",
1516 /** parse integer from integer argument */
1518 parse_integer_arg(const char *key __rte_unused,
1519 const char *value, void *extra_args)
1521 int *i = (int *)extra_args;
1524 *i = strtol(value, &end, 10);
1526 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1533 /** parse name argument */
1535 parse_name_arg(const char *key __rte_unused,
1536 const char *value, void *extra_args)
1538 char *name = extra_args;
1540 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1541 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1546 strlcpy(name, value, IFNAMSIZ);
1551 /** parse xdp prog argument */
1553 parse_prog_arg(const char *key __rte_unused,
1554 const char *value, void *extra_args)
1556 char *path = extra_args;
1558 if (strnlen(value, PATH_MAX) == PATH_MAX) {
1559 AF_XDP_LOG(ERR, "Invalid path %s, should be less than %u bytes.\n",
1564 if (access(value, F_OK) != 0) {
1565 AF_XDP_LOG(ERR, "Error accessing %s: %s\n",
1566 value, strerror(errno));
1570 strlcpy(path, value, PATH_MAX);
1576 xdp_get_channels_info(const char *if_name, int *max_queues,
1577 int *combined_queues)
1579 struct ethtool_channels channels;
1583 fd = socket(AF_INET, SOCK_DGRAM, 0);
1587 channels.cmd = ETHTOOL_GCHANNELS;
1588 ifr.ifr_data = (void *)&channels;
1589 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1590 ret = ioctl(fd, SIOCETHTOOL, &ifr);
1592 if (errno == EOPNOTSUPP) {
1600 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1601 /* If the device says it has no channels, then all traffic
1602 * is sent to a single stream, so max queues = 1.
1605 *combined_queues = 1;
1607 *max_queues = channels.max_combined;
1608 *combined_queues = channels.combined_count;
1617 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1618 int *queue_cnt, int *shared_umem, char *prog_path,
1623 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1624 &parse_name_arg, if_name);
1628 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1629 &parse_integer_arg, start_queue);
1633 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1634 &parse_integer_arg, queue_cnt);
1635 if (ret < 0 || *queue_cnt <= 0) {
1640 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1641 &parse_integer_arg, shared_umem);
1645 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
1646 &parse_prog_arg, prog_path);
1650 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
1651 &parse_budget_arg, busy_budget);
1656 rte_kvargs_free(kvlist);
1661 get_iface_info(const char *if_name,
1662 struct rte_ether_addr *eth_addr,
1666 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1671 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1672 if (ioctl(sock, SIOCGIFINDEX, &ifr))
1675 *if_index = ifr.ifr_ifindex;
1677 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1680 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1690 static struct rte_eth_dev *
1691 init_internals(struct rte_vdev_device *dev, const char *if_name,
1692 int start_queue_idx, int queue_cnt, int shared_umem,
1693 const char *prog_path, int busy_budget)
1695 const char *name = rte_vdev_device_name(dev);
1696 const unsigned int numa_node = dev->device.numa_node;
1697 struct pmd_internals *internals;
1698 struct rte_eth_dev *eth_dev;
1702 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1703 if (internals == NULL)
1706 internals->start_queue_idx = start_queue_idx;
1707 internals->queue_cnt = queue_cnt;
1708 strlcpy(internals->if_name, if_name, IFNAMSIZ);
1709 strlcpy(internals->prog_path, prog_path, PATH_MAX);
1710 internals->custom_prog_configured = 0;
1712 #ifndef ETH_AF_XDP_SHARED_UMEM
1714 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1715 "Check kernel and libbpf version\n");
1716 goto err_free_internals;
1719 internals->shared_umem = shared_umem;
1721 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1722 &internals->combined_queue_cnt)) {
1723 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1725 goto err_free_internals;
1728 if (queue_cnt > internals->combined_queue_cnt) {
1729 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1730 queue_cnt, internals->combined_queue_cnt);
1731 goto err_free_internals;
1734 internals->rx_queues = rte_zmalloc_socket(NULL,
1735 sizeof(struct pkt_rx_queue) * queue_cnt,
1737 if (internals->rx_queues == NULL) {
1738 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1739 goto err_free_internals;
1742 internals->tx_queues = rte_zmalloc_socket(NULL,
1743 sizeof(struct pkt_tx_queue) * queue_cnt,
1745 if (internals->tx_queues == NULL) {
1746 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1749 for (i = 0; i < queue_cnt; i++) {
1750 internals->tx_queues[i].pair = &internals->rx_queues[i];
1751 internals->rx_queues[i].pair = &internals->tx_queues[i];
1752 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1753 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1754 internals->rx_queues[i].busy_budget = busy_budget;
1757 ret = get_iface_info(if_name, &internals->eth_addr,
1758 &internals->if_index);
1762 eth_dev = rte_eth_vdev_allocate(dev, 0);
1763 if (eth_dev == NULL)
1766 eth_dev->data->dev_private = internals;
1767 eth_dev->data->dev_link = pmd_link;
1768 eth_dev->data->mac_addrs = &internals->eth_addr;
1769 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1770 eth_dev->dev_ops = &ops;
1771 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1772 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1774 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1775 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1781 rte_free(internals->tx_queues);
1783 rte_free(internals->rx_queues);
1785 rte_free(internals);
1790 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1792 struct rte_kvargs *kvlist;
1793 char if_name[IFNAMSIZ] = {'\0'};
1794 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1795 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1796 int shared_umem = 0;
1797 char prog_path[PATH_MAX] = {'\0'};
1798 int busy_budget = -1;
1799 struct rte_eth_dev *eth_dev = NULL;
1802 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1803 rte_vdev_device_name(dev));
1805 name = rte_vdev_device_name(dev);
1806 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1807 AF_XDP_LOG(ERR, "Failed to probe %s. "
1808 "AF_XDP PMD does not support secondary processes.\n",
1813 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1814 if (kvlist == NULL) {
1815 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1819 if (dev->device.numa_node == SOCKET_ID_ANY)
1820 dev->device.numa_node = rte_socket_id();
1822 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1823 &xsk_queue_cnt, &shared_umem, prog_path,
1824 &busy_budget) < 0) {
1825 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1829 if (strlen(if_name) == 0) {
1830 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1834 busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
1837 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1838 xsk_queue_cnt, shared_umem, prog_path,
1840 if (eth_dev == NULL) {
1841 AF_XDP_LOG(ERR, "Failed to init internals\n");
1845 rte_eth_dev_probing_finish(eth_dev);
1851 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1853 struct rte_eth_dev *eth_dev = NULL;
1855 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1861 /* find the ethdev entry */
1862 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1863 if (eth_dev == NULL)
1866 eth_dev_close(eth_dev);
1867 rte_eth_dev_release_port(eth_dev);
1873 static struct rte_vdev_driver pmd_af_xdp_drv = {
1874 .probe = rte_pmd_af_xdp_probe,
1875 .remove = rte_pmd_af_xdp_remove,
1878 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1879 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1881 "start_queue=<int> "
1882 "queue_count=<int> "
1883 "shared_umem=<int> "
1884 "xdp_prog=<string> "
1885 "busy_budget=<int>");