如何在连接到pox控制器时识别Mininet中的特定交换机

0 投票
3 回答
9908 浏览
提问于 2025-04-18 03:00

我在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

3 个回答

0

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

_handle_ConnectionUp (event)

你可以通过以下方式获取启动的交换机或路由器的ID:

event.connection.dpid

这些ID通常是按照设备启动的顺序分配的,从1开始,一直到设备的总数。

1

为了找到交换机 "s1" 的 dpid,比如在 mininet 中,你可以使用:

py s1.dpid

不过,如果你想在 POX 中查看它,最好的办法是使用像 Pycharm 这样的开发环境来运行你的组件,这样你就可以用下面的代码来调试你的 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()
2

这个链接 http://squarey.me/cloud-virtualization/pox-controller-learning-four.html 提供了一个POX组件的例子,这个组件可以监听所有交换机的连接状态变化,并获取它们的设备ID(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控制器的终端) 启动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终端) 启动包含多个交换机的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控制器的终端,这里是你在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.

5. POX应该对交换机的任何变化做出反应:

mininet> py s1.stop()
mininet> py s1.start([c0])
mininet>

4. 再次回到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.
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.

撰写回答