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