#include <sys/param.h>
#include <sys/socket.h>
#ifndef ALTQ
#define ALTQ
#endif
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/ifq.h>
int
drbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m)
{
int error = 0;
if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
IFQ_ENQUEUE(&ifp->if_snd, m, error);
if (error)
if_inc_counter((ifp), IFCOUNTER_OQDROPS, 1);
return (error);
}
error = buf_ring_enqueue(br, m);
if (error)
m_freem(m);
return (error);
}
void
drbr_putback(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m_new)
{
if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
IFQ_DRV_PREPEND(&ifp->if_snd, m_new);
return;
}
buf_ring_putback_sc(br, m_new);
}
struct mbuf *
drbr_peek(struct ifnet *ifp, struct buf_ring *br)
{
struct mbuf *m;
if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
IFQ_DEQUEUE(&ifp->if_snd, m);
return (m);
}
return ((struct mbuf *)buf_ring_peek_clear_sc(br));
}
void
drbr_flush(struct ifnet *ifp, struct buf_ring *br)
{
struct mbuf *m;
if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
IFQ_PURGE(&ifp->if_snd);
while ((m = (struct mbuf *)buf_ring_dequeue_sc(br)) != NULL)
m_freem(m);
}
struct mbuf *
drbr_dequeue(struct ifnet *ifp, struct buf_ring *br)
{
struct mbuf *m;
if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd)) {
IFQ_DEQUEUE(&ifp->if_snd, m);
return (m);
}
return ((struct mbuf *)buf_ring_dequeue_sc(br));
}
void
drbr_advance(struct ifnet *ifp, struct buf_ring *br)
{
if (ifp != NULL && ALTQ_IS_ENABLED(&ifp->if_snd))
return;
return (buf_ring_advance_sc(br));
}
struct mbuf *
drbr_dequeue_cond(struct ifnet *ifp, struct buf_ring *br,
int (*func) (struct mbuf *, void *), void *arg)
{
struct mbuf *m;
if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
IFQ_LOCK(&ifp->if_snd);
IFQ_POLL_NOLOCK(&ifp->if_snd, m);
if (m != NULL && func(m, arg) == 0) {
IFQ_UNLOCK(&ifp->if_snd);
return (NULL);
}
IFQ_DEQUEUE_NOLOCK(&ifp->if_snd, m);
IFQ_UNLOCK(&ifp->if_snd);
return (m);
}
m = (struct mbuf *)buf_ring_peek(br);
if (m == NULL || func(m, arg) == 0)
return (NULL);
return ((struct mbuf *)buf_ring_dequeue_sc(br));
}
int
drbr_empty(struct ifnet *ifp, struct buf_ring *br)
{
if (ALTQ_IS_ENABLED(&ifp->if_snd))
return (IFQ_IS_EMPTY(&ifp->if_snd));
return (buf_ring_empty(br));
}
int
drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
{
if (ALTQ_IS_ENABLED(&ifp->if_snd))
return (1);
return (!buf_ring_empty(br));
}
int
drbr_inuse(struct ifnet *ifp, struct buf_ring *br)
{
if (ALTQ_IS_ENABLED(&ifp->if_snd))
return (ifp->if_snd.ifq_len);
return (buf_ring_count(br));
}