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