2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 import nepi.util.ipaddr2 as ipaddr2
6 import nepi.util.server as server
15 class NodeIface(object):
16 def __init__(self, api=None):
24 # These get initialized at configuration time
30 self._interface_id = None
32 # These get initialized when the iface is connected to its node
35 # These get initialized when the iface is connected to the internet
36 self.has_internet = False
38 # Generate an initial random cryptographic key to use for tunnelling
39 # Upon connection, both endpoints will agree on a common one based on
41 self.tun_key = ( ''.join(map(chr, [
44 for r in (random.SystemRandom(),) ])
45 ).encode("base64").strip() )
48 return "%s<ip:%s/%s up mac:%s>" % (
49 self.__class__.__name__,
50 self.address, self.netmask,
54 def add_address(self, address, netprefix, broadcast):
55 raise RuntimeError, "Cannot add explicit addresses to public interface"
57 def pick_iface(self, siblings):
59 Picks an interface using the PLCAPI to query information about the node.
61 Needs an assigned node.
64 siblings: other NodeIface elements attached to the same node
67 if self.node is None or self.node._node_id is None:
68 raise RuntimeError, "Cannot pick interface without an assigned node"
70 avail = self._api.GetInterfaces(
71 node_id=self.node._node_id,
72 is_primary=self.primary,
73 fields=('interface_id','mac','netmask','ip') )
75 used = set([sibling._interface_id for sibling in siblings
76 if sibling._interface_id is not None])
78 for candidate in avail:
79 candidate_id = candidate['interface_id']
80 if candidate_id not in used:
82 self._interface_id = candidate_id
83 self.address = candidate['ip']
84 self.lladdr = candidate['mac']
85 self.netprefix = candidate['netmask']
86 self.netmask = ipaddr2.ipv4_dot2mask(self.netprefix) if self.netprefix else None
89 raise RuntimeError, "Cannot configure interface: cannot find suitable interface in PlanetLab node"
92 if not self.has_internet:
93 raise RuntimeError, "All external interface devices must be connected to the Internet"
96 class _CrossIface(object):
97 def __init__(self, proto, addr, port):
98 self.tun_proto = proto
102 class TunIface(object):
103 _PROTO_MAP = tunproto.TUN_PROTO_MAP
106 def __init__(self, api=None):
108 api = plcapi.PLCAPI()
113 self.netprefix = None
117 self.device_name = None
120 self.txqueuelen = None
125 # These get initialized when the iface is connected to its node
128 # These get initialized when the iface is configured
129 self.external_iface = None
131 # These get initialized when the iface is configured
132 # They're part of the TUN standard attribute set
137 # These get initialized when the iface is connected to its peer
138 self.peer_iface = None
139 self.peer_proto = None
140 self.peer_proto_impl = None
142 # same as peer proto, but for execute-time standard attribute lookups
143 self.tun_proto = None
146 return "%s<ip:%s/%s %s%s>" % (
147 self.__class__.__name__,
148 self.address, self.netprefix,
149 " up" if self.up else " down",
150 " snat" if self.snat else "",
153 def add_address(self, address, netprefix, broadcast):
154 if (self.address or self.netprefix or self.netmask) is not None:
155 raise RuntimeError, "Cannot add more than one address to %s interfaces" % (self._KIND,)
157 raise ValueError, "%s interfaces cannot broadcast in PlanetLab" % (self._KIND,)
159 self.address = address
160 self.netprefix = netprefix
161 self.netmask = ipaddr2.ipv4_mask2dot(netprefix)
165 raise RuntimeError, "Unconnected %s iface - missing node" % (self._KIND,)
166 if self.peer_iface and self.peer_proto not in self._PROTO_MAP:
167 raise RuntimeError, "Unsupported tunnelling protocol: %s" % (self.peer_proto,)
168 if not self.address or not self.netprefix or not self.netmask:
169 raise RuntimeError, "Misconfigured %s iface - missing address" % (self._KIND,)
171 def _impl_instance(self, home_path, listening):
172 impl = self._PROTO_MAP[self.peer_proto](
173 self, self.peer_iface, home_path, self.tun_key, listening)
174 impl.port = self.tun_port
177 def prepare(self, home_path, listening):
178 if not self.peer_iface and (self.peer_proto and (listening or (self.peer_addr and self.peer_port))):
180 self.peer_iface = CrossIface(
185 if not self.peer_proto_impl:
186 self.peer_proto_impl = self._impl_instance(home_path, listening)
187 self.peer_proto_impl.prepare()
190 if self.peer_proto_impl:
191 self.peer_proto_impl.setup()
194 if self.peer_proto_impl:
195 self.peer_proto_impl.shutdown()
196 self.peer_proto_impl = None
198 def sync_trace(self, local_dir, whichtrace):
199 if self.peer_proto_impl:
200 return self.peer_proto_impl.sync_trace(local_dir, whichtrace)
204 class TapIface(TunIface):
205 _PROTO_MAP = tunproto.TAP_PROTO_MAP
208 # Yep, it does nothing - yet
209 class Internet(object):
210 def __init__(self, api=None):
212 api = plcapi.PLCAPI()
215 class NetPipe(object):
216 def __init__(self, api=None):
218 api = plcapi.PLCAPI()
234 # These get initialized when the pipe is connected to its node
236 self.configured = False
240 raise RuntimeError, "Undefined NetPipe mode"
241 if not self.portList:
242 raise RuntimeError, "Undefined NetPipe port list - must always define the scope"
243 if not (self.plrIn or self.bwIn or self.delayIn):
244 raise RuntimeError, "Undefined NetPipe inbound characteristics"
245 if not (self.plrOut or self.bwOut or self.delayOut):
246 raise RuntimeError, "Undefined NetPipe outbound characteristics"
248 raise RuntimeError, "Unconnected NetPipe"
250 def _add_pipedef(self, bw, plr, delay, options):
252 options.extend(("delay","%dms" % (delay,)))
254 options.extend(("bw","%.8fMbit/s" % (bw,)))
256 options.extend(("plr","%.8f" % (plr,)))
258 def _get_ruledef(self):
261 "@" if self.addrList else "",
266 if self.bwIn or self.plrIn or self.delayIn:
268 self._add_pipedef(self.bwIn, self.plrIn, self.delayIn, options)
269 if self.bwOut or self.plrOut or self.delayOut:
270 options.append("OUT")
271 self._add_pipedef(self.bwOut, self.plrOut, self.delayOut, options)
272 options = ' '.join(options)
274 return (scope,options)
278 scope, options = self._get_ruledef()
279 command = "sudo -S netconfig config %s %s %s" % (self.mode, scope, options)
281 (out,err),proc = server.popen_ssh_command(
283 host = self.node.hostname,
285 user = self.node.slicename,
287 ident_key = self.node.ident_path,
288 server_key = self.node.server_key
292 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
294 # we have to clean up afterwards
295 self.configured = True
300 scope, options = self._get_ruledef()
301 command = "sudo -S netconfig refresh %s %s %s" % (self.mode, scope, options)
303 (out,err),proc = server.popen_ssh_command(
305 host = self.node.hostname,
307 user = self.node.slicename,
309 ident_key = self.node.ident_path,
310 server_key = self.node.server_key
314 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
319 scope, options = self._get_ruledef()
320 command = "sudo -S netconfig delete %s %s" % (self.mode, scope)
322 (out,err),proc = server.popen_ssh_command(
324 host = self.node.hostname,
326 user = self.node.slicename,
328 ident_key = self.node.ident_path,
329 server_key = self.node.server_key
333 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
335 self.configured = False
337 def sync_trace(self, local_dir, whichtrace):
338 if whichtrace != 'netpipeStats':
339 raise ValueError, "Unsupported trace %s" % (whichtrace,)
341 local_path = os.path.join(local_dir, "netpipe_stats_%s" % (self.mode,))
343 # create parent local folders
344 proc = subprocess.Popen(
345 ["mkdir", "-p", os.path.dirname(local_path)],
346 stdout = open("/dev/null","w"),
347 stdin = open("/dev/null","r"))
350 raise RuntimeError, "Failed to synchronize trace: %s %s" % (out,err,)
352 (out,err),proc = server.popen_ssh_command(
353 "echo 'Rules:' ; sudo -S netconfig show rules ; echo 'Pipes:' ; sudo -S netconfig show pipes",
354 host = self.node.hostname,
356 user = self.node.slicename,
358 ident_key = self.node.ident_path,
359 server_key = self.node.server_key
363 raise RuntimeError, "Failed to synchronize trace: %s %s" % (out,err,)
365 # dump results to file
366 f = open(local_path, "wb")