4 Vhosts setup test
[csit.git] / resources / traffic_scripts / send_gre_check_icmp_headers.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 a GRE encapsulated ICMPv4 packet from one
16 interface to the other, where is expected ICMPv4 without GRE header.
17 """
18
19 import sys
20
21 from robot.api import logger
22 from scapy.all import Ether
23 from scapy.layers.inet import IP_PROTOS
24 from scapy.layers.inet import IP
25 from scapy.layers.inet import ICMP
26 from scapy.layers.l2 import GRE
27
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
29 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
30
31
32 def main():
33     args = TrafficScriptArg(
34         ['tx_dst_mac', 'rx_dst_mac',
35          'inner_src_ip', 'inner_dst_ip',
36          'outer_src_ip', 'outer_dst_ip'])
37
38     tx_if = args.get_arg('tx_if')
39     rx_if = args.get_arg('rx_if')
40     tx_dst_mac = args.get_arg('tx_dst_mac')
41     rx_dst_mac = args.get_arg('rx_dst_mac')
42     inner_src_ip = args.get_arg('inner_src_ip')
43     inner_dst_ip = args.get_arg('inner_dst_ip')
44     outer_src_ip = args.get_arg('outer_src_ip')
45     outer_dst_ip = args.get_arg('outer_dst_ip')
46
47     rxq = RxQueue(rx_if)
48     txq = TxQueue(tx_if)
49     sent_packets = []
50
51     tx_pkt_raw = Ether(dst=tx_dst_mac) / \
52         IP(src=outer_src_ip, dst=outer_dst_ip) / \
53         GRE() / \
54         IP(src=inner_src_ip, dst=inner_dst_ip) / \
55         ICMP()
56
57     sent_packets.append(tx_pkt_raw)
58     txq.send(tx_pkt_raw)
59     ether = rxq.recv(2)
60
61     if ether is None:
62         raise RuntimeError("ICMP echo Rx timeout")
63
64     # Check RX headers
65     if ether.dst != rx_dst_mac:
66         raise RuntimeError("Matching of received destination MAC unsuccessful.")
67     logger.debug("Comparison of received destination MAC: OK.")
68
69     if ether['IP'].src != inner_src_ip:
70         raise RuntimeError("Matching of received inner source IP unsuccessful.")
71     logger.debug("Comparison of received outer source IP: OK.")
72
73     if ether['IP'].dst != inner_dst_ip:
74         raise RuntimeError(
75             "Matching of received inner destination IP unsuccessful.")
76     logger.debug("Comparison of received outer destination IP: OK.")
77
78     if ether['IP'].proto != IP_PROTOS.icmp:
79         raise RuntimeError("IP protocol is other than ICMP.")
80     logger.debug("Comparison of received ICMP protocol: OK.")
81
82     sys.exit(0)
83
84
85 if __name__ == "__main__":
86     main()