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. Changes in the /etc/network/interfaces file

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

10.4.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 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