From 55cf19c4de6ff7bda7825e2537aa7e2a602a06bd Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Wed, 25 Apr 2012 14:21:39 -0700 Subject: [PATCH] Improve error handling for defaultDpid() I think it's worth considering how we want to specify dpids for switches. One way would be to have Mininet (optionally) pick them automatically. Another way, which I have currently implemented, is to intuit them from the name, for example s1 -> 1. The latter is slightly inefficient, but is convenient because it ensures that there is a logical mapping between switch names and dpids, which is very helpful for debugging an OpenFlow system! Probably we should just clarify that the easiest way to set a dpid is to include it in the switch name, but you can also pass it in as a custom parameter to the constructor. --- mininet/node.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mininet/node.py b/mininet/node.py index 238e9bf..25721e5 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -727,7 +727,7 @@ class Switch( Node ): portBase = 1 # Switches start with port 1 in OpenFlow def __init__( self, name, dpid=None, opts='', listenPort=None, **params): - """dpid: dpid for switch (or None for default) + """dpid: dpid for switch (or None to derive from name, e.g. s1 -> 1) opts: additional switch options listenPort: port to listen on for dpctl connections""" Node.__init__( self, name, **params ) @@ -739,10 +739,15 @@ class Switch( Node ): def defaultDpid( self ): "Derive dpid from switch name, s1 -> 1" - dpid = int( re.findall( '\d+', self.name )[ 0 ] ) - dpid = hex( dpid )[ 2: ] - dpid = '0' * ( 16 - len( dpid ) ) + dpid - return dpid + try: + dpid = int( re.findall( '\d+', self.name )[ 0 ] ) + dpid = hex( dpid )[ 2: ] + dpid = '0' * ( 16 - len( dpid ) ) + dpid + return dpid + except IndexError: + raise Exception( 'Unable to derive default datapath ID - ' + 'please either specify a dpid or use a ' + 'canonical switch name such as s23.' ) def defaultIntf( self ): "Return control interface"