Change the bootstrap script file and test the CI-management.
[csit.git] / resources / libraries / python / SFC / VerifyPacket.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 This module defines the common functions.
17 """
18
19 import ipaddress
20
21 from scapy.layers.inet import IP, UDP
22 from scapy.all import Raw
23 from resources.libraries.python.constants import Constants as con
24 from resources.libraries.python.SFC.SFCConstants import SFCConstants as sfccon
25 from resources.libraries.python.SFC.TunnelProtocol import VxLAN, VxLANGPE, NSH
26
27 from robot.api import logger
28
29 def valid_ipv4(ipaddr):
30     """Check if IP address has the correct IPv4 address format.
31
32     :param ipaddr: IP address.
33     :type ipaddr: str
34     :returns: True in case of correct IPv4 address format,
35               otherwise return False.
36     :rtype: bool
37     """
38     try:
39         ipaddress.IPv4Address(unicode(ipaddr))
40         return True
41     except (AttributeError, ipaddress.AddressValueError):
42         return False
43
44
45 def valid_ipv6(ipaddr):
46     """Check if IP address has the correct IPv6 address format.
47
48     :param ipaddr: IP address.
49     :type ipaddr: str
50     :returns: True in case of correct IPv6 address format,
51               otherwise return False.
52     :rtype: bool
53     """
54     try:
55         ipaddress.IPv6Address(unicode(ipaddr))
56         return True
57     except (AttributeError, ipaddress.AddressValueError):
58         return False
59
60
61 class VerifyPacket(object):
62     """Define some functions for the test filed verify."""
63
64     @staticmethod
65     def check_vxlan_protocol(payload_data):
66         """
67         verify the vxlan protocol in the payload data.
68
69         :param payload_data: the payload data in the packet.
70         :type payload_data: str
71         :returns: none
72         :raises RuntimeError: If the vxlan protocol field verify fails.
73         """
74         # get the vxlan packet and check it
75         vxlan_pkt = VxLAN(payload_data[0:8])
76         if vxlan_pkt.flags != sfccon.VxLAN_FLAGS:
77             raise RuntimeError("Unexpected Vxlan flags: {0}".
78                                format(vxlan_pkt.flags))
79
80         if vxlan_pkt.vni != sfccon.VxLAN_DEFAULT_VNI:
81             raise RuntimeError("Unexpected VNI flag: {0}".format(vxlan_pkt.vni))
82
83     @staticmethod
84     def check_vxlangpe_nsh_protocol(payload_data, test_type):
85         """
86         verify the vxlangpe and nsh protocol in the payload data.
87
88         :param payload_data: the payload data in the packet.
89         :param test_type: the functional test type.
90         :type payload_data: str
91         :type test_type: str
92         :returns: none
93         :raises RuntimeError: If the vxlangpe and nsh protocol
94                               field verify fails.
95         """
96         # get the vxlan-gpe packet and check it
97         vxlangpe_pkt = VxLANGPE(payload_data[0:8])
98         if vxlangpe_pkt.flags != sfccon.VxLANGPE_FLAGS:
99             raise RuntimeError("Unexpected Vxlan-GPE flags: {0}".
100                                format(vxlangpe_pkt.flags))
101
102         if vxlangpe_pkt.nextproto != sfccon.VxLANGPE_NEXT_PROTOCOL:
103             raise RuntimeError("next protocol not the NSH")
104
105         if vxlangpe_pkt.vni != sfccon.VxLANGPE_DEFAULT_VNI:
106             raise RuntimeError("Unexpected VNI flag: {0}".
107                                format(vxlangpe_pkt.vni))
108
109         # get the NSH packet and check it
110         nsh_pkt = NSH(payload_data[8:32])
111         if nsh_pkt.flags != sfccon.NSH_FLAGS:
112             raise RuntimeError("Unexpected NSH flags: {0}".
113                                format(nsh_pkt.flags))
114
115         if nsh_pkt.length != sfccon.NSH_HEADER_LENGTH:
116             raise RuntimeError("NSH length {0} incorrect".
117                                format(nsh_pkt.length))
118
119         if nsh_pkt.MDtype != sfccon.NSH_DEFAULT_MDTYPE:
120             raise RuntimeError("NSH MD-Type {0} incorrect".
121                                format(nsh_pkt.MDtype))
122
123         if nsh_pkt.nextproto != sfccon.NSH_NEXT_PROTOCOL:
124             raise RuntimeError("NSH next protocol {0} incorrect".
125                                format(nsh_pkt.nextproto))
126
127         if test_type == "Proxy Outbound" or test_type == "SFF":
128             expect_nsi = sfccon.NSH_DEFAULT_NSI - 1
129         else:
130             expect_nsi = sfccon.NSH_DEFAULT_NSI
131
132         nsp_nsi = nsh_pkt.nsp_nsi
133         nsp = nsp_nsi >> 8
134         nsi = nsp_nsi & 0x000000FF
135         if nsp != sfccon.NSH_DEFAULT_NSP:
136             raise RuntimeError("NSH Service Path ID {0} incorrect".format(nsp))
137
138         if nsi != expect_nsi:
139             raise RuntimeError("NSH Service Index {0} incorrect".format(nsi))
140
141         nsh_c1 = nsh_pkt.c1
142         if nsh_c1 != sfccon.NSH_DEFAULT_C1:
143             raise RuntimeError("NSH c1 {0} incorrect".format(nsh_c1))
144
145         nsh_c2 = nsh_pkt.c2
146         if nsh_c2 != sfccon.NSH_DEFAULT_C2:
147             raise RuntimeError("NSH c2 {0} incorrect".format(nsh_c2))
148
149         nsh_c3 = nsh_pkt.c3
150         if nsh_c3 != sfccon.NSH_DEFAULT_C3:
151             raise RuntimeError("NSH c3 {0} incorrect".format(nsh_c3))
152
153         nsh_c4 = nsh_pkt.c4
154         if nsh_c4 != sfccon.NSH_DEFAULT_C4:
155             raise RuntimeError("NSH c4 {0} incorrect".format(nsh_c4))
156
157
158     @staticmethod
159     def check_the_nsh_sfc_packet(ether, frame_size, test_type):
160         """
161         verify the NSH SFC functional test loopback packet field
162         is correct.
163
164         :param ether: The Ethernet packet data.
165         :param frame_size: The origin frame size.
166         :param test_type: The test type.
167                          (Classifier, Proxy Inbound, Proxy Outbound, SFF).
168
169         :type ether: scapy.Ether
170         :type frame_size: Integer
171         :type test_type: str
172         :returns: none
173         :raises RuntimeError: If the packet field verify fails.
174         """
175
176         origin_size = int(frame_size)
177         if test_type == "Classifier":
178             expect_pkt_len = origin_size + 74 - 4
179         elif test_type == "Proxy Inbound":
180             expect_pkt_len = origin_size - 24 - 4
181         elif test_type == "Proxy Outbound":
182             expect_pkt_len = origin_size + 24 - 4
183         else:
184             expect_pkt_len = origin_size - 4
185
186         recv_pkt_len = len(ether)
187         if recv_pkt_len != expect_pkt_len:
188             raise RuntimeError("Received packet size {0} not " \
189                                "the expect size {1}".format(recv_pkt_len, \
190                                expect_pkt_len))
191
192         if not ether.haslayer(IP):
193             raise RuntimeError("Not a IPv4 packet")
194
195         pkt_proto = ether[IP].proto
196         if pkt_proto != sfccon.UDP_PROTOCOL:
197             raise RuntimeError("Not a UDP packet , {0}".format(pkt_proto))
198
199         if test_type == "Proxy Inbound":
200             expect_udp_port = sfccon.VxLAN_UDP_PORT
201         else:
202             expect_udp_port = sfccon.VxLANGPE_UDP_PORT
203
204         dst_port = ether[UDP].dport
205         if dst_port != expect_udp_port:
206             raise RuntimeError("UDP dest port must be {0}, {1}".
207                                format(expect_udp_port, dst_port))
208
209         payload_data = ether[Raw].load
210
211         if test_type == "Proxy Inbound":
212             VerifyPacket.check_vxlan_protocol(payload_data)
213         else:
214             VerifyPacket.check_vxlangpe_nsh_protocol(payload_data, test_type)