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