Update of latest tests.
[csit.git] / resources / traffic_scripts / ipv4_sweep_ping.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Traffic script for IPv4 sweep ping."""
17
18 import sys
19 import logging
20 import os
21 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
22 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue,\
23     auto_pad, create_gratuitous_arp_request
24 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
25 from scapy.layers.inet import IP, ICMP
26 from scapy.all import Ether, Raw
27
28
29 def main():
30     # start_size - start size of the ICMPv4 echo data
31     # end_size - end size of the ICMPv4 echo data
32     # step - increment step
33     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
34                              'start_size', 'end_size', 'step'])
35
36     rxq = RxQueue(args.get_arg('rx_if'))
37     txq = TxQueue(args.get_arg('tx_if'))
38
39     src_mac = args.get_arg('src_mac')
40     dst_mac = args.get_arg('dst_mac')
41     src_ip = args.get_arg('src_ip')
42     dst_ip = args.get_arg('dst_ip')
43     start_size = int(args.get_arg('start_size'))
44     end_size = int(args.get_arg('end_size'))
45     step = int(args.get_arg('step'))
46     echo_id = 0xa
47     # generate some random data buffer
48     data = bytearray(os.urandom(end_size))
49
50     sent_packets = []
51     pkt_send = create_gratuitous_arp_request(src_mac, src_ip)
52     sent_packets.append(pkt_send)
53     txq.send(pkt_send)
54
55     # send ICMP echo request with incremented data length and receive ICMP
56     # echo reply
57     for echo_seq in range(start_size, end_size+1, step):
58         pkt_send = auto_pad(Ether(src=src_mac, dst=dst_mac) /
59                             IP(src=src_ip, dst=dst_ip) /
60                             ICMP(id=echo_id, seq=echo_seq) /
61                             Raw(load=data[0:echo_seq]))
62         sent_packets.append(pkt_send)
63         txq.send(pkt_send)
64
65         ether = rxq.recv(ignore=sent_packets)
66         if ether is None:
67             rxq._proc.terminate()
68             raise RuntimeError(
69                 'ICMP echo reply seq {0} Rx timeout'.format(echo_seq))
70
71         if not ether.haslayer(IP):
72             rxq._proc.terminate()
73             raise RuntimeError(
74                 'Unexpected packet with no IPv4 received {0}'.format(
75                     ether.__repr__()))
76
77         ipv4 = ether['IP']
78
79         if not ipv4.haslayer(ICMP):
80             rxq._proc.terminate()
81             raise RuntimeError(
82                 'Unexpected packet with no ICMP received {0}'.format(
83                     ipv4.__repr__()))
84
85         icmpv4 = ipv4['ICMP']
86
87         if icmpv4.id != echo_id or icmpv4.seq != echo_seq:
88             rxq._proc.terminate()
89             raise RuntimeError(
90                 'Invalid ICMP echo reply received ID {0} seq {1} should be ' +
91                 'ID {2} seq {3}, {0}'.format(icmpv4.id, icmpv4.seq, echo_id,
92                                              echo_seq))
93
94         chksum = icmpv4.chksum
95         del icmpv4.chksum
96         tmp = ICMP(str(icmpv4))
97         if tmp.chksum != chksum:
98             rxq._proc.terminate()
99             raise RuntimeError(
100                 'Invalid checksum {0} should be {1}'.format(chksum, tmp.chksum))
101         recv_payload_len = ipv4.len - 20 - 8
102         load = tmp['Raw'].load[0:recv_payload_len]
103         if load != data[0:echo_seq]:
104             rxq._proc.terminate()
105             raise RuntimeError(
106                 'Received ICMP payload does not match sent payload')
107
108     rxq._proc.terminate()
109     sys.exit(0)
110
111 if __name__ == "__main__":
112     main()