tc - tbf

最後更新: 2024-07-11

目錄

  • Token bucket (Algorithm)
  • 應用

Token bucket (Algorithm)

 

tbf = The Token Bucket Filter

運作

- A token is added to the bucket every 1/r seconds.
- token:data => 1:1
- The bucket can hold at the most b tokens.
- Each token is permission for the source to send a specific number of bits into the network.

r = Average rate

b = Accumulation of token => allows a short burst

If no tokens are available, packets are queued, up to a configured limit.

 * This limit is caused by the fact that the kernel can only throttle for at minimum 1 'jiffy',
    which depends on HZ as 1/HZ.

e.g.

only a single packet can get sent per jiffy - for HZ=100,
  this means 100 packets of on average 1000 bytes each,
  which roughly corresponds to 1mbit/s.

 * 設定限速時, 必須 rate, burst, latency 缺一不可 !!

Help

man 8 tc-tbf

Either "limit" or "latency" is required

 * latency => maximum amount of time a packet can sit in the TBF

 * limit => the number of bytes that can be queued waiting for tokens to become available.

Settings

 * burst    => Size of the bucket, in bytes.

                     Also known as buffer

                     This is the maximum amount of bytes that tokens can be available for instantaneously.

                     If your buffer is too small, packets may be dropped,
                       because more tokens arrive per timer tick than fit in your bucket.

                     The minimum  buffer  size  can  be  calculated by dividing the rate by HZ.

                     * 10mbit/s => need at least 10kbyte buffer

* mpu       => A zero-sized packet does not use zero bandwidth. (Unit: bytes)

                      For ethernet, no packet uses less than 64 bytes.


應用

 

應用 1)

# 對 Container 的 NIC(ollama) 限速

Guest DL:

(Host)ollama -Outgoing-> eth0(Guest)

Guest UP:

(Host)ollama <-Incoming- eth0(Guest)

CLI

NIC=ollama

tc qdisc add dev $NIC root tbf rate 30mbit burst 5mb latency 500ms

tc qdisc show dev $NIC             # 查看. Default: "qdisc noqueue 0: root refcnt 2"

tc qdisc del dev $NIC root         # 取消限速

應用 2)

# If you have a networking device with a large queue,
# like a DSL modem or a cable modem(ppp0),
# and you talk to it over a fast device, like over an ethernet interface,
# you will find that uploading absolutely destroys interactivity.

tc qdisc add dev ppp0 root tbf rate 220kbit latency 50ms burst 1540

 

 

 

 

Creative Commons license icon Creative Commons license icon