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>
42 #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;
78 /* PCAP buffers and indexes */
92 * Load PCAP file, allocate and copy packets in the file to memory
95 * Parameters for source port
97 * Handle to source port
99 * Socket id where the memory is created
102 * error code otherwise
105 pcap_source_load(struct rte_port_source_params *p,
106 struct rte_port_source *port,
112 uint32_t *pkt_len_aligns = NULL;
113 size_t total_buff_len = 0;
115 char pcap_errbuf[PCAP_ERRBUF_SIZE];
117 struct pcap_pkthdr pcap_hdr;
119 uint8_t *buff = NULL;
120 uint32_t pktmbuf_maxlen = (uint32_t)
121 (rte_pktmbuf_data_room_size(port->mempool) -
122 RTE_PKTMBUF_HEADROOM);
124 if (p->file_name == NULL)
127 if (p->n_bytes_per_pkt == 0)
128 max_len = pktmbuf_maxlen;
130 max_len = RTE_MIN(p->n_bytes_per_pkt, pktmbuf_maxlen);
132 /* first time open, get packet number */
133 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
134 if (pcap_handle == NULL) {
139 while ((pkt = pcap_next(pcap_handle, &pcap_hdr)) != NULL)
142 pcap_close(pcap_handle);
144 port->pkt_len = rte_zmalloc_socket("PCAP",
145 (sizeof(*port->pkt_len) * n_pkts), 0, socket_id);
146 if (port->pkt_len == NULL) {
151 pkt_len_aligns = rte_malloc("PCAP",
152 (sizeof(*pkt_len_aligns) * n_pkts), 0);
153 if (pkt_len_aligns == NULL) {
158 port->pkts = rte_zmalloc_socket("PCAP",
159 (sizeof(*port->pkts) * n_pkts), 0, socket_id);
160 if (port->pkts == NULL) {
165 /* open 2nd time, get pkt_len */
166 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
167 if (pcap_handle == NULL) {
172 for (i = 0; i < n_pkts; i++) {
173 pkt = pcap_next(pcap_handle, &pcap_hdr);
174 port->pkt_len[i] = RTE_MIN(max_len, pcap_hdr.len);
175 pkt_len_aligns[i] = RTE_CACHE_LINE_ROUNDUP(
177 total_buff_len += pkt_len_aligns[i];
180 pcap_close(pcap_handle);
182 /* allocate a big trunk of data for pcap file load */
183 buff = rte_zmalloc_socket("PCAP",
184 total_buff_len, 0, socket_id);
190 port->pkt_buff = buff;
192 /* open file one last time to copy the pkt content */
193 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
194 if (pcap_handle == NULL) {
199 for (i = 0; i < n_pkts; i++) {
200 pkt = pcap_next(pcap_handle, &pcap_hdr);
201 rte_memcpy(buff, pkt, port->pkt_len[i]);
202 port->pkts[i] = buff;
203 buff += pkt_len_aligns[i];
206 pcap_close(pcap_handle);
208 port->n_pkts = n_pkts;
210 rte_free(pkt_len_aligns);
216 rte_free(pkt_len_aligns);
218 rte_free(port->pkt_len);
220 rte_free(port->pkts);
222 rte_free(port->pkt_buff);
229 pcap_source_load(__rte_unused struct rte_port_source_params *p,
230 struct rte_port_source *port,
231 __rte_unused int socket_id)
233 port->pkt_buff = NULL;
234 port->pkt_len = NULL;
240 #endif /* RTE_PORT_PCAP */
245 rte_port_source_create(void *params, int socket_id)
247 struct rte_port_source_params *p =
248 (struct rte_port_source_params *) params;
249 struct rte_port_source *port;
251 /* Check input arguments*/
252 if ((p == NULL) || (p->mempool == NULL)) {
253 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
257 /* Memory allocation */
258 port = rte_zmalloc_socket("PORT", sizeof(*port),
259 RTE_CACHE_LINE_SIZE, socket_id);
261 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
266 port->mempool = (struct rte_mempool *) p->mempool;
270 /* pcap file load and initialization */
271 int status = pcap_source_load(p, port, socket_id);
274 if (port->pkt_buff != NULL) {
275 RTE_LOG(INFO, PORT, "Successfully load pcap file "
276 "'%s' with %u pkts\n",
277 p->file_name, port->n_pkts);
279 } else if (status != -ENOTSUP) {
280 /* ENOTSUP is not treated as error */
283 RTE_LOG(ERR, PORT, "%s: Failed to open pcap file "
284 "'%s' for reading\n",
285 __func__, p->file_name);
288 RTE_LOG(ERR, PORT, "%s: Not enough memory\n",
292 RTE_LOG(ERR, PORT, "%s: Failed to enable PCAP "
293 "support for unknown reason\n",
308 rte_port_source_free(void *port)
310 struct rte_port_source *p =
311 (struct rte_port_source *)port;
313 /* Check input parameters */
320 rte_free(p->pkt_len);
324 rte_free(p->pkt_buff);
333 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
335 struct rte_port_source *p = (struct rte_port_source *) port;
338 if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
341 for (i = 0; i < n_pkts; i++) {
342 rte_mbuf_refcnt_set(pkts[i], 1);
343 rte_pktmbuf_reset(pkts[i]);
348 if (p->pkt_buff != NULL) {
349 for (i = 0; i < n_pkts; i++) {
350 uint8_t *pkt_data = rte_pktmbuf_mtod(pkts[i],
353 rte_memcpy(pkt_data, p->pkts[p->pkt_index],
354 p->pkt_len[p->pkt_index]);
355 pkts[i]->data_len = p->pkt_len[p->pkt_index];
356 pkts[i]->pkt_len = pkts[i]->data_len;
359 if (p->pkt_index >= p->n_pkts)
366 RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
372 rte_port_source_stats_read(void *port,
373 struct rte_port_in_stats *stats, int clear)
375 struct rte_port_source *p =
376 (struct rte_port_source *) port;
379 memcpy(stats, &p->stats, sizeof(p->stats));
382 memset(&p->stats, 0, sizeof(p->stats));
390 #ifdef RTE_PORT_STATS_COLLECT
392 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val) \
393 (port->stats.n_pkts_in += val)
394 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val) \
395 (port->stats.n_pkts_drop += val)
399 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val)
400 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val)
404 struct rte_port_sink {
405 struct rte_port_out_stats stats;
407 /* PCAP dumper handle and pkts number */
411 uint32_t dump_finish;
417 * Open PCAP file for dumping packets to the file later
420 * Handle to sink port
422 * Sink port parameter
425 * error code otherwise
428 pcap_sink_open(struct rte_port_sink *port,
429 __rte_unused struct rte_port_sink_params *p)
432 pcap_dumper_t *pcap_dumper;
434 if (p->file_name == NULL) {
438 port->dump_finish = 0;
442 /** Open a dead pcap handler for opening dumper file */
443 tx_pcap = pcap_open_dead(DLT_EN10MB, 65535);
447 /* The dumper is created using the previous pcap_t reference */
448 pcap_dumper = pcap_dump_open(tx_pcap, p->file_name);
449 if (pcap_dumper == NULL)
452 port->dumper = pcap_dumper;
453 port->max_pkts = p->max_n_pkts;
455 port->dump_finish = 0;
460 uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN];
463 * Dump a packet to PCAP dumper
466 * Handle to sink port
468 * Handle to mbuf structure holding the packet
471 pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf)
473 uint8_t *pcap_dumper = (uint8_t *)(port->dumper);
474 struct pcap_pkthdr pcap_hdr;
477 /* Maximum num packets already reached */
478 if (port->dump_finish)
481 pkt = rte_pktmbuf_mtod(mbuf, uint8_t *);
483 pcap_hdr.len = mbuf->pkt_len;
484 pcap_hdr.caplen = pcap_hdr.len;
485 gettimeofday(&(pcap_hdr.ts), NULL);
487 if (mbuf->nb_segs > 1) {
488 struct rte_mbuf *jumbo_mbuf;
489 uint32_t pkt_index = 0;
491 /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN,
494 if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN)
497 for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL;
498 jumbo_mbuf = jumbo_mbuf->next) {
499 rte_memcpy(&jumbo_pkt_buf[pkt_index],
500 rte_pktmbuf_mtod(jumbo_mbuf, uint8_t *),
501 jumbo_mbuf->data_len);
502 pkt_index += jumbo_mbuf->data_len;
505 jumbo_pkt_buf[pkt_index] = '\0';
510 pcap_dump(pcap_dumper, &pcap_hdr, pkt);
514 if ((port->max_pkts != 0) && (port->pkt_index >= port->max_pkts)) {
515 port->dump_finish = 1;
516 RTE_LOG(INFO, PORT, "Dumped %u packets to file\n",
526 * Handle to pcap dumper
530 pcap_sink_flush_pkt(void *dumper)
532 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
534 pcap_dump_flush(pcap_dumper);
538 * Close a PCAP dumper handle
541 * Handle to pcap dumper
544 pcap_sink_close(void *dumper)
546 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
548 pcap_dump_close(pcap_dumper);
554 pcap_sink_open(struct rte_port_sink *port,
555 __rte_unused struct rte_port_sink_params *p)
560 port->dump_finish = 0;
566 pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
567 __rte_unused struct rte_mbuf *mbuf) {}
570 pcap_sink_flush_pkt(__rte_unused void *dumper) {}
573 pcap_sink_close(__rte_unused void *dumper) {}
578 rte_port_sink_create(__rte_unused void *params, int socket_id)
580 struct rte_port_sink *port;
581 struct rte_port_sink_params *p = params;
584 /* Memory allocation */
585 port = rte_zmalloc_socket("PORT", sizeof(*port),
586 RTE_CACHE_LINE_SIZE, socket_id);
588 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
592 /* Try to open PCAP file for dumping, if possible */
593 status = pcap_sink_open(port, p);
595 if (port->dumper != NULL)
596 RTE_LOG(INFO, PORT, "Ready to dump packets "
597 "to file %s\n", p->file_name);
599 } else if (status != -ENOTSUP) {
600 if (status == -ENOENT)
601 RTE_LOG(ERR, PORT, "%s: Failed to open pcap file "
602 "%s for writing\n", __func__,
605 RTE_LOG(ERR, PORT, "%s: Failed to enable pcap "
606 "support for unknown reason\n", __func__);
616 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
618 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
620 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
621 if (p->dumper != NULL)
622 pcap_sink_dump_pkt(p, pkt);
623 rte_pktmbuf_free(pkt);
624 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
630 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
633 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
635 if ((pkts_mask & (pkts_mask + 1)) == 0) {
636 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
639 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
640 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
643 for (i = 0; i < n_pkts; i++) {
644 struct rte_mbuf *pkt = pkts[i];
646 pcap_sink_dump_pkt(p, pkt);
650 for (i = 0; i < n_pkts; i++) {
651 struct rte_mbuf *pkt = pkts[i];
653 rte_pktmbuf_free(pkt);
658 uint64_t dump_pkts_mask = pkts_mask;
661 for ( ; dump_pkts_mask; ) {
662 pkt_index = __builtin_ctzll(
664 pcap_sink_dump_pkt(p, pkts[pkt_index]);
665 dump_pkts_mask &= ~(1LLU << pkt_index);
669 for ( ; pkts_mask; ) {
670 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
671 uint64_t pkt_mask = 1LLU << pkt_index;
672 struct rte_mbuf *pkt = pkts[pkt_index];
674 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
675 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
676 rte_pktmbuf_free(pkt);
677 pkts_mask &= ~pkt_mask;
685 rte_port_sink_flush(void *port)
687 struct rte_port_sink *p = (struct rte_port_sink *)port;
689 if (p->dumper != NULL)
690 pcap_sink_flush_pkt(p->dumper);
696 rte_port_sink_free(void *port)
698 struct rte_port_sink *p =
699 (struct rte_port_sink *)port;
700 /* Check input parameters */
704 if (p->dumper != NULL)
705 pcap_sink_close(p->dumper);
713 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
716 struct rte_port_sink *p =
717 (struct rte_port_sink *) port;
720 memcpy(stats, &p->stats, sizeof(p->stats));
723 memset(&p->stats, 0, sizeof(p->stats));
729 * Summary of port operations
731 struct rte_port_in_ops rte_port_source_ops = {
732 .f_create = rte_port_source_create,
733 .f_free = rte_port_source_free,
734 .f_rx = rte_port_source_rx,
735 .f_stats = rte_port_source_stats_read,
738 struct rte_port_out_ops rte_port_sink_ops = {
739 .f_create = rte_port_sink_create,
740 .f_free = rte_port_sink_free,
741 .f_tx = rte_port_sink_tx,
742 .f_tx_bulk = rte_port_sink_tx_bulk,
743 .f_flush = rte_port_sink_flush,
744 .f_stats = rte_port_sink_stats_read,