drivers/net: build Intel NIC PMDs with meson
[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
11 #include <pcap.h>
12
13 #include <rte_cycles.h>
14 #include <rte_ethdev_driver.h>
15 #include <rte_ethdev_vdev.h>
16 #include <rte_kvargs.h>
17 #include <rte_malloc.h>
18 #include <rte_mbuf.h>
19 #include <rte_bus_vdev.h>
20
21 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
22 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
23 #define RTE_ETH_PCAP_PROMISC 1
24 #define RTE_ETH_PCAP_TIMEOUT -1
25
26 #define ETH_PCAP_RX_PCAP_ARG  "rx_pcap"
27 #define ETH_PCAP_TX_PCAP_ARG  "tx_pcap"
28 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
29 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
30 #define ETH_PCAP_IFACE_ARG    "iface"
31
32 #define ETH_PCAP_ARG_MAXLEN     64
33
34 #define RTE_PMD_PCAP_MAX_QUEUES 16
35
36 static char errbuf[PCAP_ERRBUF_SIZE];
37 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
38 static struct timeval start_time;
39 static uint64_t start_cycles;
40 static uint64_t hz;
41
42 struct queue_stat {
43         volatile unsigned long pkts;
44         volatile unsigned long bytes;
45         volatile unsigned long err_pkts;
46 };
47
48 struct pcap_rx_queue {
49         pcap_t *pcap;
50         uint16_t in_port;
51         struct rte_mempool *mb_pool;
52         struct queue_stat rx_stat;
53         char name[PATH_MAX];
54         char type[ETH_PCAP_ARG_MAXLEN];
55 };
56
57 struct pcap_tx_queue {
58         pcap_dumper_t *dumper;
59         pcap_t *pcap;
60         struct queue_stat tx_stat;
61         char name[PATH_MAX];
62         char type[ETH_PCAP_ARG_MAXLEN];
63 };
64
65 struct pmd_internals {
66         struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
67         struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
68         int if_index;
69         int single_iface;
70 };
71
72 struct pmd_devargs {
73         unsigned int num_of_queue;
74         struct devargs_queue {
75                 pcap_dumper_t *dumper;
76                 pcap_t *pcap;
77                 const char *name;
78                 const char *type;
79         } queue[RTE_PMD_PCAP_MAX_QUEUES];
80 };
81
82 static const char *valid_arguments[] = {
83         ETH_PCAP_RX_PCAP_ARG,
84         ETH_PCAP_TX_PCAP_ARG,
85         ETH_PCAP_RX_IFACE_ARG,
86         ETH_PCAP_TX_IFACE_ARG,
87         ETH_PCAP_IFACE_ARG,
88         NULL
89 };
90
91 static struct ether_addr eth_addr = {
92         .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 }
93 };
94
95 static struct rte_eth_link pmd_link = {
96                 .link_speed = ETH_SPEED_NUM_10G,
97                 .link_duplex = ETH_LINK_FULL_DUPLEX,
98                 .link_status = ETH_LINK_DOWN,
99                 .link_autoneg = ETH_LINK_AUTONEG,
100 };
101
102 static int
103 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
104                 const u_char *data, uint16_t data_len)
105 {
106         /* Copy the first segment. */
107         uint16_t len = rte_pktmbuf_tailroom(mbuf);
108         struct rte_mbuf *m = mbuf;
109
110         rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
111         data_len -= len;
112         data += len;
113
114         while (data_len > 0) {
115                 /* Allocate next mbuf and point to that. */
116                 m->next = rte_pktmbuf_alloc(mb_pool);
117
118                 if (unlikely(!m->next))
119                         return -1;
120
121                 m = m->next;
122
123                 /* Headroom is not needed in chained mbufs. */
124                 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
125                 m->pkt_len = 0;
126                 m->data_len = 0;
127
128                 /* Copy next segment. */
129                 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
130                 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
131
132                 mbuf->nb_segs++;
133                 data_len -= len;
134                 data += len;
135         }
136
137         return mbuf->nb_segs;
138 }
139
140 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
141 static void
142 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
143 {
144         uint16_t data_len = 0;
145
146         while (mbuf) {
147                 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
148                         mbuf->data_len);
149
150                 data_len += mbuf->data_len;
151                 mbuf = mbuf->next;
152         }
153 }
154
155 static uint16_t
156 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
157 {
158         unsigned int i;
159         struct pcap_pkthdr header;
160         const u_char *packet;
161         struct rte_mbuf *mbuf;
162         struct pcap_rx_queue *pcap_q = queue;
163         uint16_t num_rx = 0;
164         uint16_t buf_size;
165         uint32_t rx_bytes = 0;
166
167         if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
168                 return 0;
169
170         /* Reads the given number of packets from the pcap file one by one
171          * and copies the packet data into a newly allocated mbuf to return.
172          */
173         for (i = 0; i < nb_pkts; i++) {
174                 /* Get the next PCAP packet */
175                 packet = pcap_next(pcap_q->pcap, &header);
176                 if (unlikely(packet == NULL))
177                         break;
178
179                 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
180                 if (unlikely(mbuf == NULL))
181                         break;
182
183                 /* Now get the space available for data in the mbuf */
184                 buf_size = rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
185                                 RTE_PKTMBUF_HEADROOM;
186
187                 if (header.caplen <= buf_size) {
188                         /* pcap packet will fit in the mbuf, can copy it */
189                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
190                                         header.caplen);
191                         mbuf->data_len = (uint16_t)header.caplen;
192                 } else {
193                         /* Try read jumbo frame into multi mbufs. */
194                         if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
195                                                        mbuf,
196                                                        packet,
197                                                        header.caplen) == -1)) {
198                                 rte_pktmbuf_free(mbuf);
199                                 break;
200                         }
201                 }
202
203                 mbuf->pkt_len = (uint16_t)header.caplen;
204                 mbuf->port = pcap_q->in_port;
205                 bufs[num_rx] = mbuf;
206                 num_rx++;
207                 rx_bytes += header.caplen;
208         }
209         pcap_q->rx_stat.pkts += num_rx;
210         pcap_q->rx_stat.bytes += rx_bytes;
211
212         return num_rx;
213 }
214
215 static inline void
216 calculate_timestamp(struct timeval *ts) {
217         uint64_t cycles;
218         struct timeval cur_time;
219
220         cycles = rte_get_timer_cycles() - start_cycles;
221         cur_time.tv_sec = cycles / hz;
222         cur_time.tv_usec = (cycles % hz) * 1e6 / hz;
223         timeradd(&start_time, &cur_time, ts);
224 }
225
226 /*
227  * Callback to handle writing packets to a pcap file.
228  */
229 static uint16_t
230 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
231 {
232         unsigned int i;
233         struct rte_mbuf *mbuf;
234         struct pcap_tx_queue *dumper_q = queue;
235         uint16_t num_tx = 0;
236         uint32_t tx_bytes = 0;
237         struct pcap_pkthdr header;
238
239         if (dumper_q->dumper == NULL || nb_pkts == 0)
240                 return 0;
241
242         /* writes the nb_pkts packets to the previously opened pcap file
243          * dumper */
244         for (i = 0; i < nb_pkts; i++) {
245                 mbuf = bufs[i];
246                 calculate_timestamp(&header.ts);
247                 header.len = mbuf->pkt_len;
248                 header.caplen = header.len;
249
250                 if (likely(mbuf->nb_segs == 1)) {
251                         pcap_dump((u_char *)dumper_q->dumper, &header,
252                                   rte_pktmbuf_mtod(mbuf, void*));
253                 } else {
254                         if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
255                                 eth_pcap_gather_data(tx_pcap_data, mbuf);
256                                 pcap_dump((u_char *)dumper_q->dumper, &header,
257                                           tx_pcap_data);
258                         } else {
259                                 RTE_LOG(ERR, PMD,
260                                         "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
261                                         mbuf->pkt_len,
262                                         ETHER_MAX_JUMBO_FRAME_LEN);
263
264                                 rte_pktmbuf_free(mbuf);
265                                 break;
266                         }
267                 }
268
269                 num_tx++;
270                 tx_bytes += mbuf->pkt_len;
271                 rte_pktmbuf_free(mbuf);
272         }
273
274         /*
275          * Since there's no place to hook a callback when the forwarding
276          * process stops and to make sure the pcap file is actually written,
277          * we flush the pcap dumper within each burst.
278          */
279         pcap_dump_flush(dumper_q->dumper);
280         dumper_q->tx_stat.pkts += num_tx;
281         dumper_q->tx_stat.bytes += tx_bytes;
282         dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
283
284         return num_tx;
285 }
286
287 /*
288  * Callback to handle sending packets through a real NIC.
289  */
290 static uint16_t
291 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
292 {
293         unsigned int i;
294         int ret;
295         struct rte_mbuf *mbuf;
296         struct pcap_tx_queue *tx_queue = queue;
297         uint16_t num_tx = 0;
298         uint32_t tx_bytes = 0;
299
300         if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
301                 return 0;
302
303         for (i = 0; i < nb_pkts; i++) {
304                 mbuf = bufs[i];
305
306                 if (likely(mbuf->nb_segs == 1)) {
307                         ret = pcap_sendpacket(tx_queue->pcap,
308                                         rte_pktmbuf_mtod(mbuf, u_char *),
309                                         mbuf->pkt_len);
310                 } else {
311                         if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
312                                 eth_pcap_gather_data(tx_pcap_data, mbuf);
313                                 ret = pcap_sendpacket(tx_queue->pcap,
314                                                 tx_pcap_data, mbuf->pkt_len);
315                         } else {
316                                 RTE_LOG(ERR, PMD,
317                                         "Dropping PCAP packet. Size (%d) > max jumbo size (%d).\n",
318                                         mbuf->pkt_len,
319                                         ETHER_MAX_JUMBO_FRAME_LEN);
320
321                                 rte_pktmbuf_free(mbuf);
322                                 break;
323                         }
324                 }
325
326                 if (unlikely(ret != 0))
327                         break;
328                 num_tx++;
329                 tx_bytes += mbuf->pkt_len;
330                 rte_pktmbuf_free(mbuf);
331         }
332
333         tx_queue->tx_stat.pkts += num_tx;
334         tx_queue->tx_stat.bytes += tx_bytes;
335         tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
336
337         return num_tx;
338 }
339
340 /*
341  * pcap_open_live wrapper function
342  */
343 static inline int
344 open_iface_live(const char *iface, pcap_t **pcap) {
345         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
346                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
347
348         if (*pcap == NULL) {
349                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
350                 return -1;
351         }
352
353         return 0;
354 }
355
356 static int
357 open_single_iface(const char *iface, pcap_t **pcap)
358 {
359         if (open_iface_live(iface, pcap) < 0) {
360                 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
361                 return -1;
362         }
363
364         return 0;
365 }
366
367 static int
368 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
369 {
370         pcap_t *tx_pcap;
371
372         /*
373          * We need to create a dummy empty pcap_t to use it
374          * with pcap_dump_open(). We create big enough an Ethernet
375          * pcap holder.
376          */
377         tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN);
378         if (tx_pcap == NULL) {
379                 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
380                 return -1;
381         }
382
383         /* The dumper is created using the previous pcap_t reference */
384         *dumper = pcap_dump_open(tx_pcap, pcap_filename);
385         if (*dumper == NULL) {
386                 pcap_close(tx_pcap);
387                 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n",
388                         pcap_filename);
389                 return -1;
390         }
391
392         pcap_close(tx_pcap);
393         return 0;
394 }
395
396 static int
397 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
398 {
399         *pcap = pcap_open_offline(pcap_filename, errbuf);
400         if (*pcap == NULL) {
401                 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename,
402                         errbuf);
403                 return -1;
404         }
405
406         return 0;
407 }
408
409 static int
410 eth_dev_start(struct rte_eth_dev *dev)
411 {
412         unsigned int i;
413         struct pmd_internals *internals = dev->data->dev_private;
414         struct pcap_tx_queue *tx;
415         struct pcap_rx_queue *rx;
416
417         /* Special iface case. Single pcap is open and shared between tx/rx. */
418         if (internals->single_iface) {
419                 tx = &internals->tx_queue[0];
420                 rx = &internals->rx_queue[0];
421
422                 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
423                         if (open_single_iface(tx->name, &tx->pcap) < 0)
424                                 return -1;
425                         rx->pcap = tx->pcap;
426                 }
427                 goto status_up;
428         }
429
430         /* If not open already, open tx pcaps/dumpers */
431         for (i = 0; i < dev->data->nb_tx_queues; i++) {
432                 tx = &internals->tx_queue[i];
433
434                 if (!tx->dumper &&
435                                 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
436                         if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
437                                 return -1;
438                 } else if (!tx->pcap &&
439                                 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
440                         if (open_single_iface(tx->name, &tx->pcap) < 0)
441                                 return -1;
442                 }
443         }
444
445         /* If not open already, open rx pcaps */
446         for (i = 0; i < dev->data->nb_rx_queues; i++) {
447                 rx = &internals->rx_queue[i];
448
449                 if (rx->pcap != NULL)
450                         continue;
451
452                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
453                         if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
454                                 return -1;
455                 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
456                         if (open_single_iface(rx->name, &rx->pcap) < 0)
457                                 return -1;
458                 }
459         }
460
461 status_up:
462         dev->data->dev_link.link_status = ETH_LINK_UP;
463
464         return 0;
465 }
466
467 /*
468  * This function gets called when the current port gets stopped.
469  * Is the only place for us to close all the tx streams dumpers.
470  * If not called the dumpers will be flushed within each tx burst.
471  */
472 static void
473 eth_dev_stop(struct rte_eth_dev *dev)
474 {
475         unsigned int i;
476         struct pmd_internals *internals = dev->data->dev_private;
477         struct pcap_tx_queue *tx;
478         struct pcap_rx_queue *rx;
479
480         /* Special iface case. Single pcap is open and shared between tx/rx. */
481         if (internals->single_iface) {
482                 tx = &internals->tx_queue[0];
483                 rx = &internals->rx_queue[0];
484                 pcap_close(tx->pcap);
485                 tx->pcap = NULL;
486                 rx->pcap = NULL;
487                 goto status_down;
488         }
489
490         for (i = 0; i < dev->data->nb_tx_queues; i++) {
491                 tx = &internals->tx_queue[i];
492
493                 if (tx->dumper != NULL) {
494                         pcap_dump_close(tx->dumper);
495                         tx->dumper = NULL;
496                 }
497
498                 if (tx->pcap != NULL) {
499                         pcap_close(tx->pcap);
500                         tx->pcap = NULL;
501                 }
502         }
503
504         for (i = 0; i < dev->data->nb_rx_queues; i++) {
505                 rx = &internals->rx_queue[i];
506
507                 if (rx->pcap != NULL) {
508                         pcap_close(rx->pcap);
509                         rx->pcap = NULL;
510                 }
511         }
512
513 status_down:
514         dev->data->dev_link.link_status = ETH_LINK_DOWN;
515 }
516
517 static int
518 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
519 {
520         return 0;
521 }
522
523 static void
524 eth_dev_info(struct rte_eth_dev *dev,
525                 struct rte_eth_dev_info *dev_info)
526 {
527         struct pmd_internals *internals = dev->data->dev_private;
528
529         dev_info->if_index = internals->if_index;
530         dev_info->max_mac_addrs = 1;
531         dev_info->max_rx_pktlen = (uint32_t) -1;
532         dev_info->max_rx_queues = dev->data->nb_rx_queues;
533         dev_info->max_tx_queues = dev->data->nb_tx_queues;
534         dev_info->min_rx_bufsize = 0;
535 }
536
537 static int
538 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
539 {
540         unsigned int i;
541         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
542         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
543         unsigned long tx_packets_err_total = 0;
544         const struct pmd_internals *internal = dev->data->dev_private;
545
546         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
547                         i < dev->data->nb_rx_queues; i++) {
548                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
549                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
550                 rx_packets_total += stats->q_ipackets[i];
551                 rx_bytes_total += stats->q_ibytes[i];
552         }
553
554         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
555                         i < dev->data->nb_tx_queues; i++) {
556                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
557                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
558                 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
559                 tx_packets_total += stats->q_opackets[i];
560                 tx_bytes_total += stats->q_obytes[i];
561                 tx_packets_err_total += stats->q_errors[i];
562         }
563
564         stats->ipackets = rx_packets_total;
565         stats->ibytes = rx_bytes_total;
566         stats->opackets = tx_packets_total;
567         stats->obytes = tx_bytes_total;
568         stats->oerrors = tx_packets_err_total;
569
570         return 0;
571 }
572
573 static void
574 eth_stats_reset(struct rte_eth_dev *dev)
575 {
576         unsigned int i;
577         struct pmd_internals *internal = dev->data->dev_private;
578
579         for (i = 0; i < dev->data->nb_rx_queues; i++) {
580                 internal->rx_queue[i].rx_stat.pkts = 0;
581                 internal->rx_queue[i].rx_stat.bytes = 0;
582         }
583
584         for (i = 0; i < dev->data->nb_tx_queues; i++) {
585                 internal->tx_queue[i].tx_stat.pkts = 0;
586                 internal->tx_queue[i].tx_stat.bytes = 0;
587                 internal->tx_queue[i].tx_stat.err_pkts = 0;
588         }
589 }
590
591 static void
592 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
593 {
594 }
595
596 static void
597 eth_queue_release(void *q __rte_unused)
598 {
599 }
600
601 static int
602 eth_link_update(struct rte_eth_dev *dev __rte_unused,
603                 int wait_to_complete __rte_unused)
604 {
605         return 0;
606 }
607
608 static int
609 eth_rx_queue_setup(struct rte_eth_dev *dev,
610                 uint16_t rx_queue_id,
611                 uint16_t nb_rx_desc __rte_unused,
612                 unsigned int socket_id __rte_unused,
613                 const struct rte_eth_rxconf *rx_conf __rte_unused,
614                 struct rte_mempool *mb_pool)
615 {
616         struct pmd_internals *internals = dev->data->dev_private;
617         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
618
619         pcap_q->mb_pool = mb_pool;
620         dev->data->rx_queues[rx_queue_id] = pcap_q;
621         pcap_q->in_port = dev->data->port_id;
622
623         return 0;
624 }
625
626 static int
627 eth_tx_queue_setup(struct rte_eth_dev *dev,
628                 uint16_t tx_queue_id,
629                 uint16_t nb_tx_desc __rte_unused,
630                 unsigned int socket_id __rte_unused,
631                 const struct rte_eth_txconf *tx_conf __rte_unused)
632 {
633         struct pmd_internals *internals = dev->data->dev_private;
634
635         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
636
637         return 0;
638 }
639
640 static const struct eth_dev_ops ops = {
641         .dev_start = eth_dev_start,
642         .dev_stop = eth_dev_stop,
643         .dev_close = eth_dev_close,
644         .dev_configure = eth_dev_configure,
645         .dev_infos_get = eth_dev_info,
646         .rx_queue_setup = eth_rx_queue_setup,
647         .tx_queue_setup = eth_tx_queue_setup,
648         .rx_queue_release = eth_queue_release,
649         .tx_queue_release = eth_queue_release,
650         .link_update = eth_link_update,
651         .stats_get = eth_stats_get,
652         .stats_reset = eth_stats_reset,
653 };
654
655 /*
656  * Function handler that opens the pcap file for reading a stores a
657  * reference of it for use it later on.
658  */
659 static int
660 open_rx_pcap(const char *key, const char *value, void *extra_args)
661 {
662         unsigned int i;
663         const char *pcap_filename = value;
664         struct pmd_devargs *rx = extra_args;
665         pcap_t *pcap = NULL;
666
667         for (i = 0; i < rx->num_of_queue; i++) {
668                 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
669                         return -1;
670
671                 rx->queue[i].pcap = pcap;
672                 rx->queue[i].name = pcap_filename;
673                 rx->queue[i].type = key;
674         }
675
676         return 0;
677 }
678
679 /*
680  * Opens a pcap file for writing and stores a reference to it
681  * for use it later on.
682  */
683 static int
684 open_tx_pcap(const char *key, const char *value, void *extra_args)
685 {
686         unsigned int i;
687         const char *pcap_filename = value;
688         struct pmd_devargs *dumpers = extra_args;
689         pcap_dumper_t *dumper;
690
691         for (i = 0; i < dumpers->num_of_queue; i++) {
692                 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
693                         return -1;
694
695                 dumpers->queue[i].dumper = dumper;
696                 dumpers->queue[i].name = pcap_filename;
697                 dumpers->queue[i].type = key;
698         }
699
700         return 0;
701 }
702
703 /*
704  * Opens an interface for reading and writing
705  */
706 static inline int
707 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
708 {
709         const char *iface = value;
710         struct pmd_devargs *tx = extra_args;
711         pcap_t *pcap = NULL;
712
713         if (open_single_iface(iface, &pcap) < 0)
714                 return -1;
715
716         tx->queue[0].pcap = pcap;
717         tx->queue[0].name = iface;
718         tx->queue[0].type = key;
719
720         return 0;
721 }
722
723 /*
724  * Opens a NIC for reading packets from it
725  */
726 static inline int
727 open_rx_iface(const char *key, const char *value, void *extra_args)
728 {
729         unsigned int i;
730         const char *iface = value;
731         struct pmd_devargs *rx = extra_args;
732         pcap_t *pcap = NULL;
733
734         for (i = 0; i < rx->num_of_queue; i++) {
735                 if (open_single_iface(iface, &pcap) < 0)
736                         return -1;
737                 rx->queue[i].pcap = pcap;
738                 rx->queue[i].name = iface;
739                 rx->queue[i].type = key;
740         }
741
742         return 0;
743 }
744
745 /*
746  * Opens a NIC for writing packets to it
747  */
748 static int
749 open_tx_iface(const char *key, const char *value, void *extra_args)
750 {
751         unsigned int i;
752         const char *iface = value;
753         struct pmd_devargs *tx = extra_args;
754         pcap_t *pcap;
755
756         for (i = 0; i < tx->num_of_queue; i++) {
757                 if (open_single_iface(iface, &pcap) < 0)
758                         return -1;
759                 tx->queue[i].pcap = pcap;
760                 tx->queue[i].name = iface;
761                 tx->queue[i].type = key;
762         }
763
764         return 0;
765 }
766
767 static struct rte_vdev_driver pmd_pcap_drv;
768
769 static int
770 pmd_init_internals(struct rte_vdev_device *vdev,
771                 const unsigned int nb_rx_queues,
772                 const unsigned int nb_tx_queues,
773                 struct pmd_internals **internals,
774                 struct rte_eth_dev **eth_dev)
775 {
776         struct rte_eth_dev_data *data = NULL;
777         unsigned int numa_node = vdev->device.numa_node;
778         const char *name;
779
780         name = rte_vdev_device_name(vdev);
781         RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %d\n",
782                 numa_node);
783
784         /* now do all data allocation - for eth_dev structure
785          * and internal (private) data
786          */
787         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
788         if (data == NULL)
789                 return -1;
790
791         /* reserve an ethdev entry */
792         *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
793         if (*eth_dev == NULL) {
794                 rte_free(data);
795                 return -1;
796         }
797
798         /* now put it all together
799          * - store queue data in internals,
800          * - store numa_node info in eth_dev
801          * - point eth_dev_data to internals
802          * - and point eth_dev structure to new eth_dev_data structure
803          */
804         *internals = (*eth_dev)->data->dev_private;
805         rte_memcpy(data, (*eth_dev)->data, sizeof(*data));
806         data->nb_rx_queues = (uint16_t)nb_rx_queues;
807         data->nb_tx_queues = (uint16_t)nb_tx_queues;
808         data->dev_link = pmd_link;
809         data->mac_addrs = &eth_addr;
810
811         /*
812          * NOTE: we'll replace the data element, of originally allocated
813          * eth_dev so the rings are local per-process
814          */
815         (*eth_dev)->data = data;
816         (*eth_dev)->dev_ops = &ops;
817
818         return 0;
819 }
820
821 static int
822 eth_from_pcaps_common(struct rte_vdev_device *vdev,
823                 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
824                 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
825                 struct rte_kvargs *kvlist, struct pmd_internals **internals,
826                 struct rte_eth_dev **eth_dev)
827 {
828         struct rte_kvargs_pair *pair = NULL;
829         unsigned int k_idx;
830         unsigned int i;
831
832         /* do some parameter checking */
833         if (rx_queues == NULL && nb_rx_queues > 0)
834                 return -1;
835         if (tx_queues == NULL && nb_tx_queues > 0)
836                 return -1;
837
838         if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
839                         eth_dev) < 0)
840                 return -1;
841
842         for (i = 0; i < nb_rx_queues; i++) {
843                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
844                 struct devargs_queue *queue = &rx_queues->queue[i];
845
846                 rx->pcap = queue->pcap;
847                 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
848                 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
849         }
850
851         for (i = 0; i < nb_tx_queues; i++) {
852                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
853                 struct devargs_queue *queue = &tx_queues->queue[i];
854
855                 tx->dumper = queue->dumper;
856                 tx->pcap = queue->pcap;
857                 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
858                 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
859         }
860
861         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
862                 pair = &kvlist->pairs[k_idx];
863                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
864                         break;
865         }
866
867         if (pair == NULL)
868                 (*internals)->if_index = 0;
869         else
870                 (*internals)->if_index = if_nametoindex(pair->value);
871
872         return 0;
873 }
874
875 static int
876 eth_from_pcaps(struct rte_vdev_device *vdev,
877                 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
878                 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
879                 struct rte_kvargs *kvlist, int single_iface,
880                 unsigned int using_dumpers)
881 {
882         struct pmd_internals *internals = NULL;
883         struct rte_eth_dev *eth_dev = NULL;
884         int ret;
885
886         ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
887                 tx_queues, nb_tx_queues, kvlist, &internals, &eth_dev);
888
889         if (ret < 0)
890                 return ret;
891
892         /* store weather we are using a single interface for rx/tx or not */
893         internals->single_iface = single_iface;
894
895         eth_dev->rx_pkt_burst = eth_pcap_rx;
896
897         if (using_dumpers)
898                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
899         else
900                 eth_dev->tx_pkt_burst = eth_pcap_tx;
901
902         return 0;
903 }
904
905 static int
906 pmd_pcap_probe(struct rte_vdev_device *dev)
907 {
908         const char *name;
909         unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
910         struct rte_kvargs *kvlist;
911         struct pmd_devargs pcaps = {0};
912         struct pmd_devargs dumpers = {0};
913         int single_iface = 0;
914         int ret;
915
916         name = rte_vdev_device_name(dev);
917         RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
918
919         gettimeofday(&start_time, NULL);
920         start_cycles = rte_get_timer_cycles();
921         hz = rte_get_timer_hz();
922
923         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
924         if (kvlist == NULL)
925                 return -1;
926
927         /*
928          * If iface argument is passed we open the NICs and use them for
929          * reading / writing
930          */
931         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
932
933                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
934                                 &open_rx_tx_iface, &pcaps);
935
936                 if (ret < 0)
937                         goto free_kvlist;
938
939                 dumpers.queue[0] = pcaps.queue[0];
940
941                 single_iface = 1;
942                 pcaps.num_of_queue = 1;
943                 dumpers.num_of_queue = 1;
944
945                 goto create_eth;
946         }
947
948         /*
949          * We check whether we want to open a RX stream from a real NIC or a
950          * pcap file
951          */
952         pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
953         if (pcaps.num_of_queue)
954                 is_rx_pcap = 1;
955         else
956                 pcaps.num_of_queue = rte_kvargs_count(kvlist,
957                                 ETH_PCAP_RX_IFACE_ARG);
958
959         if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
960                 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
961
962         if (is_rx_pcap)
963                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
964                                 &open_rx_pcap, &pcaps);
965         else
966                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
967                                 &open_rx_iface, &pcaps);
968
969         if (ret < 0)
970                 goto free_kvlist;
971
972         /*
973          * We check whether we want to open a TX stream to a real NIC or a
974          * pcap file
975          */
976         dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
977         if (dumpers.num_of_queue)
978                 is_tx_pcap = 1;
979         else
980                 dumpers.num_of_queue = rte_kvargs_count(kvlist,
981                                 ETH_PCAP_TX_IFACE_ARG);
982
983         if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
984                 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
985
986         if (is_tx_pcap)
987                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
988                                 &open_tx_pcap, &dumpers);
989         else
990                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
991                                 &open_tx_iface, &dumpers);
992
993         if (ret < 0)
994                 goto free_kvlist;
995
996 create_eth:
997         ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
998                 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
999
1000 free_kvlist:
1001         rte_kvargs_free(kvlist);
1002
1003         return ret;
1004 }
1005
1006 static int
1007 pmd_pcap_remove(struct rte_vdev_device *dev)
1008 {
1009         struct rte_eth_dev *eth_dev = NULL;
1010
1011         RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %d\n",
1012                         rte_socket_id());
1013
1014         if (!dev)
1015                 return -1;
1016
1017         /* reserve an ethdev entry */
1018         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1019         if (eth_dev == NULL)
1020                 return -1;
1021
1022         rte_free(eth_dev->data->dev_private);
1023         rte_free(eth_dev->data);
1024
1025         rte_eth_dev_release_port(eth_dev);
1026
1027         return 0;
1028 }
1029
1030 static struct rte_vdev_driver pmd_pcap_drv = {
1031         .probe = pmd_pcap_probe,
1032         .remove = pmd_pcap_remove,
1033 };
1034
1035 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1036 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1037 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1038         ETH_PCAP_RX_PCAP_ARG "=<string> "
1039         ETH_PCAP_TX_PCAP_ARG "=<string> "
1040         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1041         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1042         ETH_PCAP_IFACE_ARG "=<ifc>");