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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 """Traffic script that sends an IP ICMPv4/ICMPv6 packets traffic
16 and check if it is divided into two paths."""
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
26 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
32 ipaddress.IPv4Address(unicode(ip))
34 except (AttributeError, ipaddress.AddressValueError):
40 ipaddress.IPv6Address(unicode(ip))
42 except (AttributeError, ipaddress.AddressValueError):
47 """Send 100 IP ICMP packets traffic and check if it is divided into
49 args = TrafficScriptArg(
50 ['src_ip', 'dst_ip', 'tg_if1_mac', 'dut_if1_mac', 'dut_if2_mac',
51 'path_1_mac', 'path_2_mac'])
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')
72 if valid_ipv4(src_ip):
74 elif valid_ipv6(src_ip):
77 raise ValueError("Source address not in correct format")
79 src_ip_base = (src_ip.rsplit(separator, 1))[0] + separator
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) /
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) /
93 raise ValueError("IP not in correct format")
95 sent_packets.append(pkt_raw)
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__()))
105 if ether['Ethernet'].src != dut_if2_mac:
106 raise RuntimeError("Source MAC address error")
108 if ether['Ethernet'].dst == path_1_mac:
110 elif ether['Ethernet'].dst == path_2_mac:
113 raise RuntimeError("Destination MAC address error")
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))
119 if path_1_counter == 0:
120 raise RuntimeError("Path 1 error!")
122 if path_2_counter == 0:
123 raise RuntimeError("Path 2 error!")
125 print "Path_1 counter: {}".format(path_1_counter)
126 print "Path_2 counter: {}".format(path_2_counter)
130 if __name__ == "__main__":