f8f5309764b5a543d091e626dcebe1f44f81a5b1
[csit.git] / resources / traffic_scripts / send_icmp_check_headers.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 icmp packet
16 from one interface to the other.
17 """
18
19 import sys
20 import ipaddress
21
22 from scapy.layers.inet import ICMP, IP
23 from scapy.layers.inet6 import IPv6
24 from scapy.all import Ether
25 from scapy.layers.inet6 import ICMPv6EchoRequest
26 from robot.api import logger
27
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
29 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
30
31
32 def valid_ipv4(ip):
33     try:
34         ipaddress.IPv4Address(unicode(ip))
35         return True
36     except (AttributeError, ipaddress.AddressValueError):
37         return False
38
39
40 def valid_ipv6(ip):
41     try:
42         ipaddress.IPv6Address(unicode(ip))
43         return True
44     except (AttributeError, ipaddress.AddressValueError):
45         return False
46
47
48 def main():
49     """Send IP ICMP packet from one traffic generator interface to the other."""
50     args = TrafficScriptArg(
51         ['tg_src_mac', 'tg_dst_mac', 'src_ip', 'dst_ip', 'dut_if1_mac',
52          'dut_if2_mac'])
53
54     src_mac = args.get_arg('tg_src_mac')
55     dst_mac = args.get_arg('tg_dst_mac')
56     dut1_if1_mac = args.get_arg('dut_if1_mac')
57     dut1_if2_mac = args.get_arg('dut_if2_mac')
58     src_ip = args.get_arg('src_ip')
59     dst_ip = args.get_arg('dst_ip')
60     tx_if = args.get_arg('tx_if')
61     rx_if = args.get_arg('rx_if')
62
63     rxq = RxQueue(rx_if)
64     txq = TxQueue(tx_if)
65     sent_packets = []
66     ip_format = ''
67     pkt_raw = ''
68     if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
69         pkt_raw = (Ether(src=src_mac, dst=dut1_if1_mac) /
70                    IP(src=src_ip, dst=dst_ip) /
71                    ICMP())
72         ip_format = 'IP'
73     elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
74         pkt_raw = (Ether(src=src_mac, dst=dut1_if1_mac) /
75                    IPv6(src=src_ip, dst=dst_ip) /
76                    ICMPv6EchoRequest())
77         ip_format = 'IPv6'
78     else:
79         raise ValueError("IP not in correct format")
80
81     sent_packets.append(pkt_raw)
82     txq.send(pkt_raw)
83     ether = rxq.recv(2)
84
85     if ether is None:
86         raise RuntimeError("ICMP echo Rx timeout")
87     if not ether.haslayer(ip_format):
88         raise RuntimeError("Not an IP packet received {0}"
89                            .format(ether.__repr__()))
90
91     # Compare data from packets
92     if src_ip == ether[ip_format].src and dst_ip == ether[ip_format].dst:
93         logger.trace("IP matched")
94         if dst_mac == ether['Ethernet'].dst and \
95                 dut1_if2_mac == ether['Ethernet'].src:
96             logger.trace("MAC matched")
97         else:
98             raise RuntimeError("Matching packet unsuccessful: {0}"
99                                .format(ether.__repr__()))
100     else:
101         raise RuntimeError("Matching packet unsuccessful: {0}"
102                            .format(ether.__repr__()))
103     sys.exit(0)
104
105
106 if __name__ == "__main__":
107     main()