net: add rte prefix to ether structures
[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 "af_xdp_deps.h"
16 #include <bpf/xsk.h>
17
18 #include <rte_ethdev.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_ethdev_vdev.h>
21 #include <rte_kvargs.h>
22 #include <rte_bus_vdev.h>
23 #include <rte_string_fns.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_common.h>
26 #include <rte_config.h>
27 #include <rte_dev.h>
28 #include <rte_eal.h>
29 #include <rte_ether.h>
30 #include <rte_lcore.h>
31 #include <rte_log.h>
32 #include <rte_memory.h>
33 #include <rte_memzone.h>
34 #include <rte_mbuf.h>
35 #include <rte_malloc.h>
36 #include <rte_ring.h>
37
38 #ifndef SOL_XDP
39 #define SOL_XDP 283
40 #endif
41
42 #ifndef AF_XDP
43 #define AF_XDP 44
44 #endif
45
46 #ifndef PF_XDP
47 #define PF_XDP AF_XDP
48 #endif
49
50 static int af_xdp_logtype;
51
52 #define AF_XDP_LOG(level, fmt, args...)                 \
53         rte_log(RTE_LOG_ ## level, af_xdp_logtype,      \
54                 "%s(): " fmt, __func__, ##args)
55
56 #define ETH_AF_XDP_FRAME_SIZE           XSK_UMEM__DEFAULT_FRAME_SIZE
57 #define ETH_AF_XDP_NUM_BUFFERS          4096
58 #define ETH_AF_XDP_DATA_HEADROOM        0
59 #define ETH_AF_XDP_DFLT_NUM_DESCS       XSK_RING_CONS__DEFAULT_NUM_DESCS
60 #define ETH_AF_XDP_DFLT_QUEUE_IDX       0
61
62 #define ETH_AF_XDP_RX_BATCH_SIZE        32
63 #define ETH_AF_XDP_TX_BATCH_SIZE        32
64
65 #define ETH_AF_XDP_MAX_QUEUE_PAIRS     16
66
67 struct xsk_umem_info {
68         struct xsk_ring_prod fq;
69         struct xsk_ring_cons cq;
70         struct xsk_umem *umem;
71         struct rte_ring *buf_ring;
72         const struct rte_memzone *mz;
73 };
74
75 struct rx_stats {
76         uint64_t rx_pkts;
77         uint64_t rx_bytes;
78         uint64_t rx_dropped;
79 };
80
81 struct pkt_rx_queue {
82         struct xsk_ring_cons rx;
83         struct xsk_umem_info *umem;
84         struct xsk_socket *xsk;
85         struct rte_mempool *mb_pool;
86
87         struct rx_stats stats;
88
89         struct pkt_tx_queue *pair;
90         uint16_t queue_idx;
91 };
92
93 struct tx_stats {
94         uint64_t tx_pkts;
95         uint64_t err_pkts;
96         uint64_t tx_bytes;
97 };
98
99 struct pkt_tx_queue {
100         struct xsk_ring_prod tx;
101
102         struct tx_stats stats;
103
104         struct pkt_rx_queue *pair;
105         uint16_t queue_idx;
106 };
107
108 struct pmd_internals {
109         int if_index;
110         char if_name[IFNAMSIZ];
111         uint16_t queue_idx;
112         struct rte_ether_addr eth_addr;
113         struct xsk_umem_info *umem;
114         struct rte_mempool *mb_pool_share;
115
116         struct pkt_rx_queue rx_queues[ETH_AF_XDP_MAX_QUEUE_PAIRS];
117         struct pkt_tx_queue tx_queues[ETH_AF_XDP_MAX_QUEUE_PAIRS];
118 };
119
120 #define ETH_AF_XDP_IFACE_ARG                    "iface"
121 #define ETH_AF_XDP_QUEUE_IDX_ARG                "queue"
122
123 static const char * const valid_arguments[] = {
124         ETH_AF_XDP_IFACE_ARG,
125         ETH_AF_XDP_QUEUE_IDX_ARG,
126         NULL
127 };
128
129 static const struct rte_eth_link pmd_link = {
130         .link_speed = ETH_SPEED_NUM_10G,
131         .link_duplex = ETH_LINK_FULL_DUPLEX,
132         .link_status = ETH_LINK_DOWN,
133         .link_autoneg = ETH_LINK_AUTONEG
134 };
135
136 static inline int
137 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size)
138 {
139         struct xsk_ring_prod *fq = &umem->fq;
140         void *addrs[reserve_size];
141         uint32_t idx;
142         uint16_t i;
143
144         if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
145                     != reserve_size) {
146                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
147                 return -1;
148         }
149
150         if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
151                 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
152                 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
153                                 reserve_size, NULL);
154                 return -1;
155         }
156
157         for (i = 0; i < reserve_size; i++) {
158                 __u64 *fq_addr;
159
160                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
161                 *fq_addr = (uint64_t)addrs[i];
162         }
163
164         xsk_ring_prod__submit(fq, reserve_size);
165
166         return 0;
167 }
168
169 static uint16_t
170 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
171 {
172         struct pkt_rx_queue *rxq = queue;
173         struct xsk_ring_cons *rx = &rxq->rx;
174         struct xsk_umem_info *umem = rxq->umem;
175         struct xsk_ring_prod *fq = &umem->fq;
176         uint32_t idx_rx = 0;
177         uint32_t free_thresh = fq->size >> 1;
178         struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
179         unsigned long dropped = 0;
180         unsigned long rx_bytes = 0;
181         int rcvd, i;
182
183         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
184
185         if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
186                 return 0;
187
188         rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
189         if (rcvd == 0)
190                 goto out;
191
192         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
193                 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE);
194
195         for (i = 0; i < rcvd; i++) {
196                 const struct xdp_desc *desc;
197                 uint64_t addr;
198                 uint32_t len;
199                 void *pkt;
200
201                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
202                 addr = desc->addr;
203                 len = desc->len;
204                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
205
206                 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
207                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
208                 rte_pktmbuf_data_len(mbufs[i]) = len;
209                 rx_bytes += len;
210                 bufs[i] = mbufs[i];
211
212                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
213         }
214
215         xsk_ring_cons__release(rx, rcvd);
216
217         /* statistics */
218         rxq->stats.rx_pkts += (rcvd - dropped);
219         rxq->stats.rx_bytes += rx_bytes;
220
221 out:
222         if (rcvd != nb_pkts)
223                 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
224                                      nb_pkts - rcvd);
225
226         return rcvd;
227 }
228
229 static void
230 pull_umem_cq(struct xsk_umem_info *umem, int size)
231 {
232         struct xsk_ring_cons *cq = &umem->cq;
233         size_t i, n;
234         uint32_t idx_cq = 0;
235
236         n = xsk_ring_cons__peek(cq, size, &idx_cq);
237
238         for (i = 0; i < n; i++) {
239                 uint64_t addr;
240                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
241                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
242         }
243
244         xsk_ring_cons__release(cq, n);
245 }
246
247 static void
248 kick_tx(struct pkt_tx_queue *txq)
249 {
250         struct xsk_umem_info *umem = txq->pair->umem;
251
252         while (send(xsk_socket__fd(txq->pair->xsk), NULL,
253                       0, MSG_DONTWAIT) < 0) {
254                 /* some thing unexpected */
255                 if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
256                         break;
257
258                 /* pull from completion queue to leave more space */
259                 if (errno == EAGAIN)
260                         pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
261         }
262         pull_umem_cq(umem, ETH_AF_XDP_TX_BATCH_SIZE);
263 }
264
265 static uint16_t
266 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
267 {
268         struct pkt_tx_queue *txq = queue;
269         struct xsk_umem_info *umem = txq->pair->umem;
270         struct rte_mbuf *mbuf;
271         void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
272         unsigned long tx_bytes = 0;
273         int i;
274         uint32_t idx_tx;
275
276         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
277
278         pull_umem_cq(umem, nb_pkts);
279
280         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
281                                         nb_pkts, NULL);
282         if (nb_pkts == 0)
283                 return 0;
284
285         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
286                 kick_tx(txq);
287                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
288                 return 0;
289         }
290
291         for (i = 0; i < nb_pkts; i++) {
292                 struct xdp_desc *desc;
293                 void *pkt;
294
295                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
296                 mbuf = bufs[i];
297
298                 desc->addr = (uint64_t)addrs[i];
299                 desc->len = mbuf->pkt_len;
300                 pkt = xsk_umem__get_data(umem->mz->addr,
301                                          desc->addr);
302                 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
303                            desc->len);
304                 tx_bytes += mbuf->pkt_len;
305
306                 rte_pktmbuf_free(mbuf);
307         }
308
309         xsk_ring_prod__submit(&txq->tx, nb_pkts);
310
311         kick_tx(txq);
312
313         txq->stats.tx_pkts += nb_pkts;
314         txq->stats.tx_bytes += tx_bytes;
315
316         return nb_pkts;
317 }
318
319 static int
320 eth_dev_start(struct rte_eth_dev *dev)
321 {
322         dev->data->dev_link.link_status = ETH_LINK_UP;
323
324         return 0;
325 }
326
327 /* This function gets called when the current port gets stopped. */
328 static void
329 eth_dev_stop(struct rte_eth_dev *dev)
330 {
331         dev->data->dev_link.link_status = ETH_LINK_DOWN;
332 }
333
334 static int
335 eth_dev_configure(struct rte_eth_dev *dev)
336 {
337         /* rx/tx must be paired */
338         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
339                 return -EINVAL;
340
341         return 0;
342 }
343
344 static void
345 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
346 {
347         struct pmd_internals *internals = dev->data->dev_private;
348
349         dev_info->if_index = internals->if_index;
350         dev_info->max_mac_addrs = 1;
351         dev_info->max_rx_pktlen = ETH_FRAME_LEN;
352         dev_info->max_rx_queues = 1;
353         dev_info->max_tx_queues = 1;
354
355         dev_info->min_mtu = ETHER_MIN_MTU;
356         dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
357
358         dev_info->default_rxportconf.nb_queues = 1;
359         dev_info->default_txportconf.nb_queues = 1;
360         dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
361         dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
362 }
363
364 static int
365 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
366 {
367         struct pmd_internals *internals = dev->data->dev_private;
368         struct xdp_statistics xdp_stats;
369         struct pkt_rx_queue *rxq;
370         socklen_t optlen;
371         int i, ret;
372
373         for (i = 0; i < dev->data->nb_rx_queues; i++) {
374                 optlen = sizeof(struct xdp_statistics);
375                 rxq = &internals->rx_queues[i];
376                 stats->q_ipackets[i] = internals->rx_queues[i].stats.rx_pkts;
377                 stats->q_ibytes[i] = internals->rx_queues[i].stats.rx_bytes;
378
379                 stats->q_opackets[i] = internals->tx_queues[i].stats.tx_pkts;
380                 stats->q_obytes[i] = internals->tx_queues[i].stats.tx_bytes;
381
382                 stats->ipackets += stats->q_ipackets[i];
383                 stats->ibytes += stats->q_ibytes[i];
384                 stats->imissed += internals->rx_queues[i].stats.rx_dropped;
385                 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
386                                 XDP_STATISTICS, &xdp_stats, &optlen);
387                 if (ret != 0) {
388                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
389                         return -1;
390                 }
391                 stats->imissed += xdp_stats.rx_dropped;
392
393                 stats->opackets += stats->q_opackets[i];
394                 stats->oerrors += internals->tx_queues[i].stats.err_pkts;
395                 stats->obytes += stats->q_obytes[i];
396         }
397
398         return 0;
399 }
400
401 static void
402 eth_stats_reset(struct rte_eth_dev *dev)
403 {
404         struct pmd_internals *internals = dev->data->dev_private;
405         int i;
406
407         for (i = 0; i < ETH_AF_XDP_MAX_QUEUE_PAIRS; i++) {
408                 memset(&internals->rx_queues[i].stats, 0,
409                                         sizeof(struct rx_stats));
410                 memset(&internals->tx_queues[i].stats, 0,
411                                         sizeof(struct tx_stats));
412         }
413 }
414
415 static void
416 remove_xdp_program(struct pmd_internals *internals)
417 {
418         uint32_t curr_prog_id = 0;
419
420         if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
421                                 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
422                 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
423                 return;
424         }
425         bpf_set_link_xdp_fd(internals->if_index, -1,
426                         XDP_FLAGS_UPDATE_IF_NOEXIST);
427 }
428
429 static void
430 xdp_umem_destroy(struct xsk_umem_info *umem)
431 {
432         rte_memzone_free(umem->mz);
433         umem->mz = NULL;
434
435         rte_ring_free(umem->buf_ring);
436         umem->buf_ring = NULL;
437
438         rte_free(umem);
439         umem = NULL;
440 }
441
442 static void
443 eth_dev_close(struct rte_eth_dev *dev)
444 {
445         struct pmd_internals *internals = dev->data->dev_private;
446         struct pkt_rx_queue *rxq;
447         int i;
448
449         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
450                 rte_socket_id());
451
452         for (i = 0; i < ETH_AF_XDP_MAX_QUEUE_PAIRS; i++) {
453                 rxq = &internals->rx_queues[i];
454                 if (rxq->umem == NULL)
455                         break;
456                 xsk_socket__delete(rxq->xsk);
457         }
458
459         (void)xsk_umem__delete(internals->umem->umem);
460
461         /*
462          * MAC is not allocated dynamically, setting it to NULL would prevent
463          * from releasing it in rte_eth_dev_release_port.
464          */
465         dev->data->mac_addrs = NULL;
466
467         xdp_umem_destroy(internals->umem);
468
469         remove_xdp_program(internals);
470 }
471
472 static void
473 eth_queue_release(void *q __rte_unused)
474 {
475 }
476
477 static int
478 eth_link_update(struct rte_eth_dev *dev __rte_unused,
479                 int wait_to_complete __rte_unused)
480 {
481         return 0;
482 }
483
484 static struct
485 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals)
486 {
487         struct xsk_umem_info *umem;
488         const struct rte_memzone *mz;
489         struct xsk_umem_config usr_config = {
490                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
491                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
492                 .frame_size = ETH_AF_XDP_FRAME_SIZE,
493                 .frame_headroom = ETH_AF_XDP_DATA_HEADROOM };
494         char ring_name[RTE_RING_NAMESIZE];
495         char mz_name[RTE_MEMZONE_NAMESIZE];
496         int ret;
497         uint64_t i;
498
499         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
500         if (umem == NULL) {
501                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
502                 return NULL;
503         }
504
505         snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
506                        internals->if_name, internals->queue_idx);
507         umem->buf_ring = rte_ring_create(ring_name,
508                                          ETH_AF_XDP_NUM_BUFFERS,
509                                          rte_socket_id(),
510                                          0x0);
511         if (umem->buf_ring == NULL) {
512                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
513                 goto err;
514         }
515
516         for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
517                 rte_ring_enqueue(umem->buf_ring,
518                                  (void *)(i * ETH_AF_XDP_FRAME_SIZE +
519                                           ETH_AF_XDP_DATA_HEADROOM));
520
521         snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
522                        internals->if_name, internals->queue_idx);
523         mz = rte_memzone_reserve_aligned(mz_name,
524                         ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
525                         rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
526                         getpagesize());
527         if (mz == NULL) {
528                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
529                 goto err;
530         }
531
532         ret = xsk_umem__create(&umem->umem, mz->addr,
533                                ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
534                                &umem->fq, &umem->cq,
535                                &usr_config);
536
537         if (ret) {
538                 AF_XDP_LOG(ERR, "Failed to create umem");
539                 goto err;
540         }
541         umem->mz = mz;
542
543         return umem;
544
545 err:
546         xdp_umem_destroy(umem);
547         return NULL;
548 }
549
550 static int
551 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
552               int ring_size)
553 {
554         struct xsk_socket_config cfg;
555         struct pkt_tx_queue *txq = rxq->pair;
556         int ret = 0;
557         int reserve_size;
558
559         rxq->umem = xdp_umem_configure(internals);
560         if (rxq->umem == NULL)
561                 return -ENOMEM;
562
563         cfg.rx_size = ring_size;
564         cfg.tx_size = ring_size;
565         cfg.libbpf_flags = 0;
566         cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
567         cfg.bind_flags = 0;
568         ret = xsk_socket__create(&rxq->xsk, internals->if_name,
569                         internals->queue_idx, rxq->umem->umem, &rxq->rx,
570                         &txq->tx, &cfg);
571         if (ret) {
572                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
573                 goto err;
574         }
575
576         reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
577         ret = reserve_fill_queue(rxq->umem, reserve_size);
578         if (ret) {
579                 xsk_socket__delete(rxq->xsk);
580                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
581                 goto err;
582         }
583
584         return 0;
585
586 err:
587         xdp_umem_destroy(rxq->umem);
588
589         return ret;
590 }
591
592 static void
593 queue_reset(struct pmd_internals *internals, uint16_t queue_idx)
594 {
595         struct pkt_rx_queue *rxq = &internals->rx_queues[queue_idx];
596         struct pkt_tx_queue *txq = rxq->pair;
597
598         memset(rxq, 0, sizeof(*rxq));
599         memset(txq, 0, sizeof(*txq));
600         rxq->pair = txq;
601         txq->pair = rxq;
602         rxq->queue_idx = queue_idx;
603         txq->queue_idx = queue_idx;
604 }
605
606 static int
607 eth_rx_queue_setup(struct rte_eth_dev *dev,
608                    uint16_t rx_queue_id,
609                    uint16_t nb_rx_desc,
610                    unsigned int socket_id __rte_unused,
611                    const struct rte_eth_rxconf *rx_conf __rte_unused,
612                    struct rte_mempool *mb_pool)
613 {
614         struct pmd_internals *internals = dev->data->dev_private;
615         uint32_t buf_size, data_size;
616         struct pkt_rx_queue *rxq;
617         int ret;
618
619         rxq = &internals->rx_queues[rx_queue_id];
620         queue_reset(internals, rx_queue_id);
621
622         /* Now get the space available for data in the mbuf */
623         buf_size = rte_pktmbuf_data_room_size(mb_pool) -
624                 RTE_PKTMBUF_HEADROOM;
625         data_size = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
626
627         if (data_size > buf_size) {
628                 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
629                         dev->device->name, data_size, buf_size);
630                 ret = -ENOMEM;
631                 goto err;
632         }
633
634         rxq->mb_pool = mb_pool;
635
636         if (xsk_configure(internals, rxq, nb_rx_desc)) {
637                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
638                 ret = -EINVAL;
639                 goto err;
640         }
641
642         internals->umem = rxq->umem;
643
644         dev->data->rx_queues[rx_queue_id] = rxq;
645         return 0;
646
647 err:
648         queue_reset(internals, rx_queue_id);
649         return ret;
650 }
651
652 static int
653 eth_tx_queue_setup(struct rte_eth_dev *dev,
654                    uint16_t tx_queue_id,
655                    uint16_t nb_tx_desc __rte_unused,
656                    unsigned int socket_id __rte_unused,
657                    const struct rte_eth_txconf *tx_conf __rte_unused)
658 {
659         struct pmd_internals *internals = dev->data->dev_private;
660         struct pkt_tx_queue *txq;
661
662         txq = &internals->tx_queues[tx_queue_id];
663
664         dev->data->tx_queues[tx_queue_id] = txq;
665         return 0;
666 }
667
668 static int
669 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
670 {
671         struct pmd_internals *internals = dev->data->dev_private;
672         struct ifreq ifr = { .ifr_mtu = mtu };
673         int ret;
674         int s;
675
676         s = socket(PF_INET, SOCK_DGRAM, 0);
677         if (s < 0)
678                 return -EINVAL;
679
680         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
681         ret = ioctl(s, SIOCSIFMTU, &ifr);
682         close(s);
683
684         return (ret < 0) ? -errno : 0;
685 }
686
687 static void
688 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
689 {
690         struct ifreq ifr;
691         int s;
692
693         s = socket(PF_INET, SOCK_DGRAM, 0);
694         if (s < 0)
695                 return;
696
697         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
698         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
699                 goto out;
700         ifr.ifr_flags &= mask;
701         ifr.ifr_flags |= flags;
702         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
703                 goto out;
704 out:
705         close(s);
706 }
707
708 static void
709 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
710 {
711         struct pmd_internals *internals = dev->data->dev_private;
712
713         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
714 }
715
716 static void
717 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
718 {
719         struct pmd_internals *internals = dev->data->dev_private;
720
721         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
722 }
723
724 static const struct eth_dev_ops ops = {
725         .dev_start = eth_dev_start,
726         .dev_stop = eth_dev_stop,
727         .dev_close = eth_dev_close,
728         .dev_configure = eth_dev_configure,
729         .dev_infos_get = eth_dev_info,
730         .mtu_set = eth_dev_mtu_set,
731         .promiscuous_enable = eth_dev_promiscuous_enable,
732         .promiscuous_disable = eth_dev_promiscuous_disable,
733         .rx_queue_setup = eth_rx_queue_setup,
734         .tx_queue_setup = eth_tx_queue_setup,
735         .rx_queue_release = eth_queue_release,
736         .tx_queue_release = eth_queue_release,
737         .link_update = eth_link_update,
738         .stats_get = eth_stats_get,
739         .stats_reset = eth_stats_reset,
740 };
741
742 /** parse integer from integer argument */
743 static int
744 parse_integer_arg(const char *key __rte_unused,
745                   const char *value, void *extra_args)
746 {
747         int *i = (int *)extra_args;
748         char *end;
749
750         *i = strtol(value, &end, 10);
751         if (*i < 0) {
752                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
753                 return -EINVAL;
754         }
755
756         return 0;
757 }
758
759 /** parse name argument */
760 static int
761 parse_name_arg(const char *key __rte_unused,
762                const char *value, void *extra_args)
763 {
764         char *name = extra_args;
765
766         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
767                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
768                            value, IFNAMSIZ);
769                 return -EINVAL;
770         }
771
772         strlcpy(name, value, IFNAMSIZ);
773
774         return 0;
775 }
776
777 static int
778 parse_parameters(struct rte_kvargs *kvlist,
779                  char *if_name,
780                  int *queue_idx)
781 {
782         int ret;
783
784         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
785                                  &parse_name_arg, if_name);
786         if (ret < 0)
787                 goto free_kvlist;
788
789         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_IDX_ARG,
790                                  &parse_integer_arg, queue_idx);
791         if (ret < 0)
792                 goto free_kvlist;
793
794 free_kvlist:
795         rte_kvargs_free(kvlist);
796         return ret;
797 }
798
799 static int
800 get_iface_info(const char *if_name,
801                struct rte_ether_addr *eth_addr,
802                int *if_index)
803 {
804         struct ifreq ifr;
805         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
806
807         if (sock < 0)
808                 return -1;
809
810         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
811         if (ioctl(sock, SIOCGIFINDEX, &ifr))
812                 goto error;
813
814         *if_index = ifr.ifr_ifindex;
815
816         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
817                 goto error;
818
819         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
820
821         close(sock);
822         return 0;
823
824 error:
825         close(sock);
826         return -1;
827 }
828
829 static struct rte_eth_dev *
830 init_internals(struct rte_vdev_device *dev,
831                const char *if_name,
832                int queue_idx)
833 {
834         const char *name = rte_vdev_device_name(dev);
835         const unsigned int numa_node = dev->device.numa_node;
836         struct pmd_internals *internals;
837         struct rte_eth_dev *eth_dev;
838         int ret;
839         int i;
840
841         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
842         if (internals == NULL)
843                 return NULL;
844
845         internals->queue_idx = queue_idx;
846         strlcpy(internals->if_name, if_name, IFNAMSIZ);
847
848         for (i = 0; i < ETH_AF_XDP_MAX_QUEUE_PAIRS; i++) {
849                 internals->tx_queues[i].pair = &internals->rx_queues[i];
850                 internals->rx_queues[i].pair = &internals->tx_queues[i];
851         }
852
853         ret = get_iface_info(if_name, &internals->eth_addr,
854                              &internals->if_index);
855         if (ret)
856                 goto err;
857
858         eth_dev = rte_eth_vdev_allocate(dev, 0);
859         if (eth_dev == NULL)
860                 goto err;
861
862         eth_dev->data->dev_private = internals;
863         eth_dev->data->dev_link = pmd_link;
864         eth_dev->data->mac_addrs = &internals->eth_addr;
865         eth_dev->dev_ops = &ops;
866         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
867         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
868         /* Let rte_eth_dev_close() release the port resources. */
869         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
870
871         return eth_dev;
872
873 err:
874         rte_free(internals);
875         return NULL;
876 }
877
878 static int
879 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
880 {
881         struct rte_kvargs *kvlist;
882         char if_name[IFNAMSIZ] = {'\0'};
883         int xsk_queue_idx = ETH_AF_XDP_DFLT_QUEUE_IDX;
884         struct rte_eth_dev *eth_dev = NULL;
885         const char *name;
886
887         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
888                 rte_vdev_device_name(dev));
889
890         name = rte_vdev_device_name(dev);
891         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
892                 strlen(rte_vdev_device_args(dev)) == 0) {
893                 eth_dev = rte_eth_dev_attach_secondary(name);
894                 if (eth_dev == NULL) {
895                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
896                         return -EINVAL;
897                 }
898                 eth_dev->dev_ops = &ops;
899                 rte_eth_dev_probing_finish(eth_dev);
900                 return 0;
901         }
902
903         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
904         if (kvlist == NULL) {
905                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
906                 return -EINVAL;
907         }
908
909         if (dev->device.numa_node == SOCKET_ID_ANY)
910                 dev->device.numa_node = rte_socket_id();
911
912         if (parse_parameters(kvlist, if_name, &xsk_queue_idx) < 0) {
913                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
914                 return -EINVAL;
915         }
916
917         if (strlen(if_name) == 0) {
918                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
919                 return -EINVAL;
920         }
921
922         eth_dev = init_internals(dev, if_name, xsk_queue_idx);
923         if (eth_dev == NULL) {
924                 AF_XDP_LOG(ERR, "Failed to init internals\n");
925                 return -1;
926         }
927
928         rte_eth_dev_probing_finish(eth_dev);
929
930         return 0;
931 }
932
933 static int
934 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
935 {
936         struct rte_eth_dev *eth_dev = NULL;
937
938         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
939                 rte_socket_id());
940
941         if (dev == NULL)
942                 return -1;
943
944         /* find the ethdev entry */
945         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
946         if (eth_dev == NULL)
947                 return -1;
948
949         eth_dev_close(eth_dev);
950         rte_eth_dev_release_port(eth_dev);
951
952
953         return 0;
954 }
955
956 static struct rte_vdev_driver pmd_af_xdp_drv = {
957         .probe = rte_pmd_af_xdp_probe,
958         .remove = rte_pmd_af_xdp_remove,
959 };
960
961 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
962 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
963                               "iface=<string> "
964                               "queue=<int> ");
965
966 RTE_INIT(af_xdp_init_log)
967 {
968         af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
969         if (af_xdp_logtype >= 0)
970                 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);
971 }