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."""
19 from scapy.layers.inet import ICMP, IP
20 from scapy.all import Ether
22 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
23 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
26 def is_icmp_reply(pkt):
27 """Return True if pkt is echo reply, else return False. If exception occurs
30 if pkt['IP']['ICMP'].type == 0: # 0 - echo-reply
38 def address_check(request, reply):
39 """Compare request packet source address with reply destination address
40 and vice versa. If exception occurs return False."""
42 return reply['IP'].src == request['IP'].dst \
43 and reply['IP'].dst == request['IP'].src
49 """Send ICMP echo request and wait for ICMP echo reply. It ignores all other
51 args = TrafficScriptArg(['dst_mac', 'src_mac', 'dst_ip', 'src_ip',
54 dst_mac = args.get_arg('dst_mac')
55 src_mac = args.get_arg('src_mac')
56 dst_ip = args.get_arg('dst_ip')
57 src_ip = args.get_arg('src_ip')
58 tx_if = args.get_arg('tx_if')
59 rx_if = args.get_arg('rx_if')
60 timeout = int(args.get_arg('timeout'))
67 # Create empty ip ICMP packet
68 icmp_request = (Ether(src=src_mac, dst=dst_mac) /
69 IP(src=src_ip, dst=dst_ip) /
71 # Send created packet on the interface
72 sent_packets.append(icmp_request)
73 txq.send(icmp_request)
76 icmp_reply = rxq.recv(wait_step)
77 if icmp_reply is None:
80 raise RuntimeError("ICMP echo Rx timeout")
81 elif is_icmp_reply(icmp_reply):
82 if address_check(icmp_request, icmp_reply):
85 raise RuntimeError("Max packet count limit reached")
87 print "ICMP echo reply received."
91 if __name__ == "__main__":