2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 import nepi.util.ipaddr2 as ipaddr2
8 class NodeIface(object):
9 def __init__(self, api=None):
17 # These get initialized at configuration time
23 self._interface_id = None
25 # These get initialized when the iface is connected to its node
28 # These get initialized when the iface is connected to the internet
29 self.has_internet = False
32 return "%s<ip:%s/%s up mac:%s>" % (
33 self.__class__.__name__,
34 self.address, self.netmask,
38 def add_address(self, address, netprefix, broadcast):
39 raise RuntimeError, "Cannot add explicit addresses to public interface"
41 def pick_iface(self, siblings):
43 Picks an interface using the PLCAPI to query information about the node.
45 Needs an assigned node.
48 siblings: other NodeIface elements attached to the same node
51 if self.node is None or self.node._node_id is None:
52 raise RuntimeError, "Cannot pick interface without an assigned node"
54 avail = self._api.GetInterfaces(
55 node_id=self.node._node_id,
56 is_primary=self.primary,
57 fields=('interface_id','mac','netmask','ip') )
59 used = set([sibling._interface_id for sibling in siblings
60 if sibling._interface_id is not None])
62 for candidate in avail:
63 candidate_id = candidate['interface_id']
64 if candidate_id not in used:
66 self._interface_id = candidate_id
67 self.address = candidate['ip']
68 self.lladdr = candidate['mac']
69 self.netprefix = candidate['netmask']
70 self.netmask = ipaddr2.ipv4_dot2mask(self.netprefix) if self.netprefix else None
73 raise RuntimeError, "Cannot configure interface: cannot find suitable interface in PlanetLab node"
76 if not self.has_internet:
77 raise RuntimeError, "All external interface devices must be connected to the Internet"
80 class TunIface(object):
81 def __init__(self, api=None):
92 self.device_name = None
96 # These get initialized when the iface is connected to its node
100 return "%s<ip:%s/%s %s%s>" % (
101 self.__class__.__name__,
102 self.address, self.netmask,
103 " up" if self.up else " down",
104 " snat" if self.snat else "",
107 def add_address(self, address, netprefix, broadcast):
108 if (self.address or self.netprefix or self.netmask) is not None:
109 raise RuntimeError, "Cannot add more than one address to TUN interfaces"
111 raise ValueError, "TUN interfaces cannot broadcast in PlanetLab"
113 self.address = address
114 self.netprefix = netprefix
115 self.netmask = ipaddr2.ipv4_dot2mask(netprefix)
121 # Yep, it does nothing - yet
122 class Internet(object):
123 def __init__(self, api=None):
125 api = plcapi.PLCAPI()