2 /* $KAME: altq_cdnr.c,v 1.14 2003/09/05 22:40:36 itojun Exp $ */
5 * Copyright (C) 1999-2002
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(__FreeBSD__) || defined(__NetBSD__)
32 #if (__FreeBSD__ != 2)
35 #include "opt_inet6.h"
38 #endif /* __FreeBSD__ || __NetBSD__ */
40 #include <sys/param.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
52 #include <net/if_types.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
60 #include <altq/altq.h>
62 #include <altq/altq_conf.h>
64 #include <altq/altq_cdnr.h>
68 * diffserv traffic conditioning module
71 int altq_cdnr_enabled = 0;
73 /* traffic conditioner is enabled by ALTQ_CDNR option in opt_altq.h */
76 /* cdnr_list keeps all cdnr's allocated. */
77 static LIST_HEAD(, top_cdnr) tcb_list;
79 static int altq_cdnr_input(struct mbuf *, int);
80 static struct top_cdnr *tcb_lookup(char *ifname);
81 static struct cdnr_block *cdnr_handle2cb(u_long);
82 static u_long cdnr_cb2handle(struct cdnr_block *);
83 static void *cdnr_cballoc(struct top_cdnr *, int,
84 struct tc_action *(*)(struct cdnr_block *, struct cdnr_pktinfo *));
85 static void cdnr_cbdestroy(void *);
86 static int tca_verify_action(struct tc_action *);
87 static void tca_import_action(struct tc_action *, struct tc_action *);
88 static void tca_invalidate_action(struct tc_action *);
90 static int generic_element_destroy(struct cdnr_block *);
91 static struct top_cdnr *top_create(struct ifaltq *);
92 static int top_destroy(struct top_cdnr *);
93 static struct cdnr_block *element_create(struct top_cdnr *, struct tc_action *);
94 static int element_destroy(struct cdnr_block *);
95 static void tb_import_profile(struct tbe *, struct tb_profile *);
96 static struct tbmeter *tbm_create(struct top_cdnr *, struct tb_profile *,
97 struct tc_action *, struct tc_action *);
98 static int tbm_destroy(struct tbmeter *);
99 static struct tc_action *tbm_input(struct cdnr_block *, struct cdnr_pktinfo *);
100 static struct trtcm *trtcm_create(struct top_cdnr *,
101 struct tb_profile *, struct tb_profile *,
102 struct tc_action *, struct tc_action *, struct tc_action *,
104 static int trtcm_destroy(struct trtcm *);
105 static struct tc_action *trtcm_input(struct cdnr_block *, struct cdnr_pktinfo *);
106 static struct tswtcm *tswtcm_create(struct top_cdnr *,
107 u_int32_t, u_int32_t, u_int32_t,
108 struct tc_action *, struct tc_action *, struct tc_action *);
109 static int tswtcm_destroy(struct tswtcm *);
110 static struct tc_action *tswtcm_input(struct cdnr_block *, struct cdnr_pktinfo *);
112 static int cdnrcmd_if_attach(char *);
113 static int cdnrcmd_if_detach(char *);
114 static int cdnrcmd_add_element(struct cdnr_add_element *);
115 static int cdnrcmd_delete_element(struct cdnr_delete_element *);
116 static int cdnrcmd_add_filter(struct cdnr_add_filter *);
117 static int cdnrcmd_delete_filter(struct cdnr_delete_filter *);
118 static int cdnrcmd_add_tbm(struct cdnr_add_tbmeter *);
119 static int cdnrcmd_modify_tbm(struct cdnr_modify_tbmeter *);
120 static int cdnrcmd_tbm_stats(struct cdnr_tbmeter_stats *);
121 static int cdnrcmd_add_trtcm(struct cdnr_add_trtcm *);
122 static int cdnrcmd_modify_trtcm(struct cdnr_modify_trtcm *);
123 static int cdnrcmd_tcm_stats(struct cdnr_tcm_stats *);
124 static int cdnrcmd_add_tswtcm(struct cdnr_add_tswtcm *);
125 static int cdnrcmd_modify_tswtcm(struct cdnr_modify_tswtcm *);
126 static int cdnrcmd_get_stats(struct cdnr_get_stats *);
131 * top level input function called from ip_input.
132 * should be called before converting header fields to host-byte-order.
135 altq_cdnr_input(m, af)
137 int af; /* address family */
141 struct top_cdnr *top;
142 struct tc_action *tca;
143 struct cdnr_block *cb;
144 struct cdnr_pktinfo pktinfo;
146 ifp = m->m_pkthdr.rcvif;
147 if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
148 /* traffic conditioner is not enabled on this interface */
151 top = ifp->if_snd.altq_cdnr;
153 ip = mtod(m, struct ip *);
155 if (af == AF_INET6) {
158 flowlabel = ((struct ip6_hdr *)ip)->ip6_flow;
159 pktinfo.pkt_dscp = (ntohl(flowlabel) >> 20) & DSCP_MASK;
162 pktinfo.pkt_dscp = ip->ip_tos & DSCP_MASK;
163 pktinfo.pkt_len = m_pktlen(m);
167 cb = acc_classify(&top->tc_classifier, m, af);
169 tca = &cb->cb_action;
172 tca = &top->tc_block.cb_action;
175 PKTCNTR_ADD(&top->tc_cnts[tca->tca_code], pktinfo.pkt_len);
177 switch (tca->tca_code) {
187 if (af == AF_INET6) {
188 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
191 flowlabel = ntohl(ip6->ip6_flow);
192 flowlabel = (tca->tca_dscp << 20) |
193 (flowlabel & ~(DSCP_MASK << 20));
194 ip6->ip6_flow = htonl(flowlabel);
197 ip->ip_tos = tca->tca_dscp |
198 (ip->ip_tos & DSCP_CUMASK);
202 tca = (*cb->cb_input)(cb, &pktinfo);
211 static struct top_cdnr *
215 struct top_cdnr *top;
218 if ((ifp = ifunit(ifname)) != NULL)
219 LIST_FOREACH(top, &tcb_list, tc_next)
220 if (top->tc_ifq->altq_ifp == ifp)
225 static struct cdnr_block *
226 cdnr_handle2cb(handle)
229 struct cdnr_block *cb;
231 cb = (struct cdnr_block *)handle;
232 if (handle != ALIGN(cb))
235 if (cb == NULL || cb->cb_handle != handle)
242 struct cdnr_block *cb;
244 return (cb->cb_handle);
248 cdnr_cballoc(top, type, input_func)
249 struct top_cdnr *top;
251 struct tc_action *(*input_func)(struct cdnr_block *,
252 struct cdnr_pktinfo *);
254 struct cdnr_block *cb;
259 size = sizeof(struct top_cdnr);
261 case TCETYPE_ELEMENT:
262 size = sizeof(struct cdnr_block);
264 case TCETYPE_TBMETER:
265 size = sizeof(struct tbmeter);
268 size = sizeof(struct trtcm);
271 size = sizeof(struct tswtcm);
277 cb = malloc(size, M_DEVBUF, M_WAITOK);
285 cb->cb_handle = (u_long)cb;
287 cb->cb_top = (struct top_cdnr *)cb;
291 if (input_func != NULL) {
293 * if this cdnr has an action function,
294 * make tc_action to call itself.
296 cb->cb_action.tca_code = TCACODE_NEXT;
297 cb->cb_action.tca_next = cb;
298 cb->cb_input = input_func;
300 cb->cb_action.tca_code = TCACODE_NONE;
302 /* if this isn't top, register the element to the top level cdnr */
304 LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
310 cdnr_cbdestroy(cblock)
313 struct cdnr_block *cb = cblock;
315 /* delete filters belonging to this cdnr */
316 acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
318 /* remove from the top level cdnr */
319 if (cb->cb_top != cblock)
320 LIST_REMOVE(cb, cb_next);
326 * conditioner common destroy routine
329 generic_element_destroy(cb)
330 struct cdnr_block *cb;
334 switch (cb->cb_type) {
336 error = top_destroy((struct top_cdnr *)cb);
338 case TCETYPE_ELEMENT:
339 error = element_destroy(cb);
341 case TCETYPE_TBMETER:
342 error = tbm_destroy((struct tbmeter *)cb);
345 error = trtcm_destroy((struct trtcm *)cb);
348 error = tswtcm_destroy((struct tswtcm *)cb);
357 tca_verify_action(utca)
358 struct tc_action *utca;
360 switch (utca->tca_code) {
368 /* verify handle value */
369 if (cdnr_handle2cb(utca->tca_handle) == NULL)
377 /* should not be passed from a user */
384 tca_import_action(ktca, utca)
385 struct tc_action *ktca, *utca;
387 struct cdnr_block *cb;
390 if (ktca->tca_code == TCACODE_HANDLE) {
391 cb = cdnr_handle2cb(ktca->tca_handle);
393 ktca->tca_code = TCACODE_NONE;
396 ktca->tca_code = TCACODE_NEXT;
399 } else if (ktca->tca_code == TCACODE_MARK) {
400 ktca->tca_dscp &= DSCP_MASK;
406 tca_invalidate_action(tca)
407 struct tc_action *tca;
409 struct cdnr_block *cb;
411 if (tca->tca_code == TCACODE_NEXT) {
417 tca->tca_code = TCACODE_NONE;
421 * top level traffic conditioner
423 static struct top_cdnr *
427 struct top_cdnr *top;
429 if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
433 /* set default action for the top level conditioner */
434 top->tc_block.cb_action.tca_code = TCACODE_PASS;
436 LIST_INSERT_HEAD(&tcb_list, top, tc_next);
438 ifq->altq_cdnr = top;
445 struct top_cdnr *top;
447 struct cdnr_block *cb;
449 if (ALTQ_IS_CNDTNING(top->tc_ifq))
450 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
451 top->tc_ifq->altq_cdnr = NULL;
454 * destroy all the conditioner elements belonging to this interface
456 while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
457 while (cb != NULL && cb->cb_ref > 0)
458 cb = LIST_NEXT(cb, cb_next);
460 generic_element_destroy(cb);
463 LIST_REMOVE(top, tc_next);
467 /* if there is no active conditioner, remove the input hook */
468 if (altq_input != NULL) {
469 LIST_FOREACH(top, &tcb_list, tc_next)
470 if (ALTQ_IS_CNDTNING(top->tc_ifq))
480 * simple tc elements without input function (e.g., dropper and makers).
482 static struct cdnr_block *
483 element_create(top, action)
484 struct top_cdnr *top;
485 struct tc_action *action;
487 struct cdnr_block *cb;
489 if (tca_verify_action(action) < 0)
492 if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
495 tca_import_action(&cb->cb_action, action);
502 struct cdnr_block *cb;
507 tca_invalidate_action(&cb->cb_action);
514 * internal representation of token bucket parameters
515 * rate: byte_per_unittime << 32
516 * (((bits_per_sec) / 8) << 32) / machclk_freq
521 #define TB_SCALE(x) ((u_int64_t)(x) << TB_SHIFT)
522 #define TB_UNSCALE(x) ((x) >> TB_SHIFT)
525 tb_import_profile(tb, profile)
527 struct tb_profile *profile;
529 tb->rate = TB_SCALE(profile->rate / 8) / machclk_freq;
530 tb->depth = TB_SCALE(profile->depth);
532 tb->filluptime = tb->depth / tb->rate;
534 tb->filluptime = 0xffffffffffffffffLL;
535 tb->token = tb->depth;
536 tb->last = read_machclk();
540 * simple token bucket meter
542 static struct tbmeter *
543 tbm_create(top, profile, in_action, out_action)
544 struct top_cdnr *top;
545 struct tb_profile *profile;
546 struct tc_action *in_action, *out_action;
548 struct tbmeter *tbm = NULL;
550 if (tca_verify_action(in_action) < 0
551 || tca_verify_action(out_action) < 0)
554 if ((tbm = cdnr_cballoc(top, TCETYPE_TBMETER,
558 tb_import_profile(&tbm->tb, profile);
560 tca_import_action(&tbm->in_action, in_action);
561 tca_import_action(&tbm->out_action, out_action);
570 if (tbm->cdnrblk.cb_ref > 0)
573 tca_invalidate_action(&tbm->in_action);
574 tca_invalidate_action(&tbm->out_action);
580 static struct tc_action *
581 tbm_input(cb, pktinfo)
582 struct cdnr_block *cb;
583 struct cdnr_pktinfo *pktinfo;
585 struct tbmeter *tbm = (struct tbmeter *)cb;
587 u_int64_t interval, now;
589 len = TB_SCALE(pktinfo->pkt_len);
591 if (tbm->tb.token < len) {
592 now = read_machclk();
593 interval = now - tbm->tb.last;
594 if (interval >= tbm->tb.filluptime)
595 tbm->tb.token = tbm->tb.depth;
597 tbm->tb.token += interval * tbm->tb.rate;
598 if (tbm->tb.token > tbm->tb.depth)
599 tbm->tb.token = tbm->tb.depth;
604 if (tbm->tb.token < len) {
605 PKTCNTR_ADD(&tbm->out_cnt, pktinfo->pkt_len);
606 return (&tbm->out_action);
609 tbm->tb.token -= len;
610 PKTCNTR_ADD(&tbm->in_cnt, pktinfo->pkt_len);
611 return (&tbm->in_action);
615 * two rate three color marker
616 * as described in draft-heinanen-diffserv-trtcm-01.txt
618 static struct trtcm *
619 trtcm_create(top, cmtd_profile, peak_profile,
620 green_action, yellow_action, red_action, coloraware)
621 struct top_cdnr *top;
622 struct tb_profile *cmtd_profile, *peak_profile;
623 struct tc_action *green_action, *yellow_action, *red_action;
626 struct trtcm *tcm = NULL;
628 if (tca_verify_action(green_action) < 0
629 || tca_verify_action(yellow_action) < 0
630 || tca_verify_action(red_action) < 0)
633 if ((tcm = cdnr_cballoc(top, TCETYPE_TRTCM,
634 trtcm_input)) == NULL)
637 tb_import_profile(&tcm->cmtd_tb, cmtd_profile);
638 tb_import_profile(&tcm->peak_tb, peak_profile);
640 tca_import_action(&tcm->green_action, green_action);
641 tca_import_action(&tcm->yellow_action, yellow_action);
642 tca_import_action(&tcm->red_action, red_action);
644 /* set dscps to use */
645 if (tcm->green_action.tca_code == TCACODE_MARK)
646 tcm->green_dscp = tcm->green_action.tca_dscp & DSCP_MASK;
648 tcm->green_dscp = DSCP_AF11;
649 if (tcm->yellow_action.tca_code == TCACODE_MARK)
650 tcm->yellow_dscp = tcm->yellow_action.tca_dscp & DSCP_MASK;
652 tcm->yellow_dscp = DSCP_AF12;
653 if (tcm->red_action.tca_code == TCACODE_MARK)
654 tcm->red_dscp = tcm->red_action.tca_dscp & DSCP_MASK;
656 tcm->red_dscp = DSCP_AF13;
658 tcm->coloraware = coloraware;
667 if (tcm->cdnrblk.cb_ref > 0)
670 tca_invalidate_action(&tcm->green_action);
671 tca_invalidate_action(&tcm->yellow_action);
672 tca_invalidate_action(&tcm->red_action);
678 static struct tc_action *
679 trtcm_input(cb, pktinfo)
680 struct cdnr_block *cb;
681 struct cdnr_pktinfo *pktinfo;
683 struct trtcm *tcm = (struct trtcm *)cb;
685 u_int64_t interval, now;
688 len = TB_SCALE(pktinfo->pkt_len);
689 if (tcm->coloraware) {
690 color = pktinfo->pkt_dscp;
691 if (color != tcm->yellow_dscp && color != tcm->red_dscp)
692 color = tcm->green_dscp;
694 /* if color-blind, precolor it as green */
695 color = tcm->green_dscp;
698 now = read_machclk();
699 if (tcm->cmtd_tb.token < len) {
700 interval = now - tcm->cmtd_tb.last;
701 if (interval >= tcm->cmtd_tb.filluptime)
702 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
704 tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
705 if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
706 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
708 tcm->cmtd_tb.last = now;
710 if (tcm->peak_tb.token < len) {
711 interval = now - tcm->peak_tb.last;
712 if (interval >= tcm->peak_tb.filluptime)
713 tcm->peak_tb.token = tcm->peak_tb.depth;
715 tcm->peak_tb.token += interval * tcm->peak_tb.rate;
716 if (tcm->peak_tb.token > tcm->peak_tb.depth)
717 tcm->peak_tb.token = tcm->peak_tb.depth;
719 tcm->peak_tb.last = now;
722 if (color == tcm->red_dscp || tcm->peak_tb.token < len) {
723 pktinfo->pkt_dscp = tcm->red_dscp;
724 PKTCNTR_ADD(&tcm->red_cnt, pktinfo->pkt_len);
725 return (&tcm->red_action);
728 if (color == tcm->yellow_dscp || tcm->cmtd_tb.token < len) {
729 pktinfo->pkt_dscp = tcm->yellow_dscp;
730 tcm->peak_tb.token -= len;
731 PKTCNTR_ADD(&tcm->yellow_cnt, pktinfo->pkt_len);
732 return (&tcm->yellow_action);
735 pktinfo->pkt_dscp = tcm->green_dscp;
736 tcm->cmtd_tb.token -= len;
737 tcm->peak_tb.token -= len;
738 PKTCNTR_ADD(&tcm->green_cnt, pktinfo->pkt_len);
739 return (&tcm->green_action);
743 * time sliding window three color marker
744 * as described in draft-fang-diffserv-tc-tswtcm-00.txt
746 static struct tswtcm *
747 tswtcm_create(top, cmtd_rate, peak_rate, avg_interval,
748 green_action, yellow_action, red_action)
749 struct top_cdnr *top;
750 u_int32_t cmtd_rate, peak_rate, avg_interval;
751 struct tc_action *green_action, *yellow_action, *red_action;
755 if (tca_verify_action(green_action) < 0
756 || tca_verify_action(yellow_action) < 0
757 || tca_verify_action(red_action) < 0)
760 if ((tsw = cdnr_cballoc(top, TCETYPE_TSWTCM,
761 tswtcm_input)) == NULL)
764 tca_import_action(&tsw->green_action, green_action);
765 tca_import_action(&tsw->yellow_action, yellow_action);
766 tca_import_action(&tsw->red_action, red_action);
768 /* set dscps to use */
769 if (tsw->green_action.tca_code == TCACODE_MARK)
770 tsw->green_dscp = tsw->green_action.tca_dscp & DSCP_MASK;
772 tsw->green_dscp = DSCP_AF11;
773 if (tsw->yellow_action.tca_code == TCACODE_MARK)
774 tsw->yellow_dscp = tsw->yellow_action.tca_dscp & DSCP_MASK;
776 tsw->yellow_dscp = DSCP_AF12;
777 if (tsw->red_action.tca_code == TCACODE_MARK)
778 tsw->red_dscp = tsw->red_action.tca_dscp & DSCP_MASK;
780 tsw->red_dscp = DSCP_AF13;
782 /* convert rates from bits/sec to bytes/sec */
783 tsw->cmtd_rate = cmtd_rate / 8;
784 tsw->peak_rate = peak_rate / 8;
787 /* timewin is converted from msec to machine clock unit */
788 tsw->timewin = (u_int64_t)machclk_freq * avg_interval / 1000;
797 if (tsw->cdnrblk.cb_ref > 0)
800 tca_invalidate_action(&tsw->green_action);
801 tca_invalidate_action(&tsw->yellow_action);
802 tca_invalidate_action(&tsw->red_action);
808 static struct tc_action *
809 tswtcm_input(cb, pktinfo)
810 struct cdnr_block *cb;
811 struct cdnr_pktinfo *pktinfo;
813 struct tswtcm *tsw = (struct tswtcm *)cb;
816 u_int64_t interval, now, tmp;
821 len = pktinfo->pkt_len;
822 now = read_machclk();
824 interval = now - tsw->t_front;
826 * calculate average rate:
827 * avg = (avg * timewin + pkt_len)/(timewin + interval)
828 * pkt_len needs to be multiplied by machclk_freq in order to
830 * note: when avg_rate (bytes/sec) and timewin (machclk unit) are
831 * less than 32 bits, the following 64-bit operation has enough
834 tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
835 + (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
836 tsw->avg_rate = avg_rate = (u_int32_t)tmp;
842 if (avg_rate > tsw->cmtd_rate) {
843 u_int32_t randval = arc4random() % avg_rate;
845 if (avg_rate > tsw->peak_rate) {
846 if (randval < avg_rate - tsw->peak_rate) {
848 pktinfo->pkt_dscp = tsw->red_dscp;
849 PKTCNTR_ADD(&tsw->red_cnt, len);
850 return (&tsw->red_action);
851 } else if (randval < avg_rate - tsw->cmtd_rate)
854 /* peak_rate >= avg_rate > cmtd_rate */
855 if (randval < avg_rate - tsw->cmtd_rate) {
857 pktinfo->pkt_dscp = tsw->yellow_dscp;
858 PKTCNTR_ADD(&tsw->yellow_cnt, len);
859 return (&tsw->yellow_action);
865 pktinfo->pkt_dscp = tsw->green_dscp;
866 PKTCNTR_ADD(&tsw->green_cnt, len);
867 return (&tsw->green_action);
874 cdnrcmd_if_attach(ifname)
878 struct top_cdnr *top;
880 if ((ifp = ifunit(ifname)) == NULL)
883 if (ifp->if_snd.altq_cdnr != NULL)
886 if ((top = top_create(&ifp->if_snd)) == NULL)
892 cdnrcmd_if_detach(ifname)
895 struct top_cdnr *top;
897 if ((top = tcb_lookup(ifname)) == NULL)
900 return top_destroy(top);
904 cdnrcmd_add_element(ap)
905 struct cdnr_add_element *ap;
907 struct top_cdnr *top;
908 struct cdnr_block *cb;
910 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
913 cb = element_create(top, &ap->action);
916 /* return a class handle to the user */
917 ap->cdnr_handle = cdnr_cb2handle(cb);
922 cdnrcmd_delete_element(ap)
923 struct cdnr_delete_element *ap;
925 struct top_cdnr *top;
926 struct cdnr_block *cb;
928 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
931 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
934 if (cb->cb_type != TCETYPE_ELEMENT)
935 return generic_element_destroy(cb);
937 return element_destroy(cb);
941 cdnrcmd_add_filter(ap)
942 struct cdnr_add_filter *ap;
944 struct top_cdnr *top;
945 struct cdnr_block *cb;
947 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
950 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
953 return acc_add_filter(&top->tc_classifier, &ap->filter,
954 cb, &ap->filter_handle);
958 cdnrcmd_delete_filter(ap)
959 struct cdnr_delete_filter *ap;
961 struct top_cdnr *top;
963 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
966 return acc_delete_filter(&top->tc_classifier, ap->filter_handle);
971 struct cdnr_add_tbmeter *ap;
973 struct top_cdnr *top;
976 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
979 tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
982 /* return a class handle to the user */
983 ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
988 cdnrcmd_modify_tbm(ap)
989 struct cdnr_modify_tbmeter *ap;
993 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
996 tb_import_profile(&tbm->tb, &ap->profile);
1002 cdnrcmd_tbm_stats(ap)
1003 struct cdnr_tbmeter_stats *ap;
1005 struct tbmeter *tbm;
1007 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1010 ap->in_cnt = tbm->in_cnt;
1011 ap->out_cnt = tbm->out_cnt;
1017 cdnrcmd_add_trtcm(ap)
1018 struct cdnr_add_trtcm *ap;
1020 struct top_cdnr *top;
1023 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1026 tcm = trtcm_create(top, &ap->cmtd_profile, &ap->peak_profile,
1027 &ap->green_action, &ap->yellow_action,
1028 &ap->red_action, ap->coloraware);
1032 /* return a class handle to the user */
1033 ap->cdnr_handle = cdnr_cb2handle(&tcm->cdnrblk);
1038 cdnrcmd_modify_trtcm(ap)
1039 struct cdnr_modify_trtcm *ap;
1043 if ((tcm = (struct trtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1046 tb_import_profile(&tcm->cmtd_tb, &ap->cmtd_profile);
1047 tb_import_profile(&tcm->peak_tb, &ap->peak_profile);
1053 cdnrcmd_tcm_stats(ap)
1054 struct cdnr_tcm_stats *ap;
1056 struct cdnr_block *cb;
1058 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1061 if (cb->cb_type == TCETYPE_TRTCM) {
1062 struct trtcm *tcm = (struct trtcm *)cb;
1064 ap->green_cnt = tcm->green_cnt;
1065 ap->yellow_cnt = tcm->yellow_cnt;
1066 ap->red_cnt = tcm->red_cnt;
1067 } else if (cb->cb_type == TCETYPE_TSWTCM) {
1068 struct tswtcm *tsw = (struct tswtcm *)cb;
1070 ap->green_cnt = tsw->green_cnt;
1071 ap->yellow_cnt = tsw->yellow_cnt;
1072 ap->red_cnt = tsw->red_cnt;
1080 cdnrcmd_add_tswtcm(ap)
1081 struct cdnr_add_tswtcm *ap;
1083 struct top_cdnr *top;
1086 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1089 if (ap->cmtd_rate > ap->peak_rate)
1092 tsw = tswtcm_create(top, ap->cmtd_rate, ap->peak_rate,
1093 ap->avg_interval, &ap->green_action,
1094 &ap->yellow_action, &ap->red_action);
1098 /* return a class handle to the user */
1099 ap->cdnr_handle = cdnr_cb2handle(&tsw->cdnrblk);
1104 cdnrcmd_modify_tswtcm(ap)
1105 struct cdnr_modify_tswtcm *ap;
1109 if ((tsw = (struct tswtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1112 if (ap->cmtd_rate > ap->peak_rate)
1115 /* convert rates from bits/sec to bytes/sec */
1116 tsw->cmtd_rate = ap->cmtd_rate / 8;
1117 tsw->peak_rate = ap->peak_rate / 8;
1120 /* timewin is converted from msec to machine clock unit */
1121 tsw->timewin = (u_int64_t)machclk_freq * ap->avg_interval / 1000;
1127 cdnrcmd_get_stats(ap)
1128 struct cdnr_get_stats *ap;
1130 struct top_cdnr *top;
1131 struct cdnr_block *cb;
1132 struct tbmeter *tbm;
1135 struct tce_stats tce, *usp;
1136 int error, n, nskip, nelements;
1138 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1141 /* copy action stats */
1142 bcopy(top->tc_cnts, ap->cnts, sizeof(ap->cnts));
1144 /* stats for each element */
1145 nelements = ap->nelements;
1146 usp = ap->tce_stats;
1147 if (nelements <= 0 || usp == NULL)
1152 LIST_FOREACH(cb, &top->tc_elements, cb_next) {
1158 bzero(&tce, sizeof(tce));
1159 tce.tce_handle = cb->cb_handle;
1160 tce.tce_type = cb->cb_type;
1161 switch (cb->cb_type) {
1162 case TCETYPE_TBMETER:
1163 tbm = (struct tbmeter *)cb;
1164 tce.tce_cnts[0] = tbm->in_cnt;
1165 tce.tce_cnts[1] = tbm->out_cnt;
1168 tcm = (struct trtcm *)cb;
1169 tce.tce_cnts[0] = tcm->green_cnt;
1170 tce.tce_cnts[1] = tcm->yellow_cnt;
1171 tce.tce_cnts[2] = tcm->red_cnt;
1173 case TCETYPE_TSWTCM:
1174 tsw = (struct tswtcm *)cb;
1175 tce.tce_cnts[0] = tsw->green_cnt;
1176 tce.tce_cnts[1] = tsw->yellow_cnt;
1177 tce.tce_cnts[2] = tsw->red_cnt;
1183 if ((error = copyout((caddr_t)&tce, (caddr_t)usp++,
1187 if (++n == nelements)
1196 * conditioner device interface
1199 cdnropen(dev, flag, fmt, p)
1202 #if (__FreeBSD_version > 500000)
1208 if (machclk_freq == 0)
1211 if (machclk_freq == 0) {
1212 printf("cdnr: no cpu clock available!\n");
1216 /* everything will be done when the queueing scheme is attached. */
1221 cdnrclose(dev, flag, fmt, p)
1224 #if (__FreeBSD_version > 500000)
1230 struct top_cdnr *top;
1233 while ((top = LIST_FIRST(&tcb_list)) != NULL) {
1235 err = top_destroy(top);
1236 if (err != 0 && error == 0)
1245 cdnrioctl(dev, cmd, addr, flag, p)
1250 #if (__FreeBSD_version > 500000)
1256 struct top_cdnr *top;
1257 struct cdnr_interface *ifacep;
1260 /* check super-user privilege */
1265 #if (__FreeBSD_version > 700000)
1266 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1267 #elsif (__FreeBSD_version > 400000)
1268 if ((error = suser(p)) != 0)
1270 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1283 case CDNR_IF_ATTACH:
1284 ifacep = (struct cdnr_interface *)addr;
1285 error = cdnrcmd_if_attach(ifacep->cdnr_ifname);
1288 case CDNR_IF_DETACH:
1289 ifacep = (struct cdnr_interface *)addr;
1290 error = cdnrcmd_if_detach(ifacep->cdnr_ifname);
1295 ifacep = (struct cdnr_interface *)addr;
1296 if ((top = tcb_lookup(ifacep->cdnr_ifname)) == NULL) {
1304 ALTQ_SET_CNDTNING(top->tc_ifq);
1305 if (altq_input == NULL)
1306 altq_input = altq_cdnr_input;
1310 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
1311 LIST_FOREACH(top, &tcb_list, tc_next)
1312 if (ALTQ_IS_CNDTNING(top->tc_ifq))
1321 error = cdnrcmd_add_element((struct cdnr_add_element *)addr);
1325 error = cdnrcmd_delete_element((struct cdnr_delete_element *)addr);
1329 error = cdnrcmd_add_tbm((struct cdnr_add_tbmeter *)addr);
1333 error = cdnrcmd_modify_tbm((struct cdnr_modify_tbmeter *)addr);
1336 case CDNR_TBM_STATS:
1337 error = cdnrcmd_tbm_stats((struct cdnr_tbmeter_stats *)addr);
1341 error = cdnrcmd_add_trtcm((struct cdnr_add_trtcm *)addr);
1345 error = cdnrcmd_modify_trtcm((struct cdnr_modify_trtcm *)addr);
1348 case CDNR_TCM_STATS:
1349 error = cdnrcmd_tcm_stats((struct cdnr_tcm_stats *)addr);
1352 case CDNR_ADD_FILTER:
1353 error = cdnrcmd_add_filter((struct cdnr_add_filter *)addr);
1356 case CDNR_DEL_FILTER:
1357 error = cdnrcmd_delete_filter((struct cdnr_delete_filter *)addr);
1361 error = cdnrcmd_get_stats((struct cdnr_get_stats *)addr);
1365 error = cdnrcmd_add_tswtcm((struct cdnr_add_tswtcm *)addr);
1369 error = cdnrcmd_modify_tswtcm((struct cdnr_modify_tswtcm *)addr);
1383 static struct altqsw cdnr_sw =
1384 {"cdnr", cdnropen, cdnrclose, cdnrioctl};
1386 ALTQ_MODULE(altq_cdnr, ALTQT_CDNR, &cdnr_sw);
1388 #endif /* KLD_MODULE */
1390 #endif /* ALTQ3_COMPAT */
1391 #endif /* ALTQ_CDNR */