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