Bob Lantz
Committed by Bob Lantz

Enable global port forwarding in root namespace

This should fix the problem seen in VirtualBox where you
may have some random interface which is host-only which needs
to have port forwarding set up on it.

Change-Id: Iacdbb129d52529783bdab1c3d768131f6eaf0364
(cherry picked from commit 930138e3)
...@@ -55,7 +55,6 @@ from sys import argv ...@@ -55,7 +55,6 @@ from sys import argv
55 from glob import glob 55 from glob import glob
56 import time 56 import time
57 from functools import partial 57 from functools import partial
58 -from re import search
59 58
60 59
61 ### ONOS Environment 60 ### ONOS Environment
...@@ -365,7 +364,7 @@ class ONOSCluster( Controller ): ...@@ -365,7 +364,7 @@ class ONOSCluster( Controller ):
365 args = list( args ) 364 args = list( args )
366 name = args.pop( 0 ) 365 name = args.pop( 0 )
367 topo = kwargs.pop( 'topo', None ) 366 topo = kwargs.pop( 'topo', None )
368 - nat = kwargs.pop( 'nat', 'nat0' ) 367 + self.nat = kwargs.pop( 'nat', 'nat0' )
369 nodeOpts = kwargs.pop( 'nodeOpts', {} ) 368 nodeOpts = kwargs.pop( 'nodeOpts', {} )
370 # Default: single switch with 1 ONOS node 369 # Default: single switch with 1 ONOS node
371 if not topo: 370 if not topo:
...@@ -384,8 +383,8 @@ class ONOSCluster( Controller ): ...@@ -384,8 +383,8 @@ class ONOSCluster( Controller ):
384 host=partial( ONOSNode, **nodeOpts ), 383 host=partial( ONOSNode, **nodeOpts ),
385 switch=LinuxBridge, 384 switch=LinuxBridge,
386 controller=None ) 385 controller=None )
387 - if nat: 386 + if self.nat:
388 - self.net.addNAT( nat ).configDefault() 387 + self.net.addNAT( self.nat ).configDefault()
389 updateNodeIPs( self.env, self.nodes() ) 388 updateNodeIPs( self.env, self.nodes() )
390 self._remoteControllers = [] 389 self._remoteControllers = []
391 390
...@@ -420,32 +419,21 @@ class ONOSCluster( Controller ): ...@@ -420,32 +419,21 @@ class ONOSCluster( Controller ):
420 "Return list of ONOS nodes" 419 "Return list of ONOS nodes"
421 return [ h for h in self.net.hosts if isinstance( h, ONOSNode ) ] 420 return [ h for h in self.net.hosts if isinstance( h, ONOSNode ) ]
422 421
423 - def defaultIntf( self ): 422 + def configPortForwarding( self, ports=[], action='A' ):
424 - "Call ip route to determine default interface" 423 + """Start or stop port forwarding (any intf) for all nodes
425 - result = quietRun( 'ip route | grep default', shell=True ).strip() 424 + ports: list of ports to forward
426 - match = search( r'dev\s+([^\s]+)', result )
427 - if match:
428 - intf = match.group( 1 )
429 - else:
430 - warn( "Can't find default network interface - using eth0\n" )
431 - intf = 'eth0'
432 - return intf
433 -
434 - def configPortForwarding( self, ports=[], intf='', action='A' ):
435 - """Start or stop forwarding on intf to all nodes
436 action: A=add/start, D=delete/stop (default: A)""" 425 action: A=add/start, D=delete/stop (default: A)"""
437 - if not intf: 426 + self.cmd( 'iptables -' + action, 'FORWARD -d', self.ipBase,
438 - intf = self.defaultIntf() 427 + '-j ACCEPT' )
439 for port in ports: 428 for port in ports:
440 for index, node in enumerate( self.nodes() ): 429 for index, node in enumerate( self.nodes() ):
441 ip, inport = node.IP(), port + index 430 ip, inport = node.IP(), port + index
442 # Configure a destination NAT rule 431 # Configure a destination NAT rule
443 - cmd = ( 'iptables -t nat -{action} PREROUTING -t nat ' 432 + self.cmd( 'iptables -t nat -' + action,
444 - '-i {intf} -p tcp --dport {inport} ' 433 + 'PREROUTING -t nat -p tcp --dport', inport,
445 - '-j DNAT --to-destination {ip}:{port}' ) 434 + '-j DNAT --to-destination %s:%s' % ( ip, port ) )
446 - self.cmd( cmd.format( **locals() ) )
447 -
448 435
436 +
449 class ONOSSwitchMixin( object ): 437 class ONOSSwitchMixin( object ):
450 "Mixin for switches that connect to an ONOSCluster" 438 "Mixin for switches that connect to an ONOSCluster"
451 def start( self, controllers ): 439 def start( self, controllers ):
......