altq_cbq.c revision 1.30 1 /* $NetBSD: altq_cbq.c,v 1.30 2016/06/20 08:30:58 knakahara 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.30 2016/06/20 08:30:58 knakahara 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 get_class_stats(&stats, cl);
475
476 if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
477 return (error);
478 *nbytes = sizeof(stats);
479 return (0);
480 }
481 #endif /* NPF > 0 */
482
483 /*
484 * int
485 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
486 * - Queue data packets.
487 *
488 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
489 * layer (e.g. ether_output). cbq_enqueue queues the given packet
490 * to the cbq, then invokes the driver's start routine.
491 *
492 * Assumptions: called in splnet
493 * Returns: 0 if the queueing is successful.
494 * ENOBUFS if a packet dropping occurred as a result of
495 * the queueing.
496 */
497
498 static int
499 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
500 {
501 struct altq_pktattr pktattr;
502 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
503 struct rm_class *cl;
504 struct m_tag *t;
505 int len;
506
507 /* grab class set by classifier */
508 if ((m->m_flags & M_PKTHDR) == 0) {
509 /* should not happen */
510 printf("altq: packet for %s does not have pkthdr\n",
511 ifq->altq_ifp->if_xname);
512 m_freem(m);
513 return (ENOBUFS);
514 }
515 cl = NULL;
516 if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID, NULL)) != NULL)
517 cl = clh_to_clp(cbqp, ((struct altq_tag *)(t+1))->qid);
518 #ifdef ALTQ3_COMPAT
519 else if (ifq->altq_flags & ALTQF_CLASSIFY)
520 cl = m->m_pkthdr.pattr_class;
521 #endif
522 if (cl == NULL) {
523 cl = cbqp->ifnp.default_;
524 if (cl == NULL) {
525 m_freem(m);
526 return (ENOBUFS);
527 }
528 }
529 #ifdef ALTQ3_COMPAT
530 if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
531 pktattr.pattr_class = m->m_pkthdr.pattr_class;
532 pktattr.pattr_af = m->m_pkthdr.pattr_af;
533 pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
534
535 cl->pktattr_ = &pktattr; /* save proto hdr used by ECN */
536 } else
537 #endif
538 cl->pktattr_ = NULL;
539 len = m_pktlen(m);
540 if (rmc_queue_packet(cl, m) != 0) {
541 /* drop occurred. some mbuf was freed in rmc_queue_packet. */
542 PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
543 return (ENOBUFS);
544 }
545
546 /* successfully queued. */
547 ++cbqp->cbq_qlen;
548 IFQ_INC_LEN(ifq);
549 return (0);
550 }
551
552 static struct mbuf *
553 cbq_dequeue(struct ifaltq *ifq, int op)
554 {
555 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
556 struct mbuf *m;
557
558 m = rmc_dequeue_next(&cbqp->ifnp, op);
559
560 if (m && op == ALTDQ_REMOVE) {
561 --cbqp->cbq_qlen; /* decrement # of packets in cbq */
562 IFQ_DEC_LEN(ifq);
563
564 /* Update the class. */
565 rmc_update_class_util(&cbqp->ifnp);
566 }
567 return (m);
568 }
569
570 /*
571 * void
572 * cbqrestart(queue_t *) - Restart sending of data.
573 * called from rmc_restart in splnet via timeout after waking up
574 * a suspended class.
575 * Returns: NONE
576 */
577
578 static void
579 cbqrestart(struct ifaltq *ifq)
580 {
581 cbq_state_t *cbqp;
582 struct ifnet *ifp;
583
584 if (!ALTQ_IS_ENABLED(ifq))
585 /* cbq must have been detached */
586 return;
587
588 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
589 /* should not happen */
590 return;
591
592 ifp = ifq->altq_ifp;
593 if (ifp->if_start &&
594 cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
595 if_start_lock(ifp);
596 }
597
598 static void
599 cbq_purge(cbq_state_t *cbqp)
600 {
601 struct rm_class *cl;
602 int i;
603
604 for (i = 0; i < CBQ_MAX_CLASSES; i++)
605 if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
606 rmc_dropall(cl);
607 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
608 cbqp->ifnp.ifq_->ifq_len = 0;
609 }
610 #ifdef ALTQ3_COMPAT
611
612 static int
613 cbq_add_class(struct cbq_add_class *acp)
614 {
615 char *ifacename;
616 struct rm_class *borrow, *parent;
617 cbq_state_t *cbqp;
618
619 ifacename = acp->cbq_iface.cbq_ifacename;
620 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
621 return (EBADF);
622
623 /* check parameters */
624 if (acp->cbq_class.priority >= CBQ_MAXPRI ||
625 acp->cbq_class.maxq > CBQ_MAXQSIZE)
626 return (EINVAL);
627
628 /* Get pointers to parent and borrow classes. */
629 parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
630 borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
631
632 /*
633 * A class must borrow from its parent or it can not
634 * borrow at all. Hence, borrow can be null.
635 */
636 if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
637 printf("cbq_add_class: no parent class!\n");
638 return (EINVAL);
639 }
640
641 if ((borrow != parent) && (borrow != NULL)) {
642 printf("cbq_add_class: borrow class != parent\n");
643 return (EINVAL);
644 }
645
646 return cbq_class_create(cbqp, acp, parent, borrow);
647 }
648
649 static int
650 cbq_delete_class(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
674 cbq_modify_class(struct cbq_modify_class *acp)
675 {
676 char *ifacename;
677 struct rm_class *cl;
678 cbq_state_t *cbqp;
679
680 ifacename = acp->cbq_iface.cbq_ifacename;
681 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
682 return (EBADF);
683
684 /* Get pointer to this class */
685 if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
686 return (EINVAL);
687
688 if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte,
689 acp->cbq_class.maxq, acp->cbq_class.maxidle,
690 acp->cbq_class.minidle, acp->cbq_class.offtime,
691 acp->cbq_class.pktsize) < 0)
692 return (EINVAL);
693 return (0);
694 }
695
696 /*
697 * struct rm_class *
698 * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
699 * struct rm_class *parent, struct rm_class *borrow)
700 *
701 * This function create a new traffic class in the CBQ class hierarchy of
702 * given paramters. The class that created is either the root, default,
703 * or a new dynamic class. If CBQ is not initilaized, the root class
704 * will be created.
705 */
706 static int
707 cbq_class_create(cbq_state_t *cbqp, struct cbq_add_class *acp,
708 struct rm_class *parent, struct rm_class *borrow)
709 {
710 struct rm_class *cl;
711 cbq_class_spec_t *spec = &acp->cbq_class;
712 u_int32_t chandle;
713 int i, error;
714
715 /*
716 * allocate class handle
717 */
718 for (i = 1; i < CBQ_MAX_CLASSES; i++)
719 if (cbqp->cbq_class_tbl[i] == NULL)
720 break;
721 if (i == CBQ_MAX_CLASSES)
722 return (EINVAL);
723 chandle = i; /* use the slot number as class handle */
724
725 /*
726 * create a class. if this is a root class, initialize the
727 * interface.
728 */
729 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
730 error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
731 spec->nano_sec_per_byte, cbqrestart, spec->maxq,
732 RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
733 spec->flags);
734 if (error)
735 return (error);
736 cl = cbqp->ifnp.root_;
737 } else {
738 cl = rmc_newclass(spec->priority,
739 &cbqp->ifnp, spec->nano_sec_per_byte,
740 rmc_delay_action, spec->maxq, parent, borrow,
741 spec->maxidle, spec->minidle, spec->offtime,
742 spec->pktsize, spec->flags);
743 }
744 if (cl == NULL)
745 return (ENOMEM);
746
747 /* return handle to user space. */
748 acp->cbq_class_handle = chandle;
749
750 cl->stats_.handle = chandle;
751 cl->stats_.depth = cl->depth_;
752
753 /* save the allocated class */
754 cbqp->cbq_class_tbl[i] = cl;
755
756 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
757 cbqp->ifnp.default_ = cl;
758 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_CTLCLASS)
759 cbqp->ifnp.ctl_ = cl;
760
761 return (0);
762 }
763
764 static int
765 cbq_add_filter(struct cbq_add_filter *afp)
766 {
767 char *ifacename;
768 cbq_state_t *cbqp;
769 struct rm_class *cl;
770
771 ifacename = afp->cbq_iface.cbq_ifacename;
772 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
773 return (EBADF);
774
775 /* Get the pointer to class. */
776 if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
777 return (EINVAL);
778
779 return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
780 cl, &afp->cbq_filter_handle);
781 }
782
783 static int
784 cbq_delete_filter(struct cbq_delete_filter *dfp)
785 {
786 char *ifacename;
787 cbq_state_t *cbqp;
788
789 ifacename = dfp->cbq_iface.cbq_ifacename;
790 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
791 return (EBADF);
792
793 return acc_delete_filter(&cbqp->cbq_classifier,
794 dfp->cbq_filter_handle);
795 }
796
797 /*
798 * cbq_clear_hierarchy deletes all classes and their filters on the
799 * given interface.
800 */
801 static int
802 cbq_clear_hierarchy(struct cbq_interface *ifacep)
803 {
804 char *ifacename;
805 cbq_state_t *cbqp;
806
807 ifacename = ifacep->cbq_ifacename;
808 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
809 return (EBADF);
810
811 return cbq_clear_interface(cbqp);
812 }
813
814 /*
815 * static int
816 * cbq_set_enable(struct cbq_enable *ep) - this function processed the
817 * ioctl request to enable class based queueing. It searches the list
818 * of interfaces for the specified interface and then enables CBQ on
819 * that interface.
820 *
821 * Returns: 0, for no error.
822 * EBADF, for specified inteface not found.
823 */
824
825 static int
826 cbq_set_enable(struct cbq_interface *ep, int enable)
827 {
828 int error = 0;
829 cbq_state_t *cbqp;
830 char *ifacename;
831
832 ifacename = ep->cbq_ifacename;
833 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
834 return (EBADF);
835
836 switch (enable) {
837 case ENABLE:
838 if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL ||
839 cbqp->ifnp.ctl_ == NULL) {
840 if (cbqp->ifnp.root_ == NULL)
841 printf("No Root Class for %s\n", ifacename);
842 if (cbqp->ifnp.default_ == NULL)
843 printf("No Default Class for %s\n", ifacename);
844 if (cbqp->ifnp.ctl_ == NULL)
845 printf("No Control Class for %s\n", ifacename);
846 error = EINVAL;
847 } else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
848 cbqp->cbq_qlen = 0;
849 }
850 break;
851
852 case DISABLE:
853 error = altq_disable(cbqp->ifnp.ifq_);
854 break;
855 }
856 return (error);
857 }
858
859 static int
860 cbq_getstats(struct cbq_getstats *gsp)
861 {
862 char *ifacename;
863 int i, n, nclasses;
864 cbq_state_t *cbqp;
865 struct rm_class *cl;
866 class_stats_t stats, *usp;
867 int error = 0;
868
869 ifacename = gsp->iface.cbq_ifacename;
870 nclasses = gsp->nclasses;
871 usp = gsp->stats;
872
873 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
874 return (EBADF);
875 if (nclasses <= 0)
876 return (EINVAL);
877
878 for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) {
879 while ((cl = cbqp->cbq_class_tbl[i]) == NULL)
880 if (++i >= CBQ_MAX_CLASSES)
881 goto out;
882
883 get_class_stats(&stats, cl);
884 stats.handle = cl->stats_.handle;
885
886 if ((error = copyout((void *)&stats, (void *)usp++,
887 sizeof(stats))) != 0)
888 return (error);
889 }
890
891 out:
892 gsp->nclasses = n;
893 return (error);
894 }
895
896 static int
897 cbq_ifattach(struct cbq_interface *ifacep)
898 {
899 int error = 0;
900 char *ifacename;
901 cbq_state_t *new_cbqp;
902 struct ifnet *ifp;
903
904 ifacename = ifacep->cbq_ifacename;
905 if ((ifp = ifunit(ifacename)) == NULL)
906 return (ENXIO);
907 if (!ALTQ_IS_READY(&ifp->if_snd))
908 return (ENXIO);
909
910 /* allocate and initialize cbq_state_t */
911 new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
912 if (new_cbqp == NULL)
913 return (ENOMEM);
914 CALLOUT_INIT(&new_cbqp->cbq_callout);
915
916 new_cbqp->cbq_qlen = 0;
917 new_cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */
918
919 /*
920 * set CBQ to this ifnet structure.
921 */
922 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
923 cbq_enqueue, cbq_dequeue, cbq_request,
924 &new_cbqp->cbq_classifier, acc_classify);
925 if (error) {
926 free(new_cbqp, M_DEVBUF);
927 return (error);
928 }
929
930 /* prepend to the list of cbq_state_t's. */
931 new_cbqp->cbq_next = cbq_list;
932 cbq_list = new_cbqp;
933
934 return (0);
935 }
936
937 static int
938 cbq_ifdetach(struct cbq_interface *ifacep)
939 {
940 char *ifacename;
941 cbq_state_t *cbqp;
942
943 ifacename = ifacep->cbq_ifacename;
944 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
945 return (EBADF);
946
947 (void)cbq_set_enable(ifacep, DISABLE);
948
949 cbq_clear_interface(cbqp);
950
951 /* remove CBQ from the ifnet structure. */
952 (void)altq_detach(cbqp->ifnp.ifq_);
953
954 /* remove from the list of cbq_state_t's. */
955 if (cbq_list == cbqp)
956 cbq_list = cbqp->cbq_next;
957 else {
958 cbq_state_t *cp;
959
960 for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
961 if (cp->cbq_next == cbqp) {
962 cp->cbq_next = cbqp->cbq_next;
963 break;
964 }
965 ASSERT(cp != NULL);
966 }
967
968 /* deallocate cbq_state_t */
969 free(cbqp, M_DEVBUF);
970
971 return (0);
972 }
973
974 /*
975 * cbq device interface
976 */
977
978 altqdev_decl(cbq);
979
980 int
981 cbqopen(dev_t dev, int flag, int fmt,
982 struct lwp *l)
983 {
984 return (0);
985 }
986
987 int
988 cbqclose(dev_t dev, int flag, int fmt,
989 struct lwp *l)
990 {
991 struct ifnet *ifp;
992 struct cbq_interface iface;
993 int err, error = 0;
994
995 while (cbq_list) {
996 ifp = cbq_list->ifnp.ifq_->altq_ifp;
997 snprintf(iface.cbq_ifacename, sizeof(iface.cbq_ifacename),
998 "%s", ifp->if_xname);
999 err = cbq_ifdetach(&iface);
1000 if (err != 0 && error == 0)
1001 error = err;
1002 }
1003
1004 return (error);
1005 }
1006
1007 int
1008 cbqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
1009 struct lwp *l)
1010 {
1011 int error = 0;
1012
1013 /* check cmd for superuser only */
1014 switch (cmd) {
1015 case CBQ_GETSTATS:
1016 /* currently only command that an ordinary user can call */
1017 break;
1018 default:
1019 #if (__FreeBSD_version > 400000)
1020 error = suser(p);
1021 #else
1022 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_ALTQ,
1023 KAUTH_REQ_NETWORK_ALTQ_CBQ, NULL, NULL, NULL);
1024 #endif
1025 if (error)
1026 return (error);
1027 break;
1028 }
1029
1030 switch (cmd) {
1031
1032 case CBQ_ENABLE:
1033 error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
1034 break;
1035
1036 case CBQ_DISABLE:
1037 error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
1038 break;
1039
1040 case CBQ_ADD_FILTER:
1041 error = cbq_add_filter((struct cbq_add_filter *)addr);
1042 break;
1043
1044 case CBQ_DEL_FILTER:
1045 error = cbq_delete_filter((struct cbq_delete_filter *)addr);
1046 break;
1047
1048 case CBQ_ADD_CLASS:
1049 error = cbq_add_class((struct cbq_add_class *)addr);
1050 break;
1051
1052 case CBQ_DEL_CLASS:
1053 error = cbq_delete_class((struct cbq_delete_class *)addr);
1054 break;
1055
1056 case CBQ_MODIFY_CLASS:
1057 error = cbq_modify_class((struct cbq_modify_class *)addr);
1058 break;
1059
1060 case CBQ_CLEAR_HIERARCHY:
1061 error = cbq_clear_hierarchy((struct cbq_interface *)addr);
1062 break;
1063
1064 case CBQ_IF_ATTACH:
1065 error = cbq_ifattach((struct cbq_interface *)addr);
1066 break;
1067
1068 case CBQ_IF_DETACH:
1069 error = cbq_ifdetach((struct cbq_interface *)addr);
1070 break;
1071
1072 case CBQ_GETSTATS:
1073 error = cbq_getstats((struct cbq_getstats *)addr);
1074 break;
1075
1076 default:
1077 error = EINVAL;
1078 break;
1079 }
1080
1081 return error;
1082 }
1083
1084 #if 0
1085 /* for debug */
1086 static void cbq_class_dump(int);
1087
1088 static void
1089 cbq_class_dump(int i)
1090 {
1091 struct rm_class *cl;
1092 rm_class_stats_t *s;
1093 struct _class_queue_ *q;
1094
1095 if (cbq_list == NULL) {
1096 printf("cbq_class_dump: no cbq_state found\n");
1097 return;
1098 }
1099 cl = cbq_list->cbq_class_tbl[i];
1100
1101 printf("class %d cl=%p\n", i, cl);
1102 if (cl != NULL) {
1103 s = &cl->stats_;
1104 q = cl->q_;
1105
1106 printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n",
1107 cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_);
1108 printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n",
1109 cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_,
1110 cl->maxidle_);
1111 printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n",
1112 cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_);
1113 printf("handle=%d, depth=%d, packets=%d, bytes=%d\n",
1114 s->handle, s->depth,
1115 (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes);
1116 printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n",
1117 s->over, s->borrows, (int)s->drop_cnt.packets,
1118 s->overactions, s->delays);
1119 printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n",
1120 q->tail_, q->head_, q->qlen_, q->qlim_,
1121 q->qthresh_, q->qtype_);
1122 }
1123 }
1124 #endif /* 0 */
1125
1126 #ifdef KLD_MODULE
1127
1128 static struct altqsw cbq_sw =
1129 {"cbq", cbqopen, cbqclose, cbqioctl};
1130
1131 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw);
1132 MODULE_DEPEND(altq_cbq, altq_red, 1, 1, 1);
1133 MODULE_DEPEND(altq_cbq, altq_rio, 1, 1, 1);
1134
1135 #endif /* KLD_MODULE */
1136 #endif /* ALTQ3_COMPAT */
1137
1138 #endif /* ALTQ_CBQ */
1139