CSIT-1288: Prepare data to be sent by Jenkins
[csit.git] / resources / traffic_scripts / send_icmp_type_code.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 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 ICMPv4/ICMPv6 packet from one interface to
16 the other one. Dot1q or Dot1ad tagging of the ethernet frame can be set.
17 """
18
19 import sys
20 import ipaddress
21
22 from scapy.layers.l2 import Ether
23 from scapy.layers.inet import ICMP, IP
24 from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest, ICMPv6ND_NS
25
26 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28
29
30 def valid_ipv4(ip):
31     """Check if IP address has the correct IPv4 address format.
32
33     :param ip: IP address.
34     :type ip: str
35     :returns: True in case of correct IPv4 address format,
36     otherwise return false.
37     :rtype: bool
38     """
39     try:
40         ipaddress.IPv4Address(unicode(ip))
41         return True
42     except (AttributeError, ipaddress.AddressValueError):
43         return False
44
45
46 def valid_ipv6(ip):
47     """Check if IP address has the correct IPv6 address format.
48
49     :param ip: IP address.
50     :type ip: str
51     :returns: True in case of correct IPv6 address format,
52     otherwise return false.
53     :rtype: bool
54     """
55     try:
56         ipaddress.IPv6Address(unicode(ip))
57         return True
58     except (AttributeError, ipaddress.AddressValueError):
59         return False
60
61
62 def main():
63     """Send IP ICMPv4/ICMPv6 packet from one traffic generator interface to
64     the other one."""
65
66     args = TrafficScriptArg(['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
67                             'icmp_type', 'icmp_code'])
68
69     src_mac = args.get_arg('src_mac')
70     dst_mac = args.get_arg('dst_mac')
71     src_ip = args.get_arg('src_ip')
72     dst_ip = args.get_arg('dst_ip')
73
74     tx_if = args.get_arg('tx_if')
75     rx_if = args.get_arg('rx_if')
76
77     icmp_type = int(args.get_arg('icmp_type'))
78     icmp_code = int(args.get_arg('icmp_code'))
79
80     rxq = RxQueue(rx_if)
81     txq = TxQueue(tx_if)
82
83     sent_packets = []
84
85     # Create empty ip ICMP packet and add padding before sending
86     if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
87         pkt_raw = (Ether(src=src_mac, dst=dst_mac) /
88                    IP(src=src_ip, dst=dst_ip) /
89                    ICMP(code=icmp_code, type=icmp_type))
90         ip_format = IP
91         icmp_format = ICMP
92     elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
93         pkt_raw = (Ether(src=src_mac, dst=dst_mac) /
94                    IPv6(src=src_ip, dst=dst_ip) /
95                    ICMPv6EchoRequest(code=icmp_code, type=icmp_type))
96         ip_format = IPv6
97         icmp_format = 'ICMPv6'
98     else:
99         raise ValueError("IP(s) not in correct format")
100
101     # Send created packet on one interface and receive on the other
102     sent_packets.append(pkt_raw)
103     txq.send(pkt_raw)
104
105     while True:
106         ether = rxq.recv(2)
107         if ether is None:
108             raise RuntimeError('ICMP echo Rx timeout')
109
110         if ether.haslayer(ICMPv6ND_NS):
111             # read another packet in the queue if the current one is ICMPv6ND_NS
112             continue
113         else:
114             # otherwise process the current packet
115             break
116
117     # Check whether received packet contains layers IP and ICMP
118     if not ether.haslayer(ip_format):
119         raise RuntimeError('Not an IP packet received {0}'.
120                            format(ether.__repr__()))
121
122     # Cannot use haslayer for ICMPv6, every type of ICMPv6 is a separate layer
123     # Next header value of 58 means the next header is ICMPv6
124     if not ether.haslayer(icmp_format) and ether[ip_format].nh != 58:
125         raise RuntimeError('Not an ICMP packet received {0}'.
126                            format(ether.__repr__()))
127
128     sys.exit(0)
129
130
131 if __name__ == "__main__":
132     main()