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/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/counter.h>
33 #include <sys/mutex.h>
34 #include <sys/malloc.h>
35 #include <machine/cpu.h>
36 #include <net/mp_ring.h>
49 IDLE = 0, /* consumer ran to completion, nothing more to do. */
50 BUSY, /* consumer is running already, or will be shortly. */
51 STALLED, /* consumer stopped due to lack of resources. */
52 ABDICATED, /* consumer stopped even though there was work to be
53 done because it wants another thread to take over. */
56 static inline uint16_t
57 space_available(struct ifmp_ring *r, union ring_state s)
59 uint16_t x = r->size - 1;
61 if (s.cidx == s.pidx_head)
63 else if (s.cidx > s.pidx_head)
64 return (s.cidx - s.pidx_head - 1);
66 return (x - s.pidx_head + s.cidx);
69 static inline uint16_t
70 increment_idx(struct ifmp_ring *r, uint16_t idx, uint16_t n)
72 int x = r->size - idx;
75 return (x > n ? idx + n : n - x);
78 /* Consumer is about to update the ring's state to s */
79 static inline uint16_t
80 state_to_flags(union ring_state s, int abdicate)
83 if (s.cidx == s.pidx_tail)
85 else if (abdicate && s.pidx_tail != s.pidx_head)
91 #ifdef MP_RING_NO_64BIT_ATOMICS
93 drain_ring_locked(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
96 int n, pending, total;
97 uint16_t cidx = os.cidx;
98 uint16_t pidx = os.pidx_tail;
100 MPASS(os.flags == BUSY);
104 counter_u64_add(r->starts, 1);
108 while (cidx != pidx) {
109 /* Items from cidx to pidx are available for consumption. */
110 n = r->drain(r, cidx, pidx);
112 os.state = ns.state = r->state;
117 counter_u64_add(r->stalls, 1);
118 else if (total > 0) {
119 counter_u64_add(r->restarts, 1);
120 counter_u64_add(r->stalls, 1);
124 cidx = increment_idx(r, cidx, n);
129 * We update the cidx only if we've caught up with the pidx, the
130 * real cidx is getting too far ahead of the one visible to
131 * everyone else, or we have exceeded our budget.
133 if (cidx != pidx && pending < 64 && total < budget)
136 os.state = ns.state = r->state;
138 ns.flags = state_to_flags(ns, total >= budget);
141 if (ns.flags == ABDICATED)
142 counter_u64_add(r->abdications, 1);
143 if (ns.flags != BUSY) {
144 /* Wrong loop exit if we're going to stall. */
145 MPASS(ns.flags != STALLED);
146 if (prev == STALLED) {
148 counter_u64_add(r->restarts, 1);
154 * The acquire style atomic above guarantees visibility of items
155 * associated with any pidx change that we notice here.
163 * Caller passes in a state, with a guarantee that there is work to do and that
164 * all items up to the pidx_tail in the state are visible.
167 drain_ring_lockless(struct ifmp_ring *r, union ring_state os, uint16_t prev, int budget)
170 int n, pending, total;
171 uint16_t cidx = os.cidx;
172 uint16_t pidx = os.pidx_tail;
174 MPASS(os.flags == BUSY);
178 counter_u64_add(r->starts, 1);
182 while (cidx != pidx) {
183 /* Items from cidx to pidx are available for consumption. */
184 n = r->drain(r, cidx, pidx);
192 } while (atomic_fcmpset_64(&r->state, &os.state,
196 counter_u64_add(r->stalls, 1);
197 else if (total > 0) {
198 counter_u64_add(r->restarts, 1);
199 counter_u64_add(r->stalls, 1);
203 cidx = increment_idx(r, cidx, n);
208 * We update the cidx only if we've caught up with the pidx, the
209 * real cidx is getting too far ahead of the one visible to
210 * everyone else, or we have exceeded our budget.
212 if (cidx != pidx && pending < 64 && total < budget)
219 ns.flags = state_to_flags(ns, total >= budget);
220 } while (atomic_fcmpset_acq_64(&r->state, &os.state,
224 if (ns.flags == ABDICATED)
225 counter_u64_add(r->abdications, 1);
226 if (ns.flags != BUSY) {
227 /* Wrong loop exit if we're going to stall. */
228 MPASS(ns.flags != STALLED);
229 if (prev == STALLED) {
231 counter_u64_add(r->restarts, 1);
237 * The acquire style atomic above guarantees visibility of items
238 * associated with any pidx change that we notice here.
247 ifmp_ring_alloc(struct ifmp_ring **pr, int size, void *cookie, mp_ring_drain_t drain,
248 mp_ring_can_drain_t can_drain, struct malloc_type *mt, int flags)
252 /* All idx are 16b so size can be 65536 at most */
253 if (pr == NULL || size < 2 || size > 65536 || drain == NULL ||
257 flags &= M_NOWAIT | M_WAITOK;
260 r = malloc(__offsetof(struct ifmp_ring, items[size]), mt, flags | M_ZERO);
267 r->can_drain = can_drain;
268 r->enqueues = counter_u64_alloc(flags);
269 r->drops = counter_u64_alloc(flags);
270 r->starts = counter_u64_alloc(flags);
271 r->stalls = counter_u64_alloc(flags);
272 r->restarts = counter_u64_alloc(flags);
273 r->abdications = counter_u64_alloc(flags);
274 if (r->enqueues == NULL || r->drops == NULL || r->starts == NULL ||
275 r->stalls == NULL || r->restarts == NULL ||
276 r->abdications == NULL) {
282 #ifdef MP_RING_NO_64BIT_ATOMICS
283 mtx_init(&r->lock, "mp_ring lock", NULL, MTX_DEF);
289 ifmp_ring_free(struct ifmp_ring *r)
295 if (r->enqueues != NULL)
296 counter_u64_free(r->enqueues);
297 if (r->drops != NULL)
298 counter_u64_free(r->drops);
299 if (r->starts != NULL)
300 counter_u64_free(r->starts);
301 if (r->stalls != NULL)
302 counter_u64_free(r->stalls);
303 if (r->restarts != NULL)
304 counter_u64_free(r->restarts);
305 if (r->abdications != NULL)
306 counter_u64_free(r->abdications);
312 * Enqueue n items and maybe drain the ring for some time.
316 #ifdef MP_RING_NO_64BIT_ATOMICS
318 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
320 union ring_state os, ns;
321 uint16_t pidx_start, pidx_stop;
324 MPASS(items != NULL);
329 * Reserve room for the new items. Our reservation, if successful, is
330 * from 'pidx_start' to 'pidx_stop'.
333 if (n >= space_available(r, os)) {
334 counter_u64_add(r->drops, n);
335 MPASS(os.flags != IDLE);
336 mtx_unlock(&r->lock);
337 if (os.flags == STALLED)
338 ifmp_ring_check_drainage(r, 0);
342 ns.pidx_head = increment_idx(r, os.pidx_head, n);
344 pidx_start = os.pidx_head;
345 pidx_stop = ns.pidx_head;
348 * Wait for other producers who got in ahead of us to enqueue their
349 * items, one producer at a time. It is our turn when the ring's
350 * pidx_tail reaches the beginning of our reservation (pidx_start).
352 while (ns.pidx_tail != pidx_start) {
357 /* Now it is our turn to fill up the area we reserved earlier. */
360 r->items[i] = *items++;
361 if (__predict_false(++i == r->size))
363 } while (i != pidx_stop);
366 * Update the ring's pidx_tail. The release style atomic guarantees
367 * that the items are visible to any thread that sees the updated pidx.
369 os.state = ns.state = r->state;
370 ns.pidx_tail = pidx_stop;
372 if (os.flags == IDLE)
373 ns.flags = ABDICATED;
377 counter_u64_add(r->enqueues, n);
381 * Turn into a consumer if some other thread isn't active as a consumer
384 if (os.flags != BUSY)
385 drain_ring_locked(r, ns, os.flags, budget);
388 mtx_unlock(&r->lock);
393 ifmp_ring_enqueue(struct ifmp_ring *r, void **items, int n, int budget, int abdicate)
395 union ring_state os, ns;
396 uint16_t pidx_start, pidx_stop;
399 MPASS(items != NULL);
403 * Reserve room for the new items. Our reservation, if successful, is
404 * from 'pidx_start' to 'pidx_stop'.
408 if (n >= space_available(r, os)) {
409 counter_u64_add(r->drops, n);
410 MPASS(os.flags != IDLE);
411 if (os.flags == STALLED)
412 ifmp_ring_check_drainage(r, 0);
416 ns.pidx_head = increment_idx(r, os.pidx_head, n);
418 if (atomic_fcmpset_64(&r->state, &os.state, ns.state))
423 pidx_start = os.pidx_head;
424 pidx_stop = ns.pidx_head;
427 * Wait for other producers who got in ahead of us to enqueue their
428 * items, one producer at a time. It is our turn when the ring's
429 * pidx_tail reaches the beginning of our reservation (pidx_start).
431 while (ns.pidx_tail != pidx_start) {
436 /* Now it is our turn to fill up the area we reserved earlier. */
439 r->items[i] = *items++;
440 if (__predict_false(++i == r->size))
442 } while (i != pidx_stop);
445 * Update the ring's pidx_tail. The release style atomic guarantees
446 * that the items are visible to any thread that sees the updated pidx.
451 ns.pidx_tail = pidx_stop;
453 if (os.flags == IDLE)
454 ns.flags = ABDICATED;
457 } while (atomic_fcmpset_rel_64(&r->state, &os.state, ns.state) == 0);
459 counter_u64_add(r->enqueues, n);
463 * Turn into a consumer if some other thread isn't active as a consumer
466 if (os.flags != BUSY)
467 drain_ring_lockless(r, ns, os.flags, budget);
475 ifmp_ring_check_drainage(struct ifmp_ring *r, int budget)
477 union ring_state os, ns;
480 if ((os.flags != STALLED && os.flags != ABDICATED) || // Only continue in STALLED and ABDICATED
481 os.pidx_head != os.pidx_tail || // Require work to be available
482 (os.flags != ABDICATED && r->can_drain(r) == 0)) // Can either drain, or everyone left
485 MPASS(os.cidx != os.pidx_tail); /* implied by STALLED */
489 #ifdef MP_RING_NO_64BIT_ATOMICS
491 if (r->state != os.state) {
492 mtx_unlock(&r->lock);
496 drain_ring_locked(r, ns, os.flags, budget);
497 mtx_unlock(&r->lock);
500 * The acquire style atomic guarantees visibility of items associated
501 * with the pidx that we read here.
503 if (!atomic_cmpset_acq_64(&r->state, os.state, ns.state))
506 drain_ring_lockless(r, ns, os.flags, budget);
511 ifmp_ring_reset_stats(struct ifmp_ring *r)
514 counter_u64_zero(r->enqueues);
515 counter_u64_zero(r->drops);
516 counter_u64_zero(r->starts);
517 counter_u64_zero(r->stalls);
518 counter_u64_zero(r->restarts);
519 counter_u64_zero(r->abdications);
523 ifmp_ring_is_idle(struct ifmp_ring *r)
528 if (s.pidx_head == s.pidx_tail && s.pidx_tail == s.cidx &&
536 ifmp_ring_is_stalled(struct ifmp_ring *r)
541 if (s.pidx_head == s.pidx_tail && s.flags == STALLED)