711bf3dd2544090fa89c3508f659d3fc4fdd049e
[csit.git] / resources / traffic_scripts / send_ip_icmp.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 icmp packet
16 from one interface to the other"""
17
18 import sys
19 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
20 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
21 from scapy.layers.inet import ICMP, IP
22 from scapy.all import Ether
23
24
25 def main():
26     """ Send IP icmp packet from one traffic generator interface to the other"""
27     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip'])
28
29     src_mac = args.get_arg('src_mac')
30     dst_mac = args.get_arg('dst_mac')
31     src_ip = args.get_arg('src_ip')
32     dst_ip = args.get_arg('dst_ip')
33     tx_if = args.get_arg('tx_if')
34     rx_if = args.get_arg('rx_if')
35
36     rxq = RxQueue(rx_if)
37     txq = TxQueue(tx_if)
38
39     sent_packets = []
40
41     # Create empty ip ICMP packet and add padding before sending
42     pkt_raw = Ether(src=src_mac, dst=dst_mac) / \
43               IP(src=src_ip, dst=dst_ip) / \
44               ICMP()
45
46     # Send created packet on one interface and receive on the other
47     sent_packets.append(pkt_raw)
48     txq.send(pkt_raw)
49
50     ether = rxq.recv(2)
51
52     # Check whether received packet contains layers Ether, IP and ICMP
53     if ether is None:
54         raise RuntimeError('ICMP echo Rx timeout')
55
56     if not ether.haslayer(IP):
57         raise RuntimeError(
58             'Not an IP packet received {0}'.format(ether.__repr__()))
59
60     if not ether.haslayer(ICMP):
61         raise RuntimeError(
62             'Not an ICMP packet received {0}'.format(ether.__repr__()))
63
64     sys.exit(0)
65
66 if __name__ == "__main__":
67     main()