Adding examples/ns3/multi_host
[nepi.git] / examples / ns3 / multi_host / experiment_interconnected_ns3_planetlab.py
diff --git a/examples/ns3/multi_host/experiment_interconnected_ns3_planetlab.py b/examples/ns3/multi_host/experiment_interconnected_ns3_planetlab.py
new file mode 100644 (file)
index 0000000..01f8bcd
--- /dev/null
@@ -0,0 +1,96 @@
+#\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 experiment_interconnected import ExperimentInterconnected\r
+\r
+class ExperimentInterconnectedNs3Planetlab(ExperimentInterconnected):\r
+    def add_tap_device(self, node, ip, prefix, pointopoint):\r
+        """\r
+        Add a point-to-point tap device on the planetlab node. This tap device\r
+        is used to exchange traffic with the simulated network\r
+        """\r
+        # Create the tap device\r
+        dev = self.ec.register_resource("planetlab::Tap")\r
+\r
+        # Define the local network associated with the TAP device\r
+        self.ec.set(dev, "ip", ip)\r
+        self.ec.set(dev, "prefix", prefix)\r
+\r
+        # Define the other side of the tap (the simulated network device)\r
+        self.ec.set(dev, "pointopoint", pointopoint)\r
+\r
+        # Associate the TAP device with the Planetlab node\r
+        self.ec.register_connection(node, dev)\r
+\r
+        return dev\r
+\r
+    def connect_with_tuntap(self, tap):\r
+        """\r
+        Connect the simulated network with the Planetlab node via the TAP of\r
+        the Planetlab node and the FD device of the simulated network\r
+        """\r
+        # Create the link\r
+        crosslink = self.ec.register_resource("planetlab::ns3::TunTapFdLink")                                                                                      \r
+\r
+        # Associate it with the Planetlab tap device on one side\r
+        self.ec.register_connection(crosslink, tap)\r
+        # \r
+        # Associate it with the simulated network FD device on the other side\r
+        self.ec.register_connection(crosslink, self.fddev)\r
+\r
+        return crosslink\r
+\r
+    def interconnect(self, netblock_pl, prefix_pl):\r
+        """\r
+        Interconnect a simulated network with a Planetlab network via a tap device\r
+        """\r
+        # sanity checks\r
+        #\r
+        # topology must be setup\r
+        if not self.topology_built:\r
+            raise Exception("Topology not setup on ", self)\r
+        #\r
+        # experiment cannot be interconnected to another experiment\r
+        if self.interconnected:\r
+            raise Exception("Experiment already interconnected on ", self)\r
+       \r
+\r
+        ip_simulated = str(ipaddr.IPv4Address(netblock_pl) + 1)\r
+        ip_pl = str(ipaddr.IPv4Address(ip_simulated) + 1)\r
+\r
+        # Add a TAP interface on the Planetlab machine to connect the real\r
+        # application with the simulated network\r
+        tap = self.add_tap_device(self.node, ip_pl, prefix_pl, ip_simulated)\r
+        \r
+        # Add an FD device on the simulator to connect to the real\r
+        self.add_fdnetdevice(ip_simulated, prefix_pl)\r
+\r
+        # Connect both Planetlab and simulated network via the tap device\r
+        self.connect_with_tuntap(tap)\r
+\r
+        # Route traffic for the Planetlab node to the Planetlab machine\r
+        self.add_route(self.nsnodes[0], netblock_pl, prefix_pl, self.ip_ap)\r
+\r
+        # Route traffic for the simulated node to the simulator\r
+        self.add_vroute(tap, self.netblock, self.prefix, ip_simulated)\r
+\r
+        # Sanity check\r
+        self.interconnected = True\r
+\r