简体   繁体   中英

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. 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? 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

1. Use the component script "connectionDown.py" inside POX directory:

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

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

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:

./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:

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

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

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:

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 :

event.connection.dpid

The IDs are normally assigned in the order of devices coming up starting from 1 to the number of devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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