port: fix sink port statistics
[dpdk.git] / lib / librte_port / rte_port_source_sink.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 #include <stdint.h>
34 #include <string.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_mempool.h>
38 #include <rte_malloc.h>
39
40 #include "rte_port_source_sink.h"
41
42 /*
43  * Port SOURCE
44  */
45 #ifdef RTE_PORT_STATS_COLLECT
46
47 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val) \
48         port->stats.n_pkts_in += val
49 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val) \
50         port->stats.n_pkts_drop += val
51
52 #else
53
54 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val)
55 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val)
56
57 #endif
58
59 struct rte_port_source {
60         struct rte_port_in_stats stats;
61
62         struct rte_mempool *mempool;
63 };
64
65 static void *
66 rte_port_source_create(void *params, int socket_id)
67 {
68         struct rte_port_source_params *p =
69                         (struct rte_port_source_params *) params;
70         struct rte_port_source *port;
71
72         /* Check input arguments*/
73         if ((p == NULL) || (p->mempool == NULL)) {
74                 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
75                 return NULL;
76         }
77
78         /* Memory allocation */
79         port = rte_zmalloc_socket("PORT", sizeof(*port),
80                         RTE_CACHE_LINE_SIZE, socket_id);
81         if (port == NULL) {
82                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
83                 return NULL;
84         }
85
86         /* Initialization */
87         port->mempool = (struct rte_mempool *) p->mempool;
88
89         return port;
90 }
91
92 static int
93 rte_port_source_free(void *port)
94 {
95         /* Check input parameters */
96         if (port == NULL)
97                 return 0;
98
99         rte_free(port);
100
101         return 0;
102 }
103
104 static int
105 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
106 {
107         struct rte_port_source *p = (struct rte_port_source *) port;
108         uint32_t i;
109
110         if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
111                 return 0;
112
113         for (i = 0; i < n_pkts; i++) {
114                 rte_mbuf_refcnt_set(pkts[i], 1);
115                 rte_pktmbuf_reset(pkts[i]);
116         }
117
118         RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
119
120         return n_pkts;
121 }
122
123 static int
124 rte_port_source_stats_read(void *port,
125                 struct rte_port_in_stats *stats, int clear)
126 {
127         struct rte_port_source *p =
128                 (struct rte_port_source *) port;
129
130         if (stats != NULL)
131                 memcpy(stats, &p->stats, sizeof(p->stats));
132
133         if (clear)
134                 memset(&p->stats, 0, sizeof(p->stats));
135
136         return 0;
137 }
138
139 /*
140  * Port SINK
141  */
142 #ifdef RTE_PORT_STATS_COLLECT
143
144 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val) \
145         (port->stats.n_pkts_in += val)
146 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val) \
147         (port->stats.n_pkts_drop += val)
148
149 #else
150
151 #define RTE_PORT_SINK_STATS_PKTS_IN_ADD(port, val)
152 #define RTE_PORT_SINK_STATS_PKTS_DROP_ADD(port, val)
153
154 #endif
155
156 struct rte_port_sink {
157         struct rte_port_out_stats stats;
158 };
159
160 static void *
161 rte_port_sink_create(__rte_unused void *params, int socket_id)
162 {
163         struct rte_port_sink *port;
164
165         /* Memory allocation */
166         port = rte_zmalloc_socket("PORT", sizeof(*port),
167                         RTE_CACHE_LINE_SIZE, socket_id);
168         if (port == NULL) {
169                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
170                 return NULL;
171         }
172
173         return port;
174 }
175
176 static int
177 rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
178 {
179         __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
180
181         RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
182         rte_pktmbuf_free(pkt);
183         RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
184
185         return 0;
186 }
187
188 static int
189 rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
190         uint64_t pkts_mask)
191 {
192         __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
193
194         if ((pkts_mask & (pkts_mask + 1)) == 0) {
195                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
196                 uint32_t i;
197
198                 RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
199                 RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
200                 for (i = 0; i < n_pkts; i++) {
201                         struct rte_mbuf *pkt = pkts[i];
202
203                         rte_pktmbuf_free(pkt);
204                 }
205         } else {
206                 for ( ; pkts_mask; ) {
207                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
208                         uint64_t pkt_mask = 1LLU << pkt_index;
209                         struct rte_mbuf *pkt = pkts[pkt_index];
210
211                         RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
212                         RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
213                         rte_pktmbuf_free(pkt);
214                         pkts_mask &= ~pkt_mask;
215                 }
216         }
217
218         return 0;
219 }
220
221 static int
222 rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
223                 int clear)
224 {
225         struct rte_port_sink *p =
226                 (struct rte_port_sink *) port;
227
228         if (stats != NULL)
229                 memcpy(stats, &p->stats, sizeof(p->stats));
230
231         if (clear)
232                 memset(&p->stats, 0, sizeof(p->stats));
233
234         return 0;
235 }
236
237 /*
238  * Summary of port operations
239  */
240 struct rte_port_in_ops rte_port_source_ops = {
241         .f_create = rte_port_source_create,
242         .f_free = rte_port_source_free,
243         .f_rx = rte_port_source_rx,
244         .f_stats = rte_port_source_stats_read,
245 };
246
247 struct rte_port_out_ops rte_port_sink_ops = {
248         .f_create = rte_port_sink_create,
249         .f_free = NULL,
250         .f_tx = rte_port_sink_tx,
251         .f_tx_bulk = rte_port_sink_tx_bulk,
252         .f_flush = NULL,
253         .f_stats = rte_port_sink_stats_read,
254 };