Home | History | Annotate | Line # | Download | only in altq
altq_wfq.c revision 1.13
      1  1.13        ad /*	$NetBSD: altq_wfq.c,v 1.13 2006/07/21 16:48:46 ad Exp $	*/
      2   1.1   thorpej /*	$KAME: altq_wfq.c,v 1.7 2000/12/14 08:12:46 thorpej Exp $	*/
      3   1.1   thorpej 
      4   1.1   thorpej /*
      5   1.1   thorpej  * Copyright (C) 1997-2000
      6   1.1   thorpej  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7   1.1   thorpej  *
      8   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      9   1.1   thorpej  * modification, are permitted provided that the following conditions
     10   1.1   thorpej  * are met:
     11   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     16   1.1   thorpej  *
     17   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27   1.1   thorpej  * SUCH DAMAGE.
     28   1.1   thorpej  */
     29   1.1   thorpej /*
     30   1.1   thorpej  *  March 27, 1997.  Written by Hiroshi Kyusojin of Keio University
     31   1.7     perry  *  (kyu (at) mt.cs.keio.ac.jp).
     32   1.1   thorpej  */
     33   1.4     lukem 
     34   1.4     lukem #include <sys/cdefs.h>
     35  1.13        ad __KERNEL_RCSID(0, "$NetBSD: altq_wfq.c,v 1.13 2006/07/21 16:48:46 ad Exp $");
     36   1.1   thorpej 
     37   1.1   thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
     38   1.1   thorpej #include "opt_altq.h"
     39   1.1   thorpej #if (__FreeBSD__ != 2)
     40   1.1   thorpej #include "opt_inet.h"
     41   1.1   thorpej #ifdef __FreeBSD__
     42   1.1   thorpej #include "opt_inet6.h"
     43   1.1   thorpej #endif
     44   1.1   thorpej #endif
     45   1.1   thorpej #endif /* __FreeBSD__ || __NetBSD__ */
     46   1.1   thorpej #ifdef ALTQ_WFQ
     47   1.1   thorpej 
     48   1.1   thorpej #include <sys/param.h>
     49   1.1   thorpej #include <sys/malloc.h>
     50   1.1   thorpej #include <sys/mbuf.h>
     51   1.1   thorpej #include <sys/uio.h>
     52   1.1   thorpej #include <sys/socket.h>
     53   1.1   thorpej #include <sys/systm.h>
     54   1.1   thorpej #include <sys/proc.h>
     55   1.1   thorpej #include <sys/errno.h>
     56   1.1   thorpej #include <sys/time.h>
     57   1.1   thorpej #include <sys/kernel.h>
     58  1.12  christos #include <sys/kauth.h>
     59   1.1   thorpej 
     60   1.1   thorpej #include <net/if.h>
     61   1.1   thorpej #include <net/if_types.h>
     62   1.1   thorpej #include <netinet/in.h>
     63   1.1   thorpej 
     64   1.1   thorpej #include <altq/altq.h>
     65   1.1   thorpej #include <altq/altq_conf.h>
     66   1.1   thorpej #include <altq/altq_wfq.h>
     67   1.1   thorpej 
     68   1.1   thorpej /*
     69   1.1   thorpej #define	WFQ_DEBUG
     70   1.1   thorpej */
     71   1.1   thorpej 
     72   1.1   thorpej static int		wfq_setenable(struct wfq_interface *, int);
     73   1.1   thorpej static int		wfq_ifattach(struct wfq_interface *);
     74   1.1   thorpej static int		wfq_ifdetach(struct wfq_interface *);
     75   1.1   thorpej static int		wfq_ifenqueue(struct ifaltq *, struct mbuf *,
     76   1.1   thorpej 				      struct altq_pktattr *);
     77   1.1   thorpej static u_long		wfq_hash(struct flowinfo *, int);
     78   1.9     perry static inline u_long	wfq_hashbydstaddr(struct flowinfo *, int);
     79   1.9     perry static inline u_long	wfq_hashbysrcport(struct flowinfo *, int);
     80   1.1   thorpej static wfq		*wfq_maxqueue(wfq_state_t *);
     81   1.1   thorpej static struct mbuf	*wfq_ifdequeue(struct ifaltq *, int);
     82   1.1   thorpej static int		wfq_getqid(struct wfq_getqid *);
     83   1.1   thorpej static int		wfq_setweight(struct wfq_setweight *);
     84   1.1   thorpej static int		wfq_getstats(struct wfq_getstats *);
     85   1.1   thorpej static int		wfq_config(struct wfq_conf *);
     86   1.1   thorpej static int		wfq_request __P((struct ifaltq *, int, void *));
     87   1.1   thorpej static int		wfq_flush(struct ifaltq *);
     88   1.1   thorpej static void		*wfq_classify(void *, struct mbuf *, int);
     89   1.1   thorpej 
     90   1.1   thorpej /* global value : pointer to wfq queue list */
     91   1.1   thorpej static wfq_state_t *wfq_list = NULL;
     92   1.1   thorpej 
     93   1.1   thorpej static int
     94   1.1   thorpej wfq_setenable(ifacep, flag)
     95   1.1   thorpej 	struct wfq_interface *ifacep;
     96   1.1   thorpej 	int flag;
     97   1.1   thorpej {
     98   1.1   thorpej 	wfq_state_t *wfqp;
     99   1.1   thorpej 	int error = 0;
    100   1.1   thorpej 
    101   1.1   thorpej 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
    102   1.1   thorpej 		return (EBADF);
    103   1.1   thorpej 
    104   1.1   thorpej 	switch(flag){
    105   1.1   thorpej 	case ENABLE:
    106   1.1   thorpej 		error = altq_enable(wfqp->ifq);
    107   1.1   thorpej 		break;
    108   1.1   thorpej 	case DISABLE:
    109   1.1   thorpej 		error = altq_disable(wfqp->ifq);
    110   1.1   thorpej 		break;
    111   1.1   thorpej 	}
    112   1.1   thorpej 	return error;
    113   1.1   thorpej }
    114   1.1   thorpej 
    115   1.1   thorpej 
    116   1.1   thorpej static int
    117   1.1   thorpej wfq_ifattach(ifacep)
    118   1.1   thorpej 	struct wfq_interface *ifacep;
    119   1.1   thorpej {
    120   1.1   thorpej 	int error = 0, i;
    121   1.1   thorpej 	struct ifnet *ifp;
    122   1.1   thorpej 	wfq_state_t *new_wfqp;
    123   1.1   thorpej 	wfq *queue;
    124   1.1   thorpej 
    125   1.1   thorpej 	if ((ifp = ifunit(ifacep->wfq_ifacename)) == NULL) {
    126   1.1   thorpej #ifdef WFQ_DEBUG
    127   1.1   thorpej 		printf("wfq_ifattach()...no ifp found\n");
    128   1.1   thorpej #endif
    129   1.1   thorpej 		return (ENXIO);
    130   1.1   thorpej 	}
    131   1.1   thorpej 
    132   1.1   thorpej 	if (!ALTQ_IS_READY(&ifp->if_snd)) {
    133   1.1   thorpej #ifdef WFQ_DEBUG
    134   1.1   thorpej 		printf("wfq_ifattach()...altq is not ready\n");
    135   1.1   thorpej #endif
    136   1.1   thorpej 		return (ENXIO);
    137   1.1   thorpej 	}
    138   1.1   thorpej 
    139   1.1   thorpej 	/* allocate and initialize wfq_state_t */
    140  1.10  christos 	new_wfqp = malloc(sizeof(wfq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    141   1.1   thorpej 	if (new_wfqp == NULL)
    142   1.1   thorpej 		return (ENOMEM);
    143  1.10  christos 	queue = malloc(sizeof(wfq) * DEFAULT_QSIZE, M_DEVBUF, M_WAITOK|M_ZERO);
    144   1.1   thorpej 	if (queue == NULL) {
    145  1.10  christos 		free(new_wfqp, M_DEVBUF);
    146   1.1   thorpej 		return (ENOMEM);
    147   1.1   thorpej 	}
    148   1.1   thorpej 
    149   1.1   thorpej 	/* keep the ifq */
    150   1.1   thorpej 	new_wfqp->ifq = &ifp->if_snd;
    151   1.1   thorpej 	new_wfqp->nums = DEFAULT_QSIZE;
    152   1.1   thorpej 	new_wfqp->hwm = HWM;
    153   1.1   thorpej 	new_wfqp->bytes = 0;
    154   1.1   thorpej 	new_wfqp->rrp = NULL;
    155   1.1   thorpej 	new_wfqp->queue = queue;
    156   1.1   thorpej 	new_wfqp->hash_func = wfq_hashbydstaddr;
    157   1.1   thorpej 	new_wfqp->fbmask = FIMB4_DADDR;
    158   1.1   thorpej 
    159   1.1   thorpej 	for (i = 0; i < new_wfqp->nums; i++, queue++) {
    160   1.1   thorpej 		queue->next = queue->prev = NULL;
    161   1.1   thorpej 		queue->head = queue->tail = NULL;
    162   1.1   thorpej 		queue->bytes = queue->quota = 0;
    163   1.1   thorpej 		queue->weight = 100;
    164   1.1   thorpej 	}
    165   1.1   thorpej 
    166   1.1   thorpej 	/*
    167   1.1   thorpej 	 * set WFQ to this ifnet structure.
    168   1.1   thorpej 	 */
    169   1.1   thorpej 	if ((error = altq_attach(&ifp->if_snd, ALTQT_WFQ, new_wfqp,
    170   1.1   thorpej 				 wfq_ifenqueue, wfq_ifdequeue, wfq_request,
    171   1.1   thorpej 				 new_wfqp, wfq_classify)) != 0) {
    172  1.10  christos 		free(queue, M_DEVBUF);
    173  1.10  christos 		free(new_wfqp, M_DEVBUF);
    174   1.1   thorpej 		return (error);
    175   1.1   thorpej 	}
    176   1.1   thorpej 
    177   1.1   thorpej 	new_wfqp->next = wfq_list;
    178   1.1   thorpej 	wfq_list = new_wfqp;
    179   1.1   thorpej 
    180   1.1   thorpej 	return (error);
    181   1.1   thorpej }
    182   1.1   thorpej 
    183   1.1   thorpej 
    184   1.1   thorpej static int
    185   1.1   thorpej wfq_ifdetach(ifacep)
    186   1.1   thorpej 	struct wfq_interface *ifacep;
    187   1.1   thorpej {
    188   1.1   thorpej 	int		error = 0;
    189   1.1   thorpej 	wfq_state_t	*wfqp;
    190   1.1   thorpej 
    191   1.1   thorpej 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
    192   1.1   thorpej 		return (EBADF);
    193   1.1   thorpej 
    194   1.1   thorpej 	/* free queued mbuf */
    195   1.1   thorpej 	wfq_flush(wfqp->ifq);
    196   1.1   thorpej 
    197   1.1   thorpej 	/* remove WFQ from the ifnet structure. */
    198   1.1   thorpej 	(void)altq_disable(wfqp->ifq);
    199   1.1   thorpej 	(void)altq_detach(wfqp->ifq);
    200   1.7     perry 
    201   1.1   thorpej 	/* remove from the wfqstate list */
    202   1.1   thorpej 	if (wfq_list == wfqp)
    203   1.1   thorpej 		wfq_list = wfqp->next;
    204   1.1   thorpej 	else {
    205   1.1   thorpej 		wfq_state_t *wp = wfq_list;
    206   1.1   thorpej 		do {
    207   1.1   thorpej 			if (wp->next == wfqp) {
    208   1.1   thorpej 				wp->next = wfqp->next;
    209   1.1   thorpej 				break;
    210   1.1   thorpej 			}
    211   1.1   thorpej 		} while ((wp = wp->next) != NULL);
    212   1.1   thorpej 	}
    213   1.7     perry 
    214   1.1   thorpej 	/* deallocate wfq_state_t */
    215  1.10  christos 	free(wfqp->queue, M_DEVBUF);
    216  1.10  christos 	free(wfqp, M_DEVBUF);
    217   1.1   thorpej 	return (error);
    218   1.1   thorpej }
    219   1.1   thorpej 
    220   1.1   thorpej static int
    221   1.1   thorpej wfq_request(ifq, req, arg)
    222   1.1   thorpej 	struct ifaltq *ifq;
    223   1.1   thorpej 	int req;
    224   1.1   thorpej 	void *arg;
    225   1.1   thorpej {
    226   1.1   thorpej 	wfq_state_t *wfqp = (wfq_state_t *)ifq->altq_disc;
    227   1.1   thorpej 
    228   1.1   thorpej 	switch (req) {
    229   1.1   thorpej 	case ALTRQ_PURGE:
    230   1.1   thorpej 		wfq_flush(wfqp->ifq);
    231   1.1   thorpej 		break;
    232   1.1   thorpej 	}
    233   1.1   thorpej 	return (0);
    234   1.1   thorpej }
    235   1.1   thorpej 
    236   1.1   thorpej 
    237   1.1   thorpej static int
    238   1.1   thorpej wfq_flush(ifq)
    239   1.1   thorpej 	struct ifaltq *ifq;
    240   1.1   thorpej {
    241   1.1   thorpej 	struct mbuf *mp;
    242   1.1   thorpej 
    243   1.1   thorpej 	while ((mp = wfq_ifdequeue(ifq, ALTDQ_REMOVE)) != NULL)
    244   1.1   thorpej 		m_freem(mp);
    245   1.1   thorpej 	if (ALTQ_IS_ENABLED(ifq))
    246   1.1   thorpej 		ifq->ifq_len = 0;
    247   1.1   thorpej 	return 0;
    248   1.1   thorpej }
    249   1.1   thorpej 
    250   1.1   thorpej static void *
    251   1.1   thorpej wfq_classify(clfier, m, af)
    252   1.1   thorpej 	void *clfier;
    253   1.1   thorpej 	struct mbuf *m;
    254   1.1   thorpej 	int af;
    255   1.1   thorpej {
    256   1.1   thorpej 	wfq_state_t *wfqp = (wfq_state_t *)clfier;
    257   1.1   thorpej 	struct flowinfo flow;
    258   1.1   thorpej 
    259   1.1   thorpej 	altq_extractflow(m, af, &flow, wfqp->fbmask);
    260   1.1   thorpej 	return (&wfqp->queue[(*wfqp->hash_func)(&flow, wfqp->nums)]);
    261   1.1   thorpej }
    262   1.1   thorpej 
    263   1.1   thorpej static int
    264   1.1   thorpej wfq_ifenqueue(ifq, mp, pktattr)
    265   1.1   thorpej 	struct ifaltq *ifq;
    266   1.1   thorpej 	struct mbuf *mp;
    267   1.1   thorpej 	struct altq_pktattr *pktattr;
    268   1.1   thorpej {
    269   1.1   thorpej 	wfq_state_t *wfqp;
    270   1.1   thorpej 	wfq *queue;
    271   1.1   thorpej 	int byte, error = 0;
    272   1.1   thorpej 
    273   1.1   thorpej 	wfqp = (wfq_state_t *)ifq->altq_disc;
    274   1.1   thorpej 	mp->m_nextpkt = NULL;
    275   1.1   thorpej 
    276   1.1   thorpej 	/* grab a queue selected by classifier */
    277   1.1   thorpej 	if (pktattr == NULL || (queue = pktattr->pattr_class) == NULL)
    278   1.1   thorpej 		queue = &wfqp->queue[0];
    279   1.1   thorpej 
    280   1.1   thorpej 	if (queue->tail == NULL)
    281   1.1   thorpej 		queue->head = mp;
    282   1.1   thorpej 	else
    283   1.1   thorpej 		queue->tail->m_nextpkt = mp;
    284   1.1   thorpej 	queue->tail = mp;
    285   1.1   thorpej 	byte = mp->m_pkthdr.len;
    286   1.1   thorpej 	queue->bytes += byte;
    287   1.1   thorpej 	wfqp->bytes += byte;
    288   1.1   thorpej 	ifq->ifq_len++;
    289   1.1   thorpej 
    290   1.1   thorpej 	if (queue->next == NULL) {
    291   1.1   thorpej 		/* this queue gets active. add the queue to the active list */
    292   1.1   thorpej 		if (wfqp->rrp == NULL){
    293   1.1   thorpej 			/* no queue in the active list */
    294   1.1   thorpej 			queue->next = queue->prev = queue;
    295   1.1   thorpej 			wfqp->rrp = queue;
    296   1.1   thorpej 			WFQ_ADDQUOTA(queue);
    297   1.1   thorpej 		} else {
    298   1.1   thorpej 			/* insert the queue at the tail of the active list */
    299   1.1   thorpej 			queue->prev = wfqp->rrp->prev;
    300   1.1   thorpej 			wfqp->rrp->prev->next = queue;
    301   1.1   thorpej 			wfqp->rrp->prev = queue;
    302   1.1   thorpej 			queue->next = wfqp->rrp;
    303   1.1   thorpej 			queue->quota = 0;
    304   1.1   thorpej 		}
    305   1.1   thorpej 	}
    306   1.1   thorpej 
    307   1.1   thorpej 	/* check overflow. if the total size exceeds the high water mark,
    308   1.1   thorpej 	   drop packets from the longest queue. */
    309   1.1   thorpej 	while (wfqp->bytes > wfqp->hwm) {
    310   1.1   thorpej 		wfq *drop_queue = wfq_maxqueue(wfqp);
    311   1.1   thorpej 
    312   1.1   thorpej 		/* drop the packet at the head. */
    313   1.1   thorpej 		mp = drop_queue->head;
    314   1.1   thorpej 		if ((drop_queue->head = mp->m_nextpkt) == NULL)
    315   1.1   thorpej 			drop_queue->tail = NULL;
    316   1.1   thorpej 		mp->m_nextpkt = NULL;
    317   1.1   thorpej 		byte = mp->m_pkthdr.len;
    318   1.1   thorpej 		drop_queue->bytes -= byte;
    319   1.1   thorpej 		PKTCNTR_ADD(&drop_queue->drop_cnt, byte);
    320   1.1   thorpej 		wfqp->bytes -= byte;
    321   1.1   thorpej 		m_freem(mp);
    322   1.1   thorpej 		ifq->ifq_len--;
    323   1.1   thorpej 		if(drop_queue == queue)
    324   1.1   thorpej 			/* the queue for this flow is selected to drop */
    325   1.1   thorpej 			error = ENOBUFS;
    326   1.1   thorpej 	}
    327   1.1   thorpej 	return error;
    328   1.1   thorpej }
    329   1.7     perry 
    330   1.1   thorpej 
    331   1.1   thorpej static u_long wfq_hash(flow, n)
    332   1.1   thorpej 	struct flowinfo *flow;
    333   1.1   thorpej 	int n;
    334   1.1   thorpej {
    335   1.1   thorpej 	u_long val = 0;
    336   1.1   thorpej 
    337   1.1   thorpej 	if (flow != NULL) {
    338   1.1   thorpej 		if (flow->fi_family == AF_INET) {
    339   1.1   thorpej 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    340   1.1   thorpej 			u_long val2;
    341   1.1   thorpej 
    342   1.1   thorpej 			val = fp->fi_dst.s_addr ^ fp->fi_src.s_addr;
    343   1.1   thorpej 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
    344   1.1   thorpej 			val2 = fp->fi_dport ^ fp->fi_sport ^ fp->fi_proto;
    345   1.1   thorpej 			val2 = val2 ^ (val2 >> 8);
    346   1.1   thorpej 			val = val ^ val2;
    347   1.1   thorpej 		}
    348   1.1   thorpej #ifdef INET6
    349   1.1   thorpej 		else if (flow->fi_family == AF_INET6) {
    350   1.1   thorpej 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    351   1.1   thorpej 
    352   1.1   thorpej 			val = ntohl(fp6->fi6_flowlabel);
    353   1.1   thorpej 		}
    354   1.1   thorpej #endif
    355   1.1   thorpej 	}
    356   1.1   thorpej 
    357   1.1   thorpej 	return (val % n);
    358   1.1   thorpej }
    359   1.1   thorpej 
    360   1.1   thorpej 
    361   1.9     perry static inline u_long wfq_hashbydstaddr(flow, n)
    362   1.1   thorpej 	struct flowinfo *flow;
    363   1.1   thorpej 	int n;
    364   1.1   thorpej {
    365   1.1   thorpej 	u_long val = 0;
    366   1.1   thorpej 
    367   1.1   thorpej 	if (flow != NULL) {
    368   1.1   thorpej 		if (flow->fi_family == AF_INET) {
    369   1.1   thorpej 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    370   1.1   thorpej 
    371   1.1   thorpej 			val = fp->fi_dst.s_addr;
    372   1.1   thorpej 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
    373   1.1   thorpej 		}
    374   1.1   thorpej #ifdef INET6
    375   1.1   thorpej 		else if (flow->fi_family == AF_INET6) {
    376   1.1   thorpej 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    377   1.1   thorpej 
    378   1.1   thorpej 			val = ntohl(fp6->fi6_flowlabel);
    379   1.1   thorpej 		}
    380   1.1   thorpej #endif
    381   1.1   thorpej 	}
    382   1.1   thorpej 
    383   1.1   thorpej 	return (val % n);
    384   1.1   thorpej }
    385   1.1   thorpej 
    386   1.9     perry static inline u_long wfq_hashbysrcport(flow, n)
    387   1.1   thorpej 	struct flowinfo *flow;
    388   1.1   thorpej 	int n;
    389   1.1   thorpej {
    390   1.1   thorpej 	u_long val = 0;
    391   1.1   thorpej 
    392   1.1   thorpej 	if (flow != NULL) {
    393   1.1   thorpej 		if (flow->fi_family == AF_INET) {
    394   1.1   thorpej 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    395   1.1   thorpej 
    396   1.1   thorpej 			val = fp->fi_sport;
    397   1.1   thorpej 		}
    398   1.1   thorpej #ifdef INET6
    399   1.1   thorpej 		else if (flow->fi_family == AF_INET6) {
    400   1.1   thorpej 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    401   1.1   thorpej 
    402   1.1   thorpej 			val = fp6->fi6_sport;
    403   1.1   thorpej 		}
    404   1.1   thorpej #endif
    405   1.1   thorpej 	}
    406   1.1   thorpej 	val = val ^ (val >> 8);
    407   1.1   thorpej 
    408   1.1   thorpej 	return (val % n);
    409   1.1   thorpej }
    410   1.1   thorpej 
    411   1.1   thorpej static wfq *wfq_maxqueue(wfqp)
    412   1.1   thorpej 	wfq_state_t *wfqp;
    413   1.1   thorpej {
    414   1.1   thorpej 	int byte, max_byte = 0;
    415   1.1   thorpej 	wfq *queue, *max_queue = NULL;
    416   1.1   thorpej 
    417   1.1   thorpej 	if((queue = wfqp->rrp) == NULL)
    418   1.1   thorpej 		/* never happens */
    419   1.1   thorpej 		return NULL;
    420   1.1   thorpej 	do{
    421   1.1   thorpej 		if ((byte = queue->bytes * 100 / queue->weight) > max_byte) {
    422   1.1   thorpej 			max_queue = queue;
    423   1.1   thorpej 			max_byte = byte;
    424   1.1   thorpej 		}
    425   1.1   thorpej 	} while ((queue = queue->next) != wfqp->rrp);
    426   1.1   thorpej 
    427   1.1   thorpej 	return max_queue;
    428   1.1   thorpej }
    429   1.1   thorpej 
    430   1.1   thorpej 
    431   1.1   thorpej static struct mbuf *
    432   1.1   thorpej wfq_ifdequeue(ifq, op)
    433   1.1   thorpej 	struct ifaltq *ifq;
    434   1.1   thorpej 	int op;
    435   1.1   thorpej {
    436   1.1   thorpej 	wfq_state_t *wfqp;
    437   1.1   thorpej 	wfq *queue;
    438   1.1   thorpej 	struct mbuf *mp;
    439   1.1   thorpej 	int byte;
    440   1.1   thorpej 
    441   1.1   thorpej 	wfqp = (wfq_state_t *)ifq->altq_disc;
    442   1.1   thorpej 
    443   1.1   thorpej 	if ((wfqp->bytes == 0) || ((queue = wfqp->rrp) == NULL))
    444   1.1   thorpej 		/* no packet in the queues */
    445   1.1   thorpej 		return NULL;
    446   1.7     perry 
    447   1.1   thorpej 	while (1) {
    448   1.1   thorpej 		if (queue->quota > 0) {
    449   1.1   thorpej 			if (queue->bytes <= 0) {
    450   1.1   thorpej 				/* this queue no longer has packet.
    451   1.1   thorpej 				   remove the queue from the active list. */
    452   1.1   thorpej 				if (queue->next == queue){
    453   1.7     perry 					/* no other active queue
    454   1.1   thorpej 					   -- this case never happens in
    455   1.1   thorpej 					   this algorithm. */
    456   1.1   thorpej 					queue->next = queue->prev = NULL;
    457   1.1   thorpej 					wfqp->rrp = NULL;
    458   1.1   thorpej 					return NULL;
    459   1.1   thorpej 				} else {
    460   1.1   thorpej 					queue->prev->next = queue->next;
    461   1.1   thorpej 					queue->next->prev = queue->prev;
    462   1.1   thorpej 					/* the round-robin pointer points
    463   1.1   thorpej 					   to this queue, advance the rrp */
    464   1.1   thorpej 					wfqp->rrp = queue->next;
    465   1.1   thorpej 					queue->next = queue->prev = NULL;
    466   1.1   thorpej 					queue = wfqp->rrp;
    467   1.1   thorpej 					WFQ_ADDQUOTA(queue);
    468   1.1   thorpej 					continue;
    469   1.1   thorpej 				}
    470   1.1   thorpej 			}
    471   1.1   thorpej 
    472   1.1   thorpej 			/* dequeue a packet from this queue */
    473   1.1   thorpej 			mp = queue->head;
    474   1.1   thorpej 			if (op == ALTDQ_REMOVE) {
    475   1.1   thorpej 				if((queue->head = mp->m_nextpkt) == NULL)
    476   1.1   thorpej 					queue->tail = NULL;
    477   1.1   thorpej 				byte = mp->m_pkthdr.len;
    478   1.1   thorpej 				mp->m_nextpkt = NULL;
    479   1.1   thorpej 				queue->quota -= byte;
    480   1.1   thorpej 				queue->bytes -= byte;
    481   1.1   thorpej 				PKTCNTR_ADD(&queue->xmit_cnt, byte);
    482   1.1   thorpej 				wfqp->bytes -= byte;
    483   1.1   thorpej 				if (ALTQ_IS_ENABLED(ifq))
    484   1.1   thorpej 					ifq->ifq_len--;
    485   1.1   thorpej 			}
    486   1.1   thorpej 			return mp;
    487   1.1   thorpej 
    488   1.1   thorpej 			/* if the queue gets empty by this dequeueing,
    489   1.1   thorpej 			   the queue will be removed from the active list
    490   1.1   thorpej 			   at the next round */
    491   1.1   thorpej 		}
    492   1.7     perry 
    493   1.1   thorpej 		/* advance the round-robin pointer */
    494   1.1   thorpej 		queue = wfqp->rrp = queue->next;
    495   1.1   thorpej 		WFQ_ADDQUOTA(queue);
    496   1.1   thorpej 	}
    497   1.1   thorpej }
    498   1.1   thorpej 
    499   1.1   thorpej static int
    500   1.1   thorpej wfq_getqid(gqidp)
    501   1.1   thorpej 	struct wfq_getqid *gqidp;
    502   1.1   thorpej {
    503   1.1   thorpej 	wfq_state_t *wfqp;
    504   1.1   thorpej 
    505   1.1   thorpej 	if ((wfqp = altq_lookup(gqidp->iface.wfq_ifacename, ALTQT_WFQ))
    506   1.1   thorpej 	    == NULL)
    507   1.1   thorpej 		return (EBADF);
    508   1.7     perry 
    509   1.1   thorpej 	gqidp->qid = (*wfqp->hash_func)(&gqidp->flow, wfqp->nums);
    510   1.1   thorpej 	return 0;
    511   1.1   thorpej }
    512   1.1   thorpej 
    513   1.1   thorpej static int
    514   1.1   thorpej wfq_setweight(swp)
    515   1.1   thorpej 	struct wfq_setweight *swp;
    516   1.1   thorpej {
    517   1.1   thorpej 	wfq_state_t	*wfqp;
    518   1.1   thorpej 	wfq *queue;
    519   1.1   thorpej 	int old;
    520   1.1   thorpej 
    521   1.1   thorpej 	if (swp->weight < 0) {
    522   1.1   thorpej 		printf("set weight in natural number\n");
    523   1.1   thorpej 		return (EINVAL);
    524   1.1   thorpej 	}
    525   1.1   thorpej 
    526   1.1   thorpej 	if ((wfqp = altq_lookup(swp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    527   1.1   thorpej 		return (EBADF);
    528   1.1   thorpej 
    529   1.1   thorpej 	queue = &wfqp->queue[swp->qid];
    530   1.1   thorpej 	old = queue->weight;
    531   1.1   thorpej 	queue->weight = swp->weight;
    532   1.1   thorpej 	swp->weight = old;
    533   1.1   thorpej 	return 0;
    534   1.1   thorpej }
    535   1.1   thorpej 
    536   1.1   thorpej 
    537   1.1   thorpej static int
    538   1.1   thorpej wfq_getstats(gsp)
    539   1.1   thorpej 	struct wfq_getstats *gsp;
    540   1.1   thorpej {
    541   1.1   thorpej 	wfq_state_t	*wfqp;
    542   1.1   thorpej 	wfq *queue;
    543   1.1   thorpej 	queue_stats *stats;
    544   1.1   thorpej 
    545   1.1   thorpej 	if ((wfqp = altq_lookup(gsp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    546   1.1   thorpej 		return (EBADF);
    547   1.1   thorpej 
    548   1.1   thorpej 	if (gsp->qid >= wfqp->nums)
    549   1.1   thorpej 		return (EINVAL);
    550   1.1   thorpej 
    551   1.1   thorpej 	queue = &wfqp->queue[gsp->qid];
    552   1.1   thorpej 	stats = &gsp->stats;
    553   1.1   thorpej 
    554   1.1   thorpej 	stats->bytes		= queue->bytes;
    555   1.1   thorpej 	stats->weight		= queue->weight;
    556   1.1   thorpej 	stats->xmit_cnt		= queue->xmit_cnt;
    557   1.1   thorpej 	stats->drop_cnt		= queue->drop_cnt;
    558   1.1   thorpej 
    559   1.1   thorpej 	return 0;
    560   1.1   thorpej }
    561   1.1   thorpej 
    562   1.1   thorpej 
    563   1.1   thorpej static int
    564   1.1   thorpej wfq_config(cf)
    565   1.1   thorpej 	struct wfq_conf *cf;
    566   1.1   thorpej {
    567   1.1   thorpej 	wfq_state_t	*wfqp;
    568   1.1   thorpej 	wfq		*queue;
    569   1.1   thorpej 	int		i, error = 0;
    570   1.1   thorpej 
    571   1.1   thorpej 	if ((wfqp = altq_lookup(cf->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    572   1.1   thorpej 		return (EBADF);
    573   1.1   thorpej 
    574   1.1   thorpej 	if(cf->nqueues <= 0 ||  MAX_QSIZE < cf->nqueues)
    575   1.1   thorpej 		cf->nqueues = DEFAULT_QSIZE;
    576   1.1   thorpej 
    577   1.1   thorpej 	if (cf->nqueues != wfqp->nums) {
    578   1.1   thorpej 		/* free queued mbuf */
    579   1.1   thorpej 		wfq_flush(wfqp->ifq);
    580  1.10  christos 		free(wfqp->queue, M_DEVBUF);
    581   1.1   thorpej 
    582  1.10  christos 		queue = malloc(sizeof(wfq) * cf->nqueues, M_DEVBUF,
    583  1.10  christos 		    M_WAITOK|M_ZERO);
    584   1.1   thorpej 		if (queue == NULL)
    585   1.1   thorpej 			return (ENOMEM);
    586   1.1   thorpej 
    587   1.1   thorpej 		wfqp->nums = cf->nqueues;
    588   1.1   thorpej 		wfqp->bytes = 0;
    589   1.1   thorpej 		wfqp->rrp = NULL;
    590   1.1   thorpej 		wfqp->queue = queue;
    591   1.1   thorpej 		for (i = 0; i < wfqp->nums; i++, queue++) {
    592   1.1   thorpej 			queue->next = queue->prev = NULL;
    593   1.1   thorpej 			queue->head = queue->tail = NULL;
    594   1.1   thorpej 			queue->bytes = queue->quota = 0;
    595   1.1   thorpej 			queue->weight = 100;
    596   1.1   thorpej 		}
    597   1.1   thorpej 	}
    598   1.1   thorpej 
    599   1.1   thorpej 	if (cf->qlimit != 0)
    600   1.1   thorpej 		wfqp->hwm = cf->qlimit;
    601   1.1   thorpej 
    602   1.1   thorpej 	switch (cf->hash_policy) {
    603   1.1   thorpej 	case WFQ_HASH_DSTADDR:
    604   1.1   thorpej 		wfqp->hash_func = wfq_hashbydstaddr;
    605   1.1   thorpej 		wfqp->fbmask = FIMB4_DADDR;
    606   1.1   thorpej #ifdef INET6
    607   1.1   thorpej 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    608   1.1   thorpej #endif
    609   1.1   thorpej 		break;
    610   1.1   thorpej 	case WFQ_HASH_SRCPORT:
    611   1.1   thorpej 		wfqp->hash_func = wfq_hashbysrcport;
    612   1.1   thorpej 		wfqp->fbmask = FIMB4_SPORT;
    613   1.1   thorpej #ifdef INET6
    614   1.1   thorpej 		wfqp->fbmask |= FIMB6_SPORT;
    615   1.1   thorpej #endif
    616   1.1   thorpej 		break;
    617   1.1   thorpej 	case WFQ_HASH_FULL:
    618   1.1   thorpej 		wfqp->hash_func = wfq_hash;
    619   1.1   thorpej 		wfqp->fbmask = FIMB4_ALL;
    620   1.1   thorpej #ifdef INET6
    621   1.1   thorpej 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    622   1.1   thorpej #endif
    623   1.1   thorpej 		break;
    624   1.1   thorpej 	default:
    625   1.1   thorpej 		error = EINVAL;
    626   1.1   thorpej 		break;
    627   1.1   thorpej 	}
    628   1.1   thorpej 	return error;
    629   1.1   thorpej }
    630   1.1   thorpej 
    631   1.1   thorpej /*
    632   1.1   thorpej  * wfq device interface
    633   1.1   thorpej  */
    634   1.1   thorpej 
    635   1.1   thorpej altqdev_decl(wfq);
    636   1.1   thorpej 
    637   1.1   thorpej int
    638   1.8  christos wfqopen(dev, flag, fmt, l)
    639   1.1   thorpej 	dev_t dev;
    640   1.1   thorpej 	int flag, fmt;
    641   1.8  christos 	struct lwp *l;
    642   1.1   thorpej {
    643   1.1   thorpej 	return 0;
    644   1.1   thorpej }
    645   1.1   thorpej 
    646   1.1   thorpej int
    647   1.8  christos wfqclose(dev, flag, fmt, l)
    648   1.1   thorpej 	dev_t dev;
    649   1.1   thorpej 	int flag, fmt;
    650   1.8  christos 	struct lwp *l;
    651   1.1   thorpej {
    652   1.1   thorpej 	struct ifnet *ifp;
    653   1.1   thorpej 	struct wfq_interface iface;
    654   1.1   thorpej 	wfq_state_t *wfqp;
    655   1.1   thorpej 	int s;
    656   1.7     perry 
    657   1.3   thorpej 	s = splnet();
    658   1.1   thorpej 	while ((wfqp = wfq_list) != NULL) {
    659   1.1   thorpej 		ifp = wfqp->ifq->altq_ifp;
    660   1.1   thorpej #if defined(__NetBSD__) || defined(__OpenBSD__)
    661   1.1   thorpej 		sprintf(iface.wfq_ifacename, "%s", ifp->if_xname);
    662   1.1   thorpej #else
    663   1.1   thorpej 		sprintf(iface.wfq_ifacename, "%s%d",
    664   1.1   thorpej 			ifp->if_name, ifp->if_unit);
    665   1.1   thorpej #endif
    666   1.1   thorpej 		wfq_ifdetach(&iface);
    667   1.1   thorpej 	}
    668   1.1   thorpej 	splx(s);
    669   1.1   thorpej 	return 0;
    670   1.1   thorpej }
    671   1.1   thorpej 
    672   1.1   thorpej int
    673   1.8  christos wfqioctl(dev, cmd, addr, flag, l)
    674   1.1   thorpej 	dev_t dev;
    675   1.1   thorpej 	ioctlcmd_t cmd;
    676   1.1   thorpej 	caddr_t addr;
    677   1.1   thorpej 	int flag;
    678   1.8  christos 	struct lwp *l;
    679   1.1   thorpej {
    680   1.1   thorpej 	int	error = 0;
    681   1.1   thorpej 	int 	s;
    682   1.1   thorpej 
    683   1.1   thorpej 	/* check cmd for superuser only */
    684   1.1   thorpej 	switch (cmd) {
    685   1.1   thorpej 	case WFQ_GET_QID:
    686   1.1   thorpej 	case WFQ_GET_STATS:
    687   1.1   thorpej 		break;
    688   1.1   thorpej 	default:
    689   1.1   thorpej #if (__FreeBSD_version > 400000)
    690   1.1   thorpej 		if ((error = suser(p)) != 0)
    691   1.1   thorpej #else
    692  1.13        ad 		if ((error = kauth_authorize_generic(l->l_cred,
    693  1.13        ad 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
    694   1.1   thorpej #endif
    695   1.1   thorpej 			return (error);
    696   1.1   thorpej 		break;
    697   1.1   thorpej 	}
    698   1.1   thorpej 
    699   1.3   thorpej 	s = splnet();
    700   1.1   thorpej 	switch (cmd) {
    701   1.7     perry 
    702   1.1   thorpej 	case WFQ_ENABLE:
    703   1.1   thorpej 		error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
    704   1.1   thorpej 		break;
    705   1.1   thorpej 
    706   1.1   thorpej 	case WFQ_DISABLE:
    707   1.1   thorpej 		error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
    708   1.1   thorpej 		break;
    709   1.1   thorpej 
    710   1.1   thorpej 	case WFQ_IF_ATTACH:
    711   1.1   thorpej 		error = wfq_ifattach((struct wfq_interface *)addr);
    712   1.1   thorpej 		break;
    713   1.1   thorpej 
    714   1.1   thorpej 	case WFQ_IF_DETACH:
    715   1.1   thorpej 		error = wfq_ifdetach((struct wfq_interface *)addr);
    716   1.1   thorpej 		break;
    717   1.1   thorpej 
    718   1.1   thorpej 	case WFQ_GET_QID:
    719   1.1   thorpej 		error = wfq_getqid((struct wfq_getqid *)addr);
    720   1.1   thorpej 		break;
    721   1.1   thorpej 
    722   1.1   thorpej 	case WFQ_SET_WEIGHT:
    723   1.1   thorpej 		error = wfq_setweight((struct wfq_setweight *)addr);
    724   1.1   thorpej 		break;
    725   1.1   thorpej 
    726   1.1   thorpej 	case WFQ_GET_STATS:
    727   1.1   thorpej 		error = wfq_getstats((struct wfq_getstats *)addr);
    728   1.1   thorpej 		break;
    729   1.1   thorpej 
    730   1.1   thorpej 	case WFQ_CONFIG:
    731   1.1   thorpej 		error = wfq_config((struct wfq_conf *)addr);
    732   1.1   thorpej 		break;
    733   1.1   thorpej 
    734   1.1   thorpej 	default:
    735   1.1   thorpej 		error = EINVAL;
    736   1.1   thorpej 		break;
    737   1.1   thorpej 	}
    738   1.1   thorpej 	splx(s);
    739   1.1   thorpej 	return error;
    740   1.1   thorpej }
    741   1.1   thorpej 
    742   1.1   thorpej #ifdef KLD_MODULE
    743   1.1   thorpej 
    744   1.1   thorpej static struct altqsw wfq_sw =
    745   1.1   thorpej 	{"wfq", wfqopen, wfqclose, wfqioctl};
    746   1.1   thorpej 
    747   1.1   thorpej ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
    748   1.1   thorpej 
    749   1.1   thorpej #endif /* KLD_MODULE */
    750   1.1   thorpej 
    751   1.1   thorpej #endif /* ALTQ_WFQ */
    752