Fixed strings with format splitting
[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 = auto_pad(pkt_send)
34     pkt_recv_raw = auto_pad(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(str(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(str(create_gratuitous_arp_request(dst_mac, dst_ip)))
70
71 pkt_req_send = (Ether(src=src_mac, dst=first_hop_mac) /
72                       IP(src=src_ip, dst=dst_ip) /
73                       ICMP())
74 src_if.send_pkt(pkt_req_send)
75
76 if dst_if_defined:
77     try:
78         pkt_req_recv = dst_if.recv_pkt()
79         if pkt_req_recv is None:
80             raise Exception('Timeout waiting for packet')
81     except:
82         src_if.close()
83         if dst_if_defined:
84             dst_if.close()
85         raise
86
87     check_ttl(pkt_req_send[IP].ttl, pkt_req_recv[IP].ttl, hops)
88     pkt_req_send_mod = pkt_req_send.copy()
89     pkt_req_send_mod[IP].ttl = pkt_req_recv[IP].ttl
90     del pkt_req_send_mod[IP].chksum  # update checksum
91     ckeck_packets_equal(pkt_req_send_mod[IP], pkt_req_recv[IP])
92
93     pkt_resp_send = (Ether(src=dst_mac, dst=pkt_req_recv.src) /
94                            IP(src=dst_ip, dst=src_ip) /
95                            ICMP(type=0))  # echo-reply
96     dst_if.send_pkt(pkt_resp_send)
97
98 try:
99     pkt_resp_recv = src_if.recv_pkt()
100     if pkt_resp_recv is None:
101         raise Exception('Timeout waiting for packet')
102 except:
103     src_if.close()
104     if dst_if_defined:
105         dst_if.close()
106     raise
107
108 if dst_if_defined:
109     check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
110     pkt_resp_send_mod = pkt_resp_send.copy()
111     pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
112     del pkt_resp_send_mod[IP].chksum  # update checksum
113     ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
114
115 src_if.close()
116 if dst_if_defined:
117     dst_if.close()