Basic interface object that can configure itself.
| 34 | |
| 35 | |
| 36 | class Intf( object ): |
| 37 | |
| 38 | "Basic interface object that can configure itself." |
| 39 | |
| 40 | def __init__( self, name, node=None, port=None, link=None, |
| 41 | mac=None, **params ): |
| 42 | """name: interface name (e.g. h1-eth0) |
| 43 | node: owning node (where this intf most likely lives) |
| 44 | link: parent link if we're part of a link |
| 45 | other arguments are passed to config()""" |
| 46 | self.node = node |
| 47 | self.name = name |
| 48 | self.link = link |
| 49 | self.mac = mac |
| 50 | self.ip, self.prefixLen = None, None |
| 51 | |
| 52 | # if interface is lo, we know the ip is 127.0.0.1. |
| 53 | # This saves an ifconfig command per node |
| 54 | if self.name == 'lo': |
| 55 | self.ip = '127.0.0.1' |
| 56 | self.prefixLen = 8 |
| 57 | # Add to node (and move ourselves if necessary ) |
| 58 | if node: |
| 59 | moveIntfFn = params.pop( 'moveIntfFn', None ) |
| 60 | if moveIntfFn: |
| 61 | node.addIntf( self, port=port, moveIntfFn=moveIntfFn ) |
| 62 | else: |
| 63 | node.addIntf( self, port=port ) |
| 64 | # Save params for future reference |
| 65 | self.params = params |
| 66 | self.config( **params ) |
| 67 | |
| 68 | def cmd( self, *args, **kwargs ): |
| 69 | "Run a command in our owning node" |
| 70 | return self.node.cmd( *args, **kwargs ) |
| 71 | |
| 72 | def ifconfig( self, *args ): |
| 73 | "Configure ourselves using ifconfig" |
| 74 | return self.cmd( 'ifconfig', self.name, *args ) |
| 75 | |
| 76 | def setIP( self, ipstr, prefixLen=None ): |
| 77 | """Set our IP address""" |
| 78 | # This is a sign that we should perhaps rethink our prefix |
| 79 | # mechanism and/or the way we specify IP addresses |
| 80 | if '/' in ipstr: |
| 81 | self.ip, self.prefixLen = ipstr.split( '/' ) |
| 82 | return self.ifconfig( ipstr, 'up' ) |
| 83 | else: |
| 84 | if prefixLen is None: |
| 85 | raise Exception( 'No prefix length set for IP address %s' |
| 86 | % ( ipstr, ) ) |
| 87 | self.ip, self.prefixLen = ipstr, prefixLen |
| 88 | return self.ifconfig( '%s/%s' % ( ipstr, prefixLen ) ) |
| 89 | |
| 90 | def setMAC( self, macstr ): |
| 91 | """Set the MAC address for an interface. |
| 92 | macstr: MAC address as string""" |
| 93 | self.mac = macstr |
no outgoing calls
no test coverage detected