routing support added for ns3 testbed
[nepi.git] / src / nepi / testbeds / netns / metadata_v01.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from constants import TESTBED_ID
5 from nepi.core import metadata
6 from nepi.core.attributes import Attribute
7 from nepi.util import validation
8 from nepi.util.constants import STATUS_NOT_STARTED, STATUS_RUNNING, \
9         STATUS_FINISHED
10
11 NODE = "Node"
12 P2PIFACE = "P2PNodeInterface"
13 TAPIFACE = "TapNodeInterface"
14 NODEIFACE = "NodeInterface"
15 SWITCH = "Switch"
16 APPLICATION = "Application"
17
18 NS3_TESTBED_ID = "ns3"
19 FDNETDEV = "ns3::FileDescriptorNetDevice"
20
21 ### Connection functions ####
22
23 def connect_switch(testbed_instance, switch, interface):
24     switch.connect(interface)
25    
26 #XXX: This connection function cannot be use to transfer a file descriptor
27 # to a remote tap device
28 def connect_fd_local(testbed_instance, tap, fdnd):
29     import passfd
30     import socket
31     fd = tap.file_descriptor
32     address = fdnd.socket_address
33     sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
34     sock.connect(address)
35     passfd.sendfd(sock, fd, '0')
36     # TODO: after succesful transfer, the tap device should close the fd
37
38 ### Creation functions ###
39
40 def create_node(testbed_instance, guid):
41     parameters = testbed_instance._get_parameters(guid)
42     forward_X11 = False
43     if "forward_X11" in parameters:
44         forward_X11 = parameters["forward_X11"]
45         del parameters["forward_X11"]
46     element = testbed_instance.netns.Node(forward_X11 = forward_X11)
47     testbed_instance.elements[guid] = element
48
49 def create_p2piface(testbed_instance, guid):
50     if guid in testbed_instance.elements:
51         # The interface pair was already instantiated
52         return
53     # search for the node asociated with the p2piface
54     node1_guid = testbed_instance.get_connected(guid, "node", "devs")
55     if len(node1_guid) == 0:
56         raise RuntimeError("Can't instantiate interface %d outside netns \
57                 node" % guid)
58     node1 = testbed_instance.elements[node1_guid[0]]
59     # search for the pair p2piface
60     p2p_guid = testbed_instance.get_connected(guid, "p2p","p2p")
61     if len(p2p_guid) == 0:
62         raise RuntimeError("Can't instantiate p2p interface %d. \
63                 Missing interface pair" % guid)
64     guid2 = p2p_guid[0]
65     node2_guid = testbed_instance.get_connected(guid2, "node", "devs")
66     if len(node2_guid) == 0:
67         raise RuntimeError("Can't instantiate interface %d outside netns \
68                 node" % guid2)
69     node2 = testbed_instance.elements[node2_guid[0]]
70     element1, element2 = testbed_instance.netns.P2PInterface.create_pair(
71         node1, node2)
72     testbed_instance.elements[guid] = element1
73     testbed_instance.elements[guid2] = element2
74
75 def create_tapiface(testbed_instance, guid):
76     node_guid = testbed_instance.get_connected(guid, "node", "devs")
77     if len(node_guid) == 0:
78         raise RuntimeError("Can't instantiate interface %d outside netns \
79                 node" % guid)
80     node = testbed_instance.elements[node_guid[0]]
81     element = node.add_tap()
82     testbed_instance.elements[guid] = element
83
84 def create_nodeiface(testbed_instance, guid):
85     node_guid = testbed_instance.get_connected(guid, "node", "devs")
86     if len(node_guid) == 0:
87         raise RuntimeError("Can't instantiate interface %d outside netns \
88                 node" % guid)
89     node = testbed_instance.elements[node_guid[0]]
90     element = node.add_if()
91     testbed_instance.elements[guid] = element
92
93 def create_switch(testbed_instance, guid):
94     element = testbed_instance.netns.Switch()
95     testbed_instance.elements[guid] = element
96
97 def create_application(testbed_instance, guid):
98     testbed_instance.elements[guid] = None # Delayed construction 
99
100 ### Start/Stop functions ###
101
102 def start_application(testbed_instance, guid):
103     parameters = testbed_instance._get_parameters(guid)
104     traces = testbed_instance._get_traces(guid)
105     user = parameters["user"]
106     command = parameters["command"]
107     stdout = stderr = None
108     if "stdout" in traces:
109         filename = testbed_instance.trace_filename(guid, "stdout")
110         stdout = open(filename, "wb")
111         testbed_instance.follow_trace("stdout", stdout)
112     if "stderr" in traces:
113         filename = testbed_instance.trace_filename(guid, "stderr")
114         stderr = open(filename, "wb")
115         testbed_instance.follow_trace("stderr", stderr)
116
117     node_guid = testbed_instance.get_connected(guid, "node", "apps")
118     if len(node_guid) == 0:
119         raise RuntimeError("Can't instantiate interface %d outside netns \
120                 node" % guid)
121     node = testbed_instance.elements[node_guid[0]]
122     element  = node.Popen(command, shell = True, stdout = stdout, 
123             stderr = stderr, user = user)
124     testbed_instance.elements[guid] = element
125
126 ### Status functions ###
127
128 def status_application(testbed_instance, guid):
129     if guid not in testbed_instance.elements.keys():
130         return STATUS_NOT_STARTED
131     app = testbed_instance.elements[guid]
132     if app.poll() == None:
133         return STATUS_RUNNING
134     return STATUS_FINISHED
135
136 ### Configure functions ###
137
138 def configure_device(testbed_instance, guid):
139     element = testbed_instance._elements[guid]
140     if not guid in testbed_instance._add_address:
141         return
142     addresses = testbed_instance._add_address[guid]
143     for address in addresses:
144         (address, netprefix, broadcast) = address
145         # TODO: Decide if we should add a ipv4 or ipv6 address
146         element.add_v4_address(address, netprefix)
147
148 def configure_node(testbed_instance, guid):
149     element = testbed_instance._elements[guid]
150     if not guid in testbed_instance._add_route:
151         return
152     routes = testbed_instance._add_route[guid]
153     for route in routes:
154         (destination, netprefix, nexthop) = route
155         element.add_route(prefix = destination, prefix_len = netprefix,
156             nexthop = nexthop)
157
158 ### Factory information ###
159
160 connector_types = dict({
161     "apps": dict({
162                 "help": "Connector from node to applications", 
163                 "name": "apps",
164                 "max": -1, 
165                 "min": 0
166             }),
167     "devs": dict({
168                 "help": "Connector from node to network interfaces", 
169                 "name": "devs",
170                 "max": -1, 
171                 "min": 0
172             }),
173     "node": dict({
174                 "help": "Connector to a Node", 
175                 "name": "node",
176                 "max": 1, 
177                 "min": 1
178             }),
179     "p2p": dict({
180                 "help": "Connector to a P2PInterface", 
181                 "name": "p2p",
182                 "max": 1, 
183                 "min": 0
184             }),
185     "fd": dict({
186                 "help": "Connector to a network interface that can receive a file descriptor", 
187                 "name": "fd",
188                 "max": 1, 
189                 "min": 0
190             }),
191     "switch": dict({
192                 "help": "Connector to a switch", 
193                 "name": "switch",
194                 "max": 1, 
195                 "min": 0
196             })
197    })
198
199 connections = [
200     dict({
201         "from": (TESTBED_ID, NODE, "devs"),
202         "to":   (TESTBED_ID, P2PIFACE, "node"),
203         "code": None,
204         "can_cross": False
205     }),
206     dict({
207         "from": (TESTBED_ID, NODE, "devs"),
208         "to":   (TESTBED_ID, TAPIFACE, "node"),
209         "code": None,
210         "can_cross": False
211     }),
212     dict({
213         "from": (TESTBED_ID, NODE, "devs"),
214         "to":   (TESTBED_ID, NODEIFACE, "node"),
215         "code": None,
216         "can_cross": False
217     }),
218     dict({
219         "from": (TESTBED_ID, P2PIFACE, "p2p"),
220         "to":   (TESTBED_ID, P2PIFACE, "p2p"),
221         "code": None,
222         "can_cross": False
223     }),
224     dict({
225         "from": (TESTBED_ID, TAPIFACE, "fd"),
226         "to":   (NS3_TESTBED_ID, FDNETDEV, "fd"),
227         "code": connect_fd_local,
228         "can_cross": True
229     }),
230      dict({
231         "from": (TESTBED_ID, SWITCH, "devs"),
232         "to":   (TESTBED_ID, NODEIFACE, "switch"),
233         "code": connect_switch,
234         "can_cross": False
235     }),
236     dict({
237         "from": (TESTBED_ID, NODE, "apps"),
238         "to":   (TESTBED_ID, APPLICATION, "node"),
239         "code": None,
240         "can_cross": False
241     })
242 ]
243
244 attributes = dict({
245     "forward_X11": dict({      
246                 "name": "forward_X11",
247                 "help": "Forward x11 from main namespace to the node",
248                 "type": Attribute.BOOL, 
249                 "value": False,
250                 "flags": Attribute.DesignOnly,
251                 "validation_function": validation.is_bool
252             }),
253     "lladdr": dict({      
254                 "name": "lladdr", 
255                 "help": "Mac address", 
256                 "type": Attribute.STRING,
257                 "flags": Attribute.DesignOnly,
258                 "validation_function": validation.is_mac_address
259             }),
260     "up": dict({
261                 "name": "up",
262                 "help": "Link up",
263                 "type": Attribute.BOOL,
264                 "value": False,
265                 "validation_function": validation.is_bool
266             }),
267     "device_name": dict({
268                 "name": "name",
269                 "help": "Device name",
270                 "type": Attribute.STRING,
271                 "flags": Attribute.DesignOnly,
272                 "validation_function": validation.is_string
273             }),
274     "mtu":  dict({
275                 "name": "mtu", 
276                 "help": "Maximum transmition unit for device",
277                 "type": Attribute.INTEGER,
278                 "validation_function": validation.is_integer
279             }),
280     "broadcast": dict({ 
281                 "name": "broadcast",
282                 "help": "Broadcast address",
283                 "type": Attribute.STRING,
284                 "validation_function": validation.is_string # TODO: should be is address!
285             }),
286     "multicast": dict({      
287                 "name": "multicast",
288                 "help": "Multicast enabled",
289                 "type": Attribute.BOOL,
290                 "value": False,
291                 "validation_function": validation.is_bool
292             }),
293     "arp": dict({
294                 "name": "arp",
295                 "help": "ARP enabled",
296                 "type": Attribute.BOOL,
297                 "value": False,
298                 "validation_function": validation.is_bool
299             }),
300     "command": dict({
301                 "name": "command",
302                 "help": "Command line string",
303                 "type": Attribute.STRING,
304                 "flags": Attribute.DesignOnly,
305                 "validation_function": validation.is_string
306             }),
307     "user": dict({
308                 "name": "user",
309                 "help": "System user",
310                 "type": Attribute.STRING,
311                 "flags": Attribute.DesignOnly,
312                 "validation_function": validation.is_string
313             }),
314     "stdin": dict({
315                 "name": "stdin",
316                 "help": "Standard input",
317                 "type": Attribute.STRING,
318                 "flags": Attribute.DesignOnly,
319                 "validation_function": validation.is_string
320             }),
321     })
322
323 traces = dict({
324     "stdout": dict({
325                 "name": "stdout",
326                 "help": "Standard output stream"
327               }),
328     "stderr": dict({
329                 "name": "stderr",
330                 "help": "Application standard error",
331         }) 
332     })
333
334 create_order = [ NODE, P2PIFACE, NODEIFACE, TAPIFACE, SWITCH,
335         APPLICATION ]
336
337 configure_order = [ P2PIFACE, NODEIFACE, TAPIFACE, SWITCH, NODE,
338         APPLICATION ]
339
340 factories_info = dict({
341     NODE: dict({
342             "allow_routes": True,
343             "help": "Emulated Node with virtualized network stack",
344             "category": "topology",
345             "create_function": create_node,
346             "configure_function": configure_node,
347             "box_attributes": ["forward_X11"],
348             "connector_types": ["devs", "apps"]
349        }),
350     P2PIFACE: dict({
351             "allow_addresses": True,
352             "help": "Point to point network interface",
353             "category": "devices",
354             "create_function": create_p2piface,
355             "configure_function": configure_device,
356             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
357                 "multicast", "broadcast", "arp"],
358             "connector_types": ["node", "p2p"]
359        }),
360     TAPIFACE: dict({
361             "allow_addresses": True,
362             "help": "Tap device network interface",
363             "category": "devices",
364             "create_function": create_tapiface,
365             "configure_function": configure_device,
366             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
367                 "multicast", "broadcast", "arp"],
368             "connector_types": ["node", "fd"]
369         }),
370     NODEIFACE: dict({
371             "allow_addresses": True,
372             "help": "Node network interface",
373             "category": "devices",
374             "create_function": create_nodeiface,
375             "configure_function": configure_device,
376             "box_attributes": ["lladdr", "up", "device_name", "mtu", 
377                 "multicast", "broadcast", "arp"],
378             "connector_types": ["node", "switch"]
379         }),
380     SWITCH: dict({
381             "display_name": "Switch",
382             "help": "Switch interface",
383             "category": "devices",
384             "create_function": create_switch,
385             "box_attributes": ["up", "device_name", "mtu", "multicast"],
386              #TODO: Add attribute ("Stp", help, type, value, range, allowed, readonly, validation_function),
387              #TODO: Add attribute ("ForwarddDelay", help, type, value, range, allowed, readonly, validation_function),
388              #TODO: Add attribute ("HelloTime", help, type, value, range, allowed, readonly, validation_function),
389              #TODO: Add attribute ("AgeingTime", help, type, value, range, allowed, readonly, validation_function),
390              #TODO: Add attribute ("MaxAge", help, type, value, range, allowed, readonly, validation_function)
391            "connector_types": ["devs"]
392         }),
393     APPLICATION: dict({
394             "help": "Generic executable command line application",
395             "category": "applications",
396             "create_function": create_application,
397             "start_function": start_application,
398             "status_function": status_application,
399             "box_attributes": ["command", "user"],
400             "connector_types": ["node"],
401             "traces": ["stdout", "stderr"]
402         }),
403 })
404
405 testbed_attributes = dict({
406         "enable_debug": dict({
407                 "name": "enableDebug",
408                 "help": "Enable netns debug output",
409                 "type": Attribute.BOOL,
410                 "value": False,
411                 "validation_function": validation.is_bool
412             }),
413          "home_directory": dict({
414                 "name": "homeDirectory",
415                 "help": "Path to the directory where traces and other files \
416                         will be stored",
417                 "type": Attribute.STRING,
418                 "value": "",
419                 "flags": Attribute.DesignOnly,
420                 "validation_function": validation.is_string
421             })
422     })
423
424 class VersionedMetadataInfo(metadata.VersionedMetadataInfo):
425     @property
426     def connector_types(self):
427         return connector_types
428
429     @property
430     def connections(self):
431         return connections
432
433     @property
434     def attributes(self):
435         return attributes
436
437     @property
438     def traces(self):
439         return traces
440
441     @property
442     def create_order(self):
443         return create_order
444
445     @property
446     def configure_order(self):
447         return configure_order
448
449     @property
450     def factories_info(self):
451         return factories_info
452
453     @property
454     def testbed_attributes(self):
455         return testbed_attributes
456