模拟两个ubuntu服务器计算机之间的慢速连接

我想模拟以下场景:假设我有4台ubuntu服务器机器A,B,C和D.我想在机器A和机器C之间减少20%的网络带宽,在A和B之间减少10%.使用网络模拟/限制工具来做到这一点?
要做到这一点,您可以单独使用 tcu32过滤器,或者与 iptables marking结合使用(如果您不想学习复杂的过滤器语法,可能会更直接).我将在下面的帖子详细介绍前一个解决方案.

模拟您的设置

例如,让我们考虑运行10 Mbit / s虚拟接口的A,C和D.

你基本上想要:

> A< ==> B:出口为9 Mbit / s整形
> A< ==> C:出口的8 Mbit / s整形

为了模拟这个,我将创建4个网络命名空间和插入桥接器的虚拟以太网接口.

当然,在您的情况下,您将使用真正的NIC,并且桥将是您的网关或交换机,具体取决于您的基础架构.

因此,在我的模拟中,我们将在10.0.0.0/24网络中进行以下设置:

10.0.0.254            

                                  +-------+                     
                                  |       |                     
                                  |  br0  |                     
                                  |       |                   
                                  +---+---+                     
                                      |                         
                                      | veth{A..D}.peer        
                                      |                      
                  +------------+------+-----+------------+     
                  |            |            |            |      
            vethA |      vethB |      vethC |      vethD |      
              +---+---+    +---+---+    +---+---+    +---+---+  
              |       |    |       |    |       |    |       |   
              |   A   |    |   B   |    |   C   |    |   D   |   
              |       |    |       |    |       |    |       |  
              +-------+    +-------+    +-------+    +-------+    

              10.0.0.1      10.0.0.2     10.0.0.3     10.0.0.4

首先,设置阶段,以便您可以了解它的构成,如果您不熟悉它,请跳过它,没什么大不了的.但你必须知道的是命令ip netns exec< namespace> <命令>允许在网络命名空间中执行命令(即在前一个绘图的一个框中).这也将在下一节中使用.

# Create the bridge
ip link add br0 type bridge

# Create network namespaces and veth interfaces and plug them into the bridge
for host in {A..D} ; do 
    ip link netns add ${host}
    ip link add veth${host} type veth peer name veth${host}.peer
    ip link set dev veth${host}.peer master br0
    ip link set dev veth${host} netns ${host}
    ip netns exec ${host} ip link set veth${host} up
done

# Assign IPs
ip addr add 10.0.0.254/24 dev br0
ip netns exec A ip addr add 10.0.0.1/24 dev vethA
ip netns exec B ip addr add 10.0.0.2/24 dev vethB
ip netns exec C ip addr add 10.0.0.3/24 dev vethC
ip netns exec D ip addr add 10.0.0.4/24 dev vethD

所以在这一点上我们有前面描述的设置.

塑造交通

现在是时候进入交通管制,以获得你想要的. tc工具允许您添加排队规则:

>对于出口:一旦内核需要发送数据包并且在访问NIC驱动程序之前.
>对于入口:访问NIC驱动程序之后和内核例程运行之前收到的数据包.

它带有3个概念:qdisc,类和过滤器.这些概念可用于设置复杂的数据包流管理,并根据您想要的任何标准/标准对流量进行优先级排序.

简而言之 :

> Qdiscs是数据包最终入队/出队的结构.
>类是用于特定行为的qdiscs的容器.
>过滤器是在类之间路由数据包的方法,可以在处理期间在优先级相同的入口点上定义多个过滤器.

所有这些通常用作树,其中叶是qdiscs,类是节点.树或子树的根将声明为< id>:并且子节点将声明为< parent_id>:< children_id>.记住这个语法.

对于你的情况,让我们拿A并渲染你想用tc设置的树:

1:
                                      |
                                      |
                                      |
                                     1:1
                                   /  |  \
                                  /   |   \
                                 /    |    \
                               1:10  1:20  1:30
                                |     |     |
                                |     |     |
                               :10   :20   :30

说明:

> 1:是附加到设备vethA的根qdisc,它将被明确地视为Htb for Hierarchy Token Bucket(设备的默认qdisc是pfifo或pfifo_fast,具体取决于操作系统).它特别适合带宽管理.与此级别定义的过滤器不匹配的数据包将转到1:30级别.
> 1:1将是一个htb类,将设备的整个流量限制为10 Mbit / s.
> 1:10将是一个htb类,将输出流量限制为9 Mbit / s(10 Mbit / s的90%).
> 1:20将是一个htb类,将输出流量限制为8 Mbit / s(10 Mbit / s的80%).
> 1:30将是一个htb类,限制流量为10 Mbit / s(后备).
>:10,:20,:30是随机公平排队的sfq qdisc.换句话说,这些qdisc将确保基于流量的传输的公平性.

整个过程由以下命令设置:

ip netns exec A tc qdisc add dev vethA root handle 1: htb default 30
ip netns exec A tc class add dev vethA parent 1: classid 1:1 htb rate 10mbit burst 15k
ip netns exec A tc class add dev vethA parent 1:1 classid 1:10 htb rate 9mbit burst 15k
ip netns exec A tc class add dev vethA parent 1:1 classid 1:20 htb rate 8mbit burst 15k
ip netns exec A tc class add dev vethA parent 1:1 classid 1:30 htb rate 10mbit burst 15k
ip netns exec A tc qdsic add dev vethA parent 1:10 handle 10: sfq perturb 10
ip netns exec A tc qdisc add dev vethA parent 1:20 handle 20: sfq perturb 10
ip netns exec A tc qdisc add dev vethA parent 1:30 handle 30: sfq perturb 10

我们需要的最后一件事就是添加过滤器,这样目的IP等于B的IP数据包将达到1:10级,目标IP等于C的IP数据包将达到1:20级:

ip netns exec A tc filter add dev vethA parent 1: protocol ip prio 1 u32 match ip dst 10.0.0.2/32 flowid 1:10
ip netns exec A tc filter add dev vethA parent 1: protocol ip prio 2 u32 match ip dst 10.0.0.3/32 flowid 1:20

现在您已经了解了这个想法,您需要在B和C中添加类似的tc规则,以便从这些装备转向A.

测试

现在让我们来测试吧.为此,我个人习惯使用iperf,它只包含一个二进制文件,可以作为客户端或服务器运行,并在两个主机之间自动发送尽可能多的流量.

在A和B之间:

$ip netns exec B iperf -s -p 8001
  ...
 $ip netns exec A iperf -c 10.0.0.2 -p 8001 -t 10 -i 2
------------------------------------------------------------
Client connecting to 10.0.0.2,TCP port 8001
TCP window size: 21.0 KByte (default)
------------------------------------------------------------
[  5] local 10.0.0.1 port 58191 connected with 10.0.0.2 port 8001
[ ID] Interval       Transfer     Bandwidth
[  5]  0.0- 2.0 sec  2.38 MBytes  9.96 Mbits/sec
[  5]  2.0- 4.0 sec  2.12 MBytes  8.91 Mbits/sec
[  5]  4.0- 6.0 sec  2.00 MBytes  8.39 Mbits/sec
[  5]  6.0- 8.0 sec  2.12 MBytes  8.91 Mbits/sec
[  5]  8.0-10.0 sec  2.00 MBytes  8.39 Mbits/sec
[  5]  0.0-10.1 sec  10.8 MBytes  8.91 Mbits/sec

我们获得9 Mbit / s带宽限制.

在A和C之间:

$ip netns exec C iperf -s -p 8001
...
$ip netns exec A iperf -c 10.0.0.3 -p 8001 -t 10 -i 2
------------------------------------------------------------
Client connecting to 10.0.0.3,TCP port 8001
TCP window size: 21.0 KByte (default)
------------------------------------------------------------
[  5] local 10.0.0.1 port 58522 connected with 10.0.0.3 port 8001
[ ID] Interval       Transfer     Bandwidth
[  5]  0.0- 2.0 sec  2.25 MBytes  9.44 Mbits/sec
[  5]  2.0- 4.0 sec  1.75 MBytes  7.34 Mbits/sec
[  5]  4.0- 6.0 sec  1.88 MBytes  7.86 Mbits/sec
[  5]  6.0- 8.0 sec  1.88 MBytes  7.86 Mbits/sec
[  5]  8.0-10.0 sec  1.75 MBytes  7.34 Mbits/sec
[  5]  0.0-10.1 sec  9.62 MBytes  7.98 Mbits/sec

我们获得8 Mbit / s带宽限制.

在A和D之间:

$ip netns exec D iperf -s -p 8001
...
$ip netns exec A iperf -c 10.0.0.4 -p 8001 -t 10 -i 2
------------------------------------------------------------
Client connecting to 10.0.0.4,TCP port 8001
TCP window size: 21.0 KByte (default)
------------------------------------------------------------
[  5] local 10.0.0.1 port 40614 connected with 10.0.0.4 port 8001
[ ID] Interval       Transfer     Bandwidth
[  5]  0.0- 2.0 sec  2.62 MBytes  11.0 Mbits/sec
[  5]  2.0- 4.0 sec  2.25 MBytes  9.44 Mbits/sec
[  5]  4.0- 6.0 sec  2.38 MBytes  9.96 Mbits/sec
[  5]  6.0- 8.0 sec  2.25 MBytes  9.44 Mbits/sec
[  5]  8.0-10.0 sec  2.38 MBytes  9.96 Mbits/sec
[  5]  0.0-10.2 sec  12.0 MBytes  9.89 Mbits/sec

这里我们的虚拟接口全速达到了10 Mbit / s.

请注意,通过调整适当的参数,可以在htb类中更好地处理每次运行的第一个度量的突发.

打扫干净

去除 :

>优先级为1的过滤器:: tc filter del dev vethA parent 1:prio 1 u32.
> 1上的所有过滤器:tc filter del dev vethA parent 1:.
>等级1:20及其子女:tc class del dev veth父母1:1 classid
1:20.
>整棵树:tc qdisc del dev vethA.

要清理模拟集:

# Remove veth pairs and network namespaces
for host in {A..D} ; do
    ip link del dev veth${host}.peer
    ip netns del ${host}
done

# Remove the bridge
ip link del dev br0

相关文章

文章浏览阅读2.3k次,点赞4次,收藏22次。最近安装了CARLA预...
文章浏览阅读6.3k次,点赞5次,收藏15次。在清华镜像中下载U...
文章浏览阅读5k次。linux环境, python3.7.问题描述: 安装...
文章浏览阅读4.2k次,点赞4次,收藏17次。要安装这个 standa...
文章浏览阅读894次,点赞51次,收藏31次。在安卓使用vscode主...