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
32#include <sys/param.h>
33#include <sys/socket.h>
34
35#ifndef ALTQ
36#define	ALTQ	/* Needed for ifq.h prototypes only. */
37#endif
38
39#include <net/if.h>
40#include <net/if_var.h>
41#include <net/if_private.h>
42#include <net/ifq.h>
43
44int
45drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
46{
47	int error = 0;
48
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);
54	}
55	error = buf_ring_enqueue(br, m);
56	if (error)
57		m_freem(m);
58
59	return (error);
60}
61
62void
63drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
64{
65	/*
66	 * The top of the list needs to be swapped
67	 * for this one.
68	 */
69	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
70		/*
71		 * Peek in altq case dequeued it
72		 * so put it back.
73		 */
74		IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
75		return;
76	}
77	buf_ring_putback_sc(br, m_new);
78}
79
80struct mbuf *
81drbr_peek(struct ifnet *ifp, struct buf_ring *br)
82{
83	struct mbuf *m;
84	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
85		/*
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.
90		 */
91		IFQ_DEQUEUE(&ifp->if_snd, m);
92		return (m);
93	}
94	return ((struct mbuf *)buf_ring_peek_clear_sc(br));
95}
96
97void
98drbr_flush(struct ifnet *ifp, struct buf_ring *br)
99{
100	struct mbuf *m;
101
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);
106}
107
108struct mbuf *
109drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
110{
111	struct mbuf *m;
112
113	if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
114		IFQ_DEQUEUE(&ifp->if_snd, m);
115		return (m);
116	}
117	return ((struct mbuf *)buf_ring_dequeue_sc(br));
118}
119
120void
121drbr_advance(struct ifnet *ifp, struct buf_ring *br)
122{
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));
127}
128
129struct mbuf *
130drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
131    int (*func) (struct mbuf *, void *), void *arg)
132{
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);
140		}
141		IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
142		IFQ_UNLOCK(&ifp->if_snd);
143		return (m);
144	}
145	m = (struct mbuf *)buf_ring_peek(br);
146	if (m == NULL || func(m, arg) == 0)
147		return (NULL);
148
149	return ((struct mbuf *)buf_ring_dequeue_sc(br));
150}
151
152int
153drbr_empty(struct ifnet *ifp, struct buf_ring *br)
154{
155	if (ALTQ_IS_ENABLED(&ifp->if_snd))
156		return (IFQ_IS_EMPTY(&ifp->if_snd));
157	return (buf_ring_empty(br));
158}
159
160int
161drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
162{
163	if (ALTQ_IS_ENABLED(&ifp->if_snd))
164		return (1);
165	return (!buf_ring_empty(br));
166}
167
168int
169drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
170{
171	if (ALTQ_IS_ENABLED(&ifp->if_snd))
172		return (ifp->if_snd.ifq_len);
173	return (buf_ring_count(br));
174}
175
176