91d8824c64e9eed1dcf01515b832742ccf7ee8fa
[dpdk.git] / lib / librte_distributor / rte_distributor_single.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <sys/queue.h>
7 #include <string.h>
8 #include <rte_mbuf.h>
9 #include <rte_memory.h>
10 #include <rte_memzone.h>
11 #include <rte_errno.h>
12 #include <rte_function_versioning.h>
13 #include <rte_string_fns.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_pause.h>
16 #include <rte_tailq.h>
17
18 #include "rte_distributor_single.h"
19 #include "distributor_private.h"
20
21 TAILQ_HEAD(rte_distributor_list, rte_distributor_single);
22
23 static struct rte_tailq_elem rte_distributor_tailq = {
24         .name = "RTE_DISTRIBUTOR",
25 };
26 EAL_REGISTER_TAILQ(rte_distributor_tailq)
27
28 /**** APIs called by workers ****/
29
30 void
31 rte_distributor_request_pkt_single(struct rte_distributor_single *d,
32                 unsigned worker_id, struct rte_mbuf *oldpkt)
33 {
34         union rte_distributor_buffer_single *buf = &d->bufs[worker_id];
35         int64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
36                         | RTE_DISTRIB_GET_BUF;
37         while (unlikely(__atomic_load_n(&buf->bufptr64, __ATOMIC_RELAXED)
38                         & RTE_DISTRIB_FLAGS_MASK))
39                 rte_pause();
40
41         /* Sync with distributor on GET_BUF flag. */
42         __atomic_store_n(&(buf->bufptr64), req, __ATOMIC_RELEASE);
43 }
44
45 struct rte_mbuf *
46 rte_distributor_poll_pkt_single(struct rte_distributor_single *d,
47                 unsigned worker_id)
48 {
49         union rte_distributor_buffer_single *buf = &d->bufs[worker_id];
50         /* Sync with distributor. Acquire bufptr64. */
51         if (__atomic_load_n(&buf->bufptr64, __ATOMIC_ACQUIRE)
52                 & RTE_DISTRIB_GET_BUF)
53                 return NULL;
54
55         /* since bufptr64 is signed, this should be an arithmetic shift */
56         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
57         return (struct rte_mbuf *)((uintptr_t)ret);
58 }
59
60 struct rte_mbuf *
61 rte_distributor_get_pkt_single(struct rte_distributor_single *d,
62                 unsigned worker_id, struct rte_mbuf *oldpkt)
63 {
64         struct rte_mbuf *ret;
65         rte_distributor_request_pkt_single(d, worker_id, oldpkt);
66         while ((ret = rte_distributor_poll_pkt_single(d, worker_id)) == NULL)
67                 rte_pause();
68         return ret;
69 }
70
71 int
72 rte_distributor_return_pkt_single(struct rte_distributor_single *d,
73                 unsigned worker_id, struct rte_mbuf *oldpkt)
74 {
75         union rte_distributor_buffer_single *buf = &d->bufs[worker_id];
76         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
77                         | RTE_DISTRIB_RETURN_BUF;
78         /* Sync with distributor on RETURN_BUF flag. */
79         __atomic_store_n(&(buf->bufptr64), req, __ATOMIC_RELEASE);
80         return 0;
81 }
82
83 /**** APIs called on distributor core ***/
84
85 /* as name suggests, adds a packet to the backlog for a particular worker */
86 static int
87 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
88 {
89         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
90                 return -1;
91
92         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
93                         = item;
94         return 0;
95 }
96
97 /* takes the next packet for a worker off the backlog */
98 static int64_t
99 backlog_pop(struct rte_distributor_backlog *bl)
100 {
101         bl->count--;
102         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
103 }
104
105 /* stores a packet returned from a worker inside the returns array */
106 static inline void
107 store_return(uintptr_t oldbuf, struct rte_distributor_single *d,
108                 unsigned *ret_start, unsigned *ret_count)
109 {
110         /* store returns in a circular buffer - code is branch-free */
111         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
112                         = (void *)oldbuf;
113         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
114         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
115 }
116
117 static inline void
118 handle_worker_shutdown(struct rte_distributor_single *d, unsigned int wkr)
119 {
120         d->in_flight_tags[wkr] = 0;
121         d->in_flight_bitmask &= ~(1UL << wkr);
122         /* Sync with worker. Release bufptr64. */
123         __atomic_store_n(&(d->bufs[wkr].bufptr64), 0, __ATOMIC_RELEASE);
124         if (unlikely(d->backlog[wkr].count != 0)) {
125                 /* On return of a packet, we need to move the
126                  * queued packets for this core elsewhere.
127                  * Easiest solution is to set things up for
128                  * a recursive call. That will cause those
129                  * packets to be queued up for the next free
130                  * core, i.e. it will return as soon as a
131                  * core becomes free to accept the first
132                  * packet, as subsequent ones will be added to
133                  * the backlog for that core.
134                  */
135                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
136                 unsigned i;
137                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
138
139                 for (i = 0; i < bl->count; i++) {
140                         unsigned idx = (bl->start + i) &
141                                         RTE_DISTRIB_BACKLOG_MASK;
142                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
143                                         RTE_DISTRIB_FLAG_BITS));
144                 }
145                 /* recursive call.
146                  * Note that the tags were set before first level call
147                  * to rte_distributor_process.
148                  */
149                 rte_distributor_process_single(d, pkts, i);
150                 bl->count = bl->start = 0;
151         }
152 }
153
154 /* this function is called when process() fn is called without any new
155  * packets. It goes through all the workers and clears any returned packets
156  * to do a partial flush.
157  */
158 static int
159 process_returns(struct rte_distributor_single *d)
160 {
161         unsigned wkr;
162         unsigned flushed = 0;
163         unsigned ret_start = d->returns.start,
164                         ret_count = d->returns.count;
165
166         for (wkr = 0; wkr < d->num_workers; wkr++) {
167                 uintptr_t oldbuf = 0;
168                 /* Sync with worker. Acquire bufptr64. */
169                 const int64_t data = __atomic_load_n(&(d->bufs[wkr].bufptr64),
170                                                         __ATOMIC_ACQUIRE);
171
172                 if (data & RTE_DISTRIB_GET_BUF) {
173                         flushed++;
174                         if (d->backlog[wkr].count)
175                                 /* Sync with worker. Release bufptr64. */
176                                 __atomic_store_n(&(d->bufs[wkr].bufptr64),
177                                         backlog_pop(&d->backlog[wkr]),
178                                         __ATOMIC_RELEASE);
179                         else {
180                                 /* Sync with worker on GET_BUF flag. */
181                                 __atomic_store_n(&(d->bufs[wkr].bufptr64),
182                                         RTE_DISTRIB_GET_BUF,
183                                         __ATOMIC_RELEASE);
184                                 d->in_flight_tags[wkr] = 0;
185                                 d->in_flight_bitmask &= ~(1UL << wkr);
186                         }
187                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
188                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
189                         handle_worker_shutdown(d, wkr);
190                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
191                 }
192
193                 store_return(oldbuf, d, &ret_start, &ret_count);
194         }
195
196         d->returns.start = ret_start;
197         d->returns.count = ret_count;
198
199         return flushed;
200 }
201
202 /* process a set of packets to distribute them to workers */
203 int
204 rte_distributor_process_single(struct rte_distributor_single *d,
205                 struct rte_mbuf **mbufs, unsigned num_mbufs)
206 {
207         unsigned next_idx = 0;
208         unsigned wkr = 0;
209         struct rte_mbuf *next_mb = NULL;
210         int64_t next_value = 0;
211         uint32_t new_tag = 0;
212         unsigned ret_start = d->returns.start,
213                         ret_count = d->returns.count;
214
215         if (unlikely(num_mbufs == 0))
216                 return process_returns(d);
217
218         while (next_idx < num_mbufs || next_mb != NULL) {
219                 uintptr_t oldbuf = 0;
220                 /* Sync with worker. Acquire bufptr64. */
221                 int64_t data = __atomic_load_n(&(d->bufs[wkr].bufptr64),
222                                                 __ATOMIC_ACQUIRE);
223
224                 if (!next_mb) {
225                         next_mb = mbufs[next_idx++];
226                         next_value = (((int64_t)(uintptr_t)next_mb)
227                                         << RTE_DISTRIB_FLAG_BITS);
228                         /*
229                          * User is advocated to set tag value for each
230                          * mbuf before calling rte_distributor_process.
231                          * User defined tags are used to identify flows,
232                          * or sessions.
233                          */
234                         new_tag = next_mb->hash.usr;
235
236                         /*
237                          * Note that if RTE_DISTRIB_MAX_WORKERS is larger than 64
238                          * then the size of match has to be expanded.
239                          */
240                         uint64_t match = 0;
241                         unsigned i;
242                         /*
243                          * to scan for a match use "xor" and "not" to get a 0/1
244                          * value, then use shifting to merge to single "match"
245                          * variable, where a one-bit indicates a match for the
246                          * worker given by the bit-position
247                          */
248                         for (i = 0; i < d->num_workers; i++)
249                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
250                                         << i);
251
252                         /* Only turned-on bits are considered as match */
253                         match &= d->in_flight_bitmask;
254
255                         if (match) {
256                                 next_mb = NULL;
257                                 unsigned worker = __builtin_ctzl(match);
258                                 if (add_to_backlog(&d->backlog[worker],
259                                                 next_value) < 0)
260                                         next_idx--;
261                         }
262                 }
263
264                 if ((data & RTE_DISTRIB_GET_BUF) &&
265                                 (d->backlog[wkr].count || next_mb)) {
266
267                         if (d->backlog[wkr].count)
268                                 /* Sync with worker. Release bufptr64. */
269                                 __atomic_store_n(&(d->bufs[wkr].bufptr64),
270                                                 backlog_pop(&d->backlog[wkr]),
271                                                 __ATOMIC_RELEASE);
272
273                         else {
274                                 /* Sync with worker. Release bufptr64.  */
275                                 __atomic_store_n(&(d->bufs[wkr].bufptr64),
276                                                 next_value,
277                                                 __ATOMIC_RELEASE);
278                                 d->in_flight_tags[wkr] = new_tag;
279                                 d->in_flight_bitmask |= (1UL << wkr);
280                                 next_mb = NULL;
281                         }
282                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
283                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
284                         handle_worker_shutdown(d, wkr);
285                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
286                 }
287
288                 /* store returns in a circular buffer */
289                 store_return(oldbuf, d, &ret_start, &ret_count);
290
291                 if (++wkr == d->num_workers)
292                         wkr = 0;
293         }
294         /* to finish, check all workers for backlog and schedule work for them
295          * if they are ready */
296         for (wkr = 0; wkr < d->num_workers; wkr++)
297                 if (d->backlog[wkr].count &&
298                                 /* Sync with worker. Acquire bufptr64. */
299                                 (__atomic_load_n(&(d->bufs[wkr].bufptr64),
300                                 __ATOMIC_ACQUIRE) & RTE_DISTRIB_GET_BUF)) {
301
302                         int64_t oldbuf = d->bufs[wkr].bufptr64 >>
303                                         RTE_DISTRIB_FLAG_BITS;
304
305                         store_return(oldbuf, d, &ret_start, &ret_count);
306
307                         /* Sync with worker. Release bufptr64. */
308                         __atomic_store_n(&(d->bufs[wkr].bufptr64),
309                                 backlog_pop(&d->backlog[wkr]),
310                                 __ATOMIC_RELEASE);
311                 }
312
313         d->returns.start = ret_start;
314         d->returns.count = ret_count;
315         return num_mbufs;
316 }
317
318 /* return to the caller, packets returned from workers */
319 int
320 rte_distributor_returned_pkts_single(struct rte_distributor_single *d,
321                 struct rte_mbuf **mbufs, unsigned max_mbufs)
322 {
323         struct rte_distributor_returned_pkts *returns = &d->returns;
324         unsigned retval = (max_mbufs < returns->count) ?
325                         max_mbufs : returns->count;
326         unsigned i;
327
328         for (i = 0; i < retval; i++) {
329                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
330                 mbufs[i] = returns->mbufs[idx];
331         }
332         returns->start += i;
333         returns->count -= i;
334
335         return retval;
336 }
337
338 /* return the number of packets in-flight in a distributor, i.e. packets
339  * being worked on or queued up in a backlog.
340  */
341 static inline unsigned
342 total_outstanding(const struct rte_distributor_single *d)
343 {
344         unsigned wkr, total_outstanding;
345
346         total_outstanding = __builtin_popcountl(d->in_flight_bitmask);
347
348         for (wkr = 0; wkr < d->num_workers; wkr++)
349                 total_outstanding += d->backlog[wkr].count;
350
351         return total_outstanding;
352 }
353
354 /* flush the distributor, so that there are no outstanding packets in flight or
355  * queued up. */
356 int
357 rte_distributor_flush_single(struct rte_distributor_single *d)
358 {
359         const unsigned flushed = total_outstanding(d);
360
361         while (total_outstanding(d) > 0)
362                 rte_distributor_process_single(d, NULL, 0);
363
364         return flushed;
365 }
366
367 /* clears the internal returns array in the distributor */
368 void
369 rte_distributor_clear_returns_single(struct rte_distributor_single *d)
370 {
371         d->returns.start = d->returns.count = 0;
372 #ifndef __OPTIMIZE__
373         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
374 #endif
375 }
376
377 /* creates a distributor instance */
378 struct rte_distributor_single *
379 rte_distributor_create_single(const char *name,
380                 unsigned socket_id,
381                 unsigned num_workers)
382 {
383         struct rte_distributor_single *d;
384         struct rte_distributor_list *distributor_list;
385         char mz_name[RTE_MEMZONE_NAMESIZE];
386         const struct rte_memzone *mz;
387
388         /* compilation-time checks */
389         RTE_BUILD_BUG_ON((sizeof(*d) & RTE_CACHE_LINE_MASK) != 0);
390         RTE_BUILD_BUG_ON((RTE_DISTRIB_MAX_WORKERS & 7) != 0);
391         RTE_BUILD_BUG_ON(RTE_DISTRIB_MAX_WORKERS >
392                                 sizeof(d->in_flight_bitmask) * CHAR_BIT);
393
394         if (name == NULL || num_workers >= RTE_DISTRIB_MAX_WORKERS) {
395                 rte_errno = EINVAL;
396                 return NULL;
397         }
398
399         snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
400         mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
401         if (mz == NULL) {
402                 rte_errno = ENOMEM;
403                 return NULL;
404         }
405
406         d = mz->addr;
407         strlcpy(d->name, name, sizeof(d->name));
408         d->num_workers = num_workers;
409
410         distributor_list = RTE_TAILQ_CAST(rte_distributor_tailq.head,
411                                           rte_distributor_list);
412
413         rte_mcfg_tailq_write_lock();
414         TAILQ_INSERT_TAIL(distributor_list, d, next);
415         rte_mcfg_tailq_write_unlock();
416
417         return d;
418 }