Add Honeycomb persistence tests
[csit.git] / resources / traffic_scripts / send_icmp_wait_for_reply.py
1 #!/usr/bin/env python
2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Traffic script that sends an IP ICMPv4."""
16
17 import sys
18
19 from scapy.layers.inet import ICMP, IP
20 from scapy.all import Ether
21
22 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
23 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
24
25
26 def is_icmp_reply(pkt):
27     """Return True if pkt is echo reply, else return False. If exception occurs
28     return False."""
29     try:
30         if pkt['IP']['ICMP'].type == 0:  # 0 - echo-reply
31             return True
32         else:
33             return False
34     except:
35         return False
36
37
38 def address_check(request, reply):
39     """Compare request packet source address with reply destination address
40     and vice versa. If exception occurs return False."""
41     try:
42         return reply['IP'].src == request['IP'].dst \
43                and reply['IP'].dst == request['IP'].src
44     except:
45         return False
46
47
48 def main():
49     """Send ICMP echo request and wait for ICMP echo reply. It ignores all other
50     packets."""
51     args = TrafficScriptArg(['dst_mac', 'src_mac', 'dst_ip', 'src_ip',
52                              'timeout'])
53
54     dst_mac = args.get_arg('dst_mac')
55     src_mac = args.get_arg('src_mac')
56     dst_ip = args.get_arg('dst_ip')
57     src_ip = args.get_arg('src_ip')
58     tx_if = args.get_arg('tx_if')
59     rx_if = args.get_arg('rx_if')
60     timeout = int(args.get_arg('timeout'))
61     wait_step = 1
62
63     rxq = RxQueue(rx_if)
64     txq = TxQueue(tx_if)
65     sent_packets = []
66
67     # Create empty ip ICMP packet
68     icmp_request = (Ether(src=src_mac, dst=dst_mac) /
69                     IP(src=src_ip, dst=dst_ip) /
70                     ICMP())
71     # Send created packet on the interface
72     sent_packets.append(icmp_request)
73     txq.send(icmp_request)
74
75     for _ in range(1000):
76         icmp_reply = rxq.recv(wait_step)
77         if icmp_reply is None:
78             timeout -= wait_step
79             if timeout < 0:
80                 raise RuntimeError("ICMP echo Rx timeout")
81         elif is_icmp_reply(icmp_reply):
82             if address_check(icmp_request, icmp_reply):
83                 break
84     else:
85         raise RuntimeError("Max packet count limit reached")
86
87     print "ICMP echo reply received."
88
89     sys.exit(0)
90
91 if __name__ == "__main__":
92     main()