2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * From: @(#)if.h 8.1 (Berkeley) 6/10/93
34 #include <sys/param.h>
35 #include <sys/socket.h>
38 #define ALTQ /* Needed for ifq.h prototypes only. */
42 #include <net/if_var.h>
43 #include <net/if_private.h>
47 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
51 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
52 IFQ_ENQUEUE(&ifp->if_snd, m, error);
54 if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
57 error = buf_ring_enqueue(br, m);
65 drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
68 * The top of the list needs to be swapped
71 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
73 * Peek in altq case dequeued it
76 IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
79 buf_ring_putback_sc(br, m_new);
83 drbr_peek(struct ifnet *ifp, struct buf_ring *br)
86 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
88 * Pull it off like a dequeue
89 * since drbr_advance() does nothing
90 * for altq and drbr_putback() will
91 * use the old prepend function.
93 IFQ_DEQUEUE(&ifp->if_snd, m);
96 return ((struct mbuf *)buf_ring_peek_clear_sc(br));
100 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
104 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
105 IFQ_PURGE(&ifp->if_snd);
106 while ((m = (struct mbuf *)buf_ring_dequeue_sc(br)) != NULL)
111 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
115 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
116 IFQ_DEQUEUE(&ifp->if_snd, m);
119 return ((struct mbuf *)buf_ring_dequeue_sc(br));
123 drbr_advance(struct ifnet *ifp, struct buf_ring *br)
125 /* Nothing to do here since peek dequeues in altq case */
126 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
128 return (buf_ring_advance_sc(br));
132 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
133 int (*func) (struct mbuf *, void *), void *arg)
136 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
137 IFQ_LOCK(&ifp->if_snd);
138 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
139 if (m != NULL && func(m, arg) == 0) {
140 IFQ_UNLOCK(&ifp->if_snd);
143 IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
144 IFQ_UNLOCK(&ifp->if_snd);
147 m = (struct mbuf *)buf_ring_peek(br);
148 if (m == NULL || func(m, arg) == 0)
151 return ((struct mbuf *)buf_ring_dequeue_sc(br));
155 drbr_empty(struct ifnet *ifp, struct buf_ring *br)
157 if (ALTQ_IS_ENABLED(&ifp->if_snd))
158 return (IFQ_IS_EMPTY(&ifp->if_snd));
159 return (buf_ring_empty(br));
163 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
165 if (ALTQ_IS_ENABLED(&ifp->if_snd))
167 return (!buf_ring_empty(br));
171 drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
173 if (ALTQ_IS_ENABLED(&ifp->if_snd))
174 return (ifp->if_snd.ifq_len);
175 return (buf_ring_count(br));