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);
1083 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1084 struct pkt_rx_queue *rxq)
1086 struct xsk_umem_info *umem;
1087 const struct rte_memzone *mz;
1088 struct xsk_umem_config usr_config = {
1089 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1090 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1091 .frame_size = ETH_AF_XDP_FRAME_SIZE,
1092 .frame_headroom = 0 };
1093 char ring_name[RTE_RING_NAMESIZE];
1094 char mz_name[RTE_MEMZONE_NAMESIZE];
1098 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
1100 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1104 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
1105 internals->if_name, rxq->xsk_queue_idx);
1106 umem->buf_ring = rte_ring_create(ring_name,
1107 ETH_AF_XDP_NUM_BUFFERS,
1110 if (umem->buf_ring == NULL) {
1111 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
1115 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
1116 rte_ring_enqueue(umem->buf_ring,
1117 (void *)(i * ETH_AF_XDP_FRAME_SIZE));
1119 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1120 internals->if_name, rxq->xsk_queue_idx);
1121 mz = rte_memzone_reserve_aligned(mz_name,
1122 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1123 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1126 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1130 ret = xsk_umem__create(&umem->umem, mz->addr,
1131 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1136 AF_XDP_LOG(ERR, "Failed to create umem");
1145 xdp_umem_destroy(umem);
1150 load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
1152 int ret, prog_fd = -1;
1153 struct bpf_object *obj;
1155 ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
1157 AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
1162 * The loaded program must provision for a map of xsks, such that some
1163 * traffic can be redirected to userspace.
1165 *map = bpf_object__find_map_by_name(obj, "xsks_map");
1167 AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
1171 /* Link the program with the given network device */
1172 ret = bpf_set_link_xdp_fd(if_index, prog_fd,
1173 XDP_FLAGS_UPDATE_IF_NOEXIST);
1175 AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
1180 AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
1181 prog_path, prog_fd);
1186 /* Detect support for busy polling through setsockopt(). */
1188 configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
1191 int fd = xsk_socket__fd(rxq->xsk);
1194 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1195 (void *)&sock_opt, sizeof(sock_opt));
1197 AF_XDP_LOG(DEBUG, "Failed to set SO_PREFER_BUSY_POLL\n");
1201 sock_opt = ETH_AF_XDP_DFLT_BUSY_TIMEOUT;
1202 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1205 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL\n");
1209 sock_opt = rxq->busy_budget;
1210 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1211 (void *)&sock_opt, sizeof(sock_opt));
1213 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL_BUDGET\n");
1215 AF_XDP_LOG(INFO, "Busy polling budget set to: %u\n",
1220 /* setsockopt failure - attempt to restore xsk to default state and
1221 * proceed without busy polling support.
1224 ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1227 AF_XDP_LOG(ERR, "Failed to unset SO_BUSY_POLL\n");
1233 ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1234 (void *)&sock_opt, sizeof(sock_opt));
1236 AF_XDP_LOG(ERR, "Failed to unset SO_PREFER_BUSY_POLL\n");
1241 rxq->busy_budget = 0;
1246 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1249 struct xsk_socket_config cfg;
1250 struct pkt_tx_queue *txq = rxq->pair;
1252 int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1253 struct rte_mbuf *fq_bufs[reserve_size];
1255 rxq->umem = xdp_umem_configure(internals, rxq);
1256 if (rxq->umem == NULL)
1258 txq->umem = rxq->umem;
1260 cfg.rx_size = ring_size;
1261 cfg.tx_size = ring_size;
1262 cfg.libbpf_flags = 0;
1263 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1266 #if defined(XDP_USE_NEED_WAKEUP)
1267 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1270 if (strnlen(internals->prog_path, PATH_MAX) &&
1271 !internals->custom_prog_configured) {
1272 ret = load_custom_xdp_prog(internals->prog_path,
1273 internals->if_index,
1276 AF_XDP_LOG(ERR, "Failed to load custom XDP program %s\n",
1277 internals->prog_path);
1280 internals->custom_prog_configured = 1;
1281 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
1284 if (internals->shared_umem)
1285 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1286 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1287 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1289 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1290 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1294 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1298 /* insert the xsk into the xsks_map */
1299 if (internals->custom_prog_configured) {
1302 fd = xsk_socket__fd(rxq->xsk);
1303 err = bpf_map_update_elem(bpf_map__fd(internals->map),
1304 &rxq->xsk_queue_idx, &fd, 0);
1306 AF_XDP_LOG(ERR, "Failed to insert xsk in map.\n");
1311 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1312 ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size);
1314 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1319 if (rxq->busy_budget) {
1320 ret = configure_preferred_busy_poll(rxq);
1322 AF_XDP_LOG(ERR, "Failed configure busy polling.\n");
1327 ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1329 xsk_socket__delete(rxq->xsk);
1330 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1337 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1338 xdp_umem_destroy(rxq->umem);
1344 eth_rx_queue_setup(struct rte_eth_dev *dev,
1345 uint16_t rx_queue_id,
1346 uint16_t nb_rx_desc,
1347 unsigned int socket_id __rte_unused,
1348 const struct rte_eth_rxconf *rx_conf __rte_unused,
1349 struct rte_mempool *mb_pool)
1351 struct pmd_internals *internals = dev->data->dev_private;
1352 struct pkt_rx_queue *rxq;
1355 rxq = &internals->rx_queues[rx_queue_id];
1357 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1358 rx_queue_id, rxq->xsk_queue_idx);
1360 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1361 uint32_t buf_size, data_size;
1363 /* Now get the space available for data in the mbuf */
1364 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1365 RTE_PKTMBUF_HEADROOM;
1366 data_size = ETH_AF_XDP_FRAME_SIZE;
1368 if (data_size > buf_size) {
1369 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1370 dev->device->name, data_size, buf_size);
1376 rxq->mb_pool = mb_pool;
1378 if (xsk_configure(internals, rxq, nb_rx_desc)) {
1379 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1384 if (!rxq->busy_budget)
1385 AF_XDP_LOG(DEBUG, "Preferred busy polling not enabled\n");
1387 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1388 rxq->fds[0].events = POLLIN;
1390 dev->data->rx_queues[rx_queue_id] = rxq;
1398 eth_tx_queue_setup(struct rte_eth_dev *dev,
1399 uint16_t tx_queue_id,
1400 uint16_t nb_tx_desc __rte_unused,
1401 unsigned int socket_id __rte_unused,
1402 const struct rte_eth_txconf *tx_conf __rte_unused)
1404 struct pmd_internals *internals = dev->data->dev_private;
1405 struct pkt_tx_queue *txq;
1407 txq = &internals->tx_queues[tx_queue_id];
1409 dev->data->tx_queues[tx_queue_id] = txq;
1414 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1416 struct pmd_internals *internals = dev->data->dev_private;
1417 struct ifreq ifr = { .ifr_mtu = mtu };
1421 s = socket(PF_INET, SOCK_DGRAM, 0);
1425 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1426 ret = ioctl(s, SIOCSIFMTU, &ifr);
1429 return (ret < 0) ? -errno : 0;
1433 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1439 s = socket(PF_INET, SOCK_DGRAM, 0);
1443 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1444 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1448 ifr.ifr_flags &= mask;
1449 ifr.ifr_flags |= flags;
1450 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1460 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1462 struct pmd_internals *internals = dev->data->dev_private;
1464 return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1468 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1470 struct pmd_internals *internals = dev->data->dev_private;
1472 return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1475 static const struct eth_dev_ops ops = {
1476 .dev_start = eth_dev_start,
1477 .dev_stop = eth_dev_stop,
1478 .dev_close = eth_dev_close,
1479 .dev_configure = eth_dev_configure,
1480 .dev_infos_get = eth_dev_info,
1481 .mtu_set = eth_dev_mtu_set,
1482 .promiscuous_enable = eth_dev_promiscuous_enable,
1483 .promiscuous_disable = eth_dev_promiscuous_disable,
1484 .rx_queue_setup = eth_rx_queue_setup,
1485 .tx_queue_setup = eth_tx_queue_setup,
1486 .link_update = eth_link_update,
1487 .stats_get = eth_stats_get,
1488 .stats_reset = eth_stats_reset,
1489 .get_monitor_addr = eth_get_monitor_addr,
1492 /** parse busy_budget argument */
1494 parse_budget_arg(const char *key __rte_unused,
1495 const char *value, void *extra_args)
1497 int *i = (int *)extra_args;
1500 *i = strtol(value, &end, 10);
1501 if (*i < 0 || *i > UINT16_MAX) {
1502 AF_XDP_LOG(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u\n",
1510 /** parse integer from integer argument */
1512 parse_integer_arg(const char *key __rte_unused,
1513 const char *value, void *extra_args)
1515 int *i = (int *)extra_args;
1518 *i = strtol(value, &end, 10);
1520 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1527 /** parse name argument */
1529 parse_name_arg(const char *key __rte_unused,
1530 const char *value, void *extra_args)
1532 char *name = extra_args;
1534 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1535 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1540 strlcpy(name, value, IFNAMSIZ);
1545 /** parse xdp prog argument */
1547 parse_prog_arg(const char *key __rte_unused,
1548 const char *value, void *extra_args)
1550 char *path = extra_args;
1552 if (strnlen(value, PATH_MAX) == PATH_MAX) {
1553 AF_XDP_LOG(ERR, "Invalid path %s, should be less than %u bytes.\n",
1558 if (access(value, F_OK) != 0) {
1559 AF_XDP_LOG(ERR, "Error accessing %s: %s\n",
1560 value, strerror(errno));
1564 strlcpy(path, value, PATH_MAX);
1570 xdp_get_channels_info(const char *if_name, int *max_queues,
1571 int *combined_queues)
1573 struct ethtool_channels channels;
1577 fd = socket(AF_INET, SOCK_DGRAM, 0);
1581 channels.cmd = ETHTOOL_GCHANNELS;
1582 ifr.ifr_data = (void *)&channels;
1583 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1584 ret = ioctl(fd, SIOCETHTOOL, &ifr);
1586 if (errno == EOPNOTSUPP) {
1594 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1595 /* If the device says it has no channels, then all traffic
1596 * is sent to a single stream, so max queues = 1.
1599 *combined_queues = 1;
1601 *max_queues = channels.max_combined;
1602 *combined_queues = channels.combined_count;
1611 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1612 int *queue_cnt, int *shared_umem, char *prog_path,
1617 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1618 &parse_name_arg, if_name);
1622 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1623 &parse_integer_arg, start_queue);
1627 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1628 &parse_integer_arg, queue_cnt);
1629 if (ret < 0 || *queue_cnt <= 0) {
1634 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1635 &parse_integer_arg, shared_umem);
1639 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
1640 &parse_prog_arg, prog_path);
1644 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
1645 &parse_budget_arg, busy_budget);
1650 rte_kvargs_free(kvlist);
1655 get_iface_info(const char *if_name,
1656 struct rte_ether_addr *eth_addr,
1660 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1665 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1666 if (ioctl(sock, SIOCGIFINDEX, &ifr))
1669 *if_index = ifr.ifr_ifindex;
1671 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1674 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1684 static struct rte_eth_dev *
1685 init_internals(struct rte_vdev_device *dev, const char *if_name,
1686 int start_queue_idx, int queue_cnt, int shared_umem,
1687 const char *prog_path, int busy_budget)
1689 const char *name = rte_vdev_device_name(dev);
1690 const unsigned int numa_node = dev->device.numa_node;
1691 struct pmd_internals *internals;
1692 struct rte_eth_dev *eth_dev;
1696 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1697 if (internals == NULL)
1700 internals->start_queue_idx = start_queue_idx;
1701 internals->queue_cnt = queue_cnt;
1702 strlcpy(internals->if_name, if_name, IFNAMSIZ);
1703 strlcpy(internals->prog_path, prog_path, PATH_MAX);
1704 internals->custom_prog_configured = 0;
1706 #ifndef ETH_AF_XDP_SHARED_UMEM
1708 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1709 "Check kernel and libbpf version\n");
1710 goto err_free_internals;
1713 internals->shared_umem = shared_umem;
1715 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1716 &internals->combined_queue_cnt)) {
1717 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1719 goto err_free_internals;
1722 if (queue_cnt > internals->combined_queue_cnt) {
1723 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1724 queue_cnt, internals->combined_queue_cnt);
1725 goto err_free_internals;
1728 internals->rx_queues = rte_zmalloc_socket(NULL,
1729 sizeof(struct pkt_rx_queue) * queue_cnt,
1731 if (internals->rx_queues == NULL) {
1732 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1733 goto err_free_internals;
1736 internals->tx_queues = rte_zmalloc_socket(NULL,
1737 sizeof(struct pkt_tx_queue) * queue_cnt,
1739 if (internals->tx_queues == NULL) {
1740 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1743 for (i = 0; i < queue_cnt; i++) {
1744 internals->tx_queues[i].pair = &internals->rx_queues[i];
1745 internals->rx_queues[i].pair = &internals->tx_queues[i];
1746 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1747 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1748 internals->rx_queues[i].busy_budget = busy_budget;
1751 ret = get_iface_info(if_name, &internals->eth_addr,
1752 &internals->if_index);
1756 eth_dev = rte_eth_vdev_allocate(dev, 0);
1757 if (eth_dev == NULL)
1760 eth_dev->data->dev_private = internals;
1761 eth_dev->data->dev_link = pmd_link;
1762 eth_dev->data->mac_addrs = &internals->eth_addr;
1763 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1764 eth_dev->dev_ops = &ops;
1765 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1766 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1768 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1769 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1775 rte_free(internals->tx_queues);
1777 rte_free(internals->rx_queues);
1779 rte_free(internals);
1784 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1786 struct rte_kvargs *kvlist;
1787 char if_name[IFNAMSIZ] = {'\0'};
1788 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1789 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1790 int shared_umem = 0;
1791 char prog_path[PATH_MAX] = {'\0'};
1792 int busy_budget = -1;
1793 struct rte_eth_dev *eth_dev = NULL;
1796 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1797 rte_vdev_device_name(dev));
1799 name = rte_vdev_device_name(dev);
1800 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1801 AF_XDP_LOG(ERR, "Failed to probe %s. "
1802 "AF_XDP PMD does not support secondary processes.\n",
1807 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1808 if (kvlist == NULL) {
1809 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1813 if (dev->device.numa_node == SOCKET_ID_ANY)
1814 dev->device.numa_node = rte_socket_id();
1816 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1817 &xsk_queue_cnt, &shared_umem, prog_path,
1818 &busy_budget) < 0) {
1819 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1823 if (strlen(if_name) == 0) {
1824 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1828 busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
1831 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1832 xsk_queue_cnt, shared_umem, prog_path,
1834 if (eth_dev == NULL) {
1835 AF_XDP_LOG(ERR, "Failed to init internals\n");
1839 rte_eth_dev_probing_finish(eth_dev);
1845 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1847 struct rte_eth_dev *eth_dev = NULL;
1849 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1855 /* find the ethdev entry */
1856 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1857 if (eth_dev == NULL)
1860 eth_dev_close(eth_dev);
1861 rte_eth_dev_release_port(eth_dev);
1867 static struct rte_vdev_driver pmd_af_xdp_drv = {
1868 .probe = rte_pmd_af_xdp_probe,
1869 .remove = rte_pmd_af_xdp_remove,
1872 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1873 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1875 "start_queue=<int> "
1876 "queue_count=<int> "
1877 "shared_umem=<int> "
1878 "xdp_prog=<string> "
1879 "busy_budget=<int>");