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 icmp packet
16 from one interface to the other.
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
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
29 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
34 ipaddress.IPv4Address(unicode(ip))
36 except (AttributeError, ipaddress.AddressValueError):
42 ipaddress.IPv6Address(unicode(ip))
44 except (AttributeError, ipaddress.AddressValueError):
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',
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')
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) /
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) /
79 raise ValueError("IP not in correct format")
81 sent_packets.append(pkt_raw)
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__()))
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")
98 raise RuntimeError("Matching packet unsuccessful: {0}"
99 .format(ether.__repr__()))
101 raise RuntimeError("Matching packet unsuccessful: {0}"
102 .format(ether.__repr__()))
106 if __name__ == "__main__":