9adda52b74557abe5e8edc572e490ae1aeee1ccd
[dpdk.git] / lib / librte_distributor / rte_distributor_v20.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_compat.h>
42 #include <rte_string_fns.h>
43 #include <rte_eal_memconfig.h>
44 #include <rte_pause.h>
45
46 #include "rte_distributor_v20.h"
47 #include "rte_distributor_private.h"
48
49 TAILQ_HEAD(rte_distributor_list, rte_distributor_v20);
50
51 static struct rte_tailq_elem rte_distributor_tailq = {
52         .name = "RTE_DISTRIBUTOR",
53 };
54 EAL_REGISTER_TAILQ(rte_distributor_tailq)
55
56 /**** APIs called by workers ****/
57
58 void
59 rte_distributor_request_pkt_v20(struct rte_distributor_v20 *d,
60                 unsigned worker_id, struct rte_mbuf *oldpkt)
61 {
62         union rte_distributor_buffer_v20 *buf = &d->bufs[worker_id];
63         int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
64                         | RTE_DISTRIB_GET_BUF;
65         while (unlikely(buf->bufptr64 & RTE_DISTRIB_FLAGS_MASK))
66                 rte_pause();
67         buf->bufptr64 = req;
68 }
69 VERSION_SYMBOL(rte_distributor_request_pkt, _v20, 2.0);
70
71 struct rte_mbuf *
72 rte_distributor_poll_pkt_v20(struct rte_distributor_v20 *d,
73                 unsigned worker_id)
74 {
75         union rte_distributor_buffer_v20 *buf = &d->bufs[worker_id];
76         if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
77                 return NULL;
78
79         /* since bufptr64 is signed, this should be an arithmetic shift */
80         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
81         return (struct rte_mbuf *)((uintptr_t)ret);
82 }
83 VERSION_SYMBOL(rte_distributor_poll_pkt, _v20, 2.0);
84
85 struct rte_mbuf *
86 rte_distributor_get_pkt_v20(struct rte_distributor_v20 *d,
87                 unsigned worker_id, struct rte_mbuf *oldpkt)
88 {
89         struct rte_mbuf *ret;
90         rte_distributor_request_pkt_v20(d, worker_id, oldpkt);
91         while ((ret = rte_distributor_poll_pkt_v20(d, worker_id)) == NULL)
92                 rte_pause();
93         return ret;
94 }
95 VERSION_SYMBOL(rte_distributor_get_pkt, _v20, 2.0);
96
97 int
98 rte_distributor_return_pkt_v20(struct rte_distributor_v20 *d,
99                 unsigned worker_id, struct rte_mbuf *oldpkt)
100 {
101         union rte_distributor_buffer_v20 *buf = &d->bufs[worker_id];
102         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
103                         | RTE_DISTRIB_RETURN_BUF;
104         buf->bufptr64 = req;
105         return 0;
106 }
107 VERSION_SYMBOL(rte_distributor_return_pkt, _v20, 2.0);
108
109 /**** APIs called on distributor core ***/
110
111 /* as name suggests, adds a packet to the backlog for a particular worker */
112 static int
113 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
114 {
115         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
116                 return -1;
117
118         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
119                         = item;
120         return 0;
121 }
122
123 /* takes the next packet for a worker off the backlog */
124 static int64_t
125 backlog_pop(struct rte_distributor_backlog *bl)
126 {
127         bl->count--;
128         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
129 }
130
131 /* stores a packet returned from a worker inside the returns array */
132 static inline void
133 store_return(uintptr_t oldbuf, struct rte_distributor_v20 *d,
134                 unsigned *ret_start, unsigned *ret_count)
135 {
136         /* store returns in a circular buffer - code is branch-free */
137         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
138                         = (void *)oldbuf;
139         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
140         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
141 }
142
143 static inline void
144 handle_worker_shutdown(struct rte_distributor_v20 *d, unsigned int wkr)
145 {
146         d->in_flight_tags[wkr] = 0;
147         d->in_flight_bitmask &= ~(1UL << wkr);
148         d->bufs[wkr].bufptr64 = 0;
149         if (unlikely(d->backlog[wkr].count != 0)) {
150                 /* On return of a packet, we need to move the
151                  * queued packets for this core elsewhere.
152                  * Easiest solution is to set things up for
153                  * a recursive call. That will cause those
154                  * packets to be queued up for the next free
155                  * core, i.e. it will return as soon as a
156                  * core becomes free to accept the first
157                  * packet, as subsequent ones will be added to
158                  * the backlog for that core.
159                  */
160                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
161                 unsigned i;
162                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
163
164                 for (i = 0; i < bl->count; i++) {
165                         unsigned idx = (bl->start + i) &
166                                         RTE_DISTRIB_BACKLOG_MASK;
167                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
168                                         RTE_DISTRIB_FLAG_BITS));
169                 }
170                 /* recursive call.
171                  * Note that the tags were set before first level call
172                  * to rte_distributor_process.
173                  */
174                 rte_distributor_process_v20(d, pkts, i);
175                 bl->count = bl->start = 0;
176         }
177 }
178
179 /* this function is called when process() fn is called without any new
180  * packets. It goes through all the workers and clears any returned packets
181  * to do a partial flush.
182  */
183 static int
184 process_returns(struct rte_distributor_v20 *d)
185 {
186         unsigned wkr;
187         unsigned flushed = 0;
188         unsigned ret_start = d->returns.start,
189                         ret_count = d->returns.count;
190
191         for (wkr = 0; wkr < d->num_workers; wkr++) {
192
193                 const int64_t data = d->bufs[wkr].bufptr64;
194                 uintptr_t oldbuf = 0;
195
196                 if (data & RTE_DISTRIB_GET_BUF) {
197                         flushed++;
198                         if (d->backlog[wkr].count)
199                                 d->bufs[wkr].bufptr64 =
200                                                 backlog_pop(&d->backlog[wkr]);
201                         else {
202                                 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
203                                 d->in_flight_tags[wkr] = 0;
204                                 d->in_flight_bitmask &= ~(1UL << wkr);
205                         }
206                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
207                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
208                         handle_worker_shutdown(d, wkr);
209                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
210                 }
211
212                 store_return(oldbuf, d, &ret_start, &ret_count);
213         }
214
215         d->returns.start = ret_start;
216         d->returns.count = ret_count;
217
218         return flushed;
219 }
220
221 /* process a set of packets to distribute them to workers */
222 int
223 rte_distributor_process_v20(struct rte_distributor_v20 *d,
224                 struct rte_mbuf **mbufs, unsigned num_mbufs)
225 {
226         unsigned next_idx = 0;
227         unsigned wkr = 0;
228         struct rte_mbuf *next_mb = NULL;
229         int64_t next_value = 0;
230         uint32_t new_tag = 0;
231         unsigned ret_start = d->returns.start,
232                         ret_count = d->returns.count;
233
234         if (unlikely(num_mbufs == 0))
235                 return process_returns(d);
236
237         while (next_idx < num_mbufs || next_mb != NULL) {
238
239                 int64_t data = d->bufs[wkr].bufptr64;
240                 uintptr_t oldbuf = 0;
241
242                 if (!next_mb) {
243                         next_mb = mbufs[next_idx++];
244                         next_value = (((int64_t)(uintptr_t)next_mb)
245                                         << RTE_DISTRIB_FLAG_BITS);
246                         /*
247                          * User is advocated to set tag value for each
248                          * mbuf before calling rte_distributor_process.
249                          * User defined tags are used to identify flows,
250                          * or sessions.
251                          */
252                         new_tag = next_mb->hash.usr;
253
254                         /*
255                          * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64
256                          * then the size of match has to be expanded.
257                          */
258                         uint64_t match = 0;
259                         unsigned i;
260                         /*
261                          * to scan for a match use "xor" and "not" to get a 0/1
262                          * value, then use shifting to merge to single "match"
263                          * variable, where a one-bit indicates a match for the
264                          * worker given by the bit-position
265                          */
266                         for (i = 0; i < d->num_workers; i++)
267                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
268                                         << i);
269
270                         /* Only turned-on bits are considered as match */
271                         match &= d->in_flight_bitmask;
272
273                         if (match) {
274                                 next_mb = NULL;
275                                 unsigned worker = __builtin_ctzl(match);
276                                 if (add_to_backlog(&d->backlog[worker],
277                                                 next_value) < 0)
278                                         next_idx--;
279                         }
280                 }
281
282                 if ((data & RTE_DISTRIB_GET_BUF) &&
283                                 (d->backlog[wkr].count || next_mb)) {
284
285                         if (d->backlog[wkr].count)
286                                 d->bufs[wkr].bufptr64 =
287                                                 backlog_pop(&d->backlog[wkr]);
288
289                         else {
290                                 d->bufs[wkr].bufptr64 = next_value;
291                                 d->in_flight_tags[wkr] = new_tag;
292                                 d->in_flight_bitmask |= (1UL << wkr);
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 VERSION_SYMBOL(rte_distributor_process, _v20, 2.0);
325
326 /* return to the caller, packets returned from workers */
327 int
328 rte_distributor_returned_pkts_v20(struct rte_distributor_v20 *d,
329                 struct rte_mbuf **mbufs, unsigned max_mbufs)
330 {
331         struct rte_distributor_returned_pkts *returns = &d->returns;
332         unsigned retval = (max_mbufs < returns->count) ?
333                         max_mbufs : returns->count;
334         unsigned i;
335
336         for (i = 0; i < retval; i++) {
337                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
338                 mbufs[i] = returns->mbufs[idx];
339         }
340         returns->start += i;
341         returns->count -= i;
342
343         return retval;
344 }
345 VERSION_SYMBOL(rte_distributor_returned_pkts, _v20, 2.0);
346
347 /* return the number of packets in-flight in a distributor, i.e. packets
348  * being workered on or queued up in a backlog. */
349 static inline unsigned
350 total_outstanding(const struct rte_distributor_v20 *d)
351 {
352         unsigned wkr, total_outstanding;
353
354         total_outstanding = __builtin_popcountl(d->in_flight_bitmask);
355
356         for (wkr = 0; wkr < d->num_workers; wkr++)
357                 total_outstanding += d->backlog[wkr].count;
358
359         return total_outstanding;
360 }
361
362 /* flush the distributor, so that there are no outstanding packets in flight or
363  * queued up. */
364 int
365 rte_distributor_flush_v20(struct rte_distributor_v20 *d)
366 {
367         const unsigned flushed = total_outstanding(d);
368
369         while (total_outstanding(d) > 0)
370                 rte_distributor_process_v20(d, NULL, 0);
371
372         return flushed;
373 }
374 VERSION_SYMBOL(rte_distributor_flush, _v20, 2.0);
375
376 /* clears the internal returns array in the distributor */
377 void
378 rte_distributor_clear_returns_v20(struct rte_distributor_v20 *d)
379 {
380         d->returns.start = d->returns.count = 0;
381 #ifndef __OPTIMIZE__
382         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
383 #endif
384 }
385 VERSION_SYMBOL(rte_distributor_clear_returns, _v20, 2.0);
386
387 /* creates a distributor instance */
388 struct rte_distributor_v20 *
389 rte_distributor_create_v20(const char *name,
390                 unsigned socket_id,
391                 unsigned num_workers)
392 {
393         struct rte_distributor_v20 *d;
394         struct rte_distributor_list *distributor_list;
395         char mz_name[RTE_MEMZONE_NAMESIZE];
396         const struct rte_memzone *mz;
397
398         /* compilation-time checks */
399         RTE_BUILD_BUG_ON((sizeof(*d) & RTE_CACHE_LINE_MASK) != 0);
400         RTE_BUILD_BUG_ON((RTE_DISTRIB_MAX_WORKERS & 7) != 0);
401         RTE_BUILD_BUG_ON(RTE_DISTRIB_MAX_WORKERS >
402                                 sizeof(d->in_flight_bitmask) * CHAR_BIT);
403
404         if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
405                 rte_errno = EINVAL;
406                 return NULL;
407         }
408
409         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         snprintf(d->name, sizeof(d->name), "%s", name);
418         d->num_workers = num_workers;
419
420         distributor_list = RTE_TAILQ_CAST(rte_distributor_tailq.head,
421                                           rte_distributor_list);
422
423         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
424         TAILQ_INSERT_TAIL(distributor_list, d, next);
425         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
426
427         return d;
428 }
429 VERSION_SYMBOL(rte_distributor_create, _v20, 2.0);