xref: /freebsd-13.1/sys/dev/netmap/netmap_mbq.h (revision 718cf2cc)
1*718cf2ccSPedro F. Giffuni /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2014 Vincenzo Maffione
537e3a6d3SLuigi Rizzo  * All rights reserved.
6f9790aebSLuigi Rizzo  *
7f9790aebSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
8f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
9f9790aebSLuigi Rizzo  * are met:
10f9790aebSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
11f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
12f9790aebSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
13f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
14f9790aebSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
15f9790aebSLuigi Rizzo  *
16f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f9790aebSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f9790aebSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f9790aebSLuigi Rizzo  * SUCH DAMAGE.
27f9790aebSLuigi Rizzo  */
28f9790aebSLuigi Rizzo 
29f9790aebSLuigi Rizzo /*
30f9790aebSLuigi Rizzo  * $FreeBSD$
31f9790aebSLuigi Rizzo  */
32f9790aebSLuigi Rizzo 
33f9790aebSLuigi Rizzo 
34c3e9b4dbSLuiz Otavio O Souza #ifndef _NET_NETMAP_MBQ_H__
35c3e9b4dbSLuiz Otavio O Souza #define _NET_NETMAP_MBQ_H__
36f9790aebSLuigi Rizzo 
37f9790aebSLuigi Rizzo /*
38f9790aebSLuigi Rizzo  * These function implement an mbuf tailq with an optional lock.
39f9790aebSLuigi Rizzo  * The base functions act ONLY ON THE QUEUE, whereas the "safe"
40f9790aebSLuigi Rizzo  * variants (mbq_safe_*) also handle the lock.
41f9790aebSLuigi Rizzo  */
42f9790aebSLuigi Rizzo 
43f9790aebSLuigi Rizzo /* XXX probably rely on a previous definition of SPINLOCK_T */
44f9790aebSLuigi Rizzo #ifdef linux
45f9790aebSLuigi Rizzo #define SPINLOCK_T  safe_spinlock_t
4637e3a6d3SLuigi Rizzo #elif defined (_WIN32)
4737e3a6d3SLuigi Rizzo #define SPINLOCK_T 	win_spinlock_t
48f9790aebSLuigi Rizzo #else
49f9790aebSLuigi Rizzo #define SPINLOCK_T  struct mtx
50f9790aebSLuigi Rizzo #endif
51f9790aebSLuigi Rizzo 
52f9790aebSLuigi Rizzo /* A FIFO queue of mbufs with an optional lock. */
53f9790aebSLuigi Rizzo struct mbq {
54f9790aebSLuigi Rizzo     struct mbuf *head;
55f9790aebSLuigi Rizzo     struct mbuf *tail;
56f9790aebSLuigi Rizzo     int count;
57f9790aebSLuigi Rizzo     SPINLOCK_T lock;
58f9790aebSLuigi Rizzo };
59f9790aebSLuigi Rizzo 
6037e3a6d3SLuigi Rizzo /* We should clarify whether init can be used while
61f9790aebSLuigi Rizzo  * holding a lock, and whether mbq_safe_destroy() is a NOP.
62f9790aebSLuigi Rizzo  */
63f9790aebSLuigi Rizzo void mbq_init(struct mbq *q);
6437e3a6d3SLuigi Rizzo void mbq_fini(struct mbq *q);
65f9790aebSLuigi Rizzo void mbq_enqueue(struct mbq *q, struct mbuf *m);
66f9790aebSLuigi Rizzo struct mbuf *mbq_dequeue(struct mbq *q);
67f9790aebSLuigi Rizzo void mbq_purge(struct mbq *q);
68f9790aebSLuigi Rizzo 
6937e3a6d3SLuigi Rizzo static inline struct mbuf *
mbq_peek(struct mbq * q)7037e3a6d3SLuigi Rizzo mbq_peek(struct mbq *q)
7137e3a6d3SLuigi Rizzo {
72c3e9b4dbSLuiz Otavio O Souza 	return q->head;
7337e3a6d3SLuigi Rizzo }
7437e3a6d3SLuigi Rizzo 
75997b054cSLuigi Rizzo static inline void
mbq_lock(struct mbq * q)76997b054cSLuigi Rizzo mbq_lock(struct mbq *q)
77997b054cSLuigi Rizzo {
78997b054cSLuigi Rizzo 	mtx_lock_spin(&q->lock);
79997b054cSLuigi Rizzo }
80997b054cSLuigi Rizzo 
81997b054cSLuigi Rizzo static inline void
mbq_unlock(struct mbq * q)82997b054cSLuigi Rizzo mbq_unlock(struct mbq *q)
83997b054cSLuigi Rizzo {
84997b054cSLuigi Rizzo 	mtx_unlock_spin(&q->lock);
85997b054cSLuigi Rizzo }
86f9790aebSLuigi Rizzo 
874bf50f18SLuigi Rizzo 
88f9790aebSLuigi Rizzo void mbq_safe_init(struct mbq *q);
8937e3a6d3SLuigi Rizzo void mbq_safe_fini(struct mbq *q);
90f9790aebSLuigi Rizzo void mbq_safe_enqueue(struct mbq *q, struct mbuf *m);
91f9790aebSLuigi Rizzo struct mbuf *mbq_safe_dequeue(struct mbq *q);
92f9790aebSLuigi Rizzo void mbq_safe_purge(struct mbq *q);
93f9790aebSLuigi Rizzo 
mbq_len(struct mbq * q)94f9790aebSLuigi Rizzo static inline unsigned int mbq_len(struct mbq *q)
95f9790aebSLuigi Rizzo {
96f9790aebSLuigi Rizzo     return q->count;
97f9790aebSLuigi Rizzo }
98f9790aebSLuigi Rizzo 
99c3e9b4dbSLuiz Otavio O Souza #endif /* _NET_NETMAP_MBQ_H_ */
100