drivers/net: check process type in close operation
[dpdk.git] / drivers / net / af_xdp / rte_eth_af_xdp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Intel Corporation.
3  */
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <poll.h>
9 #include <netinet/in.h>
10 #include <net/if.h>
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_xdp.h>
15 #include <linux/if_link.h>
16 #include <linux/ethtool.h>
17 #include <linux/sockios.h>
18 #include "af_xdp_deps.h"
19 #include <bpf/xsk.h>
20
21 #include <rte_ethdev.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_bus_vdev.h>
26 #include <rte_string_fns.h>
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
29 #include <rte_dev.h>
30 #include <rte_eal.h>
31 #include <rte_ether.h>
32 #include <rte_lcore.h>
33 #include <rte_log.h>
34 #include <rte_memory.h>
35 #include <rte_memzone.h>
36 #include <rte_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_malloc.h>
39 #include <rte_ring.h>
40 #include <rte_spinlock.h>
41
42 #include "compat.h"
43
44
45 #ifndef SOL_XDP
46 #define SOL_XDP 283
47 #endif
48
49 #ifndef AF_XDP
50 #define AF_XDP 44
51 #endif
52
53 #ifndef PF_XDP
54 #define PF_XDP AF_XDP
55 #endif
56
57 RTE_LOG_REGISTER(af_xdp_logtype, pmd.net.af_xdp, NOTICE);
58
59 #define AF_XDP_LOG(level, fmt, args...)                 \
60         rte_log(RTE_LOG_ ## level, af_xdp_logtype,      \
61                 "%s(): " fmt, __func__, ##args)
62
63 #define ETH_AF_XDP_FRAME_SIZE           2048
64 #define ETH_AF_XDP_NUM_BUFFERS          4096
65 #define ETH_AF_XDP_DFLT_NUM_DESCS       XSK_RING_CONS__DEFAULT_NUM_DESCS
66 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
67 #define ETH_AF_XDP_DFLT_QUEUE_COUNT     1
68
69 #define ETH_AF_XDP_RX_BATCH_SIZE        32
70 #define ETH_AF_XDP_TX_BATCH_SIZE        32
71
72
73 struct xsk_umem_info {
74         struct xsk_umem *umem;
75         struct rte_ring *buf_ring;
76         const struct rte_memzone *mz;
77         struct rte_mempool *mb_pool;
78         void *buffer;
79         uint8_t refcnt;
80         uint32_t max_xsks;
81 };
82
83 struct rx_stats {
84         uint64_t rx_pkts;
85         uint64_t rx_bytes;
86         uint64_t rx_dropped;
87 };
88
89 struct pkt_rx_queue {
90         struct xsk_ring_cons rx;
91         struct xsk_umem_info *umem;
92         struct xsk_socket *xsk;
93         struct rte_mempool *mb_pool;
94
95         struct rx_stats stats;
96
97         struct xsk_ring_prod fq;
98         struct xsk_ring_cons cq;
99
100         struct pkt_tx_queue *pair;
101         struct pollfd fds[1];
102         int xsk_queue_idx;
103 };
104
105 struct tx_stats {
106         uint64_t tx_pkts;
107         uint64_t tx_bytes;
108         uint64_t tx_dropped;
109 };
110
111 struct pkt_tx_queue {
112         struct xsk_ring_prod tx;
113         struct xsk_umem_info *umem;
114
115         struct tx_stats stats;
116
117         struct pkt_rx_queue *pair;
118         int xsk_queue_idx;
119 };
120
121 struct pmd_internals {
122         int if_index;
123         char if_name[IFNAMSIZ];
124         int start_queue_idx;
125         int queue_cnt;
126         int max_queue_cnt;
127         int combined_queue_cnt;
128         bool shared_umem;
129
130         struct rte_ether_addr eth_addr;
131
132         struct pkt_rx_queue *rx_queues;
133         struct pkt_tx_queue *tx_queues;
134 };
135
136 #define ETH_AF_XDP_IFACE_ARG                    "iface"
137 #define ETH_AF_XDP_START_QUEUE_ARG              "start_queue"
138 #define ETH_AF_XDP_QUEUE_COUNT_ARG              "queue_count"
139 #define ETH_AF_XDP_SHARED_UMEM_ARG              "shared_umem"
140
141 static const char * const valid_arguments[] = {
142         ETH_AF_XDP_IFACE_ARG,
143         ETH_AF_XDP_START_QUEUE_ARG,
144         ETH_AF_XDP_QUEUE_COUNT_ARG,
145         ETH_AF_XDP_SHARED_UMEM_ARG,
146         NULL
147 };
148
149 static const struct rte_eth_link pmd_link = {
150         .link_speed = ETH_SPEED_NUM_10G,
151         .link_duplex = ETH_LINK_FULL_DUPLEX,
152         .link_status = ETH_LINK_DOWN,
153         .link_autoneg = ETH_LINK_AUTONEG
154 };
155
156 /* List which tracks PMDs to facilitate sharing UMEMs across them. */
157 struct internal_list {
158         TAILQ_ENTRY(internal_list) next;
159         struct rte_eth_dev *eth_dev;
160 };
161
162 TAILQ_HEAD(internal_list_head, internal_list);
163 static struct internal_list_head internal_list =
164         TAILQ_HEAD_INITIALIZER(internal_list);
165
166 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
167
168 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
169 static inline int
170 reserve_fill_queue_zc(struct xsk_umem_info *umem, uint16_t reserve_size,
171                       struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
172 {
173         uint32_t idx;
174         uint16_t i;
175
176         if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
177                 for (i = 0; i < reserve_size; i++)
178                         rte_pktmbuf_free(bufs[i]);
179                 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
180                 return -1;
181         }
182
183         for (i = 0; i < reserve_size; i++) {
184                 __u64 *fq_addr;
185                 uint64_t addr;
186
187                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
188                 addr = (uint64_t)bufs[i] - (uint64_t)umem->buffer -
189                                 umem->mb_pool->header_size;
190                 *fq_addr = addr;
191         }
192
193         xsk_ring_prod__submit(fq, reserve_size);
194
195         return 0;
196 }
197 #else
198 static inline int
199 reserve_fill_queue_cp(struct xsk_umem_info *umem, uint16_t reserve_size,
200                       struct rte_mbuf **bufs __rte_unused,
201                       struct xsk_ring_prod *fq)
202 {
203         void *addrs[reserve_size];
204         uint32_t idx;
205         uint16_t i;
206
207         if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
208                     != reserve_size) {
209                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
210                 return -1;
211         }
212
213         if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
214                 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
215                 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
216                                 reserve_size, NULL);
217                 return -1;
218         }
219
220         for (i = 0; i < reserve_size; i++) {
221                 __u64 *fq_addr;
222
223                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
224                 *fq_addr = (uint64_t)addrs[i];
225         }
226
227         xsk_ring_prod__submit(fq, reserve_size);
228
229         return 0;
230 }
231 #endif
232
233 static inline int
234 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size,
235                    struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
236 {
237 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
238         return reserve_fill_queue_zc(umem, reserve_size, bufs, fq);
239 #else
240         return reserve_fill_queue_cp(umem, reserve_size, bufs, fq);
241 #endif
242 }
243
244 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
245 static uint16_t
246 af_xdp_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
247 {
248         struct pkt_rx_queue *rxq = queue;
249         struct xsk_ring_cons *rx = &rxq->rx;
250         struct xsk_ring_prod *fq = &rxq->fq;
251         struct xsk_umem_info *umem = rxq->umem;
252         uint32_t idx_rx = 0;
253         unsigned long rx_bytes = 0;
254         int rcvd, i;
255         struct rte_mbuf *fq_bufs[ETH_AF_XDP_RX_BATCH_SIZE];
256
257         /* allocate bufs for fill queue replenishment after rx */
258         if (rte_pktmbuf_alloc_bulk(umem->mb_pool, fq_bufs, nb_pkts)) {
259                 AF_XDP_LOG(DEBUG,
260                         "Failed to get enough buffers for fq.\n");
261                 return 0;
262         }
263
264         rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
265
266         if (rcvd == 0) {
267 #if defined(XDP_USE_NEED_WAKEUP)
268                 if (xsk_ring_prod__needs_wakeup(fq))
269                         (void)poll(rxq->fds, 1, 1000);
270 #endif
271
272                 goto out;
273         }
274
275         for (i = 0; i < rcvd; i++) {
276                 const struct xdp_desc *desc;
277                 uint64_t addr;
278                 uint32_t len;
279                 uint64_t offset;
280
281                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
282                 addr = desc->addr;
283                 len = desc->len;
284
285                 offset = xsk_umem__extract_offset(addr);
286                 addr = xsk_umem__extract_addr(addr);
287
288                 bufs[i] = (struct rte_mbuf *)
289                                 xsk_umem__get_data(umem->buffer, addr +
290                                         umem->mb_pool->header_size);
291                 bufs[i]->data_off = offset - sizeof(struct rte_mbuf) -
292                         rte_pktmbuf_priv_size(umem->mb_pool) -
293                         umem->mb_pool->header_size;
294
295                 rte_pktmbuf_pkt_len(bufs[i]) = len;
296                 rte_pktmbuf_data_len(bufs[i]) = len;
297                 rx_bytes += len;
298         }
299
300         xsk_ring_cons__release(rx, rcvd);
301
302         (void)reserve_fill_queue(umem, rcvd, fq_bufs, fq);
303
304         /* statistics */
305         rxq->stats.rx_pkts += rcvd;
306         rxq->stats.rx_bytes += rx_bytes;
307
308 out:
309         if (rcvd != nb_pkts)
310                 rte_mempool_put_bulk(umem->mb_pool, (void **)&fq_bufs[rcvd],
311                                      nb_pkts - rcvd);
312
313         return rcvd;
314 }
315 #else
316 static uint16_t
317 af_xdp_rx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
318 {
319         struct pkt_rx_queue *rxq = queue;
320         struct xsk_ring_cons *rx = &rxq->rx;
321         struct xsk_umem_info *umem = rxq->umem;
322         struct xsk_ring_prod *fq = &rxq->fq;
323         uint32_t idx_rx = 0;
324         unsigned long rx_bytes = 0;
325         int rcvd, i;
326         uint32_t free_thresh = fq->size >> 1;
327         struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
328
329         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
330                 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE,
331                                          NULL, fq);
332
333         if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
334                 return 0;
335
336         rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
337         if (rcvd == 0) {
338 #if defined(XDP_USE_NEED_WAKEUP)
339                 if (xsk_ring_prod__needs_wakeup(fq))
340                         (void)poll(rxq->fds, 1, 1000);
341 #endif
342
343                 goto out;
344         }
345
346         for (i = 0; i < rcvd; i++) {
347                 const struct xdp_desc *desc;
348                 uint64_t addr;
349                 uint32_t len;
350                 void *pkt;
351
352                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
353                 addr = desc->addr;
354                 len = desc->len;
355                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
356
357                 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
358                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
359                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
360                 rte_pktmbuf_data_len(mbufs[i]) = len;
361                 rx_bytes += len;
362                 bufs[i] = mbufs[i];
363         }
364
365         xsk_ring_cons__release(rx, rcvd);
366
367         /* statistics */
368         rxq->stats.rx_pkts += rcvd;
369         rxq->stats.rx_bytes += rx_bytes;
370
371 out:
372         if (rcvd != nb_pkts)
373                 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
374                                      nb_pkts - rcvd);
375
376         return rcvd;
377 }
378 #endif
379
380 static uint16_t
381 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
382 {
383         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
384
385 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
386         return af_xdp_rx_zc(queue, bufs, nb_pkts);
387 #else
388         return af_xdp_rx_cp(queue, bufs, nb_pkts);
389 #endif
390 }
391
392 static void
393 pull_umem_cq(struct xsk_umem_info *umem, int size, struct xsk_ring_cons *cq)
394 {
395         size_t i, n;
396         uint32_t idx_cq = 0;
397
398         n = xsk_ring_cons__peek(cq, size, &idx_cq);
399
400         for (i = 0; i < n; i++) {
401                 uint64_t addr;
402                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
403 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
404                 addr = xsk_umem__extract_addr(addr);
405                 rte_pktmbuf_free((struct rte_mbuf *)
406                                         xsk_umem__get_data(umem->buffer,
407                                         addr + umem->mb_pool->header_size));
408 #else
409                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
410 #endif
411         }
412
413         xsk_ring_cons__release(cq, n);
414 }
415
416 static void
417 kick_tx(struct pkt_tx_queue *txq, struct xsk_ring_cons *cq)
418 {
419         struct xsk_umem_info *umem = txq->umem;
420
421         pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
422
423 #if defined(XDP_USE_NEED_WAKEUP)
424         if (xsk_ring_prod__needs_wakeup(&txq->tx))
425 #endif
426                 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
427                             0, MSG_DONTWAIT) < 0) {
428                         /* some thing unexpected */
429                         if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
430                                 break;
431
432                         /* pull from completion queue to leave more space */
433                         if (errno == EAGAIN)
434                                 pull_umem_cq(umem,
435                                              XSK_RING_CONS__DEFAULT_NUM_DESCS,
436                                              cq);
437                 }
438 }
439
440 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
441 static uint16_t
442 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
443 {
444         struct pkt_tx_queue *txq = queue;
445         struct xsk_umem_info *umem = txq->umem;
446         struct rte_mbuf *mbuf;
447         unsigned long tx_bytes = 0;
448         int i;
449         uint32_t idx_tx;
450         uint16_t count = 0;
451         struct xdp_desc *desc;
452         uint64_t addr, offset;
453         struct xsk_ring_cons *cq = &txq->pair->cq;
454         uint32_t free_thresh = cq->size >> 1;
455
456         if (xsk_cons_nb_avail(cq, free_thresh) >= free_thresh)
457                 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
458
459         for (i = 0; i < nb_pkts; i++) {
460                 mbuf = bufs[i];
461
462                 if (mbuf->pool == umem->mb_pool) {
463                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
464                                 kick_tx(txq, cq);
465                                 if (!xsk_ring_prod__reserve(&txq->tx, 1,
466                                                             &idx_tx))
467                                         goto out;
468                         }
469                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
470                         desc->len = mbuf->pkt_len;
471                         addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
472                                         umem->mb_pool->header_size;
473                         offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
474                                         (uint64_t)mbuf +
475                                         umem->mb_pool->header_size;
476                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
477                         desc->addr = addr | offset;
478                         count++;
479                 } else {
480                         struct rte_mbuf *local_mbuf =
481                                         rte_pktmbuf_alloc(umem->mb_pool);
482                         void *pkt;
483
484                         if (local_mbuf == NULL)
485                                 goto out;
486
487                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
488                                 rte_pktmbuf_free(local_mbuf);
489                                 kick_tx(txq, cq);
490                                 goto out;
491                         }
492
493                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
494                         desc->len = mbuf->pkt_len;
495
496                         addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
497                                         umem->mb_pool->header_size;
498                         offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
499                                         (uint64_t)local_mbuf +
500                                         umem->mb_pool->header_size;
501                         pkt = xsk_umem__get_data(umem->buffer, addr + offset);
502                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
503                         desc->addr = addr | offset;
504                         rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
505                                         desc->len);
506                         rte_pktmbuf_free(mbuf);
507                         count++;
508                 }
509
510                 tx_bytes += mbuf->pkt_len;
511         }
512
513         kick_tx(txq, cq);
514
515 out:
516         xsk_ring_prod__submit(&txq->tx, count);
517
518         txq->stats.tx_pkts += count;
519         txq->stats.tx_bytes += tx_bytes;
520         txq->stats.tx_dropped += nb_pkts - count;
521
522         return count;
523 }
524 #else
525 static uint16_t
526 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
527 {
528         struct pkt_tx_queue *txq = queue;
529         struct xsk_umem_info *umem = txq->umem;
530         struct rte_mbuf *mbuf;
531         void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
532         unsigned long tx_bytes = 0;
533         int i;
534         uint32_t idx_tx;
535         struct xsk_ring_cons *cq = &txq->pair->cq;
536
537         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
538
539         pull_umem_cq(umem, nb_pkts, cq);
540
541         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
542                                         nb_pkts, NULL);
543         if (nb_pkts == 0)
544                 return 0;
545
546         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
547                 kick_tx(txq, cq);
548                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
549                 return 0;
550         }
551
552         for (i = 0; i < nb_pkts; i++) {
553                 struct xdp_desc *desc;
554                 void *pkt;
555
556                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
557                 mbuf = bufs[i];
558                 desc->len = mbuf->pkt_len;
559
560                 desc->addr = (uint64_t)addrs[i];
561                 pkt = xsk_umem__get_data(umem->mz->addr,
562                                          desc->addr);
563                 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
564                 tx_bytes += mbuf->pkt_len;
565                 rte_pktmbuf_free(mbuf);
566         }
567
568         xsk_ring_prod__submit(&txq->tx, nb_pkts);
569
570         kick_tx(txq, cq);
571
572         txq->stats.tx_pkts += nb_pkts;
573         txq->stats.tx_bytes += tx_bytes;
574
575         return nb_pkts;
576 }
577 #endif
578
579 static uint16_t
580 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
581 {
582 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
583         return af_xdp_tx_zc(queue, bufs, nb_pkts);
584 #else
585         return af_xdp_tx_cp(queue, bufs, nb_pkts);
586 #endif
587 }
588
589 static int
590 eth_dev_start(struct rte_eth_dev *dev)
591 {
592         dev->data->dev_link.link_status = ETH_LINK_UP;
593
594         return 0;
595 }
596
597 /* This function gets called when the current port gets stopped. */
598 static void
599 eth_dev_stop(struct rte_eth_dev *dev)
600 {
601         dev->data->dev_link.link_status = ETH_LINK_DOWN;
602 }
603
604 /* Find ethdev in list */
605 static inline struct internal_list *
606 find_internal_resource(struct pmd_internals *port_int)
607 {
608         int found = 0;
609         struct internal_list *list = NULL;
610
611         if (port_int == NULL)
612                 return NULL;
613
614         pthread_mutex_lock(&internal_list_lock);
615
616         TAILQ_FOREACH(list, &internal_list, next) {
617                 struct pmd_internals *list_int =
618                                 list->eth_dev->data->dev_private;
619                 if (list_int == port_int) {
620                         found = 1;
621                         break;
622                 }
623         }
624
625         pthread_mutex_unlock(&internal_list_lock);
626
627         if (!found)
628                 return NULL;
629
630         return list;
631 }
632
633 /* Get a pointer to an existing UMEM which overlays the rxq's mb_pool */
634 static inline struct xsk_umem_info *
635 get_shared_umem(struct pkt_rx_queue *rxq) {
636         struct internal_list *list;
637         struct pmd_internals *internals;
638         int i = 0;
639         struct rte_mempool *mb_pool = rxq->mb_pool;
640
641         if (mb_pool == NULL)
642                 return NULL;
643
644         pthread_mutex_lock(&internal_list_lock);
645
646         TAILQ_FOREACH(list, &internal_list, next) {
647                 internals = list->eth_dev->data->dev_private;
648                 for (i = 0; i < internals->queue_cnt; i++) {
649                         struct pkt_rx_queue *list_rxq =
650                                                 &internals->rx_queues[i];
651                         if (rxq == list_rxq)
652                                 continue;
653                         if (mb_pool == internals->rx_queues[i].mb_pool) {
654                                 if (__atomic_load_n(
655                                         &internals->rx_queues[i].umem->refcnt,
656                                                         __ATOMIC_ACQUIRE)) {
657                                         pthread_mutex_unlock(
658                                                         &internal_list_lock);
659                                         return internals->rx_queues[i].umem;
660                                 }
661                         }
662                 }
663         }
664
665         pthread_mutex_unlock(&internal_list_lock);
666
667         return NULL;
668 }
669
670 static int
671 eth_dev_configure(struct rte_eth_dev *dev)
672 {
673         struct pmd_internals *internal = dev->data->dev_private;
674
675         /* rx/tx must be paired */
676         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
677                 return -EINVAL;
678
679         if (internal->shared_umem) {
680                 struct internal_list *list = NULL;
681                 const char *name = dev->device->name;
682
683                 /* Ensure PMD is not already inserted into the list */
684                 list = find_internal_resource(internal);
685                 if (list)
686                         return 0;
687
688                 list = rte_zmalloc_socket(name, sizeof(*list), 0,
689                                         dev->device->numa_node);
690                 if (list == NULL)
691                         return -1;
692
693                 list->eth_dev = dev;
694                 pthread_mutex_lock(&internal_list_lock);
695                 TAILQ_INSERT_TAIL(&internal_list, list, next);
696                 pthread_mutex_unlock(&internal_list_lock);
697         }
698
699         return 0;
700 }
701
702 static int
703 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
704 {
705         struct pmd_internals *internals = dev->data->dev_private;
706
707         dev_info->if_index = internals->if_index;
708         dev_info->max_mac_addrs = 1;
709         dev_info->max_rx_pktlen = ETH_FRAME_LEN;
710         dev_info->max_rx_queues = internals->queue_cnt;
711         dev_info->max_tx_queues = internals->queue_cnt;
712
713         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
714 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
715         dev_info->max_mtu = getpagesize() -
716                                 sizeof(struct rte_mempool_objhdr) -
717                                 sizeof(struct rte_mbuf) -
718                                 RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
719 #else
720         dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
721 #endif
722
723         dev_info->default_rxportconf.nb_queues = 1;
724         dev_info->default_txportconf.nb_queues = 1;
725         dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
726         dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
727
728         return 0;
729 }
730
731 static int
732 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
733 {
734         struct pmd_internals *internals = dev->data->dev_private;
735         struct xdp_statistics xdp_stats;
736         struct pkt_rx_queue *rxq;
737         struct pkt_tx_queue *txq;
738         socklen_t optlen;
739         int i, ret;
740
741         for (i = 0; i < dev->data->nb_rx_queues; i++) {
742                 optlen = sizeof(struct xdp_statistics);
743                 rxq = &internals->rx_queues[i];
744                 txq = rxq->pair;
745                 stats->q_ipackets[i] = rxq->stats.rx_pkts;
746                 stats->q_ibytes[i] = rxq->stats.rx_bytes;
747
748                 stats->q_opackets[i] = txq->stats.tx_pkts;
749                 stats->q_obytes[i] = txq->stats.tx_bytes;
750
751                 stats->ipackets += stats->q_ipackets[i];
752                 stats->ibytes += stats->q_ibytes[i];
753                 stats->imissed += rxq->stats.rx_dropped;
754                 stats->oerrors += txq->stats.tx_dropped;
755                 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
756                                 XDP_STATISTICS, &xdp_stats, &optlen);
757                 if (ret != 0) {
758                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
759                         return -1;
760                 }
761                 stats->imissed += xdp_stats.rx_dropped;
762
763                 stats->opackets += stats->q_opackets[i];
764                 stats->obytes += stats->q_obytes[i];
765         }
766
767         return 0;
768 }
769
770 static int
771 eth_stats_reset(struct rte_eth_dev *dev)
772 {
773         struct pmd_internals *internals = dev->data->dev_private;
774         int i;
775
776         for (i = 0; i < internals->queue_cnt; i++) {
777                 memset(&internals->rx_queues[i].stats, 0,
778                                         sizeof(struct rx_stats));
779                 memset(&internals->tx_queues[i].stats, 0,
780                                         sizeof(struct tx_stats));
781         }
782
783         return 0;
784 }
785
786 static void
787 remove_xdp_program(struct pmd_internals *internals)
788 {
789         uint32_t curr_prog_id = 0;
790
791         if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
792                                 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
793                 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
794                 return;
795         }
796         bpf_set_link_xdp_fd(internals->if_index, -1,
797                         XDP_FLAGS_UPDATE_IF_NOEXIST);
798 }
799
800 static void
801 xdp_umem_destroy(struct xsk_umem_info *umem)
802 {
803 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
804         umem->mb_pool = NULL;
805 #else
806         rte_memzone_free(umem->mz);
807         umem->mz = NULL;
808
809         rte_ring_free(umem->buf_ring);
810         umem->buf_ring = NULL;
811 #endif
812
813         rte_free(umem);
814         umem = NULL;
815 }
816
817 static int
818 eth_dev_close(struct rte_eth_dev *dev)
819 {
820         struct pmd_internals *internals = dev->data->dev_private;
821         struct pkt_rx_queue *rxq;
822         int i;
823
824         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
825                 return 0;
826
827         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
828                 rte_socket_id());
829
830         for (i = 0; i < internals->queue_cnt; i++) {
831                 rxq = &internals->rx_queues[i];
832                 if (rxq->umem == NULL)
833                         break;
834                 xsk_socket__delete(rxq->xsk);
835
836                 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE)
837                                 == 0) {
838                         (void)xsk_umem__delete(rxq->umem->umem);
839                         xdp_umem_destroy(rxq->umem);
840                 }
841
842                 /* free pkt_tx_queue */
843                 rte_free(rxq->pair);
844                 rte_free(rxq);
845         }
846
847         /*
848          * MAC is not allocated dynamically, setting it to NULL would prevent
849          * from releasing it in rte_eth_dev_release_port.
850          */
851         dev->data->mac_addrs = NULL;
852
853         remove_xdp_program(internals);
854
855         if (internals->shared_umem) {
856                 struct internal_list *list;
857
858                 /* Remove ethdev from list used to track and share UMEMs */
859                 list = find_internal_resource(internals);
860                 if (list) {
861                         pthread_mutex_lock(&internal_list_lock);
862                         TAILQ_REMOVE(&internal_list, list, next);
863                         pthread_mutex_unlock(&internal_list_lock);
864                         rte_free(list);
865                 }
866         }
867
868         return 0;
869 }
870
871 static void
872 eth_queue_release(void *q __rte_unused)
873 {
874 }
875
876 static int
877 eth_link_update(struct rte_eth_dev *dev __rte_unused,
878                 int wait_to_complete __rte_unused)
879 {
880         return 0;
881 }
882
883 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
884 static inline uint64_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
885 {
886         struct rte_mempool_memhdr *memhdr;
887         uint64_t memhdr_addr, aligned_addr;
888
889         memhdr = STAILQ_FIRST(&mp->mem_list);
890         memhdr_addr = (uint64_t)memhdr->addr;
891         aligned_addr = memhdr_addr & ~(getpagesize() - 1);
892         *align = memhdr_addr - aligned_addr;
893
894         return aligned_addr;
895 }
896
897 static struct
898 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
899                                   struct pkt_rx_queue *rxq)
900 {
901         struct xsk_umem_info *umem = NULL;
902         int ret;
903         struct xsk_umem_config usr_config = {
904                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
905                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
906                 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
907         void *base_addr = NULL;
908         struct rte_mempool *mb_pool = rxq->mb_pool;
909         uint64_t umem_size, align = 0;
910
911         if (internals->shared_umem) {
912                 umem = get_shared_umem(rxq);
913                 if (umem != NULL &&
914                         __atomic_load_n(&umem->refcnt, __ATOMIC_ACQUIRE) <
915                                         umem->max_xsks) {
916                         AF_XDP_LOG(INFO, "%s,qid%i sharing UMEM\n",
917                                         internals->if_name, rxq->xsk_queue_idx);
918                         __atomic_fetch_add(&umem->refcnt, 1, __ATOMIC_ACQUIRE);
919                 }
920         }
921
922         if (umem == NULL) {
923                 usr_config.frame_size =
924                         rte_mempool_calc_obj_size(mb_pool->elt_size,
925                                                   mb_pool->flags, NULL);
926                 usr_config.frame_headroom = mb_pool->header_size +
927                                                 sizeof(struct rte_mbuf) +
928                                                 rte_pktmbuf_priv_size(mb_pool) +
929                                                 RTE_PKTMBUF_HEADROOM;
930
931                 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0,
932                                           rte_socket_id());
933                 if (umem == NULL) {
934                         AF_XDP_LOG(ERR, "Failed to allocate umem info");
935                         return NULL;
936                 }
937
938                 umem->mb_pool = mb_pool;
939                 base_addr = (void *)get_base_addr(mb_pool, &align);
940                 umem_size = mb_pool->populated_size * usr_config.frame_size +
941                                 align;
942
943                 ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
944                                 &rxq->fq, &rxq->cq, &usr_config);
945                 if (ret) {
946                         AF_XDP_LOG(ERR, "Failed to create umem");
947                         goto err;
948                 }
949                 umem->buffer = base_addr;
950
951                 if (internals->shared_umem) {
952                         umem->max_xsks = mb_pool->populated_size /
953                                                 ETH_AF_XDP_NUM_BUFFERS;
954                         AF_XDP_LOG(INFO, "Max xsks for UMEM %s: %u\n",
955                                                 mb_pool->name, umem->max_xsks);
956                 }
957
958                 __atomic_store_n(&umem->refcnt, 1, __ATOMIC_RELEASE);
959         }
960
961 #else
962 static struct
963 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
964                                   struct pkt_rx_queue *rxq)
965 {
966         struct xsk_umem_info *umem;
967         const struct rte_memzone *mz;
968         struct xsk_umem_config usr_config = {
969                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
970                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
971                 .frame_size = ETH_AF_XDP_FRAME_SIZE,
972                 .frame_headroom = 0 };
973         char ring_name[RTE_RING_NAMESIZE];
974         char mz_name[RTE_MEMZONE_NAMESIZE];
975         int ret;
976         uint64_t i;
977
978         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
979         if (umem == NULL) {
980                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
981                 return NULL;
982         }
983
984         snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
985                        internals->if_name, rxq->xsk_queue_idx);
986         umem->buf_ring = rte_ring_create(ring_name,
987                                          ETH_AF_XDP_NUM_BUFFERS,
988                                          rte_socket_id(),
989                                          0x0);
990         if (umem->buf_ring == NULL) {
991                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
992                 goto err;
993         }
994
995         for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
996                 rte_ring_enqueue(umem->buf_ring,
997                                  (void *)(i * ETH_AF_XDP_FRAME_SIZE));
998
999         snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1000                        internals->if_name, rxq->xsk_queue_idx);
1001         mz = rte_memzone_reserve_aligned(mz_name,
1002                         ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1003                         rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1004                         getpagesize());
1005         if (mz == NULL) {
1006                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1007                 goto err;
1008         }
1009
1010         ret = xsk_umem__create(&umem->umem, mz->addr,
1011                                ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1012                                &rxq->fq, &rxq->cq,
1013                                &usr_config);
1014
1015         if (ret) {
1016                 AF_XDP_LOG(ERR, "Failed to create umem");
1017                 goto err;
1018         }
1019         umem->mz = mz;
1020
1021 #endif
1022         return umem;
1023
1024 err:
1025         xdp_umem_destroy(umem);
1026         return NULL;
1027 }
1028
1029 static int
1030 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1031               int ring_size)
1032 {
1033         struct xsk_socket_config cfg;
1034         struct pkt_tx_queue *txq = rxq->pair;
1035         int ret = 0;
1036         int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1037         struct rte_mbuf *fq_bufs[reserve_size];
1038
1039         rxq->umem = xdp_umem_configure(internals, rxq);
1040         if (rxq->umem == NULL)
1041                 return -ENOMEM;
1042         txq->umem = rxq->umem;
1043
1044         cfg.rx_size = ring_size;
1045         cfg.tx_size = ring_size;
1046         cfg.libbpf_flags = 0;
1047         cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1048         cfg.bind_flags = 0;
1049
1050 #if defined(XDP_USE_NEED_WAKEUP)
1051         cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1052 #endif
1053
1054         if (internals->shared_umem)
1055                 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1056                                 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1057                                 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1058         else
1059                 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1060                                 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1061                                 &txq->tx, &cfg);
1062
1063         if (ret) {
1064                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1065                 goto err;
1066         }
1067
1068 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1069         if (rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size)) {
1070                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1071                 goto err;
1072         }
1073 #endif
1074         ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1075         if (ret) {
1076                 xsk_socket__delete(rxq->xsk);
1077                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1078                 goto err;
1079         }
1080
1081         return 0;
1082
1083 err:
1084         if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1085                 xdp_umem_destroy(rxq->umem);
1086
1087         return ret;
1088 }
1089
1090 static int
1091 eth_rx_queue_setup(struct rte_eth_dev *dev,
1092                    uint16_t rx_queue_id,
1093                    uint16_t nb_rx_desc,
1094                    unsigned int socket_id __rte_unused,
1095                    const struct rte_eth_rxconf *rx_conf __rte_unused,
1096                    struct rte_mempool *mb_pool)
1097 {
1098         struct pmd_internals *internals = dev->data->dev_private;
1099         struct pkt_rx_queue *rxq;
1100         int ret;
1101
1102         rxq = &internals->rx_queues[rx_queue_id];
1103
1104         AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1105                    rx_queue_id, rxq->xsk_queue_idx);
1106
1107 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1108         uint32_t buf_size, data_size;
1109
1110         /* Now get the space available for data in the mbuf */
1111         buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1112                 RTE_PKTMBUF_HEADROOM;
1113         data_size = ETH_AF_XDP_FRAME_SIZE;
1114
1115         if (data_size > buf_size) {
1116                 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1117                         dev->device->name, data_size, buf_size);
1118                 ret = -ENOMEM;
1119                 goto err;
1120         }
1121 #endif
1122
1123         rxq->mb_pool = mb_pool;
1124
1125         if (xsk_configure(internals, rxq, nb_rx_desc)) {
1126                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1127                 ret = -EINVAL;
1128                 goto err;
1129         }
1130
1131         rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1132         rxq->fds[0].events = POLLIN;
1133
1134         dev->data->rx_queues[rx_queue_id] = rxq;
1135         return 0;
1136
1137 err:
1138         return ret;
1139 }
1140
1141 static int
1142 eth_tx_queue_setup(struct rte_eth_dev *dev,
1143                    uint16_t tx_queue_id,
1144                    uint16_t nb_tx_desc __rte_unused,
1145                    unsigned int socket_id __rte_unused,
1146                    const struct rte_eth_txconf *tx_conf __rte_unused)
1147 {
1148         struct pmd_internals *internals = dev->data->dev_private;
1149         struct pkt_tx_queue *txq;
1150
1151         txq = &internals->tx_queues[tx_queue_id];
1152
1153         dev->data->tx_queues[tx_queue_id] = txq;
1154         return 0;
1155 }
1156
1157 static int
1158 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1159 {
1160         struct pmd_internals *internals = dev->data->dev_private;
1161         struct ifreq ifr = { .ifr_mtu = mtu };
1162         int ret;
1163         int s;
1164
1165         s = socket(PF_INET, SOCK_DGRAM, 0);
1166         if (s < 0)
1167                 return -EINVAL;
1168
1169         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1170         ret = ioctl(s, SIOCSIFMTU, &ifr);
1171         close(s);
1172
1173         return (ret < 0) ? -errno : 0;
1174 }
1175
1176 static int
1177 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1178 {
1179         struct ifreq ifr;
1180         int ret = 0;
1181         int s;
1182
1183         s = socket(PF_INET, SOCK_DGRAM, 0);
1184         if (s < 0)
1185                 return -errno;
1186
1187         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1188         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1189                 ret = -errno;
1190                 goto out;
1191         }
1192         ifr.ifr_flags &= mask;
1193         ifr.ifr_flags |= flags;
1194         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1195                 ret = -errno;
1196                 goto out;
1197         }
1198 out:
1199         close(s);
1200         return ret;
1201 }
1202
1203 static int
1204 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1205 {
1206         struct pmd_internals *internals = dev->data->dev_private;
1207
1208         return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1209 }
1210
1211 static int
1212 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1213 {
1214         struct pmd_internals *internals = dev->data->dev_private;
1215
1216         return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1217 }
1218
1219 static const struct eth_dev_ops ops = {
1220         .dev_start = eth_dev_start,
1221         .dev_stop = eth_dev_stop,
1222         .dev_close = eth_dev_close,
1223         .dev_configure = eth_dev_configure,
1224         .dev_infos_get = eth_dev_info,
1225         .mtu_set = eth_dev_mtu_set,
1226         .promiscuous_enable = eth_dev_promiscuous_enable,
1227         .promiscuous_disable = eth_dev_promiscuous_disable,
1228         .rx_queue_setup = eth_rx_queue_setup,
1229         .tx_queue_setup = eth_tx_queue_setup,
1230         .rx_queue_release = eth_queue_release,
1231         .tx_queue_release = eth_queue_release,
1232         .link_update = eth_link_update,
1233         .stats_get = eth_stats_get,
1234         .stats_reset = eth_stats_reset,
1235 };
1236
1237 /** parse integer from integer argument */
1238 static int
1239 parse_integer_arg(const char *key __rte_unused,
1240                   const char *value, void *extra_args)
1241 {
1242         int *i = (int *)extra_args;
1243         char *end;
1244
1245         *i = strtol(value, &end, 10);
1246         if (*i < 0) {
1247                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1248                 return -EINVAL;
1249         }
1250
1251         return 0;
1252 }
1253
1254 /** parse name argument */
1255 static int
1256 parse_name_arg(const char *key __rte_unused,
1257                const char *value, void *extra_args)
1258 {
1259         char *name = extra_args;
1260
1261         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1262                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1263                            value, IFNAMSIZ);
1264                 return -EINVAL;
1265         }
1266
1267         strlcpy(name, value, IFNAMSIZ);
1268
1269         return 0;
1270 }
1271
1272 static int
1273 xdp_get_channels_info(const char *if_name, int *max_queues,
1274                                 int *combined_queues)
1275 {
1276         struct ethtool_channels channels;
1277         struct ifreq ifr;
1278         int fd, ret;
1279
1280         fd = socket(AF_INET, SOCK_DGRAM, 0);
1281         if (fd < 0)
1282                 return -1;
1283
1284         channels.cmd = ETHTOOL_GCHANNELS;
1285         ifr.ifr_data = (void *)&channels;
1286         strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
1287         ret = ioctl(fd, SIOCETHTOOL, &ifr);
1288         if (ret) {
1289                 if (errno == EOPNOTSUPP) {
1290                         ret = 0;
1291                 } else {
1292                         ret = -errno;
1293                         goto out;
1294                 }
1295         }
1296
1297         if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1298                 /* If the device says it has no channels, then all traffic
1299                  * is sent to a single stream, so max queues = 1.
1300                  */
1301                 *max_queues = 1;
1302                 *combined_queues = 1;
1303         } else {
1304                 *max_queues = channels.max_combined;
1305                 *combined_queues = channels.combined_count;
1306         }
1307
1308  out:
1309         close(fd);
1310         return ret;
1311 }
1312
1313 static int
1314 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1315                         int *queue_cnt, int *shared_umem)
1316 {
1317         int ret;
1318
1319         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1320                                  &parse_name_arg, if_name);
1321         if (ret < 0)
1322                 goto free_kvlist;
1323
1324         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1325                                  &parse_integer_arg, start_queue);
1326         if (ret < 0)
1327                 goto free_kvlist;
1328
1329         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1330                                  &parse_integer_arg, queue_cnt);
1331         if (ret < 0 || *queue_cnt <= 0) {
1332                 ret = -EINVAL;
1333                 goto free_kvlist;
1334         }
1335
1336         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1337                                 &parse_integer_arg, shared_umem);
1338         if (ret < 0)
1339                 goto free_kvlist;
1340
1341 free_kvlist:
1342         rte_kvargs_free(kvlist);
1343         return ret;
1344 }
1345
1346 static int
1347 get_iface_info(const char *if_name,
1348                struct rte_ether_addr *eth_addr,
1349                int *if_index)
1350 {
1351         struct ifreq ifr;
1352         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1353
1354         if (sock < 0)
1355                 return -1;
1356
1357         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1358         if (ioctl(sock, SIOCGIFINDEX, &ifr))
1359                 goto error;
1360
1361         *if_index = ifr.ifr_ifindex;
1362
1363         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1364                 goto error;
1365
1366         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1367
1368         close(sock);
1369         return 0;
1370
1371 error:
1372         close(sock);
1373         return -1;
1374 }
1375
1376 static struct rte_eth_dev *
1377 init_internals(struct rte_vdev_device *dev, const char *if_name,
1378                         int start_queue_idx, int queue_cnt, int shared_umem)
1379 {
1380         const char *name = rte_vdev_device_name(dev);
1381         const unsigned int numa_node = dev->device.numa_node;
1382         struct pmd_internals *internals;
1383         struct rte_eth_dev *eth_dev;
1384         int ret;
1385         int i;
1386
1387         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1388         if (internals == NULL)
1389                 return NULL;
1390
1391         internals->start_queue_idx = start_queue_idx;
1392         internals->queue_cnt = queue_cnt;
1393         strlcpy(internals->if_name, if_name, IFNAMSIZ);
1394
1395 #ifndef ETH_AF_XDP_SHARED_UMEM
1396         if (shared_umem) {
1397                 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1398                                 "Check kernel and libbpf version\n");
1399                 goto err_free_internals;
1400         }
1401 #endif
1402         internals->shared_umem = shared_umem;
1403
1404         if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1405                                   &internals->combined_queue_cnt)) {
1406                 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1407                                 if_name);
1408                 goto err_free_internals;
1409         }
1410
1411         if (queue_cnt > internals->combined_queue_cnt) {
1412                 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1413                                 queue_cnt, internals->combined_queue_cnt);
1414                 goto err_free_internals;
1415         }
1416
1417         internals->rx_queues = rte_zmalloc_socket(NULL,
1418                                         sizeof(struct pkt_rx_queue) * queue_cnt,
1419                                         0, numa_node);
1420         if (internals->rx_queues == NULL) {
1421                 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1422                 goto err_free_internals;
1423         }
1424
1425         internals->tx_queues = rte_zmalloc_socket(NULL,
1426                                         sizeof(struct pkt_tx_queue) * queue_cnt,
1427                                         0, numa_node);
1428         if (internals->tx_queues == NULL) {
1429                 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1430                 goto err_free_rx;
1431         }
1432         for (i = 0; i < queue_cnt; i++) {
1433                 internals->tx_queues[i].pair = &internals->rx_queues[i];
1434                 internals->rx_queues[i].pair = &internals->tx_queues[i];
1435                 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1436                 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1437         }
1438
1439         ret = get_iface_info(if_name, &internals->eth_addr,
1440                              &internals->if_index);
1441         if (ret)
1442                 goto err_free_tx;
1443
1444         eth_dev = rte_eth_vdev_allocate(dev, 0);
1445         if (eth_dev == NULL)
1446                 goto err_free_tx;
1447
1448         eth_dev->data->dev_private = internals;
1449         eth_dev->data->dev_link = pmd_link;
1450         eth_dev->data->mac_addrs = &internals->eth_addr;
1451         eth_dev->dev_ops = &ops;
1452         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1453         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1454
1455 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1456         AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1457 #endif
1458
1459         return eth_dev;
1460
1461 err_free_tx:
1462         rte_free(internals->tx_queues);
1463 err_free_rx:
1464         rte_free(internals->rx_queues);
1465 err_free_internals:
1466         rte_free(internals);
1467         return NULL;
1468 }
1469
1470 static int
1471 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1472 {
1473         struct rte_kvargs *kvlist;
1474         char if_name[IFNAMSIZ] = {'\0'};
1475         int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1476         int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1477         int shared_umem = 0;
1478         struct rte_eth_dev *eth_dev = NULL;
1479         const char *name;
1480
1481         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1482                 rte_vdev_device_name(dev));
1483
1484         name = rte_vdev_device_name(dev);
1485         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1486                 strlen(rte_vdev_device_args(dev)) == 0) {
1487                 eth_dev = rte_eth_dev_attach_secondary(name);
1488                 if (eth_dev == NULL) {
1489                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1490                         return -EINVAL;
1491                 }
1492                 eth_dev->dev_ops = &ops;
1493                 rte_eth_dev_probing_finish(eth_dev);
1494                 return 0;
1495         }
1496
1497         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1498         if (kvlist == NULL) {
1499                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1500                 return -EINVAL;
1501         }
1502
1503         if (dev->device.numa_node == SOCKET_ID_ANY)
1504                 dev->device.numa_node = rte_socket_id();
1505
1506         if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1507                              &xsk_queue_cnt, &shared_umem) < 0) {
1508                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1509                 return -EINVAL;
1510         }
1511
1512         if (strlen(if_name) == 0) {
1513                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1514                 return -EINVAL;
1515         }
1516
1517         eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1518                                         xsk_queue_cnt, shared_umem);
1519         if (eth_dev == NULL) {
1520                 AF_XDP_LOG(ERR, "Failed to init internals\n");
1521                 return -1;
1522         }
1523
1524         rte_eth_dev_probing_finish(eth_dev);
1525
1526         return 0;
1527 }
1528
1529 static int
1530 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1531 {
1532         struct rte_eth_dev *eth_dev = NULL;
1533
1534         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1535                 rte_socket_id());
1536
1537         if (dev == NULL)
1538                 return -1;
1539
1540         /* find the ethdev entry */
1541         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1542         if (eth_dev == NULL)
1543                 return 0;
1544
1545         eth_dev_close(eth_dev);
1546         rte_eth_dev_release_port(eth_dev);
1547
1548
1549         return 0;
1550 }
1551
1552 static struct rte_vdev_driver pmd_af_xdp_drv = {
1553         .probe = rte_pmd_af_xdp_probe,
1554         .remove = rte_pmd_af_xdp_remove,
1555 };
1556
1557 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1558 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1559                               "iface=<string> "
1560                               "start_queue=<int> "
1561                               "queue_count=<int> "
1562                               "shared_umem=<int> ");