altq_cbq.c revision 1.18 1 /* $NetBSD: altq_cbq.c,v 1.18 2006/10/12 01:30:42 christos Exp $ */
2 /* $KAME: altq_cbq.c,v 1.11 2002/10/04 14:24:09 kjc 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.18 2006/10/12 01:30:42 christos Exp $");
36
37 #if defined(__FreeBSD__) || defined(__NetBSD__)
38 #include "opt_altq.h"
39 #if (__FreeBSD__ != 2)
40 #include "opt_inet.h"
41 #ifdef __FreeBSD__
42 #include "opt_inet6.h"
43 #endif
44 #endif
45 #endif /* __FreeBSD__ || __NetBSD__ */
46 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
47
48 /* #pragma ident "@(#)cbq.c 1.39 98/05/13 SMI" */
49
50 #include <sys/param.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/uio.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/proc.h>
57 #include <sys/errno.h>
58 #include <sys/time.h>
59 #include <sys/kernel.h>
60 #include <sys/kauth.h>
61
62 #include <net/if.h>
63 #include <net/if_types.h>
64 #include <netinet/in.h>
65
66 #include <altq/altq.h>
67 #include <altq/altq_conf.h>
68 #include <altq/altq_cbq.h>
69
70 /*
71 * Local Data structures.
72 */
73 static cbq_state_t *cbq_list = NULL;
74
75 /*
76 * Forward Declarations.
77 */
78
79 static int cbq_add_class __P((struct cbq_add_class *));
80 static int cbq_delete_class __P((struct cbq_delete_class *));
81 static int cbq_modify_class __P((struct cbq_modify_class *));
82 static int cbq_class_create __P((cbq_state_t *, struct cbq_add_class *,
83 struct rm_class *, struct rm_class *));
84 static int cbq_class_destroy __P((cbq_state_t *, struct rm_class *));
85 static struct rm_class *clh_to_clp __P((cbq_state_t *, u_long));
86 static int cbq_add_filter __P((struct cbq_add_filter *));
87 static int cbq_delete_filter __P((struct cbq_delete_filter *));
88
89 static int cbq_clear_hierarchy __P((struct cbq_interface *));
90 static int cbq_clear_interface __P((cbq_state_t *));
91 static int cbq_request __P((struct ifaltq *, int, void *));
92 static int cbq_set_enable __P((struct cbq_interface *, int));
93 static int cbq_ifattach __P((struct cbq_interface *));
94 static int cbq_ifdetach __P((struct cbq_interface *));
95 static int cbq_enqueue __P((struct ifaltq *, struct mbuf *,
96 struct altq_pktattr *));
97 static struct mbuf *cbq_dequeue __P((struct ifaltq *, int));
98 static void cbqrestart __P((struct ifaltq *));
99 static void get_class_stats __P((class_stats_t *, struct rm_class *));
100 static int cbq_getstats __P((struct cbq_getstats *));
101 static void cbq_purge(cbq_state_t *);
102
103 static int
104 cbq_add_class(acp)
105 struct cbq_add_class *acp;
106 {
107 char *ifacename;
108 struct rm_class *borrow, *parent;
109 cbq_state_t *cbqp;
110
111 ifacename = acp->cbq_iface.cbq_ifacename;
112 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
113 return (EBADF);
114
115 /* check parameters */
116 if (acp->cbq_class.priority >= CBQ_MAXPRI ||
117 acp->cbq_class.maxq > CBQ_MAXQSIZE)
118 return (EINVAL);
119
120 /* Get pointers to parent and borrow classes. */
121 parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
122 borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
123
124 /*
125 * A class must borrow from it's parent or it can not
126 * borrow at all. Hence, borrow can be null.
127 */
128 if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
129 printf("cbq_add_class: no parent class!\n");
130 return (EINVAL);
131 }
132
133 if ((borrow != parent) && (borrow != NULL)) {
134 printf("cbq_add_class: borrow class != parent\n");
135 return (EINVAL);
136 }
137
138 return cbq_class_create(cbqp, acp, parent, borrow);
139 }
140
141 static int
142 cbq_delete_class(dcp)
143 struct cbq_delete_class *dcp;
144 {
145 char *ifacename;
146 struct rm_class *cl;
147 cbq_state_t *cbqp;
148
149 ifacename = dcp->cbq_iface.cbq_ifacename;
150 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
151 return (EBADF);
152
153 if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL)
154 return (EINVAL);
155
156 /* if we are a parent class, then return an error. */
157 if (is_a_parent_class(cl))
158 return (EINVAL);
159
160 /* if a filter has a reference to this class delete the filter */
161 acc_discard_filters(&cbqp->cbq_classifier, cl, 0);
162
163 return cbq_class_destroy(cbqp, cl);
164 }
165
166 static int
167 cbq_modify_class(acp)
168 struct cbq_modify_class *acp;
169 {
170 char *ifacename;
171 struct rm_class *cl;
172 cbq_state_t *cbqp;
173
174 ifacename = acp->cbq_iface.cbq_ifacename;
175 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
176 return (EBADF);
177
178 /* Get pointer to this class */
179 if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
180 return (EINVAL);
181
182 if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte,
183 acp->cbq_class.maxq, acp->cbq_class.maxidle,
184 acp->cbq_class.minidle, acp->cbq_class.offtime,
185 acp->cbq_class.pktsize) < 0)
186 return (EINVAL);
187 return (0);
188 }
189
190 /*
191 * struct rm_class *
192 * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
193 * u_long handle, struct rm_class *parent,
194 * struct rm_class *borrow)
195 *
196 * This function create a new traffic class in the CBQ class hierarchy of
197 * given parameters. The class that created is either the root, default,
198 * or a new dynamic class. If CBQ is not initilaized, the root class will
199 * be created.
200 */
201 static int
202 cbq_class_create(cbqp, acp, parent, borrow)
203 cbq_state_t *cbqp;
204 struct cbq_add_class *acp;
205 struct rm_class *parent, *borrow;
206 {
207 struct rm_class *cl;
208 cbq_class_spec_t *spec = &acp->cbq_class;
209 u_long chandle;
210 int i;
211
212 /*
213 * allocate class handle
214 */
215 switch (spec->flags & CBQCLF_CLASSMASK) {
216 case CBQCLF_ROOTCLASS:
217 if (parent != NULL)
218 return (EINVAL);
219 if (cbqp->ifnp.root_)
220 return (EINVAL);
221 chandle = ROOT_CLASS_HANDLE;
222 break;
223 case CBQCLF_DEFCLASS:
224 if (cbqp->ifnp.default_)
225 return (EINVAL);
226 chandle = DEFAULT_CLASS_HANDLE;
227 break;
228 case CBQCLF_CTLCLASS:
229 if (cbqp->ifnp.ctl_)
230 return (EINVAL);
231 chandle = CTL_CLASS_HANDLE;
232 break;
233 case 0:
234 /* find a free class slot */
235 for (i = 0; i < CBQ_MAX_CLASSES; i++)
236 if (cbqp->cbq_class_tbl[i] == NULL)
237 break;
238 if (i == CBQ_MAX_CLASSES)
239 return (ENOSPC);
240 chandle = (u_long)i;
241 break;
242 default:
243 /* more than two flags bits set */
244 return (EINVAL);
245 }
246
247 /*
248 * create a class. if this is a root class, initialize the
249 * interface.
250 */
251 if (chandle == ROOT_CLASS_HANDLE) {
252 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, spec->nano_sec_per_byte,
253 cbqrestart, spec->maxq, RM_MAXQUEUED,
254 spec->maxidle, spec->minidle, spec->offtime,
255 spec->flags);
256 cl = cbqp->ifnp.root_;
257 } else {
258 cl = rmc_newclass(spec->priority,
259 &cbqp->ifnp, spec->nano_sec_per_byte,
260 rmc_delay_action, spec->maxq, parent, borrow,
261 spec->maxidle, spec->minidle, spec->offtime,
262 spec->pktsize, spec->flags);
263 }
264 if (cl == NULL)
265 return (ENOMEM);
266
267 /* return handle to user space. */
268 acp->cbq_class_handle = chandle;
269
270 cl->stats_.handle = chandle;
271 cl->stats_.depth = cl->depth_;
272
273 /* save the allocated class */
274 switch (chandle) {
275 case NULL_CLASS_HANDLE:
276 case ROOT_CLASS_HANDLE:
277 break;
278 case DEFAULT_CLASS_HANDLE:
279 cbqp->ifnp.default_ = cl;
280 break;
281 case CTL_CLASS_HANDLE:
282 cbqp->ifnp.ctl_ = cl;
283 break;
284 default:
285 cbqp->cbq_class_tbl[chandle] = cl;
286 break;
287 }
288 return (0);
289 }
290
291 /*
292 * int
293 * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
294 * function destroys a given traffic class. Before destorying
295 * the class, all traffic for that class is released.
296 */
297 static int
298 cbq_class_destroy(cbqp, cl)
299 cbq_state_t *cbqp;
300 struct rm_class *cl;
301 {
302 u_long chandle;
303
304 chandle = cl->stats_.handle;
305
306 /* delete the class */
307 rmc_delete_class(&cbqp->ifnp, cl);
308
309 /*
310 * free the class handle
311 */
312 switch (chandle) {
313 case ROOT_CLASS_HANDLE:
314 cbqp->ifnp.root_ = NULL;
315 break;
316 case DEFAULT_CLASS_HANDLE:
317 cbqp->ifnp.default_ = NULL;
318 break;
319 case CTL_CLASS_HANDLE:
320 cbqp->ifnp.ctl_ = NULL;
321 break;
322 case NULL_CLASS_HANDLE:
323 break;
324 default:
325 if (chandle >= CBQ_MAX_CLASSES)
326 break;
327 cbqp->cbq_class_tbl[chandle] = NULL;
328 }
329
330 return (0);
331 }
332
333 /* convert class handle to class pointer */
334 static struct rm_class *
335 clh_to_clp(cbqp, chandle)
336 cbq_state_t *cbqp;
337 u_long chandle;
338 {
339 switch (chandle) {
340 case NULL_CLASS_HANDLE:
341 return (NULL);
342 case ROOT_CLASS_HANDLE:
343 return (cbqp->ifnp.root_);
344 case DEFAULT_CLASS_HANDLE:
345 return (cbqp->ifnp.default_);
346 case CTL_CLASS_HANDLE:
347 return (cbqp->ifnp.ctl_);
348 }
349
350 if (chandle >= CBQ_MAX_CLASSES)
351 return (NULL);
352
353 return (cbqp->cbq_class_tbl[chandle]);
354 }
355
356 static int
357 cbq_add_filter(afp)
358 struct cbq_add_filter *afp;
359 {
360 char *ifacename;
361 cbq_state_t *cbqp;
362 struct rm_class *cl;
363
364 ifacename = afp->cbq_iface.cbq_ifacename;
365 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
366 return (EBADF);
367
368 /* Get the pointer to class. */
369 if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
370 return (EINVAL);
371
372 return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
373 cl, &afp->cbq_filter_handle);
374 }
375
376 static int
377 cbq_delete_filter(dfp)
378 struct cbq_delete_filter *dfp;
379 {
380 char *ifacename;
381 cbq_state_t *cbqp;
382
383 ifacename = dfp->cbq_iface.cbq_ifacename;
384 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
385 return (EBADF);
386
387 return acc_delete_filter(&cbqp->cbq_classifier,
388 dfp->cbq_filter_handle);
389 }
390
391 /*
392 * cbq_clear_hierarchy deletes all classes and their filters on the
393 * given interface.
394 */
395 static int
396 cbq_clear_hierarchy(ifacep)
397 struct cbq_interface *ifacep;
398 {
399 char *ifacename;
400 cbq_state_t *cbqp;
401
402 ifacename = ifacep->cbq_ifacename;
403 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
404 return (EBADF);
405
406 return cbq_clear_interface(cbqp);
407 }
408
409 static int
410 cbq_clear_interface(cbqp)
411 cbq_state_t *cbqp;
412 {
413 int again, i;
414 struct rm_class *cl;
415
416 /* free the filters for this interface */
417 acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
418
419 /* clear out the classes now */
420 do {
421 again = 0;
422 for (i = 0; i < CBQ_MAX_CLASSES; i++) {
423 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
424 if (is_a_parent_class(cl))
425 again++;
426 else {
427 cbq_class_destroy(cbqp, cl);
428 cbqp->cbq_class_tbl[i] = NULL;
429 }
430 }
431 }
432 if (cbqp->ifnp.ctl_ != NULL &&
433 !is_a_parent_class(cbqp->ifnp.ctl_)) {
434 cbq_class_destroy(cbqp, cbqp->ifnp.ctl_);
435 cbqp->ifnp.ctl_ = NULL;
436 }
437 if (cbqp->ifnp.default_ != NULL &&
438 !is_a_parent_class(cbqp->ifnp.default_)) {
439 cbq_class_destroy(cbqp, cbqp->ifnp.default_);
440 cbqp->ifnp.default_ = NULL;
441 }
442 if (cbqp->ifnp.root_ != NULL &&
443 !is_a_parent_class(cbqp->ifnp.root_)) {
444 cbq_class_destroy(cbqp, cbqp->ifnp.root_);
445 cbqp->ifnp.root_ = NULL;
446 }
447 } while (again);
448
449 return (0);
450 }
451
452 static int
453 cbq_request(struct ifaltq *ifq, int req, void *arg __unused)
454 {
455 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
456
457 switch (req) {
458 case ALTRQ_PURGE:
459 cbq_purge(cbqp);
460 break;
461 }
462 return (0);
463 }
464
465 /*
466 * static int
467 * cbq_set_enable(struct cbq_enable *ep) - this function processed the
468 * ioctl request to enable class based queueing. It searches the list
469 * of interfaces for the specified interface and then enables CBQ on
470 * that interface.
471 *
472 * Returns: 0, for no error.
473 * EBADF, for specified inteface not found.
474 */
475
476 static int
477 cbq_set_enable(ep, enable)
478 struct cbq_interface *ep;
479 int enable;
480 {
481 int error = 0;
482 cbq_state_t *cbqp;
483 char *ifacename;
484
485 ifacename = ep->cbq_ifacename;
486 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
487 return (EBADF);
488
489 switch (enable) {
490 case ENABLE:
491 if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL ||
492 cbqp->ifnp.ctl_ == NULL) {
493 if (cbqp->ifnp.root_ == NULL)
494 printf("No Root Class for %s\n", ifacename);
495 if (cbqp->ifnp.default_ == NULL)
496 printf("No Default Class for %s\n", ifacename);
497 if (cbqp->ifnp.ctl_ == NULL)
498 printf("No Control Class for %s\n", ifacename);
499 error = EINVAL;
500 } else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
501 cbqp->cbq_qlen = 0;
502 }
503 break;
504
505 case DISABLE:
506 error = altq_disable(cbqp->ifnp.ifq_);
507 break;
508 }
509 return (error);
510 }
511
512 /* copy the stats info in rm_class to class_states_t */
513 static void
514 get_class_stats(statsp, cl)
515 class_stats_t *statsp;
516 struct rm_class *cl;
517 {
518 statsp->xmit_cnt = cl->stats_.xmit_cnt;
519 statsp->drop_cnt = cl->stats_.drop_cnt;
520 statsp->over = cl->stats_.over;
521 statsp->borrows = cl->stats_.borrows;
522 statsp->overactions = cl->stats_.overactions;
523 statsp->delays = cl->stats_.delays;
524
525 statsp->depth = cl->depth_;
526 statsp->priority = cl->pri_;
527 statsp->maxidle = cl->maxidle_;
528 statsp->minidle = cl->minidle_;
529 statsp->offtime = cl->offtime_;
530 statsp->qmax = qlimit(cl->q_);
531 statsp->ns_per_byte = cl->ns_per_byte_;
532 statsp->wrr_allot = cl->w_allotment_;
533 statsp->qcnt = qlen(cl->q_);
534 statsp->avgidle = cl->avgidle_;
535
536 statsp->qtype = qtype(cl->q_);
537 #ifdef ALTQ_RED
538 if (q_is_red(cl->q_))
539 red_getstats(cl->red_, &statsp->red[0]);
540 #endif
541 #ifdef ALTQ_RIO
542 if (q_is_rio(cl->q_))
543 rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
544 #endif
545 }
546
547 static int
548 cbq_getstats(gsp)
549 struct cbq_getstats *gsp;
550 {
551 char *ifacename;
552 int chandle, n, nclasses;
553 cbq_state_t *cbqp;
554 struct rm_class *cl;
555 class_stats_t stats, *usp;
556 int error = 0;
557
558 ifacename = gsp->iface.cbq_ifacename;
559 nclasses = gsp->nclasses;
560 usp = gsp->stats;
561
562 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
563 return (EBADF);
564 if (nclasses <= 0)
565 return (EINVAL);
566
567 for (n = 0, chandle = 0; n < nclasses && chandle < CBQ_MAX_CLASSES;
568 n++) {
569 switch(n) {
570 case 0:
571 cl = cbqp->ifnp.root_;
572 stats.handle = ROOT_CLASS_HANDLE;
573 break;
574 case 1:
575 cl = cbqp->ifnp.default_;
576 stats.handle = DEFAULT_CLASS_HANDLE;
577 break;
578 case 2:
579 cl = cbqp->ifnp.ctl_;
580 stats.handle = CTL_CLASS_HANDLE;
581 break;
582 default:
583 while ((cl = cbqp->cbq_class_tbl[chandle]) == NULL)
584 if (++chandle >= CBQ_MAX_CLASSES)
585 goto out;
586 stats.handle = chandle++;
587 break;
588 }
589
590 get_class_stats(&stats, cl);
591
592 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
593 sizeof(stats))) != 0)
594 return (error);
595 }
596
597 out:
598 gsp->nclasses = n;
599 return (error);
600 }
601
602 static int
603 cbq_ifattach(ifacep)
604 struct cbq_interface *ifacep;
605 {
606 int error = 0;
607 char *ifacename;
608 cbq_state_t *new_cbqp;
609 struct ifnet *ifp;
610
611 ifacename = ifacep->cbq_ifacename;
612 if ((ifp = ifunit(ifacename)) == NULL)
613 return (ENXIO);
614 if (!ALTQ_IS_READY(&ifp->if_snd))
615 return (ENXIO);
616
617 /* allocate and initialize cbq_state_t */
618 new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
619 if (new_cbqp == NULL)
620 return (ENOMEM);
621 CALLOUT_INIT(&new_cbqp->cbq_callout);
622 new_cbqp->cbq_class_tbl = malloc(sizeof(struct rm_class *) *
623 CBQ_MAX_CLASSES, M_DEVBUF, M_WAITOK|M_ZERO);
624 if (new_cbqp->cbq_class_tbl == NULL) {
625 free(new_cbqp, M_DEVBUF);
626 return (ENOMEM);
627 }
628 new_cbqp->cbq_qlen = 0;
629 new_cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */
630
631 /*
632 * set CBQ to this ifnet structure.
633 */
634 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
635 cbq_enqueue, cbq_dequeue, cbq_request,
636 &new_cbqp->cbq_classifier, acc_classify);
637 if (error) {
638 free(new_cbqp->cbq_class_tbl, M_DEVBUF);
639 free(new_cbqp, M_DEVBUF);
640 return (error);
641 }
642
643 /* prepend to the list of cbq_state_t's. */
644 new_cbqp->cbq_next = cbq_list;
645 cbq_list = new_cbqp;
646
647 return (0);
648 }
649
650 static int
651 cbq_ifdetach(ifacep)
652 struct cbq_interface *ifacep;
653 {
654 char *ifacename;
655 cbq_state_t *cbqp;
656
657 ifacename = ifacep->cbq_ifacename;
658 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
659 return (EBADF);
660
661 (void)cbq_set_enable(ifacep, DISABLE);
662
663 cbq_clear_interface(cbqp);
664
665 if (cbqp->ifnp.ctl_)
666 cbq_class_destroy(cbqp, cbqp->ifnp.ctl_);
667 if (cbqp->ifnp.default_)
668 cbq_class_destroy(cbqp, cbqp->ifnp.default_);
669 if (cbqp->ifnp.root_)
670 cbq_class_destroy(cbqp, cbqp->ifnp.root_);
671
672 /* remove CBQ from the ifnet structure. */
673 (void)altq_detach(cbqp->ifnp.ifq_);
674
675 /* remove from the list of cbq_state_t's. */
676 if (cbq_list == cbqp)
677 cbq_list = cbqp->cbq_next;
678 else {
679 cbq_state_t *cp;
680
681 for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
682 if (cp->cbq_next == cbqp) {
683 cp->cbq_next = cbqp->cbq_next;
684 break;
685 }
686 ASSERT(cp != NULL);
687 }
688
689 /* deallocate cbq_state_t */
690 free(cbqp->cbq_class_tbl, M_DEVBUF);
691 free(cbqp, M_DEVBUF);
692
693 return (0);
694 }
695
696 /*
697 * int
698 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr)
699 * - Queue data packets.
700 *
701 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
702 * layer (e.g. ether_output). cbq_enqueue queues the given packet
703 * to the cbq, then invokes the driver's start routine.
704 *
705 * Assumptions: called in splnet
706 * Returns: 0 if the queueing is successful.
707 * ENOBUFS if a packet dropping occurred as a result of
708 * the queueing.
709 */
710
711 static int
712 cbq_enqueue(ifq, m, pktattr)
713 struct ifaltq *ifq;
714 struct mbuf *m;
715 struct altq_pktattr *pktattr;
716 {
717 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
718 struct rm_class *cl;
719 int len;
720
721 /* grab class set by classifier */
722 if (pktattr == NULL || (cl = pktattr->pattr_class) == NULL)
723 cl = cbqp->ifnp.default_;
724 cl->pktattr_ = pktattr; /* save proto hdr used by ECN */
725
726 len = m_pktlen(m);
727 if (rmc_queue_packet(cl, m) != 0) {
728 /* drop occurred. some mbuf was freed in rmc_queue_packet. */
729 PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
730 return (ENOBUFS);
731 }
732
733 /* successfully queued. */
734 ++cbqp->cbq_qlen;
735 IFQ_INC_LEN(ifq);
736 return (0);
737 }
738
739 static struct mbuf *
740 cbq_dequeue(ifq, op)
741 struct ifaltq *ifq;
742 int op;
743 {
744 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
745 struct mbuf *m;
746
747 m = rmc_dequeue_next(&cbqp->ifnp, op);
748
749 if (m && op == ALTDQ_REMOVE) {
750 --cbqp->cbq_qlen; /* decrement # of packets in cbq */
751 IFQ_DEC_LEN(ifq);
752
753 /* Update the class. */
754 rmc_update_class_util(&cbqp->ifnp);
755 }
756 return (m);
757 }
758
759 /*
760 * void
761 * cbqrestart(queue_t *) - Restart sending of data.
762 * called from rmc_restart in splnet via timeout after waking up
763 * a suspended class.
764 * Returns: NONE
765 */
766
767 static void
768 cbqrestart(ifq)
769 struct ifaltq *ifq;
770 {
771 cbq_state_t *cbqp;
772 struct ifnet *ifp;
773
774 if (!ALTQ_IS_ENABLED(ifq))
775 /* cbq must have been detached */
776 return;
777 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
778 /* should not happen */
779 return;
780
781 ifp = ifq->altq_ifp;
782 if (ifp->if_start &&
783 cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
784 (*ifp->if_start)(ifp);
785 }
786
787 static void cbq_purge(cbqp)
788 cbq_state_t *cbqp;
789 {
790 struct rm_class *cl;
791 int i;
792
793 for (i = 0; i < CBQ_MAX_CLASSES; i++)
794 if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
795 rmc_dropall(cl);
796 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
797 cbqp->ifnp.ifq_->ifq_len = 0;
798 }
799
800 /*
801 * cbq device interface
802 */
803
804 altqdev_decl(cbq);
805
806 int
807 cbqopen(dev_t dev __unused, int flag __unused, int fmt __unused,
808 struct lwp *l __unused)
809 {
810 return (0);
811 }
812
813 int
814 cbqclose(dev_t dev __unused, int flag __unused, int fmt __unused,
815 struct lwp *l __unused)
816 {
817 struct ifnet *ifp;
818 struct cbq_interface iface;
819 int err, error = 0;
820
821 while (cbq_list) {
822 ifp = cbq_list->ifnp.ifq_->altq_ifp;
823 #if defined(__NetBSD__) || defined(__OpenBSD__)
824 sprintf(iface.cbq_ifacename, "%s", ifp->if_xname);
825 #else
826 sprintf(iface.cbq_ifacename,
827 "%s%d", ifp->if_name, ifp->if_unit);
828 #endif
829 err = cbq_ifdetach(&iface);
830 if (err != 0 && error == 0)
831 error = err;
832 }
833
834 return (error);
835 }
836
837 int
838 cbqioctl(dev_t dev __unused, ioctlcmd_t cmd, caddr_t addr, int flag __unused,
839 struct lwp *l)
840 {
841 int error = 0;
842
843 /* check cmd for superuser only */
844 switch (cmd) {
845 case CBQ_GETSTATS:
846 /* currently only command that an ordinary user can call */
847 break;
848 default:
849 #if (__FreeBSD_version > 400000)
850 error = suser(p);
851 #else
852 error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
853 &l->l_acflag);
854 #endif
855 if (error)
856 return (error);
857 break;
858 }
859
860 switch (cmd) {
861
862 case CBQ_ENABLE:
863 error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
864 break;
865
866 case CBQ_DISABLE:
867 error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
868 break;
869
870 case CBQ_ADD_FILTER:
871 error = cbq_add_filter((struct cbq_add_filter *)addr);
872 break;
873
874 case CBQ_DEL_FILTER:
875 error = cbq_delete_filter((struct cbq_delete_filter *)addr);
876 break;
877
878 case CBQ_ADD_CLASS:
879 error = cbq_add_class((struct cbq_add_class *)addr);
880 break;
881
882 case CBQ_DEL_CLASS:
883 error = cbq_delete_class((struct cbq_delete_class *)addr);
884 break;
885
886 case CBQ_MODIFY_CLASS:
887 error = cbq_modify_class((struct cbq_modify_class *)addr);
888 break;
889
890 case CBQ_CLEAR_HIERARCHY:
891 error = cbq_clear_hierarchy((struct cbq_interface *)addr);
892 break;
893
894 case CBQ_IF_ATTACH:
895 error = cbq_ifattach((struct cbq_interface *)addr);
896 break;
897
898 case CBQ_IF_DETACH:
899 error = cbq_ifdetach((struct cbq_interface *)addr);
900 break;
901
902 case CBQ_GETSTATS:
903 error = cbq_getstats((struct cbq_getstats *)addr);
904 break;
905
906 default:
907 error = EINVAL;
908 break;
909 }
910
911 return error;
912 }
913
914 #if 0
915 /* for debug */
916 static void cbq_class_dump(int);
917
918 static void cbq_class_dump(i)
919 int i;
920 {
921 struct rm_class *cl;
922 rm_class_stats_t *s;
923 struct _class_queue_ *q;
924
925 if (cbq_list == NULL) {
926 printf("cbq_class_dump: no cbq_state found\n");
927 return;
928 }
929 cl = cbq_list->cbq_class_tbl[i];
930
931 printf("class %d cl=%p\n", i, cl);
932 if (cl != NULL) {
933 s = &cl->stats_;
934 q = cl->q_;
935
936 printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n",
937 cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_);
938 printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n",
939 cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_,
940 cl->maxidle_);
941 printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n",
942 cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_);
943 printf("handle=%d, depth=%d, packets=%d, bytes=%d\n",
944 s->handle, s->depth,
945 (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes);
946 printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n",
947 s->over, s->borrows, (int)s->drop_cnt.packets,
948 s->overactions, s->delays);
949 printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n",
950 q->tail_, q->head_, q->qlen_, q->qlim_,
951 q->qthresh_, q->qtype_);
952 }
953 }
954 #endif /* 0 */
955
956 #ifdef KLD_MODULE
957
958 static struct altqsw cbq_sw =
959 {"cbq", cbqopen, cbqclose, cbqioctl};
960
961 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw);
962
963 #endif /* KLD_MODULE */
964
965 #endif /* ALTQ_CBQ */
966