mbuf: add usr alias for hash
[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                 /**< Tracks the tag being processed per core, 0 == no pkt */
96         struct rte_distributor_backlog backlog[RTE_MAX_LCORE];
97
98         union rte_distributor_buffer bufs[RTE_MAX_LCORE];
99
100         struct rte_distributor_returned_pkts returns;
101 };
102
103 TAILQ_HEAD(rte_distributor_list, rte_distributor);
104
105 /**** APIs called by workers ****/
106
107 void
108 rte_distributor_request_pkt(struct rte_distributor *d,
109                 unsigned worker_id, struct rte_mbuf *oldpkt)
110 {
111         union rte_distributor_buffer *buf = &d->bufs[worker_id];
112         int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
113                         | RTE_DISTRIB_GET_BUF;
114         while (unlikely(buf->bufptr64 & RTE_DISTRIB_FLAGS_MASK))
115                 rte_pause();
116         buf->bufptr64 = req;
117 }
118
119 struct rte_mbuf *
120 rte_distributor_poll_pkt(struct rte_distributor *d,
121                 unsigned worker_id)
122 {
123         union rte_distributor_buffer *buf = &d->bufs[worker_id];
124         if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
125                 return NULL;
126
127         /* since bufptr64 is signed, this should be an arithmetic shift */
128         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
129         return (struct rte_mbuf *)((uintptr_t)ret);
130 }
131
132 struct rte_mbuf *
133 rte_distributor_get_pkt(struct rte_distributor *d,
134                 unsigned worker_id, struct rte_mbuf *oldpkt)
135 {
136         struct rte_mbuf *ret;
137         rte_distributor_request_pkt(d, worker_id, oldpkt);
138         while ((ret = rte_distributor_poll_pkt(d, worker_id)) == NULL)
139                 rte_pause();
140         return ret;
141 }
142
143 int
144 rte_distributor_return_pkt(struct rte_distributor *d,
145                 unsigned worker_id, struct rte_mbuf *oldpkt)
146 {
147         union rte_distributor_buffer *buf = &d->bufs[worker_id];
148         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
149                         | RTE_DISTRIB_RETURN_BUF;
150         buf->bufptr64 = req;
151         return 0;
152 }
153
154 /**** APIs called on distributor core ***/
155
156 /* as name suggests, adds a packet to the backlog for a particular worker */
157 static int
158 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
159 {
160         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
161                 return -1;
162
163         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
164                         = item;
165         return 0;
166 }
167
168 /* takes the next packet for a worker off the backlog */
169 static int64_t
170 backlog_pop(struct rte_distributor_backlog *bl)
171 {
172         bl->count--;
173         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
174 }
175
176 /* stores a packet returned from a worker inside the returns array */
177 static inline void
178 store_return(uintptr_t oldbuf, struct rte_distributor *d,
179                 unsigned *ret_start, unsigned *ret_count)
180 {
181         /* store returns in a circular buffer - code is branch-free */
182         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
183                         = (void *)oldbuf;
184         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
185         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
186 }
187
188 static inline void
189 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
190 {
191         d->in_flight_tags[wkr] = 0;
192         d->bufs[wkr].bufptr64 = 0;
193         if (unlikely(d->backlog[wkr].count != 0)) {
194                 /* On return of a packet, we need to move the
195                  * queued packets for this core elsewhere.
196                  * Easiest solution is to set things up for
197                  * a recursive call. That will cause those
198                  * packets to be queued up for the next free
199                  * core, i.e. it will return as soon as a
200                  * core becomes free to accept the first
201                  * packet, as subsequent ones will be added to
202                  * the backlog for that core.
203                  */
204                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
205                 unsigned i;
206                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
207
208                 for (i = 0; i < bl->count; i++) {
209                         unsigned idx = (bl->start + i) &
210                                         RTE_DISTRIB_BACKLOG_MASK;
211                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
212                                         RTE_DISTRIB_FLAG_BITS));
213                 }
214                 /* recursive call */
215                 rte_distributor_process(d, pkts, i);
216                 bl->count = bl->start = 0;
217         }
218 }
219
220 /* this function is called when process() fn is called without any new
221  * packets. It goes through all the workers and clears any returned packets
222  * to do a partial flush.
223  */
224 static int
225 process_returns(struct rte_distributor *d)
226 {
227         unsigned wkr;
228         unsigned flushed = 0;
229         unsigned ret_start = d->returns.start,
230                         ret_count = d->returns.count;
231
232         for (wkr = 0; wkr < d->num_workers; wkr++) {
233
234                 const int64_t data = d->bufs[wkr].bufptr64;
235                 uintptr_t oldbuf = 0;
236
237                 if (data & RTE_DISTRIB_GET_BUF) {
238                         flushed++;
239                         if (d->backlog[wkr].count)
240                                 d->bufs[wkr].bufptr64 =
241                                                 backlog_pop(&d->backlog[wkr]);
242                         else {
243                                 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
244                                 d->in_flight_tags[wkr] = 0;
245                         }
246                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
247                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
248                         handle_worker_shutdown(d, wkr);
249                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
250                 }
251
252                 store_return(oldbuf, d, &ret_start, &ret_count);
253         }
254
255         d->returns.start = ret_start;
256         d->returns.count = ret_count;
257
258         return flushed;
259 }
260
261 /* process a set of packets to distribute them to workers */
262 int
263 rte_distributor_process(struct rte_distributor *d,
264                 struct rte_mbuf **mbufs, unsigned num_mbufs)
265 {
266         unsigned next_idx = 0;
267         unsigned wkr = 0;
268         struct rte_mbuf *next_mb = NULL;
269         int64_t next_value = 0;
270         uint32_t new_tag = 0;
271         unsigned ret_start = d->returns.start,
272                         ret_count = d->returns.count;
273
274         if (unlikely(num_mbufs == 0))
275                 return process_returns(d);
276
277         while (next_idx < num_mbufs || next_mb != NULL) {
278
279                 int64_t data = d->bufs[wkr].bufptr64;
280                 uintptr_t oldbuf = 0;
281
282                 if (!next_mb) {
283                         next_mb = mbufs[next_idx++];
284                         next_value = (((int64_t)(uintptr_t)next_mb)
285                                         << RTE_DISTRIB_FLAG_BITS);
286                         /*
287                          * Set the low bit on the tag, so we can guarantee that
288                          * we never store a tag value of zero. That means we can
289                          * use the zero-value to indicate that no packet is
290                          * being processed by a worker.
291                          */
292                         new_tag = (next_mb->hash.usr | 1);
293
294                         uint32_t match = 0;
295                         unsigned i;
296                         /*
297                          * to scan for a match use "xor" and "not" to get a 0/1
298                          * value, then use shifting to merge to single "match"
299                          * variable, where a one-bit indicates a match for the
300                          * worker given by the bit-position
301                          */
302                         for (i = 0; i < d->num_workers; i++)
303                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
304                                         << i);
305
306                         if (match) {
307                                 next_mb = NULL;
308                                 unsigned worker = __builtin_ctz(match);
309                                 if (add_to_backlog(&d->backlog[worker],
310                                                 next_value) < 0)
311                                         next_idx--;
312                         }
313                 }
314
315                 if ((data & RTE_DISTRIB_GET_BUF) &&
316                                 (d->backlog[wkr].count || next_mb)) {
317
318                         if (d->backlog[wkr].count)
319                                 d->bufs[wkr].bufptr64 =
320                                                 backlog_pop(&d->backlog[wkr]);
321
322                         else {
323                                 d->bufs[wkr].bufptr64 = next_value;
324                                 d->in_flight_tags[wkr] = new_tag;
325                                 next_mb = NULL;
326                         }
327                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
328                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
329                         handle_worker_shutdown(d, wkr);
330                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
331                 }
332
333                 /* store returns in a circular buffer */
334                 store_return(oldbuf, d, &ret_start, &ret_count);
335
336                 if (++wkr == d->num_workers)
337                         wkr = 0;
338         }
339         /* to finish, check all workers for backlog and schedule work for them
340          * if they are ready */
341         for (wkr = 0; wkr < d->num_workers; wkr++)
342                 if (d->backlog[wkr].count &&
343                                 (d->bufs[wkr].bufptr64 & RTE_DISTRIB_GET_BUF)) {
344
345                         int64_t oldbuf = d->bufs[wkr].bufptr64 >>
346                                         RTE_DISTRIB_FLAG_BITS;
347                         store_return(oldbuf, d, &ret_start, &ret_count);
348
349                         d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
350                 }
351
352         d->returns.start = ret_start;
353         d->returns.count = ret_count;
354         return num_mbufs;
355 }
356
357 /* return to the caller, packets returned from workers */
358 int
359 rte_distributor_returned_pkts(struct rte_distributor *d,
360                 struct rte_mbuf **mbufs, unsigned max_mbufs)
361 {
362         struct rte_distributor_returned_pkts *returns = &d->returns;
363         unsigned retval = (max_mbufs < returns->count) ?
364                         max_mbufs : returns->count;
365         unsigned i;
366
367         for (i = 0; i < retval; i++) {
368                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
369                 mbufs[i] = returns->mbufs[idx];
370         }
371         returns->start += i;
372         returns->count -= i;
373
374         return retval;
375 }
376
377 /* return the number of packets in-flight in a distributor, i.e. packets
378  * being workered on or queued up in a backlog. */
379 static inline unsigned
380 total_outstanding(const struct rte_distributor *d)
381 {
382         unsigned wkr, total_outstanding = 0;
383
384         for (wkr = 0; wkr < d->num_workers; wkr++)
385                 total_outstanding += d->backlog[wkr].count +
386                                 !!(d->in_flight_tags[wkr]);
387         return total_outstanding;
388 }
389
390 /* flush the distributor, so that there are no outstanding packets in flight or
391  * queued up. */
392 int
393 rte_distributor_flush(struct rte_distributor *d)
394 {
395         const unsigned flushed = total_outstanding(d);
396
397         while (total_outstanding(d) > 0)
398                 rte_distributor_process(d, NULL, 0);
399
400         return flushed;
401 }
402
403 /* clears the internal returns array in the distributor */
404 void
405 rte_distributor_clear_returns(struct rte_distributor *d)
406 {
407         d->returns.start = d->returns.count = 0;
408 #ifndef __OPTIMIZE__
409         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
410 #endif
411 }
412
413 /* creates a distributor instance */
414 struct rte_distributor *
415 rte_distributor_create(const char *name,
416                 unsigned socket_id,
417                 unsigned num_workers)
418 {
419         struct rte_distributor *d;
420         struct rte_distributor_list *distributor_list;
421         char mz_name[RTE_MEMZONE_NAMESIZE];
422         const struct rte_memzone *mz;
423
424         /* compilation-time checks */
425         RTE_BUILD_BUG_ON((sizeof(*d) & CACHE_LINE_MASK) != 0);
426         RTE_BUILD_BUG_ON((RTE_MAX_LCORE & 7) != 0);
427
428         if (name == NULL || num_workers >= RTE_MAX_LCORE) {
429                 rte_errno = EINVAL;
430                 return NULL;
431         }
432
433         /* check that we have an initialised tail queue */
434         distributor_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_DISTRIBUTOR,
435                                 rte_distributor_list);
436         if (distributor_list == NULL) {
437                 rte_errno = E_RTE_NO_TAILQ;
438                 return NULL;
439         }
440
441         snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
442         mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
443         if (mz == NULL) {
444                 rte_errno = ENOMEM;
445                 return NULL;
446         }
447
448         d = mz->addr;
449         snprintf(d->name, sizeof(d->name), "%s", name);
450         d->num_workers = num_workers;
451
452         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
453         TAILQ_INSERT_TAIL(distributor_list, d, next);
454         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
455
456         return d;
457 }