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