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