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