ethdev: change device info get callback to return int
[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 <poll.h>
9 #include <netinet/in.h>
10 #include <net/if.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"
19 #include <bpf/xsk.h>
20
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>
30 #include <rte_dev.h>
31 #include <rte_eal.h>
32 #include <rte_ether.h>
33 #include <rte_lcore.h>
34 #include <rte_log.h>
35 #include <rte_memory.h>
36 #include <rte_memzone.h>
37 #include <rte_mbuf.h>
38 #include <rte_malloc.h>
39 #include <rte_ring.h>
40
41 #ifndef SOL_XDP
42 #define SOL_XDP 283
43 #endif
44
45 #ifndef AF_XDP
46 #define AF_XDP 44
47 #endif
48
49 #ifndef PF_XDP
50 #define PF_XDP AF_XDP
51 #endif
52
53 static int af_xdp_logtype;
54
55 #define AF_XDP_LOG(level, fmt, args...)                 \
56         rte_log(RTE_LOG_ ## level, af_xdp_logtype,      \
57                 "%s(): " fmt, __func__, ##args)
58
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
65
66 #define ETH_AF_XDP_RX_BATCH_SIZE        32
67 #define ETH_AF_XDP_TX_BATCH_SIZE        32
68
69
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;
76         int pmd_zc;
77 };
78
79 struct rx_stats {
80         uint64_t rx_pkts;
81         uint64_t rx_bytes;
82         uint64_t rx_dropped;
83 };
84
85 struct pkt_rx_queue {
86         struct xsk_ring_cons rx;
87         struct xsk_umem_info *umem;
88         struct xsk_socket *xsk;
89         struct rte_mempool *mb_pool;
90
91         struct rx_stats stats;
92
93         struct pkt_tx_queue *pair;
94         struct pollfd fds[1];
95         int xsk_queue_idx;
96 };
97
98 struct tx_stats {
99         uint64_t tx_pkts;
100         uint64_t tx_bytes;
101 };
102
103 struct pkt_tx_queue {
104         struct xsk_ring_prod tx;
105
106         struct tx_stats stats;
107
108         struct pkt_rx_queue *pair;
109         int xsk_queue_idx;
110 };
111
112 struct pmd_internals {
113         int if_index;
114         char if_name[IFNAMSIZ];
115         int start_queue_idx;
116         int queue_cnt;
117         int max_queue_cnt;
118         int combined_queue_cnt;
119
120         int pmd_zc;
121         struct rte_ether_addr eth_addr;
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 #if defined(XDP_USE_NEED_WAKEUP)
212                 if (xsk_ring_prod__needs_wakeup(fq))
213                         (void)poll(rxq->fds, 1, 1000);
214 #endif
215
216                 goto out;
217         }
218
219         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
220                 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
221
222         for (i = 0; i < rcvd; i++) {
223                 const struct xdp_desc *desc;
224                 uint64_t addr;
225                 uint32_t len;
226                 void *pkt;
227                 uint16_t buf_len = ETH_AF_XDP_FRAME_SIZE;
228                 struct rte_mbuf_ext_shared_info *shinfo;
229
230                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
231                 addr = desc->addr;
232                 len = desc->len;
233                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
234
235                 if (pmd_zc) {
236                         shinfo = rte_pktmbuf_ext_shinfo_init_helper(pkt,
237                                         &buf_len, umem_buf_release_to_fq, umem);
238
239                         rte_pktmbuf_attach_extbuf(mbufs[i], pkt, 0, buf_len,
240                                                   shinfo);
241                 } else {
242                         rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *),
243                                                         pkt, len);
244                         rte_ring_enqueue(umem->buf_ring, (void *)addr);
245                 }
246                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
247                 rte_pktmbuf_data_len(mbufs[i]) = len;
248                 rx_bytes += len;
249                 bufs[i] = mbufs[i];
250         }
251
252         xsk_ring_cons__release(rx, rcvd);
253
254         /* statistics */
255         rxq->stats.rx_pkts += (rcvd - dropped);
256         rxq->stats.rx_bytes += rx_bytes;
257
258 out:
259         if (rcvd != nb_pkts)
260                 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
261                                      nb_pkts - rcvd);
262
263         return rcvd;
264 }
265
266 static void
267 pull_umem_cq(struct xsk_umem_info *umem, int size)
268 {
269         struct xsk_ring_cons *cq = &umem->cq;
270         size_t i, n;
271         uint32_t idx_cq = 0;
272
273         n = xsk_ring_cons__peek(cq, size, &idx_cq);
274
275         for (i = 0; i < n; i++) {
276                 uint64_t addr;
277                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
278                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
279         }
280
281         xsk_ring_cons__release(cq, n);
282 }
283
284 static void
285 kick_tx(struct pkt_tx_queue *txq)
286 {
287         struct xsk_umem_info *umem = txq->pair->umem;
288
289 #if defined(XDP_USE_NEED_WAKEUP)
290         if (xsk_ring_prod__needs_wakeup(&txq->tx))
291 #endif
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)
296                                 break;
297
298                         /* pull from completion queue to leave more space */
299                         if (errno == EAGAIN)
300                                 pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
301                 }
302         pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
303 }
304
305 static inline bool
306 in_umem_range(struct xsk_umem_info *umem, uint64_t addr)
307 {
308         uint64_t mz_base_addr = umem->mz->addr_64;
309
310         return addr >= mz_base_addr && addr < mz_base_addr + umem->mz->len;
311 }
312
313 static uint16_t
314 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
315 {
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;
322         int i;
323         uint32_t idx_tx;
324
325         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
326
327         pull_umem_cq(umem, nb_pkts);
328
329         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
330                                         nb_pkts, NULL);
331         if (nb_pkts == 0)
332                 return 0;
333
334         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
335                 kick_tx(txq);
336                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
337                 return 0;
338         }
339
340         for (i = 0; i < nb_pkts; i++) {
341                 struct xdp_desc *desc;
342                 void *pkt;
343
344                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
345                 mbuf = bufs[i];
346                 desc->len = mbuf->pkt_len;
347
348                 /*
349                  * We need to make sure the external mbuf address is within
350                  * current port's umem memzone range
351                  */
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 -
355                                 umem->mz->addr_64;
356                         mbuf->buf_addr = xsk_umem__get_data(umem->mz->addr,
357                                         (uint64_t)addrs[i]);
358                 } else {
359                         desc->addr = (uint64_t)addrs[i];
360                         pkt = xsk_umem__get_data(umem->mz->addr,
361                                         desc->addr);
362                         rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
363                                         desc->len);
364                 }
365                 tx_bytes += mbuf->pkt_len;
366         }
367
368         xsk_ring_prod__submit(&txq->tx, nb_pkts);
369
370         kick_tx(txq);
371
372         txq->stats.tx_pkts += nb_pkts;
373         txq->stats.tx_bytes += tx_bytes;
374
375         for (i = 0; i < nb_pkts; i++)
376                 rte_pktmbuf_free(bufs[i]);
377
378         return nb_pkts;
379 }
380
381 static int
382 eth_dev_start(struct rte_eth_dev *dev)
383 {
384         dev->data->dev_link.link_status = ETH_LINK_UP;
385
386         return 0;
387 }
388
389 /* This function gets called when the current port gets stopped. */
390 static void
391 eth_dev_stop(struct rte_eth_dev *dev)
392 {
393         dev->data->dev_link.link_status = ETH_LINK_DOWN;
394 }
395
396 static int
397 eth_dev_configure(struct rte_eth_dev *dev)
398 {
399         /* rx/tx must be paired */
400         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
401                 return -EINVAL;
402
403         return 0;
404 }
405
406 static int
407 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
408 {
409         struct pmd_internals *internals = dev->data->dev_private;
410
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;
416
417         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
418         dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
419
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;
424
425         return 0;
426 }
427
428 static int
429 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
430 {
431         struct pmd_internals *internals = dev->data->dev_private;
432         struct xdp_statistics xdp_stats;
433         struct pkt_rx_queue *rxq;
434         struct pkt_tx_queue *txq;
435         socklen_t optlen;
436         int i, ret;
437
438         for (i = 0; i < dev->data->nb_rx_queues; i++) {
439                 optlen = sizeof(struct xdp_statistics);
440                 rxq = &internals->rx_queues[i];
441                 txq = rxq->pair;
442                 stats->q_ipackets[i] = rxq->stats.rx_pkts;
443                 stats->q_ibytes[i] = rxq->stats.rx_bytes;
444
445                 stats->q_opackets[i] = txq->stats.tx_pkts;
446                 stats->q_obytes[i] = txq->stats.tx_bytes;
447
448                 stats->ipackets += stats->q_ipackets[i];
449                 stats->ibytes += stats->q_ibytes[i];
450                 stats->imissed += rxq->stats.rx_dropped;
451                 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
452                                 XDP_STATISTICS, &xdp_stats, &optlen);
453                 if (ret != 0) {
454                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
455                         return -1;
456                 }
457                 stats->imissed += xdp_stats.rx_dropped;
458
459                 stats->opackets += stats->q_opackets[i];
460                 stats->obytes += stats->q_obytes[i];
461         }
462
463         return 0;
464 }
465
466 static void
467 eth_stats_reset(struct rte_eth_dev *dev)
468 {
469         struct pmd_internals *internals = dev->data->dev_private;
470         int i;
471
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));
477         }
478 }
479
480 static void
481 remove_xdp_program(struct pmd_internals *internals)
482 {
483         uint32_t curr_prog_id = 0;
484
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");
488                 return;
489         }
490         bpf_set_link_xdp_fd(internals->if_index, -1,
491                         XDP_FLAGS_UPDATE_IF_NOEXIST);
492 }
493
494 static void
495 xdp_umem_destroy(struct xsk_umem_info *umem)
496 {
497         rte_memzone_free(umem->mz);
498         umem->mz = NULL;
499
500         rte_ring_free(umem->buf_ring);
501         umem->buf_ring = NULL;
502
503         rte_free(umem);
504         umem = NULL;
505 }
506
507 static void
508 eth_dev_close(struct rte_eth_dev *dev)
509 {
510         struct pmd_internals *internals = dev->data->dev_private;
511         struct pkt_rx_queue *rxq;
512         int i;
513
514         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
515                 rte_socket_id());
516
517         for (i = 0; i < internals->queue_cnt; i++) {
518                 rxq = &internals->rx_queues[i];
519                 if (rxq->umem == NULL)
520                         break;
521                 xsk_socket__delete(rxq->xsk);
522                 (void)xsk_umem__delete(rxq->umem->umem);
523                 xdp_umem_destroy(rxq->umem);
524
525                 /* free pkt_tx_queue */
526                 rte_free(rxq->pair);
527                 rte_free(rxq);
528         }
529
530         /*
531          * MAC is not allocated dynamically, setting it to NULL would prevent
532          * from releasing it in rte_eth_dev_release_port.
533          */
534         dev->data->mac_addrs = NULL;
535
536         remove_xdp_program(internals);
537 }
538
539 static void
540 eth_queue_release(void *q __rte_unused)
541 {
542 }
543
544 static int
545 eth_link_update(struct rte_eth_dev *dev __rte_unused,
546                 int wait_to_complete __rte_unused)
547 {
548         return 0;
549 }
550
551 static struct
552 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
553                                   struct pkt_rx_queue *rxq)
554 {
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];
564         int ret;
565         uint64_t i;
566
567         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
568         if (umem == NULL) {
569                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
570                 return NULL;
571         }
572
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,
577                                          rte_socket_id(),
578                                          0x0);
579         if (umem->buf_ring == NULL) {
580                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
581                 goto err;
582         }
583
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));
588
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,
594                         getpagesize());
595         if (mz == NULL) {
596                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
597                 goto err;
598         }
599
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,
603                                &usr_config);
604
605         if (ret) {
606                 AF_XDP_LOG(ERR, "Failed to create umem");
607                 goto err;
608         }
609         umem->mz = mz;
610
611         return umem;
612
613 err:
614         xdp_umem_destroy(umem);
615         return NULL;
616 }
617
618 static int
619 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
620               int ring_size)
621 {
622         struct xsk_socket_config cfg;
623         struct pkt_tx_queue *txq = rxq->pair;
624         int ret = 0;
625         int reserve_size;
626
627         rxq->umem = xdp_umem_configure(internals, rxq);
628         if (rxq->umem == NULL)
629                 return -ENOMEM;
630
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;
635         cfg.bind_flags = 0;
636
637 #if defined(XDP_USE_NEED_WAKEUP)
638         cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
639 #endif
640
641         ret = xsk_socket__create(&rxq->xsk, internals->if_name,
642                         rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
643                         &txq->tx, &cfg);
644         if (ret) {
645                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
646                 goto err;
647         }
648
649         reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
650         ret = reserve_fill_queue(rxq->umem, reserve_size);
651         if (ret) {
652                 xsk_socket__delete(rxq->xsk);
653                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
654                 goto err;
655         }
656
657         return 0;
658
659 err:
660         xdp_umem_destroy(rxq->umem);
661
662         return ret;
663 }
664
665 static int
666 eth_rx_queue_setup(struct rte_eth_dev *dev,
667                    uint16_t rx_queue_id,
668                    uint16_t nb_rx_desc,
669                    unsigned int socket_id __rte_unused,
670                    const struct rte_eth_rxconf *rx_conf __rte_unused,
671                    struct rte_mempool *mb_pool)
672 {
673         struct pmd_internals *internals = dev->data->dev_private;
674         uint32_t buf_size, data_size;
675         struct pkt_rx_queue *rxq;
676         int ret;
677
678         rxq = &internals->rx_queues[rx_queue_id];
679
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;
686
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);
690                 ret = -ENOMEM;
691                 goto err;
692         }
693
694         rxq->mb_pool = mb_pool;
695
696         if (xsk_configure(internals, rxq, nb_rx_desc)) {
697                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
698                 ret = -EINVAL;
699                 goto err;
700         }
701
702         rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
703         rxq->fds[0].events = POLLIN;
704
705         rxq->umem->pmd_zc = internals->pmd_zc;
706
707         dev->data->rx_queues[rx_queue_id] = rxq;
708         return 0;
709
710 err:
711         return ret;
712 }
713
714 static int
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)
720 {
721         struct pmd_internals *internals = dev->data->dev_private;
722         struct pkt_tx_queue *txq;
723
724         txq = &internals->tx_queues[tx_queue_id];
725
726         dev->data->tx_queues[tx_queue_id] = txq;
727         return 0;
728 }
729
730 static int
731 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
732 {
733         struct pmd_internals *internals = dev->data->dev_private;
734         struct ifreq ifr = { .ifr_mtu = mtu };
735         int ret;
736         int s;
737
738         s = socket(PF_INET, SOCK_DGRAM, 0);
739         if (s < 0)
740                 return -EINVAL;
741
742         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
743         ret = ioctl(s, SIOCSIFMTU, &ifr);
744         close(s);
745
746         return (ret < 0) ? -errno : 0;
747 }
748
749 static void
750 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
751 {
752         struct ifreq ifr;
753         int s;
754
755         s = socket(PF_INET, SOCK_DGRAM, 0);
756         if (s < 0)
757                 return;
758
759         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
760         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
761                 goto out;
762         ifr.ifr_flags &= mask;
763         ifr.ifr_flags |= flags;
764         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
765                 goto out;
766 out:
767         close(s);
768 }
769
770 static void
771 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
772 {
773         struct pmd_internals *internals = dev->data->dev_private;
774
775         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
776 }
777
778 static void
779 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
780 {
781         struct pmd_internals *internals = dev->data->dev_private;
782
783         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
784 }
785
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,
802 };
803
804 /** parse integer from integer argument */
805 static int
806 parse_integer_arg(const char *key __rte_unused,
807                   const char *value, void *extra_args)
808 {
809         int *i = (int *)extra_args;
810         char *end;
811
812         *i = strtol(value, &end, 10);
813         if (*i < 0) {
814                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
815                 return -EINVAL;
816         }
817
818         return 0;
819 }
820
821 /** parse name argument */
822 static int
823 parse_name_arg(const char *key __rte_unused,
824                const char *value, void *extra_args)
825 {
826         char *name = extra_args;
827
828         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
829                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
830                            value, IFNAMSIZ);
831                 return -EINVAL;
832         }
833
834         strlcpy(name, value, IFNAMSIZ);
835
836         return 0;
837 }
838
839 static int
840 xdp_get_channels_info(const char *if_name, int *max_queues,
841                                 int *combined_queues)
842 {
843         struct ethtool_channels channels;
844         struct ifreq ifr;
845         int fd, ret;
846
847         fd = socket(AF_INET, SOCK_DGRAM, 0);
848         if (fd < 0)
849                 return -1;
850
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) {
856                 if (errno == EOPNOTSUPP) {
857                         ret = 0;
858                 } else {
859                         ret = -errno;
860                         goto out;
861                 }
862         }
863
864         if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
865                 /* If the device says it has no channels, then all traffic
866                  * is sent to a single stream, so max queues = 1.
867                  */
868                 *max_queues = 1;
869                 *combined_queues = 1;
870         } else {
871                 *max_queues = channels.max_combined;
872                 *combined_queues = channels.combined_count;
873         }
874
875  out:
876         close(fd);
877         return ret;
878 }
879
880 static int
881 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
882                         int *queue_cnt, int *pmd_zc)
883 {
884         int ret;
885
886         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
887                                  &parse_name_arg, if_name);
888         if (ret < 0)
889                 goto free_kvlist;
890
891         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
892                                  &parse_integer_arg, start_queue);
893         if (ret < 0)
894                 goto free_kvlist;
895
896         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
897                                  &parse_integer_arg, queue_cnt);
898         if (ret < 0 || *queue_cnt <= 0) {
899                 ret = -EINVAL;
900                 goto free_kvlist;
901         }
902
903         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PMD_ZC_ARG,
904                                  &parse_integer_arg, pmd_zc);
905         if (ret < 0)
906                 goto free_kvlist;
907
908 free_kvlist:
909         rte_kvargs_free(kvlist);
910         return ret;
911 }
912
913 static int
914 get_iface_info(const char *if_name,
915                struct rte_ether_addr *eth_addr,
916                int *if_index)
917 {
918         struct ifreq ifr;
919         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
920
921         if (sock < 0)
922                 return -1;
923
924         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
925         if (ioctl(sock, SIOCGIFINDEX, &ifr))
926                 goto error;
927
928         *if_index = ifr.ifr_ifindex;
929
930         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
931                 goto error;
932
933         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
934
935         close(sock);
936         return 0;
937
938 error:
939         close(sock);
940         return -1;
941 }
942
943 static struct rte_eth_dev *
944 init_internals(struct rte_vdev_device *dev, const char *if_name,
945                         int start_queue_idx, int queue_cnt, int pmd_zc)
946 {
947         const char *name = rte_vdev_device_name(dev);
948         const unsigned int numa_node = dev->device.numa_node;
949         struct pmd_internals *internals;
950         struct rte_eth_dev *eth_dev;
951         int ret;
952         int i;
953
954         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
955         if (internals == NULL)
956                 return NULL;
957
958         internals->start_queue_idx = start_queue_idx;
959         internals->queue_cnt = queue_cnt;
960         internals->pmd_zc = pmd_zc;
961         strlcpy(internals->if_name, if_name, IFNAMSIZ);
962
963         if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
964                                   &internals->combined_queue_cnt)) {
965                 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
966                                 if_name);
967                 goto err_free_internals;
968         }
969
970         if (queue_cnt > internals->combined_queue_cnt) {
971                 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
972                                 queue_cnt, internals->combined_queue_cnt);
973                 goto err_free_internals;
974         }
975
976         internals->rx_queues = rte_zmalloc_socket(NULL,
977                                         sizeof(struct pkt_rx_queue) * queue_cnt,
978                                         0, numa_node);
979         if (internals->rx_queues == NULL) {
980                 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
981                 goto err_free_internals;
982         }
983
984         internals->tx_queues = rte_zmalloc_socket(NULL,
985                                         sizeof(struct pkt_tx_queue) * queue_cnt,
986                                         0, numa_node);
987         if (internals->tx_queues == NULL) {
988                 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
989                 goto err_free_rx;
990         }
991         for (i = 0; i < queue_cnt; i++) {
992                 internals->tx_queues[i].pair = &internals->rx_queues[i];
993                 internals->rx_queues[i].pair = &internals->tx_queues[i];
994                 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
995                 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
996         }
997
998         ret = get_iface_info(if_name, &internals->eth_addr,
999                              &internals->if_index);
1000         if (ret)
1001                 goto err_free_tx;
1002
1003         eth_dev = rte_eth_vdev_allocate(dev, 0);
1004         if (eth_dev == NULL)
1005                 goto err_free_tx;
1006
1007         eth_dev->data->dev_private = internals;
1008         eth_dev->data->dev_link = pmd_link;
1009         eth_dev->data->mac_addrs = &internals->eth_addr;
1010         eth_dev->dev_ops = &ops;
1011         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1012         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1013         /* Let rte_eth_dev_close() release the port resources. */
1014         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1015
1016         if (internals->pmd_zc)
1017                 AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1018
1019         return eth_dev;
1020
1021 err_free_tx:
1022         rte_free(internals->tx_queues);
1023 err_free_rx:
1024         rte_free(internals->rx_queues);
1025 err_free_internals:
1026         rte_free(internals);
1027         return NULL;
1028 }
1029
1030 static int
1031 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1032 {
1033         struct rte_kvargs *kvlist;
1034         char if_name[IFNAMSIZ] = {'\0'};
1035         int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1036         int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1037         struct rte_eth_dev *eth_dev = NULL;
1038         const char *name;
1039         int pmd_zc = 0;
1040
1041         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1042                 rte_vdev_device_name(dev));
1043
1044         name = rte_vdev_device_name(dev);
1045         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1046                 strlen(rte_vdev_device_args(dev)) == 0) {
1047                 eth_dev = rte_eth_dev_attach_secondary(name);
1048                 if (eth_dev == NULL) {
1049                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1050                         return -EINVAL;
1051                 }
1052                 eth_dev->dev_ops = &ops;
1053                 rte_eth_dev_probing_finish(eth_dev);
1054                 return 0;
1055         }
1056
1057         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1058         if (kvlist == NULL) {
1059                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1060                 return -EINVAL;
1061         }
1062
1063         if (dev->device.numa_node == SOCKET_ID_ANY)
1064                 dev->device.numa_node = rte_socket_id();
1065
1066         if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1067                              &xsk_queue_cnt, &pmd_zc) < 0) {
1068                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1069                 return -EINVAL;
1070         }
1071
1072         if (strlen(if_name) == 0) {
1073                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1074                 return -EINVAL;
1075         }
1076
1077         eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1078                                         xsk_queue_cnt, pmd_zc);
1079         if (eth_dev == NULL) {
1080                 AF_XDP_LOG(ERR, "Failed to init internals\n");
1081                 return -1;
1082         }
1083
1084         rte_eth_dev_probing_finish(eth_dev);
1085
1086         return 0;
1087 }
1088
1089 static int
1090 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1091 {
1092         struct rte_eth_dev *eth_dev = NULL;
1093
1094         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1095                 rte_socket_id());
1096
1097         if (dev == NULL)
1098                 return -1;
1099
1100         /* find the ethdev entry */
1101         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1102         if (eth_dev == NULL)
1103                 return 0;
1104
1105         eth_dev_close(eth_dev);
1106         rte_eth_dev_release_port(eth_dev);
1107
1108
1109         return 0;
1110 }
1111
1112 static struct rte_vdev_driver pmd_af_xdp_drv = {
1113         .probe = rte_pmd_af_xdp_probe,
1114         .remove = rte_pmd_af_xdp_remove,
1115 };
1116
1117 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1118 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1119                               "iface=<string> "
1120                               "start_queue=<int> "
1121                               "queue_count=<int> "
1122                               "pmd_zero_copy=<0|1>");
1123
1124 RTE_INIT(af_xdp_init_log)
1125 {
1126         af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
1127         if (af_xdp_logtype >= 0)
1128                 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);
1129 }