Adding flag NoDefaultValue to interface up attribute for NETNS and PlanetLab
[nepi.git] / src / nepi / testbeds / netns / metadata.py
1 # -*- coding: utf-8 -*-
2
3 from constants import TESTBED_ID, TESTBED_VERSION
4 from nepi.core import metadata
5 from nepi.core.attributes import Attribute
6 from nepi.util import tags, validation
7 from nepi.util.constants import ApplicationStatus as AS, \
8         FactoryCategories as FC, DeploymentConfiguration as DC
9
10 from nepi.util.tunchannel_impl import \
11     preconfigure_tunchannel, postconfigure_tunchannel, \
12     prestart_tunchannel, create_tunchannel, \
13     crossconnect_tunchannel_peer_init, \
14     crossconnect_tunchannel_peer_compl
15
16 import functools
17
18 # Factories
19 NODE = "Node"
20 P2PIFACE = "P2PNodeInterface"
21 TAPIFACE = "TapNodeInterface"
22 TUNIFACE = "TunNodeInterface"
23 NODEIFACE = "NodeInterface"
24 SWITCH = "Switch"
25 APPLICATION = "Application"
26 TUNCHANNEL = "TunChannel"
27
28 NS3_TESTBED_ID = "ns3"
29 FDNETDEV = "ns3::FdNetDevice"
30
31 def _follow_trace(testbed_instance, guid, trace_id, filename):
32     filepath = testbed_instance.trace_filepath(guid, trace_id, filename)
33     trace = open(filepath, "wb")
34     testbed_instance.follow_trace(guid, trace_id, trace, filename)
35     return trace
36
37 ### Connection functions ####
38
39 def connect_switch(testbed_instance, switch_guid, interface_guid):
40     switch = testbed_instance._elements[switch_guid]
41     interface = testbed_instance._elements[interface_guid]
42     switch.connect(interface)
43    
44 def connect_fd(testbed_instance, tap_guid, cross_data):
45     import passfd
46     import socket
47     tap = testbed_instance._elements[tap_guid]
48     address = cross_data["tun_addr"]
49     sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
50     sock.connect(address)
51     passfd.sendfd(sock, tap.fd, '0')
52     # TODO: after succesful transfer, the tap device should close the fd
53
54 def connect_tunchannel_tun(testbed_instance, chan_guid, tap_guid):
55     connect_tunchannel_tap(testbed_instance, chan_guid, tap_guid, ethernet_mode=False)
56
57 def connect_tunchannel_tap(testbed_instance, chan_guid, tap_guid, ethernet_mode=True):
58     tap = testbed_instance._elements[tap_guid]
59     chan = testbed_instance._elements[chan_guid]
60
61     # Create a file object for the tap's interface device 
62     # and send it to the channel. It should comply with all the
63     # requirements for the channel's tun_socket.
64     import os
65     chan.tun_socket = os.fdopen(tap.fd)
66     
67     # Set the channel to ethernet mode (it's a tap)
68     chan.ethernet_mode = ethernet_mode
69     
70     # Check to see if the device uses PI headers
71     # It's normally so
72     with_pi = True
73     try:
74         import fcntl
75         import struct
76         TUNGETIFF = 0x800454d2
77         IFF_NO_PI = 0x00001000
78         struct_ifreq = "x"*16+"H"+"x"*22
79         flags = struct.unpack(struct_ifreq,
80             fcntl.ioctl(tap.fd, TUNGETIFF, struct.pack(struct_ifreq,0)) )[0]
81         with_pi = (0 == (flags & IFF_NO_PI))
82     except:
83         # maybe the kernel doesn't support the IOCTL,
84         # in which case, we assume it uses PI headers (as is usual)
85         pass
86     chan.with_pi = with_pi
87
88 ### Trace functions ###
89
90 def nodepcap_trace(testbed_instance, guid, trace_id):
91     node = testbed_instance._elements[guid]
92     parameters = testbed_instance._get_parameters(guid)
93     filename = "%d-pcap.stdout" % guid
94     stdout = _follow_trace(testbed_instance, guid, "pcap_stdout", filename)
95     filename = "%d-pcap.stderr" % guid
96     stderr = _follow_trace(testbed_instance, guid, "pcap_stderr", filename)
97     filename = "%d-node.pcap" % guid
98     filepath = testbed_instance.trace_filepath(guid, trace_id, filename)
99     command = "tcpdump -i 'any' -w %s" % filepath
100     user = "root"
101     trace = node.Popen(command, shell = True, stdout = stdout, 
102             stderr = stderr, user = user)
103     testbed_instance.follow_trace(guid, trace_id, trace, filename)
104
105 trace_functions = dict({
106     "pcap": nodepcap_trace,
107     })
108
109 ### Creation functions ###
110
111 def create_node(testbed_instance, guid):
112     parameters = testbed_instance._get_parameters(guid)
113     forward_X11 = False
114     if "forward_X11" in parameters:
115         forward_X11 = parameters["forward_X11"]
116         del parameters["forward_X11"]
117     element = testbed_instance.netns.Node(forward_X11 = forward_X11)
118     testbed_instance.elements[guid] = element
119
120 def create_p2piface(testbed_instance, guid):
121     if guid in testbed_instance.elements:
122         # The interface pair was already instantiated
123         return
124     # search for the node asociated with the p2piface
125     node1_guid = testbed_instance.get_connected(guid, "node", "devs")
126     if len(node1_guid) == 0:
127         raise RuntimeError("Can't instantiate interface %d outside netns \
128                 node" % guid)
129     node1 = testbed_instance.elements[node1_guid[0]]
130     # search for the pair p2piface
131     p2p_guid = testbed_instance.get_connected(guid, "p2p","p2p")
132     if len(p2p_guid) == 0:
133         raise RuntimeError("Can't instantiate p2p interface %d. \
134                 Missing interface pair" % guid)
135     guid2 = p2p_guid[0]
136     node2_guid = testbed_instance.get_connected(guid2, "node", "devs")
137     if len(node2_guid) == 0:
138         raise RuntimeError("Can't instantiate interface %d outside netns \
139                 node" % guid2)
140     node2 = testbed_instance.elements[node2_guid[0]]
141     element1, element2 = testbed_instance.netns.P2PInterface.create_pair(
142         node1, node2)
143     testbed_instance.elements[guid] = element1
144     testbed_instance.elements[guid2] = element2
145
146 def create_tapiface(testbed_instance, guid):
147     node_guid = testbed_instance.get_connected(guid, "node", "devs")
148     if len(node_guid) == 0:
149         raise RuntimeError("Can't instantiate interface %d outside netns \
150                 node" % guid)
151     node = testbed_instance.elements[node_guid[0]]
152     element = node.add_tap()
153     testbed_instance.elements[guid] = element
154
155 def create_tuniface(testbed_instance, guid):
156     node_guid = testbed_instance.get_connected(guid, "node", "devs")
157     if len(node_guid) == 0:
158         raise RuntimeError("Can't instantiate interface %d outside netns \
159                 node" % guid)
160     node = testbed_instance.elements[node_guid[0]]
161     element = node.add_tun()
162     testbed_instance.elements[guid] = element
163
164 def create_nodeiface(testbed_instance, guid):
165     node_guid = testbed_instance.get_connected(guid, "node", "devs")
166     if len(node_guid) == 0:
167         raise RuntimeError("Can't instantiate interface %d outside netns \
168                 node" % guid)
169     node = testbed_instance.elements[node_guid[0]]
170     element = node.add_if()
171     testbed_instance.elements[guid] = element
172
173 def create_switch(testbed_instance, guid):
174     element = testbed_instance.netns.Switch()
175     testbed_instance.elements[guid] = element
176
177 def create_application(testbed_instance, guid):
178     testbed_instance.elements[guid] = None # Delayed construction 
179
180 ### Start/Stop functions ###
181
182 def start_application(testbed_instance, guid):
183     parameters = testbed_instance._get_parameters(guid)
184     traces = testbed_instance._get_traces(guid)
185     command = parameters["command"]
186     user = None
187     if "user" in parameters:
188         user = parameters["user"]
189     stdout = stderr = None
190     if "stdout" in traces:
191         filename = "%d-stdout.trace" % guid
192         stdout = _follow_trace(testbed_instance, guid, "stdout", filename)
193     if "stderr" in traces:
194         filename = "%d-stderr.trace" % guid
195         stderr = _follow_trace(testbed_instance, guid, "stderr", filename)
196     node_guid = testbed_instance.get_connected(guid, "node", "apps")
197     if len(node_guid) == 0:
198         raise RuntimeError("Can't instantiate interface %d outside netns \
199                 node" % guid)
200     node = testbed_instance.elements[node_guid[0]]
201     element  = node.Popen(command, shell = True, stdout = stdout, 
202             stderr = stderr, user = user)
203     testbed_instance.elements[guid] = element
204
205 def stop_application(testbed_instance, guid):
206     #app = testbed_instance.elements[guid]
207     #app.signal()
208     pass
209
210 ### Status functions ###
211
212 def status_application(testbed_instance, guid):
213     if guid not in testbed_instance.elements.keys():
214         return AS.STATUS_NOT_STARTED
215     app = testbed_instance.elements[guid]
216     if app.poll() == None:
217         return AS.STATUS_RUNNING
218     return AS.STATUS_FINISHED
219
220 ### Configure functions ###
221
222 def configure_traces(testbed_instance, guid):
223     traces = testbed_instance._get_traces(guid)
224     for trace_id in traces:
225         if trace_id not in trace_functions:
226             continue
227         trace_func = trace_functions[trace_id]
228         trace_func(testbed_instance, guid, trace_id)
229
230 def configure_device(testbed_instance, guid):
231     configure_traces(testbed_instance, guid)
232     element = testbed_instance._elements[guid]
233     if not guid in testbed_instance._add_address:
234         return
235     addresses = testbed_instance._add_address[guid]
236     for address in addresses:
237         (address, netprefix, broadcast) = address
238         # TODO: Decide if we should add a ipv4 or ipv6 address
239         element.add_v4_address(address, netprefix)
240
241 def configure_node(testbed_instance, guid):
242     configure_traces(testbed_instance, guid)
243     element = testbed_instance._elements[guid]
244     if not guid in testbed_instance._add_route:
245         return
246     routes = testbed_instance._add_route[guid]
247     for route in routes:
248         (destination, netprefix, nexthop, metric) = route
249         element.add_route(prefix = destination, prefix_len = netprefix,
250             nexthop = nexthop, metric = metric)
251
252 ### Factory information ###
253
254 connector_types = dict({
255     "apps": dict({
256                 "help": "Connector from node to applications", 
257                 "name": "apps",
258                 "max": -1, 
259                 "min": 0
260             }),
261     "devs": dict({
262                 "help": "Connector from node to network interfaces", 
263                 "name": "devs",
264                 "max": -1, 
265                 "min": 0
266             }),
267     "node": dict({
268                 "help": "Connector to a Node", 
269                 "name": "node",
270                 "max": 1, 
271                 "min": 1
272             }),
273     "p2p": dict({
274                 "help": "Connector to a P2PInterface", 
275                 "name": "p2p",
276                 "max": 1, 
277                 "min": 0
278             }),
279     "->fd": dict({
280                 "help": "File descriptor receptor for devices with file descriptors",
281                 "name": "->fd",
282                 "max": 1,
283                 "min": 0
284             }),
285     "fd->": dict({
286                 "help": "File descriptor provider for devices with file descriptors",
287                 "name": "fd->",
288                 "max": 1,
289                 "min": 0
290             }),
291     "switch": dict({
292                 "help": "Connector to a switch", 
293                 "name": "switch",
294                 "max": 1, 
295                 "min": 0
296             }),
297     "tcp": dict({
298                 "help": "ip-ip tunneling over TCP link", 
299                 "name": "tcp",
300                 "max": 1, 
301                 "min": 0
302             }),
303     "udp": dict({
304                 "help": "ip-ip tunneling over UDP datagrams", 
305                 "name": "udp",
306                 "max": 1, 
307                 "min": 0
308             }),
309    })
310
311 connections = [
312     dict({
313         "from": (TESTBED_ID, NODE, "devs"),
314         "to":   (TESTBED_ID, P2PIFACE, "node"),
315         "can_cross": False
316     }),
317     dict({
318         "from": (TESTBED_ID, NODE, "devs"),
319         "to":   (TESTBED_ID, TAPIFACE, "node"),
320         "can_cross": False
321     }),
322     dict({
323         "from": (TESTBED_ID, NODE, "devs"),
324         "to":   (TESTBED_ID, TUNIFACE, "node"),
325         "can_cross": False
326     }),
327     dict({
328         "from": (TESTBED_ID, NODE, "devs"),
329         "to":   (TESTBED_ID, NODEIFACE, "node"),
330         "can_cross": False
331     }),
332     dict({
333         "from": (TESTBED_ID, P2PIFACE, "p2p"),
334         "to":   (TESTBED_ID, P2PIFACE, "p2p"),
335         "can_cross": False
336     }),
337     dict({
338         "from": (TESTBED_ID, TAPIFACE, "fd->"),
339         "to":   (None, None, "->fd"),
340         "compl_code": connect_fd,
341         "can_cross": True
342     }),
343     dict({
344         "from": (TESTBED_ID, TUNIFACE, "fd->"),
345         "to":   (None, None, "->fd"),
346         "compl_code": connect_fd,
347         "can_cross": True
348     }),
349      dict({
350         "from": (TESTBED_ID, SWITCH, "devs"),
351         "to":   (TESTBED_ID, NODEIFACE, "switch"),
352         "init_code": connect_switch,
353         "can_cross": False
354     }),
355     dict({
356         "from": (TESTBED_ID, NODE, "apps"),
357         "to":   (TESTBED_ID, APPLICATION, "node"),
358         "can_cross": False
359     }),
360     dict({
361         "from": (TESTBED_ID, TUNCHANNEL, "->fd" ),
362         "to":   (TESTBED_ID, TAPIFACE, "fd->" ),
363         "init_code": connect_tunchannel_tap,
364         "can_cross": False
365     }),
366     dict({
367         "from": (TESTBED_ID, TUNCHANNEL, "->fd" ),
368         "to":   (TESTBED_ID, TUNIFACE, "fd->" ),
369         "init_code": connect_tunchannel_tun,
370         "can_cross": False
371     }),
372     dict({
373         "from": (TESTBED_ID, TUNCHANNEL, "tcp"),
374         "to":   (None, None, "tcp"),
375         "init_code": functools.partial(crossconnect_tunchannel_peer_init,"tcp"),
376         "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"tcp"),
377         "can_cross": True
378     }),
379     dict({
380         "from": (TESTBED_ID, TUNCHANNEL, "udp"),
381         "to":   (None, None, "udp"),
382         "init_code": functools.partial(crossconnect_tunchannel_peer_init,"udp"),
383         "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"udp"),
384         "can_cross": True
385     }),
386 ]
387
388 attributes = dict({
389     "forward_X11": dict({      
390                 "name": "forward_X11",
391                 "help": "Forward x11 from main namespace to the node",
392                 "type": Attribute.BOOL, 
393                 "value": False,
394                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
395                 "validation_function": validation.is_bool
396             }),
397     "lladdr": dict({      
398                 "name": "lladdr", 
399                 "help": "Mac address", 
400                 "type": Attribute.STRING,
401                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
402                 "validation_function": validation.is_mac_address
403             }),
404     "up": dict({
405                 "name": "up",
406                 "help": "Link up",
407                 "type": Attribute.BOOL,
408                 "value": True,
409                 "flags": Attribute.NoDefaultValue, 
410                 "validation_function": validation.is_bool
411             }),
412     "device_name": dict({
413                 "name": "name",
414                 "help": "Device name",
415                 "type": Attribute.STRING,
416                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
417                 "validation_function": validation.is_string
418             }),
419     "mtu":  dict({
420                 "name": "mtu", 
421                 "help": "Maximum transmition unit for device",
422                 "type": Attribute.INTEGER,
423                 "validation_function": validation.is_integer
424             }),
425     "broadcast": dict({ 
426                 "name": "broadcast",
427                 "help": "Broadcast address",
428                 "type": Attribute.STRING,
429                 "validation_function": validation.is_string # TODO: should be is address!
430             }),
431     "multicast": dict({      
432                 "name": "multicast",
433                 "help": "Multicast enabled",
434                 "type": Attribute.BOOL,
435                 "value": False,
436                 "validation_function": validation.is_bool
437             }),
438     "arp": dict({
439                 "name": "arp",
440                 "help": "ARP enabled",
441                 "type": Attribute.BOOL,
442                 "value": False,
443                 "validation_function": validation.is_bool
444             }),
445     "command": dict({
446                 "name": "command",
447                 "help": "Command line string",
448                 "type": Attribute.STRING,
449                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
450                 "validation_function": validation.is_string
451             }),
452     "user": dict({
453                 "name": "user",
454                 "help": "System user",
455                 "type": Attribute.STRING,
456                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
457                 "validation_function": validation.is_string
458             }),
459     "stdin": dict({
460                 "name": "stdin",
461                 "help": "Standard input",
462                 "type": Attribute.STRING,
463                 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
464                 "validation_function": validation.is_string
465             }),
466     })
467
468 traces = dict({
469     "stdout": dict({
470                 "name": "stdout",
471                 "help": "Standard output stream"
472               }),
473     "stderr": dict({
474                 "name": "stderr",
475                 "help": "Application standard error",
476         }),
477     "node_pcap": dict({
478                 "name": "pcap",
479                 "help": "tcpdump at all node interfaces",
480         }) 
481     })
482
483 create_order = [ NODE, P2PIFACE, NODEIFACE, TAPIFACE, 
484         TUNIFACE, TUNCHANNEL, SWITCH,
485         APPLICATION ]
486
487 configure_order = [ P2PIFACE, NODEIFACE, TAPIFACE, 
488         TUNIFACE, TUNCHANNEL, SWITCH, 
489         NODE, APPLICATION ]
490
491 factories_info = dict({
492     NODE: dict({
493             "help": "Emulated Node with virtualized network stack",
494             "category": FC.CATEGORY_NODES,
495             "create_function": create_node,
496             "configure_function": configure_node,
497             "box_attributes": ["forward_X11"],
498             "connector_types": ["devs", "apps"],
499             "traces": ["node_pcap"],
500             "tags": [tags.NODE, tags.ALLOW_ROUTES],
501        }),
502     P2PIFACE: dict({
503             "help": "Point to point network interface",
504             "category": FC.CATEGORY_DEVICES,
505             "create_function": create_p2piface,
506             "configure_function": configure_device,
507             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
508                 "multicast", "broadcast", "arp"],
509             "connector_types": ["node", "p2p"],
510             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
511        }),
512     TAPIFACE: dict({
513             "help": "Tap device network interface",
514             "category": FC.CATEGORY_DEVICES,
515             "create_function": create_tapiface,
516             "configure_function": configure_device,
517             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
518                 "multicast", "broadcast", "arp"],
519             "connector_types": ["node", "fd->"],
520             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
521         }),
522     TUNIFACE: dict({
523             "help": "Tun device network interface",
524             "category": FC.CATEGORY_DEVICES,
525             "create_function": create_tuniface,
526             "configure_function": configure_device,
527             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
528                 "multicast", "broadcast", "arp"],
529             "connector_types": ["node", "fd->"],
530             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
531         }),
532     NODEIFACE: dict({
533             "help": "Node network interface",
534             "category": FC.CATEGORY_DEVICES,
535             "create_function": create_nodeiface,
536             "configure_function": configure_device,
537             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
538                 "multicast", "broadcast", "arp"],
539             "connector_types": ["node", "switch"],
540             "tags": [tags.INTERFACE, tags.ALLOW_ADDRESSES],
541         }),
542     SWITCH: dict({
543             "display_name": "Switch",
544             "help": "Switch interface",
545             "category": FC.CATEGORY_DEVICES,
546             "create_function": create_switch,
547             "box_attributes": ["up", "device_name", "mtu", "multicast"],
548              #TODO: Add attribute ("Stp", help, type, value, range, allowed, readonly, validation_function),
549              #TODO: Add attribute ("ForwarddDelay", help, type, value, range, allowed, readonly, validation_function),
550              #TODO: Add attribute ("HelloTime", help, type, value, range, allowed, readonly, validation_function),
551              #TODO: Add attribute ("AgeingTime", help, type, value, range, allowed, readonly, validation_function),
552              #TODO: Add attribute ("MaxAge", help, type, value, range, allowed, readonly, validation_function)
553             "connector_types": ["devs"],
554             "tags": [tags.SWITCH],
555         }),
556     APPLICATION: dict({
557             "help": "Generic executable command line application",
558             "category": FC.CATEGORY_APPLICATIONS,
559             "create_function": create_application,
560             "start_function": start_application,
561             "stop_function": stop_application,
562             "status_function": status_application,
563             "box_attributes": ["command", "user"],
564             "connector_types": ["node"],
565             "traces": ["stdout", "stderr"],
566             "tags": [tags.APPLICATION],
567         }),
568      TUNCHANNEL : dict({
569             "category": FC.CATEGORY_TUNNELS,
570             "create_function": create_tunchannel,
571             "preconfigure_function": preconfigure_tunchannel,
572             "configure_function": postconfigure_tunchannel,
573             "prestart_function": prestart_tunchannel,
574             "help": "Channel to forward "+TAPIFACE+" data to "
575                 "other TAP interfaces supporting the NEPI tunneling protocol.",
576             "connector_types": ["->fd", "udp", "tcp"],
577             "allow_addresses": False,
578             "box_attributes": ["tun_proto", "tun_addr", "tun_port", "tun_key", "tun_cipher"],
579             "tags": [tags.TUNNEL],
580     }),
581 })
582
583 testbed_attributes = dict({
584         "enable_debug": dict({
585                 "name": "enableDebug",
586                 "help": "Enable netns debug output",
587                 "type": Attribute.BOOL,
588                 "value": False,
589                 "validation_function": validation.is_bool
590             }),
591     })
592
593 supported_recovery_policies = [
594         DC.POLICY_FAIL,
595     ]
596
597 class MetadataInfo(metadata.MetadataInfo):
598     @property
599     def connector_types(self):
600         return connector_types
601
602     @property
603     def connections(self):
604         return connections
605
606     @property
607     def attributes(self):
608         return attributes
609
610     @property
611     def traces(self):
612         return traces
613
614     @property
615     def create_order(self):
616         return create_order
617
618     @property
619     def configure_order(self):
620         return configure_order
621
622     @property
623     def factories_info(self):
624         return factories_info
625
626     @property
627     def testbed_attributes(self):
628         return testbed_attributes
629
630     @property
631     def testbed_id(self):
632         return TESTBED_ID
633
634     @property
635     def testbed_version(self):
636         return TESTBED_VERSION
637     
638     @property
639     def supported_recover_policies(self):
640         return supported_recovery_policies
641