]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/netinet/ip_id_period/ip_id_period.py
Merge vendor lld/docs directory from r337145
[FreeBSD/FreeBSD.git] / tools / regression / netinet / ip_id_period / ip_id_period.py
1 # Copyright (C) 2008 Michael J. Silbersack.  All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1. Redistributions of source code must retain the above copyright
7 #    notice unmodified, this list of conditions, and the following
8 #    disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 #    notice, this list of conditions and the following disclaimer in the
11 #    documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #
24 # $FreeBSD$
25 #
26 # This is a regression test to verify the proper behavior of IP ID generation
27 # code.  It will push 200000 packets, then report back what the min and max
28 # periods it saw for different IDs were.
29
30 from __future__ import print_function
31 import os
32 import signal
33 import subprocess
34 import time
35
36 if os.path.exists('results.pcap'):
37     os.remove('results.pcap')
38 tcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)
39 time.sleep(1) # Give tcpdump time to start
40
41 os.system('sysctl net.inet.icmp.icmplim=0')
42 os.system('ping -q -i .001 -c 100000 127.0.0.1')
43
44 time.sleep(3) # Give tcpdump time to catch up
45 os.kill(tcpdump.pid, signal.SIGTERM)
46
47 os.system('tcpdump -n -v -r results.pcap > results.txt')
48
49 id_lastseen = {}
50 id_minperiod = {}
51
52 count = 0
53 for line in open('results.txt').readlines():
54     id = int(line.split(' id ')[1].split(',')[0])
55     if id in id_lastseen:
56         period = count - id_lastseen[id]
57         if id not in id_minperiod or period < id_minperiod[id]:
58             id_minperiod[id] = period
59     id_lastseen[id] = count
60     count += 1
61
62 sorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))
63 sorted_minperiod.sort()
64
65 print("Lowest 10 ID periods detected:")
66 x = 0
67 while x < 10:
68     id_tuple = sorted_minperiod.pop(0)
69     print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
70     x += 1
71
72 print("Highest 10 ID periods detected:")
73 x = 0
74 while x < 10:
75     id_tuple = sorted_minperiod.pop()
76     print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
77     x += 1