Imported Upstream version 16.04
[deb_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_memory.h>
39 #include <rte_memzone.h>
40 #include <rte_errno.h>
41 #include <rte_string_fns.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  * Maximum number of workers allowed.
66  * Be aware of increasing the limit, becaus it is limited by how we track
67  * in-flight tags. See @in_flight_bitmask and @rte_distributor_process
68  */
69 #define RTE_DISTRIB_MAX_WORKERS 64
70
71 /**
72  * Buffer structure used to pass the pointer data between cores. This is cache
73  * line aligned, but to improve performance and prevent adjacent cache-line
74  * prefetches of buffers for other workers, e.g. when worker 1's buffer is on
75  * the next cache line to worker 0, we pad this out to three cache lines.
76  * Only 64-bits of the memory is actually used though.
77  */
78 union rte_distributor_buffer {
79         volatile int64_t bufptr64;
80         char pad[RTE_CACHE_LINE_SIZE*3];
81 } __rte_cache_aligned;
82
83 struct rte_distributor_backlog {
84         unsigned start;
85         unsigned count;
86         int64_t pkts[RTE_DISTRIB_BACKLOG_SIZE];
87 };
88
89 struct rte_distributor_returned_pkts {
90         unsigned start;
91         unsigned count;
92         struct rte_mbuf *mbufs[RTE_DISTRIB_MAX_RETURNS];
93 };
94
95 struct rte_distributor {
96         TAILQ_ENTRY(rte_distributor) next;    /**< Next in list. */
97
98         char name[RTE_DISTRIBUTOR_NAMESIZE];  /**< Name of the ring. */
99         unsigned num_workers;                 /**< Number of workers polling */
100
101         uint32_t in_flight_tags[RTE_DISTRIB_MAX_WORKERS];
102                 /**< Tracks the tag being processed per core */
103         uint64_t in_flight_bitmask;
104                 /**< on/off bits for in-flight tags.
105                  * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64 then
106                  * the bitmask has to expand.
107                  */
108
109         struct rte_distributor_backlog backlog[RTE_DISTRIB_MAX_WORKERS];
110
111         union rte_distributor_buffer bufs[RTE_DISTRIB_MAX_WORKERS];
112
113         struct rte_distributor_returned_pkts returns;
114 };
115
116 TAILQ_HEAD(rte_distributor_list, rte_distributor);
117
118 static struct rte_tailq_elem rte_distributor_tailq = {
119         .name = "RTE_DISTRIBUTOR",
120 };
121 EAL_REGISTER_TAILQ(rte_distributor_tailq)
122
123 /**** APIs called by workers ****/
124
125 void
126 rte_distributor_request_pkt(struct rte_distributor *d,
127                 unsigned worker_id, struct rte_mbuf *oldpkt)
128 {
129         union rte_distributor_buffer *buf = &d->bufs[worker_id];
130         int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
131                         | RTE_DISTRIB_GET_BUF;
132         while (unlikely(buf->bufptr64 & RTE_DISTRIB_FLAGS_MASK))
133                 rte_pause();
134         buf->bufptr64 = req;
135 }
136
137 struct rte_mbuf *
138 rte_distributor_poll_pkt(struct rte_distributor *d,
139                 unsigned worker_id)
140 {
141         union rte_distributor_buffer *buf = &d->bufs[worker_id];
142         if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
143                 return NULL;
144
145         /* since bufptr64 is signed, this should be an arithmetic shift */
146         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
147         return (struct rte_mbuf *)((uintptr_t)ret);
148 }
149
150 struct rte_mbuf *
151 rte_distributor_get_pkt(struct rte_distributor *d,
152                 unsigned worker_id, struct rte_mbuf *oldpkt)
153 {
154         struct rte_mbuf *ret;
155         rte_distributor_request_pkt(d, worker_id, oldpkt);
156         while ((ret = rte_distributor_poll_pkt(d, worker_id)) == NULL)
157                 rte_pause();
158         return ret;
159 }
160
161 int
162 rte_distributor_return_pkt(struct rte_distributor *d,
163                 unsigned worker_id, struct rte_mbuf *oldpkt)
164 {
165         union rte_distributor_buffer *buf = &d->bufs[worker_id];
166         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
167                         | RTE_DISTRIB_RETURN_BUF;
168         buf->bufptr64 = req;
169         return 0;
170 }
171
172 /**** APIs called on distributor core ***/
173
174 /* as name suggests, adds a packet to the backlog for a particular worker */
175 static int
176 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
177 {
178         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
179                 return -1;
180
181         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
182                         = item;
183         return 0;
184 }
185
186 /* takes the next packet for a worker off the backlog */
187 static int64_t
188 backlog_pop(struct rte_distributor_backlog *bl)
189 {
190         bl->count--;
191         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
192 }
193
194 /* stores a packet returned from a worker inside the returns array */
195 static inline void
196 store_return(uintptr_t oldbuf, struct rte_distributor *d,
197                 unsigned *ret_start, unsigned *ret_count)
198 {
199         /* store returns in a circular buffer - code is branch-free */
200         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
201                         = (void *)oldbuf;
202         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
203         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
204 }
205
206 static inline void
207 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
208 {
209         d->in_flight_tags[wkr] = 0;
210         d->in_flight_bitmask &= ~(1UL << wkr);
211         d->bufs[wkr].bufptr64 = 0;
212         if (unlikely(d->backlog[wkr].count != 0)) {
213                 /* On return of a packet, we need to move the
214                  * queued packets for this core elsewhere.
215                  * Easiest solution is to set things up for
216                  * a recursive call. That will cause those
217                  * packets to be queued up for the next free
218                  * core, i.e. it will return as soon as a
219                  * core becomes free to accept the first
220                  * packet, as subsequent ones will be added to
221                  * the backlog for that core.
222                  */
223                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
224                 unsigned i;
225                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
226
227                 for (i = 0; i < bl->count; i++) {
228                         unsigned idx = (bl->start + i) &
229                                         RTE_DISTRIB_BACKLOG_MASK;
230                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
231                                         RTE_DISTRIB_FLAG_BITS));
232                 }
233                 /* recursive call.
234                  * Note that the tags were set before first level call
235                  * to rte_distributor_process.
236                  */
237                 rte_distributor_process(d, pkts, i);
238                 bl->count = bl->start = 0;
239         }
240 }
241
242 /* this function is called when process() fn is called without any new
243  * packets. It goes through all the workers and clears any returned packets
244  * to do a partial flush.
245  */
246 static int
247 process_returns(struct rte_distributor *d)
248 {
249         unsigned wkr;
250         unsigned flushed = 0;
251         unsigned ret_start = d->returns.start,
252                         ret_count = d->returns.count;
253
254         for (wkr = 0; wkr < d->num_workers; wkr++) {
255
256                 const int64_t data = d->bufs[wkr].bufptr64;
257                 uintptr_t oldbuf = 0;
258
259                 if (data & RTE_DISTRIB_GET_BUF) {
260                         flushed++;
261                         if (d->backlog[wkr].count)
262                                 d->bufs[wkr].bufptr64 =
263                                                 backlog_pop(&d->backlog[wkr]);
264                         else {
265                                 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
266                                 d->in_flight_tags[wkr] = 0;
267                                 d->in_flight_bitmask &= ~(1UL << wkr);
268                         }
269                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
270                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
271                         handle_worker_shutdown(d, wkr);
272                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
273                 }
274
275                 store_return(oldbuf, d, &ret_start, &ret_count);
276         }
277
278         d->returns.start = ret_start;
279         d->returns.count = ret_count;
280
281         return flushed;
282 }
283
284 /* process a set of packets to distribute them to workers */
285 int
286 rte_distributor_process(struct rte_distributor *d,
287                 struct rte_mbuf **mbufs, unsigned num_mbufs)
288 {
289         unsigned next_idx = 0;
290         unsigned wkr = 0;
291         struct rte_mbuf *next_mb = NULL;
292         int64_t next_value = 0;
293         uint32_t new_tag = 0;
294         unsigned ret_start = d->returns.start,
295                         ret_count = d->returns.count;
296
297         if (unlikely(num_mbufs == 0))
298                 return process_returns(d);
299
300         while (next_idx < num_mbufs || next_mb != NULL) {
301
302                 int64_t data = d->bufs[wkr].bufptr64;
303                 uintptr_t oldbuf = 0;
304
305                 if (!next_mb) {
306                         next_mb = mbufs[next_idx++];
307                         next_value = (((int64_t)(uintptr_t)next_mb)
308                                         << RTE_DISTRIB_FLAG_BITS);
309                         /*
310                          * User is advocated to set tag vaue for each
311                          * mbuf before calling rte_distributor_process.
312                          * User defined tags are used to identify flows,
313                          * or sessions.
314                          */
315                         new_tag = next_mb->hash.usr;
316
317                         /*
318                          * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64
319                          * then the size of match has to be expanded.
320                          */
321                         uint64_t match = 0;
322                         unsigned i;
323                         /*
324                          * to scan for a match use "xor" and "not" to get a 0/1
325                          * value, then use shifting to merge to single "match"
326                          * variable, where a one-bit indicates a match for the
327                          * worker given by the bit-position
328                          */
329                         for (i = 0; i < d->num_workers; i++)
330                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
331                                         << i);
332
333                         /* Only turned-on bits are considered as match */
334                         match &= d->in_flight_bitmask;
335
336                         if (match) {
337                                 next_mb = NULL;
338                                 unsigned worker = __builtin_ctzl(match);
339                                 if (add_to_backlog(&d->backlog[worker],
340                                                 next_value) < 0)
341                                         next_idx--;
342                         }
343                 }
344
345                 if ((data & RTE_DISTRIB_GET_BUF) &&
346                                 (d->backlog[wkr].count || next_mb)) {
347
348                         if (d->backlog[wkr].count)
349                                 d->bufs[wkr].bufptr64 =
350                                                 backlog_pop(&d->backlog[wkr]);
351
352                         else {
353                                 d->bufs[wkr].bufptr64 = next_value;
354                                 d->in_flight_tags[wkr] = new_tag;
355                                 d->in_flight_bitmask |= (1UL << wkr);
356                                 next_mb = NULL;
357                         }
358                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
359                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
360                         handle_worker_shutdown(d, wkr);
361                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
362                 }
363
364                 /* store returns in a circular buffer */
365                 store_return(oldbuf, d, &ret_start, &ret_count);
366
367                 if (++wkr == d->num_workers)
368                         wkr = 0;
369         }
370         /* to finish, check all workers for backlog and schedule work for them
371          * if they are ready */
372         for (wkr = 0; wkr < d->num_workers; wkr++)
373                 if (d->backlog[wkr].count &&
374                                 (d->bufs[wkr].bufptr64 & RTE_DISTRIB_GET_BUF)) {
375
376                         int64_t oldbuf = d->bufs[wkr].bufptr64 >>
377                                         RTE_DISTRIB_FLAG_BITS;
378                         store_return(oldbuf, d, &ret_start, &ret_count);
379
380                         d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
381                 }
382
383         d->returns.start = ret_start;
384         d->returns.count = ret_count;
385         return num_mbufs;
386 }
387
388 /* return to the caller, packets returned from workers */
389 int
390 rte_distributor_returned_pkts(struct rte_distributor *d,
391                 struct rte_mbuf **mbufs, unsigned max_mbufs)
392 {
393         struct rte_distributor_returned_pkts *returns = &d->returns;
394         unsigned retval = (max_mbufs < returns->count) ?
395                         max_mbufs : returns->count;
396         unsigned i;
397
398         for (i = 0; i < retval; i++) {
399                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
400                 mbufs[i] = returns->mbufs[idx];
401         }
402         returns->start += i;
403         returns->count -= i;
404
405         return retval;
406 }
407
408 /* return the number of packets in-flight in a distributor, i.e. packets
409  * being workered on or queued up in a backlog. */
410 static inline unsigned
411 total_outstanding(const struct rte_distributor *d)
412 {
413         unsigned wkr, total_outstanding;
414
415         total_outstanding = __builtin_popcountl(d->in_flight_bitmask);
416
417         for (wkr = 0; wkr < d->num_workers; wkr++)
418                 total_outstanding += d->backlog[wkr].count;
419
420         return total_outstanding;
421 }
422
423 /* flush the distributor, so that there are no outstanding packets in flight or
424  * queued up. */
425 int
426 rte_distributor_flush(struct rte_distributor *d)
427 {
428         const unsigned flushed = total_outstanding(d);
429
430         while (total_outstanding(d) > 0)
431                 rte_distributor_process(d, NULL, 0);
432
433         return flushed;
434 }
435
436 /* clears the internal returns array in the distributor */
437 void
438 rte_distributor_clear_returns(struct rte_distributor *d)
439 {
440         d->returns.start = d->returns.count = 0;
441 #ifndef __OPTIMIZE__
442         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
443 #endif
444 }
445
446 /* creates a distributor instance */
447 struct rte_distributor *
448 rte_distributor_create(const char *name,
449                 unsigned socket_id,
450                 unsigned num_workers)
451 {
452         struct rte_distributor *d;
453         struct rte_distributor_list *distributor_list;
454         char mz_name[RTE_MEMZONE_NAMESIZE];
455         const struct rte_memzone *mz;
456
457         /* compilation-time checks */
458         RTE_BUILD_BUG_ON((sizeof(*d) & RTE_CACHE_LINE_MASK) != 0);
459         RTE_BUILD_BUG_ON((RTE_DISTRIB_MAX_WORKERS & 7) != 0);
460         RTE_BUILD_BUG_ON(RTE_DISTRIB_MAX_WORKERS >
461                                 sizeof(d->in_flight_bitmask) * CHAR_BIT);
462
463         if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
464                 rte_errno = EINVAL;
465                 return NULL;
466         }
467
468         snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
469         mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
470         if (mz == NULL) {
471                 rte_errno = ENOMEM;
472                 return NULL;
473         }
474
475         d = mz->addr;
476         snprintf(d->name, sizeof(d->name), "%s", name);
477         d->num_workers = num_workers;
478
479         distributor_list = RTE_TAILQ_CAST(rte_distributor_tailq.head,
480                                           rte_distributor_list);
481
482         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
483         TAILQ_INSERT_TAIL(distributor_list, d, next);
484         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
485
486         return d;
487 }