2 /* $KAME: altq_cdnr.c,v 1.15 2005/04/13 03:44:24 suz 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__)
34 #include "opt_inet6.h"
36 #endif /* __FreeBSD__ || __NetBSD__ */
38 #include <sys/param.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/systm.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/queue.h>
50 #include <net/if_types.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
58 #include <altq/if_altq.h>
59 #include <altq/altq.h>
61 #include <altq/altq_conf.h>
63 #include <altq/altq_cdnr.h>
67 * diffserv traffic conditioning module
70 int altq_cdnr_enabled = 0;
72 /* traffic conditioner is enabled by ALTQ_CDNR option in opt_altq.h */
75 /* cdnr_list keeps all cdnr's allocated. */
76 static LIST_HEAD(, top_cdnr) tcb_list;
78 static int altq_cdnr_input(struct mbuf *, int);
79 static struct top_cdnr *tcb_lookup(char *ifname);
80 static struct cdnr_block *cdnr_handle2cb(u_long);
81 static u_long cdnr_cb2handle(struct cdnr_block *);
82 static void *cdnr_cballoc(struct top_cdnr *, int,
83 struct tc_action *(*)(struct cdnr_block *, struct cdnr_pktinfo *));
84 static void cdnr_cbdestroy(void *);
85 static int tca_verify_action(struct tc_action *);
86 static void tca_import_action(struct tc_action *, struct tc_action *);
87 static void tca_invalidate_action(struct tc_action *);
89 static int generic_element_destroy(struct cdnr_block *);
90 static struct top_cdnr *top_create(struct ifaltq *);
91 static int top_destroy(struct top_cdnr *);
92 static struct cdnr_block *element_create(struct top_cdnr *, struct tc_action *);
93 static int element_destroy(struct cdnr_block *);
94 static void tb_import_profile(struct tbe *, struct tb_profile *);
95 static struct tbmeter *tbm_create(struct top_cdnr *, struct tb_profile *,
96 struct tc_action *, struct tc_action *);
97 static int tbm_destroy(struct tbmeter *);
98 static struct tc_action *tbm_input(struct cdnr_block *, struct cdnr_pktinfo *);
99 static struct trtcm *trtcm_create(struct top_cdnr *,
100 struct tb_profile *, struct tb_profile *,
101 struct tc_action *, struct tc_action *, struct tc_action *,
103 static int trtcm_destroy(struct trtcm *);
104 static struct tc_action *trtcm_input(struct cdnr_block *, struct cdnr_pktinfo *);
105 static struct tswtcm *tswtcm_create(struct top_cdnr *,
106 u_int32_t, u_int32_t, u_int32_t,
107 struct tc_action *, struct tc_action *, struct tc_action *);
108 static int tswtcm_destroy(struct tswtcm *);
109 static struct tc_action *tswtcm_input(struct cdnr_block *, struct cdnr_pktinfo *);
111 static int cdnrcmd_if_attach(char *);
112 static int cdnrcmd_if_detach(char *);
113 static int cdnrcmd_add_element(struct cdnr_add_element *);
114 static int cdnrcmd_delete_element(struct cdnr_delete_element *);
115 static int cdnrcmd_add_filter(struct cdnr_add_filter *);
116 static int cdnrcmd_delete_filter(struct cdnr_delete_filter *);
117 static int cdnrcmd_add_tbm(struct cdnr_add_tbmeter *);
118 static int cdnrcmd_modify_tbm(struct cdnr_modify_tbmeter *);
119 static int cdnrcmd_tbm_stats(struct cdnr_tbmeter_stats *);
120 static int cdnrcmd_add_trtcm(struct cdnr_add_trtcm *);
121 static int cdnrcmd_modify_trtcm(struct cdnr_modify_trtcm *);
122 static int cdnrcmd_tcm_stats(struct cdnr_tcm_stats *);
123 static int cdnrcmd_add_tswtcm(struct cdnr_add_tswtcm *);
124 static int cdnrcmd_modify_tswtcm(struct cdnr_modify_tswtcm *);
125 static int cdnrcmd_get_stats(struct cdnr_get_stats *);
130 * top level input function called from ip_input.
131 * should be called before converting header fields to host-byte-order.
134 altq_cdnr_input(m, af)
136 int af; /* address family */
140 struct top_cdnr *top;
141 struct tc_action *tca;
142 struct cdnr_block *cb;
143 struct cdnr_pktinfo pktinfo;
145 ifp = m->m_pkthdr.rcvif;
146 if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
147 /* traffic conditioner is not enabled on this interface */
150 top = ifp->if_snd.altq_cdnr;
152 ip = mtod(m, struct ip *);
154 if (af == AF_INET6) {
157 flowlabel = ((struct ip6_hdr *)ip)->ip6_flow;
158 pktinfo.pkt_dscp = (ntohl(flowlabel) >> 20) & DSCP_MASK;
161 pktinfo.pkt_dscp = ip->ip_tos & DSCP_MASK;
162 pktinfo.pkt_len = m_pktlen(m);
166 cb = acc_classify(&top->tc_classifier, m, af);
168 tca = &cb->cb_action;
171 tca = &top->tc_block.cb_action;
174 PKTCNTR_ADD(&top->tc_cnts[tca->tca_code], pktinfo.pkt_len);
176 switch (tca->tca_code) {
186 if (af == AF_INET6) {
187 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
190 flowlabel = ntohl(ip6->ip6_flow);
191 flowlabel = (tca->tca_dscp << 20) |
192 (flowlabel & ~(DSCP_MASK << 20));
193 ip6->ip6_flow = htonl(flowlabel);
196 ip->ip_tos = tca->tca_dscp |
197 (ip->ip_tos & DSCP_CUMASK);
201 tca = (*cb->cb_input)(cb, &pktinfo);
210 static struct top_cdnr *
214 struct top_cdnr *top;
217 if ((ifp = ifunit(ifname)) != NULL)
218 LIST_FOREACH(top, &tcb_list, tc_next)
219 if (top->tc_ifq->altq_ifp == ifp)
224 static struct cdnr_block *
225 cdnr_handle2cb(handle)
228 struct cdnr_block *cb;
230 cb = (struct cdnr_block *)handle;
231 if (handle != ALIGN(cb))
234 if (cb == NULL || cb->cb_handle != handle)
241 struct cdnr_block *cb;
243 return (cb->cb_handle);
247 cdnr_cballoc(top, type, input_func)
248 struct top_cdnr *top;
250 struct tc_action *(*input_func)(struct cdnr_block *,
251 struct cdnr_pktinfo *);
253 struct cdnr_block *cb;
258 size = sizeof(struct top_cdnr);
260 case TCETYPE_ELEMENT:
261 size = sizeof(struct cdnr_block);
263 case TCETYPE_TBMETER:
264 size = sizeof(struct tbmeter);
267 size = sizeof(struct trtcm);
270 size = sizeof(struct tswtcm);
276 cb = malloc(size, M_DEVBUF, M_WAITOK);
284 cb->cb_handle = (u_long)cb;
286 cb->cb_top = (struct top_cdnr *)cb;
290 if (input_func != NULL) {
292 * if this cdnr has an action function,
293 * make tc_action to call itself.
295 cb->cb_action.tca_code = TCACODE_NEXT;
296 cb->cb_action.tca_next = cb;
297 cb->cb_input = input_func;
299 cb->cb_action.tca_code = TCACODE_NONE;
301 /* if this isn't top, register the element to the top level cdnr */
303 LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
309 cdnr_cbdestroy(cblock)
312 struct cdnr_block *cb = cblock;
314 /* delete filters belonging to this cdnr */
315 acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
317 /* remove from the top level cdnr */
318 if (cb->cb_top != cblock)
319 LIST_REMOVE(cb, cb_next);
325 * conditioner common destroy routine
328 generic_element_destroy(cb)
329 struct cdnr_block *cb;
333 switch (cb->cb_type) {
335 error = top_destroy((struct top_cdnr *)cb);
337 case TCETYPE_ELEMENT:
338 error = element_destroy(cb);
340 case TCETYPE_TBMETER:
341 error = tbm_destroy((struct tbmeter *)cb);
344 error = trtcm_destroy((struct trtcm *)cb);
347 error = tswtcm_destroy((struct tswtcm *)cb);
356 tca_verify_action(utca)
357 struct tc_action *utca;
359 switch (utca->tca_code) {
367 /* verify handle value */
368 if (cdnr_handle2cb(utca->tca_handle) == NULL)
376 /* should not be passed from a user */
383 tca_import_action(ktca, utca)
384 struct tc_action *ktca, *utca;
386 struct cdnr_block *cb;
389 if (ktca->tca_code == TCACODE_HANDLE) {
390 cb = cdnr_handle2cb(ktca->tca_handle);
392 ktca->tca_code = TCACODE_NONE;
395 ktca->tca_code = TCACODE_NEXT;
398 } else if (ktca->tca_code == TCACODE_MARK) {
399 ktca->tca_dscp &= DSCP_MASK;
405 tca_invalidate_action(tca)
406 struct tc_action *tca;
408 struct cdnr_block *cb;
410 if (tca->tca_code == TCACODE_NEXT) {
416 tca->tca_code = TCACODE_NONE;
420 * top level traffic conditioner
422 static struct top_cdnr *
426 struct top_cdnr *top;
428 if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
432 /* set default action for the top level conditioner */
433 top->tc_block.cb_action.tca_code = TCACODE_PASS;
435 LIST_INSERT_HEAD(&tcb_list, top, tc_next);
437 ifq->altq_cdnr = top;
444 struct top_cdnr *top;
446 struct cdnr_block *cb;
448 if (ALTQ_IS_CNDTNING(top->tc_ifq))
449 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
450 top->tc_ifq->altq_cdnr = NULL;
453 * destroy all the conditioner elements belonging to this interface
455 while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
456 while (cb != NULL && cb->cb_ref > 0)
457 cb = LIST_NEXT(cb, cb_next);
459 generic_element_destroy(cb);
462 LIST_REMOVE(top, tc_next);
466 /* if there is no active conditioner, remove the input hook */
467 if (altq_input != NULL) {
468 LIST_FOREACH(top, &tcb_list, tc_next)
469 if (ALTQ_IS_CNDTNING(top->tc_ifq))
479 * simple tc elements without input function (e.g., dropper and makers).
481 static struct cdnr_block *
482 element_create(top, action)
483 struct top_cdnr *top;
484 struct tc_action *action;
486 struct cdnr_block *cb;
488 if (tca_verify_action(action) < 0)
491 if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
494 tca_import_action(&cb->cb_action, action);
501 struct cdnr_block *cb;
506 tca_invalidate_action(&cb->cb_action);
513 * internal representation of token bucket parameters
514 * rate: byte_per_unittime << 32
515 * (((bits_per_sec) / 8) << 32) / machclk_freq
520 #define TB_SCALE(x) ((u_int64_t)(x) << TB_SHIFT)
521 #define TB_UNSCALE(x) ((x) >> TB_SHIFT)
524 tb_import_profile(tb, profile)
526 struct tb_profile *profile;
528 tb->rate = TB_SCALE(profile->rate / 8) / machclk_freq;
529 tb->depth = TB_SCALE(profile->depth);
531 tb->filluptime = tb->depth / tb->rate;
533 tb->filluptime = 0xffffffffffffffffLL;
534 tb->token = tb->depth;
535 tb->last = read_machclk();
539 * simple token bucket meter
541 static struct tbmeter *
542 tbm_create(top, profile, in_action, out_action)
543 struct top_cdnr *top;
544 struct tb_profile *profile;
545 struct tc_action *in_action, *out_action;
547 struct tbmeter *tbm = NULL;
549 if (tca_verify_action(in_action) < 0
550 || tca_verify_action(out_action) < 0)
553 if ((tbm = cdnr_cballoc(top, TCETYPE_TBMETER,
557 tb_import_profile(&tbm->tb, profile);
559 tca_import_action(&tbm->in_action, in_action);
560 tca_import_action(&tbm->out_action, out_action);
569 if (tbm->cdnrblk.cb_ref > 0)
572 tca_invalidate_action(&tbm->in_action);
573 tca_invalidate_action(&tbm->out_action);
579 static struct tc_action *
580 tbm_input(cb, pktinfo)
581 struct cdnr_block *cb;
582 struct cdnr_pktinfo *pktinfo;
584 struct tbmeter *tbm = (struct tbmeter *)cb;
586 u_int64_t interval, now;
588 len = TB_SCALE(pktinfo->pkt_len);
590 if (tbm->tb.token < len) {
591 now = read_machclk();
592 interval = now - tbm->tb.last;
593 if (interval >= tbm->tb.filluptime)
594 tbm->tb.token = tbm->tb.depth;
596 tbm->tb.token += interval * tbm->tb.rate;
597 if (tbm->tb.token > tbm->tb.depth)
598 tbm->tb.token = tbm->tb.depth;
603 if (tbm->tb.token < len) {
604 PKTCNTR_ADD(&tbm->out_cnt, pktinfo->pkt_len);
605 return (&tbm->out_action);
608 tbm->tb.token -= len;
609 PKTCNTR_ADD(&tbm->in_cnt, pktinfo->pkt_len);
610 return (&tbm->in_action);
614 * two rate three color marker
615 * as described in draft-heinanen-diffserv-trtcm-01.txt
617 static struct trtcm *
618 trtcm_create(top, cmtd_profile, peak_profile,
619 green_action, yellow_action, red_action, coloraware)
620 struct top_cdnr *top;
621 struct tb_profile *cmtd_profile, *peak_profile;
622 struct tc_action *green_action, *yellow_action, *red_action;
625 struct trtcm *tcm = NULL;
627 if (tca_verify_action(green_action) < 0
628 || tca_verify_action(yellow_action) < 0
629 || tca_verify_action(red_action) < 0)
632 if ((tcm = cdnr_cballoc(top, TCETYPE_TRTCM,
633 trtcm_input)) == NULL)
636 tb_import_profile(&tcm->cmtd_tb, cmtd_profile);
637 tb_import_profile(&tcm->peak_tb, peak_profile);
639 tca_import_action(&tcm->green_action, green_action);
640 tca_import_action(&tcm->yellow_action, yellow_action);
641 tca_import_action(&tcm->red_action, red_action);
643 /* set dscps to use */
644 if (tcm->green_action.tca_code == TCACODE_MARK)
645 tcm->green_dscp = tcm->green_action.tca_dscp & DSCP_MASK;
647 tcm->green_dscp = DSCP_AF11;
648 if (tcm->yellow_action.tca_code == TCACODE_MARK)
649 tcm->yellow_dscp = tcm->yellow_action.tca_dscp & DSCP_MASK;
651 tcm->yellow_dscp = DSCP_AF12;
652 if (tcm->red_action.tca_code == TCACODE_MARK)
653 tcm->red_dscp = tcm->red_action.tca_dscp & DSCP_MASK;
655 tcm->red_dscp = DSCP_AF13;
657 tcm->coloraware = coloraware;
666 if (tcm->cdnrblk.cb_ref > 0)
669 tca_invalidate_action(&tcm->green_action);
670 tca_invalidate_action(&tcm->yellow_action);
671 tca_invalidate_action(&tcm->red_action);
677 static struct tc_action *
678 trtcm_input(cb, pktinfo)
679 struct cdnr_block *cb;
680 struct cdnr_pktinfo *pktinfo;
682 struct trtcm *tcm = (struct trtcm *)cb;
684 u_int64_t interval, now;
687 len = TB_SCALE(pktinfo->pkt_len);
688 if (tcm->coloraware) {
689 color = pktinfo->pkt_dscp;
690 if (color != tcm->yellow_dscp && color != tcm->red_dscp)
691 color = tcm->green_dscp;
693 /* if color-blind, precolor it as green */
694 color = tcm->green_dscp;
697 now = read_machclk();
698 if (tcm->cmtd_tb.token < len) {
699 interval = now - tcm->cmtd_tb.last;
700 if (interval >= tcm->cmtd_tb.filluptime)
701 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
703 tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
704 if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
705 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
707 tcm->cmtd_tb.last = now;
709 if (tcm->peak_tb.token < len) {
710 interval = now - tcm->peak_tb.last;
711 if (interval >= tcm->peak_tb.filluptime)
712 tcm->peak_tb.token = tcm->peak_tb.depth;
714 tcm->peak_tb.token += interval * tcm->peak_tb.rate;
715 if (tcm->peak_tb.token > tcm->peak_tb.depth)
716 tcm->peak_tb.token = tcm->peak_tb.depth;
718 tcm->peak_tb.last = now;
721 if (color == tcm->red_dscp || tcm->peak_tb.token < len) {
722 pktinfo->pkt_dscp = tcm->red_dscp;
723 PKTCNTR_ADD(&tcm->red_cnt, pktinfo->pkt_len);
724 return (&tcm->red_action);
727 if (color == tcm->yellow_dscp || tcm->cmtd_tb.token < len) {
728 pktinfo->pkt_dscp = tcm->yellow_dscp;
729 tcm->peak_tb.token -= len;
730 PKTCNTR_ADD(&tcm->yellow_cnt, pktinfo->pkt_len);
731 return (&tcm->yellow_action);
734 pktinfo->pkt_dscp = tcm->green_dscp;
735 tcm->cmtd_tb.token -= len;
736 tcm->peak_tb.token -= len;
737 PKTCNTR_ADD(&tcm->green_cnt, pktinfo->pkt_len);
738 return (&tcm->green_action);
742 * time sliding window three color marker
743 * as described in draft-fang-diffserv-tc-tswtcm-00.txt
745 static struct tswtcm *
746 tswtcm_create(top, cmtd_rate, peak_rate, avg_interval,
747 green_action, yellow_action, red_action)
748 struct top_cdnr *top;
749 u_int32_t cmtd_rate, peak_rate, avg_interval;
750 struct tc_action *green_action, *yellow_action, *red_action;
754 if (tca_verify_action(green_action) < 0
755 || tca_verify_action(yellow_action) < 0
756 || tca_verify_action(red_action) < 0)
759 if ((tsw = cdnr_cballoc(top, TCETYPE_TSWTCM,
760 tswtcm_input)) == NULL)
763 tca_import_action(&tsw->green_action, green_action);
764 tca_import_action(&tsw->yellow_action, yellow_action);
765 tca_import_action(&tsw->red_action, red_action);
767 /* set dscps to use */
768 if (tsw->green_action.tca_code == TCACODE_MARK)
769 tsw->green_dscp = tsw->green_action.tca_dscp & DSCP_MASK;
771 tsw->green_dscp = DSCP_AF11;
772 if (tsw->yellow_action.tca_code == TCACODE_MARK)
773 tsw->yellow_dscp = tsw->yellow_action.tca_dscp & DSCP_MASK;
775 tsw->yellow_dscp = DSCP_AF12;
776 if (tsw->red_action.tca_code == TCACODE_MARK)
777 tsw->red_dscp = tsw->red_action.tca_dscp & DSCP_MASK;
779 tsw->red_dscp = DSCP_AF13;
781 /* convert rates from bits/sec to bytes/sec */
782 tsw->cmtd_rate = cmtd_rate / 8;
783 tsw->peak_rate = peak_rate / 8;
786 /* timewin is converted from msec to machine clock unit */
787 tsw->timewin = (u_int64_t)machclk_freq * avg_interval / 1000;
796 if (tsw->cdnrblk.cb_ref > 0)
799 tca_invalidate_action(&tsw->green_action);
800 tca_invalidate_action(&tsw->yellow_action);
801 tca_invalidate_action(&tsw->red_action);
807 static struct tc_action *
808 tswtcm_input(cb, pktinfo)
809 struct cdnr_block *cb;
810 struct cdnr_pktinfo *pktinfo;
812 struct tswtcm *tsw = (struct tswtcm *)cb;
815 u_int64_t interval, now, tmp;
820 len = pktinfo->pkt_len;
821 now = read_machclk();
823 interval = now - tsw->t_front;
825 * calculate average rate:
826 * avg = (avg * timewin + pkt_len)/(timewin + interval)
827 * pkt_len needs to be multiplied by machclk_freq in order to
829 * note: when avg_rate (bytes/sec) and timewin (machclk unit) are
830 * less than 32 bits, the following 64-bit operation has enough
833 tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
834 + (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
835 tsw->avg_rate = avg_rate = (u_int32_t)tmp;
841 if (avg_rate > tsw->cmtd_rate) {
842 u_int32_t randval = arc4random() % avg_rate;
844 if (avg_rate > tsw->peak_rate) {
845 if (randval < avg_rate - tsw->peak_rate) {
847 pktinfo->pkt_dscp = tsw->red_dscp;
848 PKTCNTR_ADD(&tsw->red_cnt, len);
849 return (&tsw->red_action);
850 } else if (randval < avg_rate - tsw->cmtd_rate)
853 /* peak_rate >= avg_rate > cmtd_rate */
854 if (randval < avg_rate - tsw->cmtd_rate) {
856 pktinfo->pkt_dscp = tsw->yellow_dscp;
857 PKTCNTR_ADD(&tsw->yellow_cnt, len);
858 return (&tsw->yellow_action);
864 pktinfo->pkt_dscp = tsw->green_dscp;
865 PKTCNTR_ADD(&tsw->green_cnt, len);
866 return (&tsw->green_action);
873 cdnrcmd_if_attach(ifname)
877 struct top_cdnr *top;
879 if ((ifp = ifunit(ifname)) == NULL)
882 if (ifp->if_snd.altq_cdnr != NULL)
885 if ((top = top_create(&ifp->if_snd)) == NULL)
891 cdnrcmd_if_detach(ifname)
894 struct top_cdnr *top;
896 if ((top = tcb_lookup(ifname)) == NULL)
899 return top_destroy(top);
903 cdnrcmd_add_element(ap)
904 struct cdnr_add_element *ap;
906 struct top_cdnr *top;
907 struct cdnr_block *cb;
909 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
912 cb = element_create(top, &ap->action);
915 /* return a class handle to the user */
916 ap->cdnr_handle = cdnr_cb2handle(cb);
921 cdnrcmd_delete_element(ap)
922 struct cdnr_delete_element *ap;
924 struct top_cdnr *top;
925 struct cdnr_block *cb;
927 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
930 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
933 if (cb->cb_type != TCETYPE_ELEMENT)
934 return generic_element_destroy(cb);
936 return element_destroy(cb);
940 cdnrcmd_add_filter(ap)
941 struct cdnr_add_filter *ap;
943 struct top_cdnr *top;
944 struct cdnr_block *cb;
946 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
949 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
952 return acc_add_filter(&top->tc_classifier, &ap->filter,
953 cb, &ap->filter_handle);
957 cdnrcmd_delete_filter(ap)
958 struct cdnr_delete_filter *ap;
960 struct top_cdnr *top;
962 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
965 return acc_delete_filter(&top->tc_classifier, ap->filter_handle);
970 struct cdnr_add_tbmeter *ap;
972 struct top_cdnr *top;
975 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
978 tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
981 /* return a class handle to the user */
982 ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
987 cdnrcmd_modify_tbm(ap)
988 struct cdnr_modify_tbmeter *ap;
992 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
995 tb_import_profile(&tbm->tb, &ap->profile);
1001 cdnrcmd_tbm_stats(ap)
1002 struct cdnr_tbmeter_stats *ap;
1004 struct tbmeter *tbm;
1006 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1009 ap->in_cnt = tbm->in_cnt;
1010 ap->out_cnt = tbm->out_cnt;
1016 cdnrcmd_add_trtcm(ap)
1017 struct cdnr_add_trtcm *ap;
1019 struct top_cdnr *top;
1022 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1025 tcm = trtcm_create(top, &ap->cmtd_profile, &ap->peak_profile,
1026 &ap->green_action, &ap->yellow_action,
1027 &ap->red_action, ap->coloraware);
1031 /* return a class handle to the user */
1032 ap->cdnr_handle = cdnr_cb2handle(&tcm->cdnrblk);
1037 cdnrcmd_modify_trtcm(ap)
1038 struct cdnr_modify_trtcm *ap;
1042 if ((tcm = (struct trtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1045 tb_import_profile(&tcm->cmtd_tb, &ap->cmtd_profile);
1046 tb_import_profile(&tcm->peak_tb, &ap->peak_profile);
1052 cdnrcmd_tcm_stats(ap)
1053 struct cdnr_tcm_stats *ap;
1055 struct cdnr_block *cb;
1057 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1060 if (cb->cb_type == TCETYPE_TRTCM) {
1061 struct trtcm *tcm = (struct trtcm *)cb;
1063 ap->green_cnt = tcm->green_cnt;
1064 ap->yellow_cnt = tcm->yellow_cnt;
1065 ap->red_cnt = tcm->red_cnt;
1066 } else if (cb->cb_type == TCETYPE_TSWTCM) {
1067 struct tswtcm *tsw = (struct tswtcm *)cb;
1069 ap->green_cnt = tsw->green_cnt;
1070 ap->yellow_cnt = tsw->yellow_cnt;
1071 ap->red_cnt = tsw->red_cnt;
1079 cdnrcmd_add_tswtcm(ap)
1080 struct cdnr_add_tswtcm *ap;
1082 struct top_cdnr *top;
1085 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1088 if (ap->cmtd_rate > ap->peak_rate)
1091 tsw = tswtcm_create(top, ap->cmtd_rate, ap->peak_rate,
1092 ap->avg_interval, &ap->green_action,
1093 &ap->yellow_action, &ap->red_action);
1097 /* return a class handle to the user */
1098 ap->cdnr_handle = cdnr_cb2handle(&tsw->cdnrblk);
1103 cdnrcmd_modify_tswtcm(ap)
1104 struct cdnr_modify_tswtcm *ap;
1108 if ((tsw = (struct tswtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1111 if (ap->cmtd_rate > ap->peak_rate)
1114 /* convert rates from bits/sec to bytes/sec */
1115 tsw->cmtd_rate = ap->cmtd_rate / 8;
1116 tsw->peak_rate = ap->peak_rate / 8;
1119 /* timewin is converted from msec to machine clock unit */
1120 tsw->timewin = (u_int64_t)machclk_freq * ap->avg_interval / 1000;
1126 cdnrcmd_get_stats(ap)
1127 struct cdnr_get_stats *ap;
1129 struct top_cdnr *top;
1130 struct cdnr_block *cb;
1131 struct tbmeter *tbm;
1134 struct tce_stats tce, *usp;
1135 int error, n, nskip, nelements;
1137 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1140 /* copy action stats */
1141 bcopy(top->tc_cnts, ap->cnts, sizeof(ap->cnts));
1143 /* stats for each element */
1144 nelements = ap->nelements;
1145 usp = ap->tce_stats;
1146 if (nelements <= 0 || usp == NULL)
1151 LIST_FOREACH(cb, &top->tc_elements, cb_next) {
1157 bzero(&tce, sizeof(tce));
1158 tce.tce_handle = cb->cb_handle;
1159 tce.tce_type = cb->cb_type;
1160 switch (cb->cb_type) {
1161 case TCETYPE_TBMETER:
1162 tbm = (struct tbmeter *)cb;
1163 tce.tce_cnts[0] = tbm->in_cnt;
1164 tce.tce_cnts[1] = tbm->out_cnt;
1167 tcm = (struct trtcm *)cb;
1168 tce.tce_cnts[0] = tcm->green_cnt;
1169 tce.tce_cnts[1] = tcm->yellow_cnt;
1170 tce.tce_cnts[2] = tcm->red_cnt;
1172 case TCETYPE_TSWTCM:
1173 tsw = (struct tswtcm *)cb;
1174 tce.tce_cnts[0] = tsw->green_cnt;
1175 tce.tce_cnts[1] = tsw->yellow_cnt;
1176 tce.tce_cnts[2] = tsw->red_cnt;
1182 if ((error = copyout((caddr_t)&tce, (caddr_t)usp++,
1186 if (++n == nelements)
1195 * conditioner device interface
1198 cdnropen(dev, flag, fmt, p)
1201 #if (__FreeBSD_version > 500000)
1207 if (machclk_freq == 0)
1210 if (machclk_freq == 0) {
1211 printf("cdnr: no cpu clock available!\n");
1215 /* everything will be done when the queueing scheme is attached. */
1220 cdnrclose(dev, flag, fmt, p)
1223 #if (__FreeBSD_version > 500000)
1229 struct top_cdnr *top;
1232 while ((top = LIST_FIRST(&tcb_list)) != NULL) {
1234 err = top_destroy(top);
1235 if (err != 0 && error == 0)
1244 cdnrioctl(dev, cmd, addr, flag, p)
1249 #if (__FreeBSD_version > 500000)
1255 struct top_cdnr *top;
1256 struct cdnr_interface *ifacep;
1259 /* check super-user privilege */
1264 #if (__FreeBSD_version > 700000)
1265 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1266 #elsif (__FreeBSD_version > 400000)
1267 if ((error = suser(p)) != 0)
1269 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1282 case CDNR_IF_ATTACH:
1283 ifacep = (struct cdnr_interface *)addr;
1284 error = cdnrcmd_if_attach(ifacep->cdnr_ifname);
1287 case CDNR_IF_DETACH:
1288 ifacep = (struct cdnr_interface *)addr;
1289 error = cdnrcmd_if_detach(ifacep->cdnr_ifname);
1294 ifacep = (struct cdnr_interface *)addr;
1295 if ((top = tcb_lookup(ifacep->cdnr_ifname)) == NULL) {
1303 ALTQ_SET_CNDTNING(top->tc_ifq);
1304 if (altq_input == NULL)
1305 altq_input = altq_cdnr_input;
1309 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
1310 LIST_FOREACH(top, &tcb_list, tc_next)
1311 if (ALTQ_IS_CNDTNING(top->tc_ifq))
1320 error = cdnrcmd_add_element((struct cdnr_add_element *)addr);
1324 error = cdnrcmd_delete_element((struct cdnr_delete_element *)addr);
1328 error = cdnrcmd_add_tbm((struct cdnr_add_tbmeter *)addr);
1332 error = cdnrcmd_modify_tbm((struct cdnr_modify_tbmeter *)addr);
1335 case CDNR_TBM_STATS:
1336 error = cdnrcmd_tbm_stats((struct cdnr_tbmeter_stats *)addr);
1340 error = cdnrcmd_add_trtcm((struct cdnr_add_trtcm *)addr);
1344 error = cdnrcmd_modify_trtcm((struct cdnr_modify_trtcm *)addr);
1347 case CDNR_TCM_STATS:
1348 error = cdnrcmd_tcm_stats((struct cdnr_tcm_stats *)addr);
1351 case CDNR_ADD_FILTER:
1352 error = cdnrcmd_add_filter((struct cdnr_add_filter *)addr);
1355 case CDNR_DEL_FILTER:
1356 error = cdnrcmd_delete_filter((struct cdnr_delete_filter *)addr);
1360 error = cdnrcmd_get_stats((struct cdnr_get_stats *)addr);
1364 error = cdnrcmd_add_tswtcm((struct cdnr_add_tswtcm *)addr);
1368 error = cdnrcmd_modify_tswtcm((struct cdnr_modify_tswtcm *)addr);
1382 static struct altqsw cdnr_sw =
1383 {"cdnr", cdnropen, cdnrclose, cdnrioctl};
1385 ALTQ_MODULE(altq_cdnr, ALTQT_CDNR, &cdnr_sw);
1387 #endif /* KLD_MODULE */
1389 #endif /* ALTQ3_COMPAT */
1390 #endif /* ALTQ_CDNR */