Este contenido no está disponible en el idioma seleccionado.
Chapter 33. Linux traffic control
Linux Traffic Control (TC) helps in policing, classifying, shaping, and scheduling network traffic. It uses queuing disciplines (qdisc) and filters to manage and manipulate packet transmission, including mangling packet content.
The scheduling mechanism arranges or rearranges the packets before they enter or exit different queues. The most common scheduler is the First-In-First-Out (FIFO) scheduler. You can do the qdiscs operations temporarily using the tc utility or permanently using NetworkManager.
In Red Hat Enterprise Linux, you can configure default queueing disciplines in various ways to manage the traffic on a network interface.
33.1. Overview of queuing disciplines Copiar enlaceEnlace copiado en el portapapeles!
Queuing disciplines (qdiscs) help with queuing up and, later, scheduling of traffic transmission by a network interface.
A qdisc has two operations;
- enqueue requests so that a packet can be queued up for later transmission and
- dequeue requests so that one of the queued-up packets can be chosen for immediate transmission.
Every qdisc has a 16-bit hexadecimal identification number called a handle, with an attached colon, such as 1: or abcd:. This number is called the qdisc major number. If a qdisc has classes, then the identifiers are formed as a pair of two numbers with the major number before the minor, <major>:<minor>, for example abcd:1. The numbering scheme for the minor numbers depends on the qdisc type. Sometimes the numbering is systematic, where the first-class has the ID <major>:1, the second one <major>:2, and so on. Some qdiscs allow the user to set class minor numbers arbitrarily when creating the class.
- Classful
qdiscs Different types of
qdiscsexist and help in the transfer of packets to and from a networking interface. You can configureqdiscswith root, parent, or child classes. The point where children can be attached are called classes. Classes inqdiscare flexible and can always contain either multiple children classes or a single child,qdisc. There is no prohibition against a class containing a classfulqdiscitself, this facilitates complex traffic control scenarios.Classful
qdiscsdo not store any packets themselves. Instead, they enqueue and dequeue requests down to one of their children according to criteria specific to theqdisc. Eventually, this recursive packet passing ends up where the packets are stored (or picked up from in the case of dequeuing).- Classless
qdiscs -
Some
qdiscscontain no child classes and they are called classlessqdiscs. Classlessqdiscsrequire less customization compared to classfulqdiscs. It is usually enough to attach them to an interface.
33.2. Introduction to connection tracking Copiar enlaceEnlace copiado en el portapapeles!
Connection tracking is a Linux kernel networking feature for logical networks that tracks connections and identifies packet flow in those connections.
At a firewall, the Netfilter framework filters packets from an external network. After a packet arrives, Netfilter assigns a connection tracking entry. Connection tracking filters and analyzes every packet, sets up the connection tracking table to store connection status, and updates the connection status based on identified packets. For example, in the case of FTP connection, Netfilter assigns a connection tracking entry to ensure all packets of FTP connection work in the same manner. The connection tracking entry stores a Netfilter mark and tracks the connection state information in the memory table in which a new packet tuple maps with an existing entry. If the packet tuple does not map with an existing entry, the packet adds a new connection tracking entry that groups packets of the same connection.
You can control and analyze traffic on the network interface. The tc traffic controller utility uses the qdisc discipline to configure the packet scheduler in the network. The qdisc kernel-configured queuing discipline enqueues packets to the interface. By using qdisc, Kernel catches all the traffic before a network interface transmits it. Also, to limit the bandwidth rate of packets belonging to the same connection, use the tc qdisc command.
To retrieve data from connection tracking marks into various fields, use the tc utility with the ctinfo module and the connmark functionality. For storing packet mark information, the ctinfo module copies the Netfilter mark and the connection state information into a socket buffer (skb) mark metadata field.
Transmitting a packet over a physical medium removes all the metadata of a packet. Before the packet loses its metadata, the ctinfo module maps and copies the Netfilter mark value to a specific value of the Diffserv code point (DSCP) in the packet’s IP field.
33.3. Inspecting qdiscs of a network interface using the tc utility Copiar enlaceEnlace copiado en el portapapeles!
By default, Red Hat Enterprise Linux systems use fq_codel qdisc. You can inspect the qdisc counters using the tc utility.
Procedure
Optional: View your current
qdisc:tc qdisc show dev enp0s1
# tc qdisc show dev enp0s1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Inspect the current
qdisccounters:tc -s qdisc show dev enp0s1
# tc -s qdisc show dev enp0s1 qdisc fq_codel 0: root refcnt 2 limit 10240p flows 1024 quantum 1514 target 5.0ms interval 100.0ms memory_limit 32Mb ecn Sent 1008193 bytes 5559 pkt (dropped 233, overlimits 55 requeues 77) backlog 0b 0p requeues 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
dropped- the number of times a packet is dropped because all queues are full -
overlimits- the number of times the configured link capacity is filled -
sent- the number of dequeues
-
33.4. Updating the default qdisc Copiar enlaceEnlace copiado en el portapapeles!
If you observe networking packet losses with the current qdisc, you can change the qdisc based on your network-requirements.
Procedure
View the current default
qdisc:sysctl -a | grep qdisc
# sysctl -a | grep qdisc net.core.default_qdisc = fq_codelCopy to Clipboard Copied! Toggle word wrap Toggle overflow View the
qdiscof current Ethernet connection:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the existing
qdisc:sysctl -w net.core.default_qdisc=pfifo_fast
# sysctl -w net.core.default_qdisc=pfifo_fastCopy to Clipboard Copied! Toggle word wrap Toggle overflow To apply the changes, reload the network driver:
modprobe -r NETWORKDRIVERNAME modprobe NETWORKDRIVERNAME
# modprobe -r NETWORKDRIVERNAME # modprobe NETWORKDRIVERNAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the network interface:
ip link set enp0s1 up
# ip link set enp0s1 upCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
View the
qdiscof the Ethernet connection:tc -s qdisc show dev enp0s1
# tc -s qdisc show dev enp0s1 qdisc pfifo_fast 0: root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1 Sent 373186 bytes 5333 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 ....Copy to Clipboard Copied! Toggle word wrap Toggle overflow
33.5. Temporarily setting the current qdisc of a network interface using the tc utility Copiar enlaceEnlace copiado en el portapapeles!
Changing the current qdisc can optimize how the kernel handles packets as they pass through a network interface. For testing and benchmarking purposes, you can change the qdisc temporarily.
Procedure
Optional: View the current
qdisc:tc -s qdisc show dev enp0s1
# tc -s qdisc show dev enp0s1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the current
qdisc:tc qdisc replace dev enp0s1 root htb
# tc qdisc replace dev enp0s1 root htbCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
View the updated current
qdisc:tc -s qdisc show dev enp0s1
# tc -s qdisc show dev enp0s1 qdisc htb 8001: root refcnt 2 r2q 10 default 0 direct_packets_stat 0 direct_qlen 1000 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow
33.6. Permanently setting the current qdisc of a network interface using NetworkManager Copiar enlaceEnlace copiado en el portapapeles!
Changing the current qdisc can optimize how the kernel handles packets as they pass through a network interface. If you identified the optimal qdisc for your scenario, set it permanently in the NetworkManager connection profile.
Procedure
Optional: View the current
qdisc:tc qdisc show dev enp0s1
# tc qdisc show dev enp0s1 qdisc fq_codel 0: root refcnt 2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the current
qdisc:nmcli connection modify enp0s1 tc.qdiscs 'root pfifo_fast'
# nmcli connection modify enp0s1 tc.qdiscs 'root pfifo_fast'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To add another
qdiscover the existingqdisc, use the+tc.qdiscoption:nmcli connection modify enp0s1 +tc.qdisc 'ingress handle ffff:'
# nmcli connection modify enp0s1 +tc.qdisc 'ingress handle ffff:'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Activate the changes:
nmcli connection up enp0s1
# nmcli connection up enp0s1Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
View current
qdiscthe network interface:tc qdisc show dev enp0s1
# tc qdisc show dev enp0s1 qdisc pfifo_fast 8001: root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1 qdisc ingress ffff: parent ffff:fff1 ----------------Copy to Clipboard Copied! Toggle word wrap Toggle overflow
33.7. Configuring the rate limiting of packets by using the tc-ctinfo utility Copiar enlaceEnlace copiado en el portapapeles!
With rate limiting, you can limit network traffic, prevent the exhaustion of resources in the network, and reduce the load on servers by limiting repetitive packet requests in a specific time frame.
The connection tracking entry stores the Netfilter mark and connection information. When a router forwards a packet from the firewall, the router either removes or modifies the connection tracking entry from the packet. The connection tracking information (ctinfo) module retrieves data from connection tracking marks into various fields. This kernel module preserves the Netfilter mark by copying it into a socket buffer (skb) mark metadata field.
Prerequisites
-
The
iperf3utility is installed on a server and a client.
Procedure
Perform the following steps on the server:
Add a virtual link to the network interface:
ip link add name ifb4eth0 numtxqueues 48 numrxqueues 48 type ifb
# ip link add name ifb4eth0 numtxqueues 48 numrxqueues 48 type ifbCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command has the following parameters:
name ifb4eth0- Sets a new virtual device interface.
numtxqueues 48- Sets the number of transmit queues.
numrxqueues 48- Sets the number of receive queues.
type ifb- Sets the type of the new device.
Change the state of the interface:
ip link set dev ifb4eth0 up
# ip link set dev ifb4eth0 upCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the
qdiscattribute on the physical network interface and apply it to the incoming traffic:tc qdisc add dev enp1s0 handle ffff: ingress
# tc qdisc add dev enp1s0 handle ffff: ingressCopy to Clipboard Copied! Toggle word wrap Toggle overflow In the
handle ffff:option, thehandleparameter assigns the major numberffff:as a default value to a classfulqdiscon theenp1s0physical network interface, whereqdiscis a queueing discipline parameter to analyze traffic control.Add a filter on the physical interface of the
ipprotocol to classify packets:tc filter add dev enp1s0 parent ffff: protocol ip u32 match u32 0 0 action ctinfo cpmark 100 action mirred egress redirect dev ifb4eth0
# tc filter add dev enp1s0 parent ffff: protocol ip u32 match u32 0 0 action ctinfo cpmark 100 action mirred egress redirect dev ifb4eth0Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command has the following attributes:
parent ffff:-
Sets major number
ffff:for the parentqdisc. u32 match u32 0 0-
Sets the
u32filter tomatchthe IP headers the ofu32pattern. The first0represents the second byte of IP header while the other0is for the mask match telling the filter which bits to match. action ctinfo- Sets action to retrieve data from the connection tracking mark into various fields.
cpmark 100-
Copies the connection tracking mark (connmark)
100into the packet IP header field. action mirred egress redirect dev ifb4eth0-
Sets the
actiontomirredto redirect the received packets to theifb4eth0destination interface.
Add a classful
qdiscto the interface:tc qdisc add dev ifb4eth0 root handle 1: htb default 1000
# tc qdisc add dev ifb4eth0 root handle 1: htb default 1000Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command sets the major number
1to rootqdiscand uses thehtbhierarchy token bucket with classfulqdiscof minor-id1000.Limit the traffic on the interface to 1 Mbit/s with an upper limit of 2 Mbit/s:
tc class add dev ifb4eth0 parent 1:1 classid 1:100 htb ceil 2mbit rate 1mbit prio 100
# tc class add dev ifb4eth0 parent 1:1 classid 1:100 htb ceil 2mbit rate 1mbit prio 100Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command has the following parameters:
parent 1:1-
Sets
parentwithclassidas1androotas1. classid 1:100-
Sets
classidas1:100where1is the number of parentqdiscand100is the number of classes of the parentqdisc. htb ceil 2mbit-
The
htbclassfulqdiscallows upper limit bandwidth of2 Mbit/sas theceilrate limit.
Apply the Stochastic Fairness Queuing (
sfq) of classlessqdiscto interface with a time interval of60seconds to reduce queue algorithm perturbation:tc qdisc add dev ifb4eth0 parent 1:100 sfq perturb 60
# tc qdisc add dev ifb4eth0 parent 1:100 sfq perturb 60Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the firewall mark (
fw) filter to the interface:tc filter add dev ifb4eth0 parent 1:0 protocol ip prio 100 handle 100 fw classid 1:100
# tc filter add dev ifb4eth0 parent 1:0 protocol ip prio 100 handle 100 fw classid 1:100Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restore the packet meta mark from the connection mark (
CONNMARK):nft add rule ip mangle PREROUTING counter meta mark set ct mark
# nft add rule ip mangle PREROUTING counter meta mark set ct markCopy to Clipboard Copied! Toggle word wrap Toggle overflow In this command, the
nftutility has amangletable with thePREROUTINGchain rule specification that alters incoming packets before routing to replace the packet mark withCONNMARK.If no
nfttable and chain exist, create a table and add a chain rule:nft add table ip mangle nft add chain ip mangle PREROUTING {type filter hook prerouting priority mangle \;}# nft add table ip mangle # nft add chain ip mangle PREROUTING {type filter hook prerouting priority mangle \;}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the meta mark on
tcppackets that are received on the specified destination address192.0.2.3:nft add rule ip mangle PREROUTING ip daddr 192.0.2.3 counter meta mark set 0x64
# nft add rule ip mangle PREROUTING ip daddr 192.0.2.3 counter meta mark set 0x64Copy to Clipboard Copied! Toggle word wrap Toggle overflow Save the packet mark into the connection mark:
nft add rule ip mangle PREROUTING counter ct mark set mark
# nft add rule ip mangle PREROUTING counter ct mark set markCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the
iperf3utility as the server on a system by using the-sparameter and the server then waits for the response of the client connection:iperf3 -s
# iperf3 -sCopy to Clipboard Copied! Toggle word wrap Toggle overflow
On the client, run
iperf3as a client and connect to the server that listens on IP address192.0.2.3for periodic HTTP request-response timestamp:iperf3 -c 192.0.2.3 -t TCP_STREAM | tee rate
# iperf3 -c 192.0.2.3 -t TCP_STREAM | tee rateCopy to Clipboard Copied! Toggle word wrap Toggle overflow 192.0.2.3is the IP address of the server while192.0.2.4is the IP address of the client.Terminate the
iperf3utility on the server by pressing Ctrl+C:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Terminate the
iperf3utility on the client by pressing Ctrl+C:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Display the statistics about packet counts of the
htbandsfqclasses on the interface:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display the statistics of packet counts for the
mirredandctinfoactions:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Display the statistics of the
htbrate-limiter and its configuration:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
33.8. Available qdiscs in RHEL Copiar enlaceEnlace copiado en el portapapeles!
Each qdisc addresses unique networking-related issues. You can use any of them to shape network traffic based on your networking requirements.
These qdiscs are available in RHEL:
qdisc name | Included in | Offload support |
|---|---|---|
| Credit-Based Shaper |
| Yes |
| CHOose and Keep for responsive flows, CHOose and Kill for unresponsive flows (CHOKE) |
| |
| Controlled Delay (CoDel) |
| |
| Enhanced Transmission Selection (ETS) |
| Yes |
| Fair Queue (FQ) |
| |
| Fair Queuing Controlled Delay (FQ_CODel) |
| |
| Generalized Random Early Detection (GRED) |
| |
| Hierarchical Fair Service Curve (HSFC) |
| |
| Heavy-Hitter Filter (HHF) |
| |
| Hierarchy Token Bucket (HTB) |
| Yes |
| INGRESS |
| Yes |
| Multi Queue Priority (MQPRIO) |
| Yes |
| Multiqueue (MULTIQ) |
| Yes |
| Network Emulator (NETEM) |
| |
| Proportional Integral-controller Enhanced (PIE) |
| |
| PLUG |
| |
| Quick Fair Queueing (QFQ) |
| |
| Random Early Detection (RED) |
| Yes |
| Stochastic Fair Blue (SFB) |
| |
| Stochastic Fairness Queueing (SFQ) |
| |
| Token Bucket Filter (TBF) |
| Yes |
| Trivial Link Equalizer (TEQL) |
|
The qdisc offload requires hardware and driver support on NIC.