cc3afdec10b6bf54f6edaa2e1d4b83151c21dbe1
[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 resources.libraries.python.TrafficScriptArg import TrafficScriptArg
20
21
22 def check_ttl(ttl_begin, ttl_end, ttl_diff):
23     if ttl_begin != ttl_end + ttl_diff:
24         raise Exception(
25             "TTL changed from {} to {} but decrease by {} expected"
26             .format(ttl_begin, ttl_end, ttl_diff))
27
28
29 def ckeck_packets_equal(pkt_send, pkt_recv):
30     pkt_send_raw = auto_pad(pkt_send)
31     pkt_recv_raw = auto_pad(pkt_recv)
32     if pkt_send_raw != pkt_recv_raw:
33         print "Sent:     {}".format(pkt_send_raw.encode('hex'))
34         print "Received: {}".format(pkt_recv_raw.encode('hex'))
35         print "Sent:"
36         Ether(pkt_send_raw).show2()
37         print "Received:"
38         Ether(pkt_recv_raw).show2()
39         raise Exception("Sent packet doesn't match received packet")
40
41
42 def main():
43     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
44                              'hops', 'first_hop_mac', 'is_dst_tg'])
45
46     src_if_name = args.get_arg('tx_if')
47     dst_if_name = args.get_arg('rx_if')
48     is_dst_tg = True if args.get_arg('is_dst_tg') == 'True' else False
49
50     src_mac = args.get_arg('src_mac')
51     first_hop_mac = args.get_arg('first_hop_mac')
52     dst_mac = args.get_arg('dst_mac')
53     src_ip = args.get_arg('src_ip')
54     dst_ip = args.get_arg('dst_ip')
55     hops = int(args.get_arg('hops'))
56
57     if is_dst_tg and (src_if_name == dst_if_name):
58         raise Exception("Source interface name equals destination interface name")
59
60     src_if = Interface(src_if_name)
61     src_if.send_pkt(str(create_gratuitous_arp_request(src_mac, src_ip)))
62     if is_dst_tg:
63         dst_if = Interface(dst_if_name)
64         dst_if.send_pkt(str(create_gratuitous_arp_request(dst_mac, dst_ip)))
65
66     pkt_req_send = (Ether(src=src_mac, dst=first_hop_mac) /
67                     IP(src=src_ip, dst=dst_ip) /
68                     ICMP())
69     src_if.send_pkt(pkt_req_send)
70
71     if is_dst_tg:
72         pkt_req_recv = dst_if.recv_pkt()
73         if pkt_req_recv is None:
74             raise Exception('Timeout waiting for packet')
75
76         check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
77         pkt_req_send_mod = pkt_req_send.copy()
78         pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
79         del pkt_req_send_mod[IP].chksum  # update checksum
80         ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
81
82         pkt_resp_send = (Ether(src=dst_mac, dst=pkt_req_recv.src) /
83                          IP(src=dst_ip, dst=src_ip) /
84                          ICMP(type=0))  # echo-reply
85         dst_if.send_pkt(pkt_resp_send)
86
87     pkt_resp_recv = src_if.recv_pkt()
88     if pkt_resp_recv is None:
89         raise Exception('Timeout waiting for packet')
90
91     if is_dst_tg:
92         check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
93         pkt_resp_send_mod = pkt_resp_send.copy()
94         pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
95         del pkt_resp_send_mod[IP].chksum  # update checksum
96         ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
97
98
99 if __name__ == "__main__":
100     main()