Compare commits

...
Author SHA1 Message Date
Bob Lantz dd0abe0e12 Default ipv6 action should be 'add' 2015-04-14 14:20:07 -07:00
Bob Lantz 5a7b8f9833 setIP(): allow multiple IP addresses and IPv6 addresses
This may be a useful feature, but it doesn't fit well into
the established Mininet API, which assumes a single default
IP address for each interface.
2015-04-09 17:57:52 -07:00
+44 -21
View File
@@ -67,19 +67,36 @@ class Intf( object ):
"Configure ourselves using ifconfig" "Configure ourselves using ifconfig"
return self.cmd( 'ifconfig', self.name, *args ) return self.cmd( 'ifconfig', self.name, *args )
def setIP( self, ipstr, prefixLen=None ): def setIP( self, ip, prefixLen=8, action='add' ):
"""Set our IP address""" """Set our IP address(es) and bring interface up
ip: IP address string or list
prefixLen: optional default prefix length if '/' not in addr (8)
action: optional action for IPv6 addrs (default 'add')"""
if isinstance( ip, basestring ):
ip = ( ip, )
ipstr = ip[ 0 ]
# This is a sign that we should perhaps rethink our prefix # This is a sign that we should perhaps rethink our prefix
# mechanism and/or the way we specify IP addresses # mechanism and/or the way we specify IP addresses
if '/' in ipstr: result = ''
self.ip, self.prefixLen = ipstr.split( '/' ) self.ips = []
return self.ifconfig( ipstr, 'up' ) for index, ipstr in enumerate( ip ):
else: if '/' in ipstr:
if prefixLen is None: ip, prefixLen = ipstr.split( '/' )
raise Exception( 'No prefix length set for IP address %s' else:
% ( ipstr, ) ) ipstr = '%s/%s' % ( ipstr, prefixLen )
self.ip, self.prefixLen = ipstr, prefixLen if index == 0:
return self.ifconfig( '%s/%s' % ( ipstr, prefixLen ) ) dev = self.name
self.ip, self.prefixLen = ip, prefixLen
else:
dev = '%s:%d' % ( self, index )
if ':' not in ipstr:
result += self.cmd( 'ifconfig', dev, ipstr, 'up' )
else:
# IPv6
result += self.cmd( 'ifconfig', dev, 'inet6', action, ipstr,
'up' )
self.ips += [ ipstr ]
return result
def setMAC( self, macstr ): def setMAC( self, macstr ):
"""Set the MAC address for an interface. """Set the MAC address for an interface.
@@ -89,18 +106,23 @@ class Intf( object ):
self.ifconfig( 'hw', 'ether', macstr ) + self.ifconfig( 'hw', 'ether', macstr ) +
self.ifconfig( 'up' ) ) self.ifconfig( 'up' ) )
_ipMatchRegex = re.compile( r'\d+\.\d+\.\d+\.\d+' ) _ipMatchRegex = re.compile( r'inet.? (.*)/' )
_macMatchRegex = re.compile( r'..:..:..:..:..:..' ) _macMatchRegex = re.compile( r'..:..:..:..:..:..' )
def updateIP( self ): def updateIP( self, all=False ):
"Return updated IP address based on ifconfig" """Return updated IP address based on ifconfig
all: return list of all IP addresses for this interface"""
# use pexec instead of node.cmd so that we dont read # use pexec instead of node.cmd so that we dont read
# backgrounded output from the cli. # backgrounded output from the cli.
ifconfig, _err, _exitCode = self.node.pexec( ipaddr, _err, _exitCode = self.node.pexec(
'ifconfig %s' % self.name ) 'ip', 'addr', 'show', self.name )
ips = self._ipMatchRegex.findall( ifconfig ) ips = self._ipMatchRegex.findall( ipaddr )
self.ip = ips[ 0 ] if ips else None self.ip = ips[ 0 ] if ips else None
return self.ip self.ips = ips
if all:
return self.ips
else:
return self.ip
def updateMAC( self ): def updateMAC( self ):
"Return updated MAC address based on ifconfig" "Return updated MAC address based on ifconfig"
@@ -122,9 +144,10 @@ class Intf( object ):
self.mac = macs[ 0 ] if macs else None self.mac = macs[ 0 ] if macs else None
return self.ip, self.mac return self.ip, self.mac
def IP( self ): def IP( self, all=False):
"Return IP address" """Return IP address
return self.ip all: return list of IP addresses for this interface (False)"""
return self.ips if all else self.ip
def MAC( self ): def MAC( self ):
"Return MAC address" "Return MAC address"