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