+#\r
+# NEPI, a framework to manage network experiments\r
+# Copyright (C) 2013 INRIA\r
+#\r
+# This program is free software: you can redistribute it and/or modify\r
+# it under the terms of the GNU General Public License version 2 as\r
+# published by the Free Software Foundation;\r
+#\r
+# This program is distributed in the hope that it will be useful,\r
+# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+# GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License\r
+# along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+#\r
+# Author: Damien Saucez <damien.saucez@inria.fr>\r
+# Alina Quereilhac <alina.quereilhac@inria.fr>\r
+\r
+import ipaddr\r
+from random import randint\r
+from nepi.execution.ec import ExperimentController \r
+from nepi.execution.resource import ResourceState, ResourceAction\r
+\r
+# ########################################################\r
+class Experiment(object):\r
+ # ec : ExperimentController\r
+ # node: planetlab::Node\r
+ def __init__(self, ec, node_info, nb_nodes, real_time = True):\r
+ print "Experiement %s %s" % (node_info, nb_nodes)\r
+\r
+ # remember the ExperimentController the experiment is associated to\r
+ self.ec = ec\r
+\r
+ # define the physical machine to run the experiment on\r
+ self.add_node(node_info)\r
+\r
+ # number of simulated nodes moving in the \r
+ self.nb_nodes = nb_nodes\r
+ \r
+ # fix the geographical boundaries of the network\r
+ self.bounds_width = self.bounds_height = 100\r
+ \r
+ # fix the speed at which mobile nodes can move\r
+ self.speed = 1\r
+\r
+ # collection of simulated node (their GID) in the simulator\r
+ # nsnodes[0] is always the GID of the simulated node containing the\r
+ # access point\r
+ self.nsnodes = list()\r
+ \r
+ # collection of application (their GID) running in the simulator\r
+ # apps[0] is always the GID of the agent application running on the\r
+ # access point\r
+ self.apps = list()\r
+\r
+ # prepare the ns-3 simulator to use for the experiment\r
+ self.add_simulator(real_time)\r
+\r
+ # for sanity check\r
+ self.topology_built = False\r
+\r
+ def add_node(self, node_info):\r
+ """\r
+ Define the physical machine on which run the experiment\r
+ """\r
+ if node_info["hostname"] == "localhost":\r
+ self.node = self.ec.register_resource("linux::Node")\r
+ self.ec.set(self.node, "hostname", "localhost")\r
+ else:\r
+ self.node = self.ec.register_resource("planetlab::Node")\r
+ self.ec.set(self.node, "hostname", node_info["hostname"])\r
+ self.ec.set(self.node, "username", node_info["username"])\r
+ self.ec.set(self.node, "identity", node_info["identity"])\r
+ self.ec.set(self.node, "cleanProcesses", True)\r
+ self.ec.set(self.node, "cleanExperiment", True)\r
+\r
+ return self.node\r
+\r
+ def add_simulator(self, real_time):\r
+ """\r
+ Add a ns-3 simulator on the node used for the experiment\r
+ """\r
+ # creat the ns-3 simulator instance\r
+ self.simu = self.ec.register_resource("linux::ns3::Simulation")\r
+ self.ec.set (self.simu, "StopTime", "200s")\r
+ #\r
+ # run it in realtime mode if asked\r
+ if real_time:\r
+ self.ec.set(self.simu, "simulatorImplementationType", "ns3::RealtimeSimulatorImpl")\r
+ # \r
+ # log additional information\r
+ self.ec.set(self.simu, "checksumEnabled", True)\r
+ self.ec.set(self.simu, "verbose", True)\r
+ self.ec.set(self.simu, "enableDump", True)\r
+ self.ec.register_connection(self.simu, self.node)\r
+\r
+ return self.simu\r
+\r
+ # == ns-3 simulation helper functions =====================================\r
+ def add_nsnode(self):\r
+ """\r
+ Create a ns-3 node and add it to the simulator\r
+ """\r
+ # create a ns-3 node\r
+ nsnode = self.ec.register_resource("ns3::Node")\r
+ # enable its network stack\r
+ self.ec.set(nsnode, "enableStack", True)\r
+ self.ec.register_connection(nsnode, self.simu)\r
+\r
+ return nsnode\r
+\r
+ def add_wifi_channel(self):\r
+ """\r
+ Create the WiFi channel on which all nodes will be connected\r
+ """\r
+ # create a channel\r
+ channel = self.ec.register_resource("ns3::YansWifiChannel")\r
+ \r
+ # specify the delay model\r
+ delay = self.ec.register_resource("ns3::ConstantSpeedPropagationDelayModel")\r
+ self.ec.register_connection(channel, delay)\r
+\r
+ # specify a loss model\r
+ loss = self.ec.register_resource("ns3::LogDistancePropagationLossModel")\r
+ self.ec.register_connection(channel, loss)\r
+\r
+ return channel\r
+\r
+ def add_wifi_device(self, node, ip, prefix, access_point = False):\r
+ """\r
+ Add and configure the WiFi interface on a simulated node\r
+ """\r
+ \r
+ # create the WiFi network interface\r
+ dev = self.ec.register_resource("ns3::WifiNetDevice")\r
+ \r
+ # specify the network layer parameters\r
+ self.ec.set(dev, "ip", ip)\r
+ self.ec.set(dev, "prefix", prefix)\r
+ self.ec.register_connection(node, dev)\r
+ \r
+ # specify the MAC layer parameters\r
+ #\r
+ # can be in access point mode or not\r
+ if access_point:\r
+ mac = self.ec.register_resource("ns3::ApWifiMac")\r
+ else:\r
+ mac = self.ec.register_resource("ns3::StaWifiMac")\r
+ # the MAC is IEEE 802.11a\r
+ self.ec.set(mac, "Standard", "WIFI_PHY_STANDARD_80211a")\r
+ self.ec.register_connection(dev, mac)\r
+\r
+ # specify the physical layer parameters\r
+ phy = self.ec.register_resource("ns3::YansWifiPhy")\r
+ #\r
+ # it physical layer is IEEE802.11a\r
+ self.ec.set(phy, "Standard", "WIFI_PHY_STANDARD_80211a")\r
+ self.ec.register_connection(dev, phy)\r
+ #\r
+ # specify an error model for transmissions\r
+ error = self.ec.register_resource("ns3::NistErrorRateModel")\r
+ self.ec.register_connection(phy, error)\r
+ \r
+ # specify the Wifi manager to be assocated with the interface\r
+ manager = self.ec.register_resource("ns3::ArfWifiManager")\r
+ self.ec.register_connection(dev, manager)\r
+\r
+ return dev, phy\r
+\r
+ def add_random_mobility(self, node, x, y, z, speed, bounds_width, bounds_height):\r
+ """\r
+ Create a mobility model for node with random movements\r
+ """\r
+ position = "%d:%d:%d" % (x, y, z)\r
+ bounds = "0|%d|0|%d" % (bounds_width, bounds_height) \r
+ speed = "ns3::UniformRandomVariable[Min=%d|Max=%s]" % (speed, speed)\r
+ pause = "ns3::ConstantRandomVariable[Constant=1.0]"\r
+\r
+ mobility = self.ec.register_resource("ns3::RandomDirection2dMobilityModel")\r
+ self.ec.set(mobility, "Position", position)\r
+ self.ec.set(mobility, "Bounds", bounds)\r
+ self.ec.set(mobility, "Speed", speed)\r
+ self.ec.set(mobility, "Pause", pause)\r
+ self.ec.register_connection(node, mobility)\r
+\r
+ return mobility\r
+\r
+ def add_constant_mobility(self, node, x, y, z):\r
+ """\r
+ Create a mobility model for node with a constant position\r
+ """\r
+ mobility = self.ec.register_resource("ns3::ConstantPositionMobilityModel") \r
+ position = "%d:%d:%d" % (x, y, z)\r
+ self.ec.set(mobility, "Position", position)\r
+ self.ec.register_connection(node, mobility)\r
+\r
+ return mobility\r
+\r
+ def create_simulated_node(self, ip, prefix, channel, access_point, x, y):\r
+ """\r
+ Create a simulated node connected on a WiFi channel\r
+ """\r
+ # Create the ns node that will run the application\r
+ nsnode = self.add_nsnode()\r
+\r
+ # Add a WiFi interface to the node\r
+ dev, phy = self.add_wifi_device(nsnode, ip, prefix, access_point)\r
+ #\r
+ # Connect the access point to the WiFi network\r
+ self.ec.register_connection(channel, phy)\r
+\r
+ # Specify that the node mobility \r
+ #\r
+ # access point is not mobile\r
+ if access_point:\r
+ mobility = self.add_constant_mobility(nsnode, x, y, 0)\r
+ # other nodes have random mobility pattern\r
+ else:\r
+ mobility = self.add_random_mobility(nsnode, x, y, 0, self.speed, self.bounds_width, self.bounds_height)\r
+\r
+ return nsnode\r
+\r
+ def add_route(self, nsnode, netblock, prefix, nexthop):\r
+ """\r
+ add a route on ns-3 node nsnode for netblock/prefix via nexthop\r
+ """\r
+ route = self.ec.register_resource("ns3::Route")\r
+ self.ec.set(route, "network", netblock)\r
+ self.ec.set(route, "prefix", prefix)\r
+ self.ec.set(route, "nexthop", nexthop)\r
+ self.ec.register_connection(route, nsnode)\r
+ print "route %s/%s via %s added on nsnode %s (%s)" % (netblock, prefix, nexthop, nsnode, self)\r
+\r
+ return route\r
+\r
+ def add_vroute(self, dev, netblock, prefix, nexthop):\r
+ """\r
+ Add a route on Planetlab node for netblock/prefix via nexthop\r
+ """\r
+ route = self.ec.register_resource("planetlab::Vroute")\r
+ self.ec.set(route, "network", netblock)\r
+ self.ec.set(route, "prefix", prefix)\r
+ self.ec.set(route, "nexthop", nexthop)\r
+ self.ec.register_connection(route, dev)\r
+ print "Vroute %s/%s via %s added on nsnode %s (%s)" % (netblock, prefix, nexthop, dev, self)\r
+\r
+ return route\r
+\r
+ def add_agent(self, nsnode):\r
+ """\r
+ Add a agent application\r
+ """\r
+ # Create a DCE application running the agent code\r
+ app = self.ec.register_resource("linux::ns3::dce::Application")\r
+ self.ec.set(app, "sources", "code/agent.c")\r
+ self.ec.set(app "build", "gcc -fPIC -pie -rdynamic ${SRC}/agent.c -o ${BIN_DCE}/agent")\r
+ self.ec.set(app, "binary", "agent")\r
+ self.ec.set(app, "arguments", "45005")\r
+ self.ec.set(app, "stackSize", 1<<20)\r
+ self.ec.set(app, "StartTime", "10s")\r
+ self.ec.set(app, "StopTime", "200s")\r
+\r
+ # Associate the application with the simulated node\r
+ self.ec.register_connection(app, nsnode)\r
+ \r
+ # Make the application start only once the simulated node is started\r
+ self.ec.register_condition(app, ResourceAction.START, \r
+ nsnode, ResourceState.STARTED, time="5s")\r
+\r
+ return app\r
+\r
+ def add_transmitter(self, nsnode):\r
+ """\r
+ Add a transmitter application\r
+ """\r
+ # Create a DCE application running the transmitter code\r
+ app = self.ec.register_resource("linux::ns3::dce::Application")\r
+ self.ec.set(app, "sources", "code/transmitter.c")\r
+ self.ec.set(app, "build", "gcc -fPIC -pie -rdynamic ${SRC}/transmitter.c -o ${BIN_DCE}/transmitter")\r
+ self.ec.set(app, "binary", "transmitter")\r
+ self.ec.set(app, "arguments", "%s;45005" % target)\r
+ self.ec.set(app, "stackSize", 1<<20)\r
+ self.ec.set(app, "StartTime", "10s")\r
+ self.ec.set(app, "StopTime", "200s")\r
+\r
+ # Associate the application with the simulated node\r
+ self.ec.register_connection(app, nsnode)\r
+ #\r
+ # Make the application start only once the simulated node and the serer are started\r
+ self.ec.register_condition(app, ResourceAction.START, \r
+ [nsnode, self.apps[0]], ResourceState.STARTED, time="10s")\r
+\r
+ return app\r
+\r
+ def add_planetlab_transmitter(self, target):\r
+ """\r
+ Add a planetlab transmitter application\r
+ """\r
+\r
+ # Create an application running the transmitter code\r
+ app = self.ec.register_resource("linux::Application")\r
+ self.ec.set(app, "sources", "code/transmitter.c")\r
+ self.ec.set(app, "build", "make ${SRC}/transmitter")\r
+ self.ec.set(app, "command", "${SRC}/transmitter %s 45005" % target)\r
+\r
+ # Associate the application with the Planetlab node\r
+ self.ec.register_connection(app, self.node)\r
+ \r
+ # Make the application start only once the simulated agent and the node are started\r
+ self.ec.register_condition(app, ResourceAction.START, \r
+ [self.apps[0], self.node], ResourceState.STARTED, time="10s")\r
+\r
+ return app\r
+\r
+ # == Topology construction ================================================\r
+ def build_topology(self, netblock, prefix, target):\r
+ """\r
+ Builds a topology composed of one fixed access point and nb_nodes\r
+ mobile nodes\r
+ """\r
+\r
+ # Rember network parameters\r
+ self.netblock = netblock\r
+ self.prefix = prefix\r
+\r
+ # Create the WiFi network via which nodes are connected\r
+ chan = self.add_wifi_channel()\r
+\r
+ # == Access point\r
+ # Geographical position of the access point\r
+ x=50\r
+ y=0\r
+\r
+ # the IP address of the access point is the first in the prefix\r
+ self.ip_ap = str(ipaddr.IPv4Address(self.netblock) + 1)\r
+ print "IP AP: %s " % (self.ip_ap)\r
+\r
+ # Create the ns node that will run the access point\r
+ nsnode = self.create_simulated_node(self.ip_ap, self.prefix, chan, True, x, y)\r
+ \r
+ # add the node in the collection of simulated nodes\r
+ self.nsnodes.append(nsnode)\r
+\r
+ # Run a agent application on the access point\r
+ agent = self.add_agent(nsnode)\r
+ \r
+ # add the agent application in the collection of applications\r
+ self.apps.append(agent)\r
+ \r
+ # Add nb_nodes mobile nodes in the WiFi network\r
+ for i in range(1, self.nb_nodes + 1):\r
+ # pic a random initial location\r
+ x = randint(0, self.bounds_width)\r
+ y = randint(0, self.bounds_height)\r
+\r
+ # define the appropriate IP address of the node (sequential IP in what remains after the access point IP)\r
+ ip = str(ipaddr.IPv4Address(self.ip_ap) + i)\r
+\r
+ print "IP mobile: " , ip\r
+\r
+ # Create the ns node that will run the mobile node\r
+ nsnode = self.create_simulated_node(ip, self.prefix, chan, False, x, y)\r
+ # \r
+ # add the node in the collection of simulated nodes\r
+ self.nsnodes.append(nsnode)\r
+\r
+ if target:\r
+ # Run a transmitter application on the mobile node\r
+ transmitter = self.add_transmitter(nsnode)\r
+ \r
+ # add the trasmitter application in the collection of applications\r
+ self.apps.append(transmitter)\r
+\r
+ # Add a default route via the access point\r
+ self.add_route(nsnode, "0.0.0.0", "0", self.ip_ap)\r
+\r
+ # for sanity check\r
+ self.topology_built = True\r