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 AF_INET, STATUS_NOT_STARTED, STATUS_RUNNING, \
11 wifi_standards = dict({
12 "WIFI_PHY_STANDARD_holland": 5,
13 "WIFI_PHY_STANDARD_80211p_SCH": 7,
14 "WIFI_PHY_STANDARD_80211_5Mhz": 4,
15 "WIFI_PHY_UNKNOWN": 8,
16 "WIFI_PHY_STANDARD_80211_10Mhz": 3,
17 "WIFI_PHY_STANDARD_80211g": 2,
18 "WIFI_PHY_STANDARD_80211p_CCH": 6,
19 "WIFI_PHY_STANDARD_80211a": 0,
20 "WIFI_PHY_STANDARD_80211b": 1
23 ### Connection functions ####
25 def connect_node_device(testbed_instance, node, device):
26 node.AddDevice(device)
28 def connect_queue_device(testbed_instance, queue, device):
29 device.SetQueue(queue)
31 def connect_manager_device(testbed_instance, manager, device):
32 device.SetRemoteStationManager(manager)
34 def connect_phy_device(testbed_instance, phy, device):
37 # search for the node asociated with the device
38 node_guid = testbed_instance.get_connected(guid, "node", "devs")
39 if len(node_guid) == 0:
40 raise RuntimeError("Can't instantiate interface %d outside netns \
42 node = testbed_instance.elements[node_guid[0]]
45 def connect_mac_device(testbed_instance, mac, device):
48 def connect_errormodel_device(testbed_instance, model, device):
49 device.SetReceiveErrorModel(model)
51 def connect_errormodel_phy(testbed_instance, err, phy):
52 phy.SetErrorRateModel(err)
54 def connect_channel_device(testbed_instance, channel, device):
55 device.Attach(channel)
57 def connect_simple_channel_device(testbed_instance, channel, device):
58 device.SetChannel(channel)
60 def connect_loss_channel(testbed_instance, loss, channel):
61 channel.SetPropagationLossModel(loss)
63 def connect_next_loss(testbed_instance, prev, next):
66 def connect_delay_channel(testbed_instance, delay, channel):
67 channel.SetPropagationDelayModel(delay)
69 def connect_node_application(testbed_instance, node, application):
70 node.AddApplication(application)
71 # works for ArpL3Protocol, Ipv4L3Protocol, UdpL4Protocol, TcpL4Protocol,
72 # NscTcpL4Protocol, MobilityModel (every subclass),
73 # RoutingProtocol (every subclass)
75 def connect_node_other(tesbed_instance, node, other):
76 node.AggregateObject(other)
78 ### Creation functions ###
80 def create_element(testbed_instance, guid, parameters, factory_parameters):
81 element_factory = testbed_instance.ns3.ObjectFactory()
82 factory_id = testbed_instance._create[guid]
83 element_factory.SetTypeId(factory_id)
84 for name, value in factory_parameters.iteritems():
85 testbed_instance._set(element_factory, factory_id, name, value)
86 element = element_factory.Create()
87 testbed_instance._elements[guid] = element
89 def create_node(testbed_instance, guid, parameters, factory_parameters):
90 create_element(testbed_instance, guid, parameters, factory_parameters)
91 element = testbed_instance._elements[guid]
92 element.AggregateObject(testbed_instance.PacketSocketFactory())
94 def create_dev(testbed_instance, guid, parameters, factory_parameters):
95 create_element(testbed_instance, guid, parameters, factory_parameters)
96 element = testbed_instance._elements[guid]
97 if "macAddress" in parameters:
98 address = parameters["macAddress"]
99 macaddr = testbed_instance.ns3.Mac48Address(address)
101 macaddr = testbed_instance.ns3.Mac48Address.Allocate()
102 element.SetAddress(macaddr)
104 def create_wifi_standard_model(testbed_instance, guid, parameters,
106 create_element(testbed_instance, guid, parameters, factory_parameters)
107 element = testbed_instance._elements[guid]
108 if "standard" in parameters:
109 standard = parameters["standard"]
111 elements.ConfigureStandard(wifi_standards[standard])
113 def create_ipv4stack(testbed_instance, guid, parameters, factory_parameters):
114 create_element(testbed_instance, guid, parameters, factory_parameters)
115 element = testbed_instance._elements[guid]
116 list_routing = testbed_instance.ns3.Ipv4ListRouting()
117 element.SetRoutingProtocol(list_routing)
118 static_routing = testbed_instance.ns3.Ipv4StaticRouting()
119 list_routing.AddRoutingProtocol(static_routing, 1)
121 ### Start/Stop functions ###
123 def start_application(testbed_instance, guid, parameters, traces):
124 element = testbed_instance.elements[guid]
127 def stop_application(testbed_instance, guid, parameters, traces):
128 element = testbed_instance.elements[guid]
131 ### Status functions ###
133 def status_application(testbed_instance, guid, parameters, traces):
134 if guid not in testbed_instance.elements.keys():
135 return STATUS_NOT_STARTED
136 app = testbed_instance.elements[guid]
137 if "stopTime" in parameters:
138 stop = parameters["stopTime"]
140 simTime = testbed_instance.ns3.Simulator.Now()
141 if simTime.Compare(stopTime) > 0:
142 return STATUS_RUNNING
143 return STATUS_FINISHED
145 ### Factory information ###
147 connector_types = dict({
149 "help": "Connector to a ns3::Node object (mandatory)",
155 "help": "Connector to network interfaces",
161 "help": "Connector to exactly two network interfaces (mandatory)",
167 "help": "Connector to exactly one network interface (mandatory)",
173 "help": "Connector to applications",
179 "help": "Connector to network stacks and protocols",
185 "help": "Connector to a channel for the device (mandatory)",
191 "help": "Connector to a queueing discipline (mandatory)",
197 "help": "Connector to an error model for the device",
203 "help": "Connector to interconnect devices with file descriptors",
209 "help": "Connector to interconnect elements with a PHY wifi model",
215 "help": "Connector to interconnect a wifi channel with PHY wifi models",
221 "help": "Connector to interconnect a device with a MAC wifi model",
227 "help": "Connector to interconnect a wifi device with a wifi manager",
233 "help": "Connector to a delay model",
239 "help": "Connector to a loss model",
245 "help": "Connector to the previous loss model",
251 "help": "Connector to the next loss model",
257 "help": "Connector to a mobility model for the node",
266 "from": ( "ns3", "ns3::Node", "devs" ),
267 "to": ( "ns3", "ns3::BridgeNetDevice", "node" ),
268 "code": connect_node_device,
272 "from": ( "ns3", "ns3::Node", "devs" ),
273 "to": ( "ns3", "ns3::CsmaNetDevice", "node" ),
274 "code": connect_node_device,
278 "from": ( "ns3", "ns3::Node", "devs" ),
279 "to": ( "ns3", "ns3::EmuNetDevice", "node" ),
280 "code": connect_node_device,
284 "from": ( "ns3", "ns3::Node", "devs" ),
285 "to": ( "ns3", "ns3::PointToPointNetDevice", "node" ),
286 "code": connect_node_device,
290 "from": ( "ns3", "ns3::Node", "devs" ),
291 "to": ( "ns3", "ns3::SimpleNetDevice", "node" ),
292 "code": connect_node_device,
296 "from": ( "ns3", "ns3::Node", "devs" ),
297 "to": ( "ns3", "ns3::FileDescriptorNetDevice", "node" ),
298 "code": connect_node_device,
302 "from": ( "ns3", "ns3::Node", "devs" ),
303 "to": ( "ns3", "ns3::WifiNetDevice", "node" ),
304 "code": connect_node_device,
308 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
309 "to": ( "ns3", "ns3::CsmaNetDevice", "queue" ),
310 "code": connect_queue_device,
314 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
315 "to": ( "ns3", "ns3::EmuNetDevice", "queue" ),
316 "code": connect_queue_device,
320 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
321 "to": ( "ns3", "ns3::PointToPointNetDevice", "queue" ),
322 "code": connect_queue_device,
326 "from": ( "ns3", "ns3::ArfWifiManager", "dev" ),
327 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
328 "code": connect_manager_device,
332 "from": ( "ns3", "ns3::ConstantRateWifiManager", "dev" ),
333 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
334 "code": connect_manager_device,
338 "from": ( "ns3", "ns3::YansWifiPhy", "dev" ),
339 "to": ( "ns3", "ns3::WifiNetDevice", "phy" ),
340 "code": connect_phy_device,
344 "from": ( "ns3", "ns3::QapWifiMac", "dev" ),
345 "to": ( "ns3", "ns3::WifiNetDevice", "mac" ),
346 "code": connect_mac_device,
350 "from": ( "ns3", "ns3::QstaWifiMac", "dev" ),
351 "to": ( "ns3", "ns3::WifiNetDevice", "mac" ),
352 "code": connect_mac_device,
356 "from": ( "ns3", "ns3::RateErrorModel", "dev" ),
357 "to": ( "ns3", "ns3::CsmaNetDevice", "err" ),
358 "code": connect_errormodel_device,
362 "from": ( "ns3", "ns3::RateErrorModel", "dev" ),
363 "to": ( "ns3", "ns3::PointToPointNetDevice", "err" ),
364 "code": connect_errormodel_device,
368 "from": ( "ns3", "ns3::ListErrorModel", "dev" ),
369 "to": ( "ns3", "ns3::CsmaNetDevice", "err" ),
370 "code": connect_errormodel_device,
374 "from": ( "ns3", "ns3::ListErrorModel", "dev" ),
375 "to": ( "ns3", "ns3::PointToPointNetDevice", "err" ),
376 "code": connect_errormodel_device,
380 "from": ( "ns3", "ns3::NistErrorRateModel", "phy" ),
381 "to": ( "ns3", "ns3::YansWifiPhy", "err" ),
382 "code": connect_errormodel_phy,
386 "from": ( "ns3", "ns3::CsmaChannel", "devs" ),
387 "to": ( "ns3", "ns3::CsmaNetDevice", "chan" ),
388 "code": connect_channel_device,
392 "from": ( "ns3", "ns3::PointToPointChannel", "dev2" ),
393 "to": ( "ns3", "ns3::PointToPointNetDevice", "chan" ),
394 "code": connect_channel_device,
398 "from": ( "ns3", "ns3::SimpleChannel", "devs" ),
399 "to": ( "ns3", "ns3::SimpleNetDevice", "chan" ),
400 "code": connect_simple_channel_device,
404 "from": ( "ns3", "ns3::YansWifiChannel", "phys" ),
405 "to": ( "ns3", "ns3::YansWifiPhy", "chan" ),
406 "code": connect_simple_channel_device,
410 "from": ( "ns3", "ns3::LogDistancePropagationLossModel", "prev" ),
411 "to": ( "ns3", "ns3::YansWifiChannel", "loss" ),
412 "code": connect_loss_channel,
416 "from": ( "ns3", "ns3::LogDistancePropagationLossModel", "prev" ),
417 "to": ( "ns3", "ns3::LogDistancePropagationLossModel", "next" ),
418 "code": connect_next_loss,
422 "from": ( "ns3", "ns3::ConstantSpeedPropagationDelayModel", "chan" ),
423 "to": ( "ns3", "ns3::YansWifiChannel", "delay" ),
424 "code": connect_delay_channel,
428 "from": ( "ns3", "ns3::Node", "apps" ),
429 "to": ( "ns3", "ns3::OnOffApplication", "node" ),
430 "code": connect_node_application,
434 "from": ( "ns3", "ns3::Node", "apps" ),
435 "to": ( "ns3", "ns3::PacketSink", "node" ),
436 "code": connect_node_application,
440 "from": ( "ns3", "ns3::Node", "apps" ),
441 "to": ( "ns3", "ns3::UdpEchoClient", "node" ),
442 "code": connect_node_application,
446 "from": ( "ns3", "ns3::Node", "apps" ),
447 "to": ( "ns3", "ns3::UdpEchoServer", "node" ),
448 "code": connect_node_application,
452 "from": ( "ns3", "ns3::Node", "apps" ),
453 "to": ( "ns3", "ns3::V4Ping", "node" ),
454 "code": connect_node_application,
458 "from": ( "ns3", "ns3::Node", "protos" ),
459 "to": ( "ns3", "ns3::ArpL3Protocol", "node" ),
460 "code": connect_node_other,
464 "from": ( "ns3", "ns3::Node", "protos" ),
465 "to": ( "ns3", "ns3::Icmpv4L4Protocol", "node" ),
466 "code": connect_node_other,
470 "from": ( "ns3", "ns3::Node", "protos" ),
471 "to": ( "ns3", "ns3::Ipv4L3Protocol", "node" ),
472 "code": connect_node_other,
476 "from": ( "ns3", "ns3::Node", "protos" ),
477 "to": ( "ns3", "ns3::UdpL4Protocol", "node" ),
478 "code": connect_node_other,
482 "from": ( "ns3", "ns3::Node", "protos" ),
483 "to": ( "ns3", "ns3::TcpL4Protocol", "node" ),
484 "code": connect_node_other,
488 "from": ( "ns3", "ns3::Node", "mobility" ),
489 "to": ( "ns3", "ns3::ConstantAccelerationMobilityModel", "node" ),
490 "code": connect_node_other,
494 "from": ( "ns3", "ns3::Node", "mobility" ),
495 "to": ( "ns3", "ns3::ConstantPositionMobilityModel", "node" ),
496 "code": connect_node_other,
500 "from": ( "ns3", "ns3::Node", "mobility" ),
501 "to": ( "ns3", "ns3::ConstantVelocityMobilityModel", "node" ),
502 "code": connect_node_other,
506 "from": ( "ns3", "ns3::Node", "mobility" ),
507 "to": ( "ns3", "ns3::HierarchicalMobilityModel", "node" ),
508 "code": connect_node_other,
512 "from": ( "ns3", "ns3::Node", "mobility" ),
513 "to": ( "ns3", "ns3::RandomDirection2dMobilityModel", "node" ),
514 "code": connect_node_other,
518 "from": ( "ns3", "ns3::Node", "mobility" ),
519 "to": ( "ns3", "ns3::RandomWalk2dMobilityModel", "node" ),
520 "code": connect_node_other,
524 "from": ( "ns3", "ns3::Node", "mobility" ),
525 "to": ( "ns3", "ns3::RandomWaypointMobilityModel", "node" ),
526 "code": connect_node_other,
533 "forward_X11": dict({
534 "name": "forward_X11",
535 "help": "Forward x11 from main namespace to the node",
536 "type": Attribute.BOOL,
540 "flags": Attribute.DesignOnly,
541 "validation_function": validation.is_bool
549 "help": "Standard output stream"
554 factories_order = [ NODE, P2PIFACE, NODEIFACE, TAPIFACE, SWITCH,
558 factories_info = dict({
560 "allow_routes": True,
561 "help": "Emulated Node with virtualized network stack",
562 "category": "topology",
563 "create_function": create_node,
564 "start_function": None,
565 "stop_function": None,
566 "status_function": None,
567 "box_attributes": ["forward_X11"],
568 "connector_types": ["devs", "apps"]
572 testbed_attributes = dict({
573 "ns3_bindings": dict({
574 "name": "ns3Bindings",
575 "help": "Location of the ns-3 python binding",
576 "type": Attribute.STRING,
580 "flags": Attribute.DesignOnly,
581 "validation_function": validation.is_string # TODO: validation.is_path
583 "ns3_library": dict({
584 "name": "ns3Library",
585 "help": "Location of the ns-3 library .so file",
586 "type": Attribute.STRING,
590 "flags": Attribute.DesignOnly,
591 "validation_function": validation.is_string # TODO: validation.is_path
593 "simu_impl_type": dict({
594 "name": "SimulatorImplementationType",
595 "help": "The object class to use as the simulator implementation",
596 "type": Attribute.STRING,
600 "flags": Attribute.DesignOnly,
601 "validation_function": validation.is_string
604 "name": "ChecksumEnabled",
605 "help": "A global switch to enable all checksums for all protocols",
606 "type": Attribute.BOOL,
610 "flags": Attribute.DesignOnly,
611 "validation_function": validation.is_bool
615 class VersionedMetadataInfo(metadata.VersionedMetadataInfo):
617 def connections_types(self):
618 return connection_types
621 def connections(self):
625 def attributes(self):
633 def factories_order(self):
634 return factories_order
637 def factories_info(self):
638 return factories_info
641 def testbed_attributes(self):
642 return testbed_attributes