34e82317b1f06f6406263af89e8f4bcd124e2b94
[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 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <unistd.h>
13
14 #if defined(RTE_EXEC_ENV_FREEBSD)
15 #include <sys/sysctl.h>
16 #include <net/if_dl.h>
17 #endif
18
19 #include <pcap.h>
20
21 #include <rte_cycles.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_vdev.h>
24 #include <rte_kvargs.h>
25 #include <rte_malloc.h>
26 #include <rte_mbuf.h>
27 #include <rte_bus_vdev.h>
28 #include <rte_string_fns.h>
29
30 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
31 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
32 #define RTE_ETH_PCAP_PROMISC 1
33 #define RTE_ETH_PCAP_TIMEOUT -1
34
35 #define ETH_PCAP_RX_PCAP_ARG  "rx_pcap"
36 #define ETH_PCAP_TX_PCAP_ARG  "tx_pcap"
37 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
38 #define ETH_PCAP_RX_IFACE_IN_ARG "rx_iface_in"
39 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
40 #define ETH_PCAP_IFACE_ARG    "iface"
41 #define ETH_PCAP_PHY_MAC_ARG  "phy_mac"
42 #define ETH_PCAP_INFINITE_RX_ARG  "infinite_rx"
43
44 #define ETH_PCAP_ARG_MAXLEN     64
45
46 #define RTE_PMD_PCAP_MAX_QUEUES 16
47
48 static char errbuf[PCAP_ERRBUF_SIZE];
49 static struct timeval start_time;
50 static uint64_t start_cycles;
51 static uint64_t hz;
52 static uint8_t iface_idx;
53
54 struct queue_stat {
55         volatile unsigned long pkts;
56         volatile unsigned long bytes;
57         volatile unsigned long err_pkts;
58 };
59
60 struct pcap_rx_queue {
61         uint16_t port_id;
62         uint16_t queue_id;
63         struct rte_mempool *mb_pool;
64         struct queue_stat rx_stat;
65         char name[PATH_MAX];
66         char type[ETH_PCAP_ARG_MAXLEN];
67
68         /* Contains pre-generated packets to be looped through */
69         struct rte_ring *pkts;
70 };
71
72 struct pcap_tx_queue {
73         uint16_t port_id;
74         uint16_t queue_id;
75         struct queue_stat tx_stat;
76         char name[PATH_MAX];
77         char type[ETH_PCAP_ARG_MAXLEN];
78 };
79
80 struct pmd_internals {
81         struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
82         struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
83         char devargs[ETH_PCAP_ARG_MAXLEN];
84         struct rte_ether_addr eth_addr;
85         int if_index;
86         int single_iface;
87         int phy_mac;
88         unsigned int infinite_rx;
89 };
90
91 struct pmd_process_private {
92         pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
93         pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
94         pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
95 };
96
97 struct pmd_devargs {
98         unsigned int num_of_queue;
99         struct devargs_queue {
100                 pcap_dumper_t *dumper;
101                 pcap_t *pcap;
102                 const char *name;
103                 const char *type;
104         } queue[RTE_PMD_PCAP_MAX_QUEUES];
105         int phy_mac;
106 };
107
108 struct pmd_devargs_all {
109         struct pmd_devargs rx_queues;
110         struct pmd_devargs tx_queues;
111         int single_iface;
112         unsigned int is_tx_pcap;
113         unsigned int is_tx_iface;
114         unsigned int is_rx_pcap;
115         unsigned int is_rx_iface;
116         unsigned int infinite_rx;
117 };
118
119 static const char *valid_arguments[] = {
120         ETH_PCAP_RX_PCAP_ARG,
121         ETH_PCAP_TX_PCAP_ARG,
122         ETH_PCAP_RX_IFACE_ARG,
123         ETH_PCAP_RX_IFACE_IN_ARG,
124         ETH_PCAP_TX_IFACE_ARG,
125         ETH_PCAP_IFACE_ARG,
126         ETH_PCAP_PHY_MAC_ARG,
127         ETH_PCAP_INFINITE_RX_ARG,
128         NULL
129 };
130
131 static struct rte_eth_link pmd_link = {
132                 .link_speed = ETH_SPEED_NUM_10G,
133                 .link_duplex = ETH_LINK_FULL_DUPLEX,
134                 .link_status = ETH_LINK_DOWN,
135                 .link_autoneg = ETH_LINK_FIXED,
136 };
137
138 RTE_LOG_REGISTER(eth_pcap_logtype, pmd.net.pcap, NOTICE);
139
140 #define PMD_LOG(level, fmt, args...) \
141         rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
142                 "%s(): " fmt "\n", __func__, ##args)
143
144 static int
145 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
146                 const u_char *data, uint16_t data_len)
147 {
148         /* Copy the first segment. */
149         uint16_t len = rte_pktmbuf_tailroom(mbuf);
150         struct rte_mbuf *m = mbuf;
151
152         rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
153         data_len -= len;
154         data += len;
155
156         while (data_len > 0) {
157                 /* Allocate next mbuf and point to that. */
158                 m->next = rte_pktmbuf_alloc(mb_pool);
159
160                 if (unlikely(!m->next))
161                         return -1;
162
163                 m = m->next;
164
165                 /* Headroom is not needed in chained mbufs. */
166                 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
167                 m->pkt_len = 0;
168                 m->data_len = 0;
169
170                 /* Copy next segment. */
171                 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
172                 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
173
174                 mbuf->nb_segs++;
175                 data_len -= len;
176                 data += len;
177         }
178
179         return mbuf->nb_segs;
180 }
181
182 static uint16_t
183 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
184 {
185         int i;
186         struct pcap_rx_queue *pcap_q = queue;
187         uint32_t rx_bytes = 0;
188
189         if (unlikely(nb_pkts == 0))
190                 return 0;
191
192         if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
193                 return 0;
194
195         for (i = 0; i < nb_pkts; i++) {
196                 struct rte_mbuf *pcap_buf;
197                 int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
198                 if (err)
199                         return i;
200
201                 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
202                                 rte_pktmbuf_mtod(pcap_buf, void *),
203                                 pcap_buf->data_len);
204                 bufs[i]->data_len = pcap_buf->data_len;
205                 bufs[i]->pkt_len = pcap_buf->pkt_len;
206                 bufs[i]->port = pcap_q->port_id;
207                 rx_bytes += pcap_buf->data_len;
208
209                 /* Enqueue packet back on ring to allow infinite rx. */
210                 rte_ring_enqueue(pcap_q->pkts, pcap_buf);
211         }
212
213         pcap_q->rx_stat.pkts += i;
214         pcap_q->rx_stat.bytes += rx_bytes;
215
216         return i;
217 }
218
219 static uint16_t
220 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
221 {
222         unsigned int i;
223         struct pcap_pkthdr header;
224         struct pmd_process_private *pp;
225         const u_char *packet;
226         struct rte_mbuf *mbuf;
227         struct pcap_rx_queue *pcap_q = queue;
228         uint16_t num_rx = 0;
229         uint32_t rx_bytes = 0;
230         pcap_t *pcap;
231
232         pp = rte_eth_devices[pcap_q->port_id].process_private;
233         pcap = pp->rx_pcap[pcap_q->queue_id];
234
235         if (unlikely(pcap == NULL || nb_pkts == 0))
236                 return 0;
237
238         /* Reads the given number of packets from the pcap file one by one
239          * and copies the packet data into a newly allocated mbuf to return.
240          */
241         for (i = 0; i < nb_pkts; i++) {
242                 /* Get the next PCAP packet */
243                 packet = pcap_next(pcap, &header);
244                 if (unlikely(packet == NULL))
245                         break;
246
247                 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
248                 if (unlikely(mbuf == NULL))
249                         break;
250
251                 if (header.caplen <= rte_pktmbuf_tailroom(mbuf)) {
252                         /* pcap packet will fit in the mbuf, can copy it */
253                         rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
254                                         header.caplen);
255                         mbuf->data_len = (uint16_t)header.caplen;
256                 } else {
257                         /* Try read jumbo frame into multi mbufs. */
258                         if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
259                                                        mbuf,
260                                                        packet,
261                                                        header.caplen) == -1)) {
262                                 rte_pktmbuf_free(mbuf);
263                                 break;
264                         }
265                 }
266
267                 mbuf->pkt_len = (uint16_t)header.caplen;
268                 mbuf->timestamp = (uint64_t)header.ts.tv_sec * 1000000
269                                                         + header.ts.tv_usec;
270                 mbuf->ol_flags |= PKT_RX_TIMESTAMP;
271                 mbuf->port = pcap_q->port_id;
272                 bufs[num_rx] = mbuf;
273                 num_rx++;
274                 rx_bytes += header.caplen;
275         }
276         pcap_q->rx_stat.pkts += num_rx;
277         pcap_q->rx_stat.bytes += rx_bytes;
278
279         return num_rx;
280 }
281
282 static uint16_t
283 eth_null_rx(void *queue __rte_unused,
284                 struct rte_mbuf **bufs __rte_unused,
285                 uint16_t nb_pkts __rte_unused)
286 {
287         return 0;
288 }
289
290 #define NSEC_PER_SEC    1000000000L
291
292 static inline void
293 calculate_timestamp(struct timeval *ts) {
294         uint64_t cycles;
295         struct timeval cur_time;
296
297         cycles = rte_get_timer_cycles() - start_cycles;
298         cur_time.tv_sec = cycles / hz;
299         cur_time.tv_usec = (cycles % hz) * NSEC_PER_SEC / hz;
300
301         ts->tv_sec = start_time.tv_sec + cur_time.tv_sec;
302         ts->tv_usec = start_time.tv_usec + cur_time.tv_usec;
303         if (ts->tv_usec >= NSEC_PER_SEC) {
304                 ts->tv_usec -= NSEC_PER_SEC;
305                 ts->tv_sec += 1;
306         }
307 }
308
309 /*
310  * Callback to handle writing packets to a pcap file.
311  */
312 static uint16_t
313 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
314 {
315         unsigned int i;
316         struct rte_mbuf *mbuf;
317         struct pmd_process_private *pp;
318         struct pcap_tx_queue *dumper_q = queue;
319         uint16_t num_tx = 0;
320         uint32_t tx_bytes = 0;
321         struct pcap_pkthdr header;
322         pcap_dumper_t *dumper;
323         unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
324         size_t len, caplen;
325
326         pp = rte_eth_devices[dumper_q->port_id].process_private;
327         dumper = pp->tx_dumper[dumper_q->queue_id];
328
329         if (dumper == NULL || nb_pkts == 0)
330                 return 0;
331
332         /* writes the nb_pkts packets to the previously opened pcap file
333          * dumper */
334         for (i = 0; i < nb_pkts; i++) {
335                 mbuf = bufs[i];
336                 len = caplen = rte_pktmbuf_pkt_len(mbuf);
337                 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
338                                 len > sizeof(temp_data))) {
339                         caplen = sizeof(temp_data);
340                 }
341
342                 calculate_timestamp(&header.ts);
343                 header.len = len;
344                 header.caplen = caplen;
345                 /* rte_pktmbuf_read() returns a pointer to the data directly
346                  * in the mbuf (when the mbuf is contiguous) or, otherwise,
347                  * a pointer to temp_data after copying into it.
348                  */
349                 pcap_dump((u_char *)dumper, &header,
350                         rte_pktmbuf_read(mbuf, 0, caplen, temp_data));
351
352                 num_tx++;
353                 tx_bytes += caplen;
354                 rte_pktmbuf_free(mbuf);
355         }
356
357         /*
358          * Since there's no place to hook a callback when the forwarding
359          * process stops and to make sure the pcap file is actually written,
360          * we flush the pcap dumper within each burst.
361          */
362         pcap_dump_flush(dumper);
363         dumper_q->tx_stat.pkts += num_tx;
364         dumper_q->tx_stat.bytes += tx_bytes;
365         dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
366
367         return nb_pkts;
368 }
369
370 /*
371  * Callback to handle dropping packets in the infinite rx case.
372  */
373 static uint16_t
374 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
375 {
376         unsigned int i;
377         uint32_t tx_bytes = 0;
378         struct pcap_tx_queue *tx_queue = queue;
379
380         if (unlikely(nb_pkts == 0))
381                 return 0;
382
383         for (i = 0; i < nb_pkts; i++) {
384                 tx_bytes += bufs[i]->data_len;
385                 rte_pktmbuf_free(bufs[i]);
386         }
387
388         tx_queue->tx_stat.pkts += nb_pkts;
389         tx_queue->tx_stat.bytes += tx_bytes;
390
391         return i;
392 }
393
394 /*
395  * Callback to handle sending packets through a real NIC.
396  */
397 static uint16_t
398 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
399 {
400         unsigned int i;
401         int ret;
402         struct rte_mbuf *mbuf;
403         struct pmd_process_private *pp;
404         struct pcap_tx_queue *tx_queue = queue;
405         uint16_t num_tx = 0;
406         uint32_t tx_bytes = 0;
407         pcap_t *pcap;
408         unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
409         size_t len;
410
411         pp = rte_eth_devices[tx_queue->port_id].process_private;
412         pcap = pp->tx_pcap[tx_queue->queue_id];
413
414         if (unlikely(nb_pkts == 0 || pcap == NULL))
415                 return 0;
416
417         for (i = 0; i < nb_pkts; i++) {
418                 mbuf = bufs[i];
419                 len = rte_pktmbuf_pkt_len(mbuf);
420                 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
421                                 len > sizeof(temp_data))) {
422                         PMD_LOG(ERR,
423                                 "Dropping multi segment PCAP packet. Size (%zd) > max size (%zd).",
424                                 len, sizeof(temp_data));
425                         rte_pktmbuf_free(mbuf);
426                         continue;
427                 }
428
429                 /* rte_pktmbuf_read() returns a pointer to the data directly
430                  * in the mbuf (when the mbuf is contiguous) or, otherwise,
431                  * a pointer to temp_data after copying into it.
432                  */
433                 ret = pcap_sendpacket(pcap,
434                         rte_pktmbuf_read(mbuf, 0, len, temp_data), len);
435                 if (unlikely(ret != 0))
436                         break;
437                 num_tx++;
438                 tx_bytes += len;
439                 rte_pktmbuf_free(mbuf);
440         }
441
442         tx_queue->tx_stat.pkts += num_tx;
443         tx_queue->tx_stat.bytes += tx_bytes;
444         tx_queue->tx_stat.err_pkts += i - num_tx;
445
446         return i;
447 }
448
449 /*
450  * pcap_open_live wrapper function
451  */
452 static inline int
453 open_iface_live(const char *iface, pcap_t **pcap) {
454         *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
455                         RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
456
457         if (*pcap == NULL) {
458                 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
459                 return -1;
460         }
461
462         return 0;
463 }
464
465 static int
466 open_single_iface(const char *iface, pcap_t **pcap)
467 {
468         if (open_iface_live(iface, pcap) < 0) {
469                 PMD_LOG(ERR, "Couldn't open interface %s", iface);
470                 return -1;
471         }
472
473         return 0;
474 }
475
476 static int
477 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
478 {
479         pcap_t *tx_pcap;
480
481         /*
482          * We need to create a dummy empty pcap_t to use it
483          * with pcap_dump_open(). We create big enough an Ethernet
484          * pcap holder.
485          */
486         tx_pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB,
487                         RTE_ETH_PCAP_SNAPSHOT_LEN, PCAP_TSTAMP_PRECISION_NANO);
488         if (tx_pcap == NULL) {
489                 PMD_LOG(ERR, "Couldn't create dead pcap");
490                 return -1;
491         }
492
493         /* The dumper is created using the previous pcap_t reference */
494         *dumper = pcap_dump_open(tx_pcap, pcap_filename);
495         if (*dumper == NULL) {
496                 pcap_close(tx_pcap);
497                 PMD_LOG(ERR, "Couldn't open %s for writing.",
498                         pcap_filename);
499                 return -1;
500         }
501
502         pcap_close(tx_pcap);
503         return 0;
504 }
505
506 static int
507 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
508 {
509         *pcap = pcap_open_offline(pcap_filename, errbuf);
510         if (*pcap == NULL) {
511                 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
512                         errbuf);
513                 return -1;
514         }
515
516         return 0;
517 }
518
519 static uint64_t
520 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
521 {
522         const u_char *packet;
523         struct pcap_pkthdr header;
524         uint64_t pcap_pkt_count = 0;
525
526         while ((packet = pcap_next(*pcap, &header)))
527                 pcap_pkt_count++;
528
529         /* The pcap is reopened so it can be used as normal later. */
530         pcap_close(*pcap);
531         *pcap = NULL;
532         open_single_rx_pcap(pcap_q->name, pcap);
533
534         return pcap_pkt_count;
535 }
536
537 static int
538 eth_dev_start(struct rte_eth_dev *dev)
539 {
540         unsigned int i;
541         struct pmd_internals *internals = dev->data->dev_private;
542         struct pmd_process_private *pp = dev->process_private;
543         struct pcap_tx_queue *tx;
544         struct pcap_rx_queue *rx;
545
546         /* Special iface case. Single pcap is open and shared between tx/rx. */
547         if (internals->single_iface) {
548                 tx = &internals->tx_queue[0];
549                 rx = &internals->rx_queue[0];
550
551                 if (!pp->tx_pcap[0] &&
552                         strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
553                         if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
554                                 return -1;
555                         pp->rx_pcap[0] = pp->tx_pcap[0];
556                 }
557
558                 goto status_up;
559         }
560
561         /* If not open already, open tx pcaps/dumpers */
562         for (i = 0; i < dev->data->nb_tx_queues; i++) {
563                 tx = &internals->tx_queue[i];
564
565                 if (!pp->tx_dumper[i] &&
566                                 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
567                         if (open_single_tx_pcap(tx->name,
568                                 &pp->tx_dumper[i]) < 0)
569                                 return -1;
570                 } else if (!pp->tx_pcap[i] &&
571                                 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
572                         if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
573                                 return -1;
574                 }
575         }
576
577         /* If not open already, open rx pcaps */
578         for (i = 0; i < dev->data->nb_rx_queues; i++) {
579                 rx = &internals->rx_queue[i];
580
581                 if (pp->rx_pcap[i] != NULL)
582                         continue;
583
584                 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
585                         if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
586                                 return -1;
587                 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
588                         if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
589                                 return -1;
590                 }
591         }
592
593 status_up:
594         for (i = 0; i < dev->data->nb_rx_queues; i++)
595                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
596
597         for (i = 0; i < dev->data->nb_tx_queues; i++)
598                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
599
600         dev->data->dev_link.link_status = ETH_LINK_UP;
601
602         return 0;
603 }
604
605 /*
606  * This function gets called when the current port gets stopped.
607  * Is the only place for us to close all the tx streams dumpers.
608  * If not called the dumpers will be flushed within each tx burst.
609  */
610 static int
611 eth_dev_stop(struct rte_eth_dev *dev)
612 {
613         unsigned int i;
614         struct pmd_internals *internals = dev->data->dev_private;
615         struct pmd_process_private *pp = dev->process_private;
616
617         /* Special iface case. Single pcap is open and shared between tx/rx. */
618         if (internals->single_iface) {
619                 pcap_close(pp->tx_pcap[0]);
620                 pp->tx_pcap[0] = NULL;
621                 pp->rx_pcap[0] = NULL;
622                 goto status_down;
623         }
624
625         for (i = 0; i < dev->data->nb_tx_queues; i++) {
626                 if (pp->tx_dumper[i] != NULL) {
627                         pcap_dump_close(pp->tx_dumper[i]);
628                         pp->tx_dumper[i] = NULL;
629                 }
630
631                 if (pp->tx_pcap[i] != NULL) {
632                         pcap_close(pp->tx_pcap[i]);
633                         pp->tx_pcap[i] = NULL;
634                 }
635         }
636
637         for (i = 0; i < dev->data->nb_rx_queues; i++) {
638                 if (pp->rx_pcap[i] != NULL) {
639                         pcap_close(pp->rx_pcap[i]);
640                         pp->rx_pcap[i] = NULL;
641                 }
642         }
643
644 status_down:
645         for (i = 0; i < dev->data->nb_rx_queues; i++)
646                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
647
648         for (i = 0; i < dev->data->nb_tx_queues; i++)
649                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
650
651         dev->data->dev_link.link_status = ETH_LINK_DOWN;
652
653         return 0;
654 }
655
656 static int
657 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
658 {
659         return 0;
660 }
661
662 static int
663 eth_dev_info(struct rte_eth_dev *dev,
664                 struct rte_eth_dev_info *dev_info)
665 {
666         struct pmd_internals *internals = dev->data->dev_private;
667
668         dev_info->if_index = internals->if_index;
669         dev_info->max_mac_addrs = 1;
670         dev_info->max_rx_pktlen = (uint32_t) -1;
671         dev_info->max_rx_queues = dev->data->nb_rx_queues;
672         dev_info->max_tx_queues = dev->data->nb_tx_queues;
673         dev_info->min_rx_bufsize = 0;
674
675         return 0;
676 }
677
678 static int
679 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
680 {
681         unsigned int i;
682         unsigned long rx_packets_total = 0, rx_bytes_total = 0;
683         unsigned long tx_packets_total = 0, tx_bytes_total = 0;
684         unsigned long tx_packets_err_total = 0;
685         const struct pmd_internals *internal = dev->data->dev_private;
686
687         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
688                         i < dev->data->nb_rx_queues; i++) {
689                 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
690                 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
691                 rx_packets_total += stats->q_ipackets[i];
692                 rx_bytes_total += stats->q_ibytes[i];
693         }
694
695         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
696                         i < dev->data->nb_tx_queues; i++) {
697                 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
698                 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
699                 tx_packets_total += stats->q_opackets[i];
700                 tx_bytes_total += stats->q_obytes[i];
701                 tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
702         }
703
704         stats->ipackets = rx_packets_total;
705         stats->ibytes = rx_bytes_total;
706         stats->opackets = tx_packets_total;
707         stats->obytes = tx_bytes_total;
708         stats->oerrors = tx_packets_err_total;
709
710         return 0;
711 }
712
713 static int
714 eth_stats_reset(struct rte_eth_dev *dev)
715 {
716         unsigned int i;
717         struct pmd_internals *internal = dev->data->dev_private;
718
719         for (i = 0; i < dev->data->nb_rx_queues; i++) {
720                 internal->rx_queue[i].rx_stat.pkts = 0;
721                 internal->rx_queue[i].rx_stat.bytes = 0;
722         }
723
724         for (i = 0; i < dev->data->nb_tx_queues; i++) {
725                 internal->tx_queue[i].tx_stat.pkts = 0;
726                 internal->tx_queue[i].tx_stat.bytes = 0;
727                 internal->tx_queue[i].tx_stat.err_pkts = 0;
728         }
729
730         return 0;
731 }
732
733 static int
734 eth_dev_close(struct rte_eth_dev *dev)
735 {
736         unsigned int i;
737         struct pmd_internals *internals = dev->data->dev_private;
738
739         PMD_LOG(INFO, "Closing pcap ethdev on NUMA socket %d",
740                         rte_socket_id());
741
742         rte_free(dev->process_private);
743
744         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
745                 return 0;
746
747         /* Device wide flag, but cleanup must be performed per queue. */
748         if (internals->infinite_rx) {
749                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
750                         struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
751                         struct rte_mbuf *pcap_buf;
752
753                         /*
754                          * 'pcap_q->pkts' can be NULL if 'eth_dev_close()'
755                          * called before 'eth_rx_queue_setup()' has been called
756                          */
757                         if (pcap_q->pkts == NULL)
758                                 continue;
759
760                         while (!rte_ring_dequeue(pcap_q->pkts,
761                                         (void **)&pcap_buf))
762                                 rte_pktmbuf_free(pcap_buf);
763
764                         rte_ring_free(pcap_q->pkts);
765                 }
766         }
767
768         if (internals->phy_mac == 0)
769                 /* not dynamically allocated, must not be freed */
770                 dev->data->mac_addrs = NULL;
771
772         return 0;
773 }
774
775 static void
776 eth_queue_release(void *q __rte_unused)
777 {
778 }
779
780 static int
781 eth_link_update(struct rte_eth_dev *dev __rte_unused,
782                 int wait_to_complete __rte_unused)
783 {
784         return 0;
785 }
786
787 static int
788 eth_rx_queue_setup(struct rte_eth_dev *dev,
789                 uint16_t rx_queue_id,
790                 uint16_t nb_rx_desc __rte_unused,
791                 unsigned int socket_id __rte_unused,
792                 const struct rte_eth_rxconf *rx_conf __rte_unused,
793                 struct rte_mempool *mb_pool)
794 {
795         struct pmd_internals *internals = dev->data->dev_private;
796         struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
797
798         pcap_q->mb_pool = mb_pool;
799         pcap_q->port_id = dev->data->port_id;
800         pcap_q->queue_id = rx_queue_id;
801         dev->data->rx_queues[rx_queue_id] = pcap_q;
802
803         if (internals->infinite_rx) {
804                 struct pmd_process_private *pp;
805                 char ring_name[NAME_MAX];
806                 static uint32_t ring_number;
807                 uint64_t pcap_pkt_count = 0;
808                 struct rte_mbuf *bufs[1];
809                 pcap_t **pcap;
810
811                 pp = rte_eth_devices[pcap_q->port_id].process_private;
812                 pcap = &pp->rx_pcap[pcap_q->queue_id];
813
814                 if (unlikely(*pcap == NULL))
815                         return -ENOENT;
816
817                 pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
818
819                 snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
820                                 ring_number);
821
822                 pcap_q->pkts = rte_ring_create(ring_name,
823                                 rte_align64pow2(pcap_pkt_count + 1), 0,
824                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
825                 ring_number++;
826                 if (!pcap_q->pkts)
827                         return -ENOENT;
828
829                 /* Fill ring with packets from PCAP file one by one. */
830                 while (eth_pcap_rx(pcap_q, bufs, 1)) {
831                         /* Check for multiseg mbufs. */
832                         if (bufs[0]->nb_segs != 1) {
833                                 rte_pktmbuf_free(*bufs);
834
835                                 while (!rte_ring_dequeue(pcap_q->pkts,
836                                                 (void **)bufs))
837                                         rte_pktmbuf_free(*bufs);
838
839                                 rte_ring_free(pcap_q->pkts);
840                                 PMD_LOG(ERR, "Multiseg mbufs are not supported in infinite_rx "
841                                                 "mode.");
842                                 return -EINVAL;
843                         }
844
845                         rte_ring_enqueue_bulk(pcap_q->pkts,
846                                         (void * const *)bufs, 1, NULL);
847                 }
848                 /*
849                  * Reset the stats for this queue since eth_pcap_rx calls above
850                  * didn't result in the application receiving packets.
851                  */
852                 pcap_q->rx_stat.pkts = 0;
853                 pcap_q->rx_stat.bytes = 0;
854         }
855
856         return 0;
857 }
858
859 static int
860 eth_tx_queue_setup(struct rte_eth_dev *dev,
861                 uint16_t tx_queue_id,
862                 uint16_t nb_tx_desc __rte_unused,
863                 unsigned int socket_id __rte_unused,
864                 const struct rte_eth_txconf *tx_conf __rte_unused)
865 {
866         struct pmd_internals *internals = dev->data->dev_private;
867         struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
868
869         pcap_q->port_id = dev->data->port_id;
870         pcap_q->queue_id = tx_queue_id;
871         dev->data->tx_queues[tx_queue_id] = pcap_q;
872
873         return 0;
874 }
875
876 static int
877 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
878 {
879         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
880
881         return 0;
882 }
883
884 static int
885 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
886 {
887         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
888
889         return 0;
890 }
891
892 static int
893 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
894 {
895         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
896
897         return 0;
898 }
899
900 static int
901 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
902 {
903         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
904
905         return 0;
906 }
907
908 static const struct eth_dev_ops ops = {
909         .dev_start = eth_dev_start,
910         .dev_stop = eth_dev_stop,
911         .dev_close = eth_dev_close,
912         .dev_configure = eth_dev_configure,
913         .dev_infos_get = eth_dev_info,
914         .rx_queue_setup = eth_rx_queue_setup,
915         .tx_queue_setup = eth_tx_queue_setup,
916         .rx_queue_start = eth_rx_queue_start,
917         .tx_queue_start = eth_tx_queue_start,
918         .rx_queue_stop = eth_rx_queue_stop,
919         .tx_queue_stop = eth_tx_queue_stop,
920         .rx_queue_release = eth_queue_release,
921         .tx_queue_release = eth_queue_release,
922         .link_update = eth_link_update,
923         .stats_get = eth_stats_get,
924         .stats_reset = eth_stats_reset,
925 };
926
927 static int
928 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
929                 pcap_t *pcap, pcap_dumper_t *dumper)
930 {
931         if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
932                 return -1;
933         if (pcap)
934                 pmd->queue[pmd->num_of_queue].pcap = pcap;
935         if (dumper)
936                 pmd->queue[pmd->num_of_queue].dumper = dumper;
937         pmd->queue[pmd->num_of_queue].name = name;
938         pmd->queue[pmd->num_of_queue].type = type;
939         pmd->num_of_queue++;
940         return 0;
941 }
942
943 /*
944  * Function handler that opens the pcap file for reading a stores a
945  * reference of it for use it later on.
946  */
947 static int
948 open_rx_pcap(const char *key, const char *value, void *extra_args)
949 {
950         const char *pcap_filename = value;
951         struct pmd_devargs *rx = extra_args;
952         pcap_t *pcap = NULL;
953
954         if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
955                 return -1;
956
957         if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
958                 pcap_close(pcap);
959                 return -1;
960         }
961
962         return 0;
963 }
964
965 /*
966  * Opens a pcap file for writing and stores a reference to it
967  * for use it later on.
968  */
969 static int
970 open_tx_pcap(const char *key, const char *value, void *extra_args)
971 {
972         const char *pcap_filename = value;
973         struct pmd_devargs *dumpers = extra_args;
974         pcap_dumper_t *dumper;
975
976         if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
977                 return -1;
978
979         if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
980                 pcap_dump_close(dumper);
981                 return -1;
982         }
983
984         return 0;
985 }
986
987 /*
988  * Opens an interface for reading and writing
989  */
990 static inline int
991 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
992 {
993         const char *iface = value;
994         struct pmd_devargs *tx = extra_args;
995         pcap_t *pcap = NULL;
996
997         if (open_single_iface(iface, &pcap) < 0)
998                 return -1;
999
1000         tx->queue[0].pcap = pcap;
1001         tx->queue[0].name = iface;
1002         tx->queue[0].type = key;
1003
1004         return 0;
1005 }
1006
1007 static inline int
1008 set_iface_direction(const char *iface, pcap_t *pcap,
1009                 pcap_direction_t direction)
1010 {
1011         const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
1012         if (pcap_setdirection(pcap, direction) < 0) {
1013                 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
1014                                 iface, direction_str, pcap_geterr(pcap));
1015                 return -1;
1016         }
1017         PMD_LOG(INFO, "Setting %s pcap direction %s\n",
1018                         iface, direction_str);
1019         return 0;
1020 }
1021
1022 static inline int
1023 open_iface(const char *key, const char *value, void *extra_args)
1024 {
1025         const char *iface = value;
1026         struct pmd_devargs *pmd = extra_args;
1027         pcap_t *pcap = NULL;
1028
1029         if (open_single_iface(iface, &pcap) < 0)
1030                 return -1;
1031         if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1032                 pcap_close(pcap);
1033                 return -1;
1034         }
1035
1036         return 0;
1037 }
1038
1039 /*
1040  * Opens a NIC for reading packets from it
1041  */
1042 static inline int
1043 open_rx_iface(const char *key, const char *value, void *extra_args)
1044 {
1045         int ret = open_iface(key, value, extra_args);
1046         if (ret < 0)
1047                 return ret;
1048         if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1049                 struct pmd_devargs *pmd = extra_args;
1050                 unsigned int qid = pmd->num_of_queue - 1;
1051
1052                 set_iface_direction(pmd->queue[qid].name,
1053                                 pmd->queue[qid].pcap,
1054                                 PCAP_D_IN);
1055         }
1056
1057         return 0;
1058 }
1059
1060 static inline int
1061 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1062 {
1063         if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1064                         strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1065                 return open_rx_iface(key, value, extra_args);
1066
1067         return 0;
1068 }
1069
1070 /*
1071  * Opens a NIC for writing packets to it
1072  */
1073 static int
1074 open_tx_iface(const char *key, const char *value, void *extra_args)
1075 {
1076         return open_iface(key, value, extra_args);
1077 }
1078
1079 static int
1080 select_phy_mac(const char *key __rte_unused, const char *value,
1081                 void *extra_args)
1082 {
1083         if (extra_args) {
1084                 const int phy_mac = atoi(value);
1085                 int *enable_phy_mac = extra_args;
1086
1087                 if (phy_mac)
1088                         *enable_phy_mac = 1;
1089         }
1090         return 0;
1091 }
1092
1093 static int
1094 get_infinite_rx_arg(const char *key __rte_unused,
1095                 const char *value, void *extra_args)
1096 {
1097         if (extra_args) {
1098                 const int infinite_rx = atoi(value);
1099                 int *enable_infinite_rx = extra_args;
1100
1101                 if (infinite_rx > 0)
1102                         *enable_infinite_rx = 1;
1103         }
1104         return 0;
1105 }
1106
1107 static int
1108 pmd_init_internals(struct rte_vdev_device *vdev,
1109                 const unsigned int nb_rx_queues,
1110                 const unsigned int nb_tx_queues,
1111                 struct pmd_internals **internals,
1112                 struct rte_eth_dev **eth_dev)
1113 {
1114         struct rte_eth_dev_data *data;
1115         struct pmd_process_private *pp;
1116         unsigned int numa_node = vdev->device.numa_node;
1117
1118         PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1119                 numa_node);
1120
1121         pp = (struct pmd_process_private *)
1122                 rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1123                                 RTE_CACHE_LINE_SIZE);
1124
1125         if (pp == NULL) {
1126                 PMD_LOG(ERR,
1127                         "Failed to allocate memory for process private");
1128                 return -1;
1129         }
1130
1131         /* reserve an ethdev entry */
1132         *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1133         if (!(*eth_dev)) {
1134                 rte_free(pp);
1135                 return -1;
1136         }
1137         (*eth_dev)->process_private = pp;
1138         /* now put it all together
1139          * - store queue data in internals,
1140          * - store numa_node info in eth_dev
1141          * - point eth_dev_data to internals
1142          * - and point eth_dev structure to new eth_dev_data structure
1143          */
1144         *internals = (*eth_dev)->data->dev_private;
1145         /*
1146          * Interface MAC = 02:70:63:61:70:<iface_idx>
1147          * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1148          * where the middle 4 characters are converted to hex.
1149          */
1150         (*internals)->eth_addr = (struct rte_ether_addr) {
1151                 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1152         };
1153         (*internals)->phy_mac = 0;
1154         data = (*eth_dev)->data;
1155         data->nb_rx_queues = (uint16_t)nb_rx_queues;
1156         data->nb_tx_queues = (uint16_t)nb_tx_queues;
1157         data->dev_link = pmd_link;
1158         data->mac_addrs = &(*internals)->eth_addr;
1159         data->promiscuous = 1;
1160         data->all_multicast = 1;
1161         data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1162
1163         /*
1164          * NOTE: we'll replace the data element, of originally allocated
1165          * eth_dev so the rings are local per-process
1166          */
1167         (*eth_dev)->dev_ops = &ops;
1168
1169         strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1170                         ETH_PCAP_ARG_MAXLEN);
1171
1172         return 0;
1173 }
1174
1175 static int
1176 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1177                 const unsigned int numa_node)
1178 {
1179 #if defined(RTE_EXEC_ENV_LINUX)
1180         void *mac_addrs;
1181         struct ifreq ifr;
1182         int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1183
1184         if (if_fd == -1)
1185                 return -1;
1186
1187         rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1188         if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1189                 close(if_fd);
1190                 return -1;
1191         }
1192
1193         mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1194         if (!mac_addrs) {
1195                 close(if_fd);
1196                 return -1;
1197         }
1198
1199         PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1200         eth_dev->data->mac_addrs = mac_addrs;
1201         rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1202                         ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1203
1204         close(if_fd);
1205
1206         return 0;
1207
1208 #elif defined(RTE_EXEC_ENV_FREEBSD)
1209         void *mac_addrs;
1210         struct if_msghdr *ifm;
1211         struct sockaddr_dl *sdl;
1212         int mib[6];
1213         size_t len = 0;
1214         char *buf;
1215
1216         mib[0] = CTL_NET;
1217         mib[1] = AF_ROUTE;
1218         mib[2] = 0;
1219         mib[3] = AF_LINK;
1220         mib[4] = NET_RT_IFLIST;
1221         mib[5] = if_nametoindex(if_name);
1222
1223         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1224                 return -1;
1225
1226         if (len == 0)
1227                 return -1;
1228
1229         buf = rte_malloc(NULL, len, 0);
1230         if (!buf)
1231                 return -1;
1232
1233         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1234                 rte_free(buf);
1235                 return -1;
1236         }
1237         ifm = (struct if_msghdr *)buf;
1238         sdl = (struct sockaddr_dl *)(ifm + 1);
1239
1240         mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1241         if (!mac_addrs) {
1242                 rte_free(buf);
1243                 return -1;
1244         }
1245
1246         PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1247         eth_dev->data->mac_addrs = mac_addrs;
1248         rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1249                         LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1250
1251         rte_free(buf);
1252
1253         return 0;
1254 #else
1255         return -1;
1256 #endif
1257 }
1258
1259 static int
1260 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1261                 struct pmd_devargs_all *devargs_all,
1262                 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1263 {
1264         struct pmd_process_private *pp;
1265         struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1266         struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1267         const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1268         const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1269         unsigned int i;
1270
1271         if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1272                         eth_dev) < 0)
1273                 return -1;
1274
1275         pp = (*eth_dev)->process_private;
1276         for (i = 0; i < nb_rx_queues; i++) {
1277                 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1278                 struct devargs_queue *queue = &rx_queues->queue[i];
1279
1280                 pp->rx_pcap[i] = queue->pcap;
1281                 strlcpy(rx->name, queue->name, sizeof(rx->name));
1282                 strlcpy(rx->type, queue->type, sizeof(rx->type));
1283         }
1284
1285         for (i = 0; i < nb_tx_queues; i++) {
1286                 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1287                 struct devargs_queue *queue = &tx_queues->queue[i];
1288
1289                 pp->tx_dumper[i] = queue->dumper;
1290                 pp->tx_pcap[i] = queue->pcap;
1291                 strlcpy(tx->name, queue->name, sizeof(tx->name));
1292                 strlcpy(tx->type, queue->type, sizeof(tx->type));
1293         }
1294
1295         return 0;
1296 }
1297
1298 static int
1299 eth_from_pcaps(struct rte_vdev_device *vdev,
1300                 struct pmd_devargs_all *devargs_all)
1301 {
1302         struct pmd_internals *internals = NULL;
1303         struct rte_eth_dev *eth_dev = NULL;
1304         struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1305         int single_iface = devargs_all->single_iface;
1306         unsigned int infinite_rx = devargs_all->infinite_rx;
1307         int ret;
1308
1309         ret = eth_from_pcaps_common(vdev, devargs_all, &internals, &eth_dev);
1310
1311         if (ret < 0)
1312                 return ret;
1313
1314         /* store weather we are using a single interface for rx/tx or not */
1315         internals->single_iface = single_iface;
1316
1317         if (single_iface) {
1318                 internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1319
1320                 /* phy_mac arg is applied only only if "iface" devarg is provided */
1321                 if (rx_queues->phy_mac) {
1322                         int ret = eth_pcap_update_mac(rx_queues->queue[0].name,
1323                                         eth_dev, vdev->device.numa_node);
1324                         if (ret == 0)
1325                                 internals->phy_mac = 1;
1326                 }
1327         }
1328
1329         internals->infinite_rx = infinite_rx;
1330         /* Assign rx ops. */
1331         if (infinite_rx)
1332                 eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1333         else if (devargs_all->is_rx_pcap || devargs_all->is_rx_iface ||
1334                         single_iface)
1335                 eth_dev->rx_pkt_burst = eth_pcap_rx;
1336         else
1337                 eth_dev->rx_pkt_burst = eth_null_rx;
1338
1339         /* Assign tx ops. */
1340         if (devargs_all->is_tx_pcap)
1341                 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1342         else if (devargs_all->is_tx_iface || single_iface)
1343                 eth_dev->tx_pkt_burst = eth_pcap_tx;
1344         else
1345                 eth_dev->tx_pkt_burst = eth_tx_drop;
1346
1347         rte_eth_dev_probing_finish(eth_dev);
1348         return 0;
1349 }
1350
1351 static int
1352 pmd_pcap_probe(struct rte_vdev_device *dev)
1353 {
1354         const char *name;
1355         struct rte_kvargs *kvlist;
1356         struct pmd_devargs pcaps = {0};
1357         struct pmd_devargs dumpers = {0};
1358         struct rte_eth_dev *eth_dev =  NULL;
1359         struct pmd_internals *internal;
1360         int ret = 0;
1361
1362         struct pmd_devargs_all devargs_all = {
1363                 .single_iface = 0,
1364                 .is_tx_pcap = 0,
1365                 .is_tx_iface = 0,
1366                 .infinite_rx = 0,
1367         };
1368
1369         name = rte_vdev_device_name(dev);
1370         PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1371
1372         gettimeofday(&start_time, NULL);
1373         start_cycles = rte_get_timer_cycles();
1374         hz = rte_get_timer_hz();
1375
1376         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1377                 eth_dev = rte_eth_dev_attach_secondary(name);
1378                 if (!eth_dev) {
1379                         PMD_LOG(ERR, "Failed to probe %s", name);
1380                         return -1;
1381                 }
1382
1383                 internal = eth_dev->data->dev_private;
1384
1385                 kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1386                 if (kvlist == NULL)
1387                         return -1;
1388         } else {
1389                 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1390                                 valid_arguments);
1391                 if (kvlist == NULL)
1392                         return -1;
1393         }
1394
1395         /*
1396          * If iface argument is passed we open the NICs and use them for
1397          * reading / writing
1398          */
1399         if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1400
1401                 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1402                                 &open_rx_tx_iface, &pcaps);
1403                 if (ret < 0)
1404                         goto free_kvlist;
1405
1406                 dumpers.queue[0] = pcaps.queue[0];
1407
1408                 ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1409                                 &select_phy_mac, &pcaps.phy_mac);
1410                 if (ret < 0)
1411                         goto free_kvlist;
1412
1413                 dumpers.phy_mac = pcaps.phy_mac;
1414
1415                 devargs_all.single_iface = 1;
1416                 pcaps.num_of_queue = 1;
1417                 dumpers.num_of_queue = 1;
1418
1419                 goto create_eth;
1420         }
1421
1422         /*
1423          * We check whether we want to open a RX stream from a real NIC, a
1424          * pcap file or open a dummy RX stream
1425          */
1426         devargs_all.is_rx_pcap =
1427                 rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1428         devargs_all.is_rx_iface =
1429                 (rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_ARG) +
1430                  rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_IN_ARG)) ? 1 : 0;
1431         pcaps.num_of_queue = 0;
1432
1433         devargs_all.is_tx_pcap =
1434                 rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1435         devargs_all.is_tx_iface =
1436                 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1437         dumpers.num_of_queue = 0;
1438
1439         if (devargs_all.is_rx_pcap) {
1440                 /*
1441                  * We check whether we want to infinitely rx the pcap file.
1442                  */
1443                 unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1444                                 ETH_PCAP_INFINITE_RX_ARG);
1445
1446                 if (infinite_rx_arg_cnt == 1) {
1447                         ret = rte_kvargs_process(kvlist,
1448                                         ETH_PCAP_INFINITE_RX_ARG,
1449                                         &get_infinite_rx_arg,
1450                                         &devargs_all.infinite_rx);
1451                         if (ret < 0)
1452                                 goto free_kvlist;
1453                         PMD_LOG(INFO, "infinite_rx has been %s for %s",
1454                                         devargs_all.infinite_rx ? "enabled" : "disabled",
1455                                         name);
1456
1457                 } else if (infinite_rx_arg_cnt > 1) {
1458                         PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1459                                         "argument has been provided more than once "
1460                                         "for %s", name);
1461                 }
1462
1463                 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1464                                 &open_rx_pcap, &pcaps);
1465         } else if (devargs_all.is_rx_iface) {
1466                 ret = rte_kvargs_process(kvlist, NULL,
1467                                 &rx_iface_args_process, &pcaps);
1468         } else if (devargs_all.is_tx_iface || devargs_all.is_tx_pcap) {
1469                 unsigned int i;
1470
1471                 /* Count number of tx queue args passed before dummy rx queue
1472                  * creation so a dummy rx queue can be created for each tx queue
1473                  */
1474                 unsigned int num_tx_queues =
1475                         (rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) +
1476                         rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG));
1477
1478                 PMD_LOG(INFO, "Creating null rx queue since no rx queues were provided.");
1479
1480                 /* Creating a dummy rx queue for each tx queue passed */
1481                 for (i = 0; i < num_tx_queues; i++)
1482                         ret = add_queue(&pcaps, "dummy_rx", "rx_null", NULL,
1483                                         NULL);
1484         } else {
1485                 PMD_LOG(ERR, "Error - No rx or tx queues provided");
1486                 ret = -ENOENT;
1487         }
1488         if (ret < 0)
1489                 goto free_kvlist;
1490
1491         /*
1492          * We check whether we want to open a TX stream to a real NIC,
1493          * a pcap file, or drop packets on tx
1494          */
1495         if (devargs_all.is_tx_pcap) {
1496                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1497                                 &open_tx_pcap, &dumpers);
1498         } else if (devargs_all.is_tx_iface) {
1499                 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1500                                 &open_tx_iface, &dumpers);
1501         } else {
1502                 unsigned int i;
1503
1504                 PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1505
1506                 /* Add 1 dummy queue per rxq which counts and drops packets. */
1507                 for (i = 0; i < pcaps.num_of_queue; i++)
1508                         ret = add_queue(&dumpers, "dummy_tx", "tx_drop", NULL,
1509                                         NULL);
1510         }
1511
1512         if (ret < 0)
1513                 goto free_kvlist;
1514
1515 create_eth:
1516         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1517                 struct pmd_process_private *pp;
1518                 unsigned int i;
1519
1520                 internal = eth_dev->data->dev_private;
1521                         pp = (struct pmd_process_private *)
1522                                 rte_zmalloc(NULL,
1523                                         sizeof(struct pmd_process_private),
1524                                         RTE_CACHE_LINE_SIZE);
1525
1526                 if (pp == NULL) {
1527                         PMD_LOG(ERR,
1528                                 "Failed to allocate memory for process private");
1529                         ret = -1;
1530                         goto free_kvlist;
1531                 }
1532
1533                 eth_dev->dev_ops = &ops;
1534                 eth_dev->device = &dev->device;
1535
1536                 /* setup process private */
1537                 for (i = 0; i < pcaps.num_of_queue; i++)
1538                         pp->rx_pcap[i] = pcaps.queue[i].pcap;
1539
1540                 for (i = 0; i < dumpers.num_of_queue; i++) {
1541                         pp->tx_dumper[i] = dumpers.queue[i].dumper;
1542                         pp->tx_pcap[i] = dumpers.queue[i].pcap;
1543                 }
1544
1545                 eth_dev->process_private = pp;
1546                 eth_dev->rx_pkt_burst = eth_pcap_rx;
1547                 if (devargs_all.is_tx_pcap)
1548                         eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1549                 else
1550                         eth_dev->tx_pkt_burst = eth_pcap_tx;
1551
1552                 rte_eth_dev_probing_finish(eth_dev);
1553                 goto free_kvlist;
1554         }
1555
1556         devargs_all.rx_queues = pcaps;
1557         devargs_all.tx_queues = dumpers;
1558
1559         ret = eth_from_pcaps(dev, &devargs_all);
1560
1561 free_kvlist:
1562         rte_kvargs_free(kvlist);
1563
1564         return ret;
1565 }
1566
1567 static int
1568 pmd_pcap_remove(struct rte_vdev_device *dev)
1569 {
1570         struct rte_eth_dev *eth_dev = NULL;
1571
1572         if (!dev)
1573                 return -1;
1574
1575         eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1576         if (eth_dev == NULL)
1577                 return 0; /* port already released */
1578
1579         eth_dev_close(eth_dev);
1580         rte_eth_dev_release_port(eth_dev);
1581
1582         return 0;
1583 }
1584
1585 static struct rte_vdev_driver pmd_pcap_drv = {
1586         .probe = pmd_pcap_probe,
1587         .remove = pmd_pcap_remove,
1588 };
1589
1590 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1591 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1592 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1593         ETH_PCAP_RX_PCAP_ARG "=<string> "
1594         ETH_PCAP_TX_PCAP_ARG "=<string> "
1595         ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1596         ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1597         ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1598         ETH_PCAP_IFACE_ARG "=<ifc> "
1599         ETH_PCAP_PHY_MAC_ARG "=<int>"
1600         ETH_PCAP_INFINITE_RX_ARG "=<0|1>");