mbuf: remove the rte_pktmbuf structure
[dpdk.git] / lib / librte_distributor / rte_distributor.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <sys/queue.h>
36 #include <string.h>
37 #include <rte_mbuf.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"
44
45 #define NO_FLAGS 0
46 #define RTE_DISTRIB_PREFIX "DT_"
47
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 */
57
58 #define RTE_DISTRIB_BACKLOG_SIZE 8
59 #define RTE_DISTRIB_BACKLOG_MASK (RTE_DISTRIB_BACKLOG_SIZE - 1)
60
61 #define RTE_DISTRIB_MAX_RETURNS 128
62 #define RTE_DISTRIB_RETURNS_MASK (RTE_DISTRIB_MAX_RETURNS - 1)
63
64 /**
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.
70  */
71 union rte_distributor_buffer {
72         volatile int64_t bufptr64;
73         char pad[CACHE_LINE_SIZE*3];
74 } __rte_cache_aligned;
75
76 struct rte_distributor_backlog {
77         unsigned start;
78         unsigned count;
79         int64_t pkts[RTE_DISTRIB_BACKLOG_SIZE];
80 };
81
82 struct rte_distributor_returned_pkts {
83         unsigned start;
84         unsigned count;
85         struct rte_mbuf *mbufs[RTE_DISTRIB_MAX_RETURNS];
86 };
87
88 struct rte_distributor {
89         TAILQ_ENTRY(rte_distributor) next;    /**< Next in list. */
90
91         char name[RTE_DISTRIBUTOR_NAMESIZE];  /**< Name of the ring. */
92         unsigned num_workers;                 /**< Number of workers polling */
93
94         uint32_t in_flight_tags[RTE_MAX_LCORE];
95         struct rte_distributor_backlog backlog[RTE_MAX_LCORE];
96
97         union rte_distributor_buffer bufs[RTE_MAX_LCORE];
98
99         struct rte_distributor_returned_pkts returns;
100 };
101
102 TAILQ_HEAD(rte_distributor_list, rte_distributor);
103
104 /**** APIs called by workers ****/
105
106 void
107 rte_distributor_request_pkt(struct rte_distributor *d,
108                 unsigned worker_id, struct rte_mbuf *oldpkt)
109 {
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))
114                 rte_pause();
115         buf->bufptr64 = req;
116 }
117
118 struct rte_mbuf *
119 rte_distributor_poll_pkt(struct rte_distributor *d,
120                 unsigned worker_id)
121 {
122         union rte_distributor_buffer *buf = &d->bufs[worker_id];
123         if (buf->bufptr64 & RTE_DISTRIB_GET_BUF)
124                 return NULL;
125
126         /* since bufptr64 is signed, this should be an arithmetic shift */
127         int64_t ret = buf->bufptr64 >> RTE_DISTRIB_FLAG_BITS;
128         return (struct rte_mbuf *)((uintptr_t)ret);
129 }
130
131 struct rte_mbuf *
132 rte_distributor_get_pkt(struct rte_distributor *d,
133                 unsigned worker_id, struct rte_mbuf *oldpkt)
134 {
135         struct rte_mbuf *ret;
136         rte_distributor_request_pkt(d, worker_id, oldpkt);
137         while ((ret = rte_distributor_poll_pkt(d, worker_id)) == NULL)
138                 rte_pause();
139         return ret;
140 }
141
142 int
143 rte_distributor_return_pkt(struct rte_distributor *d,
144                 unsigned worker_id, struct rte_mbuf *oldpkt)
145 {
146         union rte_distributor_buffer *buf = &d->bufs[worker_id];
147         uint64_t req = (((int64_t)(uintptr_t)oldpkt) << RTE_DISTRIB_FLAG_BITS)
148                         | RTE_DISTRIB_RETURN_BUF;
149         buf->bufptr64 = req;
150         return 0;
151 }
152
153 /**** APIs called on distributor core ***/
154
155 /* as name suggests, adds a packet to the backlog for a particular worker */
156 static int
157 add_to_backlog(struct rte_distributor_backlog *bl, int64_t item)
158 {
159         if (bl->count == RTE_DISTRIB_BACKLOG_SIZE)
160                 return -1;
161
162         bl->pkts[(bl->start + bl->count++) & (RTE_DISTRIB_BACKLOG_MASK)]
163                         = item;
164         return 0;
165 }
166
167 /* takes the next packet for a worker off the backlog */
168 static int64_t
169 backlog_pop(struct rte_distributor_backlog *bl)
170 {
171         bl->count--;
172         return bl->pkts[bl->start++ & RTE_DISTRIB_BACKLOG_MASK];
173 }
174
175 /* stores a packet returned from a worker inside the returns array */
176 static inline void
177 store_return(uintptr_t oldbuf, struct rte_distributor *d,
178                 unsigned *ret_start, unsigned *ret_count)
179 {
180         /* store returns in a circular buffer - code is branch-free */
181         d->returns.mbufs[(*ret_start + *ret_count) & RTE_DISTRIB_RETURNS_MASK]
182                         = (void *)oldbuf;
183         *ret_start += (*ret_count == RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
184         *ret_count += (*ret_count != RTE_DISTRIB_RETURNS_MASK) & !!(oldbuf);
185 }
186
187 static inline void
188 handle_worker_shutdown(struct rte_distributor *d, unsigned wkr)
189 {
190         d->in_flight_tags[wkr] = 0;
191         d->bufs[wkr].bufptr64 = 0;
192         if (unlikely(d->backlog[wkr].count != 0)) {
193                 /* On return of a packet, we need to move the
194                  * queued packets for this core elsewhere.
195                  * Easiest solution is to set things up for
196                  * a recursive call. That will cause those
197                  * packets to be queued up for the next free
198                  * core, i.e. it will return as soon as a
199                  * core becomes free to accept the first
200                  * packet, as subsequent ones will be added to
201                  * the backlog for that core.
202                  */
203                 struct rte_mbuf *pkts[RTE_DISTRIB_BACKLOG_SIZE];
204                 unsigned i;
205                 struct rte_distributor_backlog *bl = &d->backlog[wkr];
206
207                 for (i = 0; i < bl->count; i++) {
208                         unsigned idx = (bl->start + i) &
209                                         RTE_DISTRIB_BACKLOG_MASK;
210                         pkts[i] = (void *)((uintptr_t)(bl->pkts[idx] >>
211                                         RTE_DISTRIB_FLAG_BITS));
212                 }
213                 /* recursive call */
214                 rte_distributor_process(d, pkts, i);
215                 bl->count = bl->start = 0;
216         }
217 }
218
219 /* this function is called when process() fn is called without any new
220  * packets. It goes through all the workers and clears any returned packets
221  * to do a partial flush.
222  */
223 static int
224 process_returns(struct rte_distributor *d)
225 {
226         unsigned wkr;
227         unsigned flushed = 0;
228         unsigned ret_start = d->returns.start,
229                         ret_count = d->returns.count;
230
231         for (wkr = 0; wkr < d->num_workers; wkr++) {
232
233                 const int64_t data = d->bufs[wkr].bufptr64;
234                 uintptr_t oldbuf = 0;
235
236                 if (data & RTE_DISTRIB_GET_BUF) {
237                         flushed++;
238                         if (d->backlog[wkr].count)
239                                 d->bufs[wkr].bufptr64 =
240                                                 backlog_pop(&d->backlog[wkr]);
241                         else {
242                                 d->bufs[wkr].bufptr64 = RTE_DISTRIB_GET_BUF;
243                                 d->in_flight_tags[wkr] = 0;
244                         }
245                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
246                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
247                         handle_worker_shutdown(d, wkr);
248                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
249                 }
250
251                 store_return(oldbuf, d, &ret_start, &ret_count);
252         }
253
254         d->returns.start = ret_start;
255         d->returns.count = ret_count;
256
257         return flushed;
258 }
259
260 /* process a set of packets to distribute them to workers */
261 int
262 rte_distributor_process(struct rte_distributor *d,
263                 struct rte_mbuf **mbufs, unsigned num_mbufs)
264 {
265         unsigned next_idx = 0;
266         unsigned wkr = 0;
267         struct rte_mbuf *next_mb = NULL;
268         int64_t next_value = 0;
269         uint32_t new_tag = 0;
270         unsigned ret_start = d->returns.start,
271                         ret_count = d->returns.count;
272
273         if (unlikely(num_mbufs == 0))
274                 return process_returns(d);
275
276         while (next_idx < num_mbufs || next_mb != NULL) {
277
278                 int64_t data = d->bufs[wkr].bufptr64;
279                 uintptr_t oldbuf = 0;
280
281                 if (!next_mb) {
282                         next_mb = mbufs[next_idx++];
283                         next_value = (((int64_t)(uintptr_t)next_mb)
284                                         << RTE_DISTRIB_FLAG_BITS);
285                         new_tag = (next_mb->hash.rss | 1);
286
287                         uint32_t match = 0;
288                         unsigned i;
289                         for (i = 0; i < d->num_workers; i++)
290                                 match |= (!(d->in_flight_tags[i] ^ new_tag)
291                                         << i);
292
293                         if (match) {
294                                 next_mb = NULL;
295                                 unsigned worker = __builtin_ctz(match);
296                                 if (add_to_backlog(&d->backlog[worker],
297                                                 next_value) < 0)
298                                         next_idx--;
299                         }
300                 }
301
302                 if ((data & RTE_DISTRIB_GET_BUF) &&
303                                 (d->backlog[wkr].count || next_mb)) {
304
305                         if (d->backlog[wkr].count)
306                                 d->bufs[wkr].bufptr64 =
307                                                 backlog_pop(&d->backlog[wkr]);
308
309                         else {
310                                 d->bufs[wkr].bufptr64 = next_value;
311                                 d->in_flight_tags[wkr] = new_tag;
312                                 next_mb = NULL;
313                         }
314                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
315                 } else if (data & RTE_DISTRIB_RETURN_BUF) {
316                         handle_worker_shutdown(d, wkr);
317                         oldbuf = data >> RTE_DISTRIB_FLAG_BITS;
318                 }
319
320                 /* store returns in a circular buffer */
321                 store_return(oldbuf, d, &ret_start, &ret_count);
322
323                 if (++wkr == d->num_workers)
324                         wkr = 0;
325         }
326         /* to finish, check all workers for backlog and schedule work for them
327          * if they are ready */
328         for (wkr = 0; wkr < d->num_workers; wkr++)
329                 if (d->backlog[wkr].count &&
330                                 (d->bufs[wkr].bufptr64 & RTE_DISTRIB_GET_BUF)) {
331
332                         int64_t oldbuf = d->bufs[wkr].bufptr64 >>
333                                         RTE_DISTRIB_FLAG_BITS;
334                         store_return(oldbuf, d, &ret_start, &ret_count);
335
336                         d->bufs[wkr].bufptr64 = backlog_pop(&d->backlog[wkr]);
337                 }
338
339         d->returns.start = ret_start;
340         d->returns.count = ret_count;
341         return num_mbufs;
342 }
343
344 /* return to the caller, packets returned from workers */
345 int
346 rte_distributor_returned_pkts(struct rte_distributor *d,
347                 struct rte_mbuf **mbufs, unsigned max_mbufs)
348 {
349         struct rte_distributor_returned_pkts *returns = &d->returns;
350         unsigned retval = (max_mbufs < returns->count) ?
351                         max_mbufs : returns->count;
352         unsigned i;
353
354         for (i = 0; i < retval; i++) {
355                 unsigned idx = (returns->start + i) & RTE_DISTRIB_RETURNS_MASK;
356                 mbufs[i] = returns->mbufs[idx];
357         }
358         returns->start += i;
359         returns->count -= i;
360
361         return retval;
362 }
363
364 /* return the number of packets in-flight in a distributor, i.e. packets
365  * being workered on or queued up in a backlog. */
366 static inline unsigned
367 total_outstanding(const struct rte_distributor *d)
368 {
369         unsigned wkr, total_outstanding = 0;
370
371         for (wkr = 0; wkr < d->num_workers; wkr++)
372                 total_outstanding += d->backlog[wkr].count +
373                                 !!(d->in_flight_tags[wkr]);
374         return total_outstanding;
375 }
376
377 /* flush the distributor, so that there are no outstanding packets in flight or
378  * queued up. */
379 int
380 rte_distributor_flush(struct rte_distributor *d)
381 {
382         const unsigned flushed = total_outstanding(d);
383
384         while (total_outstanding(d) > 0)
385                 rte_distributor_process(d, NULL, 0);
386
387         return flushed;
388 }
389
390 /* clears the internal returns array in the distributor */
391 void
392 rte_distributor_clear_returns(struct rte_distributor *d)
393 {
394         d->returns.start = d->returns.count = 0;
395 #ifndef __OPTIMIZE__
396         memset(d->returns.mbufs, 0, sizeof(d->returns.mbufs));
397 #endif
398 }
399
400 /* creates a distributor instance */
401 struct rte_distributor *
402 rte_distributor_create(const char *name,
403                 unsigned socket_id,
404                 unsigned num_workers)
405 {
406         struct rte_distributor *d;
407         struct rte_distributor_list *distributor_list;
408         char mz_name[RTE_MEMZONE_NAMESIZE];
409         const struct rte_memzone *mz;
410
411         /* compilation-time checks */
412         RTE_BUILD_BUG_ON((sizeof(*d) & CACHE_LINE_MASK) != 0);
413         RTE_BUILD_BUG_ON((RTE_MAX_LCORE & 7) != 0);
414
415         if (name == NULL || num_workers >= RTE_MAX_LCORE) {
416                 rte_errno = EINVAL;
417                 return NULL;
418         }
419
420         /* check that we have an initialised tail queue */
421         distributor_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_DISTRIBUTOR,
422                                 rte_distributor_list);
423         if (distributor_list == NULL) {
424                 rte_errno = E_RTE_NO_TAILQ;
425                 return NULL;
426         }
427
428         snprintf(mz_name, sizeof(mz_name), RTE_DISTRIB_PREFIX"%s", name);
429         mz = rte_memzone_reserve(mz_name, sizeof(*d), socket_id, NO_FLAGS);
430         if (mz == NULL) {
431                 rte_errno = ENOMEM;
432                 return NULL;
433         }
434
435         d = mz->addr;
436         snprintf(d->name, sizeof(d->name), "%s", name);
437         d->num_workers = num_workers;
438
439         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
440         TAILQ_INSERT_TAIL(distributor_list, d, next);
441         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
442
443         return d;
444 }