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>
50 #include "rte_port_source_sink.h"
55 #ifdef RTE_PORT_STATS_COLLECT
57 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val) \
58 port->stats.n_pkts_in += val
59 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val) \
60 port->stats.n_pkts_drop += val
64 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val)
65 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val)
69 struct rte_port_source {
70 struct rte_port_in_stats stats;
72 struct rte_mempool *mempool;
75 /* PCAP buffers and indexes */
89 * Load PCAP file, allocate and copy packets in the file to memory
92 * Parameters for source port
94 * Handle to source port
96 * Socket id where the memory is created
99 * error code otherwise
102 pcap_source_load(struct rte_port_source_params *p,
103 struct rte_port_source *port,
109 uint32_t *pkt_len_aligns = NULL;
110 size_t total_buff_len = 0;
112 char pcap_errbuf[PCAP_ERRBUF_SIZE];
114 struct pcap_pkthdr pcap_hdr;
116 uint8_t *buff = NULL;
117 uint32_t pktmbuf_maxlen = (uint32_t)
118 (rte_pktmbuf_data_room_size(port->mempool) -
119 RTE_PKTMBUF_HEADROOM);
121 if (p->file_name == NULL)
124 if (p->n_bytes_per_pkt == 0)
125 max_len = pktmbuf_maxlen;
127 max_len = RTE_MIN(p->n_bytes_per_pkt, pktmbuf_maxlen);
129 /* first time open, get packet number */
130 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
131 if (pcap_handle == NULL) {
136 while ((pkt = pcap_next(pcap_handle, &pcap_hdr)) != NULL)
139 pcap_close(pcap_handle);
141 port->pkt_len = rte_zmalloc_socket("PCAP",
142 (sizeof(*port->pkt_len) * n_pkts), 0, socket_id);
143 if (port->pkt_len == NULL) {
148 pkt_len_aligns = rte_malloc("PCAP",
149 (sizeof(*pkt_len_aligns) * n_pkts), 0);
150 if (pkt_len_aligns == NULL) {
155 port->pkts = rte_zmalloc_socket("PCAP",
156 (sizeof(*port->pkts) * n_pkts), 0, socket_id);
157 if (port->pkts == NULL) {
162 /* open 2nd time, get pkt_len */
163 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
164 if (pcap_handle == NULL) {
169 for (i = 0; i < n_pkts; i++) {
170 pkt = pcap_next(pcap_handle, &pcap_hdr);
171 port->pkt_len[i] = RTE_MIN(max_len, pcap_hdr.len);
172 pkt_len_aligns[i] = RTE_CACHE_LINE_ROUNDUP(
174 total_buff_len += pkt_len_aligns[i];
177 pcap_close(pcap_handle);
179 /* allocate a big trunk of data for pcap file load */
180 buff = rte_zmalloc_socket("PCAP",
181 total_buff_len, 0, socket_id);
187 port->pkt_buff = buff;
189 /* open file one last time to copy the pkt content */
190 pcap_handle = pcap_open_offline(p->file_name, pcap_errbuf);
191 if (pcap_handle == NULL) {
196 for (i = 0; i < n_pkts; i++) {
197 pkt = pcap_next(pcap_handle, &pcap_hdr);
198 rte_memcpy(buff, pkt, port->pkt_len[i]);
199 port->pkts[i] = buff;
200 buff += pkt_len_aligns[i];
203 pcap_close(pcap_handle);
205 port->n_pkts = n_pkts;
207 rte_free(pkt_len_aligns);
213 rte_free(pkt_len_aligns);
215 rte_free(port->pkt_len);
217 rte_free(port->pkts);
219 rte_free(port->pkt_buff);
226 pcap_source_load(__rte_unused struct rte_port_source_params *p,
227 struct rte_port_source *port,
228 __rte_unused int socket_id)
230 port->pkt_buff = NULL;
231 port->pkt_len = NULL;
237 #endif /* RTE_PORT_PCAP */
242 rte_port_source_create(void *params, int socket_id)
244 struct rte_port_source_params *p =
245 (struct rte_port_source_params *) params;
246 struct rte_port_source *port;
248 /* Check input arguments*/
249 if ((p == NULL) || (p->mempool == NULL)) {
250 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
254 /* Memory allocation */
255 port = rte_zmalloc_socket("PORT", sizeof(*port),
256 RTE_CACHE_LINE_SIZE, socket_id);
258 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
263 port->mempool = (struct rte_mempool *) p->mempool;
267 /* pcap file load and initialization */
268 int status = pcap_source_load(p, port, socket_id);
271 if (port->pkt_buff != NULL) {
272 RTE_LOG(INFO, PORT, "Successfully load pcap file "
273 "'%s' with %u pkts\n",
274 p->file_name, port->n_pkts);
276 } else if (status != -ENOTSUP) {
277 /* ENOTSUP is not treated as error */
280 RTE_LOG(ERR, PORT, "%s: Failed to open pcap file "
281 "'%s' for reading\n",
282 __func__, p->file_name);
285 RTE_LOG(ERR, PORT, "%s: Not enough memory\n",
289 RTE_LOG(ERR, PORT, "%s: Failed to enable PCAP "
290 "support for unknown reason\n",
305 rte_port_source_free(void *port)
307 struct rte_port_source *p =
308 (struct rte_port_source *)port;
310 /* Check input parameters */
317 rte_free(p->pkt_len);
321 rte_free(p->pkt_buff);
330 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
332 struct rte_port_source *p = (struct rte_port_source *) port;
335 if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
338 for (i = 0; i < n_pkts; i++) {
339 rte_mbuf_refcnt_set(pkts[i], 1);
340 rte_pktmbuf_reset(pkts[i]);
345 if (p->pkt_buff != NULL) {
346 for (i = 0; i < n_pkts; i++) {
347 uint8_t *pkt_data = rte_pktmbuf_mtod(pkts[i],
350 rte_memcpy(pkt_data, p->pkts[p->pkt_index],
351 p->pkt_len[p->pkt_index]);
352 pkts[i]->data_len = p->pkt_len[p->pkt_index];
353 pkts[i]->pkt_len = pkts[i]->data_len;
356 if (p->pkt_index >= p->n_pkts)
363 RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
369 rte_port_source_stats_read(void *port,
370 struct rte_port_in_stats *stats, int clear)
372 struct rte_port_source *p =
373 (struct rte_port_source *) port;
376 memcpy(stats, &p->stats, sizeof(p->stats));
379 memset(&p->stats, 0, sizeof(p->stats));
387 #ifdef RTE_PORT_STATS_COLLECT
389 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val) \
390 (port->stats.n_pkts_in += val)
391 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val) \
392 (port->stats.n_pkts_drop += val)
396 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val)
397 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val)
401 struct rte_port_sink {
402 struct rte_port_out_stats stats;
406 rte_port_sink_create(__rte_unused void *params, int socket_id)
408 struct rte_port_sink *port;
410 /* Memory allocation */
411 port = rte_zmalloc_socket("PORT", sizeof(*port),
412 RTE_CACHE_LINE_SIZE, socket_id);
414 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
422 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
424 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
426 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
427 rte_pktmbuf_free(pkt);
428 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
434 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
437 __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
439 if ((pkts_mask & (pkts_mask + 1)) == 0) {
440 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
443 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
444 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
445 for (i = 0; i < n_pkts; i++) {
446 struct rte_mbuf *pkt = pkts[i];
448 rte_pktmbuf_free(pkt);
451 for ( ; pkts_mask; ) {
452 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
453 uint64_t pkt_mask = 1LLU << pkt_index;
454 struct rte_mbuf *pkt = pkts[pkt_index];
456 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
457 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
458 rte_pktmbuf_free(pkt);
459 pkts_mask &= ~pkt_mask;
467 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
470 struct rte_port_sink *p =
471 (struct rte_port_sink *) port;
474 memcpy(stats, &p->stats, sizeof(p->stats));
477 memset(&p->stats, 0, sizeof(p->stats));
483 * Summary of port operations
485 struct rte_port_in_ops rte_port_source_ops = {
486 .f_create = rte_port_source_create,
487 .f_free = rte_port_source_free,
488 .f_rx = rte_port_source_rx,
489 .f_stats = rte_port_source_stats_read,
492 struct rte_port_out_ops rte_port_sink_ops = {
493 .f_create = rte_port_sink_create,
495 .f_tx = rte_port_sink_tx,
496 .f_tx_bulk = rte_port_sink_tx_bulk,
498 .f_stats = rte_port_sink_stats_read,