2 # -*- coding: utf-8 -*-
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, \
12 P2PIFACE = "P2PNodeInterface"
13 TAPIFACE = "TapNodeInterface"
14 NODEIFACE = "NodeInterface"
16 APPLICATION = "Application"
18 NS3_TESTBED_ID = "ns3"
19 FDNETDEV = "ns3::FileDescriptorNetDevice"
21 ### Connection functions ####
23 def connect_switch(testbed_instance, switch, interface):
24 switch.connect(interface)
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):
31 fd = tap.file_descriptor
32 address = fdnd.socket_address
33 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
35 passfd.sendfd(sock, fd, '0')
36 # TODO: after succesful transfer, the tap device should close the fd
38 ### Creation functions ###
40 def create_node(testbed_instance, guid):
41 parameters = testbed_instance._get_parameters(guid)
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
49 def create_p2piface(testbed_instance, guid):
50 if guid in testbed_instance.elements:
51 # The interface pair was already instantiated
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 \
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)
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 \
69 node2 = testbed_instance.elements[node2_guid[0]]
70 element1, element2 = testbed_instance.netns.P2PInterface.create_pair(
72 testbed_instance.elements[guid] = element1
73 testbed_instance.elements[guid2] = element2
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 \
80 node = testbed_instance.elements[node_guid[0]]
81 element = node.add_tap()
82 testbed_instance.elements[guid] = element
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 \
89 node = testbed_instance.elements[node_guid[0]]
90 element = node.add_if()
91 testbed_instance.elements[guid] = element
93 def create_switch(testbed_instance, guid):
94 element = testbed_instance.netns.Switch()
95 testbed_instance.elements[guid] = element
97 def create_application(testbed_instance, guid):
98 testbed_instance.elements[guid] = None # Delayed construction
100 ### Start/Stop functions ###
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)
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 \
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
126 ### Status functions ###
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
136 ### Factory information ###
138 connector_types = dict({
140 "help": "Connector from node to applications",
146 "help": "Connector from node to network interfaces",
152 "help": "Connector to a Node",
158 "help": "Connector to a P2PInterface",
164 "help": "Connector to a network interface that can receive a file descriptor",
170 "help": "Connector to a switch",
179 "from": (TESTBED_ID, NODE, "devs"),
180 "to": (TESTBED_ID, P2PIFACE, "node"),
185 "from": (TESTBED_ID, NODE, "devs"),
186 "to": (TESTBED_ID, TAPIFACE, "node"),
191 "from": (TESTBED_ID, NODE, "devs"),
192 "to": (TESTBED_ID, NODEIFACE, "node"),
197 "from": (TESTBED_ID, P2PIFACE, "p2p"),
198 "to": (TESTBED_ID, P2PIFACE, "p2p"),
203 "from": (TESTBED_ID, TAPIFACE, "fd"),
204 "to": (NS3_TESTBED_ID, FDNETDEV, "fd"),
205 "code": connect_fd_local,
209 "from": (TESTBED_ID, SWITCH, "devs"),
210 "to": (TESTBED_ID, NODEIFACE, "switch"),
211 "code": connect_switch,
215 "from": (TESTBED_ID, NODE, "apps"),
216 "to": (TESTBED_ID, APPLICATION, "node"),
223 "forward_X11": dict({
224 "name": "forward_X11",
225 "help": "Forward x11 from main namespace to the node",
226 "type": Attribute.BOOL,
228 "flags": Attribute.DesignOnly,
229 "validation_function": validation.is_bool
233 "help": "Mac address",
234 "type": Attribute.STRING,
235 "flags": Attribute.DesignOnly,
236 "validation_function": validation.is_mac_address
241 "type": Attribute.BOOL,
243 "validation_function": validation.is_bool
245 "device_name": dict({
247 "help": "Device name",
248 "type": Attribute.STRING,
249 "flags": Attribute.DesignOnly,
250 "validation_function": validation.is_string
254 "help": "Maximum transmition unit for device",
255 "type": Attribute.INTEGER,
256 "validation_function": validation.is_integer
260 "help": "Broadcast address",
261 "type": Attribute.STRING,
262 "validation_function": validation.is_string # TODO: should be is address!
266 "help": "Multicast enabled",
267 "type": Attribute.BOOL,
269 "validation_function": validation.is_bool
273 "help": "ARP enabled",
274 "type": Attribute.BOOL,
276 "validation_function": validation.is_bool
280 "help": "Command line string",
281 "type": Attribute.STRING,
282 "flags": Attribute.DesignOnly,
283 "validation_function": validation.is_string
287 "help": "System user",
288 "type": Attribute.STRING,
289 "flags": Attribute.DesignOnly,
290 "validation_function": validation.is_string
294 "help": "Standard input",
295 "type": Attribute.STRING,
296 "flags": Attribute.DesignOnly,
297 "validation_function": validation.is_string
304 "help": "Standard output stream"
308 "help": "Application standard error",
312 factories_order = [ NODE, P2PIFACE, NODEIFACE, TAPIFACE, SWITCH,
315 factories_info = dict({
317 "allow_routes": True,
318 "help": "Emulated Node with virtualized network stack",
319 "category": "topology",
320 "create_function": create_node,
321 "box_attributes": ["forward_X11"],
322 "connector_types": ["devs", "apps"]
325 "allow_addresses": True,
326 "help": "Point to point network interface",
327 "category": "devices",
328 "create_function": create_p2piface,
329 "box_attributes": ["lladdr", "up", "device_name", "mtu",
330 "multicast", "broadcast", "arp"],
331 "connector_types": ["node", "p2p"]
334 "allow_addresses": True,
335 "help": "Tap device network interface",
336 "category": "devices",
337 "create_function": create_tapiface,
338 "box_attributes": ["lladdr", "up", "device_name", "mtu",
339 "multicast", "broadcast", "arp"],
340 "connector_types": ["node", "fd"]
343 "allow_addresses": True,
344 "help": "Node network interface",
345 "category": "devices",
346 "create_function": create_nodeiface,
347 "box_attributes": ["lladdr", "up", "device_name", "mtu",
348 "multicast", "broadcast", "arp"],
349 "connector_types": ["node", "switch"]
352 "display_name": "Switch",
353 "help": "Switch interface",
354 "category": "devices",
355 "create_function": create_switch,
356 "box_attributes": ["up", "device_name", "mtu", "multicast"],
357 #TODO: Add attribute ("Stp", help, type, value, range, allowed, readonly, validation_function),
358 #TODO: Add attribute ("ForwarddDelay", help, type, value, range, allowed, readonly, validation_function),
359 #TODO: Add attribute ("HelloTime", help, type, value, range, allowed, readonly, validation_function),
360 #TODO: Add attribute ("AgeingTime", help, type, value, range, allowed, readonly, validation_function),
361 #TODO: Add attribute ("MaxAge", help, type, value, range, allowed, readonly, validation_function)
362 "connector_types": ["devs"]
365 "help": "Generic executable command line application",
366 "category": "applications",
367 "create_function": create_application,
368 "start_function": start_application,
369 "status_function": status_application,
370 "box_attributes": ["command", "user"],
371 "connector_types": ["node"],
372 "traces": ["stdout", "stderr"]
376 testbed_attributes = dict({
377 "enable_debug": dict({
378 "name": "enableDebug",
379 "help": "Enable netns debug output",
380 "type": Attribute.BOOL,
382 "validation_function": validation.is_bool
384 "home_directory": dict({
385 "name": "homeDirectory",
386 "help": "Path to the directory where traces and other files \
388 "type": Attribute.STRING,
390 "flags": Attribute.DesignOnly,
391 "validation_function": validation.is_string
395 class VersionedMetadataInfo(metadata.VersionedMetadataInfo):
397 def connector_types(self):
398 return connector_types
401 def connections(self):
405 def attributes(self):
413 def factories_order(self):
414 return factories_order
417 def factories_info(self):
418 return factories_info
421 def testbed_attributes(self):
422 return testbed_attributes