2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
6 from nepi.util.constants import CONNECTION_DELAY
7 from nepi.util.tunchannel_impl import \
8 crossconnect_tunchannel_peer_init, \
9 crossconnect_tunchannel_peer_compl
11 ### Connection functions ####
13 def connect_dummy(testbed_instance, guid1, guid2):
16 def connect_node_device(testbed_instance, node_guid, device_guid):
17 node = testbed_instance._elements[node_guid]
18 device = testbed_instance._elements[device_guid]
19 node.AddDevice(device)
21 def connect_queue_device(testbed_instance, queue_guid, device_guid):
22 queue = testbed_instance._elements[queue_guid]
23 device = testbed_instance._elements[device_guid]
24 device.SetQueue(queue)
26 def connect_manager_device(testbed_instance, manager_guid, device_guid):
27 manager = testbed_instance._elements[manager_guid]
28 device = testbed_instance._elements[device_guid]
29 device.SetRemoteStationManager(manager)
31 def connect_phy_device(testbed_instance, phy_guid, device_guid):
32 phy = testbed_instance._elements[phy_guid]
33 if hasattr(phy, "GetErrorRateModel") and phy.GetErrorRateModel() == None:
34 return CONNECTION_DELAY
35 device = testbed_instance._elements[device_guid]
38 # search for the node asociated with the device
39 node_guid = testbed_instance.get_connected(device_guid, "node", "devs")
40 if len(node_guid) == 0:
41 raise RuntimeError("Can't instantiate interface %d outside netns \
43 node = testbed_instance.elements[node_guid[0]]
46 def connect_mac_device(testbed_instance, mac_guid, device_guid):
47 mac = testbed_instance._elements[mac_guid]
48 device = testbed_instance._elements[device_guid]
51 def connect_errormodel_device(testbed_instance, model_guid, device_guid):
52 model = testbed_instance._elements[model_guid]
53 device = testbed_instance._elements[device_guid]
54 device.SetReceiveErrorModel(model)
56 def connect_errormodel_phy(testbed_instance, err_guid, phy_guid):
57 err = testbed_instance._elements[err_guid]
58 phy = testbed_instance._elements[phy_guid]
59 phy.SetErrorRateModel(err)
61 def connect_channel_device(testbed_instance, channel_guid, device_guid):
62 channel = testbed_instance._elements[channel_guid]
63 device = testbed_instance._elements[device_guid]
64 device.Attach(channel)
66 def connect_simple_channel_device(testbed_instance, channel_guid, device_guid):
67 channel = testbed_instance._elements[channel_guid]
68 device = testbed_instance._elements[device_guid]
69 device.SetChannel(channel)
71 def connect_loss_channel(testbed_instance, loss_guid, channel_guid):
72 loss = testbed_instance._elements[loss_guid]
73 channel = testbed_instance._elements[channel_guid]
74 channel.SetPropagationLossModel(loss)
76 def connect_next_loss(testbed_instance, prev_guid, next_guid):
77 prev = testbed_instance._elements[prev_guid]
78 next = testbed_instance._elements[next_guid]
81 def connect_delay_channel(testbed_instance, delay_guid, channel_guid):
82 delay = testbed_instance._elements[delay_guid]
83 channel = testbed_instance._elements[channel_guid]
84 channel.SetPropagationDelayModel(delay)
86 def connect_node_application(testbed_instance, node_guid, application_guid):
87 node = testbed_instance._elements[node_guid]
88 application = testbed_instance._elements[application_guid]
89 node.AddApplication(application)
90 # works for ArpL3Protocol, Ipv4L3Protocol, UdpL4Protocol, TcpL4Protocol,
91 # NscTcpL4Protocol, MobilityModel (every subclass),
92 # RoutingProtocol (every subclass)
94 def connect_node_other(testbed_instance, node_guid, other_guid):
95 node = testbed_instance._elements[node_guid]
96 other = testbed_instance._elements[other_guid]
97 node.AggregateObject(other)
99 def connect_ss_sflow(testbed_instance, station_guid, sflow_guid):
100 ss = testbed_instance._elements[station_guid]
101 sflow = testbed_instance._elements[sflow_guid]
102 ss.AddServiceFlow(sflow)
104 #def connect_bs_sflow(testbed_instance, station_guid, sflow_guid):
105 # bs = testbed_instance._elements[station_guid]
106 # sflow = testbed_instance._elements[sflow_guid]
107 # bs.GetServiceFlowManager().AddServiceFlow (sflow)
109 def connect_bstation_linksched(testbed_instance, bstation_guid, linksched_guid):
110 bstation = testbed_instance._elements[bstation_guid]
111 linksched = testbed_instance._elements[linksched_guid]
112 linksched.SetBs(bstation)
114 def connect_classifier_sflow(testbed_instance, classifier_guid, sflow_guid):
115 classifier = testbed_instance._elements[classifier_guid]
116 sflow = testbed_instance._elements[sflow_guid]
117 csparam = testbed_instance.ns3.CsParameters(testbed_instance.ns3.CsParameters.ADD, classifier)
118 sflow.SetConvergenceSublayerParam (csparam);
120 def connect_fd(testbed_instance, fdnd_guid, cross_data):
121 def recvfd(sock, fdnd):
122 (fd, msg) = passfd.recvfd(sock)
123 # Store a reference to the endpoint to keep the socket alive
124 fdnd.SetFileDescriptor(fd)
129 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
131 address = sock.getsockname()
132 # Set tun standard contract attributes
133 testbed_instance.set(fdnd_guid, "tun_addr", address)
134 testbed_instance.set(fdnd_guid, "tun_proto", "fd")
135 testbed_instance.set(fdnd_guid, "tun_port", 0)
136 testbed_instance.set(fdnd_guid, "tun_key", ("\xfa"*32).encode("base64").strip()) # unimportant, fds aren't encrypted
137 fdnd = testbed_instance._elements[fdnd_guid]
138 t = threading.Thread(target=recvfd, args=(sock,fdnd))
141 def connect_tunchannel_fd(testbed_instance, tun_guid, fdnd_guid):
142 fdnd = testbed_instance._elements[fdnd_guid]
143 tun = testbed_instance._elements[tun_guid]
145 # Create socket pair to connect the FDND and the TunChannel with it
147 sock1, sock2 = socket.socketpair(
148 socket.AF_UNIX, socket.SOCK_SEQPACKET)
150 # Store a reference to the endpoint to keep the socket alive
151 fdnd._endpoint_socket = sock1
152 fdnd.SetFileDescriptor(sock1.fileno())
154 # Send the other endpoint to the TUN channel
155 tun.tun_socket = sock2
157 # With this kind of tun_socket, NS3 will expect a PI header
158 # (sockets don't support the TUNGETIFF ioctl, so it will assume
159 # the default presence of PI headers)
163 ### Connector information ###
165 connector_types = dict({
167 "help": "Connector to a ns3::Node object (mandatory)",
173 "help": "Connector to network interfaces",
179 "help": "Connector to exactly two network interfaces (mandatory)",
185 "help": "Connector to exactly one network interface (mandatory)",
191 "help": "Connector to applications",
197 "help": "Connector to network stacks and protocols",
203 "help": "Connector to a channel for the device (mandatory)",
209 "help": "Connector to a queueing discipline (mandatory)",
215 "help": "Connector to an error model for the device",
221 "help": "Connector for file descriptor reception for devices with file descriptors",
227 "help": "Connector for file descriptor providing for devices with file descriptors",
233 "help": "Connector to a PHY wifi model",
239 "help": "Connector to a wifi channel with PHY wifi models",
245 "help": "Connector to a MAC wifi model",
251 "help": "Connector to a wifi manager",
257 "help": "Connector to a delay model",
263 "help": "Connector to a loss model",
269 "help": "Connector to the previous loss model",
275 "help": "Connector to the next loss model",
281 "help": "Connector to a mobility model",
287 "help": "Connector for ip-ip tunneling over TCP link",
293 "help": "Connector for ip-ip tunneling over UDP datagrams",
299 "help": "Connector to service flows",
305 "help": "Connector to a uplink scheduler",
311 "help": "Connector to a dowlink scheduler",
317 "help": "Connector to a classifier recod",
323 "help": "Connector to a service flow",
332 "from": ( "ns3", "ns3::Node", "devs" ),
333 "to": ( "ns3", "ns3::BridgeNetDevice", "node" ),
334 "init_code": connect_node_device,
338 "from": ( "ns3", "ns3::Node", "devs" ),
339 "to": ( "ns3", "ns3::CsmaNetDevice", "node" ),
340 "init_code": connect_node_device,
344 "from": ( "ns3", "ns3::Node", "devs" ),
345 "to": ( "ns3", "ns3::EmuNetDevice", "node" ),
346 "init_code": connect_node_device,
350 "from": ( "ns3", "ns3::Node", "devs" ),
351 "to": ( "ns3", "ns3::PointToPointNetDevice", "node" ),
352 "init_code": connect_node_device,
356 "from": ( "ns3", "ns3::Node", "devs" ),
357 "to": ( "ns3", "ns3::SimpleNetDevice", "node" ),
358 "init_code": connect_node_device,
362 "from": ( "ns3", "ns3::Node", "devs" ),
363 "to": ( "ns3", "ns3::FdNetDevice", "node" ),
364 "init_code": connect_node_device,
368 "from": ( "ns3", "ns3::Node", "devs" ),
369 "to": ( "ns3", "ns3::WifiNetDevice", "node" ),
370 "init_code": connect_node_device,
374 "from": ( "ns3", "ns3::Node", "devs" ),
375 "to": ( "ns3", "ns3::SubscriberStationNetDevice", "node" ),
376 "init_code": connect_node_device,
380 "from": ( "ns3", "ns3::Node", "devs" ),
381 "to": ( "ns3", "ns3::BaseStationNetDevice", "node" ),
382 "init_code": connect_node_device,
386 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
387 "to": ( "ns3", "ns3::CsmaNetDevice", "queue" ),
388 "init_code": connect_queue_device,
392 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
393 "to": ( "ns3", "ns3::EmuNetDevice", "queue" ),
394 "init_code": connect_queue_device,
398 "from": ( "ns3", "ns3::DropTailQueue", "dev" ),
399 "to": ( "ns3", "ns3::PointToPointNetDevice", "queue" ),
400 "init_code": connect_queue_device,
404 "from": ( "ns3", "ns3::ArfWifiManager", "dev" ),
405 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
406 "init_code": connect_manager_device,
410 "from": ( "ns3", "ns3::ConstantRateWifiManager", "dev" ),
411 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
412 "init_code": connect_manager_device,
416 "from": ( "ns3", "ns3::AarfcdWifiManager", "dev" ),
417 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
418 "init_code": connect_manager_device,
422 "from": ( "ns3", "ns3::AarfWifiManager", "dev" ),
423 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
424 "init_code": connect_manager_device,
428 "from": ( "ns3", "ns3::AmrrWifiManager", "dev" ),
429 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
430 "init_code": connect_manager_device,
434 "from": ( "ns3", "ns3::CaraWifiManager", "dev" ),
435 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
436 "init_code": connect_manager_device,
440 "from": ( "ns3", "ns3::IdealWifiManager", "dev" ),
441 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
442 "init_code": connect_manager_device,
446 "from": ( "ns3", "ns3::MinstrelWifiManager", "dev" ),
447 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
448 "init_code": connect_manager_device,
452 "from": ( "ns3", "ns3::OnoeWifiManager", "dev" ),
453 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
454 "init_code": connect_manager_device,
458 "from": ( "ns3", "ns3::RraaWifiManager", "dev" ),
459 "to": ( "ns3", "ns3::WifiNetDevice", "manager" ),
460 "init_code": connect_manager_device,
464 "from": ( "ns3", "ns3::YansWifiPhy", "dev" ),
465 "to": ( "ns3", "ns3::WifiNetDevice", "phy" ),
466 "init_code": connect_phy_device,
470 "from": ( "ns3", "ns3::SimpleOfdmWimaxPhy", "dev" ),
471 "to": ( "ns3", "ns3::SubscriberStationNetDevice", "phy" ),
472 "init_code": connect_phy_device,
476 "from": ( "ns3", "ns3::SimpleOfdmWimaxPhy", "dev" ),
477 "to": ( "ns3", "ns3::BaseStationNetDevice", "phy" ),
478 "init_code": connect_phy_device,
482 "from": ( "ns3", "ns3::ApWifiMac", "dev" ),
483 "to": ( "ns3", "ns3::WifiNetDevice", "mac" ),
484 "init_code": connect_mac_device,
488 "from": ( "ns3", "ns3::StaWifiMac", "dev" ),
489 "to": ( "ns3", "ns3::WifiNetDevice", "mac" ),
490 "init_code": connect_mac_device,
494 "from": ( "ns3", "ns3::RateErrorModel", "dev" ),
495 "to": ( "ns3", "ns3::CsmaNetDevice", "err" ),
496 "init_code": connect_errormodel_device,
500 "from": ( "ns3", "ns3::RateErrorModel", "dev" ),
501 "to": ( "ns3", "ns3::PointToPointNetDevice", "err" ),
502 "init_code": connect_errormodel_device,
506 "from": ( "ns3", "ns3::ListErrorModel", "dev" ),
507 "to": ( "ns3", "ns3::CsmaNetDevice", "err" ),
508 "init_code": connect_errormodel_device,
512 "from": ( "ns3", "ns3::ListErrorModel", "dev" ),
513 "to": ( "ns3", "ns3::PointToPointNetDevice", "err" ),
514 "init_code": connect_errormodel_device,
518 "from": ( "ns3", "ns3::NistErrorRateModel", "phy" ),
519 "to": ( "ns3", "ns3::YansWifiPhy", "err" ),
520 "init_code": connect_errormodel_phy,
524 "from": ( "ns3", "ns3::CsmaChannel", "devs" ),
525 "to": ( "ns3", "ns3::CsmaNetDevice", "chan" ),
526 "init_code": connect_channel_device,
530 "from": ( "ns3", "ns3::PointToPointChannel", "dev2" ),
531 "to": ( "ns3", "ns3::PointToPointNetDevice", "chan" ),
532 "init_code": connect_channel_device,
536 "from": ( "ns3", "ns3::SimpleChannel", "devs" ),
537 "to": ( "ns3", "ns3::SimpleNetDevice", "chan" ),
538 "init_code": connect_simple_channel_device,
542 "from": ( "ns3", "ns3::SimpleOfdmWimaxChannel", "devs" ),
543 "to": ( "ns3", "ns3::SubscriberStationNetDevice", "chan" ),
544 "init_code": connect_channel_device,
548 "from": ( "ns3", "ns3::SimpleOfdmWimaxChannel", "devs" ),
549 "to": ( "ns3", "ns3::BaseStationNetDevice", "chan" ),
550 "init_code": connect_channel_device,
554 "from": ( "ns3", "ns3::YansWifiChannel", "phys" ),
555 "to": ( "ns3", "ns3::YansWifiPhy", "chan" ),
556 "init_code": connect_simple_channel_device,
560 "from": ( "ns3", "ns3::YansWifiChannel", "phys" ),
561 "to": ( "ns3", "ns3::YansWifiPhy", "chan" ),
562 "init_code": connect_simple_channel_device,
566 "from": ( "ns3", "ns3::LogDistancePropagationLossModel", "prev" ),
567 "to": ( "ns3", "ns3::YansWifiChannel", "loss" ),
568 "init_code": connect_loss_channel,
572 "from": ( "ns3", "ns3::LogDistancePropagationLossModel", "prev" ),
573 "to": ( "ns3", "ns3::LogDistancePropagationLossModel", "next" ),
574 "init_code": connect_next_loss,
578 "from": ( "ns3", "ns3::ConstantSpeedPropagationDelayModel", "chan" ),
579 "to": ( "ns3", "ns3::YansWifiChannel", "delay" ),
580 "init_code": connect_delay_channel,
584 "from": ( "ns3", "ns3::Node", "apps" ),
585 "to": ( "ns3", "ns3::OnOffApplication", "node" ),
586 "init_code": connect_node_application,
590 "from": ( "ns3", "ns3::Node", "apps" ),
591 "to": ( "ns3", "ns3::PacketSink", "node" ),
592 "init_code": connect_node_application,
596 "from": ( "ns3", "ns3::Node", "apps" ),
597 "to": ( "ns3", "ns3::UdpEchoClient", "node" ),
598 "init_code": connect_node_application,
602 "from": ( "ns3", "ns3::Node", "apps" ),
603 "to": ( "ns3", "ns3::UdpEchoServer", "node" ),
604 "init_code": connect_node_application,
608 "from": ( "ns3", "ns3::Node", "apps" ),
609 "to": ( "ns3", "ns3::UdpClient", "node" ),
610 "init_code": connect_node_application,
614 "from": ( "ns3", "ns3::Node", "apps" ),
615 "to": ( "ns3", "ns3::UdpServer", "node" ),
616 "init_code": connect_node_application,
619 "from": ( "ns3", "ns3::Node", "apps" ),
620 "to": ( "ns3", "ns3::V4Ping", "node" ),
621 "init_code": connect_node_application,
625 "from": ( "ns3", "ns3::Node", "protos" ),
626 "to": ( "ns3", "ns3::ArpL3Protocol", "node" ),
627 "init_code": connect_node_other,
631 "from": ( "ns3", "ns3::Node", "protos" ),
632 "to": ( "ns3", "ns3::Icmpv4L4Protocol", "node" ),
633 "init_code": connect_node_other,
637 "from": ( "ns3", "ns3::Node", "protos" ),
638 "to": ( "ns3", "ns3::Icmpv6L4Protocol", "node" ),
639 "init_code": connect_node_other,
643 "from": ( "ns3", "ns3::Node", "protos" ),
644 "to": ( "ns3", "ns3::Ipv4L3Protocol", "node" ),
645 "init_code": connect_node_other,
649 "from": ( "ns3", "ns3::Node", "protos" ),
650 "to": ( "ns3", "ns3::Ipv6L3Protocol", "node" ),
651 "init_code": connect_node_other,
655 "from": ( "ns3", "ns3::Node", "protos" ),
656 "to": ( "ns3", "ns3::UdpL4Protocol", "node" ),
657 "init_code": connect_node_other,
661 "from": ( "ns3", "ns3::Node", "protos" ),
662 "to": ( "ns3", "ns3::TcpL4Protocol", "node" ),
663 "init_code": connect_node_other,
667 "from": ( "ns3", "ns3::Node", "mobility" ),
668 "to": ( "ns3", "ns3::ConstantAccelerationMobilityModel", "node" ),
669 "init_code": connect_node_other,
673 "from": ( "ns3", "ns3::Node", "mobility" ),
674 "to": ( "ns3", "ns3::ConstantPositionMobilityModel", "node" ),
675 "init_code": connect_node_other,
679 "from": ( "ns3", "ns3::Node", "mobility" ),
680 "to": ( "ns3", "ns3::ConstantVelocityMobilityModel", "node" ),
681 "init_code": connect_node_other,
685 "from": ( "ns3", "ns3::Node", "mobility" ),
686 "to": ( "ns3", "ns3::HierarchicalMobilityModel", "node" ),
687 "init_code": connect_node_other,
691 "from": ( "ns3", "ns3::Node", "mobility" ),
692 "to": ( "ns3", "ns3::RandomDirection2dMobilityModel", "node" ),
693 "init_code": connect_node_other,
697 "from": ( "ns3", "ns3::Node", "mobility" ),
698 "to": ( "ns3", "ns3::RandomWalk2dMobilityModel", "node" ),
699 "init_code": connect_node_other,
703 "from": ( "ns3", "ns3::Node", "mobility" ),
704 "to": ( "ns3", "ns3::RandomWaypointMobilityModel", "node" ),
705 "init_code": connect_node_other,
709 "from": ( "ns3", "ns3::Node", "mobility" ),
710 "to": ( "ns3", "ns3::WaypointMobilityModel", "node" ),
711 "init_code": connect_node_other,
715 "from": ( "ns3", "ns3::SubscriberStationNetDevice", "sflows" ),
716 "to": ( "ns3", "ns3::ServiceFlow", "dev" ),
717 "init_code": connect_ss_sflow,
721 # "from": ( "ns3", "ns3::BaseStationNetDevice", "sflows" ),
722 # "to": ( "ns3", "ns3::ServiceFlow", "dev" ),
723 # "init_code": connect_bs_sflow,
727 "from": ( "ns3", "ns3::BaseStationNetDevice", "uplnk" ),
728 "to": ( "ns3", "ns3::UplinkSchedulerSimple", "dev" ),
729 "init_code": connect_bstation_linksched,
733 "from": ( "ns3", "ns3::BaseStationNetDevice", "uplnk" ),
734 "to": ( "ns3", "ns3::UplinkSchedulerRtps", "dev" ),
735 "init_code": connect_bstation_linksched,
739 "from": ( "ns3", "ns3::BaseStationNetDevice", "dwnlnk" ),
740 "to": ( "ns3", "ns3::BSSchedulerSimple", "dev" ),
741 "init_code": connect_bstation_linksched,
745 "from": ( "ns3", "ns3::BaseStationNetDevice", "dwnlnk" ),
746 "to": ( "ns3", "ns3::BSSchedulerRtps", "dev" ),
747 "init_code": connect_bstation_linksched,
751 "from": ( "ns3", "ns3::IpcsClassifierRecord", "sflow" ),
752 "to": ( "ns3", "ns3::ServiceFlow", "classif" ),
753 "init_code": connect_classifier_sflow,
757 "from": ( "ns3", "ns3::FdNetDevice", "->fd" ),
758 "to": ( None, None, "fd->" ),
759 "init_code": connect_fd,
763 "from": ( "ns3", "ns3::Nepi::TunChannel", "fd->" ),
764 "to": ( "ns3", "ns3::FdNetDevice", "->fd" ),
765 "init_code": connect_tunchannel_fd,
769 "from": ( "ns3", "ns3::Nepi::TunChannel", "tcp"),
770 "to": (None, None, "tcp"),
771 "init_code": functools.partial(crossconnect_tunchannel_peer_init,"tcp"),
772 "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"tcp"),
776 "from": ( "ns3", "ns3::Nepi::TunChannel", "udp"),
777 "to": (None, None, "udp"),
778 "init_code": functools.partial(crossconnect_tunchannel_peer_init,"udp"),
779 "compl_code": functools.partial(crossconnect_tunchannel_peer_compl,"udp"),