此链接http://squarey.me/cloud-virtualization/pox-controller-learning-four.html
提供了 POX 组件的示例,该组件侦听来自所有交换机的 ConnectionUp 事件并获取 dpid
1. 使用 POX 目录中的组件脚本“connectionDown.py”:
#!/usr/bin/python
from pox.core import core
from pox.lib.util import dpid_to_str
from pox.lib.revent import *
log = core.getLogger()
class ConnectionUp(Event):
def __init__(self,connection,ofp):
Event.__init__(self)
self.connection = connection
self.dpid = connection.dpid
self.ofp = ofp
class ConnectionDown(Event):
def __init__(self,connection,ofp):
Event.__init__(self)
self.connection = connection
self.dpid = connection.dpid
class MyComponent(object):
def __init__(self):
core.openflow.addListeners(self)
def _handle_ConnectionUp(self,event):
ConnectionUp(event.connection,event.ofp)
log.info("Switch %s has come up.",dpid_to_str(event.dpid))
def _handle_ConnectionDown(self,event):
ConnectionDown(event.connection,event.dpid)
log.info("Switch %s has shutdown.",dpid_to_str(event.dpid))
def launch():
core.registerNew(MyComponent)
2-(POX 控制器 xterm)使用自定义组件启动 POX 控制器
mininet@mininet-vm:~/pox$ ./pox.py connectionDown
POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
INFO:core:POX 0.1.0 (betta) is up.
3- (mininet xterm) 使用多个交换机启动 mininet 拓扑
mininet@mininet-vm:~$ sudo mn --topo linear --mac --controller remote --switch ovsk
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2
*** Adding switches:
s1 s2
*** Adding links:
(h1, s1) (h2, s2) (s1, s2)
*** Configuring hosts
h1 h2
*** Starting controller
*** Starting 2 switches
s1 s2
*** Starting CLI:
mininet>
4- 回到 POX 控制器 xterm,这是在 POX xterm 中观察到的内容:
./pox.py connectionDown
POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
INFO:core:POX 0.1.0 (betta) is up.
INFO:openflow.of_01:[00-00-00-00-00-02 2] connected
INFO:connectionDown:Switch 00-00-00-00-00-02 has come up.
INFO:openflow.of_01:[00-00-00-00-00-01 1] connected
INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.
5- S#POX 应该对开关的任何更改做出反应:
mininet> py s1.stop()
mininet> py s1.start([c0])
mininet>
4-回到 POX 控制器 xterm:
mininet@mininet-vm:~/pox$ ./pox.py connectionDown
POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
INFO:core:POX 0.1.0 (betta) is up.
INFO:openflow.of_01:[00-00-00-00-00-02 2] connected
INFO:connectionDown:Switch 00-00-00-00-00-02 has come up.
INFO:openflow.of_01:[00-00-00-00-00-01 1] connected
INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.
INFO:openflow.of_01:[00-00-00-00-00-01 1] closed
INFO:connectionDown:Switch 00-00-00-00-00-01 has shutdown.
INFO:openflow.of_01:[00-00-00-00-00-01 3] connected
INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.