Use only Qemu Kill on the teardown of double qemu setup
[csit.git] / resources / traffic_scripts / send_icmp_check_multipath.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 packets traffic
16 and check if it is divided into two paths."""
17
18 import sys
19 import ipaddress
20
21 from scapy.layers.inet import ICMP, IP
22 from scapy.layers.inet6 import IPv6
23 from scapy.all import Ether
24 from scapy.layers.inet6 import ICMPv6EchoRequest
25
26 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28
29
30 def valid_ipv4(ip):
31     try:
32         ipaddress.IPv4Address(unicode(ip))
33         return True
34     except (AttributeError, ipaddress.AddressValueError):
35         return False
36
37
38 def valid_ipv6(ip):
39     try:
40         ipaddress.IPv6Address(unicode(ip))
41         return True
42     except (AttributeError, ipaddress.AddressValueError):
43         return False
44
45
46 def main():
47     """Send 100 IP ICMP packets traffic and check if it is divided into
48     two paths."""
49     args = TrafficScriptArg(
50         ['src_ip', 'dst_ip', 'tg_if1_mac', 'dut_if1_mac', 'dut_if2_mac',
51          'path_1_mac', 'path_2_mac'])
52
53     src_ip = args.get_arg('src_ip')
54     dst_ip = args.get_arg('dst_ip')
55     tg_if1_mac = args.get_arg('tg_if1_mac')
56     dut_if1_mac = args.get_arg('dut_if1_mac')
57     dut_if2_mac = args.get_arg('dut_if2_mac')
58     path_1_mac = args.get_arg('path_1_mac')
59     path_2_mac = args.get_arg('path_2_mac')
60     tx_if = args.get_arg('tx_if')
61     rx_if = args.get_arg('rx_if')
62     path_1_counter = 0
63     path_2_counter = 0
64
65     rxq = RxQueue(rx_if)
66     txq = TxQueue(tx_if)
67     sent_packets = []
68     ip_format = ''
69     pkt_raw = ''
70     separator = ''
71
72     if valid_ipv4(src_ip):
73         separator = '.'
74     elif valid_ipv6(src_ip):
75         separator = ':'
76     else:
77         raise ValueError("Source address not in correct format")
78
79     src_ip_base = (src_ip.rsplit(separator, 1))[0] + separator
80
81     for i in range(1, 101):
82         if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
83             pkt_raw = (Ether(src=tg_if1_mac, dst=dut_if1_mac) /
84                        IP(src=src_ip_base+str(i), dst=dst_ip) /
85                        ICMP())
86             ip_format = 'IP'
87         elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
88             pkt_raw = (Ether(src=tg_if1_mac, dst=dut_if1_mac) /
89                        IPv6(src=src_ip_base+str(i), dst=dst_ip) /
90                        ICMPv6EchoRequest())
91             ip_format = 'IPv6'
92         else:
93             raise ValueError("IP not in correct format")
94
95         sent_packets.append(pkt_raw)
96         txq.send(pkt_raw)
97         ether = rxq.recv(2)
98
99         if ether is None:
100             raise RuntimeError("ICMP echo Rx timeout")
101         if not ether.haslayer(ip_format):
102             raise RuntimeError("Not an IP packet received {0}"
103                                .format(ether.__repr__()))
104
105         if ether['Ethernet'].src != dut_if2_mac:
106             raise RuntimeError("Source MAC address error")
107
108         if ether['Ethernet'].dst == path_1_mac:
109             path_1_counter += 1
110         elif ether['Ethernet'].dst == path_2_mac:
111             path_2_counter += 1
112         else:
113             raise RuntimeError("Destination MAC address error")
114
115     if (path_1_counter + path_2_counter) != 100:
116         raise RuntimeError("Packet loss: recevied only {} packets of 100 "
117                            .format(path_1_counter + path_2_counter))
118
119     if path_1_counter == 0:
120         raise RuntimeError("Path 1 error!")
121
122     if path_2_counter == 0:
123         raise RuntimeError("Path 2 error!")
124
125     print "Path_1 counter: {}".format(path_1_counter)
126     print "Path_2 counter: {}".format(path_2_counter)
127
128     sys.exit(0)
129
130 if __name__ == "__main__":
131     main()