How to simulate network packet loss and delay in Linux

How to simulate network packet loss and delay in Linux

netem and tc:

netem is a network simulation module provided by Linux kernel versions 2.6 and above. This functional module can be used to simulate complex Internet transmission performance, such as low bandwidth, transmission delay, packet loss, etc., in a local area network with good performance. Many Linux distributions using Linux 2.6 (or above) kernel version have enabled this kernel feature, such as Fedora, Ubuntu, Redhat, OpenSuse, CentOS, Debian, etc.

tc is a tool in the Linux system, and its full name is traffic control. tc can be used to control the working mode of netem. That is to say, if you want to use netem, at least two conditions are required. One is that the netem function in the kernel is included, and the other is that tc is required.

It should be noted that the flow control introduced in this article can only control the packet sending action, but not the packet receiving action. At the same time, it takes effect directly on the physical interface. If the physical eth0 is controlled, the logical network card (such as eth0:1) will also be affected. Conversely, if you control the logical network card, the control may be invalid. (Note: Multiple network cards in a virtual machine can be regarded as multiple physical network cards in the virtual machine).

Linux has a tc tool, namely traffic control, which can be used to simulate network packet loss and delay. When developing a backend server, if we want to know whether the server can perform well under a specific network packet loss condition, we can use tc to simulate the packet loss rate.

Simulating packet loss

The following command can simulate packet loss, and 10% of the packets going out of the eth0 network port will be randomly lost:

sudo tc qdisc add dev eth0 root netem loss 10%

Analog Delay

The following command will delay the packets sent out of the eth0 network port by 40ms:

sudo tc qdisc add dev eth0 root netem delay 40ms

Packet loss and latency in specific scenarios

Note that the commands introduced above are effective for the entire eth0 network port, that is, all packets going out from eth0 will experience random packet loss or delay. But sometimes, we only want packet loss and delay to affect a certain destination address. What should we do?

sudo tc qdisc add dev eth0 root handle 1: prio
sudo tc qdisc add dev eth0 parent 1:3 handle 30: netem loss 13% delay 40ms
sudo tc filter add dev eth0 protocol ip parent 1:0 u32 match ip dst 199.91.72.192 match ip dport 36000 0xffff flowid 1:3

In the above command, we tell tc to generate 13% packet loss and 40ms delay for network packets sent to 199.91.72.192:36000, while network packets sent to other destination addresses will not be affected.

Deleting a rule

OK, after simulating packet loss and latency, remember to delete the rule:

sudo tc qdisc del dev eth0 root

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to set up dual network cards in Linux
  • Linux network programming: reliable file transfer example based on UDP
  • Use netstat to view network status and port status in Linux
  • Linux command learning: 10 network commands and monitoring commands
  • Two scripts to check the Linux network status
  • Script for monitoring network traffic under Linux
  • Introduction to Linux basic network configuration method
  • Linux detects whether the server is connected to the network
  • Common network commands in Linux
  • One of the network monitoring technologies under Linux

<<:  React Hook usage examples (6 common hooks)

>>:  MySQL exposes Riddle vulnerability that can cause username and password leakage

Recommend

Example of using rem to replace px in vue project

Table of contents tool Install the plugin Add a ....

How to Fix File System Errors in Linux Using ‘fsck’

Preface The file system is responsible for organi...

MySQL 5.7.17 installation and configuration method graphic tutorial

This article shares the installation and configur...

What are the advantages of using B+Tree as an index in MySQL?

Table of contents Why do databases need indexes? ...

Using react-virtualized to implement a long list of images with dynamic height

Table of contents Problems encountered during dev...

File sharing between Ubuntu and Windows under VMware

This article records the method of sharing files ...

Installation and configuration method of Zabbix Agent on Linux platform

Here is a brief summary of the installation and c...

Reasons and methods for Waiting for table metadata lock in MySQL

When MySQL performs DDL operations such as alter ...

Some common properties of CSS

CSS background: background:#00ffee; //Set the back...

Detailed explanation of JavaScript clipboard usage

(1) Introduction: clipboard.js is a lightweight J...

JS implements simple calendar effect

This article shares the specific code of JS to ac...

A brief introduction to the usage of decimal type in MySQL

The floating-point types supported in MySQL are F...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

How to smoothly upgrade nginx after compiling and installing nginx

After nginx is compiled and installed and used fo...