首先构建一个拓扑,可以使用mininet的可视化界面构建,也可以直接使用代码构建,拓扑及代码如下所示:
#!/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 myNetwork():
net = Mininet( topo=None,
build=False,
ipBase='10.0.0.0/8')
info( '*** Adding controller\n' )
c0=net.addController(name='c0',
controller=RemoteController,
ip='127.0.0.1',
protocol='tcp',
port=6633)
info( '*** Add switches\n')
s1 = net.addSwitch('s1', cls=OVSKernelSwitch, dpid='0000000000000001')
s2 = net.addSwitch('s2', cls=OVSKernelSwitch, dpid='0000000000000002')
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(s1, h1)
net.addLink(s1, h2)
net.addLink(s2, h3)
net.addLink(s2, h4)
net.addLink(s1, s2)
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')
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
myNetwork()
关于拓扑和代码配置及描述,这里就不一一阐述了。
配置好代码后,首先需要启动一个ryu控制器,这里以simple_switch.py为例:
cd ryu/ryu/app
ryu-manager simple_switch.py
启动完Ryu控制器后,运行上述代码:
sudo python3 flowtable01.py
dpctl dump-flows
dpctl del-flows
dpctl add-flow in_port=1,actions=output:2
dpctl add-flow in_port=2,actions=output:1
为什么要添加两条流表呢?(此处可以回忆一下TCP/IP的三次握手)
2. 添加丢弃数据包流表
dpctl add-flow in_port=2,actions=drop
增加这条流表后,mininet中主机之间将无法通信。
sh ovs-ofctl del-flows s1 in_port=2
或者是:
dpctl del-flows in_port=1
dpctl del-flows in_port=2