LinuxCommandLibrary

tc-netem

Simulate network impairments for testing

SYNOPSIS

tc qdisc { add | change | replace | del } dev <DEV> { root | parent <HANDLE> } [handle <MAJOR>:] netem [OPTIONS]

PARAMETERS

add | change | replace | del
    Action to perform on the queuing discipline: add a new qdisc, change an existing one, replace an existing one, or del (delete) it.

dev <DEV>
    Specifies the network interface (e.g., eth0, enp0s3) on which to apply the netem emulation.

root | parent <HANDLE>
    Determines where the netem qdisc is attached in the qdisc hierarchy. root attaches it at the top level of the interface's qdisc tree. parent <HANDLE> attaches it as a child of an existing qdisc identified by its handle.

handle <MAJOR>:
    Assigns a unique identifier (major number) to the netem qdisc, allowing it to be referenced by other tc commands or sub-qdiscs. This is optional if using root with no children.

delay <TIME> [JITTER [CORRELATION]]
    Introduces a specified latency to packets.
TIME: The base delay in milliseconds (e.g., '100ms').
JITTER: Optional; adds random variation to the delay (e.g., '20ms').
CORRELATION: Optional; a percentage (0-100%) indicating how strongly the delay of one packet correlates with the next, creating burstier or smoother delay patterns.

distribution { normal | pareto | paretonormal }
    Specifies the statistical distribution for delay values when JITTER is used, providing more realistic delay patterns than a uniform distribution.

loss { PERCENT [CORRELATION] | GONE }
    Simulates packet loss.
PERCENT: The probability of a packet being dropped (e.g., '5%').
CORRELATION: Optional; a percentage (0-100%) indicating the correlation between consecutive packet losses, simulating bursty loss.
GONE: (Deprecated) Causes permanent packet loss for all subsequent packets.

duplicate PERCENT [CORRELATION]
    Duplicates packets with a given probability.
PERCENT: The percentage probability of duplicating a packet (e.g., '2%').
CORRELATION: Optional; a percentage (0-100%) indicating correlation for duplication patterns.

corrupt PERCENT [CORRELATION]
    Corrupts packets with a given probability (introduces bit errors).
PERCENT: The percentage probability of a packet being corrupted (e.g., '0.1%').
CORRELATION: Optional; a percentage (0-100%) indicating correlation for corruption patterns.

reorder PERCENT [CORRELATION] [gap JITTER]
    Reorders packets, sending some packets out of their original sequence.
PERCENT: The probability that a packet will be sent immediately (out of order) rather than being delayed.
CORRELATION: Optional; a percentage (0-100%) influencing the reordering pattern.
gap JITTER: Optional; influences the gap between reordered packets.

rate RATE [PACKETOVERHEAD BYTES] [CELLSIZE BYTES] [CELLOVERHEAD BYTES]
    Emulates a fixed bandwidth rate. This is a basic rate limiter within netem.
RATE: The desired bandwidth, e.g., '1mbit', '512kbit'.
Additional parameters (PACKETOVERHEAD, CELLSIZE, CELLOVERHEAD) allow for fine-tuning the rate calculation based on network layer overheads and cell-based network characteristics.

ecn
    Enables Explicit Congestion Notification (ECN) marking for packets under congestion, rather than dropping them. Requires kernel support.

DESCRIPTION

tc-netem is a powerful queuing discipline (qdisc) within the Linux traffic control (tc) subsystem. Its primary purpose is to emulate various characteristics of real-world networks, enabling developers and testers to simulate adverse network conditions without needing a physically impaired network.

Netem can introduce latency (delay), packet loss, packet corruption, packet duplication, and packet reordering. It can also emulate a fixed bandwidth rate. This functionality is invaluable for testing the robustness and performance of applications, network protocols, and services under non-ideal network scenarios, such as high-latency satellite links, unreliable wireless connections, or congested internet paths.

Typically applied to an egress (outgoing) network interface, netem manipulates packets as they leave the system. Its flexibility allows for fine-grained control over network impairments, making it a crucial tool for quality assurance, development, and research in network-dependent systems.

CAVEATS

  • Egress Traffic Only: Netem primarily affects outgoing (egress) traffic on the specified interface. To apply network emulation to incoming (ingress) traffic, it's typically necessary to use an ifb (Intermediate Functional Block) device in conjunction with an ingress qdisc to redirect ingress traffic for shaping.
  • Basic Rate Limiting: While netem includes a rate parameter, for complex or precise bandwidth shaping scenarios, it is often more effective to combine netem with dedicated rate-limiting qdiscs like tbf (Token Bucket Filter) or htb (Hierarchical Token Bucket).
  • Root Privileges: Configuring netem rules requires root privileges (specifically, the CAP_NET_ADMIN capability).

COMMON USE CASES

Netem is widely used for:

  • Simulating Wide Area Network (WAN) conditions to test applications designed for distributed environments.
  • Evaluating the resilience and performance of network protocols (e.g., TCP, UDP) under adverse conditions like high latency or packet loss.
  • Benchmarking software and services to understand their behavior and limitations when network quality degrades.
  • Developing and testing error-handling mechanisms in network-aware applications.

CLEARING NETEM RULES

To remove all netem (or any other root) rules from an interface and restore its default traffic behavior, use the following command:
tc qdisc del dev <DEV> root
Replace <DEV> with the actual network interface name.

HISTORY

Netem was developed by Stephen Hemminger and Jamal Hadi Salim, becoming a standard part of the Linux kernel's traffic control framework. It was introduced around the Linux kernel 2.6.x series, significantly enhancing the kernel's capabilities for network testing and research. Initially, it provided basic delay and packet loss simulation, but over time, its feature set expanded to include corruption, duplication, and reordering, making it a comprehensive tool for emulating a wide range of real-world network conditions.

SEE ALSO

tc(8), tc-qdisc(8), tc-tbf(8), tc-htb(8), ip(8), ifb(4)

Copied to clipboard