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 * 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.
71 union rte_distributor_buffer {
72 volatile int64_t bufptr64;
73 char pad[CACHE_LINE_SIZE*3];
74 } __rte_cache_aligned;
76 struct rte_distributor_backlog {
79 int64_t pkts[RTE_DISTRIB_BACKLOG_SIZE];
82 struct rte_distributor_returned_pkts {
85 struct rte_mbuf *mbufs[RTE_DISTRIB_MAX_RETURNS];
88 struct rte_distributor {
89 TAILQ_ENTRY(rte_distributor) next; /**< Next in list. */
91 char name[RTE_DISTRIBUTOR_NAMESIZE]; /**< Name of the ring. */
92 unsigned num_workers; /**< Number of workers polling */
94 uint32_t in_flight_tags[RTE_MAX_LCORE];
95 struct rte_distributor_backlog backlog[RTE_MAX_LCORE];
97 union rte_distributor_buffer bufs[RTE_MAX_LCORE];
99 struct rte_distributor_returned_pkts returns;
102 TAILQ_HEAD(rte_distributor_list, rte_distributor);
104 /**** APIs called by workers ****/
107 rte_distributor_get_pkt(struct rte_distributor *d,
108 unsigned worker_id, struct rte_mbuf *oldpkt)
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))
116 while (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
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);
124 rte_distributor_return_pkt(struct rte_distributor *d,
125 unsigned worker_id, struct rte_mbuf *oldpkt)
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;
134 /**** APIs called on distributor core ***/
136 /* as name suggests, adds a packet to the backlog for a particular worker */
138 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
140 if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
143 bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
148 /* takes the next packet for a worker off the backlog */
150 backlog_pop(struct rte_distributor_backlog *bl)
153 return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
156 /* stores a packet returned from a worker inside the returns array */
158 store_return(uintptr_t oldbuf, struct rte_distributor *d,
159 unsigned *ret_start, unsigned *ret_count)
161 /* store returns in a circular buffer - code is branch-free */
162 d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
164 *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
165 *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
169 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
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.
184 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
186 struct rte_distributor_backlog *bl = &d->backlog[wkr];
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));
195 rte_distributor_process(d, pkts, i);
196 bl->count = bl->start = 0;
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.
205 process_returns(struct rte_distributor *d)
208 unsigned flushed = 0;
209 unsigned ret_start = d->returns.start,
210 ret_count = d->returns.count;
212 for (wkr = 0; wkr < d->num_workers; wkr++) {
214 const int64_t data = d->bufs[wkr].bufptr64;
215 uintptr_t oldbuf = 0;
217 if (data & RTE_DISTRIB_GET_BUF) {
219 if (d->backlog[wkr].count)
220 d->bufs[wkr].bufptr64 =
221 backlog_pop(&d->backlog[wkr]);
223 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
224 d->in_flight_tags[wkr] = 0;
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;
232 store_return(oldbuf, d, &ret_start, &ret_count);
235 d->returns.start = ret_start;
236 d->returns.count = ret_count;
241 /* process a set of packets to distribute them to workers */
243 rte_distributor_process(struct rte_distributor *d,
244 struct rte_mbuf **mbufs, unsigned num_mbufs)
246 unsigned next_idx = 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;
254 if (unlikely(num_mbufs == 0))
255 return process_returns(d);
257 while (next_idx < num_mbufs || next_mb != NULL) {
259 int64_t data = d->bufs[wkr].bufptr64;
260 uintptr_t oldbuf = 0;
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);
270 for (i = 0; i < d->num_workers; i++)
271 match |= (!(d->in_flight_tags[i] ^ new_tag)
276 unsigned worker = __builtin_ctz(match);
277 if (add_to_backlog(&d->backlog[worker],
283 if ((data & RTE_DISTRIB_GET_BUF) &&
284 (d->backlog[wkr].count || next_mb)) {
286 if (d->backlog[wkr].count)
287 d->bufs[wkr].bufptr64 =
288 backlog_pop(&d->backlog[wkr]);
291 d->bufs[wkr].bufptr64 = next_value;
292 d->in_flight_tags[wkr] = new_tag;
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;
301 /* store returns in a circular buffer */
302 store_return(oldbuf, d, &ret_start, &ret_count);
304 if (++wkr == d->num_workers)
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)) {
313 int64_t oldbuf = d->bufs[wkr].bufptr64 >>
314 RTE_DISTRIB_FLAG_BITS;
315 store_return(oldbuf, d, &ret_start, &ret_count);
317 d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
320 d->returns.start = ret_start;
321 d->returns.count = ret_count;
325 /* return to the caller, packets returned from workers */
327 rte_distributor_returned_pkts(struct rte_distributor *d,
328 struct rte_mbuf **mbufs, unsigned max_mbufs)
330 struct rte_distributor_returned_pkts *returns = &d->returns;
331 unsigned retval = (max_mbufs < returns->count) ?
332 max_mbufs : returns->count;
335 for (i = 0; i < retval; i++) {
336 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
337 mbufs[i] = returns->mbufs[idx];
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)
350 unsigned wkr, total_outstanding = 0;
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;
358 /* flush the distributor, so that there are no outstanding packets in flight or
361 rte_distributor_flush(struct rte_distributor *d)
363 const unsigned flushed = total_outstanding(d);
365 while (total_outstanding(d) > 0)
366 rte_distributor_process(d, NULL, 0);
371 /* clears the internal returns array in the distributor */
373 rte_distributor_clear_returns(struct rte_distributor *d)
375 d->returns.start = d->returns.count = 0;
377 memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
381 /* creates a distributor instance */
382 struct rte_distributor *
383 rte_distributor_create(const char *name,
385 unsigned num_workers)
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;
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);
396 if (name == NULL || num_workers >= RTE_MAX_LCORE) {
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;
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);
417 rte_snprintf(d->name, sizeof(d->name), "%s", name);
418 d->num_workers = num_workers;
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);