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