如何识别Mininet中连接到pox-con的特定交换机

2024-06-01 08:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在Mininet上运行的自定义拓扑,它有两个开关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

Tags: 方法代码fromcoreimporteventforconnection
3条回答

例如,要在mininet中查找开关“s1”dpid,可以使用:

py s1.dpid

但是,如果您想在POX中看到它,最好的方法是在类似Pycharm的IDE中运行您的组件,该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()

此链接http://squarey.me/cloud-virtualization/pox-controller-learning-four.html 提供从所有交换机侦听ConnectionUp事件并获取dpid的POX组件的示例

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应对rhe开关的任何变化作出反应:

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.

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

_handle_ConnectionUp (event)

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

event.connection.dpid

这些id通常按照从1到设备数量的顺序进行分配。

相关问题 更多 >