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

What does mysql database do?

MySQL is a relational database management system....

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...

Nginx access log and error log parameter description

illustrate: There are two main types of nginx log...

RGBA alpha transparency conversion calculation table

Conversion between rgba and filter values ​​under...

Linux command line operation Baidu cloud upload and download files

Table of contents 0. Background 1. Installation 2...

Detailed process of configuring NIS in Centos7

Table of contents principle Network environment p...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

Web Standard Application: Redesign of Tencent QQ Home Page

Tencent QQ’s homepage has been redesigned, and Web...

Centos7 mysql database installation and configuration tutorial

1. System environment The system version after yu...

Windows cannot start MySQL service and reports error 1067 solution

Suddenly when I logged into MySQL, it said that a...

Summary of the use of Datetime and Timestamp in MySQL

Table of contents 1. How to represent the current...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

Detailed explanation of Nodejs array queue and forEach application

This article mainly records the problems and solu...

How to retrieve password for mysql 8.0.22 on Mac

Mac latest version of MySQL 8.0.22 password recov...

The difference between html empty link href="#" and href="javascript:void(0)"

# contains a location information. The default anc...