net/af_xdp: support multi-queue
[dpdk.git] / drivers / net / af_xdp / rte_eth_af_xdp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation.
3  */
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <netinet/in.h>
9 #include <net/if.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"
18 #include <bpf/xsk.h>
19
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>
29 #include <rte_dev.h>
30 #include <rte_eal.h>
31 #include <rte_ether.h>
32 #include <rte_lcore.h>
33 #include <rte_log.h>
34 #include <rte_memory.h>
35 #include <rte_memzone.h>
36 #include <rte_mbuf.h>
37 #include <rte_malloc.h>
38 #include <rte_ring.h>
39
40 #ifndef SOL_XDP
41 #define SOL_XDP 283
42 #endif
43
44 #ifndef AF_XDP
45 #define AF_XDP 44
46 #endif
47
48 #ifndef PF_XDP
49 #define PF_XDP AF_XDP
50 #endif
51
52 static int af_xdp_logtype;
53
54 #define AF_XDP_LOG(level, fmt, args...)                 \
55         rte_log(RTE_LOG_ ## level, af_xdp_logtype,      \
56                 "%s(): " fmt, __func__, ##args)
57
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
64
65 #define ETH_AF_XDP_RX_BATCH_SIZE        32
66 #define ETH_AF_XDP_TX_BATCH_SIZE        32
67
68
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;
75         int pmd_zc;
76 };
77
78 struct rx_stats {
79         uint64_t rx_pkts;
80         uint64_t rx_bytes;
81         uint64_t rx_dropped;
82 };
83
84 struct pkt_rx_queue {
85         struct xsk_ring_cons rx;
86         struct xsk_umem_info *umem;
87         struct xsk_socket *xsk;
88         struct rte_mempool *mb_pool;
89
90         struct rx_stats stats;
91
92         struct pkt_tx_queue *pair;
93         int xsk_queue_idx;
94 };
95
96 struct tx_stats {
97         uint64_t tx_pkts;
98         uint64_t err_pkts;
99         uint64_t tx_bytes;
100 };
101
102 struct pkt_tx_queue {
103         struct xsk_ring_prod tx;
104
105         struct tx_stats stats;
106
107         struct pkt_rx_queue *pair;
108         int xsk_queue_idx;
109 };
110
111 struct pmd_internals {
112         int if_index;
113         char if_name[IFNAMSIZ];
114         int start_queue_idx;
115         int queue_cnt;
116         int max_queue_cnt;
117         int combined_queue_cnt;
118
119         int pmd_zc;
120         struct rte_ether_addr eth_addr;
121         struct rte_mempool *mb_pool_share;
122
123         struct pkt_rx_queue *rx_queues;
124         struct pkt_tx_queue *tx_queues;
125 };
126
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"
131
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,
137         NULL
138 };
139
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
145 };
146
147 static inline int
148 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size)
149 {
150         struct xsk_ring_prod *fq = &umem->fq;
151         void *addrs[reserve_size];
152         uint32_t idx;
153         uint16_t i;
154
155         if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
156                     != reserve_size) {
157                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
158                 return -1;
159         }
160
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,
164                                 reserve_size, NULL);
165                 return -1;
166         }
167
168         for (i = 0; i < reserve_size; i++) {
169                 __u64 *fq_addr;
170
171                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
172                 *fq_addr = (uint64_t)addrs[i];
173         }
174
175         xsk_ring_prod__submit(fq, reserve_size);
176
177         return 0;
178 }
179
180 static void
181 umem_buf_release_to_fq(void *addr, void *opaque)
182 {
183         struct xsk_umem_info *umem = (struct xsk_umem_info *)opaque;
184         uint64_t umem_addr = (uint64_t)addr - umem->mz->addr_64;
185
186         rte_ring_enqueue(umem->buf_ring, (void *)umem_addr);
187 }
188
189 static uint16_t
190 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
191 {
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;
196         uint32_t idx_rx = 0;
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;
202         int rcvd, i;
203
204         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
205
206         if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
207                 return 0;
208
209         rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
210         if (rcvd == 0)
211                 goto out;
212
213         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
214                 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
215
216         for (i = 0; i < rcvd; i++) {
217                 const struct xdp_desc *desc;
218                 uint64_t addr;
219                 uint32_t len;
220                 void *pkt;
221                 uint16_t buf_len = ETH_AF_XDP_FRAME_SIZE;
222                 struct rte_mbuf_ext_shared_info *shinfo;
223
224                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
225                 addr = desc->addr;
226                 len = desc->len;
227                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
228
229                 if (pmd_zc) {
230                         shinfo = rte_pktmbuf_ext_shinfo_init_helper(pkt,
231                                         &buf_len, umem_buf_release_to_fq, umem);
232
233                         rte_pktmbuf_attach_extbuf(mbufs[i], pkt, 0, buf_len,
234                                                   shinfo);
235                 } else {
236                         rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *),
237                                                         pkt, len);
238                         rte_ring_enqueue(umem->buf_ring, (void *)addr);
239                 }
240                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
241                 rte_pktmbuf_data_len(mbufs[i]) = len;
242                 rx_bytes += len;
243                 bufs[i] = mbufs[i];
244         }
245
246         xsk_ring_cons__release(rx, rcvd);
247
248         /* statistics */
249         rxq->stats.rx_pkts += (rcvd - dropped);
250         rxq->stats.rx_bytes += rx_bytes;
251
252 out:
253         if (rcvd != nb_pkts)
254                 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
255                                      nb_pkts - rcvd);
256
257         return rcvd;
258 }
259
260 static void
261 pull_umem_cq(struct xsk_umem_info *umem, int size)
262 {
263         struct xsk_ring_cons *cq = &umem->cq;
264         size_t i, n;
265         uint32_t idx_cq = 0;
266
267         n = xsk_ring_cons__peek(cq, size, &idx_cq);
268
269         for (i = 0; i < n; i++) {
270                 uint64_t addr;
271                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
272                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
273         }
274
275         xsk_ring_cons__release(cq, n);
276 }
277
278 static void
279 kick_tx(struct pkt_tx_queue *txq)
280 {
281         struct xsk_umem_info *umem = txq->pair->umem;
282
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)
287                         break;
288
289                 /* pull from completion queue to leave more space */
290                 if (errno == EAGAIN)
291                         pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
292         }
293         pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
294 }
295
296 static inline bool
297 in_umem_range(struct xsk_umem_info *umem, uint64_t addr)
298 {
299         uint64_t mz_base_addr = umem->mz->addr_64;
300
301         return addr >= mz_base_addr && addr < mz_base_addr + umem->mz->len;
302 }
303
304 static uint16_t
305 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
306 {
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;
313         int i;
314         uint32_t idx_tx;
315
316         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
317
318         pull_umem_cq(umem, nb_pkts);
319
320         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
321                                         nb_pkts, NULL);
322         if (nb_pkts == 0)
323                 return 0;
324
325         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
326                 kick_tx(txq);
327                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
328                 return 0;
329         }
330
331         for (i = 0; i < nb_pkts; i++) {
332                 struct xdp_desc *desc;
333                 void *pkt;
334
335                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
336                 mbuf = bufs[i];
337                 desc->len = mbuf->pkt_len;
338
339                 /*
340                  * We need to make sure the external mbuf address is within
341                  * current port's umem memzone range
342                  */
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 -
346                                 umem->mz->addr_64;
347                         mbuf->buf_addr = xsk_umem__get_data(umem->mz->addr,
348                                         (uint64_t)addrs[i]);
349                 } else {
350                         desc->addr = (uint64_t)addrs[i];
351                         pkt = xsk_umem__get_data(umem->mz->addr,
352                                         desc->addr);
353                         rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
354                                         desc->len);
355                 }
356                 tx_bytes += mbuf->pkt_len;
357         }
358
359         xsk_ring_prod__submit(&txq->tx, nb_pkts);
360
361         kick_tx(txq);
362
363         txq->stats.tx_pkts += nb_pkts;
364         txq->stats.tx_bytes += tx_bytes;
365
366         for (i = 0; i < nb_pkts; i++)
367                 rte_pktmbuf_free(bufs[i]);
368
369         return nb_pkts;
370 }
371
372 static int
373 eth_dev_start(struct rte_eth_dev *dev)
374 {
375         dev->data->dev_link.link_status = ETH_LINK_UP;
376
377         return 0;
378 }
379
380 /* This function gets called when the current port gets stopped. */
381 static void
382 eth_dev_stop(struct rte_eth_dev *dev)
383 {
384         dev->data->dev_link.link_status = ETH_LINK_DOWN;
385 }
386
387 static int
388 eth_dev_configure(struct rte_eth_dev *dev)
389 {
390         /* rx/tx must be paired */
391         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
392                 return -EINVAL;
393
394         return 0;
395 }
396
397 static void
398 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
399 {
400         struct pmd_internals *internals = dev->data->dev_private;
401
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;
407
408         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
409         dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
410
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;
415 }
416
417 static int
418 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
419 {
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;
424         socklen_t optlen;
425         int i, ret;
426
427         for (i = 0; i < dev->data->nb_rx_queues; i++) {
428                 optlen = sizeof(struct xdp_statistics);
429                 rxq = &internals->rx_queues[i];
430                 txq = rxq->pair;
431                 stats->q_ipackets[i] = rxq->stats.rx_pkts;
432                 stats->q_ibytes[i] = rxq->stats.rx_bytes;
433
434                 stats->q_opackets[i] = txq->stats.tx_pkts;
435                 stats->q_obytes[i] = txq->stats.tx_bytes;
436
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);
442                 if (ret != 0) {
443                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
444                         return -1;
445                 }
446                 stats->imissed += xdp_stats.rx_dropped;
447
448                 stats->opackets += stats->q_opackets[i];
449                 stats->oerrors += txq->stats.err_pkts;
450                 stats->obytes += stats->q_obytes[i];
451         }
452
453         return 0;
454 }
455
456 static void
457 eth_stats_reset(struct rte_eth_dev *dev)
458 {
459         struct pmd_internals *internals = dev->data->dev_private;
460         int i;
461
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));
467         }
468 }
469
470 static void
471 remove_xdp_program(struct pmd_internals *internals)
472 {
473         uint32_t curr_prog_id = 0;
474
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");
478                 return;
479         }
480         bpf_set_link_xdp_fd(internals->if_index, -1,
481                         XDP_FLAGS_UPDATE_IF_NOEXIST);
482 }
483
484 static void
485 xdp_umem_destroy(struct xsk_umem_info *umem)
486 {
487         rte_memzone_free(umem->mz);
488         umem->mz = NULL;
489
490         rte_ring_free(umem->buf_ring);
491         umem->buf_ring = NULL;
492
493         rte_free(umem);
494         umem = NULL;
495 }
496
497 static void
498 eth_dev_close(struct rte_eth_dev *dev)
499 {
500         struct pmd_internals *internals = dev->data->dev_private;
501         struct pkt_rx_queue *rxq;
502         int i;
503
504         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
505                 rte_socket_id());
506
507         for (i = 0; i < internals->queue_cnt; i++) {
508                 rxq = &internals->rx_queues[i];
509                 if (rxq->umem == NULL)
510                         break;
511                 xsk_socket__delete(rxq->xsk);
512                 (void)xsk_umem__delete(rxq->umem->umem);
513                 xdp_umem_destroy(rxq->umem);
514
515                 /* free pkt_tx_queue */
516                 rte_free(rxq->pair);
517                 rte_free(rxq);
518         }
519
520         /*
521          * MAC is not allocated dynamically, setting it to NULL would prevent
522          * from releasing it in rte_eth_dev_release_port.
523          */
524         dev->data->mac_addrs = NULL;
525
526         remove_xdp_program(internals);
527 }
528
529 static void
530 eth_queue_release(void *q __rte_unused)
531 {
532 }
533
534 static int
535 eth_link_update(struct rte_eth_dev *dev __rte_unused,
536                 int wait_to_complete __rte_unused)
537 {
538         return 0;
539 }
540
541 static struct
542 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
543                                   struct pkt_rx_queue *rxq)
544 {
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];
554         int ret;
555         uint64_t i;
556
557         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
558         if (umem == NULL) {
559                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
560                 return NULL;
561         }
562
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,
567                                          rte_socket_id(),
568                                          0x0);
569         if (umem->buf_ring == NULL) {
570                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
571                 goto err;
572         }
573
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));
578
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,
584                         getpagesize());
585         if (mz == NULL) {
586                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
587                 goto err;
588         }
589
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,
593                                &usr_config);
594
595         if (ret) {
596                 AF_XDP_LOG(ERR, "Failed to create umem");
597                 goto err;
598         }
599         umem->mz = mz;
600
601         return umem;
602
603 err:
604         xdp_umem_destroy(umem);
605         return NULL;
606 }
607
608 static int
609 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
610               int ring_size)
611 {
612         struct xsk_socket_config cfg;
613         struct pkt_tx_queue *txq = rxq->pair;
614         int ret = 0;
615         int reserve_size;
616
617         rxq->umem = xdp_umem_configure(internals, rxq);
618         if (rxq->umem == NULL)
619                 return -ENOMEM;
620
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;
625         cfg.bind_flags = 0;
626         ret = xsk_socket__create(&rxq->xsk, internals->if_name,
627                         rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
628                         &txq->tx, &cfg);
629         if (ret) {
630                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
631                 goto err;
632         }
633
634         reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
635         ret = reserve_fill_queue(rxq->umem, reserve_size);
636         if (ret) {
637                 xsk_socket__delete(rxq->xsk);
638                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
639                 goto err;
640         }
641
642         return 0;
643
644 err:
645         xdp_umem_destroy(rxq->umem);
646
647         return ret;
648 }
649
650 static int
651 eth_rx_queue_setup(struct rte_eth_dev *dev,
652                    uint16_t rx_queue_id,
653                    uint16_t nb_rx_desc,
654                    unsigned int socket_id __rte_unused,
655                    const struct rte_eth_rxconf *rx_conf __rte_unused,
656                    struct rte_mempool *mb_pool)
657 {
658         struct pmd_internals *internals = dev->data->dev_private;
659         uint32_t buf_size, data_size;
660         struct pkt_rx_queue *rxq;
661         int ret;
662
663         rxq = &internals->rx_queues[rx_queue_id];
664
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;
671
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);
675                 ret = -ENOMEM;
676                 goto err;
677         }
678
679         rxq->mb_pool = mb_pool;
680
681         if (xsk_configure(internals, rxq, nb_rx_desc)) {
682                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
683                 ret = -EINVAL;
684                 goto err;
685         }
686
687         rxq->umem->pmd_zc = internals->pmd_zc;
688
689         dev->data->rx_queues[rx_queue_id] = rxq;
690         return 0;
691
692 err:
693         return ret;
694 }
695
696 static int
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)
702 {
703         struct pmd_internals *internals = dev->data->dev_private;
704         struct pkt_tx_queue *txq;
705
706         txq = &internals->tx_queues[tx_queue_id];
707
708         dev->data->tx_queues[tx_queue_id] = txq;
709         return 0;
710 }
711
712 static int
713 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
714 {
715         struct pmd_internals *internals = dev->data->dev_private;
716         struct ifreq ifr = { .ifr_mtu = mtu };
717         int ret;
718         int s;
719
720         s = socket(PF_INET, SOCK_DGRAM, 0);
721         if (s < 0)
722                 return -EINVAL;
723
724         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
725         ret = ioctl(s, SIOCSIFMTU, &ifr);
726         close(s);
727
728         return (ret < 0) ? -errno : 0;
729 }
730
731 static void
732 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
733 {
734         struct ifreq ifr;
735         int s;
736
737         s = socket(PF_INET, SOCK_DGRAM, 0);
738         if (s < 0)
739                 return;
740
741         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
742         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
743                 goto out;
744         ifr.ifr_flags &= mask;
745         ifr.ifr_flags |= flags;
746         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
747                 goto out;
748 out:
749         close(s);
750 }
751
752 static void
753 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
754 {
755         struct pmd_internals *internals = dev->data->dev_private;
756
757         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
758 }
759
760 static void
761 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
762 {
763         struct pmd_internals *internals = dev->data->dev_private;
764
765         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
766 }
767
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,
784 };
785
786 /** parse integer from integer argument */
787 static int
788 parse_integer_arg(const char *key __rte_unused,
789                   const char *value, void *extra_args)
790 {
791         int *i = (int *)extra_args;
792         char *end;
793
794         *i = strtol(value, &end, 10);
795         if (*i < 0) {
796                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
797                 return -EINVAL;
798         }
799
800         return 0;
801 }
802
803 /** parse name argument */
804 static int
805 parse_name_arg(const char *key __rte_unused,
806                const char *value, void *extra_args)
807 {
808         char *name = extra_args;
809
810         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
811                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
812                            value, IFNAMSIZ);
813                 return -EINVAL;
814         }
815
816         strlcpy(name, value, IFNAMSIZ);
817
818         return 0;
819 }
820
821 static int
822 xdp_get_channels_info(const char *if_name, int *max_queues,
823                                 int *combined_queues)
824 {
825         struct ethtool_channels channels;
826         struct ifreq ifr;
827         int fd, ret;
828
829         fd = socket(AF_INET, SOCK_DGRAM, 0);
830         if (fd < 0)
831                 return -1;
832
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) {
838                 ret = -errno;
839                 goto out;
840         }
841
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.
845                  */
846                 *max_queues = 1;
847                 *combined_queues = 1;
848         } else {
849                 *max_queues = channels.max_combined;
850                 *combined_queues = channels.combined_count;
851         }
852
853  out:
854         close(fd);
855         return ret;
856 }
857
858 static int
859 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
860                         int *queue_cnt, int *pmd_zc)
861 {
862         int ret;
863
864         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
865                                  &parse_name_arg, if_name);
866         if (ret < 0)
867                 goto free_kvlist;
868
869         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
870                                  &parse_integer_arg, start_queue);
871         if (ret < 0)
872                 goto free_kvlist;
873
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) {
877                 ret = -EINVAL;
878                 goto free_kvlist;
879         }
880
881         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PMD_ZC_ARG,
882                                  &parse_integer_arg, pmd_zc);
883         if (ret < 0)
884                 goto free_kvlist;
885
886 free_kvlist:
887         rte_kvargs_free(kvlist);
888         return ret;
889 }
890
891 static int
892 get_iface_info(const char *if_name,
893                struct rte_ether_addr *eth_addr,
894                int *if_index)
895 {
896         struct ifreq ifr;
897         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
898
899         if (sock < 0)
900                 return -1;
901
902         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
903         if (ioctl(sock, SIOCGIFINDEX, &ifr))
904                 goto error;
905
906         *if_index = ifr.ifr_ifindex;
907
908         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
909                 goto error;
910
911         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
912
913         close(sock);
914         return 0;
915
916 error:
917         close(sock);
918         return -1;
919 }
920
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)
924 {
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;
929         int ret;
930         int i;
931
932         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
933         if (internals == NULL)
934                 return NULL;
935
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);
940
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",
944                                 if_name);
945                 goto err_free_internals;
946         }
947
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;
952         }
953
954         internals->rx_queues = rte_zmalloc_socket(NULL,
955                                         sizeof(struct pkt_rx_queue) * queue_cnt,
956                                         0, numa_node);
957         if (internals->rx_queues == NULL) {
958                 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
959                 goto err_free_internals;
960         }
961
962         internals->tx_queues = rte_zmalloc_socket(NULL,
963                                         sizeof(struct pkt_tx_queue) * queue_cnt,
964                                         0, numa_node);
965         if (internals->tx_queues == NULL) {
966                 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
967                 goto err_free_rx;
968         }
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;
974         }
975
976         ret = get_iface_info(if_name, &internals->eth_addr,
977                              &internals->if_index);
978         if (ret)
979                 goto err_free_tx;
980
981         eth_dev = rte_eth_vdev_allocate(dev, 0);
982         if (eth_dev == NULL)
983                 goto err_free_tx;
984
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;
993
994         if (internals->pmd_zc)
995                 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
996
997         return eth_dev;
998
999 err_free_tx:
1000         rte_free(internals->tx_queues);
1001 err_free_rx:
1002         rte_free(internals->rx_queues);
1003 err_free_internals:
1004         rte_free(internals);
1005         return NULL;
1006 }
1007
1008 static int
1009 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1010 {
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;
1016         const char *name;
1017         int pmd_zc = 0;
1018
1019         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1020                 rte_vdev_device_name(dev));
1021
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);
1028                         return -EINVAL;
1029                 }
1030                 eth_dev->dev_ops = &ops;
1031                 rte_eth_dev_probing_finish(eth_dev);
1032                 return 0;
1033         }
1034
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");
1038                 return -EINVAL;
1039         }
1040
1041         if (dev->device.numa_node == SOCKET_ID_ANY)
1042                 dev->device.numa_node = rte_socket_id();
1043
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");
1047                 return -EINVAL;
1048         }
1049
1050         if (strlen(if_name) == 0) {
1051                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1052                 return -EINVAL;
1053         }
1054
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");
1059                 return -1;
1060         }
1061
1062         rte_eth_dev_probing_finish(eth_dev);
1063
1064         return 0;
1065 }
1066
1067 static int
1068 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1069 {
1070         struct rte_eth_dev *eth_dev = NULL;
1071
1072         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1073                 rte_socket_id());
1074
1075         if (dev == NULL)
1076                 return -1;
1077
1078         /* find the ethdev entry */
1079         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1080         if (eth_dev == NULL)
1081                 return 0;
1082
1083         eth_dev_close(eth_dev);
1084         rte_eth_dev_release_port(eth_dev);
1085
1086
1087         return 0;
1088 }
1089
1090 static struct rte_vdev_driver pmd_af_xdp_drv = {
1091         .probe = rte_pmd_af_xdp_probe,
1092         .remove = rte_pmd_af_xdp_remove,
1093 };
1094
1095 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1096 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1097                               "iface=<string> "
1098                               "start_queue=<int> "
1099                               "queue_count=<int> "
1100                               "pmd_zero_copy=<0|1>");
1101
1102 RTE_INIT(af_xdp_init_log)
1103 {
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);
1107 }