Home | History | Annotate | Line # | Download | only in altq
altq_wfq.c revision 1.12
      1 /*	$NetBSD: altq_wfq.c,v 1.12 2006/05/15 00:05:39 christos 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.12 2006/05/15 00:05:39 christos 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 #include <sys/kauth.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_types.h>
     62 #include <netinet/in.h>
     63 
     64 #include <altq/altq.h>
     65 #include <altq/altq_conf.h>
     66 #include <altq/altq_wfq.h>
     67 
     68 /*
     69 #define	WFQ_DEBUG
     70 */
     71 
     72 static int		wfq_setenable(struct wfq_interface *, int);
     73 static int		wfq_ifattach(struct wfq_interface *);
     74 static int		wfq_ifdetach(struct wfq_interface *);
     75 static int		wfq_ifenqueue(struct ifaltq *, struct mbuf *,
     76 				      struct altq_pktattr *);
     77 static u_long		wfq_hash(struct flowinfo *, int);
     78 static inline u_long	wfq_hashbydstaddr(struct flowinfo *, int);
     79 static inline u_long	wfq_hashbysrcport(struct flowinfo *, int);
     80 static wfq		*wfq_maxqueue(wfq_state_t *);
     81 static struct mbuf	*wfq_ifdequeue(struct ifaltq *, int);
     82 static int		wfq_getqid(struct wfq_getqid *);
     83 static int		wfq_setweight(struct wfq_setweight *);
     84 static int		wfq_getstats(struct wfq_getstats *);
     85 static int		wfq_config(struct wfq_conf *);
     86 static int		wfq_request __P((struct ifaltq *, int, void *));
     87 static int		wfq_flush(struct ifaltq *);
     88 static void		*wfq_classify(void *, struct mbuf *, int);
     89 
     90 /* global value : pointer to wfq queue list */
     91 static wfq_state_t *wfq_list = NULL;
     92 
     93 static int
     94 wfq_setenable(ifacep, flag)
     95 	struct wfq_interface *ifacep;
     96 	int flag;
     97 {
     98 	wfq_state_t *wfqp;
     99 	int error = 0;
    100 
    101 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
    102 		return (EBADF);
    103 
    104 	switch(flag){
    105 	case ENABLE:
    106 		error = altq_enable(wfqp->ifq);
    107 		break;
    108 	case DISABLE:
    109 		error = altq_disable(wfqp->ifq);
    110 		break;
    111 	}
    112 	return error;
    113 }
    114 
    115 
    116 static int
    117 wfq_ifattach(ifacep)
    118 	struct wfq_interface *ifacep;
    119 {
    120 	int error = 0, i;
    121 	struct ifnet *ifp;
    122 	wfq_state_t *new_wfqp;
    123 	wfq *queue;
    124 
    125 	if ((ifp = ifunit(ifacep->wfq_ifacename)) == NULL) {
    126 #ifdef WFQ_DEBUG
    127 		printf("wfq_ifattach()...no ifp found\n");
    128 #endif
    129 		return (ENXIO);
    130 	}
    131 
    132 	if (!ALTQ_IS_READY(&ifp->if_snd)) {
    133 #ifdef WFQ_DEBUG
    134 		printf("wfq_ifattach()...altq is not ready\n");
    135 #endif
    136 		return (ENXIO);
    137 	}
    138 
    139 	/* allocate and initialize wfq_state_t */
    140 	new_wfqp = malloc(sizeof(wfq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    141 	if (new_wfqp == NULL)
    142 		return (ENOMEM);
    143 	queue = malloc(sizeof(wfq) * DEFAULT_QSIZE, M_DEVBUF, M_WAITOK|M_ZERO);
    144 	if (queue == NULL) {
    145 		free(new_wfqp, M_DEVBUF);
    146 		return (ENOMEM);
    147 	}
    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 		queue = malloc(sizeof(wfq) * cf->nqueues, M_DEVBUF,
    583 		    M_WAITOK|M_ZERO);
    584 		if (queue == NULL)
    585 			return (ENOMEM);
    586 
    587 		wfqp->nums = cf->nqueues;
    588 		wfqp->bytes = 0;
    589 		wfqp->rrp = NULL;
    590 		wfqp->queue = queue;
    591 		for (i = 0; i < wfqp->nums; i++, queue++) {
    592 			queue->next = queue->prev = NULL;
    593 			queue->head = queue->tail = NULL;
    594 			queue->bytes = queue->quota = 0;
    595 			queue->weight = 100;
    596 		}
    597 	}
    598 
    599 	if (cf->qlimit != 0)
    600 		wfqp->hwm = cf->qlimit;
    601 
    602 	switch (cf->hash_policy) {
    603 	case WFQ_HASH_DSTADDR:
    604 		wfqp->hash_func = wfq_hashbydstaddr;
    605 		wfqp->fbmask = FIMB4_DADDR;
    606 #ifdef INET6
    607 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    608 #endif
    609 		break;
    610 	case WFQ_HASH_SRCPORT:
    611 		wfqp->hash_func = wfq_hashbysrcport;
    612 		wfqp->fbmask = FIMB4_SPORT;
    613 #ifdef INET6
    614 		wfqp->fbmask |= FIMB6_SPORT;
    615 #endif
    616 		break;
    617 	case WFQ_HASH_FULL:
    618 		wfqp->hash_func = wfq_hash;
    619 		wfqp->fbmask = FIMB4_ALL;
    620 #ifdef INET6
    621 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
    622 #endif
    623 		break;
    624 	default:
    625 		error = EINVAL;
    626 		break;
    627 	}
    628 	return error;
    629 }
    630 
    631 /*
    632  * wfq device interface
    633  */
    634 
    635 altqdev_decl(wfq);
    636 
    637 int
    638 wfqopen(dev, flag, fmt, l)
    639 	dev_t dev;
    640 	int flag, fmt;
    641 	struct lwp *l;
    642 {
    643 	return 0;
    644 }
    645 
    646 int
    647 wfqclose(dev, flag, fmt, l)
    648 	dev_t dev;
    649 	int flag, fmt;
    650 	struct lwp *l;
    651 {
    652 	struct ifnet *ifp;
    653 	struct wfq_interface iface;
    654 	wfq_state_t *wfqp;
    655 	int s;
    656 
    657 	s = splnet();
    658 	while ((wfqp = wfq_list) != NULL) {
    659 		ifp = wfqp->ifq->altq_ifp;
    660 #if defined(__NetBSD__) || defined(__OpenBSD__)
    661 		sprintf(iface.wfq_ifacename, "%s", ifp->if_xname);
    662 #else
    663 		sprintf(iface.wfq_ifacename, "%s%d",
    664 			ifp->if_name, ifp->if_unit);
    665 #endif
    666 		wfq_ifdetach(&iface);
    667 	}
    668 	splx(s);
    669 	return 0;
    670 }
    671 
    672 int
    673 wfqioctl(dev, cmd, addr, flag, l)
    674 	dev_t dev;
    675 	ioctlcmd_t cmd;
    676 	caddr_t addr;
    677 	int flag;
    678 	struct lwp *l;
    679 {
    680 	struct proc *p = l->l_proc;
    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 = kauth_authorize_generic(p->p_cred,
    694 					       KAUTH_GENERIC_ISSUSER,
    695 					       &p->p_acflag)) != 0)
    696 #endif
    697 			return (error);
    698 		break;
    699 	}
    700 
    701 	s = splnet();
    702 	switch (cmd) {
    703 
    704 	case WFQ_ENABLE:
    705 		error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
    706 		break;
    707 
    708 	case WFQ_DISABLE:
    709 		error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
    710 		break;
    711 
    712 	case WFQ_IF_ATTACH:
    713 		error = wfq_ifattach((struct wfq_interface *)addr);
    714 		break;
    715 
    716 	case WFQ_IF_DETACH:
    717 		error = wfq_ifdetach((struct wfq_interface *)addr);
    718 		break;
    719 
    720 	case WFQ_GET_QID:
    721 		error = wfq_getqid((struct wfq_getqid *)addr);
    722 		break;
    723 
    724 	case WFQ_SET_WEIGHT:
    725 		error = wfq_setweight((struct wfq_setweight *)addr);
    726 		break;
    727 
    728 	case WFQ_GET_STATS:
    729 		error = wfq_getstats((struct wfq_getstats *)addr);
    730 		break;
    731 
    732 	case WFQ_CONFIG:
    733 		error = wfq_config((struct wfq_conf *)addr);
    734 		break;
    735 
    736 	default:
    737 		error = EINVAL;
    738 		break;
    739 	}
    740 	splx(s);
    741 	return error;
    742 }
    743 
    744 #ifdef KLD_MODULE
    745 
    746 static struct altqsw wfq_sw =
    747 	{"wfq", wfqopen, wfqclose, wfqioctl};
    748 
    749 ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
    750 
    751 #endif /* KLD_MODULE */
    752 
    753 #endif /* ALTQ_WFQ */
    754