如何在连接到pox控制器时识别Mininet中的特定开关

时间:2014-04-16 15:48:33

标签: python networking openflow

我在Mininet上运行自定义拓扑,它有2个开关s1和s2。我使用痘作为控制器。我已经编写了一个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 :(得分:2)

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

<强> 1。使用组件脚本&#34; connectionDown.py&#34;在POX目录中:

#!/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.

答案 1 :(得分:1)

为了找到开关&#34; s1&#34;例如,在mininet中,你可以使用dpid:

var keylist = [ "_", "-"];

window.onkeydown = function(e) {
   keychar = String.fromCharCode(e.keyCode);
   if(keylist.indexOf(keychar)===-1 && e.target == document.body){
      e.preventDefault();
      return false;
   }
}

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

py s1.dpid

答案 2 :(得分:0)

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

_handle_ConnectionUp (event)

起床的交换机/路由器的ID可以通过以下方式重新获得:

event.connection.dpid

ID通常按设备从1开始到设备数量的顺序分配。