Simple cross ping experiment between linux and ns3
[nepi.git] / src / nepi / resources / linux / route.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy, ResourceState
22 from nepi.resources.linux.application import LinuxApplication
23
24 import os
25
26 @clsinit_copy
27 class LinuxRoute(LinuxApplication):
28     _rtype = "linux::Route"
29     _help = "Adds a route to the host using iptools "
30     _backend = "linux"
31
32     @classmethod
33     def _register_attributes(cls):
34         network = Attribute("network", "Network address", flags=Flags.Design)
35         prefix = Attribute("prefix", "IP prefix length", flags=Flags.Design)
36         nexthop = Attribute("nexthop", "Nexthop IP", flags=Flags.Design)
37
38         cls._register_attribute(network)
39         cls._register_attribute(prefix)
40         cls._register_attribute(nexthop)
41
42     def __init__(self, ec, guid):
43         super(LinuxRoute, self).__init__(ec, guid)
44         self._home = "route-%s" % self.guid
45         self._device = None
46
47     @property
48     def device(self):
49         if not self._device:
50             from nepi.resources.linux.tap import LinuxTap
51             from nepi.resources.linux.tun import LinuxTun
52             from nepi.resources.linux.interface import LinuxInterface
53             tap = self.get_connected(LinuxTap.get_rtype())
54             tun = self.get_connected(LinuxTun.get_rtype())
55             interface = self.get_connected(LinuxInterface.get_rtype())
56             if tap: self._device = tap[0]
57             elif tun: self._device = tun[0]
58             elif interface: self._device = interface[0]
59             else:
60                 raise RuntimeError, "linux::Routes must be connected to a "\
61                         "linux::TAP, linux::TUN, or linux::Interface"
62         return self._device
63
64     @property
65     def node(self):
66         return self.device.node
67
68     def upload_start_command(self):
69         # We want to make sure the route is configured
70         # before the deploy is over, so we execute the 
71         # start script now and wait until it finishes. 
72         command = self.get("command")
73         command = self.replace_paths(command)
74
75         shfile = os.path.join(self.app_home, "start.sh")
76         self.node.run_and_wait(command, self.run_home,
77             shfile = shfile,
78             overwrite = True)
79
80     def upload_sources(self):
81         # upload stop.sh script
82         stop_command = self.replace_paths(self._stop_command)
83
84         self.node.upload(stop_command,
85                 os.path.join(self.app_home, "stop.sh"),
86                 text = True,
87                 # Overwrite file every time. 
88                 # The stop.sh has the path to the socket, which should change
89                 # on every experiment run.
90                 overwrite = True)
91
92     def do_deploy(self):
93         if not self.device or self.device.state < ResourceState.PROVISIONED:
94             self.ec.schedule(self.reschedule_delay, self.deploy)
95         else:
96             if not self.get("command"):
97                 self.set("command", self._start_command)
98
99             self.do_discover()
100             self.do_provision()
101
102             self.set_ready()
103
104     def do_start(self):
105         if self.state == ResourceState.READY:
106             command = self.get("command")
107             self.info("Starting command '%s'" % command)
108
109             self.set_started()
110         else:
111             msg = " Failed to execute command '%s'" % command
112             self.error(msg, out, err)
113             raise RuntimeError, msg
114
115     def do_stop(self):
116         command = self.get('command') or ''
117         
118         if self.state == ResourceState.STARTED:
119             self.info("Stopping command '%s'" % command)
120
121             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
122             (out, err), proc = self.execute_command(command,
123                     blocking = True)
124
125             if err:
126                 msg = " Failed to stop command '%s' " % command
127                 self.error(msg, out, err)
128
129             self.set_stopped()
130
131     @property
132     def _start_command(self):
133         network = self.get("network")
134         prefix = self.get("prefix")
135         nexthop = self.get("nexthop")
136         devicename = self.device.get("deviceName")
137
138         command = []
139         command.append("sudo -S ip route add %s/%s %s dev %s" % (
140             self.get("network"),
141             self.get("prefix"),
142             "default" if not nexthop else "via %s" % nexthop,
143             devicename))
144
145         return " ".join(command)
146
147     @property
148     def _stop_command(self):
149         network = self.get("network")
150         prefix = self.get("prefix")
151         nexthop = self.get("nexthop")
152         devicename = self.device.get("deviceName")
153
154         command = []
155         command.append("sudo -S ip route del %s/%s %s dev %s" % (
156             self.get("network"),
157             self.get("prefix"),
158             "default" if not nexthop else "via %s" % nexthop,
159             devicename))
160
161         return " ".join(command)
162