0

我在 Mininet 上运行了一个自定义拓扑,它有 2 个交换机 s1 和 s2。我正在使用 pox 作为控制器。我写了一个python代码来识别开关,这是正确的方法吗?我可以使用其他更好的方法吗?任何机构都可以提出其他替代方案吗?

代码:

from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.util import dpidToStr
log = core.getLogger()
s1_dpid=0
s2_dpid=0
def _handle_ConnectionUp (event):
global s1_dpid, s2_dpid
print "ConnectionUp: ", 
dpidToStr(event.connection.dpid)
#remember the connection dpid for switch 
for m in event.connection.features.ports:
if m.name == "s1-eth1":
s1_dpid = event.connection.dpid
print "s1_dpid=", s1_dpid
elif m.name == "s2-eth1":
s2_dpid = event.connection.dpid
print "s2_dpid=", s2_dpid
4

3 回答 3

2

此链接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.
于 2014-05-19T16:48:16.127 回答
1

为了找到开关“s1”dpid,例如,在 mininet 中,您可以使用:

py s1.dpid

但是,如果您想在 POX 中查看它,最好的方法是在像 Pycharm 这样的 IDE 中运行您的组件,它允许您在控制器运行时使用以下代码调试您的代码以调试 POX 组件:

import sys
sys.path.append('ADDRESS_TO_YOUR_POX_FOLDER')
from pox.boot import boot

components = ['YOUR_COMPONENT1','YOURCOMPONENT2']

def main():
    boot(components)

if __name__=='__main__':
    main()
于 2016-09-11T23:20:33.050 回答
0

在函数中(每当交换机或路由器启动时调用):

_handle_ConnectionUp (event)

起床的交换机/路由器的 ID 可以通过以下方式检索:

event.connection.dpid

ID 通常按照设备出现的顺序从 1 到设备数量进行分配。

于 2016-11-30T12:41:33.670 回答