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