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