Merge TCP handshake stuff
[nepi.git] / src / nepi / testbeds / planetlab / metadata.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import time
5
6 from constants import TESTBED_ID, TESTBED_VERSION
7 from nepi.core import metadata
8 from nepi.core.metadata import Parallel
9 from nepi.core.attributes import Attribute
10 from nepi.util import tags, validation
11 from nepi.util.constants import ApplicationStatus as AS, \
12         FactoryCategories as FC, \
13         ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP, \
14         DeploymentConfiguration as DC
15
16 import functools
17 import os
18 import os.path
19 import weakref
20
21 NODE = "Node"
22 NODEIFACE = "NodeInterface"
23 TUNIFACE = "TunInterface"
24 TAPIFACE = "TapInterface"
25 APPLICATION = "Application"
26 DEPENDENCY = "Dependency"
27 NEPIDEPENDENCY = "NepiDependency"
28 NS3DEPENDENCY = "NS3Dependency"
29 INTERNET = "Internet"
30 NETPIPE = "NetPipe"
31 TUNFILTER = "TunFilter"
32 CLASSQUEUEFILTER = "ClassQueueFilter"
33 TOSQUEUEFILTER = "TosQueueFilter"
34 MULTICASTFORWARDER = "MulticastForwarder"
35 MULTICASTANNOUNCER = "MulticastAnnouncer"
36 MULTICASTROUTER = "MulticastRouter"
37
38 TUNFILTERS = (TUNFILTER, CLASSQUEUEFILTER, TOSQUEUEFILTER)
39 TAPFILTERS = (TUNFILTER, )
40 ALLFILTERS = (TUNFILTER, CLASSQUEUEFILTER, TOSQUEUEFILTER)
41
42 PL_TESTBED_ID = "planetlab"
43
44
45 ### Custom validation functions ###
46 def is_addrlist(attribute, value):
47     if not validation.is_string(attribute, value):
48         return False
49     
50     if not value:
51         # No empty strings
52         return False
53     
54     components = value.split(',')
55     
56     for component in components:
57         if '/' in component:
58             addr, mask = component.split('/',1)
59         else:
60             addr, mask = component, '32'
61         
62         if mask is not None and not (mask and mask.isdigit()):
63             # No empty or nonnumeric masks
64             return False
65         
66         if not validation.is_ip4_address(attribute, addr):
67             # Address part must be ipv4
68             return False
69         
70     return True
71
72 def is_portlist(attribute, value):
73     if not validation.is_string(attribute, value):
74         return False
75     
76     if not value:
77         # No empty strings
78         return False
79     
80     components = value.split(',')
81     
82     for component in components:
83         if '-' in component:
84             pfrom, pto = component.split('-',1)
85         else:
86             pfrom = pto = component
87         
88         if not pfrom or not pto or not pfrom.isdigit() or not pto.isdigit():
89             # No empty or nonnumeric ports
90             return False
91         
92     return True
93
94
95 ### Connection functions ####
96
97 def connect_node_iface_node(testbed_instance, node_guid, iface_guid):
98     node = testbed_instance._elements[node_guid]
99     iface = testbed_instance._elements[iface_guid]
100     iface.node = node
101
102 def connect_node_iface_inet(testbed_instance, iface_guid, inet_guid):
103     iface = testbed_instance._elements[iface_guid]
104     iface.has_internet = True
105
106 def connect_tun_iface_node(testbed_instance, node_guid, iface_guid):
107     node = testbed_instance._elements[node_guid]
108     iface = testbed_instance._elements[iface_guid]
109     iface.node = node
110     node.required_vsys.update(('fd_tuntap', 'vif_up', 'vif_down'))
111     node.required_packages.update(('python', 'python-crypto', 'python-setuptools', 'gcc'))
112
113 def connect_tun_iface_peer(proto, testbed_instance, iface_guid, peer_iface_guid):
114     iface = testbed_instance._elements[iface_guid]
115     peer_iface = testbed_instance._elements[peer_iface_guid]
116     iface.peer_iface = peer_iface
117     peer_iface.peer_iface = iface
118     iface.peer_proto = \
119     iface.tun_proto = \
120     peer_iface.peer_proto = \
121     peer_iface.tun_proto = proto
122     iface.tun_key = peer_iface.tun_key
123
124 def connect_tun_iface_filter(testbed_instance, iface_guid, filter_guid):
125     iface = testbed_instance._elements[iface_guid]
126     filt = testbed_instance._elements[filter_guid]
127     traces = testbed_instance._get_traces(filter_guid)
128     if 'dropped_stats' in traces: 
129         args = filt.args if filt.args else ""
130         filt.args = ','.join(filt.args.split(',') + ["logdropped=true",])
131     iface.filter_module = filt
132     filt.iface_guid = iface_guid
133     filt.iface = weakref.ref(iface)
134
135     if filt.peer_guid:
136         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
137
138 def connect_filter_peer(proto, testbed_instance, filter_guid, peer_guid):
139     peer = testbed_instance._elements[peer_guid]
140     filt = testbed_instance._elements[filter_guid]
141     filt.peer_proto = proto
142     filt.peer_guid = peer_guid
143     if filt.iface_guid:
144         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
145
146 def connect_filter_filter(proto, testbed_instance, filter_guid, peer_guid):
147     peer = testbed_instance._elements[peer_guid]
148     filt = testbed_instance._elements[filter_guid]
149     filt.peer_proto = proto
150     peer.peer_proto = proto
151     if filt.iface_guid:
152         peer.peer_guid = filt.iface_guid
153     if peer.iface_guid:
154         filt.peer_guid = peer.iface_guid
155     if filt.iface_guid and filt.peer_guid:
156         connect_tun_iface_peer(filt.peer_proto, testbed_instance, filt.iface_guid, filt.peer_guid)
157
158 def crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data):
159     iface = testbed_instance._elements[iface_guid]
160     iface.peer_iface = None
161     iface.peer_addr = peer_iface_data.get("tun_addr")
162     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
163     iface.peer_port = peer_iface_data.get("tun_port")
164     iface.peer_cipher = peer_iface_data.get("tun_cipher")
165     iface.tun_key = min(iface.tun_key, peer_iface_data.get("tun_key"))
166     iface.tun_proto = proto
167     
168     preconfigure_tuniface(testbed_instance, iface_guid)
169
170 def crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data):
171     # refresh (refreshable) attributes for second-phase
172     iface = testbed_instance._elements[iface_guid]
173     iface.peer_addr = peer_iface_data.get("tun_addr")
174     iface.peer_proto = peer_iface_data.get("tun_proto") or proto
175     iface.peer_port = peer_iface_data.get("tun_port")
176     iface.peer_cipher = peer_iface_data.get("tun_cipher")
177     
178     postconfigure_tuniface(testbed_instance, iface_guid)
179
180 def crossconnect_tun_iface_peer_both(proto, testbed_instance, iface_guid, peer_iface_data):
181     crossconnect_tun_iface_peer_init(proto, testbed_instance, iface_guid, peer_iface_data)
182     crossconnect_tun_iface_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data)
183
184 def crossconnect_filter_peer_init(proto, testbed_instance, filter_guid, peer_data):
185     filt = testbed_instance._elements[filter_guid]
186     filt.peer_proto = proto
187     crossconnect_tun_iface_peer_init(filt.peer_proto, testbed_instance, filt.iface_guid, peer_data)
188
189 def crossconnect_filter_peer_compl(proto, testbed_instance, filter_guid, peer_data):
190     filt = testbed_instance._elements[filter_guid]
191     filt.peer_proto = proto
192     crossconnect_tun_iface_peer_compl(filt.peer_proto, testbed_instance, filt.iface_guid, peer_data)
193
194 def crossconnect_filter_peer_both(proto, testbed_instance, filter_guid, peer_data):
195     crossconnect_filter_peer_init(proto, testbed_instance, iface_guid, peer_iface_data)
196     crossconnect_filter_peer_compl(proto, testbed_instance, iface_guid, peer_iface_data)
197
198 def connect_dep(testbed_instance, node_guid, app_guid, node=None, app=None):
199     node = node or testbed_instance._elements[node_guid]
200     app = app or testbed_instance._elements[app_guid]
201     app.node = node
202     
203     if app.depends:
204         node.required_packages.update(set(
205             app.depends.split() ))
206     
207     if app.add_to_path:
208         if app.home_path and app.home_path not in node.pythonpath:
209             node.pythonpath.append(app.home_path)
210     
211     if app.env:
212         for envkey, envval in app.env.iteritems():
213             envval = app._replace_paths(envval)
214             node.env[envkey].append(envval)
215     
216     if app.rpmFusion:
217         node.rpmFusion = True
218
219 def connect_forwarder(testbed_instance, node_guid, fwd_guid):
220     node = testbed_instance._elements[node_guid]
221     fwd = testbed_instance._elements[fwd_guid]
222     node.multicast_forwarder = fwd
223     
224     if fwd.router:
225         connect_dep(testbed_instance, node_guid, None, app=fwd.router)
226
227     connect_dep(testbed_instance, node_guid, fwd_guid)
228
229 def connect_router(testbed_instance, fwd_guid, router_guid):
230     fwd = testbed_instance._elements[fwd_guid]
231     router = testbed_instance._elements[router_guid]
232     fwd.router = router
233     
234     if fwd.node:
235         connect_dep(testbed_instance, None, router_guid, node=fwd.node)
236
237 def connect_node_netpipe(testbed_instance, node_guid, netpipe_guid):
238     node = testbed_instance._elements[node_guid]
239     netpipe = testbed_instance._elements[netpipe_guid]
240     netpipe.node = node
241     node.required_vsys.add('ipfw-be')
242     node.required_packages.add('ipfwslice')
243     
244
245 ### Creation functions ###
246
247 def create_node(testbed_instance, guid):
248     parameters = testbed_instance._get_parameters(guid)
249     
250     # create element with basic attributes
251     element = testbed_instance._make_node(parameters)
252     
253     # add constraint on number of (real) interfaces
254     # by counting connected devices
255     dev_guids = testbed_instance.get_connected(guid, "devs", "node")
256     num_open_ifaces = sum( # count True values
257         NODEIFACE == testbed_instance._get_factory_id(guid)
258         for guid in dev_guids )
259     element.min_num_external_ifaces = num_open_ifaces
260     
261     # require vroute vsys if we have routes to set up
262     routes = testbed_instance._add_route.get(guid)
263     if routes:
264         vsys = element.routing_method(routes,
265             testbed_instance.vsys_vnet)
266         element.required_vsys.add(vsys)
267     
268     testbed_instance.elements[guid] = element
269
270 def create_nodeiface(testbed_instance, guid):
271     parameters = testbed_instance._get_parameters(guid)
272     element = testbed_instance._make_node_iface(parameters)
273     testbed_instance.elements[guid] = element
274
275 def create_tuniface(testbed_instance, guid):
276     parameters = testbed_instance._get_parameters(guid)
277     element = testbed_instance._make_tun_iface(parameters)
278     
279     # Set custom addresses, if there are any already
280     # Setting this early helps set up P2P links
281     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
282         addresses = testbed_instance._add_address[guid]
283         for address in addresses:
284             (address, netprefix, broadcast) = address
285             element.add_address(address, netprefix, broadcast)
286     
287     testbed_instance.elements[guid] = element
288
289 def create_tapiface(testbed_instance, guid):
290     parameters = testbed_instance._get_parameters(guid)
291     element = testbed_instance._make_tap_iface(parameters)
292     
293     # Set custom addresses, if there are any already
294     # Setting this early helps set up P2P links
295     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
296         addresses = testbed_instance._add_address[guid]
297         for address in addresses:
298             (address, netprefix, broadcast) = address
299             element.add_address(address, netprefix, broadcast)
300     
301     testbed_instance.elements[guid] = element
302
303 def create_tunfilter(testbed_instance, guid):
304     parameters = testbed_instance._get_parameters(guid)
305     element = testbed_instance._make_tun_filter(parameters)
306     testbed_instance.elements[guid] = element
307
308 def create_classqueuefilter(testbed_instance, guid):
309     parameters = testbed_instance._get_parameters(guid)
310     element = testbed_instance._make_class_queue_filter(parameters)
311     testbed_instance.elements[guid] = element
312
313 def create_tosqueuefilter(testbed_instance, guid):
314     parameters = testbed_instance._get_parameters(guid)
315     element = testbed_instance._make_tos_queue_filter(parameters)
316     testbed_instance.elements[guid] = element
317
318 def create_application(testbed_instance, guid):
319     parameters = testbed_instance._get_parameters(guid)
320     element = testbed_instance._make_application(parameters)
321     
322     # Just inject configuration stuff
323     element.home_path = "nepi-app-%s" % (guid,)
324     
325     testbed_instance.elements[guid] = element
326
327 def create_dependency(testbed_instance, guid):
328     parameters = testbed_instance._get_parameters(guid)
329     element = testbed_instance._make_dependency(parameters)
330     
331     # Just inject configuration stuff
332     element.home_path = "nepi-dep-%s" % (guid,)
333     
334     testbed_instance.elements[guid] = element
335
336 def create_nepi_dependency(testbed_instance, guid):
337     parameters = testbed_instance._get_parameters(guid)
338     element = testbed_instance._make_nepi_dependency(parameters)
339     
340     # Just inject configuration stuff
341     element.home_path = "nepi-nepi-%s" % (guid,)
342     
343     testbed_instance.elements[guid] = element
344
345 def create_ns3_dependency(testbed_instance, guid):
346     parameters = testbed_instance._get_parameters(guid)
347     element = testbed_instance._make_ns3_dependency(parameters)
348     
349     # Just inject configuration stuff
350     element.home_path = "nepi-ns3-%s" % (guid,)
351     
352     testbed_instance.elements[guid] = element
353
354 def create_multicast_forwarder(testbed_instance, guid):
355     parameters = testbed_instance._get_parameters(guid)
356     element = testbed_instance._make_multicast_forwarder(parameters)
357     
358     # Just inject configuration stuff
359     element.home_path = "nepi-mcfwd-%s" % (guid,)
360     
361     testbed_instance.elements[guid] = element
362
363 def create_multicast_announcer(testbed_instance, guid):
364     parameters = testbed_instance._get_parameters(guid)
365     element = testbed_instance._make_multicast_announcer(parameters)
366     
367     # Just inject configuration stuff
368     element.home_path = "nepi-mcann-%s" % (guid,)
369     
370     testbed_instance.elements[guid] = element
371
372 def create_multicast_router(testbed_instance, guid):
373     parameters = testbed_instance._get_parameters(guid)
374     element = testbed_instance._make_multicast_router(parameters)
375     
376     # Just inject configuration stuff
377     element.home_path = "nepi-mcrt-%s" % (guid,)
378     
379     testbed_instance.elements[guid] = element
380
381 def create_internet(testbed_instance, guid):
382     parameters = testbed_instance._get_parameters(guid)
383     element = testbed_instance._make_internet(parameters)
384     testbed_instance.elements[guid] = element
385
386 def create_netpipe(testbed_instance, guid):
387     parameters = testbed_instance._get_parameters(guid)
388     element = testbed_instance._make_netpipe(parameters)
389     testbed_instance.elements[guid] = element
390
391 ### Start/Stop functions ###
392
393 def start_application(testbed_instance, guid):
394     parameters = testbed_instance._get_parameters(guid)
395     traces = testbed_instance._get_traces(guid)
396     app = testbed_instance.elements[guid]
397     
398     app.stdout = "stdout" in traces
399     app.stderr = "stderr" in traces
400     app.buildlog = "buildlog" in traces
401     app.outout = "output" in traces
402     
403     app.start()
404
405 def stop_application(testbed_instance, guid):
406     app = testbed_instance.elements[guid]
407     app.kill()
408
409 ### Status functions ###
410
411 def status_application(testbed_instance, guid):
412     if guid not in testbed_instance.elements.keys():
413         return AS.STATUS_NOT_STARTED
414     
415     app = testbed_instance.elements[guid]
416     return app.status()
417
418 ### Configure functions ###
419
420 def configure_nodeiface(testbed_instance, guid):
421     element = testbed_instance._elements[guid]
422     
423     # Cannot explicitly configure addresses
424     if guid in testbed_instance._add_address:
425         raise ValueError, "Cannot explicitly set address of public PlanetLab interface"
426     
427     # Get siblings
428     node_guid = testbed_instance.get_connected(guid, "node", "devs")[0]
429     dev_guids = testbed_instance.get_connected(node_guid, "node", "devs")
430     siblings = [ self._element[dev_guid] 
431                  for dev_guid in dev_guids
432                  if dev_guid != guid ]
433     
434     # Fetch address from PLC api
435     element.pick_iface(siblings)
436     
437     # Do some validations
438     element.validate()
439
440 def preconfigure_tuniface(testbed_instance, guid):
441     element = testbed_instance._elements[guid]
442     
443     # Set custom addresses if any, and if not set already
444     if guid in testbed_instance._add_address and not (element.address or element.netmask or element.netprefix):
445         addresses = testbed_instance._add_address[guid]
446         for address in addresses:
447             (address, netprefix, broadcast) = address
448             element.add_address(address, netprefix, broadcast)
449     
450     # Link to external interface, if any
451     for iface in testbed_instance._elements.itervalues():
452         if isinstance(iface, testbed_instance._interfaces.NodeIface) and iface.node is element.node and iface.has_internet:
453             element.external_iface = iface
454             break
455
456     # Set standard TUN attributes
457     if (not element.tun_addr or not element.tun_port) and element.external_iface:
458         element.tun_addr = element.external_iface.address
459         element.tun_port = testbed_instance.tapPortBase + int(guid)
460
461     # Set enabled traces
462     traces = testbed_instance._get_traces(guid)
463     for capmode in ('pcap', 'packets'):
464         if capmode in traces:
465             element.capture = capmode
466             break
467     else:
468         element.capture = False
469     
470     # Do some validations
471     element.validate()
472     
473     # First-phase setup
474     element.prepare('tun-%s' % (guid,))
475
476 def postconfigure_tuniface(testbed_instance, guid):
477     element = testbed_instance._elements[guid]
478     
479     # Second-phase setup
480     element.launch()
481     
482 def prestart_tuniface(testbed_instance, guid):
483     element = testbed_instance._elements[guid]
484     
485     # Second-phase setup
486     element.wait()
487
488 def configure_node(testbed_instance, guid):
489     node = testbed_instance._elements[guid]
490     
491     # Just inject configuration stuff
492     node.home_path = "nepi-node-%s" % (guid,)
493     node.ident_path = testbed_instance.sliceSSHKey
494     node.slicename = testbed_instance.slicename
495     
496     # Do some validations
497     node.validate()
498     
499     # this will be done in parallel in all nodes
500     # this call only spawns the process
501     node.install_dependencies()
502
503 def configure_node_routes(testbed_instance, guid):
504     node = testbed_instance._elements[guid]
505     routes = testbed_instance._add_route.get(guid)
506     
507     if routes:
508         devs = [ dev
509             for dev_guid in testbed_instance.get_connected(guid, "devs", "node")
510             for dev in ( testbed_instance._elements.get(dev_guid) ,)
511             if dev and isinstance(dev, testbed_instance._interfaces.TunIface) ]
512     
513         vsys = testbed_instance.vsys_vnet
514         
515         node.configure_routes(routes, devs, vsys)
516
517 def configure_application(testbed_instance, guid):
518     app = testbed_instance._elements[guid]
519     
520     # Do some validations
521     app.validate()
522     
523     # Wait for dependencies
524     app.node.wait_dependencies()
525     
526     # Install stuff
527     app.async_setup()
528
529 def configure_dependency(testbed_instance, guid):
530     dep = testbed_instance._elements[guid]
531     
532     # Do some validations
533     dep.validate()
534     
535     # Wait for dependencies
536     dep.node.wait_dependencies()
537     
538     # Install stuff
539     dep.async_setup()
540
541 def configure_announcer(testbed_instance, guid):
542     # Link ifaces
543     fwd = testbed_instance._elements[guid]
544     fwd.ifaces = [ dev
545         for node_guid in testbed_instance.get_connected(guid, "node", "apps")
546         for dev_guid in testbed_instance.get_connected(node_guid, "devs", "node")
547         for dev in ( testbed_instance._elements.get(dev_guid) ,)
548         if dev and isinstance(dev, testbed_instance._interfaces.TunIface)
549             and dev.multicast ]
550     
551     # Install stuff
552     configure_dependency(testbed_instance, guid)
553
554 def configure_forwarder(testbed_instance, guid):
555     configure_announcer(testbed_instance, guid)
556     
557     # Link ifaces to forwarder
558     fwd = testbed_instance._elements[guid]
559     for iface in fwd.ifaces:
560         iface.multicast_forwarder = '/var/run/mcastfwd'
561
562 def configure_router(testbed_instance, guid):
563     # Link ifaces
564     rt = testbed_instance._elements[guid]
565     rt.nonifaces = [ dev
566         for fwd_guid in testbed_instance.get_connected(guid, "fwd", "router")
567         for node_guid in testbed_instance.get_connected(fwd_guid, "node", "apps")
568         for dev_guid in testbed_instance.get_connected(node_guid, "devs", "node")
569         for dev in ( testbed_instance._elements.get(dev_guid) ,)
570         if dev and isinstance(dev, testbed_instance._interfaces.TunIface)
571             and not dev.multicast ]
572     
573     # Install stuff
574     configure_dependency(testbed_instance, guid)
575
576 def configure_netpipe(testbed_instance, guid):
577     netpipe = testbed_instance._elements[guid]
578     
579     # Do some validations
580     netpipe.validate()
581     
582     # Wait for dependencies
583     netpipe.node.wait_dependencies()
584     
585     # Install rules
586     netpipe.configure()
587
588 ### Factory information ###
589
590 connector_types = dict({
591     "apps": dict({
592                 "help": "Connector from node to applications", 
593                 "name": "apps",
594                 "max": -1, 
595                 "min": 0
596             }),
597     "devs": dict({
598                 "help": "Connector from node to network interfaces", 
599                 "name": "devs",
600                 "max": -1, 
601                 "min": 0
602             }),
603     "deps": dict({
604                 "help": "Connector from node to application dependencies "
605                         "(packages and applications that need to be installed)", 
606                 "name": "deps",
607                 "max": -1, 
608                 "min": 0
609             }),
610     "inet": dict({
611                 "help": "Connector from network interfaces to the internet", 
612                 "name": "inet",
613                 "max": 1, 
614                 "min": 1
615             }),
616     "node": dict({
617                 "help": "Connector to a Node", 
618                 "name": "node",
619                 "max": 1, 
620                 "min": 1
621             }),
622     "router": dict({
623                 "help": "Connector to a routing daemon", 
624                 "name": "router",
625                 "max": 1, 
626                 "min": 1
627             }),
628     "fwd": dict({
629                 "help": "Forwarder this routing daemon communicates with", 
630                 "name": "fwd",
631                 "max": 1, 
632                 "min": 1
633             }),
634     "pipes": dict({
635                 "help": "Connector to a NetPipe", 
636                 "name": "pipes",
637                 "max": 2, 
638                 "min": 0
639             }),
640     
641     "tcp": dict({
642                 "help": "ip-ip tunneling over TCP link", 
643                 "name": "tcp",
644                 "max": 1, 
645                 "min": 0
646             }),
647     "udp": dict({
648                 "help": "ip-ip tunneling over UDP datagrams", 
649                 "name": "udp",
650                 "max": 1, 
651                 "min": 0
652             }),
653     "gre": dict({
654                 "help": "IP or Ethernet tunneling using the GRE protocol", 
655                 "name": "gre",
656                 "max": 1, 
657                 "min": 0
658             }),
659     "fd->": dict({
660                 "help": "TUN device file descriptor provider", 
661                 "name": "fd->",
662                 "max": 1, 
663                 "min": 0
664             }),
665     "->fd": dict({
666                 "help": "TUN device file descriptor slot", 
667                 "name": "->fd",
668                 "max": 1, 
669                 "min": 0
670             }),
671    })
672
673 connections = [
674     dict({
675         "from": (TESTBED_ID, NODE, "devs"),
676         "to":   (TESTBED_ID, NODEIFACE, "node"),
677         "init_code": connect_node_iface_node,
678         "can_cross": False
679     }),
680     dict({
681         "from": (TESTBED_ID, NODE, "devs"),
682         "to":   (TESTBED_ID, TUNIFACE, "node"),
683         "init_code": connect_tun_iface_node,
684         "can_cross": False
685     }),
686     dict({
687         "from": (TESTBED_ID, NODE, "devs"),
688         "to":   (TESTBED_ID, TAPIFACE, "node"),
689         "init_code": connect_tun_iface_node,
690         "can_cross": False
691     }),
692     dict({
693         "from": (TESTBED_ID, NODEIFACE, "inet"),
694         "to":   (TESTBED_ID, INTERNET, "devs"),
695         "init_code": connect_node_iface_inet,
696         "can_cross": False
697     }),
698     dict({
699         "from": (TESTBED_ID, NODE, "apps"),
700         "to":   (TESTBED_ID, (APPLICATION, DEPENDENCY, NEPIDEPENDENCY, NS3DEPENDENCY, MULTICASTANNOUNCER), "node"),
701         "init_code": connect_dep,
702         "can_cross": False
703     }),
704     dict({
705         "from": (TESTBED_ID, NODE, "apps"),
706         "to":   (TESTBED_ID, MULTICASTFORWARDER, "node"),
707         "init_code": connect_forwarder,
708         "can_cross": False
709     }),
710     dict({
711         "from": (TESTBED_ID, MULTICASTFORWARDER, "router"),
712         "to":   (TESTBED_ID, MULTICASTROUTER, "fwd"),
713         "init_code": connect_router,
714         "can_cross": False
715     }),
716     dict({
717         "from": (TESTBED_ID, TUNIFACE, "tcp"),
718         "to":   (TESTBED_ID, TUNIFACE, "tcp"),
719         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
720         "can_cross": False
721     }),
722     dict({
723         "from": (TESTBED_ID, TUNIFACE, "udp"),
724         "to":   (TESTBED_ID, TUNIFACE, "udp"),
725         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
726         "can_cross": False
727     }),
728     dict({
729         "from": (TESTBED_ID, TUNIFACE, "gre"),
730         "to":   (TESTBED_ID, TUNIFACE, "gre"),
731         "init_code": functools.partial(connect_tun_iface_peer,"gre"),
732         "can_cross": False
733     }),
734     dict({
735         "from": (TESTBED_ID, TUNIFACE, "fd->"),
736         "to":   (TESTBED_ID, TUNFILTERS, "->fd"),
737         "init_code": connect_tun_iface_filter,
738         "can_cross": False
739     }),
740     dict({
741         "from": (TESTBED_ID, TUNFILTERS, "tcp"),
742         "to":   (TESTBED_ID, TUNIFACE, "tcp"),
743         "init_code": functools.partial(connect_filter_peer,"tcp"),
744         "can_cross": False
745     }),
746     dict({
747         "from": (TESTBED_ID, TUNFILTERS, "udp"),
748         "to":   (TESTBED_ID, TUNIFACE, "udp"),
749         "init_code": functools.partial(connect_filter_peer,"udp"),
750         "can_cross": False
751     }),
752     dict({
753         "from": (TESTBED_ID, TAPIFACE, "tcp"),
754         "to":   (TESTBED_ID, TAPIFACE, "tcp"),
755         "init_code": functools.partial(connect_tun_iface_peer,"tcp"),
756         "can_cross": False
757     }),
758     dict({
759         "from": (TESTBED_ID, TAPIFACE, "udp"),
760         "to":   (TESTBED_ID, TAPIFACE, "udp"),
761         "init_code": functools.partial(connect_tun_iface_peer,"udp"),
762         "can_cross": False
763     }),
764     dict({
765         "from": (TESTBED_ID, TAPIFACE, "gre"),
766         "to":   (TESTBED_ID, TAPIFACE, "gre"),
767         "init_code": functools.partial(connect_tun_iface_peer,"gre"),
768         "can_cross": False
769     }),
770     dict({
771         "from": (TESTBED_ID, TAPIFACE, "fd->"),
772         "to":   (TESTBED_ID, TAPFILTERS, "->fd"),
773         "init_code": connect_tun_iface_filter,
774         "can_cross": False
775     }),
776     dict({
777         "from": (TESTBED_ID, TAPFILTERS, "tcp"),
778         "to":   (TESTBED_ID, TAPIFACE, "tcp"),
779         "init_code": functools.partial(connect_filter_peer,"tcp"),
780         "can_cross": False
781     }),
782     dict({
783         "from": (TESTBED_ID, TAPFILTERS, "udp"),
784         "to":   (TESTBED_ID, TAPIFACE, "udp"),
785         "init_code": functools.partial(connect_filter_peer,"udp"),
786         "can_cross": False
787     }),
788     dict({
789         "from": (TESTBED_ID, TUNFILTERS, "tcp"),
790         "to":   (TESTBED_ID, TUNFILTERS, "tcp"),
791         "init_code": functools.partial(connect_filter_filter,"tcp"),
792         "can_cross": False
793     }),
794     dict({
795         "from": (TESTBED_ID, TUNFILTERS, "udp"),
796         "to":   (TESTBED_ID, TUNFILTERS, "udp"),
797         "init_code": functools.partial(connect_filter_filter,"udp"),
798         "can_cross": False
799     }),
800     dict({
801         "from": (TESTBED_ID, TAPFILTERS, "tcp"),
802         "to":   (TESTBED_ID, TAPFILTERS, "tcp"),
803         "init_code": functools.partial(connect_filter_filter,"tcp"),
804         "can_cross": False
805     }),
806     dict({
807         "from": (TESTBED_ID, TAPFILTERS, "udp"),
808         "to":   (TESTBED_ID, TAPFILTERS, "udp"),
809         "init_code": functools.partial(connect_filter_filter,"udp"),
810         "can_cross": False
811     }),
812     dict({
813         "from": (TESTBED_ID, TUNIFACE, "tcp"),
814         "to":   (None, None, "tcp"),
815         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
816         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
817         "can_cross": True
818     }),
819     dict({
820         "from": (TESTBED_ID, TUNIFACE, "udp"),
821         "to":   (None, None, "udp"),
822         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
823         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
824         "can_cross": True
825     }),
826     dict({
827         "from": (TESTBED_ID, TUNIFACE, "fd->"),
828         "to":   (None, None, "->fd"),
829         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
830         "can_cross": True
831     }),
832     dict({
833         "from": (TESTBED_ID, TUNIFACE, "gre"),
834         "to":   (None, None, "gre"),
835         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"gre"),
836         "can_cross": True
837     }),
838     dict({
839         "from": (TESTBED_ID, TAPIFACE, "tcp"),
840         "to":   (None, None, "tcp"),
841         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"tcp"),
842         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"tcp"),
843         "can_cross": True
844     }),
845     dict({
846         "from": (TESTBED_ID, TAPIFACE, "udp"),
847         "to":   (None, None, "udp"),
848         "init_code": functools.partial(crossconnect_tun_iface_peer_init,"udp"),
849         "compl_code": functools.partial(crossconnect_tun_iface_peer_compl,"udp"),
850         "can_cross": True
851     }),
852     dict({
853         "from": (TESTBED_ID, TAPIFACE, "fd->"),
854         "to":   (None, None, "->fd"),
855         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"fd"),
856         "can_cross": True
857     }),
858     # EGRE is an extension of PlanetLab, so we can't connect externally
859     # if the other testbed isn't another PlanetLab
860     dict({
861         "from": (TESTBED_ID, TAPIFACE, "gre"),
862         "to":   (TESTBED_ID, None, "gre"),
863         "compl_code": functools.partial(crossconnect_tun_iface_peer_both,"gre"),
864         "can_cross": True
865     }),
866     dict({
867         "from": (TESTBED_ID, ALLFILTERS, "tcp"),
868         "to":   (None, None, "tcp"),
869         "init_code": functools.partial(crossconnect_filter_peer_init,"tcp"),
870         "compl_code": functools.partial(crossconnect_filter_peer_compl,"tcp"),
871         "can_cross": True
872     }),
873     dict({
874         "from": (TESTBED_ID, ALLFILTERS, "udp"),
875         "to":   (None, None, "udp"),
876         "init_code": functools.partial(crossconnect_filter_peer_init,"udp"),
877         "compl_code": functools.partial(crossconnect_filter_peer_compl,"udp"),
878         "can_cross": True
879     }),
880 ]
881
882 attributes = dict({
883     "forward_X11": dict({      
884                 "name": "forward_X11",
885                 "help": "Forward x11 from main namespace to the node",
886                 "type": Attribute.BOOL, 
887                 "value": False,
888                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
889                 "validation_function": validation.is_bool,
890             }),
891     "hostname": dict({      
892                 "name": "hostname",
893                 "help": "Constrain hostname during resource discovery. May use wildcards.",
894                 "type": Attribute.STRING, 
895                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
896                 "validation_function": validation.is_string,
897             }),
898     "city": dict({      
899                 "name": "city",
900                 "help": "Constrain location (city) during resource discovery. May use wildcards.",
901                 "type": Attribute.STRING, 
902                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
903                 "validation_function": validation.is_string,
904             }),
905     "country": dict({      
906                 "name": "hostname",
907                 "help": "Constrain location (country) during resource discovery. May use wildcards.",
908                 "type": Attribute.STRING, 
909                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
910                 "validation_function": validation.is_string,
911             }),
912     "region": dict({      
913                 "name": "hostname",
914                 "help": "Constrain location (region) during resource discovery. May use wildcards.",
915                 "type": Attribute.STRING, 
916                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
917                 "validation_function": validation.is_string,
918             }),
919     "architecture": dict({      
920                 "name": "architecture",
921                 "help": "Constrain architexture during resource discovery.",
922                 "type": Attribute.ENUM, 
923                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
924                 "allowed": ["x86_64",
925                             "i386"],
926                 "validation_function": validation.is_enum,
927             }),
928     "operating_system": dict({      
929                 "name": "operatingSystem",
930                 "help": "Constrain operating system during resource discovery.",
931                 "type": Attribute.ENUM, 
932                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
933                 "allowed": ["f8",
934                             "f12",
935                             "f14",
936                             "centos",
937                             "other"],
938                 "validation_function": validation.is_enum,
939             }),
940     "site": dict({      
941                 "name": "site",
942                 "help": "Constrain the PlanetLab site this node should reside on.",
943                 "type": Attribute.ENUM, 
944                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
945                 "allowed": ["PLE",
946                             "PLC",
947                             "PLJ"],
948                 "validation_function": validation.is_enum,
949             }),
950     "min_reliability": dict({
951                 "name": "minReliability",
952                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies a lower acceptable bound.",
953                 "type": Attribute.DOUBLE,
954                 "range": (0,100),
955                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
956                 "validation_function": validation.is_number,
957             }),
958     "max_reliability": dict({
959                 "name": "maxReliability",
960                 "help": "Constrain reliability while picking PlanetLab nodes. Specifies an upper acceptable bound.",
961                 "type": Attribute.DOUBLE,
962                 "range": (0,100),
963                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
964                 "validation_function": validation.is_number,
965             }),
966     "min_bandwidth": dict({
967                 "name": "minBandwidth",
968                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies a lower acceptable bound.",
969                 "type": Attribute.DOUBLE,
970                 "range": (0,2**31),
971                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
972                 "validation_function": validation.is_number,
973             }),
974     "max_bandwidth": dict({
975                 "name": "maxBandwidth",
976                 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies an upper acceptable bound.",
977                 "type": Attribute.DOUBLE,
978                 "range": (0,2**31),
979                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
980                 "validation_function": validation.is_number,
981             }),
982     "min_load": dict({
983                 "name": "minLoad",
984                 "help": "Constrain node load average while picking PlanetLab nodes. Specifies a lower acceptable bound.",
985                 "type": Attribute.DOUBLE,
986                 "range": (0,2**31),
987                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
988                 "validation_function": validation.is_number,
989             }),
990     "max_load": dict({
991                 "name": "maxLoad",
992                 "help": "Constrain node load average while picking PlanetLab nodes. Specifies an upper acceptable bound.",
993                 "type": Attribute.DOUBLE,
994                 "range": (0,2**31),
995                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
996                 "validation_function": validation.is_number,
997             }),
998     "min_cpu": dict({
999                 "name": "minCpu",
1000                 "help": "Constrain available cpu time while picking PlanetLab nodes. Specifies a lower acceptable bound.",
1001                 "type": Attribute.DOUBLE,
1002                 "range": (0,100),
1003                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1004                 "validation_function": validation.is_number,
1005             }),
1006     "max_cpu": dict({
1007                 "name": "maxCpu",
1008                 "help": "Constrain available cpu time while picking PlanetLab nodes. Specifies an upper acceptable bound.",
1009                 "type": Attribute.DOUBLE,
1010                 "range": (0,100),
1011                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1012                 "validation_function": validation.is_number,
1013             }),
1014             
1015     "up": dict({
1016                 "name": "up",
1017                 "help": "Link up",
1018                 "type": Attribute.BOOL,
1019                 "value": False,
1020                 "validation_function": validation.is_bool
1021             }),
1022     "primary": dict({
1023                 "name": "primary",
1024                 "help": "This is the primary interface for the attached node",
1025                 "type": Attribute.BOOL,
1026                 "value": True,
1027                 "validation_function": validation.is_bool
1028             }),
1029     "if_name": dict({
1030                 "name": "if_name",
1031                 "help": "Device name",
1032                 "type": Attribute.STRING,
1033                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1034                 "validation_function": validation.is_string
1035             }),
1036     "mtu":  dict({
1037                 "name": "mtu", 
1038                 "help": "Maximum transmition unit for device",
1039                 "type": Attribute.INTEGER,
1040                 "range": (0,1500),
1041                 "validation_function": validation.is_integer_range(0,1500)
1042             }),
1043     "mask":  dict({
1044                 "name": "mask", 
1045                 "help": "Network mask for the device (eg: 24 for /24 network)",
1046                 "type": Attribute.INTEGER,
1047                 "validation_function": validation.is_integer_range(8,24)
1048             }),
1049     "snat":  dict({
1050                 "name": "snat", 
1051                 "help": "Enable SNAT (source NAT to the internet) no this device",
1052                 "type": Attribute.BOOL,
1053                 "value": False,
1054                 "validation_function": validation.is_bool
1055             }),
1056     "multicast":  dict({
1057                 "name": "multicast", 
1058                 "help": "Enable multicast forwarding on this device. "
1059                         "Note that you still need a multicast routing daemon "
1060                         "in the node.",
1061                 "type": Attribute.BOOL,
1062                 "value": False,
1063                 "validation_function": validation.is_bool
1064             }),
1065     "pointopoint":  dict({
1066                 "name": "pointopoint", 
1067                 "help": "If the interface is a P2P link, the remote endpoint's IP "
1068                         "should be set on this attribute.",
1069                 "type": Attribute.STRING,
1070                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1071                 "validation_function": validation.is_string
1072             }),
1073     "bwlimit":  dict({
1074                 "name": "bwlimit", 
1075                 "help": "Emulated transmission speed (in kbytes per second)",
1076                 "type": Attribute.INTEGER,
1077                 "range" : (1,10*2**20),
1078                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1079                 "validation_function": validation.is_integer
1080             }),
1081     "txqueuelen":  dict({
1082                 "name": "txqueuelen", 
1083                 "help": "Transmission queue length (in packets)",
1084                 "type": Attribute.INTEGER,
1085                 "value": 1000,
1086                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1087                 "range" : (1,10000),
1088                 "validation_function": validation.is_integer
1089             }),
1090             
1091     "command": dict({
1092                 "name": "command",
1093                 "help": "Command line string",
1094                 "type": Attribute.STRING,
1095                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1096                 "validation_function": validation.is_string
1097             }),
1098     "sudo": dict({
1099                 "name": "sudo",
1100                 "help": "Run with root privileges",
1101                 "type": Attribute.BOOL,
1102                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1103                 "value": False,
1104                 "validation_function": validation.is_bool
1105             }),
1106     "stdin": dict({
1107                 "name": "stdin",
1108                 "help": "Standard input",
1109                 "type": Attribute.STRING,
1110                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1111                 "validation_function": validation.is_string
1112             }),
1113             
1114     "depends": dict({
1115                 "name": "depends",
1116                 "help": "Space-separated list of packages required to run the application",
1117                 "type": Attribute.STRING,
1118                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1119                 "validation_function": validation.is_string
1120             }),
1121     "build-depends": dict({
1122                 "name": "buildDepends",
1123                 "help": "Space-separated list of packages required to build the application",
1124                 "type": Attribute.STRING,
1125                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1126                 "validation_function": validation.is_string
1127             }),
1128     "rpm-fusion": dict({
1129                 "name": "rpmFusion",
1130                 "help": "True if required packages can be found in the RpmFusion repository",
1131                 "type": Attribute.BOOL,
1132                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1133                 "value": False,
1134                 "validation_function": validation.is_bool
1135             }),
1136     "sources": dict({
1137                 "name": "sources",
1138                 "help": "Space-separated list of regular files to be deployed in the working path prior to building. "
1139                         "Archives won't be expanded automatically.",
1140                 "type": Attribute.STRING,
1141                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1142                 "validation_function": validation.is_string
1143             }),
1144     "build": dict({
1145                 "name": "build",
1146                 "help": "Build commands to execute after deploying the sources. "
1147                         "Sources will be in the ${SOURCES} folder. "
1148                         "Example: tar xzf ${SOURCES}/my-app.tgz && cd my-app && ./configure && make && make clean.\n"
1149                         "Try to make the commands return with a nonzero exit code on error.\n"
1150                         "Also, do not install any programs here, use the 'install' attribute. This will "
1151                         "help keep the built files constrained to the build folder (which may "
1152                         "not be the home folder), and will result in faster deployment. Also, "
1153                         "make sure to clean up temporary files, to reduce bandwidth usage between "
1154                         "nodes when transferring built packages.",
1155                 "type": Attribute.STRING,
1156                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1157                 "validation_function": validation.is_string
1158             }),
1159     "install": dict({
1160                 "name": "install",
1161                 "help": "Commands to transfer built files to their final destinations. "
1162                         "Sources will be in the initial working folder, and a special "
1163                         "tag ${SOURCES} can be used to reference the experiment's "
1164                         "home folder (where the application commands will run).\n"
1165                         "ALL sources and targets needed for execution must be copied there, "
1166                         "if building has been enabled.\n"
1167                         "That is, 'slave' nodes will not automatically get any source files. "
1168                         "'slave' nodes don't get build dependencies either, so if you need "
1169                         "make and other tools to install, be sure to provide them as "
1170                         "actual dependencies instead.",
1171                 "type": Attribute.STRING,
1172                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1173                 "validation_function": validation.is_string
1174             }),
1175     
1176     "netpipe_mode": dict({      
1177                 "name": "mode",
1178                 "help": "Link mode:\n"
1179                         " * SERVER: applies to incoming connections\n"
1180                         " * CLIENT: applies to outgoing connections\n"
1181                         " * SERVICE: applies to both",
1182                 "type": Attribute.ENUM, 
1183                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1184                 "allowed": ["SERVER",
1185                             "CLIENT",
1186                             "SERVICE"],
1187                 "validation_function": validation.is_enum,
1188             }),
1189     "port_list":  dict({
1190                 "name": "portList", 
1191                 "help": "Port list or range. Eg: '22', '22,23,27', '20-2000'",
1192                 "type": Attribute.STRING,
1193                 "validation_function": is_portlist,
1194             }),
1195     "addr_list":  dict({
1196                 "name": "addrList", 
1197                 "help": "Address list or range. Eg: '127.0.0.1', '127.0.0.1,127.0.1.1', '127.0.0.1/8'",
1198                 "type": Attribute.STRING,
1199                 "validation_function": is_addrlist,
1200             }),
1201     "bw_in":  dict({
1202                 "name": "bwIn", 
1203                 "help": "Inbound bandwidth limit (in Mbit/s)",
1204                 "type": Attribute.DOUBLE,
1205                 "validation_function": validation.is_number,
1206             }),
1207     "bw_out":  dict({
1208                 "name": "bwOut", 
1209                 "help": "Outbound bandwidth limit (in Mbit/s)",
1210                 "type": Attribute.DOUBLE,
1211                 "validation_function": validation.is_number,
1212             }),
1213     "plr_in":  dict({
1214                 "name": "plrIn", 
1215                 "help": "Inbound packet loss rate (0 = no loss, 1 = 100% loss)",
1216                 "type": Attribute.DOUBLE,
1217                 "validation_function": validation.is_number,
1218             }),
1219     "plr_out":  dict({
1220                 "name": "plrOut", 
1221                 "help": "Outbound packet loss rate (0 = no loss, 1 = 100% loss)",
1222                 "type": Attribute.DOUBLE,
1223                 "validation_function": validation.is_number,
1224             }),
1225     "delay_in":  dict({
1226                 "name": "delayIn", 
1227                 "help": "Inbound packet delay (in milliseconds)",
1228                 "type": Attribute.INTEGER,
1229                 "range": (0,60000),
1230                 "validation_function": validation.is_integer,
1231             }),
1232     "delay_out":  dict({
1233                 "name": "delayOut", 
1234                 "help": "Outbound packet delay (in milliseconds)",
1235                 "type": Attribute.INTEGER,
1236                 "range": (0,60000),
1237                 "validation_function": validation.is_integer,
1238             }),
1239     "module": dict({
1240                 "name": "module",
1241                 "help": "Path to a .c or .py source for a filter module, or a binary .so",
1242                 "type": Attribute.STRING,
1243                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1244                 "validation_function": validation.is_string
1245             }),
1246     "args": dict({
1247                 "name": "args",
1248                 "help": "Module arguments - comma-separated list of name=value pairs",
1249                 "type": Attribute.STRING,
1250                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1251                 "validation_function": validation.is_string
1252             }),
1253     "routing_algorithm": dict({      
1254             "name": "algorithm",
1255             "help": "Routing algorithm.",
1256             "value": "dvmrp",
1257             "type": Attribute.ENUM, 
1258             "allowed": ["dvmrp"],
1259             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1260             "validation_function": validation.is_enum,
1261         }),
1262     })
1263
1264 traces = dict({
1265     "stdout": dict({
1266                 "name": "stdout",
1267                 "help": "Standard output stream"
1268               }),
1269     "stderr": dict({
1270                 "name": "stderr",
1271                 "help": "Application standard error",
1272               }),
1273     "buildlog": dict({
1274                 "name": "buildlog",
1275                 "help": "Output of the build process",
1276               }), 
1277     
1278     "netpipe_stats": dict({
1279                 "name": "netpipeStats",
1280                 "help": "Information about rule match counters, packets dropped, etc.",
1281               }),
1282
1283     "packets": dict({
1284                 "name": "packets",
1285                 "help": "Detailled log of all packets going through the interface",
1286               }),
1287     "pcap": dict({
1288                 "name": "pcap",
1289                 "help": "PCAP trace of all packets going through the interface",
1290               }),
1291     "output": dict({
1292                 "name": "output",
1293                 "help": "Extra output trace for applications. When activated this trace can be referenced with wildcard a reference from an Application command line. Ex: command: 'tcpdump -w {#[elemet-label].trace[trace-id].[name|path]#}' ",
1294               }),
1295     "dropped_stats": dict({
1296                 "name": "dropped_stats",
1297                 "help": "Information on dropped packets on a filer or queue associated to a network interface",
1298             }),
1299     })
1300
1301 create_order = [ 
1302     INTERNET, NODE, NODEIFACE, CLASSQUEUEFILTER, TOSQUEUEFILTER, 
1303     MULTICASTANNOUNCER, MULTICASTFORWARDER, MULTICASTROUTER, 
1304     TUNFILTER, TAPIFACE, TUNIFACE, NETPIPE, 
1305     NEPIDEPENDENCY, NS3DEPENDENCY, DEPENDENCY, APPLICATION ]
1306
1307 configure_order = [ 
1308     INTERNET, Parallel(NODE), 
1309     NODEIFACE, 
1310     Parallel(MULTICASTANNOUNCER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTROUTER), 
1311     Parallel(TAPIFACE), Parallel(TUNIFACE), NETPIPE, 
1312     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), Parallel(APPLICATION) ]
1313
1314 # Start (and prestart) node after ifaces, because the node needs the ifaces in order to set up routes
1315 start_order = [ INTERNET, 
1316     NODEIFACE, 
1317     Parallel(TAPIFACE), Parallel(TUNIFACE), 
1318     Parallel(NODE), NETPIPE, 
1319     Parallel(MULTICASTANNOUNCER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTROUTER), 
1320     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), Parallel(APPLICATION) ]
1321
1322 # cleanup order
1323 shutdown_order = [ 
1324     Parallel(APPLICATION), 
1325     Parallel(MULTICASTROUTER), Parallel(MULTICASTFORWARDER), Parallel(MULTICASTANNOUNCER), 
1326     Parallel(TAPIFACE), Parallel(TUNIFACE), Parallel(NETPIPE), 
1327     Parallel(NEPIDEPENDENCY), Parallel(NS3DEPENDENCY), Parallel(DEPENDENCY), 
1328     NODEIFACE, Parallel(NODE) ]
1329
1330 factories_info = dict({
1331     NODE: dict({
1332             "help": "Virtualized Node (V-Server style)",
1333             "category": FC.CATEGORY_NODES,
1334             "create_function": create_node,
1335             "preconfigure_function": configure_node,
1336             "prestart_function": configure_node_routes,
1337             "box_attributes": [
1338                 "forward_X11",
1339                 "hostname",
1340                 "architecture",
1341                 "operating_system",
1342                 "site",
1343                 "min_reliability",
1344                 "max_reliability",
1345                 "min_bandwidth",
1346                 "max_bandwidth",
1347                 
1348                 # NEPI-in-NEPI attributes
1349                 ATTR_NEPI_TESTBED_ENVIRONMENT_SETUP,
1350             ],
1351             "connector_types": ["devs", "apps", "pipes", "deps"],
1352             "tags": [tags.NODE, tags.ALLOW_ROUTES],
1353        }),
1354     NODEIFACE: dict({
1355             "help": "External network interface - they cannot be brought up or down, and they MUST be connected to the internet.",
1356             "category": FC.CATEGORY_DEVICES,
1357             "create_function": create_nodeiface,
1358             "preconfigure_function": configure_nodeiface,
1359             "box_attributes": [ ],
1360             "connector_types": ["node", "inet"],
1361             "tags": [tags.INTERFACE, tags.HAS_ADDRESSES],
1362         }),
1363     TUNIFACE: dict({
1364             "help": "Virtual TUN network interface (layer 3)",
1365             "category": FC.CATEGORY_DEVICES,
1366             "create_function": create_tuniface,
1367             "preconfigure_function": preconfigure_tuniface,
1368             "configure_function": postconfigure_tuniface,
1369             "prestart_function": prestart_tuniface,
1370             "box_attributes": [
1371                 "up", "if_name", "mtu", "snat", "pointopoint", "multicast", "bwlimit",
1372                 "txqueuelen",
1373                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1374             ],
1375             "traces": ["packets", "pcap"],
1376             "connector_types": ["node","udp","tcp","fd->","gre"],
1377             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
1378         }),
1379     TAPIFACE: dict({
1380             "help": "Virtual TAP network interface (layer 2)",
1381             "category": FC.CATEGORY_DEVICES,
1382             "create_function": create_tapiface,
1383             "preconfigure_function": preconfigure_tuniface,
1384             "configure_function": postconfigure_tuniface,
1385             "prestart_function": prestart_tuniface,
1386             "box_attributes": [
1387                 "up", "if_name", "mtu", "snat", "pointopoint", "multicast", "bwlimit",
1388                 "txqueuelen",
1389                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1390             ],
1391             "traces": ["packets", "pcap"],
1392             "connector_types": ["node","udp","tcp","fd->","gre"],
1393             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
1394         }),
1395     TUNFILTER: dict({
1396             "help": "TUN/TAP stream filter\n\n"
1397                     "If specified, it should be either a .py or .so module. "
1398                     "It will be loaded, and all incoming and outgoing packets "
1399                     "will be routed through it. The filter will not be responsible "
1400                     "for buffering, packet queueing is performed in tun_connect "
1401                     "already, so it should not concern itself with it. It should "
1402                     "not, however, block in one direction if the other is congested.\n"
1403                     "\n"
1404                     "Modules are expected to have the following methods:\n"
1405                     "\tinit(**args)\n"
1406                     "\t\tIf arguments are given, this method will be called with the\n"
1407                     "\t\tgiven arguments (as keyword args in python modules, or a single\n"
1408                     "\taccept_packet(packet, direction):\n"
1409                     "\t\tDecide whether to drop the packet. Direction is 0 for packets "
1410                         "coming from the local side to the remote, and 1 is for packets "
1411                         "coming from the remote side to the local. Return a boolean, "
1412                         "true if the packet is not to be dropped.\n"
1413                     "\tfilter_init():\n"
1414                     "\t\tInitializes a filtering pipe (filter_run). It should "
1415                         "return two file descriptors to use as a bidirectional "
1416                         "pipe: local and remote. 'local' is where packets from the "
1417                         "local side will be written to. After filtering, those packets "
1418                         "should be written to 'remote', where tun_connect will read "
1419                         "from, and it will forward them to the remote peer. "
1420                         "Packets from the remote peer will be written to 'remote', "
1421                         "where the filter is expected to read from, and eventually "
1422                         "forward them to the local side. If the file descriptors are "
1423                         "not nonblocking, they will be set to nonblocking. So it's "
1424                         "better to set them from the start like that.\n"
1425                     "\tfilter_run(local, remote):\n"
1426                     "\t\tIf filter_init is provided, it will be called repeatedly, "
1427                         "in a separate thread until the process is killed. It should "
1428                         "sleep at most for a second.\n"
1429                     "\tfilter_close(local, remote):\n"
1430                     "\t\tCalled then the process is killed, if filter_init was provided. "
1431                         "It should, among other things, close the file descriptors.\n"
1432                     "\n"
1433                     "Python modules are expected to return a tuple in filter_init, "
1434                     "either of file descriptors or file objects, while native ones "
1435                     "will receive two int*.\n"
1436                     "\n"
1437                     "Python modules can additionally contain a custom queue class "
1438                     "that will replace the FIFO used by default. The class should "
1439                     "be named 'queueclass' and contain an interface compatible with "
1440                     "collections.deque. That is, indexing (especiall for q[0]), "
1441                     "bool(q), popleft, appendleft, pop (right), append (right), "
1442                     "len(q) and clear.",
1443             "category": FC.CATEGORY_CHANNELS,
1444             "create_function": create_tunfilter,
1445             "box_attributes": [
1446                 "module", "args",
1447                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1448             ],
1449             "connector_types": ["->fd","udp","tcp"],
1450         }),
1451     CLASSQUEUEFILTER : dict({
1452             "help": "TUN classfull queue, uses a separate queue for each user-definable class.\n\n"
1453                     "It takes two arguments, both of which have sensible defaults:\n"
1454                     "\tsize: the base size of each class' queue\n"
1455                     "\tclasses: the class definitions, which follow the following syntax:\n"
1456                     '\t   <CLASSLIST> ::= <CLASS> ":" CLASSLIST\n'
1457                     '\t                |  <CLASS>\n'
1458                     '\t   <CLASS>     ::= <PROTOLIST> "*" <PRIORITYSPEC>\n'
1459                     '\t                |  <DFLTCLASS>\n'
1460                     '\t   <DFLTCLASS> ::= "*" <PRIORITYSPEC>\n'
1461                     '\t   <PROTOLIST> ::= <PROTO> "." <PROTOLIST>\n'
1462                     '\t                |  <PROTO>\n'
1463                     '\t   <PROTO>     ::= <NAME> | <NUMBER>\n'
1464                     '\t   <NAME>      ::= --see http://en.wikipedia.org/wiki/List_of_IP_protocol_numbers --\n'
1465                     '\t                   --only in lowercase, with special characters removed--\n'
1466                     '\t                   --or see below--\n'
1467                     '\t   <NUMBER>    ::= [0-9]+\n'
1468                     '\t   <PRIORITYSPEC> ::= <THOUGHPUT> [ "#" <SIZE> ] [ "p" <PRIORITY> ]\n'
1469                     '\t   <THOUGHPUT> ::= NUMBER -- default 1\n'
1470                     '\t   <PRIORITY>  ::= NUMBER -- default 0\n'
1471                     '\t   <SIZE>      ::= NUMBER -- default 1\n'
1472                     "\n"
1473                     "Size, thoughput and priority are all relative terms. "
1474                     "Sizes are multipliers for the size argument, thoughput "
1475                     "is applied relative to other classes and the same with "
1476                     "priority.",
1477             "category": FC.CATEGORY_CHANNELS,
1478             "create_function": create_classqueuefilter,
1479             "box_attributes": [
1480                 "args",
1481                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1482             ],
1483             "connector_types": ["->fd","udp","tcp"],
1484             "traces": ["dropped_stats"],
1485         }),
1486     TOSQUEUEFILTER : dict({
1487             "help": "TUN classfull queue that classifies according to the TOS (RFC 791) IP field.\n\n"
1488                     "It takes a size argument that specifies the size of each class. As TOS is a "
1489                     "subset of DiffServ, this queue half-implements DiffServ.",
1490             "category": FC.CATEGORY_CHANNELS,
1491             "create_function": create_tosqueuefilter,
1492             "box_attributes": [
1493                 "args",
1494                 "tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher",
1495             ],
1496             "connector_types": ["->fd","udp","tcp"],
1497         }),
1498
1499     APPLICATION: dict({
1500             "help": "Generic executable command line application",
1501             "category": FC.CATEGORY_APPLICATIONS,
1502             "create_function": create_application,
1503             "start_function": start_application,
1504             "status_function": status_application,
1505             "stop_function": stop_application,
1506             "configure_function": configure_application,
1507             "box_attributes": ["command", "sudo", "stdin",
1508                                "depends", "build-depends", "build", "install",
1509                                "sources", "rpm-fusion" ],
1510             "connector_types": ["node"],
1511             "traces": ["stdout", "stderr", "buildlog", "output"],
1512             "tags": [tags.APPLICATION],
1513         }),
1514     DEPENDENCY: dict({
1515             "help": "Requirement for package or application to be installed on some node",
1516             "category": FC.CATEGORY_APPLICATIONS,
1517             "create_function": create_dependency,
1518             "preconfigure_function": configure_dependency,
1519             "box_attributes": ["depends", "build-depends", "build", "install",
1520                                "sources", "rpm-fusion" ],
1521             "connector_types": ["node"],
1522             "traces": ["buildlog"],
1523         }),
1524     NEPIDEPENDENCY: dict({
1525             "help": "Requirement for NEPI inside NEPI - required to run testbed instances inside a node",
1526             "category": FC.CATEGORY_APPLICATIONS,
1527             "create_function": create_nepi_dependency,
1528             "preconfigure_function": configure_dependency,
1529             "box_attributes": [],
1530             "connector_types": ["node"],
1531             "traces": ["buildlog"],
1532         }),
1533     NS3DEPENDENCY: dict({
1534             "help": "Requirement for NS3 inside NEPI - required to run NS3 testbed instances inside a node. It also needs NepiDependency.",
1535             "category": FC.CATEGORY_APPLICATIONS,
1536             "create_function": create_ns3_dependency,
1537             "preconfigure_function": configure_dependency,
1538             "box_attributes": [ ],
1539             "connector_types": ["node"],
1540             "traces": ["buildlog"],
1541         }),
1542     MULTICASTFORWARDER: dict({
1543             "help": "This application installs a userspace packet forwarder "
1544                     "that, when connected to a node, filters all packets "
1545                     "flowing through multicast-capable virtual interfaces "
1546                     "and applies custom-specified routing policies.",
1547             "category": FC.CATEGORY_APPLICATIONS,
1548             "create_function": create_multicast_forwarder,
1549             "preconfigure_function": configure_forwarder,
1550             "start_function": start_application,
1551             "status_function": status_application,
1552             "stop_function": stop_application,
1553             "box_attributes": [ ],
1554             "connector_types": ["node","router"],
1555             "traces": ["buildlog","stderr"],
1556         }),
1557     MULTICASTANNOUNCER: dict({
1558             "help": "This application installs a userspace daemon that "
1559                     "monitors multicast membership and announces it on all "
1560                     "multicast-capable interfaces.\n"
1561                     "This does not usually happen automatically on PlanetLab slivers.",
1562             "category": FC.CATEGORY_APPLICATIONS,
1563             "create_function": create_multicast_announcer,
1564             "preconfigure_function": configure_announcer,
1565             "start_function": start_application,
1566             "status_function": status_application,
1567             "stop_function": stop_application,
1568             "box_attributes": [ ],
1569             "connector_types": ["node"],
1570             "traces": ["buildlog","stderr"],
1571         }),
1572     MULTICASTROUTER: dict({
1573             "help": "This application installs a userspace daemon that "
1574                     "monitors multicast membership and announces it on all "
1575                     "multicast-capable interfaces.\n"
1576                     "This does not usually happen automatically on PlanetLab slivers.",
1577             "category": FC.CATEGORY_APPLICATIONS,
1578             "create_function": create_multicast_router,
1579             "preconfigure_function": configure_router,
1580             "start_function": start_application,
1581             "status_function": status_application,
1582             "stop_function": stop_application,
1583             "box_attributes": ["routing_algorithm"],
1584             "connector_types": ["fwd"],
1585             "traces": ["buildlog","stdout","stderr"],
1586         }),
1587     INTERNET: dict({
1588             "help": "Internet routing",
1589             "category": FC.CATEGORY_CHANNELS,
1590             "create_function": create_internet,
1591             "connector_types": ["devs"],
1592             "tags": [tags.INTERNET],
1593         }),
1594     NETPIPE: dict({
1595             "help": "Link emulation",
1596             "category": FC.CATEGORY_CHANNELS,
1597             "create_function": create_netpipe,
1598             "configure_function": configure_netpipe,
1599             "box_attributes": ["netpipe_mode",
1600                                "addr_list", "port_list",
1601                                "bw_in","plr_in","delay_in",
1602                                "bw_out","plr_out","delay_out"],
1603             "connector_types": ["node"],
1604             "traces": ["netpipe_stats"],
1605         }),
1606 })
1607
1608 testbed_attributes = dict({
1609         "slice": dict({
1610             "name": "slice",
1611             "help": "The name of the PlanetLab slice to use",
1612             "type": Attribute.STRING,
1613             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1614             "validation_function": validation.is_string
1615         }),
1616         "auth_user": dict({
1617             "name": "authUser",
1618             "help": "The name of the PlanetLab user to use for API calls - it must have at least a User role.",
1619             "type": Attribute.STRING,
1620             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1621             "validation_function": validation.is_string
1622         }),
1623         "auth_pass": dict({
1624             "name": "authPass",
1625             "help": "The PlanetLab user's password.",
1626             "type": Attribute.STRING,
1627             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1628             "validation_function": validation.is_string
1629         }),
1630         "plc_host": dict({
1631             "name": "plcHost",
1632             "help": "The PlanetLab PLC API host",
1633             "type": Attribute.STRING,
1634             "value": "www.planet-lab.eu",
1635             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1636             "validation_function": validation.is_string
1637         }),
1638         "plc_url": dict({
1639             "name": "plcUrl",
1640             "help": "The PlanetLab PLC API url pattern - %(hostname)s is replaced by plcHost.",
1641             "type": Attribute.STRING,
1642             "value": "https://%(hostname)s:443/PLCAPI/",
1643             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1644             "validation_function": validation.is_string
1645         }),
1646         "p2p_deployment": dict({
1647             "name": "p2pDeployment",
1648             "help": "Enable peer-to-peer deployment of applications and dependencies. "
1649                     "When enabled, dependency packages and applications are "
1650                     "deployed in a P2P fashion, picking a single node to do "
1651                     "the building or repo download, while all the others "
1652                     "cooperatively exchange resulting binaries or rpms. "
1653                     "When deploying to many nodes, this is a far more efficient "
1654                     "use of resources. It does require re-encrypting and distributing "
1655                     "the slice's private key. Though it is implemented in a secure "
1656                     "fashion, if they key's sole purpose is not PlanetLab, then this "
1657                     "feature should be disabled.",
1658             "type": Attribute.BOOL,
1659             "value": True,
1660             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1661             "validation_function": validation.is_bool
1662         }),
1663         "slice_ssh_key": dict({
1664             "name": "sliceSSHKey",
1665             "help": "The controller-local path to the slice user's ssh private key. "
1666                     "It is the user's responsability to deploy this file where the controller "
1667                     "will run, it won't be done automatically because it's sensitive information. "
1668                     "It is recommended that a NEPI-specific user be created for this purpose and "
1669                     "this purpose alone.",
1670             "type": Attribute.STRING,
1671             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable | Attribute.NoDefaultValue,
1672             "validation_function": validation.is_string
1673         }),
1674         "pl_log_level": dict({      
1675             "name": "plLogLevel",
1676             "help": "Verbosity of logging of planetlab events.",
1677             "value": "ERROR",
1678             "type": Attribute.ENUM, 
1679             "allowed": ["DEBUG",
1680                         "INFO",
1681                         "WARNING",
1682                         "ERROR",
1683                         "CRITICAL"],
1684             "validation_function": validation.is_enum,
1685         }),
1686         "tap_port_base":  dict({
1687             "name": "tapPortBase", 
1688             "help": "Base port to use when connecting TUN/TAPs. Effective port will be BASE + GUID.",
1689             "type": Attribute.INTEGER,
1690             "value": 15000,
1691             "range": (2000,30000),
1692             "validation_function": validation.is_integer_range(2000,30000)
1693         }),
1694         "dedicated_slice": dict({
1695             "name": "dedicatedSlice",
1696             "help": "Set to True if the slice will be dedicated to this experiment. "
1697                     "NEPI will perform node and slice cleanup, making sure slices are "
1698                     "in a clean, repeatable state before running the experiment.",
1699             "type": Attribute.BOOL,
1700             "value": False,
1701             "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
1702             "validation_function": validation.is_bool
1703         }),
1704     })
1705
1706 supported_recovery_policies = [
1707         DC.POLICY_FAIL,
1708         DC.POLICY_RESTART,
1709         DC.POLICY_RECOVER,
1710     ]
1711
1712 class MetadataInfo(metadata.MetadataInfo):
1713     @property
1714     def connector_types(self):
1715         return connector_types
1716
1717     @property
1718     def connections(self):
1719         return connections
1720
1721     @property
1722     def attributes(self):
1723         return attributes
1724
1725     @property
1726     def traces(self):
1727         return traces
1728
1729     @property
1730     def create_order(self):
1731         return create_order
1732
1733     @property
1734     def configure_order(self):
1735         return configure_order
1736
1737     @property
1738     def prestart_order(self):
1739         return start_order
1740
1741     @property
1742     def start_order(self):
1743         return start_order
1744
1745     @property
1746     def factories_info(self):
1747         return factories_info
1748
1749     @property
1750     def testbed_attributes(self):
1751         return testbed_attributes
1752
1753     @property
1754     def testbed_id(self):
1755         return TESTBED_ID
1756
1757     @property
1758     def testbed_version(self):
1759         return TESTBED_VERSION
1760
1761     @property
1762     def supported_recovery_policies(self):
1763         return supported_recovery_policies
1764
1765