Bug fixing and ordering openvswitch code
[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 from nepi.resources.linux.application import LinuxApplication
23 from nepi.resources.linux.node import LinuxNode
24 from nepi.util.timefuncs import tnow, tdiffsec
25
26 import os
27 import time
28
29 PYTHON_VSYS_VERSION = "1.0"
30
31 @clsinit_copy
32 class LinuxTap(LinuxApplication):
33     _rtype = "linux::Tap"
34     _help = "Creates a TAP device on a Linux host"
35
36     IFF_TUN = 0x0001
37     IFF_TAP = 0x0002
38
39     @classmethod
40     def _register_attributes(cls):
41         ip = Attribute("ip", "IPv4 Address",
42               flags = Flags.Design)
43
44         mac = Attribute("mac", "MAC Address",
45                 flags = Flags.Design)
46
47         prefix = Attribute("prefix", "IPv4 network prefix",
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(ip)
87         cls._register_attribute(mac)
88         cls._register_attribute(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, "linux::TAP/TUN devices must be connected to a linux::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(self.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("ip a")
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(self.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 to the IP of the device
336         self.set("pointopoint", remote_endpoint.get("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         # Public IP of the remote NODE to stablish tunnel
344         remote_ip = remote_endpoint.node.get("ip")
345
346         local_port_file = os.path.join(self.run_home, 
347                 "local_port")
348
349         remote_port_file = os.path.join(self.run_home, 
350                 "remote_port")
351
352         ret_file = os.path.join(self.run_home, 
353                 "ret_file")
354
355         # Generate UDP connect command
356         # Use the start command to configure TAP with peer info
357         start_command = self._start_command
358         
359         command = ["( "]
360         command.append(start_command)
361
362         # Use pl-vid-udp-connect.py to stablish the tunnel between endpoints
363         command.append(") & (")
364         command.append("sudo -S")
365         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
366         command.append("python ${SRC}/linux-udp-connect.py")
367         command.append("-N %s" % self.get("deviceName"))
368         command.append("-t %s" % self.vif_type)
369         if self.get("pi"):
370             command.append("-p")
371         command.append("-l %s " % local_port_file)
372         command.append("-r %s " % remote_port_file)
373         command.append("-H %s " % remote_ip)
374         command.append("-R %s " % ret_file)
375         if cipher:
376             command.append("-c %s " % cipher)
377         if cipher_key:
378             command.append("-k %s " % cipher_key)
379         if txqueuelen:
380             command.append("-q %s " % txqueuelen)
381         if bwlimit:
382             command.append("-b %s " % bwlimit)
383
384         command.append(")")
385
386         command = " ".join(command)
387         command = self.replace_paths(command)
388
389         return command
390
391     def _gre_connect_command(self, remote_endpoint, connection_run_home): 
392         # Set the remote endpoint to (private) device IP
393         self.set("pointopoint", remote_endpoint.get("ip"))
394         ## public node IP
395         self.set("greRemote", remote_endpoint.node.get("ip"))
396
397         # Generate GRE connect command
398         command = ["("]
399         command.append(self._stop_command)
400         command.append(") ; (")
401         command.append(self._start_gre_command)
402         command.append(")")
403
404         command = " ".join(command)
405         command = self.replace_paths(command)
406
407         return command
408
409     def establish_udp_connection(self, remote_endpoint,
410             connection_app_home,
411             connection_run_home, 
412             port):
413         # upload remote port number to file
414         rem_port = "%s\n" % port
415         self.node.upload(rem_port,
416                 os.path.join(self.run_home, "remote_port"),
417                 text = True, 
418                 overwrite = False)
419
420     def verify_connection(self):
421         self.wait_result()
422
423     def terminate_connection(self):
424         if  self._pid and self._ppid:
425             (out, err), proc = self.node.kill(self._pid, self._ppid, 
426                     sudo = True) 
427
428             # check if execution errors occurred
429             if proc.poll() and err:
430                 msg = " Failed to Kill the Tap"
431                 self.error(msg, out, err)
432                 raise RuntimeError, msg
433
434     def check_status(self):
435         return self.node.status(self._pid, self._ppid)
436
437     def wait_local_port(self):
438         """ Waits until the local_port file for the endpoint is generated, 
439         and returns the port number 
440         
441         """
442         return self.wait_file("local_port")
443
444     def wait_result(self):
445         """ Waits until the return code file for the endpoint is generated 
446         
447         """ 
448         return self.wait_file("ret_file")
449  
450     def wait_file(self, filename):
451         """ Waits until file on endpoint is generated """
452         result = None
453         delay = 1.0
454
455         for i in xrange(20):
456             (out, err), proc = self.node.check_output(
457                     self.run_home, filename)
458             if out:
459                 result = out.strip()
460                 break
461             else:
462                 time.sleep(delay)
463                 delay = delay * 1.5
464         else:
465             msg = "Couldn't retrieve %s" % filename
466             self.error(msg, out, err)
467             raise RuntimeError, msg
468
469         return result
470
471     @property
472     def _start_command(self):
473         command = []
474         if not self.gre_enabled:
475             # Make sure to clean TAP if it existed
476             stop_command = self._stop_command
477             
478             start_command = []
479             start_command.append("sudo -S ip tuntap add %s mode %s %s" % (
480                 self.get("deviceName"),
481                 self.vif_prefix,
482                 "pi" if self.get("pi") else ""))
483             start_command.append("sudo -S ip link set %s up" % self.get("deviceName"))
484             start_command.append("sudo -S ip addr add %s/%s dev %s" % (
485                 self.get("ip"),
486                 self.get("prefix"),
487                 self.get("deviceName"),
488                 ))
489
490             start_command = ";".join(start_command)
491
492             command.append("(")
493             command.append(stop_command)
494             command.append(") ; (")
495             command.append(start_command)
496             command.append(")")
497
498         return " ".join(command)
499
500     @property
501     def _stop_command(self):
502         command = []
503         command.append("sudo -S ip link set %s down" % self.get("deviceName"))
504         command.append("sudo -S ip link del %s" % self.get("deviceName"))
505         
506         return ";".join(command)
507
508     @property
509     def _start_gre_command(self):
510         command = []
511         command.append("sudo -S modprobe ip_gre")
512         command.append("sudo -S ip link add %s type gre remote %s local %s ttl 64 csum key %s" % (
513                 self.get("deviceName"),
514                 self.get("greRemote"),
515                 self.node.get("ip"),
516                 self.get("greKey")
517             ))
518         command.append("sudo -S ip addr add %s/%s peer %s/%s dev %s" % (
519                 self.get("ip"),
520                 self.get("prefix"),
521                 self.get("pointopoint"),
522                 self.get("prefix"),
523                 self.get("deviceName"),
524                 ))
525         command.append("sudo -S ip link set %s up " % self.get("deviceName"))
526
527         return ";".join(command)
528
529     @property
530     def vif_type(self):
531         return "IFF_TAP"
532
533     @property
534     def vif_type_flag(self):
535         return LinuxTap.IFF_TAP
536  
537     @property
538     def vif_prefix(self):
539         return "tap"
540
541     def sock_name(self):
542         return os.path.join(self.run_home, "tap.sock")
543
544     def valid_connection(self, guid):
545         # TODO: Validate!
546         return True
547