10.3. 品质服务

10.3.1. 原则与机制

服务品质 (Quality of Service 或缩写为 QoS) 系指提供应用时足以保证或改进服务品质的技术。最常见的技术包括分类网络流量、及依照其类别区分流量的作用。这种区分服务的概念是 流量成形,限制部分服务或主机的数据传输率,使其不致塞饱可用的带宽以及压缩其他的服务。流量成形是 TCP 流量的特定形式,此协议自动调整可用的频。

也可能改变流量的优先级,允许与交互服务有关的 (如 sshtelnet) 或只服务小量数据的封包优先处理。

Debian 核心包括 QoS 必备的功能与相关的模块。这些模块提供多样服务,多半系供 IP 封包数组使用;这些数组行为包括多种可能的需求。

文化 LARTC — Linux 高端路由与流量控制

Linux 高端路由与流量控制 做法是一份包括网络服务品质的参考文档。

http://www.lartc.org/howto/

10.3.2. 配置与应用

经由 tc 命令 (由 iproute 软件包提供) 设置 QoS 参数。因为其接口较为复杂,建议使用高端的工具。

10.3.2.1. 减少延迟:wondershaper

wondershaper (在同名的软件包中) 的主要目的是最小化独立网络负载的延迟。经由限制整体流量的值缩短链接的饱和值。

配置网络接口后,运行 wondershaper *interface* *download_rate* *upload_rate*命令,设置流量控制。可以是 eth0ppp0 接口,两者的速度均以每秒千位为单位。wondershaper remove *interface* 命令可以在特定接口停用流量控制。

For an Ethernet connection, this script is best called right after the interface is configured. This is done by adding up and down directives to the /etc/network/interfaces file allowing declared commands to be run, respectively, after the interface is configured and before it is deconfigured. For example:

例 10.9. Changes in the /etc/network/interfaces file

  1. iface eth0 inet dhcp
  2. up /sbin/wondershaper eth0 500 100
  3. down /sbin/wondershaper remove eth0

In the PPP case, creating a script that calls wondershaper in /etc/ppp/ip-up.d/ will enable traffic control as soon as the connection is up.

GOING FURTHER Optimal configuration

The /usr/share/doc/wondershaper/README.Debian.gz file describes, in some detail, the configuration method recommended by the package maintainer. In particular, it advises measuring the download and upload speeds so as to best evaluate real limits.

10.3.2.2. Standard Configuration

Barring a specific QoS configuration, the Linux kernel uses the pfifo_fast queue scheduler, which provides a few interesting features by itself. The priority of each processed IP packet is based on the ToS field (Type of Service) of this packet; modifying this field is enough to take advantage of the scheduling features. There are five possible values:

  • Normal-Service (0);

  • Minimize-Cost (2);

  • Maximize-Reliability (4);

  • Maximize-Throughput (8);

  • Minimize-Delay (16).

The ToS field can be set by applications that generate IP packets, or modified on the fly by netfilter. The following rules are sufficient to increase responsiveness for a server’s SSH service:

  1. iptables -t mangle -A PREROUTING -p tcp --sport ssh -j TOS --set-tos Minimize-Delay
  2. iptables -t mangle -A PREROUTING -p tcp --dport ssh -j TOS --set-tos Minimize-Delay