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