X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Flibrte_port%2Frte_port_source_sink.c;h=74b7385a2869bbeb8452baab0351677b9a36d49a;hb=a951e147ea95b26815c72eb710e0b2b8e5d85e40;hp=5c24ceacf5771a375b8d6914a20141f186b659d6;hpb=e1535fb7b8bf693fd9b703b1ead418bba8fd8fe1;p=dpdk.git diff --git a/lib/librte_port/rte_port_source_sink.c b/lib/librte_port/rte_port_source_sink.c index 5c24ceacf5..74b7385a28 100644 --- a/lib/librte_port/rte_port_source_sink.c +++ b/lib/librte_port/rte_port_source_sink.c @@ -1,34 +1,5 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2010-2016 Intel Corporation. All rights reserved. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2016 Intel Corporation */ #include #include @@ -36,20 +7,13 @@ #include #include #include - #include -#ifdef RTE_NEXT_ABI - #ifdef RTE_PORT_PCAP #include #include #endif -#else -#undef RTE_PORT_PCAP -#endif - #include "rte_port_source_sink.h" /* @@ -82,8 +46,6 @@ struct rte_port_source { uint32_t pkt_index; }; -#ifdef RTE_NEXT_ABI - #ifdef RTE_PORT_PCAP static int @@ -233,13 +195,11 @@ error_exit: #endif /* RTE_PORT_PCAP */ -#endif /* RTE_NEXT_ABI */ - static void * rte_port_source_create(void *params, int socket_id) { struct rte_port_source_params *p = - (struct rte_port_source_params *) params; + params; struct rte_port_source *port; /* Check input arguments*/ @@ -259,8 +219,6 @@ rte_port_source_create(void *params, int socket_id) /* Initialization */ port->mempool = (struct rte_mempool *) p->mempool; -#ifdef RTE_NEXT_ABI - if (p->file_name) { int status = PCAP_SOURCE_LOAD(port, p->file_name, p->n_bytes_per_pkt, socket_id); @@ -271,8 +229,6 @@ rte_port_source_create(void *params, int socket_id) } } -#endif - return port; } @@ -280,21 +236,18 @@ static int rte_port_source_free(void *port) { struct rte_port_source *p = - (struct rte_port_source *)port; + port; /* Check input parameters */ if (p == NULL) return 0; -#ifdef RTE_NEXT_ABI - if (p->pkt_len) rte_free(p->pkt_len); if (p->pkts) rte_free(p->pkts); if (p->pkt_buff) rte_free(p->pkt_buff); -#endif rte_free(p); @@ -304,19 +257,12 @@ rte_port_source_free(void *port) static int rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts) { - struct rte_port_source *p = (struct rte_port_source *) port; + struct rte_port_source *p = port; uint32_t i; - if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0) + if (rte_pktmbuf_alloc_bulk(p->mempool, pkts, n_pkts) != 0) return 0; - for (i = 0; i < n_pkts; i++) { - rte_mbuf_refcnt_set(pkts[i], 1); - rte_pktmbuf_reset(pkts[i]); - } - -#ifdef RTE_NEXT_ABI - if (p->pkt_buff != NULL) { for (i = 0; i < n_pkts; i++) { uint8_t *pkt_data = rte_pktmbuf_mtod(pkts[i], @@ -333,8 +279,6 @@ rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts) } } -#endif - RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts); return n_pkts; @@ -345,7 +289,7 @@ rte_port_source_stats_read(void *port, struct rte_port_in_stats *stats, int clear) { struct rte_port_source *p = - (struct rte_port_source *) port; + port; if (stats != NULL) memcpy(stats, &p->stats, sizeof(p->stats)); @@ -385,65 +329,46 @@ struct rte_port_sink { #ifdef RTE_PORT_PCAP -/** - * Open PCAP file for dumping packets to the file later - * - * @param port - * Handle to sink port - * @param p - * Sink port parameter - * @return - * 0 on SUCCESS - * error code otherwise - */ static int pcap_sink_open(struct rte_port_sink *port, - __rte_unused struct rte_port_sink_params *p) + const char *file_name, + uint32_t max_n_pkts) { pcap_t *tx_pcap; pcap_dumper_t *pcap_dumper; - if (p->file_name == NULL) { - port->dumper = NULL; - port->max_pkts = 0; - port->pkt_index = 0; - port->dump_finish = 0; - return 0; - } - /** Open a dead pcap handler for opening dumper file */ tx_pcap = pcap_open_dead(DLT_EN10MB, 65535); - if (tx_pcap == NULL) - return -ENOENT; + if (tx_pcap == NULL) { + RTE_LOG(ERR, PORT, "Cannot open pcap dead handler\n"); + return -1; + } /* The dumper is created using the previous pcap_t reference */ - pcap_dumper = pcap_dump_open(tx_pcap, p->file_name); - if (pcap_dumper == NULL) - return -ENOENT; + pcap_dumper = pcap_dump_open(tx_pcap, file_name); + if (pcap_dumper == NULL) { + RTE_LOG(ERR, PORT, "Failed to open pcap file " + "\"%s\" for writing\n", file_name); + return -1; + } port->dumper = pcap_dumper; - port->max_pkts = p->max_n_pkts; + port->max_pkts = max_n_pkts; port->pkt_index = 0; port->dump_finish = 0; + RTE_LOG(INFO, PORT, "Ready to dump packets to file \"%s\"\n", + file_name); + return 0; } -uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN]; - -/** - * Dump a packet to PCAP dumper - * - * @param p - * Handle to sink port - * @param mbuf - * Handle to mbuf structure holding the packet - */ static void -pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf) +pcap_sink_write_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf) { - uint8_t *pcap_dumper = (uint8_t *)(port->dumper); + uint8_t *pcap_dumper = (port->dumper); struct pcap_pkthdr pcap_hdr; + uint8_t jumbo_pkt_buf[RTE_ETHER_MAX_JUMBO_FRAME_LEN]; uint8_t *pkt; /* Maximum num packets already reached */ @@ -460,10 +385,10 @@ pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf) struct rte_mbuf *jumbo_mbuf; uint32_t pkt_index = 0; - /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN, + /* if packet size longer than RTE_ETHER_MAX_JUMBO_FRAME_LEN, * ignore it. */ - if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN) + if (mbuf->pkt_len > RTE_ETHER_MAX_JUMBO_FRAME_LEN) return; for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL; @@ -491,67 +416,52 @@ pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf) } -/** - * Flush pcap dumper - * - * @param dumper - * Handle to pcap dumper - */ +#define PCAP_SINK_OPEN(port, file_name, max_n_pkts) \ + pcap_sink_open(port, file_name, max_n_pkts) -static void -pcap_sink_flush_pkt(void *dumper) -{ - pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper; +#define PCAP_SINK_WRITE_PKT(port, mbuf) \ + pcap_sink_write_pkt(port, mbuf) - pcap_dump_flush(pcap_dumper); -} +#define PCAP_SINK_FLUSH_PKT(dumper) \ +do { \ + if (dumper) \ + pcap_dump_flush((pcap_dumper_t *)dumper); \ +} while (0) -/** - * Close a PCAP dumper handle - * - * @param dumper - * Handle to pcap dumper - */ -static void -pcap_sink_close(void *dumper) -{ - pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper; - - pcap_dump_close(pcap_dumper); -} +#define PCAP_SINK_CLOSE(dumper) \ +do { \ + if (dumper) \ + pcap_dump_close((pcap_dumper_t *)dumper); \ +} while (0) #else -static int -pcap_sink_open(struct rte_port_sink *port, - __rte_unused struct rte_port_sink_params *p) -{ - port->dumper = NULL; - port->max_pkts = 0; - port->pkt_index = 0; - port->dump_finish = 0; +#define PCAP_SINK_OPEN(port, file_name, max_n_pkts) \ +({ \ + int _ret = 0; \ + \ + if (file_name) { \ + RTE_LOG(ERR, PORT, "Sink port field " \ + "\"file_name\" is not NULL.\n"); \ + _ret = -1; \ + } \ + \ + _ret; \ +}) - return -ENOTSUP; -} +#define PCAP_SINK_WRITE_PKT(port, mbuf) {} -static void -pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port, - __rte_unused struct rte_mbuf *mbuf) {} +#define PCAP_SINK_FLUSH_PKT(dumper) -static void -pcap_sink_flush_pkt(__rte_unused void *dumper) {} - -static void -pcap_sink_close(__rte_unused void *dumper) {} +#define PCAP_SINK_CLOSE(dumper) #endif static void * -rte_port_sink_create(__rte_unused void *params, int socket_id) +rte_port_sink_create(void *params, int socket_id) { struct rte_port_sink *port; struct rte_port_sink_params *p = params; - int status; /* Memory allocation */ port = rte_zmalloc_socket("PORT", sizeof(*port), @@ -561,24 +471,17 @@ rte_port_sink_create(__rte_unused void *params, int socket_id) return NULL; } - /* Try to open PCAP file for dumping, if possible */ - status = pcap_sink_open(port, p); - if (status == 0) { - if (port->dumper != NULL) - RTE_LOG(INFO, PORT, "Ready to dump packets " - "to file %s\n", p->file_name); - - } else if (status != -ENOTSUP) { - if (status == -ENOENT) - RTE_LOG(ERR, PORT, "%s: Failed to open pcap file " - "%s for writing\n", __func__, - p->file_name); - else - RTE_LOG(ERR, PORT, "%s: Failed to enable pcap " - "support for unknown reason\n", __func__); - - rte_free(port); - port = NULL; + if (!p) + return port; + + if (p->file_name) { + int status = PCAP_SINK_OPEN(port, p->file_name, + p->max_n_pkts); + + if (status < 0) { + rte_free(port); + port = NULL; + } } return port; @@ -587,11 +490,11 @@ rte_port_sink_create(__rte_unused void *params, int socket_id) static int rte_port_sink_tx(void *port, struct rte_mbuf *pkt) { - __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port; + struct rte_port_sink *p = port; RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1); if (p->dumper != NULL) - pcap_sink_dump_pkt(p, pkt); + PCAP_SINK_WRITE_PKT(p, pkt); rte_pktmbuf_free(pkt); RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1); @@ -602,7 +505,7 @@ static int rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts, uint64_t pkts_mask) { - __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port; + struct rte_port_sink *p = port; if ((pkts_mask & (pkts_mask + 1)) == 0) { uint64_t n_pkts = __builtin_popcountll(pkts_mask); @@ -612,11 +515,8 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts, RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts); if (p->dumper) { - for (i = 0; i < n_pkts; i++) { - struct rte_mbuf *pkt = pkts[i]; - - pcap_sink_dump_pkt(p, pkt); - } + for (i = 0; i < n_pkts; i++) + PCAP_SINK_WRITE_PKT(p, pkts[i]); } for (i = 0; i < n_pkts; i++) { @@ -633,7 +533,7 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts, for ( ; dump_pkts_mask; ) { pkt_index = __builtin_ctzll( dump_pkts_mask); - pcap_sink_dump_pkt(p, pkts[pkt_index]); + PCAP_SINK_WRITE_PKT(p, pkts[pkt_index]); dump_pkts_mask &= ~(1LLU << pkt_index); } } @@ -656,10 +556,13 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts, static int rte_port_sink_flush(void *port) { - struct rte_port_sink *p = (struct rte_port_sink *)port; + struct rte_port_sink *p = + port; - if (p->dumper != NULL) - pcap_sink_flush_pkt(p->dumper); + if (p == NULL) + return 0; + + PCAP_SINK_FLUSH_PKT(p->dumper); return 0; } @@ -668,13 +571,12 @@ static int rte_port_sink_free(void *port) { struct rte_port_sink *p = - (struct rte_port_sink *)port; - /* Check input parameters */ + port; + if (p == NULL) return 0; - if (p->dumper != NULL) - pcap_sink_close(p->dumper); + PCAP_SINK_CLOSE(p->dumper); rte_free(p); @@ -686,7 +588,7 @@ rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats, int clear) { struct rte_port_sink *p = - (struct rte_port_sink *) port; + port; if (stats != NULL) memcpy(stats, &p->stats, sizeof(p->stats));