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