linux使用tc和ifb限制入方向速度

自用,直接上脚本

#!/bin/bash

INTERFACE="eth0"           # 替换为你的实际网卡名称
TOTAL_BANDWIDTH="6400kbps" # 总带宽参数 (单位 KB/s)
RATE="256kbps"             # 每个IP的限速 (单位 KB/s)
CEIL="384kbps"             # 允许的最大速率 (单位 KB/s)

# 加载必要的模块
# modprobe ip_tables    # iptables 应该都有
modprobe ifb

# 检查 ifb0 设备是否已经存在
if ! ip link show ifb0 > /dev/null 2>&1; then
    # 设置 ifb 设备
    ip link add ifb0 type ifb
fi
ip link set ifb0 up

# 设置ingress qdisc
tc qdisc add dev $INTERFACE handle ffff: ingress

# 重定向所有流量到ifb0
tc filter add dev $INTERFACE parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0

# 创建根qdisc和默认类
tc qdisc add dev ifb0 root handle 1: htb default 30
tc class add dev ifb0 parent 1: classid 1:1 htb rate $TOTAL_BANDWIDTH

# 添加带宽限制类和过滤器
tc class add dev ifb0 parent 1:1 classid 1:10 htb rate $RATE ceil $CEIL
tc filter add dev ifb0 protocol ip parent 1:0 prio 1 u32 match ip src 0.0.0.0/0 flowid 1:10

echo "完成入站带宽限制设置。"

本文链接:

https://blog.nkxingxh.top/archives/441/
1 + 2 =
快来做第一个评论的人吧~