Linux/Ns-3/Dce cross experiments
[nepi.git] / src / nepi / resources / planetlab / 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
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import ResourceManager, ResourceState, \
22         clsinit_copy
23
24 import os
25 import socket
26 import struct
27 import fcntl
28
29 @clsinit_copy
30 class PlanetlabTunTapFdLink(ResourceManager):
31     """ Interconnects a TAP or TUN Linux device to a FdNetDevice
32     """
33     _rtype = "planetlab::ns3::TunTapFdLink"
34
35     def __init__(self, ec, guid):
36         super(PlanetlabTunTapFdLink, self).__init__(ec, guid)
37         self._tap = None
38         self._fdnetdevice = None
39         self._fd_sock_address = None
40
41     @property
42     def fdnetdevice(self):
43         if not self._fdnetdevice:
44             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
45             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
46             if not devices or len(devices) != 1: 
47                 msg = "TapFdLink must be connected to exactly one FdNetDevices"
48                 self.error(msg)
49                 raise RuntimeError, msg
50
51             self._fdnetdevice = devices[0]
52         
53         return self._fdnetdevice
54
55     @property
56     def tap(self):
57         if not self._tap:
58             from nepi.resources.planetlab.tap import PlanetlabTap
59             devices = self.get_connected(PlanetlabTap.get_rtype())
60
61             if not devices or len(devices) != 1: 
62                 msg = "TapFdLink must be connected to exactly one PlanetlabTap"
63                 self.error(msg)
64                 raise RuntimeError, msg
65
66             self._tap = devices[0]
67         
68         return self._tap
69
70     @property
71     def fd_sock_address(self):
72         return self._fd_sock_address
73
74     @property
75     def node(self):
76         return self.tap.node
77
78     def upload_sources(self):
79         scripts = []
80
81         # vif-passfd python script
82         pl_vif_passfd = os.path.join(os.path.dirname(__file__), "scripts",
83                 "pl-vif-passfd.py")
84
85         scripts.append(pl_vif_passfd)
86         
87         # Upload scripts
88         scripts = ";".join(scripts)
89
90         self.node.upload(scripts,
91                 os.path.join(self.node.src_dir),
92                 overwrite = False)
93
94     def upload_start_command(self):
95         if self.tap.node.get("hostname") != \
96                 self.fdnetdevice.node.get("hostname"):
97             msg = "Tap and FdNetDevice are not in the same host"
98             self.error(msg)
99             raise RuntimeError, msg
100
101         self._fd_sock_address = self.fdnetdevice.recv_fd()
102         self.set("command", self._start_command)
103
104         command = self.get("command")
105         env = self.get("env")
106
107         # We want to make sure the ccnd is running
108         # before the experiment starts.
109         # Run the command as a bash script in background,
110         # in the host ( but wait until the command has
111         # finished to continue )
112         env = self.replace_paths(env)
113         command = self.replace_paths(command)
114
115         shfile = os.path.join(self.app_home, "start.sh")
116         self.node.run_and_wait(command, self.run_home,
117                 shfile = shfile,
118                 overwrite = True)
119
120     def do_deploy(self):
121         if self.tap.state < ResourceState.READY or \
122                 self.fdnetdevice.state < ResourceState.READY:
123             self.ec.schedule(self.reschedule_delay, self.deploy)
124         else:
125             self.do_discover()
126             self.do_provision()
127
128             super(PlanetlabTunTapFdLink, self).do_deploy()
129
130     def do_start(self):
131         if self.state == ResourceState.READY:
132             command = self.get("command")
133             self.info("Starting command '%s'" % command)
134
135             self.set_started()
136         else:
137             msg = " Failed to execute command '%s'" % command
138             self.error(msg, out, err)
139             raise RuntimeError, msg
140
141     @property
142     def _start_command(self):
143         command = []
144         # Use pl-vif-passfd.py to send fd from TAP to FdNetDevice
145         command.append("sudo -S")
146         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
147         command.append("python ${SRC}/pl-vif-passfd")
148         command.append("-a %s" % self.fd_sock_address)
149         command.append("-S %s " % self.tap.sock_name)
150
151         command = " ".join(command)
152         command = self.replace_paths(command)
153
154         return command
155