Remove Interface.close, rename script argument
[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, hops))
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 args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
43                          'hops', 'first_hop_mac', 'is_dst_tg'])
44
45 src_if_name = args.get_arg('tx_if')
46 dst_if_name = args.get_arg('rx_if')
47 is_dst_tg = True if args.get_arg('is_dst_tg') == 'True' else False
48
49 src_mac = args.get_arg('src_mac')
50 first_hop_mac = args.get_arg('first_hop_mac')
51 dst_mac = args.get_arg('dst_mac')
52 src_ip = args.get_arg('src_ip')
53 dst_ip = args.get_arg('dst_ip')
54 hops = int(args.get_arg('hops'))
55
56 if is_dst_tg and (src_if_name == dst_if_name):
57     raise Exception("Source interface name equals destination interface name")
58
59 src_if = Interface(src_if_name)
60 src_if.send_pkt(str(create_gratuitous_arp_request(src_mac, src_ip)))
61 if is_dst_tg:
62     dst_if = Interface(dst_if_name)
63     dst_if.send_pkt(str(create_gratuitous_arp_request(dst_mac, dst_ip)))
64
65 pkt_req_send = (Ether(src=src_mac, dst=first_hop_mac) /
66                       IP(src=src_ip, dst=dst_ip) /
67                       ICMP())
68 src_if.send_pkt(pkt_req_send)
69
70 if is_dst_tg:
71     pkt_req_recv = dst_if.recv_pkt()
72     if pkt_req_recv is None:
73         raise Exception('Timeout waiting for packet')
74
75     check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
76     pkt_req_send_mod = pkt_req_send.copy()
77     pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
78     del pkt_req_send_mod[IP].chksum  # update checksum
79     ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
80
81     pkt_resp_send = (Ether(src=dst_mac, dst=pkt_req_recv.src) /
82                            IP(src=dst_ip, dst=src_ip) /
83                            ICMP(type=0))  # echo-reply
84     dst_if.send_pkt(pkt_resp_send)
85
86 pkt_resp_recv = src_if.recv_pkt()
87 if pkt_resp_recv is None:
88     raise Exception('Timeout waiting for packet')
89
90 if is_dst_tg:
91     check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
92     pkt_resp_send_mod = pkt_resp_send.copy()
93     pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
94     del pkt_resp_send_mod[IP].chksum  # update checksum
95     ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])