2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import clsinit_copy, ResourceState, \
24 from nepi.resources.linux.application import LinuxApplication
25 from nepi.resources.linux.ccn.ccnd import LinuxCCND
26 from nepi.util.timefuncs import tnow
33 # TODO: Add rest of options for ccndc!!!
34 # Implement ENTRY DELETE!!
37 class LinuxFIBEntry(LinuxApplication):
38 _rtype = "LinuxFIBEntry"
41 def _register_attributes(cls):
42 uri = Attribute("uri",
43 "URI prefix to match and route for this FIB entry",
47 protocol = Attribute("protocol",
48 "Transport protocol used in network connection to peer "
49 "for this FIB entry. One of 'udp' or 'tcp'.",
50 type = Types.Enumerate,
52 allowed = ["udp", "tcp"],
55 host = Attribute("host",
56 "Peer hostname used in network connection for this FIB entry. ",
59 port = Attribute("port",
60 "Peer port address used in network connection to peer "
61 "for this FIB entry.",
65 "Peer host public IP used in network connection for this FIB entry. ",
68 cls._register_attribute(uri)
69 cls._register_attribute(protocol)
70 cls._register_attribute(host)
71 cls._register_attribute(port)
72 cls._register_attribute(ip)
75 def _register_traces(cls):
76 ping = Trace("ping", "Ping to the peer end")
77 mtr = Trace("mtr", "Mtr to the peer end")
78 traceroute = Trace("traceroute", "Tracerout to the peer end")
80 cls._register_trace(ping)
81 cls._register_trace(mtr)
82 cls._register_trace(traceroute)
84 def __init__(self, ec, guid):
85 super(LinuxFIBEntry, self).__init__(ec, guid)
86 self._home = "fib-%s" % self.guid
88 self._traceroute = None
94 ccnd = self.get_connected(LinuxCCND.get_rtype())
103 from nepi.resources.linux.ping import LinuxPing
104 ping = self.get_connected(LinuxPing.get_rtype())
111 def traceroute(self):
112 if not self._traceroute:
113 from nepi.resources.linux.traceroute import LinuxTraceroute
114 traceroute = self.get_connected(LinuxTraceroute.get_rtype())
116 self._traceroute = traceroute[0]
118 return self._traceroute
122 if self.ccnd: return self.ccnd.node
125 def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
129 return self.ec.trace(self.ping.guid, "stdout", attr, block, offset)
131 if name == "traceroute":
132 if not self.traceroute:
134 return self.ec.trace(self.traceroute.guid, "stdout", attr, block, offset)
136 return super(LinuxFIBEntry, self).trace(name, attr, block, offset)
139 # Wait until associated ccnd is provisioned
140 if not self.ccnd or self.ccnd.state < ResourceState.READY:
141 # ccnr needs to wait until ccnd is deployed and running
142 self.ec.schedule(self.reschedule_delay, self.deploy)
144 if not self.get("ip"):
145 host = self.get("host")
146 ip = socket.gethostbyname(host)
149 if not self.get("command"):
150 self.set("command", self._start_command)
152 if not self.get("env"):
153 self.set("env", self._environment)
155 command = self.get("command")
157 self.info("Deploying command '%s' " % command)
165 def upload_start_command(self):
166 command = self.get("command")
167 env = self.get("env")
169 # We want to make sure the FIB entries are created
170 # before the experiment starts.
171 # Run the command as a bash script in the background,
172 # in the host ( but wait until the command has
173 # finished to continue )
174 env = env and self.replace_paths(env)
175 command = self.replace_paths(command)
177 # ccndc seems to return exitcode OK even if a (dns) error
178 # occurred, so we need to account for this case here.
179 (out, err), proc = self.execute_command(command,
180 env, blocking = True)
183 msg = "Failed to execute command"
184 self.error(msg, out, err)
185 raise RuntimeError, msg
188 if self.trace_enabled("ping") and not self.ping:
189 self.info("Configuring PING trace")
190 ping = self.ec.register_resource("LinuxPing")
191 self.ec.set(ping, "printTimestamp", True)
192 self.ec.set(ping, "target", self.get("host"))
193 self.ec.set(ping, "earlyStart", True)
194 self.ec.register_connection(ping, self.node.guid)
195 self.ec.register_connection(ping, self.guid)
196 # schedule ping deploy
197 self.ec.deploy(guids=[ping], group = self.deployment_group)
199 if self.trace_enabled("traceroute") and not self.traceroute:
200 self.info("Configuring TRACEROUTE trace")
201 traceroute = self.ec.register_resource("LinuxTraceroute")
202 self.ec.set(traceroute, "printTimestamp", True)
203 self.ec.set(traceroute, "continuous", True)
204 self.ec.set(traceroute, "target", self.get("host"))
205 self.ec.set(traceroute, "earlyStart", True)
206 self.ec.register_connection(traceroute, self.node.guid)
207 self.ec.register_connection(traceroute, self.guid)
208 # schedule mtr deploy
209 self.ec.deploy(guids=[traceroute], group = self.deployment_group)
212 if self.state == ResourceState.READY:
213 command = self.get("command")
214 self.info("Starting command '%s'" % command)
218 msg = " Failed to execute command '%s'" % command
219 self.error(msg, out, err)
220 raise RuntimeError, msg
223 command = self.get('command')
224 env = self.get('env')
226 if self.state == ResourceState.STARTED:
227 self.info("Stopping command '%s'" % command)
229 command = self._stop_command
230 (out, err), proc = self.execute_command(command, env,
236 msg = " Failed to execute command '%s'" % command
237 self.error(msg, out, err)
238 raise RuntimeError, msg
241 def _start_command(self):
242 uri = self.get("uri") or ""
243 protocol = self.get("protocol") or ""
244 ip = self.get("ip") or ""
245 port = self.get("port") or ""
247 # add ccnx:/example.com/ udp 224.0.0.204 52428
248 return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
250 "protocol": protocol,
256 def _stop_command(self):
257 uri = self.get("uri") or ""
258 protocol = self.get("protocol") or ""
259 ip = self.get("ip") or ""
260 port = self.get("port") or ""
262 # add ccnx:/example.com/ udp 224.0.0.204 52428
263 return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
265 "protocol": protocol,
271 def _environment(self):
272 return self.ccnd.path
274 def valid_connection(self, guid):