5a3a77b438669ca96b6da30bd3dabd9815413c01
[nepi.git] / src / nepi / resources / linux / ns3 / tun_tap_fd_link.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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 from nepi.execution.resource import ResourceState, clsinit_copy
20 from nepi.resources.linux.application import LinuxApplication
21
22 import base64
23 import fcntl
24 import os
25 import socket
26 import struct
27
28 @clsinit_copy
29 class LinuxTunTapFdLink(LinuxApplication):
30     """ Interconnects a TAP or TUN Linux device to a FdNetDevice
31     """
32     _rtype = "linux::ns3::TunTapFdLink"
33
34     def __init__(self, ec, guid):
35         super(LinuxTunTapFdLink, self).__init__(ec, guid)
36         self._tap = None
37         self._fdnetdevice = None
38         self._fd_node = None
39         self.send_address = None
40         self._home = "tuntap-link-%s" % self.guid
41
42     @property
43     def fdnetdevice(self):
44         if not self._fdnetdevice:
45             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
46             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
47             if not devices or len(devices) != 1: 
48                 msg = "linux::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
49                 self.error(msg)
50                 raise RuntimeError, msg
51
52             self._fdnetdevice = devices[0]
53         
54             simu = self._fdnetdevice.simulation
55             from nepi.resources.linux.node import LinuxNode
56             nodes = simu.get_connected(LinuxNode.get_rtype())
57             self._fd_node = nodes[0]
58         
59         return self._fdnetdevice
60
61     @property
62     def fdnode(self):
63         return self._fd_node
64
65     @property
66     def tap(self):
67         if not self._tap:
68             from nepi.resources.linux.tap import LinuxTap
69             devices = self.get_connected(LinuxTap.get_rtype())
70             if not devices or len(devices) != 1: 
71                 msg = "linux::ns3::TunTapLink must be connected to exactly one LinuxTap"
72                 self.error(msg)
73                 raise RuntimeError, msg
74
75             self._tap = devices[0]
76         
77         return self._tap
78
79     @property
80     def tapnode(self):
81         return self.tap.node
82
83     @property
84     def node(self):
85         return self.tapnode
86
87     def upload_sources(self):
88         scripts = []
89
90         # vif-passfd python script
91         linux_passfd = os.path.join(os.path.dirname(__file__),
92                 "..",
93                 "scripts",
94                 "linux-passfd.py")
95
96         scripts.append(linux_passfd)
97         
98         # Upload scripts
99         scripts = ";".join(scripts)
100
101         self.node.upload(scripts,
102                 os.path.join(self.node.src_dir),
103                 overwrite = False)
104
105     def upload_start_command(self):
106         if self.tapnode.get("hostname") != \
107                 self.fdnode.get("hostname"):
108             msg = "Tap and FdNetDevice are not in the same host"
109             self.error(msg)
110             raise RuntimeError, msg
111
112         self.send_address = self.fdnetdevice.recv_fd()
113         self.set("command", self._start_command)
114
115         command = self.get("command")
116
117         shfile = os.path.join(self.app_home, "start.sh")
118         self.node.run_and_wait(command, self.run_home,
119                 shfile=shfile,
120                 wait_run=False,
121                 overwrite=True)
122         
123     def do_deploy(self):
124         if self.tap.state < ResourceState.READY or \
125                 self.fdnetdevice.state < ResourceState.READY:
126             self.ec.schedule(self.reschedule_delay, self.deploy)
127         else:
128             self.do_discover()
129             self.do_provision()
130
131             self.set_ready()
132
133     def do_start(self):
134         if self.state == ResourceState.READY:
135             command = self.get("command")
136             self.info("Starting command '%s'" % command)
137
138             self.set_started()
139         else:
140             msg = " Failed to execute command '%s'" % command
141             self.error(msg, out, err)
142             raise RuntimeError, msg
143
144     @property
145     def _start_command(self):
146         vif_name = self.ec.get(self.tap.guid, "deviceName")
147         vif_type = self.tap.vif_type_flag
148         pi = self.ec.get(self.tap.guid, "pi")
149         address = base64.b64encode(self.send_address)
150
151         command = []
152         # Use pl-vif-passfd.py to send fd from TAP to FdNetDevice
153         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
154         command.append("python ${SRC}/linux-passfd.py")
155         command.append("-a %s" % address)
156         command.append("-N %s " % vif_name)
157         command.append("-t %s " % vif_type)
158         if pi:
159             command.append("-p")
160
161         command = " ".join(command)
162         command = self.replace_paths(command)
163
164         return command
165