4 * Copyright(c) 2010-2014 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.
37 #include <rte_ethdev.h>
38 #include <rte_malloc.h>
39 #include <rte_memcpy.h>
40 #include <rte_string_fns.h>
41 #include <rte_cycles.h>
42 #include <rte_kvargs.h>
49 #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535
50 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
51 #define RTE_ETH_PCAP_PROMISC 1
52 #define RTE_ETH_PCAP_TIMEOUT -1
53 #define ETH_PCAP_RX_PCAP_ARG "rx_pcap"
54 #define ETH_PCAP_TX_PCAP_ARG "tx_pcap"
55 #define ETH_PCAP_RX_IFACE_ARG "rx_iface"
56 #define ETH_PCAP_TX_IFACE_ARG "tx_iface"
57 #define ETH_PCAP_IFACE_ARG "iface"
59 #define ETH_PCAP_ARG_MAXLEN 64
61 static char errbuf[PCAP_ERRBUF_SIZE];
62 static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
63 static struct timeval start_time;
64 static uint64_t start_cycles;
67 struct pcap_rx_queue {
70 struct rte_mempool *mb_pool;
71 volatile unsigned long rx_pkts;
72 volatile unsigned long rx_bytes;
73 volatile unsigned long err_pkts;
75 char type[ETH_PCAP_ARG_MAXLEN];
78 struct pcap_tx_queue {
79 pcap_dumper_t *dumper;
81 volatile unsigned long tx_pkts;
82 volatile unsigned long tx_bytes;
83 volatile unsigned long err_pkts;
85 char type[ETH_PCAP_ARG_MAXLEN];
90 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
91 const char *names[RTE_PMD_RING_MAX_RX_RINGS];
92 const char *types[RTE_PMD_RING_MAX_RX_RINGS];
97 pcap_dumper_t *dumpers[RTE_PMD_RING_MAX_TX_RINGS];
98 pcap_t *pcaps[RTE_PMD_RING_MAX_RX_RINGS];
99 const char *names[RTE_PMD_RING_MAX_RX_RINGS];
100 const char *types[RTE_PMD_RING_MAX_RX_RINGS];
103 struct pmd_internals {
104 struct pcap_rx_queue rx_queue[RTE_PMD_RING_MAX_RX_RINGS];
105 struct pcap_tx_queue tx_queue[RTE_PMD_RING_MAX_TX_RINGS];
106 unsigned nb_rx_queues;
107 unsigned nb_tx_queues;
112 const char *valid_arguments[] = {
113 ETH_PCAP_RX_PCAP_ARG,
114 ETH_PCAP_TX_PCAP_ARG,
115 ETH_PCAP_RX_IFACE_ARG,
116 ETH_PCAP_TX_IFACE_ARG,
121 static int open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper);
122 static int open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap);
123 static int open_single_iface(const char *iface, pcap_t **pcap);
125 static struct ether_addr eth_addr = { .addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 } };
126 static const char *drivername = "Pcap PMD";
127 static struct rte_eth_link pmd_link = {
129 .link_duplex = ETH_LINK_FULL_DUPLEX,
134 eth_pcap_rx_jumbo(struct rte_mempool *mb_pool,
135 struct rte_mbuf *mbuf,
139 struct rte_mbuf *m = mbuf;
141 /* Copy the first segment. */
142 uint16_t len = rte_pktmbuf_tailroom(mbuf);
144 rte_memcpy(rte_pktmbuf_append(mbuf, len), data, len);
148 while (data_len > 0) {
149 /* Allocate next mbuf and point to that. */
150 m->next = rte_pktmbuf_alloc(mb_pool);
152 if (unlikely(!m->next))
157 /* Headroom is not needed in chained mbufs. */
158 rte_pktmbuf_prepend(m, rte_pktmbuf_headroom(m));
162 /* Copy next segment. */
163 len = RTE_MIN(rte_pktmbuf_tailroom(m), data_len);
164 rte_memcpy(rte_pktmbuf_append(m, len), data, len);
171 return mbuf->nb_segs;
174 /* Copy data from mbuf chain to a buffer suitable for writing to a PCAP file. */
176 eth_pcap_gather_data(unsigned char *data, struct rte_mbuf *mbuf)
178 uint16_t data_len = 0;
181 rte_memcpy(data + data_len, rte_pktmbuf_mtod(mbuf, void *),
184 data_len += mbuf->data_len;
190 eth_pcap_rx(void *queue,
191 struct rte_mbuf **bufs,
195 struct pcap_pkthdr header;
196 const u_char *packet;
197 struct rte_mbuf *mbuf;
198 struct pcap_rx_queue *pcap_q = queue;
201 uint32_t rx_bytes = 0;
203 if (unlikely(pcap_q->pcap == NULL || nb_pkts == 0))
206 /* Reads the given number of packets from the pcap file one by one
207 * and copies the packet data into a newly allocated mbuf to return.
209 for (i = 0; i < nb_pkts; i++) {
210 /* Get the next PCAP packet */
211 packet = pcap_next(pcap_q->pcap, &header);
212 if (unlikely(packet == NULL))
215 mbuf = rte_pktmbuf_alloc(pcap_q->mb_pool);
216 if (unlikely(mbuf == NULL))
219 /* Now get the space available for data in the mbuf */
220 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(pcap_q->mb_pool) -
221 RTE_PKTMBUF_HEADROOM);
223 if (header.len <= buf_size) {
224 /* pcap packet will fit in the mbuf, go ahead and copy */
225 rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), packet,
227 mbuf->data_len = (uint16_t)header.len;
229 /* Try read jumbo frame into multi mbufs. */
230 if (unlikely(eth_pcap_rx_jumbo(pcap_q->mb_pool,
237 mbuf->pkt_len = (uint16_t)header.len;
238 mbuf->port = pcap_q->in_port;
241 rx_bytes += header.len;
243 pcap_q->rx_pkts += num_rx;
244 pcap_q->rx_bytes += rx_bytes;
249 calculate_timestamp(struct timeval *ts) {
251 struct timeval cur_time;
253 cycles = rte_get_timer_cycles() - start_cycles;
254 cur_time.tv_sec = cycles / hz;
255 cur_time.tv_usec = (cycles % hz) * 10e6 / hz;
256 timeradd(&start_time, &cur_time, ts);
260 * Callback to handle writing packets to a pcap file.
263 eth_pcap_tx_dumper(void *queue,
264 struct rte_mbuf **bufs,
268 struct rte_mbuf *mbuf;
269 struct pcap_tx_queue *dumper_q = queue;
271 uint32_t tx_bytes = 0;
272 struct pcap_pkthdr header;
274 if (dumper_q->dumper == NULL || nb_pkts == 0)
277 /* writes the nb_pkts packets to the previously opened pcap file dumper */
278 for (i = 0; i < nb_pkts; i++) {
280 calculate_timestamp(&header.ts);
281 header.len = mbuf->pkt_len;
282 header.caplen = header.len;
284 if (likely(mbuf->nb_segs == 1)) {
285 pcap_dump((u_char *)dumper_q->dumper, &header,
286 rte_pktmbuf_mtod(mbuf, void*));
288 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
289 eth_pcap_gather_data(tx_pcap_data, mbuf);
290 pcap_dump((u_char *)dumper_q->dumper, &header,
294 "Dropping PCAP packet. "
295 "Size (%d) > max jumbo size (%d).\n",
297 ETHER_MAX_JUMBO_FRAME_LEN);
299 rte_pktmbuf_free(mbuf);
304 rte_pktmbuf_free(mbuf);
306 tx_bytes += mbuf->pkt_len;
310 * Since there's no place to hook a callback when the forwarding
311 * process stops and to make sure the pcap file is actually written,
312 * we flush the pcap dumper within each burst.
314 pcap_dump_flush(dumper_q->dumper);
315 dumper_q->tx_pkts += num_tx;
316 dumper_q->tx_bytes += tx_bytes;
317 dumper_q->err_pkts += nb_pkts - num_tx;
322 * Callback to handle sending packets through a real NIC.
325 eth_pcap_tx(void *queue,
326 struct rte_mbuf **bufs,
331 struct rte_mbuf *mbuf;
332 struct pcap_tx_queue *tx_queue = queue;
334 uint32_t tx_bytes = 0;
336 if (unlikely(nb_pkts == 0 || tx_queue->pcap == NULL))
339 for (i = 0; i < nb_pkts; i++) {
342 if (likely(mbuf->nb_segs == 1)) {
343 ret = pcap_sendpacket(tx_queue->pcap,
344 rte_pktmbuf_mtod(mbuf, u_char *),
347 if (mbuf->pkt_len <= ETHER_MAX_JUMBO_FRAME_LEN) {
348 eth_pcap_gather_data(tx_pcap_data, mbuf);
349 ret = pcap_sendpacket(tx_queue->pcap,
354 "Dropping PCAP packet. "
355 "Size (%d) > max jumbo size (%d).\n",
357 ETHER_MAX_JUMBO_FRAME_LEN);
359 rte_pktmbuf_free(mbuf);
364 if (unlikely(ret != 0))
367 tx_bytes += mbuf->pkt_len;
368 rte_pktmbuf_free(mbuf);
371 tx_queue->tx_pkts += num_tx;
372 tx_queue->tx_bytes += tx_bytes;
373 tx_queue->err_pkts += nb_pkts - num_tx;
378 eth_dev_start(struct rte_eth_dev *dev)
381 struct pmd_internals *internals = dev->data->dev_private;
382 struct pcap_tx_queue *tx;
383 struct pcap_rx_queue *rx;
385 /* Special iface case. Single pcap is open and shared between tx/rx. */
386 if (internals->single_iface) {
387 tx = &internals->tx_queue[0];
388 rx = &internals->rx_queue[0];
390 if (!tx->pcap && strcmp(tx->type, ETH_PCAP_IFACE_ARG) == 0) {
391 if (open_single_iface(tx->name, &tx->pcap) < 0)
398 /* If not open already, open tx pcaps/dumpers */
399 for (i = 0; i < internals->nb_tx_queues; i++) {
400 tx = &internals->tx_queue[i];
402 if (!tx->dumper && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) {
403 if (open_single_tx_pcap(tx->name, &tx->dumper) < 0)
407 else if (!tx->pcap && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) {
408 if (open_single_iface(tx->name, &tx->pcap) < 0)
413 /* If not open already, open rx pcaps */
414 for (i = 0; i < internals->nb_rx_queues; i++) {
415 rx = &internals->rx_queue[i];
417 if (rx->pcap != NULL)
420 if (strcmp(rx->type, ETH_PCAP_RX_PCAP_ARG) == 0) {
421 if (open_single_rx_pcap(rx->name, &rx->pcap) < 0)
425 else if (strcmp(rx->type, ETH_PCAP_RX_IFACE_ARG) == 0) {
426 if (open_single_iface(rx->name, &rx->pcap) < 0)
433 dev->data->dev_link.link_status = 1;
438 * This function gets called when the current port gets stopped.
439 * Is the only place for us to close all the tx streams dumpers.
440 * If not called the dumpers will be flushed within each tx burst.
443 eth_dev_stop(struct rte_eth_dev *dev)
446 struct pmd_internals *internals = dev->data->dev_private;
447 struct pcap_tx_queue *tx;
448 struct pcap_rx_queue *rx;
450 /* Special iface case. Single pcap is open and shared between tx/rx. */
451 if (internals->single_iface) {
452 tx = &internals->tx_queue[0];
453 rx = &internals->rx_queue[0];
454 pcap_close(tx->pcap);
460 for (i = 0; i < internals->nb_tx_queues; i++) {
461 tx = &internals->tx_queue[i];
463 if (tx->dumper != NULL) {
464 pcap_dump_close(tx->dumper);
468 if (tx->pcap != NULL) {
469 pcap_close(tx->pcap);
474 for (i = 0; i < internals->nb_rx_queues; i++) {
475 rx = &internals->rx_queue[i];
477 if (rx->pcap != NULL) {
478 pcap_close(rx->pcap);
484 dev->data->dev_link.link_status = 0;
488 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
494 eth_dev_info(struct rte_eth_dev *dev,
495 struct rte_eth_dev_info *dev_info)
497 struct pmd_internals *internals = dev->data->dev_private;
498 dev_info->driver_name = drivername;
499 dev_info->if_index = internals->if_index;
500 dev_info->max_mac_addrs = 1;
501 dev_info->max_rx_pktlen = (uint32_t) -1;
502 dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues;
503 dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues;
504 dev_info->min_rx_bufsize = 0;
505 dev_info->pci_dev = NULL;
509 eth_stats_get(struct rte_eth_dev *dev,
510 struct rte_eth_stats *igb_stats)
513 unsigned long rx_packets_total = 0, rx_bytes_total = 0;
514 unsigned long tx_packets_total = 0, tx_bytes_total = 0;
515 unsigned long tx_packets_err_total = 0;
516 const struct pmd_internals *internal = dev->data->dev_private;
518 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_rx_queues;
520 igb_stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts;
521 igb_stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes;
522 rx_packets_total += igb_stats->q_ipackets[i];
523 rx_bytes_total += igb_stats->q_ibytes[i];
526 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && i < internal->nb_tx_queues;
528 igb_stats->q_opackets[i] = internal->tx_queue[i].tx_pkts;
529 igb_stats->q_obytes[i] = internal->tx_queue[i].tx_bytes;
530 igb_stats->q_errors[i] = internal->tx_queue[i].err_pkts;
531 tx_packets_total += igb_stats->q_opackets[i];
532 tx_bytes_total += igb_stats->q_obytes[i];
533 tx_packets_err_total += igb_stats->q_errors[i];
536 igb_stats->ipackets = rx_packets_total;
537 igb_stats->ibytes = rx_bytes_total;
538 igb_stats->opackets = tx_packets_total;
539 igb_stats->obytes = tx_bytes_total;
540 igb_stats->oerrors = tx_packets_err_total;
544 eth_stats_reset(struct rte_eth_dev *dev)
547 struct pmd_internals *internal = dev->data->dev_private;
548 for (i = 0; i < internal->nb_rx_queues; i++) {
549 internal->rx_queue[i].rx_pkts = 0;
550 internal->rx_queue[i].rx_bytes = 0;
552 for (i = 0; i < internal->nb_tx_queues; i++) {
553 internal->tx_queue[i].tx_pkts = 0;
554 internal->tx_queue[i].tx_bytes = 0;
555 internal->tx_queue[i].err_pkts = 0;
560 eth_dev_close(struct rte_eth_dev *dev __rte_unused)
565 eth_queue_release(void *q __rte_unused)
570 eth_link_update(struct rte_eth_dev *dev __rte_unused,
571 int wait_to_complete __rte_unused)
577 eth_rx_queue_setup(struct rte_eth_dev *dev,
578 uint16_t rx_queue_id,
579 uint16_t nb_rx_desc __rte_unused,
580 unsigned int socket_id __rte_unused,
581 const struct rte_eth_rxconf *rx_conf __rte_unused,
582 struct rte_mempool *mb_pool)
584 struct pmd_internals *internals = dev->data->dev_private;
585 struct pcap_rx_queue *pcap_q = &internals->rx_queue[rx_queue_id];
586 pcap_q->mb_pool = mb_pool;
587 dev->data->rx_queues[rx_queue_id] = pcap_q;
588 pcap_q->in_port = dev->data->port_id;
593 eth_tx_queue_setup(struct rte_eth_dev *dev,
594 uint16_t tx_queue_id,
595 uint16_t nb_tx_desc __rte_unused,
596 unsigned int socket_id __rte_unused,
597 const struct rte_eth_txconf *tx_conf __rte_unused)
600 struct pmd_internals *internals = dev->data->dev_private;
601 dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id];
605 static const struct eth_dev_ops ops = {
606 .dev_start = eth_dev_start,
607 .dev_stop = eth_dev_stop,
608 .dev_close = eth_dev_close,
609 .dev_configure = eth_dev_configure,
610 .dev_infos_get = eth_dev_info,
611 .rx_queue_setup = eth_rx_queue_setup,
612 .tx_queue_setup = eth_tx_queue_setup,
613 .rx_queue_release = eth_queue_release,
614 .tx_queue_release = eth_queue_release,
615 .link_update = eth_link_update,
616 .stats_get = eth_stats_get,
617 .stats_reset = eth_stats_reset,
620 static struct eth_driver rte_pcap_pmd = {
622 .name = "rte_pcap_pmd",
623 .drv_flags = RTE_PCI_DRV_DETACHABLE,
628 * Function handler that opens the pcap file for reading a stores a
629 * reference of it for use it later on.
632 open_rx_pcap(const char *key, const char *value, void *extra_args)
635 const char *pcap_filename = value;
636 struct rx_pcaps *pcaps = extra_args;
639 for (i = 0; i < pcaps->num_of_rx; i++) {
640 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
643 pcaps->pcaps[i] = pcap;
644 pcaps->names[i] = pcap_filename;
645 pcaps->types[i] = key;
652 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
654 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
655 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
662 * Opens a pcap file for writing and stores a reference to it
663 * for use it later on.
666 open_tx_pcap(const char *key, const char *value, void *extra_args)
669 const char *pcap_filename = value;
670 struct tx_pcaps *dumpers = extra_args;
671 pcap_dumper_t *dumper;
673 for (i = 0; i < dumpers->num_of_tx; i++) {
674 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
677 dumpers->dumpers[i] = dumper;
678 dumpers->names[i] = pcap_filename;
679 dumpers->types[i] = key;
686 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
690 * We need to create a dummy empty pcap_t to use it
691 * with pcap_dump_open(). We create big enough an Ethernet
695 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
697 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
701 /* The dumper is created using the previous pcap_t reference */
702 if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
703 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
711 * pcap_open_live wrapper function
714 open_iface_live(const char *iface, pcap_t **pcap) {
715 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
716 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
719 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
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 rx_pcaps *pcaps = extra_args;
735 if (open_single_iface(iface, &pcap) < 0)
738 pcaps->pcaps[0] = pcap;
739 pcaps->names[0] = iface;
740 pcaps->types[0] = 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 rx_pcaps *pcaps = extra_args;
756 for (i = 0; i < pcaps->num_of_rx; i++) {
757 if (open_single_iface(iface, &pcap) < 0)
759 pcaps->pcaps[i] = pcap;
760 pcaps->names[i] = iface;
761 pcaps->types[i] = 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 tx_pcaps *pcaps = extra_args;
778 for (i = 0; i < pcaps->num_of_tx; i++) {
779 if (open_single_iface(iface, &pcap) < 0)
781 pcaps->pcaps[i] = pcap;
782 pcaps->names[i] = iface;
783 pcaps->types[i] = key;
790 open_single_iface(const char *iface, pcap_t **pcap)
792 if (open_iface_live(iface, pcap) < 0) {
793 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
801 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
802 const unsigned nb_tx_queues,
803 const unsigned numa_node,
804 struct pmd_internals **internals,
805 struct rte_eth_dev **eth_dev,
806 struct rte_kvargs *kvlist)
808 struct rte_eth_dev_data *data = NULL;
809 struct rte_pci_device *pci_dev = NULL;
811 struct rte_kvargs_pair *pair = NULL;
813 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
814 pair = &kvlist->pairs[k_idx];
815 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
820 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
822 /* now do all data allocation - for eth_dev structure, dummy pci driver
823 * and internal (private) data
825 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
829 pci_dev = rte_zmalloc_socket(name, sizeof(*pci_dev), 0, numa_node);
833 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
834 if (*internals == NULL)
837 /* reserve an ethdev entry */
838 *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
839 if (*eth_dev == NULL)
842 /* check length of device name */
843 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
846 /* now put it all together
847 * - store queue data in internals,
848 * - store numa_node info in pci_driver
849 * - point eth_dev_data to internals and pci_driver
850 * - and point eth_dev structure to new eth_dev_data structure
852 /* NOTE: we'll replace the data element, of originally allocated eth_dev
853 * so the rings are local per-process */
855 (*internals)->nb_rx_queues = nb_rx_queues;
856 (*internals)->nb_tx_queues = nb_tx_queues;
859 (*internals)->if_index = 0;
861 (*internals)->if_index = if_nametoindex(pair->value);
863 pci_dev->numa_node = numa_node;
865 data->dev_private = *internals;
866 data->port_id = (*eth_dev)->data->port_id;
867 snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
868 data->nb_rx_queues = (uint16_t)nb_rx_queues;
869 data->nb_tx_queues = (uint16_t)nb_tx_queues;
870 data->dev_link = pmd_link;
871 data->mac_addrs = ð_addr;
873 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
875 (*eth_dev)->data = data;
876 (*eth_dev)->dev_ops = &ops;
877 (*eth_dev)->pci_dev = pci_dev;
878 (*eth_dev)->driver = &rte_pcap_pmd;
885 rte_free(*internals);
891 rte_eth_from_pcaps_n_dumpers(const char *name,
892 struct rx_pcaps *rx_queues,
893 const unsigned nb_rx_queues,
894 struct tx_pcaps *tx_queues,
895 const unsigned nb_tx_queues,
896 const unsigned numa_node,
897 struct rte_kvargs *kvlist)
899 struct pmd_internals *internals = NULL;
900 struct rte_eth_dev *eth_dev = NULL;
903 /* do some parameter checking */
904 if (rx_queues == NULL && nb_rx_queues > 0)
906 if (tx_queues == NULL && nb_tx_queues > 0)
909 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
910 &internals, ð_dev, kvlist) < 0)
913 for (i = 0; i < nb_rx_queues; i++) {
914 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
915 snprintf(internals->rx_queue[i].name,
916 sizeof(internals->rx_queue[i].name), "%s",
917 rx_queues->names[i]);
918 snprintf(internals->rx_queue[i].type,
919 sizeof(internals->rx_queue[i].type), "%s",
920 rx_queues->types[i]);
922 for (i = 0; i < nb_tx_queues; i++) {
923 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
924 snprintf(internals->tx_queue[i].name,
925 sizeof(internals->tx_queue[i].name), "%s",
926 tx_queues->names[i]);
927 snprintf(internals->tx_queue[i].type,
928 sizeof(internals->tx_queue[i].type), "%s",
929 tx_queues->types[i]);
932 /* using multiple pcaps/interfaces */
933 internals->single_iface = 0;
935 eth_dev->rx_pkt_burst = eth_pcap_rx;
936 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
942 rte_eth_from_pcaps(const char *name,
943 struct rx_pcaps *rx_queues,
944 const unsigned nb_rx_queues,
945 struct tx_pcaps *tx_queues,
946 const unsigned nb_tx_queues,
947 const unsigned numa_node,
948 struct rte_kvargs *kvlist,
951 struct pmd_internals *internals = NULL;
952 struct rte_eth_dev *eth_dev = NULL;
955 /* do some parameter checking */
956 if (rx_queues == NULL && nb_rx_queues > 0)
958 if (tx_queues == NULL && nb_tx_queues > 0)
961 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
962 &internals, ð_dev, kvlist) < 0)
965 for (i = 0; i < nb_rx_queues; i++) {
966 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
967 snprintf(internals->rx_queue[i].name,
968 sizeof(internals->rx_queue[i].name), "%s",
969 rx_queues->names[i]);
970 snprintf(internals->rx_queue[i].type,
971 sizeof(internals->rx_queue[i].type), "%s",
972 rx_queues->types[i]);
974 for (i = 0; i < nb_tx_queues; i++) {
975 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
976 snprintf(internals->tx_queue[i].name,
977 sizeof(internals->tx_queue[i].name), "%s",
978 tx_queues->names[i]);
979 snprintf(internals->tx_queue[i].type,
980 sizeof(internals->tx_queue[i].type), "%s",
981 tx_queues->types[i]);
984 /* store wether we are using a single interface for rx/tx or not */
985 internals->single_iface = single_iface;
987 eth_dev->rx_pkt_burst = eth_pcap_rx;
988 eth_dev->tx_pkt_burst = eth_pcap_tx;
995 rte_pmd_pcap_devinit(const char *name, const char *params)
997 unsigned numa_node, using_dumpers = 0;
999 struct rte_kvargs *kvlist;
1000 struct rx_pcaps pcaps;
1001 struct tx_pcaps dumpers;
1003 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
1005 numa_node = rte_socket_id();
1007 gettimeofday(&start_time, NULL);
1008 start_cycles = rte_get_timer_cycles();
1009 hz = rte_get_timer_hz();
1011 kvlist = rte_kvargs_parse(params, valid_arguments);
1016 * If iface argument is passed we open the NICs and use them for
1019 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1021 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1022 &open_rx_tx_iface, &pcaps);
1025 dumpers.pcaps[0] = pcaps.pcaps[0];
1026 dumpers.names[0] = pcaps.names[0];
1027 dumpers.types[0] = pcaps.types[0];
1028 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
1029 numa_node, kvlist, 1);
1034 * We check whether we want to open a RX stream from a real NIC or a
1037 if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
1038 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1039 &open_rx_pcap, &pcaps);
1041 pcaps.num_of_rx = rte_kvargs_count(kvlist,
1042 ETH_PCAP_RX_IFACE_ARG);
1043 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1044 &open_rx_iface, &pcaps);
1051 * We check whether we want to open a TX stream to a real NIC or a
1054 if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
1055 ETH_PCAP_TX_PCAP_ARG))) {
1056 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1057 &open_tx_pcap, &dumpers);
1060 dumpers.num_of_tx = rte_kvargs_count(kvlist,
1061 ETH_PCAP_TX_IFACE_ARG);
1062 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1063 &open_tx_iface, &dumpers);
1070 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
1071 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
1073 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
1074 dumpers.num_of_tx, numa_node, kvlist, 0);
1077 rte_kvargs_free(kvlist);
1082 rte_pmd_pcap_devuninit(const char *name)
1084 struct rte_eth_dev *eth_dev = NULL;
1086 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1092 /* reserve an ethdev entry */
1093 eth_dev = rte_eth_dev_allocated(name);
1094 if (eth_dev == NULL)
1097 rte_free(eth_dev->data->dev_private);
1098 rte_free(eth_dev->data);
1099 rte_free(eth_dev->pci_dev);
1101 rte_eth_dev_release_port(eth_dev);
1106 static struct rte_driver pmd_pcap_drv = {
1109 .init = rte_pmd_pcap_devinit,
1110 .uninit = rte_pmd_pcap_devuninit,
1113 PMD_REGISTER_DRIVER(pmd_pcap_drv);