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