2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/random.h>
34 #include <sys/kernel_types.h>
35 #include <sys/sysctl.h>
37 #include <kern/zalloc.h>
40 #include <net/net_osdep.h>
41 #include <net/classq/classq.h>
43 #include <net/classq/classq_red.h>
44 #endif /* CLASSQ_RED */
46 #include <net/classq/classq_rio.h>
47 #endif /* CLASSQ_RIO */
49 #include <net/classq/classq_blue.h>
50 #endif /* CLASSQ_BLUE */
51 #include <net/classq/classq_sfb.h>
52 #include <net/pktsched/pktsched.h>
54 #include <libkern/libkern.h>
57 #include <net/altq/altq.h>
60 static errno_t
ifclassq_dequeue_common(struct ifclassq
*, mbuf_svc_class_t
,
61 u_int32_t
, struct mbuf
**, struct mbuf
**, u_int32_t
*, u_int32_t
*,
63 static struct mbuf
*ifclassq_poll_common(struct ifclassq
*,
64 mbuf_svc_class_t
, boolean_t
);
65 static struct mbuf
*ifclassq_tbr_dequeue_common(struct ifclassq
*, int,
66 mbuf_svc_class_t
, boolean_t
);
71 _CASSERT(MBUF_TC_BE
== 0);
72 _CASSERT(MBUF_SC_BE
== 0);
73 _CASSERT(IFCQ_SC_MAX
== MBUF_SC_MAX_CLASSES
);
77 #endif /* CLASSQ_RED */
80 #endif /* CLASSQ_RIO */
83 #endif /* CLASSQ_BLUE */
88 ifclassq_setup(struct ifnet
*ifp
, u_int32_t sflags
, boolean_t reuse
)
91 struct ifclassq
*ifq
= &ifp
->if_snd
;
95 VERIFY(IFCQ_IS_EMPTY(ifq
));
98 bzero(&ifq
->ifcq_xmitcnt
, sizeof (ifq
->ifcq_xmitcnt
));
99 bzero(&ifq
->ifcq_dropcnt
, sizeof (ifq
->ifcq_dropcnt
));
101 VERIFY(!IFCQ_TBR_IS_ENABLED(ifq
));
102 VERIFY(ifq
->ifcq_type
== PKTSCHEDT_NONE
);
103 VERIFY(ifq
->ifcq_flags
== 0);
104 VERIFY(ifq
->ifcq_sflags
== 0);
105 VERIFY(ifq
->ifcq_disc
== NULL
);
106 VERIFY(ifq
->ifcq_enqueue
== NULL
);
107 VERIFY(ifq
->ifcq_dequeue
== NULL
);
108 VERIFY(ifq
->ifcq_dequeue_sc
== NULL
);
109 VERIFY(ifq
->ifcq_request
== NULL
);
111 if (ifp
->if_eflags
& IFEF_TXSTART
) {
112 u_int32_t maxlen
= 0;
114 if ((maxlen
= IFCQ_MAXLEN(ifq
)) == 0)
115 maxlen
= if_sndq_maxlen
;
116 IFCQ_SET_MAXLEN(ifq
, maxlen
);
118 ifq
->ifcq_sflags
= sflags
;
119 err
= ifclassq_pktsched_setup(ifq
);
121 ifq
->ifcq_flags
= (IFCQF_READY
| IFCQF_ENABLED
);
126 IFCQ_ALTQ(ifq
)->altq_ifcq
= ifq
;
127 VERIFY(IFCQ_ALTQ(ifq
)->altq_type
== ALTQT_NONE
);
128 VERIFY(IFCQ_ALTQ(ifq
)->altq_flags
== 0);
129 VERIFY(IFCQ_ALTQ(ifq
)->altq_disc
== NULL
);
130 VERIFY(IFCQ_ALTQ(ifq
)->altq_enqueue
== NULL
);
131 VERIFY(IFCQ_ALTQ(ifq
)->altq_dequeue
== NULL
);
132 VERIFY(IFCQ_ALTQ(ifq
)->altq_dequeue_sc
== NULL
);
133 VERIFY(IFCQ_ALTQ(ifq
)->altq_request
== NULL
);
135 if ((ifp
->if_eflags
& IFEF_TXSTART
) &&
136 ifp
->if_output_sched_model
!= IFNET_SCHED_MODEL_DRIVER_MANAGED
)
137 ALTQ_SET_READY(IFCQ_ALTQ(ifq
));
139 ALTQ_CLEAR_READY(IFCQ_ALTQ(ifq
));
147 ifclassq_teardown(struct ifnet
*ifp
)
149 struct ifclassq
*ifq
= &ifp
->if_snd
;
153 if (ALTQ_IS_READY(IFCQ_ALTQ(ifq
))) {
154 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
155 altq_disable(IFCQ_ALTQ(ifq
));
156 if (ALTQ_IS_ATTACHED(IFCQ_ALTQ(ifq
)))
157 altq_detach(IFCQ_ALTQ(ifq
));
158 IFCQ_ALTQ(ifq
)->altq_flags
= 0;
161 IFCQ_ALTQ(ifq
)->altq_ifcq
= NULL
;
162 VERIFY(IFCQ_ALTQ(ifq
)->altq_type
== ALTQT_NONE
);
163 VERIFY(IFCQ_ALTQ(ifq
)->altq_flags
== 0);
164 VERIFY(IFCQ_ALTQ(ifq
)->altq_disc
== NULL
);
165 VERIFY(IFCQ_ALTQ(ifq
)->altq_enqueue
== NULL
);
166 VERIFY(IFCQ_ALTQ(ifq
)->altq_dequeue
== NULL
);
167 VERIFY(IFCQ_ALTQ(ifq
)->altq_dequeue_sc
== NULL
);
168 VERIFY(IFCQ_ALTQ(ifq
)->altq_request
== NULL
);
171 if (IFCQ_IS_READY(ifq
)) {
172 if (IFCQ_TBR_IS_ENABLED(ifq
)) {
173 struct tb_profile tb
= { 0, 0, 0 };
174 (void) ifclassq_tbr_set(ifq
, &tb
, FALSE
);
176 (void) pktsched_teardown(ifq
);
179 ifq
->ifcq_sflags
= 0;
181 VERIFY(IFCQ_IS_EMPTY(ifq
));
182 VERIFY(!IFCQ_TBR_IS_ENABLED(ifq
));
183 VERIFY(ifq
->ifcq_type
== PKTSCHEDT_NONE
);
184 VERIFY(ifq
->ifcq_flags
== 0);
185 VERIFY(ifq
->ifcq_sflags
== 0);
186 VERIFY(ifq
->ifcq_disc
== NULL
);
187 VERIFY(ifq
->ifcq_enqueue
== NULL
);
188 VERIFY(ifq
->ifcq_dequeue
== NULL
);
189 VERIFY(ifq
->ifcq_dequeue_sc
== NULL
);
190 VERIFY(ifq
->ifcq_request
== NULL
);
192 IFCQ_MAXLEN(ifq
) = 0;
193 bzero(&ifq
->ifcq_xmitcnt
, sizeof (ifq
->ifcq_xmitcnt
));
194 bzero(&ifq
->ifcq_dropcnt
, sizeof (ifq
->ifcq_dropcnt
));
200 ifclassq_pktsched_setup(struct ifclassq
*ifq
)
202 struct ifnet
*ifp
= ifq
->ifcq_ifp
;
205 IFCQ_LOCK_ASSERT_HELD(ifq
);
206 VERIFY(ifp
->if_eflags
& IFEF_TXSTART
);
208 switch (ifp
->if_output_sched_model
) {
209 case IFNET_SCHED_MODEL_DRIVER_MANAGED
:
210 err
= pktsched_setup(ifq
, PKTSCHEDT_TCQ
, ifq
->ifcq_sflags
);
213 case IFNET_SCHED_MODEL_NORMAL
:
214 err
= pktsched_setup(ifq
, PKTSCHEDT_QFQ
, ifq
->ifcq_sflags
);
226 ifclassq_set_maxlen(struct ifclassq
*ifq
, u_int32_t maxqlen
)
230 maxqlen
= if_sndq_maxlen
;
231 IFCQ_SET_MAXLEN(ifq
, maxqlen
);
236 ifclassq_get_maxlen(struct ifclassq
*ifq
)
238 return (IFCQ_MAXLEN(ifq
));
242 ifclassq_get_len(struct ifclassq
*ifq
)
244 return (IFCQ_LEN(ifq
));
248 ifclassq_enqueue(struct ifclassq
*ifq
, struct mbuf
*m
)
255 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
))) {
256 ALTQ_ENQUEUE(IFCQ_ALTQ(ifq
), m
, err
);
258 u_int32_t qlen
= IFCQ_LEN(ifq
);
259 IFCQ_ENQUEUE(ifq
, m
, err
);
260 if (IFCQ_LEN(ifq
) > qlen
)
261 ifq
->ifcq_drain
+= (IFCQ_LEN(ifq
) - qlen
);
264 IFCQ_ENQUEUE(ifq
, m
, err
);
273 ifclassq_dequeue(struct ifclassq
*ifq
, u_int32_t limit
, struct mbuf
**head
,
274 struct mbuf
**tail
, u_int32_t
*cnt
, u_int32_t
*len
)
276 return (ifclassq_dequeue_common(ifq
, MBUF_SC_UNSPEC
, limit
, head
, tail
,
281 ifclassq_dequeue_sc(struct ifclassq
*ifq
, mbuf_svc_class_t sc
,
282 u_int32_t limit
, struct mbuf
**head
, struct mbuf
**tail
, u_int32_t
*cnt
,
285 return (ifclassq_dequeue_common(ifq
, sc
, limit
, head
, tail
,
290 ifclassq_dequeue_common(struct ifclassq
*ifq
, mbuf_svc_class_t sc
,
291 u_int32_t limit
, struct mbuf
**head
, struct mbuf
**tail
, u_int32_t
*cnt
,
292 u_int32_t
*len
, boolean_t drvmgt
)
294 struct ifnet
*ifp
= ifq
->ifcq_ifp
;
295 u_int32_t i
= 0, l
= 0;
296 struct mbuf
**first
, *last
;
298 struct ifaltq
*altq
= IFCQ_ALTQ(ifq
);
302 VERIFY(!drvmgt
|| MBUF_VALID_SC(sc
));
316 qlen
= IFCQ_LEN(ifq
);
317 draining
= IFCQ_IS_DRAINING(ifq
);
320 if (IFCQ_TBR_IS_ENABLED(ifq
))
321 IFCQ_TBR_DEQUEUE_SC(ifq
, sc
, *head
);
323 IFCQ_DEQUEUE_SC(ifq
, sc
, *head
);
324 else if (ALTQ_IS_ENABLED(altq
))
325 ALTQ_DEQUEUE_SC(altq
, sc
, *head
);
329 if (IFCQ_TBR_IS_ENABLED(ifq
))
330 IFCQ_TBR_DEQUEUE(ifq
, *head
);
332 IFCQ_DEQUEUE(ifq
, *head
);
333 else if (ALTQ_IS_ENABLED(altq
))
334 ALTQ_DEQUEUE(altq
, *head
);
339 if (draining
&& *head
!= NULL
) {
340 VERIFY(ifq
->ifcq_drain
>= (qlen
- IFCQ_LEN(ifq
)));
341 ifq
->ifcq_drain
-= (qlen
- IFCQ_LEN(ifq
));
343 #else /* ! PF_ALTQ */
345 if (IFCQ_TBR_IS_ENABLED(ifq
))
346 IFCQ_TBR_DEQUEUE_SC(ifq
, sc
, *head
);
348 IFCQ_DEQUEUE_SC(ifq
, sc
, *head
);
350 if (IFCQ_TBR_IS_ENABLED(ifq
))
351 IFCQ_TBR_DEQUEUE(ifq
, *head
);
353 IFCQ_DEQUEUE(ifq
, *head
);
355 #endif /* !PF_ALTQ */
360 (*head
)->m_nextpkt
= NULL
;
363 l
+= (*head
)->m_pkthdr
.len
;
364 pktlen
= (*head
)->m_pkthdr
.len
;
366 (*head
)->m_pkthdr
.pf_mtag
.pftag_pktseq
=
367 atomic_add_64_ov(&(ifp
->if_bw
.cur_seq
), pktlen
);
369 head
= &(*head
)->m_nextpkt
;
382 return ((*first
!= NULL
) ? 0 : EAGAIN
);
386 ifclassq_poll(struct ifclassq
*ifq
)
388 return (ifclassq_poll_common(ifq
, MBUF_SC_UNSPEC
, FALSE
));
392 ifclassq_poll_sc(struct ifclassq
*ifq
, mbuf_svc_class_t sc
)
394 return (ifclassq_poll_common(ifq
, sc
, TRUE
));
398 ifclassq_poll_common(struct ifclassq
*ifq
, mbuf_svc_class_t sc
,
402 struct ifaltq
*altq
= IFCQ_ALTQ(ifq
);
406 VERIFY(!drvmgt
|| MBUF_VALID_SC(sc
));
410 if (IFCQ_TBR_IS_ENABLED(ifq
))
411 IFCQ_TBR_POLL_SC(ifq
, sc
, m
);
412 else if (IFCQ_IS_DRAINING(ifq
))
413 IFCQ_POLL_SC(ifq
, sc
, m
);
414 else if (ALTQ_IS_ENABLED(altq
))
415 ALTQ_POLL_SC(altq
, sc
, m
);
419 if (IFCQ_TBR_IS_ENABLED(ifq
))
420 IFCQ_TBR_POLL(ifq
, m
);
421 else if (IFCQ_IS_DRAINING(ifq
))
423 else if (ALTQ_IS_ENABLED(altq
))
428 #else /* ! PF_ALTQ */
430 if (IFCQ_TBR_IS_ENABLED(ifq
))
431 IFCQ_TBR_POLL_SC(ifq
, sc
, m
);
433 IFCQ_POLL_SC(ifq
, sc
, m
);
435 if (IFCQ_TBR_IS_ENABLED(ifq
))
436 IFCQ_TBR_POLL(ifq
, m
);
440 #endif /* !PF_ALTQ */
446 ifclassq_update(struct ifclassq
*ifq
, cqev_t ev
)
448 IFCQ_LOCK_ASSERT_HELD(ifq
);
449 VERIFY(IFCQ_IS_READY(ifq
));
452 if (ALTQ_IS_ENABLED(IFCQ_ALTQ(ifq
)))
453 ALTQ_UPDATE(IFCQ_ALTQ(ifq
), ev
);
455 IFCQ_UPDATE(ifq
, ev
);
459 ifclassq_attach(struct ifclassq
*ifq
, u_int32_t type
, void *discipline
,
460 ifclassq_enq_func enqueue
, ifclassq_deq_func dequeue
,
461 ifclassq_deq_sc_func dequeue_sc
, ifclassq_req_func request
)
463 IFCQ_LOCK_ASSERT_HELD(ifq
);
465 VERIFY(ifq
->ifcq_disc
== NULL
);
466 VERIFY(enqueue
!= NULL
);
467 VERIFY(!(dequeue
!= NULL
&& dequeue_sc
!= NULL
));
468 VERIFY(request
!= NULL
);
470 ifq
->ifcq_type
= type
;
471 ifq
->ifcq_disc
= discipline
;
472 ifq
->ifcq_enqueue
= enqueue
;
473 ifq
->ifcq_dequeue
= dequeue
;
474 ifq
->ifcq_dequeue_sc
= dequeue_sc
;
475 ifq
->ifcq_request
= request
;
481 ifclassq_detach(struct ifclassq
*ifq
)
483 IFCQ_LOCK_ASSERT_HELD(ifq
);
485 VERIFY(ifq
->ifcq_disc
== NULL
);
487 ifq
->ifcq_type
= PKTSCHEDT_NONE
;
488 ifq
->ifcq_disc
= NULL
;
489 ifq
->ifcq_enqueue
= NULL
;
490 ifq
->ifcq_dequeue
= NULL
;
491 ifq
->ifcq_dequeue_sc
= NULL
;
492 ifq
->ifcq_request
= NULL
;
498 ifclassq_getqstats(struct ifclassq
*ifq
, u_int32_t qid
, void *ubuf
,
501 struct if_ifclassq_stats
*ifqs
;
504 if (*nbytes
< sizeof (*ifqs
))
507 ifqs
= _MALLOC(sizeof (*ifqs
), M_TEMP
, M_WAITOK
| M_ZERO
);
512 if (!IFCQ_IS_READY(ifq
)) {
518 ifqs
->ifqs_len
= IFCQ_LEN(ifq
);
519 ifqs
->ifqs_maxlen
= IFCQ_MAXLEN(ifq
);
520 *(&ifqs
->ifqs_xmitcnt
) = *(&ifq
->ifcq_xmitcnt
);
521 *(&ifqs
->ifqs_dropcnt
) = *(&ifq
->ifcq_dropcnt
);
522 ifqs
->ifqs_scheduler
= ifq
->ifcq_type
;
524 err
= pktsched_getqstats(ifq
, qid
, ifqs
);
527 if (err
== 0 && (err
= copyout((caddr_t
)ifqs
,
528 (user_addr_t
)(uintptr_t)ubuf
, sizeof (*ifqs
))) == 0)
529 *nbytes
= sizeof (*ifqs
);
537 ifclassq_ev2str(cqev_t ev
)
542 case CLASSQ_EV_LINK_SPEED
:
546 case CLASSQ_EV_LINK_MTU
:
550 case CLASSQ_EV_LINK_UP
:
554 case CLASSQ_EV_LINK_DOWN
:
567 * internal representation of token bucket parameters
568 * rate: byte_per_unittime << 32
569 * (((bits_per_sec) / 8) << 32) / machclk_freq
574 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
575 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
578 ifclassq_tbr_dequeue(struct ifclassq
*ifq
, int op
)
580 return (ifclassq_tbr_dequeue_common(ifq
, op
, MBUF_SC_UNSPEC
, FALSE
));
584 ifclassq_tbr_dequeue_sc(struct ifclassq
*ifq
, int op
, mbuf_svc_class_t sc
)
586 return (ifclassq_tbr_dequeue_common(ifq
, op
, sc
, TRUE
));
590 ifclassq_tbr_dequeue_common(struct ifclassq
*ifq
, int op
,
591 mbuf_svc_class_t sc
, boolean_t drvmgt
)
593 struct tb_regulator
*tbr
;
598 IFCQ_LOCK_ASSERT_HELD(ifq
);
600 VERIFY(!drvmgt
|| MBUF_VALID_SC(sc
));
601 VERIFY(IFCQ_TBR_IS_ENABLED(ifq
));
603 tbr
= &ifq
->ifcq_tbr
;
604 if (op
== CLASSQDQ_REMOVE
&& tbr
->tbr_lastop
== CLASSQDQ_POLL
) {
605 /* if this is a remove after poll, bypass tbr check */
607 /* update token only when it is negative */
608 if (tbr
->tbr_token
<= 0) {
609 now
= read_machclk();
610 interval
= now
- tbr
->tbr_last
;
611 if (interval
>= tbr
->tbr_filluptime
) {
612 tbr
->tbr_token
= tbr
->tbr_depth
;
614 tbr
->tbr_token
+= interval
* tbr
->tbr_rate
;
615 if (tbr
->tbr_token
> tbr
->tbr_depth
)
616 tbr
->tbr_token
= tbr
->tbr_depth
;
620 /* if token is still negative, don't allow dequeue */
621 if (tbr
->tbr_token
<= 0)
626 * ifclassq takes precedence over ALTQ queue;
627 * ifcq_drain count is adjusted by the caller.
630 if (IFCQ_IS_DRAINING(ifq
)) {
632 if (op
== CLASSQDQ_POLL
) {
634 IFCQ_POLL_SC(ifq
, sc
, m
);
639 IFCQ_DEQUEUE_SC(ifq
, sc
, m
);
641 IFCQ_DEQUEUE(ifq
, m
);
645 struct ifaltq
*altq
= IFCQ_ALTQ(ifq
);
646 if (ALTQ_IS_ENABLED(altq
)) {
648 m
= (*altq
->altq_dequeue_sc
)(altq
, sc
, op
);
650 m
= (*altq
->altq_dequeue
)(altq
, op
);
657 if (m
!= NULL
&& op
== CLASSQDQ_REMOVE
)
658 tbr
->tbr_token
-= TBR_SCALE(m_pktlen(m
));
659 tbr
->tbr_lastop
= op
;
665 * set a token bucket regulator.
666 * if the specified rate is zero, the token bucket regulator is deleted.
669 ifclassq_tbr_set(struct ifclassq
*ifq
, struct tb_profile
*profile
,
672 struct tb_regulator
*tbr
;
673 struct ifnet
*ifp
= ifq
->ifcq_ifp
;
674 u_int64_t rate
, old_rate
;
676 IFCQ_LOCK_ASSERT_HELD(ifq
);
677 VERIFY(IFCQ_IS_READY(ifq
));
679 VERIFY(machclk_freq
!= 0);
681 tbr
= &ifq
->ifcq_tbr
;
682 old_rate
= tbr
->tbr_rate_raw
;
684 rate
= profile
->rate
;
685 if (profile
->percent
> 0) {
688 if (profile
->percent
> 100)
690 if ((eff_rate
= ifp
->if_output_bw
.eff_bw
) == 0)
692 rate
= (eff_rate
* profile
->percent
) / 100;
696 if (!IFCQ_TBR_IS_ENABLED(ifq
))
699 if (pktsched_verbose
)
700 printf("%s: TBR disabled\n", if_name(ifp
));
702 /* disable this TBR */
703 ifq
->ifcq_flags
&= ~IFCQF_TBR
;
704 bzero(tbr
, sizeof (*tbr
));
705 ifnet_set_start_cycle(ifp
, NULL
);
707 ifclassq_update(ifq
, CLASSQ_EV_LINK_SPEED
);
711 if (pktsched_verbose
) {
712 printf("%s: TBR %s (rate %llu bps depth %u)\n", if_name(ifp
),
713 (ifq
->ifcq_flags
& IFCQF_TBR
) ? "reconfigured" :
714 "enabled", rate
, profile
->depth
);
717 /* set the new TBR */
718 bzero(tbr
, sizeof (*tbr
));
719 tbr
->tbr_rate_raw
= rate
;
720 tbr
->tbr_percent
= profile
->percent
;
721 ifq
->ifcq_flags
|= IFCQF_TBR
;
724 * Note that the TBR fill up time (hence the ifnet restart time)
725 * is directly related to the specified TBR depth. The ideal
726 * depth value should be computed such that the interval time
727 * between each successive wakeup is adequately spaced apart,
728 * in order to reduce scheduling overheads. A target interval
729 * of 10 ms seems to provide good performance balance. This can be
730 * overridden by specifying the depth profile. Values smaller than
731 * the ideal depth will reduce delay at the expense of CPU cycles.
733 tbr
->tbr_rate
= TBR_SCALE(rate
/ 8) / machclk_freq
;
734 if (tbr
->tbr_rate
> 0) {
735 u_int32_t mtu
= ifp
->if_mtu
;
736 int64_t ival
, idepth
= 0;
742 ival
= pktsched_nsecs_to_abstime(10 * NSEC_PER_MSEC
); /* 10ms */
745 idepth
= TBR_SCALE(i
* mtu
);
746 if ((idepth
/ tbr
->tbr_rate
) > ival
)
751 tbr
->tbr_depth
= TBR_SCALE(profile
->depth
);
752 if (tbr
->tbr_depth
== 0) {
753 tbr
->tbr_filluptime
= idepth
/ tbr
->tbr_rate
;
754 /* a little fudge factor to get closer to rate */
755 tbr
->tbr_depth
= idepth
+ (idepth
>> 3);
757 tbr
->tbr_filluptime
= tbr
->tbr_depth
/ tbr
->tbr_rate
;
760 tbr
->tbr_depth
= TBR_SCALE(profile
->depth
);
761 tbr
->tbr_filluptime
= 0xffffffffffffffffLL
;
763 tbr
->tbr_token
= tbr
->tbr_depth
;
764 tbr
->tbr_last
= read_machclk();
765 tbr
->tbr_lastop
= CLASSQDQ_REMOVE
;
767 if (tbr
->tbr_rate
> 0 && (ifp
->if_flags
& IFF_UP
)) {
769 { 0, pktsched_abs_to_nsecs(tbr
->tbr_filluptime
) };
770 if (pktsched_verbose
) {
771 printf("%s: TBR calculated tokens %lld "
772 "filluptime %llu ns\n", if_name(ifp
),
773 TBR_UNSCALE(tbr
->tbr_token
),
774 pktsched_abs_to_nsecs(tbr
->tbr_filluptime
));
776 ifnet_set_start_cycle(ifp
, &ts
);
778 if (pktsched_verbose
) {
779 if (tbr
->tbr_rate
== 0) {
780 printf("%s: TBR calculated tokens %lld "
781 "infinite filluptime\n", if_name(ifp
),
782 TBR_UNSCALE(tbr
->tbr_token
));
783 } else if (!(ifp
->if_flags
& IFF_UP
)) {
784 printf("%s: TBR suspended (link is down)\n",
788 ifnet_set_start_cycle(ifp
, NULL
);
790 if (update
&& tbr
->tbr_rate_raw
!= old_rate
)
791 ifclassq_update(ifq
, CLASSQ_EV_LINK_SPEED
);