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