4 * Copyright(c) 2010-2015 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,
621 * Function handler that opens the pcap file for reading a stores a
622 * reference of it for use it later on.
625 open_rx_pcap(const char *key, const char *value, void *extra_args)
628 const char *pcap_filename = value;
629 struct rx_pcaps *pcaps = extra_args;
632 for (i = 0; i < pcaps->num_of_rx; i++) {
633 if (open_single_rx_pcap(pcap_filename, &pcap) < 0)
636 pcaps->pcaps[i] = pcap;
637 pcaps->names[i] = pcap_filename;
638 pcaps->types[i] = key;
645 open_single_rx_pcap(const char *pcap_filename, pcap_t **pcap)
647 if ((*pcap = pcap_open_offline(pcap_filename, errbuf)) == NULL) {
648 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", pcap_filename, errbuf);
655 * Opens a pcap file for writing and stores a reference to it
656 * for use it later on.
659 open_tx_pcap(const char *key, const char *value, void *extra_args)
662 const char *pcap_filename = value;
663 struct tx_pcaps *dumpers = extra_args;
664 pcap_dumper_t *dumper;
666 for (i = 0; i < dumpers->num_of_tx; i++) {
667 if (open_single_tx_pcap(pcap_filename, &dumper) < 0)
670 dumpers->dumpers[i] = dumper;
671 dumpers->names[i] = pcap_filename;
672 dumpers->types[i] = key;
679 open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper)
683 * We need to create a dummy empty pcap_t to use it
684 * with pcap_dump_open(). We create big enough an Ethernet
688 if ((tx_pcap = pcap_open_dead(DLT_EN10MB, RTE_ETH_PCAP_SNAPSHOT_LEN))
690 RTE_LOG(ERR, PMD, "Couldn't create dead pcap\n");
694 /* The dumper is created using the previous pcap_t reference */
695 if ((*dumper = pcap_dump_open(tx_pcap, pcap_filename)) == NULL) {
696 RTE_LOG(ERR, PMD, "Couldn't open %s for writing.\n", pcap_filename);
704 * pcap_open_live wrapper function
707 open_iface_live(const char *iface, pcap_t **pcap) {
708 *pcap = pcap_open_live(iface, RTE_ETH_PCAP_SNAPLEN,
709 RTE_ETH_PCAP_PROMISC, RTE_ETH_PCAP_TIMEOUT, errbuf);
712 RTE_LOG(ERR, PMD, "Couldn't open %s: %s\n", iface, errbuf);
719 * Opens an interface for reading and writing
722 open_rx_tx_iface(const char *key, const char *value, void *extra_args)
724 const char *iface = value;
725 struct rx_pcaps *pcaps = extra_args;
728 if (open_single_iface(iface, &pcap) < 0)
731 pcaps->pcaps[0] = pcap;
732 pcaps->names[0] = iface;
733 pcaps->types[0] = key;
739 * Opens a NIC for reading packets from it
742 open_rx_iface(const char *key, const char *value, void *extra_args)
745 const char *iface = value;
746 struct rx_pcaps *pcaps = extra_args;
749 for (i = 0; i < pcaps->num_of_rx; i++) {
750 if (open_single_iface(iface, &pcap) < 0)
752 pcaps->pcaps[i] = pcap;
753 pcaps->names[i] = iface;
754 pcaps->types[i] = key;
761 * Opens a NIC for writing packets to it
764 open_tx_iface(const char *key, const char *value, void *extra_args)
767 const char *iface = value;
768 struct tx_pcaps *pcaps = extra_args;
771 for (i = 0; i < pcaps->num_of_tx; i++) {
772 if (open_single_iface(iface, &pcap) < 0)
774 pcaps->pcaps[i] = pcap;
775 pcaps->names[i] = iface;
776 pcaps->types[i] = key;
783 open_single_iface(const char *iface, pcap_t **pcap)
785 if (open_iface_live(iface, pcap) < 0) {
786 RTE_LOG(ERR, PMD, "Couldn't open interface %s\n", iface);
794 rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues,
795 const unsigned nb_tx_queues,
796 const unsigned numa_node,
797 struct pmd_internals **internals,
798 struct rte_eth_dev **eth_dev,
799 struct rte_kvargs *kvlist)
801 struct rte_eth_dev_data *data = NULL;
803 struct rte_kvargs_pair *pair = NULL;
805 for (k_idx = 0; k_idx < kvlist->count; k_idx++) {
806 pair = &kvlist->pairs[k_idx];
807 if (strstr(pair->key, ETH_PCAP_IFACE_ARG) != NULL)
812 "Creating pcap-backed ethdev on numa socket %u\n", numa_node);
814 /* now do all data allocation - for eth_dev structure
815 * and internal (private) data
817 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
821 *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, numa_node);
822 if (*internals == NULL)
825 /* reserve an ethdev entry */
826 *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
827 if (*eth_dev == NULL)
830 /* check length of device name */
831 if ((strlen((*eth_dev)->data->name) + 1) > sizeof(data->name))
834 /* now put it all together
835 * - store queue data in internals,
836 * - store numa_node info in eth_dev
837 * - point eth_dev_data to internals
838 * - and point eth_dev structure to new eth_dev_data structure
840 /* NOTE: we'll replace the data element, of originally allocated eth_dev
841 * so the rings are local per-process */
843 (*internals)->nb_rx_queues = nb_rx_queues;
844 (*internals)->nb_tx_queues = nb_tx_queues;
847 (*internals)->if_index = 0;
849 (*internals)->if_index = if_nametoindex(pair->value);
851 data->dev_private = *internals;
852 data->port_id = (*eth_dev)->data->port_id;
853 snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name);
854 data->nb_rx_queues = (uint16_t)nb_rx_queues;
855 data->nb_tx_queues = (uint16_t)nb_tx_queues;
856 data->dev_link = pmd_link;
857 data->mac_addrs = ð_addr;
859 (*eth_dev)->data->name, strlen((*eth_dev)->data->name));
861 (*eth_dev)->data = data;
862 (*eth_dev)->dev_ops = &ops;
863 (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE;
864 (*eth_dev)->driver = NULL;
865 (*eth_dev)->data->kdrv = RTE_KDRV_NONE;
866 (*eth_dev)->data->drv_name = drivername;
867 (*eth_dev)->data->numa_node = numa_node;
873 rte_free(*internals);
879 rte_eth_from_pcaps_n_dumpers(const char *name,
880 struct rx_pcaps *rx_queues,
881 const unsigned nb_rx_queues,
882 struct tx_pcaps *tx_queues,
883 const unsigned nb_tx_queues,
884 const unsigned numa_node,
885 struct rte_kvargs *kvlist)
887 struct pmd_internals *internals = NULL;
888 struct rte_eth_dev *eth_dev = NULL;
891 /* do some parameter checking */
892 if (rx_queues == NULL && nb_rx_queues > 0)
894 if (tx_queues == NULL && nb_tx_queues > 0)
897 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
898 &internals, ð_dev, kvlist) < 0)
901 for (i = 0; i < nb_rx_queues; i++) {
902 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
903 snprintf(internals->rx_queue[i].name,
904 sizeof(internals->rx_queue[i].name), "%s",
905 rx_queues->names[i]);
906 snprintf(internals->rx_queue[i].type,
907 sizeof(internals->rx_queue[i].type), "%s",
908 rx_queues->types[i]);
910 for (i = 0; i < nb_tx_queues; i++) {
911 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
912 snprintf(internals->tx_queue[i].name,
913 sizeof(internals->tx_queue[i].name), "%s",
914 tx_queues->names[i]);
915 snprintf(internals->tx_queue[i].type,
916 sizeof(internals->tx_queue[i].type), "%s",
917 tx_queues->types[i]);
920 /* using multiple pcaps/interfaces */
921 internals->single_iface = 0;
923 eth_dev->rx_pkt_burst = eth_pcap_rx;
924 eth_dev->tx_pkt_burst = eth_pcap_tx_dumper;
930 rte_eth_from_pcaps(const char *name,
931 struct rx_pcaps *rx_queues,
932 const unsigned nb_rx_queues,
933 struct tx_pcaps *tx_queues,
934 const unsigned nb_tx_queues,
935 const unsigned numa_node,
936 struct rte_kvargs *kvlist,
939 struct pmd_internals *internals = NULL;
940 struct rte_eth_dev *eth_dev = NULL;
943 /* do some parameter checking */
944 if (rx_queues == NULL && nb_rx_queues > 0)
946 if (tx_queues == NULL && nb_tx_queues > 0)
949 if (rte_pmd_init_internals(name, nb_rx_queues, nb_tx_queues, numa_node,
950 &internals, ð_dev, kvlist) < 0)
953 for (i = 0; i < nb_rx_queues; i++) {
954 internals->rx_queue[i].pcap = rx_queues->pcaps[i];
955 snprintf(internals->rx_queue[i].name,
956 sizeof(internals->rx_queue[i].name), "%s",
957 rx_queues->names[i]);
958 snprintf(internals->rx_queue[i].type,
959 sizeof(internals->rx_queue[i].type), "%s",
960 rx_queues->types[i]);
962 for (i = 0; i < nb_tx_queues; i++) {
963 internals->tx_queue[i].dumper = tx_queues->dumpers[i];
964 snprintf(internals->tx_queue[i].name,
965 sizeof(internals->tx_queue[i].name), "%s",
966 tx_queues->names[i]);
967 snprintf(internals->tx_queue[i].type,
968 sizeof(internals->tx_queue[i].type), "%s",
969 tx_queues->types[i]);
972 /* store wether we are using a single interface for rx/tx or not */
973 internals->single_iface = single_iface;
975 eth_dev->rx_pkt_burst = eth_pcap_rx;
976 eth_dev->tx_pkt_burst = eth_pcap_tx;
983 rte_pmd_pcap_devinit(const char *name, const char *params)
985 unsigned numa_node, using_dumpers = 0;
987 struct rte_kvargs *kvlist;
988 struct rx_pcaps pcaps;
989 struct tx_pcaps dumpers;
991 RTE_LOG(INFO, PMD, "Initializing pmd_pcap for %s\n", name);
993 numa_node = rte_socket_id();
995 gettimeofday(&start_time, NULL);
996 start_cycles = rte_get_timer_cycles();
997 hz = rte_get_timer_hz();
999 kvlist = rte_kvargs_parse(params, valid_arguments);
1004 * If iface argument is passed we open the NICs and use them for
1007 if (rte_kvargs_count(kvlist, ETH_PCAP_IFACE_ARG) == 1) {
1009 ret = rte_kvargs_process(kvlist, ETH_PCAP_IFACE_ARG,
1010 &open_rx_tx_iface, &pcaps);
1013 dumpers.pcaps[0] = pcaps.pcaps[0];
1014 dumpers.names[0] = pcaps.names[0];
1015 dumpers.types[0] = pcaps.types[0];
1016 ret = rte_eth_from_pcaps(name, &pcaps, 1, &dumpers, 1,
1017 numa_node, kvlist, 1);
1022 * We check whether we want to open a RX stream from a real NIC or a
1025 if ((pcaps.num_of_rx = rte_kvargs_count(kvlist, ETH_PCAP_RX_PCAP_ARG))) {
1026 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_PCAP_ARG,
1027 &open_rx_pcap, &pcaps);
1029 pcaps.num_of_rx = rte_kvargs_count(kvlist,
1030 ETH_PCAP_RX_IFACE_ARG);
1031 ret = rte_kvargs_process(kvlist, ETH_PCAP_RX_IFACE_ARG,
1032 &open_rx_iface, &pcaps);
1039 * We check whether we want to open a TX stream to a real NIC or a
1042 if ((dumpers.num_of_tx = rte_kvargs_count(kvlist,
1043 ETH_PCAP_TX_PCAP_ARG))) {
1044 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_PCAP_ARG,
1045 &open_tx_pcap, &dumpers);
1048 dumpers.num_of_tx = rte_kvargs_count(kvlist,
1049 ETH_PCAP_TX_IFACE_ARG);
1050 ret = rte_kvargs_process(kvlist, ETH_PCAP_TX_IFACE_ARG,
1051 &open_tx_iface, &dumpers);
1058 ret = rte_eth_from_pcaps_n_dumpers(name, &pcaps, pcaps.num_of_rx,
1059 &dumpers, dumpers.num_of_tx, numa_node, kvlist);
1061 ret = rte_eth_from_pcaps(name, &pcaps, pcaps.num_of_rx, &dumpers,
1062 dumpers.num_of_tx, numa_node, kvlist, 0);
1065 rte_kvargs_free(kvlist);
1070 rte_pmd_pcap_devuninit(const char *name)
1072 struct rte_eth_dev *eth_dev = NULL;
1074 RTE_LOG(INFO, PMD, "Closing pcap ethdev on numa socket %u\n",
1080 /* reserve an ethdev entry */
1081 eth_dev = rte_eth_dev_allocated(name);
1082 if (eth_dev == NULL)
1085 rte_free(eth_dev->data->dev_private);
1086 rte_free(eth_dev->data);
1088 rte_eth_dev_release_port(eth_dev);
1093 static struct rte_driver pmd_pcap_drv = {
1096 .init = rte_pmd_pcap_devinit,
1097 .uninit = rte_pmd_pcap_devuninit,
1100 PMD_REGISTER_DRIVER(pmd_pcap_drv);