xref: /freebsd/sys/net/ifq.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
18  *
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.
30  *
31  *	From: @(#)if.h	8.1 (Berkeley) 6/10/93
32  */
33 
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 
37 #ifndef ALTQ
38 #define	ALTQ	/* Needed for ifq.h prototypes only. */
39 #endif
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_private.h>
44 #include <net/ifq.h>
45 
46 int
47 drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
48 {
49 	int error = 0;
50 
51 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
52 		IFQ_ENQUEUE(&ifp->if_snd, m, error);
53 		if (error)
54 			if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
55 		return (error);
56 	}
57 	error = buf_ring_enqueue(br, m);
58 	if (error)
59 		m_freem(m);
60 
61 	return (error);
62 }
63 
64 void
65 drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
66 {
67 	/*
68 	 * The top of the list needs to be swapped
69 	 * for this one.
70 	 */
71 	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
72 		/*
73 		 * Peek in altq case dequeued it
74 		 * so put it back.
75 		 */
76 		IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
77 		return;
78 	}
79 	buf_ring_putback_sc(br, m_new);
80 }
81 
82 struct mbuf *
83 drbr_peek(struct ifnet *ifp, struct buf_ring *br)
84 {
85 	struct mbuf *m;
86 	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
87 		/*
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.
92 		 */
93 		IFQ_DEQUEUE(&ifp->if_snd, m);
94 		return (m);
95 	}
96 	return ((struct mbuf *)buf_ring_peek_clear_sc(br));
97 }
98 
99 void
100 drbr_flush(struct ifnet *ifp, struct buf_ring *br)
101 {
102 	struct mbuf *m;
103 
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)
107 		m_freem(m);
108 }
109 
110 struct mbuf *
111 drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
112 {
113 	struct mbuf *m;
114 
115 	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
116 		IFQ_DEQUEUE(&ifp->if_snd, m);
117 		return (m);
118 	}
119 	return ((struct mbuf *)buf_ring_dequeue_sc(br));
120 }
121 
122 void
123 drbr_advance(struct ifnet *ifp, struct buf_ring *br)
124 {
125 	/* Nothing to do here since peek dequeues in altq case */
126 	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
127 		return;
128 	return (buf_ring_advance_sc(br));
129 }
130 
131 struct mbuf *
132 drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
133     int (*func) (struct mbuf *, void *), void *arg)
134 {
135 	struct mbuf *m;
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);
141 			return (NULL);
142 		}
143 		IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
144 		IFQ_UNLOCK(&ifp->if_snd);
145 		return (m);
146 	}
147 	m = (struct mbuf *)buf_ring_peek(br);
148 	if (m == NULL || func(m, arg) == 0)
149 		return (NULL);
150 
151 	return ((struct mbuf *)buf_ring_dequeue_sc(br));
152 }
153 
154 int
155 drbr_empty(struct ifnet *ifp, struct buf_ring *br)
156 {
157 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
158 		return (IFQ_IS_EMPTY(&ifp->if_snd));
159 	return (buf_ring_empty(br));
160 }
161 
162 int
163 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
164 {
165 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
166 		return (1);
167 	return (!buf_ring_empty(br));
168 }
169 
170 int
171 drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
172 {
173 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
174 		return (ifp->if_snd.ifq_len);
175 	return (buf_ring_count(br));
176 }
177 
178