15.8. 终极流量调节器:低延迟、快速上传与下载

注意:此脚本最近已升级,以前仅适用于您网络中的 Linux 客户端!因此,如果您的网络中有 Windows 机器或 Mac,并且注意到它们在其他人上传时无法更快地下载,您可能需要更新。

我试图创造圣杯

始终保持交互式流量的低延迟

这意味着下载或上传文件不应干扰 SSH 甚至 telnet。这些是最重要的事情,即使 200 毫秒的延迟也让人难以忍受。

允许在上传或下载时以合理的速度“冲浪”

即使 http 是“批量”流量,其他流量也不应过度淹没它。

确保上传不会损害下载,反之亦然

这是一个经常观察到的现象,上行流量会严重破坏下载速度。

事实证明,所有这些都是可能的,代价是牺牲一点点带宽。上传、下载和 ssh 互相影响的原因是许多家用接入设备(如电缆或 DSL 调制解调器)中存在大型队列。

下一节将深入解释导致延迟的原因,以及我们如何修复它们。如果您不关心魔术是如何实现的,您可以安全地跳过它,直接阅读脚本。

15.8.1. 为什么默认情况下效果不佳

ISP 知道他们的基准完全取决于人们下载的速度。除了可用带宽外,下载速度还受到数据包丢失的严重影响,这严重阻碍了 TCP/IP 的性能。大型队列可以帮助防止数据包丢失,并加快下载速度。因此,ISP 配置了大型队列。

然而,这些大型队列损害了交互性。一个击键必须首先通过上行队列(可能长达数秒!)并到达您的远程主机。然后它会被显示出来,这会导致一个数据包返回,然后该数据包必须遍历位于您的 ISP 的下行队列,然后才会出现在您的屏幕上。

本 HOWTO 教您如何以多种方式处理和操作队列,但遗憾的是,并非所有队列我们都可以访问。ISP 上的队列完全禁止访问,而上行队列可能位于您的电缆调制解调器或 DSL 设备内部。您或许可以配置它,也可能无法配置。很可能无法配置。

那么,接下来怎么办?由于我们无法控制这些队列中的任何一个,因此必须消除它们,并将它们移动到您的 Linux 路由器。幸运的是,这是可能的。

限制上传速度

通过将我们的上传速度限制为略低于真正可用的速率,我们的调制解调器中不会建立队列。队列现在被移动到 Linux。

限制下载速度

这稍微棘手,因为我们无法真正影响互联网向我们发送数据的速度。但是,我们可以丢弃传入速度过快的数据包,这会导致 TCP/IP 减速到我们想要的速度。因为我们不想不必要地丢弃流量,所以我们配置了一个“突发”大小,允许以更高的速度进行。

现在,一旦我们完成此操作,我们就完全消除了下行队列(除了短时突发),并获得了管理上行队列的能力,并拥有 Linux 提供的所有强大功能。

接下来要做的是确保交互式流量跳转到上行队列的前面。为了确保上传不会损害下载,我们还将 ACK 数据包移动到队列的前面。这通常是导致双向生成批量流量时观察到的巨大减速的原因。下行流量的确认必须与上行流量竞争,并在过程中被延迟。

如果我们完成所有这些操作,我们将获得以下使用荷兰 xs4all 提供的优秀 ADSL 连接的测量结果

Baseline latency:
round-trip min/avg/max = 14.4/17.1/21.7 ms

Without traffic conditioner, while downloading:
round-trip min/avg/max = 560.9/573.6/586.4 ms

Without traffic conditioner, while uploading:
round-trip min/avg/max = 2041.4/2332.1/2427.6 ms

With conditioner, during 220kbit/s upload:
round-trip min/avg/max = 15.7/51.8/79.9 ms

With conditioner, during 850kbit/s download:
round-trip min/avg/max = 20.4/46.9/74.0 ms

When uploading, downloads proceed at ~80% of the available speed. Uploads
at around 90%. Latency then jumps to 850 ms, still figuring out why.

您可以从此脚本中期望获得的结果很大程度上取决于您的实际上行链路速度。当以全速上传时,在您的击键之前总会有一个数据包。这是您可以达到的延迟的下限 - 将您的 MTU 除以上行速度即可计算出来。典型值将略高于此值。降低您的 MTU 以获得更好的效果!

接下来,是此脚本的两个版本,一个使用 Devik 出色的 HTB,另一个使用每个 Linux 内核中都包含的 CBQ,这与 HTB 不同。两者都经过测试,效果良好。

15.8.2. 实际脚本 (CBQ)

适用于所有内核。在 CBQ qdisc 中,我们放置了两个随机公平队列,以确保多个批量流不会相互淹没。

下行流量使用包含令牌桶过滤器的 tc 过滤器进行监管。

您可以通过在以“tc class add .. classid 1:20”开头的行中添加“bounded”来改进此脚本。如果您降低了 MTU,也请降低 allot & avpkt 数字!

#!/bin/bash 

# The Ultimate Setup For Your Internet Connection At Home
# 
#
# Set the following values to somewhat less than your actual download
# and uplink speed. In kilobits
DOWNLINK=800
UPLINK=220
DEV=ppp0

# clean existing down- and uplink qdiscs, hide errors
tc qdisc del dev $DEV root    2> /dev/null > /dev/null
tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null

###### uplink

# install root CBQ

tc qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth 10mbit 

# shape everything at $UPLINK speed - this prevents huge queues in your
# DSL modem which destroy latency:
# main class

tc class add dev $DEV parent 1: classid 1:1 cbq rate ${UPLINK}kbit \
allot 1500 prio 5 bounded isolated 

# high prio class 1:10:

tc class add dev $DEV parent 1:1 classid 1:10 cbq rate ${UPLINK}kbit \
   allot 1600 prio 1 avpkt 1000

# bulk and default class 1:20 - gets slightly less traffic, 
#  and a lower priority:

tc class add dev $DEV parent 1:1 classid 1:20 cbq rate $[9*$UPLINK/10]kbit \
   allot 1600 prio 2 avpkt 1000

# both get Stochastic Fairness:
tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10

# start filters
# TOS Minimum Delay (ssh, NOT scp) in 1:10:
tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
      match ip tos 0x10 0xff  flowid 1:10

# ICMP (ip protocol 1) in the interactive class 1:10 so we 
# can do measurements & impress our friends:
tc filter add dev $DEV parent 1:0 protocol ip prio 11 u32 \
	match ip protocol 1 0xff flowid 1:10

# To speed up downloads while an upload is going on, put ACK packets in
# the interactive class:

tc filter add dev $DEV parent 1: protocol ip prio 12 u32 \
   match ip protocol 6 0xff \
   match u8 0x05 0x0f at 0 \
   match u16 0x0000 0xffc0 at 2 \
   match u8 0x10 0xff at 33 \
   flowid 1:10

# rest is 'non-interactive' ie 'bulk' and ends up in 1:20

tc filter add dev $DEV parent 1: protocol ip prio 13 u32 \
   match ip dst 0.0.0.0/0 flowid 1:20

########## downlink #############
# slow downloads down to somewhat less than the real speed  to prevent 
# queuing at our ISP. Tune to see how high you can set it.
# ISPs tend to have *huge* queues to make sure big downloads are fast
#
# attach ingress policer:

tc qdisc add dev $DEV handle ffff: ingress

# filter *everything* to it (0.0.0.0/0), drop everything that's
# coming in too fast:

tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
   0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
如果您希望此脚本在 ppp 连接时运行,请将其复制到 /etc/ppp/ip-up.d。

如果最后两行给出错误,请将您的 tc 工具更新到较新版本!

15.8.3. 实际脚本 (HTB)

以下脚本使用出色的 HTB 队列实现了所有目标,请参阅相关章节。非常值得为您的内核打补丁!
#!/bin/bash

# The Ultimate Setup For Your Internet Connection At Home
# 
#
# Set the following values to somewhat less than your actual download
# and uplink speed. In kilobits
DOWNLINK=800
UPLINK=220
DEV=ppp0

# clean existing down- and uplink qdiscs, hide errors
tc qdisc del dev $DEV root    2> /dev/null > /dev/null
tc qdisc del dev $DEV ingress 2> /dev/null > /dev/null

###### uplink

# install root HTB, point default traffic to 1:20:

tc qdisc add dev $DEV root handle 1: htb default 20

# shape everything at $UPLINK speed - this prevents huge queues in your
# DSL modem which destroy latency:

tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k

# high prio class 1:10:

tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${UPLINK}kbit \
   burst 6k prio 1

# bulk & default class 1:20 - gets slightly less traffic, 
# and a lower priority:

tc class add dev $DEV parent 1:1 classid 1:20 htb rate $[9*$UPLINK/10]kbit \
   burst 6k prio 2

# both get Stochastic Fairness:
tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10

# TOS Minimum Delay (ssh, NOT scp) in 1:10:
tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
      match ip tos 0x10 0xff  flowid 1:10

# ICMP (ip protocol 1) in the interactive class 1:10 so we 
# can do measurements & impress our friends:
tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
	match ip protocol 1 0xff flowid 1:10

# To speed up downloads while an upload is going on, put ACK packets in
# the interactive class:

tc filter add dev $DEV parent 1: protocol ip prio 10 u32 \
   match ip protocol 6 0xff \
   match u8 0x05 0x0f at 0 \
   match u16 0x0000 0xffc0 at 2 \
   match u8 0x10 0xff at 33 \
   flowid 1:10

# rest is 'non-interactive' ie 'bulk' and ends up in 1:20


########## downlink #############
# slow downloads down to somewhat less than the real speed  to prevent 
# queuing at our ISP. Tune to see how high you can set it.
# ISPs tend to have *huge* queues to make sure big downloads are fast
#
# attach ingress policer:

tc qdisc add dev $DEV handle ffff: ingress

# filter *everything* to it (0.0.0.0/0), drop everything that's
# coming in too fast:

tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
   0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1

如果您希望此脚本在 ppp 连接时运行,请将其复制到 /etc/ppp/ip-up.d。

如果最后两行给出错误,请将您的 tc 工具更新到较新版本!