Backport CRC checking from master
[csit.git] / resources / libraries / python / Constants.py
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Constants used in CSIT."""
15
16
17 import os
18
19
20 def get_str_from_env(env_var_names, default_value):
21     """Attempt to read string from environment variable, return that or default.
22
23     If environment variable exists, but is empty (and default is not),
24     empty string is returned.
25
26     Several environment variable names are examined, as CSIT currently supports
27     a mix of naming conventions.
28     Here "several" means there are hard coded prefixes to try,
29     and env_var_names itself can be single name, or a list or a tuple of names.
30
31     :param env_var_names: Base names of environment variable to attempt to read.
32     :param default_value: Value to return if the env var does not exist.
33     :type env_var_names: str, or list of str, or tuple of str
34     :type default_value: str
35     :returns: The value read, or default value.
36     :rtype: str
37     """
38     prefixes = ("FDIO_CSIT_", "CSIT_", "")
39     if not isinstance(env_var_names, (list, tuple)):
40         env_var_names = [env_var_names]
41     for name in env_var_names:
42         for prefix in prefixes:
43             value = os.environ.get(prefix + name, None)
44             if value is not None:
45                 return value
46     return default_value
47
48
49 def get_int_from_env(env_var_names, default_value):
50     """Attempt to read int from environment variable, return that or default.
51
52     String value is read, default is returned also if conversion fails.
53
54     :param env_var_names: Base names of environment variable to attempt to read.
55     :param default_value: Value to return if read or conversion fails.
56     :type env_var_names: str, or list of str, or tuple of str
57     :type default_value: int
58     :returns: The value read, or default value.
59     :rtype: int
60     """
61     env_str = get_str_from_env(env_var_names, "")
62     try:
63         return int(env_str)
64     except ValueError:
65         return default_value
66
67
68 def get_float_from_env(env_var_names, default_value):
69     """Attempt to read float from environment variable, return that or default.
70
71     String value is read, default is returned also if conversion fails.
72
73     :param env_var_names: Base names of environment variable to attempt to read.
74     :param default_value: Value to return if read or conversion fails.
75     :type env_var_names: str, or list of str, or tuple of str
76     :type default_value: float
77     :returns: The value read, or default value.
78     :rtype: float
79     """
80     env_str = get_str_from_env(env_var_names, "")
81     try:
82         return float(env_str)
83     except ValueError:
84         return default_value
85
86
87 def get_pessimistic_bool_from_env(env_var_names):
88     """Attempt to read bool from environment variable, assume False by default.
89
90     Conversion is lenient and pessimistic, only few strings are considered true.
91
92     :param env_var_names: Base names of environment variable to attempt to read.
93     :type env_var_names: str, or list of str, or tuple of str
94     :returns: The value read, or False.
95     :rtype: bool
96     """
97     env_str = get_str_from_env(env_var_names, "").lower()
98     return bool(env_str in ("true", "yes", "y", "1"))
99
100
101 def get_optimistic_bool_from_env(env_var_names):
102     """Attempt to read bool from environment variable, assume True by default.
103
104     Conversion is lenient and optimistic, only few strings are considered false.
105
106     :param env_var_names: Base names of environment variable to attempt to read.
107     :type env_var_names: str, or list of str, or tuple of str
108     :returns: The value read, or True.
109     :rtype: bool
110     """
111     env_str = get_str_from_env(env_var_names, "").lower()
112     return bool(env_str not in ("false", "no", "n", "0"))
113
114
115 class Constants(object):
116     """Constants used in CSIT.
117
118     TODO: Yaml files are easier for humans to edit.
119     Figure out how to set the attributes by parsing a file
120     that works regardless of current working directory.
121     """
122
123     # OpenVPP testing directory location at topology nodes
124     REMOTE_FW_DIR = '/tmp/openvpp-testing'
125
126     # shell scripts location
127     RESOURCES_LIB_SH = 'resources/libraries/bash'
128
129     # Python API provider location
130     RESOURCES_PAPI_PROVIDER = 'resources/tools/papi/vpp_papi_provider.py'
131
132     # vat templates location
133     RESOURCES_TPL_VAT = 'resources/templates/vat'
134
135     # Kubernetes templates location
136     RESOURCES_TPL_K8S = 'resources/templates/kubernetes'
137
138     # KernelVM templates location
139     RESOURCES_TPL_VM = 'resources/templates/vm'
140
141     # Container templates location
142     RESOURCES_TPL_CONTAINER = 'resources/templates/container'
143
144     # HTTP Server www root directory
145     RESOURCES_TP_WRK_WWW = 'resources/traffic_profiles/wrk/www'
146
147     # OpenVPP VAT binary name
148     VAT_BIN_NAME = 'vpp_api_test'
149
150     # VPP service unit name
151     VPP_UNIT = 'vpp'
152
153     # Number of system CPU cores.
154     CPU_CNT_SYSTEM = 1
155
156     # Number of vswitch main thread CPU cores.
157     CPU_CNT_MAIN = 1
158
159     # QEMU binary path
160     QEMU_BIN_PATH = '/usr/bin'
161
162     # QEMU VM kernel image path
163     QEMU_VM_KERNEL = '/opt/boot/vmlinuz'
164
165     # QEMU VM kernel initrd path
166     QEMU_VM_KERNEL_INITRD = '/opt/boot/initrd.img'
167
168     # QEMU VM nested image path
169     QEMU_VM_IMAGE = '/var/lib/vm/vhost-nested.img'
170
171     # QEMU VM DPDK path
172     QEMU_VM_DPDK = '/opt/dpdk-19.02'
173
174     # Docker container SUT image
175     DOCKER_SUT_IMAGE_UBUNTU = 'snergster/csit-sut:latest'
176
177     # Docker container arm SUT image
178     DOCKER_SUT_IMAGE_UBUNTU_ARM = 'snergster/csit-arm-sut:latest'
179
180     # TRex install version
181     TREX_INSTALL_VERSION = '2.54'
182
183     # TRex install directory
184     TREX_INSTALL_DIR = '/opt/trex-core-2.54'
185
186     # Honeycomb directory location at topology nodes:
187     REMOTE_HC_DIR = '/opt/honeycomb'
188
189     # Honeycomb persistence files location
190     REMOTE_HC_PERSIST = '/var/lib/honeycomb/persist'
191
192     # Honeycomb log file location
193     REMOTE_HC_LOG = '/var/log/honeycomb/honeycomb.log'
194
195     # Honeycomb templates location
196     RESOURCES_TPL_HC = 'resources/templates/honeycomb'
197
198     # ODL Client Restconf listener port
199     ODL_PORT = 8181
200
201     # Sysctl kernel.core_pattern
202     KERNEL_CORE_PATTERN = '/tmp/%p-%u-%g-%s-%t-%h-%e.core'
203
204     # Core dump directory
205     CORE_DUMP_DIR = '/tmp'
206
207     # Equivalent to ~0 used in vpp code
208     BITWISE_NON_ZERO = 0xffffffff
209
210     # Global "kill switch" for CRC checking during runtime.
211     FAIL_ON_CRC_MISMATCH = get_pessimistic_bool_from_env("FAIL_ON_CRC_MISMATCH")
212
213     # Mapping from NIC name to its bps limit.
214     # TODO: Implement logic to lower limits to TG NIC or software. Or PCI.
215     NIC_NAME_TO_LIMIT = {
216         # TODO: Explain why ~40Gbps NICs are using ~25Gbps limit.
217         "Cisco-VIC-1227": 10000000000,
218         "Cisco-VIC-1385": 24500000000,
219         "Intel-X520-DA2": 10000000000,
220         "Intel-X553": 10000000000,
221         "Intel-X710": 10000000000,
222         "Intel-XL710": 24500000000,
223         "Intel-XXV710": 24500000000,
224         "virtual": 100000000,
225     }
226
227     # Suite file names use somewhat more rich (less readable) codes for NICs.
228     NIC_NAME_TO_CODE = {
229         "Cisco-VIC-1227": "10ge2p1vic1227",
230         "Cisco-VIC-1385": "40ge2p1vic1385",
231         "Intel-X520-DA2": "10ge2p1x520",
232         "Intel-X553": "10ge2p1x553",
233         "Intel-X710": "10ge2p1x710",
234         "Intel-XL710": "40ge2p1xl710",
235         "Intel-XXV710": "25ge2p1xxv710",
236     }
237
238     # TODO CSIT-1481: Crypto HW should be read from topology file instead.
239     NIC_NAME_TO_CRYPTO_HW = {
240         "Intel-X553": "HW_C3xxx",
241         "Intel-X710": "HW_DH895xcc",
242         "Intel-XL710": "HW_DH895xcc",
243     }
244
245     PERF_TYPE_TO_KEYWORD = {
246         "mrr": "Traffic should pass with maximum rate",
247         "ndrpdr": "Find NDR and PDR intervals using optimized search",
248         "soak": "Find critical load using PLRsearch",
249     }
250
251     PERF_TYPE_TO_SUITE_DOC_VER = {
252         "mrr" : '''fication:* In MaxReceivedRate tests TG sends traffic\\
253 | ... | at line rate and reports total received packets over trial period.\\''',
254         # TODO: Figure out how to include the full "*[Ver] TG verification:*"
255         # while keeping this readable and without breaking line length limit.
256         "ndrpdr": '''fication:* TG finds and reports throughput NDR (Non Drop\\
257 | ... | Rate) with zero packet loss tolerance and throughput PDR (Partial Drop\\
258 | ... | Rate) with non-zero packet loss tolerance (LT) expressed in percentage\\
259 | ... | of packets transmitted. NDR and PDR are discovered for different\\
260 | ... | Ethernet L2 frame sizes using MLRsearch library.\\''',
261         "soak": '''fication:* TG sends traffic at dynamically computed\\
262 | ... | rate as PLRsearch algorithm gathers data and improves its estimate\\
263 | ... | of a rate at which a prescribed small fraction of packets\\
264 | ... | would be lost. After set time, the serarch stops\\
265 | ... | and the algorithm reports its current estimate.\\''',
266     }
267
268     PERF_TYPE_TO_TEMPLATE_DOC_VER = {
269         "mrr": '''Measure MaxReceivedRate for ${frame_size}B frames\\
270 | | ... | using burst trials throughput test.\\''',
271         "ndrpdr": '''Measure NDR and PDR values using MLRsearch algorithm.\\''',
272         "soak": '''Estimate critical rate using PLRsearch algorithm.\\''',
273     }