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