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