4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <sys/queue.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"
46 #define RTE_DISTRIB_PREFIX "DT_"
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 */
58 #define RTE_DISTRIB_BACKLOG_SIZE 8
59 #define RTE_DISTRIB_BACKLOG_MASK (RTE_DISTRIB_BACKLOG_SIZE - 1)
61 #define RTE_DISTRIB_MAX_RETURNS 128
62 #define RTE_DISTRIB_RETURNS_MASK (RTE_DISTRIB_MAX_RETURNS - 1)
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
69 #define RTE_DISTRIB_MAX_WORKERS 64
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.
78 union rte_distributor_buffer {
79 volatile int64_t bufptr64;
80 char pad[CACHE_LINE_SIZE*3];
81 } __rte_cache_aligned;
83 struct rte_distributor_backlog {
86 int64_t pkts[RTE_DISTRIB_BACKLOG_SIZE];
89 struct rte_distributor_returned_pkts {
92 struct rte_mbuf *mbufs[RTE_DISTRIB_MAX_RETURNS];
95 struct rte_distributor {
96 TAILQ_ENTRY(rte_distributor) next; /**< Next in list. */
98 char name[RTE_DISTRIBUTOR_NAMESIZE]; /**< Name of the ring. */
99 unsigned num_workers; /**< Number of workers polling */
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.
109 struct rte_distributor_backlog backlog[RTE_DISTRIB_MAX_WORKERS];
111 union rte_distributor_buffer bufs[RTE_DISTRIB_MAX_WORKERS];
113 struct rte_distributor_returned_pkts returns;
116 TAILQ_HEAD(rte_distributor_list, rte_distributor);
118 /**** APIs called by workers ****/
121 rte_distributor_request_pkt(struct rte_distributor *d,
122 unsigned worker_id, struct rte_mbuf *oldpkt)
124 union rte_distributor_buffer *buf = &d->bufs[worker_id];
125 int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
126 | RTE_DISTRIB_GET_BUF;
127 while (unlikely(buf->bufptr64 & RTE_DISTRIB_FLAGS_MASK))
133 rte_distributor_poll_pkt(struct rte_distributor *d,
136 union rte_distributor_buffer *buf = &d->bufs[worker_id];
137 if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
140 /* since bufptr64 is signed, this should be an arithmetic shift */
141 int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
142 return (struct rte_mbuf *)((uintptr_t)ret);
146 rte_distributor_get_pkt(struct rte_distributor *d,
147 unsigned worker_id, struct rte_mbuf *oldpkt)
149 struct rte_mbuf *ret;
150 rte_distributor_request_pkt(d, worker_id, oldpkt);
151 while ((ret = rte_distributor_poll_pkt(d, worker_id)) == NULL)
157 rte_distributor_return_pkt(struct rte_distributor *d,
158 unsigned worker_id, struct rte_mbuf *oldpkt)
160 union rte_distributor_buffer *buf = &d->bufs[worker_id];
161 uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
162 | RTE_DISTRIB_RETURN_BUF;
167 /**** APIs called on distributor core ***/
169 /* as name suggests, adds a packet to the backlog for a particular worker */
171 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
173 if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
176 bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
181 /* takes the next packet for a worker off the backlog */
183 backlog_pop(struct rte_distributor_backlog *bl)
186 return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
189 /* stores a packet returned from a worker inside the returns array */
191 store_return(uintptr_t oldbuf, struct rte_distributor *d,
192 unsigned *ret_start, unsigned *ret_count)
194 /* store returns in a circular buffer - code is branch-free */
195 d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
197 *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
198 *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
202 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
204 d->in_flight_tags[wkr] = 0;
205 d->in_flight_bitmask &= ~(1UL << wkr);
206 d->bufs[wkr].bufptr64 = 0;
207 if (unlikely(d->backlog[wkr].count != 0)) {
208 /* On return of a packet, we need to move the
209 * queued packets for this core elsewhere.
210 * Easiest solution is to set things up for
211 * a recursive call. That will cause those
212 * packets to be queued up for the next free
213 * core, i.e. it will return as soon as a
214 * core becomes free to accept the first
215 * packet, as subsequent ones will be added to
216 * the backlog for that core.
218 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
220 struct rte_distributor_backlog *bl = &d->backlog[wkr];
222 for (i = 0; i < bl->count; i++) {
223 unsigned idx = (bl->start + i) &
224 RTE_DISTRIB_BACKLOG_MASK;
225 pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
226 RTE_DISTRIB_FLAG_BITS));
229 * Note that the tags were set before first level call
230 * to rte_distributor_process.
232 rte_distributor_process(d, pkts, i);
233 bl->count = bl->start = 0;
237 /* this function is called when process() fn is called without any new
238 * packets. It goes through all the workers and clears any returned packets
239 * to do a partial flush.
242 process_returns(struct rte_distributor *d)
245 unsigned flushed = 0;
246 unsigned ret_start = d->returns.start,
247 ret_count = d->returns.count;
249 for (wkr = 0; wkr < d->num_workers; wkr++) {
251 const int64_t data = d->bufs[wkr].bufptr64;
252 uintptr_t oldbuf = 0;
254 if (data & RTE_DISTRIB_GET_BUF) {
256 if (d->backlog[wkr].count)
257 d->bufs[wkr].bufptr64 =
258 backlog_pop(&d->backlog[wkr]);
260 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
261 d->in_flight_tags[wkr] = 0;
262 d->in_flight_bitmask &= ~(1UL << wkr);
264 oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
265 } else if (data & RTE_DISTRIB_RETURN_BUF) {
266 handle_worker_shutdown(d, wkr);
267 oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
270 store_return(oldbuf, d, &ret_start, &ret_count);
273 d->returns.start = ret_start;
274 d->returns.count = ret_count;
279 /* process a set of packets to distribute them to workers */
281 rte_distributor_process(struct rte_distributor *d,
282 struct rte_mbuf **mbufs, unsigned num_mbufs)
284 unsigned next_idx = 0;
286 struct rte_mbuf *next_mb = NULL;
287 int64_t next_value = 0;
288 uint32_t new_tag = 0;
289 unsigned ret_start = d->returns.start,
290 ret_count = d->returns.count;
292 if (unlikely(num_mbufs == 0))
293 return process_returns(d);
295 while (next_idx < num_mbufs || next_mb != NULL) {
297 int64_t data = d->bufs[wkr].bufptr64;
298 uintptr_t oldbuf = 0;
301 next_mb = mbufs[next_idx++];
302 next_value = (((int64_t)(uintptr_t)next_mb)
303 << RTE_DISTRIB_FLAG_BITS);
305 * User is advocated to set tag vaue for each
306 * mbuf before calling rte_distributor_process.
307 * User defined tags are used to identify flows,
310 new_tag = next_mb->hash.usr;
313 * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64
314 * then the size of match has to be expanded.
319 * to scan for a match use "xor" and "not" to get a 0/1
320 * value, then use shifting to merge to single "match"
321 * variable, where a one-bit indicates a match for the
322 * worker given by the bit-position
324 for (i = 0; i < d->num_workers; i++)
325 match |= (!(d->in_flight_tags[i] ^ new_tag)
328 /* Only turned-on bits are considered as match */
329 match &= d->in_flight_bitmask;
333 unsigned worker = __builtin_ctzl(match);
334 if (add_to_backlog(&d->backlog[worker],
340 if ((data & RTE_DISTRIB_GET_BUF) &&
341 (d->backlog[wkr].count || next_mb)) {
343 if (d->backlog[wkr].count)
344 d->bufs[wkr].bufptr64 =
345 backlog_pop(&d->backlog[wkr]);
348 d->bufs[wkr].bufptr64 = next_value;
349 d->in_flight_tags[wkr] = new_tag;
350 d->in_flight_bitmask |= (1UL << wkr);
353 oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
354 } else if (data & RTE_DISTRIB_RETURN_BUF) {
355 handle_worker_shutdown(d, wkr);
356 oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
359 /* store returns in a circular buffer */
360 store_return(oldbuf, d, &ret_start, &ret_count);
362 if (++wkr == d->num_workers)
365 /* to finish, check all workers for backlog and schedule work for them
366 * if they are ready */
367 for (wkr = 0; wkr < d->num_workers; wkr++)
368 if (d->backlog[wkr].count &&
369 (d->bufs[wkr].bufptr64 & RTE_DISTRIB_GET_BUF)) {
371 int64_t oldbuf = d->bufs[wkr].bufptr64 >>
372 RTE_DISTRIB_FLAG_BITS;
373 store_return(oldbuf, d, &ret_start, &ret_count);
375 d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
378 d->returns.start = ret_start;
379 d->returns.count = ret_count;
383 /* return to the caller, packets returned from workers */
385 rte_distributor_returned_pkts(struct rte_distributor *d,
386 struct rte_mbuf **mbufs, unsigned max_mbufs)
388 struct rte_distributor_returned_pkts *returns = &d->returns;
389 unsigned retval = (max_mbufs < returns->count) ?
390 max_mbufs : returns->count;
393 for (i = 0; i < retval; i++) {
394 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
395 mbufs[i] = returns->mbufs[idx];
403 /* return the number of packets in-flight in a distributor, i.e. packets
404 * being workered on or queued up in a backlog. */
405 static inline unsigned
406 total_outstanding(const struct rte_distributor *d)
408 unsigned wkr, total_outstanding;
410 total_outstanding = __builtin_popcountl(d->in_flight_bitmask);
412 for (wkr = 0; wkr < d->num_workers; wkr++)
413 total_outstanding += d->backlog[wkr].count;
415 return total_outstanding;
418 /* flush the distributor, so that there are no outstanding packets in flight or
421 rte_distributor_flush(struct rte_distributor *d)
423 const unsigned flushed = total_outstanding(d);
425 while (total_outstanding(d) > 0)
426 rte_distributor_process(d, NULL, 0);
431 /* clears the internal returns array in the distributor */
433 rte_distributor_clear_returns(struct rte_distributor *d)
435 d->returns.start = d->returns.count = 0;
437 memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
441 /* creates a distributor instance */
442 struct rte_distributor *
443 rte_distributor_create(const char *name,
445 unsigned num_workers)
447 struct rte_distributor *d;
448 struct rte_distributor_list *distributor_list;
449 char mz_name[RTE_MEMZONE_NAMESIZE];
450 const struct rte_memzone *mz;
452 /* compilation-time checks */
453 RTE_BUILD_BUG_ON((sizeof(*d) & CACHE_LINE_MASK) != 0);
454 RTE_BUILD_BUG_ON((RTE_DISTRIB_MAX_WORKERS & 7) != 0);
455 RTE_BUILD_BUG_ON(RTE_DISTRIB_MAX_WORKERS >
456 sizeof(d->in_flight_bitmask) * CHAR_BIT);
458 if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
463 /* check that we have an initialised tail queue */
464 distributor_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_DISTRIBUTOR,
465 rte_distributor_list);
466 if (distributor_list == NULL) {
467 rte_errno = E_RTE_NO_TAILQ;
471 snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
472 mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
479 snprintf(d->name, sizeof(d->name), "%s", name);
480 d->num_workers = num_workers;
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);