Imported Upstream version 16.04
[deb_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 static struct rte_tailq_elem rte_reorder_tailq = {
49         .name = "RTE_REORDER",
50 };
51 EAL_REGISTER_TAILQ(rte_reorder_tailq)
52
53 #define NO_FLAGS 0
54 #define RTE_REORDER_PREFIX "RO_"
55 #define RTE_REORDER_NAMESIZE 32
56
57 /* Macros for printing using RTE_LOG */
58 #define RTE_LOGTYPE_REORDER     RTE_LOGTYPE_USER1
59
60 /* A generic circular buffer */
61 struct cir_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;
68
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         int is_initialized;
77 } __rte_cache_aligned;
78
79 static void
80 rte_reorder_free_mbufs(struct rte_reorder_buffer *b);
81
82 struct rte_reorder_buffer *
83 rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
84                 const char *name, unsigned int size)
85 {
86         const unsigned int min_bufsize = sizeof(*b) +
87                                         (2 * size * sizeof(struct rte_mbuf *));
88
89         if (b == NULL) {
90                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer parameter:"
91                                         " NULL\n");
92                 rte_errno = EINVAL;
93                 return NULL;
94         }
95         if (!rte_is_power_of_2(size)) {
96                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer size"
97                                 " - Not a power of 2\n");
98                 rte_errno = EINVAL;
99                 return NULL;
100         }
101         if (name == NULL) {
102                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
103                                         " NULL\n");
104                 rte_errno = EINVAL;
105                 return NULL;
106         }
107         if (bufsize < min_bufsize) {
108                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer memory size: %u, "
109                         "minimum required: %u\n", bufsize, min_bufsize);
110                 rte_errno = EINVAL;
111                 return NULL;
112         }
113
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]));
122
123         return b;
124 }
125
126 struct rte_reorder_buffer*
127 rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
128 {
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 *));
134
135         reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
136
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");
141                 rte_errno = EINVAL;
142                 return NULL;
143         }
144         if (name == NULL) {
145                 RTE_LOG(ERR, REORDER, "Invalid reorder buffer name ptr:"
146                                         " NULL\n");
147                 rte_errno = EINVAL;
148                 return NULL;
149         }
150
151         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
152
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)
157                         break;
158         }
159         if (te != NULL)
160                 goto exit;
161
162         /* allocate tailq entry */
163         te = rte_zmalloc("REORDER_TAILQ_ENTRY", sizeof(*te), 0);
164         if (te == NULL) {
165                 RTE_LOG(ERR, REORDER, "Failed to allocate tailq entry\n");
166                 rte_errno = ENOMEM;
167                 b = NULL;
168                 goto exit;
169         }
170
171         /* Allocate memory to store the reorder buffer structure. */
172         b = rte_zmalloc_socket("REORDER_BUFFER", bufsize, 0, socket_id);
173         if (b == NULL) {
174                 RTE_LOG(ERR, REORDER, "Memzone allocation failed\n");
175                 rte_errno = ENOMEM;
176                 rte_free(te);
177         } else {
178                 rte_reorder_init(b, bufsize, name, size);
179                 te->data = (void *)b;
180                 TAILQ_INSERT_TAIL(reorder_list, te, next);
181         }
182
183 exit:
184         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
185         return b;
186 }
187
188 void
189 rte_reorder_reset(struct rte_reorder_buffer *b)
190 {
191         char name[RTE_REORDER_NAMESIZE];
192
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);
197 }
198
199 static void
200 rte_reorder_free_mbufs(struct rte_reorder_buffer *b)
201 {
202         unsigned i;
203
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]);
210         }
211 }
212
213 void
214 rte_reorder_free(struct rte_reorder_buffer *b)
215 {
216         struct rte_reorder_list *reorder_list;
217         struct rte_tailq_entry *te;
218
219         /* Check user arguments. */
220         if (b == NULL)
221                 return;
222
223         reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
224
225         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
226
227         /* find our tailq entry */
228         TAILQ_FOREACH(te, reorder_list, next) {
229                 if (te->data == (void *) b)
230                         break;
231         }
232         if (te == NULL) {
233                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
234                 return;
235         }
236
237         TAILQ_REMOVE(reorder_list, te, next);
238
239         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
240
241         rte_reorder_free_mbufs(b);
242
243         rte_free(b);
244         rte_free(te);
245 }
246
247 struct rte_reorder_buffer *
248 rte_reorder_find_existing(const char *name)
249 {
250         struct rte_reorder_buffer *b = NULL;
251         struct rte_tailq_entry *te;
252         struct rte_reorder_list *reorder_list;
253
254         reorder_list = RTE_TAILQ_CAST(rte_reorder_tailq.head, rte_reorder_list);
255
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)
260                         break;
261         }
262         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
263
264         if (te == NULL) {
265                 rte_errno = ENOENT;
266                 return NULL;
267         }
268
269         return b;
270 }
271
272 static unsigned
273 rte_reorder_fill_overflow(struct rte_reorder_buffer *b, unsigned n)
274 {
275         /*
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
281          */
282
283         struct cir_buffer *order_buf = &b->order_buf,
284                         *ready_buf = &b->ready_buf;
285
286         unsigned int order_head_adv = 0;
287
288         /*
289          * move at least n packets to ready buffer, assuming ready buffer
290          * has room for those packets.
291          */
292         while (order_head_adv < n &&
293                         ((ready_buf->head + 1) & ready_buf->mask) != ready_buf->tail) {
294
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;
298                         order_head_adv++;
299                 }
300
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];
305
306                         order_buf->entries[order_buf->head] = NULL;
307                         order_head_adv++;
308
309                         order_buf->head = (order_buf->head + 1) & order_buf->mask;
310
311                         if (((ready_buf->head + 1) & ready_buf->mask) == ready_buf->tail)
312                                 break;
313
314                         ready_buf->head = (ready_buf->head + 1) & ready_buf->mask;
315                 }
316         }
317
318         b->min_seqn += order_head_adv;
319         /* Return the number of positions the order_buf head has moved */
320         return order_head_adv;
321 }
322
323 int
324 rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
325 {
326         uint32_t offset, position;
327         struct cir_buffer *order_buf = &b->order_buf;
328
329         if (!b->is_initialized) {
330                 b->min_seqn = mbuf->seqn;
331                 b->is_initialized = 1;
332         }
333
334         /*
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):
338          *      min_seqn  = 0xFFFD
339          *      mbuf_seqn = 0x0010
340          *      offset    = 0x0010 - 0xFFFD = 0x13
341          */
342         offset = mbuf->seqn - b->min_seqn;
343
344         /*
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.
361          */
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 */
369                         rte_errno = ENOSPC;
370                         return -1;
371                 }
372                 offset = mbuf->seqn - b->min_seqn;
373                 position = (order_buf->head + offset) & order_buf->mask;
374                 order_buf->entries[position] = mbuf;
375         } else {
376                 /* Put in handling for enqueue straight to output */
377                 rte_errno = ERANGE;
378                 return -1;
379         }
380         return 0;
381 }
382
383 unsigned int
384 rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs,
385                 unsigned max_mbufs)
386 {
387         unsigned int drain_cnt = 0;
388
389         struct cir_buffer *order_buf = &b->order_buf,
390                         *ready_buf = &b->ready_buf;
391
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;
396         }
397
398         /*
399          * If requested number of buffers not fetched from ready buffer, fetch
400          * remaining buffers from order buffer
401          */
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;
406                 b->min_seqn++;
407                 order_buf->head = (order_buf->head + 1) & order_buf->mask;
408         }
409
410         return drain_cnt;
411 }