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