From 8139d7d1b42a2bcd7ed1a5046ce1f0cf15ebc463 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Wed, 10 Oct 2018 18:34:06 -0700 Subject: [PATCH] Better IP address selection for controllers Previously we looked for 'eth0', but linux has renamed interfaces. Instead of looking by name, we now look for an interface which is the appropriate gateway to a remote server address. fixes #831 --- examples/cluster.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/cluster.py b/examples/cluster.py index 078dca4..d2764e9 100755 --- a/examples/cluster.py +++ b/examples/cluster.py @@ -843,11 +843,26 @@ class MininetCluster( Mininet ): def addController( self, *args, **kwargs ): "Patch to update IP address to global IP address" controller = Mininet.addController( self, *args, **kwargs ) - # Update IP address for controller that may not be local - if ( isinstance( controller, Controller) - and controller.IP() == '127.0.0.1' - and ' eth0:' in controller.cmd( 'ip link show' ) ): - Intf( 'eth0', node=controller ).updateIP() + loopback = '127.0.0.1' + if ( not isinstance( controller, Controller ) or + controller.IP() != loopback ): + return + # Find route to a different server IP address + serverIPs = [ ip for ip in self.serverIP.values() + if ip is not controller.IP() ] + if not serverIPs: + return # no remote servers - loopback is fine + remoteIP = serverIPs[ 0 ] + # Route should contain 'dev ' + route = controller.cmd( 'ip route get', remoteIP, + '| egrep -o "dev\s[^[:space:]]+"' ) + if not route: + raise Exception('addController: no route from', c0, 'to', + remoteIP ) + intf = route.split()[ 1 ].strip() + debug( 'adding', intf, 'to', controller ) + Intf( intf, node=controller ).updateIP() + debug( controller, 'IP address updated to', controller.IP() ) return controller def buildFromTopo( self, *args, **kwargs ):