8736010f0dcac3a4ba06f0ed55dd4f5388d53392
[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 static uint8_t iface_idx;
43
44 struct queue_stat {
45         volatile unsigned long pkts;
46         volatile unsigned long bytes;
47         volatile unsigned long err_pkts;
48 };
49
50 struct pcap_rx_queue {
51         pcap_t *pcap;
52         uint16_t in_port;
53         struct rte_mempool *mb_pool;
54         struct queue_stat rx_stat;
55         char name[PATH_MAX];
56         char type[ETH_PCAP_ARG_MAXLEN];
57 };
58
59 struct pcap_tx_queue {
60         pcap_dumper_t *dumper;
61         pcap_t *pcap;
62         struct queue_stat tx_stat;
63         char name[PATH_MAX];
64         char type[ETH_PCAP_ARG_MAXLEN];
65 };
66
67 struct pmd_internals {
68         struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
69         struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
70         struct ether_addr eth_addr;
71         int if_index;
72         int single_iface;
73 };
74
75 struct pmd_devargs {
76         unsigned int num_of_queue;
77         struct devargs_queue {
78                 pcap_dumper_t *dumper;
79                 pcap_t *pcap;
80                 const char *name;
81                 const char *type;
82         } queue[RTE_PMD_PCAP_MAX_QUEUES];
83 };
84
85 static const char *valid_arguments[] = {
86         ETH_PCAP_RX_PCAP_ARG,
87         ETH_PCAP_TX_PCAP_ARG,
88         ETH_PCAP_RX_IFACE_ARG,
89         ETH_PCAP_RX_IFACE_IN_ARG,
90         ETH_PCAP_TX_IFACE_ARG,
91         ETH_PCAP_IFACE_ARG,
92         NULL
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
434                 goto status_up;
435         }
436
437         /* If not open already, open tx pcaps/dumpers */
438         for (i = 0; i < dev->data->nb_tx_queues; i++) {
439                 tx = &internals->tx_queue[i];
440
441                 if (!tx->dumper &&
442                                 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
443                         if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
444                                 return -1;
445                 } else if (!tx->pcap &&
446                                 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
447                         if (open_single_iface(tx->name, &tx->pcap) < 0)
448                                 return -1;
449                 }
450         }
451
452         /* If not open already, open rx pcaps */
453         for (i = 0; i < dev->data->nb_rx_queues; i++) {
454                 rx = &internals->rx_queue[i];
455
456                 if (rx->pcap != NULL)
457                         continue;
458
459                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
460                         if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
461                                 return -1;
462                 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
463                         if (open_single_iface(rx->name, &rx->pcap) < 0)
464                                 return -1;
465                 }
466         }
467
468 status_up:
469         for (i = 0; i < dev->data->nb_rx_queues; i++)
470                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
471
472         for (i = 0; i < dev->data->nb_tx_queues; i++)
473                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
474
475         dev->data->dev_link.link_status = ETH_LINK_UP;
476
477         return 0;
478 }
479
480 /*
481  * This function gets called when the current port gets stopped.
482  * Is the only place for us to close all the tx streams dumpers.
483  * If not called the dumpers will be flushed within each tx burst.
484  */
485 static void
486 eth_dev_stop(struct rte_eth_dev *dev)
487 {
488         unsigned int i;
489         struct pmd_internals *internals = dev->data->dev_private;
490         struct pcap_tx_queue *tx;
491         struct pcap_rx_queue *rx;
492
493         /* Special iface case. Single pcap is open and shared between tx/rx. */
494         if (internals->single_iface) {
495                 tx = &internals->tx_queue[0];
496                 rx = &internals->rx_queue[0];
497                 pcap_close(tx->pcap);
498                 tx->pcap = NULL;
499                 rx->pcap = NULL;
500                 goto status_down;
501         }
502
503         for (i = 0; i < dev->data->nb_tx_queues; i++) {
504                 tx = &internals->tx_queue[i];
505
506                 if (tx->dumper != NULL) {
507                         pcap_dump_close(tx->dumper);
508                         tx->dumper = NULL;
509                 }
510
511                 if (tx->pcap != NULL) {
512                         pcap_close(tx->pcap);
513                         tx->pcap = NULL;
514                 }
515         }
516
517         for (i = 0; i < dev->data->nb_rx_queues; i++) {
518                 rx = &internals->rx_queue[i];
519
520                 if (rx->pcap != NULL) {
521                         pcap_close(rx->pcap);
522                         rx->pcap = NULL;
523                 }
524         }
525
526 status_down:
527         for (i = 0; i < dev->data->nb_rx_queues; i++)
528                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
529
530         for (i = 0; i < dev->data->nb_tx_queues; i++)
531                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
532
533         dev->data->dev_link.link_status = ETH_LINK_DOWN;
534 }
535
536 static int
537 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
538 {
539         return 0;
540 }
541
542 static void
543 eth_dev_info(struct rte_eth_dev *dev,
544                 struct rte_eth_dev_info *dev_info)
545 {
546         struct pmd_internals *internals = dev->data->dev_private;
547
548         dev_info->if_index = internals->if_index;
549         dev_info->max_mac_addrs = 1;
550         dev_info->max_rx_pktlen = (uint32_t) -1;
551         dev_info->max_rx_queues = dev->data->nb_rx_queues;
552         dev_info->max_tx_queues = dev->data->nb_tx_queues;
553         dev_info->min_rx_bufsize = 0;
554 }
555
556 static int
557 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
558 {
559         unsigned int i;
560         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
561         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
562         unsigned long tx_packets_err_total = 0;
563         const struct pmd_internals *internal = dev->data->dev_private;
564
565         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
566                         i < dev->data->nb_rx_queues; i++) {
567                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
568                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
569                 rx_packets_total += stats->q_ipackets[i];
570                 rx_bytes_total += stats->q_ibytes[i];
571         }
572
573         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
574                         i < dev->data->nb_tx_queues; i++) {
575                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
576                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
577                 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
578                 tx_packets_total += stats->q_opackets[i];
579                 tx_bytes_total += stats->q_obytes[i];
580                 tx_packets_err_total += stats->q_errors[i];
581         }
582
583         stats->ipackets = rx_packets_total;
584         stats->ibytes = rx_bytes_total;
585         stats->opackets = tx_packets_total;
586         stats->obytes = tx_bytes_total;
587         stats->oerrors = tx_packets_err_total;
588
589         return 0;
590 }
591
592 static void
593 eth_stats_reset(struct rte_eth_dev *dev)
594 {
595         unsigned int i;
596         struct pmd_internals *internal = dev->data->dev_private;
597
598         for (i = 0; i < dev->data->nb_rx_queues; i++) {
599                 internal->rx_queue[i].rx_stat.pkts = 0;
600                 internal->rx_queue[i].rx_stat.bytes = 0;
601         }
602
603         for (i = 0; i < dev->data->nb_tx_queues; i++) {
604                 internal->tx_queue[i].tx_stat.pkts = 0;
605                 internal->tx_queue[i].tx_stat.bytes = 0;
606                 internal->tx_queue[i].tx_stat.err_pkts = 0;
607         }
608 }
609
610 static void
611 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
612 {
613 }
614
615 static void
616 eth_queue_release(void *q __rte_unused)
617 {
618 }
619
620 static int
621 eth_link_update(struct rte_eth_dev *dev __rte_unused,
622                 int wait_to_complete __rte_unused)
623 {
624         return 0;
625 }
626
627 static int
628 eth_rx_queue_setup(struct rte_eth_dev *dev,
629                 uint16_t rx_queue_id,
630                 uint16_t nb_rx_desc __rte_unused,
631                 unsigned int socket_id __rte_unused,
632                 const struct rte_eth_rxconf *rx_conf __rte_unused,
633                 struct rte_mempool *mb_pool)
634 {
635         struct pmd_internals *internals = dev->data->dev_private;
636         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
637
638         pcap_q->mb_pool = mb_pool;
639         dev->data->rx_queues[rx_queue_id] = pcap_q;
640         pcap_q->in_port = dev->data->port_id;
641
642         return 0;
643 }
644
645 static int
646 eth_tx_queue_setup(struct rte_eth_dev *dev,
647                 uint16_t tx_queue_id,
648                 uint16_t nb_tx_desc __rte_unused,
649                 unsigned int socket_id __rte_unused,
650                 const struct rte_eth_txconf *tx_conf __rte_unused)
651 {
652         struct pmd_internals *internals = dev->data->dev_private;
653
654         dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
655
656         return 0;
657 }
658
659 static int
660 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
661 {
662         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
663
664         return 0;
665 }
666
667 static int
668 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
669 {
670         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
671
672         return 0;
673 }
674
675 static int
676 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
677 {
678         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
679
680         return 0;
681 }
682
683 static int
684 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
685 {
686         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
687
688         return 0;
689 }
690
691 static const struct eth_dev_ops ops = {
692         .dev_start = eth_dev_start,
693         .dev_stop = eth_dev_stop,
694         .dev_close = eth_dev_close,
695         .dev_configure = eth_dev_configure,
696         .dev_infos_get = eth_dev_info,
697         .rx_queue_setup = eth_rx_queue_setup,
698         .tx_queue_setup = eth_tx_queue_setup,
699         .rx_queue_start = eth_rx_queue_start,
700         .tx_queue_start = eth_tx_queue_start,
701         .rx_queue_stop = eth_rx_queue_stop,
702         .tx_queue_stop = eth_tx_queue_stop,
703         .rx_queue_release = eth_queue_release,
704         .tx_queue_release = eth_queue_release,
705         .link_update = eth_link_update,
706         .stats_get = eth_stats_get,
707         .stats_reset = eth_stats_reset,
708 };
709
710 static int
711 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
712                 pcap_t *pcap, pcap_dumper_t *dumper)
713 {
714         if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
715                 return -1;
716         if (pcap)
717                 pmd->queue[pmd->num_of_queue].pcap = pcap;
718         if (dumper)
719                 pmd->queue[pmd->num_of_queue].dumper = dumper;
720         pmd->queue[pmd->num_of_queue].name = name;
721         pmd->queue[pmd->num_of_queue].type = type;
722         pmd->num_of_queue++;
723         return 0;
724 }
725
726 /*
727  * Function handler that opens the pcap file for reading a stores a
728  * reference of it for use it later on.
729  */
730 static int
731 open_rx_pcap(const char *key, const char *value, void *extra_args)
732 {
733         const char *pcap_filename = value;
734         struct pmd_devargs *rx = extra_args;
735         pcap_t *pcap = NULL;
736
737         if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
738                 return -1;
739
740         if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
741                 pcap_close(pcap);
742                 return -1;
743         }
744
745         return 0;
746 }
747
748 /*
749  * Opens a pcap file for writing and stores a reference to it
750  * for use it later on.
751  */
752 static int
753 open_tx_pcap(const char *key, const char *value, void *extra_args)
754 {
755         const char *pcap_filename = value;
756         struct pmd_devargs *dumpers = extra_args;
757         pcap_dumper_t *dumper;
758
759         if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
760                 return -1;
761
762         if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
763                 pcap_dump_close(dumper);
764                 return -1;
765         }
766
767         return 0;
768 }
769
770 /*
771  * Opens an interface for reading and writing
772  */
773 static inline int
774 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
775 {
776         const char *iface = value;
777         struct pmd_devargs *tx = extra_args;
778         pcap_t *pcap = NULL;
779
780         if (open_single_iface(iface, &pcap) < 0)
781                 return -1;
782
783         tx->queue[0].pcap = pcap;
784         tx->queue[0].name = iface;
785         tx->queue[0].type = key;
786
787         return 0;
788 }
789
790 static inline int
791 set_iface_direction(const char *iface, pcap_t *pcap,
792                 pcap_direction_t direction)
793 {
794         const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
795         if (pcap_setdirection(pcap, direction) < 0) {
796                 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
797                                 iface, direction_str, pcap_geterr(pcap));
798                 return -1;
799         }
800         PMD_LOG(INFO, "Setting %s pcap direction %s\n",
801                         iface, direction_str);
802         return 0;
803 }
804
805 static inline int
806 open_iface(const char *key, const char *value, void *extra_args)
807 {
808         const char *iface = value;
809         struct pmd_devargs *pmd = extra_args;
810         pcap_t *pcap = NULL;
811
812         if (open_single_iface(iface, &pcap) < 0)
813                 return -1;
814         if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
815                 pcap_close(pcap);
816                 return -1;
817         }
818
819         return 0;
820 }
821
822 /*
823  * Opens a NIC for reading packets from it
824  */
825 static inline int
826 open_rx_iface(const char *key, const char *value, void *extra_args)
827 {
828         int ret = open_iface(key, value, extra_args);
829         if (ret < 0)
830                 return ret;
831         if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
832                 struct pmd_devargs *pmd = extra_args;
833                 unsigned int qid = pmd->num_of_queue - 1;
834
835                 set_iface_direction(pmd->queue[qid].name,
836                                 pmd->queue[qid].pcap,
837                                 PCAP_D_IN);
838         }
839
840         return 0;
841 }
842
843 static inline int
844 rx_iface_args_process(const char *key, const char *value, void *extra_args)
845 {
846         if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
847                         strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
848                 return open_rx_iface(key, value, extra_args);
849
850         return 0;
851 }
852
853 /*
854  * Opens a NIC for writing packets to it
855  */
856 static int
857 open_tx_iface(const char *key, const char *value, void *extra_args)
858 {
859         return open_iface(key, value, extra_args);
860 }
861
862 static struct rte_vdev_driver pmd_pcap_drv;
863
864 static int
865 pmd_init_internals(struct rte_vdev_device *vdev,
866                 const unsigned int nb_rx_queues,
867                 const unsigned int nb_tx_queues,
868                 struct pmd_internals **internals,
869                 struct rte_eth_dev **eth_dev)
870 {
871         struct rte_eth_dev_data *data;
872         unsigned int numa_node = vdev->device.numa_node;
873
874         PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
875                 numa_node);
876
877         /* reserve an ethdev entry */
878         *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
879         if (!(*eth_dev))
880                 return -1;
881
882         /* now put it all together
883          * - store queue data in internals,
884          * - store numa_node info in eth_dev
885          * - point eth_dev_data to internals
886          * - and point eth_dev structure to new eth_dev_data structure
887          */
888         *internals = (*eth_dev)->data->dev_private;
889         /*
890          * Interface MAC = 02:70:63:61:70:<iface_idx>
891          * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
892          * where the middle 4 characters are converted to hex.
893          */
894         (*internals)->eth_addr = (struct ether_addr) {
895                 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
896         };
897         data = (*eth_dev)->data;
898         data->nb_rx_queues = (uint16_t)nb_rx_queues;
899         data->nb_tx_queues = (uint16_t)nb_tx_queues;
900         data->dev_link = pmd_link;
901         data->mac_addrs = &(*internals)->eth_addr;
902
903         /*
904          * NOTE: we'll replace the data element, of originally allocated
905          * eth_dev so the rings are local per-process
906          */
907         (*eth_dev)->dev_ops = &ops;
908
909         return 0;
910 }
911
912 static int
913 eth_from_pcaps_common(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, struct pmd_internals **internals,
917                 struct rte_eth_dev **eth_dev)
918 {
919         struct rte_kvargs_pair *pair = NULL;
920         unsigned int k_idx;
921         unsigned int i;
922
923         /* do some parameter checking */
924         if (rx_queues == NULL && nb_rx_queues > 0)
925                 return -1;
926         if (tx_queues == NULL && nb_tx_queues > 0)
927                 return -1;
928
929         if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
930                         eth_dev) < 0)
931                 return -1;
932
933         for (i = 0; i < nb_rx_queues; i++) {
934                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
935                 struct devargs_queue *queue = &rx_queues->queue[i];
936
937                 rx->pcap = queue->pcap;
938                 snprintf(rx->name, sizeof(rx->name), "%s", queue->name);
939                 snprintf(rx->type, sizeof(rx->type), "%s", queue->type);
940         }
941
942         for (i = 0; i < nb_tx_queues; i++) {
943                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
944                 struct devargs_queue *queue = &tx_queues->queue[i];
945
946                 tx->dumper = queue->dumper;
947                 tx->pcap = queue->pcap;
948                 snprintf(tx->name, sizeof(tx->name), "%s", queue->name);
949                 snprintf(tx->type, sizeof(tx->type), "%s", queue->type);
950         }
951
952         for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
953                 pair = &kvlist->pairs[k_idx];
954                 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
955                         break;
956         }
957
958         if (pair == NULL)
959                 (*internals)->if_index = 0;
960         else
961                 (*internals)->if_index = if_nametoindex(pair->value);
962
963         return 0;
964 }
965
966 static int
967 eth_from_pcaps(struct rte_vdev_device *vdev,
968                 struct pmd_devargs *rx_queues, const unsigned int nb_rx_queues,
969                 struct pmd_devargs *tx_queues, const unsigned int nb_tx_queues,
970                 struct rte_kvargs *kvlist, int single_iface,
971                 unsigned int using_dumpers)
972 {
973         struct pmd_internals *internals = NULL;
974         struct rte_eth_dev *eth_dev = NULL;
975         int ret;
976
977         ret = eth_from_pcaps_common(vdev, rx_queues, nb_rx_queues,
978                 tx_queues, nb_tx_queues, kvlist, &internals, &eth_dev);
979
980         if (ret < 0)
981                 return ret;
982
983         /* store weather we are using a single interface for rx/tx or not */
984         internals->single_iface = single_iface;
985
986         eth_dev->rx_pkt_burst = eth_pcap_rx;
987
988         if (using_dumpers)
989                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
990         else
991                 eth_dev->tx_pkt_burst = eth_pcap_tx;
992
993         rte_eth_dev_probing_finish(eth_dev);
994         return 0;
995 }
996
997 static int
998 pmd_pcap_probe(struct rte_vdev_device *dev)
999 {
1000         const char *name;
1001         unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
1002         struct rte_kvargs *kvlist;
1003         struct pmd_devargs pcaps = {0};
1004         struct pmd_devargs dumpers = {0};
1005         struct rte_eth_dev *eth_dev;
1006         int single_iface = 0;
1007         int ret;
1008
1009         name = rte_vdev_device_name(dev);
1010         PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1011
1012         gettimeofday(&start_time, NULL);
1013         start_cycles = rte_get_timer_cycles();
1014         hz = rte_get_timer_hz();
1015
1016         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
1017             strlen(rte_vdev_device_args(dev)) == 0) {
1018                 eth_dev = rte_eth_dev_attach_secondary(name);
1019                 if (!eth_dev) {
1020                         PMD_LOG(ERR, "Failed to probe %s", name);
1021                         return -1;
1022                 }
1023                 /* TODO: request info from primary to set up Rx and Tx */
1024                 eth_dev->dev_ops = &ops;
1025                 eth_dev->device = &dev->device;
1026                 rte_eth_dev_probing_finish(eth_dev);
1027                 return 0;
1028         }
1029
1030         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_arguments);
1031         if (kvlist == NULL)
1032                 return -1;
1033
1034         /*
1035          * If iface argument is passed we open the NICs and use them for
1036          * reading / writing
1037          */
1038         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1039
1040                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1041                                 &open_rx_tx_iface, &pcaps);
1042
1043                 if (ret < 0)
1044                         goto free_kvlist;
1045
1046                 dumpers.queue[0] = pcaps.queue[0];
1047
1048                 single_iface = 1;
1049                 pcaps.num_of_queue = 1;
1050                 dumpers.num_of_queue = 1;
1051
1052                 goto create_eth;
1053         }
1054
1055         /*
1056          * We check whether we want to open a RX stream from a real NIC or a
1057          * pcap file
1058          */
1059         is_rx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1060         pcaps.num_of_queue = 0;
1061
1062         if (is_rx_pcap) {
1063                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1064                                 &open_rx_pcap, &pcaps);
1065         } else {
1066                 ret = rte_kvargs_process(kvlist, NULL,
1067                                 &rx_iface_args_process, &pcaps);
1068         }
1069
1070         if (ret < 0)
1071                 goto free_kvlist;
1072
1073         /*
1074          * We check whether we want to open a TX stream to a real NIC or a
1075          * pcap file
1076          */
1077         is_tx_pcap = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1078         dumpers.num_of_queue = 0;
1079
1080         if (is_tx_pcap)
1081                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1082                                 &open_tx_pcap, &dumpers);
1083         else
1084                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1085                                 &open_tx_iface, &dumpers);
1086
1087         if (ret < 0)
1088                 goto free_kvlist;
1089
1090 create_eth:
1091         ret = eth_from_pcaps(dev, &pcaps, pcaps.num_of_queue, &dumpers,
1092                 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1093
1094 free_kvlist:
1095         rte_kvargs_free(kvlist);
1096
1097         return ret;
1098 }
1099
1100 static int
1101 pmd_pcap_remove(struct rte_vdev_device *dev)
1102 {
1103         struct rte_eth_dev *eth_dev = NULL;
1104
1105         PMD_LOG(INFO, "Closing pcap ethdev on numa socket %d",
1106                         rte_socket_id());
1107
1108         if (!dev)
1109                 return -1;
1110
1111         /* reserve an ethdev entry */
1112         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1113         if (eth_dev == NULL)
1114                 return -1;
1115
1116         rte_free(eth_dev->data->dev_private);
1117
1118         rte_eth_dev_release_port(eth_dev);
1119
1120         return 0;
1121 }
1122
1123 static struct rte_vdev_driver pmd_pcap_drv = {
1124         .probe = pmd_pcap_probe,
1125         .remove = pmd_pcap_remove,
1126 };
1127
1128 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1129 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1130 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1131         ETH_PCAP_RX_PCAP_ARG "=<string> "
1132         ETH_PCAP_TX_PCAP_ARG "=<string> "
1133         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1134         ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1135         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1136         ETH_PCAP_IFACE_ARG "=<ifc>");
1137
1138 RTE_INIT(eth_pcap_init_log)
1139 {
1140         eth_pcap_logtype = rte_log_register("pmd.net.pcap");
1141         if (eth_pcap_logtype >= 0)
1142                 rte_log_set_level(eth_pcap_logtype, RTE_LOG_NOTICE);
1143 }