Replacing RM.rtype() for RM.get_type() for consistency
[nepi.git] / src / nepi / resources / planetlab / tap.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 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         reschedule_delay
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.planetlab.node import PlanetlabNode
25 from nepi.util.timefuncs import tnow, tdiffsec
26
27 import os
28 import time
29
30 # TODO: - routes!!!
31 #       - CREATE GRE - PlanetlabGRE - it only needs to set the gre and remote
32 #               properties when configuring the vif_up
33
34 PYTHON_VSYS_VERSION = "1.0"
35
36 @clsinit_copy
37 class PlanetlabTap(LinuxApplication):
38     _rtype = "PlanetlabTap"
39     _help = "Creates a TAP device on a PlanetLab host"
40     _backend = "planetlab"
41
42     @classmethod
43     def _register_attributes(cls):
44         ip4 = Attribute("ip4", "IPv4 Address",
45               flags = Flags.ExecReadOnly)
46
47         mac = Attribute("mac", "MAC Address",
48                 flags = Flags.ExecReadOnly)
49
50         prefix4 = Attribute("prefix4", "IPv4 network prefix",
51                 type = Types.Integer,
52                 flags = Flags.ExecReadOnly)
53
54         mtu = Attribute("mtu", "Maximum transmition unit for device",
55                 type = Types.Integer)
56
57         devname = Attribute("deviceName", 
58                 "Name of the network interface (e.g. eth0, wlan0, etc)",
59                 flags = Flags.ReadOnly)
60
61         up = Attribute("up", "Link up", 
62                 type = Types.Bool)
63         
64         snat = Attribute("snat", "Set SNAT=1", 
65                 type = Types.Bool,
66                 flags = Flags.ExecReadOnly)
67         
68         pointopoint = Attribute("pointopoint", "Peer IP address", 
69                 flags = Flags.ExecReadOnly)
70
71         tear_down = Attribute("tearDown", "Bash script to be executed before " + \
72                 "releasing the resource",
73                 flags = Flags.ExecReadOnly)
74
75         cls._register_attribute(ip4)
76         cls._register_attribute(mac)
77         cls._register_attribute(prefix4)
78         cls._register_attribute(mtu)
79         cls._register_attribute(devname)
80         cls._register_attribute(up)
81         cls._register_attribute(snat)
82         cls._register_attribute(pointopoint)
83         cls._register_attribute(tear_down)
84
85     def __init__(self, ec, guid):
86         super(PlanetlabTap, self).__init__(ec, guid)
87         self._home = "tap-%s" % self.guid
88
89     @property
90     def node(self):
91         node = self.get_connected(PlanetlabNode.get_rtype())
92         if node: return node[0]
93         return None
94
95     """
96     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
97         self.info("Retrieving '%s' trace %s " % (name, attr))
98
99         path = os.path.join(self.run_home, name)
100         
101         command = "(test -f %s && echo 'success') || echo 'error'" % path
102         (out, err), proc = self.node.execute(command)
103
104         if (err and proc.poll()) or out.find("error") != -1:
105             msg = " Couldn't find trace %s " % name
106             self.error(msg, out, err)
107             return None
108     
109         if attr == TraceAttr.PATH:
110             return path
111
112         if attr == TraceAttr.ALL:
113             (out, err), proc = self.node.check_output(self.run_home, name)
114             
115             if proc.poll():
116                 msg = " Couldn't read trace %s " % name
117                 self.error(msg, out, err)
118                 return None
119
120             return out
121
122         if attr == TraceAttr.STREAM:
123             cmd = "dd if=%s bs=%d count=1 skip=%d" % (path, block, offset)
124         elif attr == TraceAttr.SIZE:
125             cmd = "stat -c%%s %s " % path
126
127         (out, err), proc = self.node.execute(cmd)
128
129         if proc.poll():
130             msg = " Couldn't find trace %s " % name
131             self.error(msg, out, err)
132             return None
133         
134         if attr == TraceAttr.SIZE:
135             out = int(out.strip())
136
137         return out
138     """
139
140     def upload_sources(self):
141         # upload vif-creation python script
142         pl_vif_create = os.path.join(os.path.dirname(__file__), "scripts",
143                 "pl-vif-create.py")
144
145         self.node.upload(pl_vif_create,
146                 os.path.join(self.node.src_dir, "pl-vif-create.py"),
147                 overwrite = False)
148
149         # upload vif-stop python script
150         pl_vif_stop = os.path.join(os.path.dirname(__file__), "scripts",
151                 "pl-vif-stop.py")
152
153         self.node.upload(pl_vif_stop,
154                 os.path.join(self.node.src_dir, "pl-vif-stop.py"),
155                 overwrite = False)
156
157         # upload vif-connect python script
158         pl_vif_connect = os.path.join(os.path.dirname(__file__), "scripts",
159                 "pl-vif-udp-connect.py")
160
161         self.node.upload(pl_vif_connect,
162                 os.path.join(self.node.src_dir, "pl-vif-udp-connect.py"),
163                 overwrite = False)
164
165         # upload tun-connect python script
166         tunchannel = os.path.join(os.path.dirname(__file__), "..", "linux",
167                 "scripts", "tunchannel.py")
168
169         self.node.upload(tunchannel,
170                 os.path.join(self.node.src_dir, "tunchannel.py"),
171                 overwrite = False)
172
173         # upload stop.sh script
174         stop_command = self.replace_paths(self._stop_command)
175         self.node.upload(stop_command,
176                 os.path.join(self.app_home, "stop.sh"),
177                 text = True, 
178                 overwrite = False)
179
180     def upload_start_command(self):
181         super(PlanetlabTap, self).upload_start_command()
182
183         # We want to make sure the device is up and running
184         # before the deploy finishes (so things will be ready
185         # before other stuff starts running).
186         # Run the command as a bash script in background,
187         # in the host ( but wait until the command has
188         # finished to continue )
189         self._run_in_background()
190         
191         # Retrive if_name
192         if_name = self.wait_if_name()
193         self.set("deviceName", if_name) 
194
195     def do_deploy(self):
196         if not self.node or self.node.state < ResourceState.PROVISIONED:
197             self.ec.schedule(reschedule_delay, self.deploy)
198         else:
199             if not self.get("command"):
200                 self.set("command", self._start_command)
201
202             if not self.get("depends"):
203                 self.set("depends", self._dependencies)
204
205             if not self.get("install"):
206                 self.set("install", self._install)
207
208             self.do_discover()
209             self.do_provision()
210
211             self.debug("----- READY ---- ")
212             self.set_ready()
213
214     def do_start(self):
215         if self.state == ResourceState.READY:
216             command = self.get("command")
217             self.info("Starting command '%s'" % command)
218
219             self.set_started()
220         else:
221             msg = " Failed to execute command '%s'" % command
222             self.error(msg, out, err)
223             raise RuntimeError, msg
224
225     def do_stop(self):
226         command = self.get('command') or ''
227         
228         if self.state == ResourceState.STARTED:
229             self.info("Stopping command '%s'" % command)
230
231             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
232             (out, err), proc = self.execute_command(command,
233                     blocking = True)
234
235             self.set_stopped()
236
237     @property
238     def state(self):
239         # First check if the ccnd has failed
240         state_check_delay = 0.5
241         if self._state == ResourceState.STARTED and \
242                 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
243
244             if self.get("deviceName"):
245                 (out, err), proc = self.node.execute("ifconfig")
246
247                 if out.strip().find(self.get("deviceName")) == -1: 
248                     # tap is not running is not running (socket not found)
249                     self.finish()
250
251             self._last_state_check = tnow()
252
253         return self._state
254
255     def do_release(self):
256         # Node needs to wait until all associated RMs are released
257         # to be released
258         from nepi.resources.linux.udptunnel import UdpTunnel
259         rms = self.get_connected(UdpTunnel.get_rtype())
260         for rm in rms:
261             if rm.state < ResourceState.STOPPED:
262                 self.ec.schedule(reschedule_delay, self.release)
263                 return 
264
265         super(PlanetlabTap, self).do_release()
266
267     def wait_if_name(self):
268         """ Waits until the if_name file for the command is generated, 
269             and returns the if_name for the device """
270         if_name = None
271         delay = 1.0
272
273         for i in xrange(4):
274             (out, err), proc = self.node.check_output(self.run_home, "if_name")
275
276             if out:
277                 if_name = out.strip()
278                 break
279             else:
280                 time.sleep(delay)
281                 delay = delay * 1.5
282         else:
283             msg = "Couldn't retrieve if_name"
284             self.error(msg, out, err)
285             raise RuntimeError, msg
286
287         return if_name
288
289     def udp_connect_command(self, remote_ip, local_port_file, 
290             remote_port_file, ret_file, cipher, cipher_key,
291             bwlimit, txqueuelen):
292         command = ["sudo -S "]
293         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
294         command.append("python ${SRC}/pl-vif-udp-connect.py")
295         command.append("-t %s" % self.vif_type)
296         command.append("-S %s " % self.sock_name)
297         command.append("-l %s " % local_port_file)
298         command.append("-r %s " % remote_port_file)
299         command.append("-H %s " % remote_ip)
300         command.append("-R %s " % ret_file)
301         if cipher:
302             command.append("-c %s " % cipher)
303         if cipher_key:
304             command.append("-k %s " % cipher_key)
305         if txqueuelen:
306             command.append("-q %s " % txqueuelen)
307         if bwlimit:
308             command.append("-b %s " % bwlimit)
309
310         command = " ".join(command)
311         command = self.replace_paths(command)
312         return command
313
314     @property
315     def _start_command(self):
316         command = ["sudo -S python ${SRC}/pl-vif-create.py"]
317         
318         command.append("-t %s" % self.vif_type)
319         command.append("-a %s" % self.get("ip4"))
320         command.append("-n %d" % self.get("prefix4"))
321         command.append("-f %s " % self.if_name_file)
322         command.append("-S %s " % self.sock_name)
323         if self.get("snat") == True:
324             command.append("-s")
325         if self.get("pointopoint"):
326             command.append("-p %s" % self.get("pointopoint"))
327
328         return " ".join(command)
329
330     @property
331     def _stop_command(self):
332         command = ["sudo -S python ${SRC}/pl-vif-stop.py"]
333         
334         command.append("-S %s " % self.sock_name)
335         return " ".join(command)
336
337     @property
338     def vif_type(self):
339         return "IFF_TAP"
340
341     @property
342     def if_name_file(self):
343         return os.path.join(self.run_home, "if_name")
344
345     @property
346     def sock_name(self):
347         return os.path.join(self.run_home, "tap.sock")
348
349     @property
350     def _dependencies(self):
351         return "mercurial make gcc"
352
353     @property
354     def _install(self):
355         # Install python-vsys and python-passfd
356         install_vsys = ( " ( "
357                     "   python -c 'import vsys, os;  vsys.__version__ == \"%(version)s\" or os._exit(1)' "
358                     " ) "
359                     " || "
360                     " ( "
361                     "   cd ${SRC} ; "
362                     "   hg clone http://nepi.inria.fr/code/python-vsys ; "
363                     "   cd python-vsys ; "
364                     "   make all ; "
365                     "   sudo -S make install "
366                     " )" ) % ({
367                         "version": PYTHON_VSYS_VERSION
368                         })
369
370         install_passfd = ( " ( python -c 'import passfd' ) "
371                     " || "
372                     " ( "
373                     "   cd ${SRC} ; "
374                     "   hg clone http://nepi.inria.fr/code/python-passfd ; "
375                     "   cd python-passfd ; "
376                     "   make all ; "
377                     "   sudo -S make install "
378                     " )" )
379
380         return "%s ; %s" % ( install_vsys, install_passfd )
381
382     def valid_connection(self, guid):
383         # TODO: Validate!
384         return True
385