net/pcap: fix Rx with small buffers
[dpdk.git] / drivers / net / pcap / rte_eth_pcap.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  * All rights reserved.
5  */
6
7 #include <time.h>
8
9 #include <net/if.h>
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <unistd.h>
13
14 #if defined(RTE_EXEC_ENV_FREEBSD)
15 #include <sys/sysctl.h>
16 #include <net/if_dl.h>
17 #endif
18
19 #include <pcap.h>
20
21 #include <rte_cycles.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_malloc.h>
26 #include <rte_mbuf.h>
27 #include <rte_bus_vdev.h>
28 #include <rte_string_fns.h>
29
30 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
31 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
32 #define RTE_ETH_PCAP_PROMISC 1
33 #define RTE_ETH_PCAP_TIMEOUT -1
34
35 #define ETH_PCAP_RX_PCAP_ARG  "rx_pcap"
36 #define ETH_PCAP_TX_PCAP_ARG  "tx_pcap"
37 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
38 #define ETH_PCAP_RX_IFACE_IN_ARG "rx_iface_in"
39 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
40 #define ETH_PCAP_IFACE_ARG    "iface"
41 #define ETH_PCAP_PHY_MAC_ARG  "phy_mac"
42 #define ETH_PCAP_INFINITE_RX_ARG  "infinite_rx"
43
44 #define ETH_PCAP_ARG_MAXLEN     64
45
46 #define RTE_PMD_PCAP_MAX_QUEUES 16
47
48 static char errbuf[PCAP_ERRBUF_SIZE];
49 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
50 static struct timeval start_time;
51 static uint64_t start_cycles;
52 static uint64_t hz;
53 static uint8_t iface_idx;
54
55 struct queue_stat {
56         volatile unsigned long pkts;
57         volatile unsigned long bytes;
58         volatile unsigned long err_pkts;
59 };
60
61 struct pcap_rx_queue {
62         uint16_t port_id;
63         uint16_t queue_id;
64         struct rte_mempool *mb_pool;
65         struct queue_stat rx_stat;
66         char name[PATH_MAX];
67         char type[ETH_PCAP_ARG_MAXLEN];
68
69         /* Contains pre-generated packets to be looped through */
70         struct rte_ring *pkts;
71 };
72
73 struct pcap_tx_queue {
74         uint16_t port_id;
75         uint16_t queue_id;
76         struct queue_stat tx_stat;
77         char name[PATH_MAX];
78         char type[ETH_PCAP_ARG_MAXLEN];
79 };
80
81 struct pmd_internals {
82         struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
83         struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
84         char devargs[ETH_PCAP_ARG_MAXLEN];
85         struct rte_ether_addr eth_addr;
86         int if_index;
87         int single_iface;
88         int phy_mac;
89         unsigned int infinite_rx;
90 };
91
92 struct pmd_process_private {
93         pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
94         pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
95         pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
96 };
97
98 struct pmd_devargs {
99         unsigned int num_of_queue;
100         struct devargs_queue {
101                 pcap_dumper_t *dumper;
102                 pcap_t *pcap;
103                 const char *name;
104                 const char *type;
105         } queue[RTE_PMD_PCAP_MAX_QUEUES];
106         int phy_mac;
107 };
108
109 struct pmd_devargs_all {
110         struct pmd_devargs rx_queues;
111         struct pmd_devargs tx_queues;
112         int single_iface;
113         unsigned int is_tx_pcap;
114         unsigned int is_tx_iface;
115         unsigned int is_rx_pcap;
116         unsigned int is_rx_iface;
117         unsigned int infinite_rx;
118 };
119
120 static const char *valid_arguments[] = {
121         ETH_PCAP_RX_PCAP_ARG,
122         ETH_PCAP_TX_PCAP_ARG,
123         ETH_PCAP_RX_IFACE_ARG,
124         ETH_PCAP_RX_IFACE_IN_ARG,
125         ETH_PCAP_TX_IFACE_ARG,
126         ETH_PCAP_IFACE_ARG,
127         ETH_PCAP_PHY_MAC_ARG,
128         ETH_PCAP_INFINITE_RX_ARG,
129         NULL
130 };
131
132 static struct rte_eth_link pmd_link = {
133                 .link_speed = ETH_SPEED_NUM_10G,
134                 .link_duplex = ETH_LINK_FULL_DUPLEX,
135                 .link_status = ETH_LINK_DOWN,
136                 .link_autoneg = ETH_LINK_FIXED,
137 };
138
139 static int eth_pcap_logtype;
140
141 #define PMD_LOG(level, fmt, args...) \
142         rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
143                 "%s(): " fmt "\n", __func__, ##args)
144
145 static int
146 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
147                 const u_char *data, uint16_t data_len)
148 {
149         /* Copy the first segment. */
150         uint16_t len = rte_pktmbuf_tailroom(mbuf);
151         struct rte_mbuf *m = mbuf;
152
153         rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
154         data_len -= len;
155         data += len;
156
157         while (data_len > 0) {
158                 /* Allocate next mbuf and point to that. */
159                 m->next = rte_pktmbuf_alloc(mb_pool);
160
161                 if (unlikely(!m->next))
162                         return -1;
163
164                 m = m->next;
165
166                 /* Headroom is not needed in chained mbufs. */
167                 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
168                 m->pkt_len = 0;
169                 m->data_len = 0;
170
171                 /* Copy next segment. */
172                 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
173                 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
174
175                 mbuf->nb_segs++;
176                 data_len -= len;
177                 data += len;
178         }
179
180         return mbuf->nb_segs;
181 }
182
183 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
184 static void
185 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
186 {
187         uint16_t data_len = 0;
188
189         while (mbuf) {
190                 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
191                         mbuf->data_len);
192
193                 data_len += mbuf->data_len;
194                 mbuf = mbuf->next;
195         }
196 }
197
198 static uint16_t
199 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
200 {
201         int i;
202         struct pcap_rx_queue *pcap_q = queue;
203         uint32_t rx_bytes = 0;
204
205         if (unlikely(nb_pkts == 0))
206                 return 0;
207
208         if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
209                 return 0;
210
211         for (i = 0; i < nb_pkts; i++) {
212                 struct rte_mbuf *pcap_buf;
213                 int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
214                 if (err)
215                         return i;
216
217                 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
218                                 rte_pktmbuf_mtod(pcap_buf, void *),
219                                 pcap_buf->data_len);
220                 bufs[i]->data_len = pcap_buf->data_len;
221                 bufs[i]->pkt_len = pcap_buf->pkt_len;
222                 bufs[i]->port = pcap_q->port_id;
223                 rx_bytes += pcap_buf->data_len;
224
225                 /* Enqueue packet back on ring to allow infinite rx. */
226                 rte_ring_enqueue(pcap_q->pkts, pcap_buf);
227         }
228
229         pcap_q->rx_stat.pkts += i;
230         pcap_q->rx_stat.bytes += rx_bytes;
231
232         return i;
233 }
234
235 static uint16_t
236 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
237 {
238         unsigned int i;
239         struct pcap_pkthdr header;
240         struct pmd_process_private *pp;
241         const u_char *packet;
242         struct rte_mbuf *mbuf;
243         struct pcap_rx_queue *pcap_q = queue;
244         uint16_t num_rx = 0;
245         uint32_t rx_bytes = 0;
246         pcap_t *pcap;
247
248         pp = rte_eth_devices[pcap_q->port_id].process_private;
249         pcap = pp->rx_pcap[pcap_q->queue_id];
250
251         if (unlikely(pcap == NULL || nb_pkts == 0))
252                 return 0;
253
254         /* Reads the given number of packets from the pcap file one by one
255          * and copies the packet data into a newly allocated mbuf to return.
256          */
257         for (i = 0; i < nb_pkts; i++) {
258                 /* Get the next PCAP packet */
259                 packet = pcap_next(pcap, &header);
260                 if (unlikely(packet == NULL))
261                         break;
262
263                 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
264                 if (unlikely(mbuf == NULL))
265                         break;
266
267                 if (header.caplen <= rte_pktmbuf_tailroom(mbuf)) {
268                         /* pcap packet will fit in the mbuf, can copy it */
269                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
270                                         header.caplen);
271                         mbuf->data_len = (uint16_t)header.caplen;
272                 } else {
273                         /* Try read jumbo frame into multi mbufs. */
274                         if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
275                                                        mbuf,
276                                                        packet,
277                                                        header.caplen) == -1)) {
278                                 rte_pktmbuf_free(mbuf);
279                                 break;
280                         }
281                 }
282
283                 mbuf->pkt_len = (uint16_t)header.caplen;
284                 mbuf->port = pcap_q->port_id;
285                 bufs[num_rx] = mbuf;
286                 num_rx++;
287                 rx_bytes += header.caplen;
288         }
289         pcap_q->rx_stat.pkts += num_rx;
290         pcap_q->rx_stat.bytes += rx_bytes;
291
292         return num_rx;
293 }
294
295 static uint16_t
296 eth_null_rx(void *queue __rte_unused,
297                 struct rte_mbuf **bufs __rte_unused,
298                 uint16_t nb_pkts __rte_unused)
299 {
300         return 0;
301 }
302
303 static inline void
304 calculate_timestamp(struct timeval *ts) {
305         uint64_t cycles;
306         struct timeval cur_time;
307
308         cycles = rte_get_timer_cycles() - start_cycles;
309         cur_time.tv_sec = cycles / hz;
310         cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
311         timeradd(&start_time, &cur_time, ts);
312 }
313
314 /*
315  * Callback to handle writing packets to a pcap file.
316  */
317 static uint16_t
318 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
319 {
320         unsigned int i;
321         struct rte_mbuf *mbuf;
322         struct pmd_process_private *pp;
323         struct pcap_tx_queue *dumper_q = queue;
324         uint16_t num_tx = 0;
325         uint32_t tx_bytes = 0;
326         struct pcap_pkthdr header;
327         pcap_dumper_t *dumper;
328
329         pp = rte_eth_devices[dumper_q->port_id].process_private;
330         dumper = pp->tx_dumper[dumper_q->queue_id];
331
332         if (dumper == NULL || nb_pkts == 0)
333                 return 0;
334
335         /* writes the nb_pkts packets to the previously opened pcap file
336          * dumper */
337         for (i = 0; i < nb_pkts; i++) {
338                 mbuf = bufs[i];
339                 calculate_timestamp(&header.ts);
340                 header.len = mbuf->pkt_len;
341                 header.caplen = header.len;
342
343                 if (likely(mbuf->nb_segs == 1)) {
344                         pcap_dump((u_char *)dumper, &header,
345                                   rte_pktmbuf_mtod(mbuf, void*));
346                 } else {
347                         if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
348                                 eth_pcap_gather_data(tx_pcap_data, mbuf);
349                                 pcap_dump((u_char *)dumper, &header,
350                                           tx_pcap_data);
351                         } else {
352                                 PMD_LOG(ERR,
353                                         "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
354                                         mbuf->pkt_len,
355                                         RTE_ETHER_MAX_JUMBO_FRAME_LEN);
356
357                                 break;
358                         }
359                 }
360
361                 num_tx++;
362                 tx_bytes += mbuf->pkt_len;
363                 rte_pktmbuf_free(mbuf);
364         }
365
366         /*
367          * Since there's no place to hook a callback when the forwarding
368          * process stops and to make sure the pcap file is actually written,
369          * we flush the pcap dumper within each burst.
370          */
371         pcap_dump_flush(dumper);
372         dumper_q->tx_stat.pkts += num_tx;
373         dumper_q->tx_stat.bytes += tx_bytes;
374         dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
375
376         return num_tx;
377 }
378
379 /*
380  * Callback to handle dropping packets in the infinite rx case.
381  */
382 static uint16_t
383 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
384 {
385         unsigned int i;
386         uint32_t tx_bytes = 0;
387         struct pcap_tx_queue *tx_queue = queue;
388
389         if (unlikely(nb_pkts == 0))
390                 return 0;
391
392         for (i = 0; i < nb_pkts; i++) {
393                 tx_bytes += bufs[i]->data_len;
394                 rte_pktmbuf_free(bufs[i]);
395         }
396
397         tx_queue->tx_stat.pkts += nb_pkts;
398         tx_queue->tx_stat.bytes += tx_bytes;
399
400         return i;
401 }
402
403 /*
404  * Callback to handle sending packets through a real NIC.
405  */
406 static uint16_t
407 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
408 {
409         unsigned int i;
410         int ret;
411         struct rte_mbuf *mbuf;
412         struct pmd_process_private *pp;
413         struct pcap_tx_queue *tx_queue = queue;
414         uint16_t num_tx = 0;
415         uint32_t tx_bytes = 0;
416         pcap_t *pcap;
417
418         pp = rte_eth_devices[tx_queue->port_id].process_private;
419         pcap = pp->tx_pcap[tx_queue->queue_id];
420
421         if (unlikely(nb_pkts == 0 || pcap == NULL))
422                 return 0;
423
424         for (i = 0; i < nb_pkts; i++) {
425                 mbuf = bufs[i];
426
427                 if (likely(mbuf->nb_segs == 1)) {
428                         ret = pcap_sendpacket(pcap,
429                                         rte_pktmbuf_mtod(mbuf, u_char *),
430                                         mbuf->pkt_len);
431                 } else {
432                         if (mbuf->pkt_len <= RTE_ETHER_MAX_JUMBO_FRAME_LEN) {
433                                 eth_pcap_gather_data(tx_pcap_data, mbuf);
434                                 ret = pcap_sendpacket(pcap,
435                                                 tx_pcap_data, mbuf->pkt_len);
436                         } else {
437                                 PMD_LOG(ERR,
438                                         "Dropping PCAP packet. Size (%d) > max jumbo size (%d).",
439                                         mbuf->pkt_len,
440                                         RTE_ETHER_MAX_JUMBO_FRAME_LEN);
441
442                                 break;
443                         }
444                 }
445
446                 if (unlikely(ret != 0))
447                         break;
448                 num_tx++;
449                 tx_bytes += mbuf->pkt_len;
450                 rte_pktmbuf_free(mbuf);
451         }
452
453         tx_queue->tx_stat.pkts += num_tx;
454         tx_queue->tx_stat.bytes += tx_bytes;
455         tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
456
457         return num_tx;
458 }
459
460 /*
461  * pcap_open_live wrapper function
462  */
463 static inline int
464 open_iface_live(const char *iface, pcap_t **pcap) {
465         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
466                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
467
468         if (*pcap == NULL) {
469                 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
470                 return -1;
471         }
472
473         return 0;
474 }
475
476 static int
477 open_single_iface(const char *iface, pcap_t **pcap)
478 {
479         if (open_iface_live(iface, pcap) < 0) {
480                 PMD_LOG(ERR, "Couldn't open interface %s", iface);
481                 return -1;
482         }
483
484         return 0;
485 }
486
487 static int
488 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
489 {
490         pcap_t *tx_pcap;
491
492         /*
493          * We need to create a dummy empty pcap_t to use it
494          * with pcap_dump_open(). We create big enough an Ethernet
495          * pcap holder.
496          */
497         tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
498         if (tx_pcap == NULL) {
499                 PMD_LOG(ERR, "Couldn't create dead pcap");
500                 return -1;
501         }
502
503         /* The dumper is created using the previous pcap_t reference */
504         *dumper = pcap_dump_open(tx_pcap, pcap_filename);
505         if (*dumper == NULL) {
506                 pcap_close(tx_pcap);
507                 PMD_LOG(ERR, "Couldn't open %s for writing.",
508                         pcap_filename);
509                 return -1;
510         }
511
512         pcap_close(tx_pcap);
513         return 0;
514 }
515
516 static int
517 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
518 {
519         *pcap = pcap_open_offline(pcap_filename, errbuf);
520         if (*pcap == NULL) {
521                 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
522                         errbuf);
523                 return -1;
524         }
525
526         return 0;
527 }
528
529 static uint64_t
530 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
531 {
532         const u_char *packet;
533         struct pcap_pkthdr header;
534         uint64_t pcap_pkt_count = 0;
535
536         while ((packet = pcap_next(*pcap, &header)))
537                 pcap_pkt_count++;
538
539         /* The pcap is reopened so it can be used as normal later. */
540         pcap_close(*pcap);
541         *pcap = NULL;
542         open_single_rx_pcap(pcap_q->name, pcap);
543
544         return pcap_pkt_count;
545 }
546
547 static int
548 eth_dev_start(struct rte_eth_dev *dev)
549 {
550         unsigned int i;
551         struct pmd_internals *internals = dev->data->dev_private;
552         struct pmd_process_private *pp = dev->process_private;
553         struct pcap_tx_queue *tx;
554         struct pcap_rx_queue *rx;
555
556         /* Special iface case. Single pcap is open and shared between tx/rx. */
557         if (internals->single_iface) {
558                 tx = &internals->tx_queue[0];
559                 rx = &internals->rx_queue[0];
560
561                 if (!pp->tx_pcap[0] &&
562                         strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
563                         if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
564                                 return -1;
565                         pp->rx_pcap[0] = pp->tx_pcap[0];
566                 }
567
568                 goto status_up;
569         }
570
571         /* If not open already, open tx pcaps/dumpers */
572         for (i = 0; i < dev->data->nb_tx_queues; i++) {
573                 tx = &internals->tx_queue[i];
574
575                 if (!pp->tx_dumper[i] &&
576                                 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
577                         if (open_single_tx_pcap(tx->name,
578                                 &pp->tx_dumper[i]) < 0)
579                                 return -1;
580                 } else if (!pp->tx_pcap[i] &&
581                                 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
582                         if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
583                                 return -1;
584                 }
585         }
586
587         /* If not open already, open rx pcaps */
588         for (i = 0; i < dev->data->nb_rx_queues; i++) {
589                 rx = &internals->rx_queue[i];
590
591                 if (pp->rx_pcap[i] != NULL)
592                         continue;
593
594                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
595                         if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
596                                 return -1;
597                 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
598                         if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
599                                 return -1;
600                 }
601         }
602
603 status_up:
604         for (i = 0; i < dev->data->nb_rx_queues; i++)
605                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
606
607         for (i = 0; i < dev->data->nb_tx_queues; i++)
608                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
609
610         dev->data->dev_link.link_status = ETH_LINK_UP;
611
612         return 0;
613 }
614
615 /*
616  * This function gets called when the current port gets stopped.
617  * Is the only place for us to close all the tx streams dumpers.
618  * If not called the dumpers will be flushed within each tx burst.
619  */
620 static void
621 eth_dev_stop(struct rte_eth_dev *dev)
622 {
623         unsigned int i;
624         struct pmd_internals *internals = dev->data->dev_private;
625         struct pmd_process_private *pp = dev->process_private;
626
627         /* Special iface case. Single pcap is open and shared between tx/rx. */
628         if (internals->single_iface) {
629                 pcap_close(pp->tx_pcap[0]);
630                 pp->tx_pcap[0] = NULL;
631                 pp->rx_pcap[0] = NULL;
632                 goto status_down;
633         }
634
635         for (i = 0; i < dev->data->nb_tx_queues; i++) {
636                 if (pp->tx_dumper[i] != NULL) {
637                         pcap_dump_close(pp->tx_dumper[i]);
638                         pp->tx_dumper[i] = NULL;
639                 }
640
641                 if (pp->tx_pcap[i] != NULL) {
642                         pcap_close(pp->tx_pcap[i]);
643                         pp->tx_pcap[i] = NULL;
644                 }
645         }
646
647         for (i = 0; i < dev->data->nb_rx_queues; i++) {
648                 if (pp->rx_pcap[i] != NULL) {
649                         pcap_close(pp->rx_pcap[i]);
650                         pp->rx_pcap[i] = NULL;
651                 }
652         }
653
654 status_down:
655         for (i = 0; i < dev->data->nb_rx_queues; i++)
656                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
657
658         for (i = 0; i < dev->data->nb_tx_queues; i++)
659                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
660
661         dev->data->dev_link.link_status = ETH_LINK_DOWN;
662 }
663
664 static int
665 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
666 {
667         return 0;
668 }
669
670 static void
671 eth_dev_info(struct rte_eth_dev *dev,
672                 struct rte_eth_dev_info *dev_info)
673 {
674         struct pmd_internals *internals = dev->data->dev_private;
675
676         dev_info->if_index = internals->if_index;
677         dev_info->max_mac_addrs = 1;
678         dev_info->max_rx_pktlen = (uint32_t) -1;
679         dev_info->max_rx_queues = dev->data->nb_rx_queues;
680         dev_info->max_tx_queues = dev->data->nb_tx_queues;
681         dev_info->min_rx_bufsize = 0;
682 }
683
684 static int
685 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
686 {
687         unsigned int i;
688         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
689         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
690         unsigned long tx_packets_err_total = 0;
691         const struct pmd_internals *internal = dev->data->dev_private;
692
693         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
694                         i < dev->data->nb_rx_queues; i++) {
695                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
696                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
697                 rx_packets_total += stats->q_ipackets[i];
698                 rx_bytes_total += stats->q_ibytes[i];
699         }
700
701         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
702                         i < dev->data->nb_tx_queues; i++) {
703                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
704                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
705                 tx_packets_total += stats->q_opackets[i];
706                 tx_bytes_total += stats->q_obytes[i];
707                 tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
708         }
709
710         stats->ipackets = rx_packets_total;
711         stats->ibytes = rx_bytes_total;
712         stats->opackets = tx_packets_total;
713         stats->obytes = tx_bytes_total;
714         stats->oerrors = tx_packets_err_total;
715
716         return 0;
717 }
718
719 static void
720 eth_stats_reset(struct rte_eth_dev *dev)
721 {
722         unsigned int i;
723         struct pmd_internals *internal = dev->data->dev_private;
724
725         for (i = 0; i < dev->data->nb_rx_queues; i++) {
726                 internal->rx_queue[i].rx_stat.pkts = 0;
727                 internal->rx_queue[i].rx_stat.bytes = 0;
728         }
729
730         for (i = 0; i < dev->data->nb_tx_queues; i++) {
731                 internal->tx_queue[i].tx_stat.pkts = 0;
732                 internal->tx_queue[i].tx_stat.bytes = 0;
733                 internal->tx_queue[i].tx_stat.err_pkts = 0;
734         }
735 }
736
737 static void
738 eth_dev_close(struct rte_eth_dev *dev)
739 {
740         unsigned int i;
741         struct pmd_internals *internals = dev->data->dev_private;
742
743         /* Device wide flag, but cleanup must be performed per queue. */
744         if (internals->infinite_rx) {
745                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
746                         struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
747                         struct rte_mbuf *pcap_buf;
748
749                         while (!rte_ring_dequeue(pcap_q->pkts,
750                                         (void **)&pcap_buf))
751                                 rte_pktmbuf_free(pcap_buf);
752
753                         rte_ring_free(pcap_q->pkts);
754                 }
755         }
756
757 }
758
759 static void
760 eth_queue_release(void *q __rte_unused)
761 {
762 }
763
764 static int
765 eth_link_update(struct rte_eth_dev *dev __rte_unused,
766                 int wait_to_complete __rte_unused)
767 {
768         return 0;
769 }
770
771 static int
772 eth_rx_queue_setup(struct rte_eth_dev *dev,
773                 uint16_t rx_queue_id,
774                 uint16_t nb_rx_desc __rte_unused,
775                 unsigned int socket_id __rte_unused,
776                 const struct rte_eth_rxconf *rx_conf __rte_unused,
777                 struct rte_mempool *mb_pool)
778 {
779         struct pmd_internals *internals = dev->data->dev_private;
780         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
781
782         pcap_q->mb_pool = mb_pool;
783         pcap_q->port_id = dev->data->port_id;
784         pcap_q->queue_id = rx_queue_id;
785         dev->data->rx_queues[rx_queue_id] = pcap_q;
786
787         if (internals->infinite_rx) {
788                 struct pmd_process_private *pp;
789                 char ring_name[NAME_MAX];
790                 static uint32_t ring_number;
791                 uint64_t pcap_pkt_count = 0;
792                 struct rte_mbuf *bufs[1];
793                 pcap_t **pcap;
794
795                 pp = rte_eth_devices[pcap_q->port_id].process_private;
796                 pcap = &pp->rx_pcap[pcap_q->queue_id];
797
798                 if (unlikely(*pcap == NULL))
799                         return -ENOENT;
800
801                 pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
802
803                 snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
804                                 ring_number);
805
806                 pcap_q->pkts = rte_ring_create(ring_name,
807                                 rte_align64pow2(pcap_pkt_count + 1), 0,
808                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
809                 ring_number++;
810                 if (!pcap_q->pkts)
811                         return -ENOENT;
812
813                 /* Fill ring with packets from PCAP file one by one. */
814                 while (eth_pcap_rx(pcap_q, bufs, 1)) {
815                         /* Check for multiseg mbufs. */
816                         if (bufs[0]->nb_segs != 1) {
817                                 rte_pktmbuf_free(*bufs);
818
819                                 while (!rte_ring_dequeue(pcap_q->pkts,
820                                                 (void **)bufs))
821                                         rte_pktmbuf_free(*bufs);
822
823                                 rte_ring_free(pcap_q->pkts);
824                                 PMD_LOG(ERR, "Multiseg mbufs are not supported in infinite_rx "
825                                                 "mode.");
826                                 return -EINVAL;
827                         }
828
829                         rte_ring_enqueue_bulk(pcap_q->pkts,
830                                         (void * const *)bufs, 1, NULL);
831                 }
832                 /*
833                  * Reset the stats for this queue since eth_pcap_rx calls above
834                  * didn't result in the application receiving packets.
835                  */
836                 pcap_q->rx_stat.pkts = 0;
837                 pcap_q->rx_stat.bytes = 0;
838         }
839
840         return 0;
841 }
842
843 static int
844 eth_tx_queue_setup(struct rte_eth_dev *dev,
845                 uint16_t tx_queue_id,
846                 uint16_t nb_tx_desc __rte_unused,
847                 unsigned int socket_id __rte_unused,
848                 const struct rte_eth_txconf *tx_conf __rte_unused)
849 {
850         struct pmd_internals *internals = dev->data->dev_private;
851         struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
852
853         pcap_q->port_id = dev->data->port_id;
854         pcap_q->queue_id = tx_queue_id;
855         dev->data->tx_queues[tx_queue_id] = pcap_q;
856
857         return 0;
858 }
859
860 static int
861 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
862 {
863         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
864
865         return 0;
866 }
867
868 static int
869 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
870 {
871         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
872
873         return 0;
874 }
875
876 static int
877 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
878 {
879         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
880
881         return 0;
882 }
883
884 static int
885 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
886 {
887         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
888
889         return 0;
890 }
891
892 static const struct eth_dev_ops ops = {
893         .dev_start = eth_dev_start,
894         .dev_stop = eth_dev_stop,
895         .dev_close = eth_dev_close,
896         .dev_configure = eth_dev_configure,
897         .dev_infos_get = eth_dev_info,
898         .rx_queue_setup = eth_rx_queue_setup,
899         .tx_queue_setup = eth_tx_queue_setup,
900         .rx_queue_start = eth_rx_queue_start,
901         .tx_queue_start = eth_tx_queue_start,
902         .rx_queue_stop = eth_rx_queue_stop,
903         .tx_queue_stop = eth_tx_queue_stop,
904         .rx_queue_release = eth_queue_release,
905         .tx_queue_release = eth_queue_release,
906         .link_update = eth_link_update,
907         .stats_get = eth_stats_get,
908         .stats_reset = eth_stats_reset,
909 };
910
911 static int
912 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
913                 pcap_t *pcap, pcap_dumper_t *dumper)
914 {
915         if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
916                 return -1;
917         if (pcap)
918                 pmd->queue[pmd->num_of_queue].pcap = pcap;
919         if (dumper)
920                 pmd->queue[pmd->num_of_queue].dumper = dumper;
921         pmd->queue[pmd->num_of_queue].name = name;
922         pmd->queue[pmd->num_of_queue].type = type;
923         pmd->num_of_queue++;
924         return 0;
925 }
926
927 /*
928  * Function handler that opens the pcap file for reading a stores a
929  * reference of it for use it later on.
930  */
931 static int
932 open_rx_pcap(const char *key, const char *value, void *extra_args)
933 {
934         const char *pcap_filename = value;
935         struct pmd_devargs *rx = extra_args;
936         pcap_t *pcap = NULL;
937
938         if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
939                 return -1;
940
941         if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
942                 pcap_close(pcap);
943                 return -1;
944         }
945
946         return 0;
947 }
948
949 /*
950  * Opens a pcap file for writing and stores a reference to it
951  * for use it later on.
952  */
953 static int
954 open_tx_pcap(const char *key, const char *value, void *extra_args)
955 {
956         const char *pcap_filename = value;
957         struct pmd_devargs *dumpers = extra_args;
958         pcap_dumper_t *dumper;
959
960         if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
961                 return -1;
962
963         if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
964                 pcap_dump_close(dumper);
965                 return -1;
966         }
967
968         return 0;
969 }
970
971 /*
972  * Opens an interface for reading and writing
973  */
974 static inline int
975 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
976 {
977         const char *iface = value;
978         struct pmd_devargs *tx = extra_args;
979         pcap_t *pcap = NULL;
980
981         if (open_single_iface(iface, &pcap) < 0)
982                 return -1;
983
984         tx->queue[0].pcap = pcap;
985         tx->queue[0].name = iface;
986         tx->queue[0].type = key;
987
988         return 0;
989 }
990
991 static inline int
992 set_iface_direction(const char *iface, pcap_t *pcap,
993                 pcap_direction_t direction)
994 {
995         const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
996         if (pcap_setdirection(pcap, direction) < 0) {
997                 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
998                                 iface, direction_str, pcap_geterr(pcap));
999                 return -1;
1000         }
1001         PMD_LOG(INFO, "Setting %s pcap direction %s\n",
1002                         iface, direction_str);
1003         return 0;
1004 }
1005
1006 static inline int
1007 open_iface(const char *key, const char *value, void *extra_args)
1008 {
1009         const char *iface = value;
1010         struct pmd_devargs *pmd = extra_args;
1011         pcap_t *pcap = NULL;
1012
1013         if (open_single_iface(iface, &pcap) < 0)
1014                 return -1;
1015         if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1016                 pcap_close(pcap);
1017                 return -1;
1018         }
1019
1020         return 0;
1021 }
1022
1023 /*
1024  * Opens a NIC for reading packets from it
1025  */
1026 static inline int
1027 open_rx_iface(const char *key, const char *value, void *extra_args)
1028 {
1029         int ret = open_iface(key, value, extra_args);
1030         if (ret < 0)
1031                 return ret;
1032         if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1033                 struct pmd_devargs *pmd = extra_args;
1034                 unsigned int qid = pmd->num_of_queue - 1;
1035
1036                 set_iface_direction(pmd->queue[qid].name,
1037                                 pmd->queue[qid].pcap,
1038                                 PCAP_D_IN);
1039         }
1040
1041         return 0;
1042 }
1043
1044 static inline int
1045 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1046 {
1047         if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1048                         strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1049                 return open_rx_iface(key, value, extra_args);
1050
1051         return 0;
1052 }
1053
1054 /*
1055  * Opens a NIC for writing packets to it
1056  */
1057 static int
1058 open_tx_iface(const char *key, const char *value, void *extra_args)
1059 {
1060         return open_iface(key, value, extra_args);
1061 }
1062
1063 static int
1064 select_phy_mac(const char *key __rte_unused, const char *value,
1065                 void *extra_args)
1066 {
1067         if (extra_args) {
1068                 const int phy_mac = atoi(value);
1069                 int *enable_phy_mac = extra_args;
1070
1071                 if (phy_mac)
1072                         *enable_phy_mac = 1;
1073         }
1074         return 0;
1075 }
1076
1077 static int
1078 get_infinite_rx_arg(const char *key __rte_unused,
1079                 const char *value, void *extra_args)
1080 {
1081         if (extra_args) {
1082                 const int infinite_rx = atoi(value);
1083                 int *enable_infinite_rx = extra_args;
1084
1085                 if (infinite_rx > 0)
1086                         *enable_infinite_rx = 1;
1087         }
1088         return 0;
1089 }
1090
1091 static int
1092 pmd_init_internals(struct rte_vdev_device *vdev,
1093                 const unsigned int nb_rx_queues,
1094                 const unsigned int nb_tx_queues,
1095                 struct pmd_internals **internals,
1096                 struct rte_eth_dev **eth_dev)
1097 {
1098         struct rte_eth_dev_data *data;
1099         struct pmd_process_private *pp;
1100         unsigned int numa_node = vdev->device.numa_node;
1101
1102         PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1103                 numa_node);
1104
1105         pp = (struct pmd_process_private *)
1106                 rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1107                                 RTE_CACHE_LINE_SIZE);
1108
1109         if (pp == NULL) {
1110                 PMD_LOG(ERR,
1111                         "Failed to allocate memory for process private");
1112                 return -1;
1113         }
1114
1115         /* reserve an ethdev entry */
1116         *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1117         if (!(*eth_dev)) {
1118                 rte_free(pp);
1119                 return -1;
1120         }
1121         (*eth_dev)->process_private = pp;
1122         /* now put it all together
1123          * - store queue data in internals,
1124          * - store numa_node info in eth_dev
1125          * - point eth_dev_data to internals
1126          * - and point eth_dev structure to new eth_dev_data structure
1127          */
1128         *internals = (*eth_dev)->data->dev_private;
1129         /*
1130          * Interface MAC = 02:70:63:61:70:<iface_idx>
1131          * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1132          * where the middle 4 characters are converted to hex.
1133          */
1134         (*internals)->eth_addr = (struct rte_ether_addr) {
1135                 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1136         };
1137         (*internals)->phy_mac = 0;
1138         data = (*eth_dev)->data;
1139         data->nb_rx_queues = (uint16_t)nb_rx_queues;
1140         data->nb_tx_queues = (uint16_t)nb_tx_queues;
1141         data->dev_link = pmd_link;
1142         data->mac_addrs = &(*internals)->eth_addr;
1143
1144         /*
1145          * NOTE: we'll replace the data element, of originally allocated
1146          * eth_dev so the rings are local per-process
1147          */
1148         (*eth_dev)->dev_ops = &ops;
1149
1150         strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1151                         ETH_PCAP_ARG_MAXLEN);
1152
1153         return 0;
1154 }
1155
1156 static int
1157 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1158                 const unsigned int numa_node)
1159 {
1160 #if defined(RTE_EXEC_ENV_LINUX)
1161         void *mac_addrs;
1162         struct ifreq ifr;
1163         int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1164
1165         if (if_fd == -1)
1166                 return -1;
1167
1168         rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1169         if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1170                 close(if_fd);
1171                 return -1;
1172         }
1173
1174         mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1175         if (!mac_addrs) {
1176                 close(if_fd);
1177                 return -1;
1178         }
1179
1180         PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1181         eth_dev->data->mac_addrs = mac_addrs;
1182         rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1183                         ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1184
1185         close(if_fd);
1186
1187         return 0;
1188
1189 #elif defined(RTE_EXEC_ENV_FREEBSD)
1190         void *mac_addrs;
1191         struct if_msghdr *ifm;
1192         struct sockaddr_dl *sdl;
1193         int mib[6];
1194         size_t len = 0;
1195         char *buf;
1196
1197         mib[0] = CTL_NET;
1198         mib[1] = AF_ROUTE;
1199         mib[2] = 0;
1200         mib[3] = AF_LINK;
1201         mib[4] = NET_RT_IFLIST;
1202         mib[5] = if_nametoindex(if_name);
1203
1204         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1205                 return -1;
1206
1207         if (len == 0)
1208                 return -1;
1209
1210         buf = rte_malloc(NULL, len, 0);
1211         if (!buf)
1212                 return -1;
1213
1214         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1215                 rte_free(buf);
1216                 return -1;
1217         }
1218         ifm = (struct if_msghdr *)buf;
1219         sdl = (struct sockaddr_dl *)(ifm + 1);
1220
1221         mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1222         if (!mac_addrs) {
1223                 rte_free(buf);
1224                 return -1;
1225         }
1226
1227         PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1228         eth_dev->data->mac_addrs = mac_addrs;
1229         rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1230                         LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1231
1232         rte_free(buf);
1233
1234         return 0;
1235 #else
1236         return -1;
1237 #endif
1238 }
1239
1240 static int
1241 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1242                 struct pmd_devargs_all *devargs_all,
1243                 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1244 {
1245         struct pmd_process_private *pp;
1246         struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1247         struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1248         const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1249         const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1250         unsigned int i;
1251
1252         /* do some parameter checking */
1253         if (rx_queues == NULL && nb_rx_queues > 0)
1254                 return -1;
1255         if (tx_queues == NULL && nb_tx_queues > 0)
1256                 return -1;
1257
1258         if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1259                         eth_dev) < 0)
1260                 return -1;
1261
1262         pp = (*eth_dev)->process_private;
1263         for (i = 0; i < nb_rx_queues; i++) {
1264                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1265                 struct devargs_queue *queue = &rx_queues->queue[i];
1266
1267                 pp->rx_pcap[i] = queue->pcap;
1268                 strlcpy(rx->name, queue->name, sizeof(rx->name));
1269                 strlcpy(rx->type, queue->type, sizeof(rx->type));
1270         }
1271
1272         for (i = 0; i < nb_tx_queues; i++) {
1273                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1274                 struct devargs_queue *queue = &tx_queues->queue[i];
1275
1276                 pp->tx_dumper[i] = queue->dumper;
1277                 pp->tx_pcap[i] = queue->pcap;
1278                 strlcpy(tx->name, queue->name, sizeof(tx->name));
1279                 strlcpy(tx->type, queue->type, sizeof(tx->type));
1280         }
1281
1282         return 0;
1283 }
1284
1285 static int
1286 eth_from_pcaps(struct rte_vdev_device *vdev,
1287                 struct pmd_devargs_all *devargs_all)
1288 {
1289         struct pmd_internals *internals = NULL;
1290         struct rte_eth_dev *eth_dev = NULL;
1291         struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1292         int single_iface = devargs_all->single_iface;
1293         unsigned int infinite_rx = devargs_all->infinite_rx;
1294         int ret;
1295
1296         ret = eth_from_pcaps_common(vdev, devargs_all, &internals, &eth_dev);
1297
1298         if (ret < 0)
1299                 return ret;
1300
1301         /* store weather we are using a single interface for rx/tx or not */
1302         internals->single_iface = single_iface;
1303
1304         if (single_iface) {
1305                 internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1306
1307                 /* phy_mac arg is applied only only if "iface" devarg is provided */
1308                 if (rx_queues->phy_mac) {
1309                         int ret = eth_pcap_update_mac(rx_queues->queue[0].name,
1310                                         eth_dev, vdev->device.numa_node);
1311                         if (ret == 0)
1312                                 internals->phy_mac = 1;
1313                 }
1314         }
1315
1316         internals->infinite_rx = infinite_rx;
1317         /* Assign rx ops. */
1318         if (infinite_rx)
1319                 eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1320         else if (devargs_all->is_rx_pcap || devargs_all->is_rx_iface ||
1321                         single_iface)
1322                 eth_dev->rx_pkt_burst = eth_pcap_rx;
1323         else
1324                 eth_dev->rx_pkt_burst = eth_null_rx;
1325
1326         /* Assign tx ops. */
1327         if (devargs_all->is_tx_pcap)
1328                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1329         else if (devargs_all->is_tx_iface || single_iface)
1330                 eth_dev->tx_pkt_burst = eth_pcap_tx;
1331         else
1332                 eth_dev->tx_pkt_burst = eth_tx_drop;
1333
1334         rte_eth_dev_probing_finish(eth_dev);
1335         return 0;
1336 }
1337
1338 static int
1339 pmd_pcap_probe(struct rte_vdev_device *dev)
1340 {
1341         const char *name;
1342         struct rte_kvargs *kvlist;
1343         struct pmd_devargs pcaps = {0};
1344         struct pmd_devargs dumpers = {0};
1345         struct rte_eth_dev *eth_dev =  NULL;
1346         struct pmd_internals *internal;
1347         int ret = 0;
1348
1349         struct pmd_devargs_all devargs_all = {
1350                 .single_iface = 0,
1351                 .is_tx_pcap = 0,
1352                 .is_tx_iface = 0,
1353                 .infinite_rx = 0,
1354         };
1355
1356         name = rte_vdev_device_name(dev);
1357         PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1358
1359         gettimeofday(&start_time, NULL);
1360         start_cycles = rte_get_timer_cycles();
1361         hz = rte_get_timer_hz();
1362
1363         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1364                 eth_dev = rte_eth_dev_attach_secondary(name);
1365                 if (!eth_dev) {
1366                         PMD_LOG(ERR, "Failed to probe %s", name);
1367                         return -1;
1368                 }
1369
1370                 internal = eth_dev->data->dev_private;
1371
1372                 kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1373                 if (kvlist == NULL)
1374                         return -1;
1375         } else {
1376                 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1377                                 valid_arguments);
1378                 if (kvlist == NULL)
1379                         return -1;
1380         }
1381
1382         /*
1383          * If iface argument is passed we open the NICs and use them for
1384          * reading / writing
1385          */
1386         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1387
1388                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1389                                 &open_rx_tx_iface, &pcaps);
1390                 if (ret < 0)
1391                         goto free_kvlist;
1392
1393                 dumpers.queue[0] = pcaps.queue[0];
1394
1395                 ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1396                                 &select_phy_mac, &pcaps.phy_mac);
1397                 if (ret < 0)
1398                         goto free_kvlist;
1399
1400                 dumpers.phy_mac = pcaps.phy_mac;
1401
1402                 devargs_all.single_iface = 1;
1403                 pcaps.num_of_queue = 1;
1404                 dumpers.num_of_queue = 1;
1405
1406                 goto create_eth;
1407         }
1408
1409         /*
1410          * We check whether we want to open a RX stream from a real NIC, a
1411          * pcap file or open a dummy RX stream
1412          */
1413         devargs_all.is_rx_pcap =
1414                 rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1415         devargs_all.is_rx_iface =
1416                 rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_ARG) ? 1 : 0;
1417         pcaps.num_of_queue = 0;
1418
1419         devargs_all.is_tx_pcap =
1420                 rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1421         devargs_all.is_tx_iface =
1422                 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1423         dumpers.num_of_queue = 0;
1424
1425         if (devargs_all.is_rx_pcap) {
1426                 /*
1427                  * We check whether we want to infinitely rx the pcap file.
1428                  */
1429                 unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1430                                 ETH_PCAP_INFINITE_RX_ARG);
1431
1432                 if (infinite_rx_arg_cnt == 1) {
1433                         ret = rte_kvargs_process(kvlist,
1434                                         ETH_PCAP_INFINITE_RX_ARG,
1435                                         &get_infinite_rx_arg,
1436                                         &devargs_all.infinite_rx);
1437                         if (ret < 0)
1438                                 goto free_kvlist;
1439                         PMD_LOG(INFO, "infinite_rx has been %s for %s",
1440                                         devargs_all.infinite_rx ? "enabled" : "disabled",
1441                                         name);
1442
1443                 } else if (infinite_rx_arg_cnt > 1) {
1444                         PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1445                                         "argument has been provided more than once "
1446                                         "for %s", name);
1447                 }
1448
1449                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1450                                 &open_rx_pcap, &pcaps);
1451         } else if (devargs_all.is_rx_iface) {
1452                 ret = rte_kvargs_process(kvlist, NULL,
1453                                 &rx_iface_args_process, &pcaps);
1454         } else if (devargs_all.is_tx_iface || devargs_all.is_tx_pcap) {
1455                 unsigned int i;
1456
1457                 /* Count number of tx queue args passed before dummy rx queue
1458                  * creation so a dummy rx queue can be created for each tx queue
1459                  */
1460                 unsigned int num_tx_queues =
1461                         (rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) +
1462                         rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG));
1463
1464                 PMD_LOG(INFO, "Creating null rx queue since no rx queues were provided.");
1465
1466                 /* Creating a dummy rx queue for each tx queue passed */
1467                 for (i = 0; i < num_tx_queues; i++)
1468                         ret = add_queue(&pcaps, "dummy_rx", "rx_null", NULL,
1469                                         NULL);
1470         } else {
1471                 PMD_LOG(ERR, "Error - No rx or tx queues provided");
1472                 ret = -ENOENT;
1473         }
1474         if (ret < 0)
1475                 goto free_kvlist;
1476
1477         /*
1478          * We check whether we want to open a TX stream to a real NIC,
1479          * a pcap file, or drop packets on tx
1480          */
1481         if (devargs_all.is_tx_pcap) {
1482                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1483                                 &open_tx_pcap, &dumpers);
1484         } else if (devargs_all.is_tx_iface) {
1485                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1486                                 &open_tx_iface, &dumpers);
1487         } else {
1488                 unsigned int i;
1489
1490                 PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1491
1492                 /* Add 1 dummy queue per rxq which counts and drops packets. */
1493                 for (i = 0; i < pcaps.num_of_queue; i++)
1494                         ret = add_queue(&dumpers, "dummy_tx", "tx_drop", NULL,
1495                                         NULL);
1496         }
1497
1498         if (ret < 0)
1499                 goto free_kvlist;
1500
1501 create_eth:
1502         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1503                 struct pmd_process_private *pp;
1504                 unsigned int i;
1505
1506                 internal = eth_dev->data->dev_private;
1507                         pp = (struct pmd_process_private *)
1508                                 rte_zmalloc(NULL,
1509                                         sizeof(struct pmd_process_private),
1510                                         RTE_CACHE_LINE_SIZE);
1511
1512                 if (pp == NULL) {
1513                         PMD_LOG(ERR,
1514                                 "Failed to allocate memory for process private");
1515                         ret = -1;
1516                         goto free_kvlist;
1517                 }
1518
1519                 eth_dev->dev_ops = &ops;
1520                 eth_dev->device = &dev->device;
1521
1522                 /* setup process private */
1523                 for (i = 0; i < pcaps.num_of_queue; i++)
1524                         pp->rx_pcap[i] = pcaps.queue[i].pcap;
1525
1526                 for (i = 0; i < dumpers.num_of_queue; i++) {
1527                         pp->tx_dumper[i] = dumpers.queue[i].dumper;
1528                         pp->tx_pcap[i] = dumpers.queue[i].pcap;
1529                 }
1530
1531                 eth_dev->process_private = pp;
1532                 eth_dev->rx_pkt_burst = eth_pcap_rx;
1533                 if (devargs_all.is_tx_pcap)
1534                         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1535                 else
1536                         eth_dev->tx_pkt_burst = eth_pcap_tx;
1537
1538                 rte_eth_dev_probing_finish(eth_dev);
1539                 goto free_kvlist;
1540         }
1541
1542         devargs_all.rx_queues = pcaps;
1543         devargs_all.tx_queues = dumpers;
1544
1545         ret = eth_from_pcaps(dev, &devargs_all);
1546
1547 free_kvlist:
1548         rte_kvargs_free(kvlist);
1549
1550         return ret;
1551 }
1552
1553 static int
1554 pmd_pcap_remove(struct rte_vdev_device *dev)
1555 {
1556         struct pmd_internals *internals = NULL;
1557         struct rte_eth_dev *eth_dev = NULL;
1558
1559         PMD_LOG(INFO, "Closing pcap ethdev on numa socket %d",
1560                         rte_socket_id());
1561
1562         if (!dev)
1563                 return -1;
1564
1565         /* reserve an ethdev entry */
1566         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1567         if (eth_dev == NULL)
1568                 return -1;
1569
1570         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1571                 internals = eth_dev->data->dev_private;
1572                 if (internals != NULL && internals->phy_mac == 0)
1573                         /* not dynamically allocated, must not be freed */
1574                         eth_dev->data->mac_addrs = NULL;
1575         }
1576
1577         eth_dev_close(eth_dev);
1578
1579         rte_free(eth_dev->process_private);
1580         rte_eth_dev_release_port(eth_dev);
1581
1582         return 0;
1583 }
1584
1585 static struct rte_vdev_driver pmd_pcap_drv = {
1586         .probe = pmd_pcap_probe,
1587         .remove = pmd_pcap_remove,
1588 };
1589
1590 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1591 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1592 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1593         ETH_PCAP_RX_PCAP_ARG "=<string> "
1594         ETH_PCAP_TX_PCAP_ARG "=<string> "
1595         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1596         ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1597         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1598         ETH_PCAP_IFACE_ARG "=<ifc> "
1599         ETH_PCAP_PHY_MAC_ARG "=<int>"
1600         ETH_PCAP_INFINITE_RX_ARG "=<0|1>");
1601
1602 RTE_INIT(eth_pcap_init_log)
1603 {
1604         eth_pcap_logtype = rte_log_register("pmd.net.pcap");
1605         if (eth_pcap_logtype >= 0)
1606                 rte_log_set_level(eth_pcap_logtype, RTE_LOG_NOTICE);
1607 }