Product SiteDocumentation Site

10.4. 品质服务

10.4.1. 原则与机制

服务品质 (Quality of Service 或缩写为 QoS) 系指提供应用时足以保证或改进服务品质的技术。最常见的技术包括分类网络流量、及依照其类别区分流量的作用。这种区分服务的概念是 流量成形,限制部分服务或主机的数据传输率,使其不致塞饱可用的带宽以及压缩其他的服务。流量成形是 TCP 流量的特定形式,此协议自动调整可用的频。
也可能改变流量的优先级,允许与交互服务有关的 (如 sshtelnet) 或只服务小量数据的封包优先处理。
Debian 核心包括 QoS 必备的功能与相关的模块。这些模块提供多样服务,多半系供 IP 封包数组使用;这些数组行为包括多种可能的需求。

10.4.2. 配置与应用

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

10.4.2.1. 减少延迟:wondershaper

wondershaper (在同名的软件包中) 的主要目的是最小化独立网络负载的延迟。经由限制整体流量的值缩短链接的饱和值。
Once a network interface is configured, setting up this traffic limitation is achieved by running wondershaper interface download_rate upload_rate. The interface can be enp1s0, eth0 or ppp0 for example, and both rates are expressed in kilobits per second. The wondershaper remove interface command disables traffic control on the specified interface.
For an Ethernet connection, historically this script would be 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. Or 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. Below is an example using this first method:

例 10.9. 对文件/etc/network/interfaces的变更

iface eth0 inet dhcp
    up /sbin/wondershaper eth0 500 100
    down /sbin/wondershaper remove eth0

10.4.2.2. 标准配置

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 DSCP field (Differentiated Services Code Point) of this packet; modifying this 6-bit field is enough to take advantage of the scheduling features. Refer to https://en.wikipedia.org/wiki/Differentiated_services#Class_Selector for more information.
The DSCP 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, note that the DSCP field must be set in hexadecimal:
nft add table ip mangle
nft add rule ip mangle PREROUTING tcp sport 22 counter ip dscp set 0x04
nft add rule ip mangle PREROUTING tcp dport 22 counter ip dscp set 0x04