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