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 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/counter.h>
34 #include <sys/mutex.h>
35 #include <sys/malloc.h>
36 #include <machine/cpu.h>
37 #include <net/mp_ring.h>
50 IDLE = 0, /* consumer ran to completion, nothing more to do. */
51 BUSY, /* consumer is running already, or will be shortly. */
52 STALLED, /* consumer stopped due to lack of resources. */
53 ABDICATED, /* consumer stopped even though there was work to be
54 done because it wants another thread to take over. */
57 static inline uint16_t
58 space_available(struct ifmp_ring *r, union ring_state s)
60 uint16_t x = r->size - 1;
62 if (s.cidx == s.pidx_head)
64 else if (s.cidx > s.pidx_head)
65 return (s.cidx - s.pidx_head - 1);
67 return (x - s.pidx_head + s.cidx);
70 static inline uint16_t
71 increment_idx(struct ifmp_ring *r, uint16_t idx, uint16_t n)
73 int x = r->size - idx;
76 return (x > n ? idx + n : n - x);
79 /* Consumer is about to update the ring's state to s */
80 static inline uint16_t
81 state_to_flags(union ring_state s, int abdicate)
84 if (s.cidx == s.pidx_tail)
86 else if (abdicate && s.pidx_tail != s.pidx_head)
92 #ifdef MP_RING_NO_64BIT_ATOMICS
94 drain_ring_locked(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
97 int n, pending, total;
98 uint16_t cidx = os.cidx;
99 uint16_t pidx = os.pidx_tail;
101 MPASS(os.flags == BUSY);
105 counter_u64_add(r->starts, 1);
109 while (cidx != pidx) {
110 /* Items from cidx to pidx are available for consumption. */
111 n = r->drain(r, cidx, pidx);
113 os.state = ns.state = r->state;
118 counter_u64_add(r->stalls, 1);
119 else if (total > 0) {
120 counter_u64_add(r->restarts, 1);
121 counter_u64_add(r->stalls, 1);
125 cidx = increment_idx(r, cidx, n);
130 * We update the cidx only if we've caught up with the pidx, the
131 * real cidx is getting too far ahead of the one visible to
132 * everyone else, or we have exceeded our budget.
134 if (cidx != pidx && pending < 64 && total < budget)
137 os.state = ns.state = r->state;
139 ns.flags = state_to_flags(ns, total >= budget);
142 if (ns.flags == ABDICATED)
143 counter_u64_add(r->abdications, 1);
144 if (ns.flags != BUSY) {
145 /* Wrong loop exit if we're going to stall. */
146 MPASS(ns.flags != STALLED);
147 if (prev == STALLED) {
149 counter_u64_add(r->restarts, 1);
155 * The acquire style atomic above guarantees visibility of items
156 * associated with any pidx change that we notice here.
164 * Caller passes in a state, with a guarantee that there is work to do and that
165 * all items up to the pidx_tail in the state are visible.
168 drain_ring_lockless(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
171 int n, pending, total;
172 uint16_t cidx = os.cidx;
173 uint16_t pidx = os.pidx_tail;
175 MPASS(os.flags == BUSY);
179 counter_u64_add(r->starts, 1);
183 while (cidx != pidx) {
184 /* Items from cidx to pidx are available for consumption. */
185 n = r->drain(r, cidx, pidx);
193 } while (atomic_fcmpset_64(&r->state, &os.state,
197 counter_u64_add(r->stalls, 1);
198 else if (total > 0) {
199 counter_u64_add(r->restarts, 1);
200 counter_u64_add(r->stalls, 1);
204 cidx = increment_idx(r, cidx, n);
209 * We update the cidx only if we've caught up with the pidx, the
210 * real cidx is getting too far ahead of the one visible to
211 * everyone else, or we have exceeded our budget.
213 if (cidx != pidx && pending < 64 && total < budget)
220 ns.flags = state_to_flags(ns, total >= budget);
221 } while (atomic_fcmpset_acq_64(&r->state, &os.state,
225 if (ns.flags == ABDICATED)
226 counter_u64_add(r->abdications, 1);
227 if (ns.flags != BUSY) {
228 /* Wrong loop exit if we're going to stall. */
229 MPASS(ns.flags != STALLED);
230 if (prev == STALLED) {
232 counter_u64_add(r->restarts, 1);
238 * The acquire style atomic above guarantees visibility of items
239 * associated with any pidx change that we notice here.
248 ifmp_ring_alloc(struct ifmp_ring **pr, int size, void *cookie, mp_ring_drain_t drain,
249 mp_ring_can_drain_t can_drain, struct malloc_type *mt, int flags)
253 /* All idx are 16b so size can be 65536 at most */
254 if (pr == NULL || size < 2 || size > 65536 || drain == NULL ||
258 flags &= M_NOWAIT | M_WAITOK;
261 r = malloc(__offsetof(struct ifmp_ring, items[size]), mt, flags | M_ZERO);
268 r->can_drain = can_drain;
269 r->enqueues = counter_u64_alloc(flags);
270 r->drops = counter_u64_alloc(flags);
271 r->starts = counter_u64_alloc(flags);
272 r->stalls = counter_u64_alloc(flags);
273 r->restarts = counter_u64_alloc(flags);
274 r->abdications = counter_u64_alloc(flags);
275 if (r->enqueues == NULL || r->drops == NULL || r->starts == NULL ||
276 r->stalls == NULL || r->restarts == NULL ||
277 r->abdications == NULL) {
283 #ifdef MP_RING_NO_64BIT_ATOMICS
284 mtx_init(&r->lock, "mp_ring lock", NULL, MTX_DEF);
290 ifmp_ring_free(struct ifmp_ring *r)
296 if (r->enqueues != NULL)
297 counter_u64_free(r->enqueues);
298 if (r->drops != NULL)
299 counter_u64_free(r->drops);
300 if (r->starts != NULL)
301 counter_u64_free(r->starts);
302 if (r->stalls != NULL)
303 counter_u64_free(r->stalls);
304 if (r->restarts != NULL)
305 counter_u64_free(r->restarts);
306 if (r->abdications != NULL)
307 counter_u64_free(r->abdications);
313 * Enqueue n items and maybe drain the ring for some time.
317 #ifdef MP_RING_NO_64BIT_ATOMICS
319 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
321 union ring_state os, ns;
322 uint16_t pidx_start, pidx_stop;
325 MPASS(items != NULL);
330 * Reserve room for the new items. Our reservation, if successful, is
331 * from 'pidx_start' to 'pidx_stop'.
334 if (n >= space_available(r, os)) {
335 counter_u64_add(r->drops, n);
336 MPASS(os.flags != IDLE);
337 mtx_unlock(&r->lock);
338 if (os.flags == STALLED)
339 ifmp_ring_check_drainage(r, 0);
343 ns.pidx_head = increment_idx(r, os.pidx_head, n);
345 pidx_start = os.pidx_head;
346 pidx_stop = ns.pidx_head;
349 * Wait for other producers who got in ahead of us to enqueue their
350 * items, one producer at a time. It is our turn when the ring's
351 * pidx_tail reaches the beginning of our reservation (pidx_start).
353 while (ns.pidx_tail != pidx_start) {
358 /* Now it is our turn to fill up the area we reserved earlier. */
361 r->items[i] = *items++;
362 if (__predict_false(++i == r->size))
364 } while (i != pidx_stop);
367 * Update the ring's pidx_tail. The release style atomic guarantees
368 * that the items are visible to any thread that sees the updated pidx.
370 os.state = ns.state = r->state;
371 ns.pidx_tail = pidx_stop;
373 if (os.flags == IDLE)
374 ns.flags = ABDICATED;
378 counter_u64_add(r->enqueues, n);
382 * Turn into a consumer if some other thread isn't active as a consumer
385 if (os.flags != BUSY)
386 drain_ring_locked(r, ns, os.flags, budget);
389 mtx_unlock(&r->lock);
394 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
396 union ring_state os, ns;
397 uint16_t pidx_start, pidx_stop;
400 MPASS(items != NULL);
404 * Reserve room for the new items. Our reservation, if successful, is
405 * from 'pidx_start' to 'pidx_stop'.
409 if (n >= space_available(r, os)) {
410 counter_u64_add(r->drops, n);
411 MPASS(os.flags != IDLE);
412 if (os.flags == STALLED)
413 ifmp_ring_check_drainage(r, 0);
417 ns.pidx_head = increment_idx(r, os.pidx_head, n);
419 if (atomic_fcmpset_64(&r->state, &os.state, ns.state))
424 pidx_start = os.pidx_head;
425 pidx_stop = ns.pidx_head;
428 * Wait for other producers who got in ahead of us to enqueue their
429 * items, one producer at a time. It is our turn when the ring's
430 * pidx_tail reaches the beginning of our reservation (pidx_start).
432 while (ns.pidx_tail != pidx_start) {
437 /* Now it is our turn to fill up the area we reserved earlier. */
440 r->items[i] = *items++;
441 if (__predict_false(++i == r->size))
443 } while (i != pidx_stop);
446 * Update the ring's pidx_tail. The release style atomic guarantees
447 * that the items are visible to any thread that sees the updated pidx.
452 ns.pidx_tail = pidx_stop;
454 if (os.flags == IDLE)
455 ns.flags = ABDICATED;
458 } while (atomic_fcmpset_rel_64(&r->state, &os.state, ns.state) == 0);
460 counter_u64_add(r->enqueues, n);
464 * Turn into a consumer if some other thread isn't active as a consumer
467 if (os.flags != BUSY)
468 drain_ring_lockless(r, ns, os.flags, budget);
476 ifmp_ring_check_drainage(struct ifmp_ring *r, int budget)
478 union ring_state os, ns;
481 if ((os.flags != STALLED && os.flags != ABDICATED) || // Only continue in STALLED and ABDICATED
482 os.pidx_head != os.pidx_tail || // Require work to be available
483 (os.flags != ABDICATED && r->can_drain(r) == 0)) // Can either drain, or everyone left
486 MPASS(os.cidx != os.pidx_tail); /* implied by STALLED */
490 #ifdef MP_RING_NO_64BIT_ATOMICS
492 if (r->state != os.state) {
493 mtx_unlock(&r->lock);
497 drain_ring_locked(r, ns, os.flags, budget);
498 mtx_unlock(&r->lock);
501 * The acquire style atomic guarantees visibility of items associated
502 * with the pidx that we read here.
504 if (!atomic_cmpset_acq_64(&r->state, os.state, ns.state))
507 drain_ring_lockless(r, ns, os.flags, budget);
512 ifmp_ring_reset_stats(struct ifmp_ring *r)
515 counter_u64_zero(r->enqueues);
516 counter_u64_zero(r->drops);
517 counter_u64_zero(r->starts);
518 counter_u64_zero(r->stalls);
519 counter_u64_zero(r->restarts);
520 counter_u64_zero(r->abdications);
524 ifmp_ring_is_idle(struct ifmp_ring *r)
529 if (s.pidx_head == s.pidx_tail && s.pidx_tail == s.cidx &&
537 ifmp_ring_is_stalled(struct ifmp_ring *r)
542 if (s.pidx_head == s.pidx_tail && s.flags == STALLED)