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
39 return "%s<ip:%s/%s up mac:%s>" % (
40 self.__class__.__name__,
41 self.address, self.netmask,
45 def add_address(self, address, netprefix, broadcast):
46 raise RuntimeError, "Cannot add explicit addresses to public interface"
48 def pick_iface(self, siblings):
50 Picks an interface using the PLCAPI to query information about the node.
52 Needs an assigned node.
55 siblings: other NodeIface elements attached to the same node
58 if self.node is None or self.node._node_id is None:
59 raise RuntimeError, "Cannot pick interface without an assigned node"
61 avail = self._api.GetInterfaces(
62 node_id=self.node._node_id,
63 is_primary=self.primary,
64 fields=('interface_id','mac','netmask','ip') )
66 used = set([sibling._interface_id for sibling in siblings
67 if sibling._interface_id is not None])
69 for candidate in avail:
70 candidate_id = candidate['interface_id']
71 if candidate_id not in used:
73 self._interface_id = candidate_id
74 self.address = candidate['ip']
75 self.lladdr = candidate['mac']
76 self.netprefix = candidate['netmask']
77 self.netmask = ipaddr2.ipv4_dot2mask(self.netprefix) if self.netprefix else None
80 raise RuntimeError, "Cannot configure interface: cannot find suitable interface in PlanetLab node"
83 if not self.has_internet:
84 raise RuntimeError, "All external interface devices must be connected to the Internet"
87 class _CrossIface(object):
88 def __init__(self, proto, addr, port):
89 self.tun_proto = proto
93 class TunIface(object):
94 _PROTO_MAP = tunproto.TUN_PROTO_MAP
97 def __init__(self, api=None):
104 self.netprefix = None
108 self.device_name = None
111 self.txqueuelen = None
116 # These get initialized when the iface is connected to its node
119 # These get initialized when the iface is configured
120 self.external_iface = None
122 # These get initialized when the iface is configured
123 # They're part of the TUN standard attribute set
127 # These get initialized when the iface is connected to its peer
128 self.peer_iface = None
129 self.peer_proto = None
130 self.peer_proto_impl = None
132 # same as peer proto, but for execute-time standard attribute lookups
133 self.tun_proto = None
136 # Generate an initial random cryptographic key to use for tunnelling
137 # Upon connection, both endpoints will agree on a common one based on
139 self.tun_key = ( ''.join(map(chr, [
142 for r in (random.SystemRandom(),) ])
143 ).encode("base64").strip() )
147 return "%s<ip:%s/%s %s%s>" % (
148 self.__class__.__name__,
149 self.address, self.netprefix,
150 " up" if self.up else " down",
151 " snat" if self.snat else "",
154 def add_address(self, address, netprefix, broadcast):
155 if (self.address or self.netprefix or self.netmask) is not None:
156 raise RuntimeError, "Cannot add more than one address to %s interfaces" % (self._KIND,)
158 raise ValueError, "%s interfaces cannot broadcast in PlanetLab" % (self._KIND,)
160 self.address = address
161 self.netprefix = netprefix
162 self.netmask = ipaddr2.ipv4_mask2dot(netprefix)
166 raise RuntimeError, "Unconnected %s iface - missing node" % (self._KIND,)
167 if self.peer_iface and self.peer_proto not in self._PROTO_MAP:
168 raise RuntimeError, "Unsupported tunnelling protocol: %s" % (self.peer_proto,)
169 if not self.address or not self.netprefix or not self.netmask:
170 raise RuntimeError, "Misconfigured %s iface - missing address" % (self._KIND,)
172 def _impl_instance(self, home_path, listening):
173 impl = self._PROTO_MAP[self.peer_proto](
174 self, self.peer_iface, home_path, self.tun_key, listening)
175 impl.port = self.tun_port
178 def prepare(self, home_path, listening):
179 if not self.peer_iface and (self.peer_proto and (listening or (self.peer_addr and self.peer_port))):
181 self.peer_iface = _CrossIface(
186 if not self.peer_proto_impl:
187 self.peer_proto_impl = self._impl_instance(home_path, listening)
188 self.peer_proto_impl.prepare()
191 if self.peer_proto_impl:
192 self.peer_proto_impl.setup()
195 if self.peer_proto_impl:
196 self.peer_proto_impl.shutdown()
197 self.peer_proto_impl = None
199 def async_launch_wait(self):
200 if self.peer_proto_impl:
201 self.peer_proto_impl.async_launch_wait()
203 def sync_trace(self, local_dir, whichtrace):
204 if self.peer_proto_impl:
205 return self.peer_proto_impl.sync_trace(local_dir, whichtrace)
209 class TapIface(TunIface):
210 _PROTO_MAP = tunproto.TAP_PROTO_MAP
213 # Yep, it does nothing - yet
214 class Internet(object):
215 def __init__(self, api=None):
217 api = plcapi.PLCAPI()
220 class NetPipe(object):
221 def __init__(self, api=None):
223 api = plcapi.PLCAPI()
239 # These get initialized when the pipe is connected to its node
241 self.configured = False
245 raise RuntimeError, "Undefined NetPipe mode"
246 if not self.portList:
247 raise RuntimeError, "Undefined NetPipe port list - must always define the scope"
248 if not (self.plrIn or self.bwIn or self.delayIn):
249 raise RuntimeError, "Undefined NetPipe inbound characteristics"
250 if not (self.plrOut or self.bwOut or self.delayOut):
251 raise RuntimeError, "Undefined NetPipe outbound characteristics"
253 raise RuntimeError, "Unconnected NetPipe"
255 def _add_pipedef(self, bw, plr, delay, options):
257 options.extend(("delay","%dms" % (delay,)))
259 options.extend(("bw","%.8fMbit/s" % (bw,)))
261 options.extend(("plr","%.8f" % (plr,)))
263 def _get_ruledef(self):
266 "@" if self.addrList else "",
271 if self.bwIn or self.plrIn or self.delayIn:
273 self._add_pipedef(self.bwIn, self.plrIn, self.delayIn, options)
274 if self.bwOut or self.plrOut or self.delayOut:
275 options.append("OUT")
276 self._add_pipedef(self.bwOut, self.plrOut, self.delayOut, options)
277 options = ' '.join(options)
279 return (scope,options)
283 scope, options = self._get_ruledef()
284 command = "sudo -S netconfig config %s %s %s" % (self.mode, scope, options)
286 (out,err),proc = server.popen_ssh_command(
288 host = self.node.hostname,
290 user = self.node.slicename,
292 ident_key = self.node.ident_path,
293 server_key = self.node.server_key
297 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
299 # we have to clean up afterwards
300 self.configured = True
305 scope, options = self._get_ruledef()
306 command = "sudo -S netconfig refresh %s %s %s" % (self.mode, scope, options)
308 (out,err),proc = server.popen_ssh_command(
310 host = self.node.hostname,
312 user = self.node.slicename,
314 ident_key = self.node.ident_path,
315 server_key = self.node.server_key
319 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
324 scope, options = self._get_ruledef()
325 command = "sudo -S netconfig delete %s %s" % (self.mode, scope)
327 (out,err),proc = server.popen_ssh_command(
329 host = self.node.hostname,
331 user = self.node.slicename,
333 ident_key = self.node.ident_path,
334 server_key = self.node.server_key
338 raise RuntimeError, "Failed instal build sources: %s %s" % (out,err,)
340 self.configured = False
342 def sync_trace(self, local_dir, whichtrace):
343 if whichtrace != 'netpipeStats':
344 raise ValueError, "Unsupported trace %s" % (whichtrace,)
346 local_path = os.path.join(local_dir, "netpipe_stats_%s" % (self.mode,))
348 # create parent local folders
349 proc = subprocess.Popen(
350 ["mkdir", "-p", os.path.dirname(local_path)],
351 stdout = open("/dev/null","w"),
352 stdin = open("/dev/null","r"))
355 raise RuntimeError, "Failed to synchronize trace: %s %s" % (out,err,)
357 (out,err),proc = server.popen_ssh_command(
358 "echo 'Rules:' ; sudo -S netconfig show rules ; echo 'Pipes:' ; sudo -S netconfig show pipes",
359 host = self.node.hostname,
361 user = self.node.slicename,
363 ident_key = self.node.ident_path,
364 server_key = self.node.server_key
368 raise RuntimeError, "Failed to synchronize trace: %s %s" % (out,err,)
370 # dump results to file
371 f = open(local_path, "wb")