1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
14 #if defined(RTE_EXEC_ENV_FREEBSD)
15 #include <sys/sysctl.h>
16 #include <net/if_dl.h>
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>
27 #include <rte_bus_vdev.h>
28 #include <rte_string_fns.h>
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
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"
44 #define ETH_PCAP_ARG_MAXLEN 64
46 #define RTE_PMD_PCAP_MAX_QUEUES 16
48 static char errbuf[PCAP_ERRBUF_SIZE];
49 static struct timeval start_time;
50 static uint64_t start_cycles;
52 static uint8_t iface_idx;
54 static uint64_t timestamp_rx_dynflag;
55 static int timestamp_dynfield_offset = -1;
58 volatile unsigned long pkts;
59 volatile unsigned long bytes;
60 volatile unsigned long err_pkts;
63 struct pcap_rx_queue {
66 struct rte_mempool *mb_pool;
67 struct queue_stat rx_stat;
69 char type[ETH_PCAP_ARG_MAXLEN];
71 /* Contains pre-generated packets to be looped through */
72 struct rte_ring *pkts;
75 struct pcap_tx_queue {
78 struct queue_stat tx_stat;
80 char type[ETH_PCAP_ARG_MAXLEN];
83 struct pmd_internals {
84 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
85 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
86 char devargs[ETH_PCAP_ARG_MAXLEN];
87 struct rte_ether_addr eth_addr;
91 unsigned int infinite_rx;
94 struct pmd_process_private {
95 pcap_t *rx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
96 pcap_t *tx_pcap[RTE_PMD_PCAP_MAX_QUEUES];
97 pcap_dumper_t *tx_dumper[RTE_PMD_PCAP_MAX_QUEUES];
101 unsigned int num_of_queue;
102 struct devargs_queue {
103 pcap_dumper_t *dumper;
107 } queue[RTE_PMD_PCAP_MAX_QUEUES];
111 struct pmd_devargs_all {
112 struct pmd_devargs rx_queues;
113 struct pmd_devargs tx_queues;
115 unsigned int is_tx_pcap;
116 unsigned int is_tx_iface;
117 unsigned int is_rx_pcap;
118 unsigned int is_rx_iface;
119 unsigned int infinite_rx;
122 static const char *valid_arguments[] = {
123 ETH_PCAP_RX_PCAP_ARG,
124 ETH_PCAP_TX_PCAP_ARG,
125 ETH_PCAP_RX_IFACE_ARG,
126 ETH_PCAP_RX_IFACE_IN_ARG,
127 ETH_PCAP_TX_IFACE_ARG,
129 ETH_PCAP_PHY_MAC_ARG,
130 ETH_PCAP_INFINITE_RX_ARG,
134 static struct rte_eth_link pmd_link = {
135 .link_speed = ETH_SPEED_NUM_10G,
136 .link_duplex = ETH_LINK_FULL_DUPLEX,
137 .link_status = ETH_LINK_DOWN,
138 .link_autoneg = ETH_LINK_FIXED,
141 RTE_LOG_REGISTER(eth_pcap_logtype, pmd.net.pcap, NOTICE);
143 #define PMD_LOG(level, fmt, args...) \
144 rte_log(RTE_LOG_ ## level, eth_pcap_logtype, \
145 "%s(): " fmt "\n", __func__, ##args)
148 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
149 const u_char *data, uint16_t data_len)
151 /* Copy the first segment. */
152 uint16_t len = rte_pktmbuf_tailroom(mbuf);
153 struct rte_mbuf *m = mbuf;
155 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
159 while (data_len > 0) {
160 /* Allocate next mbuf and point to that. */
161 m->next = rte_pktmbuf_alloc(mb_pool);
163 if (unlikely(!m->next))
168 /* Headroom is not needed in chained mbufs. */
169 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
173 /* Copy next segment. */
174 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
175 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
182 return mbuf->nb_segs;
186 eth_pcap_rx_infinite(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
189 struct pcap_rx_queue *pcap_q = queue;
190 uint32_t rx_bytes = 0;
192 if (unlikely(nb_pkts == 0))
195 if (rte_pktmbuf_alloc_bulk(pcap_q->mb_pool, bufs, nb_pkts) != 0)
198 for (i = 0; i < nb_pkts; i++) {
199 struct rte_mbuf *pcap_buf;
200 int err = rte_ring_dequeue(pcap_q->pkts, (void **)&pcap_buf);
204 rte_memcpy(rte_pktmbuf_mtod(bufs[i], void *),
205 rte_pktmbuf_mtod(pcap_buf, void *),
207 bufs[i]->data_len = pcap_buf->data_len;
208 bufs[i]->pkt_len = pcap_buf->pkt_len;
209 bufs[i]->port = pcap_q->port_id;
210 rx_bytes += pcap_buf->data_len;
212 /* Enqueue packet back on ring to allow infinite rx. */
213 rte_ring_enqueue(pcap_q->pkts, pcap_buf);
216 pcap_q->rx_stat.pkts += i;
217 pcap_q->rx_stat.bytes += rx_bytes;
223 eth_pcap_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
226 struct pcap_pkthdr header;
227 struct pmd_process_private *pp;
228 const u_char *packet;
229 struct rte_mbuf *mbuf;
230 struct pcap_rx_queue *pcap_q = queue;
232 uint32_t rx_bytes = 0;
235 pp = rte_eth_devices[pcap_q->port_id].process_private;
236 pcap = pp->rx_pcap[pcap_q->queue_id];
238 if (unlikely(pcap == NULL || nb_pkts == 0))
241 /* Reads the given number of packets from the pcap file one by one
242 * and copies the packet data into a newly allocated mbuf to return.
244 for (i = 0; i < nb_pkts; i++) {
245 /* Get the next PCAP packet */
246 packet = pcap_next(pcap, &header);
247 if (unlikely(packet == NULL))
250 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
251 if (unlikely(mbuf == NULL))
254 if (header.caplen <= rte_pktmbuf_tailroom(mbuf)) {
255 /* pcap packet will fit in the mbuf, can copy it */
256 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
258 mbuf->data_len = (uint16_t)header.caplen;
260 /* Try read jumbo frame into multi mbufs. */
261 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
264 header.caplen) == -1)) {
265 rte_pktmbuf_free(mbuf);
270 mbuf->pkt_len = (uint16_t)header.caplen;
271 *RTE_MBUF_DYNFIELD(mbuf, timestamp_dynfield_offset,
272 rte_mbuf_timestamp_t *) =
273 (uint64_t)header.ts.tv_sec * 1000000 +
275 mbuf->ol_flags |= timestamp_rx_dynflag;
276 mbuf->port = pcap_q->port_id;
279 rx_bytes += header.caplen;
281 pcap_q->rx_stat.pkts += num_rx;
282 pcap_q->rx_stat.bytes += rx_bytes;
288 eth_null_rx(void *queue __rte_unused,
289 struct rte_mbuf **bufs __rte_unused,
290 uint16_t nb_pkts __rte_unused)
295 #define NSEC_PER_SEC 1000000000L
298 calculate_timestamp(struct timeval *ts) {
300 struct timeval cur_time;
302 cycles = rte_get_timer_cycles() - start_cycles;
303 cur_time.tv_sec = cycles / hz;
304 cur_time.tv_usec = (cycles % hz) * NSEC_PER_SEC / hz;
306 ts->tv_sec = start_time.tv_sec + cur_time.tv_sec;
307 ts->tv_usec = start_time.tv_usec + cur_time.tv_usec;
308 if (ts->tv_usec >= NSEC_PER_SEC) {
309 ts->tv_usec -= NSEC_PER_SEC;
315 * Callback to handle writing packets to a pcap file.
318 eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
321 struct rte_mbuf *mbuf;
322 struct pmd_process_private *pp;
323 struct pcap_tx_queue *dumper_q = queue;
325 uint32_t tx_bytes = 0;
326 struct pcap_pkthdr header;
327 pcap_dumper_t *dumper;
328 unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
331 pp = rte_eth_devices[dumper_q->port_id].process_private;
332 dumper = pp->tx_dumper[dumper_q->queue_id];
334 if (dumper == NULL || nb_pkts == 0)
337 /* writes the nb_pkts packets to the previously opened pcap file
339 for (i = 0; i < nb_pkts; i++) {
341 len = caplen = rte_pktmbuf_pkt_len(mbuf);
342 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
343 len > sizeof(temp_data))) {
344 caplen = sizeof(temp_data);
347 calculate_timestamp(&header.ts);
349 header.caplen = caplen;
350 /* rte_pktmbuf_read() returns a pointer to the data directly
351 * in the mbuf (when the mbuf is contiguous) or, otherwise,
352 * a pointer to temp_data after copying into it.
354 pcap_dump((u_char *)dumper, &header,
355 rte_pktmbuf_read(mbuf, 0, caplen, temp_data));
359 rte_pktmbuf_free(mbuf);
363 * Since there's no place to hook a callback when the forwarding
364 * process stops and to make sure the pcap file is actually written,
365 * we flush the pcap dumper within each burst.
367 pcap_dump_flush(dumper);
368 dumper_q->tx_stat.pkts += num_tx;
369 dumper_q->tx_stat.bytes += tx_bytes;
370 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
376 * Callback to handle dropping packets in the infinite rx case.
379 eth_tx_drop(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
382 uint32_t tx_bytes = 0;
383 struct pcap_tx_queue *tx_queue = queue;
385 if (unlikely(nb_pkts == 0))
388 for (i = 0; i < nb_pkts; i++) {
389 tx_bytes += bufs[i]->pkt_len;
390 rte_pktmbuf_free(bufs[i]);
393 tx_queue->tx_stat.pkts += nb_pkts;
394 tx_queue->tx_stat.bytes += tx_bytes;
400 * Callback to handle sending packets through a real NIC.
403 eth_pcap_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
407 struct rte_mbuf *mbuf;
408 struct pmd_process_private *pp;
409 struct pcap_tx_queue *tx_queue = queue;
411 uint32_t tx_bytes = 0;
413 unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN];
416 pp = rte_eth_devices[tx_queue->port_id].process_private;
417 pcap = pp->tx_pcap[tx_queue->queue_id];
419 if (unlikely(nb_pkts == 0 || pcap == NULL))
422 for (i = 0; i < nb_pkts; i++) {
424 len = rte_pktmbuf_pkt_len(mbuf);
425 if (unlikely(!rte_pktmbuf_is_contiguous(mbuf) &&
426 len > sizeof(temp_data))) {
428 "Dropping multi segment PCAP packet. Size (%zd) > max size (%zd).",
429 len, sizeof(temp_data));
430 rte_pktmbuf_free(mbuf);
434 /* rte_pktmbuf_read() returns a pointer to the data directly
435 * in the mbuf (when the mbuf is contiguous) or, otherwise,
436 * a pointer to temp_data after copying into it.
438 ret = pcap_sendpacket(pcap,
439 rte_pktmbuf_read(mbuf, 0, len, temp_data), len);
440 if (unlikely(ret != 0))
444 rte_pktmbuf_free(mbuf);
447 tx_queue->tx_stat.pkts += num_tx;
448 tx_queue->tx_stat.bytes += tx_bytes;
449 tx_queue->tx_stat.err_pkts += i - num_tx;
455 * pcap_open_live wrapper function
458 open_iface_live(const char *iface, pcap_t **pcap) {
459 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
460 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
463 PMD_LOG(ERR, "Couldn't open %s: %s", iface, errbuf);
471 open_single_iface(const char *iface, pcap_t **pcap)
473 if (open_iface_live(iface, pcap) < 0) {
474 PMD_LOG(ERR, "Couldn't open interface %s", iface);
482 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
487 * We need to create a dummy empty pcap_t to use it
488 * with pcap_dump_open(). We create big enough an Ethernet
491 tx_pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB,
492 RTE_ETH_PCAP_SNAPSHOT_LEN, PCAP_TSTAMP_PRECISION_NANO);
493 if (tx_pcap == NULL) {
494 PMD_LOG(ERR, "Couldn't create dead pcap");
498 /* The dumper is created using the previous pcap_t reference */
499 *dumper = pcap_dump_open(tx_pcap, pcap_filename);
500 if (*dumper == NULL) {
502 PMD_LOG(ERR, "Couldn't open %s for writing.",
512 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
514 *pcap = pcap_open_offline(pcap_filename, errbuf);
516 PMD_LOG(ERR, "Couldn't open %s: %s", pcap_filename,
525 count_packets_in_pcap(pcap_t **pcap, struct pcap_rx_queue *pcap_q)
527 const u_char *packet;
528 struct pcap_pkthdr header;
529 uint64_t pcap_pkt_count = 0;
531 while ((packet = pcap_next(*pcap, &header)))
534 /* The pcap is reopened so it can be used as normal later. */
537 open_single_rx_pcap(pcap_q->name, pcap);
539 return pcap_pkt_count;
543 eth_dev_start(struct rte_eth_dev *dev)
546 struct pmd_internals *internals = dev->data->dev_private;
547 struct pmd_process_private *pp = dev->process_private;
548 struct pcap_tx_queue *tx;
549 struct pcap_rx_queue *rx;
551 /* Special iface case. Single pcap is open and shared between tx/rx. */
552 if (internals->single_iface) {
553 tx = &internals->tx_queue[0];
554 rx = &internals->rx_queue[0];
556 if (!pp->tx_pcap[0] &&
557 strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
558 if (open_single_iface(tx->name, &pp->tx_pcap[0]) < 0)
560 pp->rx_pcap[0] = pp->tx_pcap[0];
566 /* If not open already, open tx pcaps/dumpers */
567 for (i = 0; i < dev->data->nb_tx_queues; i++) {
568 tx = &internals->tx_queue[i];
570 if (!pp->tx_dumper[i] &&
571 strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
572 if (open_single_tx_pcap(tx->name,
573 &pp->tx_dumper[i]) < 0)
575 } else if (!pp->tx_pcap[i] &&
576 strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
577 if (open_single_iface(tx->name, &pp->tx_pcap[i]) < 0)
582 /* If not open already, open rx pcaps */
583 for (i = 0; i < dev->data->nb_rx_queues; i++) {
584 rx = &internals->rx_queue[i];
586 if (pp->rx_pcap[i] != NULL)
589 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
590 if (open_single_rx_pcap(rx->name, &pp->rx_pcap[i]) < 0)
592 } else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
593 if (open_single_iface(rx->name, &pp->rx_pcap[i]) < 0)
599 for (i = 0; i < dev->data->nb_rx_queues; i++)
600 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
602 for (i = 0; i < dev->data->nb_tx_queues; i++)
603 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
605 dev->data->dev_link.link_status = ETH_LINK_UP;
611 * This function gets called when the current port gets stopped.
612 * Is the only place for us to close all the tx streams dumpers.
613 * If not called the dumpers will be flushed within each tx burst.
616 eth_dev_stop(struct rte_eth_dev *dev)
619 struct pmd_internals *internals = dev->data->dev_private;
620 struct pmd_process_private *pp = dev->process_private;
622 /* Special iface case. Single pcap is open and shared between tx/rx. */
623 if (internals->single_iface) {
624 pcap_close(pp->tx_pcap[0]);
625 pp->tx_pcap[0] = NULL;
626 pp->rx_pcap[0] = NULL;
630 for (i = 0; i < dev->data->nb_tx_queues; i++) {
631 if (pp->tx_dumper[i] != NULL) {
632 pcap_dump_close(pp->tx_dumper[i]);
633 pp->tx_dumper[i] = NULL;
636 if (pp->tx_pcap[i] != NULL) {
637 pcap_close(pp->tx_pcap[i]);
638 pp->tx_pcap[i] = NULL;
642 for (i = 0; i < dev->data->nb_rx_queues; i++) {
643 if (pp->rx_pcap[i] != NULL) {
644 pcap_close(pp->rx_pcap[i]);
645 pp->rx_pcap[i] = NULL;
650 for (i = 0; i < dev->data->nb_rx_queues; i++)
651 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
653 for (i = 0; i < dev->data->nb_tx_queues; i++)
654 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
656 dev->data->dev_link.link_status = ETH_LINK_DOWN;
662 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
668 eth_dev_info(struct rte_eth_dev *dev,
669 struct rte_eth_dev_info *dev_info)
671 struct pmd_internals *internals = dev->data->dev_private;
673 dev_info->if_index = internals->if_index;
674 dev_info->max_mac_addrs = 1;
675 dev_info->max_rx_pktlen = (uint32_t) -1;
676 dev_info->max_rx_queues = dev->data->nb_rx_queues;
677 dev_info->max_tx_queues = dev->data->nb_tx_queues;
678 dev_info->min_rx_bufsize = 0;
684 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
687 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
688 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
689 unsigned long tx_packets_err_total = 0;
690 const struct pmd_internals *internal = dev->data->dev_private;
692 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
693 i < dev->data->nb_rx_queues; i++) {
694 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
695 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
696 rx_packets_total += stats->q_ipackets[i];
697 rx_bytes_total += stats->q_ibytes[i];
700 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
701 i < dev->data->nb_tx_queues; i++) {
702 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
703 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
704 tx_packets_total += stats->q_opackets[i];
705 tx_bytes_total += stats->q_obytes[i];
706 tx_packets_err_total += internal->tx_queue[i].tx_stat.err_pkts;
709 stats->ipackets = rx_packets_total;
710 stats->ibytes = rx_bytes_total;
711 stats->opackets = tx_packets_total;
712 stats->obytes = tx_bytes_total;
713 stats->oerrors = tx_packets_err_total;
719 eth_stats_reset(struct rte_eth_dev *dev)
722 struct pmd_internals *internal = dev->data->dev_private;
724 for (i = 0; i < dev->data->nb_rx_queues; i++) {
725 internal->rx_queue[i].rx_stat.pkts = 0;
726 internal->rx_queue[i].rx_stat.bytes = 0;
729 for (i = 0; i < dev->data->nb_tx_queues; i++) {
730 internal->tx_queue[i].tx_stat.pkts = 0;
731 internal->tx_queue[i].tx_stat.bytes = 0;
732 internal->tx_queue[i].tx_stat.err_pkts = 0;
739 infinite_rx_ring_free(struct rte_ring *pkts)
741 struct rte_mbuf *bufs;
743 while (!rte_ring_dequeue(pkts, (void **)&bufs))
744 rte_pktmbuf_free(bufs);
750 eth_dev_close(struct rte_eth_dev *dev)
753 struct pmd_internals *internals = dev->data->dev_private;
755 PMD_LOG(INFO, "Closing pcap ethdev on NUMA socket %d",
758 rte_free(dev->process_private);
760 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
763 /* Device wide flag, but cleanup must be performed per queue. */
764 if (internals->infinite_rx) {
765 for (i = 0; i < dev->data->nb_rx_queues; i++) {
766 struct pcap_rx_queue *pcap_q = &internals->rx_queue[i];
769 * 'pcap_q->pkts' can be NULL if 'eth_dev_close()'
770 * called before 'eth_rx_queue_setup()' has been called
772 if (pcap_q->pkts == NULL)
775 infinite_rx_ring_free(pcap_q->pkts);
779 if (internals->phy_mac == 0)
780 /* not dynamically allocated, must not be freed */
781 dev->data->mac_addrs = NULL;
787 eth_queue_release(void *q __rte_unused)
792 eth_link_update(struct rte_eth_dev *dev __rte_unused,
793 int wait_to_complete __rte_unused)
799 eth_rx_queue_setup(struct rte_eth_dev *dev,
800 uint16_t rx_queue_id,
801 uint16_t nb_rx_desc __rte_unused,
802 unsigned int socket_id __rte_unused,
803 const struct rte_eth_rxconf *rx_conf __rte_unused,
804 struct rte_mempool *mb_pool)
806 struct pmd_internals *internals = dev->data->dev_private;
807 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
809 pcap_q->mb_pool = mb_pool;
810 pcap_q->port_id = dev->data->port_id;
811 pcap_q->queue_id = rx_queue_id;
812 dev->data->rx_queues[rx_queue_id] = pcap_q;
814 if (internals->infinite_rx) {
815 struct pmd_process_private *pp;
816 char ring_name[NAME_MAX];
817 static uint32_t ring_number;
818 uint64_t pcap_pkt_count = 0;
819 struct rte_mbuf *bufs[1];
822 pp = rte_eth_devices[pcap_q->port_id].process_private;
823 pcap = &pp->rx_pcap[pcap_q->queue_id];
825 if (unlikely(*pcap == NULL))
828 pcap_pkt_count = count_packets_in_pcap(pcap, pcap_q);
830 snprintf(ring_name, sizeof(ring_name), "PCAP_RING%" PRIu16,
833 pcap_q->pkts = rte_ring_create(ring_name,
834 rte_align64pow2(pcap_pkt_count + 1), 0,
835 RING_F_SP_ENQ | RING_F_SC_DEQ);
840 /* Fill ring with packets from PCAP file one by one. */
841 while (eth_pcap_rx(pcap_q, bufs, 1)) {
842 /* Check for multiseg mbufs. */
843 if (bufs[0]->nb_segs != 1) {
844 infinite_rx_ring_free(pcap_q->pkts);
846 "Multiseg mbufs are not supported in infinite_rx mode.");
850 rte_ring_enqueue_bulk(pcap_q->pkts,
851 (void * const *)bufs, 1, NULL);
854 if (rte_ring_count(pcap_q->pkts) < pcap_pkt_count) {
855 infinite_rx_ring_free(pcap_q->pkts);
857 "Not enough mbufs to accommodate packets in pcap file. "
858 "At least %" PRIu64 " mbufs per queue is required.",
864 * Reset the stats for this queue since eth_pcap_rx calls above
865 * didn't result in the application receiving packets.
867 pcap_q->rx_stat.pkts = 0;
868 pcap_q->rx_stat.bytes = 0;
875 eth_tx_queue_setup(struct rte_eth_dev *dev,
876 uint16_t tx_queue_id,
877 uint16_t nb_tx_desc __rte_unused,
878 unsigned int socket_id __rte_unused,
879 const struct rte_eth_txconf *tx_conf __rte_unused)
881 struct pmd_internals *internals = dev->data->dev_private;
882 struct pcap_tx_queue *pcap_q = &internals->tx_queue[tx_queue_id];
884 pcap_q->port_id = dev->data->port_id;
885 pcap_q->queue_id = tx_queue_id;
886 dev->data->tx_queues[tx_queue_id] = pcap_q;
892 eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
894 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
900 eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
902 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
908 eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
910 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
916 eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
918 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
923 static const struct eth_dev_ops ops = {
924 .dev_start = eth_dev_start,
925 .dev_stop = eth_dev_stop,
926 .dev_close = eth_dev_close,
927 .dev_configure = eth_dev_configure,
928 .dev_infos_get = eth_dev_info,
929 .rx_queue_setup = eth_rx_queue_setup,
930 .tx_queue_setup = eth_tx_queue_setup,
931 .rx_queue_start = eth_rx_queue_start,
932 .tx_queue_start = eth_tx_queue_start,
933 .rx_queue_stop = eth_rx_queue_stop,
934 .tx_queue_stop = eth_tx_queue_stop,
935 .rx_queue_release = eth_queue_release,
936 .tx_queue_release = eth_queue_release,
937 .link_update = eth_link_update,
938 .stats_get = eth_stats_get,
939 .stats_reset = eth_stats_reset,
943 add_queue(struct pmd_devargs *pmd, const char *name, const char *type,
944 pcap_t *pcap, pcap_dumper_t *dumper)
946 if (pmd->num_of_queue >= RTE_PMD_PCAP_MAX_QUEUES)
949 pmd->queue[pmd->num_of_queue].pcap = pcap;
951 pmd->queue[pmd->num_of_queue].dumper = dumper;
952 pmd->queue[pmd->num_of_queue].name = name;
953 pmd->queue[pmd->num_of_queue].type = type;
959 * Function handler that opens the pcap file for reading a stores a
960 * reference of it for use it later on.
963 open_rx_pcap(const char *key, const char *value, void *extra_args)
965 const char *pcap_filename = value;
966 struct pmd_devargs *rx = extra_args;
969 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
972 if (add_queue(rx, pcap_filename, key, pcap, NULL) < 0) {
981 * Opens a pcap file for writing and stores a reference to it
982 * for use it later on.
985 open_tx_pcap(const char *key, const char *value, void *extra_args)
987 const char *pcap_filename = value;
988 struct pmd_devargs *dumpers = extra_args;
989 pcap_dumper_t *dumper;
991 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
994 if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) {
995 pcap_dump_close(dumper);
1003 * Opens an interface for reading and writing
1006 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
1008 const char *iface = value;
1009 struct pmd_devargs *tx = extra_args;
1010 pcap_t *pcap = NULL;
1012 if (open_single_iface(iface, &pcap) < 0)
1015 tx->queue[0].pcap = pcap;
1016 tx->queue[0].name = iface;
1017 tx->queue[0].type = key;
1023 set_iface_direction(const char *iface, pcap_t *pcap,
1024 pcap_direction_t direction)
1026 const char *direction_str = (direction == PCAP_D_IN) ? "IN" : "OUT";
1027 if (pcap_setdirection(pcap, direction) < 0) {
1028 PMD_LOG(ERR, "Setting %s pcap direction %s failed - %s\n",
1029 iface, direction_str, pcap_geterr(pcap));
1032 PMD_LOG(INFO, "Setting %s pcap direction %s\n",
1033 iface, direction_str);
1038 open_iface(const char *key, const char *value, void *extra_args)
1040 const char *iface = value;
1041 struct pmd_devargs *pmd = extra_args;
1042 pcap_t *pcap = NULL;
1044 if (open_single_iface(iface, &pcap) < 0)
1046 if (add_queue(pmd, iface, key, pcap, NULL) < 0) {
1055 * Opens a NIC for reading packets from it
1058 open_rx_iface(const char *key, const char *value, void *extra_args)
1060 int ret = open_iface(key, value, extra_args);
1063 if (strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0) {
1064 struct pmd_devargs *pmd = extra_args;
1065 unsigned int qid = pmd->num_of_queue - 1;
1067 set_iface_direction(pmd->queue[qid].name,
1068 pmd->queue[qid].pcap,
1076 rx_iface_args_process(const char *key, const char *value, void *extra_args)
1078 if (strcmp(key, ETH_PCAP_RX_IFACE_ARG) == 0 ||
1079 strcmp(key, ETH_PCAP_RX_IFACE_IN_ARG) == 0)
1080 return open_rx_iface(key, value, extra_args);
1086 * Opens a NIC for writing packets to it
1089 open_tx_iface(const char *key, const char *value, void *extra_args)
1091 return open_iface(key, value, extra_args);
1095 select_phy_mac(const char *key __rte_unused, const char *value,
1099 const int phy_mac = atoi(value);
1100 int *enable_phy_mac = extra_args;
1103 *enable_phy_mac = 1;
1109 get_infinite_rx_arg(const char *key __rte_unused,
1110 const char *value, void *extra_args)
1113 const int infinite_rx = atoi(value);
1114 int *enable_infinite_rx = extra_args;
1116 if (infinite_rx > 0)
1117 *enable_infinite_rx = 1;
1123 pmd_init_internals(struct rte_vdev_device *vdev,
1124 const unsigned int nb_rx_queues,
1125 const unsigned int nb_tx_queues,
1126 struct pmd_internals **internals,
1127 struct rte_eth_dev **eth_dev)
1129 struct rte_eth_dev_data *data;
1130 struct pmd_process_private *pp;
1131 unsigned int numa_node = vdev->device.numa_node;
1133 PMD_LOG(INFO, "Creating pcap-backed ethdev on numa socket %d",
1136 pp = (struct pmd_process_private *)
1137 rte_zmalloc(NULL, sizeof(struct pmd_process_private),
1138 RTE_CACHE_LINE_SIZE);
1142 "Failed to allocate memory for process private");
1146 /* reserve an ethdev entry */
1147 *eth_dev = rte_eth_vdev_allocate(vdev, sizeof(**internals));
1152 (*eth_dev)->process_private = pp;
1153 /* now put it all together
1154 * - store queue data in internals,
1155 * - store numa_node info in eth_dev
1156 * - point eth_dev_data to internals
1157 * - and point eth_dev structure to new eth_dev_data structure
1159 *internals = (*eth_dev)->data->dev_private;
1161 * Interface MAC = 02:70:63:61:70:<iface_idx>
1162 * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
1163 * where the middle 4 characters are converted to hex.
1165 (*internals)->eth_addr = (struct rte_ether_addr) {
1166 .addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
1168 (*internals)->phy_mac = 0;
1169 data = (*eth_dev)->data;
1170 data->nb_rx_queues = (uint16_t)nb_rx_queues;
1171 data->nb_tx_queues = (uint16_t)nb_tx_queues;
1172 data->dev_link = pmd_link;
1173 data->mac_addrs = &(*internals)->eth_addr;
1174 data->promiscuous = 1;
1175 data->all_multicast = 1;
1176 data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1179 * NOTE: we'll replace the data element, of originally allocated
1180 * eth_dev so the rings are local per-process
1182 (*eth_dev)->dev_ops = &ops;
1184 strlcpy((*internals)->devargs, rte_vdev_device_args(vdev),
1185 ETH_PCAP_ARG_MAXLEN);
1191 eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
1192 const unsigned int numa_node)
1194 #if defined(RTE_EXEC_ENV_LINUX)
1197 int if_fd = socket(AF_INET, SOCK_DGRAM, 0);
1202 rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
1203 if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) {
1208 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1214 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1215 eth_dev->data->mac_addrs = mac_addrs;
1216 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1217 ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1223 #elif defined(RTE_EXEC_ENV_FREEBSD)
1225 struct if_msghdr *ifm;
1226 struct sockaddr_dl *sdl;
1235 mib[4] = NET_RT_IFLIST;
1236 mib[5] = if_nametoindex(if_name);
1238 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
1244 buf = rte_malloc(NULL, len, 0);
1248 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1252 ifm = (struct if_msghdr *)buf;
1253 sdl = (struct sockaddr_dl *)(ifm + 1);
1255 mac_addrs = rte_zmalloc_socket(NULL, RTE_ETHER_ADDR_LEN, 0, numa_node);
1261 PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
1262 eth_dev->data->mac_addrs = mac_addrs;
1263 rte_memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
1264 LLADDR(sdl), RTE_ETHER_ADDR_LEN);
1275 eth_from_pcaps_common(struct rte_vdev_device *vdev,
1276 struct pmd_devargs_all *devargs_all,
1277 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
1279 struct pmd_process_private *pp;
1280 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1281 struct pmd_devargs *tx_queues = &devargs_all->tx_queues;
1282 const unsigned int nb_rx_queues = rx_queues->num_of_queue;
1283 const unsigned int nb_tx_queues = tx_queues->num_of_queue;
1286 if (pmd_init_internals(vdev, nb_rx_queues, nb_tx_queues, internals,
1290 pp = (*eth_dev)->process_private;
1291 for (i = 0; i < nb_rx_queues; i++) {
1292 struct pcap_rx_queue *rx = &(*internals)->rx_queue[i];
1293 struct devargs_queue *queue = &rx_queues->queue[i];
1295 pp->rx_pcap[i] = queue->pcap;
1296 strlcpy(rx->name, queue->name, sizeof(rx->name));
1297 strlcpy(rx->type, queue->type, sizeof(rx->type));
1300 for (i = 0; i < nb_tx_queues; i++) {
1301 struct pcap_tx_queue *tx = &(*internals)->tx_queue[i];
1302 struct devargs_queue *queue = &tx_queues->queue[i];
1304 pp->tx_dumper[i] = queue->dumper;
1305 pp->tx_pcap[i] = queue->pcap;
1306 strlcpy(tx->name, queue->name, sizeof(tx->name));
1307 strlcpy(tx->type, queue->type, sizeof(tx->type));
1314 eth_from_pcaps(struct rte_vdev_device *vdev,
1315 struct pmd_devargs_all *devargs_all)
1317 struct pmd_internals *internals = NULL;
1318 struct rte_eth_dev *eth_dev = NULL;
1319 struct pmd_devargs *rx_queues = &devargs_all->rx_queues;
1320 int single_iface = devargs_all->single_iface;
1321 unsigned int infinite_rx = devargs_all->infinite_rx;
1324 ret = eth_from_pcaps_common(vdev, devargs_all, &internals, ð_dev);
1329 /* store weather we are using a single interface for rx/tx or not */
1330 internals->single_iface = single_iface;
1333 internals->if_index = if_nametoindex(rx_queues->queue[0].name);
1335 /* phy_mac arg is applied only only if "iface" devarg is provided */
1336 if (rx_queues->phy_mac) {
1337 if (eth_pcap_update_mac(rx_queues->queue[0].name,
1338 eth_dev, vdev->device.numa_node) == 0)
1339 internals->phy_mac = 1;
1343 internals->infinite_rx = infinite_rx;
1344 /* Assign rx ops. */
1346 eth_dev->rx_pkt_burst = eth_pcap_rx_infinite;
1347 else if (devargs_all->is_rx_pcap || devargs_all->is_rx_iface ||
1349 eth_dev->rx_pkt_burst = eth_pcap_rx;
1351 eth_dev->rx_pkt_burst = eth_null_rx;
1353 /* Assign tx ops. */
1354 if (devargs_all->is_tx_pcap)
1355 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1356 else if (devargs_all->is_tx_iface || single_iface)
1357 eth_dev->tx_pkt_burst = eth_pcap_tx;
1359 eth_dev->tx_pkt_burst = eth_tx_drop;
1361 rte_eth_dev_probing_finish(eth_dev);
1366 pmd_pcap_probe(struct rte_vdev_device *dev)
1369 struct rte_kvargs *kvlist;
1370 struct pmd_devargs pcaps = {0};
1371 struct pmd_devargs dumpers = {0};
1372 struct rte_eth_dev *eth_dev = NULL;
1373 struct pmd_internals *internal;
1376 struct pmd_devargs_all devargs_all = {
1383 name = rte_vdev_device_name(dev);
1384 PMD_LOG(INFO, "Initializing pmd_pcap for %s", name);
1386 gettimeofday(&start_time, NULL);
1387 start_cycles = rte_get_timer_cycles();
1388 hz = rte_get_timer_hz();
1390 ret = rte_mbuf_dyn_rx_timestamp_register(×tamp_dynfield_offset,
1391 ×tamp_rx_dynflag);
1393 PMD_LOG(ERR, "Failed to register Rx timestamp field/flag");
1397 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1398 eth_dev = rte_eth_dev_attach_secondary(name);
1400 PMD_LOG(ERR, "Failed to probe %s", name);
1404 internal = eth_dev->data->dev_private;
1406 kvlist = rte_kvargs_parse(internal->devargs, valid_arguments);
1410 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
1417 * If iface argument is passed we open the NICs and use them for
1420 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1422 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1423 &open_rx_tx_iface, &pcaps);
1427 dumpers.queue[0] = pcaps.queue[0];
1429 ret = rte_kvargs_process(kvlist, ETH_PCAP_PHY_MAC_ARG,
1430 &select_phy_mac, &pcaps.phy_mac);
1434 dumpers.phy_mac = pcaps.phy_mac;
1436 devargs_all.single_iface = 1;
1437 pcaps.num_of_queue = 1;
1438 dumpers.num_of_queue = 1;
1444 * We check whether we want to open a RX stream from a real NIC, a
1445 * pcap file or open a dummy RX stream
1447 devargs_all.is_rx_pcap =
1448 rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG) ? 1 : 0;
1449 devargs_all.is_rx_iface =
1450 (rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_ARG) +
1451 rte_kvargs_count(kvlist, ETH_PCAP_RX_IFACE_IN_ARG)) ? 1 : 0;
1452 pcaps.num_of_queue = 0;
1454 devargs_all.is_tx_pcap =
1455 rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) ? 1 : 0;
1456 devargs_all.is_tx_iface =
1457 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG) ? 1 : 0;
1458 dumpers.num_of_queue = 0;
1460 if (devargs_all.is_rx_pcap) {
1462 * We check whether we want to infinitely rx the pcap file.
1464 unsigned int infinite_rx_arg_cnt = rte_kvargs_count(kvlist,
1465 ETH_PCAP_INFINITE_RX_ARG);
1467 if (infinite_rx_arg_cnt == 1) {
1468 ret = rte_kvargs_process(kvlist,
1469 ETH_PCAP_INFINITE_RX_ARG,
1470 &get_infinite_rx_arg,
1471 &devargs_all.infinite_rx);
1474 PMD_LOG(INFO, "infinite_rx has been %s for %s",
1475 devargs_all.infinite_rx ? "enabled" : "disabled",
1478 } else if (infinite_rx_arg_cnt > 1) {
1479 PMD_LOG(WARNING, "infinite_rx has not been enabled since the "
1480 "argument has been provided more than once "
1484 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1485 &open_rx_pcap, &pcaps);
1486 } else if (devargs_all.is_rx_iface) {
1487 ret = rte_kvargs_process(kvlist, NULL,
1488 &rx_iface_args_process, &pcaps);
1489 } else if (devargs_all.is_tx_iface || devargs_all.is_tx_pcap) {
1492 /* Count number of tx queue args passed before dummy rx queue
1493 * creation so a dummy rx queue can be created for each tx queue
1495 unsigned int num_tx_queues =
1496 (rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG) +
1497 rte_kvargs_count(kvlist, ETH_PCAP_TX_IFACE_ARG));
1499 PMD_LOG(INFO, "Creating null rx queue since no rx queues were provided.");
1501 /* Creating a dummy rx queue for each tx queue passed */
1502 for (i = 0; i < num_tx_queues; i++)
1503 ret = add_queue(&pcaps, "dummy_rx", "rx_null", NULL,
1506 PMD_LOG(ERR, "Error - No rx or tx queues provided");
1513 * We check whether we want to open a TX stream to a real NIC,
1514 * a pcap file, or drop packets on tx
1516 if (devargs_all.is_tx_pcap) {
1517 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1518 &open_tx_pcap, &dumpers);
1519 } else if (devargs_all.is_tx_iface) {
1520 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1521 &open_tx_iface, &dumpers);
1525 PMD_LOG(INFO, "Dropping packets on tx since no tx queues were provided.");
1527 /* Add 1 dummy queue per rxq which counts and drops packets. */
1528 for (i = 0; i < pcaps.num_of_queue; i++)
1529 ret = add_queue(&dumpers, "dummy_tx", "tx_drop", NULL,
1537 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1538 struct pmd_process_private *pp;
1541 internal = eth_dev->data->dev_private;
1542 pp = (struct pmd_process_private *)
1544 sizeof(struct pmd_process_private),
1545 RTE_CACHE_LINE_SIZE);
1549 "Failed to allocate memory for process private");
1554 eth_dev->dev_ops = &ops;
1555 eth_dev->device = &dev->device;
1557 /* setup process private */
1558 for (i = 0; i < pcaps.num_of_queue; i++)
1559 pp->rx_pcap[i] = pcaps.queue[i].pcap;
1561 for (i = 0; i < dumpers.num_of_queue; i++) {
1562 pp->tx_dumper[i] = dumpers.queue[i].dumper;
1563 pp->tx_pcap[i] = dumpers.queue[i].pcap;
1566 eth_dev->process_private = pp;
1567 eth_dev->rx_pkt_burst = eth_pcap_rx;
1568 if (devargs_all.is_tx_pcap)
1569 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
1571 eth_dev->tx_pkt_burst = eth_pcap_tx;
1573 rte_eth_dev_probing_finish(eth_dev);
1577 devargs_all.rx_queues = pcaps;
1578 devargs_all.tx_queues = dumpers;
1580 ret = eth_from_pcaps(dev, &devargs_all);
1583 rte_kvargs_free(kvlist);
1589 pmd_pcap_remove(struct rte_vdev_device *dev)
1591 struct rte_eth_dev *eth_dev = NULL;
1596 eth_dev = rte_eth_dev_allocated(rte_vdev_device_name(dev));
1597 if (eth_dev == NULL)
1598 return 0; /* port already released */
1600 eth_dev_close(eth_dev);
1601 rte_eth_dev_release_port(eth_dev);
1606 static struct rte_vdev_driver pmd_pcap_drv = {
1607 .probe = pmd_pcap_probe,
1608 .remove = pmd_pcap_remove,
1611 RTE_PMD_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1612 RTE_PMD_REGISTER_ALIAS(net_pcap, eth_pcap);
1613 RTE_PMD_REGISTER_PARAM_STRING(net_pcap,
1614 ETH_PCAP_RX_PCAP_ARG "=<string> "
1615 ETH_PCAP_TX_PCAP_ARG "=<string> "
1616 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1617 ETH_PCAP_RX_IFACE_IN_ARG "=<ifc> "
1618 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1619 ETH_PCAP_IFACE_ARG "=<ifc> "
1620 ETH_PCAP_PHY_MAC_ARG "=<int>"
1621 ETH_PCAP_INFINITE_RX_ARG "=<0|1>");