From dd159b4a7335db227be2963f46c49a6317189194 Mon Sep 17 00:00:00 2001
From: Bob Lantz <rlantz@cs.stanford.edu>
Date: Mon, 16 Aug 2010 14:49:02 -0700
Subject: [PATCH] mininet.node.SWITCH_PORT_BASE specifies first switch port
 number.

This should be mostly cosmetic, but it causes switches to number
their ports consistently with OpenFlow 1.0, which starts at 1.
For older versions of OpenFlow, SWITCH_PORT_BASE may be set to zero.
---
 mininet/node.py | 11 +++++++----
 mininet/topo.py | 23 +++++++++++++----------
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/mininet/node.py b/mininet/node.py
index b22e9c7d..403ed00c 100644
--- a/mininet/node.py
+++ b/mininet/node.py
@@ -48,12 +48,11 @@
 from subprocess import Popen, PIPE, STDOUT
 from time import sleep
 
-from mininet.log import info, error, debug
+from mininet.log import info, error, warn, debug
 from mininet.util import quietRun, makeIntfPair, moveIntf, isShellBuiltin
 from mininet.moduledeps import moduleDeps, OVS_KMOD, OF_KMOD, TUN
 
-PORT_BASE = 1  # Port numbering to start from.  OF > v0.9 is 1-indexed.
-
+SWITCH_PORT_BASE = 1  # For OF > 0.9, switch ports start at 1 rather than zero
 
 class Node( object ):
     """A virtual network node is simply a shell in a network namespace.
@@ -62,6 +61,8 @@ class Node( object ):
     inToNode = {}  # mapping of input fds to nodes
     outToNode = {}  # mapping of output fds to nodes
 
+    portBase = 0  # Nodes always start with eth0/port0, even in OF 1.0
+    
     def __init__( self, name, inNamespace=True,
         defaultMAC=None, defaultIP=None, **kwargs ):
         """name: name of node
@@ -261,7 +262,7 @@ def newPort( self ):
         "Return the next port number to allocate."
         if len( self.ports ) > 0:
             return max( self.ports.values() ) + 1
-        return PORT_BASE
+        return self.portBase
 
     def addIntf( self, intf, port=None ):
         """Add an interface.
@@ -401,6 +402,8 @@ class Switch( Node ):
     """A Switch is a Node that is running (or has execed?)
        an OpenFlow switch."""
 
+    portBase = SWITCH_PORT_BASE  # 0 for OF < 1.0, 1 for OF >= 1.0
+
     def __init__( self, name, opts='', **kwargs):
         Node.__init__( self, name, **kwargs )
         self.opts = opts
diff --git a/mininet/topo.py b/mininet/topo.py
index 3cb80ae6..9547fa12 100644
--- a/mininet/topo.py
+++ b/mininet/topo.py
@@ -12,7 +12,7 @@
 '''
 
 from networkx.classes.graph import Graph
-
+from mininet.node import SWITCH_PORT_BASE
 
 class NodeID(object):
     '''Topo node identifier.'''
@@ -127,15 +127,18 @@ def add_port(self, src, dst):
         @param src source switch DPID
         @param dst destination switch DPID
         '''
+        src_base = SWITCH_PORT_BASE if self.is_switch(src) else 0
+        dst_base = SWITCH_PORT_BASE if self.is_switch(dst) else 0
         if src not in self.ports:
             self.ports[src] = {}
         if dst not in self.ports[src]:
-            self.ports[src][dst] = len(self.ports[src])  # num outlinks
-
+            # num outlinks    
+            self.ports[src][dst] = len(self.ports[src]) + src_base
         if dst not in self.ports:
             self.ports[dst] = {}
         if src not in self.ports[dst]:
-            self.ports[dst][src] = len(self.ports[dst])  # num outlinks
+            # num outlinks 
+            self.ports[dst][src] = len(self.ports[dst]) + dst_base
 
     def node_enabled(self, dpid):
         '''Is node connected, admin on, powered on, and fault-free?
@@ -178,6 +181,11 @@ def nodes_str(self, dpids):
         '''
         return [str(self.id_gen(dpid = dpid)) for dpid in dpids]
 
+    def is_switch(self, n):
+        '''Returns true if node is a switch.'''
+        return self.node_info[n].is_switch
+
+
     def switches(self, enabled = True):
         '''Return switches.
 
@@ -185,12 +193,7 @@ def switches(self, enabled = True):
 
         @return dpids list of dpids
         '''
-
-        def is_switch(n):
-            '''Returns true if node is a switch.'''
-            return self.node_info[n].is_switch
-
-        nodes = [n for n in self.g.nodes() if is_switch(n)]
+        nodes = [n for n in self.g.nodes() if self.is_switch(n)]
         return self.nodes_enabled(nodes, enabled)
 
     def hosts(self, enabled = True):
-- 
GitLab