net/pcap: fix multiple queues
[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 }
542
543 static int
544 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
545 {
546         unsigned int i;
547         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
548         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
549         unsigned long tx_packets_err_total = 0;
550         const struct pmd_internals *internal = dev->data->dev_private;
551
552         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
553                         i < dev->data->nb_rx_queues; i++) {
554                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
555                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
556                 rx_packets_total += stats->q_ipackets[i];
557                 rx_bytes_total += stats->q_ibytes[i];
558         }
559
560         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
561                         i < dev->data->nb_tx_queues; i++) {
562                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
563                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
564                 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
565                 tx_packets_total += stats->q_opackets[i];
566                 tx_bytes_total += stats->q_obytes[i];
567                 tx_packets_err_total += stats->q_errors[i];
568         }
569
570         stats->ipackets = rx_packets_total;
571         stats->ibytes = rx_bytes_total;
572         stats->opackets = tx_packets_total;
573         stats->obytes = tx_bytes_total;
574         stats->oerrors = tx_packets_err_total;
575
576         return 0;
577 }
578
579 static void
580 eth_stats_reset(struct rte_eth_dev *dev)
581 {
582         unsigned int i;
583         struct pmd_internals *internal = dev->data->dev_private;
584
585         for (i = 0; i < dev->data->nb_rx_queues; i++) {
586                 internal->rx_queue[i].rx_stat.pkts = 0;
587                 internal->rx_queue[i].rx_stat.bytes = 0;
588         }
589
590         for (i = 0; i < dev->data->nb_tx_queues; i++) {
591                 internal->tx_queue[i].tx_stat.pkts = 0;
592                 internal->tx_queue[i].tx_stat.bytes = 0;
593                 internal->tx_queue[i].tx_stat.err_pkts = 0;
594         }
595 }
596
597 static void
598 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
599 {
600 }
601
602 static void
603 eth_queue_release(void *q __rte_unused)
604 {
605 }
606
607 static int
608 eth_link_update(struct rte_eth_dev *dev __rte_unused,
609                 int wait_to_complete __rte_unused)
610 {
611         return 0;
612 }
613
614 static int
615 eth_rx_queue_setup(struct rte_eth_dev *dev,
616                 uint16_t rx_queue_id,
617                 uint16_t nb_rx_desc __rte_unused,
618                 unsigned int socket_id __rte_unused,
619                 const struct rte_eth_rxconf *rx_conf __rte_unused,
620                 struct rte_mempool *mb_pool)
621 {
622         struct pmd_internals *internals = dev->data->dev_private;
623         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
624
625         pcap_q->mb_pool = mb_pool;
626         dev->data->rx_queues[rx_queue_id] = pcap_q;
627         pcap_q->in_port = dev->data->port_id;
628
629         return 0;
630 }
631
632 static int
633 eth_tx_queue_setup(struct rte_eth_dev *dev,
634                 uint16_t tx_queue_id,
635                 uint16_t nb_tx_desc __rte_unused,
636                 unsigned int socket_id __rte_unused,
637                 const struct rte_eth_txconf *tx_conf __rte_unused)
638 {
639         struct pmd_internals *internals = dev->data->dev_private;
640
641         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
642
643         return 0;
644 }
645
646 static const struct eth_dev_ops ops = {
647         .dev_start = eth_dev_start,
648         .dev_stop = eth_dev_stop,
649         .dev_close = eth_dev_close,
650         .dev_configure = eth_dev_configure,
651         .dev_infos_get = eth_dev_info,
652         .rx_queue_setup = eth_rx_queue_setup,
653         .tx_queue_setup = eth_tx_queue_setup,
654         .rx_queue_release = eth_queue_release,
655         .tx_queue_release = eth_queue_release,
656         .link_update = eth_link_update,
657         .stats_get = eth_stats_get,
658         .stats_reset = eth_stats_reset,
659 };
660
661 /*
662  * Function handler that opens the pcap file for reading a stores a
663  * reference of it for use it later on.
664  */
665 static int
666 open_rx_pcap(const char *key, const char *value, void *extra_args)
667 {
668         const char *pcap_filename = value;
669         struct pmd_devargs *rx = extra_args;
670         pcap_t *pcap = NULL;
671
672         if (rx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
673                 return -1;
674         if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
675                 return -1;
676
677         rx->queue[rx->num_of_queue].pcap = pcap;
678         rx->queue[rx->num_of_queue].name = pcap_filename;
679         rx->queue[rx->num_of_queue].type = key;
680         rx->num_of_queue++;
681
682         return 0;
683 }
684
685 /*
686  * Opens a pcap file for writing and stores a reference to it
687  * for use it later on.
688  */
689 static int
690 open_tx_pcap(const char *key, const char *value, void *extra_args)
691 {
692         const char *pcap_filename = value;
693         struct pmd_devargs *dumpers = extra_args;
694         pcap_dumper_t *dumper;
695
696         if (dumpers->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
697                 return -1;
698         if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
699                 return -1;
700
701         dumpers->queue[dumpers->num_of_queue].dumper = dumper;
702         dumpers->queue[dumpers->num_of_queue].name = pcap_filename;
703         dumpers->queue[dumpers->num_of_queue].type = key;
704         dumpers->num_of_queue++;
705
706         return 0;
707 }
708
709 /*
710  * Opens an interface for reading and writing
711  */
712 static inline int
713 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
714 {
715         const char *iface = value;
716         struct pmd_devargs *tx = extra_args;
717         pcap_t *pcap = NULL;
718
719         if (open_single_iface(iface, &pcap) < 0)
720                 return -1;
721
722         tx->queue[0].pcap = pcap;
723         tx->queue[0].name = iface;
724         tx->queue[0].type = key;
725
726         return 0;
727 }
728
729 /*
730  * Opens a NIC for reading packets from it
731  */
732 static inline int
733 open_rx_iface(const char *key, const char *value, void *extra_args)
734 {
735         const char *iface = value;
736         struct pmd_devargs *rx = extra_args;
737         pcap_t *pcap = NULL;
738
739         if (rx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
740                 return -1;
741         if (open_single_iface(iface, &pcap) < 0)
742                 return -1;
743         rx->queue[rx->num_of_queue].pcap = pcap;
744         rx->queue[rx->num_of_queue].name = iface;
745         rx->queue[rx->num_of_queue].type = key;
746         rx->num_of_queue++;
747
748         return 0;
749 }
750
751 /*
752  * Opens a NIC for writing packets to it
753  */
754 static int
755 open_tx_iface(const char *key, const char *value, void *extra_args)
756 {
757         const char *iface = value;
758         struct pmd_devargs *tx = extra_args;
759         pcap_t *pcap;
760
761         if (tx->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
762                 return -1;
763         if (open_single_iface(iface, &pcap) < 0)
764                 return -1;
765         tx->queue[tx->num_of_queue].pcap = pcap;
766         tx->queue[tx->num_of_queue].name = iface;
767         tx->queue[tx->num_of_queue].type = key;
768         tx->num_of_queue++;
769
770         return 0;
771 }
772
773 static struct rte_vdev_driver pmd_pcap_drv;
774
775 static int
776 pmd_init_internals(struct rte_vdev_device *vdev,
777                 const unsigned int nb_rx_queues,
778                 const unsigned int nb_tx_queues,
779                 struct pmd_internals **internals,
780                 struct rte_eth_dev **eth_dev)
781 {
782         struct rte_eth_dev_data *data;
783         unsigned int numa_node = vdev->device.numa_node;
784
785         PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
786                 numa_node);
787
788         /* reserve an ethdev entry */
789         *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
790         if (!(*eth_dev))
791                 return -1;
792
793         /* now put it all together
794          * - store queue data in internals,
795          * - store numa_node info in eth_dev
796          * - point eth_dev_data to internals
797          * - and point eth_dev structure to new eth_dev_data structure
798          */
799         *internals = (*eth_dev)->data->dev_private;
800         data = (*eth_dev)->data;
801         data->nb_rx_queues = (uint16_t)nb_rx_queues;
802         data->nb_tx_queues = (uint16_t)nb_tx_queues;
803         data->dev_link = pmd_link;
804         data->mac_addrs = &eth_addr;
805
806         /*
807          * NOTE: we'll replace the data element, of originally allocated
808          * eth_dev so the rings are local per-process
809          */
810         (*eth_dev)->dev_ops = &ops;
811
812         return 0;
813 }
814
815 static int
816 eth_from_pcaps_common(struct rte_vdev_device *vdev,
817                 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
818                 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
819                 struct rte_kvargs *kvlist, struct pmd_internals **internals,
820                 struct rte_eth_dev **eth_dev)
821 {
822         struct rte_kvargs_pair *pair = NULL;
823         unsigned int k_idx;
824         unsigned int i;
825
826         /* do some parameter checking */
827         if (rx_queues == NULL && nb_rx_queues > 0)
828                 return -1;
829         if (tx_queues == NULL && nb_tx_queues > 0)
830                 return -1;
831
832         if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
833                         eth_dev) < 0)
834                 return -1;
835
836         for (i = 0; i < nb_rx_queues; i++) {
837                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
838                 struct devargs_queue *queue = &rx_queues->queue[i];
839
840                 rx->pcap = queue->pcap;
841                 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
842                 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
843         }
844
845         for (i = 0; i < nb_tx_queues; i++) {
846                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
847                 struct devargs_queue *queue = &tx_queues->queue[i];
848
849                 tx->dumper = queue->dumper;
850                 tx->pcap = queue->pcap;
851                 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
852                 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
853         }
854
855         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
856                 pair = &kvlist->pairs[k_idx];
857                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
858                         break;
859         }
860
861         if (pair == NULL)
862                 (*internals)->if_index = 0;
863         else
864                 (*internals)->if_index = if_nametoindex(pair->value);
865
866         return 0;
867 }
868
869 static int
870 eth_from_pcaps(struct rte_vdev_device *vdev,
871                 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
872                 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
873                 struct rte_kvargs *kvlist, int single_iface,
874                 unsigned int using_dumpers)
875 {
876         struct pmd_internals *internals = NULL;
877         struct rte_eth_dev *eth_dev = NULL;
878         int ret;
879
880         ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
881                 tx_queues, nb_tx_queues, kvlist, &internals, &eth_dev);
882
883         if (ret < 0)
884                 return ret;
885
886         /* store weather we are using a single interface for rx/tx or not */
887         internals->single_iface = single_iface;
888
889         eth_dev->rx_pkt_burst = eth_pcap_rx;
890
891         if (using_dumpers)
892                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
893         else
894                 eth_dev->tx_pkt_burst = eth_pcap_tx;
895
896         rte_eth_dev_probing_finish(eth_dev);
897         return 0;
898 }
899
900 static int
901 pmd_pcap_probe(struct rte_vdev_device *dev)
902 {
903         const char *name;
904         unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
905         struct rte_kvargs *kvlist;
906         struct pmd_devargs pcaps = {0};
907         struct pmd_devargs dumpers = {0};
908         struct rte_eth_dev *eth_dev;
909         int single_iface = 0;
910         int ret;
911
912         name = rte_vdev_device_name(dev);
913         PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
914
915         gettimeofday(&start_time, NULL);
916         start_cycles = rte_get_timer_cycles();
917         hz = rte_get_timer_hz();
918
919         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
920             strlen(rte_vdev_device_args(dev)) == 0) {
921                 eth_dev = rte_eth_dev_attach_secondary(name);
922                 if (!eth_dev) {
923                         PMD_LOG(ERR, "Failed to probe %s", name);
924                         return -1;
925                 }
926                 /* TODO: request info from primary to set up Rx and Tx */
927                 eth_dev->dev_ops = &ops;
928                 rte_eth_dev_probing_finish(eth_dev);
929                 return 0;
930         }
931
932         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
933         if (kvlist == NULL)
934                 return -1;
935
936         /*
937          * If iface argument is passed we open the NICs and use them for
938          * reading / writing
939          */
940         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
941
942                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
943                                 &open_rx_tx_iface, &pcaps);
944
945                 if (ret < 0)
946                         goto free_kvlist;
947
948                 dumpers.queue[0] = pcaps.queue[0];
949
950                 single_iface = 1;
951                 pcaps.num_of_queue = 1;
952                 dumpers.num_of_queue = 1;
953
954                 goto create_eth;
955         }
956
957         /*
958          * We check whether we want to open a RX stream from a real NIC or a
959          * pcap file
960          */
961         is_rx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
962         pcaps.num_of_queue = 0;
963
964         if (is_rx_pcap)
965                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
966                                 &open_rx_pcap, &pcaps);
967         else
968                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
969                                 &open_rx_iface, &pcaps);
970
971         if (ret < 0)
972                 goto free_kvlist;
973
974         /*
975          * We check whether we want to open a TX stream to a real NIC or a
976          * pcap file
977          */
978         is_tx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
979         dumpers.num_of_queue = 0;
980
981         if (is_tx_pcap)
982                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
983                                 &open_tx_pcap, &dumpers);
984         else
985                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
986                                 &open_tx_iface, &dumpers);
987
988         if (ret < 0)
989                 goto free_kvlist;
990
991 create_eth:
992         ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
993                 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
994
995 free_kvlist:
996         rte_kvargs_free(kvlist);
997
998         return ret;
999 }
1000
1001 static int
1002 pmd_pcap_remove(struct rte_vdev_device *dev)
1003 {
1004         struct rte_eth_dev *eth_dev = NULL;
1005
1006         PMD_LOG(INFO, "Closing pcap ethdev on numa socket %d",
1007                         rte_socket_id());
1008
1009         if (!dev)
1010                 return -1;
1011
1012         /* reserve an ethdev entry */
1013         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1014         if (eth_dev == NULL)
1015                 return -1;
1016
1017         rte_free(eth_dev->data->dev_private);
1018
1019         rte_eth_dev_release_port(eth_dev);
1020
1021         return 0;
1022 }
1023
1024 static struct rte_vdev_driver pmd_pcap_drv = {
1025         .probe = pmd_pcap_probe,
1026         .remove = pmd_pcap_remove,
1027 };
1028
1029 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1030 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1031 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1032         ETH_PCAP_RX_PCAP_ARG "=<string> "
1033         ETH_PCAP_TX_PCAP_ARG "=<string> "
1034         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1035         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1036         ETH_PCAP_IFACE_ARG "=<ifc>");
1037
1038 RTE_INIT(eth_pcap_init_log);
1039 static void
1040 eth_pcap_init_log(void)
1041 {
1042         eth_pcap_logtype = rte_log_register("pmd.net.pcap");
1043         if (eth_pcap_logtype >= 0)
1044                 rte_log_set_level(eth_pcap_logtype, RTE_LOG_NOTICE);
1045 }