altq_cdnr.c revision 1.1 1 /* $KAME: altq_cdnr.c,v 1.8 2000/12/14 08:12:45 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1999-2000
5 * Sony Computer Science Laboratories Inc. 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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #if defined(__FreeBSD__) || defined(__NetBSD__)
30 #include "opt_altq.h"
31 #if (__FreeBSD__ != 2)
32 #include "opt_inet.h"
33 #ifdef __FreeBSD__
34 #include "opt_inet6.h"
35 #endif
36 #endif
37 #endif /* __FreeBSD__ || __NetBSD__ */
38
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #ifdef INET6
56 #include <netinet/ip6.h>
57 #endif
58
59 #include <altq/altq.h>
60 #include <altq/altq_conf.h>
61 #include <altq/altq_cdnr.h>
62
63 /*
64 * diffserv traffic conditioning module
65 */
66
67 int altq_cdnr_enabled = 0;
68
69 /* traffic conditioner is enabled by ALTQ_CDNR option in opt_altq.h */
70 #ifdef ALTQ_CDNR
71
72 /* cdnr_list keeps all cdnr's allocated. */
73 static LIST_HEAD(, top_cdnr) tcb_list;
74
75 int cdnropen __P((dev_t, int, int, struct proc *));
76 int cdnrclose __P((dev_t, int, int, struct proc *));
77 int cdnrioctl __P((dev_t, ioctlcmd_t, caddr_t, int, struct proc *));
78
79 static int altq_cdnr_input __P((struct mbuf *, int));
80 static struct top_cdnr *tcb_lookup __P((char *ifname));
81 static struct cdnr_block *cdnr_handle2cb __P((u_long));
82 static u_long cdnr_cb2handle __P((struct cdnr_block *));
83 static void *cdnr_cballoc __P((struct top_cdnr *, int,
84 struct tc_action *(*)(struct cdnr_block *, struct cdnr_pktinfo *)));
85 static void cdnr_cbdestroy __P((void *));
86 static int tca_verify_action __P((struct tc_action *));
87 static void tca_import_action __P((struct tc_action *, struct tc_action *));
88 static void tca_invalidate_action __P((struct tc_action *));
89
90 static int generic_element_destroy __P((struct cdnr_block *));
91 static struct top_cdnr *top_create __P((struct ifaltq *));
92 static int top_destroy __P((struct top_cdnr *));
93 static struct cdnr_block *element_create __P((struct top_cdnr *,
94 struct tc_action *));
95 static int element_destroy __P((struct cdnr_block *));
96 static void tb_import_profile __P((struct tbe *, struct tb_profile *));
97 static struct tbmeter *tbm_create __P((struct top_cdnr *, struct tb_profile *,
98 struct tc_action *, struct tc_action *));
99 static int tbm_destroy __P((struct tbmeter *));
100 static struct tc_action *tbm_input __P((struct cdnr_block *,
101 struct cdnr_pktinfo *));
102 static struct trtcm *trtcm_create __P((struct top_cdnr *,
103 struct tb_profile *, struct tb_profile *,
104 struct tc_action *, struct tc_action *, struct tc_action *,
105 int));
106 static int trtcm_destroy __P((struct trtcm *));
107 static struct tc_action *trtcm_input __P((struct cdnr_block *,
108 struct cdnr_pktinfo *));
109 static struct tswtcm *tswtcm_create __P((struct top_cdnr *,
110 u_int32_t, u_int32_t, u_int32_t,
111 struct tc_action *, struct tc_action *, struct tc_action *));
112 static int tswtcm_destroy __P((struct tswtcm *));
113 static struct tc_action *tswtcm_input __P((struct cdnr_block *,
114 struct cdnr_pktinfo *));
115
116 static int cdnrcmd_if_attach __P((char *));
117 static int cdnrcmd_if_detach __P((char *));
118 static int cdnrcmd_add_element __P((struct cdnr_add_element *));
119 static int cdnrcmd_delete_element __P((struct cdnr_delete_element *));
120 static int cdnrcmd_add_filter __P((struct cdnr_add_filter *));
121 static int cdnrcmd_delete_filter __P((struct cdnr_delete_filter *));
122 static int cdnrcmd_add_tbm __P((struct cdnr_add_tbmeter *));
123 static int cdnrcmd_modify_tbm __P((struct cdnr_modify_tbmeter *));
124 static int cdnrcmd_tbm_stats __P((struct cdnr_tbmeter_stats *));
125 static int cdnrcmd_add_trtcm __P((struct cdnr_add_trtcm *));
126 static int cdnrcmd_modify_trtcm __P((struct cdnr_modify_trtcm *));
127 static int cdnrcmd_tcm_stats __P((struct cdnr_tcm_stats *));
128 static int cdnrcmd_add_tswtcm __P((struct cdnr_add_tswtcm *));
129 static int cdnrcmd_modify_tswtcm __P((struct cdnr_modify_tswtcm *));
130 static int cdnrcmd_get_stats __P((struct cdnr_get_stats *));
131
132 /*
133 * top level input function called from ip_input.
134 * should be called before converting header fields to host-byte-order.
135 */
136 int
137 altq_cdnr_input(m, af)
138 struct mbuf *m;
139 int af; /* address family */
140 {
141 struct ifnet *ifp;
142 struct ip *ip;
143 struct top_cdnr *top;
144 struct tc_action *tca;
145 struct cdnr_block *cb;
146 struct cdnr_pktinfo pktinfo;
147
148 ifp = m->m_pkthdr.rcvif;
149 if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
150 /* traffic conditioner is not enabled on this interface */
151 return (1);
152
153 top = ifp->if_snd.altq_cdnr;
154
155 ip = mtod(m, struct ip *);
156 #ifdef INET6
157 if (af == AF_INET6) {
158 u_int32_t flowlabel;
159
160 flowlabel = ((struct ip6_hdr *)ip)->ip6_flow;
161 pktinfo.pkt_dscp = (ntohl(flowlabel) >> 20) & DSCP_MASK;
162 } else
163 #endif
164 pktinfo.pkt_dscp = ip->ip_tos & DSCP_MASK;
165 pktinfo.pkt_len = m_pktlen(m);
166
167 tca = NULL;
168
169 cb = acc_classify(&top->tc_classifier, m, af);
170 if (cb != NULL)
171 tca = &cb->cb_action;
172
173 if (tca == NULL)
174 tca = &top->tc_block.cb_action;
175
176 while (1) {
177 PKTCNTR_ADD(&top->tc_cnts[tca->tca_code], pktinfo.pkt_len);
178
179 switch (tca->tca_code) {
180 case TCACODE_PASS:
181 return (1);
182 case TCACODE_DROP:
183 m_freem(m);
184 return (0);
185 case TCACODE_RETURN:
186 return (0);
187 case TCACODE_MARK:
188 #ifdef INET6
189 if (af == AF_INET6) {
190 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
191 u_int32_t flowlabel;
192
193 flowlabel = ntohl(ip6->ip6_flow);
194 flowlabel = (tca->tca_dscp << 20) |
195 (flowlabel & ~(DSCP_MASK << 20));
196 ip6->ip6_flow = htonl(flowlabel);
197 } else
198 #endif
199 ip->ip_tos = tca->tca_dscp |
200 (ip->ip_tos & DSCP_CUMASK);
201 return (1);
202 case TCACODE_NEXT:
203 cb = tca->tca_next;
204 tca = (*cb->cb_input)(cb, &pktinfo);
205 break;
206 case TCACODE_NONE:
207 default:
208 return (1);
209 }
210 }
211 }
212
213 static struct top_cdnr *
214 tcb_lookup(ifname)
215 char *ifname;
216 {
217 struct top_cdnr *top;
218 struct ifnet *ifp;
219
220 if ((ifp = ifunit(ifname)) != NULL)
221 LIST_FOREACH(top, &tcb_list, tc_next)
222 if (top->tc_ifq->altq_ifp == ifp)
223 return (top);
224 return (NULL);
225 }
226
227 static struct cdnr_block *
228 cdnr_handle2cb(handle)
229 u_long handle;
230 {
231 struct cdnr_block *cb;
232
233 cb = (struct cdnr_block *)handle;
234 if (handle != ALIGN(cb))
235 return (NULL);
236
237 if (cb == NULL || cb->cb_handle != handle)
238 return (NULL);
239 return (cb);
240 }
241
242 static u_long
243 cdnr_cb2handle(cb)
244 struct cdnr_block *cb;
245 {
246 return (cb->cb_handle);
247 }
248
249 static void *
250 cdnr_cballoc(top, type, input_func)
251 struct top_cdnr *top;
252 int type;
253 struct tc_action *(*input_func)(struct cdnr_block *,
254 struct cdnr_pktinfo *);
255 {
256 struct cdnr_block *cb;
257 int size;
258
259 switch (type) {
260 case TCETYPE_TOP:
261 size = sizeof(struct top_cdnr);
262 break;
263 case TCETYPE_ELEMENT:
264 size = sizeof(struct cdnr_block);
265 break;
266 case TCETYPE_TBMETER:
267 size = sizeof(struct tbmeter);
268 break;
269 case TCETYPE_TRTCM:
270 size = sizeof(struct trtcm);
271 break;
272 case TCETYPE_TSWTCM:
273 size = sizeof(struct tswtcm);
274 break;
275 default:
276 return (NULL);
277 }
278
279 MALLOC(cb, struct cdnr_block *, size, M_DEVBUF, M_WAITOK);
280 if (cb == NULL)
281 return (NULL);
282 bzero(cb, size);
283
284 cb->cb_len = size;
285 cb->cb_type = type;
286 cb->cb_ref = 0;
287 cb->cb_handle = (u_long)cb;
288 if (top == NULL)
289 cb->cb_top = (struct top_cdnr *)cb;
290 else
291 cb->cb_top = top;
292
293 if (input_func != NULL) {
294 /*
295 * if this cdnr has an action function,
296 * make tc_action to call itself.
297 */
298 cb->cb_action.tca_code = TCACODE_NEXT;
299 cb->cb_action.tca_next = cb;
300 cb->cb_input = input_func;
301 } else
302 cb->cb_action.tca_code = TCACODE_NONE;
303
304 /* if this isn't top, register the element to the top level cdnr */
305 if (top != NULL)
306 LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
307
308 return ((void *)cb);
309 }
310
311 static void
312 cdnr_cbdestroy(cblock)
313 void *cblock;
314 {
315 struct cdnr_block *cb = cblock;
316
317 /* delete filters belonging to this cdnr */
318 acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
319
320 /* remove from the top level cdnr */
321 if (cb->cb_top != cblock)
322 LIST_REMOVE(cb, cb_next);
323
324 FREE(cb, M_DEVBUF);
325 }
326
327 /*
328 * conditioner common destroy routine
329 */
330 static int
331 generic_element_destroy(cb)
332 struct cdnr_block *cb;
333 {
334 int error = 0;
335
336 switch (cb->cb_type) {
337 case TCETYPE_TOP:
338 error = top_destroy((struct top_cdnr *)cb);
339 break;
340 case TCETYPE_ELEMENT:
341 error = element_destroy(cb);
342 break;
343 case TCETYPE_TBMETER:
344 error = tbm_destroy((struct tbmeter *)cb);
345 break;
346 case TCETYPE_TRTCM:
347 error = trtcm_destroy((struct trtcm *)cb);
348 break;
349 case TCETYPE_TSWTCM:
350 error = tswtcm_destroy((struct tswtcm *)cb);
351 break;
352 default:
353 error = EINVAL;
354 }
355 return (error);
356 }
357
358 static int
359 tca_verify_action(utca)
360 struct tc_action *utca;
361 {
362 switch (utca->tca_code) {
363 case TCACODE_PASS:
364 case TCACODE_DROP:
365 case TCACODE_MARK:
366 /* these are ok */
367 break;
368
369 case TCACODE_HANDLE:
370 /* verify handle value */
371 if (cdnr_handle2cb(utca->tca_handle) == NULL)
372 return (-1);
373 break;
374
375 case TCACODE_NONE:
376 case TCACODE_RETURN:
377 case TCACODE_NEXT:
378 default:
379 /* should not be passed from a user */
380 return (-1);
381 }
382 return (0);
383 }
384
385 static void
386 tca_import_action(ktca, utca)
387 struct tc_action *ktca, *utca;
388 {
389 struct cdnr_block *cb;
390
391 *ktca = *utca;
392 if (ktca->tca_code == TCACODE_HANDLE) {
393 cb = cdnr_handle2cb(ktca->tca_handle);
394 if (cb == NULL) {
395 ktca->tca_code = TCACODE_NONE;
396 return;
397 }
398 ktca->tca_code = TCACODE_NEXT;
399 ktca->tca_next = cb;
400 cb->cb_ref++;
401 } else if (ktca->tca_code == TCACODE_MARK) {
402 ktca->tca_dscp &= DSCP_MASK;
403 }
404 return;
405 }
406
407 static void
408 tca_invalidate_action(tca)
409 struct tc_action *tca;
410 {
411 struct cdnr_block *cb;
412
413 if (tca->tca_code == TCACODE_NEXT) {
414 cb = tca->tca_next;
415 if (cb == NULL)
416 return;
417 cb->cb_ref--;
418 }
419 tca->tca_code = TCACODE_NONE;
420 }
421
422 /*
423 * top level traffic conditioner
424 */
425 static struct top_cdnr *
426 top_create(ifq)
427 struct ifaltq *ifq;
428 {
429 struct top_cdnr *top;
430
431 if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
432 return (NULL);
433
434 top->tc_ifq = ifq;
435 /* set default action for the top level conditioner */
436 top->tc_block.cb_action.tca_code = TCACODE_PASS;
437
438 LIST_INSERT_HEAD(&tcb_list, top, tc_next);
439
440 ifq->altq_cdnr = top;
441
442 return (top);
443 }
444
445 static int
446 top_destroy(top)
447 struct top_cdnr *top;
448 {
449 struct cdnr_block *cb;
450
451 if (ALTQ_IS_CNDTNING(top->tc_ifq))
452 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
453 top->tc_ifq->altq_cdnr = NULL;
454
455 /*
456 * destroy all the conditioner elements belonging to this interface
457 */
458 while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
459 while (cb != NULL && cb->cb_ref > 0)
460 cb = LIST_NEXT(cb, cb_next);
461 if (cb != NULL)
462 generic_element_destroy(cb);
463 }
464
465 LIST_REMOVE(top, tc_next);
466
467 cdnr_cbdestroy(top);
468
469 /* if there is no active conditioner, remove the input hook */
470 if (altq_input != NULL) {
471 LIST_FOREACH(top, &tcb_list, tc_next)
472 if (ALTQ_IS_CNDTNING(top->tc_ifq))
473 break;
474 if (top == NULL)
475 altq_input = NULL;
476 }
477
478 return (0);
479 }
480
481 /*
482 * simple tc elements without input function (e.g., dropper and makers).
483 */
484 static struct cdnr_block *
485 element_create(top, action)
486 struct top_cdnr *top;
487 struct tc_action *action;
488 {
489 struct cdnr_block *cb;
490
491 if (tca_verify_action(action) < 0)
492 return (NULL);
493
494 if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
495 return (NULL);
496
497 tca_import_action(&cb->cb_action, action);
498
499 return (cb);
500 }
501
502 static int
503 element_destroy(cb)
504 struct cdnr_block *cb;
505 {
506 if (cb->cb_ref > 0)
507 return (EBUSY);
508
509 tca_invalidate_action(&cb->cb_action);
510
511 cdnr_cbdestroy(cb);
512 return (0);
513 }
514
515 /*
516 * internal representation of token bucket parameters
517 * rate: byte_per_unittime << 32
518 * (((bits_per_sec) / 8) << 32) / machclk_freq
519 * depth: byte << 32
520 *
521 */
522 #define TB_SHIFT 32
523 #define TB_SCALE(x) ((u_int64_t)(x) << TB_SHIFT)
524 #define TB_UNSCALE(x) ((x) >> TB_SHIFT)
525
526 static void
527 tb_import_profile(tb, profile)
528 struct tbe *tb;
529 struct tb_profile *profile;
530 {
531 tb->rate = TB_SCALE(profile->rate / 8) / machclk_freq;
532 tb->depth = TB_SCALE(profile->depth);
533 if (tb->rate > 0)
534 tb->filluptime = tb->depth / tb->rate;
535 else
536 tb->filluptime = 0xffffffffffffffffLL;
537 tb->token = tb->depth;
538 tb->last = read_machclk();
539 }
540
541 /*
542 * simple token bucket meter
543 */
544 static struct tbmeter *
545 tbm_create(top, profile, in_action, out_action)
546 struct top_cdnr *top;
547 struct tb_profile *profile;
548 struct tc_action *in_action, *out_action;
549 {
550 struct tbmeter *tbm = NULL;
551
552 if (tca_verify_action(in_action) < 0
553 || tca_verify_action(out_action) < 0)
554 return (NULL);
555
556 if ((tbm = cdnr_cballoc(top, TCETYPE_TBMETER,
557 tbm_input)) == NULL)
558 return (NULL);
559
560 tb_import_profile(&tbm->tb, profile);
561
562 tca_import_action(&tbm->in_action, in_action);
563 tca_import_action(&tbm->out_action, out_action);
564
565 return (tbm);
566 }
567
568 static int
569 tbm_destroy(tbm)
570 struct tbmeter *tbm;
571 {
572 if (tbm->cdnrblk.cb_ref > 0)
573 return (EBUSY);
574
575 tca_invalidate_action(&tbm->in_action);
576 tca_invalidate_action(&tbm->out_action);
577
578 cdnr_cbdestroy(tbm);
579 return (0);
580 }
581
582 static struct tc_action *
583 tbm_input(cb, pktinfo)
584 struct cdnr_block *cb;
585 struct cdnr_pktinfo *pktinfo;
586 {
587 struct tbmeter *tbm = (struct tbmeter *)cb;
588 u_int64_t len;
589 u_int64_t interval, now;
590
591 len = TB_SCALE(pktinfo->pkt_len);
592
593 if (tbm->tb.token < len) {
594 now = read_machclk();
595 interval = now - tbm->tb.last;
596 if (interval >= tbm->tb.filluptime)
597 tbm->tb.token = tbm->tb.depth;
598 else {
599 tbm->tb.token += interval * tbm->tb.rate;
600 if (tbm->tb.token > tbm->tb.depth)
601 tbm->tb.token = tbm->tb.depth;
602 }
603 tbm->tb.last = now;
604 }
605
606 if (tbm->tb.token < len) {
607 PKTCNTR_ADD(&tbm->out_cnt, pktinfo->pkt_len);
608 return (&tbm->out_action);
609 }
610
611 tbm->tb.token -= len;
612 PKTCNTR_ADD(&tbm->in_cnt, pktinfo->pkt_len);
613 return (&tbm->in_action);
614 }
615
616 /*
617 * two rate three color marker
618 * as described in draft-heinanen-diffserv-trtcm-01.txt
619 */
620 static struct trtcm *
621 trtcm_create(top, cmtd_profile, peak_profile,
622 green_action, yellow_action, red_action, coloraware)
623 struct top_cdnr *top;
624 struct tb_profile *cmtd_profile, *peak_profile;
625 struct tc_action *green_action, *yellow_action, *red_action;
626 int coloraware;
627 {
628 struct trtcm *tcm = NULL;
629
630 if (tca_verify_action(green_action) < 0
631 || tca_verify_action(yellow_action) < 0
632 || tca_verify_action(red_action) < 0)
633 return (NULL);
634
635 if ((tcm = cdnr_cballoc(top, TCETYPE_TRTCM,
636 trtcm_input)) == NULL)
637 return (NULL);
638
639 tb_import_profile(&tcm->cmtd_tb, cmtd_profile);
640 tb_import_profile(&tcm->peak_tb, peak_profile);
641
642 tca_import_action(&tcm->green_action, green_action);
643 tca_import_action(&tcm->yellow_action, yellow_action);
644 tca_import_action(&tcm->red_action, red_action);
645
646 /* set dscps to use */
647 if (tcm->green_action.tca_code == TCACODE_MARK)
648 tcm->green_dscp = tcm->green_action.tca_dscp & DSCP_MASK;
649 else
650 tcm->green_dscp = DSCP_AF11;
651 if (tcm->yellow_action.tca_code == TCACODE_MARK)
652 tcm->yellow_dscp = tcm->yellow_action.tca_dscp & DSCP_MASK;
653 else
654 tcm->yellow_dscp = DSCP_AF12;
655 if (tcm->red_action.tca_code == TCACODE_MARK)
656 tcm->red_dscp = tcm->red_action.tca_dscp & DSCP_MASK;
657 else
658 tcm->red_dscp = DSCP_AF13;
659
660 tcm->coloraware = coloraware;
661
662 return (tcm);
663 }
664
665 static int
666 trtcm_destroy(tcm)
667 struct trtcm *tcm;
668 {
669 if (tcm->cdnrblk.cb_ref > 0)
670 return (EBUSY);
671
672 tca_invalidate_action(&tcm->green_action);
673 tca_invalidate_action(&tcm->yellow_action);
674 tca_invalidate_action(&tcm->red_action);
675
676 cdnr_cbdestroy(tcm);
677 return (0);
678 }
679
680 static struct tc_action *
681 trtcm_input(cb, pktinfo)
682 struct cdnr_block *cb;
683 struct cdnr_pktinfo *pktinfo;
684 {
685 struct trtcm *tcm = (struct trtcm *)cb;
686 u_int64_t len;
687 u_int64_t interval, now;
688 u_int8_t color;
689
690 len = TB_SCALE(pktinfo->pkt_len);
691 if (tcm->coloraware) {
692 color = pktinfo->pkt_dscp;
693 if (color != tcm->yellow_dscp && color != tcm->red_dscp)
694 color = tcm->green_dscp;
695 } else {
696 /* if color-blind, precolor it as green */
697 color = tcm->green_dscp;
698 }
699
700 now = read_machclk();
701 if (tcm->cmtd_tb.token < len) {
702 interval = now - tcm->cmtd_tb.last;
703 if (interval >= tcm->cmtd_tb.filluptime)
704 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
705 else {
706 tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
707 if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
708 tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
709 }
710 tcm->cmtd_tb.last = now;
711 }
712 if (tcm->peak_tb.token < len) {
713 interval = now - tcm->peak_tb.last;
714 if (interval >= tcm->peak_tb.filluptime)
715 tcm->peak_tb.token = tcm->peak_tb.depth;
716 else {
717 tcm->peak_tb.token += interval * tcm->peak_tb.rate;
718 if (tcm->peak_tb.token > tcm->peak_tb.depth)
719 tcm->peak_tb.token = tcm->peak_tb.depth;
720 }
721 tcm->peak_tb.last = now;
722 }
723
724 if (color == tcm->red_dscp || tcm->peak_tb.token < len) {
725 pktinfo->pkt_dscp = tcm->red_dscp;
726 PKTCNTR_ADD(&tcm->red_cnt, pktinfo->pkt_len);
727 return (&tcm->red_action);
728 }
729
730 if (color == tcm->yellow_dscp || tcm->cmtd_tb.token < len) {
731 pktinfo->pkt_dscp = tcm->yellow_dscp;
732 tcm->peak_tb.token -= len;
733 PKTCNTR_ADD(&tcm->yellow_cnt, pktinfo->pkt_len);
734 return (&tcm->yellow_action);
735 }
736
737 pktinfo->pkt_dscp = tcm->green_dscp;
738 tcm->cmtd_tb.token -= len;
739 tcm->peak_tb.token -= len;
740 PKTCNTR_ADD(&tcm->green_cnt, pktinfo->pkt_len);
741 return (&tcm->green_action);
742 }
743
744 /*
745 * time sliding window three color marker
746 * as described in draft-fang-diffserv-tc-tswtcm-00.txt
747 */
748 static struct tswtcm *
749 tswtcm_create(top, cmtd_rate, peak_rate, avg_interval,
750 green_action, yellow_action, red_action)
751 struct top_cdnr *top;
752 u_int32_t cmtd_rate, peak_rate, avg_interval;
753 struct tc_action *green_action, *yellow_action, *red_action;
754 {
755 struct tswtcm *tsw;
756
757 if (tca_verify_action(green_action) < 0
758 || tca_verify_action(yellow_action) < 0
759 || tca_verify_action(red_action) < 0)
760 return (NULL);
761
762 if ((tsw = cdnr_cballoc(top, TCETYPE_TSWTCM,
763 tswtcm_input)) == NULL)
764 return (NULL);
765
766 tca_import_action(&tsw->green_action, green_action);
767 tca_import_action(&tsw->yellow_action, yellow_action);
768 tca_import_action(&tsw->red_action, red_action);
769
770 /* set dscps to use */
771 if (tsw->green_action.tca_code == TCACODE_MARK)
772 tsw->green_dscp = tsw->green_action.tca_dscp & DSCP_MASK;
773 else
774 tsw->green_dscp = DSCP_AF11;
775 if (tsw->yellow_action.tca_code == TCACODE_MARK)
776 tsw->yellow_dscp = tsw->yellow_action.tca_dscp & DSCP_MASK;
777 else
778 tsw->yellow_dscp = DSCP_AF12;
779 if (tsw->red_action.tca_code == TCACODE_MARK)
780 tsw->red_dscp = tsw->red_action.tca_dscp & DSCP_MASK;
781 else
782 tsw->red_dscp = DSCP_AF13;
783
784 /* convert rates from bits/sec to bytes/sec */
785 tsw->cmtd_rate = cmtd_rate / 8;
786 tsw->peak_rate = peak_rate / 8;
787 tsw->avg_rate = 0;
788
789 /* timewin is converted from msec to machine clock unit */
790 tsw->timewin = (u_int64_t)machclk_freq * avg_interval / 1000;
791
792 return (tsw);
793 }
794
795 static int
796 tswtcm_destroy(tsw)
797 struct tswtcm *tsw;
798 {
799 if (tsw->cdnrblk.cb_ref > 0)
800 return (EBUSY);
801
802 tca_invalidate_action(&tsw->green_action);
803 tca_invalidate_action(&tsw->yellow_action);
804 tca_invalidate_action(&tsw->red_action);
805
806 cdnr_cbdestroy(tsw);
807 return (0);
808 }
809
810 static struct tc_action *
811 tswtcm_input(cb, pktinfo)
812 struct cdnr_block *cb;
813 struct cdnr_pktinfo *pktinfo;
814 {
815 struct tswtcm *tsw = (struct tswtcm *)cb;
816 int len;
817 u_int32_t avg_rate;
818 u_int64_t interval, now, tmp;
819
820 /*
821 * rate estimator
822 */
823 len = pktinfo->pkt_len;
824 now = read_machclk();
825
826 interval = now - tsw->t_front;
827 /*
828 * calculate average rate:
829 * avg = (avg * timewin + pkt_len)/(timewin + interval)
830 * pkt_len needs to be multiplied by machclk_freq in order to
831 * get (bytes/sec).
832 * note: when avg_rate (bytes/sec) and timewin (machclk unit) are
833 * less than 32 bits, the following 64-bit operation has enough
834 * precision.
835 */
836 tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
837 + (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
838 tsw->avg_rate = avg_rate = (u_int32_t)tmp;
839 tsw->t_front = now;
840
841 /*
842 * marker
843 */
844 if (avg_rate > tsw->cmtd_rate) {
845 u_int32_t randval = random() % avg_rate;
846
847 if (avg_rate > tsw->peak_rate) {
848 if (randval < avg_rate - tsw->peak_rate) {
849 /* mark red */
850 pktinfo->pkt_dscp = tsw->red_dscp;
851 PKTCNTR_ADD(&tsw->red_cnt, len);
852 return (&tsw->red_action);
853 } else if (randval < avg_rate - tsw->cmtd_rate)
854 goto mark_yellow;
855 } else {
856 /* peak_rate >= avg_rate > cmtd_rate */
857 if (randval < avg_rate - tsw->cmtd_rate) {
858 mark_yellow:
859 pktinfo->pkt_dscp = tsw->yellow_dscp;
860 PKTCNTR_ADD(&tsw->yellow_cnt, len);
861 return (&tsw->yellow_action);
862 }
863 }
864 }
865
866 /* mark green */
867 pktinfo->pkt_dscp = tsw->green_dscp;
868 PKTCNTR_ADD(&tsw->green_cnt, len);
869 return (&tsw->green_action);
870 }
871
872 /*
873 * ioctl requests
874 */
875 static int
876 cdnrcmd_if_attach(ifname)
877 char *ifname;
878 {
879 struct ifnet *ifp;
880 struct top_cdnr *top;
881
882 if ((ifp = ifunit(ifname)) == NULL)
883 return (EBADF);
884
885 if (ifp->if_snd.altq_cdnr != NULL)
886 return (EBUSY);
887
888 if ((top = top_create(&ifp->if_snd)) == NULL)
889 return (ENOMEM);
890 return (0);
891 }
892
893 static int
894 cdnrcmd_if_detach(ifname)
895 char *ifname;
896 {
897 struct top_cdnr *top;
898
899 if ((top = tcb_lookup(ifname)) == NULL)
900 return (EBADF);
901
902 return top_destroy(top);
903 }
904
905 static int
906 cdnrcmd_add_element(ap)
907 struct cdnr_add_element *ap;
908 {
909 struct top_cdnr *top;
910 struct cdnr_block *cb;
911
912 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
913 return (EBADF);
914
915 cb = element_create(top, &ap->action);
916 if (cb == NULL)
917 return (EINVAL);
918 /* return a class handle to the user */
919 ap->cdnr_handle = cdnr_cb2handle(cb);
920 return (0);
921 }
922
923 static int
924 cdnrcmd_delete_element(ap)
925 struct cdnr_delete_element *ap;
926 {
927 struct top_cdnr *top;
928 struct cdnr_block *cb;
929
930 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
931 return (EBADF);
932
933 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
934 return (EINVAL);
935
936 if (cb->cb_type != TCETYPE_ELEMENT)
937 return generic_element_destroy(cb);
938
939 return element_destroy(cb);
940 }
941
942 static int
943 cdnrcmd_add_filter(ap)
944 struct cdnr_add_filter *ap;
945 {
946 struct top_cdnr *top;
947 struct cdnr_block *cb;
948
949 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
950 return (EBADF);
951
952 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
953 return (EINVAL);
954
955 return acc_add_filter(&top->tc_classifier, &ap->filter,
956 cb, &ap->filter_handle);
957 }
958
959 static int
960 cdnrcmd_delete_filter(ap)
961 struct cdnr_delete_filter *ap;
962 {
963 struct top_cdnr *top;
964
965 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
966 return (EBADF);
967
968 return acc_delete_filter(&top->tc_classifier, ap->filter_handle);
969 }
970
971 static int
972 cdnrcmd_add_tbm(ap)
973 struct cdnr_add_tbmeter *ap;
974 {
975 struct top_cdnr *top;
976 struct tbmeter *tbm;
977
978 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
979 return (EBADF);
980
981 tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
982 if (tbm == NULL)
983 return (EINVAL);
984 /* return a class handle to the user */
985 ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
986 return (0);
987 }
988
989 static int
990 cdnrcmd_modify_tbm(ap)
991 struct cdnr_modify_tbmeter *ap;
992 {
993 struct tbmeter *tbm;
994
995 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
996 return (EINVAL);
997
998 tb_import_profile(&tbm->tb, &ap->profile);
999
1000 return (0);
1001 }
1002
1003 static int
1004 cdnrcmd_tbm_stats(ap)
1005 struct cdnr_tbmeter_stats *ap;
1006 {
1007 struct tbmeter *tbm;
1008
1009 if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1010 return (EINVAL);
1011
1012 ap->in_cnt = tbm->in_cnt;
1013 ap->out_cnt = tbm->out_cnt;
1014
1015 return (0);
1016 }
1017
1018 static int
1019 cdnrcmd_add_trtcm(ap)
1020 struct cdnr_add_trtcm *ap;
1021 {
1022 struct top_cdnr *top;
1023 struct trtcm *tcm;
1024
1025 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1026 return (EBADF);
1027
1028 tcm = trtcm_create(top, &ap->cmtd_profile, &ap->peak_profile,
1029 &ap->green_action, &ap->yellow_action,
1030 &ap->red_action, ap->coloraware);
1031 if (tcm == NULL)
1032 return (EINVAL);
1033
1034 /* return a class handle to the user */
1035 ap->cdnr_handle = cdnr_cb2handle(&tcm->cdnrblk);
1036 return (0);
1037 }
1038
1039 static int
1040 cdnrcmd_modify_trtcm(ap)
1041 struct cdnr_modify_trtcm *ap;
1042 {
1043 struct trtcm *tcm;
1044
1045 if ((tcm = (struct trtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1046 return (EINVAL);
1047
1048 tb_import_profile(&tcm->cmtd_tb, &ap->cmtd_profile);
1049 tb_import_profile(&tcm->peak_tb, &ap->peak_profile);
1050
1051 return (0);
1052 }
1053
1054 static int
1055 cdnrcmd_tcm_stats(ap)
1056 struct cdnr_tcm_stats *ap;
1057 {
1058 struct cdnr_block *cb;
1059
1060 if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1061 return (EINVAL);
1062
1063 if (cb->cb_type == TCETYPE_TRTCM) {
1064 struct trtcm *tcm = (struct trtcm *)cb;
1065
1066 ap->green_cnt = tcm->green_cnt;
1067 ap->yellow_cnt = tcm->yellow_cnt;
1068 ap->red_cnt = tcm->red_cnt;
1069 } else if (cb->cb_type == TCETYPE_TSWTCM) {
1070 struct tswtcm *tsw = (struct tswtcm *)cb;
1071
1072 ap->green_cnt = tsw->green_cnt;
1073 ap->yellow_cnt = tsw->yellow_cnt;
1074 ap->red_cnt = tsw->red_cnt;
1075 } else
1076 return (EINVAL);
1077
1078 return (0);
1079 }
1080
1081 static int
1082 cdnrcmd_add_tswtcm(ap)
1083 struct cdnr_add_tswtcm *ap;
1084 {
1085 struct top_cdnr *top;
1086 struct tswtcm *tsw;
1087
1088 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1089 return (EBADF);
1090
1091 if (ap->cmtd_rate > ap->peak_rate)
1092 return (EINVAL);
1093
1094 tsw = tswtcm_create(top, ap->cmtd_rate, ap->peak_rate,
1095 ap->avg_interval, &ap->green_action,
1096 &ap->yellow_action, &ap->red_action);
1097 if (tsw == NULL)
1098 return (EINVAL);
1099
1100 /* return a class handle to the user */
1101 ap->cdnr_handle = cdnr_cb2handle(&tsw->cdnrblk);
1102 return (0);
1103 }
1104
1105 static int
1106 cdnrcmd_modify_tswtcm(ap)
1107 struct cdnr_modify_tswtcm *ap;
1108 {
1109 struct tswtcm *tsw;
1110
1111 if ((tsw = (struct tswtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1112 return (EINVAL);
1113
1114 if (ap->cmtd_rate > ap->peak_rate)
1115 return (EINVAL);
1116
1117 /* convert rates from bits/sec to bytes/sec */
1118 tsw->cmtd_rate = ap->cmtd_rate / 8;
1119 tsw->peak_rate = ap->peak_rate / 8;
1120 tsw->avg_rate = 0;
1121
1122 /* timewin is converted from msec to machine clock unit */
1123 tsw->timewin = (u_int64_t)machclk_freq * ap->avg_interval / 1000;
1124
1125 return (0);
1126 }
1127
1128 static int
1129 cdnrcmd_get_stats(ap)
1130 struct cdnr_get_stats *ap;
1131 {
1132 struct top_cdnr *top;
1133 struct cdnr_block *cb;
1134 struct tbmeter *tbm;
1135 struct trtcm *tcm;
1136 struct tswtcm *tsw;
1137 struct tce_stats tce, *usp;
1138 int error, n, nskip, nelements;
1139
1140 if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1141 return (EBADF);
1142
1143 /* copy action stats */
1144 bcopy(top->tc_cnts, ap->cnts, sizeof(ap->cnts));
1145
1146 /* stats for each element */
1147 nelements = ap->nelements;
1148 usp = ap->tce_stats;
1149 if (nelements <= 0 || usp == NULL)
1150 return (0);
1151
1152 nskip = ap->nskip;
1153 n = 0;
1154 LIST_FOREACH(cb, &top->tc_elements, cb_next) {
1155 if (nskip > 0) {
1156 nskip--;
1157 continue;
1158 }
1159
1160 bzero(&tce, sizeof(tce));
1161 tce.tce_handle = cb->cb_handle;
1162 tce.tce_type = cb->cb_type;
1163 switch (cb->cb_type) {
1164 case TCETYPE_TBMETER:
1165 tbm = (struct tbmeter *)cb;
1166 tce.tce_cnts[0] = tbm->in_cnt;
1167 tce.tce_cnts[1] = tbm->out_cnt;
1168 break;
1169 case TCETYPE_TRTCM:
1170 tcm = (struct trtcm *)cb;
1171 tce.tce_cnts[0] = tcm->green_cnt;
1172 tce.tce_cnts[1] = tcm->yellow_cnt;
1173 tce.tce_cnts[2] = tcm->red_cnt;
1174 break;
1175 case TCETYPE_TSWTCM:
1176 tsw = (struct tswtcm *)cb;
1177 tce.tce_cnts[0] = tsw->green_cnt;
1178 tce.tce_cnts[1] = tsw->yellow_cnt;
1179 tce.tce_cnts[2] = tsw->red_cnt;
1180 break;
1181 default:
1182 continue;
1183 }
1184
1185 if ((error = copyout((caddr_t)&tce, (caddr_t)usp++,
1186 sizeof(tce))) != 0)
1187 return (error);
1188
1189 if (++n == nelements)
1190 break;
1191 }
1192 ap->nelements = n;
1193
1194 return (0);
1195 }
1196
1197 /*
1198 * conditioner device interface
1199 */
1200 int
1201 cdnropen(dev, flag, fmt, p)
1202 dev_t dev;
1203 int flag, fmt;
1204 struct proc *p;
1205 {
1206 if (machclk_freq == 0)
1207 init_machclk();
1208
1209 if (machclk_freq == 0) {
1210 printf("cdnr: no cpu clock available!\n");
1211 return (ENXIO);
1212 }
1213
1214 /* everything will be done when the queueing scheme is attached. */
1215 return 0;
1216 }
1217
1218 int
1219 cdnrclose(dev, flag, fmt, p)
1220 dev_t dev;
1221 int flag, fmt;
1222 struct proc *p;
1223 {
1224 struct top_cdnr *top;
1225 int err, error = 0;
1226
1227 while ((top = LIST_FIRST(&tcb_list)) != NULL) {
1228 /* destroy all */
1229 err = top_destroy(top);
1230 if (err != 0 && error == 0)
1231 error = err;
1232 }
1233 altq_input = NULL;
1234
1235 return (error);
1236 }
1237
1238 int
1239 cdnrioctl(dev, cmd, addr, flag, p)
1240 dev_t dev;
1241 ioctlcmd_t cmd;
1242 caddr_t addr;
1243 int flag;
1244 struct proc *p;
1245 {
1246 struct top_cdnr *top;
1247 struct cdnr_interface *ifacep;
1248 int s, error = 0;
1249
1250 /* check super-user privilege */
1251 switch (cmd) {
1252 case CDNR_GETSTATS:
1253 break;
1254 default:
1255 #if (__FreeBSD_version > 400000)
1256 if ((error = suser(p)) != 0)
1257 #else
1258 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1259 #endif
1260 return (error);
1261 break;
1262 }
1263
1264 s = splimp();
1265 switch (cmd) {
1266
1267 case CDNR_IF_ATTACH:
1268 ifacep = (struct cdnr_interface *)addr;
1269 error = cdnrcmd_if_attach(ifacep->cdnr_ifname);
1270 break;
1271
1272 case CDNR_IF_DETACH:
1273 ifacep = (struct cdnr_interface *)addr;
1274 error = cdnrcmd_if_detach(ifacep->cdnr_ifname);
1275 break;
1276
1277 case CDNR_ENABLE:
1278 case CDNR_DISABLE:
1279 ifacep = (struct cdnr_interface *)addr;
1280 if ((top = tcb_lookup(ifacep->cdnr_ifname)) == NULL) {
1281 error = EBADF;
1282 break;
1283 }
1284
1285 switch (cmd) {
1286
1287 case CDNR_ENABLE:
1288 ALTQ_SET_CNDTNING(top->tc_ifq);
1289 if (altq_input == NULL)
1290 altq_input = altq_cdnr_input;
1291 break;
1292
1293 case CDNR_DISABLE:
1294 ALTQ_CLEAR_CNDTNING(top->tc_ifq);
1295 LIST_FOREACH(top, &tcb_list, tc_next)
1296 if (ALTQ_IS_CNDTNING(top->tc_ifq))
1297 break;
1298 if (top == NULL)
1299 altq_input = NULL;
1300 break;
1301 }
1302 break;
1303
1304 case CDNR_ADD_ELEM:
1305 error = cdnrcmd_add_element((struct cdnr_add_element *)addr);
1306 break;
1307
1308 case CDNR_DEL_ELEM:
1309 error = cdnrcmd_delete_element((struct cdnr_delete_element *)addr);
1310 break;
1311
1312 case CDNR_ADD_TBM:
1313 error = cdnrcmd_add_tbm((struct cdnr_add_tbmeter *)addr);
1314 break;
1315
1316 case CDNR_MOD_TBM:
1317 error = cdnrcmd_modify_tbm((struct cdnr_modify_tbmeter *)addr);
1318 break;
1319
1320 case CDNR_TBM_STATS:
1321 error = cdnrcmd_tbm_stats((struct cdnr_tbmeter_stats *)addr);
1322 break;
1323
1324 case CDNR_ADD_TCM:
1325 error = cdnrcmd_add_trtcm((struct cdnr_add_trtcm *)addr);
1326 break;
1327
1328 case CDNR_MOD_TCM:
1329 error = cdnrcmd_modify_trtcm((struct cdnr_modify_trtcm *)addr);
1330 break;
1331
1332 case CDNR_TCM_STATS:
1333 error = cdnrcmd_tcm_stats((struct cdnr_tcm_stats *)addr);
1334 break;
1335
1336 case CDNR_ADD_FILTER:
1337 error = cdnrcmd_add_filter((struct cdnr_add_filter *)addr);
1338 break;
1339
1340 case CDNR_DEL_FILTER:
1341 error = cdnrcmd_delete_filter((struct cdnr_delete_filter *)addr);
1342 break;
1343
1344 case CDNR_GETSTATS:
1345 error = cdnrcmd_get_stats((struct cdnr_get_stats *)addr);
1346 break;
1347
1348 case CDNR_ADD_TSW:
1349 error = cdnrcmd_add_tswtcm((struct cdnr_add_tswtcm *)addr);
1350 break;
1351
1352 case CDNR_MOD_TSW:
1353 error = cdnrcmd_modify_tswtcm((struct cdnr_modify_tswtcm *)addr);
1354 break;
1355
1356 default:
1357 error = EINVAL;
1358 break;
1359 }
1360 splx(s);
1361
1362 return error;
1363 }
1364
1365 #ifdef KLD_MODULE
1366
1367 static struct altqsw cdnr_sw =
1368 {"cdnr", cdnropen, cdnrclose, cdnrioctl};
1369
1370 ALTQ_MODULE(altq_cdnr, ALTQT_CDNR, &cdnr_sw);
1371
1372 #endif /* KLD_MODULE */
1373
1374 #endif /* ALTQ_CDNR */
1375