简体   繁体   English

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

[英]How to identify a specific switch in Mininet when connected to a pox controller

I have a custom topology running on Mininet and it has 2 switches s1, and s2. 我在Mininet上运行了一个自定义拓扑,它有2个交换机s1和s2。 I am using pox as the controller. 我使用痘作为控制器。 I have written a python code to identify the switches, is this the correct way to do it? 我已经编写了python代码来识别开关,这是正确的方法吗? Are there any other better methods that i can use? 我还能使用其他更好的方法吗? could any body suggest other alternatives? 任何机构都可以提出其他替代方案吗?

Code: 码:

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

This link http://squarey.me/cloud-virtualization/pox-controller-learning-four.html provides examples of POX component that listens to ConnectionUp events from all switches and get the dpid 此链接http://squarey.me/cloud-virtualization/pox-controller-learning-four.html提供了POX组件的示例,该组件侦听来自所有交换机的ConnectionUp事件并获取dpid

1. Use the component script "connectionDown.py" inside POX directory: 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 controller xterm) Start the POX controller with custom component 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) start mininet topology with multiple switches 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- Back to POX controller xterm, here is what observe in POX xterm: 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 should react to any changes to rhe switches: 5- S#POX应该对rhe开关的任何更改做出反应:

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

4- Back to POX controller xterm: 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. 

In order to find switch "s1" dpid, for instance, in mininet, you can use: 为了找到交换机“ s1”的dpid,例如在mininet中,可以使用:

py s1.dpid

However if you want to see it in POX, the best way is to run your component in an IDE like Pycharm which lets you debug your code using below code for debugging POX component as your controller is running: 但是,如果要在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()

In the function (which is called whenever a switch or router gets up): 在函数中(每当交换机或路由器启动时都会调用):

_handle_ConnectionUp (event)

The ID of the switch/router which got up can be retrived by : 可以通过以下方式检索启动的交换机/路由器的ID:

event.connection.dpid

The IDs are normally assigned in the order of devices coming up starting from 1 to the number of devices. 通常按照设备从1到设备数量的顺序分配ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM