Fixing GRE tunnel between localhost and remote host
[nepi.git] / src / nepi / resources / linux / 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.linux.node import LinuxNode
25 from nepi.util.timefuncs import tnow, tdiffsec
26
27 import os
28 import socket
29 import time
30
31 PYTHON_VSYS_VERSION = "1.0"
32
33 @clsinit_copy
34 class LinuxTap(LinuxApplication):
35     _rtype = "LinuxTap"
36     _help = "Creates a TAP device on a Linux host"
37     _backend = "linux"
38
39     @classmethod
40     def _register_attributes(cls):
41         ip4 = Attribute("ip4", "IPv4 Address",
42               flags = Flags.Design)
43
44         mac = Attribute("mac", "MAC Address",
45                 flags = Flags.Design)
46
47         prefix4 = Attribute("prefix4", "IPv4 network prefix",
48                 type = Types.Integer,
49                 flags = Flags.Design)
50
51         mtu = Attribute("mtu", "Maximum transmition unit for device",
52                 type = Types.Integer)
53
54         devname = Attribute("deviceName", 
55                 "Name of the network interface (e.g. eth0, wlan0, etc)",
56                 flags = Flags.NoWrite)
57
58         up = Attribute("up", "Link up", 
59                 type = Types.Bool)
60         
61         pointopoint = Attribute("pointopoint", "Peer IP address", 
62                 flags = Flags.Design)
63
64         txqueuelen = Attribute("txqueuelen", "Length of transmission queue", 
65                 flags = Flags.Design)
66
67         txqueuelen = Attribute("txqueuelen", "Length of transmission queue", 
68                 flags = Flags.Design)
69
70         gre_key = Attribute("greKey", 
71                 "GRE key to be used to configure GRE tunnel", 
72                 default = "1",
73                 flags = Flags.Design)
74
75         gre_remote = Attribute("greRemote", 
76                 "Public IP of remote endpoint for GRE tunnel", 
77                 flags = Flags.Design)
78
79         pi = Attribute("pi", "Add PI (protocol information) header", 
80                 default = False,
81                 type = Types.Bool)
82  
83         tear_down = Attribute("tearDown", 
84                 "Bash script to be executed before releasing the resource",
85                 flags = Flags.Design)
86
87         cls._register_attribute(ip4)
88         cls._register_attribute(mac)
89         cls._register_attribute(prefix4)
90         cls._register_attribute(mtu)
91         cls._register_attribute(devname)
92         cls._register_attribute(up)
93         cls._register_attribute(pointopoint)
94         cls._register_attribute(txqueuelen)
95         cls._register_attribute(gre_key)
96         cls._register_attribute(gre_remote)
97         cls._register_attribute(pi)
98         cls._register_attribute(tear_down)
99
100     def __init__(self, ec, guid):
101         super(LinuxTap, self).__init__(ec, guid)
102         self._home = "tap-%s" % self.guid
103         self._gre_enabled = False
104         self._tunnel_mode = False
105
106     @property
107     def node(self):
108         node = self.get_connected(LinuxNode.get_rtype())
109         if node: return node[0]
110         raise RuntimeError, "TAP/TUN devices must be connected to Node"
111
112     @property
113     def gre_enabled(self):
114         if not self._gre_enabled:
115             from nepi.resources.linux.gretunnel import LinuxGRETunnel
116             gre = self.get_connected(LinuxGRETunnel.get_rtype())
117             if gre: self._gre_enabled = True
118
119         return self._gre_enabled
120
121     @property
122     def tunnel_mode(self):
123         if not self._tunnel_mode:
124             from nepi.resources.linux.tunnel import LinuxTunnel
125             tunnel = self.get_connected(LinuxTunnel.get_rtype())
126             if tunnel: self._tunnel_mode = True
127
128         return self._tunnel_mode
129
130     def upload_sources(self):
131         scripts = []
132
133         # udp-connect python script
134         udp_connect = os.path.join(os.path.dirname(__file__), "scripts",
135                 "linux-udp-connect.py")
136         
137         scripts.append(udp_connect)
138
139         # tunnel creation python script
140         tunchannel = os.path.join(os.path.dirname(__file__), "scripts", 
141                 "tunchannel.py")
142
143         scripts.append(tunchannel)
144
145         # Upload scripts
146         scripts = ";".join(scripts)
147
148         self.node.upload(scripts,
149                 os.path.join(self.node.src_dir),
150                 overwrite = False)
151
152         # upload stop.sh script
153         stop_command = self.replace_paths(self._stop_command)
154
155         self.node.upload(stop_command,
156                 os.path.join(self.app_home, "stop.sh"),
157                 text = True,
158                 # Overwrite file every time. 
159                 # The stop.sh has the path to the socket, which should change
160                 # on every experiment run.
161                 overwrite = True)
162
163     def upload_start_command(self):
164         # If GRE mode is enabled, TAP creation is delayed until the
165         # tunnel is established
166         if not self.tunnel_mode:
167             # We want to make sure the device is up and running
168             # before the deploy is over, so we execute the 
169             # start script now and wait until it finishes. 
170             command = self.get("command")
171             command = self.replace_paths(command)
172
173             shfile = os.path.join(self.app_home, "start.sh")
174             self.node.run_and_wait(command, self.run_home,
175                 shfile = shfile,
176                 overwrite = True)
177
178     def do_deploy(self):
179         if not self.node or self.node.state < ResourceState.PROVISIONED:
180             self.ec.schedule(reschedule_delay, self.deploy)
181         else:
182             if not self.get("deviceName"):
183                 self.set("deviceName", "%s%d" % (self.vif_prefix, self.guid)) 
184
185             if not self.get("command"):
186                 self.set("command", self._start_command)
187
188             self.do_discover()
189             self.do_provision()
190
191             self.set_ready()
192
193     def do_start(self):
194         if self.state == ResourceState.READY:
195             command = self.get("command")
196             self.info("Starting command '%s'" % command)
197
198             self.set_started()
199         else:
200             msg = " Failed to execute command '%s'" % command
201             self.error(msg, out, err)
202             raise RuntimeError, msg
203
204     def do_stop(self):
205         command = self.get('command') or ''
206         
207         if self.state == ResourceState.STARTED:
208             self.info("Stopping command '%s'" % command)
209
210             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
211             (out, err), proc = self.execute_command(command,
212                     blocking = True)
213
214             if err:
215                 msg = " Failed to stop command '%s' " % command
216                 self.error(msg, out, err)
217
218             self.set_stopped()
219
220     @property
221     def state(self):
222         state_check_delay = 0.5
223         if self._state == ResourceState.STARTED and \
224                 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
225
226             if self.get("deviceName"):
227                 (out, err), proc = self.node.execute("ifconfig")
228
229                 if out.strip().find(self.get("deviceName")) == -1: 
230                     # tap is not running is not running (socket not found)
231                     self.set_stopped()
232
233             self._last_state_check = tnow()
234
235         return self._state
236
237     def do_release(self):
238         # Node needs to wait until all associated RMs are released
239         # to be released
240         from nepi.resources.linux.tunnel import LinuxTunnel
241         rms = self.get_connected(LinuxTunnel.get_rtype())
242
243         for rm in rms:
244             if rm.state < ResourceState.STOPPED:
245                 self.ec.schedule(reschedule_delay, self.release)
246                 return 
247
248         super(LinuxTap, self).do_release()
249
250     def gre_connect(self, remote_endpoint, connection_app_home,
251             connection_run_home):
252         gre_connect_command = self._gre_connect_command(
253                 remote_endpoint, connection_run_home)
254
255         # upload command to connect.sh script
256         shfile = os.path.join(connection_app_home, "gre-connect.sh")
257         self.node.upload_command(gre_connect_command,
258                 shfile = shfile,
259                 overwrite = False)
260
261         # invoke connect script
262         cmd = "bash %s" % shfile
263         (out, err), proc = self.node.run(cmd, connection_run_home)
264              
265         # check if execution errors occurred
266         msg = " Failed to connect endpoints "
267         
268         if proc.poll() or err:
269             self.error(msg, out, err)
270             raise RuntimeError, msg
271     
272         # Wait for pid file to be generated
273         pid, ppid = self.node.wait_pid(connection_run_home)
274         
275         # If the process is not running, check for error information
276         # on the remote machine
277         if not pid or not ppid:
278             (out, err), proc = self.node.check_errors(connection_run_home)
279             # Out is what was written in the stderr file
280             if err:
281                 msg = " Failed to start command '%s' " % command
282                 self.error(msg, out, err)
283                 raise RuntimeError, msg
284         
285         return True
286
287     ## XXX: NOT REALLY WORKING YET!
288     def udp_connect(self, remote_endpoint, connection_app_home, 
289             connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
290         udp_connect_command = self._udp_connect_command(
291                 remote_endpoint, connection_run_home,
292                 cipher, cipher_key, bwlimit, txqueuelen)
293
294         # upload command to connect.sh script
295         shfile = os.path.join(connection_app_home, "udp-connect.sh")
296         self.node.upload_command(udp_connect_command,
297                 shfile = shfile,
298                 overwrite = False)
299
300         # invoke connect script
301         cmd = "bash %s" % shfile
302         (out, err), proc = self.node.run(cmd, connection_run_home) 
303              
304         # check if execution errors occurred
305         msg = "Failed to connect endpoints "
306         
307         if proc.poll():
308             self.error(msg, out, err)
309             raise RuntimeError, msg
310     
311         # Wait for pid file to be generated
312         pid, ppid = self.node.wait_pid(connection_run_home)
313         
314         # If the process is not running, check for error information
315         # on the remote machine
316         if not pid or not ppid:
317             (out, err), proc = self.node.check_errors(connection_run_home)
318             # Out is what was written in the stderr file
319             if err:
320                 msg = " Failed to start command '%s' " % command
321                 self.error(msg, out, err)
322                 raise RuntimeError, msg
323
324         return pid, ppid
325
326     def _udp_connect_command(self, remote_endpoint, connection_run_home, 
327             cipher, cipher_key, bwlimit, txqueuelen):
328
329         # Set the remote endpoint
330         self.set("pointopoint", remote_endpoint.get("ip4"))
331         
332         # Planetlab TAPs always use PI headers
333         from nepi.resources.planetlab.tap import PlanetlabTap
334         if self.is_rm_instance(PlanetlabTap.get_rtype()):
335             self.set("pi", True)
336
337         remote_ip = socket.gethostbyname(
338                 remote_endpoint.node.get("ip"))
339
340         local_port_file = os.path.join(connection_run_home, 
341                 "local_port")
342
343         remote_port_file = os.path.join(connection_run_home, 
344                 "remote_port")
345
346         ret_file = os.path.join(connection_run_home, 
347                 "ret_file")
348
349         # Generate UDP connect command
350         # Use the start command to configure TAP with peer info
351         start_command = self._start_command
352         
353         command = ["( "]
354         command.append(start_command)
355
356         # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
357         command.append(") & (")
358         command.append("sudo -S")
359         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
360         command.append("python ${SRC}/linux-udp-connect.py")
361         command.append("-N %s" % self.get("deviceName"))
362         command.append("-t %s" % self.vif_type)
363         if self.get("pi"):
364             command.append("-p")
365         command.append("-l %s " % local_port_file)
366         command.append("-r %s " % remote_port_file)
367         command.append("-H %s " % remote_ip)
368         command.append("-R %s " % ret_file)
369         if cipher:
370             command.append("-c %s " % cipher)
371         if cipher_key:
372             command.append("-k %s " % cipher_key)
373         if txqueuelen:
374             command.append("-q %s " % txqueuelen)
375         if bwlimit:
376             command.append("-b %s " % bwlimit)
377
378         command.append(")")
379
380         command = " ".join(command)
381         command = self.replace_paths(command)
382
383         return command
384
385     def _gre_connect_command(self, remote_endpoint, connection_run_home): 
386         # Set the remote endpoint
387         self.set("pointopoint", remote_endpoint.get("ip4"))
388         self.set("greRemote", socket.gethostbyname(
389             remote_endpoint.node.get("ip")))
390
391         # Generate GRE connect command
392         command = ["("]
393         command.append(self._stop_command)
394         command.append(") ; (")
395         command.append(self._start_gre_command)
396         command.append(")")
397
398         command = " ".join(command)
399         command = self.replace_paths(command)
400
401         return command
402
403     @property
404     def _start_command(self):
405         command = []
406         if not self.gre_enabled:
407             # Make sure to clean TAP if it existed
408             stop_command = self._stop_command
409             
410             start_command = []
411             start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
412                 self.get("deviceName"),
413                 self.vif_prefix,
414                 "pi" if self.get("pi") else ""))
415             start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
416             start_command.append("sudo -S ip addr add %s/%d dev %s" % (
417                 self.get("ip4"),
418                 self.get("prefix4"),
419                 self.get("deviceName"),
420                 ))
421
422             start_command = ";".join(start_command)
423
424             command.append("(")
425             command.append(stop_command)
426             command.append(") ; (")
427             command.append(start_command)
428             command.append(")")
429
430         return " ".join(command)
431
432     @property
433     def _stop_command(self):
434         command = []
435         command.append("sudo -S ip link set %s down" % self.get("deviceName"))
436         command.append("sudo -S ip link del %s" % self.get("deviceName"))
437         
438         return ";".join(command)
439
440     @property
441     def _start_gre_command(self):
442         command = []
443         command.append("sudo -S modprobe ip_gre")
444         command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
445                 self.get("deviceName"),
446                 self.get("greRemote"),
447                 socket.gethostbyname(self.node.get("hostname")),
448                 self.get("greKey")
449             ))
450         command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
451                 self.get("ip4"),
452                 self.get("prefix4"),
453                 self.get("pointopoint"),
454                 self.get("prefix4"),
455                 self.get("deviceName"),
456                 ))
457         command.append("sudo -S ip link set %s up " % self.get("deviceName"))
458
459         return ";".join(command)
460
461     @property
462     def vif_type(self):
463         return "IFF_TAP"
464
465     @property
466     def vif_prefix(self):
467         return "tap"
468
469     def sock_name(self):
470         return os.path.join(self.run_home, "tap.sock")
471
472     def valid_connection(self, guid):
473         # TODO: Validate!
474         return True
475