4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/queue.h>
37 #include <rte_memory.h>
38 #include <rte_cycles.h>
39 #include <rte_memzone.h>
40 #include <rte_errno.h>
41 #include <rte_string_fns.h>
42 #include <rte_eal_memconfig.h>
43 #include <rte_compat.h>
44 #include "rte_distributor_private.h"
45 #include "rte_distributor_next.h"
46 #include "rte_distributor_v20.h"
48 TAILQ_HEAD(rte_dist_burst_list, rte_distributor_v1705);
50 static struct rte_tailq_elem rte_dist_burst_tailq = {
51 .name = "RTE_DIST_BURST",
53 EAL_REGISTER_TAILQ(rte_dist_burst_tailq)
55 /**** APIs called by workers ****/
57 /**** Burst Packet APIs called by workers ****/
60 rte_distributor_request_pkt_v1705(struct rte_distributor_v1705 *d,
61 unsigned int worker_id, struct rte_mbuf **oldpkt,
64 struct rte_distributor_buffer_v1705 *buf = &(d->bufs[worker_id]);
67 volatile int64_t *retptr64;
69 if (unlikely(d->alg_type == RTE_DIST_ALG_SINGLE)) {
70 rte_distributor_request_pkt(d->d_v20,
71 worker_id, oldpkt[0]);
75 retptr64 = &(buf->retptr64[0]);
76 /* Spin while handshake bits are set (scheduler clears it) */
77 while (unlikely(*retptr64 & RTE_DISTRIB_GET_BUF)) {
79 uint64_t t = rte_rdtsc()+100;
81 while (rte_rdtsc() < t)
86 * OK, if we've got here, then the scheduler has just cleared the
87 * handshake bits. Populate the retptrs with returning packets.
90 for (i = count; i < RTE_DIST_BURST_SIZE; i++)
93 /* Set Return bit for each packet returned */
94 for (i = count; i-- > 0; )
96 (((int64_t)(uintptr_t)(oldpkt[i])) <<
97 RTE_DISTRIB_FLAG_BITS) | RTE_DISTRIB_RETURN_BUF;
100 * Finally, set the GET_BUF to signal to distributor that cache
101 * line is ready for processing
103 *retptr64 |= RTE_DISTRIB_GET_BUF;
107 rte_distributor_poll_pkt_v1705(struct rte_distributor_v1705 *d,
108 unsigned int worker_id, struct rte_mbuf **pkts)
110 struct rte_distributor_buffer_v1705 *buf = &d->bufs[worker_id];
115 if (unlikely(d->alg_type == RTE_DIST_ALG_SINGLE)) {
116 pkts[0] = rte_distributor_poll_pkt(d->d_v20, worker_id);
117 return (pkts[0]) ? 1 : 0;
120 /* If bit is set, return */
121 if (buf->bufptr64[0] & RTE_DISTRIB_GET_BUF)
124 /* since bufptr64 is signed, this should be an arithmetic shift */
125 for (i = 0; i < RTE_DIST_BURST_SIZE; i++) {
126 if (likely(buf->bufptr64[i] & RTE_DISTRIB_VALID_BUF)) {
127 ret = buf->bufptr64[i] >> RTE_DISTRIB_FLAG_BITS;
128 pkts[count++] = (struct rte_mbuf *)((uintptr_t)(ret));
133 * so now we've got the contents of the cacheline into an array of
134 * mbuf pointers, so toggle the bit so scheduler can start working
135 * on the next cacheline while we're working.
137 buf->bufptr64[0] |= RTE_DISTRIB_GET_BUF;
143 rte_distributor_get_pkt_v1705(struct rte_distributor_v1705 *d,
144 unsigned int worker_id, struct rte_mbuf **pkts,
145 struct rte_mbuf **oldpkt, unsigned int return_count)
149 if (unlikely(d->alg_type == RTE_DIST_ALG_SINGLE)) {
150 if (return_count <= 1) {
151 pkts[0] = rte_distributor_get_pkt(d->d_v20,
152 worker_id, oldpkt[0]);
153 return (pkts[0]) ? 1 : 0;
158 rte_distributor_request_pkt_v1705(d, worker_id, oldpkt, return_count);
160 count = rte_distributor_poll_pkt_v1705(d, worker_id, pkts);
161 while (count == -1) {
162 uint64_t t = rte_rdtsc() + 100;
164 while (rte_rdtsc() < t)
167 count = rte_distributor_poll_pkt_v1705(d, worker_id, pkts);
173 rte_distributor_return_pkt_v1705(struct rte_distributor_v1705 *d,
174 unsigned int worker_id, struct rte_mbuf **oldpkt, int num)
176 struct rte_distributor_buffer_v1705 *buf = &d->bufs[worker_id];
179 if (unlikely(d->alg_type == RTE_DIST_ALG_SINGLE)) {
181 return rte_distributor_return_pkt(d->d_v20,
182 worker_id, oldpkt[0]);
187 for (i = 0; i < RTE_DIST_BURST_SIZE; i++)
188 /* Switch off the return bit first */
189 buf->retptr64[i] &= ~RTE_DISTRIB_RETURN_BUF;
191 for (i = num; i-- > 0; )
192 buf->retptr64[i] = (((int64_t)(uintptr_t)oldpkt[i]) <<
193 RTE_DISTRIB_FLAG_BITS) | RTE_DISTRIB_RETURN_BUF;
195 /* set the GET_BUF but even if we got no returns */
196 buf->retptr64[0] |= RTE_DISTRIB_GET_BUF;
201 /**** APIs called on distributor core ***/
203 /* stores a packet returned from a worker inside the returns array */
205 store_return(uintptr_t oldbuf, struct rte_distributor_v1705 *d,
206 unsigned int *ret_start, unsigned int *ret_count)
210 /* store returns in a circular buffer */
211 d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
213 *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK);
214 *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK);
218 * Match then flow_ids (tags) of the incoming packets to the flow_ids
219 * of the inflight packets (both inflight on the workers and in each worker
220 * backlog). This will then allow us to pin those packets to the relevant
221 * workers to give us our atomic flow pinning.
224 find_match_scalar(struct rte_distributor_v1705 *d,
226 uint16_t *output_ptr)
228 struct rte_distributor_backlog *bl;
233 * 1. Loop through all worker ID's
234 * 2. Compare the current inflights to the incoming tags
235 * 3. Compare the current backlog to the incoming tags
236 * 4. Add any matches to the output
239 for (j = 0 ; j < RTE_DIST_BURST_SIZE; j++)
242 for (i = 0; i < d->num_workers; i++) {
245 for (j = 0; j < RTE_DIST_BURST_SIZE ; j++)
246 for (w = 0; w < RTE_DIST_BURST_SIZE; w++)
247 if (d->in_flight_tags[i][j] == data_ptr[w]) {
251 for (j = 0; j < RTE_DIST_BURST_SIZE; j++)
252 for (w = 0; w < RTE_DIST_BURST_SIZE; w++)
253 if (bl->tags[j] == data_ptr[w]) {
260 * At this stage, the output contains 8 16-bit values, with
261 * each non-zero value containing the worker ID on which the
262 * corresponding flow is pinned to.
268 * When the handshake bits indicate that there are packets coming
269 * back from the worker, this function is called to copy and store
270 * the valid returned pointers (store_return).
273 handle_returns(struct rte_distributor_v1705 *d, unsigned int wkr)
275 struct rte_distributor_buffer_v1705 *buf = &(d->bufs[wkr]);
277 unsigned int ret_start = d->returns.start,
278 ret_count = d->returns.count;
279 unsigned int count = 0;
282 if (buf->retptr64[0] & RTE_DISTRIB_GET_BUF) {
283 for (i = 0; i < RTE_DIST_BURST_SIZE; i++) {
284 if (buf->retptr64[i] & RTE_DISTRIB_RETURN_BUF) {
285 oldbuf = ((uintptr_t)(buf->retptr64[i] >>
286 RTE_DISTRIB_FLAG_BITS));
287 /* store returns in a circular buffer */
288 store_return(oldbuf, d, &ret_start, &ret_count);
290 buf->retptr64[i] &= ~RTE_DISTRIB_RETURN_BUF;
293 d->returns.start = ret_start;
294 d->returns.count = ret_count;
295 /* Clear for the worker to populate with more returns */
296 buf->retptr64[0] = 0;
302 * This function releases a burst (cache line) to a worker.
303 * It is called from the process function when a cacheline is
304 * full to make room for more packets for that worker, or when
305 * all packets have been assigned to bursts and need to be flushed
307 * It also needs to wait for any outstanding packets from the worker
308 * before sending out new packets.
311 release(struct rte_distributor_v1705 *d, unsigned int wkr)
313 struct rte_distributor_buffer_v1705 *buf = &(d->bufs[wkr]);
316 while (!(d->bufs[wkr].bufptr64[0] & RTE_DISTRIB_GET_BUF))
319 handle_returns(d, wkr);
323 for (i = 0; i < d->backlog[wkr].count; i++) {
324 d->bufs[wkr].bufptr64[i] = d->backlog[wkr].pkts[i] |
325 RTE_DISTRIB_GET_BUF | RTE_DISTRIB_VALID_BUF;
326 d->in_flight_tags[wkr][i] = d->backlog[wkr].tags[i];
329 for ( ; i < RTE_DIST_BURST_SIZE ; i++) {
330 buf->bufptr64[i] = RTE_DISTRIB_GET_BUF;
331 d->in_flight_tags[wkr][i] = 0;
334 d->backlog[wkr].count = 0;
336 /* Clear the GET bit */
337 buf->bufptr64[0] &= ~RTE_DISTRIB_GET_BUF;
343 /* process a set of packets to distribute them to workers */
345 rte_distributor_process_v1705(struct rte_distributor_v1705 *d,
346 struct rte_mbuf **mbufs, unsigned int num_mbufs)
348 unsigned int next_idx = 0;
349 static unsigned int wkr;
350 struct rte_mbuf *next_mb = NULL;
351 int64_t next_value = 0;
352 uint16_t new_tag = 0;
353 uint16_t flows[RTE_DIST_BURST_SIZE] __rte_cache_aligned;
354 unsigned int i, j, w, wid;
356 if (d->alg_type == RTE_DIST_ALG_SINGLE) {
357 /* Call the old API */
358 return rte_distributor_process(d->d_v20, mbufs, num_mbufs);
361 if (unlikely(num_mbufs == 0)) {
362 /* Flush out all non-full cache-lines to workers. */
363 for (wid = 0 ; wid < d->num_workers; wid++) {
364 if ((d->bufs[wid].bufptr64[0] & RTE_DISTRIB_GET_BUF)) {
366 handle_returns(d, wid);
372 while (next_idx < num_mbufs) {
373 uint16_t matches[RTE_DIST_BURST_SIZE];
376 if (d->bufs[wkr].bufptr64[0] & RTE_DISTRIB_GET_BUF)
377 d->bufs[wkr].count = 0;
379 if ((num_mbufs - next_idx) < RTE_DIST_BURST_SIZE)
380 pkts = num_mbufs - next_idx;
382 pkts = RTE_DIST_BURST_SIZE;
384 for (i = 0; i < pkts; i++) {
385 if (mbufs[next_idx + i]) {
386 /* flows have to be non-zero */
387 flows[i] = mbufs[next_idx + i]->hash.usr | 1;
391 for (; i < RTE_DIST_BURST_SIZE; i++)
394 switch (d->dist_match_fn) {
395 case RTE_DIST_MATCH_VECTOR:
396 find_match_vec(d, &flows[0], &matches[0]);
399 find_match_scalar(d, &flows[0], &matches[0]);
403 * Matches array now contain the intended worker ID (+1) of
404 * the incoming packets. Any zeroes need to be assigned
408 for (j = 0; j < pkts; j++) {
410 next_mb = mbufs[next_idx++];
411 next_value = (((int64_t)(uintptr_t)next_mb) <<
412 RTE_DISTRIB_FLAG_BITS);
414 * User is advocated to set tag vaue for each
415 * mbuf before calling rte_distributor_process.
416 * User defined tags are used to identify flows,
419 /* flows MUST be non-zero */
420 new_tag = (uint16_t)(next_mb->hash.usr) | 1;
423 * Uncommenting the next line will cause the find_match
424 * function to be optimised out, making this function
425 * do parallel (non-atomic) distribution
427 /* matches[j] = 0; */
430 struct rte_distributor_backlog *bl =
431 &d->backlog[matches[j]-1];
432 if (unlikely(bl->count ==
433 RTE_DIST_BURST_SIZE)) {
434 release(d, matches[j]-1);
437 /* Add to worker that already has flow */
438 unsigned int idx = bl->count++;
440 bl->tags[idx] = new_tag;
441 bl->pkts[idx] = next_value;
444 struct rte_distributor_backlog *bl =
446 if (unlikely(bl->count ==
447 RTE_DIST_BURST_SIZE)) {
451 /* Add to current worker worker */
452 unsigned int idx = bl->count++;
454 bl->tags[idx] = new_tag;
455 bl->pkts[idx] = next_value;
457 * Now that we've just added an unpinned flow
458 * to a worker, we need to ensure that all
459 * other packets with that same flow will go
460 * to the same worker in this burst.
462 for (w = j; w < pkts; w++)
463 if (flows[w] == new_tag)
468 if (wkr >= d->num_workers)
472 /* Flush out all non-full cache-lines to workers. */
473 for (wid = 0 ; wid < d->num_workers; wid++)
474 if ((d->bufs[wid].bufptr64[0] & RTE_DISTRIB_GET_BUF))
480 /* return to the caller, packets returned from workers */
482 rte_distributor_returned_pkts_v1705(struct rte_distributor_v1705 *d,
483 struct rte_mbuf **mbufs, unsigned int max_mbufs)
485 struct rte_distributor_returned_pkts *returns = &d->returns;
486 unsigned int retval = (max_mbufs < returns->count) ?
487 max_mbufs : returns->count;
490 if (d->alg_type == RTE_DIST_ALG_SINGLE) {
491 /* Call the old API */
492 return rte_distributor_returned_pkts(d->d_v20,
496 for (i = 0; i < retval; i++) {
497 unsigned int idx = (returns->start + i) &
498 RTE_DISTRIB_RETURNS_MASK;
500 mbufs[i] = returns->mbufs[idx];
509 * Return the number of packets in-flight in a distributor, i.e. packets
510 * being workered on or queued up in a backlog.
512 static inline unsigned int
513 total_outstanding(const struct rte_distributor_v1705 *d)
515 unsigned int wkr, total_outstanding = 0;
517 for (wkr = 0; wkr < d->num_workers; wkr++)
518 total_outstanding += d->backlog[wkr].count;
520 return total_outstanding;
524 * Flush the distributor, so that there are no outstanding packets in flight or
528 rte_distributor_flush_v1705(struct rte_distributor_v1705 *d)
530 const unsigned int flushed = total_outstanding(d);
533 if (d->alg_type == RTE_DIST_ALG_SINGLE) {
534 /* Call the old API */
535 return rte_distributor_flush(d->d_v20);
538 while (total_outstanding(d) > 0)
539 rte_distributor_process_v1705(d, NULL, 0);
542 * Send empty burst to all workers to allow them to exit
543 * gracefully, should they need to.
545 rte_distributor_process_v1705(d, NULL, 0);
547 for (wkr = 0; wkr < d->num_workers; wkr++)
548 handle_returns(d, wkr);
553 /* clears the internal returns array in the distributor */
555 rte_distributor_clear_returns_v1705(struct rte_distributor_v1705 *d)
559 if (d->alg_type == RTE_DIST_ALG_SINGLE) {
560 /* Call the old API */
561 rte_distributor_clear_returns(d->d_v20);
564 /* throw away returns, so workers can exit */
565 for (wkr = 0; wkr < d->num_workers; wkr++)
566 d->bufs[wkr].retptr64[0] = 0;
569 /* creates a distributor instance */
570 struct rte_distributor_v1705 *
571 rte_distributor_create_v1705(const char *name,
572 unsigned int socket_id,
573 unsigned int num_workers,
574 unsigned int alg_type)
576 struct rte_distributor_v1705 *d;
577 struct rte_dist_burst_list *dist_burst_list;
578 char mz_name[RTE_MEMZONE_NAMESIZE];
579 const struct rte_memzone *mz;
582 /* TODO Reorganise function properly around RTE_DIST_ALG_SINGLE/BURST */
584 /* compilation-time checks */
585 RTE_BUILD_BUG_ON((sizeof(*d) & RTE_CACHE_LINE_MASK) != 0);
586 RTE_BUILD_BUG_ON((RTE_DISTRIB_MAX_WORKERS & 7) != 0);
588 if (alg_type == RTE_DIST_ALG_SINGLE) {
589 d = malloc(sizeof(struct rte_distributor_v1705));
590 d->d_v20 = rte_distributor_create(name,
591 socket_id, num_workers);
592 if (d->d_v20 == NULL) {
593 /* rte_errno will have been set */
596 d->alg_type = alg_type;
600 if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
605 snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
606 mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
613 snprintf(d->name, sizeof(d->name), "%s", name);
614 d->num_workers = num_workers;
615 d->alg_type = alg_type;
617 #if defined(RTE_ARCH_X86)
618 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_2))
619 d->dist_match_fn = RTE_DIST_MATCH_VECTOR;
622 d->dist_match_fn = RTE_DIST_MATCH_SCALAR;
625 * Set up the backog tags so they're pointing at the second cache
626 * line for performance during flow matching
628 for (i = 0 ; i < num_workers ; i++)
629 d->backlog[i].tags = &d->in_flight_tags[i][RTE_DIST_BURST_SIZE];
631 dist_burst_list = RTE_TAILQ_CAST(rte_dist_burst_tailq.head,
632 rte_dist_burst_list);
635 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
636 TAILQ_INSERT_TAIL(dist_burst_list, d, next);
637 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);