xref: /netbsd-src/sys/altq/altq_cbq.c (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
1 /*	$NetBSD: altq_cbq.c,v 1.31 2017/07/28 13:53:17 riastradh Exp $	*/
2 /*	$KAME: altq_cbq.c,v 1.21 2005/04/13 03:44:24 suz Exp $	*/
3 
4 /*
5  * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the SMCC Technology
21  *      Development Group at Sun Microsystems, Inc.
22  *
23  * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
24  *      promote products derived from this software without specific prior
25  *      written permission.
26  *
27  * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
28  * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is
29  * provided "as is" without express or implied warranty of any kind.
30  *
31  * These notices must be retained in any copies of any part of this software.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: altq_cbq.c,v 1.31 2017/07/28 13:53:17 riastradh Exp $");
36 
37 #ifdef _KERNEL_OPT
38 #include "opt_altq.h"
39 #include "opt_inet.h"
40 #include "pf.h"
41 #endif
42 
43 #ifdef ALTQ_CBQ	/* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
44 
45 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.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 #ifdef ALTQ3_COMPAT
54 #include <sys/uio.h>
55 #include <sys/kernel.h>
56 #endif
57 #include <sys/kauth.h>
58 
59 #include <net/if.h>
60 #include <netinet/in.h>
61 
62 #if NPF > 0
63 #include <net/pfvar.h>
64 #endif
65 #include <altq/altq.h>
66 #include <altq/altq_cbq.h>
67 #ifdef ALTQ3_COMPAT
68 #include <altq/altq_conf.h>
69 #endif
70 
71 #ifdef ALTQ3_COMPAT
72 /*
73  * Local Data structures.
74  */
75 static cbq_state_t *cbq_list = NULL;
76 #endif
77 
78 /*
79  * Forward Declarations.
80  */
81 static int		 cbq_class_destroy(cbq_state_t *, struct rm_class *);
82 static struct rm_class  *clh_to_clp(cbq_state_t *, u_int32_t);
83 static int		 cbq_clear_interface(cbq_state_t *);
84 static int		 cbq_request(struct ifaltq *, int, void *);
85 static int		 cbq_enqueue(struct ifaltq *, struct mbuf *);
86 static struct mbuf	*cbq_dequeue(struct ifaltq *, int);
87 static void		 cbqrestart(struct ifaltq *);
88 static void		 get_class_stats(class_stats_t *, struct rm_class *);
89 static void		 cbq_purge(cbq_state_t *);
90 #ifdef ALTQ3_COMPAT
91 static int	cbq_add_class(struct cbq_add_class *);
92 static int	cbq_delete_class(struct cbq_delete_class *);
93 static int	cbq_modify_class(struct cbq_modify_class *);
94 static int 	cbq_class_create(cbq_state_t *, struct cbq_add_class *,
95 				 struct rm_class *, struct rm_class *);
96 static int	cbq_clear_hierarchy(struct cbq_interface *);
97 static int	cbq_set_enable(struct cbq_interface *, int);
98 static int	cbq_ifattach(struct cbq_interface *);
99 static int	cbq_ifdetach(struct cbq_interface *);
100 static int 	cbq_getstats(struct cbq_getstats *);
101 
102 static int	cbq_add_filter(struct cbq_add_filter *);
103 static int	cbq_delete_filter(struct cbq_delete_filter *);
104 #endif /* ALTQ3_COMPAT */
105 
106 /*
107  * int
108  * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
109  *	function destroys a given traffic class.  Before destroying
110  *	the class, all traffic for that class is released.
111  */
112 static int
113 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
114 {
115 	int	i;
116 
117 	/* delete the class */
118 	rmc_delete_class(&cbqp->ifnp, cl);
119 
120 	/*
121 	 * free the class handle
122 	 */
123 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
124 		if (cbqp->cbq_class_tbl[i] == cl)
125 			cbqp->cbq_class_tbl[i] = NULL;
126 
127 	if (cl == cbqp->ifnp.root_)
128 		cbqp->ifnp.root_ = NULL;
129 	if (cl == cbqp->ifnp.default_)
130 		cbqp->ifnp.default_ = NULL;
131 #ifdef ALTQ3_COMPAT
132 	if (cl == cbqp->ifnp.ctl_)
133 		cbqp->ifnp.ctl_ = NULL;
134 #endif
135 	return (0);
136 }
137 
138 /* convert class handle to class pointer */
139 static struct rm_class *
140 clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle)
141 {
142 	int i;
143 	struct rm_class *cl;
144 
145 	if (chandle == 0)
146 		return (NULL);
147 	/*
148 	 * first, try optimistically the slot matching the lower bits of
149 	 * the handle.  if it fails, do the linear table search.
150 	 */
151 	i = chandle % CBQ_MAX_CLASSES;
152 	if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
153 	    cl->stats_.handle == chandle)
154 		return (cl);
155 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
156 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
157 		    cl->stats_.handle == chandle)
158 			return (cl);
159 	return (NULL);
160 }
161 
162 static int
163 cbq_clear_interface(cbq_state_t *cbqp)
164 {
165 	int		 again, i;
166 	struct rm_class	*cl;
167 
168 #ifdef ALTQ3_CLFIER_COMPAT
169 	/* free the filters for this interface */
170 	acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
171 #endif
172 
173 	/* clear out the classes now */
174 	do {
175 		again = 0;
176 		for (i = 0; i < CBQ_MAX_CLASSES; i++) {
177 			if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
178 				if (is_a_parent_class(cl))
179 					again++;
180 				else {
181 					cbq_class_destroy(cbqp, cl);
182 					cbqp->cbq_class_tbl[i] = NULL;
183 					if (cl == cbqp->ifnp.root_)
184 						cbqp->ifnp.root_ = NULL;
185 					if (cl == cbqp->ifnp.default_)
186 						cbqp->ifnp.default_ = NULL;
187 #ifdef ALTQ3_COMPAT
188 					if (cl == cbqp->ifnp.ctl_)
189 						cbqp->ifnp.ctl_ = NULL;
190 #endif
191 				}
192 			}
193 		}
194 	} while (again);
195 
196 	return (0);
197 }
198 
199 static int
200 cbq_request(struct ifaltq *ifq, int req, void *arg)
201 {
202 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
203 
204 	switch (req) {
205 	case ALTRQ_PURGE:
206 		cbq_purge(cbqp);
207 		break;
208 	}
209 	return (0);
210 }
211 
212 /* copy the stats info in rm_class to class_states_t */
213 static void
214 get_class_stats(class_stats_t *statsp, struct rm_class *cl)
215 {
216 	statsp->xmit_cnt	= cl->stats_.xmit_cnt;
217 	statsp->drop_cnt	= cl->stats_.drop_cnt;
218 	statsp->over		= cl->stats_.over;
219 	statsp->borrows		= cl->stats_.borrows;
220 	statsp->overactions	= cl->stats_.overactions;
221 	statsp->delays		= cl->stats_.delays;
222 
223 	statsp->depth		= cl->depth_;
224 	statsp->priority	= cl->pri_;
225 	statsp->maxidle		= cl->maxidle_;
226 	statsp->minidle		= cl->minidle_;
227 	statsp->offtime		= cl->offtime_;
228 	statsp->qmax		= qlimit(cl->q_);
229 	statsp->ns_per_byte	= cl->ns_per_byte_;
230 	statsp->wrr_allot	= cl->w_allotment_;
231 	statsp->qcnt		= qlen(cl->q_);
232 	statsp->avgidle		= cl->avgidle_;
233 
234 	statsp->qtype		= qtype(cl->q_);
235 #ifdef ALTQ_RED
236 	if (q_is_red(cl->q_))
237 		red_getstats(cl->red_, &statsp->red[0]);
238 #endif
239 #ifdef ALTQ_RIO
240 	if (q_is_rio(cl->q_))
241 		rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
242 #endif
243 }
244 
245 #if NPF > 0
246 int
247 cbq_pfattach(struct pf_altq *a)
248 {
249 	struct ifnet	*ifp;
250 	int		 s, error;
251 
252 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
253 		return (EINVAL);
254 	s = splnet();
255 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
256 	    cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL);
257 	splx(s);
258 	return (error);
259 }
260 
261 int
262 cbq_add_altq(struct pf_altq *a)
263 {
264 	cbq_state_t	*cbqp;
265 	struct ifnet	*ifp;
266 
267 	if ((ifp = ifunit(a->ifname)) == NULL)
268 		return (EINVAL);
269 	if (!ALTQ_IS_READY(&ifp->if_snd))
270 		return (ENODEV);
271 
272 	/* allocate and initialize cbq_state_t */
273 	cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
274 	if (cbqp == NULL)
275 		return (ENOMEM);
276 	(void)memset(cbqp, 0, sizeof(cbq_state_t));
277 	CALLOUT_INIT(&cbqp->cbq_callout);
278 	cbqp->cbq_qlen = 0;
279 	cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
280 
281 	/* keep the state in pf_altq */
282 	a->altq_disc = cbqp;
283 
284 	return (0);
285 }
286 
287 int
288 cbq_remove_altq(struct pf_altq *a)
289 {
290 	cbq_state_t	*cbqp;
291 
292 	if ((cbqp = a->altq_disc) == NULL)
293 		return (EINVAL);
294 	a->altq_disc = NULL;
295 
296 	cbq_clear_interface(cbqp);
297 
298 	if (cbqp->ifnp.default_)
299 		cbq_class_destroy(cbqp, cbqp->ifnp.default_);
300 	if (cbqp->ifnp.root_)
301 		cbq_class_destroy(cbqp, cbqp->ifnp.root_);
302 
303 	/* deallocate cbq_state_t */
304 	free(cbqp, M_DEVBUF);
305 
306 	return (0);
307 }
308 
309 int
310 cbq_add_queue(struct pf_altq *a)
311 {
312 	struct rm_class	*borrow, *parent;
313 	cbq_state_t	*cbqp;
314 	struct rm_class	*cl;
315 	struct cbq_opts	*opts;
316 	int		i, error;
317 
318 	if ((cbqp = a->altq_disc) == NULL)
319 		return (EINVAL);
320 	if (a->qid == 0)
321 		return (EINVAL);
322 
323 	/*
324 	 * find a free slot in the class table.  if the slot matching
325 	 * the lower bits of qid is free, use this slot.  otherwise,
326 	 * use the first free slot.
327 	 */
328 	i = a->qid % CBQ_MAX_CLASSES;
329 	if (cbqp->cbq_class_tbl[i] != NULL) {
330 		for (i = 0; i < CBQ_MAX_CLASSES; i++)
331 			if (cbqp->cbq_class_tbl[i] == NULL)
332 				break;
333 		if (i == CBQ_MAX_CLASSES)
334 			return (EINVAL);
335 	}
336 
337 	opts = &a->pq_u.cbq_opts;
338 	/* check parameters */
339 	if (a->priority >= CBQ_MAXPRI)
340 		return (EINVAL);
341 
342 	/* Get pointers to parent and borrow classes.  */
343 	parent = clh_to_clp(cbqp, a->parent_qid);
344 	if (opts->flags & CBQCLF_BORROW)
345 		borrow = parent;
346 	else
347 		borrow = NULL;
348 
349 	/*
350 	 * A class must borrow from its parent or it can not
351 	 * borrow at all.  Hence, borrow can be null.
352 	 */
353 	if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
354 		printf("cbq_add_queue: no parent class!\n");
355 		return (EINVAL);
356 	}
357 
358 	if ((borrow != parent)  && (borrow != NULL)) {
359 		printf("cbq_add_class: borrow class != parent\n");
360 		return (EINVAL);
361 	}
362 
363 	/*
364 	 * check parameters
365 	 */
366 	switch (opts->flags & CBQCLF_CLASSMASK) {
367 	case CBQCLF_ROOTCLASS:
368 		if (parent != NULL)
369 			return (EINVAL);
370 		if (cbqp->ifnp.root_)
371 			return (EINVAL);
372 		break;
373 	case CBQCLF_DEFCLASS:
374 		if (cbqp->ifnp.default_)
375 			return (EINVAL);
376 		break;
377 	case 0:
378 		if (a->qid == 0)
379 			return (EINVAL);
380 		break;
381 	default:
382 		/* more than two flags bits set */
383 		return (EINVAL);
384 	}
385 
386 	/*
387 	 * create a class.  if this is a root class, initialize the
388 	 * interface.
389 	 */
390 	if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
391 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
392 		    opts->ns_per_byte, cbqrestart, a->qlimit, RM_MAXQUEUED,
393 		    opts->maxidle, opts->minidle, opts->offtime,
394 		    opts->flags);
395 		if (error != 0)
396 			return (error);
397 		cl = cbqp->ifnp.root_;
398 	} else {
399 		cl = rmc_newclass(a->priority,
400 				  &cbqp->ifnp, opts->ns_per_byte,
401 				  rmc_delay_action, a->qlimit, parent, borrow,
402 				  opts->maxidle, opts->minidle, opts->offtime,
403 				  opts->pktsize, opts->flags);
404 	}
405 	if (cl == NULL)
406 		return (ENOMEM);
407 
408 	/* return handle to user space. */
409 	cl->stats_.handle = a->qid;
410 	cl->stats_.depth = cl->depth_;
411 
412 	/* save the allocated class */
413 	cbqp->cbq_class_tbl[i] = cl;
414 
415 	if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
416 		cbqp->ifnp.default_ = cl;
417 
418 	return (0);
419 }
420 
421 int
422 cbq_remove_queue(struct pf_altq *a)
423 {
424 	struct rm_class	*cl;
425 	cbq_state_t	*cbqp;
426 	int		i;
427 
428 	if ((cbqp = a->altq_disc) == NULL)
429 		return (EINVAL);
430 
431 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
432 		return (EINVAL);
433 
434 	/* if we are a parent class, then return an error. */
435 	if (is_a_parent_class(cl))
436 		return (EINVAL);
437 
438 	/* delete the class */
439 	rmc_delete_class(&cbqp->ifnp, cl);
440 
441 	/*
442 	 * free the class handle
443 	 */
444 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
445 		if (cbqp->cbq_class_tbl[i] == cl) {
446 			cbqp->cbq_class_tbl[i] = NULL;
447 			if (cl == cbqp->ifnp.root_)
448 				cbqp->ifnp.root_ = NULL;
449 			if (cl == cbqp->ifnp.default_)
450 				cbqp->ifnp.default_ = NULL;
451 			break;
452 		}
453 
454 	return (0);
455 }
456 
457 int
458 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
459 {
460 	cbq_state_t	*cbqp;
461 	struct rm_class	*cl;
462 	class_stats_t	 stats;
463 	int		 error = 0;
464 
465 	if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
466 		return (EBADF);
467 
468 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
469 		return (EINVAL);
470 
471 	if (*nbytes < sizeof(stats))
472 		return (EINVAL);
473 
474 	memset(&stats, 0, sizeof(stats));
475 	get_class_stats(&stats, cl);
476 
477 	if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
478 		return (error);
479 	*nbytes = sizeof(stats);
480 	return (0);
481 }
482 #endif /* NPF > 0 */
483 
484 /*
485  * int
486  * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
487  *		- Queue data packets.
488  *
489  *	cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
490  *	layer (e.g. ether_output).  cbq_enqueue queues the given packet
491  *	to the cbq, then invokes the driver's start routine.
492  *
493  *	Assumptions:	called in splnet
494  *	Returns:	0 if the queueing is successful.
495  *			ENOBUFS if a packet dropping occurred as a result of
496  *			the queueing.
497  */
498 
499 static int
500 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
501 {
502 	struct altq_pktattr pktattr;
503 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
504 	struct rm_class	*cl;
505 	struct m_tag	*t;
506 	int		 len;
507 
508 	/* grab class set by classifier */
509 	if ((m->m_flags & M_PKTHDR) == 0) {
510 		/* should not happen */
511 		printf("altq: packet for %s does not have pkthdr\n",
512 		    ifq->altq_ifp->if_xname);
513 		m_freem(m);
514 		return (ENOBUFS);
515 	}
516 	cl = NULL;
517 	if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID, NULL)) != NULL)
518 		cl = clh_to_clp(cbqp, ((struct altq_tag *)(t+1))->qid);
519 #ifdef ALTQ3_COMPAT
520 	else if (ifq->altq_flags & ALTQF_CLASSIFY)
521 		cl = m->m_pkthdr.pattr_class;
522 #endif
523 	if (cl == NULL) {
524 		cl = cbqp->ifnp.default_;
525 		if (cl == NULL) {
526 			m_freem(m);
527 			return (ENOBUFS);
528 		}
529 	}
530 #ifdef ALTQ3_COMPAT
531 	if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
532 		pktattr.pattr_class = m->m_pkthdr.pattr_class;
533 		pktattr.pattr_af = m->m_pkthdr.pattr_af;
534 		pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
535 
536 		cl->pktattr_ = &pktattr;  /* save proto hdr used by ECN */
537 	} else
538 #endif
539 		cl->pktattr_ = NULL;
540 	len = m_pktlen(m);
541 	if (rmc_queue_packet(cl, m) != 0) {
542 		/* drop occurred.  some mbuf was freed in rmc_queue_packet. */
543 		PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
544 		return (ENOBUFS);
545 	}
546 
547 	/* successfully queued. */
548 	++cbqp->cbq_qlen;
549 	IFQ_INC_LEN(ifq);
550 	return (0);
551 }
552 
553 static struct mbuf *
554 cbq_dequeue(struct ifaltq *ifq, int op)
555 {
556 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
557 	struct mbuf	*m;
558 
559 	m = rmc_dequeue_next(&cbqp->ifnp, op);
560 
561 	if (m && op == ALTDQ_REMOVE) {
562 		--cbqp->cbq_qlen;  /* decrement # of packets in cbq */
563 		IFQ_DEC_LEN(ifq);
564 
565 		/* Update the class. */
566 		rmc_update_class_util(&cbqp->ifnp);
567 	}
568 	return (m);
569 }
570 
571 /*
572  * void
573  * cbqrestart(queue_t *) - Restart sending of data.
574  * called from rmc_restart in splnet via timeout after waking up
575  * a suspended class.
576  *	Returns:	NONE
577  */
578 
579 static void
580 cbqrestart(struct ifaltq *ifq)
581 {
582 	cbq_state_t	*cbqp;
583 	struct ifnet	*ifp;
584 
585 	if (!ALTQ_IS_ENABLED(ifq))
586 		/* cbq must have been detached */
587 		return;
588 
589 	if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
590 		/* should not happen */
591 		return;
592 
593 	ifp = ifq->altq_ifp;
594 	if (ifp->if_start &&
595 	    cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
596 		if_start_lock(ifp);
597 }
598 
599 static void
600 cbq_purge(cbq_state_t *cbqp)
601 {
602 	struct rm_class	*cl;
603 	int		 i;
604 
605 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
606 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
607 			rmc_dropall(cl);
608 	if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
609 		cbqp->ifnp.ifq_->ifq_len = 0;
610 }
611 #ifdef ALTQ3_COMPAT
612 
613 static int
614 cbq_add_class(struct cbq_add_class *acp)
615 {
616 	char		*ifacename;
617 	struct rm_class	*borrow, *parent;
618 	cbq_state_t	*cbqp;
619 
620 	ifacename = acp->cbq_iface.cbq_ifacename;
621 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
622 		return (EBADF);
623 
624 	/* check parameters */
625 	if (acp->cbq_class.priority >= CBQ_MAXPRI ||
626 	    acp->cbq_class.maxq > CBQ_MAXQSIZE)
627 		return (EINVAL);
628 
629 	/* Get pointers to parent and borrow classes.  */
630 	parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
631 	borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
632 
633 	/*
634 	 * A class must borrow from its parent or it can not
635 	 * borrow at all.  Hence, borrow can be null.
636 	 */
637 	if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
638 		printf("cbq_add_class: no parent class!\n");
639 		return (EINVAL);
640 	}
641 
642 	if ((borrow != parent)  && (borrow != NULL)) {
643 		printf("cbq_add_class: borrow class != parent\n");
644 		return (EINVAL);
645 	}
646 
647 	return cbq_class_create(cbqp, acp, parent, borrow);
648 }
649 
650 static int
651 cbq_delete_class(struct cbq_delete_class *dcp)
652 {
653 	char		*ifacename;
654 	struct rm_class	*cl;
655 	cbq_state_t	*cbqp;
656 
657 	ifacename = dcp->cbq_iface.cbq_ifacename;
658 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
659 		return (EBADF);
660 
661 	if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL)
662 		return (EINVAL);
663 
664 	/* if we are a parent class, then return an error. */
665 	if (is_a_parent_class(cl))
666 		return (EINVAL);
667 
668 	/* if a filter has a reference to this class delete the filter */
669 	acc_discard_filters(&cbqp->cbq_classifier, cl, 0);
670 
671 	return cbq_class_destroy(cbqp, cl);
672 }
673 
674 static int
675 cbq_modify_class(struct cbq_modify_class *acp)
676 {
677 	char		*ifacename;
678 	struct rm_class	*cl;
679 	cbq_state_t	*cbqp;
680 
681 	ifacename = acp->cbq_iface.cbq_ifacename;
682 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
683 		return (EBADF);
684 
685 	/* Get pointer to this class */
686 	if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
687 		return (EINVAL);
688 
689 	if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte,
690 			 acp->cbq_class.maxq, acp->cbq_class.maxidle,
691 			 acp->cbq_class.minidle, acp->cbq_class.offtime,
692 			 acp->cbq_class.pktsize) < 0)
693 		return (EINVAL);
694 	return (0);
695 }
696 
697 /*
698  * struct rm_class *
699  * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
700  *		struct rm_class *parent, struct rm_class *borrow)
701  *
702  * This function create a new traffic class in the CBQ class hierarchy of
703  * given paramters.  The class that created is either the root, default,
704  * or a new dynamic class.  If CBQ is not initilaized, the root class
705  * will be created.
706  */
707 static int
708 cbq_class_create(cbq_state_t *cbqp, struct cbq_add_class *acp,
709     struct rm_class *parent, struct rm_class *borrow)
710 {
711 	struct rm_class	*cl;
712 	cbq_class_spec_t *spec = &acp->cbq_class;
713 	u_int32_t	chandle;
714 	int		i, error;
715 
716 	/*
717 	 * allocate class handle
718 	 */
719 	for (i = 1; i < CBQ_MAX_CLASSES; i++)
720 		if (cbqp->cbq_class_tbl[i] == NULL)
721 			break;
722 	if (i == CBQ_MAX_CLASSES)
723 		return (EINVAL);
724 	chandle = i;	/* use the slot number as class handle */
725 
726 	/*
727 	 * create a class.  if this is a root class, initialize the
728 	 * interface.
729 	 */
730 	if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
731 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
732 		    spec->nano_sec_per_byte, cbqrestart, spec->maxq,
733 		    RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
734 		    spec->flags);
735 		if (error)
736 			return (error);
737 		cl = cbqp->ifnp.root_;
738 	} else {
739 		cl = rmc_newclass(spec->priority,
740 				  &cbqp->ifnp, spec->nano_sec_per_byte,
741 				  rmc_delay_action, spec->maxq, parent, borrow,
742 				  spec->maxidle, spec->minidle, spec->offtime,
743 				  spec->pktsize, spec->flags);
744 	}
745 	if (cl == NULL)
746 		return (ENOMEM);
747 
748 	/* return handle to user space. */
749 	acp->cbq_class_handle = chandle;
750 
751 	cl->stats_.handle = chandle;
752 	cl->stats_.depth = cl->depth_;
753 
754 	/* save the allocated class */
755 	cbqp->cbq_class_tbl[i] = cl;
756 
757 	if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
758 		cbqp->ifnp.default_ = cl;
759 	if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_CTLCLASS)
760 		cbqp->ifnp.ctl_ = cl;
761 
762 	return (0);
763 }
764 
765 static int
766 cbq_add_filter(struct cbq_add_filter *afp)
767 {
768 	char		*ifacename;
769 	cbq_state_t	*cbqp;
770 	struct rm_class	*cl;
771 
772 	ifacename = afp->cbq_iface.cbq_ifacename;
773 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
774 		return (EBADF);
775 
776 	/* Get the pointer to class. */
777 	if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
778 		return (EINVAL);
779 
780 	return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
781 			      cl, &afp->cbq_filter_handle);
782 }
783 
784 static int
785 cbq_delete_filter(struct cbq_delete_filter *dfp)
786 {
787 	char		*ifacename;
788 	cbq_state_t	*cbqp;
789 
790 	ifacename = dfp->cbq_iface.cbq_ifacename;
791 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
792 		return (EBADF);
793 
794 	return acc_delete_filter(&cbqp->cbq_classifier,
795 				 dfp->cbq_filter_handle);
796 }
797 
798 /*
799  * cbq_clear_hierarchy deletes all classes and their filters on the
800  * given interface.
801  */
802 static int
803 cbq_clear_hierarchy(struct cbq_interface *ifacep)
804 {
805 	char		*ifacename;
806 	cbq_state_t	*cbqp;
807 
808 	ifacename = ifacep->cbq_ifacename;
809 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
810 		return (EBADF);
811 
812 	return cbq_clear_interface(cbqp);
813 }
814 
815 /*
816  * static int
817  * cbq_set_enable(struct cbq_enable *ep) - this function processed the
818  *	ioctl request to enable class based queueing.  It searches the list
819  *	of interfaces for the specified interface and then enables CBQ on
820  *	that interface.
821  *
822  *	Returns:	0, for no error.
823  *			EBADF, for specified inteface not found.
824  */
825 
826 static int
827 cbq_set_enable(struct cbq_interface *ep, int enable)
828 {
829 	int 	error = 0;
830 	cbq_state_t	*cbqp;
831 	char 	*ifacename;
832 
833 	ifacename = ep->cbq_ifacename;
834 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
835 		return (EBADF);
836 
837 	switch (enable) {
838 	case ENABLE:
839 		if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL ||
840 		    cbqp->ifnp.ctl_ == NULL) {
841 			if (cbqp->ifnp.root_ == NULL)
842 				printf("No Root Class for %s\n", ifacename);
843 			if (cbqp->ifnp.default_ == NULL)
844 				printf("No Default Class for %s\n", ifacename);
845 			if (cbqp->ifnp.ctl_ == NULL)
846 				printf("No Control Class for %s\n", ifacename);
847 			error = EINVAL;
848 		} else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
849 			cbqp->cbq_qlen = 0;
850 		}
851 		break;
852 
853 	case DISABLE:
854 		error = altq_disable(cbqp->ifnp.ifq_);
855 		break;
856 	}
857 	return (error);
858 }
859 
860 static int
861 cbq_getstats(struct cbq_getstats *gsp)
862 {
863 	char		*ifacename;
864 	int		i, n, nclasses;
865 	cbq_state_t	*cbqp;
866 	struct rm_class	*cl;
867 	class_stats_t	stats, *usp;
868 	int error = 0;
869 
870 	ifacename = gsp->iface.cbq_ifacename;
871 	nclasses = gsp->nclasses;
872 	usp = gsp->stats;
873 
874 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
875 		return (EBADF);
876 	if (nclasses <= 0)
877 		return (EINVAL);
878 
879 	for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) {
880 		while ((cl = cbqp->cbq_class_tbl[i]) == NULL)
881 			if (++i >= CBQ_MAX_CLASSES)
882 				goto out;
883 
884 		memset(&stats, 0, sizeof(stats));
885 		get_class_stats(&stats, cl);
886 		stats.handle = cl->stats_.handle;
887 
888 		if ((error = copyout((void *)&stats, (void *)usp++,
889 		    sizeof(stats))) != 0)
890 			return (error);
891 	}
892 
893  out:
894 	gsp->nclasses = n;
895 	return (error);
896 }
897 
898 static int
899 cbq_ifattach(struct cbq_interface *ifacep)
900 {
901 	int		error = 0;
902 	char		*ifacename;
903 	cbq_state_t	*new_cbqp;
904 	struct ifnet 	*ifp;
905 
906 	ifacename = ifacep->cbq_ifacename;
907 	if ((ifp = ifunit(ifacename)) == NULL)
908 		return (ENXIO);
909 	if (!ALTQ_IS_READY(&ifp->if_snd))
910 		return (ENXIO);
911 
912 	/* allocate and initialize cbq_state_t */
913 	new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
914 	if (new_cbqp == NULL)
915 		return (ENOMEM);
916  	CALLOUT_INIT(&new_cbqp->cbq_callout);
917 
918 	new_cbqp->cbq_qlen = 0;
919 	new_cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
920 
921 	/*
922 	 * set CBQ to this ifnet structure.
923 	 */
924 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
925 			    cbq_enqueue, cbq_dequeue, cbq_request,
926 			    &new_cbqp->cbq_classifier, acc_classify);
927 	if (error) {
928 		free(new_cbqp, M_DEVBUF);
929 		return (error);
930 	}
931 
932 	/* prepend to the list of cbq_state_t's. */
933 	new_cbqp->cbq_next = cbq_list;
934 	cbq_list = new_cbqp;
935 
936 	return (0);
937 }
938 
939 static int
940 cbq_ifdetach(struct cbq_interface *ifacep)
941 {
942 	char		*ifacename;
943 	cbq_state_t 	*cbqp;
944 
945 	ifacename = ifacep->cbq_ifacename;
946 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
947 		return (EBADF);
948 
949 	(void)cbq_set_enable(ifacep, DISABLE);
950 
951 	cbq_clear_interface(cbqp);
952 
953 	/* remove CBQ from the ifnet structure. */
954 	(void)altq_detach(cbqp->ifnp.ifq_);
955 
956 	/* remove from the list of cbq_state_t's. */
957 	if (cbq_list == cbqp)
958 		cbq_list = cbqp->cbq_next;
959 	else {
960 		cbq_state_t *cp;
961 
962 		for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
963 			if (cp->cbq_next == cbqp) {
964 				cp->cbq_next = cbqp->cbq_next;
965 				break;
966 			}
967 		ASSERT(cp != NULL);
968 	}
969 
970 	/* deallocate cbq_state_t */
971 	free(cbqp, M_DEVBUF);
972 
973 	return (0);
974 }
975 
976 /*
977  * cbq device interface
978  */
979 
980 altqdev_decl(cbq);
981 
982 int
983 cbqopen(dev_t dev, int flag, int fmt,
984     struct lwp *l)
985 {
986 	return (0);
987 }
988 
989 int
990 cbqclose(dev_t dev, int flag, int fmt,
991     struct lwp *l)
992 {
993 	struct ifnet *ifp;
994 	struct cbq_interface iface;
995 	int err, error = 0;
996 
997 	while (cbq_list) {
998 		ifp = cbq_list->ifnp.ifq_->altq_ifp;
999 		snprintf(iface.cbq_ifacename, sizeof(iface.cbq_ifacename),
1000 		    "%s", ifp->if_xname);
1001 		err = cbq_ifdetach(&iface);
1002 		if (err != 0 && error == 0)
1003 			error = err;
1004 	}
1005 
1006 	return (error);
1007 }
1008 
1009 int
1010 cbqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
1011     struct lwp *l)
1012 {
1013 	int	error = 0;
1014 
1015 	/* check cmd for superuser only */
1016 	switch (cmd) {
1017 	case CBQ_GETSTATS:
1018 		/* currently only command that an ordinary user can call */
1019 		break;
1020 	default:
1021 #if (__FreeBSD_version > 400000)
1022 		error = suser(p);
1023 #else
1024 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_ALTQ,
1025 		    KAUTH_REQ_NETWORK_ALTQ_CBQ, NULL, NULL, NULL);
1026 #endif
1027 		if (error)
1028 			return (error);
1029 		break;
1030 	}
1031 
1032 	switch (cmd) {
1033 
1034 	case CBQ_ENABLE:
1035 		error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
1036 		break;
1037 
1038 	case CBQ_DISABLE:
1039 		error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
1040 		break;
1041 
1042 	case CBQ_ADD_FILTER:
1043 		error = cbq_add_filter((struct cbq_add_filter *)addr);
1044 		break;
1045 
1046 	case CBQ_DEL_FILTER:
1047 		error = cbq_delete_filter((struct cbq_delete_filter *)addr);
1048 		break;
1049 
1050 	case CBQ_ADD_CLASS:
1051 		error = cbq_add_class((struct cbq_add_class *)addr);
1052 		break;
1053 
1054 	case CBQ_DEL_CLASS:
1055 		error = cbq_delete_class((struct cbq_delete_class *)addr);
1056 		break;
1057 
1058 	case CBQ_MODIFY_CLASS:
1059 		error = cbq_modify_class((struct cbq_modify_class *)addr);
1060 		break;
1061 
1062 	case CBQ_CLEAR_HIERARCHY:
1063 		error = cbq_clear_hierarchy((struct cbq_interface *)addr);
1064 		break;
1065 
1066 	case CBQ_IF_ATTACH:
1067 		error = cbq_ifattach((struct cbq_interface *)addr);
1068 		break;
1069 
1070 	case CBQ_IF_DETACH:
1071 		error = cbq_ifdetach((struct cbq_interface *)addr);
1072 		break;
1073 
1074 	case CBQ_GETSTATS:
1075 		error = cbq_getstats((struct cbq_getstats *)addr);
1076 		break;
1077 
1078 	default:
1079 		error = EINVAL;
1080 		break;
1081 	}
1082 
1083 	return error;
1084 }
1085 
1086 #if 0
1087 /* for debug */
1088 static void cbq_class_dump(int);
1089 
1090 static void
1091 cbq_class_dump(int i)
1092 {
1093 	struct rm_class *cl;
1094 	rm_class_stats_t *s;
1095 	struct _class_queue_ *q;
1096 
1097 	if (cbq_list == NULL) {
1098 		printf("cbq_class_dump: no cbq_state found\n");
1099 		return;
1100 	}
1101 	cl = cbq_list->cbq_class_tbl[i];
1102 
1103 	printf("class %d cl=%p\n", i, cl);
1104 	if (cl != NULL) {
1105 		s = &cl->stats_;
1106 		q = cl->q_;
1107 
1108 		printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n",
1109 		       cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_);
1110 		printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n",
1111 		       cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_,
1112 		       cl->maxidle_);
1113 		printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n",
1114 		       cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_);
1115 		printf("handle=%d, depth=%d, packets=%d, bytes=%d\n",
1116 		       s->handle, s->depth,
1117 		       (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes);
1118 		printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n",
1119 		       s->over, s->borrows, (int)s->drop_cnt.packets,
1120 		       s->overactions, s->delays);
1121 		printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n",
1122 		       q->tail_, q->head_, q->qlen_, q->qlim_,
1123 		       q->qthresh_, q->qtype_);
1124 	}
1125 }
1126 #endif /* 0 */
1127 
1128 #ifdef KLD_MODULE
1129 
1130 static struct altqsw cbq_sw =
1131 	{"cbq", cbqopen, cbqclose, cbqioctl};
1132 
1133 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw);
1134 MODULE_DEPEND(altq_cbq, altq_red, 1, 1, 1);
1135 MODULE_DEPEND(altq_cbq, altq_rio, 1, 1, 1);
1136 
1137 #endif /* KLD_MODULE */
1138 #endif /* ALTQ3_COMPAT */
1139 
1140 #endif /* ALTQ_CBQ */
1141