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
32 #include <sys/param.h>
33 #include <sys/socket.h>
36 #define ALTQ /* Needed for ifq.h prototypes only. */
40 #include <net/if_var.h>
41 #include <net/if_private.h>
45 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
49 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
50 IFQ_ENQUEUE(&ifp->if_snd, m, error);
52 if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
55 error = buf_ring_enqueue(br, m);
63 drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
66 * The top of the list needs to be swapped
69 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
71 * Peek in altq case dequeued it
74 IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
77 buf_ring_putback_sc(br, m_new);
81 drbr_peek(struct ifnet *ifp, struct buf_ring *br)
84 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
86 * Pull it off like a dequeue
87 * since drbr_advance() does nothing
88 * for altq and drbr_putback() will
89 * use the old prepend function.
91 IFQ_DEQUEUE(&ifp->if_snd, m);
94 return ((struct mbuf *)buf_ring_peek_clear_sc(br));
98 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
102 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
103 IFQ_PURGE(&ifp->if_snd);
104 while ((m = (struct mbuf *)buf_ring_dequeue_sc(br)) != NULL)
109 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
113 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
114 IFQ_DEQUEUE(&ifp->if_snd, m);
117 return ((struct mbuf *)buf_ring_dequeue_sc(br));
121 drbr_advance(struct ifnet *ifp, struct buf_ring *br)
123 /* Nothing to do here since peek dequeues in altq case */
124 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
126 return (buf_ring_advance_sc(br));
130 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
131 int (*func) (struct mbuf *, void *), void *arg)
134 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
135 IFQ_LOCK(&ifp->if_snd);
136 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
137 if (m != NULL && func(m, arg) == 0) {
138 IFQ_UNLOCK(&ifp->if_snd);
141 IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
142 IFQ_UNLOCK(&ifp->if_snd);
145 m = (struct mbuf *)buf_ring_peek(br);
146 if (m == NULL || func(m, arg) == 0)
149 return ((struct mbuf *)buf_ring_dequeue_sc(br));
153 drbr_empty(struct ifnet *ifp, struct buf_ring *br)
155 if (ALTQ_IS_ENABLED(&ifp->if_snd))
156 return (IFQ_IS_EMPTY(&ifp->if_snd));
157 return (buf_ring_empty(br));
161 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
163 if (ALTQ_IS_ENABLED(&ifp->if_snd))
165 return (!buf_ring_empty(br));
169 drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
171 if (ALTQ_IS_ENABLED(&ifp->if_snd))
172 return (ifp->if_snd.ifq_len);
173 return (buf_ring_count(br));