Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of simulating Network packet loss and delay by Linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Netem and tc:

Netem is a network simulation function module provided by Linux kernel version 2.6 and above. This function module can be used to simulate complex Internet transmission performance in a good local area network, such as low bandwidth, transmission delay, packet loss and so on. Many distributions of Linux that use Linux version 2.6 (or above) kernel features such as Fedora, Ubuntu, Redhat, OpenSuse, CentOS, Debian, and so on.

Tc is a tool in the Linux system, the full name is traffic control (flow Control). Tc can be used to control the working mode of netem, that is, if you want to use netem, you need at least two conditions, one is that the netem function in the kernel is included, and the other is to have tc.

(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, traffic control, that can be used to simulate network packet loss and latency. When developing background server, if we want to know whether this server can perform well in a particular network packet loss situation, we can use tc to simulate the packet loss rate.

Simulated packet loss

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

Sudo tc qdisc add dev eth0 root netem loss 10%

Analog delay

The following command is used to delay 40ms for packets leaving the eth0 network port:

Sudo tc qdisc add dev eth0 root netem delay 40ms

Packet loss and delay in specific scenarios

Note that the command we introduced above works for the entire eth0 port, that is, as long as all packets leaving the eth0 will cause random packet loss or delay. But sometimes we just want packet loss and delay to act on a destination address, so what do we do?

Sudo tc qdisc add dev eth0 root handle 1: priosudo tc qdisc add dev eth0 parent 1:3 handle 30: netem loss 13% delay 40mssudo 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 told tc that there would be a 13% packet loss and 40ms delay for network packets destined for 199.91.72.192 purge 36000, while network packets destined for other destinations would not be affected.

Delete a rule

Well, after simulating packet loss and delay, remember to delete the rule:

Sudo tc qdisc del dev eth0 root

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report