xref: /freebsd/tools/regression/netinet/ip_id_period/ip_id_period.py (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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
30from __future__ import print_function
31import os
32import signal
33import subprocess
34import time
35
36if os.path.exists('results.pcap'):
37    os.remove('results.pcap')
38tcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)
39time.sleep(1) # Give tcpdump time to start
40
41os.system('sysctl net.inet.icmp.icmplim=0')
42os.system('ping -q -i .001 -c 100000 127.0.0.1')
43
44time.sleep(3) # Give tcpdump time to catch up
45os.kill(tcpdump.pid, signal.SIGTERM)
46
47os.system('tcpdump -n -v -r results.pcap > results.txt')
48
49id_lastseen = {}
50id_minperiod = {}
51
52count = 0
53for 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
62sorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))
63sorted_minperiod.sort()
64
65print("Lowest 10 ID periods detected:")
66x = 0
67while 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
72print("Highest 10 ID periods detected:")
73x = 0
74while x < 10:
75    id_tuple = sorted_minperiod.pop()
76    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
77    x += 1
78