4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_mempool.h>
38 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
45 #include <rte_ether.h>
53 #include "rte_port_source_sink.h"
58 #ifdef RTE_PORT_STATS_COLLECT
60 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val) \
61 port->stats.n_pkts_in += val
62 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val) \
63 port->stats.n_pkts_drop += val
67 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val)
68 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val)
72 struct rte_port_source {
73 struct rte_port_in_stats stats;
75 struct rte_mempool *mempool;
77 /* PCAP buffers and indices */
90 pcap_source_load(struct rte_port_source *port,
91 const char *file_name,
92 uint32_t n_bytes_per_pkt,
97 uint32_t *pkt_len_aligns = NULL;
98 size_t total_buff_len = 0;
100 char pcap_errbuf[PCAP_ERRBUF_SIZE];
102 struct pcap_pkthdr pcap_hdr;
104 uint8_t *buff = NULL;
105 uint32_t pktmbuf_maxlen = (uint32_t)
106 (rte_pktmbuf_data_room_size(port->mempool) -
107 RTE_PKTMBUF_HEADROOM);
109 if (n_bytes_per_pkt == 0)
110 max_len = pktmbuf_maxlen;
112 max_len = RTE_MIN(n_bytes_per_pkt, pktmbuf_maxlen);
114 /* first time open, get packet number */
115 pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
116 if (pcap_handle == NULL) {
117 RTE_LOG(ERR, PORT, "Failed to open pcap file "
118 "'%s' for reading\n", file_name);
122 while ((pkt = pcap_next(pcap_handle, &pcap_hdr)) != NULL)
125 pcap_close(pcap_handle);
127 port->pkt_len = rte_zmalloc_socket("PCAP",
128 (sizeof(*port->pkt_len) * n_pkts), 0, socket_id);
129 if (port->pkt_len == NULL) {
130 RTE_LOG(ERR, PORT, "No enough memory\n");
134 pkt_len_aligns = rte_malloc("PCAP",
135 (sizeof(*pkt_len_aligns) * n_pkts), 0);
136 if (pkt_len_aligns == NULL) {
137 RTE_LOG(ERR, PORT, "No enough memory\n");
141 port->pkts = rte_zmalloc_socket("PCAP",
142 (sizeof(*port->pkts) * n_pkts), 0, socket_id);
143 if (port->pkts == NULL) {
144 RTE_LOG(ERR, PORT, "No enough memory\n");
148 /* open 2nd time, get pkt_len */
149 pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
150 if (pcap_handle == NULL) {
151 RTE_LOG(ERR, PORT, "Failed to open pcap file "
152 "'%s' for reading\n", file_name);
156 for (i = 0; i < n_pkts; i++) {
157 pkt = pcap_next(pcap_handle, &pcap_hdr);
158 port->pkt_len[i] = RTE_MIN(max_len, pcap_hdr.len);
159 pkt_len_aligns[i] = RTE_CACHE_LINE_ROUNDUP(
161 total_buff_len += pkt_len_aligns[i];
164 pcap_close(pcap_handle);
166 /* allocate a big trunk of data for pcap file load */
167 buff = rte_zmalloc_socket("PCAP",
168 total_buff_len, 0, socket_id);
170 RTE_LOG(ERR, PORT, "No enough memory\n");
174 port->pkt_buff = buff;
176 /* open file one last time to copy the pkt content */
177 pcap_handle = pcap_open_offline(file_name, pcap_errbuf);
178 if (pcap_handle == NULL) {
179 RTE_LOG(ERR, PORT, "Failed to open pcap file "
180 "'%s' for reading\n", file_name);
184 for (i = 0; i < n_pkts; i++) {
185 pkt = pcap_next(pcap_handle, &pcap_hdr);
186 rte_memcpy(buff, pkt, port->pkt_len[i]);
187 port->pkts[i] = buff;
188 buff += pkt_len_aligns[i];
191 pcap_close(pcap_handle);
193 port->n_pkts = n_pkts;
195 rte_free(pkt_len_aligns);
197 RTE_LOG(INFO, PORT, "Successfully load pcap file "
198 "'%s' with %u pkts\n",
199 file_name, port->n_pkts);
205 rte_free(pkt_len_aligns);
207 rte_free(port->pkt_len);
209 rte_free(port->pkts);
211 rte_free(port->pkt_buff);
216 #define PCAP_SOURCE_LOAD(port, file_name, n_bytes, socket_id) \
217 pcap_source_load(port, file_name, n_bytes, socket_id)
219 #else /* RTE_PORT_PCAP */
221 #define PCAP_SOURCE_LOAD(port, file_name, n_bytes, socket_id) \
226 RTE_LOG(ERR, PORT, "Source port field " \
227 "\"file_name\" is not NULL.\n"); \
234 #endif /* RTE_PORT_PCAP */
236 #endif /* RTE_NEXT_ABI */
239 rte_port_source_create(void *params, int socket_id)
241 struct rte_port_source_params *p =
242 (struct rte_port_source_params *) params;
243 struct rte_port_source *port;
245 /* Check input arguments*/
246 if ((p == NULL) || (p->mempool == NULL)) {
247 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
251 /* Memory allocation */
252 port = rte_zmalloc_socket("PORT", sizeof(*port),
253 RTE_CACHE_LINE_SIZE, socket_id);
255 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
260 port->mempool = (struct rte_mempool *) p->mempool;
265 int status = PCAP_SOURCE_LOAD(port, p->file_name,
266 p->n_bytes_per_pkt, socket_id);
280 rte_port_source_free(void *port)
282 struct rte_port_source *p =
283 (struct rte_port_source *)port;
285 /* Check input parameters */
292 rte_free(p->pkt_len);
296 rte_free(p->pkt_buff);
305 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
307 struct rte_port_source *p = (struct rte_port_source *) port;
310 if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
313 for (i = 0; i < n_pkts; i++) {
314 rte_mbuf_refcnt_set(pkts[i], 1);
315 rte_pktmbuf_reset(pkts[i]);
320 if (p->pkt_buff != NULL) {
321 for (i = 0; i < n_pkts; i++) {
322 uint8_t *pkt_data = rte_pktmbuf_mtod(pkts[i],
325 rte_memcpy(pkt_data, p->pkts[p->pkt_index],
326 p->pkt_len[p->pkt_index]);
327 pkts[i]->data_len = p->pkt_len[p->pkt_index];
328 pkts[i]->pkt_len = pkts[i]->data_len;
331 if (p->pkt_index >= p->n_pkts)
338 RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
344 rte_port_source_stats_read(void *port,
345 struct rte_port_in_stats *stats, int clear)
347 struct rte_port_source *p =
348 (struct rte_port_source *) port;
351 memcpy(stats, &p->stats, sizeof(p->stats));
354 memset(&p->stats, 0, sizeof(p->stats));
362 #ifdef RTE_PORT_STATS_COLLECT
364 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val) \
365 (port->stats.n_pkts_in += val)
366 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val) \
367 (port->stats.n_pkts_drop += val)
371 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val)
372 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val)
376 struct rte_port_sink {
377 struct rte_port_out_stats stats;
379 /* PCAP dumper handle and pkts number */
383 uint32_t dump_finish;
389 * Open PCAP file for dumping packets to the file later
392 * Handle to sink port
394 * Sink port parameter
397 * error code otherwise
400 pcap_sink_open(struct rte_port_sink *port,
401 __rte_unused struct rte_port_sink_params *p)
404 pcap_dumper_t *pcap_dumper;
406 if (p->file_name == NULL) {
410 port->dump_finish = 0;
414 /** Open a dead pcap handler for opening dumper file */
415 tx_pcap = pcap_open_dead(DLT_EN10MB, 65535);
419 /* The dumper is created using the previous pcap_t reference */
420 pcap_dumper = pcap_dump_open(tx_pcap, p->file_name);
421 if (pcap_dumper == NULL)
424 port->dumper = pcap_dumper;
425 port->max_pkts = p->max_n_pkts;
427 port->dump_finish = 0;
432 uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN];
435 * Dump a packet to PCAP dumper
438 * Handle to sink port
440 * Handle to mbuf structure holding the packet
443 pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf)
445 uint8_t *pcap_dumper = (uint8_t *)(port->dumper);
446 struct pcap_pkthdr pcap_hdr;
449 /* Maximum num packets already reached */
450 if (port->dump_finish)
453 pkt = rte_pktmbuf_mtod(mbuf, uint8_t *);
455 pcap_hdr.len = mbuf->pkt_len;
456 pcap_hdr.caplen = pcap_hdr.len;
457 gettimeofday(&(pcap_hdr.ts), NULL);
459 if (mbuf->nb_segs > 1) {
460 struct rte_mbuf *jumbo_mbuf;
461 uint32_t pkt_index = 0;
463 /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN,
466 if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN)
469 for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL;
470 jumbo_mbuf = jumbo_mbuf->next) {
471 rte_memcpy(&jumbo_pkt_buf[pkt_index],
472 rte_pktmbuf_mtod(jumbo_mbuf, uint8_t *),
473 jumbo_mbuf->data_len);
474 pkt_index += jumbo_mbuf->data_len;
477 jumbo_pkt_buf[pkt_index] = '\0';
482 pcap_dump(pcap_dumper, &pcap_hdr, pkt);
486 if ((port->max_pkts != 0) && (port->pkt_index >= port->max_pkts)) {
487 port->dump_finish = 1;
488 RTE_LOG(INFO, PORT, "Dumped %u packets to file\n",
498 * Handle to pcap dumper
502 pcap_sink_flush_pkt(void *dumper)
504 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
506 pcap_dump_flush(pcap_dumper);
510 * Close a PCAP dumper handle
513 * Handle to pcap dumper
516 pcap_sink_close(void *dumper)
518 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
520 pcap_dump_close(pcap_dumper);
526 pcap_sink_open(struct rte_port_sink *port,
527 __rte_unused struct rte_port_sink_params *p)
532 port->dump_finish = 0;
538 pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
539 __rte_unused struct rte_mbuf *mbuf) {}
542 pcap_sink_flush_pkt(__rte_unused void *dumper) {}
545 pcap_sink_close(__rte_unused void *dumper) {}
550 rte_port_sink_create(__rte_unused void *params, int socket_id)
552 struct rte_port_sink *port;
553 struct rte_port_sink_params *p = params;
556 /* Memory allocation */
557 port = rte_zmalloc_socket("PORT", sizeof(*port),
558 RTE_CACHE_LINE_SIZE, socket_id);
560 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
564 /* Try to open PCAP file for dumping, if possible */
565 status = pcap_sink_open(port, p);
567 if (port->dumper != NULL)
568 RTE_LOG(INFO, PORT, "Ready to dump packets "
569 "to file %s\n", p->file_name);
571 } else if (status != -ENOTSUP) {
572 if (status == -ENOENT)
573 RTE_LOG(ERR, PORT, "%s: Failed to open pcap file "
574 "%s for writing\n", __func__,
577 RTE_LOG(ERR, PORT, "%s: Failed to enable pcap "
578 "support for unknown reason\n", __func__);
588 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
590 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
592 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
593 if (p->dumper != NULL)
594 pcap_sink_dump_pkt(p, pkt);
595 rte_pktmbuf_free(pkt);
596 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
602 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
605 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
607 if ((pkts_mask & (pkts_mask + 1)) == 0) {
608 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
611 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
612 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
615 for (i = 0; i < n_pkts; i++) {
616 struct rte_mbuf *pkt = pkts[i];
618 pcap_sink_dump_pkt(p, pkt);
622 for (i = 0; i < n_pkts; i++) {
623 struct rte_mbuf *pkt = pkts[i];
625 rte_pktmbuf_free(pkt);
630 uint64_t dump_pkts_mask = pkts_mask;
633 for ( ; dump_pkts_mask; ) {
634 pkt_index = __builtin_ctzll(
636 pcap_sink_dump_pkt(p, pkts[pkt_index]);
637 dump_pkts_mask &= ~(1LLU << pkt_index);
641 for ( ; pkts_mask; ) {
642 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
643 uint64_t pkt_mask = 1LLU << pkt_index;
644 struct rte_mbuf *pkt = pkts[pkt_index];
646 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
647 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
648 rte_pktmbuf_free(pkt);
649 pkts_mask &= ~pkt_mask;
657 rte_port_sink_flush(void *port)
659 struct rte_port_sink *p = (struct rte_port_sink *)port;
661 if (p->dumper != NULL)
662 pcap_sink_flush_pkt(p->dumper);
668 rte_port_sink_free(void *port)
670 struct rte_port_sink *p =
671 (struct rte_port_sink *)port;
672 /* Check input parameters */
676 if (p->dumper != NULL)
677 pcap_sink_close(p->dumper);
685 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
688 struct rte_port_sink *p =
689 (struct rte_port_sink *) port;
692 memcpy(stats, &p->stats, sizeof(p->stats));
695 memset(&p->stats, 0, sizeof(p->stats));
701 * Summary of port operations
703 struct rte_port_in_ops rte_port_source_ops = {
704 .f_create = rte_port_source_create,
705 .f_free = rte_port_source_free,
706 .f_rx = rte_port_source_rx,
707 .f_stats = rte_port_source_stats_read,
710 struct rte_port_out_ops rte_port_sink_ops = {
711 .f_create = rte_port_sink_create,
712 .f_free = rte_port_sink_free,
713 .f_tx = rte_port_sink_tx,
714 .f_tx_bulk = rte_port_sink_tx_bulk,
715 .f_flush = rte_port_sink_flush,
716 .f_stats = rte_port_sink_stats_read,