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);
48 static struct rte_tailq_elem rte_reorder_tailq = {
49 .name = "RTE_REORDER",
51 EAL_REGISTER_TAILQ(rte_reorder_tailq)
54 #define RTE_REORDER_PREFIX "RO_"
55 #define RTE_REORDER_NAMESIZE 32
57 /* Macros for printing using RTE_LOG */
58 #define RTE_LOGTYPE_REORDER RTE_LOGTYPE_USER1
60 /* A generic circular buffer */
62 unsigned int size; /**< Number of entries that can be stored */
63 unsigned int mask; /**< [buffer_size - 1]: used for wrap-around */
64 unsigned int head; /**< insertion point in buffer */
65 unsigned int tail; /**< extraction point in buffer */
66 struct rte_mbuf **entries;
67 } __rte_cache_aligned;
69 /* The reorder buffer data structure itself */
70 struct rte_reorder_buffer {
71 char name[RTE_REORDER_NAMESIZE];
72 uint32_t min_seqn; /**< Lowest seq. number that can be in the buffer */
73 unsigned int memsize; /**< memory area size of reorder buffer */
74 struct cir_buffer ready_buf; /**< temp buffer for dequeued entries */
75 struct cir_buffer order_buf; /**< buffer used to reorder entries */
76 } __rte_cache_aligned;
79 rte_reorder_free_mbufs(struct rte_reorder_buffer *b);
81 struct rte_reorder_buffer *
82 rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
83 const char *name, unsigned int size)
85 const unsigned int min_bufsize = sizeof(*b) +
86 (2 * size * sizeof(struct rte_mbuf *));
89 RTE_LOG(ERR, REORDER, "Invalid reorder buffer parameter:"
94 if (!rte_is_power_of_2(size)) {
95 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
96 " - Not a power of 2\n");
101 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
106 if (bufsize < min_bufsize) {
107 RTE_LOG(ERR, REORDER, "Invalid reorder buffer memory size: %u, "
108 "minimum required: %u\n", bufsize, min_bufsize);
113 memset(b, 0, bufsize);
114 snprintf(b->name, sizeof(b->name), "%s", name);
115 b->memsize = bufsize;
116 b->order_buf.size = b->ready_buf.size = size;
117 b->order_buf.mask = b->ready_buf.mask = size - 1;
118 b->ready_buf.entries = (void *)&b[1];
119 b->order_buf.entries = RTE_PTR_ADD(&b[1],
120 size * sizeof(b->ready_buf.entries[0]));
125 struct rte_reorder_buffer*
126 rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
128 struct rte_reorder_buffer *b = NULL;
129 struct rte_tailq_entry *te;
130 struct rte_reorder_list *reorder_list;
131 const unsigned int bufsize = sizeof(struct rte_reorder_buffer) +
132 (2 * size * sizeof(struct rte_mbuf *));
134 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
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 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
224 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
226 /* find our tailq entry */
227 TAILQ_FOREACH(te, reorder_list, next) {
228 if (te->data == (void *) b)
232 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
236 TAILQ_REMOVE(reorder_list, te, next);
238 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
240 rte_reorder_free_mbufs(b);
246 struct rte_reorder_buffer *
247 rte_reorder_find_existing(const char *name)
249 struct rte_reorder_buffer *b = NULL;
250 struct rte_tailq_entry *te;
251 struct rte_reorder_list *reorder_list;
253 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
255 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
256 TAILQ_FOREACH(te, reorder_list, next) {
257 b = (struct rte_reorder_buffer *) te->data;
258 if (strncmp(name, b->name, RTE_REORDER_NAMESIZE) == 0)
261 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
272 rte_reorder_fill_overflow(struct rte_reorder_buffer *b, unsigned n)
275 * 1. Move all ready entries that fit to the ready_buf
276 * 2. check if we meet the minimum needed (n).
277 * 3. If not, then skip any gaps and keep moving.
278 * 4. If at any point the ready buffer is full, stop
279 * 5. Return the number of positions the order_buf head has moved
282 struct cir_buffer *order_buf = &b->order_buf,
283 *ready_buf = &b->ready_buf;
285 unsigned int order_head_adv = 0;
288 * move at least n packets to ready buffer, assuming ready buffer
289 * has room for those packets.
291 while (order_head_adv < n &&
292 ((ready_buf->head + 1) & ready_buf->mask) != ready_buf->tail) {
294 /* if we are blocked waiting on a packet, skip it */
295 if (order_buf->entries[order_buf->head] == NULL) {
296 order_buf->head = (order_buf->head + 1) & order_buf->mask;
300 /* Move all ready entries that fit to the ready_buf */
301 while (order_buf->entries[order_buf->head] != NULL) {
302 ready_buf->entries[ready_buf->head] =
303 order_buf->entries[order_buf->head];
305 order_buf->entries[order_buf->head] = NULL;
308 order_buf->head = (order_buf->head + 1) & order_buf->mask;
310 if (((ready_buf->head + 1) & ready_buf->mask) == ready_buf->tail)
313 ready_buf->head = (ready_buf->head + 1) & ready_buf->mask;
317 b->min_seqn += order_head_adv;
318 /* Return the number of positions the order_buf head has moved */
319 return order_head_adv;
323 rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
325 uint32_t offset, position;
326 struct cir_buffer *order_buf = &b->order_buf;
329 * calculate the offset from the head pointer we need to go.
330 * The subtraction takes care of the sequence number wrapping.
331 * For example (using 16-bit for brevity):
334 * offset = 0x0010 - 0xFFFD = 0x13
336 offset = mbuf->seqn - b->min_seqn;
339 * action to take depends on offset.
340 * offset < buffer->size: the mbuf fits within the current window of
341 * sequence numbers we can reorder. EXPECTED CASE.
342 * offset > buffer->size: the mbuf is outside the current window. There
343 * are a number of cases to consider:
344 * 1. The packet sequence is just outside the window, then we need
345 * to see about shifting the head pointer and taking any ready
346 * to return packets out of the ring. If there was a delayed
347 * or dropped packet preventing drains from shifting the window
348 * this case will skip over the dropped packet instead, and any
349 * packets dequeued here will be returned on the next drain call.
350 * 2. The packet sequence number is vastly outside our window, taken
351 * here as having offset greater than twice the buffer size. In
352 * this case, the packet is probably an old or late packet that
353 * was previously skipped, so just enqueue the packet for
354 * immediate return on the next drain call, or else return error.
356 if (offset < b->order_buf.size) {
357 position = (order_buf->head + offset) & order_buf->mask;
358 order_buf->entries[position] = mbuf;
359 } else if (offset < 2 * b->order_buf.size) {
360 if (rte_reorder_fill_overflow(b, offset + 1 - order_buf->size)
361 < (offset + 1 - order_buf->size)) {
362 /* Put in handling for enqueue straight to output */
366 offset = mbuf->seqn - b->min_seqn;
367 position = (order_buf->head + offset) & order_buf->mask;
368 order_buf->entries[position] = mbuf;
370 /* Put in handling for enqueue straight to output */
378 rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
381 unsigned int drain_cnt = 0;
383 struct cir_buffer *order_buf = &b->order_buf,
384 *ready_buf = &b->ready_buf;
386 /* Try to fetch requested number of mbufs from ready buffer */
387 while ((drain_cnt < max_mbufs) && (ready_buf->tail != ready_buf->head)) {
388 mbufs[drain_cnt++] = ready_buf->entries[ready_buf->tail];
389 ready_buf->tail = (ready_buf->tail + 1) & ready_buf->mask;
393 * If requested number of buffers not fetched from ready buffer, fetch
394 * remaining buffers from order buffer
396 while ((drain_cnt < max_mbufs) &&
397 (order_buf->entries[order_buf->head] != NULL)) {
398 mbufs[drain_cnt++] = order_buf->entries[order_buf->head];
399 order_buf->entries[order_buf->head] = NULL;
401 order_buf->head = (order_buf->head + 1) & order_buf->mask;