net/ark: add performance optimizations
[dpdk.git] / drivers / net / af_xdp / rte_eth_af_xdp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Intel Corporation.
3  */
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <netinet/in.h>
9 #include <net/if.h>
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <linux/if_ether.h>
13 #include <linux/if_xdp.h>
14 #include <linux/if_link.h>
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
17 #include "af_xdp_deps.h"
18 #include <bpf/bpf.h>
19
20 #include <rte_ethdev.h>
21 #include <ethdev_driver.h>
22 #include <ethdev_vdev.h>
23 #include <rte_kvargs.h>
24 #include <rte_bus_vdev.h>
25 #include <rte_string_fns.h>
26 #include <rte_branch_prediction.h>
27 #include <rte_common.h>
28 #include <rte_dev.h>
29 #include <rte_eal.h>
30 #include <rte_ether.h>
31 #include <rte_lcore.h>
32 #include <rte_log.h>
33 #include <rte_memory.h>
34 #include <rte_memzone.h>
35 #include <rte_mempool.h>
36 #include <rte_mbuf.h>
37 #include <rte_malloc.h>
38 #include <rte_ring.h>
39 #include <rte_spinlock.h>
40 #include <rte_power_intrinsics.h>
41
42 #include "compat.h"
43
44 #ifndef SO_PREFER_BUSY_POLL
45 #define SO_PREFER_BUSY_POLL 69
46 #endif
47 #ifndef SO_BUSY_POLL_BUDGET
48 #define SO_BUSY_POLL_BUDGET 70
49 #endif
50
51
52 #ifndef SOL_XDP
53 #define SOL_XDP 283
54 #endif
55
56 #ifndef AF_XDP
57 #define AF_XDP 44
58 #endif
59
60 #ifndef PF_XDP
61 #define PF_XDP AF_XDP
62 #endif
63
64 RTE_LOG_REGISTER_DEFAULT(af_xdp_logtype, NOTICE);
65
66 #define AF_XDP_LOG(level, fmt, args...)                 \
67         rte_log(RTE_LOG_ ## level, af_xdp_logtype,      \
68                 "%s(): " fmt, __func__, ##args)
69
70 #define ETH_AF_XDP_FRAME_SIZE           2048
71 #define ETH_AF_XDP_NUM_BUFFERS          4096
72 #define ETH_AF_XDP_DFLT_NUM_DESCS       XSK_RING_CONS__DEFAULT_NUM_DESCS
73 #define ETH_AF_XDP_DFLT_START_QUEUE_IDX 0
74 #define ETH_AF_XDP_DFLT_QUEUE_COUNT     1
75 #define ETH_AF_XDP_DFLT_BUSY_BUDGET     64
76 #define ETH_AF_XDP_DFLT_BUSY_TIMEOUT    20
77
78 #define ETH_AF_XDP_RX_BATCH_SIZE        XSK_RING_CONS__DEFAULT_NUM_DESCS
79 #define ETH_AF_XDP_TX_BATCH_SIZE        XSK_RING_CONS__DEFAULT_NUM_DESCS
80
81 #define ETH_AF_XDP_ETH_OVERHEAD         (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
82
83 #define ETH_AF_XDP_MP_KEY "afxdp_mp_send_fds"
84
85 static int afxdp_dev_count;
86
87 /* Message header to synchronize fds via IPC */
88 struct ipc_hdr {
89         char port_name[RTE_DEV_NAME_MAX_LEN];
90         /* The file descriptors are in the dedicated part
91          * of the Unix message to be translated by the kernel.
92          */
93 };
94
95 struct xsk_umem_info {
96         struct xsk_umem *umem;
97         struct rte_ring *buf_ring;
98         const struct rte_memzone *mz;
99         struct rte_mempool *mb_pool;
100         void *buffer;
101         uint8_t refcnt;
102         uint32_t max_xsks;
103 };
104
105 struct rx_stats {
106         uint64_t rx_pkts;
107         uint64_t rx_bytes;
108         uint64_t rx_dropped;
109 };
110
111 struct pkt_rx_queue {
112         struct xsk_ring_cons rx;
113         struct xsk_umem_info *umem;
114         struct xsk_socket *xsk;
115         struct rte_mempool *mb_pool;
116
117         struct rx_stats stats;
118
119         struct xsk_ring_prod fq;
120         struct xsk_ring_cons cq;
121
122         struct pkt_tx_queue *pair;
123         struct pollfd fds[1];
124         int xsk_queue_idx;
125         int busy_budget;
126 };
127
128 struct tx_stats {
129         uint64_t tx_pkts;
130         uint64_t tx_bytes;
131         uint64_t tx_dropped;
132 };
133
134 struct pkt_tx_queue {
135         struct xsk_ring_prod tx;
136         struct xsk_umem_info *umem;
137
138         struct tx_stats stats;
139
140         struct pkt_rx_queue *pair;
141         int xsk_queue_idx;
142 };
143
144 struct pmd_internals {
145         int if_index;
146         char if_name[IFNAMSIZ];
147         int start_queue_idx;
148         int queue_cnt;
149         int max_queue_cnt;
150         int combined_queue_cnt;
151         bool shared_umem;
152         char prog_path[PATH_MAX];
153         bool custom_prog_configured;
154         struct bpf_map *map;
155
156         struct rte_ether_addr eth_addr;
157
158         struct pkt_rx_queue *rx_queues;
159         struct pkt_tx_queue *tx_queues;
160 };
161
162 struct pmd_process_private {
163         int rxq_xsk_fds[RTE_MAX_QUEUES_PER_PORT];
164 };
165
166 #define ETH_AF_XDP_IFACE_ARG                    "iface"
167 #define ETH_AF_XDP_START_QUEUE_ARG              "start_queue"
168 #define ETH_AF_XDP_QUEUE_COUNT_ARG              "queue_count"
169 #define ETH_AF_XDP_SHARED_UMEM_ARG              "shared_umem"
170 #define ETH_AF_XDP_PROG_ARG                     "xdp_prog"
171 #define ETH_AF_XDP_BUDGET_ARG                   "busy_budget"
172
173 static const char * const valid_arguments[] = {
174         ETH_AF_XDP_IFACE_ARG,
175         ETH_AF_XDP_START_QUEUE_ARG,
176         ETH_AF_XDP_QUEUE_COUNT_ARG,
177         ETH_AF_XDP_SHARED_UMEM_ARG,
178         ETH_AF_XDP_PROG_ARG,
179         ETH_AF_XDP_BUDGET_ARG,
180         NULL
181 };
182
183 static const struct rte_eth_link pmd_link = {
184         .link_speed = RTE_ETH_SPEED_NUM_10G,
185         .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
186         .link_status = RTE_ETH_LINK_DOWN,
187         .link_autoneg = RTE_ETH_LINK_AUTONEG
188 };
189
190 /* List which tracks PMDs to facilitate sharing UMEMs across them. */
191 struct internal_list {
192         TAILQ_ENTRY(internal_list) next;
193         struct rte_eth_dev *eth_dev;
194 };
195
196 TAILQ_HEAD(internal_list_head, internal_list);
197 static struct internal_list_head internal_list =
198         TAILQ_HEAD_INITIALIZER(internal_list);
199
200 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
201
202 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
203 static inline int
204 reserve_fill_queue_zc(struct xsk_umem_info *umem, uint16_t reserve_size,
205                       struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
206 {
207         uint32_t idx;
208         uint16_t i;
209
210         if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
211                 for (i = 0; i < reserve_size; i++)
212                         rte_pktmbuf_free(bufs[i]);
213                 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
214                 return -1;
215         }
216
217         for (i = 0; i < reserve_size; i++) {
218                 __u64 *fq_addr;
219                 uint64_t addr;
220
221                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
222                 addr = (uint64_t)bufs[i] - (uint64_t)umem->buffer -
223                                 umem->mb_pool->header_size;
224                 *fq_addr = addr;
225         }
226
227         xsk_ring_prod__submit(fq, reserve_size);
228
229         return 0;
230 }
231 #else
232 static inline int
233 reserve_fill_queue_cp(struct xsk_umem_info *umem, uint16_t reserve_size,
234                       struct rte_mbuf **bufs __rte_unused,
235                       struct xsk_ring_prod *fq)
236 {
237         void *addrs[reserve_size];
238         uint32_t idx;
239         uint16_t i;
240
241         if (rte_ring_dequeue_bulk(umem->buf_ring, addrs, reserve_size, NULL)
242                     != reserve_size) {
243                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
244                 return -1;
245         }
246
247         if (unlikely(!xsk_ring_prod__reserve(fq, reserve_size, &idx))) {
248                 AF_XDP_LOG(DEBUG, "Failed to reserve enough fq descs.\n");
249                 rte_ring_enqueue_bulk(umem->buf_ring, addrs,
250                                 reserve_size, NULL);
251                 return -1;
252         }
253
254         for (i = 0; i < reserve_size; i++) {
255                 __u64 *fq_addr;
256
257                 fq_addr = xsk_ring_prod__fill_addr(fq, idx++);
258                 *fq_addr = (uint64_t)addrs[i];
259         }
260
261         xsk_ring_prod__submit(fq, reserve_size);
262
263         return 0;
264 }
265 #endif
266
267 static inline int
268 reserve_fill_queue(struct xsk_umem_info *umem, uint16_t reserve_size,
269                    struct rte_mbuf **bufs, struct xsk_ring_prod *fq)
270 {
271 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
272         return reserve_fill_queue_zc(umem, reserve_size, bufs, fq);
273 #else
274         return reserve_fill_queue_cp(umem, reserve_size, bufs, fq);
275 #endif
276 }
277
278 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
279 static uint16_t
280 af_xdp_rx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
281 {
282         struct pkt_rx_queue *rxq = queue;
283         struct xsk_ring_cons *rx = &rxq->rx;
284         struct xsk_ring_prod *fq = &rxq->fq;
285         struct xsk_umem_info *umem = rxq->umem;
286         uint32_t idx_rx = 0;
287         unsigned long rx_bytes = 0;
288         int i;
289         struct rte_mbuf *fq_bufs[ETH_AF_XDP_RX_BATCH_SIZE];
290
291         nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
292
293         if (nb_pkts == 0) {
294                 /* we can assume a kernel >= 5.11 is in use if busy polling is
295                  * enabled and thus we can safely use the recvfrom() syscall
296                  * which is only supported for AF_XDP sockets in kernels >=
297                  * 5.11.
298                  */
299                 if (rxq->busy_budget) {
300                         (void)recvfrom(xsk_socket__fd(rxq->xsk), NULL, 0,
301                                        MSG_DONTWAIT, NULL, NULL);
302                 } else if (xsk_ring_prod__needs_wakeup(fq)) {
303                         (void)poll(&rxq->fds[0], 1, 1000);
304                 }
305
306                 return 0;
307         }
308
309         /* allocate bufs for fill queue replenishment after rx */
310         if (rte_pktmbuf_alloc_bulk(umem->mb_pool, fq_bufs, nb_pkts)) {
311                 AF_XDP_LOG(DEBUG,
312                         "Failed to get enough buffers for fq.\n");
313                 /* rollback cached_cons which is added by
314                  * xsk_ring_cons__peek
315                  */
316                 rx->cached_cons -= nb_pkts;
317                 return 0;
318         }
319
320         for (i = 0; i < nb_pkts; i++) {
321                 const struct xdp_desc *desc;
322                 uint64_t addr;
323                 uint32_t len;
324                 uint64_t offset;
325
326                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
327                 addr = desc->addr;
328                 len = desc->len;
329
330                 offset = xsk_umem__extract_offset(addr);
331                 addr = xsk_umem__extract_addr(addr);
332
333                 bufs[i] = (struct rte_mbuf *)
334                                 xsk_umem__get_data(umem->buffer, addr +
335                                         umem->mb_pool->header_size);
336                 bufs[i]->data_off = offset - sizeof(struct rte_mbuf) -
337                         rte_pktmbuf_priv_size(umem->mb_pool) -
338                         umem->mb_pool->header_size;
339
340                 rte_pktmbuf_pkt_len(bufs[i]) = len;
341                 rte_pktmbuf_data_len(bufs[i]) = len;
342                 rx_bytes += len;
343         }
344
345         xsk_ring_cons__release(rx, nb_pkts);
346         (void)reserve_fill_queue(umem, nb_pkts, fq_bufs, fq);
347
348         /* statistics */
349         rxq->stats.rx_pkts += nb_pkts;
350         rxq->stats.rx_bytes += rx_bytes;
351
352         return nb_pkts;
353 }
354 #else
355 static uint16_t
356 af_xdp_rx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
357 {
358         struct pkt_rx_queue *rxq = queue;
359         struct xsk_ring_cons *rx = &rxq->rx;
360         struct xsk_umem_info *umem = rxq->umem;
361         struct xsk_ring_prod *fq = &rxq->fq;
362         uint32_t idx_rx = 0;
363         unsigned long rx_bytes = 0;
364         int i;
365         uint32_t free_thresh = fq->size >> 1;
366         struct rte_mbuf *mbufs[ETH_AF_XDP_RX_BATCH_SIZE];
367
368         if (xsk_prod_nb_free(fq, free_thresh) >= free_thresh)
369                 (void)reserve_fill_queue(umem, nb_pkts, NULL, fq);
370
371         nb_pkts = xsk_ring_cons__peek(rx, nb_pkts, &idx_rx);
372         if (nb_pkts == 0) {
373 #if defined(XDP_USE_NEED_WAKEUP)
374                 if (xsk_ring_prod__needs_wakeup(fq))
375                         (void)poll(rxq->fds, 1, 1000);
376 #endif
377                 return 0;
378         }
379
380         if (unlikely(rte_pktmbuf_alloc_bulk(rxq->mb_pool, mbufs, nb_pkts))) {
381                 /* rollback cached_cons which is added by
382                  * xsk_ring_cons__peek
383                  */
384                 rx->cached_cons -= nb_pkts;
385                 return 0;
386         }
387
388         for (i = 0; i < nb_pkts; i++) {
389                 const struct xdp_desc *desc;
390                 uint64_t addr;
391                 uint32_t len;
392                 void *pkt;
393
394                 desc = xsk_ring_cons__rx_desc(rx, idx_rx++);
395                 addr = desc->addr;
396                 len = desc->len;
397                 pkt = xsk_umem__get_data(rxq->umem->mz->addr, addr);
398
399                 rte_memcpy(rte_pktmbuf_mtod(mbufs[i], void *), pkt, len);
400                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
401                 rte_pktmbuf_pkt_len(mbufs[i]) = len;
402                 rte_pktmbuf_data_len(mbufs[i]) = len;
403                 rx_bytes += len;
404                 bufs[i] = mbufs[i];
405         }
406
407         xsk_ring_cons__release(rx, nb_pkts);
408
409         /* statistics */
410         rxq->stats.rx_pkts += nb_pkts;
411         rxq->stats.rx_bytes += rx_bytes;
412
413         return nb_pkts;
414 }
415 #endif
416
417 static uint16_t
418 af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
419 {
420 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
421         return af_xdp_rx_zc(queue, bufs, nb_pkts);
422 #else
423         return af_xdp_rx_cp(queue, bufs, nb_pkts);
424 #endif
425 }
426
427 static uint16_t
428 eth_af_xdp_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
429 {
430         uint16_t nb_rx;
431
432         if (likely(nb_pkts <= ETH_AF_XDP_RX_BATCH_SIZE))
433                 return af_xdp_rx(queue, bufs, nb_pkts);
434
435         /* Split larger batch into smaller batches of size
436          * ETH_AF_XDP_RX_BATCH_SIZE or less.
437          */
438         nb_rx = 0;
439         while (nb_pkts) {
440                 uint16_t ret, n;
441
442                 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_RX_BATCH_SIZE);
443                 ret = af_xdp_rx(queue, &bufs[nb_rx], n);
444                 nb_rx = (uint16_t)(nb_rx + ret);
445                 nb_pkts = (uint16_t)(nb_pkts - ret);
446                 if (ret < n)
447                         break;
448         }
449
450         return nb_rx;
451 }
452
453 static void
454 pull_umem_cq(struct xsk_umem_info *umem, int size, struct xsk_ring_cons *cq)
455 {
456         size_t i, n;
457         uint32_t idx_cq = 0;
458
459         n = xsk_ring_cons__peek(cq, size, &idx_cq);
460
461         for (i = 0; i < n; i++) {
462                 uint64_t addr;
463                 addr = *xsk_ring_cons__comp_addr(cq, idx_cq++);
464 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
465                 addr = xsk_umem__extract_addr(addr);
466                 rte_pktmbuf_free((struct rte_mbuf *)
467                                         xsk_umem__get_data(umem->buffer,
468                                         addr + umem->mb_pool->header_size));
469 #else
470                 rte_ring_enqueue(umem->buf_ring, (void *)addr);
471 #endif
472         }
473
474         xsk_ring_cons__release(cq, n);
475 }
476
477 static void
478 kick_tx(struct pkt_tx_queue *txq, struct xsk_ring_cons *cq)
479 {
480         struct xsk_umem_info *umem = txq->umem;
481
482         pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
483
484         if (tx_syscall_needed(&txq->tx))
485                 while (send(xsk_socket__fd(txq->pair->xsk), NULL,
486                             0, MSG_DONTWAIT) < 0) {
487                         /* some thing unexpected */
488                         if (errno != EBUSY && errno != EAGAIN && errno != EINTR)
489                                 break;
490
491                         /* pull from completion queue to leave more space */
492                         if (errno == EAGAIN)
493                                 pull_umem_cq(umem,
494                                              XSK_RING_CONS__DEFAULT_NUM_DESCS,
495                                              cq);
496                 }
497 }
498
499 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
500 static uint16_t
501 af_xdp_tx_zc(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
502 {
503         struct pkt_tx_queue *txq = queue;
504         struct xsk_umem_info *umem = txq->umem;
505         struct rte_mbuf *mbuf;
506         unsigned long tx_bytes = 0;
507         int i;
508         uint32_t idx_tx;
509         uint16_t count = 0;
510         struct xdp_desc *desc;
511         uint64_t addr, offset;
512         struct xsk_ring_cons *cq = &txq->pair->cq;
513         uint32_t free_thresh = cq->size >> 1;
514
515         if (xsk_cons_nb_avail(cq, free_thresh) >= free_thresh)
516                 pull_umem_cq(umem, XSK_RING_CONS__DEFAULT_NUM_DESCS, cq);
517
518         for (i = 0; i < nb_pkts; i++) {
519                 mbuf = bufs[i];
520
521                 if (mbuf->pool == umem->mb_pool) {
522                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
523                                 kick_tx(txq, cq);
524                                 if (!xsk_ring_prod__reserve(&txq->tx, 1,
525                                                             &idx_tx))
526                                         goto out;
527                         }
528                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
529                         desc->len = mbuf->pkt_len;
530                         addr = (uint64_t)mbuf - (uint64_t)umem->buffer -
531                                         umem->mb_pool->header_size;
532                         offset = rte_pktmbuf_mtod(mbuf, uint64_t) -
533                                         (uint64_t)mbuf +
534                                         umem->mb_pool->header_size;
535                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
536                         desc->addr = addr | offset;
537                         count++;
538                 } else {
539                         struct rte_mbuf *local_mbuf =
540                                         rte_pktmbuf_alloc(umem->mb_pool);
541                         void *pkt;
542
543                         if (local_mbuf == NULL)
544                                 goto out;
545
546                         if (!xsk_ring_prod__reserve(&txq->tx, 1, &idx_tx)) {
547                                 rte_pktmbuf_free(local_mbuf);
548                                 goto out;
549                         }
550
551                         desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx);
552                         desc->len = mbuf->pkt_len;
553
554                         addr = (uint64_t)local_mbuf - (uint64_t)umem->buffer -
555                                         umem->mb_pool->header_size;
556                         offset = rte_pktmbuf_mtod(local_mbuf, uint64_t) -
557                                         (uint64_t)local_mbuf +
558                                         umem->mb_pool->header_size;
559                         pkt = xsk_umem__get_data(umem->buffer, addr + offset);
560                         offset = offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT;
561                         desc->addr = addr | offset;
562                         rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *),
563                                         desc->len);
564                         rte_pktmbuf_free(mbuf);
565                         count++;
566                 }
567
568                 tx_bytes += mbuf->pkt_len;
569         }
570
571 out:
572         xsk_ring_prod__submit(&txq->tx, count);
573         kick_tx(txq, cq);
574
575         txq->stats.tx_pkts += count;
576         txq->stats.tx_bytes += tx_bytes;
577         txq->stats.tx_dropped += nb_pkts - count;
578
579         return count;
580 }
581 #else
582 static uint16_t
583 af_xdp_tx_cp(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
584 {
585         struct pkt_tx_queue *txq = queue;
586         struct xsk_umem_info *umem = txq->umem;
587         struct rte_mbuf *mbuf;
588         void *addrs[ETH_AF_XDP_TX_BATCH_SIZE];
589         unsigned long tx_bytes = 0;
590         int i;
591         uint32_t idx_tx;
592         struct xsk_ring_cons *cq = &txq->pair->cq;
593
594         pull_umem_cq(umem, nb_pkts, cq);
595
596         nb_pkts = rte_ring_dequeue_bulk(umem->buf_ring, addrs,
597                                         nb_pkts, NULL);
598         if (nb_pkts == 0)
599                 return 0;
600
601         if (xsk_ring_prod__reserve(&txq->tx, nb_pkts, &idx_tx) != nb_pkts) {
602                 kick_tx(txq, cq);
603                 rte_ring_enqueue_bulk(umem->buf_ring, addrs, nb_pkts, NULL);
604                 return 0;
605         }
606
607         for (i = 0; i < nb_pkts; i++) {
608                 struct xdp_desc *desc;
609                 void *pkt;
610
611                 desc = xsk_ring_prod__tx_desc(&txq->tx, idx_tx + i);
612                 mbuf = bufs[i];
613                 desc->len = mbuf->pkt_len;
614
615                 desc->addr = (uint64_t)addrs[i];
616                 pkt = xsk_umem__get_data(umem->mz->addr,
617                                          desc->addr);
618                 rte_memcpy(pkt, rte_pktmbuf_mtod(mbuf, void *), desc->len);
619                 tx_bytes += mbuf->pkt_len;
620                 rte_pktmbuf_free(mbuf);
621         }
622
623         xsk_ring_prod__submit(&txq->tx, nb_pkts);
624
625         kick_tx(txq, cq);
626
627         txq->stats.tx_pkts += nb_pkts;
628         txq->stats.tx_bytes += tx_bytes;
629
630         return nb_pkts;
631 }
632
633 static uint16_t
634 af_xdp_tx_cp_batch(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
635 {
636         uint16_t nb_tx;
637
638         if (likely(nb_pkts <= ETH_AF_XDP_TX_BATCH_SIZE))
639                 return af_xdp_tx_cp(queue, bufs, nb_pkts);
640
641         nb_tx = 0;
642         while (nb_pkts) {
643                 uint16_t ret, n;
644
645                 /* Split larger batch into smaller batches of size
646                  * ETH_AF_XDP_TX_BATCH_SIZE or less.
647                  */
648                 n = (uint16_t)RTE_MIN(nb_pkts, ETH_AF_XDP_TX_BATCH_SIZE);
649                 ret = af_xdp_tx_cp(queue, &bufs[nb_tx], n);
650                 nb_tx = (uint16_t)(nb_tx + ret);
651                 nb_pkts = (uint16_t)(nb_pkts - ret);
652                 if (ret < n)
653                         break;
654         }
655
656         return nb_tx;
657 }
658 #endif
659
660 static uint16_t
661 eth_af_xdp_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
662 {
663 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
664         return af_xdp_tx_zc(queue, bufs, nb_pkts);
665 #else
666         return af_xdp_tx_cp_batch(queue, bufs, nb_pkts);
667 #endif
668 }
669
670 static int
671 eth_dev_start(struct rte_eth_dev *dev)
672 {
673         dev->data->dev_link.link_status = RTE_ETH_LINK_UP;
674
675         return 0;
676 }
677
678 /* This function gets called when the current port gets stopped. */
679 static int
680 eth_dev_stop(struct rte_eth_dev *dev)
681 {
682         dev->data->dev_link.link_status = RTE_ETH_LINK_DOWN;
683         return 0;
684 }
685
686 /* Find ethdev in list */
687 static inline struct internal_list *
688 find_internal_resource(struct pmd_internals *port_int)
689 {
690         int found = 0;
691         struct internal_list *list = NULL;
692
693         if (port_int == NULL)
694                 return NULL;
695
696         pthread_mutex_lock(&internal_list_lock);
697
698         TAILQ_FOREACH(list, &internal_list, next) {
699                 struct pmd_internals *list_int =
700                                 list->eth_dev->data->dev_private;
701                 if (list_int == port_int) {
702                         found = 1;
703                         break;
704                 }
705         }
706
707         pthread_mutex_unlock(&internal_list_lock);
708
709         if (!found)
710                 return NULL;
711
712         return list;
713 }
714
715 static int
716 eth_dev_configure(struct rte_eth_dev *dev)
717 {
718         struct pmd_internals *internal = dev->data->dev_private;
719
720         /* rx/tx must be paired */
721         if (dev->data->nb_rx_queues != dev->data->nb_tx_queues)
722                 return -EINVAL;
723
724         if (internal->shared_umem) {
725                 struct internal_list *list = NULL;
726                 const char *name = dev->device->name;
727
728                 /* Ensure PMD is not already inserted into the list */
729                 list = find_internal_resource(internal);
730                 if (list)
731                         return 0;
732
733                 list = rte_zmalloc_socket(name, sizeof(*list), 0,
734                                         dev->device->numa_node);
735                 if (list == NULL)
736                         return -1;
737
738                 list->eth_dev = dev;
739                 pthread_mutex_lock(&internal_list_lock);
740                 TAILQ_INSERT_TAIL(&internal_list, list, next);
741                 pthread_mutex_unlock(&internal_list_lock);
742         }
743
744         return 0;
745 }
746
747 #define CLB_VAL_IDX 0
748 static int
749 eth_monitor_callback(const uint64_t value,
750                 const uint64_t opaque[RTE_POWER_MONITOR_OPAQUE_SZ])
751 {
752         const uint64_t v = opaque[CLB_VAL_IDX];
753         const uint64_t m = (uint32_t)~0;
754
755         /* if the value has changed, abort entering power optimized state */
756         return (value & m) == v ? 0 : -1;
757 }
758
759 static int
760 eth_get_monitor_addr(void *rx_queue, struct rte_power_monitor_cond *pmc)
761 {
762         struct pkt_rx_queue *rxq = rx_queue;
763         unsigned int *prod = rxq->rx.producer;
764         const uint32_t cur_val = rxq->rx.cached_prod; /* use cached value */
765
766         /* watch for changes in producer ring */
767         pmc->addr = (void *)prod;
768
769         /* store current value */
770         pmc->opaque[CLB_VAL_IDX] = cur_val;
771         pmc->fn = eth_monitor_callback;
772
773         /* AF_XDP producer ring index is 32-bit */
774         pmc->size = sizeof(uint32_t);
775
776         return 0;
777 }
778
779 static int
780 eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
781 {
782         struct pmd_internals *internals = dev->data->dev_private;
783
784         dev_info->if_index = internals->if_index;
785         dev_info->max_mac_addrs = 1;
786         dev_info->max_rx_queues = internals->queue_cnt;
787         dev_info->max_tx_queues = internals->queue_cnt;
788
789         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
790 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
791         dev_info->max_rx_pktlen = getpagesize() -
792                                   sizeof(struct rte_mempool_objhdr) -
793                                   sizeof(struct rte_mbuf) -
794                                   RTE_PKTMBUF_HEADROOM - XDP_PACKET_HEADROOM;
795 #else
796         dev_info->max_rx_pktlen = ETH_AF_XDP_FRAME_SIZE - XDP_PACKET_HEADROOM;
797 #endif
798         dev_info->max_mtu = dev_info->max_rx_pktlen - ETH_AF_XDP_ETH_OVERHEAD;
799
800         dev_info->default_rxportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
801         dev_info->default_txportconf.burst_size = ETH_AF_XDP_DFLT_BUSY_BUDGET;
802         dev_info->default_rxportconf.nb_queues = 1;
803         dev_info->default_txportconf.nb_queues = 1;
804         dev_info->default_rxportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
805         dev_info->default_txportconf.ring_size = ETH_AF_XDP_DFLT_NUM_DESCS;
806
807         return 0;
808 }
809
810 static int
811 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
812 {
813         struct pmd_internals *internals = dev->data->dev_private;
814         struct pmd_process_private *process_private = dev->process_private;
815         struct xdp_statistics xdp_stats;
816         struct pkt_rx_queue *rxq;
817         struct pkt_tx_queue *txq;
818         socklen_t optlen;
819         int i, ret, fd;
820
821         for (i = 0; i < dev->data->nb_rx_queues; i++) {
822                 optlen = sizeof(struct xdp_statistics);
823                 rxq = &internals->rx_queues[i];
824                 txq = rxq->pair;
825                 stats->q_ipackets[i] = rxq->stats.rx_pkts;
826                 stats->q_ibytes[i] = rxq->stats.rx_bytes;
827
828                 stats->q_opackets[i] = txq->stats.tx_pkts;
829                 stats->q_obytes[i] = txq->stats.tx_bytes;
830
831                 stats->ipackets += stats->q_ipackets[i];
832                 stats->ibytes += stats->q_ibytes[i];
833                 stats->imissed += rxq->stats.rx_dropped;
834                 stats->oerrors += txq->stats.tx_dropped;
835                 fd = process_private->rxq_xsk_fds[i];
836                 ret = fd >= 0 ? getsockopt(fd, SOL_XDP, XDP_STATISTICS,
837                                            &xdp_stats, &optlen) : -1;
838                 if (ret != 0) {
839                         AF_XDP_LOG(ERR, "getsockopt() failed for XDP_STATISTICS.\n");
840                         return -1;
841                 }
842                 stats->imissed += xdp_stats.rx_dropped;
843
844                 stats->opackets += stats->q_opackets[i];
845                 stats->obytes += stats->q_obytes[i];
846         }
847
848         return 0;
849 }
850
851 static int
852 eth_stats_reset(struct rte_eth_dev *dev)
853 {
854         struct pmd_internals *internals = dev->data->dev_private;
855         int i;
856
857         for (i = 0; i < internals->queue_cnt; i++) {
858                 memset(&internals->rx_queues[i].stats, 0,
859                                         sizeof(struct rx_stats));
860                 memset(&internals->tx_queues[i].stats, 0,
861                                         sizeof(struct tx_stats));
862         }
863
864         return 0;
865 }
866
867 static void
868 remove_xdp_program(struct pmd_internals *internals)
869 {
870         uint32_t curr_prog_id = 0;
871
872         if (bpf_get_link_xdp_id(internals->if_index, &curr_prog_id,
873                                 XDP_FLAGS_UPDATE_IF_NOEXIST)) {
874                 AF_XDP_LOG(ERR, "bpf_get_link_xdp_id failed\n");
875                 return;
876         }
877         bpf_set_link_xdp_fd(internals->if_index, -1,
878                         XDP_FLAGS_UPDATE_IF_NOEXIST);
879 }
880
881 static void
882 xdp_umem_destroy(struct xsk_umem_info *umem)
883 {
884 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
885         umem->mb_pool = NULL;
886 #else
887         rte_memzone_free(umem->mz);
888         umem->mz = NULL;
889
890         rte_ring_free(umem->buf_ring);
891         umem->buf_ring = NULL;
892 #endif
893
894         rte_free(umem);
895 }
896
897 static int
898 eth_dev_close(struct rte_eth_dev *dev)
899 {
900         struct pmd_internals *internals = dev->data->dev_private;
901         struct pkt_rx_queue *rxq;
902         int i;
903
904         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
905                 goto out;
906
907         AF_XDP_LOG(INFO, "Closing AF_XDP ethdev on numa socket %u\n",
908                 rte_socket_id());
909
910         for (i = 0; i < internals->queue_cnt; i++) {
911                 rxq = &internals->rx_queues[i];
912                 if (rxq->umem == NULL)
913                         break;
914                 xsk_socket__delete(rxq->xsk);
915
916                 if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE)
917                                 == 0) {
918                         (void)xsk_umem__delete(rxq->umem->umem);
919                         xdp_umem_destroy(rxq->umem);
920                 }
921
922                 /* free pkt_tx_queue */
923                 rte_free(rxq->pair);
924                 rte_free(rxq);
925         }
926
927         /*
928          * MAC is not allocated dynamically, setting it to NULL would prevent
929          * from releasing it in rte_eth_dev_release_port.
930          */
931         dev->data->mac_addrs = NULL;
932
933         remove_xdp_program(internals);
934
935         if (internals->shared_umem) {
936                 struct internal_list *list;
937
938                 /* Remove ethdev from list used to track and share UMEMs */
939                 list = find_internal_resource(internals);
940                 if (list) {
941                         pthread_mutex_lock(&internal_list_lock);
942                         TAILQ_REMOVE(&internal_list, list, next);
943                         pthread_mutex_unlock(&internal_list_lock);
944                         rte_free(list);
945                 }
946         }
947
948 out:
949         rte_free(dev->process_private);
950
951         return 0;
952 }
953
954 static int
955 eth_link_update(struct rte_eth_dev *dev __rte_unused,
956                 int wait_to_complete __rte_unused)
957 {
958         return 0;
959 }
960
961 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
962 static inline uintptr_t get_base_addr(struct rte_mempool *mp, uint64_t *align)
963 {
964         struct rte_mempool_memhdr *memhdr;
965         uintptr_t memhdr_addr, aligned_addr;
966
967         memhdr = STAILQ_FIRST(&mp->mem_list);
968         memhdr_addr = (uintptr_t)memhdr->addr;
969         aligned_addr = memhdr_addr & ~(getpagesize() - 1);
970         *align = memhdr_addr - aligned_addr;
971
972         return aligned_addr;
973 }
974
975 /* Check if the netdev,qid context already exists */
976 static inline bool
977 ctx_exists(struct pkt_rx_queue *rxq, const char *ifname,
978                 struct pkt_rx_queue *list_rxq, const char *list_ifname)
979 {
980         bool exists = false;
981
982         if (rxq->xsk_queue_idx == list_rxq->xsk_queue_idx &&
983                         !strncmp(ifname, list_ifname, IFNAMSIZ)) {
984                 AF_XDP_LOG(ERR, "ctx %s,%i already exists, cannot share umem\n",
985                                         ifname, rxq->xsk_queue_idx);
986                 exists = true;
987         }
988
989         return exists;
990 }
991
992 /* Get a pointer to an existing UMEM which overlays the rxq's mb_pool */
993 static inline int
994 get_shared_umem(struct pkt_rx_queue *rxq, const char *ifname,
995                         struct xsk_umem_info **umem)
996 {
997         struct internal_list *list;
998         struct pmd_internals *internals;
999         int i = 0, ret = 0;
1000         struct rte_mempool *mb_pool = rxq->mb_pool;
1001
1002         if (mb_pool == NULL)
1003                 return ret;
1004
1005         pthread_mutex_lock(&internal_list_lock);
1006
1007         TAILQ_FOREACH(list, &internal_list, next) {
1008                 internals = list->eth_dev->data->dev_private;
1009                 for (i = 0; i < internals->queue_cnt; i++) {
1010                         struct pkt_rx_queue *list_rxq =
1011                                                 &internals->rx_queues[i];
1012                         if (rxq == list_rxq)
1013                                 continue;
1014                         if (mb_pool == internals->rx_queues[i].mb_pool) {
1015                                 if (ctx_exists(rxq, ifname, list_rxq,
1016                                                 internals->if_name)) {
1017                                         ret = -1;
1018                                         goto out;
1019                                 }
1020                                 if (__atomic_load_n(&internals->rx_queues[i].umem->refcnt,
1021                                                     __ATOMIC_ACQUIRE)) {
1022                                         *umem = internals->rx_queues[i].umem;
1023                                         goto out;
1024                                 }
1025                         }
1026                 }
1027         }
1028
1029 out:
1030         pthread_mutex_unlock(&internal_list_lock);
1031
1032         return ret;
1033 }
1034
1035 static struct
1036 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1037                                   struct pkt_rx_queue *rxq)
1038 {
1039         struct xsk_umem_info *umem = NULL;
1040         int ret;
1041         struct xsk_umem_config usr_config = {
1042                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS * 2,
1043                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1044                 .flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG};
1045         void *base_addr = NULL;
1046         struct rte_mempool *mb_pool = rxq->mb_pool;
1047         uint64_t umem_size, align = 0;
1048
1049         if (internals->shared_umem) {
1050                 if (get_shared_umem(rxq, internals->if_name, &umem) < 0)
1051                         return NULL;
1052
1053                 if (umem != NULL &&
1054                         __atomic_load_n(&umem->refcnt, __ATOMIC_ACQUIRE) <
1055                                         umem->max_xsks) {
1056                         AF_XDP_LOG(INFO, "%s,qid%i sharing UMEM\n",
1057                                         internals->if_name, rxq->xsk_queue_idx);
1058                         __atomic_fetch_add(&umem->refcnt, 1, __ATOMIC_ACQUIRE);
1059                 }
1060         }
1061
1062         if (umem == NULL) {
1063                 usr_config.frame_size =
1064                         rte_mempool_calc_obj_size(mb_pool->elt_size,
1065                                                   mb_pool->flags, NULL);
1066                 usr_config.frame_headroom = mb_pool->header_size +
1067                                                 sizeof(struct rte_mbuf) +
1068                                                 rte_pktmbuf_priv_size(mb_pool) +
1069                                                 RTE_PKTMBUF_HEADROOM;
1070
1071                 umem = rte_zmalloc_socket("umem", sizeof(*umem), 0,
1072                                           rte_socket_id());
1073                 if (umem == NULL) {
1074                         AF_XDP_LOG(ERR, "Failed to allocate umem info");
1075                         return NULL;
1076                 }
1077
1078                 umem->mb_pool = mb_pool;
1079                 base_addr = (void *)get_base_addr(mb_pool, &align);
1080                 umem_size = (uint64_t)mb_pool->populated_size *
1081                                 (uint64_t)usr_config.frame_size +
1082                                 align;
1083
1084                 ret = xsk_umem__create(&umem->umem, base_addr, umem_size,
1085                                 &rxq->fq, &rxq->cq, &usr_config);
1086                 if (ret) {
1087                         AF_XDP_LOG(ERR, "Failed to create umem");
1088                         goto err;
1089                 }
1090                 umem->buffer = base_addr;
1091
1092                 if (internals->shared_umem) {
1093                         umem->max_xsks = mb_pool->populated_size /
1094                                                 ETH_AF_XDP_NUM_BUFFERS;
1095                         AF_XDP_LOG(INFO, "Max xsks for UMEM %s: %u\n",
1096                                                 mb_pool->name, umem->max_xsks);
1097                 }
1098
1099                 __atomic_store_n(&umem->refcnt, 1, __ATOMIC_RELEASE);
1100         }
1101
1102         return umem;
1103
1104 err:
1105         xdp_umem_destroy(umem);
1106         return NULL;
1107 }
1108 #else
1109 static struct
1110 xsk_umem_info *xdp_umem_configure(struct pmd_internals *internals,
1111                                   struct pkt_rx_queue *rxq)
1112 {
1113         struct xsk_umem_info *umem;
1114         const struct rte_memzone *mz;
1115         struct xsk_umem_config usr_config = {
1116                 .fill_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1117                 .comp_size = ETH_AF_XDP_DFLT_NUM_DESCS,
1118                 .frame_size = ETH_AF_XDP_FRAME_SIZE,
1119                 .frame_headroom = 0 };
1120         char ring_name[RTE_RING_NAMESIZE];
1121         char mz_name[RTE_MEMZONE_NAMESIZE];
1122         int ret;
1123         uint64_t i;
1124
1125         umem = rte_zmalloc_socket("umem", sizeof(*umem), 0, rte_socket_id());
1126         if (umem == NULL) {
1127                 AF_XDP_LOG(ERR, "Failed to allocate umem info");
1128                 return NULL;
1129         }
1130
1131         snprintf(ring_name, sizeof(ring_name), "af_xdp_ring_%s_%u",
1132                        internals->if_name, rxq->xsk_queue_idx);
1133         umem->buf_ring = rte_ring_create(ring_name,
1134                                          ETH_AF_XDP_NUM_BUFFERS,
1135                                          rte_socket_id(),
1136                                          0x0);
1137         if (umem->buf_ring == NULL) {
1138                 AF_XDP_LOG(ERR, "Failed to create rte_ring\n");
1139                 goto err;
1140         }
1141
1142         for (i = 0; i < ETH_AF_XDP_NUM_BUFFERS; i++)
1143                 rte_ring_enqueue(umem->buf_ring,
1144                                  (void *)(i * ETH_AF_XDP_FRAME_SIZE));
1145
1146         snprintf(mz_name, sizeof(mz_name), "af_xdp_umem_%s_%u",
1147                        internals->if_name, rxq->xsk_queue_idx);
1148         mz = rte_memzone_reserve_aligned(mz_name,
1149                         ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1150                         rte_socket_id(), RTE_MEMZONE_IOVA_CONTIG,
1151                         getpagesize());
1152         if (mz == NULL) {
1153                 AF_XDP_LOG(ERR, "Failed to reserve memzone for af_xdp umem.\n");
1154                 goto err;
1155         }
1156
1157         ret = xsk_umem__create(&umem->umem, mz->addr,
1158                                ETH_AF_XDP_NUM_BUFFERS * ETH_AF_XDP_FRAME_SIZE,
1159                                &rxq->fq, &rxq->cq,
1160                                &usr_config);
1161
1162         if (ret) {
1163                 AF_XDP_LOG(ERR, "Failed to create umem");
1164                 goto err;
1165         }
1166         umem->mz = mz;
1167
1168         return umem;
1169
1170 err:
1171         xdp_umem_destroy(umem);
1172         return NULL;
1173 }
1174 #endif
1175
1176 static int
1177 load_custom_xdp_prog(const char *prog_path, int if_index, struct bpf_map **map)
1178 {
1179         int ret, prog_fd = -1;
1180         struct bpf_object *obj;
1181
1182         ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
1183         if (ret) {
1184                 AF_XDP_LOG(ERR, "Failed to load program %s\n", prog_path);
1185                 return ret;
1186         }
1187
1188         /*
1189          * The loaded program must provision for a map of xsks, such that some
1190          * traffic can be redirected to userspace.
1191          */
1192         *map = bpf_object__find_map_by_name(obj, "xsks_map");
1193         if (!*map) {
1194                 AF_XDP_LOG(ERR, "Failed to find xsks_map in %s\n", prog_path);
1195                 return -1;
1196         }
1197
1198         /* Link the program with the given network device */
1199         ret = bpf_set_link_xdp_fd(if_index, prog_fd,
1200                                         XDP_FLAGS_UPDATE_IF_NOEXIST);
1201         if (ret) {
1202                 AF_XDP_LOG(ERR, "Failed to set prog fd %d on interface\n",
1203                                 prog_fd);
1204                 return -1;
1205         }
1206
1207         AF_XDP_LOG(INFO, "Successfully loaded XDP program %s with fd %d\n",
1208                                 prog_path, prog_fd);
1209
1210         return 0;
1211 }
1212
1213 /* Detect support for busy polling through setsockopt(). */
1214 static int
1215 configure_preferred_busy_poll(struct pkt_rx_queue *rxq)
1216 {
1217         int sock_opt = 1;
1218         int fd = xsk_socket__fd(rxq->xsk);
1219         int ret = 0;
1220
1221         ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1222                         (void *)&sock_opt, sizeof(sock_opt));
1223         if (ret < 0) {
1224                 AF_XDP_LOG(DEBUG, "Failed to set SO_PREFER_BUSY_POLL\n");
1225                 goto err_prefer;
1226         }
1227
1228         sock_opt = ETH_AF_XDP_DFLT_BUSY_TIMEOUT;
1229         ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1230                         sizeof(sock_opt));
1231         if (ret < 0) {
1232                 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL\n");
1233                 goto err_timeout;
1234         }
1235
1236         sock_opt = rxq->busy_budget;
1237         ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL_BUDGET,
1238                         (void *)&sock_opt, sizeof(sock_opt));
1239         if (ret < 0) {
1240                 AF_XDP_LOG(DEBUG, "Failed to set SO_BUSY_POLL_BUDGET\n");
1241         } else {
1242                 AF_XDP_LOG(INFO, "Busy polling budget set to: %u\n",
1243                                         rxq->busy_budget);
1244                 return 0;
1245         }
1246
1247         /* setsockopt failure - attempt to restore xsk to default state and
1248          * proceed without busy polling support.
1249          */
1250         sock_opt = 0;
1251         ret = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, (void *)&sock_opt,
1252                         sizeof(sock_opt));
1253         if (ret < 0) {
1254                 AF_XDP_LOG(ERR, "Failed to unset SO_BUSY_POLL\n");
1255                 return -1;
1256         }
1257
1258 err_timeout:
1259         sock_opt = 0;
1260         ret = setsockopt(fd, SOL_SOCKET, SO_PREFER_BUSY_POLL,
1261                         (void *)&sock_opt, sizeof(sock_opt));
1262         if (ret < 0) {
1263                 AF_XDP_LOG(ERR, "Failed to unset SO_PREFER_BUSY_POLL\n");
1264                 return -1;
1265         }
1266
1267 err_prefer:
1268         rxq->busy_budget = 0;
1269         return 0;
1270 }
1271
1272 static int
1273 xsk_configure(struct pmd_internals *internals, struct pkt_rx_queue *rxq,
1274               int ring_size)
1275 {
1276         struct xsk_socket_config cfg;
1277         struct pkt_tx_queue *txq = rxq->pair;
1278         int ret = 0;
1279         int reserve_size = ETH_AF_XDP_DFLT_NUM_DESCS;
1280         struct rte_mbuf *fq_bufs[reserve_size];
1281
1282         rxq->umem = xdp_umem_configure(internals, rxq);
1283         if (rxq->umem == NULL)
1284                 return -ENOMEM;
1285         txq->umem = rxq->umem;
1286
1287         cfg.rx_size = ring_size;
1288         cfg.tx_size = ring_size;
1289         cfg.libbpf_flags = 0;
1290         cfg.xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
1291         cfg.bind_flags = 0;
1292
1293 #if defined(XDP_USE_NEED_WAKEUP)
1294         cfg.bind_flags |= XDP_USE_NEED_WAKEUP;
1295 #endif
1296
1297         if (strnlen(internals->prog_path, PATH_MAX) &&
1298                                 !internals->custom_prog_configured) {
1299                 ret = load_custom_xdp_prog(internals->prog_path,
1300                                            internals->if_index,
1301                                            &internals->map);
1302                 if (ret) {
1303                         AF_XDP_LOG(ERR, "Failed to load custom XDP program %s\n",
1304                                         internals->prog_path);
1305                         goto err;
1306                 }
1307                 internals->custom_prog_configured = 1;
1308                 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
1309         }
1310
1311         if (internals->shared_umem)
1312                 ret = create_shared_socket(&rxq->xsk, internals->if_name,
1313                                 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1314                                 &txq->tx, &rxq->fq, &rxq->cq, &cfg);
1315         else
1316                 ret = xsk_socket__create(&rxq->xsk, internals->if_name,
1317                                 rxq->xsk_queue_idx, rxq->umem->umem, &rxq->rx,
1318                                 &txq->tx, &cfg);
1319
1320         if (ret) {
1321                 AF_XDP_LOG(ERR, "Failed to create xsk socket.\n");
1322                 goto err;
1323         }
1324
1325         /* insert the xsk into the xsks_map */
1326         if (internals->custom_prog_configured) {
1327                 int err, fd;
1328
1329                 fd = xsk_socket__fd(rxq->xsk);
1330                 err = bpf_map_update_elem(bpf_map__fd(internals->map),
1331                                           &rxq->xsk_queue_idx, &fd, 0);
1332                 if (err) {
1333                         AF_XDP_LOG(ERR, "Failed to insert xsk in map.\n");
1334                         goto err;
1335                 }
1336         }
1337
1338 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1339         ret = rte_pktmbuf_alloc_bulk(rxq->umem->mb_pool, fq_bufs, reserve_size);
1340         if (ret) {
1341                 AF_XDP_LOG(DEBUG, "Failed to get enough buffers for fq.\n");
1342                 goto err;
1343         }
1344 #endif
1345
1346         if (rxq->busy_budget) {
1347                 ret = configure_preferred_busy_poll(rxq);
1348                 if (ret) {
1349                         AF_XDP_LOG(ERR, "Failed configure busy polling.\n");
1350                         goto err;
1351                 }
1352         }
1353
1354         ret = reserve_fill_queue(rxq->umem, reserve_size, fq_bufs, &rxq->fq);
1355         if (ret) {
1356                 xsk_socket__delete(rxq->xsk);
1357                 AF_XDP_LOG(ERR, "Failed to reserve fill queue.\n");
1358                 goto err;
1359         }
1360
1361         return 0;
1362
1363 err:
1364         if (__atomic_sub_fetch(&rxq->umem->refcnt, 1, __ATOMIC_ACQUIRE) == 0)
1365                 xdp_umem_destroy(rxq->umem);
1366
1367         return ret;
1368 }
1369
1370 static int
1371 eth_rx_queue_setup(struct rte_eth_dev *dev,
1372                    uint16_t rx_queue_id,
1373                    uint16_t nb_rx_desc,
1374                    unsigned int socket_id __rte_unused,
1375                    const struct rte_eth_rxconf *rx_conf __rte_unused,
1376                    struct rte_mempool *mb_pool)
1377 {
1378         struct pmd_internals *internals = dev->data->dev_private;
1379         struct pmd_process_private *process_private = dev->process_private;
1380         struct pkt_rx_queue *rxq;
1381         int ret;
1382
1383         rxq = &internals->rx_queues[rx_queue_id];
1384
1385         AF_XDP_LOG(INFO, "Set up rx queue, rx queue id: %d, xsk queue id: %d\n",
1386                    rx_queue_id, rxq->xsk_queue_idx);
1387
1388 #ifndef XDP_UMEM_UNALIGNED_CHUNK_FLAG
1389         uint32_t buf_size, data_size;
1390
1391         /* Now get the space available for data in the mbuf */
1392         buf_size = rte_pktmbuf_data_room_size(mb_pool) -
1393                 RTE_PKTMBUF_HEADROOM;
1394         data_size = ETH_AF_XDP_FRAME_SIZE;
1395
1396         if (data_size > buf_size) {
1397                 AF_XDP_LOG(ERR, "%s: %d bytes will not fit in mbuf (%d bytes)\n",
1398                         dev->device->name, data_size, buf_size);
1399                 ret = -ENOMEM;
1400                 goto err;
1401         }
1402 #endif
1403
1404         rxq->mb_pool = mb_pool;
1405
1406         if (xsk_configure(internals, rxq, nb_rx_desc)) {
1407                 AF_XDP_LOG(ERR, "Failed to configure xdp socket\n");
1408                 ret = -EINVAL;
1409                 goto err;
1410         }
1411
1412         if (!rxq->busy_budget)
1413                 AF_XDP_LOG(DEBUG, "Preferred busy polling not enabled\n");
1414
1415         rxq->fds[0].fd = xsk_socket__fd(rxq->xsk);
1416         rxq->fds[0].events = POLLIN;
1417
1418         process_private->rxq_xsk_fds[rx_queue_id] = rxq->fds[0].fd;
1419
1420         dev->data->rx_queues[rx_queue_id] = rxq;
1421         return 0;
1422
1423 err:
1424         return ret;
1425 }
1426
1427 static int
1428 eth_tx_queue_setup(struct rte_eth_dev *dev,
1429                    uint16_t tx_queue_id,
1430                    uint16_t nb_tx_desc __rte_unused,
1431                    unsigned int socket_id __rte_unused,
1432                    const struct rte_eth_txconf *tx_conf __rte_unused)
1433 {
1434         struct pmd_internals *internals = dev->data->dev_private;
1435         struct pkt_tx_queue *txq;
1436
1437         txq = &internals->tx_queues[tx_queue_id];
1438
1439         dev->data->tx_queues[tx_queue_id] = txq;
1440         return 0;
1441 }
1442
1443 static int
1444 eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1445 {
1446         struct pmd_internals *internals = dev->data->dev_private;
1447         struct ifreq ifr = { .ifr_mtu = mtu };
1448         int ret;
1449         int s;
1450
1451         s = socket(PF_INET, SOCK_DGRAM, 0);
1452         if (s < 0)
1453                 return -EINVAL;
1454
1455         strlcpy(ifr.ifr_name, internals->if_name, IFNAMSIZ);
1456         ret = ioctl(s, SIOCSIFMTU, &ifr);
1457         close(s);
1458
1459         return (ret < 0) ? -errno : 0;
1460 }
1461
1462 static int
1463 eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)
1464 {
1465         struct ifreq ifr;
1466         int ret = 0;
1467         int s;
1468
1469         s = socket(PF_INET, SOCK_DGRAM, 0);
1470         if (s < 0)
1471                 return -errno;
1472
1473         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1474         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
1475                 ret = -errno;
1476                 goto out;
1477         }
1478         ifr.ifr_flags &= mask;
1479         ifr.ifr_flags |= flags;
1480         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
1481                 ret = -errno;
1482                 goto out;
1483         }
1484 out:
1485         close(s);
1486         return ret;
1487 }
1488
1489 static int
1490 eth_dev_promiscuous_enable(struct rte_eth_dev *dev)
1491 {
1492         struct pmd_internals *internals = dev->data->dev_private;
1493
1494         return eth_dev_change_flags(internals->if_name, IFF_PROMISC, ~0);
1495 }
1496
1497 static int
1498 eth_dev_promiscuous_disable(struct rte_eth_dev *dev)
1499 {
1500         struct pmd_internals *internals = dev->data->dev_private;
1501
1502         return eth_dev_change_flags(internals->if_name, 0, ~IFF_PROMISC);
1503 }
1504
1505 static const struct eth_dev_ops ops = {
1506         .dev_start = eth_dev_start,
1507         .dev_stop = eth_dev_stop,
1508         .dev_close = eth_dev_close,
1509         .dev_configure = eth_dev_configure,
1510         .dev_infos_get = eth_dev_info,
1511         .mtu_set = eth_dev_mtu_set,
1512         .promiscuous_enable = eth_dev_promiscuous_enable,
1513         .promiscuous_disable = eth_dev_promiscuous_disable,
1514         .rx_queue_setup = eth_rx_queue_setup,
1515         .tx_queue_setup = eth_tx_queue_setup,
1516         .link_update = eth_link_update,
1517         .stats_get = eth_stats_get,
1518         .stats_reset = eth_stats_reset,
1519         .get_monitor_addr = eth_get_monitor_addr,
1520 };
1521
1522 /** parse busy_budget argument */
1523 static int
1524 parse_budget_arg(const char *key __rte_unused,
1525                   const char *value, void *extra_args)
1526 {
1527         int *i = (int *)extra_args;
1528         char *end;
1529
1530         *i = strtol(value, &end, 10);
1531         if (*i < 0 || *i > UINT16_MAX) {
1532                 AF_XDP_LOG(ERR, "Invalid busy_budget %i, must be >= 0 and <= %u\n",
1533                                 *i, UINT16_MAX);
1534                 return -EINVAL;
1535         }
1536
1537         return 0;
1538 }
1539
1540 /** parse integer from integer argument */
1541 static int
1542 parse_integer_arg(const char *key __rte_unused,
1543                   const char *value, void *extra_args)
1544 {
1545         int *i = (int *)extra_args;
1546         char *end;
1547
1548         *i = strtol(value, &end, 10);
1549         if (*i < 0) {
1550                 AF_XDP_LOG(ERR, "Argument has to be positive.\n");
1551                 return -EINVAL;
1552         }
1553
1554         return 0;
1555 }
1556
1557 /** parse name argument */
1558 static int
1559 parse_name_arg(const char *key __rte_unused,
1560                const char *value, void *extra_args)
1561 {
1562         char *name = extra_args;
1563
1564         if (strnlen(value, IFNAMSIZ) > IFNAMSIZ - 1) {
1565                 AF_XDP_LOG(ERR, "Invalid name %s, should be less than %u bytes.\n",
1566                            value, IFNAMSIZ);
1567                 return -EINVAL;
1568         }
1569
1570         strlcpy(name, value, IFNAMSIZ);
1571
1572         return 0;
1573 }
1574
1575 /** parse xdp prog argument */
1576 static int
1577 parse_prog_arg(const char *key __rte_unused,
1578                const char *value, void *extra_args)
1579 {
1580         char *path = extra_args;
1581
1582         if (strnlen(value, PATH_MAX) == PATH_MAX) {
1583                 AF_XDP_LOG(ERR, "Invalid path %s, should be less than %u bytes.\n",
1584                            value, PATH_MAX);
1585                 return -EINVAL;
1586         }
1587
1588         if (access(value, F_OK) != 0) {
1589                 AF_XDP_LOG(ERR, "Error accessing %s: %s\n",
1590                            value, strerror(errno));
1591                 return -EINVAL;
1592         }
1593
1594         strlcpy(path, value, PATH_MAX);
1595
1596         return 0;
1597 }
1598
1599 static int
1600 xdp_get_channels_info(const char *if_name, int *max_queues,
1601                                 int *combined_queues)
1602 {
1603         struct ethtool_channels channels;
1604         struct ifreq ifr;
1605         int fd, ret;
1606
1607         fd = socket(AF_INET, SOCK_DGRAM, 0);
1608         if (fd < 0)
1609                 return -1;
1610
1611         channels.cmd = ETHTOOL_GCHANNELS;
1612         ifr.ifr_data = (void *)&channels;
1613         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1614         ret = ioctl(fd, SIOCETHTOOL, &ifr);
1615         if (ret) {
1616                 if (errno == EOPNOTSUPP) {
1617                         ret = 0;
1618                 } else {
1619                         ret = -errno;
1620                         goto out;
1621                 }
1622         }
1623
1624         if (channels.max_combined == 0 || errno == EOPNOTSUPP) {
1625                 /* If the device says it has no channels, then all traffic
1626                  * is sent to a single stream, so max queues = 1.
1627                  */
1628                 *max_queues = 1;
1629                 *combined_queues = 1;
1630         } else {
1631                 *max_queues = channels.max_combined;
1632                 *combined_queues = channels.combined_count;
1633         }
1634
1635  out:
1636         close(fd);
1637         return ret;
1638 }
1639
1640 static int
1641 parse_parameters(struct rte_kvargs *kvlist, char *if_name, int *start_queue,
1642                         int *queue_cnt, int *shared_umem, char *prog_path,
1643                         int *busy_budget)
1644 {
1645         int ret;
1646
1647         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_IFACE_ARG,
1648                                  &parse_name_arg, if_name);
1649         if (ret < 0)
1650                 goto free_kvlist;
1651
1652         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_START_QUEUE_ARG,
1653                                  &parse_integer_arg, start_queue);
1654         if (ret < 0)
1655                 goto free_kvlist;
1656
1657         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_QUEUE_COUNT_ARG,
1658                                  &parse_integer_arg, queue_cnt);
1659         if (ret < 0 || *queue_cnt <= 0) {
1660                 ret = -EINVAL;
1661                 goto free_kvlist;
1662         }
1663
1664         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_SHARED_UMEM_ARG,
1665                                 &parse_integer_arg, shared_umem);
1666         if (ret < 0)
1667                 goto free_kvlist;
1668
1669         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_PROG_ARG,
1670                                  &parse_prog_arg, prog_path);
1671         if (ret < 0)
1672                 goto free_kvlist;
1673
1674         ret = rte_kvargs_process(kvlist, ETH_AF_XDP_BUDGET_ARG,
1675                                 &parse_budget_arg, busy_budget);
1676         if (ret < 0)
1677                 goto free_kvlist;
1678
1679 free_kvlist:
1680         rte_kvargs_free(kvlist);
1681         return ret;
1682 }
1683
1684 static int
1685 get_iface_info(const char *if_name,
1686                struct rte_ether_addr *eth_addr,
1687                int *if_index)
1688 {
1689         struct ifreq ifr;
1690         int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
1691
1692         if (sock < 0)
1693                 return -1;
1694
1695         strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
1696         if (ioctl(sock, SIOCGIFINDEX, &ifr))
1697                 goto error;
1698
1699         *if_index = ifr.ifr_ifindex;
1700
1701         if (ioctl(sock, SIOCGIFHWADDR, &ifr))
1702                 goto error;
1703
1704         rte_memcpy(eth_addr, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1705
1706         close(sock);
1707         return 0;
1708
1709 error:
1710         close(sock);
1711         return -1;
1712 }
1713
1714 static struct rte_eth_dev *
1715 init_internals(struct rte_vdev_device *dev, const char *if_name,
1716                 int start_queue_idx, int queue_cnt, int shared_umem,
1717                 const char *prog_path, int busy_budget)
1718 {
1719         const char *name = rte_vdev_device_name(dev);
1720         const unsigned int numa_node = dev->device.numa_node;
1721         struct pmd_process_private *process_private;
1722         struct pmd_internals *internals;
1723         struct rte_eth_dev *eth_dev;
1724         int ret;
1725         int i;
1726
1727         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
1728         if (internals == NULL)
1729                 return NULL;
1730
1731         internals->start_queue_idx = start_queue_idx;
1732         internals->queue_cnt = queue_cnt;
1733         strlcpy(internals->if_name, if_name, IFNAMSIZ);
1734         strlcpy(internals->prog_path, prog_path, PATH_MAX);
1735         internals->custom_prog_configured = 0;
1736
1737 #ifndef ETH_AF_XDP_SHARED_UMEM
1738         if (shared_umem) {
1739                 AF_XDP_LOG(ERR, "Shared UMEM feature not available. "
1740                                 "Check kernel and libbpf version\n");
1741                 goto err_free_internals;
1742         }
1743 #endif
1744         internals->shared_umem = shared_umem;
1745
1746         if (xdp_get_channels_info(if_name, &internals->max_queue_cnt,
1747                                   &internals->combined_queue_cnt)) {
1748                 AF_XDP_LOG(ERR, "Failed to get channel info of interface: %s\n",
1749                                 if_name);
1750                 goto err_free_internals;
1751         }
1752
1753         if (queue_cnt > internals->combined_queue_cnt) {
1754                 AF_XDP_LOG(ERR, "Specified queue count %d is larger than combined queue count %d.\n",
1755                                 queue_cnt, internals->combined_queue_cnt);
1756                 goto err_free_internals;
1757         }
1758
1759         internals->rx_queues = rte_zmalloc_socket(NULL,
1760                                         sizeof(struct pkt_rx_queue) * queue_cnt,
1761                                         0, numa_node);
1762         if (internals->rx_queues == NULL) {
1763                 AF_XDP_LOG(ERR, "Failed to allocate memory for rx queues.\n");
1764                 goto err_free_internals;
1765         }
1766
1767         internals->tx_queues = rte_zmalloc_socket(NULL,
1768                                         sizeof(struct pkt_tx_queue) * queue_cnt,
1769                                         0, numa_node);
1770         if (internals->tx_queues == NULL) {
1771                 AF_XDP_LOG(ERR, "Failed to allocate memory for tx queues.\n");
1772                 goto err_free_rx;
1773         }
1774         for (i = 0; i < queue_cnt; i++) {
1775                 internals->tx_queues[i].pair = &internals->rx_queues[i];
1776                 internals->rx_queues[i].pair = &internals->tx_queues[i];
1777                 internals->rx_queues[i].xsk_queue_idx = start_queue_idx + i;
1778                 internals->tx_queues[i].xsk_queue_idx = start_queue_idx + i;
1779                 internals->rx_queues[i].busy_budget = busy_budget;
1780         }
1781
1782         ret = get_iface_info(if_name, &internals->eth_addr,
1783                              &internals->if_index);
1784         if (ret)
1785                 goto err_free_tx;
1786
1787         process_private = (struct pmd_process_private *)
1788                 rte_zmalloc_socket(name, sizeof(struct pmd_process_private),
1789                                    RTE_CACHE_LINE_SIZE, numa_node);
1790         if (process_private == NULL) {
1791                 AF_XDP_LOG(ERR, "Failed to alloc memory for process private\n");
1792                 goto err_free_tx;
1793         }
1794
1795         eth_dev = rte_eth_vdev_allocate(dev, 0);
1796         if (eth_dev == NULL)
1797                 goto err_free_pp;
1798
1799         eth_dev->data->dev_private = internals;
1800         eth_dev->data->dev_link = pmd_link;
1801         eth_dev->data->mac_addrs = &internals->eth_addr;
1802         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1803         eth_dev->dev_ops = &ops;
1804         eth_dev->rx_pkt_burst = eth_af_xdp_rx;
1805         eth_dev->tx_pkt_burst = eth_af_xdp_tx;
1806         eth_dev->process_private = process_private;
1807
1808         for (i = 0; i < queue_cnt; i++)
1809                 process_private->rxq_xsk_fds[i] = -1;
1810
1811 #if defined(XDP_UMEM_UNALIGNED_CHUNK_FLAG)
1812         AF_XDP_LOG(INFO, "Zero copy between umem and mbuf enabled.\n");
1813 #endif
1814
1815         return eth_dev;
1816
1817 err_free_pp:
1818         rte_free(process_private);
1819 err_free_tx:
1820         rte_free(internals->tx_queues);
1821 err_free_rx:
1822         rte_free(internals->rx_queues);
1823 err_free_internals:
1824         rte_free(internals);
1825         return NULL;
1826 }
1827
1828 /* Secondary process requests rxq fds from primary. */
1829 static int
1830 afxdp_mp_request_fds(const char *name, struct rte_eth_dev *dev)
1831 {
1832         struct pmd_process_private *process_private = dev->process_private;
1833         struct timespec timeout = {.tv_sec = 1, .tv_nsec = 0};
1834         struct rte_mp_msg request, *reply;
1835         struct rte_mp_reply replies;
1836         struct ipc_hdr *request_param = (struct ipc_hdr *)request.param;
1837         int i, ret;
1838
1839         /* Prepare the request */
1840         memset(&request, 0, sizeof(request));
1841         strlcpy(request.name, ETH_AF_XDP_MP_KEY, sizeof(request.name));
1842         strlcpy(request_param->port_name, name,
1843                 sizeof(request_param->port_name));
1844         request.len_param = sizeof(*request_param);
1845
1846         /* Send the request and receive the reply */
1847         AF_XDP_LOG(DEBUG, "Sending multi-process IPC request for %s\n", name);
1848         ret = rte_mp_request_sync(&request, &replies, &timeout);
1849         if (ret < 0 || replies.nb_received != 1) {
1850                 AF_XDP_LOG(ERR, "Failed to request fds from primary: %d",
1851                            rte_errno);
1852                 return -1;
1853         }
1854         reply = replies.msgs;
1855         AF_XDP_LOG(DEBUG, "Received multi-process IPC reply for %s\n", name);
1856         if (dev->data->nb_rx_queues != reply->num_fds) {
1857                 AF_XDP_LOG(ERR, "Incorrect number of fds received: %d != %d\n",
1858                            reply->num_fds, dev->data->nb_rx_queues);
1859                 return -EINVAL;
1860         }
1861
1862         for (i = 0; i < reply->num_fds; i++)
1863                 process_private->rxq_xsk_fds[i] = reply->fds[i];
1864
1865         free(reply);
1866         return 0;
1867 }
1868
1869 /* Primary process sends rxq fds to secondary. */
1870 static int
1871 afxdp_mp_send_fds(const struct rte_mp_msg *request, const void *peer)
1872 {
1873         struct rte_eth_dev *dev;
1874         struct pmd_process_private *process_private;
1875         struct rte_mp_msg reply;
1876         const struct ipc_hdr *request_param =
1877                 (const struct ipc_hdr *)request->param;
1878         struct ipc_hdr *reply_param =
1879                 (struct ipc_hdr *)reply.param;
1880         const char *request_name = request_param->port_name;
1881         int i;
1882
1883         AF_XDP_LOG(DEBUG, "Received multi-process IPC request for %s\n",
1884                    request_name);
1885
1886         /* Find the requested port */
1887         dev = rte_eth_dev_get_by_name(request_name);
1888         if (!dev) {
1889                 AF_XDP_LOG(ERR, "Failed to get port id for %s\n", request_name);
1890                 return -1;
1891         }
1892         process_private = dev->process_private;
1893
1894         /* Populate the reply with the xsk fd for each queue */
1895         reply.num_fds = 0;
1896         if (dev->data->nb_rx_queues > RTE_MP_MAX_FD_NUM) {
1897                 AF_XDP_LOG(ERR, "Number of rx queues (%d) exceeds max number of fds (%d)\n",
1898                            dev->data->nb_rx_queues, RTE_MP_MAX_FD_NUM);
1899                 return -EINVAL;
1900         }
1901
1902         for (i = 0; i < dev->data->nb_rx_queues; i++)
1903                 reply.fds[reply.num_fds++] = process_private->rxq_xsk_fds[i];
1904
1905         /* Send the reply */
1906         strlcpy(reply.name, request->name, sizeof(reply.name));
1907         strlcpy(reply_param->port_name, request_name,
1908                 sizeof(reply_param->port_name));
1909         reply.len_param = sizeof(*reply_param);
1910         AF_XDP_LOG(DEBUG, "Sending multi-process IPC reply for %s\n",
1911                    reply_param->port_name);
1912         if (rte_mp_reply(&reply, peer) < 0) {
1913                 AF_XDP_LOG(ERR, "Failed to reply to multi-process IPC request\n");
1914                 return -1;
1915         }
1916         return 0;
1917 }
1918
1919 static int
1920 rte_pmd_af_xdp_probe(struct rte_vdev_device *dev)
1921 {
1922         struct rte_kvargs *kvlist;
1923         char if_name[IFNAMSIZ] = {'\0'};
1924         int xsk_start_queue_idx = ETH_AF_XDP_DFLT_START_QUEUE_IDX;
1925         int xsk_queue_cnt = ETH_AF_XDP_DFLT_QUEUE_COUNT;
1926         int shared_umem = 0;
1927         char prog_path[PATH_MAX] = {'\0'};
1928         int busy_budget = -1, ret;
1929         struct rte_eth_dev *eth_dev = NULL;
1930         const char *name = rte_vdev_device_name(dev);
1931
1932         AF_XDP_LOG(INFO, "Initializing pmd_af_xdp for %s\n", name);
1933
1934         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1935                 eth_dev = rte_eth_dev_attach_secondary(name);
1936                 if (eth_dev == NULL) {
1937                         AF_XDP_LOG(ERR, "Failed to probe %s\n", name);
1938                         return -EINVAL;
1939                 }
1940                 eth_dev->dev_ops = &ops;
1941                 eth_dev->device = &dev->device;
1942                 eth_dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
1943                 eth_dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
1944                 eth_dev->process_private = (struct pmd_process_private *)
1945                         rte_zmalloc_socket(name,
1946                                            sizeof(struct pmd_process_private),
1947                                            RTE_CACHE_LINE_SIZE,
1948                                            eth_dev->device->numa_node);
1949                 if (eth_dev->process_private == NULL) {
1950                         AF_XDP_LOG(ERR,
1951                                 "Failed to alloc memory for process private\n");
1952                         return -ENOMEM;
1953                 }
1954
1955                 /* Obtain the xsk fds from the primary process. */
1956                 if (afxdp_mp_request_fds(name, eth_dev))
1957                         return -1;
1958
1959                 rte_eth_dev_probing_finish(eth_dev);
1960                 return 0;
1961         }
1962
1963         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1964         if (kvlist == NULL) {
1965                 AF_XDP_LOG(ERR, "Invalid kvargs key\n");
1966                 return -EINVAL;
1967         }
1968
1969         if (dev->device.numa_node == SOCKET_ID_ANY)
1970                 dev->device.numa_node = rte_socket_id();
1971
1972         if (parse_parameters(kvlist, if_name, &xsk_start_queue_idx,
1973                              &xsk_queue_cnt, &shared_umem, prog_path,
1974                              &busy_budget) < 0) {
1975                 AF_XDP_LOG(ERR, "Invalid kvargs value\n");
1976                 return -EINVAL;
1977         }
1978
1979         if (strlen(if_name) == 0) {
1980                 AF_XDP_LOG(ERR, "Network interface must be specified\n");
1981                 return -EINVAL;
1982         }
1983
1984         busy_budget = busy_budget == -1 ? ETH_AF_XDP_DFLT_BUSY_BUDGET :
1985                                         busy_budget;
1986
1987         eth_dev = init_internals(dev, if_name, xsk_start_queue_idx,
1988                                         xsk_queue_cnt, shared_umem, prog_path,
1989                                         busy_budget);
1990         if (eth_dev == NULL) {
1991                 AF_XDP_LOG(ERR, "Failed to init internals\n");
1992                 return -1;
1993         }
1994
1995         /* Register IPC callback which shares xsk fds from primary to secondary */
1996         if (!afxdp_dev_count) {
1997                 ret = rte_mp_action_register(ETH_AF_XDP_MP_KEY, afxdp_mp_send_fds);
1998                 if (ret < 0) {
1999                         AF_XDP_LOG(ERR, "%s: Failed to register multi-process IPC callback: %s",
2000                                    name, strerror(rte_errno));
2001                         return -1;
2002                 }
2003         }
2004         afxdp_dev_count++;
2005
2006         rte_eth_dev_probing_finish(eth_dev);
2007
2008         return 0;
2009 }
2010
2011 static int
2012 rte_pmd_af_xdp_remove(struct rte_vdev_device *dev)
2013 {
2014         struct rte_eth_dev *eth_dev = NULL;
2015
2016         AF_XDP_LOG(INFO, "Removing AF_XDP ethdev on numa socket %u\n",
2017                 rte_socket_id());
2018
2019         if (dev == NULL)
2020                 return -1;
2021
2022         /* find the ethdev entry */
2023         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
2024         if (eth_dev == NULL)
2025                 return 0;
2026
2027         eth_dev_close(eth_dev);
2028         if (afxdp_dev_count == 1)
2029                 rte_mp_action_unregister(ETH_AF_XDP_MP_KEY);
2030         afxdp_dev_count--;
2031         rte_eth_dev_release_port(eth_dev);
2032
2033         return 0;
2034 }
2035
2036 static struct rte_vdev_driver pmd_af_xdp_drv = {
2037         .probe = rte_pmd_af_xdp_probe,
2038         .remove = rte_pmd_af_xdp_remove,
2039 };
2040
2041 RTE_PMD_REGISTER_VDEV(net_af_xdp, pmd_af_xdp_drv);
2042 RTE_PMD_REGISTER_PARAM_STRING(net_af_xdp,
2043                               "iface=<string> "
2044                               "start_queue=<int> "
2045                               "queue_count=<int> "
2046                               "shared_umem=<int> "
2047                               "xdp_prog=<string> "
2048                               "busy_budget=<int>");