xref: /freebsd/sys/net/altq/altq_priq.c (revision da8ae05d)
1 /*-
2  * Copyright (C) 2000-2003
3  *	Sony Computer Science Laboratories Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $
27  * $FreeBSD$
28  */
29 /*
30  * priority queue
31  */
32 
33 #include "opt_altq.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 
37 #ifdef ALTQ_PRIQ  /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
38 
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
53 
54 #include <netpfil/pf/pf.h>
55 #include <netpfil/pf/pf_altq.h>
56 #include <netpfil/pf/pf_mtag.h>
57 #include <net/altq/altq.h>
58 #ifdef ALTQ3_COMPAT
59 #include <net/altq/altq_conf.h>
60 #endif
61 #include <net/altq/altq_priq.h>
62 
63 /*
64  * function prototypes
65  */
66 #ifdef ALTQ3_COMPAT
67 static struct priq_if *priq_attach(struct ifaltq *, u_int);
68 static int priq_detach(struct priq_if *);
69 #endif
70 static int priq_clear_interface(struct priq_if *);
71 static int priq_request(struct ifaltq *, int, void *);
72 static void priq_purge(struct priq_if *);
73 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
74     int);
75 static int priq_class_destroy(struct priq_class *);
76 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
77 static struct mbuf *priq_dequeue(struct ifaltq *, int);
78 
79 static int priq_addq(struct priq_class *, struct mbuf *);
80 static struct mbuf *priq_getq(struct priq_class *);
81 static struct mbuf *priq_pollq(struct priq_class *);
82 static void priq_purgeq(struct priq_class *);
83 
84 #ifdef ALTQ3_COMPAT
85 static int priqcmd_if_attach(struct priq_interface *);
86 static int priqcmd_if_detach(struct priq_interface *);
87 static int priqcmd_add_class(struct priq_add_class *);
88 static int priqcmd_delete_class(struct priq_delete_class *);
89 static int priqcmd_modify_class(struct priq_modify_class *);
90 static int priqcmd_add_filter(struct priq_add_filter *);
91 static int priqcmd_delete_filter(struct priq_delete_filter *);
92 static int priqcmd_class_stats(struct priq_class_stats *);
93 #endif /* ALTQ3_COMPAT */
94 
95 static void get_class_stats(struct priq_classstats *, struct priq_class *);
96 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
97 
98 #ifdef ALTQ3_COMPAT
99 altqdev_decl(priq);
100 
101 /* pif_list keeps all priq_if's allocated. */
102 static struct priq_if *pif_list = NULL;
103 #endif /* ALTQ3_COMPAT */
104 
105 int
106 priq_pfattach(struct pf_altq *a)
107 {
108 	struct ifnet *ifp;
109 	int s, error;
110 
111 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
112 		return (EINVAL);
113 	s = splnet();
114 	error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
115 	    priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
116 	splx(s);
117 	return (error);
118 }
119 
120 int
121 priq_add_altq(struct pf_altq *a)
122 {
123 	struct priq_if	*pif;
124 	struct ifnet	*ifp;
125 
126 	if ((ifp = ifunit(a->ifname)) == NULL)
127 		return (EINVAL);
128 	if (!ALTQ_IS_READY(&ifp->if_snd))
129 		return (ENODEV);
130 
131 	pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
132 	if (pif == NULL)
133 		return (ENOMEM);
134 	pif->pif_bandwidth = a->ifbandwidth;
135 	pif->pif_maxpri = -1;
136 	pif->pif_ifq = &ifp->if_snd;
137 
138 	/* keep the state in pf_altq */
139 	a->altq_disc = pif;
140 
141 	return (0);
142 }
143 
144 int
145 priq_remove_altq(struct pf_altq *a)
146 {
147 	struct priq_if *pif;
148 
149 	if ((pif = a->altq_disc) == NULL)
150 		return (EINVAL);
151 	a->altq_disc = NULL;
152 
153 	(void)priq_clear_interface(pif);
154 
155 	free(pif, M_DEVBUF);
156 	return (0);
157 }
158 
159 int
160 priq_add_queue(struct pf_altq *a)
161 {
162 	struct priq_if *pif;
163 	struct priq_class *cl;
164 
165 	if ((pif = a->altq_disc) == NULL)
166 		return (EINVAL);
167 
168 	/* check parameters */
169 	if (a->priority >= PRIQ_MAXPRI)
170 		return (EINVAL);
171 	if (a->qid == 0)
172 		return (EINVAL);
173 	if (pif->pif_classes[a->priority] != NULL)
174 		return (EBUSY);
175 	if (clh_to_clp(pif, a->qid) != NULL)
176 		return (EBUSY);
177 
178 	cl = priq_class_create(pif, a->priority, a->qlimit,
179 	    a->pq_u.priq_opts.flags, a->qid);
180 	if (cl == NULL)
181 		return (ENOMEM);
182 
183 	return (0);
184 }
185 
186 int
187 priq_remove_queue(struct pf_altq *a)
188 {
189 	struct priq_if *pif;
190 	struct priq_class *cl;
191 
192 	if ((pif = a->altq_disc) == NULL)
193 		return (EINVAL);
194 
195 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
196 		return (EINVAL);
197 
198 	return (priq_class_destroy(cl));
199 }
200 
201 int
202 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
203 {
204 	struct priq_if *pif;
205 	struct priq_class *cl;
206 	struct priq_classstats stats;
207 	int error = 0;
208 
209 	if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
210 		return (EBADF);
211 
212 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
213 		return (EINVAL);
214 
215 	if (*nbytes < sizeof(stats))
216 		return (EINVAL);
217 
218 	get_class_stats(&stats, cl);
219 
220 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
221 		return (error);
222 	*nbytes = sizeof(stats);
223 	return (0);
224 }
225 
226 /*
227  * bring the interface back to the initial state by discarding
228  * all the filters and classes.
229  */
230 static int
231 priq_clear_interface(struct priq_if *pif)
232 {
233 	struct priq_class	*cl;
234 	int pri;
235 
236 #ifdef ALTQ3_CLFIER_COMPAT
237 	/* free the filters for this interface */
238 	acc_discard_filters(&pif->pif_classifier, NULL, 1);
239 #endif
240 
241 	/* clear out the classes */
242 	for (pri = 0; pri <= pif->pif_maxpri; pri++)
243 		if ((cl = pif->pif_classes[pri]) != NULL)
244 			priq_class_destroy(cl);
245 
246 	return (0);
247 }
248 
249 static int
250 priq_request(struct ifaltq *ifq, int req, void *arg)
251 {
252 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
253 
254 	IFQ_LOCK_ASSERT(ifq);
255 
256 	switch (req) {
257 	case ALTRQ_PURGE:
258 		priq_purge(pif);
259 		break;
260 	}
261 	return (0);
262 }
263 
264 /* discard all the queued packets on the interface */
265 static void
266 priq_purge(struct priq_if *pif)
267 {
268 	struct priq_class *cl;
269 	int pri;
270 
271 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
272 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
273 			priq_purgeq(cl);
274 	}
275 	if (ALTQ_IS_ENABLED(pif->pif_ifq))
276 		pif->pif_ifq->ifq_len = 0;
277 }
278 
279 static struct priq_class *
280 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
281 {
282 	struct priq_class *cl;
283 	int s;
284 
285 #ifndef ALTQ_RED
286 	if (flags & PRCF_RED) {
287 #ifdef ALTQ_DEBUG
288 		printf("priq_class_create: RED not configured for PRIQ!\n");
289 #endif
290 		return (NULL);
291 	}
292 #endif
293 
294 	if ((cl = pif->pif_classes[pri]) != NULL) {
295 		/* modify the class instead of creating a new one */
296 		s = splnet();
297 		IFQ_LOCK(cl->cl_pif->pif_ifq);
298 		if (!qempty(cl->cl_q))
299 			priq_purgeq(cl);
300 		IFQ_UNLOCK(cl->cl_pif->pif_ifq);
301 		splx(s);
302 #ifdef ALTQ_RIO
303 		if (q_is_rio(cl->cl_q))
304 			rio_destroy((rio_t *)cl->cl_red);
305 #endif
306 #ifdef ALTQ_RED
307 		if (q_is_red(cl->cl_q))
308 			red_destroy(cl->cl_red);
309 #endif
310 	} else {
311 		cl = malloc(sizeof(struct priq_class), M_DEVBUF,
312 		    M_NOWAIT | M_ZERO);
313 		if (cl == NULL)
314 			return (NULL);
315 
316 		cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
317 		    M_NOWAIT | M_ZERO);
318 		if (cl->cl_q == NULL)
319 			goto err_ret;
320 	}
321 
322 	pif->pif_classes[pri] = cl;
323 	if (flags & PRCF_DEFAULTCLASS)
324 		pif->pif_default = cl;
325 	if (qlimit == 0)
326 		qlimit = 50;  /* use default */
327 	qlimit(cl->cl_q) = qlimit;
328 	qtype(cl->cl_q) = Q_DROPTAIL;
329 	qlen(cl->cl_q) = 0;
330 	cl->cl_flags = flags;
331 	cl->cl_pri = pri;
332 	if (pri > pif->pif_maxpri)
333 		pif->pif_maxpri = pri;
334 	cl->cl_pif = pif;
335 	cl->cl_handle = qid;
336 
337 #ifdef ALTQ_RED
338 	if (flags & (PRCF_RED|PRCF_RIO)) {
339 		int red_flags, red_pkttime;
340 
341 		red_flags = 0;
342 		if (flags & PRCF_ECN)
343 			red_flags |= REDF_ECN;
344 #ifdef ALTQ_RIO
345 		if (flags & PRCF_CLEARDSCP)
346 			red_flags |= RIOF_CLEARDSCP;
347 #endif
348 		if (pif->pif_bandwidth < 8)
349 			red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
350 		else
351 			red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
352 			  * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
353 #ifdef ALTQ_RIO
354 		if (flags & PRCF_RIO) {
355 			cl->cl_red = (red_t *)rio_alloc(0, NULL,
356 						red_flags, red_pkttime);
357 			if (cl->cl_red == NULL)
358 				goto err_ret;
359 			qtype(cl->cl_q) = Q_RIO;
360 		} else
361 #endif
362 		if (flags & PRCF_RED) {
363 			cl->cl_red = red_alloc(0, 0,
364 			    qlimit(cl->cl_q) * 10/100,
365 			    qlimit(cl->cl_q) * 30/100,
366 			    red_flags, red_pkttime);
367 			if (cl->cl_red == NULL)
368 				goto err_ret;
369 			qtype(cl->cl_q) = Q_RED;
370 		}
371 	}
372 #endif /* ALTQ_RED */
373 
374 	return (cl);
375 
376  err_ret:
377 	if (cl->cl_red != NULL) {
378 #ifdef ALTQ_RIO
379 		if (q_is_rio(cl->cl_q))
380 			rio_destroy((rio_t *)cl->cl_red);
381 #endif
382 #ifdef ALTQ_RED
383 		if (q_is_red(cl->cl_q))
384 			red_destroy(cl->cl_red);
385 #endif
386 	}
387 	if (cl->cl_q != NULL)
388 		free(cl->cl_q, M_DEVBUF);
389 	free(cl, M_DEVBUF);
390 	return (NULL);
391 }
392 
393 static int
394 priq_class_destroy(struct priq_class *cl)
395 {
396 	struct priq_if *pif;
397 	int s, pri;
398 
399 	s = splnet();
400 	IFQ_LOCK(cl->cl_pif->pif_ifq);
401 
402 #ifdef ALTQ3_CLFIER_COMPAT
403 	/* delete filters referencing to this class */
404 	acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
405 #endif
406 
407 	if (!qempty(cl->cl_q))
408 		priq_purgeq(cl);
409 
410 	pif = cl->cl_pif;
411 	pif->pif_classes[cl->cl_pri] = NULL;
412 	if (pif->pif_maxpri == cl->cl_pri) {
413 		for (pri = cl->cl_pri; pri >= 0; pri--)
414 			if (pif->pif_classes[pri] != NULL) {
415 				pif->pif_maxpri = pri;
416 				break;
417 			}
418 		if (pri < 0)
419 			pif->pif_maxpri = -1;
420 	}
421 	IFQ_UNLOCK(cl->cl_pif->pif_ifq);
422 	splx(s);
423 
424 	if (cl->cl_red != NULL) {
425 #ifdef ALTQ_RIO
426 		if (q_is_rio(cl->cl_q))
427 			rio_destroy((rio_t *)cl->cl_red);
428 #endif
429 #ifdef ALTQ_RED
430 		if (q_is_red(cl->cl_q))
431 			red_destroy(cl->cl_red);
432 #endif
433 	}
434 	free(cl->cl_q, M_DEVBUF);
435 	free(cl, M_DEVBUF);
436 	return (0);
437 }
438 
439 /*
440  * priq_enqueue is an enqueue function to be registered to
441  * (*altq_enqueue) in struct ifaltq.
442  */
443 static int
444 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
445 {
446 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
447 	struct priq_class *cl;
448 	struct pf_mtag *t;
449 	int len;
450 
451 	IFQ_LOCK_ASSERT(ifq);
452 
453 	/* grab class set by classifier */
454 	if ((m->m_flags & M_PKTHDR) == 0) {
455 		/* should not happen */
456 		printf("altq: packet for %s does not have pkthdr\n",
457 		    ifq->altq_ifp->if_xname);
458 		m_freem(m);
459 		return (ENOBUFS);
460 	}
461 	cl = NULL;
462 	if ((t = pf_find_mtag(m)) != NULL)
463 		cl = clh_to_clp(pif, t->qid);
464 #ifdef ALTQ3_COMPAT
465 	else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
466 		cl = pktattr->pattr_class;
467 #endif
468 	if (cl == NULL) {
469 		cl = pif->pif_default;
470 		if (cl == NULL) {
471 			m_freem(m);
472 			return (ENOBUFS);
473 		}
474 	}
475 #ifdef ALTQ3_COMPAT
476 	if (pktattr != NULL)
477 		cl->cl_pktattr = pktattr;  /* save proto hdr used by ECN */
478 	else
479 #endif
480 		cl->cl_pktattr = NULL;
481 	len = m_pktlen(m);
482 	if (priq_addq(cl, m) != 0) {
483 		/* drop occurred.  mbuf was freed in priq_addq. */
484 		PKTCNTR_ADD(&cl->cl_dropcnt, len);
485 		return (ENOBUFS);
486 	}
487 	IFQ_INC_LEN(ifq);
488 
489 	/* successfully queued. */
490 	return (0);
491 }
492 
493 /*
494  * priq_dequeue is a dequeue function to be registered to
495  * (*altq_dequeue) in struct ifaltq.
496  *
497  * note: ALTDQ_POLL returns the next packet without removing the packet
498  *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
499  *	ALTDQ_REMOVE must return the same packet if called immediately
500  *	after ALTDQ_POLL.
501  */
502 static struct mbuf *
503 priq_dequeue(struct ifaltq *ifq, int op)
504 {
505 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
506 	struct priq_class *cl;
507 	struct mbuf *m;
508 	int pri;
509 
510 	IFQ_LOCK_ASSERT(ifq);
511 
512 	if (IFQ_IS_EMPTY(ifq))
513 		/* no packet in the queue */
514 		return (NULL);
515 
516 	for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
517 		if ((cl = pif->pif_classes[pri]) != NULL &&
518 		    !qempty(cl->cl_q)) {
519 			if (op == ALTDQ_POLL)
520 				return (priq_pollq(cl));
521 
522 			m = priq_getq(cl);
523 			if (m != NULL) {
524 				IFQ_DEC_LEN(ifq);
525 				if (qempty(cl->cl_q))
526 					cl->cl_period++;
527 				PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
528 			}
529 			return (m);
530 		}
531 	}
532 	return (NULL);
533 }
534 
535 static int
536 priq_addq(struct priq_class *cl, struct mbuf *m)
537 {
538 
539 #ifdef ALTQ_RIO
540 	if (q_is_rio(cl->cl_q))
541 		return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
542 				cl->cl_pktattr);
543 #endif
544 #ifdef ALTQ_RED
545 	if (q_is_red(cl->cl_q))
546 		return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
547 #endif
548 	if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
549 		m_freem(m);
550 		return (-1);
551 	}
552 
553 	if (cl->cl_flags & PRCF_CLEARDSCP)
554 		write_dsfield(m, cl->cl_pktattr, 0);
555 
556 	_addq(cl->cl_q, m);
557 
558 	return (0);
559 }
560 
561 static struct mbuf *
562 priq_getq(struct priq_class *cl)
563 {
564 #ifdef ALTQ_RIO
565 	if (q_is_rio(cl->cl_q))
566 		return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
567 #endif
568 #ifdef ALTQ_RED
569 	if (q_is_red(cl->cl_q))
570 		return red_getq(cl->cl_red, cl->cl_q);
571 #endif
572 	return _getq(cl->cl_q);
573 }
574 
575 static struct mbuf *
576 priq_pollq(cl)
577 	struct priq_class *cl;
578 {
579 	return qhead(cl->cl_q);
580 }
581 
582 static void
583 priq_purgeq(struct priq_class *cl)
584 {
585 	struct mbuf *m;
586 
587 	if (qempty(cl->cl_q))
588 		return;
589 
590 	while ((m = _getq(cl->cl_q)) != NULL) {
591 		PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
592 		m_freem(m);
593 	}
594 	ASSERT(qlen(cl->cl_q) == 0);
595 }
596 
597 static void
598 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
599 {
600 	sp->class_handle = cl->cl_handle;
601 	sp->qlength = qlen(cl->cl_q);
602 	sp->qlimit = qlimit(cl->cl_q);
603 	sp->period = cl->cl_period;
604 	sp->xmitcnt = cl->cl_xmitcnt;
605 	sp->dropcnt = cl->cl_dropcnt;
606 
607 	sp->qtype = qtype(cl->cl_q);
608 #ifdef ALTQ_RED
609 	if (q_is_red(cl->cl_q))
610 		red_getstats(cl->cl_red, &sp->red[0]);
611 #endif
612 #ifdef ALTQ_RIO
613 	if (q_is_rio(cl->cl_q))
614 		rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
615 #endif
616 
617 }
618 
619 /* convert a class handle to the corresponding class pointer */
620 static struct priq_class *
621 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
622 {
623 	struct priq_class *cl;
624 	int idx;
625 
626 	if (chandle == 0)
627 		return (NULL);
628 
629 	for (idx = pif->pif_maxpri; idx >= 0; idx--)
630 		if ((cl = pif->pif_classes[idx]) != NULL &&
631 		    cl->cl_handle == chandle)
632 			return (cl);
633 
634 	return (NULL);
635 }
636 
637 
638 #ifdef ALTQ3_COMPAT
639 
640 static struct priq_if *
641 priq_attach(ifq, bandwidth)
642 	struct ifaltq *ifq;
643 	u_int bandwidth;
644 {
645 	struct priq_if *pif;
646 
647 	pif = malloc(sizeof(struct priq_if),
648 	       M_DEVBUF, M_WAITOK);
649 	if (pif == NULL)
650 		return (NULL);
651 	bzero(pif, sizeof(struct priq_if));
652 	pif->pif_bandwidth = bandwidth;
653 	pif->pif_maxpri = -1;
654 	pif->pif_ifq = ifq;
655 
656 	/* add this state to the priq list */
657 	pif->pif_next = pif_list;
658 	pif_list = pif;
659 
660 	return (pif);
661 }
662 
663 static int
664 priq_detach(pif)
665 	struct priq_if *pif;
666 {
667 	(void)priq_clear_interface(pif);
668 
669 	/* remove this interface from the pif list */
670 	if (pif_list == pif)
671 		pif_list = pif->pif_next;
672 	else {
673 		struct priq_if *p;
674 
675 		for (p = pif_list; p != NULL; p = p->pif_next)
676 			if (p->pif_next == pif) {
677 				p->pif_next = pif->pif_next;
678 				break;
679 			}
680 		ASSERT(p != NULL);
681 	}
682 
683 	free(pif, M_DEVBUF);
684 	return (0);
685 }
686 
687 /*
688  * priq device interface
689  */
690 int
691 priqopen(dev, flag, fmt, p)
692 	dev_t dev;
693 	int flag, fmt;
694 #if (__FreeBSD_version > 500000)
695 	struct thread *p;
696 #else
697 	struct proc *p;
698 #endif
699 {
700 	/* everything will be done when the queueing scheme is attached. */
701 	return 0;
702 }
703 
704 int
705 priqclose(dev, flag, fmt, p)
706 	dev_t dev;
707 	int flag, fmt;
708 #if (__FreeBSD_version > 500000)
709 	struct thread *p;
710 #else
711 	struct proc *p;
712 #endif
713 {
714 	struct priq_if *pif;
715 	int err, error = 0;
716 
717 	while ((pif = pif_list) != NULL) {
718 		/* destroy all */
719 		if (ALTQ_IS_ENABLED(pif->pif_ifq))
720 			altq_disable(pif->pif_ifq);
721 
722 		err = altq_detach(pif->pif_ifq);
723 		if (err == 0)
724 			err = priq_detach(pif);
725 		if (err != 0 && error == 0)
726 			error = err;
727 	}
728 
729 	return error;
730 }
731 
732 int
733 priqioctl(dev, cmd, addr, flag, p)
734 	dev_t dev;
735 	ioctlcmd_t cmd;
736 	caddr_t addr;
737 	int flag;
738 #if (__FreeBSD_version > 500000)
739 	struct thread *p;
740 #else
741 	struct proc *p;
742 #endif
743 {
744 	struct priq_if *pif;
745 	struct priq_interface *ifacep;
746 	int	error = 0;
747 
748 	/* check super-user privilege */
749 	switch (cmd) {
750 	case PRIQ_GETSTATS:
751 		break;
752 	default:
753 #if (__FreeBSD_version > 700000)
754 		if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
755 			return (error);
756 #elsif (__FreeBSD_version > 400000)
757 		if ((error = suser(p)) != 0)
758 			return (error);
759 #else
760 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
761 			return (error);
762 #endif
763 		break;
764 	}
765 
766 	switch (cmd) {
767 
768 	case PRIQ_IF_ATTACH:
769 		error = priqcmd_if_attach((struct priq_interface *)addr);
770 		break;
771 
772 	case PRIQ_IF_DETACH:
773 		error = priqcmd_if_detach((struct priq_interface *)addr);
774 		break;
775 
776 	case PRIQ_ENABLE:
777 	case PRIQ_DISABLE:
778 	case PRIQ_CLEAR:
779 		ifacep = (struct priq_interface *)addr;
780 		if ((pif = altq_lookup(ifacep->ifname,
781 				       ALTQT_PRIQ)) == NULL) {
782 			error = EBADF;
783 			break;
784 		}
785 
786 		switch (cmd) {
787 		case PRIQ_ENABLE:
788 			if (pif->pif_default == NULL) {
789 #ifdef ALTQ_DEBUG
790 				printf("priq: no default class\n");
791 #endif
792 				error = EINVAL;
793 				break;
794 			}
795 			error = altq_enable(pif->pif_ifq);
796 			break;
797 
798 		case PRIQ_DISABLE:
799 			error = altq_disable(pif->pif_ifq);
800 			break;
801 
802 		case PRIQ_CLEAR:
803 			priq_clear_interface(pif);
804 			break;
805 		}
806 		break;
807 
808 	case PRIQ_ADD_CLASS:
809 		error = priqcmd_add_class((struct priq_add_class *)addr);
810 		break;
811 
812 	case PRIQ_DEL_CLASS:
813 		error = priqcmd_delete_class((struct priq_delete_class *)addr);
814 		break;
815 
816 	case PRIQ_MOD_CLASS:
817 		error = priqcmd_modify_class((struct priq_modify_class *)addr);
818 		break;
819 
820 	case PRIQ_ADD_FILTER:
821 		error = priqcmd_add_filter((struct priq_add_filter *)addr);
822 		break;
823 
824 	case PRIQ_DEL_FILTER:
825 		error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
826 		break;
827 
828 	case PRIQ_GETSTATS:
829 		error = priqcmd_class_stats((struct priq_class_stats *)addr);
830 		break;
831 
832 	default:
833 		error = EINVAL;
834 		break;
835 	}
836 	return error;
837 }
838 
839 static int
840 priqcmd_if_attach(ap)
841 	struct priq_interface *ap;
842 {
843 	struct priq_if *pif;
844 	struct ifnet *ifp;
845 	int error;
846 
847 	if ((ifp = ifunit(ap->ifname)) == NULL)
848 		return (ENXIO);
849 
850 	if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
851 		return (ENOMEM);
852 
853 	/*
854 	 * set PRIQ to this ifnet structure.
855 	 */
856 	if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
857 				 priq_enqueue, priq_dequeue, priq_request,
858 				 &pif->pif_classifier, acc_classify)) != 0)
859 		(void)priq_detach(pif);
860 
861 	return (error);
862 }
863 
864 static int
865 priqcmd_if_detach(ap)
866 	struct priq_interface *ap;
867 {
868 	struct priq_if *pif;
869 	int error;
870 
871 	if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
872 		return (EBADF);
873 
874 	if (ALTQ_IS_ENABLED(pif->pif_ifq))
875 		altq_disable(pif->pif_ifq);
876 
877 	if ((error = altq_detach(pif->pif_ifq)))
878 		return (error);
879 
880 	return priq_detach(pif);
881 }
882 
883 static int
884 priqcmd_add_class(ap)
885 	struct priq_add_class *ap;
886 {
887 	struct priq_if *pif;
888 	struct priq_class *cl;
889 	int qid;
890 
891 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
892 		return (EBADF);
893 
894 	if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
895 		return (EINVAL);
896 	if (pif->pif_classes[ap->pri] != NULL)
897 		return (EBUSY);
898 
899 	qid = ap->pri + 1;
900 	if ((cl = priq_class_create(pif, ap->pri,
901 	    ap->qlimit, ap->flags, qid)) == NULL)
902 		return (ENOMEM);
903 
904 	/* return a class handle to the user */
905 	ap->class_handle = cl->cl_handle;
906 
907 	return (0);
908 }
909 
910 static int
911 priqcmd_delete_class(ap)
912 	struct priq_delete_class *ap;
913 {
914 	struct priq_if *pif;
915 	struct priq_class *cl;
916 
917 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
918 		return (EBADF);
919 
920 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
921 		return (EINVAL);
922 
923 	return priq_class_destroy(cl);
924 }
925 
926 static int
927 priqcmd_modify_class(ap)
928 	struct priq_modify_class *ap;
929 {
930 	struct priq_if *pif;
931 	struct priq_class *cl;
932 
933 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
934 		return (EBADF);
935 
936 	if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
937 		return (EINVAL);
938 
939 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
940 		return (EINVAL);
941 
942 	/*
943 	 * if priority is changed, move the class to the new priority
944 	 */
945 	if (pif->pif_classes[ap->pri] != cl) {
946 		if (pif->pif_classes[ap->pri] != NULL)
947 			return (EEXIST);
948 		pif->pif_classes[cl->cl_pri] = NULL;
949 		pif->pif_classes[ap->pri] = cl;
950 		cl->cl_pri = ap->pri;
951 	}
952 
953 	/* call priq_class_create to change class parameters */
954 	if ((cl = priq_class_create(pif, ap->pri,
955 	    ap->qlimit, ap->flags, ap->class_handle)) == NULL)
956 		return (ENOMEM);
957 	return 0;
958 }
959 
960 static int
961 priqcmd_add_filter(ap)
962 	struct priq_add_filter *ap;
963 {
964 	struct priq_if *pif;
965 	struct priq_class *cl;
966 
967 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
968 		return (EBADF);
969 
970 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
971 		return (EINVAL);
972 
973 	return acc_add_filter(&pif->pif_classifier, &ap->filter,
974 			      cl, &ap->filter_handle);
975 }
976 
977 static int
978 priqcmd_delete_filter(ap)
979 	struct priq_delete_filter *ap;
980 {
981 	struct priq_if *pif;
982 
983 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
984 		return (EBADF);
985 
986 	return acc_delete_filter(&pif->pif_classifier,
987 				 ap->filter_handle);
988 }
989 
990 static int
991 priqcmd_class_stats(ap)
992 	struct priq_class_stats *ap;
993 {
994 	struct priq_if *pif;
995 	struct priq_class *cl;
996 	struct priq_classstats stats, *usp;
997 	int	pri, error;
998 
999 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1000 		return (EBADF);
1001 
1002 	ap->maxpri = pif->pif_maxpri;
1003 
1004 	/* then, read the next N classes in the tree */
1005 	usp = ap->stats;
1006 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1007 		cl = pif->pif_classes[pri];
1008 		if (cl != NULL)
1009 			get_class_stats(&stats, cl);
1010 		else
1011 			bzero(&stats, sizeof(stats));
1012 		if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1013 				     sizeof(stats))) != 0)
1014 			return (error);
1015 	}
1016 	return (0);
1017 }
1018 
1019 #ifdef KLD_MODULE
1020 
1021 static struct altqsw priq_sw =
1022 	{"priq", priqopen, priqclose, priqioctl};
1023 
1024 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1025 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1026 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1027 
1028 #endif /* KLD_MODULE */
1029 
1030 #endif /* ALTQ3_COMPAT */
1031 #endif /* ALTQ_PRIQ */
1032