4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <rte_cycles.h>
42 #include <rte_ethdev.h>
43 #include <rte_kvargs.h>
44 #include <rte_malloc.h>
48 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
49 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
50 #define RTE_ETH_PCAP_PROMISC 1
51 #define RTE_ETH_PCAP_TIMEOUT -1
52 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
53 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
54 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
55 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
56 #define ETH_PCAP_IFACE_ARG "iface"
58 #define ETH_PCAP_ARG_MAXLEN 64
60 #define RTE_PMD_PCAP_MAX_QUEUES 16
62 static char errbuf[PCAP_ERRBUF_SIZE];
63 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
64 static struct timeval start_time;
65 static uint64_t start_cycles;
69 volatile unsigned long pkts;
70 volatile unsigned long bytes;
71 volatile unsigned long err_pkts;
74 struct pcap_rx_queue {
77 struct rte_mempool *mb_pool;
78 struct queue_stat rx_stat;
80 char type[ETH_PCAP_ARG_MAXLEN];
83 struct pcap_tx_queue {
84 pcap_dumper_t *dumper;
86 struct queue_stat tx_stat;
88 char type[ETH_PCAP_ARG_MAXLEN];
91 struct pmd_internals {
92 struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
93 struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
99 unsigned num_of_queue;
100 struct devargs_queue {
101 pcap_dumper_t *dumper;
105 } queue[RTE_PMD_PCAP_MAX_QUEUES];
108 static const char *valid_arguments[] = {
109 ETH_PCAP_RX_PCAP_ARG,
110 ETH_PCAP_TX_PCAP_ARG,
111 ETH_PCAP_RX_IFACE_ARG,
112 ETH_PCAP_TX_IFACE_ARG,
117 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
118 static const char *drivername = "Pcap PMD";
119 static struct rte_eth_link pmd_link = {
120 .link_speed = ETH_SPEED_NUM_10G,
121 .link_duplex = ETH_LINK_FULL_DUPLEX,
122 .link_status = ETH_LINK_DOWN,
123 .link_autoneg = ETH_LINK_SPEED_FIXED,
127 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool,
128 struct rte_mbuf *mbuf,
132 struct rte_mbuf *m = mbuf;
134 /* Copy the first segment. */
135 uint16_t len = rte_pktmbuf_tailroom(mbuf);
137 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
141 while (data_len > 0) {
142 /* Allocate next mbuf and point to that. */
143 m->next = rte_pktmbuf_alloc(mb_pool);
145 if (unlikely(!m->next))
150 /* Headroom is not needed in chained mbufs. */
151 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
155 /* Copy next segment. */
156 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
157 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
164 return mbuf->nb_segs;
167 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
169 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
171 uint16_t data_len = 0;
174 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
177 data_len += mbuf->data_len;
183 eth_pcap_rx(void *queue,
184 struct rte_mbuf **bufs,
188 struct pcap_pkthdr header;
189 const u_char *packet;
190 struct rte_mbuf *mbuf;
191 struct pcap_rx_queue *pcap_q = queue;
194 uint32_t rx_bytes = 0;
196 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
199 /* Reads the given number of packets from the pcap file one by one
200 * and copies the packet data into a newly allocated mbuf to return.
202 for (i = 0; i < nb_pkts; i++) {
203 /* Get the next PCAP packet */
204 packet = pcap_next(pcap_q->pcap, &header);
205 if (unlikely(packet == NULL))
208 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
209 if (unlikely(mbuf == NULL))
212 /* Now get the space available for data in the mbuf */
213 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
214 RTE_PKTMBUF_HEADROOM);
216 if (header.caplen <= buf_size) {
217 /* pcap packet will fit in the mbuf, go ahead and copy */
218 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
220 mbuf->data_len = (uint16_t)header.caplen;
222 /* Try read jumbo frame into multi mbufs. */
223 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
226 header.caplen) == -1))
230 mbuf->pkt_len = (uint16_t)header.caplen;
231 mbuf->port = pcap_q->in_port;
234 rx_bytes += header.caplen;
236 pcap_q->rx_stat.pkts += num_rx;
237 pcap_q->rx_stat.bytes += rx_bytes;
242 calculate_timestamp(struct timeval *ts) {
244 struct timeval cur_time;
246 cycles = rte_get_timer_cycles() - start_cycles;
247 cur_time.tv_sec = cycles / hz;
248 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
249 timeradd(&start_time, &cur_time, ts);
253 * Callback to handle writing packets to a pcap file.
256 eth_pcap_tx_dumper(void *queue,
257 struct rte_mbuf **bufs,
261 struct rte_mbuf *mbuf;
262 struct pcap_tx_queue *dumper_q = queue;
264 uint32_t tx_bytes = 0;
265 struct pcap_pkthdr header;
267 if (dumper_q->dumper == NULL || nb_pkts == 0)
270 /* writes the nb_pkts packets to the previously opened pcap file dumper */
271 for (i = 0; i < nb_pkts; i++) {
273 calculate_timestamp(&header.ts);
274 header.len = mbuf->pkt_len;
275 header.caplen = header.len;
277 if (likely(mbuf->nb_segs == 1)) {
278 pcap_dump((u_char *)dumper_q->dumper, &header,
279 rte_pktmbuf_mtod(mbuf, void*));
281 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
282 eth_pcap_gather_data(tx_pcap_data, mbuf);
283 pcap_dump((u_char *)dumper_q->dumper, &header,
287 "Dropping PCAP packet. "
288 "Size (%d) > max jumbo size (%d).\n",
290 ETHER_MAX_JUMBO_FRAME_LEN);
292 rte_pktmbuf_free(mbuf);
297 rte_pktmbuf_free(mbuf);
299 tx_bytes += mbuf->pkt_len;
303 * Since there's no place to hook a callback when the forwarding
304 * process stops and to make sure the pcap file is actually written,
305 * we flush the pcap dumper within each burst.
307 pcap_dump_flush(dumper_q->dumper);
308 dumper_q->tx_stat.pkts += num_tx;
309 dumper_q->tx_stat.bytes += tx_bytes;
310 dumper_q->tx_stat.err_pkts += nb_pkts - num_tx;
315 * Callback to handle sending packets through a real NIC.
318 eth_pcap_tx(void *queue,
319 struct rte_mbuf **bufs,
324 struct rte_mbuf *mbuf;
325 struct pcap_tx_queue *tx_queue = queue;
327 uint32_t tx_bytes = 0;
329 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
332 for (i = 0; i < nb_pkts; i++) {
335 if (likely(mbuf->nb_segs == 1)) {
336 ret = pcap_sendpacket(tx_queue->pcap,
337 rte_pktmbuf_mtod(mbuf, u_char *),
340 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
341 eth_pcap_gather_data(tx_pcap_data, mbuf);
342 ret = pcap_sendpacket(tx_queue->pcap,
347 "Dropping PCAP packet. "
348 "Size (%d) > max jumbo size (%d).\n",
350 ETHER_MAX_JUMBO_FRAME_LEN);
352 rte_pktmbuf_free(mbuf);
357 if (unlikely(ret != 0))
360 tx_bytes += mbuf->pkt_len;
361 rte_pktmbuf_free(mbuf);
364 tx_queue->tx_stat.pkts += num_tx;
365 tx_queue->tx_stat.bytes += tx_bytes;
366 tx_queue->tx_stat.err_pkts += nb_pkts - num_tx;
371 * pcap_open_live wrapper function
374 open_iface_live(const char *iface, pcap_t **pcap) {
375 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
376 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
379 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
387 open_single_iface(const char *iface, pcap_t **pcap)
389 if (open_iface_live(iface, pcap) < 0) {
390 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
398 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
403 * We need to create a dummy empty pcap_t to use it
404 * with pcap_dump_open(). We create big enough an Ethernet
407 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
409 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
413 /* The dumper is created using the previous pcap_t reference */
414 if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
415 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n",
424 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
426 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
427 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
435 eth_dev_start(struct rte_eth_dev *dev)
438 struct pmd_internals *internals = dev->data->dev_private;
439 struct pcap_tx_queue *tx;
440 struct pcap_rx_queue *rx;
442 /* Special iface case. Single pcap is open and shared between tx/rx. */
443 if (internals->single_iface) {
444 tx = &internals->tx_queue[0];
445 rx = &internals->rx_queue[0];
447 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
448 if (open_single_iface(tx->name, &tx->pcap) < 0)
455 /* If not open already, open tx pcaps/dumpers */
456 for (i = 0; i < dev->data->nb_tx_queues; i++) {
457 tx = &internals->tx_queue[i];
459 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
460 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
464 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
465 if (open_single_iface(tx->name, &tx->pcap) < 0)
470 /* If not open already, open rx pcaps */
471 for (i = 0; i < dev->data->nb_rx_queues; i++) {
472 rx = &internals->rx_queue[i];
474 if (rx->pcap != NULL)
477 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
478 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
482 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
483 if (open_single_iface(rx->name, &rx->pcap) < 0)
490 dev->data->dev_link.link_status = ETH_LINK_UP;
495 * This function gets called when the current port gets stopped.
496 * Is the only place for us to close all the tx streams dumpers.
497 * If not called the dumpers will be flushed within each tx burst.
500 eth_dev_stop(struct rte_eth_dev *dev)
503 struct pmd_internals *internals = dev->data->dev_private;
504 struct pcap_tx_queue *tx;
505 struct pcap_rx_queue *rx;
507 /* Special iface case. Single pcap is open and shared between tx/rx. */
508 if (internals->single_iface) {
509 tx = &internals->tx_queue[0];
510 rx = &internals->rx_queue[0];
511 pcap_close(tx->pcap);
517 for (i = 0; i < dev->data->nb_tx_queues; i++) {
518 tx = &internals->tx_queue[i];
520 if (tx->dumper != NULL) {
521 pcap_dump_close(tx->dumper);
525 if (tx->pcap != NULL) {
526 pcap_close(tx->pcap);
531 for (i = 0; i < dev->data->nb_rx_queues; i++) {
532 rx = &internals->rx_queue[i];
534 if (rx->pcap != NULL) {
535 pcap_close(rx->pcap);
541 dev->data->dev_link.link_status = ETH_LINK_DOWN;
545 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
551 eth_dev_info(struct rte_eth_dev *dev,
552 struct rte_eth_dev_info *dev_info)
554 struct pmd_internals *internals = dev->data->dev_private;
555 dev_info->driver_name = drivername;
556 dev_info->if_index = internals->if_index;
557 dev_info->max_mac_addrs = 1;
558 dev_info->max_rx_pktlen = (uint32_t) -1;
559 dev_info->max_rx_queues = dev->data->nb_rx_queues;
560 dev_info->max_tx_queues = dev->data->nb_tx_queues;
561 dev_info->min_rx_bufsize = 0;
562 dev_info->pci_dev = NULL;
566 eth_stats_get(struct rte_eth_dev *dev,
567 struct rte_eth_stats *stats)
570 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
571 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
572 unsigned long tx_packets_err_total = 0;
573 const struct pmd_internals *internal = dev->data->dev_private;
575 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
576 i < dev->data->nb_rx_queues; i++) {
577 stats->q_ipackets[i] = internal->rx_queue[i].rx_stat.pkts;
578 stats->q_ibytes[i] = internal->rx_queue[i].rx_stat.bytes;
579 rx_packets_total += stats->q_ipackets[i];
580 rx_bytes_total += stats->q_ibytes[i];
583 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS &&
584 i < dev->data->nb_tx_queues; i++) {
585 stats->q_opackets[i] = internal->tx_queue[i].tx_stat.pkts;
586 stats->q_obytes[i] = internal->tx_queue[i].tx_stat.bytes;
587 stats->q_errors[i] = internal->tx_queue[i].tx_stat.err_pkts;
588 tx_packets_total += stats->q_opackets[i];
589 tx_bytes_total += stats->q_obytes[i];
590 tx_packets_err_total += stats->q_errors[i];
593 stats->ipackets = rx_packets_total;
594 stats->ibytes = rx_bytes_total;
595 stats->opackets = tx_packets_total;
596 stats->obytes = tx_bytes_total;
597 stats->oerrors = tx_packets_err_total;
601 eth_stats_reset(struct rte_eth_dev *dev)
604 struct pmd_internals *internal = dev->data->dev_private;
605 for (i = 0; i < dev->data->nb_rx_queues; i++) {
606 internal->rx_queue[i].rx_stat.pkts = 0;
607 internal->rx_queue[i].rx_stat.bytes = 0;
609 for (i = 0; i < dev->data->nb_tx_queues; i++) {
610 internal->tx_queue[i].tx_stat.pkts = 0;
611 internal->tx_queue[i].tx_stat.bytes = 0;
612 internal->tx_queue[i].tx_stat.err_pkts = 0;
617 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
622 eth_queue_release(void *q __rte_unused)
627 eth_link_update(struct rte_eth_dev *dev __rte_unused,
628 int wait_to_complete __rte_unused)
634 eth_rx_queue_setup(struct rte_eth_dev *dev,
635 uint16_t rx_queue_id,
636 uint16_t nb_rx_desc __rte_unused,
637 unsigned int socket_id __rte_unused,
638 const struct rte_eth_rxconf *rx_conf __rte_unused,
639 struct rte_mempool *mb_pool)
641 struct pmd_internals *internals = dev->data->dev_private;
642 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
643 pcap_q->mb_pool = mb_pool;
644 dev->data->rx_queues[rx_queue_id] = pcap_q;
645 pcap_q->in_port = dev->data->port_id;
650 eth_tx_queue_setup(struct rte_eth_dev *dev,
651 uint16_t tx_queue_id,
652 uint16_t nb_tx_desc __rte_unused,
653 unsigned int socket_id __rte_unused,
654 const struct rte_eth_txconf *tx_conf __rte_unused)
657 struct pmd_internals *internals = dev->data->dev_private;
658 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
662 static const struct eth_dev_ops ops = {
663 .dev_start = eth_dev_start,
664 .dev_stop = eth_dev_stop,
665 .dev_close = eth_dev_close,
666 .dev_configure = eth_dev_configure,
667 .dev_infos_get = eth_dev_info,
668 .rx_queue_setup = eth_rx_queue_setup,
669 .tx_queue_setup = eth_tx_queue_setup,
670 .rx_queue_release = eth_queue_release,
671 .tx_queue_release = eth_queue_release,
672 .link_update = eth_link_update,
673 .stats_get = eth_stats_get,
674 .stats_reset = eth_stats_reset,
678 * Function handler that opens the pcap file for reading a stores a
679 * reference of it for use it later on.
682 open_rx_pcap(const char *key, const char *value, void *extra_args)
685 const char *pcap_filename = value;
686 struct pmd_devargs *rx = extra_args;
689 for (i = 0; i < rx->num_of_queue; i++) {
690 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
693 rx->queue[i].pcap = pcap;
694 rx->queue[i].name = pcap_filename;
695 rx->queue[i].type = key;
702 * Opens a pcap file for writing and stores a reference to it
703 * for use it later on.
706 open_tx_pcap(const char *key, const char *value, void *extra_args)
709 const char *pcap_filename = value;
710 struct pmd_devargs *dumpers = extra_args;
711 pcap_dumper_t *dumper;
713 for (i = 0; i < dumpers->num_of_queue; i++) {
714 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
717 dumpers->queue[i].dumper = dumper;
718 dumpers->queue[i].name = pcap_filename;
719 dumpers->queue[i].type = key;
726 * Opens an interface for reading and writing
729 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
731 const char *iface = value;
732 struct pmd_devargs *tx = extra_args;
735 if (open_single_iface(iface, &pcap) < 0)
738 tx->queue[0].pcap = pcap;
739 tx->queue[0].name = iface;
740 tx->queue[0].type = key;
746 * Opens a NIC for reading packets from it
749 open_rx_iface(const char *key, const char *value, void *extra_args)
752 const char *iface = value;
753 struct pmd_devargs *rx = extra_args;
756 for (i = 0; i < rx->num_of_queue; i++) {
757 if (open_single_iface(iface, &pcap) < 0)
759 rx->queue[i].pcap = pcap;
760 rx->queue[i].name = iface;
761 rx->queue[i].type = key;
768 * Opens a NIC for writing packets to it
771 open_tx_iface(const char *key, const char *value, void *extra_args)
774 const char *iface = value;
775 struct pmd_devargs *tx = extra_args;
778 for (i = 0; i < tx->num_of_queue; i++) {
779 if (open_single_iface(iface, &pcap) < 0)
781 tx->queue[i].pcap = pcap;
782 tx->queue[i].name = iface;
783 tx->queue[i].type = key;
790 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
791 const unsigned nb_tx_queues, struct pmd_internals **internals,
792 struct rte_eth_dev **eth_dev)
794 struct rte_eth_dev_data *data = NULL;
795 unsigned int numa_node = rte_socket_id();
797 RTE_LOG(INFO, PMD, "Creating pcap-backed ethdev on numa socket %u\n",
800 /* now do all data allocation - for eth_dev structure
801 * and internal (private) data
803 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
807 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0,
809 if (*internals == NULL)
812 /* reserve an ethdev entry */
813 *eth_dev = rte_eth_dev_allocate(name);
814 if (*eth_dev == NULL)
817 /* check length of device name */
818 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
821 /* now put it all together
822 * - store queue data in internals,
823 * - store numa_node info in eth_dev
824 * - point eth_dev_data to internals
825 * - and point eth_dev structure to new eth_dev_data structure
827 data->dev_private = *internals;
828 data->port_id = (*eth_dev)->data->port_id;
829 snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
830 data->nb_rx_queues = (uint16_t)nb_rx_queues;
831 data->nb_tx_queues = (uint16_t)nb_tx_queues;
832 data->dev_link = pmd_link;
833 data->mac_addrs = ð_addr;
835 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
838 * NOTE: we'll replace the data element, of originally allocated
839 * eth_dev so the rings are local per-process
841 (*eth_dev)->data = data;
842 (*eth_dev)->dev_ops = &ops;
843 (*eth_dev)->driver = NULL;
844 data->dev_flags = RTE_ETH_DEV_DETACHABLE;
845 data->kdrv = RTE_KDRV_NONE;
846 data->drv_name = drivername;
847 data->numa_node = numa_node;
853 rte_free(*internals);
859 rte_eth_from_pcaps_common(const char *name, struct pmd_devargs *rx_queues,
860 const unsigned nb_rx_queues, struct pmd_devargs *tx_queues,
861 const unsigned nb_tx_queues, struct rte_kvargs *kvlist,
862 struct pmd_internals **internals, struct rte_eth_dev **eth_dev)
864 struct rte_kvargs_pair *pair = NULL;
868 /* do some parameter checking */
869 if (rx_queues == NULL && nb_rx_queues > 0)
871 if (tx_queues == NULL && nb_tx_queues > 0)
874 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, internals,
878 for (i = 0; i < nb_rx_queues; i++) {
879 (*internals)->rx_queue[i].pcap = rx_queues->queue[i].pcap;
880 snprintf((*internals)->rx_queue[i].name,
881 sizeof((*internals)->rx_queue[i].name), "%s",
882 rx_queues->queue[i].name);
883 snprintf((*internals)->rx_queue[i].type,
884 sizeof((*internals)->rx_queue[i].type), "%s",
885 rx_queues->queue[i].type);
887 for (i = 0; i < nb_tx_queues; i++) {
888 (*internals)->tx_queue[i].dumper = tx_queues->queue[i].dumper;
889 snprintf((*internals)->tx_queue[i].name,
890 sizeof((*internals)->tx_queue[i].name), "%s",
891 tx_queues->queue[i].name);
892 snprintf((*internals)->tx_queue[i].type,
893 sizeof((*internals)->tx_queue[i].type), "%s",
894 tx_queues->queue[i].type);
897 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
898 pair = &kvlist->pairs[k_idx];
899 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
904 (*internals)->if_index = 0;
906 (*internals)->if_index = if_nametoindex(pair->value);
912 rte_eth_from_pcaps(const char *name, struct pmd_devargs *rx_queues,
913 const unsigned nb_rx_queues, struct pmd_devargs *tx_queues,
914 const unsigned nb_tx_queues, struct rte_kvargs *kvlist,
915 int single_iface, unsigned int using_dumpers)
917 struct pmd_internals *internals = NULL;
918 struct rte_eth_dev *eth_dev = NULL;
921 ret = rte_eth_from_pcaps_common(name, rx_queues, nb_rx_queues,
922 tx_queues, nb_tx_queues, kvlist, &internals, ð_dev);
927 /* store wether we are using a single interface for rx/tx or not */
928 internals->single_iface = single_iface;
930 eth_dev->rx_pkt_burst = eth_pcap_rx;
933 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
935 eth_dev->tx_pkt_burst = eth_pcap_tx;
942 rte_pmd_pcap_devinit(const char *name, const char *params)
944 unsigned int is_rx_pcap = 0, is_tx_pcap = 0;
945 struct rte_kvargs *kvlist;
946 struct pmd_devargs pcaps = {0};
947 struct pmd_devargs dumpers = {0};
948 int single_iface = 0;
951 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
953 gettimeofday(&start_time, NULL);
954 start_cycles = rte_get_timer_cycles();
955 hz = rte_get_timer_hz();
957 kvlist = rte_kvargs_parse(params, valid_arguments);
962 * If iface argument is passed we open the NICs and use them for
965 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
967 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
968 &open_rx_tx_iface, &pcaps);
973 dumpers.queue[0] = pcaps.queue[0];
976 pcaps.num_of_queue = 1;
977 dumpers.num_of_queue = 1;
983 * We check whether we want to open a RX stream from a real NIC or a
986 pcaps.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG);
987 if (pcaps.num_of_queue)
990 pcaps.num_of_queue = rte_kvargs_count(kvlist,
991 ETH_PCAP_RX_IFACE_ARG);
993 if (pcaps.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
994 pcaps.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
997 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
998 &open_rx_pcap, &pcaps);
1000 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1001 &open_rx_iface, &pcaps);
1007 * We check whether we want to open a TX stream to a real NIC or a
1010 dumpers.num_of_queue = rte_kvargs_count(kvlist, ETH_PCAP_TX_PCAP_ARG);
1011 if (dumpers.num_of_queue)
1014 dumpers.num_of_queue = rte_kvargs_count(kvlist,
1015 ETH_PCAP_TX_IFACE_ARG);
1017 if (dumpers.num_of_queue > RTE_PMD_PCAP_MAX_QUEUES)
1018 dumpers.num_of_queue = RTE_PMD_PCAP_MAX_QUEUES;
1021 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1022 &open_tx_pcap, &dumpers);
1024 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1025 &open_tx_iface, &dumpers);
1031 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_queue, &dumpers,
1032 dumpers.num_of_queue, kvlist, single_iface, is_tx_pcap);
1035 rte_kvargs_free(kvlist);
1040 rte_pmd_pcap_devuninit(const char *name)
1042 struct rte_eth_dev *eth_dev = NULL;
1044 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1050 /* reserve an ethdev entry */
1051 eth_dev = rte_eth_dev_allocated(name);
1052 if (eth_dev == NULL)
1055 rte_free(eth_dev->data->dev_private);
1056 rte_free(eth_dev->data);
1058 rte_eth_dev_release_port(eth_dev);
1063 static struct rte_vdev_driver pmd_pcap_drv = {
1064 .init = rte_pmd_pcap_devinit,
1065 .uninit = rte_pmd_pcap_devuninit,
1068 DRIVER_REGISTER_VDEV(net_pcap, pmd_pcap_drv);
1069 DRIVER_REGISTER_PARAM_STRING(net_pcap,
1070 ETH_PCAP_RX_PCAP_ARG "=<string> "
1071 ETH_PCAP_TX_PCAP_ARG "=<string> "
1072 ETH_PCAP_RX_IFACE_ARG "=<ifc> "
1073 ETH_PCAP_TX_IFACE_ARG "=<ifc> "
1074 ETH_PCAP_IFACE_ARG "=<ifc>");