Progress with LinuxTAP and UDP connections
[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         return None
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(gre_connect_command,
258                 shfile,
259                 text = True, 
260                 overwrite = False)
261
262         # invoke connect script
263         cmd = "bash %s" % shfile
264         (out, err), proc = self.node.run(cmd, connection_run_home)
265              
266         # check if execution errors occurred
267         msg = " Failed to connect endpoints "
268         
269         if proc.poll() or err:
270             self.error(msg, out, err)
271             raise RuntimeError, msg
272     
273         # Wait for pid file to be generated
274         pid, ppid = self.node.wait_pid(connection_run_home)
275         
276         # If the process is not running, check for error information
277         # on the remote machine
278         if not pid or not ppid:
279             (out, err), proc = self.node.check_errors(connection_run_home)
280             # Out is what was written in the stderr file
281             if err:
282                 msg = " Failed to start command '%s' " % command
283                 self.error(msg, out, err)
284                 raise RuntimeError, msg
285         
286         return True
287
288     ## XXX: NOT REALLY WORKING YET!
289     def udp_connect(self, remote_endpoint, connection_app_home, 
290             connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
291         udp_connect_command = self._udp_connect_command(
292                 remote_endpoint, connection_run_home,
293                 cipher, cipher_key, bwlimit, txqueuelen)
294
295         # upload command to connect.sh script
296         shfile = os.path.join(connection_app_home, "udp-connect.sh")
297         self.node.upload(udp_connect_command,
298                 shfile,
299                 text = True, 
300                 overwrite = False)
301
302         # invoke connect script
303         cmd = "bash %s" % shfile
304         (out, err), proc = self.node.run(cmd, connection_run_home) 
305              
306         # check if execution errors occurred
307         msg = "Failed to connect endpoints "
308         
309         if proc.poll():
310             self.error(msg, out, err)
311             raise RuntimeError, msg
312     
313         # Wait for pid file to be generated
314         pid, ppid = self.node.wait_pid(connection_run_home)
315         
316         # If the process is not running, check for error information
317         # on the remote machine
318         if not pid or not ppid:
319             (out, err), proc = self.node.check_errors(connection_run_home)
320             # Out is what was written in the stderr file
321             if err:
322                 msg = " Failed to start command '%s' " % command
323                 self.error(msg, out, err)
324                 raise RuntimeError, msg
325
326         return pid, ppid
327
328     def _udp_connect_command(self, remote_endpoint, connection_run_home, 
329             cipher, cipher_key, bwlimit, txqueuelen):
330
331         # Set the remote endpoint
332         self.set("pointopoint", remote_endpoint.get("ip4"))
333         
334         # Planetlab TAPs always use PI headers
335         from nepi.resources.planetlab.tap import PlanetlabTap
336         if self.is_rm_instance(PlanetlabTap.get_rtype()):
337             self.set("pi", True)
338
339         remote_ip = socket.gethostbyname(
340                 remote_endpoint.node.get("hostname"))
341
342         local_port_file = os.path.join(connection_run_home, 
343                 "local_port")
344
345         remote_port_file = os.path.join(connection_run_home, 
346                 "remote_port")
347
348         ret_file = os.path.join(connection_run_home, 
349                 "ret_file")
350
351         # Generate UDP connect command
352         # Use the start command to configure TAP with peer info
353         start_command = self._start_command
354         
355         command = ["( "]
356         command.append(start_command)
357
358         # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
359         command.append(") & (")
360         command.append("sudo -S")
361         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
362         command.append("python ${SRC}/linux-udp-connect.py")
363         command.append("-N %s" % self.get("deviceName"))
364         command.append("-t %s" % self.vif_type)
365         if self.get("pi"):
366             command.append("-p")
367         command.append("-l %s " % local_port_file)
368         command.append("-r %s " % remote_port_file)
369         command.append("-H %s " % remote_ip)
370         command.append("-R %s " % ret_file)
371         if cipher:
372             command.append("-c %s " % cipher)
373         if cipher_key:
374             command.append("-k %s " % cipher_key)
375         if txqueuelen:
376             command.append("-q %s " % txqueuelen)
377         if bwlimit:
378             command.append("-b %s " % bwlimit)
379
380         command.append(")")
381
382         command = " ".join(command)
383         command = self.replace_paths(command)
384
385         return command
386
387     def _gre_connect_command(self, remote_endpoint, connection_run_home): 
388         # Set the remote endpoint
389         self.set("pointopoint", remote_endpoint.get("ip4"))
390         self.set("greRemote", socket.gethostbyname(
391             remote_endpoint.node.get("hostname")))
392
393         # Generate GRE connect command
394         command = ["("]
395         command.append(self._stop_command)
396         command.append(") ; (")
397         command.append(self._start_gre_command)
398         command.append(")")
399
400         command = " ".join(command)
401         command = self.replace_paths(command)
402
403         return command
404
405     @property
406     def _start_command(self):
407         command = []
408         if not self.gre_enabled:
409             # Make sure to clean TAP if it existed
410             stop_command = self._stop_command
411             
412             start_command = []
413             start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
414                 self.get("deviceName"),
415                 self.vif_prefix,
416                 "pi" if self.get("pi") else ""))
417             start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
418             start_command.append("sudo -S ip addr add %s/%d dev %s" % (
419                 self.get("ip4"),
420                 self.get("prefix4"),
421                 self.get("deviceName"),
422                 ))
423
424             start_command = ";".join(start_command)
425
426             command.append("(")
427             command.append(stop_command)
428             command.append(") ; (")
429             command.append(start_command)
430             command.append(")")
431
432         return " ".join(command)
433
434     @property
435     def _stop_command(self):
436         command = []
437         command.append("sudo -S ip link set %s down" % self.get("deviceName"))
438         command.append("sudo -S ip link del %s" % self.get("deviceName"))
439         
440         return ";".join(command)
441
442     @property
443     def _start_gre_command(self):
444         command = []
445         command.append("sudo -S modprobe ip_gre")
446         command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
447                 self.get("deviceName"),
448                 self.get("greRemote"),
449                 socket.gethostbyname(self.node.get("hostname")),
450                 self.get("greKey")
451             ))
452         command.append("sudo -S ip addr add %s/%d peer %s/%d dev %s" % (
453                 self.get("ip4"),
454                 self.get("prefix4"),
455                 self.get("pointopoint"),
456                 self.get("prefix4"),
457                 self.get("deviceName"),
458                 ))
459         command.append("sudo -S ip link set %s up " % self.get("deviceName"))
460
461         return ";".join(command)
462
463     @property
464     def vif_type(self):
465         return "IFF_TAP"
466
467     @property
468     def vif_prefix(self):
469         return "tap"
470
471     def sock_name(self):
472         return os.path.join(self.run_home, "tap.sock")
473
474     def valid_connection(self, guid):
475         # TODO: Validate!
476         return True
477