FreeBSD/sys/net/altq/altq_fairq.c
/*
 * fairq_dequeue is a dequeue function to be registered to
 * (*altq_dequeue) in struct ifaltq.
 *
 * note: ALTDQ_POLL returns the next packet without removing the packet
 *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
 *	ALTDQ_REMOVE must return the same packet if called immediately
 *	after ALTDQ_POLL.
 */
fairq_dequeue is a dequeue function to be registered to
(*altq_dequeue) in struct ifaltq.

ALTDQ_POLL returns the next packet without removing the packet
from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
ALTDQ_REMOVE must return the same packet if called immediately
after ALTDQ_POLL.
fairq_dequeue is a dequeue function to be registered to ( * altq_dequeue ) in struct ifaltq . ALTDQ_POLL returns the next packet without removing the packet from the queue . ALTDQ_REMOVE is a normal dequeue operation . ALTDQ_REMOVE must return the same packet if called immediately after ALTDQ_POLL .
static struct mbuf *
fairq_dequeue(struct ifaltq *ifq, int op)
{
	struct fairq_if *pif = (struct fairq_if *)ifq->altq_disc;
	struct fairq_class *cl;
	struct fairq_class *best_cl;
	struct mbuf *best_m;
	struct mbuf *m = NULL;
	uint64_t cur_time = read_machclk();
	int pri;
	int hit_limit;
	IFQ_LOCK_ASSERT(ifq);
	if (IFQ_IS_EMPTY(ifq)) {
		return (NULL);
	}
	if (pif->pif_poll_cache && op == ALTDQ_REMOVE) {
		best_cl = pif->pif_poll_cache;
		m = fairq_getq(best_cl, cur_time);
		pif->pif_poll_cache = NULL;
		if (m) {
			IFQ_DEC_LEN(ifq);
			PKTCNTR_ADD(&best_cl->cl_xmitcnt, m_pktlen(m));
			return (m);
		}
	} else {
		best_cl = NULL;
		best_m = NULL;
		for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
			if ((cl = pif->pif_classes[pri]) == NULL)
				continue;
			if ((cl->cl_flags & FARF_HAS_PACKETS) == 0)
				continue;
			m = fairq_pollq(cl, cur_time, &hit_limit);
			if (m == NULL) {
				cl->cl_flags &= ~FARF_HAS_PACKETS;
				continue;
			}
			if (hit_limit == 0 || best_cl == NULL) {
				best_cl = cl;
				best_m = m;
			}
			if (hit_limit)
				continue;
			break;
		}
		if (op == ALTDQ_POLL) {
			pif->pif_poll_cache = best_cl;
			m = best_m;
		} else if (best_cl) {
			m = fairq_getq(best_cl, cur_time);
			if (m != NULL) {
				IFQ_DEC_LEN(ifq);
				PKTCNTR_ADD(&best_cl->cl_xmitcnt, m_pktlen(m));
			}
		} 
		return (m);
	}
	return (NULL);
}
static struct mbuf * fairq_dequeue ( struct ifaltq * ifq , int op ) { struct fairq_if * pif = ( struct fairq_if * ) ifq -> altq_disc ; struct fairq_class * cl ; struct fairq_class * best_cl ; struct mbuf * best_m ; struct mbuf * m = NULL ; uint64_t cur_time = read_machclk ( ) ; int pri ; int hit_limit ; IFQ_LOCK_ASSERT ( ifq ) ; if ( IFQ_IS_EMPTY ( ifq ) ) { return ( NULL ) ; } if ( pif -> pif_poll_cache && op == ALTDQ_REMOVE ) { best_cl = pif -> pif_poll_cache ; m = fairq_getq ( best_cl , cur_time ) ; pif -> pif_poll_cache = NULL ; if ( m ) { IFQ_DEC_LEN ( ifq ) ; PKTCNTR_ADD ( & best_cl -> cl_xmitcnt , m_pktlen ( m ) ) ; return ( m ) ; } } else { best_cl = NULL ; best_m = NULL ; for ( pri = pif -> pif_maxpri ; pri >= 0 ; pri -- ) { if ( ( cl = pif -> pif_classes [ pri ] ) == NULL ) continue ; if ( ( cl -> cl_flags & FARF_HAS_PACKETS ) == 0 ) continue ; m = fairq_pollq ( cl , cur_time , & hit_limit ) ; if ( m == NULL ) { cl -> cl_flags &= ~ FARF_HAS_PACKETS ; continue ; } if ( hit_limit == 0 || best_cl == NULL ) { best_cl = cl ; best_m = m ; } if ( hit_limit ) continue ; break ; } if ( op == ALTDQ_POLL ) { pif -> pif_poll_cache = best_cl ; m = best_m ; } else if ( best_cl ) { m = fairq_getq ( best_cl , cur_time ) ; if ( m != NULL ) { IFQ_DEC_LEN ( ifq ) ; PKTCNTR_ADD ( & best_cl -> cl_xmitcnt , m_pktlen ( m ) ) ; } } return ( m ) ; } return ( NULL ) ; }
fairq_dequeue is a dequeue function to be registered to
 (*altq_dequeue) in struct ifaltq.
fairq_dequeue is a dequeue function to be registered to ( * altq_dequeue ) in struct ifaltq .
/*
			 * Only override the best choice if we are under
			 * the BW limit.
			 */ /*
			 * Remember the highest priority mbuf in case we
			 * do not find any lower priority mbufs.
			 */