2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID, TESTBED_VERSION
5 from nepi.core import metadata
6 from nepi.core.attributes import Attribute
7 from nepi.util import tags, validation
8 from nepi.util.constants import ApplicationStatus as AS, \
9 FactoryCategories as FC, DeploymentConfiguration as DC
11 from nepi.util.tunchannel_impl import \
12 preconfigure_tunchannel, postconfigure_tunchannel, \
13 prestart_tunchannel, create_tunchannel, \
14 crossconnect_tunchannel_peer_init, \
15 crossconnect_tunchannel_peer_compl
21 P2PIFACE = "P2PNodeInterface"
22 TAPIFACE = "TapNodeInterface"
23 TUNIFACE = "TunNodeInterface"
24 NODEIFACE = "NodeInterface"
26 APPLICATION = "Application"
27 TUNCHANNEL = "TunChannel"
29 NS3_TESTBED_ID = "ns3"
30 FDNETDEV = "ns3::FdNetDevice"
32 def _follow_trace(testbed_instance, guid, trace_id, filename):
33 filepath = testbed_instance.trace_filepath(guid, trace_id, filename)
34 trace = open(filepath, "wb")
35 testbed_instance.follow_trace(guid, trace_id, trace, filename)
38 ### Connection functions ####
40 def connect_switch(testbed_instance, switch_guid, interface_guid):
41 switch = testbed_instance._elements[switch_guid]
42 interface = testbed_instance._elements[interface_guid]
43 switch.connect(interface)
45 def connect_fd(testbed_instance, tap_guid, cross_data):
48 tap = testbed_instance._elements[tap_guid]
49 address = cross_data["tun_addr"]
50 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
52 passfd.sendfd(sock, tap.fd, '0')
53 # TODO: after succesful transfer, the tap device should close the fd
55 def connect_tunchannel_tun(testbed_instance, chan_guid, tap_guid):
56 connect_tunchannel_tap(testbed_instance, chan_guid, tap_guid, ethernet_mode=False)
58 def connect_tunchannel_tap(testbed_instance, chan_guid, tap_guid, ethernet_mode=True):
59 tap = testbed_instance._elements[tap_guid]
60 chan = testbed_instance._elements[chan_guid]
62 # Create a file object for the tap's interface device
63 # and send it to the channel. It should comply with all the
64 # requirements for the channel's tun_socket.
66 chan.tun_socket = os.fdopen(tap.fd)
68 # Set the channel to ethernet mode (it's a tap)
69 chan.ethernet_mode = ethernet_mode
71 # Check to see if the device uses PI headers
77 TUNGETIFF = 0x800454d2
78 IFF_NO_PI = 0x00001000
79 struct_ifreq = "x"*16+"H"+"x"*22
80 flags = struct.unpack(struct_ifreq,
81 fcntl.ioctl(tap.fd, TUNGETIFF, struct.pack(struct_ifreq,0)) )[0]
82 with_pi = (0 == (flags & IFF_NO_PI))
84 # maybe the kernel doesn't support the IOCTL,
85 # in which case, we assume it uses PI headers (as is usual)
87 chan.with_pi = with_pi
89 ### Trace functions ###
91 def nodepcap_trace(testbed_instance, guid, trace_id):
92 node = testbed_instance._elements[guid]
93 parameters = testbed_instance._get_parameters(guid)
94 filename = "%d-pcap.stdout" % guid
95 stdout = _follow_trace(testbed_instance, guid, "pcap_stdout", filename)
96 filename = "%d-pcap.stderr" % guid
97 stderr = _follow_trace(testbed_instance, guid, "pcap_stderr", filename)
98 filename = "%d-node.pcap" % guid
99 filepath = testbed_instance.trace_filepath(guid, trace_id, filename)
100 command = "tcpdump -i 'any' -w %s" % filepath
102 trace = node.Popen(command, shell = True, stdout = stdout,
103 stderr = stderr, user = user)
104 testbed_instance.follow_trace(guid, trace_id, trace, filename)
106 trace_functions = dict({
107 "pcap": nodepcap_trace,
110 ### Creation functions ###
112 def create_node(testbed_instance, guid):
113 parameters = testbed_instance._get_parameters(guid)
115 if "forward_X11" in parameters:
116 forward_X11 = parameters["forward_X11"]
117 del parameters["forward_X11"]
118 element = testbed_instance.netns.Node(forward_X11 = forward_X11)
119 testbed_instance.elements[guid] = element
121 def create_p2piface(testbed_instance, guid):
122 if guid in testbed_instance.elements:
123 # The interface pair was already instantiated
125 # search for the node asociated with the p2piface
126 node1_guid = testbed_instance.get_connected(guid, "node", "devs")
127 if len(node1_guid) == 0:
128 raise RuntimeError("Can't instantiate interface %d outside netns \
130 node1 = testbed_instance.elements[node1_guid[0]]
131 # search for the pair p2piface
132 p2p_guid = testbed_instance.get_connected(guid, "p2p","p2p")
133 if len(p2p_guid) == 0:
134 raise RuntimeError("Can't instantiate p2p interface %d. \
135 Missing interface pair" % guid)
137 node2_guid = testbed_instance.get_connected(guid2, "node", "devs")
138 if len(node2_guid) == 0:
139 raise RuntimeError("Can't instantiate interface %d outside netns \
141 node2 = testbed_instance.elements[node2_guid[0]]
142 element1, element2 = testbed_instance.netns.P2PInterface.create_pair(
144 testbed_instance.elements[guid] = element1
145 testbed_instance.elements[guid2] = element2
147 def create_tapiface(testbed_instance, guid):
148 node_guid = testbed_instance.get_connected(guid, "node", "devs")
149 if len(node_guid) == 0:
150 raise RuntimeError("Can't instantiate interface %d outside netns \
152 node = testbed_instance.elements[node_guid[0]]
153 element = node.add_tap()
154 testbed_instance.elements[guid] = element
156 def create_tuniface(testbed_instance, guid):
157 node_guid = testbed_instance.get_connected(guid, "node", "devs")
158 if len(node_guid) == 0:
159 raise RuntimeError("Can't instantiate interface %d outside netns \
161 node = testbed_instance.elements[node_guid[0]]
162 element = node.add_tun()
163 testbed_instance.elements[guid] = element
165 def create_nodeiface(testbed_instance, guid):
166 node_guid = testbed_instance.get_connected(guid, "node", "devs")
167 if len(node_guid) == 0:
168 raise RuntimeError("Can't instantiate interface %d outside netns \
170 node = testbed_instance.elements[node_guid[0]]
171 element = node.add_if()
172 testbed_instance.elements[guid] = element
174 def create_switch(testbed_instance, guid):
175 element = testbed_instance.netns.Switch()
176 testbed_instance.elements[guid] = element
178 def create_application(testbed_instance, guid):
179 testbed_instance.elements[guid] = None # Delayed construction
181 ### Start/Stop functions ###
183 def start_application(testbed_instance, guid):
184 parameters = testbed_instance._get_parameters(guid)
185 traces = testbed_instance._get_traces(guid)
186 command = parameters["command"]
188 if "user" in parameters:
189 user = parameters["user"]
190 stdout = stderr = None
191 if "stdout" in traces:
192 filename = "%d-stdout.trace" % guid
193 stdout = _follow_trace(testbed_instance, guid, "stdout", filename)
194 if "stderr" in traces:
195 filename = "%d-stderr.trace" % guid
196 stderr = _follow_trace(testbed_instance, guid, "stderr", filename)
197 node_guid = testbed_instance.get_connected(guid, "node", "apps")
198 if len(node_guid) == 0:
199 raise RuntimeError("Can't instantiate interface %d outside netns \
201 node = testbed_instance.elements[node_guid[0]]
202 element = node.Popen(command, shell = True, stdout = stdout,
203 stderr = stderr, user = user)
204 testbed_instance.elements[guid] = element
206 def stop_application(testbed_instance, guid):
207 #app = testbed_instance.elements[guid]
211 ### Status functions ###
213 def status_application(testbed_instance, guid):
214 if guid not in testbed_instance.elements.keys():
215 return AS.STATUS_NOT_STARTED
216 app = testbed_instance.elements[guid]
217 if app.poll() == None:
218 return AS.STATUS_RUNNING
219 return AS.STATUS_FINISHED
221 ### Configure functions ###
223 def configure_traces(testbed_instance, guid):
224 traces = testbed_instance._get_traces(guid)
225 for trace_id in traces:
226 if trace_id not in trace_functions:
228 trace_func = trace_functions[trace_id]
229 trace_func(testbed_instance, guid, trace_id)
231 def configure_device(testbed_instance, guid):
232 configure_traces(testbed_instance, guid)
233 element = testbed_instance._elements[guid]
234 if not guid in testbed_instance._add_address:
236 addresses = testbed_instance._add_address[guid]
237 for address in addresses:
238 (address, netprefix, broadcast) = address
239 # TODO: Decide if we should add a ipv4 or ipv6 address
240 element.add_v4_address(address, netprefix)
242 def configure_node(testbed_instance, guid):
243 configure_traces(testbed_instance, guid)
244 element = testbed_instance._elements[guid]
245 if not guid in testbed_instance._add_route:
247 routes = testbed_instance._add_route[guid]
249 (destination, netprefix, nexthop, metric) = route
250 element.add_route(prefix = destination, prefix_len = netprefix,
251 nexthop = nexthop, metric = metric)
253 ### Factory information ###
255 connector_types = dict({
257 "help": "Connector from node to applications",
263 "help": "Connector from node to network interfaces",
269 "help": "Connector to a Node",
275 "help": "Connector to a P2PInterface",
281 "help": "File descriptor receptor for devices with file descriptors",
287 "help": "File descriptor provider for devices with file descriptors",
293 "help": "Connector to a switch",
299 "help": "ip-ip tunneling over TCP link",
305 "help": "ip-ip tunneling over UDP datagrams",
314 "from": (TESTBED_ID, NODE, "devs"),
315 "to": (TESTBED_ID, P2PIFACE, "node"),
319 "from": (TESTBED_ID, NODE, "devs"),
320 "to": (TESTBED_ID, TAPIFACE, "node"),
324 "from": (TESTBED_ID, NODE, "devs"),
325 "to": (TESTBED_ID, TUNIFACE, "node"),
329 "from": (TESTBED_ID, NODE, "devs"),
330 "to": (TESTBED_ID, NODEIFACE, "node"),
334 "from": (TESTBED_ID, P2PIFACE, "p2p"),
335 "to": (TESTBED_ID, P2PIFACE, "p2p"),
339 "from": (TESTBED_ID, TAPIFACE, "fd->"),
340 "to": (None, None, "->fd"),
341 "compl_code": connect_fd,
345 "from": (TESTBED_ID, TUNIFACE, "fd->"),
346 "to": (None, None, "->fd"),
347 "compl_code": connect_fd,
351 "from": (TESTBED_ID, SWITCH, "devs"),
352 "to": (TESTBED_ID, NODEIFACE, "switch"),
353 "init_code": connect_switch,
357 "from": (TESTBED_ID, NODE, "apps"),
358 "to": (TESTBED_ID, APPLICATION, "node"),
362 "from": (TESTBED_ID, TUNCHANNEL, "->fd" ),
363 "to": (TESTBED_ID, TAPIFACE, "fd->" ),
364 "init_code": connect_tunchannel_tap,
368 "from": (TESTBED_ID, TUNCHANNEL, "->fd" ),
369 "to": (TESTBED_ID, TUNIFACE, "fd->" ),
370 "init_code": connect_tunchannel_tun,
374 "from": (TESTBED_ID, TUNCHANNEL, "tcp"),
375 "to": (None, None, "tcp"),
376 "init_code": functools.partial(crossconnect_tunchannel_peer_init,"tcp"),
377 "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"tcp"),
381 "from": (TESTBED_ID, TUNCHANNEL, "udp"),
382 "to": (None, None, "udp"),
383 "init_code": functools.partial(crossconnect_tunchannel_peer_init,"udp"),
384 "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"udp"),
390 "forward_X11": dict({
391 "name": "forward_X11",
392 "help": "Forward x11 from main namespace to the node",
393 "type": Attribute.BOOL,
395 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
396 "validation_function": validation.is_bool
400 "help": "Mac address",
401 "type": Attribute.STRING,
402 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
403 "validation_function": validation.is_mac_address
408 "type": Attribute.BOOL,
410 "validation_function": validation.is_bool
412 "device_name": dict({
414 "help": "Device name",
415 "type": Attribute.STRING,
416 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
417 "validation_function": validation.is_string
421 "help": "Maximum transmition unit for device",
422 "type": Attribute.INTEGER,
423 "validation_function": validation.is_integer
427 "help": "Broadcast address",
428 "type": Attribute.STRING,
429 "validation_function": validation.is_string # TODO: should be is address!
433 "help": "Multicast enabled",
434 "type": Attribute.BOOL,
436 "validation_function": validation.is_bool
440 "help": "ARP enabled",
441 "type": Attribute.BOOL,
443 "validation_function": validation.is_bool
447 "help": "Command line string",
448 "type": Attribute.STRING,
449 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
450 "validation_function": validation.is_string
454 "help": "System user",
455 "type": Attribute.STRING,
456 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
457 "validation_function": validation.is_string
461 "help": "Standard input",
462 "type": Attribute.STRING,
463 "flags": Attribute.ExecReadOnly | Attribute.ExecImmutable,
464 "validation_function": validation.is_string
471 "help": "Standard output stream"
475 "help": "Application standard error",
479 "help": "tcpdump at all node interfaces",
483 create_order = [ NODE, P2PIFACE, NODEIFACE, TAPIFACE,
484 TUNIFACE, TUNCHANNEL, SWITCH,
487 configure_order = [ P2PIFACE, NODEIFACE, TAPIFACE,
488 TUNIFACE, TUNCHANNEL, SWITCH,
491 factories_info = 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],
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],
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],
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],
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],
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],
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],
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],
583 testbed_attributes = dict({
584 "enable_debug": dict({
585 "name": "enableDebug",
586 "help": "Enable netns debug output",
587 "type": Attribute.BOOL,
589 "validation_function": validation.is_bool
593 supported_recovery_policies = [
597 class MetadataInfo(metadata.MetadataInfo):
599 def connector_types(self):
600 return connector_types
603 def connections(self):
607 def attributes(self):
615 def create_order(self):
619 def configure_order(self):
620 return configure_order
623 def factories_info(self):
624 return factories_info
627 def testbed_attributes(self):
628 return testbed_attributes
631 def testbed_id(self):
635 def testbed_version(self):
636 return TESTBED_VERSION
639 def supported_recover_policies(self):
640 return supported_recovery_policies