mptcp-topo1.py 脚本解析与iperf命令
程序员文章站
2022-03-05 09:00:17
...
https://github.com/mininet/mininet/wiki/Introduction-to-Mininet
addlink()
self.addLink(node1, node2, bw=10, delay='5ms', max_queue_size=1000, loss=10, use_htb=True)
上述表示添加双向链路、延迟和丢包率等特性,参数:
- bw: 表示带宽的数值,单位Mbps
- delay: 表示延迟,如'5ms' '100us'
- loss: 表示丢包率,0-100
简单用法:
linkopts = dict(bw=10, delay='5ms', loss=10, max_queue_size=1000, use_htb=True)
# (or you can use brace syntax: linkopts = {'bw':10, 'delay':'5ms', ... } )
self.addLink(node1, node2, **linkopts)
可以通过设置此参数,来修改链路的延迟和丢包率。
注释版:mptcp-topo1.py
#!/usr/bin/env python
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import Link, TCLink,Intf
from subprocess import Popen, PIPE
from mininet.log import setLogLevel
if '__main__' == __name__:
setLogLevel('info')
#创建对象net
net = Mininet(link=TCLink)
key = "net.mptcp.mptcp_enabled"
#value数值表示是否开启mptcp
#1表示开启,0表示不开启,详见笔记02
value = 1
#执行命令
p = Popen("sysctl -w %s=%s" % (key, value), shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
print "stdout=",stdout,"stderr=", stderr
#增加主机
h1 = net.addHost('h1')
h2 = net.addHost('h2')
r1 = net.addHost('r1')
#设置链路参数10Mbps,当然也可以添加其他选项
linkopt={'bw':10}
#增加一个双向链接,详细参数见下方详解
net.addLink(r1,h1,cls=TCLink, **linkopt)
net.addLink(r1,h1,cls=TCLink, **linkopt)
net.addLink(r1,h2,cls=TCLink, **linkopt)
net.addLink(r1,h2,cls=TCLink, **linkopt)
net.build()
#配置网络设备
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ifconfig r1-eth2 0")
r1.cmd("ifconfig r1-eth3 0")
h1.cmd("ifconfig h1-eth0 0")
h1.cmd("ifconfig h1-eth1 0")
h2.cmd("ifconfig h2-eth0 0")
h2.cmd("ifconfig h2-eth1 0")
#该文件表示是否打开IP转发
#出于安全考虑,Linux系统默认是禁止数据包转发的。
#当主机拥有多于一块的网卡时,其中一块收到数据包,根据数据包的目的ip地址将包发往本机另一网卡
#该网卡根据路由表继续发送数据包。这通常就是路由器所要实现的功能。
#0:禁止
#1:转发
r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
#临时为网卡配置ip地址和掩码
r1.cmd("ifconfig r1-eth0 10.0.0.1 netmask 255.255.255.0")
r1.cmd("ifconfig r1-eth1 10.0.1.1 netmask 255.255.255.0")
r1.cmd("ifconfig r1-eth2 10.0.2.1 netmask 255.255.255.0")
r1.cmd("ifconfig r1-eth3 10.0.3.1 netmask 255.255.255.0")
h1.cmd("ifconfig h1-eth0 10.0.0.2 netmask 255.255.255.0")
h1.cmd("ifconfig h1-eth1 10.0.1.2 netmask 255.255.255.0")
h2.cmd("ifconfig h2-eth0 10.0.2.2 netmask 255.255.255.0")
h2.cmd("ifconfig h2-eth1 10.0.3.2 netmask 255.255.255.0")
#路由规则的添加
h1.cmd("ip rule add from 10.0.0.2 table 1")
h1.cmd("ip rule add from 10.0.1.2 table 2")
#更改默认路由
h1.cmd("ip route add 10.0.0.0/24 dev h1-eth0 scope link table 1")
h1.cmd("ip route add default via 10.0.0.1 dev h1-eth0 table 1")
h1.cmd("ip route add 10.0.1.0/24 dev h1-eth1 scope link table 2")
h1.cmd("ip route add default via 10.0.1.1 dev h1-eth1 table 2")
h1.cmd("ip route add default scope global nexthop via 10.0.0.1 dev h1-eth0")
h2.cmd("ip rule add from 10.0.2.2 table 1")
h2.cmd("ip rule add from 10.0.3.2 table 2")
h2.cmd("ip route add 10.0.2.0/24 dev h2-eth0 scope link table 1")
h2.cmd("ip route add default via 10.0.2.1 dev h2-eth0 table 1")
h2.cmd("ip route add 10.0.3.0/24 dev h2-eth1 scope link table 2")
h2.cmd("ip route add default via 10.0.3.1 dev h2-eth1 table 2")
h2.cmd("ip route add default scope global nexthop via 10.0.2.1 dev h2-eth0")
CLI(net)
net.stop()
推荐阅读