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