Removing MyPLC credentials requirements in update_fedora_repo.py
[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.resource import ResourceState, clsinit_copy
21 from nepi.resources.linux.ns3.tun_tap_fd_link import LinuxTunTapFdLink
22
23 import base64
24 import fcntl
25 import os
26 import socket
27 import struct
28
29 @clsinit_copy
30 class PlanetlabTunTapFdLink(LinuxTunTapFdLink):
31     """ Interconnects a TAP or TUN PlanetLab 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
38     @property
39     def fdnetdevice(self):
40         if not self._fdnetdevice:
41             from nepi.resources.ns3.ns3fdnetdevice import NS3BaseFdNetDevice
42             devices = self.get_connected(NS3BaseFdNetDevice.get_rtype())
43             if not devices or len(devices) != 1: 
44                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one FdNetDevice"
45                 self.error(msg)
46                 raise RuntimeError, msg
47
48             self._fdnetdevice = devices[0]
49
50             # Set PI headers on
51             self._fdnetdevice.set("EncapsulationMode", "DixPi")
52         
53             simu = self._fdnetdevice.simulation
54             from nepi.resources.planetlab.node import PlanetlabNode
55             nodes = simu.get_connected(PlanetlabNode.get_rtype())
56             self._fd_node = nodes[0]
57         
58         return self._fdnetdevice
59
60     @property
61     def tap(self):
62         if not self._tap:
63             from nepi.resources.planetlab.tap import PlanetlabTap
64             devices = self.get_connected(PlanetlabTap.get_rtype())
65
66             if not devices or len(devices) != 1: 
67                 msg = "planetlab::ns3::TunTapFdLink must be connected to exactly one PlanetlabTap"
68                 self.error(msg)
69                 raise RuntimeError, msg
70
71             self._tap = devices[0]
72         
73         return self._tap
74
75     def upload_sources(self):
76         scripts = []
77
78         # vif-passfd python script
79         pl_vif_passfd = os.path.join(os.path.dirname(__file__), 
80                 "..",
81                 "scripts",
82                 "pl-vif-passfd.py")
83
84         scripts.append(pl_vif_passfd)
85         
86         # Upload scripts
87         scripts = ";".join(scripts)
88
89         self.node.upload(scripts,
90                 os.path.join(self.node.src_dir),
91                 overwrite = False)
92
93     @property
94     def _start_command(self):
95         address = base64.b64encode(self.send_address)
96
97         command = []
98         # Use pl-vif-passfd.py to send fd from TAP to FdNetDevice
99         command.append("sudo -S")
100         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
101         command.append("python ${SRC}/pl-vif-passfd.py")
102         command.append("-a %s" % address)
103         command.append("-S %s " % self.tap.sock_name)
104
105         command = " ".join(command)
106         command = self.replace_paths(command)
107
108         return command
109