1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 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 <rte_ethdev_driver.h>
22 #include <rte_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>
28 #include <rte_config.h>
31 #include <rte_ether.h>
32 #include <rte_lcore.h>
34 #include <rte_memory.h>
35 #include <rte_memzone.h>
37 #include <rte_malloc.h>
52 static int af_xdp_logtype;
54 #define AF_XDP_LOG(level, fmt, args...) \
55 rte_log(RTE_LOG_ ## level, af_xdp_logtype, \
56 "%s(): " fmt, __func__, ##args)
58 #define ETH_AF_XDP_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
59 #define ETH_AF_XDP_NUM_BUFFERS 4096
60 #define ETH_AF_XDP_DATA_HEADROOM 0
61 #define ETH_AF_XDP_DFLT_NUM_DESCS XSK_RING_CONS__DEFAULT_NUM_DESCS
62 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
63 #define ETH_AF_XDP_DFLT_QUEUE_COUNT 1
65 #define ETH_AF_XDP_RX_BATCH_SIZE 32
66 #define ETH_AF_XDP_TX_BATCH_SIZE 32
69 struct xsk_umem_info {
70 struct xsk_ring_prod fq;
71 struct xsk_ring_cons cq;
72 struct xsk_umem *umem;
73 struct rte_ring *buf_ring;
74 const struct rte_memzone *mz;
85 struct xsk_ring_cons rx;
86 struct xsk_umem_info *umem;
87 struct xsk_socket *xsk;
88 struct rte_mempool *mb_pool;
90 struct rx_stats stats;
92 struct pkt_tx_queue *pair;
102 struct pkt_tx_queue {
103 struct xsk_ring_prod tx;
105 struct tx_stats stats;
107 struct pkt_rx_queue *pair;
111 struct pmd_internals {
113 char if_name[IFNAMSIZ];
117 int combined_queue_cnt;
120 struct rte_ether_addr eth_addr;
121 struct rte_mempool *mb_pool_share;
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);
213 if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
214 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
216 for (i = 0; i < rcvd; i++) {
217 const struct xdp_desc *desc;
221 uint16_t buf_len = ETH_AF_XDP_FRAME_SIZE;
222 struct rte_mbuf_ext_shared_info *shinfo;
224 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
227 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
230 shinfo = rte_pktmbuf_ext_shinfo_init_helper(pkt,
231 &buf_len, umem_buf_release_to_fq, umem);
233 rte_pktmbuf_attach_extbuf(mbufs[i], pkt, 0, buf_len,
236 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *),
238 rte_ring_enqueue(umem->buf_ring, (void *)addr);
240 rte_pktmbuf_pkt_len(mbufs[i]) = len;
241 rte_pktmbuf_data_len(mbufs[i]) = len;
246 xsk_ring_cons__release(rx, rcvd);
249 rxq->stats.rx_pkts += (rcvd - dropped);
250 rxq->stats.rx_bytes += rx_bytes;
254 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
261 pull_umem_cq(struct xsk_umem_info *umem, int size)
263 struct xsk_ring_cons *cq = &umem->cq;
267 n = xsk_ring_cons__peek(cq, size, &idx_cq);
269 for (i = 0; i < n; i++) {
271 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
272 rte_ring_enqueue(umem->buf_ring, (void *)addr);
275 xsk_ring_cons__release(cq, n);
279 kick_tx(struct pkt_tx_queue *txq)
281 struct xsk_umem_info *umem = txq->pair->umem;
283 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
284 0, MSG_DONTWAIT) < 0) {
285 /* some thing unexpected */
286 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
289 /* pull from completion queue to leave more space */
291 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
293 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
297 in_umem_range(struct xsk_umem_info *umem, uint64_t addr)
299 uint64_t mz_base_addr = umem->mz->addr_64;
301 return addr >= mz_base_addr && addr < mz_base_addr + umem->mz->len;
305 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
307 struct pkt_tx_queue *txq = queue;
308 struct xsk_umem_info *umem = txq->pair->umem;
309 struct rte_mbuf *mbuf;
310 int pmd_zc = umem->pmd_zc;
311 void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
312 unsigned long tx_bytes = 0;
316 nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
318 pull_umem_cq(umem, nb_pkts);
320 nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
325 if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
327 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
331 for (i = 0; i < nb_pkts; i++) {
332 struct xdp_desc *desc;
335 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
337 desc->len = mbuf->pkt_len;
340 * We need to make sure the external mbuf address is within
341 * current port's umem memzone range
343 if (pmd_zc && RTE_MBUF_HAS_EXTBUF(mbuf) &&
344 in_umem_range(umem, (uint64_t)mbuf->buf_addr)) {
345 desc->addr = (uint64_t)mbuf->buf_addr -
347 mbuf->buf_addr = xsk_umem__get_data(umem->mz->addr,
350 desc->addr = (uint64_t)addrs[i];
351 pkt = xsk_umem__get_data(umem->mz->addr,
353 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
356 tx_bytes += mbuf->pkt_len;
359 xsk_ring_prod__submit(&txq->tx, nb_pkts);
363 txq->stats.tx_pkts += nb_pkts;
364 txq->stats.tx_bytes += tx_bytes;
366 for (i = 0; i < nb_pkts; i++)
367 rte_pktmbuf_free(bufs[i]);
373 eth_dev_start(struct rte_eth_dev *dev)
375 dev->data->dev_link.link_status = ETH_LINK_UP;
380 /* This function gets called when the current port gets stopped. */
382 eth_dev_stop(struct rte_eth_dev *dev)
384 dev->data->dev_link.link_status = ETH_LINK_DOWN;
388 eth_dev_configure(struct rte_eth_dev *dev)
390 /* rx/tx must be paired */
391 if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
398 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
400 struct pmd_internals *internals = dev->data->dev_private;
402 dev_info->if_index = internals->if_index;
403 dev_info->max_mac_addrs = 1;
404 dev_info->max_rx_pktlen = ETH_FRAME_LEN;
405 dev_info->max_rx_queues = internals->queue_cnt;
406 dev_info->max_tx_queues = internals->queue_cnt;
408 dev_info->min_mtu = RTE_ETHER_MIN_MTU;
409 dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
411 dev_info->default_rxportconf.nb_queues = 1;
412 dev_info->default_txportconf.nb_queues = 1;
413 dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
414 dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
418 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
420 struct pmd_internals *internals = dev->data->dev_private;
421 struct xdp_statistics xdp_stats;
422 struct pkt_rx_queue *rxq;
423 struct pkt_tx_queue *txq;
427 for (i = 0; i < dev->data->nb_rx_queues; i++) {
428 optlen = sizeof(struct xdp_statistics);
429 rxq = &internals->rx_queues[i];
431 stats->q_ipackets[i] = rxq->stats.rx_pkts;
432 stats->q_ibytes[i] = rxq->stats.rx_bytes;
434 stats->q_opackets[i] = txq->stats.tx_pkts;
435 stats->q_obytes[i] = txq->stats.tx_bytes;
437 stats->ipackets += stats->q_ipackets[i];
438 stats->ibytes += stats->q_ibytes[i];
439 stats->imissed += rxq->stats.rx_dropped;
440 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
441 XDP_STATISTICS, &xdp_stats, &optlen);
443 AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
446 stats->imissed += xdp_stats.rx_dropped;
448 stats->opackets += stats->q_opackets[i];
449 stats->oerrors += txq->stats.err_pkts;
450 stats->obytes += stats->q_obytes[i];
457 eth_stats_reset(struct rte_eth_dev *dev)
459 struct pmd_internals *internals = dev->data->dev_private;
462 for (i = 0; i < internals->queue_cnt; i++) {
463 memset(&internals->rx_queues[i].stats, 0,
464 sizeof(struct rx_stats));
465 memset(&internals->tx_queues[i].stats, 0,
466 sizeof(struct tx_stats));
471 remove_xdp_program(struct pmd_internals *internals)
473 uint32_t curr_prog_id = 0;
475 if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
476 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
477 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
480 bpf_set_link_xdp_fd(internals->if_index, -1,
481 XDP_FLAGS_UPDATE_IF_NOEXIST);
485 xdp_umem_destroy(struct xsk_umem_info *umem)
487 rte_memzone_free(umem->mz);
490 rte_ring_free(umem->buf_ring);
491 umem->buf_ring = NULL;
498 eth_dev_close(struct rte_eth_dev *dev)
500 struct pmd_internals *internals = dev->data->dev_private;
501 struct pkt_rx_queue *rxq;
504 AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
507 for (i = 0; i < internals->queue_cnt; i++) {
508 rxq = &internals->rx_queues[i];
509 if (rxq->umem == NULL)
511 xsk_socket__delete(rxq->xsk);
512 (void)xsk_umem__delete(rxq->umem->umem);
513 xdp_umem_destroy(rxq->umem);
515 /* free pkt_tx_queue */
521 * MAC is not allocated dynamically, setting it to NULL would prevent
522 * from releasing it in rte_eth_dev_release_port.
524 dev->data->mac_addrs = NULL;
526 remove_xdp_program(internals);
530 eth_queue_release(void *q __rte_unused)
535 eth_link_update(struct rte_eth_dev *dev __rte_unused,
536 int wait_to_complete __rte_unused)
542 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
543 struct pkt_rx_queue *rxq)
545 struct xsk_umem_info *umem;
546 const struct rte_memzone *mz;
547 struct xsk_umem_config usr_config = {
548 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
549 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
550 .frame_size = ETH_AF_XDP_FRAME_SIZE,
551 .frame_headroom = ETH_AF_XDP_DATA_HEADROOM };
552 char ring_name[RTE_RING_NAMESIZE];
553 char mz_name[RTE_MEMZONE_NAMESIZE];
557 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
559 AF_XDP_LOG(ERR, "Failed to allocate umem info");
563 snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
564 internals->if_name, rxq->xsk_queue_idx);
565 umem->buf_ring = rte_ring_create(ring_name,
566 ETH_AF_XDP_NUM_BUFFERS,
569 if (umem->buf_ring == NULL) {
570 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
574 for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
575 rte_ring_enqueue(umem->buf_ring,
576 (void *)(i * ETH_AF_XDP_FRAME_SIZE +
577 ETH_AF_XDP_DATA_HEADROOM));
579 snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
580 internals->if_name, rxq->xsk_queue_idx);
581 mz = rte_memzone_reserve_aligned(mz_name,
582 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
583 rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
586 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
590 ret = xsk_umem__create(&umem->umem, mz->addr,
591 ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
592 &umem->fq, &umem->cq,
596 AF_XDP_LOG(ERR, "Failed to create umem");
604 xdp_umem_destroy(umem);
609 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
612 struct xsk_socket_config cfg;
613 struct pkt_tx_queue *txq = rxq->pair;
617 rxq->umem = xdp_umem_configure(internals, rxq);
618 if (rxq->umem == NULL)
621 cfg.rx_size = ring_size;
622 cfg.tx_size = ring_size;
623 cfg.libbpf_flags = 0;
624 cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
626 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
627 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
630 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
634 reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
635 ret = reserve_fill_queue(rxq->umem, reserve_size);
637 xsk_socket__delete(rxq->xsk);
638 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
645 xdp_umem_destroy(rxq->umem);
651 eth_rx_queue_setup(struct rte_eth_dev *dev,
652 uint16_t rx_queue_id,
654 unsigned int socket_id __rte_unused,
655 const struct rte_eth_rxconf *rx_conf __rte_unused,
656 struct rte_mempool *mb_pool)
658 struct pmd_internals *internals = dev->data->dev_private;
659 uint32_t buf_size, data_size;
660 struct pkt_rx_queue *rxq;
663 rxq = &internals->rx_queues[rx_queue_id];
665 AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
666 rx_queue_id, rxq->xsk_queue_idx);
667 /* Now get the space available for data in the mbuf */
668 buf_size = rte_pktmbuf_data_room_size(mb_pool) -
669 RTE_PKTMBUF_HEADROOM;
670 data_size = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
672 if (data_size > buf_size) {
673 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
674 dev->device->name, data_size, buf_size);
679 rxq->mb_pool = mb_pool;
681 if (xsk_configure(internals, rxq, nb_rx_desc)) {
682 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
687 rxq->umem->pmd_zc = internals->pmd_zc;
689 dev->data->rx_queues[rx_queue_id] = rxq;
697 eth_tx_queue_setup(struct rte_eth_dev *dev,
698 uint16_t tx_queue_id,
699 uint16_t nb_tx_desc __rte_unused,
700 unsigned int socket_id __rte_unused,
701 const struct rte_eth_txconf *tx_conf __rte_unused)
703 struct pmd_internals *internals = dev->data->dev_private;
704 struct pkt_tx_queue *txq;
706 txq = &internals->tx_queues[tx_queue_id];
708 dev->data->tx_queues[tx_queue_id] = txq;
713 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
715 struct pmd_internals *internals = dev->data->dev_private;
716 struct ifreq ifr = { .ifr_mtu = mtu };
720 s = socket(PF_INET, SOCK_DGRAM, 0);
724 strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
725 ret = ioctl(s, SIOCSIFMTU, &ifr);
728 return (ret < 0) ? -errno : 0;
732 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
737 s = socket(PF_INET, SOCK_DGRAM, 0);
741 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
742 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
744 ifr.ifr_flags &= mask;
745 ifr.ifr_flags |= flags;
746 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
753 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
755 struct pmd_internals *internals = dev->data->dev_private;
757 eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
761 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
763 struct pmd_internals *internals = dev->data->dev_private;
765 eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
768 static const struct eth_dev_ops ops = {
769 .dev_start = eth_dev_start,
770 .dev_stop = eth_dev_stop,
771 .dev_close = eth_dev_close,
772 .dev_configure = eth_dev_configure,
773 .dev_infos_get = eth_dev_info,
774 .mtu_set = eth_dev_mtu_set,
775 .promiscuous_enable = eth_dev_promiscuous_enable,
776 .promiscuous_disable = eth_dev_promiscuous_disable,
777 .rx_queue_setup = eth_rx_queue_setup,
778 .tx_queue_setup = eth_tx_queue_setup,
779 .rx_queue_release = eth_queue_release,
780 .tx_queue_release = eth_queue_release,
781 .link_update = eth_link_update,
782 .stats_get = eth_stats_get,
783 .stats_reset = eth_stats_reset,
786 /** parse integer from integer argument */
788 parse_integer_arg(const char *key __rte_unused,
789 const char *value, void *extra_args)
791 int *i = (int *)extra_args;
794 *i = strtol(value, &end, 10);
796 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
803 /** parse name argument */
805 parse_name_arg(const char *key __rte_unused,
806 const char *value, void *extra_args)
808 char *name = extra_args;
810 if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
811 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
816 strlcpy(name, value, IFNAMSIZ);
822 xdp_get_channels_info(const char *if_name, int *max_queues,
823 int *combined_queues)
825 struct ethtool_channels channels;
829 fd = socket(AF_INET, SOCK_DGRAM, 0);
833 channels.cmd = ETHTOOL_GCHANNELS;
834 ifr.ifr_data = (void *)&channels;
835 strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
836 ret = ioctl(fd, SIOCETHTOOL, &ifr);
837 if (ret && errno != EOPNOTSUPP) {
842 if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
843 /* If the device says it has no channels, then all traffic
844 * is sent to a single stream, so max queues = 1.
847 *combined_queues = 1;
849 *max_queues = channels.max_combined;
850 *combined_queues = channels.combined_count;
859 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
860 int *queue_cnt, int *pmd_zc)
864 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
865 &parse_name_arg, if_name);
869 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
870 &parse_integer_arg, start_queue);
874 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
875 &parse_integer_arg, queue_cnt);
876 if (ret < 0 || *queue_cnt <= 0) {
881 ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PMD_ZC_ARG,
882 &parse_integer_arg, pmd_zc);
887 rte_kvargs_free(kvlist);
892 get_iface_info(const char *if_name,
893 struct rte_ether_addr *eth_addr,
897 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
902 strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
903 if (ioctl(sock, SIOCGIFINDEX, &ifr))
906 *if_index = ifr.ifr_ifindex;
908 if (ioctl(sock, SIOCGIFHWADDR, &ifr))
911 rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
921 static struct rte_eth_dev *
922 init_internals(struct rte_vdev_device *dev, const char *if_name,
923 int start_queue_idx, int queue_cnt, int pmd_zc)
925 const char *name = rte_vdev_device_name(dev);
926 const unsigned int numa_node = dev->device.numa_node;
927 struct pmd_internals *internals;
928 struct rte_eth_dev *eth_dev;
932 internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
933 if (internals == NULL)
936 internals->start_queue_idx = start_queue_idx;
937 internals->queue_cnt = queue_cnt;
938 internals->pmd_zc = pmd_zc;
939 strlcpy(internals->if_name, if_name, IFNAMSIZ);
941 if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
942 &internals->combined_queue_cnt)) {
943 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
945 goto err_free_internals;
948 if (queue_cnt > internals->combined_queue_cnt) {
949 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
950 queue_cnt, internals->combined_queue_cnt);
951 goto err_free_internals;
954 internals->rx_queues = rte_zmalloc_socket(NULL,
955 sizeof(struct pkt_rx_queue) * queue_cnt,
957 if (internals->rx_queues == NULL) {
958 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
959 goto err_free_internals;
962 internals->tx_queues = rte_zmalloc_socket(NULL,
963 sizeof(struct pkt_tx_queue) * queue_cnt,
965 if (internals->tx_queues == NULL) {
966 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
969 for (i = 0; i < queue_cnt; i++) {
970 internals->tx_queues[i].pair = &internals->rx_queues[i];
971 internals->rx_queues[i].pair = &internals->tx_queues[i];
972 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
973 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
976 ret = get_iface_info(if_name, &internals->eth_addr,
977 &internals->if_index);
981 eth_dev = rte_eth_vdev_allocate(dev, 0);
985 eth_dev->data->dev_private = internals;
986 eth_dev->data->dev_link = pmd_link;
987 eth_dev->data->mac_addrs = &internals->eth_addr;
988 eth_dev->dev_ops = &ops;
989 eth_dev->rx_pkt_burst = eth_af_xdp_rx;
990 eth_dev->tx_pkt_burst = eth_af_xdp_tx;
991 /* Let rte_eth_dev_close() release the port resources. */
992 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
994 if (internals->pmd_zc)
995 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1000 rte_free(internals->tx_queues);
1002 rte_free(internals->rx_queues);
1004 rte_free(internals);
1009 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1011 struct rte_kvargs *kvlist;
1012 char if_name[IFNAMSIZ] = {'\0'};
1013 int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1014 int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1015 struct rte_eth_dev *eth_dev = NULL;
1019 AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1020 rte_vdev_device_name(dev));
1022 name = rte_vdev_device_name(dev);
1023 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1024 strlen(rte_vdev_device_args(dev)) == 0) {
1025 eth_dev = rte_eth_dev_attach_secondary(name);
1026 if (eth_dev == NULL) {
1027 AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1030 eth_dev->dev_ops = &ops;
1031 rte_eth_dev_probing_finish(eth_dev);
1035 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1036 if (kvlist == NULL) {
1037 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1041 if (dev->device.numa_node == SOCKET_ID_ANY)
1042 dev->device.numa_node = rte_socket_id();
1044 if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1045 &xsk_queue_cnt, &pmd_zc) < 0) {
1046 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1050 if (strlen(if_name) == 0) {
1051 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1055 eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1056 xsk_queue_cnt, pmd_zc);
1057 if (eth_dev == NULL) {
1058 AF_XDP_LOG(ERR, "Failed to init internals\n");
1062 rte_eth_dev_probing_finish(eth_dev);
1068 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1070 struct rte_eth_dev *eth_dev = NULL;
1072 AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1078 /* find the ethdev entry */
1079 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1080 if (eth_dev == NULL)
1083 eth_dev_close(eth_dev);
1084 rte_eth_dev_release_port(eth_dev);
1090 static struct rte_vdev_driver pmd_af_xdp_drv = {
1091 .probe = rte_pmd_af_xdp_probe,
1092 .remove = rte_pmd_af_xdp_remove,
1095 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1096 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1098 "start_queue=<int> "
1099 "queue_count=<int> "
1100 "pmd_zero_copy=<0|1>");
1102 RTE_INIT(af_xdp_init_log)
1104 af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
1105 if (af_xdp_logtype >= 0)
1106 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);