distributor: new packet distributor library
[dpdk.git] / lib / librte_distributor / rte_distributor.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 <stdio.h>
35 #include <sys/queue.h>
36 #include <string.h>
37 #include <rte_mbuf.h>
38 #include <rte_memzone.h>
39 #include <rte_errno.h>
40 #include <rte_string_fns.h>
41 #include <rte_tailq.h>
42 #include <rte_eal_memconfig.h>
43 #include "rte_distributor.h"
44
45 #define NO_FLAGS 0
46 #define RTE_DISTRIB_PREFIX "DT_"
47
48 /* we will use the bottom four bits of pointer for flags, shifting out
49  * the top four bits to make room (since a 64-bit pointer actually only uses
50  * 48 bits). An arithmetic-right-shift will then appropriately restore the
51  * original pointer value with proper sign extension into the top bits. */
52 #define RTE_DISTRIB_FLAG_BITS 4
53 #define RTE_DISTRIB_FLAGS_MASK (0x0F)
54 #define RTE_DISTRIB_NO_BUF 0       /**< empty flags: no buffer requested */
55 #define RTE_DISTRIB_GET_BUF (1)    /**< worker requests a buffer, returns old */
56 #define RTE_DISTRIB_RETURN_BUF (2) /**< worker returns a buffer, no request */
57
58 #define RTE_DISTRIB_BACKLOG_SIZE 8
59 #define RTE_DISTRIB_BACKLOG_MASK (RTE_DISTRIB_BACKLOG_SIZE - 1)
60
61 #define RTE_DISTRIB_MAX_RETURNS 128
62 #define RTE_DISTRIB_RETURNS_MASK (RTE_DISTRIB_MAX_RETURNS - 1)
63
64 /**
65  * Buffer structure used to pass the pointer data between cores. This is cache
66  * line aligned, but to improve performance and prevent adjacent cache-line
67  * prefetches of buffers for other workers, e.g. when worker 1's buffer is on
68  * the next cache line to worker 0, we pad this out to three cache lines.
69  * Only 64-bits of the memory is actually used though.
70  */
71 union rte_distributor_buffer {
72         volatile int64_t bufptr64;
73         char pad[CACHE_LINE_SIZE*3];
74 } __rte_cache_aligned;
75
76 struct rte_distributor_backlog {
77         unsigned start;
78         unsigned count;
79         int64_t pkts[RTE_DISTRIB_BACKLOG_SIZE];
80 };
81
82 struct rte_distributor_returned_pkts {
83         unsigned start;
84         unsigned count;
85         struct rte_mbuf *mbufs[RTE_DISTRIB_MAX_RETURNS];
86 };
87
88 struct rte_distributor {
89         TAILQ_ENTRY(rte_distributor) next;    /**< Next in list. */
90
91         char name[RTE_DISTRIBUTOR_NAMESIZE];  /**< Name of the ring. */
92         unsigned num_workers;                 /**< Number of workers polling */
93
94         uint32_t in_flight_tags[RTE_MAX_LCORE];
95         struct rte_distributor_backlog backlog[RTE_MAX_LCORE];
96
97         union rte_distributor_buffer bufs[RTE_MAX_LCORE];
98
99         struct rte_distributor_returned_pkts returns;
100 };
101
102 TAILQ_HEAD(rte_distributor_list, rte_distributor);
103
104 /**** APIs called by workers ****/
105
106 struct rte_mbuf *
107 rte_distributor_get_pkt(struct rte_distributor *d,
108                 unsigned worker_id, struct rte_mbuf *oldpkt)
109 {
110         union rte_distributor_buffer *buf = &d->bufs[worker_id];
111         int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
112                         | RTE_DISTRIB_GET_BUF;
113         while (unlikely(buf->bufptr64 & RTE_DISTRIB_FLAGS_MASK))
114                 rte_pause();
115         buf->bufptr64 = req;
116         while (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
117                 rte_pause();
118         /* since bufptr64 is signed, this should be an arithmetic shift */
119         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
120         return (struct rte_mbuf *)((uintptr_t)ret);
121 }
122
123 int
124 rte_distributor_return_pkt(struct rte_distributor *d,
125                 unsigned worker_id, struct rte_mbuf *oldpkt)
126 {
127         union rte_distributor_buffer *buf = &d->bufs[worker_id];
128         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
129                         | RTE_DISTRIB_RETURN_BUF;
130         buf->bufptr64 = req;
131         return 0;
132 }
133
134 /**** APIs called on distributor core ***/
135
136 /* as name suggests, adds a packet to the backlog for a particular worker */
137 static int
138 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
139 {
140         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
141                 return -1;
142
143         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
144                         = item;
145         return 0;
146 }
147
148 /* takes the next packet for a worker off the backlog */
149 static int64_t
150 backlog_pop(struct rte_distributor_backlog *bl)
151 {
152         bl->count--;
153         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
154 }
155
156 /* stores a packet returned from a worker inside the returns array */
157 static inline void
158 store_return(uintptr_t oldbuf, struct rte_distributor *d,
159                 unsigned *ret_start, unsigned *ret_count)
160 {
161         /* store returns in a circular buffer - code is branch-free */
162         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
163                         = (void *)oldbuf;
164         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
165         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
166 }
167
168 static inline void
169 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
170 {
171         d->in_flight_tags[wkr] = 0;
172         d->bufs[wkr].bufptr64 = 0;
173         if (unlikely(d->backlog[wkr].count != 0)) {
174                 /* On return of a packet, we need to move the
175                  * queued packets for this core elsewhere.
176                  * Easiest solution is to set things up for
177                  * a recursive call. That will cause those
178                  * packets to be queued up for the next free
179                  * core, i.e. it will return as soon as a
180                  * core becomes free to accept the first
181                  * packet, as subsequent ones will be added to
182                  * the backlog for that core.
183                  */
184                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
185                 unsigned i;
186                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
187
188                 for (i = 0; i < bl->count; i++) {
189                         unsigned idx = (bl->start + i) &
190                                         RTE_DISTRIB_BACKLOG_MASK;
191                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
192                                         RTE_DISTRIB_FLAG_BITS));
193                 }
194                 /* recursive call */
195                 rte_distributor_process(d, pkts, i);
196                 bl->count = bl->start = 0;
197         }
198 }
199
200 /* this function is called when process() fn is called without any new
201  * packets. It goes through all the workers and clears any returned packets
202  * to do a partial flush.
203  */
204 static int
205 process_returns(struct rte_distributor *d)
206 {
207         unsigned wkr;
208         unsigned flushed = 0;
209         unsigned ret_start = d->returns.start,
210                         ret_count = d->returns.count;
211
212         for (wkr = 0; wkr < d->num_workers; wkr++) {
213
214                 const int64_t data = d->bufs[wkr].bufptr64;
215                 uintptr_t oldbuf = 0;
216
217                 if (data & RTE_DISTRIB_GET_BUF) {
218                         flushed++;
219                         if (d->backlog[wkr].count)
220                                 d->bufs[wkr].bufptr64 =
221                                                 backlog_pop(&d->backlog[wkr]);
222                         else {
223                                 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
224                                 d->in_flight_tags[wkr] = 0;
225                         }
226                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
227                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
228                         handle_worker_shutdown(d, wkr);
229                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
230                 }
231
232                 store_return(oldbuf, d, &ret_start, &ret_count);
233         }
234
235         d->returns.start = ret_start;
236         d->returns.count = ret_count;
237
238         return flushed;
239 }
240
241 /* process a set of packets to distribute them to workers */
242 int
243 rte_distributor_process(struct rte_distributor *d,
244                 struct rte_mbuf **mbufs, unsigned num_mbufs)
245 {
246         unsigned next_idx = 0;
247         unsigned wkr = 0;
248         struct rte_mbuf *next_mb = NULL;
249         int64_t next_value = 0;
250         uint32_t new_tag = 0;
251         unsigned ret_start = d->returns.start,
252                         ret_count = d->returns.count;
253
254         if (unlikely(num_mbufs == 0))
255                 return process_returns(d);
256
257         while (next_idx < num_mbufs || next_mb != NULL) {
258
259                 int64_t data = d->bufs[wkr].bufptr64;
260                 uintptr_t oldbuf = 0;
261
262                 if (!next_mb) {
263                         next_mb = mbufs[next_idx++];
264                         next_value = (((int64_t)(uintptr_t)next_mb)
265                                         << RTE_DISTRIB_FLAG_BITS);
266                         new_tag = (next_mb->pkt.hash.rss | 1);
267
268                         uint32_t match = 0;
269                         unsigned i;
270                         for (i = 0; i < d->num_workers; i++)
271                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
272                                         << i);
273
274                         if (match) {
275                                 next_mb = NULL;
276                                 unsigned worker = __builtin_ctz(match);
277                                 if (add_to_backlog(&d->backlog[worker],
278                                                 next_value) < 0)
279                                         next_idx--;
280                         }
281                 }
282
283                 if ((data & RTE_DISTRIB_GET_BUF) &&
284                                 (d->backlog[wkr].count || next_mb)) {
285
286                         if (d->backlog[wkr].count)
287                                 d->bufs[wkr].bufptr64 =
288                                                 backlog_pop(&d->backlog[wkr]);
289
290                         else {
291                                 d->bufs[wkr].bufptr64 = next_value;
292                                 d->in_flight_tags[wkr] = new_tag;
293                                 next_mb = NULL;
294                         }
295                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
296                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
297                         handle_worker_shutdown(d, wkr);
298                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
299                 }
300
301                 /* store returns in a circular buffer */
302                 store_return(oldbuf, d, &ret_start, &ret_count);
303
304                 if (++wkr == d->num_workers)
305                         wkr = 0;
306         }
307         /* to finish, check all workers for backlog and schedule work for them
308          * if they are ready */
309         for (wkr = 0; wkr < d->num_workers; wkr++)
310                 if (d->backlog[wkr].count &&
311                                 (d->bufs[wkr].bufptr64 & RTE_DISTRIB_GET_BUF)) {
312
313                         int64_t oldbuf = d->bufs[wkr].bufptr64 >>
314                                         RTE_DISTRIB_FLAG_BITS;
315                         store_return(oldbuf, d, &ret_start, &ret_count);
316
317                         d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
318                 }
319
320         d->returns.start = ret_start;
321         d->returns.count = ret_count;
322         return num_mbufs;
323 }
324
325 /* return to the caller, packets returned from workers */
326 int
327 rte_distributor_returned_pkts(struct rte_distributor *d,
328                 struct rte_mbuf **mbufs, unsigned max_mbufs)
329 {
330         struct rte_distributor_returned_pkts *returns = &d->returns;
331         unsigned retval = (max_mbufs < returns->count) ?
332                         max_mbufs : returns->count;
333         unsigned i;
334
335         for (i = 0; i < retval; i++) {
336                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
337                 mbufs[i] = returns->mbufs[idx];
338         }
339         returns->start += i;
340         returns->count -= i;
341
342         return retval;
343 }
344
345 /* return the number of packets in-flight in a distributor, i.e. packets
346  * being workered on or queued up in a backlog. */
347 static inline unsigned
348 total_outstanding(const struct rte_distributor *d)
349 {
350         unsigned wkr, total_outstanding = 0;
351
352         for (wkr = 0; wkr < d->num_workers; wkr++)
353                 total_outstanding += d->backlog[wkr].count +
354                                 !!(d->in_flight_tags[wkr]);
355         return total_outstanding;
356 }
357
358 /* flush the distributor, so that there are no outstanding packets in flight or
359  * queued up. */
360 int
361 rte_distributor_flush(struct rte_distributor *d)
362 {
363         const unsigned flushed = total_outstanding(d);
364
365         while (total_outstanding(d) > 0)
366                 rte_distributor_process(d, NULL, 0);
367
368         return flushed;
369 }
370
371 /* clears the internal returns array in the distributor */
372 void
373 rte_distributor_clear_returns(struct rte_distributor *d)
374 {
375         d->returns.start = d->returns.count = 0;
376 #ifndef __OPTIMIZE__
377         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
378 #endif
379 }
380
381 /* creates a distributor instance */
382 struct rte_distributor *
383 rte_distributor_create(const char *name,
384                 unsigned socket_id,
385                 unsigned num_workers)
386 {
387         struct rte_distributor *d;
388         struct rte_distributor_list *distributor_list;
389         char mz_name[RTE_MEMZONE_NAMESIZE];
390         const struct rte_memzone *mz;
391
392         /* compilation-time checks */
393         RTE_BUILD_BUG_ON((sizeof(*d) & CACHE_LINE_MASK) != 0);
394         RTE_BUILD_BUG_ON((RTE_MAX_LCORE & 7) != 0);
395
396         if (name == NULL || num_workers >= RTE_MAX_LCORE) {
397                 rte_errno = EINVAL;
398                 return NULL;
399         }
400
401         /* check that we have an initialised tail queue */
402         distributor_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_DISTRIBUTOR,
403                                 rte_distributor_list);
404         if (distributor_list == NULL) {
405                 rte_errno = E_RTE_NO_TAILQ;
406                 return NULL;
407         }
408
409         rte_snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
410         mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
411         if (mz == NULL) {
412                 rte_errno = ENOMEM;
413                 return NULL;
414         }
415
416         d = mz->addr;
417         rte_snprintf(d->name, sizeof(d->name), "%s", name);
418         d->num_workers = num_workers;
419
420         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
421         TAILQ_INSERT_TAIL(distributor_list, d, next);
422         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
423
424         return d;
425 }