11bfa548bSJustin Hibbits /*-
21bfa548bSJustin Hibbits * SPDX-License-Identifier: BSD-3-Clause
31bfa548bSJustin Hibbits *
41bfa548bSJustin Hibbits * Copyright (c) 1982, 1986, 1989, 1993
51bfa548bSJustin Hibbits * The Regents of the University of California. All rights reserved.
61bfa548bSJustin Hibbits *
71bfa548bSJustin Hibbits * Redistribution and use in source and binary forms, with or without
81bfa548bSJustin Hibbits * modification, are permitted provided that the following conditions
91bfa548bSJustin Hibbits * are met:
101bfa548bSJustin Hibbits * 1. Redistributions of source code must retain the above copyright
111bfa548bSJustin Hibbits * notice, this list of conditions and the following disclaimer.
121bfa548bSJustin Hibbits * 2. Redistributions in binary form must reproduce the above copyright
131bfa548bSJustin Hibbits * notice, this list of conditions and the following disclaimer in the
141bfa548bSJustin Hibbits * documentation and/or other materials provided with the distribution.
151bfa548bSJustin Hibbits * 3. Neither the name of the University nor the names of its contributors
161bfa548bSJustin Hibbits * may be used to endorse or promote products derived from this software
171bfa548bSJustin Hibbits * without specific prior written permission.
181bfa548bSJustin Hibbits *
191bfa548bSJustin Hibbits * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201bfa548bSJustin Hibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211bfa548bSJustin Hibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221bfa548bSJustin Hibbits * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231bfa548bSJustin Hibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241bfa548bSJustin Hibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251bfa548bSJustin Hibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261bfa548bSJustin Hibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271bfa548bSJustin Hibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281bfa548bSJustin Hibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291bfa548bSJustin Hibbits * SUCH DAMAGE.
301bfa548bSJustin Hibbits */
311bfa548bSJustin Hibbits
321bfa548bSJustin Hibbits #include <sys/param.h>
331bfa548bSJustin Hibbits #include <sys/socket.h>
341bfa548bSJustin Hibbits
351bfa548bSJustin Hibbits #ifndef ALTQ
361bfa548bSJustin Hibbits #define ALTQ /* Needed for ifq.h prototypes only. */
371bfa548bSJustin Hibbits #endif
381bfa548bSJustin Hibbits
391bfa548bSJustin Hibbits #include <net/if.h>
401bfa548bSJustin Hibbits #include <net/if_var.h>
41*3d0d5b21SJustin Hibbits #include <net/if_private.h>
421bfa548bSJustin Hibbits #include <net/ifq.h>
431bfa548bSJustin Hibbits
441bfa548bSJustin Hibbits int
drbr_enqueue(struct ifnet * ifp,struct buf_ring * br,struct mbuf * m)451bfa548bSJustin Hibbits drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
461bfa548bSJustin Hibbits {
471bfa548bSJustin Hibbits int error = 0;
481bfa548bSJustin Hibbits
491bfa548bSJustin Hibbits if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
501bfa548bSJustin Hibbits IFQ_ENQUEUE(&ifp->if_snd, m, error);
511bfa548bSJustin Hibbits if (error)
521bfa548bSJustin Hibbits if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
531bfa548bSJustin Hibbits return (error);
541bfa548bSJustin Hibbits }
551bfa548bSJustin Hibbits error = buf_ring_enqueue(br, m);
561bfa548bSJustin Hibbits if (error)
571bfa548bSJustin Hibbits m_freem(m);
581bfa548bSJustin Hibbits
591bfa548bSJustin Hibbits return (error);
601bfa548bSJustin Hibbits }
611bfa548bSJustin Hibbits
621bfa548bSJustin Hibbits void
drbr_putback(struct ifnet * ifp,struct buf_ring * br,struct mbuf * m_new)631bfa548bSJustin Hibbits drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
641bfa548bSJustin Hibbits {
651bfa548bSJustin Hibbits /*
661bfa548bSJustin Hibbits * The top of the list needs to be swapped
671bfa548bSJustin Hibbits * for this one.
681bfa548bSJustin Hibbits */
691bfa548bSJustin Hibbits if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
701bfa548bSJustin Hibbits /*
711bfa548bSJustin Hibbits * Peek in altq case dequeued it
721bfa548bSJustin Hibbits * so put it back.
731bfa548bSJustin Hibbits */
741bfa548bSJustin Hibbits IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
751bfa548bSJustin Hibbits return;
761bfa548bSJustin Hibbits }
771bfa548bSJustin Hibbits buf_ring_putback_sc(br, m_new);
781bfa548bSJustin Hibbits }
791bfa548bSJustin Hibbits
801bfa548bSJustin Hibbits struct mbuf *
drbr_peek(struct ifnet * ifp,struct buf_ring * br)811bfa548bSJustin Hibbits drbr_peek(struct ifnet *ifp, struct buf_ring *br)
821bfa548bSJustin Hibbits {
831bfa548bSJustin Hibbits struct mbuf *m;
841bfa548bSJustin Hibbits if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
851bfa548bSJustin Hibbits /*
861bfa548bSJustin Hibbits * Pull it off like a dequeue
871bfa548bSJustin Hibbits * since drbr_advance() does nothing
881bfa548bSJustin Hibbits * for altq and drbr_putback() will
891bfa548bSJustin Hibbits * use the old prepend function.
901bfa548bSJustin Hibbits */
911bfa548bSJustin Hibbits IFQ_DEQUEUE(&ifp->if_snd, m);
921bfa548bSJustin Hibbits return (m);
931bfa548bSJustin Hibbits }
941bfa548bSJustin Hibbits return ((struct mbuf *)buf_ring_peek_clear_sc(br));
951bfa548bSJustin Hibbits }
961bfa548bSJustin Hibbits
971bfa548bSJustin Hibbits void
drbr_flush(struct ifnet * ifp,struct buf_ring * br)981bfa548bSJustin Hibbits drbr_flush(struct ifnet *ifp, struct buf_ring *br)
991bfa548bSJustin Hibbits {
1001bfa548bSJustin Hibbits struct mbuf *m;
1011bfa548bSJustin Hibbits
1021bfa548bSJustin Hibbits if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
1031bfa548bSJustin Hibbits IFQ_PURGE(&ifp->if_snd);
1041bfa548bSJustin Hibbits while ((m = (struct mbuf *)buf_ring_dequeue_sc(br)) != NULL)
1051bfa548bSJustin Hibbits m_freem(m);
1061bfa548bSJustin Hibbits }
1071bfa548bSJustin Hibbits
1081bfa548bSJustin Hibbits struct mbuf *
drbr_dequeue(struct ifnet * ifp,struct buf_ring * br)1091bfa548bSJustin Hibbits drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
1101bfa548bSJustin Hibbits {
1111bfa548bSJustin Hibbits struct mbuf *m;
1121bfa548bSJustin Hibbits
1131bfa548bSJustin Hibbits if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
1141bfa548bSJustin Hibbits IFQ_DEQUEUE(&ifp->if_snd, m);
1151bfa548bSJustin Hibbits return (m);
1161bfa548bSJustin Hibbits }
1171bfa548bSJustin Hibbits return ((struct mbuf *)buf_ring_dequeue_sc(br));
1181bfa548bSJustin Hibbits }
1191bfa548bSJustin Hibbits
1201bfa548bSJustin Hibbits void
drbr_advance(struct ifnet * ifp,struct buf_ring * br)1211bfa548bSJustin Hibbits drbr_advance(struct ifnet *ifp, struct buf_ring *br)
1221bfa548bSJustin Hibbits {
1231bfa548bSJustin Hibbits /* Nothing to do here since peek dequeues in altq case */
1241bfa548bSJustin Hibbits if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
1251bfa548bSJustin Hibbits return;
1261bfa548bSJustin Hibbits return (buf_ring_advance_sc(br));
1271bfa548bSJustin Hibbits }
1281bfa548bSJustin Hibbits
1291bfa548bSJustin Hibbits struct mbuf *
drbr_dequeue_cond(struct ifnet * ifp,struct buf_ring * br,int (* func)(struct mbuf *,void *),void * arg)1301bfa548bSJustin Hibbits drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
1311bfa548bSJustin Hibbits int (*func) (struct mbuf *, void *), void *arg)
1321bfa548bSJustin Hibbits {
1331bfa548bSJustin Hibbits struct mbuf *m;
1341bfa548bSJustin Hibbits if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1351bfa548bSJustin Hibbits IFQ_LOCK(&ifp->if_snd);
1361bfa548bSJustin Hibbits IFQ_POLL_NOLOCK(&ifp->if_snd, m);
1371bfa548bSJustin Hibbits if (m != NULL && func(m, arg) == 0) {
1381bfa548bSJustin Hibbits IFQ_UNLOCK(&ifp->if_snd);
1391bfa548bSJustin Hibbits return (NULL);
1401bfa548bSJustin Hibbits }
1411bfa548bSJustin Hibbits IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
1421bfa548bSJustin Hibbits IFQ_UNLOCK(&ifp->if_snd);
1431bfa548bSJustin Hibbits return (m);
1441bfa548bSJustin Hibbits }
1451bfa548bSJustin Hibbits m = (struct mbuf *)buf_ring_peek(br);
1461bfa548bSJustin Hibbits if (m == NULL || func(m, arg) == 0)
1471bfa548bSJustin Hibbits return (NULL);
1481bfa548bSJustin Hibbits
1491bfa548bSJustin Hibbits return ((struct mbuf *)buf_ring_dequeue_sc(br));
1501bfa548bSJustin Hibbits }
1511bfa548bSJustin Hibbits
1521bfa548bSJustin Hibbits int
drbr_empty(struct ifnet * ifp,struct buf_ring * br)1531bfa548bSJustin Hibbits drbr_empty(struct ifnet *ifp, struct buf_ring *br)
1541bfa548bSJustin Hibbits {
1551bfa548bSJustin Hibbits if (ALTQ_IS_ENABLED(&ifp->if_snd))
1561bfa548bSJustin Hibbits return (IFQ_IS_EMPTY(&ifp->if_snd));
1571bfa548bSJustin Hibbits return (buf_ring_empty(br));
1581bfa548bSJustin Hibbits }
1591bfa548bSJustin Hibbits
1601bfa548bSJustin Hibbits int
drbr_needs_enqueue(struct ifnet * ifp,struct buf_ring * br)1611bfa548bSJustin Hibbits drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
1621bfa548bSJustin Hibbits {
1631bfa548bSJustin Hibbits if (ALTQ_IS_ENABLED(&ifp->if_snd))
1641bfa548bSJustin Hibbits return (1);
1651bfa548bSJustin Hibbits return (!buf_ring_empty(br));
1661bfa548bSJustin Hibbits }
1671bfa548bSJustin Hibbits
1681bfa548bSJustin Hibbits int
drbr_inuse(struct ifnet * ifp,struct buf_ring * br)1691bfa548bSJustin Hibbits drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
1701bfa548bSJustin Hibbits {
1711bfa548bSJustin Hibbits if (ALTQ_IS_ENABLED(&ifp->if_snd))
1721bfa548bSJustin Hibbits return (ifp->if_snd.ifq_len);
1731bfa548bSJustin Hibbits return (buf_ring_count(br));
1741bfa548bSJustin Hibbits }
1751bfa548bSJustin Hibbits
176