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 pcap_sink_open(struct rte_port_sink *port,
390 const char *file_name,
394 pcap_dumper_t *pcap_dumper;
396 /** Open a dead pcap handler for opening dumper file */
397 tx_pcap = pcap_open_dead(DLT_EN10MB, 65535);
398 if (tx_pcap == NULL) {
399 RTE_LOG(ERR, PORT, "Cannot open pcap dead handler\n");
403 /* The dumper is created using the previous pcap_t reference */
404 pcap_dumper = pcap_dump_open(tx_pcap, file_name);
405 if (pcap_dumper == NULL) {
406 RTE_LOG(ERR, PORT, "Failed to open pcap file "
407 "\"%s\" for writing\n", file_name);
411 port->dumper = pcap_dumper;
412 port->max_pkts = max_n_pkts;
414 port->dump_finish = 0;
416 RTE_LOG(INFO, PORT, "Ready to dump packets to file \"%s\"\n",
422 uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN];
425 * Dump a packet to PCAP dumper
428 * Handle to sink port
430 * Handle to mbuf structure holding the packet
433 pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf)
435 uint8_t *pcap_dumper = (uint8_t *)(port->dumper);
436 struct pcap_pkthdr pcap_hdr;
439 /* Maximum num packets already reached */
440 if (port->dump_finish)
443 pkt = rte_pktmbuf_mtod(mbuf, uint8_t *);
445 pcap_hdr.len = mbuf->pkt_len;
446 pcap_hdr.caplen = pcap_hdr.len;
447 gettimeofday(&(pcap_hdr.ts), NULL);
449 if (mbuf->nb_segs > 1) {
450 struct rte_mbuf *jumbo_mbuf;
451 uint32_t pkt_index = 0;
453 /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN,
456 if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN)
459 for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL;
460 jumbo_mbuf = jumbo_mbuf->next) {
461 rte_memcpy(&jumbo_pkt_buf[pkt_index],
462 rte_pktmbuf_mtod(jumbo_mbuf, uint8_t *),
463 jumbo_mbuf->data_len);
464 pkt_index += jumbo_mbuf->data_len;
467 jumbo_pkt_buf[pkt_index] = '\0';
472 pcap_dump(pcap_dumper, &pcap_hdr, pkt);
476 if ((port->max_pkts != 0) && (port->pkt_index >= port->max_pkts)) {
477 port->dump_finish = 1;
478 RTE_LOG(INFO, PORT, "Dumped %u packets to file\n",
488 * Handle to pcap dumper
492 pcap_sink_flush_pkt(void *dumper)
494 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
496 pcap_dump_flush(pcap_dumper);
500 * Close a PCAP dumper handle
503 * Handle to pcap dumper
506 pcap_sink_close(void *dumper)
508 pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
510 pcap_dump_close(pcap_dumper);
513 #define PCAP_SINK_OPEN(port, file_name, max_n_pkts) \
514 pcap_sink_open(port, file_name, max_n_pkts)
518 #define PCAP_SINK_OPEN(port, file_name, max_n_pkts) \
523 RTE_LOG(ERR, PORT, "Sink port field " \
524 "\"file_name\" is not NULL.\n"); \
532 pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
533 __rte_unused struct rte_mbuf *mbuf) {}
536 pcap_sink_flush_pkt(__rte_unused void *dumper) {}
539 pcap_sink_close(__rte_unused void *dumper) {}
544 rte_port_sink_create(void *params, int socket_id)
546 struct rte_port_sink *port;
547 struct rte_port_sink_params *p = params;
549 /* Memory allocation */
550 port = rte_zmalloc_socket("PORT", sizeof(*port),
551 RTE_CACHE_LINE_SIZE, socket_id);
553 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
561 int status = PCAP_SINK_OPEN(port, p->file_name,
574 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
576 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
578 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
579 if (p->dumper != NULL)
580 pcap_sink_dump_pkt(p, pkt);
581 rte_pktmbuf_free(pkt);
582 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
588 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
591 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
593 if ((pkts_mask & (pkts_mask + 1)) == 0) {
594 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
597 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
598 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
601 for (i = 0; i < n_pkts; i++) {
602 struct rte_mbuf *pkt = pkts[i];
604 pcap_sink_dump_pkt(p, pkt);
608 for (i = 0; i < n_pkts; i++) {
609 struct rte_mbuf *pkt = pkts[i];
611 rte_pktmbuf_free(pkt);
616 uint64_t dump_pkts_mask = pkts_mask;
619 for ( ; dump_pkts_mask; ) {
620 pkt_index = __builtin_ctzll(
622 pcap_sink_dump_pkt(p, pkts[pkt_index]);
623 dump_pkts_mask &= ~(1LLU << pkt_index);
627 for ( ; pkts_mask; ) {
628 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
629 uint64_t pkt_mask = 1LLU << pkt_index;
630 struct rte_mbuf *pkt = pkts[pkt_index];
632 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
633 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
634 rte_pktmbuf_free(pkt);
635 pkts_mask &= ~pkt_mask;
643 rte_port_sink_flush(void *port)
645 struct rte_port_sink *p = (struct rte_port_sink *)port;
647 if (p->dumper != NULL)
648 pcap_sink_flush_pkt(p->dumper);
654 rte_port_sink_free(void *port)
656 struct rte_port_sink *p =
657 (struct rte_port_sink *)port;
658 /* Check input parameters */
662 if (p->dumper != NULL)
663 pcap_sink_close(p->dumper);
671 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
674 struct rte_port_sink *p =
675 (struct rte_port_sink *) port;
678 memcpy(stats, &p->stats, sizeof(p->stats));
681 memset(&p->stats, 0, sizeof(p->stats));
687 * Summary of port operations
689 struct rte_port_in_ops rte_port_source_ops = {
690 .f_create = rte_port_source_create,
691 .f_free = rte_port_source_free,
692 .f_rx = rte_port_source_rx,
693 .f_stats = rte_port_source_stats_read,
696 struct rte_port_out_ops rte_port_sink_ops = {
697 .f_create = rte_port_sink_create,
698 .f_free = rte_port_sink_free,
699 .f_tx = rte_port_sink_tx,
700 .f_tx_bulk = rte_port_sink_tx_bulk,
701 .f_flush = rte_port_sink_flush,
702 .f_stats = rte_port_sink_stats_read,