• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import math
7import re
8
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11
12PLATFORM_LINUX = 'LINUX'
13PLATFORM_MACOS = 'MAC_OS'
14
15
16def _get_platform_delegate(platform):
17    if platform == PLATFORM_LINUX:
18        return LinuxPingDelegate
19    elif platform == PLATFORM_MACOS:
20        return MacPingDelegate
21    else:
22      raise error.TestError('%s is not a valid platform type', platform)
23
24
25def _regex_int_from_string(pattern, line):
26    """Retrieve an integer from a string, using regex.
27
28    @param pattern: The regular expression to apply to the input string.
29    @param line: String input to retrieve an integer from.
30    @return integer retrieved from the input string, or None if there is no
31        match.
32    """
33    m = re.search(pattern, line)
34    if m is not None:
35        return int(m.group(1))
36    return None
37
38
39def _regex_float_from_string(pattern, line):
40    """Retrieve a float from a string, using regex.
41
42    @param pattern: The regular expression to apply to the input string.
43    @param line: String input to retrieve a float from.
44    @return float retrieved from the input string, or None if there is no
45        match.
46    """
47    m = re.search(pattern, line)
48    if m is not None:
49        return float(m.group(1))
50    return None
51
52
53class MacPingDelegate(object):
54    """Implement ping functionality for MacOS hosts."""
55
56    @staticmethod
57    def ping_arguments(ping_config):
58        """
59        @param ping_config PingConfig object describing the ping test for which
60           arguments are needed.
61        @return list of parameters to ping.
62        """
63        args = []
64        args.append('-c %d' % ping_config.count)
65        if ping_config.size is not None:
66            args.append('-s %d' % ping_config.size)
67        if ping_config.interval is not None:
68            args.append('-i %f' % ping_config.interval)
69        if ping_config.qos is not None:
70            if ping_config.qos == 'be':
71                ping_config.append('-k 0')
72            elif ping_config.qos == 'bk':
73                ping_config.append('-k 1')
74            elif ping_config.qos == 'vi':
75                args.append('-k 2')
76            elif ping_config.qos == 'vo':
77                args.append('-k 3')
78            else:
79                raise error.TestFail('Unknown QoS value: %s' % ping_config.qos)
80
81        # The last argument is the IP address to ping.
82        args.append(ping_config.target_ip)
83        return args
84
85
86    @staticmethod
87    def parse_from_output(ping_output):
88        """Extract the ping results from stdout.
89
90        @param ping_output string stdout from a ping command.
91
92        PING 8.8.8.8 (8.8.8.8): 56 data bytes
93        64 bytes from 8.8.8.8: icmp_seq=0 ttl=57 time=3.770 ms
94        64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=4.165 ms
95        64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=4.901 ms
96
97        --- 8.8.8.8 ping statistics ---
98        3 packets transmitted, 3 packets received, 0.0% packet loss
99        round-trip min/avg/max/stddev = 3.770/4.279/4.901/0.469 ms
100
101        """
102        loss_line = (filter(lambda x: x.find('packets transmitted') > 0,
103                            ping_output.splitlines()) or [''])[0]
104        sent = _regex_int_from_string('([0-9]+) packets transmitted', loss_line)
105        received = _regex_int_from_string('([0-9]+) packets received',
106                                          loss_line)
107        loss = _regex_float_from_string('([0-9]+\.[0-9]+)% packet loss',
108                                        loss_line)
109        if None in (sent, received, loss):
110            raise error.TestFail('Failed to parse transmission statistics.')
111
112        m = re.search('round-trip min\/avg\/max\/stddev = ([0-9.]+)\/([0-9.]+)'
113                      '\/([0-9.]+)\/([0-9.]+) ms', ping_output)
114        if m is not None:
115            return PingResult(sent, received, loss,
116                              min_latency=float(m.group(1)),
117                              avg_latency=float(m.group(2)),
118                              max_latency=float(m.group(3)),
119                              dev_latency=float(m.group(4)))
120        if received > 0:
121            raise error.TestFail('Failed to parse latency statistics.')
122
123        return PingResult(sent, received, loss)
124
125
126class LinuxPingDelegate(object):
127    """Implement ping functionality specific to the linux platform."""
128    @staticmethod
129    def ping_arguments(ping_config):
130        """
131        @param ping_config PingConfig object describing the ping test for which
132           arguments are needed.
133        @return list of parameters to ping.
134        """
135        args = []
136        args.append('-c %d' % ping_config.count)
137        if ping_config.size is not None:
138            args.append('-s %d' % ping_config.size)
139        if ping_config.interval is not None:
140            args.append('-i %f' % ping_config.interval)
141        if ping_config.qos is not None:
142            if ping_config.qos == 'be':
143                args.append('-Q 0x04')
144            elif ping_config.qos == 'bk':
145                args.append('-Q 0x02')
146            elif ping_config.qos == 'vi':
147                args.append('-Q 0x08')
148            elif ping_config.qos == 'vo':
149                args.append('-Q 0x10')
150            else:
151                raise error.TestFail('Unknown QoS value: %s' % ping_config.qos)
152
153        # The last argument is the IP address to ping.
154        args.append(ping_config.target_ip)
155        return args
156
157
158    @staticmethod
159    def parse_from_output(ping_output):
160        """Extract the ping results from stdout.
161
162        @param ping_output string stdout from a ping command.
163        On error, some statistics may be missing entirely from the output.
164
165        An example of output with some errors is:
166
167        PING 192.168.0.254 (192.168.0.254) 56(84) bytes of data.
168        From 192.168.0.124 icmp_seq=1 Destination Host Unreachable
169        From 192.168.0.124 icmp_seq=2 Destination Host Unreachable
170        From 192.168.0.124 icmp_seq=3 Destination Host Unreachable
171        64 bytes from 192.168.0.254: icmp_req=4 ttl=64 time=1171 ms
172        [...]
173        64 bytes from 192.168.0.254: icmp_req=10 ttl=64 time=1.95 ms
174
175        --- 192.168.0.254 ping statistics ---
176        10 packets transmitted, 7 received, +3 errors, 30% packet loss,
177            time 9007ms
178        rtt min/avg/max/mdev = 1.806/193.625/1171.174/403.380 ms, pipe 3
179
180        A more normal run looks like:
181
182        PING google.com (74.125.239.137) 56(84) bytes of data.
183        64 bytes from 74.125.239.137: icmp_req=1 ttl=57 time=1.77 ms
184        64 bytes from 74.125.239.137: icmp_req=2 ttl=57 time=1.78 ms
185        [...]
186        64 bytes from 74.125.239.137: icmp_req=5 ttl=57 time=1.79 ms
187
188        --- google.com ping statistics ---
189        5 packets transmitted, 5 received, 0% packet loss, time 4007ms
190        rtt min/avg/max/mdev = 1.740/1.771/1.799/0.042 ms
191
192        We also sometimes see result lines like:
193        9 packets transmitted, 9 received, +1 duplicates, 0% packet loss,
194            time 90 ms
195
196        """
197        loss_line = (filter(lambda x: x.find('packets transmitted') > 0,
198                            ping_output.splitlines()) or [''])[0]
199        sent = _regex_int_from_string('([0-9]+) packets transmitted', loss_line)
200        received = _regex_int_from_string('([0-9]+) received', loss_line)
201        loss = _regex_int_from_string('([0-9]+)% packet loss', loss_line)
202        if None in (sent, received, loss):
203            raise error.TestFail('Failed to parse transmission statistics.')
204
205        m = re.search('(round-trip|rtt) min[^=]*= '
206                      '([0-9.]+)/([0-9.]+)/([0-9.]+)/([0-9.]+)', ping_output)
207        if m is not None:
208            return PingResult(sent, received, loss,
209                              min_latency=float(m.group(2)),
210                              avg_latency=float(m.group(3)),
211                              max_latency=float(m.group(4)),
212                              dev_latency=float(m.group(5)))
213        if received > 0:
214            raise error.TestFail('Failed to parse latency statistics.')
215
216        return PingResult(sent, received, loss)
217
218
219class PingConfig(object):
220    """Describes the parameters for a ping command."""
221
222    DEFAULT_COUNT = 10
223    PACKET_WAIT_MARGIN_SECONDS = 120
224
225    def __init__(self, target_ip, count=DEFAULT_COUNT, size=None,
226                 interval=None, qos=None,
227                 ignore_status=False, ignore_result=False):
228        super(PingConfig, self).__init__()
229        self.target_ip = target_ip
230        self.count = count
231        self.size = size
232        self.interval = interval
233        if qos:
234            qos = qos.lower()
235        self.qos = qos
236        self.ignore_status = ignore_status
237        self.ignore_result = ignore_result
238        interval_seconds = self.interval or 1
239        command_time = math.ceil(interval_seconds * self.count)
240        self.command_timeout_seconds = int(command_time +
241                                           self.PACKET_WAIT_MARGIN_SECONDS)
242
243
244class PingResult(object):
245    """Represents a parsed ping command result."""
246    def __init__(self, sent, received, loss,
247                 min_latency=-1.0, avg_latency=-1.0,
248                 max_latency=-1.0, dev_latency=-1.0):
249        """Construct a PingResult.
250
251        @param sent: int number of packets sent.
252        @param received: int number of replies received.
253        @param loss: int loss as a percentage (0-100)
254        @param min_latency: float min response latency in ms.
255        @param avg_latency: float average response latency in ms.
256        @param max_latency: float max response latency in ms.
257        @param dev_latency: float response latency deviation in ms.
258
259        """
260        super(PingResult, self).__init__()
261        self.sent = sent
262        self.received = received
263        self.loss = loss
264        self.min_latency = min_latency
265        self.avg_latency = avg_latency
266        self.max_latency = max_latency
267        self.dev_latency = dev_latency
268
269
270    def __repr__(self):
271        return '%s(%s)' % (self.__class__.__name__,
272                           ', '.join(['%s=%r' % item
273                                      for item in vars(self).iteritems()]))
274
275
276class PingRunner(object):
277    """Delegate to run the ping command on a local or remote host."""
278    DEFAULT_PING_COMMAND = 'ping'
279    PING_LOSS_THRESHOLD = 20  # A percentage.
280
281
282    def __init__(self, command_ping=DEFAULT_PING_COMMAND, host=None,
283                 platform=PLATFORM_LINUX):
284        """Construct a PingRunner.
285
286        @param command_ping optional path or alias of the ping command.
287        @param host optional host object when a remote host is desired.
288
289        """
290        super(PingRunner, self).__init__()
291        self._run = utils.run
292        if host is not None:
293            self._run = host.run
294        self.command_ping = command_ping
295        self._platform_delegate = _get_platform_delegate(platform)
296
297
298    def simple_ping(self, host_name):
299        """Quickly test that a hostname or IPv4 address responds to ping.
300
301        @param host_name: string name or IPv4 address.
302        @return True if host_name responds to at least one ping.
303
304        """
305        ping_config = PingConfig(host_name, count=3, interval=0.5,
306                                 ignore_status=True, ignore_result=True)
307        ping_result = self.ping(ping_config)
308        if ping_result is None or ping_result.received == 0:
309            return False
310        return True
311
312
313    def ping(self, ping_config):
314        """Run ping with the given |ping_config|.
315
316        Will assert that the ping had reasonable levels of loss unless
317        requested not to in |ping_config|.
318
319        @param ping_config PingConfig object describing the ping to run.
320
321        """
322        command_pieces = ([self.command_ping] +
323                          self._platform_delegate.ping_arguments(ping_config))
324        command = ' '.join(command_pieces)
325        command_result = self._run(command,
326                                   timeout=ping_config.command_timeout_seconds,
327                                   ignore_status=True,
328                                   ignore_timeout=True)
329        if not command_result:
330            if ping_config.ignore_status:
331                logging.warning('Ping command timed out; cannot parse output.')
332                return PingResult(ping_config.count, 0, 100)
333
334            raise error.TestFail('Ping command timed out unexpectedly.')
335
336        if not command_result.stdout:
337            logging.warning('Ping command returned no output; stderr was %s.',
338                            command_result.stderr)
339            if ping_config.ignore_result:
340                return PingResult(ping_config.count, 0, 100)
341            raise error.TestFail('Ping command failed to yield any output')
342
343        if command_result.exit_status and not ping_config.ignore_status:
344            raise error.TestFail('Ping command failed with code=%d' %
345                                 command_result.exit_status)
346
347        ping_result = self._platform_delegate.parse_from_output(
348                command_result.stdout)
349        if ping_config.ignore_result:
350            return ping_result
351
352        if ping_result.loss > self.PING_LOSS_THRESHOLD:
353            raise error.TestFail('Lost ping packets: %r.' % ping_result)
354
355        logging.info('Ping successful.')
356        return ping_result
357