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