Update of latest tests.
[csit.git] / resources / traffic_scripts / ipv4_ping_ttl_check.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 from scapy.all import Ether, IP, ICMP
17 from resources.libraries.python.PacketVerifier \
18     import Interface, create_gratuitous_arp_request, auto_pad
19 from optparse import OptionParser
20
21
22 def check_ttl(ttl_begin, ttl_end, ttl_diff):
23     if ttl_begin != ttl_end + ttl_diff:
24         src_if.close()
25         if dst_if_defined:
26             dst_if.close()
27         raise Exception(
28             "TTL changed from {} to {} but decrease by {} expected"
29             .format(ttl_begin, ttl_end, hops))
30
31
32 def ckeck_packets_equal(pkt_send, pkt_recv):
33     pkt_send_raw = str(pkt_send)
34     pkt_recv_raw = str(pkt_recv)
35     if pkt_send_raw != pkt_recv_raw:
36         print "Sent:     {}".format(pkt_send_raw.encode('hex'))
37         print "Received: {}".format(pkt_recv_raw.encode('hex'))
38         print "Sent:"
39         Ether(pkt_send_raw).show2()
40         print "Received:"
41         Ether(pkt_recv_raw).show2()
42         src_if.close()
43         if dst_if_defined:
44             dst_if.close()
45         raise Exception("Sent packet doesn't match received packet")
46
47
48 parser = OptionParser()
49 parser.add_option("--src_if", dest="src_if")
50 parser.add_option("--dst_if", dest="dst_if")  # optional
51 parser.add_option("--src_mac", dest="src_mac")
52 parser.add_option("--first_hop_mac", dest="first_hop_mac")
53 parser.add_option("--dst_mac", dest="dst_mac")  # optional
54 parser.add_option("--src_ip", dest="src_ip")
55 parser.add_option("--dst_ip", dest="dst_ip")
56 parser.add_option("--hops", dest="hops")  # optional
57 # If one of 'dst_if', 'dst_mac' and 'hops' is specified all must be specified.
58 (opts, args) = parser.parse_args()
59 src_if_name = opts.src_if
60 dst_if_name = opts.dst_if
61 dst_if_defined = True
62 if dst_if_name is None:
63     dst_if_defined = False
64 src_mac = opts.src_mac
65 first_hop_mac = opts.first_hop_mac
66 dst_mac = opts.dst_mac
67 src_ip = opts.src_ip
68 dst_ip = opts.dst_ip
69 hops = int(opts.hops)
70
71 if dst_if_defined and (src_if_name == dst_if_name):
72     raise Exception("Source interface name equals destination interface name")
73
74 src_if = Interface(src_if_name)
75 src_if.send_pkt(create_gratuitous_arp_request(src_mac, src_ip))
76 if dst_if_defined:
77     dst_if = Interface(dst_if_name)
78     dst_if.send_pkt(create_gratuitous_arp_request(dst_mac, dst_ip))
79
80 pkt_req_send = auto_pad(Ether(src=src_mac, dst=first_hop_mac) /
81                         IP(src=src_ip, dst=dst_ip) /
82                         ICMP())
83 pkt_req_send = Ether(pkt_req_send)
84 src_if.send_pkt(pkt_req_send)
85
86 if dst_if_defined:
87     try:
88         pkt_req_recv = dst_if.recv_pkt()
89     except:
90         src_if.close()
91         if dst_if_defined:
92             dst_if.close()
93         raise
94
95     check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
96     pkt_req_send_mod = pkt_req_send.copy()
97     pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
98     del pkt_req_send_mod[IP].chksum  # update checksum
99     ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
100
101     pkt_resp_send = auto_pad(Ether(src=dst_mac, dst=pkt_req_recv.src) /
102                              IP(src=dst_ip, dst=src_ip) /
103                              ICMP(type=0))  # echo-reply
104     pkt_resp_send = Ether(pkt_resp_send)
105     dst_if.send_pkt(pkt_resp_send)
106
107 try:
108     pkt_resp_recv = src_if.recv_pkt()
109 except:
110     src_if.close()
111     if dst_if_defined:
112         dst_if.close()
113     raise
114
115 if dst_if_defined:
116     check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
117     pkt_resp_send_mod = pkt_resp_send.copy()
118     pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
119     del pkt_resp_send_mod[IP].chksum  # update checksum
120     ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
121
122 src_if.close()
123 if dst_if_defined:
124     dst_if.close()