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