mtw(4) remove misplaced DEBUG_FLAGS
[freebsd/src.git] / sys / net / ifq.c
blob27094d5641fd55e0915b1eb1a56819c5aa806c1d
1 /*-
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
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 * 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
29 * SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/socket.h>
35 #ifndef ALTQ
36 #define ALTQ /* Needed for ifq.h prototypes only. */
37 #endif
39 #include <net/if.h>
40 #include <net/if_var.h>
41 #include <net/if_private.h>
42 #include <net/ifq.h>
44 int
45 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
47 int error = 0;
49 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
50 IFQ_ENQUEUE(&ifp->if_snd, m, error);
51 if (error)
52 if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
53 return (error);
55 error = buf_ring_enqueue(br, m);
56 if (error)
57 m_freem(m);
59 return (error);
62 void
63 drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
66 * The top of the list needs to be swapped
67 * for this one.
69 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
71 * Peek in altq case dequeued it
72 * so put it back.
74 IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
75 return;
77 buf_ring_putback_sc(br, m_new);
80 struct mbuf *
81 drbr_peek(struct ifnet *ifp, struct buf_ring *br)
83 struct mbuf *m;
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);
92 return (m);
94 return ((struct mbuf *)buf_ring_peek_clear_sc(br));
97 void
98 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
100 struct mbuf *m;
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)
105 m_freem(m);
108 struct mbuf *
109 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
111 struct mbuf *m;
113 if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
114 IFQ_DEQUEUE(&ifp->if_snd, m);
115 return (m);
117 return ((struct mbuf *)buf_ring_dequeue_sc(br));
120 void
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))
125 return;
126 return (buf_ring_advance_sc(br));
129 struct mbuf *
130 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
131 int (*func) (struct mbuf *, void *), void *arg)
133 struct mbuf *m;
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);
139 return (NULL);
141 IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
142 IFQ_UNLOCK(&ifp->if_snd);
143 return (m);
145 m = (struct mbuf *)buf_ring_peek(br);
146 if (m == NULL || func(m, arg) == 0)
147 return (NULL);
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))
164 return (1);
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));