1 /*	$NetBSD: altq_wfq.c,v 1.21 2016/04/20 08:58:48 knakahara Exp $	*/
2 /*	$KAME: altq_wfq.c,v 1.14 2005/04/13 03:44:25 suz Exp $	*/
3 
4 /*
5  * Copyright (C) 1997-2002
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@mt.cs.keio.ac.jp).
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: altq_wfq.c,v 1.21 2016/04/20 08:58:48 knakahara Exp $");
36 
37 #ifdef _KERNEL_OPT
38 #include "opt_altq.h"
39 #include "opt_inet.h"
40 #endif
41 
42 #ifdef ALTQ_WFQ
43 
44 #include <sys/param.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/uio.h>
48 #include <sys/socket.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/errno.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/kauth.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 #ifdef ALTQ3_COMPAT
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 static u_long		wfq_hash(struct flowinfo *, int);
74 static inline u_long	wfq_hashbydstaddr(struct flowinfo *, int);
75 static inline u_long	wfq_hashbysrcaddr(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(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
wfq_setenable(struct wfq_interface * ifacep,int flag)91 wfq_setenable(struct wfq_interface *ifacep, int flag)
92 {
93 	wfq_state_t *wfqp;
94 	int error = 0;
95 
96 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
97 		return (EBADF);
98 
99 	switch(flag){
100 	case ENABLE:
101 		error = altq_enable(wfqp->ifq);
102 		break;
103 	case DISABLE:
104 		error = altq_disable(wfqp->ifq);
105 		break;
106 	}
107 	return error;
108 }
109 
110 
111 static int
wfq_ifattach(struct wfq_interface * ifacep)112 wfq_ifattach(struct wfq_interface *ifacep)
113 {
114 	int error = 0, i;
115 	struct ifnet *ifp;
116 	wfq_state_t *new_wfqp;
117 	wfq *queue;
118 
119 	if ((ifp = ifunit(ifacep->wfq_ifacename)) == NULL) {
120 #ifdef WFQ_DEBUG
121 		printf("wfq_ifattach()...no ifp found\n");
122 #endif
123 		return (ENXIO);
124 	}
125 
126 	if (!ALTQ_IS_READY(&ifp->if_snd)) {
127 #ifdef WFQ_DEBUG
128 		printf("wfq_ifattach()...altq is not ready\n");
129 #endif
130 		return (ENXIO);
131 	}
132 
133 	/* allocate and initialize wfq_state_t */
134 	new_wfqp = malloc(sizeof(wfq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
135 	if (new_wfqp == NULL)
136 		return (ENOMEM);
137 
138 	queue = malloc(sizeof(wfq) * DEFAULT_QSIZE, M_DEVBUF, M_WAITOK|M_ZERO);
139 	if (queue == NULL) {
140 		free(new_wfqp, M_DEVBUF);
141 		return (ENOMEM);
142 	}
143 
144 	/* keep the ifq */
145 	new_wfqp->ifq = &ifp->if_snd;
146 	new_wfqp->nums = DEFAULT_QSIZE;
147 	new_wfqp->hwm = HWM;
148 	new_wfqp->bytes = 0;
149 	new_wfqp->rrp = NULL;
150 	new_wfqp->queue = queue;
151 	new_wfqp->hash_func = wfq_hashbydstaddr;
152 	new_wfqp->fbmask = FIMB4_DADDR;
153 
154 	for (i = 0; i < new_wfqp->nums; i++, queue++) {
155 		queue->next = queue->prev = NULL;
156 		queue->head = queue->tail = NULL;
157 		queue->bytes = queue->quota = 0;
158 		queue->weight = 100;
159 	}
160 
161 	/*
162 	 * set WFQ to this ifnet structure.
163 	 */
164 	if ((error = altq_attach(&ifp->if_snd, ALTQT_WFQ, new_wfqp,
165 				 wfq_ifenqueue, wfq_ifdequeue, wfq_request,
166 				 new_wfqp, wfq_classify)) != 0) {
167 		free(queue, M_DEVBUF);
168 		free(new_wfqp, M_DEVBUF);
169 		return (error);
170 	}
171 
172 	new_wfqp->next = wfq_list;
173 	wfq_list = new_wfqp;
174 
175 	return (error);
176 }
177 
178 
179 static int
wfq_ifdetach(struct wfq_interface * ifacep)180 wfq_ifdetach(struct wfq_interface *ifacep)
181 {
182 	int		error = 0;
183 	wfq_state_t	*wfqp;
184 
185 	if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
186 		return (EBADF);
187 
188 	/* free queued mbuf */
189 	wfq_flush(wfqp->ifq);
190 
191 	/* remove WFQ from the ifnet structure. */
192 	(void)altq_disable(wfqp->ifq);
193 	(void)altq_detach(wfqp->ifq);
194 
195 	/* remove from the wfqstate list */
196 	if (wfq_list == wfqp)
197 		wfq_list = wfqp->next;
198 	else {
199 		wfq_state_t *wp = wfq_list;
200 		do {
201 			if (wp->next == wfqp) {
202 				wp->next = wfqp->next;
203 				break;
204 			}
205 		} while ((wp = wp->next) != NULL);
206 	}
207 
208 	/* deallocate wfq_state_t */
209 	free(wfqp->queue, M_DEVBUF);
210 	free(wfqp, M_DEVBUF);
211 	return (error);
212 }
213 
214 static int
wfq_request(struct ifaltq * ifq,int req,void * arg)215 wfq_request(struct ifaltq *ifq, int req, void *arg)
216 {
217 	wfq_state_t *wfqp = (wfq_state_t *)ifq->altq_disc;
218 
219 	switch (req) {
220 	case ALTRQ_PURGE:
221 		wfq_flush(wfqp->ifq);
222 		break;
223 	}
224 	return (0);
225 }
226 
227 
228 static int
wfq_flush(struct ifaltq * ifq)229 wfq_flush(struct ifaltq *ifq)
230 {
231 	struct mbuf *mp;
232 
233 	while ((mp = wfq_ifdequeue(ifq, ALTDQ_REMOVE)) != NULL)
234 		m_freem(mp);
235 	if (ALTQ_IS_ENABLED(ifq))
236 		ifq->ifq_len = 0;
237 	return 0;
238 }
239 
240 static void *
wfq_classify(void * clfier,struct mbuf * m,int af)241 wfq_classify(void *clfier, struct mbuf *m, int af)
242 {
243 	wfq_state_t *wfqp = (wfq_state_t *)clfier;
244 	struct flowinfo flow;
245 
246 	altq_extractflow(m, af, &flow, wfqp->fbmask);
247 	return (&wfqp->queue[(*wfqp->hash_func)(&flow, wfqp->nums)]);
248 }
249 
250 static int
wfq_ifenqueue(struct ifaltq * ifq,struct mbuf * mp)251 wfq_ifenqueue(struct ifaltq *ifq, struct mbuf *mp)
252 {
253 	wfq_state_t *wfqp;
254 	wfq *queue;
255 	int byte, error = 0;
256 
257 	wfqp = (wfq_state_t *)ifq->altq_disc;
258 	mp->m_nextpkt = NULL;
259 
260 	/* grab a queue selected by classifier */
261 	if ((queue = mp->m_pkthdr.pattr_class) == NULL)
262 		queue = &wfqp->queue[0];
263 
264 	if (queue->tail == NULL)
265 		queue->head = mp;
266 	else
267 		queue->tail->m_nextpkt = mp;
268 	queue->tail = mp;
269 	byte = mp->m_pkthdr.len;
270 	queue->bytes += byte;
271 	wfqp->bytes += byte;
272 	ifq->ifq_len++;
273 
274 	if (queue->next == NULL) {
275 		/* this queue gets active. add the queue to the active list */
276 		if (wfqp->rrp == NULL){
277 			/* no queue in the active list */
278 			queue->next = queue->prev = queue;
279 			wfqp->rrp = queue;
280 			WFQ_ADDQUOTA(queue);
281 		} else {
282 			/* insert the queue at the tail of the active list */
283 			queue->prev = wfqp->rrp->prev;
284 			wfqp->rrp->prev->next = queue;
285 			wfqp->rrp->prev = queue;
286 			queue->next = wfqp->rrp;
287 			queue->quota = 0;
288 		}
289 	}
290 
291 	/* check overflow. if the total size exceeds the high water mark,
292 	   drop packets from the longest queue. */
293 	while (wfqp->bytes > wfqp->hwm) {
294 		wfq *drop_queue = wfq_maxqueue(wfqp);
295 
296 		/* drop the packet at the head. */
297 		mp = drop_queue->head;
298 		if ((drop_queue->head = mp->m_nextpkt) == NULL)
299 			drop_queue->tail = NULL;
300 		mp->m_nextpkt = NULL;
301 		byte = mp->m_pkthdr.len;
302 		drop_queue->bytes -= byte;
303 		PKTCNTR_ADD(&drop_queue->drop_cnt, byte);
304 		wfqp->bytes -= byte;
305 		m_freem(mp);
306 		ifq->ifq_len--;
307 		if(drop_queue == queue)
308 			/* the queue for this flow is selected to drop */
309 			error = ENOBUFS;
310 	}
311 	return error;
312 }
313 
314 static u_long
wfq_hash(struct flowinfo * flow,int n)315 wfq_hash(struct flowinfo *flow, int n)
316 {
317 	u_long val = 0;
318 
319 	if (flow != NULL) {
320 		if (flow->fi_family == AF_INET) {
321 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
322 			u_long val2;
323 
324 			val = fp->fi_dst.s_addr ^ fp->fi_src.s_addr;
325 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
326 			val2 = fp->fi_dport ^ fp->fi_sport ^ fp->fi_proto;
327 			val2 = val2 ^ (val2 >> 8);
328 			val = val ^ val2;
329 		}
330 #ifdef INET6
331 		else if (flow->fi_family == AF_INET6) {
332 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
333 
334 			val = ntohl(fp6->fi6_flowlabel);
335 		}
336 #endif
337 	}
338 
339 	return (val % n);
340 }
341 
342 static inline u_long
wfq_hashbydstaddr(struct flowinfo * flow,int n)343 wfq_hashbydstaddr(struct flowinfo *flow, int n)
344 {
345 	u_long val = 0;
346 
347 	if (flow != NULL) {
348 		if (flow->fi_family == AF_INET) {
349 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
350 
351 			val = fp->fi_dst.s_addr;
352 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
353 		}
354 #ifdef INET6
355 		else if (flow->fi_family == AF_INET6) {
356 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
357 
358 			val = ntohl(fp6->fi6_flowlabel);
359 		}
360 #endif
361 	}
362 
363 	return (val % n);
364 }
365 
366 static inline u_long
wfq_hashbysrcaddr(struct flowinfo * flow,int n)367 wfq_hashbysrcaddr(struct flowinfo *flow, int n)
368 {
369 	u_long val = 0;
370 
371 	if (flow != NULL) {
372 		if (flow->fi_family == AF_INET) {
373 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
374 
375 			val = fp->fi_src.s_addr;
376 			val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
377 		}
378 #ifdef INET6
379 		else if (flow->fi_family == AF_INET6) {
380 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
381 
382 			val = ntohl(fp6->fi6_flowlabel);
383 		}
384 #endif
385 	}
386 
387 	return (val % n);
388 }
389 
390 static inline u_long
wfq_hashbysrcport(struct flowinfo * flow,int n)391 wfq_hashbysrcport(struct flowinfo *flow, int n)
392 {
393 	u_long val = 0;
394 
395 	if (flow != NULL) {
396 		if (flow->fi_family == AF_INET) {
397 			struct flowinfo_in *fp = (struct flowinfo_in *)flow;
398 
399 			val = fp->fi_sport;
400 		}
401 #ifdef INET6
402 		else if (flow->fi_family == AF_INET6) {
403 			struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
404 
405 			val = fp6->fi6_sport;
406 		}
407 #endif
408 	}
409 	val = val ^ (val >> 8);
410 
411 	return (val % n);
412 }
413 
414 static wfq *
wfq_maxqueue(wfq_state_t * wfqp)415 wfq_maxqueue(wfq_state_t *wfqp)
416 {
417 	int byte, max_byte = 0;
418 	wfq *queue, *max_queue = NULL;
419 
420 	if((queue = wfqp->rrp) == NULL)
421 		/* never happens */
422 		return NULL;
423 	do{
424 		if ((byte = queue->bytes * 100 / queue->weight) > max_byte) {
425 			max_queue = queue;
426 			max_byte = byte;
427 		}
428 	} while ((queue = queue->next) != wfqp->rrp);
429 
430 	return max_queue;
431 }
432 
433 
434 static struct mbuf *
wfq_ifdequeue(struct ifaltq * ifq,int op)435 wfq_ifdequeue(struct ifaltq *ifq, 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
wfq_getqid(struct wfq_getqid * gqidp)501 wfq_getqid(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
wfq_setweight(struct wfq_setweight * swp)514 wfq_setweight(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
wfq_getstats(struct wfq_getstats * gsp)537 wfq_getstats(struct wfq_getstats *gsp)
538 {
539 	wfq_state_t	*wfqp;
540 	wfq *queue;
541 	queue_stats *stats;
542 
543 	if ((wfqp = altq_lookup(gsp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
544 		return (EBADF);
545 
546 	if (gsp->qid >= wfqp->nums)
547 		return (EINVAL);
548 
549 	queue = &wfqp->queue[gsp->qid];
550 	stats = &gsp->stats;
551 
552 	stats->bytes		= queue->bytes;
553 	stats->weight		= queue->weight;
554 	stats->xmit_cnt		= queue->xmit_cnt;
555 	stats->drop_cnt		= queue->drop_cnt;
556 
557 	return 0;
558 }
559 
560 
561 static int
wfq_config(struct wfq_conf * cf)562 wfq_config(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 	case WFQ_HASH_SRCADDR:
622 		wfqp->hash_func = wfq_hashbysrcaddr;
623 		wfqp->fbmask = FIMB4_DADDR;
624 #ifdef INET6
625 		wfqp->fbmask |= FIMB6_FLABEL;	/* use flowlabel for ipv6 */
626 #endif
627 		break;
628 	default:
629 		error = EINVAL;
630 		break;
631 	}
632 	return error;
633 }
634 
635 /*
636  * wfq device interface
637  */
638 
639 altqdev_decl(wfq);
640 
641 int
wfqopen(dev_t dev,int flag,int fmt,struct lwp * l)642 wfqopen(dev_t dev, int flag, int fmt,
643     struct lwp *l)
644 {
645 	return 0;
646 }
647 
648 int
wfqclose(dev_t dev,int flag,int fmt,struct lwp * l)649 wfqclose(dev_t dev, int flag, int 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 		snprintf(iface.wfq_ifacename, sizeof(iface.wfq_ifacename),
661 		    "%s", ifp->if_xname);
662 		wfq_ifdetach(&iface);
663 	}
664 	splx(s);
665 	return 0;
666 }
667 
668 int
wfqioctl(dev_t dev,ioctlcmd_t cmd,void * addr,int flag,struct lwp * l)669 wfqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
670     struct lwp *l)
671 {
672 	int	error = 0;
673 	int 	s;
674 
675 	/* check cmd for superuser only */
676 	switch (cmd) {
677 	case WFQ_GET_QID:
678 	case WFQ_GET_STATS:
679 		break;
680 	default:
681 #if (__FreeBSD_version > 400000)
682 		if ((error = suser(p)) != 0)
683 #else
684 		if ((error = kauth_authorize_network(l->l_cred,
685 		    KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_WFQ, NULL,
686 		    NULL, NULL)) != 0)
687 #endif
688 			return (error);
689 		break;
690 	}
691 
692 	s = splnet();
693 	switch (cmd) {
694 
695 	case WFQ_ENABLE:
696 		error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
697 		break;
698 
699 	case WFQ_DISABLE:
700 		error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
701 		break;
702 
703 	case WFQ_IF_ATTACH:
704 		error = wfq_ifattach((struct wfq_interface *)addr);
705 		break;
706 
707 	case WFQ_IF_DETACH:
708 		error = wfq_ifdetach((struct wfq_interface *)addr);
709 		break;
710 
711 	case WFQ_GET_QID:
712 		error = wfq_getqid((struct wfq_getqid *)addr);
713 		break;
714 
715 	case WFQ_SET_WEIGHT:
716 		error = wfq_setweight((struct wfq_setweight *)addr);
717 		break;
718 
719 	case WFQ_GET_STATS:
720 		error = wfq_getstats((struct wfq_getstats *)addr);
721 		break;
722 
723 	case WFQ_CONFIG:
724 		error = wfq_config((struct wfq_conf *)addr);
725 		break;
726 
727 	default:
728 		error = EINVAL;
729 		break;
730 	}
731 	splx(s);
732 	return error;
733 }
734 
735 #ifdef KLD_MODULE
736 
737 static struct altqsw wfq_sw =
738 	{"wfq", wfqopen, wfqclose, wfqioctl};
739 
740 ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
741 
742 #endif /* KLD_MODULE */
743 
744 #endif /* ALTQ3_COMPAT */
745 #endif /* ALTQ_WFQ */
746