Constants.py revision 45b4d916
1# Copyright (c) 2019 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"Constant" means a value that keeps its value since initialization. The value
17does not need to be hard coded here, but can be read from environment variables.
18"""
19
20
21import os
22
23
24def get_str_from_env(env_var_names, default_value):
25    """Attempt to read string from environment variable, return that or default.
26
27    If environment variable exists, but is empty (and default is not),
28    empty string is returned.
29
30    Several environment variable names are examined, as CSIT currently supports
31    a mix of naming conventions.
32    Here "several" means there are hard coded prefixes to try,
33    and env_var_names itself can be single name, or a list or a tuple of names.
34
35    :param env_var_names: Base names of environment variable to attempt to read.
36    :param default_value: Value to return if the env var does not exist.
37    :type env_var_names: str, or list of str, or tuple of str
38    :type default_value: str
39    :returns: The value read, or default value.
40    :rtype: str
41    """
42    prefixes = (u"FDIO_CSIT_", u"CSIT_", u"")
43    if not isinstance(env_var_names, (list, tuple)):
44        env_var_names = [env_var_names]
45    for name in env_var_names:
46        for prefix in prefixes:
47            value = os.environ.get(prefix + name, None)
48            if value is not None:
49                return value
50    return default_value
51
52
53def get_int_from_env(env_var_names, default_value):
54    """Attempt to read int from environment variable, return that or default.
55
56    String value is read, default is returned also if conversion fails.
57
58    :param env_var_names: Base names of environment variable to attempt to read.
59    :param default_value: Value to return if read or conversion fails.
60    :type env_var_names: str, or list of str, or tuple of str
61    :type default_value: int
62    :returns: The value read, or default value.
63    :rtype: int
64    """
65    env_str = get_str_from_env(env_var_names, u"")
66    try:
67        return int(env_str)
68    except ValueError:
69        return default_value
70
71
72def get_float_from_env(env_var_names, default_value):
73    """Attempt to read float from environment variable, return that or default.
74
75    String value is read, default is returned also if conversion fails.
76
77    :param env_var_names: Base names of environment variable to attempt to read.
78    :param default_value: Value to return if read or conversion fails.
79    :type env_var_names: str, or list of str, or tuple of str
80    :type default_value: float
81    :returns: The value read, or default value.
82    :rtype: float
83    """
84    env_str = get_str_from_env(env_var_names, u"")
85    try:
86        return float(env_str)
87    except ValueError:
88        return default_value
89
90
91def get_pessimistic_bool_from_env(env_var_names):
92    """Attempt to read bool from environment variable, assume False by default.
93
94    Conversion is lenient and pessimistic, only few strings are considered true.
95
96    :param env_var_names: Base names of environment variable to attempt to read.
97    :type env_var_names: str, or list of str, or tuple of str
98    :returns: The value read, or False.
99    :rtype: bool
100    """
101    env_str = get_str_from_env(env_var_names, u"").lower()
102    return bool(env_str in (u"true", u"yes", u"y", u"1"))
103
104
105def get_optimistic_bool_from_env(env_var_names):
106    """Attempt to read bool from environment variable, assume True by default.
107
108    Conversion is lenient and optimistic, only few strings are considered false.
109
110    :param env_var_names: Base names of environment variable to attempt to read.
111    :type env_var_names: str, or list of str, or tuple of str
112    :returns: The value read, or True.
113    :rtype: bool
114    """
115    env_str = get_str_from_env(env_var_names, u"").lower()
116    return bool(env_str not in (u"false", u"no", u"n", u"0"))
117
118
119class Constants:
120    """Constants used in CSIT.
121
122    TODO: Yaml files are easier for humans to edit.
123    Figure out how to set the attributes by parsing a file
124    that works regardless of current working directory.
125    """
126
127    # OpenVPP testing directory location at topology nodes
128    REMOTE_FW_DIR = u"/tmp/openvpp-testing"
129
130    # shell scripts location
131    RESOURCES_LIB_SH = u"resources/libraries/bash"
132
133    # Python API provider location
134    RESOURCES_PAPI_PROVIDER = u"resources/tools/papi/vpp_papi_provider.py"
135
136    # vat templates location
137    RESOURCES_TPL_VAT = u"resources/templates/vat"
138
139    # Kubernetes templates location
140    RESOURCES_TPL_K8S = u"resources/templates/kubernetes"
141
142    # KernelVM templates location
143    RESOURCES_TPL_VM = u"resources/templates/vm"
144
145    # Container templates location
146    RESOURCES_TPL_CONTAINER = u"resources/templates/container"
147
148    # HTTP Server www root directory
149    RESOURCES_TP_WRK_WWW = u"resources/traffic_profiles/wrk/www"
150
151    # OpenVPP VAT binary name
152    VAT_BIN_NAME = u"vpp_api_test"
153
154    # VPP service unit name
155    VPP_UNIT = u"vpp"
156
157    # Number of system CPU cores.
158    CPU_CNT_SYSTEM = 1
159
160    # Number of vswitch main thread CPU cores.
161    CPU_CNT_MAIN = 1
162
163    # QEMU binary path
164    QEMU_BIN_PATH = u"/usr/bin"
165
166    # QEMU VM kernel image path
167    QEMU_VM_KERNEL = u"/opt/boot/vmlinuz"
168
169    # QEMU VM kernel initrd path
170    QEMU_VM_KERNEL_INITRD = u"/opt/boot/initrd.img"
171
172    # QEMU VM nested image path
173    QEMU_VM_IMAGE = u"/var/lib/vm/vhost-nested.img"
174
175    # QEMU VM DPDK path
176    QEMU_VM_DPDK = u"/opt/dpdk-19.02"
177
178    # Docker container SUT image
179    DOCKER_SUT_IMAGE_UBUNTU = u"snergster/csit-sut:latest"
180
181    # Docker container arm SUT image
182    DOCKER_SUT_IMAGE_UBUNTU_ARM = u"snergster/csit-arm-sut:latest"
183
184    # TRex install directory
185    TREX_INSTALL_DIR = u"/opt/trex-core-2.73"
186
187    # TRex limit memory.
188    TREX_LIMIT_MEMORY = get_int_from_env(u"TREX_LIMIT_MEMORY ", 4096)
189
190    # TRex number of cores
191    TREX_CORE_COUNT = get_int_from_env(u"TREX_CORE_COUNT", 7)
192
193    # TRex extra commandline arguments
194    TREX_EXTRA_CMDLINE = get_str_from_env(u"TREX_EXTRA_CMDLINE", u"")
195
196    # Sysctl kernel.core_pattern
197    KERNEL_CORE_PATTERN = u"/tmp/%p-%u-%g-%s-%t-%h-%e.core"
198
199    # Core dump directory
200    CORE_DUMP_DIR = u"/tmp"
201
202    # Equivalent to ~0 used in vpp code
203    BITWISE_NON_ZERO = 0xffffffff
204
205    # Default path to VPP API socket.
206    SOCKSVR_PATH = u"/run/vpp/api.sock"
207
208    # Number of trials to execute in MRR test.
209    PERF_TRIAL_MULTIPLICITY = get_int_from_env(u"PERF_TRIAL_MULTIPLICITY", 10)
210
211    # Duration of one trial in MRR test.
212    PERF_TRIAL_DURATION = get_float_from_env(u"PERF_TRIAL_DURATION", 1.0)
213
214    # UUID string of DUT1 /tmp volume created outside of the
215    # DUT1 docker in case of vpp-device test. ${EMPTY} value means that
216    #  /tmp directory is inside the DUT1 docker.
217    DUT1_UUID = get_str_from_env(u"DUT1_UUID", u"")
218
219    # Default path to VPP API Stats socket.
220    SOCKSTAT_PATH = u"/run/vpp/stats.sock"
221
222    # Global "kill switch" for CRC checking during runtime.
223    FAIL_ON_CRC_MISMATCH = get_pessimistic_bool_from_env(
224        u"FAIL_ON_CRC_MISMATCH"
225    )
226
227    # Mapping from NIC name to its bps limit.
228    NIC_NAME_TO_BPS_LIMIT = {
229        u"Cisco-VIC-1227": 10000000000,
230        u"Cisco-VIC-1385": 24500000000,
231        u"Intel-X520-DA2": 10000000000,
232        u"Intel-X553": 10000000000,
233        u"Intel-X710": 10000000000,
234        u"Intel-XL710": 24500000000,
235        u"Intel-XXV710": 24500000000,
236        u"Mellanox-CX556A": 100000000000,
237        u"Amazon-Nitro-50G": 10000000000,
238        u"virtual": 100000000,
239    }
240
241    # Mapping from NIC name to its pps limit.
242    NIC_NAME_TO_PPS_LIMIT = {
243        u"Cisco-VIC-1227": 14880952,
244        u"Cisco-VIC-1385": 18750000,
245        u"Intel-X520-DA2": 14880952,
246        u"Intel-X553": 14880952,
247        u"Intel-X710": 14880952,
248        u"Intel-XL710": 18750000,
249        u"Intel-XXV710": 18750000,
250        u"Mellanox-CX556A": 60000000, # 148809523,
251        u"Amazon-Nitro-50G": 1500000,
252        u"virtual": 14880952,
253    }
254
255    # Suite file names use codes for NICs.
256    NIC_NAME_TO_CODE = {
257        u"Cisco-VIC-1227": u"10ge2p1vic1227",
258        u"Cisco-VIC-1385": u"40ge2p1vic1385",
259        u"Intel-X520-DA2": u"10ge2p1x520",
260        u"Intel-X553": u"10ge2p1x553",
261        u"Intel-X710": u"10ge2p1x710",
262        u"Intel-XL710": u"40ge2p1xl710",
263        u"Intel-XXV710": u"25ge2p1xxv710",
264        u"Mellanox-CX556A": u"100ge2p1cx556a",
265    }
266
267    # TODO CSIT-1481: Crypto HW should be read from topology file instead.
268    NIC_NAME_TO_CRYPTO_HW = {
269        u"Intel-X553": u"HW_C3xxx",
270        u"Intel-X710": u"HW_DH895xcc",
271        u"Intel-XL710": u"HW_DH895xcc",
272    }
273
274    PERF_TYPE_TO_KEYWORD = {
275        u"mrr": u"Traffic should pass with maximum rate",
276        u"ndrpdr": u"Find NDR and PDR intervals using optimized search",
277        u"soak": u"Find critical load using PLRsearch",
278    }
279
280    PERF_TYPE_TO_SUITE_DOC_VER = {
281        u"mrr": u'''fication:* In MaxReceivedRate tests TG sends traffic\\
282| ... | at line rate and reports total received packets over trial period.\\''',
283        # TODO: Figure out how to include the full "*[Ver] TG verification:*"
284        # while keeping this readable and without breaking line length limit.
285        u"ndrpdr": u'''ication:* TG finds and reports throughput NDR (Non Drop\\
286| ... | Rate) with zero packet loss tolerance and throughput PDR (Partial Drop\\
287| ... | Rate) with non-zero packet loss tolerance (LT) expressed in percentage\\
288| ... | of packets transmitted. NDR and PDR are discovered for different\\
289| ... | Ethernet L2 frame sizes using MLRsearch library.\\''',
290        u"soak": u'''fication:* TG sends traffic at dynamically computed\\
291| ... | rate as PLRsearch algorithm gathers data and improves its estimate\\
292| ... | of a rate at which a prescribed small fraction of packets\\
293| ... | would be lost. After set time, the serarch stops\\
294| ... | and the algorithm reports its current estimate.\\''',
295    }
296
297    PERF_TYPE_TO_TEMPLATE_DOC_VER = {
298        u"mrr": u'''Measure MaxReceivedRate for ${frame_size}B frames\\
299| | ... | using burst trials throughput test.\\''',
300        u"ndrpdr": u"Measure NDR and PDR values using MLRsearch algorithm.\\",
301        u"soak": u"Estimate critical rate using PLRsearch algorithm.\\",
302    }
303