from mininet.net import Mininet
from mininet.node import Node
from mininet.link import TCLink
from mininet.log import setLogLevel, info
def myNet():
"Create network from scratch using Open vSwitch."
info("*** Creating nodes\n")
switch1 = Node(‘s1‘, inNamespace=False)
switch2 = Node(‘s2‘, inNamespace=False)
h1 = Node(‘h1‘)
h2 = Node(‘h2‘)
h3 = Node(‘h3‘)
h4 = Node(‘h4‘)
info("*** Creating links\n")
linkopts1 = dict(bw=100, delay=‘1ms‘, loss=0)
linkopts2 = dict(bw=1, delay=‘100ms‘, loss=0)
TCLink(h1, switch1, **linkopts1)
TCLink(h2, switch1, **linkopts1)
TCLink(h3, switch2, **linkopts1)
TCLink(h4, switch2, **linkopts1)
TCLink(switch1, switch2, **linkopts2)
info("\n*** Configuring hosts\n")
h1.setIP(‘192.168.123.1/24‘)
h2.setIP(‘192.168.123.2/24‘)
h3.setIP(‘192.168.123.3/24‘)
h4.setIP(‘192.168.123.4/24‘)
info(str(h1) + ‘\n‘)
info(str(h2) + ‘\n‘)
info(str(h3) + ‘\n‘)
info(str(h4) + ‘\n‘)
info(str(h1) + ‘\x20‘ + h1.IP() + ‘\n‘)
info(str(h2) + ‘\x20‘ + h2.IP() + ‘\n‘)
info(str(h3) + ‘ ‘ + h3.IP() + ‘\n‘)
info(str(h4) + ‘ ‘ + h4.IP() + ‘\n‘)
info("*** Starting network using Open vSwitch\n")
# switch1--s1--dp1
# switch2--s2--dp2
switch1.cmd(‘ovs-vsctl del-br dp1‘)
switch1.cmd(‘ovs-vsctl add-br dp1‘)
switch2.cmd(‘ovs-vsctl del-br dp2‘)
switch2.cmd(‘ovs-vsctl add-br dp2‘)
for intf in switch1.intfs.values():
print(intf)
print(switch1.cmd(‘ovs-vsctl add-port dp1 %s‘ % intf))
for intf in switch2.intfs.values():
print(intf)
print(switch1.cmd(‘ovs-vsctl add-port dp2 %s‘ % intf))
info("*** ovs-ofctl\n ")
print(switch1.cmd(
r‘ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3‘))
print(switch1.cmd(
r‘ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3‘))
print(switch1.cmd(r‘ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output:1‘))
print(switch1.cmd(r‘ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=1,actions=pop_vlan,output:2‘))
print(switch2.cmd(
r‘ovs-ofctl -O OpenFlow13 add-flow dp2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3‘))
print(switch2.cmd(
r‘ovs-ofctl -O OpenFlow13 add-flow dp2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3‘))
print(switch2.cmd(r‘ovs-ofctl -O OpenFlow13 add-flow dp2 priority=1,dl_vlan=0,actions=pop_vlan,output:1‘))
print(switch2.cmd(r‘ovs-ofctl -O OpenFlow13 add-flow dp2 priority=1,dl_vlan=1,actions=pop_vlan,output:2‘))
info("*** Running test\n\n")
info("*** h1 ping h2\n")
h1.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h2.IP())
info("*** h1 ping h3\n")
h1.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h3.IP())
info("*** h1 ping h4\n")
h1.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h4.IP())
info("*** h2 ping h3\n")
h2.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h3.IP())
info("*** h2 ping h4\n")
h2.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h4.IP())
info("*** h3 ping h4\n")
h3.cmdPrint(‘ping -Q 0x10 -c 3 ‘ + h4.IP())
info("*** Stopping network\n")
switch1.cmd(‘ovs-vsctl del-br dp1‘)
switch1.deleteIntfs()
switch2.cmd(‘ovs-vsctl del-br dp2‘)
switch2.deleteIntfs()
info(‘\n‘)
if __name__ == ‘__main__‘:
setLogLevel(‘info‘)
info(‘*** Scratch network demo (kernel datapath)\n‘)
Mininet.init()
myNet()
通过本次实验了解了ovs的一些命令,遇到了以下几点实验中需要注意的:
1、使用miniedit创建拓扑时需要注意交换机(s)与主机(h)接线的先后顺序,使用命令时要注意端口号是否对应正确。还有创建后要记得配主机IP。下发流表的ovs命令是长句一行,输入的时候要注意。
2、进阶的话是照着作业里的链接,对应我们要建立的拓扑进行一些代码的增删和命令的修改,因为一些python语法和ovs命令不熟悉,导致完成时间较长。
总体来说本次作业相对前一次作业有更多需要先理解再自己完成的内容,遇到困难的时候参考了樊朝伟和张炜龙同学的博文以及一些网络资料,感谢他们对我的帮助。感觉本身网络方面的基础较为欠缺,完成SDN的作业容易走许多弯路,耗时占比较大,道阻且长
原文:https://www.cnblogs.com/ggbang007/p/15265724.html