d998d7c6a0aca20f412de8362ab31258a91cfedf
[csit.git] / resources / traffic_scripts / send_vxlangpe_nsh_for_proxy_test.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 VxLAN-GPE+NSH packet
16 from TG to DUT.
17 """
18 import sys
19 import time
20
21 from scapy.layers.inet import IP, UDP, TCP
22 from scapy.layers.inet6 import IPv6
23 from scapy.all import Ether, Packet, Raw
24
25 from resources.libraries.python.SFC.VerifyPacket import *
26 from resources.libraries.python.SFC.SFCConstants import SFCConstants as sfccon
27 from resources.libraries.python.TrafficScriptArg import TrafficScriptArg
28 from resources.libraries.python.PacketVerifier import RxQueue, TxQueue
29
30 from robot.api import logger
31
32 def main():
33     """Send VxLAN-GPE+NSH packet from TG to DUT.
34
35     :raises: If the IP address is invalid.
36     """
37     args = TrafficScriptArg(
38         ['src_mac', 'dst_mac', 'src_ip', 'dst_ip',
39         'timeout', 'framesize', 'testtype'])
40
41     src_mac = args.get_arg('src_mac')
42     dst_mac = args.get_arg('dst_mac')
43     src_ip = args.get_arg('src_ip')
44     dst_ip = args.get_arg('dst_ip')
45     tx_if = args.get_arg('tx_if')
46     rx_if = args.get_arg('rx_if')
47     timeout = int(args.get_arg('timeout'))
48     frame_size = int(args.get_arg('framesize'))
49     test_type = args.get_arg('testtype')
50
51     rxq = RxQueue(rx_if)
52     txq = TxQueue(tx_if)
53     sent_packets = []
54
55     protocol = TCP
56     source_port = sfccon.DEF_SRC_PORT
57     destination_port = sfccon.DEF_DST_PORT
58
59     ip_version = None
60     if valid_ipv4(src_ip) and valid_ipv4(dst_ip):
61         ip_version = IP
62     elif valid_ipv6(src_ip) and valid_ipv6(dst_ip):
63         ip_version = IPv6
64     else:
65         raise ValueError("Invalid IP version!")
66
67     innerpkt = (Ether(src=src_mac, dst=dst_mac) /
68                ip_version(src=src_ip, dst=dst_ip) /
69                protocol(sport=int(source_port), dport=int(destination_port)))
70
71     vxlangpe_nsh = '\x0c\x00\x00\x04\x00\x00\x09\x00\x00\x06' \
72                    '\x01\x03\x00\x00\xb9\xff\xC0\xA8\x32\x4B' \
73                    '\x00\x00\x00\x09\xC0\xA8\x32\x48\x03\x00\x12\xB5'
74
75     raw_data = vxlangpe_nsh + str(innerpkt)
76
77     pkt_header = (Ether(src=src_mac, dst=dst_mac) /
78               ip_version(src=src_ip, dst=dst_ip) /
79               UDP(sport=int(source_port), dport=4790) /
80               Raw(load=raw_data))
81
82     fsize_no_fcs = frame_size - 4
83     pad_len = max(0, fsize_no_fcs - len(pkt_header))
84     pad_data = "A" * pad_len
85
86     pkt_raw = pkt_header / Raw(load=pad_data)
87
88     # Send created packet on one interface and receive on the other
89     sent_packets.append(pkt_raw)
90     txq.send(pkt_raw)
91
92     ether = rxq.recv(2)
93
94     if ether is None:
95         raise RuntimeError("No packet is received!")
96
97     # let us begin to check the proxy inbound packet
98     VerifyPacket.check_the_nsh_sfc_packet(ether, frame_size, test_type)
99
100     # we check all the fields about the proxy inbound, this test will pass
101     sys.exit(0)
102
103
104 if __name__ == "__main__":
105     main()