net/af_xdp: fix typos in Rx function
[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 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 eth_dev_close(struct rte_eth_dev *dev)
431 {
432         struct pmd_internals *internals = dev->data->dev_private;
433         struct pkt_rx_queue *rxq;
434         int i;
435
436         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
437                 rte_socket_id());
438
439         for (i = 0; i < ETH_AF_XDP_MAX_QUEUE_PAIRS; i++) {
440                 rxq = &internals->rx_queues[i];
441                 if (rxq->umem == NULL)
442                         break;
443                 xsk_socket__delete(rxq->xsk);
444         }
445
446         (void)xsk_umem__delete(internals->umem->umem);
447         remove_xdp_program(internals);
448 }
449
450 static void
451 eth_queue_release(void *q __rte_unused)
452 {
453 }
454
455 static int
456 eth_link_update(struct rte_eth_dev *dev __rte_unused,
457                 int wait_to_complete __rte_unused)
458 {
459         return 0;
460 }
461
462 static void
463 xdp_umem_destroy(struct xsk_umem_info *umem)
464 {
465         rte_memzone_free(umem->mz);
466         umem->mz = NULL;
467
468         rte_ring_free(umem->buf_ring);
469         umem->buf_ring = NULL;
470
471         rte_free(umem);
472         umem = NULL;
473 }
474
475 static struct
476 xsk_umem_info *xdp_umem_configure(void)
477 {
478         struct xsk_umem_info *umem;
479         const struct rte_memzone *mz;
480         struct xsk_umem_config usr_config = {
481                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
482                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
483                 .frame_size = ETH_AF_XDP_FRAME_SIZE,
484                 .frame_headroom = ETH_AF_XDP_DATA_HEADROOM };
485         int ret;
486         uint64_t i;
487
488         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
489         if (umem == NULL) {
490                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
491                 return NULL;
492         }
493
494         umem->buf_ring = rte_ring_create("af_xdp_ring",
495                                          ETH_AF_XDP_NUM_BUFFERS,
496                                          rte_socket_id(),
497                                          0x0);
498         if (umem->buf_ring == NULL) {
499                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
500                 goto err;
501         }
502
503         for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
504                 rte_ring_enqueue(umem->buf_ring,
505                                  (void *)(i * ETH_AF_XDP_FRAME_SIZE +
506                                           ETH_AF_XDP_DATA_HEADROOM));
507
508         mz = rte_memzone_reserve_aligned("af_xdp uemem",
509                         ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
510                         rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
511                         getpagesize());
512         if (mz == NULL) {
513                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
514                 goto err;
515         }
516
517         ret = xsk_umem__create(&umem->umem, mz->addr,
518                                ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
519                                &umem->fq, &umem->cq,
520                                &usr_config);
521
522         if (ret) {
523                 AF_XDP_LOG(ERR, "Failed to create umem");
524                 goto err;
525         }
526         umem->mz = mz;
527
528         return umem;
529
530 err:
531         xdp_umem_destroy(umem);
532         return NULL;
533 }
534
535 static int
536 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
537               int ring_size)
538 {
539         struct xsk_socket_config cfg;
540         struct pkt_tx_queue *txq = rxq->pair;
541         int ret = 0;
542         int reserve_size;
543
544         rxq->umem = xdp_umem_configure();
545         if (rxq->umem == NULL)
546                 return -ENOMEM;
547
548         cfg.rx_size = ring_size;
549         cfg.tx_size = ring_size;
550         cfg.libbpf_flags = 0;
551         cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
552         cfg.bind_flags = 0;
553         ret = xsk_socket__create(&rxq->xsk, internals->if_name,
554                         internals->queue_idx, rxq->umem->umem, &rxq->rx,
555                         &txq->tx, &cfg);
556         if (ret) {
557                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
558                 goto err;
559         }
560
561         reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS / 2;
562         ret = reserve_fill_queue(rxq->umem, reserve_size);
563         if (ret) {
564                 xsk_socket__delete(rxq->xsk);
565                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
566                 goto err;
567         }
568
569         return 0;
570
571 err:
572         xdp_umem_destroy(rxq->umem);
573
574         return ret;
575 }
576
577 static void
578 queue_reset(struct pmd_internals *internals, uint16_t queue_idx)
579 {
580         struct pkt_rx_queue *rxq = &internals->rx_queues[queue_idx];
581         struct pkt_tx_queue *txq = rxq->pair;
582
583         memset(rxq, 0, sizeof(*rxq));
584         memset(txq, 0, sizeof(*txq));
585         rxq->pair = txq;
586         txq->pair = rxq;
587         rxq->queue_idx = queue_idx;
588         txq->queue_idx = queue_idx;
589 }
590
591 static int
592 eth_rx_queue_setup(struct rte_eth_dev *dev,
593                    uint16_t rx_queue_id,
594                    uint16_t nb_rx_desc,
595                    unsigned int socket_id __rte_unused,
596                    const struct rte_eth_rxconf *rx_conf __rte_unused,
597                    struct rte_mempool *mb_pool)
598 {
599         struct pmd_internals *internals = dev->data->dev_private;
600         uint32_t buf_size, data_size;
601         struct pkt_rx_queue *rxq;
602         int ret;
603
604         rxq = &internals->rx_queues[rx_queue_id];
605         queue_reset(internals, rx_queue_id);
606
607         /* Now get the space available for data in the mbuf */
608         buf_size = rte_pktmbuf_data_room_size(mb_pool) -
609                 RTE_PKTMBUF_HEADROOM;
610         data_size = ETH_AF_XDP_FRAME_SIZE - ETH_AF_XDP_DATA_HEADROOM;
611
612         if (data_size > buf_size) {
613                 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
614                         dev->device->name, data_size, buf_size);
615                 ret = -ENOMEM;
616                 goto err;
617         }
618
619         rxq->mb_pool = mb_pool;
620
621         if (xsk_configure(internals, rxq, nb_rx_desc)) {
622                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
623                 ret = -EINVAL;
624                 goto err;
625         }
626
627         internals->umem = rxq->umem;
628
629         dev->data->rx_queues[rx_queue_id] = rxq;
630         return 0;
631
632 err:
633         queue_reset(internals, rx_queue_id);
634         return ret;
635 }
636
637 static int
638 eth_tx_queue_setup(struct rte_eth_dev *dev,
639                    uint16_t tx_queue_id,
640                    uint16_t nb_tx_desc __rte_unused,
641                    unsigned int socket_id __rte_unused,
642                    const struct rte_eth_txconf *tx_conf __rte_unused)
643 {
644         struct pmd_internals *internals = dev->data->dev_private;
645         struct pkt_tx_queue *txq;
646
647         txq = &internals->tx_queues[tx_queue_id];
648
649         dev->data->tx_queues[tx_queue_id] = txq;
650         return 0;
651 }
652
653 static int
654 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
655 {
656         struct pmd_internals *internals = dev->data->dev_private;
657         struct ifreq ifr = { .ifr_mtu = mtu };
658         int ret;
659         int s;
660
661         s = socket(PF_INET, SOCK_DGRAM, 0);
662         if (s < 0)
663                 return -EINVAL;
664
665         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
666         ret = ioctl(s, SIOCSIFMTU, &ifr);
667         close(s);
668
669         return (ret < 0) ? -errno : 0;
670 }
671
672 static void
673 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
674 {
675         struct ifreq ifr;
676         int s;
677
678         s = socket(PF_INET, SOCK_DGRAM, 0);
679         if (s < 0)
680                 return;
681
682         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
683         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
684                 goto out;
685         ifr.ifr_flags &= mask;
686         ifr.ifr_flags |= flags;
687         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
688                 goto out;
689 out:
690         close(s);
691 }
692
693 static void
694 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
695 {
696         struct pmd_internals *internals = dev->data->dev_private;
697
698         eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
699 }
700
701 static void
702 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
703 {
704         struct pmd_internals *internals = dev->data->dev_private;
705
706         eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
707 }
708
709 static const struct eth_dev_ops ops = {
710         .dev_start = eth_dev_start,
711         .dev_stop = eth_dev_stop,
712         .dev_close = eth_dev_close,
713         .dev_configure = eth_dev_configure,
714         .dev_infos_get = eth_dev_info,
715         .mtu_set = eth_dev_mtu_set,
716         .promiscuous_enable = eth_dev_promiscuous_enable,
717         .promiscuous_disable = eth_dev_promiscuous_disable,
718         .rx_queue_setup = eth_rx_queue_setup,
719         .tx_queue_setup = eth_tx_queue_setup,
720         .rx_queue_release = eth_queue_release,
721         .tx_queue_release = eth_queue_release,
722         .link_update = eth_link_update,
723         .stats_get = eth_stats_get,
724         .stats_reset = eth_stats_reset,
725 };
726
727 /** parse integer from integer argument */
728 static int
729 parse_integer_arg(const char *key __rte_unused,
730                   const char *value, void *extra_args)
731 {
732         int *i = (int *)extra_args;
733         char *end;
734
735         *i = strtol(value, &end, 10);
736         if (*i < 0) {
737                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
738                 return -EINVAL;
739         }
740
741         return 0;
742 }
743
744 /** parse name argument */
745 static int
746 parse_name_arg(const char *key __rte_unused,
747                const char *value, void *extra_args)
748 {
749         char *name = extra_args;
750
751         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
752                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
753                            value, IFNAMSIZ);
754                 return -EINVAL;
755         }
756
757         strlcpy(name, value, IFNAMSIZ);
758
759         return 0;
760 }
761
762 static int
763 parse_parameters(struct rte_kvargs *kvlist,
764                  char *if_name,
765                  int *queue_idx)
766 {
767         int ret;
768
769         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
770                                  &parse_name_arg, if_name);
771         if (ret < 0)
772                 goto free_kvlist;
773
774         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_IDX_ARG,
775                                  &parse_integer_arg, queue_idx);
776         if (ret < 0)
777                 goto free_kvlist;
778
779 free_kvlist:
780         rte_kvargs_free(kvlist);
781         return ret;
782 }
783
784 static int
785 get_iface_info(const char *if_name,
786                struct ether_addr *eth_addr,
787                int *if_index)
788 {
789         struct ifreq ifr;
790         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
791
792         if (sock < 0)
793                 return -1;
794
795         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
796         if (ioctl(sock, SIOCGIFINDEX, &ifr))
797                 goto error;
798
799         *if_index = ifr.ifr_ifindex;
800
801         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
802                 goto error;
803
804         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
805
806         close(sock);
807         return 0;
808
809 error:
810         close(sock);
811         return -1;
812 }
813
814 static struct rte_eth_dev *
815 init_internals(struct rte_vdev_device *dev,
816                const char *if_name,
817                int queue_idx)
818 {
819         const char *name = rte_vdev_device_name(dev);
820         const unsigned int numa_node = dev->device.numa_node;
821         struct pmd_internals *internals;
822         struct rte_eth_dev *eth_dev;
823         int ret;
824         int i;
825
826         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
827         if (internals == NULL)
828                 return NULL;
829
830         internals->queue_idx = queue_idx;
831         strlcpy(internals->if_name, if_name, IFNAMSIZ);
832
833         for (i = 0; i < ETH_AF_XDP_MAX_QUEUE_PAIRS; i++) {
834                 internals->tx_queues[i].pair = &internals->rx_queues[i];
835                 internals->rx_queues[i].pair = &internals->tx_queues[i];
836         }
837
838         ret = get_iface_info(if_name, &internals->eth_addr,
839                              &internals->if_index);
840         if (ret)
841                 goto err;
842
843         eth_dev = rte_eth_vdev_allocate(dev, 0);
844         if (eth_dev == NULL)
845                 goto err;
846
847         eth_dev->data->dev_private = internals;
848         eth_dev->data->dev_link = pmd_link;
849         eth_dev->data->mac_addrs = &internals->eth_addr;
850         eth_dev->dev_ops = &ops;
851         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
852         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
853
854         return eth_dev;
855
856 err:
857         rte_free(internals);
858         return NULL;
859 }
860
861 static int
862 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
863 {
864         struct rte_kvargs *kvlist;
865         char if_name[IFNAMSIZ] = {'\0'};
866         int xsk_queue_idx = ETH_AF_XDP_DFLT_QUEUE_IDX;
867         struct rte_eth_dev *eth_dev = NULL;
868         const char *name;
869
870         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
871                 rte_vdev_device_name(dev));
872
873         name = rte_vdev_device_name(dev);
874         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
875                 strlen(rte_vdev_device_args(dev)) == 0) {
876                 eth_dev = rte_eth_dev_attach_secondary(name);
877                 if (eth_dev == NULL) {
878                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
879                         return -EINVAL;
880                 }
881                 eth_dev->dev_ops = &ops;
882                 rte_eth_dev_probing_finish(eth_dev);
883                 return 0;
884         }
885
886         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
887         if (kvlist == NULL) {
888                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
889                 return -EINVAL;
890         }
891
892         if (dev->device.numa_node == SOCKET_ID_ANY)
893                 dev->device.numa_node = rte_socket_id();
894
895         if (parse_parameters(kvlist, if_name, &xsk_queue_idx) < 0) {
896                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
897                 return -EINVAL;
898         }
899
900         if (strlen(if_name) == 0) {
901                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
902                 return -EINVAL;
903         }
904
905         eth_dev = init_internals(dev, if_name, xsk_queue_idx);
906         if (eth_dev == NULL) {
907                 AF_XDP_LOG(ERR, "Failed to init internals\n");
908                 return -1;
909         }
910
911         rte_eth_dev_probing_finish(eth_dev);
912
913         return 0;
914 }
915
916 static int
917 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
918 {
919         struct rte_eth_dev *eth_dev = NULL;
920         struct pmd_internals *internals;
921
922         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
923                 rte_socket_id());
924
925         if (dev == NULL)
926                 return -1;
927
928         /* find the ethdev entry */
929         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
930         if (eth_dev == NULL)
931                 return -1;
932
933         internals = eth_dev->data->dev_private;
934
935         rte_ring_free(internals->umem->buf_ring);
936         rte_memzone_free(internals->umem->mz);
937         rte_free(internals->umem);
938
939         rte_eth_dev_release_port(eth_dev);
940
941
942         return 0;
943 }
944
945 static struct rte_vdev_driver pmd_af_xdp_drv = {
946         .probe = rte_pmd_af_xdp_probe,
947         .remove = rte_pmd_af_xdp_remove,
948 };
949
950 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
951 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
952                               "iface=<string> "
953                               "queue=<int> ");
954
955 RTE_INIT(af_xdp_init_log)
956 {
957         af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
958         if (af_xdp_logtype >= 0)
959                 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);
960 }