本例子主要演示通过VTN协调器及两个VTN管理器配置L2网络,利用mininet虚拟出openflow交换机,并对其进行控制,mininet交换机拓扑图如下:
拓扑图创建:
1 |
sudo python multitree.py |
其中multitree.py如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#!/usr/bin/python """ Run Mininet network using tree topology per remote controller. """ from mininet.cli import CLI from mininet.log import info, setLogLevel from mininet.net import Mininet from mininet.node import Host, OVSKernelSwitch, RemoteController from mininet.topo import Topo TreeDepth = 2 FanOut = 2 ControllerAddress = ["172.31.2.70", "172.31.2.72"] class MultiTreeTopo(Topo): """Topology for multiple tree network using remote controllers. A tree network is assigned to a remote controller.""" def __init__(self): Topo.__init__(self) self.hostSize = 1 self.switchSize = 1 self.treeSwitches = [] prev = None for cidx in range(len(ControllerAddress)): switches = [] self.treeSwitches.append(switches) root = self.addTree(switches, TreeDepth, FanOut) if prev: self.addLink(prev, root) prev = root def addTree(self, switches, depth, fanout): """Add a tree node.""" if depth > 0: node = self.addSwitch('s%u' % self.switchSize) self.switchSize += 1 switches.append(node) for i in range(fanout): child = self.addTree(switches, depth - 1, fanout) self.addLink(node, child) else: node = self.addHost('h%u' % self.hostSize) self.hostSize += 1 return node def start(self, net): """Start all controllers and switches in the network.""" cidx = 0 for c in net.controllers: info("*** Starting controller: %s\n" % c) info(" + Starting switches ... ") switches = self.treeSwitches[cidx] for sname in switches: s = net.getNodeByName(sname) info(" %s" % s) s.start([c]) cidx += 1 info("\n") self.treeSwitches = None class MultiTreeNet(Mininet): """Mininet network environment with multiple tree network using remote controllers.""" def __init__(self, **args): args['topo'] = MultiTreeTopo() args['switch'] = OVSKernelSwitch args['controller'] = RemoteController args['build'] = False Mininet.__init__(self, **args) idx = 1 for addr in ControllerAddress: name = 'c%d' % idx info('*** Creating remote controller: %s (%s)\n' % (name, addr)) self.addController(name, ip=addr, port=6633) idx = idx + 1 def start(self): "Start controller and switches." if not self.built: self.build() self.topo.start(self) if __name__ == '__main__': setLogLevel('info') # for CLI output net = MultiTreeNet() net.build() print "*** Starting network" net.start() print "*** Running CLI" CLI(net) print "*** Stopping network" net.stop() |
测试连通性:
1 2 3 4 5 |
mininet> h2 ping h6 PING 10.0.0.6 (10.0.0.6) 56(84) bytes of data. From 10.0.0.2 icmp_seq=1 Destination Host Unreachable From 10.0.0.2 icmp_seq=2 Destination Host Unreachable From 10.0.0.2 icmp_seq=3 Destination Host Unreachable |
下面通过vtn规则,使得h2与h6可以通信,调用接口创建相关资源:
创建vtn
1 2 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"vtn" : {"vtn_name":"vtn_one","description":"test VTN" }}' http://172.31.2.70:8083/vtn-webapi/vtns HTTP/1.1 201 Created |
创建两个控制器
1 2 3 4 5 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"controller": {"controller_id": "controller1", "ipaddr":"172.31.2.70", "type": "odc", "version": "1.0", "auditstatus":"enable"}}' http://172.31.2.70:8083/vtn-webapi/controllers HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"controller": {"controller_id": "controller2", "ipaddr":"172.31.2.72", "type": "odc", "version": "1.0", "auditstatus":"enable"}}' http://172.31.2.70:8083/vtn-webapi/controllers HTTP/1.1 201 Created |
创建虚拟桥
分别在Controller1中创建vBridge1,Controller2中创建vBridge2。
1 2 3 4 5 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"vbridge" : {"vbr_name":"vbr1","controller_id":"controller1","domain_id":"(DEFAULT)" }}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"vbridge" : {"vbr_name":"vbr2","controller_id":"controller2","domain_id":"(DEFAULT)" }}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges HTTP/1.1 201 Created |
创建接口
创建4个接口,vbr1中if1,if2,vbr2中if1,if2,其中两个if2做boundary的接口,if1做h2和h6的接口。
1 2 3 4 5 6 7 8 9 10 11 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"interface": {"if_name": "if1","description": "if_desc1"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr1/interfaces HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"interface": {"if_name": "if2","description": "if_desc2"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr1/interfaces HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"interface": {"if_name": "if1","description": "if_desc1"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr2/interfaces HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"interface": {"if_name": "if2","description": "if_desc2"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr2/interfaces HTTP/1.1 201 Created |
创建boundary和vLink
1 2 3 4 5 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"boundary": {"boundary_id": "b1", "link": {"controller1_id": "controller1", "domain1_id": "(DEFAULT)","logical_port1_id": "PP-OF:openflow:1-s1-eth3", "controller2_id": "controller2", "domain2_id": "(DEFAULT)", "logical_port2_id": "PP-OF:openflow:4-s4-eth3"}}}' http://172.31.2.70:8083/vtn-webapi/boundaries HTTP/1.1 201 Created curl -i --user admin:adminpass -H 'content-type: application/json' -X POST -d '{"vlink": {"vlk_name": "vlink1" , "vnode1_name": "vbr1", "if1_name":"if2", "vnode2_name": "vbr2", "if2_name": "if2", "boundary_map": {"boundary_id":"b1","vlan_id": "50"}}}' http://127.0.0.1:8083/vtn-webapi/vtns/vtn_one/vlinks.json HTTP/1.1 201 Created |
配置端口映射关系
1 2 3 4 5 |
curl -i --user admin:adminpass -H 'content-type: application/json' -X PUT -d '{"portmap":{"logical_port_id": "PP-OF:openflow:2-s2-eth2"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr1/interfaces/if1/portmap HTTP/1.1 204 No Content curl -i --user admin:adminpass -H 'content-type: application/json' -X PUT -d '{"portmap":{"logical_port_id": "PP-OF:openflow:5-s5-eth2"}}' http://172.31.2.70:8083/vtn-webapi/vtns/vtn_one/vbridges/vbr2/interfaces/if1/portmap HTTP/1.1 204 No Content |
监测是否可以通信:
1 2 3 4 5 |
mininet> h2 ping h6 PING 10.0.0.6 (10.0.0.6) 56(84) bytes of data. 64 bytes from 10.0.0.6: icmp_seq=1 ttl=64 time=12.2 ms 64 bytes from 10.0.0.6: icmp_seq=2 ttl=64 time=0.184 ms 64 bytes from 10.0.0.6: icmp_seq=3 ttl=64 time=0.042 ms |