How do I configure a network interface bandwidth limitation?

本文介绍如何在Red Hat Enterprise Linux中配置网络接口的带宽限制,包括检查现有队列纪律、计算速率限制参数、应用速率限制到接口、确认配置及测试确认等步骤。适用于需要控制网络流量的系统或服务器。

https://access.redhat.com/solutions/40824

SOLUTION 已验证 - 已更新 2016年十二月22日14:58 - 

English 

环境

  • Red Hat Enterprise Linux
  • Networking

问题

  • How do I configure a network interface bandwidth limitation?
  • How to ratelimit a NIC?
  • How can we stop a system or server sending so much LAN traffic?

决议

Inspect Existing qdisc

Ensure the existing queueing discipline on the network interface is pfifo_fast or noqueue:

Raw

# ip link | grep noqueue
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
2: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT 

Raw

# tc -s qdisc
qdisc pfifo_fast 0: dev eth0 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
 Sent 3485442 bytes 40223 pkt (dropped 0, overlimits 0 requeues 0) 
 rate 0bit 0pps backlog 0b 0p requeues 0 

If there is already a classless or classful qdisc applied, this needs to be removed with tc qdisc del, refer to man tc for full syntax of the various qdiscs.

Calculate Ratelimit Parameters

Units are described further on the man tc section PARAMETERS, common values for reference:

Raw

kbit       Kilobits per second
mbit       Megabits per second
gbit       Gigabits per second

kb or k    Kilobytes
mbit       Megabits
mb or m    Megabytes

The rate can be expressed as: rate 20mbit (20 megabit)

The buffer needs to be at least 1 kilobyte per megabit, this buffer is oversized: buffer 256kb (256 kilobytes)

Select how long you wish a packet to sit in the buffer before being dropped: latency 100ms (100 milliseconds)

The exact values for your network will need to be determined through configuration and testing. It is recommended to test with both bandwidth testing tools such as iperf and with the actual production workload to ensure desired outcomes are met.

Apply the Ratelimit to the Interface

We use the Token Bucket Filter to apply the ratelimit:

Raw

# tc qdisc add dev eth0 root tbf rate 20mbit buffer 256kb latency 100ms

Confirm Configuration

We can see qdisc tbf is now applied:

Raw

# tc -s qdisc
qdisc tbf 8001: dev eth0 root refcnt 2 rate 20000Kbit burst 256Kb lat 100.0ms 
 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) 
 rate 0bit 0pps backlog 0b 0p requeues 0 

Test and Confirm

A bandwidth test shows we're reaching roughly our desired bandwidth:

Raw

# iperf -l 1M -w 4M -f m -t 20 -c 172.16.1.7
------------------------------------------------------------
Client connecting to 172.16.1.7, TCP port 5001
TCP window size: 8.00 MByte (WARNING: requested 4.00 MByte)
------------------------------------------------------------
[  3] local 172.16.1.6 port 61900 connected with 172.16.1.7 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-20.8 sec  53.0 MBytes  21.4 Mbits/sec

The qdisc has been working to limit traffic it submits to the NIC:

Raw

# tc -s qdisc
qdisc tbf 8001: dev eth0 root refcnt 2 rate 20000Kbit burst 256Kb lat 100.0ms 
 Sent 2344194 bytes 1561 pkt (dropped 19, overlimits 130675 requeues 0) 
 rate 0bit 0pps backlog 0b 0p requeues 0 

To Persist Across Reboots

The methods described at How to make NIC ethtool settings persistent (apply automatically at boot) can also be used to persist these changes.

In RHEL 5, RHEL 6, and RHEL 7 without NetworkManager, write /sbin/ifup-local to apply the ratelimit at interface start.

In RHEL 7 with NetworkManager, write a NM dispatcher script as described in man NetworkManager.

Removal

If you wish to remove the TBF limitation, the qdisc can be deleted with:

Raw

# tc qdisc del dev eth0 root

The interface will revert back to its default queueing discipline:

Raw

# tc -s qdisc
qdisc pfifo_fast 0: dev eth0 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) 
 rate 0bit 0pps backlog 0b 0p requeues 0 

Troubleshooting

  • Ratelimiting will never be 100% accurate, but within 10% accuracy is a reasonable expectation. Faster ratelimits (eg: gigabit) will likely be more accurate than lower ratelimits (eg: kilobit).
  • Note that the TBF can only restrict what we send out. It is possible to ratelimit incoming traffic with moderate accuracy, as described at How to limit the bandwidth of incoming packets.
  • If the ratelimit encountered in testing is far off, NIC offloading features may need to be disabled, as described at tc command's transport rate does not seem normal.

根源

The TBF is explained in further detail at Linux Advanced Routing & Traffic Control HOWTO - 9.2. Simple, classless Queueing Disciplines.

源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值