Merging the openflow part to the nepi-3-dev branch
[nepi.git] / src / nepi / resources / linux / ping.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
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow
24
25 import os
26
27 @clsinit_copy
28 class LinuxPing(LinuxApplication):
29     _rtype = "LinuxPing"
30
31     @classmethod
32     def _register_attributes(cls):
33         count = Attribute("count",
34             "Sets ping -c option. Determines the number of ECHO_REQUEST "
35             "packates to send before stopping.",
36             type = Types.Integer,
37             flags = Flags.Design)
38
39         mark = Attribute("mark",
40             "Sets ping -m option. Uses 'mark' to tag outgoing packets. ",
41             flags = Flags.Design)
42
43         interval = Attribute("interval",
44             "Sets ping -i option. Leaves interval seconds between "
45             "successive ECHO_REUQEST packets. ",
46             flags = Flags.Design)
47
48         address = Attribute("address",
49             "Sets ping -I option. Sets ECHO_REQUEST packets souce address "
50             "to the specified interface address ",
51             flags = Flags.Design)
52
53         preload = Attribute("preload",
54             "Sets ping -l option. Sends preload amount of packets "
55             "without waiting for a reply ",
56             flags = Flags.Design)
57
58         numeric = Attribute("numeric",
59             "Sets ping -n option. Disables resolution of host addresses into "
60             "symbolic names. ",
61             type = Types.Bool,
62             default = False,
63             flags = Flags.Design)
64
65         pattern = Attribute("pattern",
66             "Sets ping -p option. Species a up to 16 ''pad'' bytes to fill "
67             "out sent packets. ",
68             flags = Flags.Design)
69
70         printtmp = Attribute("printTimestamp",
71             "Sets ping -D option. Prints timestamp befor each line as: "
72             "unix time + microseconds as in gettimeofday ",
73             type = Types.Bool,
74             default = False,
75             flags = Flags.Design)
76
77         tos = Attribute("tos",
78             "Sets ping -Q option. Sets Quality of Service related bits in ICMP "
79             "datagrams. tos can be either a decimal or hexadecime number ",
80             flags = Flags.Design)
81
82         quiet = Attribute("quiet",
83             "Sets ping -q option. Disables ping standard output ",
84             type = Types.Bool,
85             default = False,
86             flags = Flags.Design)
87
88         rec_route = Attribute("recordRoute",
89             "Sets ping -R option. Includes the RECORD_ROUTE option in the "
90             "ECHO REQUEST packet and displays route buffer on the Disables "
91             "ping standard output.",
92             type = Types.Bool,
93             default = False,
94             flags = Flags.Design)
95
96         route_bypass = Attribute("routeBypass",
97             "Sets ping -r option. Bypasses normal routing tables and sends "
98             "ECHO REQUEST packets directly yo a host on an attached interface. ",
99             type = Types.Bool,
100             default = False,
101             flags = Flags.Design)
102
103         packetsize = Attribute("packetSize",
104             "Sets ping -s option. Specifies the number of data bytes to be "
105             "sent. Defaults to 56. ",
106             flags = Flags.Design)
107
108         sendbuff = Attribute("sendBuff",
109             "Sets ping -S option. Specifies the number of packets to buffer. "
110             "Defaults to one. ",
111             flags = Flags.Design)
112
113         ttl = Attribute("ttl",
114             "Sets ping -t option. Specifies the IP Time to Live for the "
115             "packets. ",
116             flags = Flags.Design)
117
118         timestamp = Attribute("timestamp",
119             "Sets ping -T option. Sets special IP timestamp options. ",
120             flags = Flags.Design)
121
122         hint = Attribute("hint",
123             "Sets ping -M option. Selects Path MTU Discovery strategy. ",
124             flags = Flags.Design)
125
126         full_latency = Attribute("fullLatency",
127             "Sets ping -U option. Calculates round trip time taking into "
128             "account the full user-to-user latency instead of only the "
129             "network round trip time. ",
130             type = Types.Bool,
131             default = False,
132             flags = Flags.Design)
133
134         verbose = Attribute("verbose",
135             "Sets ping -v option. Verbose output. ",
136             type = Types.Bool,
137             default = False,
138             flags = Flags.Design)
139
140         flood = Attribute("flood",
141             "Sets ping -f option. Flood ping. ",
142             type = Types.Bool,
143             default = False,
144             flags = Flags.Design)
145
146         deadline = Attribute("deadline",
147             "Sets ping -w option. Specify a timeout, in seconds, before ping "
148             "exits regardless of how many packets have been sent or received.",
149             flags = Flags.Design)
150
151         timeout = Attribute("timeout",
152             "Sets ping -W option. Time to wait for a respone in seconds .",
153             flags = Flags.Design)
154
155         target = Attribute("target",
156             "The host to ping .",
157             flags = Flags.Design)
158
159         cls._register_attribute(count)
160         cls._register_attribute(mark)
161         cls._register_attribute(interval)
162         cls._register_attribute(address)
163         cls._register_attribute(preload)
164         cls._register_attribute(numeric)
165         cls._register_attribute(pattern)
166         cls._register_attribute(printtmp)
167         cls._register_attribute(tos)
168         cls._register_attribute(quiet)
169         cls._register_attribute(rec_route)
170         cls._register_attribute(route_bypass)
171         cls._register_attribute(packetsize)
172         cls._register_attribute(sendbuff)
173         cls._register_attribute(ttl)
174         cls._register_attribute(timestamp)
175         cls._register_attribute(hint)
176         cls._register_attribute(full_latency)
177         cls._register_attribute(verbose)
178         cls._register_attribute(flood)
179         cls._register_attribute(deadline)
180         cls._register_attribute(timeout)
181         cls._register_attribute(target)
182
183     def __init__(self, ec, guid):
184         super(LinuxPing, self).__init__(ec, guid)
185         self._home = "ping-%s" % self.guid
186
187     def do_deploy(self):
188         if not self.get("command"):
189             self.set("command", self._start_command)
190
191         super(LinuxPing, self).do_deploy()
192
193     @property
194     def _start_command(self):
195         args = []
196
197         args.append("echo 'Starting PING to %s' ;" % self.get("target"))
198
199         if self.get("printTimestamp") == True:
200             args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
201
202         args.append("ping ")
203         
204         if self.get("count"):
205             args.append("-c %s" % self.get("count"))
206         if self.get("mark"):
207             args.append("-m %s" % self.get("mark"))
208         if self.get("interval"):
209             args.append("-i %s" % self.get("interval"))
210         if self.get("address"):
211             args.append("-I %s" % self.get("address"))
212         if self.get("preload"):
213             args.append("-l %s" % self.get("preload"))
214         if self.get("numeric") == True:
215             args.append("-n")
216         if self.get("pattern"):
217             args.append("-p %s" % self.get("pattern"))
218         if self.get("tos"):
219             args.append("-Q %s" % self.get("tos"))
220         if self.get("quiet"):
221             args.append("-q %s" % self.get("quiet"))
222         if self.get("recordRoute") == True:
223             args.append("-R")
224         if self.get("routeBypass") == True:
225             args.append("-r")
226         if self.get("packetSize"):
227             args.append("-s %s" % self.get("packetSize"))
228         if self.get("sendBuff"):
229             args.append("-S %s" % self.get("sendBuff"))
230         if self.get("ttl"):
231             args.append("-t %s" % self.get("ttl"))
232         if self.get("timestamp"):
233             args.append("-T %s" % self.get("timestamp"))
234         if self.get("hint"):
235             args.append("-M %s" % self.get("hint"))
236         if self.get("fullLatency") == True:
237             args.append("-U")
238         if self.get("verbose") == True:
239             args.append("-v")
240         if self.get("flood") == True:
241             args.append("-f")
242         if self.get("deadline"):
243             args.append("-w %s" % self.get("deadline"))
244         if self.get("timeout"):
245             args.append("-W %s" % self.get("timeout"))
246         args.append(self.get("target"))
247
248         command = " ".join(args)
249
250         return command
251
252     def valid_connection(self, guid):
253         # TODO: Validate!
254         return True
255