01f8bcd57b65ca28f4ddd8f7409c007531f4c89b
[nepi.git] / examples / ns3 / multi_host / experiment_interconnected_ns3_planetlab.py
1 #\r
2 #    NEPI, a framework to manage network experiments\r
3 #    Copyright (C) 2013 INRIA\r
4 #\r
5 #    This program is free software: you can redistribute it and/or modify\r
6 #    it under the terms of the GNU General Public License version 2 as\r
7 #    published by the Free Software Foundation;\r
8 #\r
9 #    This program is distributed in the hope that it will be useful,\r
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12 #    GNU General Public License for more details.\r
13 #\r
14 #    You should have received a copy of the GNU General Public License\r
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16 #\r
17 # Author: Damien Saucez <damien.saucez@inria.fr>\r
18 #         Alina Quereilhac <alina.quereilhac@inria.fr>\r
19 \r
20 import ipaddr\r
21 from experiment_interconnected import ExperimentInterconnected\r
22 \r
23 class ExperimentInterconnectedNs3Planetlab(ExperimentInterconnected):\r
24     def add_tap_device(self, node, ip, prefix, pointopoint):\r
25         """\r
26         Add a point-to-point tap device on the planetlab node. This tap device\r
27         is used to exchange traffic with the simulated network\r
28         """\r
29         # Create the tap device\r
30         dev = self.ec.register_resource("planetlab::Tap")\r
31 \r
32         # Define the local network associated with the TAP device\r
33         self.ec.set(dev, "ip", ip)\r
34         self.ec.set(dev, "prefix", prefix)\r
35 \r
36         # Define the other side of the tap (the simulated network device)\r
37         self.ec.set(dev, "pointopoint", pointopoint)\r
38 \r
39         # Associate the TAP device with the Planetlab node\r
40         self.ec.register_connection(node, dev)\r
41 \r
42         return dev\r
43 \r
44     def connect_with_tuntap(self, tap):\r
45         """\r
46         Connect the simulated network with the Planetlab node via the TAP of\r
47         the Planetlab node and the FD device of the simulated network\r
48         """\r
49         # Create the link\r
50         crosslink = self.ec.register_resource("planetlab::ns3::TunTapFdLink")                                                                                      \r
51 \r
52         # Associate it with the Planetlab tap device on one side\r
53         self.ec.register_connection(crosslink, tap)\r
54         # \r
55         # Associate it with the simulated network FD device on the other side\r
56         self.ec.register_connection(crosslink, self.fddev)\r
57 \r
58         return crosslink\r
59 \r
60     def interconnect(self, netblock_pl, prefix_pl):\r
61         """\r
62         Interconnect a simulated network with a Planetlab network via a tap device\r
63         """\r
64         # sanity checks\r
65         #\r
66         # topology must be setup\r
67         if not self.topology_built:\r
68             raise Exception("Topology not setup on ", self)\r
69         #\r
70         # experiment cannot be interconnected to another experiment\r
71         if self.interconnected:\r
72             raise Exception("Experiment already interconnected on ", self)\r
73        \r
74 \r
75         ip_simulated = str(ipaddr.IPv4Address(netblock_pl) + 1)\r
76         ip_pl = str(ipaddr.IPv4Address(ip_simulated) + 1)\r
77 \r
78         # Add a TAP interface on the Planetlab machine to connect the real\r
79         # application with the simulated network\r
80         tap = self.add_tap_device(self.node, ip_pl, prefix_pl, ip_simulated)\r
81         \r
82         # Add an FD device on the simulator to connect to the real\r
83         self.add_fdnetdevice(ip_simulated, prefix_pl)\r
84 \r
85         # Connect both Planetlab and simulated network via the tap device\r
86         self.connect_with_tuntap(tap)\r
87 \r
88         # Route traffic for the Planetlab node to the Planetlab machine\r
89         self.add_route(self.nsnodes[0], netblock_pl, prefix_pl, self.ip_ap)\r
90 \r
91         # Route traffic for the simulated node to the simulator\r
92         self.add_vroute(tap, self.netblock, self.prefix, ip_simulated)\r
93 \r
94         # Sanity check\r
95         self.interconnected = True\r
96 \r