tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_reorder / rte_reorder.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
34 #include <inttypes.h>
35 #include <string.h>
36
37 #include <rte_log.h>
38 #include <rte_mbuf.h>
39 #include <rte_memzone.h>
40 #include <rte_eal_memconfig.h>
41 #include <rte_errno.h>
42 #include <rte_malloc.h>
43
44 #include "rte_reorder.h"
45
46 TAILQ_HEAD(rte_reorder_list, rte_tailq_entry);
47
48 #define NO_FLAGS 0
49 #define RTE_REORDER_PREFIX "RO_"
50 #define RTE_REORDER_NAMESIZE 32
51
52 /* Macros for printing using RTE_LOG */
53 #define RTE_LOGTYPE_REORDER     RTE_LOGTYPE_USER1
54
55 /* A generic circular buffer */
56 struct cir_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;
63
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;
72
73 static void
74 rte_reorder_free_mbufs(struct rte_reorder_buffer *b);
75
76 struct rte_reorder_buffer *
77 rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
78                 const char *name, unsigned int size)
79 {
80         const unsigned int min_bufsize = sizeof(*b) +
81                                         (2 * size * sizeof(struct rte_mbuf *));
82
83         if (b == NULL) {
84                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer parameter:"
85                                         " NULL\n");
86                 rte_errno = EINVAL;
87                 return NULL;
88         }
89         if (!rte_is_power_of_2(size)) {
90                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
91                                 " - Not a power of 2\n");
92                 rte_errno = EINVAL;
93                 return NULL;
94         }
95         if (name == NULL) {
96                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
97                                         " NULL\n");
98                 rte_errno = EINVAL;
99                 return NULL;
100         }
101         if (bufsize < min_bufsize) {
102                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer memory size: %u, "
103                         "minimum required: %u\n", bufsize, min_bufsize);
104                 rte_errno = EINVAL;
105                 return NULL;
106         }
107
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]));
116
117         return b;
118 }
119
120 struct rte_reorder_buffer*
121 rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
122 {
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 *));
128
129         /* check that we have an initialised tail queue */
130         reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
131         if (!reorder_list) {
132                 rte_errno = E_RTE_NO_TAILQ;
133                 return NULL;
134         }
135
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");
140                 rte_errno = EINVAL;
141                 return NULL;
142         }
143         if (name == NULL) {
144                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
145                                         " NULL\n");
146                 rte_errno = EINVAL;
147                 return NULL;
148         }
149
150         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
151
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)
156                         break;
157         }
158         if (te != NULL)
159                 goto exit;
160
161         /* allocate tailq entry */
162         te = rte_zmalloc("REORDER_TAILQ_ENTRY", sizeof(*te), 0);
163         if (te == NULL) {
164                 RTE_LOG(ERR, REORDER, "Failed to allocate tailq entry\n");
165                 rte_errno = ENOMEM;
166                 b = NULL;
167                 goto exit;
168         }
169
170         /* Allocate memory to store the reorder buffer structure. */
171         b = rte_zmalloc_socket("REORDER_BUFFER", bufsize, 0, socket_id);
172         if (b == NULL) {
173                 RTE_LOG(ERR, REORDER, "Memzone allocation failed\n");
174                 rte_errno = ENOMEM;
175                 rte_free(te);
176         } else {
177                 rte_reorder_init(b, bufsize, name, size);
178                 te->data = (void *)b;
179                 TAILQ_INSERT_TAIL(reorder_list, te, next);
180         }
181
182 exit:
183         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
184         return b;
185 }
186
187 void
188 rte_reorder_reset(struct rte_reorder_buffer *b)
189 {
190         char name[RTE_REORDER_NAMESIZE];
191
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);
196 }
197
198 static void
199 rte_reorder_free_mbufs(struct rte_reorder_buffer *b)
200 {
201         unsigned i;
202
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]);
209         }
210 }
211
212 void
213 rte_reorder_free(struct rte_reorder_buffer *b)
214 {
215         struct rte_reorder_list *reorder_list;
216         struct rte_tailq_entry *te;
217
218         /* Check user arguments. */
219         if (b == NULL)
220                 return;
221
222         /* check that we have an initialised tail queue */
223         reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
224         if (!reorder_list) {
225                 rte_errno = E_RTE_NO_TAILQ;
226                 return;
227         }
228
229         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
230
231         /* find our tailq entry */
232         TAILQ_FOREACH(te, reorder_list, next) {
233                 if (te->data == (void *) b)
234                         break;
235         }
236         if (te == NULL) {
237                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
238                 return;
239         }
240
241         TAILQ_REMOVE(reorder_list, te, next);
242
243         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
244
245         rte_reorder_free_mbufs(b);
246
247         rte_free(b);
248         rte_free(te);
249 }
250
251 struct rte_reorder_buffer *
252 rte_reorder_find_existing(const char *name)
253 {
254         struct rte_reorder_buffer *b = NULL;
255         struct rte_tailq_entry *te;
256         struct rte_reorder_list *reorder_list;
257
258         /* check that we have an initialised tail queue */
259         reorder_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_REORDER, rte_reorder_list);
260         if (!reorder_list) {
261                 rte_errno = E_RTE_NO_TAILQ;
262                 return NULL;
263         }
264
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)
269                         break;
270         }
271         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
272
273         if (te == NULL) {
274                 rte_errno = ENOENT;
275                 return NULL;
276         }
277
278         return b;
279 }
280
281 static unsigned
282 rte_reorder_fill_overflow(struct rte_reorder_buffer *b, unsigned n)
283 {
284         /*
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
290          */
291
292         struct cir_buffer *order_buf = &b->order_buf,
293                         *ready_buf = &b->ready_buf;
294
295         unsigned int order_head_adv = 0;
296
297         /*
298          * move at least n packets to ready buffer, assuming ready buffer
299          * has room for those packets.
300          */
301         while (order_head_adv < n &&
302                         ((ready_buf->head + 1) & ready_buf->mask) != ready_buf->tail) {
303
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;
307                         order_head_adv++;
308                 }
309
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];
314
315                         order_buf->entries[order_buf->head] = NULL;
316                         order_head_adv++;
317
318                         order_buf->head = (order_buf->head + 1) & order_buf->mask;
319
320                         if (((ready_buf->head + 1) & ready_buf->mask) == ready_buf->tail)
321                                 break;
322
323                         ready_buf->head = (ready_buf->head + 1) & ready_buf->mask;
324                 }
325         }
326
327         b->min_seqn += order_head_adv;
328         /* Return the number of positions the order_buf head has moved */
329         return order_head_adv;
330 }
331
332 int
333 rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
334 {
335         uint32_t offset, position;
336         struct cir_buffer *order_buf = &b->order_buf;
337
338         /*
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):
342          *      min_seqn  = 0xFFFD
343          *      mbuf_seqn = 0x0010
344          *      offset    = 0x0010 - 0xFFFD = 0x13
345          */
346         offset = mbuf->seqn - b->min_seqn;
347
348         /*
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.
365          */
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 */
373                         rte_errno = ENOSPC;
374                         return -1;
375                 }
376                 offset = mbuf->seqn - b->min_seqn;
377                 position = (order_buf->head + offset) & order_buf->mask;
378                 order_buf->entries[position] = mbuf;
379         } else {
380                 /* Put in handling for enqueue straight to output */
381                 rte_errno = ERANGE;
382                 return -1;
383         }
384         return 0;
385 }
386
387 unsigned int
388 rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
389                 unsigned max_mbufs)
390 {
391         unsigned int drain_cnt = 0;
392
393         struct cir_buffer *order_buf = &b->order_buf,
394                         *ready_buf = &b->ready_buf;
395
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;
400         }
401
402         /*
403          * If requested number of buffers not fetched from ready buffer, fetch
404          * remaining buffers from order buffer
405          */
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;
410                 b->min_seqn++;
411                 order_buf->head = (order_buf->head + 1) & order_buf->mask;
412         }
413
414         return drain_cnt;
415 }