1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
.\" $OpenBSD: mq_init.9,v 1.6 2015/11/21 11:46:24 mpi Exp $
.\"
.\" Copyright (c) 2015 David Gwynne <dlg@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: November 21 2015 $
.Dt MQ_INIT 9
.Os
.Sh NAME
.Nm mq_init ,
.Nm mq_enqueue ,
.Nm mq_dequeue ,
.Nm mq_enlist ,
.Nm mq_delist ,
.Nm mq_dechain ,
.Nm mq_len ,
.Nm mq_empty ,
.Nm mq_purge ,
.Nm mq_drops ,
.Nm mq_set_maxlen ,
.Nm MBUF_QUEUE_INITIALIZER
.Nd mbuf queue API
.Sh SYNOPSIS
.In sys/mbuf.h
.Ft void
.Fn mq_init "struct mbuf_queue *mq" "unsigned int maxlen" "int ipl"
.Ft int
.Fn mq_enqueue "struct mbuf_queue *mq" "struct mbuf *m"
.Ft struct mbuf *
.Fn mq_dequeue "struct mbuf_queue *mq"
.Ft int
.Fn mq_enlist "struct mbuf_queue *mq" "struct mbuf_list *ml"
.Ft void
.Fn mq_delist "struct mbuf_queue *mq" "struct mbuf_list *ml"
.Ft struct mbuf *
.Fn mq_dechain "struct mbuf_queue *mq"
.Ft struct mbuf *
.Fo mq_filter
.Fa "struct mbuf_queue *mq"
.Fa "int (*filter)(void *, struct mbuf *)"
.Fa "void *context"
.Fc
.Ft unsigned int
.Fn mq_len "struct mbuf_queue *mq"
.Ft int
.Fn mq_empty "struct mbuf_queue *mq"
.Ft unsigned int
.Fn mq_purge "struct mbuf_queue *mq"
.Ft unsigned int
.Fn mq_drops "struct mbuf_queue *mq"
.Ft void
.Fn mq_set_maxlen "struct mbuf_queue *mq" "unsigned int"
.Ft struct mbuf_queue
.Fn MBUF_QUEUE_INITIALIZER "unsigned int maxlen" "int ipl"
.Sh DESCRIPTION
The mbuf queue API provides implementions of data structures and operations
for queueing mbufs and lists of mbufs between contexts.
.Pp
mbuf_queue data structures provide a superset of the functionality
available in mbuf_lists, and protect themselves internally with a
.Xr mutex 9 ,
making them useful for moving mbufs between contexts or subsystems.
Additionally, mbuf_queues provide a limit on the number of mbufs that
may be queued.
.Pp
mbuf_queue structures support the following functionality:
.Pp
.Bl -enum -compact -offset indent
.It
Insertion of a new mbuf at the end of the queue.
.It
Removal of an mbuf from the head of the queue.
.It
Reinsertion of an mbuf at the head of the queue.
.It
Removal of the entire chain of mbufs on the queue.
.It
Insertion of the mbufs in an mbuf_list at the end of the queue.
.It
Removal of all the mbufs on the queue as an mbuf_list.
.El
.Bl -tag -width Ds
.It Fn mq_init "struct mbuf_queue *mq" "unsigned int maxlen" "int ipl"
Initialises the mbuf queue structure
.Fa mq .
The maximum number of mbufs that should be queued is specified with
.Fa maxlen .
The highest interrupt priority level the queue will be operated at is
specified via
.Fa ipl .
.It Fn MBUF_QUEUE_INITIALIZER "unsigned int maxlen" "int ipl"
Initialises an mbuf queue structure declaration.
The maximum number of mbufs that should be queued is specified with
.Fa maxlen .
The highest interrupt priority level the queue will be operated at is
specified via
.Fa ipl .
.It Fn mq_enqueue "struct mbuf_queue *mq" "struct mbuf *m"
Enqueue mbuf
.Fa m
on the end of the
.Fa mq
mbuf queue.
.It Fn mq_dequeue "struct mbuf_queue *mq"
Dequeue an mbuf from the front of the
.Fa mq
mbuf queue.
.It Fn mq_enlist "struct mbuf_queue *mq" "struct mbuf_list *ml"
Enqueue all the mbufs on the
.Fa ml
mbuf list on to the end of the
.Fa mq
mbuf queue.
Note, the number of mbufs placed on the queue may exceed its maximum length.
.It Fn mq_delist "struct mbuf_queue *mq" "struct mbuf_list *ml"
Dequeue all the mbufs on the
.Fa mq
mbuf queue on to the
.Fa ml
mbuf list.
.It Fn mq_dechain "struct mbuf_queue *mq"
Dequeue all mbufs from the
.Fa mq
mbuf queue.
.It Fo mq_filter
.Fa "struct mbuf_queue *mq"
.Fa "int (*filter)(void *, struct mbuf *)"
.Fa "void *context"
.Fc
Iterates over the mbufs on the
.Fa mq
mbuf queue, passing each of them to the
.Fa filter
function.
If the
.Fa filter
returns non-zero, the packet is removed from the
.Fa mq
mbuf queue to be returned as part of an mbuf chain by
.Fn mq_filter .
.Fa context
is passed as the first argument to each call of
.Fa filter .
.It Fn mq_len "struct mbuf_queue *mq"
Return the number of mbufs on the
.Fa mq
mbuf queue.
.It Fn mq_empty "struct mbuf_queue *mq"
Return if the
.Fa mq
mbuf queue is empty.
.It Fn mq_purge "struct mbuf_queue *mq"
Free all the mbufs on the
.Fa mq
mbuf queue.
.It Fn mq_drops "struct mbuf_queue *mq"
Return how many mbufs were dropped and freed by
.Xr m_freem 9
if the
.Fa mq
mbuf queue was too full.
.It Fn mq_set_maxlen "struct mbuf_queue *mq" "unsigned int"
Alter the maximum number of mbufs that should be queued on the
.Fa mq
mbuf queue.
Note,
.Fn mq_set_maxlen
will only set a new limit, it will not free any excess mbufs that may
already exist on the queue.
.El
.Sh CONTEXT
.Fn mq_init ,
.Fn mq_enqueue ,
.Fn mq_dequeue ,
.Fn mq_enlist ,
.Fn mq_delist ,
.Fn mq_dechain ,
.Fn mq_len ,
.Fn mq_empty ,
.Fn mq_purge ,
.Fn mq_drops ,
.Fn mq_set_maxlen ,
and
.Fn MBUF_QUEUE_INITIALIZER
can be called during autoconf, from process context, or from interrupt context.
.Sh RETURN VALUES
.Fn mq_dequeue
returns the mbuf that was at the head of its queue.
If the queue was empty,
.Dv NULL
is returned.
.Pp
.Fn mq_dechain
returns all the mbufs that were on its queues via a pointer to an mbuf
with the chain accessible via m_nextpkt members.
If the queue was empty,
.Dv NULL
is returned.
.Pp
.Fn mq_filter
returns the mbufs that were successfully matched by the filter
function on the queue via a pointer to a chain of mbufs.
If no packets matched the filter,
.Dv NULL
is returned.
.Pp
.Fn mq_len
returns the number of mbufs on the queue.
.Pp
.Fn mq_empty
returns a non-zero value if the queue is empty, otherwise 0.
.Pp
.Fn mq_enqueue
returns 0 if the mbuf was successfully queued, or non-zero if the
mbuf was freed because it would cause the queue to exceed its maximum
length.
.Pp
.Fn mq_enlist
returns the number of mbufs that were dropped from the list if the
length of the queue exceeded its maximum length.
.Pp
.Fn mq_purge
returns the number of mbufs that were freed.
.Pp
.Fn mq_drops
returns the number of mbufs that were freed during
.Fn mq_enqueue
operations that would have caused the queue to exceed its maximum length.
.Sh SEE ALSO
.Xr mbuf 9 ,
.Xr ml_init 9 ,
.Xr mutex 9
|