Home | History | Annotate | Line # | Download | only in altq
altq_wfq.c revision 1.11
      1 /*	$NetBSD: altq_wfq.c,v 1.11 2006/05/14 21:24:49 elad 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.11 2006/05/14 21:24:49 elad 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 	new_wfqp = malloc(sizeof(wfq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    140 	if (new_wfqp == NULL)
    141 		return (ENOMEM);
    142 	queue = malloc(sizeof(wfq) * DEFAULT_QSIZE, M_DEVBUF, M_WAITOK|M_ZERO);
    143 	if (queue == NULL) {
    144 		free(new_wfqp, M_DEVBUF);
    145 		return (ENOMEM);
    146 	}
    147 
    148 	/* keep the ifq */
    149 	new_wfqp->ifq = &ifp->if_snd;
    150 	new_wfqp->nums = DEFAULT_QSIZE;
    151 	new_wfqp->hwm = HWM;
    152 	new_wfqp->bytes = 0;
    153 	new_wfqp->rrp = NULL;
    154 	new_wfqp->queue = queue;
    155 	new_wfqp->hash_func = wfq_hashbydstaddr;
    156 	new_wfqp->fbmask = FIMB4_DADDR;
    157 
    158 	for (i = 0; i < new_wfqp->nums; i++, queue++) {
    159 		queue->next = queue->prev = NULL;
    160 		queue->head = queue->tail = NULL;
    161 		queue->bytes = queue->quota = 0;
    162 		queue->weight = 100;
    163 	}
    164 
    165 	/*
    166 	 * set WFQ to this ifnet structure.
    167 	 */
    168 	if ((error = altq_attach(&ifp->if_snd, ALTQT_WFQ, new_wfqp,
    169 				 wfq_ifenqueue, wfq_ifdequeue, wfq_request,
    170 				 new_wfqp, wfq_classify)) != 0) {
    171 		free(queue, M_DEVBUF);
    172 		free(new_wfqp, M_DEVBUF);
    173 		return (error);
    174 	}
    175 
    176 	new_wfqp->next = wfq_list;
    177 	wfq_list = new_wfqp;
    178 
    179 	return (error);
    180 }
    181 
    182 
    183 static int
    184 wfq_ifdetach(ifacep)
    185 	struct wfq_interface *ifacep;
    186 {
    187 	int		error = 0;
    188 	wfq_state_t	*wfqp;
    189 
    190 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
    191 		return (EBADF);
    192 
    193 	/* free queued mbuf */
    194 	wfq_flush(wfqp->ifq);
    195 
    196 	/* remove WFQ from the ifnet structure. */
    197 	(void)altq_disable(wfqp->ifq);
    198 	(void)altq_detach(wfqp->ifq);
    199 
    200 	/* remove from the wfqstate list */
    201 	if (wfq_list == wfqp)
    202 		wfq_list = wfqp->next;
    203 	else {
    204 		wfq_state_t *wp = wfq_list;
    205 		do {
    206 			if (wp->next == wfqp) {
    207 				wp->next = wfqp->next;
    208 				break;
    209 			}
    210 		} while ((wp = wp->next) != NULL);
    211 	}
    212 
    213 	/* deallocate wfq_state_t */
    214 	free(wfqp->queue, M_DEVBUF);
    215 	free(wfqp, M_DEVBUF);
    216 	return (error);
    217 }
    218 
    219 static int
    220 wfq_request(ifq, req, arg)
    221 	struct ifaltq *ifq;
    222 	int req;
    223 	void *arg;
    224 {
    225 	wfq_state_t *wfqp = (wfq_state_t *)ifq->altq_disc;
    226 
    227 	switch (req) {
    228 	case ALTRQ_PURGE:
    229 		wfq_flush(wfqp->ifq);
    230 		break;
    231 	}
    232 	return (0);
    233 }
    234 
    235 
    236 static int
    237 wfq_flush(ifq)
    238 	struct ifaltq *ifq;
    239 {
    240 	struct mbuf *mp;
    241 
    242 	while ((mp = wfq_ifdequeue(ifq, ALTDQ_REMOVE)) != NULL)
    243 		m_freem(mp);
    244 	if (ALTQ_IS_ENABLED(ifq))
    245 		ifq->ifq_len = 0;
    246 	return 0;
    247 }
    248 
    249 static void *
    250 wfq_classify(clfier, m, af)
    251 	void *clfier;
    252 	struct mbuf *m;
    253 	int af;
    254 {
    255 	wfq_state_t *wfqp = (wfq_state_t *)clfier;
    256 	struct flowinfo flow;
    257 
    258 	altq_extractflow(m, af, &flow, wfqp->fbmask);
    259 	return (&wfqp->queue[(*wfqp->hash_func)(&flow, wfqp->nums)]);
    260 }
    261 
    262 static int
    263 wfq_ifenqueue(ifq, mp, pktattr)
    264 	struct ifaltq *ifq;
    265 	struct mbuf *mp;
    266 	struct altq_pktattr *pktattr;
    267 {
    268 	wfq_state_t *wfqp;
    269 	wfq *queue;
    270 	int byte, error = 0;
    271 
    272 	wfqp = (wfq_state_t *)ifq->altq_disc;
    273 	mp->m_nextpkt = NULL;
    274 
    275 	/* grab a queue selected by classifier */
    276 	if (pktattr == NULL || (queue = pktattr->pattr_class) == NULL)
    277 		queue = &wfqp->queue[0];
    278 
    279 	if (queue->tail == NULL)
    280 		queue->head = mp;
    281 	else
    282 		queue->tail->m_nextpkt = mp;
    283 	queue->tail = mp;
    284 	byte = mp->m_pkthdr.len;
    285 	queue->bytes += byte;
    286 	wfqp->bytes += byte;
    287 	ifq->ifq_len++;
    288 
    289 	if (queue->next == NULL) {
    290 		/* this queue gets active. add the queue to the active list */
    291 		if (wfqp->rrp == NULL){
    292 			/* no queue in the active list */
    293 			queue->next = queue->prev = queue;
    294 			wfqp->rrp = queue;
    295 			WFQ_ADDQUOTA(queue);
    296 		} else {
    297 			/* insert the queue at the tail of the active list */
    298 			queue->prev = wfqp->rrp->prev;
    299 			wfqp->rrp->prev->next = queue;
    300 			wfqp->rrp->prev = queue;
    301 			queue->next = wfqp->rrp;
    302 			queue->quota = 0;
    303 		}
    304 	}
    305 
    306 	/* check overflow. if the total size exceeds the high water mark,
    307 	   drop packets from the longest queue. */
    308 	while (wfqp->bytes > wfqp->hwm) {
    309 		wfq *drop_queue = wfq_maxqueue(wfqp);
    310 
    311 		/* drop the packet at the head. */
    312 		mp = drop_queue->head;
    313 		if ((drop_queue->head = mp->m_nextpkt) == NULL)
    314 			drop_queue->tail = NULL;
    315 		mp->m_nextpkt = NULL;
    316 		byte = mp->m_pkthdr.len;
    317 		drop_queue->bytes -= byte;
    318 		PKTCNTR_ADD(&drop_queue->drop_cnt, byte);
    319 		wfqp->bytes -= byte;
    320 		m_freem(mp);
    321 		ifq->ifq_len--;
    322 		if(drop_queue == queue)
    323 			/* the queue for this flow is selected to drop */
    324 			error = ENOBUFS;
    325 	}
    326 	return error;
    327 }
    328 
    329 
    330 static u_long wfq_hash(flow, n)
    331 	struct flowinfo *flow;
    332 	int n;
    333 {
    334 	u_long val = 0;
    335 
    336 	if (flow != NULL) {
    337 		if (flow->fi_family == AF_INET) {
    338 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    339 			u_long val2;
    340 
    341 			val = fp->fi_dst.s_addr ^ fp->fi_src.s_addr;
    342 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
    343 			val2 = fp->fi_dport ^ fp->fi_sport ^ fp->fi_proto;
    344 			val2 = val2 ^ (val2 >> 8);
    345 			val = val ^ val2;
    346 		}
    347 #ifdef INET6
    348 		else if (flow->fi_family == AF_INET6) {
    349 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    350 
    351 			val = ntohl(fp6->fi6_flowlabel);
    352 		}
    353 #endif
    354 	}
    355 
    356 	return (val % n);
    357 }
    358 
    359 
    360 static inline u_long wfq_hashbydstaddr(flow, n)
    361 	struct flowinfo *flow;
    362 	int n;
    363 {
    364 	u_long val = 0;
    365 
    366 	if (flow != NULL) {
    367 		if (flow->fi_family == AF_INET) {
    368 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    369 
    370 			val = fp->fi_dst.s_addr;
    371 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
    372 		}
    373 #ifdef INET6
    374 		else if (flow->fi_family == AF_INET6) {
    375 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    376 
    377 			val = ntohl(fp6->fi6_flowlabel);
    378 		}
    379 #endif
    380 	}
    381 
    382 	return (val % n);
    383 }
    384 
    385 static inline u_long wfq_hashbysrcport(flow, n)
    386 	struct flowinfo *flow;
    387 	int n;
    388 {
    389 	u_long val = 0;
    390 
    391 	if (flow != NULL) {
    392 		if (flow->fi_family == AF_INET) {
    393 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
    394 
    395 			val = fp->fi_sport;
    396 		}
    397 #ifdef INET6
    398 		else if (flow->fi_family == AF_INET6) {
    399 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
    400 
    401 			val = fp6->fi6_sport;
    402 		}
    403 #endif
    404 	}
    405 	val = val ^ (val >> 8);
    406 
    407 	return (val % n);
    408 }
    409 
    410 static wfq *wfq_maxqueue(wfqp)
    411 	wfq_state_t *wfqp;
    412 {
    413 	int byte, max_byte = 0;
    414 	wfq *queue, *max_queue = NULL;
    415 
    416 	if((queue = wfqp->rrp) == NULL)
    417 		/* never happens */
    418 		return NULL;
    419 	do{
    420 		if ((byte = queue->bytes * 100 / queue->weight) > max_byte) {
    421 			max_queue = queue;
    422 			max_byte = byte;
    423 		}
    424 	} while ((queue = queue->next) != wfqp->rrp);
    425 
    426 	return max_queue;
    427 }
    428 
    429 
    430 static struct mbuf *
    431 wfq_ifdequeue(ifq, op)
    432 	struct ifaltq *ifq;
    433 	int op;
    434 {
    435 	wfq_state_t *wfqp;
    436 	wfq *queue;
    437 	struct mbuf *mp;
    438 	int byte;
    439 
    440 	wfqp = (wfq_state_t *)ifq->altq_disc;
    441 
    442 	if ((wfqp->bytes == 0) || ((queue = wfqp->rrp) == NULL))
    443 		/* no packet in the queues */
    444 		return NULL;
    445 
    446 	while (1) {
    447 		if (queue->quota > 0) {
    448 			if (queue->bytes <= 0) {
    449 				/* this queue no longer has packet.
    450 				   remove the queue from the active list. */
    451 				if (queue->next == queue){
    452 					/* no other active queue
    453 					   -- this case never happens in
    454 					   this algorithm. */
    455 					queue->next = queue->prev = NULL;
    456 					wfqp->rrp = NULL;
    457 					return NULL;
    458 				} else {
    459 					queue->prev->next = queue->next;
    460 					queue->next->prev = queue->prev;
    461 					/* the round-robin pointer points
    462 					   to this queue, advance the rrp */
    463 					wfqp->rrp = queue->next;
    464 					queue->next = queue->prev = NULL;
    465 					queue = wfqp->rrp;
    466 					WFQ_ADDQUOTA(queue);
    467 					continue;
    468 				}
    469 			}
    470 
    471 			/* dequeue a packet from this queue */
    472 			mp = queue->head;
    473 			if (op == ALTDQ_REMOVE) {
    474 				if((queue->head = mp->m_nextpkt) == NULL)
    475 					queue->tail = NULL;
    476 				byte = mp->m_pkthdr.len;
    477 				mp->m_nextpkt = NULL;
    478 				queue->quota -= byte;
    479 				queue->bytes -= byte;
    480 				PKTCNTR_ADD(&queue->xmit_cnt, byte);
    481 				wfqp->bytes -= byte;
    482 				if (ALTQ_IS_ENABLED(ifq))
    483 					ifq->ifq_len--;
    484 			}
    485 			return mp;
    486 
    487 			/* if the queue gets empty by this dequeueing,
    488 			   the queue will be removed from the active list
    489 			   at the next round */
    490 		}
    491 
    492 		/* advance the round-robin pointer */
    493 		queue = wfqp->rrp = queue->next;
    494 		WFQ_ADDQUOTA(queue);
    495 	}
    496 }
    497 
    498 static int
    499 wfq_getqid(gqidp)
    500 	struct wfq_getqid *gqidp;
    501 {
    502 	wfq_state_t *wfqp;
    503 
    504 	if ((wfqp = altq_lookup(gqidp->iface.wfq_ifacename, ALTQT_WFQ))
    505 	    == NULL)
    506 		return (EBADF);
    507 
    508 	gqidp->qid = (*wfqp->hash_func)(&gqidp->flow, wfqp->nums);
    509 	return 0;
    510 }
    511 
    512 static int
    513 wfq_setweight(swp)
    514 	struct wfq_setweight *swp;
    515 {
    516 	wfq_state_t	*wfqp;
    517 	wfq *queue;
    518 	int old;
    519 
    520 	if (swp->weight < 0) {
    521 		printf("set weight in natural number\n");
    522 		return (EINVAL);
    523 	}
    524 
    525 	if ((wfqp = altq_lookup(swp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    526 		return (EBADF);
    527 
    528 	queue = &wfqp->queue[swp->qid];
    529 	old = queue->weight;
    530 	queue->weight = swp->weight;
    531 	swp->weight = old;
    532 	return 0;
    533 }
    534 
    535 
    536 static int
    537 wfq_getstats(gsp)
    538 	struct wfq_getstats *gsp;
    539 {
    540 	wfq_state_t	*wfqp;
    541 	wfq *queue;
    542 	queue_stats *stats;
    543 
    544 	if ((wfqp = altq_lookup(gsp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    545 		return (EBADF);
    546 
    547 	if (gsp->qid >= wfqp->nums)
    548 		return (EINVAL);
    549 
    550 	queue = &wfqp->queue[gsp->qid];
    551 	stats = &gsp->stats;
    552 
    553 	stats->bytes		= queue->bytes;
    554 	stats->weight		= queue->weight;
    555 	stats->xmit_cnt		= queue->xmit_cnt;
    556 	stats->drop_cnt		= queue->drop_cnt;
    557 
    558 	return 0;
    559 }
    560 
    561 
    562 static int
    563 wfq_config(cf)
    564 	struct wfq_conf *cf;
    565 {
    566 	wfq_state_t	*wfqp;
    567 	wfq		*queue;
    568 	int		i, error = 0;
    569 
    570 	if ((wfqp = altq_lookup(cf->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
    571 		return (EBADF);
    572 
    573 	if(cf->nqueues <= 0 ||  MAX_QSIZE < cf->nqueues)
    574 		cf->nqueues = DEFAULT_QSIZE;
    575 
    576 	if (cf->nqueues != wfqp->nums) {
    577 		/* free queued mbuf */
    578 		wfq_flush(wfqp->ifq);
    579 		free(wfqp->queue, M_DEVBUF);
    580 
    581 		queue = malloc(sizeof(wfq) * cf->nqueues, M_DEVBUF,
    582 		    M_WAITOK|M_ZERO);
    583 		if (queue == NULL)
    584 			return (ENOMEM);
    585 
    586 		wfqp->nums = cf->nqueues;
    587 		wfqp->bytes = 0;
    588 		wfqp->rrp = NULL;
    589 		wfqp->queue = queue;
    590 		for (i = 0; i < wfqp->nums; i++, queue++) {
    591 			queue->next = queue->prev = NULL;
    592 			queue->head = queue->tail = NULL;
    593 			queue->bytes = queue->quota = 0;
    594 			queue->weight = 100;
    595 		}
    596 	}
    597 
    598 	if (cf->qlimit != 0)
    599 		wfqp->hwm = cf->qlimit;
    600 
    601 	switch (cf->hash_policy) {
    602 	case WFQ_HASH_DSTADDR:
    603 		wfqp->hash_func = wfq_hashbydstaddr;
    604 		wfqp->fbmask = FIMB4_DADDR;
    605 #ifdef INET6
    606 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    607 #endif
    608 		break;
    609 	case WFQ_HASH_SRCPORT:
    610 		wfqp->hash_func = wfq_hashbysrcport;
    611 		wfqp->fbmask = FIMB4_SPORT;
    612 #ifdef INET6
    613 		wfqp->fbmask |= FIMB6_SPORT;
    614 #endif
    615 		break;
    616 	case WFQ_HASH_FULL:
    617 		wfqp->hash_func = wfq_hash;
    618 		wfqp->fbmask = FIMB4_ALL;
    619 #ifdef INET6
    620 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    621 #endif
    622 		break;
    623 	default:
    624 		error = EINVAL;
    625 		break;
    626 	}
    627 	return error;
    628 }
    629 
    630 /*
    631  * wfq device interface
    632  */
    633 
    634 altqdev_decl(wfq);
    635 
    636 int
    637 wfqopen(dev, flag, fmt, l)
    638 	dev_t dev;
    639 	int flag, fmt;
    640 	struct lwp *l;
    641 {
    642 	return 0;
    643 }
    644 
    645 int
    646 wfqclose(dev, flag, fmt, l)
    647 	dev_t dev;
    648 	int flag, fmt;
    649 	struct lwp *l;
    650 {
    651 	struct ifnet *ifp;
    652 	struct wfq_interface iface;
    653 	wfq_state_t *wfqp;
    654 	int s;
    655 
    656 	s = splnet();
    657 	while ((wfqp = wfq_list) != NULL) {
    658 		ifp = wfqp->ifq->altq_ifp;
    659 #if defined(__NetBSD__) || defined(__OpenBSD__)
    660 		sprintf(iface.wfq_ifacename, "%s", ifp->if_xname);
    661 #else
    662 		sprintf(iface.wfq_ifacename, "%s%d",
    663 			ifp->if_name, ifp->if_unit);
    664 #endif
    665 		wfq_ifdetach(&iface);
    666 	}
    667 	splx(s);
    668 	return 0;
    669 }
    670 
    671 int
    672 wfqioctl(dev, cmd, addr, flag, l)
    673 	dev_t dev;
    674 	ioctlcmd_t cmd;
    675 	caddr_t addr;
    676 	int flag;
    677 	struct lwp *l;
    678 {
    679 	struct proc *p = l->l_proc;
    680 	int	error = 0;
    681 	int 	s;
    682 
    683 	/* check cmd for superuser only */
    684 	switch (cmd) {
    685 	case WFQ_GET_QID:
    686 	case WFQ_GET_STATS:
    687 		break;
    688 	default:
    689 #if (__FreeBSD_version > 400000)
    690 		if ((error = suser(p)) != 0)
    691 #else
    692 		if ((error = kauth_authorize_generic(p->p_cred,
    693 					       KAUTH_GENERIC_ISSUSER,
    694 					       &p->p_acflag)) != 0)
    695 #endif
    696 			return (error);
    697 		break;
    698 	}
    699 
    700 	s = splnet();
    701 	switch (cmd) {
    702 
    703 	case WFQ_ENABLE:
    704 		error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
    705 		break;
    706 
    707 	case WFQ_DISABLE:
    708 		error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
    709 		break;
    710 
    711 	case WFQ_IF_ATTACH:
    712 		error = wfq_ifattach((struct wfq_interface *)addr);
    713 		break;
    714 
    715 	case WFQ_IF_DETACH:
    716 		error = wfq_ifdetach((struct wfq_interface *)addr);
    717 		break;
    718 
    719 	case WFQ_GET_QID:
    720 		error = wfq_getqid((struct wfq_getqid *)addr);
    721 		break;
    722 
    723 	case WFQ_SET_WEIGHT:
    724 		error = wfq_setweight((struct wfq_setweight *)addr);
    725 		break;
    726 
    727 	case WFQ_GET_STATS:
    728 		error = wfq_getstats((struct wfq_getstats *)addr);
    729 		break;
    730 
    731 	case WFQ_CONFIG:
    732 		error = wfq_config((struct wfq_conf *)addr);
    733 		break;
    734 
    735 	default:
    736 		error = EINVAL;
    737 		break;
    738 	}
    739 	splx(s);
    740 	return error;
    741 }
    742 
    743 #ifdef KLD_MODULE
    744 
    745 static struct altqsw wfq_sw =
    746 	{"wfq", wfqopen, wfqclose, wfqioctl};
    747 
    748 ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
    749 
    750 #endif /* KLD_MODULE */
    751 
    752 #endif /* ALTQ_WFQ */
    753