4 * Copyright(c) 2010-2014 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.
39 #include <rte_memzone.h>
40 #include <rte_eal_memconfig.h>
41 #include <rte_errno.h>
42 #include <rte_malloc.h>
44 #include "rte_reorder.h"
46 TAILQ_HEAD(rte_reorder_list, rte_tailq_entry);
49 #define RTE_REORDER_PREFIX "RO_"
50 #define RTE_REORDER_NAMESIZE 32
52 /* Macros for printing using RTE_LOG */
53 #define RTE_LOGTYPE_REORDER RTE_LOGTYPE_USER1
55 /* A generic circular buffer */
57 unsigned int size; /**< Number of entries that can be stored */
58 unsigned int mask; /**< [buffer_size - 1]: used for wrap-around */
59 unsigned int head; /**< insertion point in buffer */
60 unsigned int tail; /**< extraction point in buffer */
61 struct rte_mbuf **entries;
62 } __rte_cache_aligned;
64 /* The reorder buffer data structure itself */
65 struct rte_reorder_buffer {
66 char name[RTE_REORDER_NAMESIZE];
67 uint32_t min_seqn; /**< Lowest seq. number that can be in the buffer */
68 unsigned int memsize; /**< memory area size of reorder buffer */
69 struct cir_buffer ready_buf; /**< temp buffer for dequeued entries */
70 struct cir_buffer order_buf; /**< buffer used to reorder entries */
71 } __rte_cache_aligned;
74 rte_reorder_free_mbufs(struct rte_reorder_buffer *b);
76 struct rte_reorder_buffer *
77 rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
78 const char *name, unsigned int size)
80 const unsigned int min_bufsize = sizeof(*b) +
81 (2 * size * sizeof(struct rte_mbuf *));
84 RTE_LOG(ERR, REORDER, "Invalid reorder buffer parameter:"
89 if (!rte_is_power_of_2(size)) {
90 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
91 " - Not a power of 2\n");
96 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
101 if (bufsize < min_bufsize) {
102 RTE_LOG(ERR, REORDER, "Invalid reorder buffer memory size: %u, "
103 "minimum required: %u\n", bufsize, min_bufsize);
108 memset(b, 0, bufsize);
109 snprintf(b->name, sizeof(b->name), "%s", name);
110 b->memsize = bufsize;
111 b->order_buf.size = b->ready_buf.size = size;
112 b->order_buf.mask = b->ready_buf.mask = size - 1;
113 b->ready_buf.entries = (void *)&b[1];
114 b->order_buf.entries = RTE_PTR_ADD(&b[1],
115 size * sizeof(b->ready_buf.entries[0]));
120 struct rte_reorder_buffer*
121 rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
123 struct rte_reorder_buffer *b = NULL;
124 struct rte_tailq_entry *te;
125 struct rte_reorder_list *reorder_list;
126 const unsigned int bufsize = sizeof(struct rte_reorder_buffer) +
127 (2 * size * sizeof(struct rte_mbuf *));
129 /* check that we have an initialised tail queue */
130 reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
132 rte_errno = E_RTE_NO_TAILQ;
136 /* Check user arguments. */
137 if (!rte_is_power_of_2(size)) {
138 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
139 " - Not a power of 2\n");
144 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
150 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
152 /* guarantee there's no existing */
153 TAILQ_FOREACH(te, reorder_list, next) {
154 b = (struct rte_reorder_buffer *) te->data;
155 if (strncmp(name, b->name, RTE_REORDER_NAMESIZE) == 0)
161 /* allocate tailq entry */
162 te = rte_zmalloc("REORDER_TAILQ_ENTRY", sizeof(*te), 0);
164 RTE_LOG(ERR, REORDER, "Failed to allocate tailq entry\n");
170 /* Allocate memory to store the reorder buffer structure. */
171 b = rte_zmalloc_socket("REORDER_BUFFER", bufsize, 0, socket_id);
173 RTE_LOG(ERR, REORDER, "Memzone allocation failed\n");
177 rte_reorder_init(b, bufsize, name, size);
178 te->data = (void *)b;
179 TAILQ_INSERT_TAIL(reorder_list, te, next);
183 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
188 rte_reorder_reset(struct rte_reorder_buffer *b)
190 char name[RTE_REORDER_NAMESIZE];
192 rte_reorder_free_mbufs(b);
193 snprintf(name, sizeof(name), "%s", b->name);
194 /* No error checking as current values should be valid */
195 rte_reorder_init(b, b->memsize, name, b->order_buf.size);
199 rte_reorder_free_mbufs(struct rte_reorder_buffer *b)
203 /* Free up the mbufs of order buffer & ready buffer */
204 for (i = 0; i < b->order_buf.size; i++) {
205 if (b->order_buf.entries[i])
206 rte_pktmbuf_free(b->order_buf.entries[i]);
207 if (b->ready_buf.entries[i])
208 rte_pktmbuf_free(b->ready_buf.entries[i]);
213 rte_reorder_free(struct rte_reorder_buffer *b)
215 struct rte_reorder_list *reorder_list;
216 struct rte_tailq_entry *te;
218 /* Check user arguments. */
222 /* check that we have an initialised tail queue */
223 reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
225 rte_errno = E_RTE_NO_TAILQ;
229 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
231 /* find our tailq entry */
232 TAILQ_FOREACH(te, reorder_list, next) {
233 if (te->data == (void *) b)
237 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
241 TAILQ_REMOVE(reorder_list, te, next);
243 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
245 rte_reorder_free_mbufs(b);
251 struct rte_reorder_buffer *
252 rte_reorder_find_existing(const char *name)
254 struct rte_reorder_buffer *b = NULL;
255 struct rte_tailq_entry *te;
256 struct rte_reorder_list *reorder_list;
258 /* check that we have an initialised tail queue */
259 reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
261 rte_errno = E_RTE_NO_TAILQ;
265 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
266 TAILQ_FOREACH(te, reorder_list, next) {
267 b = (struct rte_reorder_buffer *) te->data;
268 if (strncmp(name, b->name, RTE_REORDER_NAMESIZE) == 0)
271 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
282 rte_reorder_fill_overflow(struct rte_reorder_buffer *b, unsigned n)
285 * 1. Move all ready entries that fit to the ready_buf
286 * 2. check if we meet the minimum needed (n).
287 * 3. If not, then skip any gaps and keep moving.
288 * 4. If at any point the ready buffer is full, stop
289 * 5. Return the number of positions the order_buf head has moved
292 struct cir_buffer *order_buf = &b->order_buf,
293 *ready_buf = &b->ready_buf;
295 unsigned int order_head_adv = 0;
298 * move at least n packets to ready buffer, assuming ready buffer
299 * has room for those packets.
301 while (order_head_adv < n &&
302 ((ready_buf->head + 1) & ready_buf->mask) != ready_buf->tail) {
304 /* if we are blocked waiting on a packet, skip it */
305 if (order_buf->entries[order_buf->head] == NULL) {
306 order_buf->head = (order_buf->head + 1) & order_buf->mask;
310 /* Move all ready entries that fit to the ready_buf */
311 while (order_buf->entries[order_buf->head] != NULL) {
312 ready_buf->entries[ready_buf->head] =
313 order_buf->entries[order_buf->head];
315 order_buf->entries[order_buf->head] = NULL;
318 order_buf->head = (order_buf->head + 1) & order_buf->mask;
320 if (((ready_buf->head + 1) & ready_buf->mask) == ready_buf->tail)
323 ready_buf->head = (ready_buf->head + 1) & ready_buf->mask;
327 b->min_seqn += order_head_adv;
328 /* Return the number of positions the order_buf head has moved */
329 return order_head_adv;
333 rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
335 uint32_t offset, position;
336 struct cir_buffer *order_buf = &b->order_buf;
339 * calculate the offset from the head pointer we need to go.
340 * The subtraction takes care of the sequence number wrapping.
341 * For example (using 16-bit for brevity):
344 * offset = 0x0010 - 0xFFFD = 0x13
346 offset = mbuf->seqn - b->min_seqn;
349 * action to take depends on offset.
350 * offset < buffer->size: the mbuf fits within the current window of
351 * sequence numbers we can reorder. EXPECTED CASE.
352 * offset > buffer->size: the mbuf is outside the current window. There
353 * are a number of cases to consider:
354 * 1. The packet sequence is just outside the window, then we need
355 * to see about shifting the head pointer and taking any ready
356 * to return packets out of the ring. If there was a delayed
357 * or dropped packet preventing drains from shifting the window
358 * this case will skip over the dropped packet instead, and any
359 * packets dequeued here will be returned on the next drain call.
360 * 2. The packet sequence number is vastly outside our window, taken
361 * here as having offset greater than twice the buffer size. In
362 * this case, the packet is probably an old or late packet that
363 * was previously skipped, so just enqueue the packet for
364 * immediate return on the next drain call, or else return error.
366 if (offset < b->order_buf.size) {
367 position = (order_buf->head + offset) & order_buf->mask;
368 order_buf->entries[position] = mbuf;
369 } else if (offset < 2 * b->order_buf.size) {
370 if (rte_reorder_fill_overflow(b, offset + 1 - order_buf->size)
371 < (offset + 1 - order_buf->size)) {
372 /* Put in handling for enqueue straight to output */
376 offset = mbuf->seqn - b->min_seqn;
377 position = (order_buf->head + offset) & order_buf->mask;
378 order_buf->entries[position] = mbuf;
380 /* Put in handling for enqueue straight to output */
388 rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
391 unsigned int drain_cnt = 0;
393 struct cir_buffer *order_buf = &b->order_buf,
394 *ready_buf = &b->ready_buf;
396 /* Try to fetch requested number of mbufs from ready buffer */
397 while ((drain_cnt < max_mbufs) && (ready_buf->tail != ready_buf->head)) {
398 mbufs[drain_cnt++] = ready_buf->entries[ready_buf->tail];
399 ready_buf->tail = (ready_buf->tail + 1) & ready_buf->mask;
403 * If requested number of buffers not fetched from ready buffer, fetch
404 * remaining buffers from order buffer
406 while ((drain_cnt < max_mbufs) &&
407 (order_buf->entries[order_buf->head] != NULL)) {
408 mbufs[drain_cnt++] = order_buf->entries[order_buf->head];
409 order_buf->entries[order_buf->head] = NULL;
411 order_buf->head = (order_buf->head + 1) & order_buf->mask;