CSIT-768: Refactor Python container libraries
[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 logging
19 import os
20 import sys
21
22 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
23 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue, \
24     create_gratuitous_arp_request, checksum_equal
25 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
26 from scapy.layers.inet import IP, ICMP
27 from scapy.all import Ether, Raw
28
29
30 def main():
31     # start_size - start size of the ICMPv4 echo data
32     # end_size - end size of the ICMPv4 echo data
33     # step - increment step
34     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
35                              'start_size', 'end_size', 'step'])
36
37     rxq = RxQueue(args.get_arg('rx_if'))
38     txq = TxQueue(args.get_arg('tx_if'))
39
40     src_mac = args.get_arg('src_mac')
41     dst_mac = args.get_arg('dst_mac')
42     src_ip = args.get_arg('src_ip')
43     dst_ip = args.get_arg('dst_ip')
44     start_size = int(args.get_arg('start_size'))
45     end_size = int(args.get_arg('end_size'))
46     step = int(args.get_arg('step'))
47     echo_id = 0xa
48     # generate some random data buffer
49     data = bytearray(os.urandom(end_size))
50
51     sent_packets = []
52     pkt_send = create_gratuitous_arp_request(src_mac, src_ip)
53     sent_packets.append(pkt_send)
54     txq.send(pkt_send)
55
56     # send ICMP echo request with incremented data length and receive ICMP
57     # echo reply
58     for echo_seq in range(start_size, end_size + 1, step):
59         pkt_send = (Ether(src=src_mac, dst=dst_mac) /
60                     IP(src=src_ip, dst=dst_ip) /
61                     ICMP(id=echo_id, seq=echo_seq) /
62                     Raw(load=data[0:echo_seq]))
63         sent_packets.append(pkt_send)
64         txq.send(pkt_send)
65
66         ether = rxq.recv(ignore=sent_packets)
67         if ether is None:
68             raise RuntimeError(
69                 'ICMP echo reply seq {0} Rx timeout'.format(echo_seq))
70
71         if not ether.haslayer(IP):
72             raise RuntimeError(
73                 'Unexpected packet with no IPv4 received {0}'.format(
74                     ether.__repr__()))
75
76         ipv4 = ether['IP']
77
78         if not ipv4.haslayer(ICMP):
79             raise RuntimeError(
80                 'Unexpected packet with no ICMP received {0}'.format(
81                     ipv4.__repr__()))
82
83         icmpv4 = ipv4['ICMP']
84
85         if icmpv4.id != echo_id or icmpv4.seq != echo_seq:
86             raise RuntimeError(
87                 'Invalid ICMP echo reply received ID {0} seq {1} should be '
88                 'ID {2} seq {3}, {0}'.format(icmpv4.id, icmpv4.seq, echo_id,
89                                              echo_seq))
90
91         chksum = icmpv4.chksum
92         del icmpv4.chksum
93         tmp = ICMP(str(icmpv4))
94         if not checksum_equal(tmp.chksum, chksum):
95             raise RuntimeError(
96                 'Invalid checksum {0} should be {1}'.format(chksum, tmp.chksum))
97
98         if 'Raw' in icmpv4:
99             load = icmpv4['Raw'].load
100         else:
101             load = ""
102         if load != data[0:echo_seq]:
103             raise RuntimeError(
104                 'Received ICMP payload does not match sent payload')
105
106         sent_packets.remove(pkt_send)
107
108     sys.exit(0)
109
110
111 if __name__ == "__main__":
112     main()