2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * From: @(#)if.h 8.1 (Berkeley) 6/10/93
37 #include <sys/mbuf.h> /* ifqueue only? */
38 #include <sys/buf_ring.h>
41 #include <sys/lock.h> /* XXX */
42 #include <sys/mutex.h> /* struct ifqueue */
45 * Couple of ugly extra definitions that are required since ifq.h
46 * is splitted from if_var.h.
48 #define IF_DUNIT_NONE -1
50 #include <net/altq/if_altq.h>
53 * Structure defining a queue for a network interface.
56 struct mbuf *ifq_head;
57 struct mbuf *ifq_tail;
65 * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq)
66 * are queues of messages stored on ifqueue structures
67 * (defined above). Entries are added to and deleted from these structures
70 #define IF_LOCK(ifq) mtx_lock(&(ifq)->ifq_mtx)
71 #define IF_UNLOCK(ifq) mtx_unlock(&(ifq)->ifq_mtx)
72 #define IF_LOCK_ASSERT(ifq) mtx_assert(&(ifq)->ifq_mtx, MA_OWNED)
73 #define _IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
74 #define _IF_QLEN(ifq) ((ifq)->ifq_len)
76 #define _IF_ENQUEUE(ifq, m) do { \
77 (m)->m_nextpkt = NULL; \
78 if ((ifq)->ifq_tail == NULL) \
79 (ifq)->ifq_head = m; \
81 (ifq)->ifq_tail->m_nextpkt = m; \
82 (ifq)->ifq_tail = m; \
86 #define IF_ENQUEUE(ifq, m) do { \
88 _IF_ENQUEUE(ifq, m); \
92 #define _IF_PREPEND(ifq, m) do { \
93 (m)->m_nextpkt = (ifq)->ifq_head; \
94 if ((ifq)->ifq_tail == NULL) \
95 (ifq)->ifq_tail = (m); \
96 (ifq)->ifq_head = (m); \
100 #define IF_PREPEND(ifq, m) do { \
102 _IF_PREPEND(ifq, m); \
106 #define _IF_DEQUEUE(ifq, m) do { \
107 (m) = (ifq)->ifq_head; \
109 if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL) \
110 (ifq)->ifq_tail = NULL; \
111 (m)->m_nextpkt = NULL; \
116 #define IF_DEQUEUE(ifq, m) do { \
118 _IF_DEQUEUE(ifq, m); \
122 #define _IF_DEQUEUE_ALL(ifq, m) do { \
123 (m) = (ifq)->ifq_head; \
124 (ifq)->ifq_head = (ifq)->ifq_tail = NULL; \
125 (ifq)->ifq_len = 0; \
128 #define IF_DEQUEUE_ALL(ifq, m) do { \
130 _IF_DEQUEUE_ALL(ifq, m); \
134 #define _IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
135 #define IF_POLL(ifq, m) _IF_POLL(ifq, m)
137 #define _IF_DRAIN(ifq) do { \
140 _IF_DEQUEUE(ifq, m); \
147 #define IF_DRAIN(ifq) do { \
153 int if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp,
155 #define IF_HANDOFF(ifq, m, ifp) \
156 if_handoff((struct ifqueue *)ifq, m, ifp, 0)
157 #define IF_HANDOFF_ADJ(ifq, m, ifp, adj) \
158 if_handoff((struct ifqueue *)ifq, m, ifp, adj)
160 void if_start(struct ifnet *);
162 #define IFQ_ENQUEUE(ifq, m, err) \
165 if (ALTQ_IS_ENABLED(ifq)) \
166 ALTQ_ENQUEUE(ifq, m, NULL, err); \
168 if (_IF_QFULL(ifq)) { \
172 _IF_ENQUEUE(ifq, m); \
179 #define IFQ_DEQUEUE_NOLOCK(ifq, m) \
181 if (TBR_IS_ENABLED(ifq)) \
182 (m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE); \
183 else if (ALTQ_IS_ENABLED(ifq)) \
184 ALTQ_DEQUEUE(ifq, m); \
186 _IF_DEQUEUE(ifq, m); \
189 #define IFQ_DEQUEUE(ifq, m) \
192 IFQ_DEQUEUE_NOLOCK(ifq, m); \
196 #define IFQ_POLL_NOLOCK(ifq, m) \
198 if (TBR_IS_ENABLED(ifq)) \
199 (m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL); \
200 else if (ALTQ_IS_ENABLED(ifq)) \
206 #define IFQ_POLL(ifq, m) \
209 IFQ_POLL_NOLOCK(ifq, m); \
213 #define IFQ_PURGE_NOLOCK(ifq) \
215 if (ALTQ_IS_ENABLED(ifq)) { \
221 #define IFQ_PURGE(ifq) \
224 IFQ_PURGE_NOLOCK(ifq); \
228 #define IFQ_SET_READY(ifq) \
229 do { ((ifq)->altq_flags |= ALTQF_READY); } while (0)
231 #define IFQ_LOCK(ifq) IF_LOCK(ifq)
232 #define IFQ_UNLOCK(ifq) IF_UNLOCK(ifq)
233 #define IFQ_LOCK_ASSERT(ifq) IF_LOCK_ASSERT(ifq)
234 #define IFQ_IS_EMPTY(ifq) ((ifq)->ifq_len == 0)
235 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++)
236 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len)
237 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len))
240 * The IFF_DRV_OACTIVE test should really occur in the device driver, not in
241 * the handoff logic, as that flag is locked by the device driver.
243 #define IFQ_HANDOFF_ADJ(ifp, m, adj, err) \
248 len = (m)->m_pkthdr.len; \
249 mflags = (m)->m_flags; \
250 IFQ_ENQUEUE(&(ifp)->if_snd, m, err); \
252 if_inc_counter((ifp), IFCOUNTER_OBYTES, len + (adj)); \
253 if (mflags & M_MCAST) \
254 if_inc_counter((ifp), IFCOUNTER_OMCASTS, 1); \
255 if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0) \
258 if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1); \
261 #define IFQ_HANDOFF(ifp, m, err) \
262 IFQ_HANDOFF_ADJ(ifp, m, 0, err)
264 #define IFQ_DRV_DEQUEUE(ifq, m) \
266 (m) = (ifq)->ifq_drv_head; \
268 if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL) \
269 (ifq)->ifq_drv_tail = NULL; \
270 (m)->m_nextpkt = NULL; \
271 (ifq)->ifq_drv_len--; \
274 IFQ_DEQUEUE_NOLOCK(ifq, m); \
275 while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) { \
277 IFQ_DEQUEUE_NOLOCK(ifq, m0); \
280 m0->m_nextpkt = NULL; \
281 if ((ifq)->ifq_drv_tail == NULL) \
282 (ifq)->ifq_drv_head = m0; \
284 (ifq)->ifq_drv_tail->m_nextpkt = m0; \
285 (ifq)->ifq_drv_tail = m0; \
286 (ifq)->ifq_drv_len++; \
292 #define IFQ_DRV_PREPEND(ifq, m) \
294 (m)->m_nextpkt = (ifq)->ifq_drv_head; \
295 if ((ifq)->ifq_drv_tail == NULL) \
296 (ifq)->ifq_drv_tail = (m); \
297 (ifq)->ifq_drv_head = (m); \
298 (ifq)->ifq_drv_len++; \
301 #define IFQ_DRV_IS_EMPTY(ifq) \
302 (((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0))
304 #define IFQ_DRV_PURGE(ifq) \
306 struct mbuf *m, *n = (ifq)->ifq_drv_head; \
307 while((m = n) != NULL) { \
311 (ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL; \
312 (ifq)->ifq_drv_len = 0; \
317 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
322 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
323 IFQ_ENQUEUE(&ifp->if_snd, m, error);
325 if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
329 error = buf_ring_enqueue(br, m);
337 drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *new)
340 * The top of the list needs to be swapped
344 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
346 * Peek in altq case dequeued it
349 IFQ_DRV_PREPEND(&ifp->if_snd, new);
353 buf_ring_putback_sc(br, new);
356 static __inline struct mbuf *
357 drbr_peek(struct ifnet *ifp, struct buf_ring *br)
361 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
363 * Pull it off like a dequeue
364 * since drbr_advance() does nothing
365 * for altq and drbr_putback() will
366 * use the old prepend function.
368 IFQ_DEQUEUE(&ifp->if_snd, m);
372 return(buf_ring_peek_clear_sc(br));
376 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
381 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
382 IFQ_PURGE(&ifp->if_snd);
384 while ((m = buf_ring_dequeue_sc(br)) != NULL)
389 drbr_free(struct buf_ring *br, struct malloc_type *type)
392 drbr_flush(NULL, br);
393 buf_ring_free(br, type);
396 static __inline struct mbuf *
397 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
402 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
403 IFQ_DEQUEUE(&ifp->if_snd, m);
407 return (buf_ring_dequeue_sc(br));
411 drbr_advance(struct ifnet *ifp, struct buf_ring *br)
414 /* Nothing to do here since peek dequeues in altq case */
415 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
418 return (buf_ring_advance_sc(br));
422 static __inline struct mbuf *
423 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
424 int (*func) (struct mbuf *, void *), void *arg)
428 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
429 IFQ_LOCK(&ifp->if_snd);
430 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
431 if (m != NULL && func(m, arg) == 0) {
432 IFQ_UNLOCK(&ifp->if_snd);
435 IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
436 IFQ_UNLOCK(&ifp->if_snd);
440 m = buf_ring_peek(br);
441 if (m == NULL || func(m, arg) == 0)
444 return (buf_ring_dequeue_sc(br));
448 drbr_empty(struct ifnet *ifp, struct buf_ring *br)
451 if (ALTQ_IS_ENABLED(&ifp->if_snd))
452 return (IFQ_IS_EMPTY(&ifp->if_snd));
454 return (buf_ring_empty(br));
458 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
461 if (ALTQ_IS_ENABLED(&ifp->if_snd))
464 return (!buf_ring_empty(br));
468 drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
471 if (ALTQ_IS_ENABLED(&ifp->if_snd))
472 return (ifp->if_snd.ifq_len);
474 return (buf_ring_count(br));
477 extern int ifqmaxlen;
479 void if_qflush(struct ifnet *);
480 void ifq_init(struct ifaltq *, struct ifnet *ifp);
481 void ifq_delete(struct ifaltq *);
484 #endif /* !_NET_IFQ_H_ */