1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 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 <rte_ethdev_driver.h>
23 #include <rte_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>
29 #include <rte_config.h>
32 #include <rte_ether.h>
33 #include <rte_lcore.h>
35 #include <rte_memory.h>
36 #include <rte_memzone.h>
38 #include <rte_malloc.h>
53 static int af_xdp_logtype;
55 #define AF_XDP_LOG(level, fmt, args...) \
56 rte_log(RTE_LOG_ ## level, af_xdp_logtype, \
57 "%s(): " fmt, __func__, ##args)
59 #define ETH_AF_XDP_FRAME_SIZE 2048
60 #define ETH_AF_XDP_NUM_BUFFERS 4096
61 #define ETH_AF_XDP_DATA_HEADROOM 0
62 #define ETH_AF_XDP_DFLT_NUM_DESCS XSK_RING_CONS__DEFAULT_NUM_DESCS
63 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
64 #define ETH_AF_XDP_DFLT_QUEUE_COUNT 1
66 #define ETH_AF_XDP_RX_BATCH_SIZE 32
67 #define ETH_AF_XDP_TX_BATCH_SIZE 32
70 struct xsk_umem_info {
71 struct xsk_ring_prod fq;
72 struct xsk_ring_cons cq;
73 struct xsk_umem *umem;
74 struct rte_ring *buf_ring;
75 const struct rte_memzone *mz;
86 struct xsk_ring_cons rx;
87 struct xsk_umem_info *umem;
88 struct xsk_socket *xsk;
89 struct rte_mempool *mb_pool;
91 struct rx_stats stats;
93 struct pkt_tx_queue *pair;
103 struct pkt_tx_queue {
104 struct xsk_ring_prod tx;
106 struct tx_stats stats;
108 struct pkt_rx_queue *pair;
112 struct pmd_internals {
114 char if_name[IFNAMSIZ];
118 int combined_queue_cnt;
121 struct rte_ether_addr eth_addr;
123 struct pkt_rx_queue *rx_queues;
124 struct pkt_tx_queue *tx_queues;
127 #define ETH_AF_XDP_IFACE_ARG "iface"
128 #define ETH_AF_XDP_START_QUEUE_ARG "start_queue"
129 #define ETH_AF_XDP_QUEUE_COUNT_ARG "queue_count"
130 #define ETH_AF_XDP_PMD_ZC_ARG "pmd_zero_copy"
132 static const char * const valid_arguments[] = {
133 ETH_AF_XDP_IFACE_ARG,
134 ETH_AF_XDP_START_QUEUE_ARG,
135 ETH_AF_XDP_QUEUE_COUNT_ARG,
136 ETH_AF_XDP_PMD_ZC_ARG,
140 static const struct rte_eth_link pmd_link = {
141 .link_speed = ETH_SPEED_NUM_10G,
142 .link_duplex = ETH_LINK_FULL_DUPLEX,
143 .link_status = ETH_LINK_DOWN,
144 .link_autoneg = ETH_LINK_AUTONEG
148 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size)
150 struct xsk_ring_prod *fq = &umem->fq;
151 void *addrs[reserve_size];
155 if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
157 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
161 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
162 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
163 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
168 for (i = 0; i < reserve_size; i++) {
171 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
172 *fq_addr = (uint64_t)addrs[i];
175 xsk_ring_prod__submit(fq, reserve_size);
181 umem_buf_release_to_fq(void *addr, void *opaque)
183 struct xsk_umem_info *umem = (struct xsk_umem_info *)opaque;
184 uint64_t umem_addr = (uint64_t)addr - umem->mz->addr_64;
186 rte_ring_enqueue(umem->buf_ring, (void *)umem_addr);
190 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
192 struct pkt_rx_queue *rxq = queue;
193 struct xsk_ring_cons *rx = &rxq->rx;
194 struct xsk_umem_info *umem = rxq->umem;
195 struct xsk_ring_prod *fq = &umem->fq;
197 uint32_t free_thresh = fq->size >> 1;
198 int pmd_zc = umem->pmd_zc;
199 struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
200 unsigned long dropped = 0;
201 unsigned long rx_bytes = 0;
204 nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
206 if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
209 rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
211 #if defined(XDP_USE_NEED_WAKEUP)
212 if (xsk_ring_prod__needs_wakeup(fq))
213 (void)poll(rxq->fds, 1, 1000);
219 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
220 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
222 for (i = 0; i < rcvd; i++) {
223 const struct xdp_desc *desc;
227 uint16_t buf_len = ETH_AF_XDP_FRAME_SIZE;
228 struct rte_mbuf_ext_shared_info *shinfo;
230 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
233 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
236 shinfo = rte_pktmbuf_ext_shinfo_init_helper(pkt,
237 &buf_len, umem_buf_release_to_fq, umem);
239 rte_pktmbuf_attach_extbuf(mbufs[i], pkt, 0, buf_len,
242 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *),
244 rte_ring_enqueue(umem->buf_ring, (void *)addr);
246 rte_pktmbuf_pkt_len(mbufs[i]) = len;
247 rte_pktmbuf_data_len(mbufs[i]) = len;
252 xsk_ring_cons__release(rx, rcvd);
255 rxq->stats.rx_pkts += (rcvd - dropped);
256 rxq->stats.rx_bytes += rx_bytes;
260 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
267 pull_umem_cq(struct xsk_umem_info *umem, int size)
269 struct xsk_ring_cons *cq = &umem->cq;
273 n = xsk_ring_cons__peek(cq, size, &idx_cq);
275 for (i = 0; i < n; i++) {
277 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
278 rte_ring_enqueue(umem->buf_ring, (void *)addr);
281 xsk_ring_cons__release(cq, n);
285 kick_tx(struct pkt_tx_queue *txq)
287 struct xsk_umem_info *umem = txq->pair->umem;
289 #if defined(XDP_USE_NEED_WAKEUP)
290 if (xsk_ring_prod__needs_wakeup(&txq->tx))
292 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
293 0, MSG_DONTWAIT) < 0) {
294 /* some thing unexpected */
295 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
298 /* pull from completion queue to leave more space */
300 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
302 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
306 in_umem_range(struct xsk_umem_info *umem, uint64_t addr)
308 uint64_t mz_base_addr = umem->mz->addr_64;
310 return addr >= mz_base_addr && addr < mz_base_addr + umem->mz->len;
314 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
316 struct pkt_tx_queue *txq = queue;
317 struct xsk_umem_info *umem = txq->pair->umem;
318 struct rte_mbuf *mbuf;
319 int pmd_zc = umem->pmd_zc;
320 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
321 unsigned long tx_bytes = 0;
325 nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
327 pull_umem_cq(umem, nb_pkts);
329 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
334 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
336 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
340 for (i = 0; i < nb_pkts; i++) {
341 struct xdp_desc *desc;
344 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
346 desc->len = mbuf->pkt_len;
349 * We need to make sure the external mbuf address is within
350 * current port's umem memzone range
352 if (pmd_zc && RTE_MBUF_HAS_EXTBUF(mbuf) &&
353 in_umem_range(umem, (uint64_t)mbuf->buf_addr)) {
354 desc->addr = (uint64_t)mbuf->buf_addr -
356 mbuf->buf_addr = xsk_umem__get_data(umem->mz->addr,
359 desc->addr = (uint64_t)addrs[i];
360 pkt = xsk_umem__get_data(umem->mz->addr,
362 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
365 tx_bytes += mbuf->pkt_len;
368 xsk_ring_prod__submit(&txq->tx, nb_pkts);
372 txq->stats.tx_pkts += nb_pkts;
373 txq->stats.tx_bytes += tx_bytes;
375 for (i = 0; i < nb_pkts; i++)
376 rte_pktmbuf_free(bufs[i]);
382 eth_dev_start(struct rte_eth_dev *dev)
384 dev->data->dev_link.link_status = ETH_LINK_UP;
389 /* This function gets called when the current port gets stopped. */
391 eth_dev_stop(struct rte_eth_dev *dev)
393 dev->data->dev_link.link_status = ETH_LINK_DOWN;
397 eth_dev_configure(struct rte_eth_dev *dev)
399 /* rx/tx must be paired */
400 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
407 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
409 struct pmd_internals *internals = dev->data->dev_private;
411 dev_info->if_index = internals->if_index;
412 dev_info->max_mac_addrs = 1;
413 dev_info->max_rx_pktlen = ETH_FRAME_LEN;
414 dev_info->max_rx_queues = internals->queue_cnt;
415 dev_info->max_tx_queues = internals->queue_cnt;
417 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
418 dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
420 dev_info->default_rxportconf.nb_queues = 1;
421 dev_info->default_txportconf.nb_queues = 1;
422 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
423 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
427 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
429 struct pmd_internals *internals = dev->data->dev_private;
430 struct xdp_statistics xdp_stats;
431 struct pkt_rx_queue *rxq;
432 struct pkt_tx_queue *txq;
436 for (i = 0; i < dev->data->nb_rx_queues; i++) {
437 optlen = sizeof(struct xdp_statistics);
438 rxq = &internals->rx_queues[i];
440 stats->q_ipackets[i] = rxq->stats.rx_pkts;
441 stats->q_ibytes[i] = rxq->stats.rx_bytes;
443 stats->q_opackets[i] = txq->stats.tx_pkts;
444 stats->q_obytes[i] = txq->stats.tx_bytes;
446 stats->ipackets += stats->q_ipackets[i];
447 stats->ibytes += stats->q_ibytes[i];
448 stats->imissed += rxq->stats.rx_dropped;
449 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
450 XDP_STATISTICS, &xdp_stats, &optlen);
452 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
455 stats->imissed += xdp_stats.rx_dropped;
457 stats->opackets += stats->q_opackets[i];
458 stats->obytes += stats->q_obytes[i];
465 eth_stats_reset(struct rte_eth_dev *dev)
467 struct pmd_internals *internals = dev->data->dev_private;
470 for (i = 0; i < internals->queue_cnt; i++) {
471 memset(&internals->rx_queues[i].stats, 0,
472 sizeof(struct rx_stats));
473 memset(&internals->tx_queues[i].stats, 0,
474 sizeof(struct tx_stats));
479 remove_xdp_program(struct pmd_internals *internals)
481 uint32_t curr_prog_id = 0;
483 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
484 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
485 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
488 bpf_set_link_xdp_fd(internals->if_index, -1,
489 XDP_FLAGS_UPDATE_IF_NOEXIST);
493 xdp_umem_destroy(struct xsk_umem_info *umem)
495 rte_memzone_free(umem->mz);
498 rte_ring_free(umem->buf_ring);
499 umem->buf_ring = NULL;
506 eth_dev_close(struct rte_eth_dev *dev)
508 struct pmd_internals *internals = dev->data->dev_private;
509 struct pkt_rx_queue *rxq;
512 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
515 for (i = 0; i < internals->queue_cnt; i++) {
516 rxq = &internals->rx_queues[i];
517 if (rxq->umem == NULL)
519 xsk_socket__delete(rxq->xsk);
520 (void)xsk_umem__delete(rxq->umem->umem);
521 xdp_umem_destroy(rxq->umem);
523 /* free pkt_tx_queue */
529 * MAC is not allocated dynamically, setting it to NULL would prevent
530 * from releasing it in rte_eth_dev_release_port.
532 dev->data->mac_addrs = NULL;
534 remove_xdp_program(internals);
538 eth_queue_release(void *q __rte_unused)
543 eth_link_update(struct rte_eth_dev *dev __rte_unused,
544 int wait_to_complete __rte_unused)
550 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
551 struct pkt_rx_queue *rxq)
553 struct xsk_umem_info *umem;
554 const struct rte_memzone *mz;
555 struct xsk_umem_config usr_config = {
556 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
557 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
558 .frame_size = ETH_AF_XDP_FRAME_SIZE,
559 .frame_headroom = ETH_AF_XDP_DATA_HEADROOM };
560 char ring_name[RTE_RING_NAMESIZE];
561 char mz_name[RTE_MEMZONE_NAMESIZE];
565 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
567 AF_XDP_LOG(ERR, "Failed to allocate umem info");
571 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
572 internals->if_name, rxq->xsk_queue_idx);
573 umem->buf_ring = rte_ring_create(ring_name,
574 ETH_AF_XDP_NUM_BUFFERS,
577 if (umem->buf_ring == NULL) {
578 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
582 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
583 rte_ring_enqueue(umem->buf_ring,
584 (void *)(i * ETH_AF_XDP_FRAME_SIZE +
585 ETH_AF_XDP_DATA_HEADROOM));
587 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
588 internals->if_name, rxq->xsk_queue_idx);
589 mz = rte_memzone_reserve_aligned(mz_name,
590 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
591 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
594 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
598 ret = xsk_umem__create(&umem->umem, mz->addr,
599 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
600 &umem->fq, &umem->cq,
604 AF_XDP_LOG(ERR, "Failed to create umem");
612 xdp_umem_destroy(umem);
617 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
620 struct xsk_socket_config cfg;
621 struct pkt_tx_queue *txq = rxq->pair;
625 rxq->umem = xdp_umem_configure(internals, rxq);
626 if (rxq->umem == NULL)
629 cfg.rx_size = ring_size;
630 cfg.tx_size = ring_size;
631 cfg.libbpf_flags = 0;
632 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
635 #if defined(XDP_USE_NEED_WAKEUP)
636 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
639 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
640 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
643 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
647 reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
648 ret = reserve_fill_queue(rxq->umem, reserve_size);
650 xsk_socket__delete(rxq->xsk);
651 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
658 xdp_umem_destroy(rxq->umem);
664 eth_rx_queue_setup(struct rte_eth_dev *dev,
665 uint16_t rx_queue_id,
667 unsigned int socket_id __rte_unused,
668 const struct rte_eth_rxconf *rx_conf __rte_unused,
669 struct rte_mempool *mb_pool)
671 struct pmd_internals *internals = dev->data->dev_private;
672 uint32_t buf_size, data_size;
673 struct pkt_rx_queue *rxq;
676 rxq = &internals->rx_queues[rx_queue_id];
678 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
679 rx_queue_id, rxq->xsk_queue_idx);
680 /* Now get the space available for data in the mbuf */
681 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
682 RTE_PKTMBUF_HEADROOM;
683 data_size = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
685 if (data_size > buf_size) {
686 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
687 dev->device->name, data_size, buf_size);
692 rxq->mb_pool = mb_pool;
694 if (xsk_configure(internals, rxq, nb_rx_desc)) {
695 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
700 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
701 rxq->fds[0].events = POLLIN;
703 rxq->umem->pmd_zc = internals->pmd_zc;
705 dev->data->rx_queues[rx_queue_id] = rxq;
713 eth_tx_queue_setup(struct rte_eth_dev *dev,
714 uint16_t tx_queue_id,
715 uint16_t nb_tx_desc __rte_unused,
716 unsigned int socket_id __rte_unused,
717 const struct rte_eth_txconf *tx_conf __rte_unused)
719 struct pmd_internals *internals = dev->data->dev_private;
720 struct pkt_tx_queue *txq;
722 txq = &internals->tx_queues[tx_queue_id];
724 dev->data->tx_queues[tx_queue_id] = txq;
729 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
731 struct pmd_internals *internals = dev->data->dev_private;
732 struct ifreq ifr = { .ifr_mtu = mtu };
736 s = socket(PF_INET, SOCK_DGRAM, 0);
740 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
741 ret = ioctl(s, SIOCSIFMTU, &ifr);
744 return (ret < 0) ? -errno : 0;
748 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
753 s = socket(PF_INET, SOCK_DGRAM, 0);
757 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
758 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
760 ifr.ifr_flags &= mask;
761 ifr.ifr_flags |= flags;
762 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
769 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
771 struct pmd_internals *internals = dev->data->dev_private;
773 eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
777 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
779 struct pmd_internals *internals = dev->data->dev_private;
781 eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
784 static const struct eth_dev_ops ops = {
785 .dev_start = eth_dev_start,
786 .dev_stop = eth_dev_stop,
787 .dev_close = eth_dev_close,
788 .dev_configure = eth_dev_configure,
789 .dev_infos_get = eth_dev_info,
790 .mtu_set = eth_dev_mtu_set,
791 .promiscuous_enable = eth_dev_promiscuous_enable,
792 .promiscuous_disable = eth_dev_promiscuous_disable,
793 .rx_queue_setup = eth_rx_queue_setup,
794 .tx_queue_setup = eth_tx_queue_setup,
795 .rx_queue_release = eth_queue_release,
796 .tx_queue_release = eth_queue_release,
797 .link_update = eth_link_update,
798 .stats_get = eth_stats_get,
799 .stats_reset = eth_stats_reset,
802 /** parse integer from integer argument */
804 parse_integer_arg(const char *key __rte_unused,
805 const char *value, void *extra_args)
807 int *i = (int *)extra_args;
810 *i = strtol(value, &end, 10);
812 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
819 /** parse name argument */
821 parse_name_arg(const char *key __rte_unused,
822 const char *value, void *extra_args)
824 char *name = extra_args;
826 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
827 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
832 strlcpy(name, value, IFNAMSIZ);
838 xdp_get_channels_info(const char *if_name, int *max_queues,
839 int *combined_queues)
841 struct ethtool_channels channels;
845 fd = socket(AF_INET, SOCK_DGRAM, 0);
849 channels.cmd = ETHTOOL_GCHANNELS;
850 ifr.ifr_data = (void *)&channels;
851 strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
852 ret = ioctl(fd, SIOCETHTOOL, &ifr);
854 if (errno == EOPNOTSUPP) {
862 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
863 /* If the device says it has no channels, then all traffic
864 * is sent to a single stream, so max queues = 1.
867 *combined_queues = 1;
869 *max_queues = channels.max_combined;
870 *combined_queues = channels.combined_count;
879 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
880 int *queue_cnt, int *pmd_zc)
884 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
885 &parse_name_arg, if_name);
889 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
890 &parse_integer_arg, start_queue);
894 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
895 &parse_integer_arg, queue_cnt);
896 if (ret < 0 || *queue_cnt <= 0) {
901 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PMD_ZC_ARG,
902 &parse_integer_arg, pmd_zc);
907 rte_kvargs_free(kvlist);
912 get_iface_info(const char *if_name,
913 struct rte_ether_addr *eth_addr,
917 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
922 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
923 if (ioctl(sock, SIOCGIFINDEX, &ifr))
926 *if_index = ifr.ifr_ifindex;
928 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
931 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
941 static struct rte_eth_dev *
942 init_internals(struct rte_vdev_device *dev, const char *if_name,
943 int start_queue_idx, int queue_cnt, int pmd_zc)
945 const char *name = rte_vdev_device_name(dev);
946 const unsigned int numa_node = dev->device.numa_node;
947 struct pmd_internals *internals;
948 struct rte_eth_dev *eth_dev;
952 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
953 if (internals == NULL)
956 internals->start_queue_idx = start_queue_idx;
957 internals->queue_cnt = queue_cnt;
958 internals->pmd_zc = pmd_zc;
959 strlcpy(internals->if_name, if_name, IFNAMSIZ);
961 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
962 &internals->combined_queue_cnt)) {
963 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
965 goto err_free_internals;
968 if (queue_cnt > internals->combined_queue_cnt) {
969 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
970 queue_cnt, internals->combined_queue_cnt);
971 goto err_free_internals;
974 internals->rx_queues = rte_zmalloc_socket(NULL,
975 sizeof(struct pkt_rx_queue) * queue_cnt,
977 if (internals->rx_queues == NULL) {
978 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
979 goto err_free_internals;
982 internals->tx_queues = rte_zmalloc_socket(NULL,
983 sizeof(struct pkt_tx_queue) * queue_cnt,
985 if (internals->tx_queues == NULL) {
986 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
989 for (i = 0; i < queue_cnt; i++) {
990 internals->tx_queues[i].pair = &internals->rx_queues[i];
991 internals->rx_queues[i].pair = &internals->tx_queues[i];
992 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
993 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
996 ret = get_iface_info(if_name, &internals->eth_addr,
997 &internals->if_index);
1001 eth_dev = rte_eth_vdev_allocate(dev, 0);
1002 if (eth_dev == NULL)
1005 eth_dev->data->dev_private = internals;
1006 eth_dev->data->dev_link = pmd_link;
1007 eth_dev->data->mac_addrs = &internals->eth_addr;
1008 eth_dev->dev_ops = &ops;
1009 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1010 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1011 /* Let rte_eth_dev_close() release the port resources. */
1012 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1014 if (internals->pmd_zc)
1015 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1020 rte_free(internals->tx_queues);
1022 rte_free(internals->rx_queues);
1024 rte_free(internals);
1029 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1031 struct rte_kvargs *kvlist;
1032 char if_name[IFNAMSIZ] = {'\0'};
1033 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1034 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1035 struct rte_eth_dev *eth_dev = NULL;
1039 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1040 rte_vdev_device_name(dev));
1042 name = rte_vdev_device_name(dev);
1043 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1044 strlen(rte_vdev_device_args(dev)) == 0) {
1045 eth_dev = rte_eth_dev_attach_secondary(name);
1046 if (eth_dev == NULL) {
1047 AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1050 eth_dev->dev_ops = &ops;
1051 rte_eth_dev_probing_finish(eth_dev);
1055 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1056 if (kvlist == NULL) {
1057 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1061 if (dev->device.numa_node == SOCKET_ID_ANY)
1062 dev->device.numa_node = rte_socket_id();
1064 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1065 &xsk_queue_cnt, &pmd_zc) < 0) {
1066 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1070 if (strlen(if_name) == 0) {
1071 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1075 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1076 xsk_queue_cnt, pmd_zc);
1077 if (eth_dev == NULL) {
1078 AF_XDP_LOG(ERR, "Failed to init internals\n");
1082 rte_eth_dev_probing_finish(eth_dev);
1088 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1090 struct rte_eth_dev *eth_dev = NULL;
1092 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1098 /* find the ethdev entry */
1099 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1100 if (eth_dev == NULL)
1103 eth_dev_close(eth_dev);
1104 rte_eth_dev_release_port(eth_dev);
1110 static struct rte_vdev_driver pmd_af_xdp_drv = {
1111 .probe = rte_pmd_af_xdp_probe,
1112 .remove = rte_pmd_af_xdp_remove,
1115 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1116 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1118 "start_queue=<int> "
1119 "queue_count=<int> "
1120 "pmd_zero_copy=<0|1>");
1122 RTE_INIT(af_xdp_init_log)
1124 af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
1125 if (af_xdp_logtype >= 0)
1126 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);