SDN第二次作业
(一)
1.创建交换机
测试连通性:
2.搭建拓扑
3.划分VLAN
4.主机连通性
wireshark测试抓包
(二)进阶
代码如下:
更新:
#!/usr/bin/python
def myVswitch():
"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")
Link(h1, switch1)
Link(h2, switch1)
Link(h3, switch2)
Link(h4, switch2)
Link(switch1, switch2)
info("*** 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("*** Starting network using Open vSwitch\n")
switch1.cmd('ovs-vsctl del-br s1')
switch1.cmd('ovs-vsctl add-br s1')
switch2.cmd('ovs-vsctl del-br s2')
switch2.cmd('ovs-vsctl add-br s2')
for intf in switch1.intfs.values():
print(intf)
print(switch1.cmd('ovs-vsctl add-port s1 %s' % intf))
for intf in switch2.intfs.values():
print(intf)
print(switch2.cmd('ovs-vsctl add-port s2 %s' % intf))
switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
switch2.cmd(
r'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
switch1.cmd(
r'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
switch2.cmd(
r'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
info("*** Running test\n")
info('h1 ping h2\n')
h1.cmdPrint('ping -Q 0x10 -c 3 ' + h2.IP())
info('h1 ping h3\n')
h1.cmdPrint('ping -Q 0x20 -c 3 ' + h3.IP())
info('h1 ping h4\n')
h1.cmdPrint('ping -Q 0x30 -c 3 ' + h4.IP())
info('h2 ping h3\n')
h2.cmdPrint('ping -Q 0x20 -c 3 ' + h3.IP())
info('h2 ping h4\n')
h2.cmdPrint('ping -Q 0x30 -c 3 '+h4.IP())
info("*** Stopping network\n")
switch1.cmd('ovs-vsctl del-br s1')
switch1.deleteIntfs()
switch2.cmd('ovs-vsctl del-br s2')
switch2.deleteIntfs()
info('\n')
if __name__ == '__main__':
setLogLevel('info')
info('*** Scratch network demo (kernel datapath)\n')
Mininet.init()
myVswitch()
原:
#!/usr/bin/env python
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call
import os
def myNetwork():
net = Mininet(topo=None,
build=False,
ipBase='10.0.0.0/8')
info('*** Adding controller\n')
c0 = net.addController(name='c0',
controller=Controller,
protocol='tcp',
port=6633)
info('*** Add switches\n')
s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
info('*** Add hosts\n')
h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)
info('*** Add links\n')
net.addLink(h1, s1, 1, 1)
net.addLink(h2, s1, 1, 2)
net.addLink(h3, s2, 1, 1)
net.addLink(h4, s2, 1, 2)
net.addLink(s1, s2, 3, 3)
info('*** Starting network\n')
net.build()
info('*** Starting controllers\n')
for controller in net.controllers:
controller.start()
info('*** Starting switches\n')
net.get('s1').start([c0])
net.get('s2').start([c0])
os.system(
'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
os.system(
'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
os.system(
'ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
os.system(
'ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
os.system('ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
os.system('ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
os.system('ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
os.system('ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
os.system('ovs-vsctl show')
info('*** Post configure switches and hosts\n')
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel('info')
myNetwork()
运行结果:
更新:
原:
总结:
更新:通过和学习小组的讨论和借鉴代码终于做出了想要的效果。一个人学习容易犯懒,一起学习就好了许多。
原:
本次作业我敲代码时较为粗心,好在胡巧玲同学帮我指出问题所在.我才能做的这么快.特别感谢!
需要注意的是图上的拓扑的连接接口是从1开始而Miniedit是从0开始的.需要自行修改代码或者修改连接方法.
在VLAN中s2也需要下放流表,当时比较粗心没有注意到,ping到奇怪的地方去
进阶实验的参考和前面实验的代码相差较大,于是用了偷鸡的办法.拓扑图用了Miniedit直接生成,并且在代码中通过import OS直接与终端交互.值得注意的是当Miniedit导出Py拓扑后会自动生成流表,此时输入pingall是可以全部ping通的.在此基础上对此流表修改即可得到想要的效果.
由于我的PyCharm是使用root权限运行,所以该代码的终端命令即使没加sudo也是可运行的.用sudo python3 xxx.py也许也可以正常运行?
凑字数 没话可说了,希望大家少点内卷吧,卷不动了