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 */
77 } __rte_cache_aligned;
80 rte_reorder_free_mbufs(struct rte_reorder_buffer *b);
82 struct rte_reorder_buffer *
83 rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
84 const char *name, unsigned int size)
86 const unsigned int min_bufsize = sizeof(*b) +
87 (2 * size * sizeof(struct rte_mbuf *));
90 RTE_LOG(ERR, REORDER, "Invalid reorder buffer parameter:"
95 if (!rte_is_power_of_2(size)) {
96 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
97 " - Not a power of 2\n");
102 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
107 if (bufsize < min_bufsize) {
108 RTE_LOG(ERR, REORDER, "Invalid reorder buffer memory size: %u, "
109 "minimum required: %u\n", bufsize, min_bufsize);
114 memset(b, 0, bufsize);
115 snprintf(b->name, sizeof(b->name), "%s", name);
116 b->memsize = bufsize;
117 b->order_buf.size = b->ready_buf.size = size;
118 b->order_buf.mask = b->ready_buf.mask = size - 1;
119 b->ready_buf.entries = (void *)&b[1];
120 b->order_buf.entries = RTE_PTR_ADD(&b[1],
121 size * sizeof(b->ready_buf.entries[0]));
126 struct rte_reorder_buffer*
127 rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
129 struct rte_reorder_buffer *b = NULL;
130 struct rte_tailq_entry *te;
131 struct rte_reorder_list *reorder_list;
132 const unsigned int bufsize = sizeof(struct rte_reorder_buffer) +
133 (2 * size * sizeof(struct rte_mbuf *));
135 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
137 /* Check user arguments. */
138 if (!rte_is_power_of_2(size)) {
139 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
140 " - Not a power of 2\n");
145 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
151 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
153 /* guarantee there's no existing */
154 TAILQ_FOREACH(te, reorder_list, next) {
155 b = (struct rte_reorder_buffer *) te->data;
156 if (strncmp(name, b->name, RTE_REORDER_NAMESIZE) == 0)
162 /* allocate tailq entry */
163 te = rte_zmalloc("REORDER_TAILQ_ENTRY", sizeof(*te), 0);
165 RTE_LOG(ERR, REORDER, "Failed to allocate tailq entry\n");
171 /* Allocate memory to store the reorder buffer structure. */
172 b = rte_zmalloc_socket("REORDER_BUFFER", bufsize, 0, socket_id);
174 RTE_LOG(ERR, REORDER, "Memzone allocation failed\n");
178 rte_reorder_init(b, bufsize, name, size);
179 te->data = (void *)b;
180 TAILQ_INSERT_TAIL(reorder_list, te, next);
184 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
189 rte_reorder_reset(struct rte_reorder_buffer *b)
191 char name[RTE_REORDER_NAMESIZE];
193 rte_reorder_free_mbufs(b);
194 snprintf(name, sizeof(name), "%s", b->name);
195 /* No error checking as current values should be valid */
196 rte_reorder_init(b, b->memsize, name, b->order_buf.size);
200 rte_reorder_free_mbufs(struct rte_reorder_buffer *b)
204 /* Free up the mbufs of order buffer & ready buffer */
205 for (i = 0; i < b->order_buf.size; i++) {
206 if (b->order_buf.entries[i])
207 rte_pktmbuf_free(b->order_buf.entries[i]);
208 if (b->ready_buf.entries[i])
209 rte_pktmbuf_free(b->ready_buf.entries[i]);
214 rte_reorder_free(struct rte_reorder_buffer *b)
216 struct rte_reorder_list *reorder_list;
217 struct rte_tailq_entry *te;
219 /* Check user arguments. */
223 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
225 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
227 /* find our tailq entry */
228 TAILQ_FOREACH(te, reorder_list, next) {
229 if (te->data == (void *) b)
233 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
237 TAILQ_REMOVE(reorder_list, te, next);
239 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
241 rte_reorder_free_mbufs(b);
247 struct rte_reorder_buffer *
248 rte_reorder_find_existing(const char *name)
250 struct rte_reorder_buffer *b = NULL;
251 struct rte_tailq_entry *te;
252 struct rte_reorder_list *reorder_list;
254 reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
256 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
257 TAILQ_FOREACH(te, reorder_list, next) {
258 b = (struct rte_reorder_buffer *) te->data;
259 if (strncmp(name, b->name, RTE_REORDER_NAMESIZE) == 0)
262 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
273 rte_reorder_fill_overflow(struct rte_reorder_buffer *b, unsigned n)
276 * 1. Move all ready entries that fit to the ready_buf
277 * 2. check if we meet the minimum needed (n).
278 * 3. If not, then skip any gaps and keep moving.
279 * 4. If at any point the ready buffer is full, stop
280 * 5. Return the number of positions the order_buf head has moved
283 struct cir_buffer *order_buf = &b->order_buf,
284 *ready_buf = &b->ready_buf;
286 unsigned int order_head_adv = 0;
289 * move at least n packets to ready buffer, assuming ready buffer
290 * has room for those packets.
292 while (order_head_adv < n &&
293 ((ready_buf->head + 1) & ready_buf->mask) != ready_buf->tail) {
295 /* if we are blocked waiting on a packet, skip it */
296 if (order_buf->entries[order_buf->head] == NULL) {
297 order_buf->head = (order_buf->head + 1) & order_buf->mask;
301 /* Move all ready entries that fit to the ready_buf */
302 while (order_buf->entries[order_buf->head] != NULL) {
303 ready_buf->entries[ready_buf->head] =
304 order_buf->entries[order_buf->head];
306 order_buf->entries[order_buf->head] = NULL;
309 order_buf->head = (order_buf->head + 1) & order_buf->mask;
311 if (((ready_buf->head + 1) & ready_buf->mask) == ready_buf->tail)
314 ready_buf->head = (ready_buf->head + 1) & ready_buf->mask;
318 b->min_seqn += order_head_adv;
319 /* Return the number of positions the order_buf head has moved */
320 return order_head_adv;
324 rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
326 uint32_t offset, position;
327 struct cir_buffer *order_buf = &b->order_buf;
329 if (!b->is_initialized) {
330 b->min_seqn = mbuf->seqn;
331 b->is_initialized = 1;
335 * calculate the offset from the head pointer we need to go.
336 * The subtraction takes care of the sequence number wrapping.
337 * For example (using 16-bit for brevity):
340 * offset = 0x0010 - 0xFFFD = 0x13
342 offset = mbuf->seqn - b->min_seqn;
345 * action to take depends on offset.
346 * offset < buffer->size: the mbuf fits within the current window of
347 * sequence numbers we can reorder. EXPECTED CASE.
348 * offset > buffer->size: the mbuf is outside the current window. There
349 * are a number of cases to consider:
350 * 1. The packet sequence is just outside the window, then we need
351 * to see about shifting the head pointer and taking any ready
352 * to return packets out of the ring. If there was a delayed
353 * or dropped packet preventing drains from shifting the window
354 * this case will skip over the dropped packet instead, and any
355 * packets dequeued here will be returned on the next drain call.
356 * 2. The packet sequence number is vastly outside our window, taken
357 * here as having offset greater than twice the buffer size. In
358 * this case, the packet is probably an old or late packet that
359 * was previously skipped, so just enqueue the packet for
360 * immediate return on the next drain call, or else return error.
362 if (offset < b->order_buf.size) {
363 position = (order_buf->head + offset) & order_buf->mask;
364 order_buf->entries[position] = mbuf;
365 } else if (offset < 2 * b->order_buf.size) {
366 if (rte_reorder_fill_overflow(b, offset + 1 - order_buf->size)
367 < (offset + 1 - order_buf->size)) {
368 /* Put in handling for enqueue straight to output */
372 offset = mbuf->seqn - b->min_seqn;
373 position = (order_buf->head + offset) & order_buf->mask;
374 order_buf->entries[position] = mbuf;
376 /* Put in handling for enqueue straight to output */
384 rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
387 unsigned int drain_cnt = 0;
389 struct cir_buffer *order_buf = &b->order_buf,
390 *ready_buf = &b->ready_buf;
392 /* Try to fetch requested number of mbufs from ready buffer */
393 while ((drain_cnt < max_mbufs) && (ready_buf->tail != ready_buf->head)) {
394 mbufs[drain_cnt++] = ready_buf->entries[ready_buf->tail];
395 ready_buf->tail = (ready_buf->tail + 1) & ready_buf->mask;
399 * If requested number of buffers not fetched from ready buffer, fetch
400 * remaining buffers from order buffer
402 while ((drain_cnt < max_mbufs) &&
403 (order_buf->entries[order_buf->head] != NULL)) {
404 mbufs[drain_cnt++] = order_buf->entries[order_buf->head];
405 order_buf->entries[order_buf->head] = NULL;
407 order_buf->head = (order_buf->head + 1) & order_buf->mask;