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