//创建ovs交换机,以ovs-switch038命名 sudo ovs-vsctl add-br ovs-switch038 //创建端口p0,设置编号为100,类型为“internal” sudo ovs-vsctl add-port ovs-switch038 p0 sudo ovs-vsctl set Interface p0 ofport_request=100 type=internal //查询p0网口的相关信息 sudo ethtool -i p0 //创建端口p1,设置编号为101,类型为“internal” sudo ovs-vsctl add-port ovs-switch038 p1 sudo ovs-vsctl set Interface p1 ofport_request=101 type=internal //查询p1网口的相关信息 sudo ethtool -i p1 //创建一个虚拟网络空间ns0,把p0接口移入网络空间ns0,并配置IP地址为 192.168.0.100 sudo ip netns add ns0 sudo ip link set p0 netns ns0 sudo ip netns exec ns0 ip addr add 192.168.0.100/24 dev p0 sudo ip netns exec ns0 ifconfig p0 promisc up //创建一个虚拟网络空间ns1,把p1接口移入网络空间ns1,并配置IP地址为 192.168.0.101 sudo ip netns add ns1 sudo ip link set p1 netns ns1 sudo ip netns exec ns1 ip addr add 192.168.0.101/24 dev p1 sudo ip netns exec ns1 ifconfig p1 promisc up
sudo ovs-vsctl show
测试p0,p1连通性
sudo ip netns exec ns0 ping 192.168.0.101
sudo ip netns exec ns1 ping 192.168.0.100
sudo ./../mininet/examples/miniedit.py
搭建拓扑VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
# s1下发流表 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2 # s2下发流表 sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1 sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2
# 查看s1的流表 sudo ovs-ofctl -O OpenFlow13 dump-flows s1 # 查看s2的流表 sudo ovs-ofctl -O OpenFlow13 dump-flows s2
在mininet命令行界面中执行pingall
指令,同时在另一个终端中输入sudo wireshark
发现s1向s3发送的包中id为0,而s2向s4发送的包中id为1,说明操作成功
阅读SDNLAB实验使用Mininet,编写Python代码,生成(一)中的SDN拓扑,并在代码中直接使用OVS命令,做到可以直接运行Python程序完成和(一)相同的VLAN划分。
# !/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 def mynet(): 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(s1, s2, 3, 3) net.addLink(h3, s2, 1, 1) net.addLink(h4, s2, 1, 2) 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]) info('*** Post configure switches and hosts\n') # 添加流表并划分VLAN s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3') s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3') s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1') s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2') s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3') s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3') s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1') s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2') CLI(net) net.stop() if __name__ == '__main__': setLogLevel('info') mynet()
sudo ovs-vsctl add-port ovs-switch013 p0
语句时,报了个error,上网搜索了一下也没有找到是什么原因,有的说要先写什么语句,但是我觉得不需要,就先往下做看看情况,结果发现它不影响后面的操作。sudo ip netns exec ns0 ping 192.168.0.101
写成了sudo ip netns exec ns0 ping 192.168.0.100
,变成“自己ping自己了”,所以导致了错误,改正后就可以正常测试了。在这次实验中,学习了Open vSwitch虚拟交换机的相关知识与概念,学习并运用了
ovs-vsctl
和ovs-ofctl
操作命令,还学习了划分VLAN,有些知识在上个学期的计算机网络中有接触过,通过这次实验也加深了印象。有了上次实验的基础,这次操作起来会更加得心应手一些,虽然有遇到一些问题,但是都能独立解决。通过实验,我也加深了对VLAN的实现方式的理解。