有没有办法在 Linux 中使用 HTB + CIDR 范围来限制每个 IP 的带宽?

有没有办法在 Linux 中使用 HTB + CIDR 范围来限制每个 IP 的带宽?

我可以创建规则来限制整个子网或使用 tc 和 htb 来限制单个 IP 地址。我希望使用 CIDR 范围来让事情变得优雅一些。

有问题的机器都运行着 CentOS 7。我一直在尝试使用 tc + htb 来实现这一点,但如果有更好的方法,我愿意使用其他工具。

我的目标是通过 CIDR 范围进行限制并为每个源 IP 地址分配单独的限制。

例如,将 192.168.1.0/24 的全局限制设置为 100Mb/s,并且 192.168.1.0/24 内的每个源 IP 都有单独的上传限制 10Mb/s,不得超过。

下面是我针对每个 IP 执行的操作的一个工作示例(尽可能简化流程):

这些步骤仅需执行一次:

Create initial HTB qdisc:
# tc qdisc add dev eth0 root handle 1: htb default 12       

Create root class:
# tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit ceil 100mbit

These steps must be performed for each IP in the CIDR range using current method (what I am looking to hopefully improve):

A class must be added for each source ip:
# tc class add dev eth0 parent 1:1 classid 1:10 htb rate 10mbit ceil 100mbit
# tc class add dev eth0 parent 1:1 classid 1:11 htb rate 10mbit ceil 100mbit
# tc class add dev eth0 parent 1:1 classid 1:12 htb rate 10mbit ceil 100mbit

A filter must be created for each source ip:
# tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 192.168.1.2 flowid 1:10
# tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 192.168.1.3 flowid 1:11

可能没有优雅的方法来做到这一点,但任何提示/建议都会非常感激。我在网上浏览了几本指南,比如http://lartc.org。 谢谢。

答案1

我认为你可以尝试使用 hash,为每个 ip 分配一个 bucket,为每个 ip 提供相等数量的 pps。你也应该使用 sfq qdisc,因为 htb 不是一个公平的 qdisc。

tc qdisc add dev eth0 root handle 1: htb default 12
tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit ceil 100mbit
# Here, you want a fair qdisc
tc qdisc add dev eth0 parent 1:1 handle 101: sfq perturb 10
# Put a range in the filter
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip src 192.168.1.0/24 flowid 1:1
# Create 254 bucket, each ip src will be attached to one bucket
tc filter add dev eth0 parent 1:1 protocol ip handle 10 flow hash keys nfct-src divisor 254

答案2

您好,您可以在这里找到:升级到 Debian 9 - 内核 4.19 后,Linux 防火墙标记分类器 (tc-fw) 不起作用适合您任务的工作配置。

Br,尼古拉

相关内容