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;
104 struct pkt_tx_queue {
105 struct xsk_ring_prod tx;
107 struct tx_stats stats;
109 struct pkt_rx_queue *pair;
113 struct pmd_internals {
115 char if_name[IFNAMSIZ];
119 int combined_queue_cnt;
122 struct rte_ether_addr eth_addr;
124 struct pkt_rx_queue *rx_queues;
125 struct pkt_tx_queue *tx_queues;
128 #define ETH_AF_XDP_IFACE_ARG "iface"
129 #define ETH_AF_XDP_START_QUEUE_ARG "start_queue"
130 #define ETH_AF_XDP_QUEUE_COUNT_ARG "queue_count"
131 #define ETH_AF_XDP_PMD_ZC_ARG "pmd_zero_copy"
133 static const char * const valid_arguments[] = {
134 ETH_AF_XDP_IFACE_ARG,
135 ETH_AF_XDP_START_QUEUE_ARG,
136 ETH_AF_XDP_QUEUE_COUNT_ARG,
137 ETH_AF_XDP_PMD_ZC_ARG,
141 static const struct rte_eth_link pmd_link = {
142 .link_speed = ETH_SPEED_NUM_10G,
143 .link_duplex = ETH_LINK_FULL_DUPLEX,
144 .link_status = ETH_LINK_DOWN,
145 .link_autoneg = ETH_LINK_AUTONEG
149 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size)
151 struct xsk_ring_prod *fq = &umem->fq;
152 void *addrs[reserve_size];
156 if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
158 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
162 if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
163 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
164 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
169 for (i = 0; i < reserve_size; i++) {
172 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
173 *fq_addr = (uint64_t)addrs[i];
176 xsk_ring_prod__submit(fq, reserve_size);
182 umem_buf_release_to_fq(void *addr, void *opaque)
184 struct xsk_umem_info *umem = (struct xsk_umem_info *)opaque;
185 uint64_t umem_addr = (uint64_t)addr - umem->mz->addr_64;
187 rte_ring_enqueue(umem->buf_ring, (void *)umem_addr);
191 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
193 struct pkt_rx_queue *rxq = queue;
194 struct xsk_ring_cons *rx = &rxq->rx;
195 struct xsk_umem_info *umem = rxq->umem;
196 struct xsk_ring_prod *fq = &umem->fq;
198 uint32_t free_thresh = fq->size >> 1;
199 int pmd_zc = umem->pmd_zc;
200 struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
201 unsigned long dropped = 0;
202 unsigned long rx_bytes = 0;
205 nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
207 if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
210 rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
212 #if defined(XDP_USE_NEED_WAKEUP)
213 if (xsk_ring_prod__needs_wakeup(fq))
214 (void)poll(rxq->fds, 1, 1000);
220 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
221 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
223 for (i = 0; i < rcvd; i++) {
224 const struct xdp_desc *desc;
228 uint16_t buf_len = ETH_AF_XDP_FRAME_SIZE;
229 struct rte_mbuf_ext_shared_info *shinfo;
231 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
234 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
237 shinfo = rte_pktmbuf_ext_shinfo_init_helper(pkt,
238 &buf_len, umem_buf_release_to_fq, umem);
240 rte_pktmbuf_attach_extbuf(mbufs[i], pkt, 0, buf_len,
243 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *),
245 rte_ring_enqueue(umem->buf_ring, (void *)addr);
247 rte_pktmbuf_pkt_len(mbufs[i]) = len;
248 rte_pktmbuf_data_len(mbufs[i]) = len;
253 xsk_ring_cons__release(rx, rcvd);
256 rxq->stats.rx_pkts += (rcvd - dropped);
257 rxq->stats.rx_bytes += rx_bytes;
261 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
268 pull_umem_cq(struct xsk_umem_info *umem, int size)
270 struct xsk_ring_cons *cq = &umem->cq;
274 n = xsk_ring_cons__peek(cq, size, &idx_cq);
276 for (i = 0; i < n; i++) {
278 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
279 rte_ring_enqueue(umem->buf_ring, (void *)addr);
282 xsk_ring_cons__release(cq, n);
286 kick_tx(struct pkt_tx_queue *txq)
288 struct xsk_umem_info *umem = txq->pair->umem;
290 #if defined(XDP_USE_NEED_WAKEUP)
291 if (xsk_ring_prod__needs_wakeup(&txq->tx))
293 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
294 0, MSG_DONTWAIT) < 0) {
295 /* some thing unexpected */
296 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
299 /* pull from completion queue to leave more space */
301 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
303 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
307 in_umem_range(struct xsk_umem_info *umem, uint64_t addr)
309 uint64_t mz_base_addr = umem->mz->addr_64;
311 return addr >= mz_base_addr && addr < mz_base_addr + umem->mz->len;
315 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
317 struct pkt_tx_queue *txq = queue;
318 struct xsk_umem_info *umem = txq->pair->umem;
319 struct rte_mbuf *mbuf;
320 int pmd_zc = umem->pmd_zc;
321 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
322 unsigned long tx_bytes = 0;
326 nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
328 pull_umem_cq(umem, nb_pkts);
330 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
335 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
337 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
341 for (i = 0; i < nb_pkts; i++) {
342 struct xdp_desc *desc;
345 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
347 desc->len = mbuf->pkt_len;
350 * We need to make sure the external mbuf address is within
351 * current port's umem memzone range
353 if (pmd_zc && RTE_MBUF_HAS_EXTBUF(mbuf) &&
354 in_umem_range(umem, (uint64_t)mbuf->buf_addr)) {
355 desc->addr = (uint64_t)mbuf->buf_addr -
357 mbuf->buf_addr = xsk_umem__get_data(umem->mz->addr,
360 desc->addr = (uint64_t)addrs[i];
361 pkt = xsk_umem__get_data(umem->mz->addr,
363 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
366 tx_bytes += mbuf->pkt_len;
369 xsk_ring_prod__submit(&txq->tx, nb_pkts);
373 txq->stats.tx_pkts += nb_pkts;
374 txq->stats.tx_bytes += tx_bytes;
376 for (i = 0; i < nb_pkts; i++)
377 rte_pktmbuf_free(bufs[i]);
383 eth_dev_start(struct rte_eth_dev *dev)
385 dev->data->dev_link.link_status = ETH_LINK_UP;
390 /* This function gets called when the current port gets stopped. */
392 eth_dev_stop(struct rte_eth_dev *dev)
394 dev->data->dev_link.link_status = ETH_LINK_DOWN;
398 eth_dev_configure(struct rte_eth_dev *dev)
400 /* rx/tx must be paired */
401 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
408 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
410 struct pmd_internals *internals = dev->data->dev_private;
412 dev_info->if_index = internals->if_index;
413 dev_info->max_mac_addrs = 1;
414 dev_info->max_rx_pktlen = ETH_FRAME_LEN;
415 dev_info->max_rx_queues = internals->queue_cnt;
416 dev_info->max_tx_queues = internals->queue_cnt;
418 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
419 dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
421 dev_info->default_rxportconf.nb_queues = 1;
422 dev_info->default_txportconf.nb_queues = 1;
423 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
424 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
428 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
430 struct pmd_internals *internals = dev->data->dev_private;
431 struct xdp_statistics xdp_stats;
432 struct pkt_rx_queue *rxq;
433 struct pkt_tx_queue *txq;
437 for (i = 0; i < dev->data->nb_rx_queues; i++) {
438 optlen = sizeof(struct xdp_statistics);
439 rxq = &internals->rx_queues[i];
441 stats->q_ipackets[i] = rxq->stats.rx_pkts;
442 stats->q_ibytes[i] = rxq->stats.rx_bytes;
444 stats->q_opackets[i] = txq->stats.tx_pkts;
445 stats->q_obytes[i] = txq->stats.tx_bytes;
447 stats->ipackets += stats->q_ipackets[i];
448 stats->ibytes += stats->q_ibytes[i];
449 stats->imissed += rxq->stats.rx_dropped;
450 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
451 XDP_STATISTICS, &xdp_stats, &optlen);
453 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
456 stats->imissed += xdp_stats.rx_dropped;
458 stats->opackets += stats->q_opackets[i];
459 stats->oerrors += txq->stats.err_pkts;
460 stats->obytes += stats->q_obytes[i];
467 eth_stats_reset(struct rte_eth_dev *dev)
469 struct pmd_internals *internals = dev->data->dev_private;
472 for (i = 0; i < internals->queue_cnt; i++) {
473 memset(&internals->rx_queues[i].stats, 0,
474 sizeof(struct rx_stats));
475 memset(&internals->tx_queues[i].stats, 0,
476 sizeof(struct tx_stats));
481 remove_xdp_program(struct pmd_internals *internals)
483 uint32_t curr_prog_id = 0;
485 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
486 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
487 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
490 bpf_set_link_xdp_fd(internals->if_index, -1,
491 XDP_FLAGS_UPDATE_IF_NOEXIST);
495 xdp_umem_destroy(struct xsk_umem_info *umem)
497 rte_memzone_free(umem->mz);
500 rte_ring_free(umem->buf_ring);
501 umem->buf_ring = NULL;
508 eth_dev_close(struct rte_eth_dev *dev)
510 struct pmd_internals *internals = dev->data->dev_private;
511 struct pkt_rx_queue *rxq;
514 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
517 for (i = 0; i < internals->queue_cnt; i++) {
518 rxq = &internals->rx_queues[i];
519 if (rxq->umem == NULL)
521 xsk_socket__delete(rxq->xsk);
522 (void)xsk_umem__delete(rxq->umem->umem);
523 xdp_umem_destroy(rxq->umem);
525 /* free pkt_tx_queue */
531 * MAC is not allocated dynamically, setting it to NULL would prevent
532 * from releasing it in rte_eth_dev_release_port.
534 dev->data->mac_addrs = NULL;
536 remove_xdp_program(internals);
540 eth_queue_release(void *q __rte_unused)
545 eth_link_update(struct rte_eth_dev *dev __rte_unused,
546 int wait_to_complete __rte_unused)
552 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
553 struct pkt_rx_queue *rxq)
555 struct xsk_umem_info *umem;
556 const struct rte_memzone *mz;
557 struct xsk_umem_config usr_config = {
558 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
559 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
560 .frame_size = ETH_AF_XDP_FRAME_SIZE,
561 .frame_headroom = ETH_AF_XDP_DATA_HEADROOM };
562 char ring_name[RTE_RING_NAMESIZE];
563 char mz_name[RTE_MEMZONE_NAMESIZE];
567 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
569 AF_XDP_LOG(ERR, "Failed to allocate umem info");
573 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
574 internals->if_name, rxq->xsk_queue_idx);
575 umem->buf_ring = rte_ring_create(ring_name,
576 ETH_AF_XDP_NUM_BUFFERS,
579 if (umem->buf_ring == NULL) {
580 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
584 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
585 rte_ring_enqueue(umem->buf_ring,
586 (void *)(i * ETH_AF_XDP_FRAME_SIZE +
587 ETH_AF_XDP_DATA_HEADROOM));
589 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
590 internals->if_name, rxq->xsk_queue_idx);
591 mz = rte_memzone_reserve_aligned(mz_name,
592 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
593 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
596 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
600 ret = xsk_umem__create(&umem->umem, mz->addr,
601 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
602 &umem->fq, &umem->cq,
606 AF_XDP_LOG(ERR, "Failed to create umem");
614 xdp_umem_destroy(umem);
619 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
622 struct xsk_socket_config cfg;
623 struct pkt_tx_queue *txq = rxq->pair;
627 rxq->umem = xdp_umem_configure(internals, rxq);
628 if (rxq->umem == NULL)
631 cfg.rx_size = ring_size;
632 cfg.tx_size = ring_size;
633 cfg.libbpf_flags = 0;
634 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
637 #if defined(XDP_USE_NEED_WAKEUP)
638 cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
641 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
642 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
645 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
649 reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
650 ret = reserve_fill_queue(rxq->umem, reserve_size);
652 xsk_socket__delete(rxq->xsk);
653 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
660 xdp_umem_destroy(rxq->umem);
666 eth_rx_queue_setup(struct rte_eth_dev *dev,
667 uint16_t rx_queue_id,
669 unsigned int socket_id __rte_unused,
670 const struct rte_eth_rxconf *rx_conf __rte_unused,
671 struct rte_mempool *mb_pool)
673 struct pmd_internals *internals = dev->data->dev_private;
674 uint32_t buf_size, data_size;
675 struct pkt_rx_queue *rxq;
678 rxq = &internals->rx_queues[rx_queue_id];
680 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
681 rx_queue_id, rxq->xsk_queue_idx);
682 /* Now get the space available for data in the mbuf */
683 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
684 RTE_PKTMBUF_HEADROOM;
685 data_size = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
687 if (data_size > buf_size) {
688 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
689 dev->device->name, data_size, buf_size);
694 rxq->mb_pool = mb_pool;
696 if (xsk_configure(internals, rxq, nb_rx_desc)) {
697 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
702 rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
703 rxq->fds[0].events = POLLIN;
705 rxq->umem->pmd_zc = internals->pmd_zc;
707 dev->data->rx_queues[rx_queue_id] = rxq;
715 eth_tx_queue_setup(struct rte_eth_dev *dev,
716 uint16_t tx_queue_id,
717 uint16_t nb_tx_desc __rte_unused,
718 unsigned int socket_id __rte_unused,
719 const struct rte_eth_txconf *tx_conf __rte_unused)
721 struct pmd_internals *internals = dev->data->dev_private;
722 struct pkt_tx_queue *txq;
724 txq = &internals->tx_queues[tx_queue_id];
726 dev->data->tx_queues[tx_queue_id] = txq;
731 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
733 struct pmd_internals *internals = dev->data->dev_private;
734 struct ifreq ifr = { .ifr_mtu = mtu };
738 s = socket(PF_INET, SOCK_DGRAM, 0);
742 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
743 ret = ioctl(s, SIOCSIFMTU, &ifr);
746 return (ret < 0) ? -errno : 0;
750 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
755 s = socket(PF_INET, SOCK_DGRAM, 0);
759 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
760 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
762 ifr.ifr_flags &= mask;
763 ifr.ifr_flags |= flags;
764 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
771 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
773 struct pmd_internals *internals = dev->data->dev_private;
775 eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
779 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
781 struct pmd_internals *internals = dev->data->dev_private;
783 eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
786 static const struct eth_dev_ops ops = {
787 .dev_start = eth_dev_start,
788 .dev_stop = eth_dev_stop,
789 .dev_close = eth_dev_close,
790 .dev_configure = eth_dev_configure,
791 .dev_infos_get = eth_dev_info,
792 .mtu_set = eth_dev_mtu_set,
793 .promiscuous_enable = eth_dev_promiscuous_enable,
794 .promiscuous_disable = eth_dev_promiscuous_disable,
795 .rx_queue_setup = eth_rx_queue_setup,
796 .tx_queue_setup = eth_tx_queue_setup,
797 .rx_queue_release = eth_queue_release,
798 .tx_queue_release = eth_queue_release,
799 .link_update = eth_link_update,
800 .stats_get = eth_stats_get,
801 .stats_reset = eth_stats_reset,
804 /** parse integer from integer argument */
806 parse_integer_arg(const char *key __rte_unused,
807 const char *value, void *extra_args)
809 int *i = (int *)extra_args;
812 *i = strtol(value, &end, 10);
814 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
821 /** parse name argument */
823 parse_name_arg(const char *key __rte_unused,
824 const char *value, void *extra_args)
826 char *name = extra_args;
828 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
829 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
834 strlcpy(name, value, IFNAMSIZ);
840 xdp_get_channels_info(const char *if_name, int *max_queues,
841 int *combined_queues)
843 struct ethtool_channels channels;
847 fd = socket(AF_INET, SOCK_DGRAM, 0);
851 channels.cmd = ETHTOOL_GCHANNELS;
852 ifr.ifr_data = (void *)&channels;
853 strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
854 ret = ioctl(fd, SIOCETHTOOL, &ifr);
855 if (ret && errno != EOPNOTSUPP) {
860 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
861 /* If the device says it has no channels, then all traffic
862 * is sent to a single stream, so max queues = 1.
865 *combined_queues = 1;
867 *max_queues = channels.max_combined;
868 *combined_queues = channels.combined_count;
877 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
878 int *queue_cnt, int *pmd_zc)
882 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
883 &parse_name_arg, if_name);
887 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
888 &parse_integer_arg, start_queue);
892 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
893 &parse_integer_arg, queue_cnt);
894 if (ret < 0 || *queue_cnt <= 0) {
899 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PMD_ZC_ARG,
900 &parse_integer_arg, pmd_zc);
905 rte_kvargs_free(kvlist);
910 get_iface_info(const char *if_name,
911 struct rte_ether_addr *eth_addr,
915 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
920 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
921 if (ioctl(sock, SIOCGIFINDEX, &ifr))
924 *if_index = ifr.ifr_ifindex;
926 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
929 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
939 static struct rte_eth_dev *
940 init_internals(struct rte_vdev_device *dev, const char *if_name,
941 int start_queue_idx, int queue_cnt, int pmd_zc)
943 const char *name = rte_vdev_device_name(dev);
944 const unsigned int numa_node = dev->device.numa_node;
945 struct pmd_internals *internals;
946 struct rte_eth_dev *eth_dev;
950 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
951 if (internals == NULL)
954 internals->start_queue_idx = start_queue_idx;
955 internals->queue_cnt = queue_cnt;
956 internals->pmd_zc = pmd_zc;
957 strlcpy(internals->if_name, if_name, IFNAMSIZ);
959 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
960 &internals->combined_queue_cnt)) {
961 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
963 goto err_free_internals;
966 if (queue_cnt > internals->combined_queue_cnt) {
967 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
968 queue_cnt, internals->combined_queue_cnt);
969 goto err_free_internals;
972 internals->rx_queues = rte_zmalloc_socket(NULL,
973 sizeof(struct pkt_rx_queue) * queue_cnt,
975 if (internals->rx_queues == NULL) {
976 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
977 goto err_free_internals;
980 internals->tx_queues = rte_zmalloc_socket(NULL,
981 sizeof(struct pkt_tx_queue) * queue_cnt,
983 if (internals->tx_queues == NULL) {
984 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
987 for (i = 0; i < queue_cnt; i++) {
988 internals->tx_queues[i].pair = &internals->rx_queues[i];
989 internals->rx_queues[i].pair = &internals->tx_queues[i];
990 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
991 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
994 ret = get_iface_info(if_name, &internals->eth_addr,
995 &internals->if_index);
999 eth_dev = rte_eth_vdev_allocate(dev, 0);
1000 if (eth_dev == NULL)
1003 eth_dev->data->dev_private = internals;
1004 eth_dev->data->dev_link = pmd_link;
1005 eth_dev->data->mac_addrs = &internals->eth_addr;
1006 eth_dev->dev_ops = &ops;
1007 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1008 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1009 /* Let rte_eth_dev_close() release the port resources. */
1010 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1012 if (internals->pmd_zc)
1013 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1018 rte_free(internals->tx_queues);
1020 rte_free(internals->rx_queues);
1022 rte_free(internals);
1027 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1029 struct rte_kvargs *kvlist;
1030 char if_name[IFNAMSIZ] = {'\0'};
1031 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1032 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1033 struct rte_eth_dev *eth_dev = NULL;
1037 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1038 rte_vdev_device_name(dev));
1040 name = rte_vdev_device_name(dev);
1041 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1042 strlen(rte_vdev_device_args(dev)) == 0) {
1043 eth_dev = rte_eth_dev_attach_secondary(name);
1044 if (eth_dev == NULL) {
1045 AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1048 eth_dev->dev_ops = &ops;
1049 rte_eth_dev_probing_finish(eth_dev);
1053 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1054 if (kvlist == NULL) {
1055 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1059 if (dev->device.numa_node == SOCKET_ID_ANY)
1060 dev->device.numa_node = rte_socket_id();
1062 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1063 &xsk_queue_cnt, &pmd_zc) < 0) {
1064 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1068 if (strlen(if_name) == 0) {
1069 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1073 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1074 xsk_queue_cnt, pmd_zc);
1075 if (eth_dev == NULL) {
1076 AF_XDP_LOG(ERR, "Failed to init internals\n");
1080 rte_eth_dev_probing_finish(eth_dev);
1086 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1088 struct rte_eth_dev *eth_dev = NULL;
1090 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1096 /* find the ethdev entry */
1097 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1098 if (eth_dev == NULL)
1101 eth_dev_close(eth_dev);
1102 rte_eth_dev_release_port(eth_dev);
1108 static struct rte_vdev_driver pmd_af_xdp_drv = {
1109 .probe = rte_pmd_af_xdp_probe,
1110 .remove = rte_pmd_af_xdp_remove,
1113 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1114 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1116 "start_queue=<int> "
1117 "queue_count=<int> "
1118 "pmd_zero_copy=<0|1>");
1120 RTE_INIT(af_xdp_init_log)
1122 af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
1123 if (af_xdp_logtype >= 0)
1124 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);