2fd9d552ea0d8bec9778f365f4c8a0c7f486116e
[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         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 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
49                          'hops', 'first_hop_mac', 'is_dst_defined'])
50
51 src_if_name = args.get_arg('tx_if')
52 dst_if_name = args.get_arg('rx_if')
53 dst_if_defined = True if args.get_arg('is_dst_defined') == 'True' else False
54
55 src_mac = args.get_arg('src_mac')
56 first_hop_mac = args.get_arg('first_hop_mac')
57 dst_mac = args.get_arg('dst_mac')
58 src_ip = args.get_arg('src_ip')
59 dst_ip = args.get_arg('dst_ip')
60 hops = int(args.get_arg('hops'))
61
62 if dst_if_defined and (src_if_name == dst_if_name):
63     raise Exception("Source interface name equals destination interface name")
64
65 src_if = Interface(src_if_name)
66 src_if.send_pkt(create_gratuitous_arp_request(src_mac, src_ip))
67 if dst_if_defined:
68     dst_if = Interface(dst_if_name)
69     dst_if.send_pkt(create_gratuitous_arp_request(dst_mac, dst_ip))
70
71 pkt_req_send = auto_pad(Ether(src=src_mac, dst=first_hop_mac) /
72                         IP(src=src_ip, dst=dst_ip) /
73                         ICMP())
74 pkt_req_send = Ether(pkt_req_send)
75 src_if.send_pkt(pkt_req_send)
76
77 if dst_if_defined:
78     try:
79         pkt_req_recv = dst_if.recv_pkt()
80     except:
81         src_if.close()
82         if dst_if_defined:
83             dst_if.close()
84         raise
85
86     check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
87     pkt_req_send_mod = pkt_req_send.copy()
88     pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
89     del pkt_req_send_mod[IP].chksum  # update checksum
90     ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
91
92     pkt_resp_send = auto_pad(Ether(src=dst_mac, dst=pkt_req_recv.src) /
93                              IP(src=dst_ip, dst=src_ip) /
94                              ICMP(type=0))  # echo-reply
95     pkt_resp_send = Ether(pkt_resp_send)
96     dst_if.send_pkt(pkt_resp_send)
97
98 try:
99     pkt_resp_recv = src_if.recv_pkt()
100 except:
101     src_if.close()
102     if dst_if_defined:
103         dst_if.close()
104     raise
105
106 if dst_if_defined:
107     check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
108     pkt_resp_send_mod = pkt_resp_send.copy()
109     pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
110     del pkt_resp_send_mod[IP].chksum  # update checksum
111     ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
112
113 src_if.close()
114 if dst_if_defined:
115     dst_if.close()