xref: /freebsd/sys/net/mp_ring.h (revision 4c7070db251a6f2153f9e4783717151b07449ae3)
1*4c7070dbSScott Long /*-
2*4c7070dbSScott Long  * Copyright (c) 2014 Chelsio Communications, Inc.
3*4c7070dbSScott Long  * All rights reserved.
4*4c7070dbSScott Long  * Written by: Navdeep Parhar <np@FreeBSD.org>
5*4c7070dbSScott Long  *
6*4c7070dbSScott Long  * Redistribution and use in source and binary forms, with or without
7*4c7070dbSScott Long  * modification, are permitted provided that the following conditions
8*4c7070dbSScott Long  * are met:
9*4c7070dbSScott Long  * 1. Redistributions of source code must retain the above copyright
10*4c7070dbSScott Long  *    notice, this list of conditions and the following disclaimer.
11*4c7070dbSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
12*4c7070dbSScott Long  *    notice, this list of conditions and the following disclaimer in the
13*4c7070dbSScott Long  *    documentation and/or other materials provided with the distribution.
14*4c7070dbSScott Long  *
15*4c7070dbSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*4c7070dbSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*4c7070dbSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*4c7070dbSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*4c7070dbSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*4c7070dbSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*4c7070dbSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*4c7070dbSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*4c7070dbSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*4c7070dbSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*4c7070dbSScott Long  * SUCH DAMAGE.
26*4c7070dbSScott Long  *
27*4c7070dbSScott Long  * $FreeBSD$
28*4c7070dbSScott Long  *
29*4c7070dbSScott Long  */
30*4c7070dbSScott Long 
31*4c7070dbSScott Long #ifndef __NET_MP_RING_H
32*4c7070dbSScott Long #define __NET_MP_RING_H
33*4c7070dbSScott Long 
34*4c7070dbSScott Long #ifndef _KERNEL
35*4c7070dbSScott Long #error "no user-serviceable parts inside"
36*4c7070dbSScott Long #endif
37*4c7070dbSScott Long 
38*4c7070dbSScott Long struct ifmp_ring;
39*4c7070dbSScott Long typedef u_int (*mp_ring_drain_t)(struct ifmp_ring *, u_int, u_int);
40*4c7070dbSScott Long typedef u_int (*mp_ring_can_drain_t)(struct ifmp_ring *);
41*4c7070dbSScott Long typedef void (*mp_ring_serial_t)(struct ifmp_ring *);
42*4c7070dbSScott Long 
43*4c7070dbSScott Long struct ifmp_ring {
44*4c7070dbSScott Long 	volatile uint64_t	state __aligned(CACHE_LINE_SIZE);
45*4c7070dbSScott Long 
46*4c7070dbSScott Long 	int			size __aligned(CACHE_LINE_SIZE);
47*4c7070dbSScott Long 	void *			cookie;
48*4c7070dbSScott Long 	struct malloc_type *	mt;
49*4c7070dbSScott Long 	mp_ring_drain_t		drain;
50*4c7070dbSScott Long 	mp_ring_can_drain_t	can_drain;	/* cheap, may be unreliable */
51*4c7070dbSScott Long 	counter_u64_t		enqueues;
52*4c7070dbSScott Long 	counter_u64_t		drops;
53*4c7070dbSScott Long 	counter_u64_t		starts;
54*4c7070dbSScott Long 	counter_u64_t		stalls;
55*4c7070dbSScott Long 	counter_u64_t		restarts;	/* recovered after stalling */
56*4c7070dbSScott Long 	counter_u64_t		abdications;
57*4c7070dbSScott Long #ifdef NO_64BIT_ATOMICS
58*4c7070dbSScott Long 	struct mtx		lock;
59*4c7070dbSScott Long #endif
60*4c7070dbSScott Long 	void * volatile		items[] __aligned(CACHE_LINE_SIZE);
61*4c7070dbSScott Long };
62*4c7070dbSScott Long 
63*4c7070dbSScott Long int ifmp_ring_alloc(struct ifmp_ring **, int, void *, mp_ring_drain_t,
64*4c7070dbSScott Long     mp_ring_can_drain_t, struct malloc_type *, int);
65*4c7070dbSScott Long void ifmp_ring_free(struct ifmp_ring *);
66*4c7070dbSScott Long int ifmp_ring_enqueue(struct ifmp_ring *, void **, int, int);
67*4c7070dbSScott Long void ifmp_ring_check_drainage(struct ifmp_ring *, int);
68*4c7070dbSScott Long void ifmp_ring_reset_stats(struct ifmp_ring *);
69*4c7070dbSScott Long int ifmp_ring_is_idle(struct ifmp_ring *);
70*4c7070dbSScott Long int ifmp_ring_is_stalled(struct ifmp_ring *r);
71*4c7070dbSScott Long #endif
72