2 * Copyright (c) 2014 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/counter.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <machine/cpu.h>
39 #include <net/mp_ring.h>
52 IDLE = 0, /* consumer ran to completion, nothing more to do. */
53 BUSY, /* consumer is running already, or will be shortly. */
54 STALLED, /* consumer stopped due to lack of resources. */
55 ABDICATED, /* consumer stopped even though there was work to be
56 done because it wants another thread to take over. */
59 static inline uint16_t
60 space_available(struct ifmp_ring *r, union ring_state s)
62 uint16_t x = r->size - 1;
64 if (s.cidx == s.pidx_head)
66 else if (s.cidx > s.pidx_head)
67 return (s.cidx - s.pidx_head - 1);
69 return (x - s.pidx_head + s.cidx);
72 static inline uint16_t
73 increment_idx(struct ifmp_ring *r, uint16_t idx, uint16_t n)
75 int x = r->size - idx;
78 return (x > n ? idx + n : n - x);
81 /* Consumer is about to update the ring's state to s */
82 static inline uint16_t
83 state_to_flags(union ring_state s, int abdicate)
86 if (s.cidx == s.pidx_tail)
88 else if (abdicate && s.pidx_tail != s.pidx_head)
94 #ifdef MP_RING_NO_64BIT_ATOMICS
96 drain_ring_locked(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
99 int n, pending, total;
100 uint16_t cidx = os.cidx;
101 uint16_t pidx = os.pidx_tail;
103 MPASS(os.flags == BUSY);
107 counter_u64_add(r->starts, 1);
111 while (cidx != pidx) {
113 /* Items from cidx to pidx are available for consumption. */
114 n = r->drain(r, cidx, pidx);
116 os.state = ns.state = r->state;
121 counter_u64_add(r->stalls, 1);
122 else if (total > 0) {
123 counter_u64_add(r->restarts, 1);
124 counter_u64_add(r->stalls, 1);
128 cidx = increment_idx(r, cidx, n);
133 * We update the cidx only if we've caught up with the pidx, the
134 * real cidx is getting too far ahead of the one visible to
135 * everyone else, or we have exceeded our budget.
137 if (cidx != pidx && pending < 64 && total < budget)
140 os.state = ns.state = r->state;
142 ns.flags = state_to_flags(ns, total >= budget);
145 if (ns.flags == ABDICATED)
146 counter_u64_add(r->abdications, 1);
147 if (ns.flags != BUSY) {
148 /* Wrong loop exit if we're going to stall. */
149 MPASS(ns.flags != STALLED);
150 if (prev == STALLED) {
152 counter_u64_add(r->restarts, 1);
158 * The acquire style atomic above guarantees visibility of items
159 * associated with any pidx change that we notice here.
167 * Caller passes in a state, with a guarantee that there is work to do and that
168 * all items up to the pidx_tail in the state are visible.
171 drain_ring_lockless(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
174 int n, pending, total;
175 uint16_t cidx = os.cidx;
176 uint16_t pidx = os.pidx_tail;
178 MPASS(os.flags == BUSY);
182 counter_u64_add(r->starts, 1);
186 while (cidx != pidx) {
188 /* Items from cidx to pidx are available for consumption. */
189 n = r->drain(r, cidx, pidx);
197 } while (atomic_fcmpset_64(&r->state, &os.state,
201 counter_u64_add(r->stalls, 1);
202 else if (total > 0) {
203 counter_u64_add(r->restarts, 1);
204 counter_u64_add(r->stalls, 1);
208 cidx = increment_idx(r, cidx, n);
213 * We update the cidx only if we've caught up with the pidx, the
214 * real cidx is getting too far ahead of the one visible to
215 * everyone else, or we have exceeded our budget.
217 if (cidx != pidx && pending < 64 && total < budget)
224 ns.flags = state_to_flags(ns, total >= budget);
225 } while (atomic_fcmpset_acq_64(&r->state, &os.state,
229 if (ns.flags == ABDICATED)
230 counter_u64_add(r->abdications, 1);
231 if (ns.flags != BUSY) {
232 /* Wrong loop exit if we're going to stall. */
233 MPASS(ns.flags != STALLED);
234 if (prev == STALLED) {
236 counter_u64_add(r->restarts, 1);
242 * The acquire style atomic above guarantees visibility of items
243 * associated with any pidx change that we notice here.
252 ifmp_ring_alloc(struct ifmp_ring **pr, int size, void *cookie, mp_ring_drain_t drain,
253 mp_ring_can_drain_t can_drain, struct malloc_type *mt, int flags)
257 /* All idx are 16b so size can be 65536 at most */
258 if (pr == NULL || size < 2 || size > 65536 || drain == NULL ||
262 flags &= M_NOWAIT | M_WAITOK;
265 r = malloc(__offsetof(struct ifmp_ring, items[size]), mt, flags | M_ZERO);
272 r->can_drain = can_drain;
273 r->enqueues = counter_u64_alloc(flags);
274 r->drops = counter_u64_alloc(flags);
275 r->starts = counter_u64_alloc(flags);
276 r->stalls = counter_u64_alloc(flags);
277 r->restarts = counter_u64_alloc(flags);
278 r->abdications = counter_u64_alloc(flags);
279 if (r->enqueues == NULL || r->drops == NULL || r->starts == NULL ||
280 r->stalls == NULL || r->restarts == NULL ||
281 r->abdications == NULL) {
287 #ifdef MP_RING_NO_64BIT_ATOMICS
288 mtx_init(&r->lock, "mp_ring lock", NULL, MTX_DEF);
294 ifmp_ring_free(struct ifmp_ring *r)
300 if (r->enqueues != NULL)
301 counter_u64_free(r->enqueues);
302 if (r->drops != NULL)
303 counter_u64_free(r->drops);
304 if (r->starts != NULL)
305 counter_u64_free(r->starts);
306 if (r->stalls != NULL)
307 counter_u64_free(r->stalls);
308 if (r->restarts != NULL)
309 counter_u64_free(r->restarts);
310 if (r->abdications != NULL)
311 counter_u64_free(r->abdications);
317 * Enqueue n items and maybe drain the ring for some time.
321 #ifdef MP_RING_NO_64BIT_ATOMICS
323 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
325 union ring_state os, ns;
326 uint16_t pidx_start, pidx_stop;
329 MPASS(items != NULL);
334 * Reserve room for the new items. Our reservation, if successful, is
335 * from 'pidx_start' to 'pidx_stop'.
338 if (n >= space_available(r, os)) {
339 counter_u64_add(r->drops, n);
340 MPASS(os.flags != IDLE);
341 mtx_unlock(&r->lock);
342 if (os.flags == STALLED)
343 ifmp_ring_check_drainage(r, 0);
347 ns.pidx_head = increment_idx(r, os.pidx_head, n);
349 pidx_start = os.pidx_head;
350 pidx_stop = ns.pidx_head;
353 * Wait for other producers who got in ahead of us to enqueue their
354 * items, one producer at a time. It is our turn when the ring's
355 * pidx_tail reaches the beginning of our reservation (pidx_start).
357 while (ns.pidx_tail != pidx_start) {
362 /* Now it is our turn to fill up the area we reserved earlier. */
365 r->items[i] = *items++;
366 if (__predict_false(++i == r->size))
368 } while (i != pidx_stop);
371 * Update the ring's pidx_tail. The release style atomic guarantees
372 * that the items are visible to any thread that sees the updated pidx.
374 os.state = ns.state = r->state;
375 ns.pidx_tail = pidx_stop;
377 if (os.flags == IDLE)
378 ns.flags = ABDICATED;
382 counter_u64_add(r->enqueues, n);
386 * Turn into a consumer if some other thread isn't active as a consumer
389 if (os.flags != BUSY)
390 drain_ring_locked(r, ns, os.flags, budget);
393 mtx_unlock(&r->lock);
398 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
400 union ring_state os, ns;
401 uint16_t pidx_start, pidx_stop;
404 MPASS(items != NULL);
408 * Reserve room for the new items. Our reservation, if successful, is
409 * from 'pidx_start' to 'pidx_stop'.
413 if (n >= space_available(r, os)) {
414 counter_u64_add(r->drops, n);
415 MPASS(os.flags != IDLE);
416 if (os.flags == STALLED)
417 ifmp_ring_check_drainage(r, 0);
421 ns.pidx_head = increment_idx(r, os.pidx_head, n);
423 if (atomic_fcmpset_64(&r->state, &os.state, ns.state))
428 pidx_start = os.pidx_head;
429 pidx_stop = ns.pidx_head;
432 * Wait for other producers who got in ahead of us to enqueue their
433 * items, one producer at a time. It is our turn when the ring's
434 * pidx_tail reaches the beginning of our reservation (pidx_start).
436 while (ns.pidx_tail != pidx_start) {
441 /* Now it is our turn to fill up the area we reserved earlier. */
444 r->items[i] = *items++;
445 if (__predict_false(++i == r->size))
447 } while (i != pidx_stop);
450 * Update the ring's pidx_tail. The release style atomic guarantees
451 * that the items are visible to any thread that sees the updated pidx.
456 ns.pidx_tail = pidx_stop;
458 if (os.flags == IDLE)
459 ns.flags = ABDICATED;
462 } while (atomic_fcmpset_rel_64(&r->state, &os.state, ns.state) == 0);
464 counter_u64_add(r->enqueues, n);
468 * Turn into a consumer if some other thread isn't active as a consumer
471 if (os.flags != BUSY)
472 drain_ring_lockless(r, ns, os.flags, budget);
480 ifmp_ring_check_drainage(struct ifmp_ring *r, int budget)
482 union ring_state os, ns;
485 if ((os.flags != STALLED && os.flags != ABDICATED) || // Only continue in STALLED and ABDICATED
486 os.pidx_head != os.pidx_tail || // Require work to be available
487 (os.flags != ABDICATED && r->can_drain(r) == 0)) // Can either drain, or everyone left
490 MPASS(os.cidx != os.pidx_tail); /* implied by STALLED */
495 #ifdef MP_RING_NO_64BIT_ATOMICS
497 if (r->state != os.state) {
498 mtx_unlock(&r->lock);
502 drain_ring_locked(r, ns, os.flags, budget);
503 mtx_unlock(&r->lock);
506 * The acquire style atomic guarantees visibility of items associated
507 * with the pidx that we read here.
509 if (!atomic_cmpset_acq_64(&r->state, os.state, ns.state))
513 drain_ring_lockless(r, ns, os.flags, budget);
518 ifmp_ring_reset_stats(struct ifmp_ring *r)
521 counter_u64_zero(r->enqueues);
522 counter_u64_zero(r->drops);
523 counter_u64_zero(r->starts);
524 counter_u64_zero(r->stalls);
525 counter_u64_zero(r->restarts);
526 counter_u64_zero(r->abdications);
530 ifmp_ring_is_idle(struct ifmp_ring *r)
535 if (s.pidx_head == s.pidx_tail && s.pidx_tail == s.cidx &&
543 ifmp_ring_is_stalled(struct ifmp_ring *r)
548 if (s.pidx_head == s.pidx_tail && s.flags == STALLED)