<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
is	O
an	O
algorithm	O
used	O
in	O
packet-switched	B-Protocol
and	O
telecommunications	O
networks	O
.	O
</s>
<s>
It	O
can	O
be	O
used	O
to	O
check	O
that	O
data	O
transmissions	O
,	O
in	O
the	O
form	O
of	O
packets	B-Protocol
,	O
conform	O
to	O
defined	O
limits	O
on	O
bandwidth	O
and	O
burstiness	O
(	O
a	O
measure	O
of	O
the	O
unevenness	O
or	O
variations	O
in	O
the	O
traffic	B-Protocol
flow	O
)	O
.	O
</s>
<s>
It	O
can	O
also	O
be	O
used	O
as	O
a	O
scheduling	O
algorithm	O
to	O
determine	O
the	O
timing	O
of	O
transmissions	O
that	O
will	O
comply	O
with	O
the	O
limits	O
set	O
for	O
the	O
bandwidth	O
and	O
burstiness	O
:	O
see	O
network	B-Application
scheduler	I-Application
.	O
</s>
<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
is	O
based	O
on	O
an	O
analogy	O
of	O
a	O
fixed	O
capacity	O
bucket	B-Device
into	O
which	O
tokens	O
,	O
normally	O
representing	O
a	O
unit	O
of	O
bytes	O
or	O
a	O
single	O
packet	B-Protocol
of	O
predetermined	O
size	O
,	O
are	O
added	O
at	O
a	O
fixed	O
rate	O
.	O
</s>
<s>
When	O
a	O
packet	B-Protocol
is	O
to	O
be	O
checked	O
for	O
conformance	O
to	O
the	O
defined	O
limits	O
,	O
the	O
bucket	B-Device
is	O
inspected	O
to	O
see	O
if	O
it	O
contains	O
sufficient	O
tokens	O
at	O
that	O
time	O
.	O
</s>
<s>
equivalent	O
to	O
the	O
length	O
of	O
the	O
packet	B-Protocol
in	O
bytes	O
,	O
are	O
removed	O
(	O
"	O
cashed	O
in	O
"	O
)	O
,	O
and	O
the	O
packet	B-Protocol
is	O
passed	O
,	O
e.g.	O
,	O
for	O
transmission	O
.	O
</s>
<s>
The	O
packet	B-Protocol
does	O
not	O
conform	O
if	O
there	O
are	O
insufficient	O
tokens	O
in	O
the	O
bucket	B-Device
,	O
and	O
the	O
contents	O
of	O
the	O
bucket	B-Device
are	O
not	O
changed	O
.	O
</s>
<s>
Non-conformant	O
packets	B-Protocol
can	O
be	O
treated	O
in	O
various	O
ways	O
:	O
</s>
<s>
They	O
may	O
be	O
enqueued	O
for	O
subsequent	O
transmission	O
when	O
sufficient	O
tokens	O
have	O
accumulated	O
in	O
the	O
bucket	B-Device
.	O
</s>
<s>
A	O
conforming	O
flow	O
can	O
thus	O
contain	O
traffic	B-Protocol
with	O
an	O
average	O
rate	O
up	O
to	O
the	O
rate	O
at	O
which	O
tokens	O
are	O
added	O
to	O
the	O
bucket	B-Device
,	O
and	O
have	O
a	O
burstiness	O
determined	O
by	O
the	O
depth	O
of	O
the	O
bucket	B-Device
.	O
</s>
<s>
how	O
much	O
sooner	O
a	O
packet	B-Protocol
might	O
conform	O
(	O
e.g.	O
</s>
<s>
how	O
much	O
more	O
than	O
the	O
average	O
level	O
of	O
traffic	B-Protocol
might	O
conform	O
in	O
some	O
finite	O
period	O
.	O
</s>
<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
can	O
be	O
conceptually	O
understood	O
as	O
follows	O
:	O
</s>
<s>
A	O
token	O
is	O
added	O
to	O
the	O
bucket	B-Device
every	O
seconds	O
.	O
</s>
<s>
The	O
bucket	B-Device
can	O
hold	O
at	O
the	O
most	O
tokens	O
.	O
</s>
<s>
If	O
a	O
token	O
arrives	O
when	O
the	O
bucket	B-Device
is	O
full	O
,	O
it	O
is	O
discarded	O
.	O
</s>
<s>
When	O
a	O
packet	B-Protocol
(	O
network	O
layer	O
PDU	B-Protocol
)	O
of	O
n	O
bytes	O
arrives	O
,	O
</s>
<s>
if	O
at	O
least	O
n	O
tokens	O
are	O
in	O
the	O
bucket	B-Device
,	O
n	O
tokens	O
are	O
removed	O
from	O
the	O
bucket	B-Device
,	O
and	O
the	O
packet	B-Protocol
is	O
sent	O
to	O
the	O
network	O
.	O
</s>
<s>
if	O
fewer	O
than	O
n	O
tokens	O
are	O
available	O
,	O
no	O
tokens	O
are	O
removed	O
from	O
the	O
bucket	B-Device
,	O
and	O
the	O
packet	B-Protocol
is	O
considered	O
to	O
be	O
non-conformant	O
.	O
</s>
<s>
Implementers	O
of	O
this	O
algorithm	O
on	O
platforms	O
lacking	O
the	O
clock	O
resolution	O
necessary	O
to	O
add	O
a	O
single	O
token	O
to	O
the	O
bucket	B-Device
every	O
seconds	O
may	O
want	O
to	O
consider	O
an	O
alternative	O
formulation	O
.	O
</s>
<s>
Given	O
the	O
ability	O
to	O
update	O
the	O
token	B-Algorithm
bucket	I-Algorithm
every	O
S	O
milliseconds	O
,	O
the	O
number	O
of	O
tokens	O
to	O
add	O
every	O
S	O
milliseconds	O
=	O
.	O
</s>
<s>
Over	O
the	O
long	O
run	O
the	O
output	O
of	O
conformant	O
packets	B-Protocol
is	O
limited	O
by	O
the	O
token	O
rate	O
,	O
.	O
</s>
<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
can	O
be	O
used	O
in	O
either	O
traffic	B-Protocol
shaping	O
or	O
traffic	B-Protocol
policing	O
.	O
</s>
<s>
In	O
traffic	B-Protocol
policing	O
,	O
nonconforming	O
packets	B-Protocol
may	O
be	O
discarded	O
(	O
dropped	O
)	O
or	O
may	O
be	O
reduced	O
in	O
priority	O
(	O
for	O
downstream	O
traffic	B-Protocol
management	O
functions	O
to	O
drop	O
if	O
there	O
is	O
congestion	O
)	O
.	O
</s>
<s>
In	O
traffic	B-Protocol
shaping	O
,	O
packets	B-Protocol
are	O
delayed	O
until	O
they	O
conform	O
.	O
</s>
<s>
Traffic	B-Protocol
policing	O
and	O
traffic	B-Protocol
shaping	O
are	O
commonly	O
used	O
to	O
protect	O
the	O
network	O
against	O
excess	O
or	O
excessively	O
bursty	O
traffic	B-Protocol
,	O
see	O
bandwidth	O
management	O
and	O
congestion	O
avoidance	O
.	O
</s>
<s>
Traffic	B-Protocol
shaping	O
is	O
commonly	O
used	O
in	O
the	O
network	B-Protocol
interfaces	I-Protocol
in	O
hosts	B-Device
to	O
prevent	O
transmissions	O
being	O
discarded	O
by	O
traffic	B-Protocol
management	O
functions	O
in	O
the	O
network	O
.	O
</s>
<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
is	O
also	O
used	O
in	O
controlling	O
database	O
IO	O
flow	O
.	O
</s>
<s>
The	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
is	O
directly	O
comparable	O
to	O
one	O
of	O
the	O
two	O
versions	O
of	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
algorithm	I-Algorithm
described	O
in	O
the	O
literature	O
.	O
</s>
<s>
This	O
comparable	O
version	O
of	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
is	O
described	O
on	O
the	O
relevant	O
Wikipedia	O
page	O
as	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
algorithm	I-Algorithm
as	O
a	O
meter	O
.	O
</s>
<s>
This	O
is	O
a	O
mirror	O
image	O
of	O
the	O
token	B-Algorithm
bucket	I-Algorithm
,	O
in	O
that	O
conforming	O
packets	B-Protocol
add	O
fluid	O
,	O
equivalent	O
to	O
the	O
tokens	O
removed	O
by	O
a	O
conforming	O
packet	B-Protocol
in	O
the	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
,	O
to	O
a	O
finite	O
capacity	O
bucket	B-Device
,	O
from	O
which	O
this	O
fluid	O
then	O
drains	O
away	O
at	O
a	O
constant	O
rate	O
,	O
equivalent	O
to	O
the	O
process	O
in	O
which	O
tokens	O
are	O
added	O
at	O
a	O
fixed	O
rate	O
.	O
</s>
<s>
There	O
is	O
,	O
however	O
,	O
another	O
version	O
of	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
algorithm	I-Algorithm
,	O
described	O
on	O
the	O
relevant	O
Wikipedia	O
page	O
as	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
algorithm	I-Algorithm
as	O
a	O
queue	O
.	O
</s>
<s>
This	O
is	O
a	O
special	O
case	O
of	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
as	O
a	O
meter	O
,	O
which	O
can	O
be	O
described	O
by	O
the	O
conforming	O
packets	B-Protocol
passing	O
through	O
the	O
bucket	B-Device
.	O
</s>
<s>
The	O
leaky	B-Algorithm
bucket	I-Algorithm
as	O
a	O
queue	O
is	O
therefore	O
applicable	O
only	O
to	O
traffic	B-Protocol
shaping	O
,	O
and	O
does	O
not	O
,	O
in	O
general	O
,	O
allow	O
the	O
output	O
packet	B-Protocol
stream	O
to	O
be	O
bursty	O
,	O
i.e.	O
</s>
<s>
It	O
is	O
therefore	O
significantly	O
different	O
from	O
the	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
.	O
</s>
<s>
These	O
two	O
versions	O
of	O
the	O
leaky	B-Algorithm
bucket	I-Algorithm
algorithm	I-Algorithm
have	O
both	O
been	O
described	O
in	O
the	O
literature	O
under	O
the	O
same	O
name	O
.	O
</s>
<s>
This	O
has	O
led	O
to	O
considerable	O
confusion	O
over	O
the	O
properties	O
of	O
that	O
algorithm	O
and	O
its	O
comparison	O
with	O
the	O
token	B-Algorithm
bucket	I-Algorithm
algorithm	O
.	O
</s>
<s>
However	O
,	O
fundamentally	O
,	O
the	O
two	O
algorithms	O
are	O
the	O
same	O
,	O
and	O
will	O
,	O
if	O
implemented	O
correctly	O
and	O
given	O
the	O
same	O
parameters	O
,	O
see	O
exactly	O
the	O
same	O
packets	B-Protocol
as	O
conforming	O
and	O
nonconforming	O
.	O
</s>
<s>
The	O
hierarchical	O
token	B-Algorithm
bucket	I-Algorithm
(	O
HTB	O
)	O
is	O
a	O
faster	O
replacement	O
for	O
the	O
class-based	B-Algorithm
queueing	I-Algorithm
(	O
CBQ	O
)	O
queuing	B-Application
discipline	I-Application
in	O
Linux	B-Application
.	O
</s>
<s>
It	O
is	O
useful	O
for	O
limiting	O
each	O
client	O
's	O
download/upload	O
rate	O
so	O
that	O
the	O
limited	O
client	O
cannot	O
saturate	O
the	O
total	O
bandwidth	O
.	O
</s>
<s>
Conceptually	O
,	O
HTB	O
is	O
an	O
arbitrary	O
number	O
of	O
token	B-Algorithm
buckets	I-Algorithm
arranged	O
in	O
a	O
hierarchy	O
.	O
</s>
<s>
The	O
primary	O
egress	O
queuing	B-Application
discipline	I-Application
(	O
qdisc	B-Application
)	O
on	O
any	O
device	O
is	O
known	O
as	O
the	O
root	O
qdisc	B-Application
.	O
</s>
<s>
The	O
root	O
qdisc	B-Application
will	O
contain	O
one	O
class	O
.	O
</s>
<s>
Hierarchical	O
Token	B-Algorithm
Bucket	I-Algorithm
implements	O
a	O
classful	O
queuing	O
mechanism	O
for	O
the	O
Linux	B-Application
traffic	B-Protocol
control	O
system	O
,	O
and	O
provides	O
rate	O
and	O
ceil	O
to	O
allow	O
the	O
user	O
to	O
control	O
the	O
absolute	O
bandwidth	O
to	O
particular	O
classes	O
of	O
traffic	B-Protocol
as	O
well	O
as	O
indicate	O
the	O
ratio	O
of	O
distribution	O
of	O
bandwidth	O
when	O
extra	O
bandwidth	O
become	O
available	O
(	O
up	O
to	O
ceil	O
)	O
.	O
</s>
<s>
When	O
choosing	O
the	O
bandwidth	O
for	O
a	O
top-level	O
class	O
,	O
traffic	B-Protocol
shaping	O
only	O
helps	O
at	O
the	O
bottleneck	O
between	O
the	O
LAN	O
and	O
the	O
Internet	O
.	O
</s>
