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