net/af_xdp: revert to MP/MC ring for copy mode
[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 static int af_xdp_logtype;
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 -1;
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 (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts) != 0))
308                 return 0;
309
310         rcvd = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
311         if (rcvd == 0) {
312 #if defined(XDP_USE_NEED_WAKEUP)
313                 if (xsk_ring_prod__needs_wakeup(fq))
314                         (void)poll(rxq->fds, 1, 1000);
315 #endif
316
317                 goto out;
318         }
319
320         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
321                 (void)reserve_fill_queue(umem, ETH_AF_XDP_RX_BATCH_SIZE, NULL);
322
323         for (i = 0; i < rcvd; i++) {
324                 const struct xdp_desc *desc;
325                 uint64_t addr;
326                 uint32_t len;
327                 void *pkt;
328
329                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
330                 addr = desc->addr;
331                 len = desc->len;
332                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
333
334                 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
335                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
336                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
337                 rte_pktmbuf_data_len(mbufs[i]) = len;
338                 rx_bytes += len;
339                 bufs[i] = mbufs[i];
340         }
341
342         xsk_ring_cons__release(rx, rcvd);
343
344         /* statistics */
345         rxq->stats.rx_pkts += rcvd;
346         rxq->stats.rx_bytes += rx_bytes;
347
348 out:
349         if (rcvd != nb_pkts)
350                 rte_mempool_put_bulk(rxq->mb_pool, (void **)&mbufs[rcvd],
351                                      nb_pkts - rcvd);
352
353         return rcvd;
354 }
355 #endif
356
357 static uint16_t
358 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
359 {
360         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
361
362 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
363         return af_xdp_rx_zc(queue, bufs, nb_pkts);
364 #else
365         return af_xdp_rx_cp(queue, bufs, nb_pkts);
366 #endif
367 }
368
369 static void
370 pull_umem_cq(struct xsk_umem_info *umem, int size)
371 {
372         struct xsk_ring_cons *cq = &umem->cq;
373         size_t i, n;
374         uint32_t idx_cq = 0;
375
376         n = xsk_ring_cons__peek(cq, size, &idx_cq);
377
378         for (i = 0; i < n; i++) {
379                 uint64_t addr;
380                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
381 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
382                 addr = xsk_umem__extract_addr(addr);
383                 rte_pktmbuf_free((struct rte_mbuf *)
384                                         xsk_umem__get_data(umem->buffer,
385                                         addr + umem->mb_pool->header_size));
386 #else
387                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
388 #endif
389         }
390
391         xsk_ring_cons__release(cq, n);
392 }
393
394 static void
395 kick_tx(struct pkt_tx_queue *txq)
396 {
397         struct xsk_umem_info *umem = txq->umem;
398
399         pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS);
400
401 #if defined(XDP_USE_NEED_WAKEUP)
402         if (xsk_ring_prod__needs_wakeup(&txq->tx))
403 #endif
404                 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
405                             0, MSG_DONTWAIT) < 0) {
406                         /* some thing unexpected */
407                         if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
408                                 break;
409
410                         /* pull from completion queue to leave more space */
411                         if (errno == EAGAIN)
412                                 pull_umem_cq(umem,
413                                              XSK_RING_CONS__DEFAULT_NUM_DESCS);
414                 }
415 }
416
417 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
418 static uint16_t
419 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
420 {
421         struct pkt_tx_queue *txq = queue;
422         struct xsk_umem_info *umem = txq->umem;
423         struct rte_mbuf *mbuf;
424         unsigned long tx_bytes = 0;
425         int i;
426         uint32_t idx_tx;
427         uint16_t count = 0;
428         struct xdp_desc *desc;
429         uint64_t addr, offset;
430         uint32_t free_thresh = umem->cq.size >> 1;
431
432         if (xsk_cons_nb_avail(&umem->cq, free_thresh) >= free_thresh)
433                 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS);
434
435         for (i = 0; i < nb_pkts; i++) {
436                 mbuf = bufs[i];
437
438                 if (mbuf->pool == umem->mb_pool) {
439                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
440                                 kick_tx(txq);
441                                 if (!xsk_ring_prod__reserve(&txq->tx, 1,
442                                                             &idx_tx))
443                                         goto out;
444                         }
445                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
446                         desc->len = mbuf->pkt_len;
447                         addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
448                                         umem->mb_pool->header_size;
449                         offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
450                                         (uint64_t)mbuf +
451                                         umem->mb_pool->header_size;
452                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
453                         desc->addr = addr | offset;
454                         count++;
455                 } else {
456                         struct rte_mbuf *local_mbuf =
457                                         rte_pktmbuf_alloc(umem->mb_pool);
458                         void *pkt;
459
460                         if (local_mbuf == NULL)
461                                 goto out;
462
463                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
464                                 rte_pktmbuf_free(local_mbuf);
465                                 kick_tx(txq);
466                                 goto out;
467                         }
468
469                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
470                         desc->len = mbuf->pkt_len;
471
472                         addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
473                                         umem->mb_pool->header_size;
474                         offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
475                                         (uint64_t)local_mbuf +
476                                         umem->mb_pool->header_size;
477                         pkt = xsk_umem__get_data(umem->buffer, addr + offset);
478                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
479                         desc->addr = addr | offset;
480                         rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
481                                         desc->len);
482                         rte_pktmbuf_free(mbuf);
483                         count++;
484                 }
485
486                 tx_bytes += mbuf->pkt_len;
487         }
488
489         kick_tx(txq);
490
491 out:
492         xsk_ring_prod__submit(&txq->tx, count);
493
494         txq->stats.tx_pkts += count;
495         txq->stats.tx_bytes += tx_bytes;
496         txq->stats.tx_dropped += nb_pkts - count;
497
498         return count;
499 }
500 #else
501 static uint16_t
502 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
503 {
504         struct pkt_tx_queue *txq = queue;
505         struct xsk_umem_info *umem = txq->umem;
506         struct rte_mbuf *mbuf;
507         void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
508         unsigned long tx_bytes = 0;
509         int i;
510         uint32_t idx_tx;
511
512         nb_pkts = RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
513
514         pull_umem_cq(umem, nb_pkts);
515
516         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
517                                         nb_pkts, NULL);
518         if (nb_pkts == 0)
519                 return 0;
520
521         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
522                 kick_tx(txq);
523                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
524                 return 0;
525         }
526
527         for (i = 0; i < nb_pkts; i++) {
528                 struct xdp_desc *desc;
529                 void *pkt;
530
531                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
532                 mbuf = bufs[i];
533                 desc->len = mbuf->pkt_len;
534
535                 desc->addr = (uint64_t)addrs[i];
536                 pkt = xsk_umem__get_data(umem->mz->addr,
537                                          desc->addr);
538                 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
539                 tx_bytes += mbuf->pkt_len;
540                 rte_pktmbuf_free(mbuf);
541         }
542
543         xsk_ring_prod__submit(&txq->tx, nb_pkts);
544
545         kick_tx(txq);
546
547         txq->stats.tx_pkts += nb_pkts;
548         txq->stats.tx_bytes += tx_bytes;
549
550         return nb_pkts;
551 }
552 #endif
553
554 static uint16_t
555 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
556 {
557 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
558         return af_xdp_tx_zc(queue, bufs, nb_pkts);
559 #else
560         return af_xdp_tx_cp(queue, bufs, nb_pkts);
561 #endif
562 }
563
564 static int
565 eth_dev_start(struct rte_eth_dev *dev)
566 {
567         dev->data->dev_link.link_status = ETH_LINK_UP;
568
569         return 0;
570 }
571
572 /* This function gets called when the current port gets stopped. */
573 static void
574 eth_dev_stop(struct rte_eth_dev *dev)
575 {
576         dev->data->dev_link.link_status = ETH_LINK_DOWN;
577 }
578
579 static int
580 eth_dev_configure(struct rte_eth_dev *dev)
581 {
582         /* rx/tx must be paired */
583         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
584                 return -EINVAL;
585
586         return 0;
587 }
588
589 static int
590 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
591 {
592         struct pmd_internals *internals = dev->data->dev_private;
593
594         dev_info->if_index = internals->if_index;
595         dev_info->max_mac_addrs = 1;
596         dev_info->max_rx_pktlen = ETH_FRAME_LEN;
597         dev_info->max_rx_queues = internals->queue_cnt;
598         dev_info->max_tx_queues = internals->queue_cnt;
599
600         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
601 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
602         dev_info->max_mtu = getpagesize() -
603                                 sizeof(struct rte_mempool_objhdr) -
604                                 sizeof(struct rte_mbuf) -
605                                 RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
606 #else
607         dev_info->max_mtu = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
608 #endif
609
610         dev_info->default_rxportconf.nb_queues = 1;
611         dev_info->default_txportconf.nb_queues = 1;
612         dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
613         dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
614
615         return 0;
616 }
617
618 static int
619 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
620 {
621         struct pmd_internals *internals = dev->data->dev_private;
622         struct xdp_statistics xdp_stats;
623         struct pkt_rx_queue *rxq;
624         struct pkt_tx_queue *txq;
625         socklen_t optlen;
626         int i, ret;
627
628         for (i = 0; i < dev->data->nb_rx_queues; i++) {
629                 optlen = sizeof(struct xdp_statistics);
630                 rxq = &internals->rx_queues[i];
631                 txq = rxq->pair;
632                 stats->q_ipackets[i] = rxq->stats.rx_pkts;
633                 stats->q_ibytes[i] = rxq->stats.rx_bytes;
634
635                 stats->q_opackets[i] = txq->stats.tx_pkts;
636                 stats->q_obytes[i] = txq->stats.tx_bytes;
637
638                 stats->ipackets += stats->q_ipackets[i];
639                 stats->ibytes += stats->q_ibytes[i];
640                 stats->imissed += rxq->stats.rx_dropped;
641                 stats->oerrors += txq->stats.tx_dropped;
642                 ret = getsockopt(xsk_socket__fd(rxq->xsk), SOL_XDP,
643                                 XDP_STATISTICS, &xdp_stats, &optlen);
644                 if (ret != 0) {
645                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
646                         return -1;
647                 }
648                 stats->imissed += xdp_stats.rx_dropped;
649
650                 stats->opackets += stats->q_opackets[i];
651                 stats->obytes += stats->q_obytes[i];
652         }
653
654         return 0;
655 }
656
657 static int
658 eth_stats_reset(struct rte_eth_dev *dev)
659 {
660         struct pmd_internals *internals = dev->data->dev_private;
661         int i;
662
663         for (i = 0; i < internals->queue_cnt; i++) {
664                 memset(&internals->rx_queues[i].stats, 0,
665                                         sizeof(struct rx_stats));
666                 memset(&internals->tx_queues[i].stats, 0,
667                                         sizeof(struct tx_stats));
668         }
669
670         return 0;
671 }
672
673 static void
674 remove_xdp_program(struct pmd_internals *internals)
675 {
676         uint32_t curr_prog_id = 0;
677
678         if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
679                                 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
680                 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
681                 return;
682         }
683         bpf_set_link_xdp_fd(internals->if_index, -1,
684                         XDP_FLAGS_UPDATE_IF_NOEXIST);
685 }
686
687 static void
688 xdp_umem_destroy(struct xsk_umem_info *umem)
689 {
690 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
691         rte_mempool_free(umem->mb_pool);
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)
751 {
752         struct rte_mempool_memhdr *memhdr;
753
754         memhdr = STAILQ_FIRST(&mp->mem_list);
755         return (uint64_t)memhdr->addr & ~(getpagesize() - 1);
756 }
757
758 static struct
759 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals __rte_unused,
760                                   struct pkt_rx_queue *rxq)
761 {
762         struct xsk_umem_info *umem;
763         int ret;
764         struct xsk_umem_config usr_config = {
765                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
766                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
767                 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
768         void *base_addr = NULL;
769         struct rte_mempool *mb_pool = rxq->mb_pool;
770
771         usr_config.frame_size = rte_mempool_calc_obj_size(mb_pool->elt_size,
772                                                                 mb_pool->flags,
773                                                                 NULL);
774         usr_config.frame_headroom = mb_pool->header_size +
775                                         sizeof(struct rte_mbuf) +
776                                         rte_pktmbuf_priv_size(mb_pool) +
777                                         RTE_PKTMBUF_HEADROOM;
778
779         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
780         if (umem == NULL) {
781                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
782                 return NULL;
783         }
784
785         umem->mb_pool = mb_pool;
786         base_addr = (void *)get_base_addr(mb_pool);
787
788         ret = xsk_umem__create(&umem->umem, base_addr,
789                                mb_pool->populated_size * usr_config.frame_size,
790                                &umem->fq, &umem->cq,
791                                &usr_config);
792
793         if (ret) {
794                 AF_XDP_LOG(ERR, "Failed to create umem");
795                 goto err;
796         }
797         umem->buffer = base_addr;
798
799 #else
800 static struct
801 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
802                                   struct pkt_rx_queue *rxq)
803 {
804         struct xsk_umem_info *umem;
805         const struct rte_memzone *mz;
806         struct xsk_umem_config usr_config = {
807                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
808                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
809                 .frame_size = ETH_AF_XDP_FRAME_SIZE,
810                 .frame_headroom = 0 };
811         char ring_name[RTE_RING_NAMESIZE];
812         char mz_name[RTE_MEMZONE_NAMESIZE];
813         int ret;
814         uint64_t i;
815
816         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
817         if (umem == NULL) {
818                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
819                 return NULL;
820         }
821
822         snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
823                        internals->if_name, rxq->xsk_queue_idx);
824         umem->buf_ring = rte_ring_create(ring_name,
825                                          ETH_AF_XDP_NUM_BUFFERS,
826                                          rte_socket_id(),
827                                          0x0);
828         if (umem->buf_ring == NULL) {
829                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
830                 goto err;
831         }
832
833         for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
834                 rte_ring_enqueue(umem->buf_ring,
835                                  (void *)(i * ETH_AF_XDP_FRAME_SIZE));
836
837         snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
838                        internals->if_name, rxq->xsk_queue_idx);
839         mz = rte_memzone_reserve_aligned(mz_name,
840                         ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
841                         rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
842                         getpagesize());
843         if (mz == NULL) {
844                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
845                 goto err;
846         }
847
848         ret = xsk_umem__create(&umem->umem, mz->addr,
849                                ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
850                                &umem->fq, &umem->cq,
851                                &usr_config);
852
853         if (ret) {
854                 AF_XDP_LOG(ERR, "Failed to create umem");
855                 goto err;
856         }
857         umem->mz = mz;
858
859 #endif
860         return umem;
861
862 err:
863         xdp_umem_destroy(umem);
864         return NULL;
865 }
866
867 static int
868 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
869               int ring_size)
870 {
871         struct xsk_socket_config cfg;
872         struct pkt_tx_queue *txq = rxq->pair;
873         int ret = 0;
874         int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
875         struct rte_mbuf *fq_bufs[reserve_size];
876
877         rxq->umem = xdp_umem_configure(internals, rxq);
878         if (rxq->umem == NULL)
879                 return -ENOMEM;
880         txq->umem = rxq->umem;
881
882         cfg.rx_size = ring_size;
883         cfg.tx_size = ring_size;
884         cfg.libbpf_flags = 0;
885         cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
886         cfg.bind_flags = 0;
887
888 #if defined(XDP_USE_NEED_WAKEUP)
889         cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
890 #endif
891
892         ret = xsk_socket__create(&rxq->xsk, internals->if_name,
893                         rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
894                         &txq->tx, &cfg);
895         if (ret) {
896                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
897                 goto err;
898         }
899
900 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
901         if (rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size)) {
902                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
903                 goto err;
904         }
905 #endif
906         ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs);
907         if (ret) {
908                 xsk_socket__delete(rxq->xsk);
909                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
910                 goto err;
911         }
912
913         return 0;
914
915 err:
916         xdp_umem_destroy(rxq->umem);
917
918         return ret;
919 }
920
921 static int
922 eth_rx_queue_setup(struct rte_eth_dev *dev,
923                    uint16_t rx_queue_id,
924                    uint16_t nb_rx_desc,
925                    unsigned int socket_id __rte_unused,
926                    const struct rte_eth_rxconf *rx_conf __rte_unused,
927                    struct rte_mempool *mb_pool)
928 {
929         struct pmd_internals *internals = dev->data->dev_private;
930         struct pkt_rx_queue *rxq;
931         int ret;
932
933         rxq = &internals->rx_queues[rx_queue_id];
934
935         AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
936                    rx_queue_id, rxq->xsk_queue_idx);
937
938 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
939         uint32_t buf_size, data_size;
940
941         /* Now get the space available for data in the mbuf */
942         buf_size = rte_pktmbuf_data_room_size(mb_pool) -
943                 RTE_PKTMBUF_HEADROOM;
944         data_size = ETH_AF_XDP_FRAME_SIZE;
945
946         if (data_size > buf_size) {
947                 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
948                         dev->device->name, data_size, buf_size);
949                 ret = -ENOMEM;
950                 goto err;
951         }
952 #endif
953
954         rxq->mb_pool = mb_pool;
955
956         if (xsk_configure(internals, rxq, nb_rx_desc)) {
957                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
958                 ret = -EINVAL;
959                 goto err;
960         }
961
962         rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
963         rxq->fds[0].events = POLLIN;
964
965         dev->data->rx_queues[rx_queue_id] = rxq;
966         return 0;
967
968 err:
969         return ret;
970 }
971
972 static int
973 eth_tx_queue_setup(struct rte_eth_dev *dev,
974                    uint16_t tx_queue_id,
975                    uint16_t nb_tx_desc __rte_unused,
976                    unsigned int socket_id __rte_unused,
977                    const struct rte_eth_txconf *tx_conf __rte_unused)
978 {
979         struct pmd_internals *internals = dev->data->dev_private;
980         struct pkt_tx_queue *txq;
981
982         txq = &internals->tx_queues[tx_queue_id];
983
984         dev->data->tx_queues[tx_queue_id] = txq;
985         return 0;
986 }
987
988 static int
989 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
990 {
991         struct pmd_internals *internals = dev->data->dev_private;
992         struct ifreq ifr = { .ifr_mtu = mtu };
993         int ret;
994         int s;
995
996         s = socket(PF_INET, SOCK_DGRAM, 0);
997         if (s < 0)
998                 return -EINVAL;
999
1000         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1001         ret = ioctl(s, SIOCSIFMTU, &ifr);
1002         close(s);
1003
1004         return (ret < 0) ? -errno : 0;
1005 }
1006
1007 static int
1008 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1009 {
1010         struct ifreq ifr;
1011         int ret = 0;
1012         int s;
1013
1014         s = socket(PF_INET, SOCK_DGRAM, 0);
1015         if (s < 0)
1016                 return -errno;
1017
1018         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1019         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1020                 ret = -errno;
1021                 goto out;
1022         }
1023         ifr.ifr_flags &= mask;
1024         ifr.ifr_flags |= flags;
1025         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1026                 ret = -errno;
1027                 goto out;
1028         }
1029 out:
1030         close(s);
1031         return ret;
1032 }
1033
1034 static int
1035 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1036 {
1037         struct pmd_internals *internals = dev->data->dev_private;
1038
1039         return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1040 }
1041
1042 static int
1043 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1044 {
1045         struct pmd_internals *internals = dev->data->dev_private;
1046
1047         return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1048 }
1049
1050 static const struct eth_dev_ops ops = {
1051         .dev_start = eth_dev_start,
1052         .dev_stop = eth_dev_stop,
1053         .dev_close = eth_dev_close,
1054         .dev_configure = eth_dev_configure,
1055         .dev_infos_get = eth_dev_info,
1056         .mtu_set = eth_dev_mtu_set,
1057         .promiscuous_enable = eth_dev_promiscuous_enable,
1058         .promiscuous_disable = eth_dev_promiscuous_disable,
1059         .rx_queue_setup = eth_rx_queue_setup,
1060         .tx_queue_setup = eth_tx_queue_setup,
1061         .rx_queue_release = eth_queue_release,
1062         .tx_queue_release = eth_queue_release,
1063         .link_update = eth_link_update,
1064         .stats_get = eth_stats_get,
1065         .stats_reset = eth_stats_reset,
1066 };
1067
1068 /** parse integer from integer argument */
1069 static int
1070 parse_integer_arg(const char *key __rte_unused,
1071                   const char *value, void *extra_args)
1072 {
1073         int *i = (int *)extra_args;
1074         char *end;
1075
1076         *i = strtol(value, &end, 10);
1077         if (*i < 0) {
1078                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1079                 return -EINVAL;
1080         }
1081
1082         return 0;
1083 }
1084
1085 /** parse name argument */
1086 static int
1087 parse_name_arg(const char *key __rte_unused,
1088                const char *value, void *extra_args)
1089 {
1090         char *name = extra_args;
1091
1092         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1093                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1094                            value, IFNAMSIZ);
1095                 return -EINVAL;
1096         }
1097
1098         strlcpy(name, value, IFNAMSIZ);
1099
1100         return 0;
1101 }
1102
1103 static int
1104 xdp_get_channels_info(const char *if_name, int *max_queues,
1105                                 int *combined_queues)
1106 {
1107         struct ethtool_channels channels;
1108         struct ifreq ifr;
1109         int fd, ret;
1110
1111         fd = socket(AF_INET, SOCK_DGRAM, 0);
1112         if (fd < 0)
1113                 return -1;
1114
1115         channels.cmd = ETHTOOL_GCHANNELS;
1116         ifr.ifr_data = (void *)&channels;
1117         strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
1118         ret = ioctl(fd, SIOCETHTOOL, &ifr);
1119         if (ret) {
1120                 if (errno == EOPNOTSUPP) {
1121                         ret = 0;
1122                 } else {
1123                         ret = -errno;
1124                         goto out;
1125                 }
1126         }
1127
1128         if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1129                 /* If the device says it has no channels, then all traffic
1130                  * is sent to a single stream, so max queues = 1.
1131                  */
1132                 *max_queues = 1;
1133                 *combined_queues = 1;
1134         } else {
1135                 *max_queues = channels.max_combined;
1136                 *combined_queues = channels.combined_count;
1137         }
1138
1139  out:
1140         close(fd);
1141         return ret;
1142 }
1143
1144 static int
1145 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1146                         int *queue_cnt)
1147 {
1148         int ret;
1149
1150         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1151                                  &parse_name_arg, if_name);
1152         if (ret < 0)
1153                 goto free_kvlist;
1154
1155         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1156                                  &parse_integer_arg, start_queue);
1157         if (ret < 0)
1158                 goto free_kvlist;
1159
1160         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1161                                  &parse_integer_arg, queue_cnt);
1162         if (ret < 0 || *queue_cnt <= 0) {
1163                 ret = -EINVAL;
1164                 goto free_kvlist;
1165         }
1166
1167 free_kvlist:
1168         rte_kvargs_free(kvlist);
1169         return ret;
1170 }
1171
1172 static int
1173 get_iface_info(const char *if_name,
1174                struct rte_ether_addr *eth_addr,
1175                int *if_index)
1176 {
1177         struct ifreq ifr;
1178         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1179
1180         if (sock < 0)
1181                 return -1;
1182
1183         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1184         if (ioctl(sock, SIOCGIFINDEX, &ifr))
1185                 goto error;
1186
1187         *if_index = ifr.ifr_ifindex;
1188
1189         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1190                 goto error;
1191
1192         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1193
1194         close(sock);
1195         return 0;
1196
1197 error:
1198         close(sock);
1199         return -1;
1200 }
1201
1202 static struct rte_eth_dev *
1203 init_internals(struct rte_vdev_device *dev, const char *if_name,
1204                         int start_queue_idx, int queue_cnt)
1205 {
1206         const char *name = rte_vdev_device_name(dev);
1207         const unsigned int numa_node = dev->device.numa_node;
1208         struct pmd_internals *internals;
1209         struct rte_eth_dev *eth_dev;
1210         int ret;
1211         int i;
1212
1213         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1214         if (internals == NULL)
1215                 return NULL;
1216
1217         internals->start_queue_idx = start_queue_idx;
1218         internals->queue_cnt = queue_cnt;
1219         strlcpy(internals->if_name, if_name, IFNAMSIZ);
1220
1221         if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1222                                   &internals->combined_queue_cnt)) {
1223                 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1224                                 if_name);
1225                 goto err_free_internals;
1226         }
1227
1228         if (queue_cnt > internals->combined_queue_cnt) {
1229                 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1230                                 queue_cnt, internals->combined_queue_cnt);
1231                 goto err_free_internals;
1232         }
1233
1234         internals->rx_queues = rte_zmalloc_socket(NULL,
1235                                         sizeof(struct pkt_rx_queue) * queue_cnt,
1236                                         0, numa_node);
1237         if (internals->rx_queues == NULL) {
1238                 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1239                 goto err_free_internals;
1240         }
1241
1242         internals->tx_queues = rte_zmalloc_socket(NULL,
1243                                         sizeof(struct pkt_tx_queue) * queue_cnt,
1244                                         0, numa_node);
1245         if (internals->tx_queues == NULL) {
1246                 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1247                 goto err_free_rx;
1248         }
1249         for (i = 0; i < queue_cnt; i++) {
1250                 internals->tx_queues[i].pair = &internals->rx_queues[i];
1251                 internals->rx_queues[i].pair = &internals->tx_queues[i];
1252                 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1253                 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1254         }
1255
1256         ret = get_iface_info(if_name, &internals->eth_addr,
1257                              &internals->if_index);
1258         if (ret)
1259                 goto err_free_tx;
1260
1261         eth_dev = rte_eth_vdev_allocate(dev, 0);
1262         if (eth_dev == NULL)
1263                 goto err_free_tx;
1264
1265         eth_dev->data->dev_private = internals;
1266         eth_dev->data->dev_link = pmd_link;
1267         eth_dev->data->mac_addrs = &internals->eth_addr;
1268         eth_dev->dev_ops = &ops;
1269         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1270         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1271         /* Let rte_eth_dev_close() release the port resources. */
1272         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1273
1274 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1275         AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1276 #endif
1277
1278         return eth_dev;
1279
1280 err_free_tx:
1281         rte_free(internals->tx_queues);
1282 err_free_rx:
1283         rte_free(internals->rx_queues);
1284 err_free_internals:
1285         rte_free(internals);
1286         return NULL;
1287 }
1288
1289 static int
1290 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1291 {
1292         struct rte_kvargs *kvlist;
1293         char if_name[IFNAMSIZ] = {'\0'};
1294         int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1295         int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1296         struct rte_eth_dev *eth_dev = NULL;
1297         const char *name;
1298
1299         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n",
1300                 rte_vdev_device_name(dev));
1301
1302         name = rte_vdev_device_name(dev);
1303         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1304                 strlen(rte_vdev_device_args(dev)) == 0) {
1305                 eth_dev = rte_eth_dev_attach_secondary(name);
1306                 if (eth_dev == NULL) {
1307                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1308                         return -EINVAL;
1309                 }
1310                 eth_dev->dev_ops = &ops;
1311                 rte_eth_dev_probing_finish(eth_dev);
1312                 return 0;
1313         }
1314
1315         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1316         if (kvlist == NULL) {
1317                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1318                 return -EINVAL;
1319         }
1320
1321         if (dev->device.numa_node == SOCKET_ID_ANY)
1322                 dev->device.numa_node = rte_socket_id();
1323
1324         if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1325                              &xsk_queue_cnt) < 0) {
1326                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1327                 return -EINVAL;
1328         }
1329
1330         if (strlen(if_name) == 0) {
1331                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1332                 return -EINVAL;
1333         }
1334
1335         eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1336                                         xsk_queue_cnt);
1337         if (eth_dev == NULL) {
1338                 AF_XDP_LOG(ERR, "Failed to init internals\n");
1339                 return -1;
1340         }
1341
1342         rte_eth_dev_probing_finish(eth_dev);
1343
1344         return 0;
1345 }
1346
1347 static int
1348 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
1349 {
1350         struct rte_eth_dev *eth_dev = NULL;
1351
1352         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
1353                 rte_socket_id());
1354
1355         if (dev == NULL)
1356                 return -1;
1357
1358         /* find the ethdev entry */
1359         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1360         if (eth_dev == NULL)
1361                 return 0;
1362
1363         eth_dev_close(eth_dev);
1364         rte_eth_dev_release_port(eth_dev);
1365
1366
1367         return 0;
1368 }
1369
1370 static struct rte_vdev_driver pmd_af_xdp_drv = {
1371         .probe = rte_pmd_af_xdp_probe,
1372         .remove = rte_pmd_af_xdp_remove,
1373 };
1374
1375 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
1376 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
1377                               "iface=<string> "
1378                               "start_queue=<int> "
1379                               "queue_count=<int> ");
1380
1381 RTE_INIT(af_xdp_init_log)
1382 {
1383         af_xdp_logtype = rte_log_register("pmd.net.af_xdp");
1384         if (af_xdp_logtype >= 0)
1385                 rte_log_set_level(af_xdp_logtype, RTE_LOG_NOTICE);
1386 }