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_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>
46 #include "rte_distributor_v20.h"
47 #include "rte_distributor_private.h"
49 TAILQ_HEAD(rte_distributor_list, rte_distributor_v20);
51 static struct rte_tailq_elem rte_distributor_tailq = {
52 .name = "RTE_DISTRIBUTOR",
54 EAL_REGISTER_TAILQ(rte_distributor_tailq)
56 /**** APIs called by workers ****/
59 rte_distributor_request_pkt_v20(struct rte_distributor_v20 *d,
60 unsigned worker_id, struct rte_mbuf *oldpkt)
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))
69 VERSION_SYMBOL(rte_distributor_request_pkt, _v20, 2.0);
72 rte_distributor_poll_pkt_v20(struct rte_distributor_v20 *d,
75 union rte_distributor_buffer_v20 *buf = &d->bufs[worker_id];
76 if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
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);
83 VERSION_SYMBOL(rte_distributor_poll_pkt, _v20, 2.0);
86 rte_distributor_get_pkt_v20(struct rte_distributor_v20 *d,
87 unsigned worker_id, struct rte_mbuf *oldpkt)
90 rte_distributor_request_pkt_v20(d, worker_id, oldpkt);
91 while ((ret = rte_distributor_poll_pkt_v20(d, worker_id)) == NULL)
95 VERSION_SYMBOL(rte_distributor_get_pkt, _v20, 2.0);
98 rte_distributor_return_pkt_v20(struct rte_distributor_v20 *d,
99 unsigned worker_id, struct rte_mbuf *oldpkt)
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;
107 VERSION_SYMBOL(rte_distributor_return_pkt, _v20, 2.0);
109 /**** APIs called on distributor core ***/
111 /* as name suggests, adds a packet to the backlog for a particular worker */
113 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
115 if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
118 bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
123 /* takes the next packet for a worker off the backlog */
125 backlog_pop(struct rte_distributor_backlog *bl)
128 return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
131 /* stores a packet returned from a worker inside the returns array */
133 store_return(uintptr_t oldbuf, struct rte_distributor_v20 *d,
134 unsigned *ret_start, unsigned *ret_count)
136 /* store returns in a circular buffer - code is branch-free */
137 d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
139 *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
140 *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
144 handle_worker_shutdown(struct rte_distributor_v20 *d, unsigned int wkr)
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.
160 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
162 struct rte_distributor_backlog *bl = &d->backlog[wkr];
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));
171 * Note that the tags were set before first level call
172 * to rte_distributor_process.
174 rte_distributor_process_v20(d, pkts, i);
175 bl->count = bl->start = 0;
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.
184 process_returns(struct rte_distributor_v20 *d)
187 unsigned flushed = 0;
188 unsigned ret_start = d->returns.start,
189 ret_count = d->returns.count;
191 for (wkr = 0; wkr < d->num_workers; wkr++) {
193 const int64_t data = d->bufs[wkr].bufptr64;
194 uintptr_t oldbuf = 0;
196 if (data & RTE_DISTRIB_GET_BUF) {
198 if (d->backlog[wkr].count)
199 d->bufs[wkr].bufptr64 =
200 backlog_pop(&d->backlog[wkr]);
202 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
203 d->in_flight_tags[wkr] = 0;
204 d->in_flight_bitmask &= ~(1UL << wkr);
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;
212 store_return(oldbuf, d, &ret_start, &ret_count);
215 d->returns.start = ret_start;
216 d->returns.count = ret_count;
221 /* process a set of packets to distribute them to workers */
223 rte_distributor_process_v20(struct rte_distributor_v20 *d,
224 struct rte_mbuf **mbufs, unsigned num_mbufs)
226 unsigned next_idx = 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;
234 if (unlikely(num_mbufs == 0))
235 return process_returns(d);
237 while (next_idx < num_mbufs || next_mb != NULL) {
239 int64_t data = d->bufs[wkr].bufptr64;
240 uintptr_t oldbuf = 0;
243 next_mb = mbufs[next_idx++];
244 next_value = (((int64_t)(uintptr_t)next_mb)
245 << RTE_DISTRIB_FLAG_BITS);
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,
252 new_tag = next_mb->hash.usr;
255 * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64
256 * then the size of match has to be expanded.
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
266 for (i = 0; i < d->num_workers; i++)
267 match |= (!(d->in_flight_tags[i] ^ new_tag)
270 /* Only turned-on bits are considered as match */
271 match &= d->in_flight_bitmask;
275 unsigned worker = __builtin_ctzl(match);
276 if (add_to_backlog(&d->backlog[worker],
282 if ((data & RTE_DISTRIB_GET_BUF) &&
283 (d->backlog[wkr].count || next_mb)) {
285 if (d->backlog[wkr].count)
286 d->bufs[wkr].bufptr64 =
287 backlog_pop(&d->backlog[wkr]);
290 d->bufs[wkr].bufptr64 = next_value;
291 d->in_flight_tags[wkr] = new_tag;
292 d->in_flight_bitmask |= (1UL << wkr);
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;
324 VERSION_SYMBOL(rte_distributor_process, _v20, 2.0);
326 /* return to the caller, packets returned from workers */
328 rte_distributor_returned_pkts_v20(struct rte_distributor_v20 *d,
329 struct rte_mbuf **mbufs, unsigned max_mbufs)
331 struct rte_distributor_returned_pkts *returns = &d->returns;
332 unsigned retval = (max_mbufs < returns->count) ?
333 max_mbufs : returns->count;
336 for (i = 0; i < retval; i++) {
337 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
338 mbufs[i] = returns->mbufs[idx];
345 VERSION_SYMBOL(rte_distributor_returned_pkts, _v20, 2.0);
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)
352 unsigned wkr, total_outstanding;
354 total_outstanding = __builtin_popcountl(d->in_flight_bitmask);
356 for (wkr = 0; wkr < d->num_workers; wkr++)
357 total_outstanding += d->backlog[wkr].count;
359 return total_outstanding;
362 /* flush the distributor, so that there are no outstanding packets in flight or
365 rte_distributor_flush_v20(struct rte_distributor_v20 *d)
367 const unsigned flushed = total_outstanding(d);
369 while (total_outstanding(d) > 0)
370 rte_distributor_process_v20(d, NULL, 0);
374 VERSION_SYMBOL(rte_distributor_flush, _v20, 2.0);
376 /* clears the internal returns array in the distributor */
378 rte_distributor_clear_returns_v20(struct rte_distributor_v20 *d)
380 d->returns.start = d->returns.count = 0;
382 memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
385 VERSION_SYMBOL(rte_distributor_clear_returns, _v20, 2.0);
387 /* creates a distributor instance */
388 struct rte_distributor_v20 *
389 rte_distributor_create_v20(const char *name,
391 unsigned num_workers)
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;
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);
404 if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
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);
417 snprintf(d->name, sizeof(d->name), "%s", name);
418 d->num_workers = num_workers;
420 distributor_list = RTE_TAILQ_CAST(rte_distributor_tailq.head,
421 rte_distributor_list);
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);
429 VERSION_SYMBOL(rte_distributor_create, _v20, 2.0);