]> git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/lib/librte_port/rte_port_sched.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dpdk / lib / librte_port / rte_port_sched.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 <string.h>
34
35 #include <rte_mbuf.h>
36 #include <rte_malloc.h>
37
38 #include "rte_port_sched.h"
39
40 /*
41 * Reader
42 */
43 #ifdef RTE_PORT_STATS_COLLECT
44
45 #define RTE_PORT_SCHED_READER_PKTS_IN_ADD(port, val) \
46 port->stats.n_pkts_in += val
47 #define RTE_PORT_SCHED_READER_PKTS_DROP_ADD(port, val) \
48 port->stats.n_pkts_drop += val
49
50 #else
51
52 #define RTE_PORT_SCHED_READER_PKTS_IN_ADD(port, val)
53 #define RTE_PORT_SCHED_READER_PKTS_DROP_ADD(port, val)
54
55 #endif
56
57 struct rte_port_sched_reader {
58 struct rte_port_in_stats stats;
59
60 struct rte_sched_port *sched;
61 };
62
63 static void *
64 rte_port_sched_reader_create(void *params, int socket_id)
65 {
66 struct rte_port_sched_reader_params *conf =
67 (struct rte_port_sched_reader_params *) params;
68 struct rte_port_sched_reader *port;
69
70 /* Check input parameters */
71 if ((conf == NULL) ||
72 (conf->sched == NULL)) {
73 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
74 return NULL;
75 }
76
77 /* Memory allocation */
78 port = rte_zmalloc_socket("PORT", sizeof(*port),
79 RTE_CACHE_LINE_SIZE, socket_id);
80 if (port == NULL) {
81 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
82 return NULL;
83 }
84
85 /* Initialization */
86 port->sched = conf->sched;
87
88 return port;
89 }
90
91 static int
92 rte_port_sched_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
93 {
94 struct rte_port_sched_reader *p = (struct rte_port_sched_reader *) port;
95 uint32_t nb_rx;
96
97 nb_rx = rte_sched_port_dequeue(p->sched, pkts, n_pkts);
98 RTE_PORT_SCHED_READER_PKTS_IN_ADD(p, nb_rx);
99
100 return nb_rx;
101 }
102
103 static int
104 rte_port_sched_reader_free(void *port)
105 {
106 if (port == NULL) {
107 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
108 return -EINVAL;
109 }
110
111 rte_free(port);
112
113 return 0;
114 }
115
116 static int
117 rte_port_sched_reader_stats_read(void *port,
118 struct rte_port_in_stats *stats, int clear)
119 {
120 struct rte_port_sched_reader *p =
121 (struct rte_port_sched_reader *) port;
122
123 if (stats != NULL)
124 memcpy(stats, &p->stats, sizeof(p->stats));
125
126 if (clear)
127 memset(&p->stats, 0, sizeof(p->stats));
128
129 return 0;
130 }
131
132 /*
133 * Writer
134 */
135 #ifdef RTE_PORT_STATS_COLLECT
136
137 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(port, val) \
138 port->stats.n_pkts_in += val
139 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(port, val) \
140 port->stats.n_pkts_drop += val
141
142 #else
143
144 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(port, val)
145 #define RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(port, val)
146
147 #endif
148
149 struct rte_port_sched_writer {
150 struct rte_port_out_stats stats;
151
152 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
153 struct rte_sched_port *sched;
154 uint32_t tx_burst_sz;
155 uint32_t tx_buf_count;
156 uint64_t bsz_mask;
157 };
158
159 static void *
160 rte_port_sched_writer_create(void *params, int socket_id)
161 {
162 struct rte_port_sched_writer_params *conf =
163 (struct rte_port_sched_writer_params *) params;
164 struct rte_port_sched_writer *port;
165
166 /* Check input parameters */
167 if ((conf == NULL) ||
168 (conf->sched == NULL) ||
169 (conf->tx_burst_sz == 0) ||
170 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
171 (!rte_is_power_of_2(conf->tx_burst_sz))) {
172 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
173 return NULL;
174 }
175
176 /* Memory allocation */
177 port = rte_zmalloc_socket("PORT", sizeof(*port),
178 RTE_CACHE_LINE_SIZE, socket_id);
179 if (port == NULL) {
180 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
181 return NULL;
182 }
183
184 /* Initialization */
185 port->sched = conf->sched;
186 port->tx_burst_sz = conf->tx_burst_sz;
187 port->tx_buf_count = 0;
188 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
189
190 return port;
191 }
192
193 static int
194 rte_port_sched_writer_tx(void *port, struct rte_mbuf *pkt)
195 {
196 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
197
198 p->tx_buf[p->tx_buf_count++] = pkt;
199 RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(p, 1);
200 if (p->tx_buf_count >= p->tx_burst_sz) {
201 __rte_unused uint32_t nb_tx;
202
203 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
204 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
205 p->tx_buf_count = 0;
206 }
207
208 return 0;
209 }
210
211 static int
212 rte_port_sched_writer_tx_bulk(void *port,
213 struct rte_mbuf **pkts,
214 uint64_t pkts_mask)
215 {
216 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
217 uint64_t bsz_mask = p->bsz_mask;
218 uint32_t tx_buf_count = p->tx_buf_count;
219 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
220 ((pkts_mask & bsz_mask) ^ bsz_mask);
221
222 if (expr == 0) {
223 __rte_unused uint32_t nb_tx;
224 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
225
226 if (tx_buf_count) {
227 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf,
228 tx_buf_count);
229 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, tx_buf_count - nb_tx);
230 p->tx_buf_count = 0;
231 }
232
233 nb_tx = rte_sched_port_enqueue(p->sched, pkts, n_pkts);
234 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, n_pkts - nb_tx);
235 } else {
236 for ( ; pkts_mask; ) {
237 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
238 uint64_t pkt_mask = 1LLU << pkt_index;
239 struct rte_mbuf *pkt = pkts[pkt_index];
240
241 p->tx_buf[tx_buf_count++] = pkt;
242 RTE_PORT_SCHED_WRITER_STATS_PKTS_IN_ADD(p, 1);
243 pkts_mask &= ~pkt_mask;
244 }
245 p->tx_buf_count = tx_buf_count;
246
247 if (tx_buf_count >= p->tx_burst_sz) {
248 __rte_unused uint32_t nb_tx;
249
250 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf,
251 tx_buf_count);
252 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, tx_buf_count - nb_tx);
253 p->tx_buf_count = 0;
254 }
255 }
256
257 return 0;
258 }
259
260 static int
261 rte_port_sched_writer_flush(void *port)
262 {
263 struct rte_port_sched_writer *p = (struct rte_port_sched_writer *) port;
264
265 if (p->tx_buf_count) {
266 __rte_unused uint32_t nb_tx;
267
268 nb_tx = rte_sched_port_enqueue(p->sched, p->tx_buf, p->tx_buf_count);
269 RTE_PORT_SCHED_WRITER_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
270 p->tx_buf_count = 0;
271 }
272
273 return 0;
274 }
275
276 static int
277 rte_port_sched_writer_free(void *port)
278 {
279 if (port == NULL) {
280 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
281 return -EINVAL;
282 }
283
284 rte_port_sched_writer_flush(port);
285 rte_free(port);
286
287 return 0;
288 }
289
290 static int
291 rte_port_sched_writer_stats_read(void *port,
292 struct rte_port_out_stats *stats, int clear)
293 {
294 struct rte_port_sched_writer *p =
295 (struct rte_port_sched_writer *) port;
296
297 if (stats != NULL)
298 memcpy(stats, &p->stats, sizeof(p->stats));
299
300 if (clear)
301 memset(&p->stats, 0, sizeof(p->stats));
302
303 return 0;
304 }
305
306 /*
307 * Summary of port operations
308 */
309 struct rte_port_in_ops rte_port_sched_reader_ops = {
310 .f_create = rte_port_sched_reader_create,
311 .f_free = rte_port_sched_reader_free,
312 .f_rx = rte_port_sched_reader_rx,
313 .f_stats = rte_port_sched_reader_stats_read,
314 };
315
316 struct rte_port_out_ops rte_port_sched_writer_ops = {
317 .f_create = rte_port_sched_writer_create,
318 .f_free = rte_port_sched_writer_free,
319 .f_tx = rte_port_sched_writer_tx,
320 .f_tx_bulk = rte_port_sched_writer_tx_bulk,
321 .f_flush = rte_port_sched_writer_flush,
322 .f_stats = rte_port_sched_writer_stats_read,
323 };