Add documentation and files related to initial host setup
[csit.git] / resources / traffic_scripts / send_icmp_check_arp.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 and expects ARP on the other one.
17 """
18
19 import sys
20 import ipaddress
21
22 from scapy.all import Ether
23 from scapy.layers.inet import ICMP, IP
24
25 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
26 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
27
28
29 def valid_ipv4(ip):
30     """Check if IP address has the correct IPv4 address format.
31
32     :param ip: IP address.
33     :type ip: str
34     :return: True in case of correct IPv4 address format,
35              otherwise return False.
36     :rtype: bool
37     """
38     try:
39         ipaddress.IPv4Address(unicode(ip))
40         return True
41     except (AttributeError, ipaddress.AddressValueError):
42         return False
43
44
45 def main():
46     """Send IP ICMP packet from one traffic generator interface and expects
47      ARP on the other."""
48     args = TrafficScriptArg(
49         ['tx_dst_mac', 'rx_dst_mac', 'tx_src_ip', 'tx_dst_ip', 'rx_arp_src_ip',
50          'rx_arp_dst_ip'])
51
52     tx_dst_mac = args.get_arg('tx_dst_mac')
53     rx_dst_mac = args.get_arg('rx_dst_mac')
54     src_ip = args.get_arg('tx_src_ip')
55     dst_ip = args.get_arg('tx_dst_ip')
56     tx_if = args.get_arg('tx_if')
57     rx_if = args.get_arg('rx_if')
58     rx_arp_src_ip = args.get_arg('rx_arp_src_ip')
59     rx_arp_dst_ip = args.get_arg('rx_arp_dst_ip')
60
61     rxq = RxQueue(rx_if)
62     txq = TxQueue(tx_if)
63
64     # Create empty IP ICMP packet
65     if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
66         pkt_raw = Ether(dst=tx_dst_mac) / IP(src=src_ip, dst=dst_ip) / ICMP()
67
68     # Send created packet on one interface and receive on the other
69     txq.send(pkt_raw)
70
71     ether = rxq.recv(2)
72
73     if ether is None:
74         raise RuntimeError("Ethernet frame Rx timeout")
75
76     # ARP check
77     if ether['ARP'] is not None:
78         print("ARP packet received.")
79     else:
80         raise RuntimeError("Not an ARP packet received {0}"
81                            .format(ether.__repr__()))
82
83     # Compare data from packets
84     if ether['ARP'].op == 1:  # 1 - who-has request
85         print("ARP request matched.")
86     else:
87         raise RuntimeError("Matching ARP request unsuccessful: {0}"
88                            .format(ether.__repr__()))
89
90     if ether['ARP'].hwsrc == rx_dst_mac:
91         print("Source MAC matched.")
92     else:
93         raise RuntimeError("Matching Source MAC unsuccessful: {0}"
94                            .format(ether.__repr__()))
95
96     if ether['ARP'].hwdst == "00:00:00:00:00:00":
97         print("Destination MAC matched.")
98     else:
99         raise RuntimeError("Matching Destination MAC unsuccessful: {0}"
100                            .format(ether.__repr__()))
101
102     if ether['ARP'].psrc == rx_arp_src_ip:
103         print("Source ARP IP address matched.")
104     else:
105         raise RuntimeError("Matching Source ARP IP address unsuccessful: {0}"
106                            .format(ether.__repr__()))
107
108     if ether['ARP'].pdst == rx_arp_dst_ip:
109         print("Destination ARP IP address matched.")
110     else:
111         raise RuntimeError("Matching Destination ARP IP address unsuccessful: "
112                            "{0}".format(ether.__repr__()))
113
114     sys.exit(0)
115
116
117 if __name__ == "__main__":
118     main()