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: releng/10.3/tools/regression/netinet/ip_id_period/ip_id_period.py 241832 2012-10-22 02:29:44Z eadler $
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
30import os
31import signal
32import subprocess
33import time
34
35if os.path.exists('results.pcap'):
36    os.remove('results.pcap')
37tcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)
38time.sleep(1) # Give tcpdump time to start
39
40os.system('sysctl net.inet.icmp.icmplim=0')
41os.system('ping -q -i .001 -c 100000 127.0.0.1')
42
43time.sleep(3) # Give tcpdump time to catch up
44os.kill(tcpdump.pid, signal.SIGTERM)
45
46os.system('tcpdump -n -v -r results.pcap > results.txt')
47
48id_lastseen = {}
49id_minperiod = {}
50
51count = 0
52for line in open('results.txt').readlines():
53    id = int(line.split(' id ')[1].split(',')[0])
54    if id in id_lastseen:
55        period = count - id_lastseen[id]
56        if id not in id_minperiod or period < id_minperiod[id]:
57            id_minperiod[id] = period
58    id_lastseen[id] = count
59    count += 1
60
61sorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))
62sorted_minperiod.sort()
63
64print("Lowest 10 ID periods detected:")
65x = 0
66while x < 10:
67    id_tuple = sorted_minperiod.pop(0)
68    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
69    x += 1
70
71print("Highest 10 ID periods detected:")
72x = 0
73while x < 10:
74    id_tuple = sorted_minperiod.pop()
75    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
76    x += 1
77