1 # Copyright (c) 2022 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """VPP Configuration File Generator library."""
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.ssh import exec_cmd_no_error
20 from resources.libraries.python.topology import NodeType
21 from resources.libraries.python.topology import Topology
22 from resources.libraries.python.VPPUtil import VPPUtil
24 __all__ = [u"VppConfigGenerator"]
27 def pci_dev_check(pci_dev):
28 """Check if provided PCI address is in correct format.
30 :param pci_dev: PCI address (expected format: xxxx:xx:xx.x).
32 :returns: True if PCI address is in correct format.
34 :raises ValueError: If PCI address is in incorrect format.
37 r"^[0-9A-Fa-f]{4}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}\.[0-9A-Fa-f]$"
39 if not re.match(pattern, pci_dev):
41 f"PCI address {pci_dev} is not in valid format xxxx:xx:xx.x"
46 class VppConfigGenerator:
47 """VPP Configuration File Generator."""
50 """Initialize library."""
51 # VPP Node to apply configuration on
56 self._nodeconfig = dict()
57 # Serialized VPP Configuration
58 self._vpp_config = u""
60 self._vpp_service_name = u"vpp"
61 # VPP Logfile location
62 self._vpp_logfile = u"/tmp/vpe.log"
63 # VPP Startup config location
64 self._vpp_startup_conf = u"/etc/vpp/startup.conf"
66 def set_node(self, node, node_key=None):
69 :param node: Node to store configuration on.
70 :param node_key: Topology node key.
73 :raises RuntimeError: If Node type is not DUT.
75 if node[u"type"] != NodeType.DUT:
77 u"Startup config can only be applied to DUTnode."
80 self._node_key = node_key
82 def get_config_str(self):
83 """Get dumped startup configuration in VPP config format.
85 :returns: Startup configuration in VPP config format.
88 self.dump_config(self._nodeconfig)
89 return self._vpp_config
91 def add_config_item(self, config, value, path):
92 """Add startup configuration item.
94 :param config: Startup configuration of node.
95 :param value: Value to insert.
96 :param path: Path where to insert item.
102 config[path[0]] = value
104 if path[0] not in config:
105 config[path[0]] = dict()
106 elif isinstance(config[path[0]], str):
107 config[path[0]] = dict() if config[path[0]] == u"" \
108 else {config[path[0]]: u""}
109 self.add_config_item(config[path[0]], value, path[1:])
111 def dump_config(self, obj, level=-1):
112 """Dump the startup configuration in VPP config format.
114 :param obj: Python Object to print.
115 :param level: Nested level for indentation.
122 self._vpp_config += f"{level * indent}{{\n"
123 if isinstance(obj, dict):
124 for key, val in obj.items():
125 if hasattr(val, u"__iter__") and not isinstance(val, str):
126 self._vpp_config += f"{(level + 1) * indent}{key}\n"
127 self.dump_config(val, level + 1)
129 self._vpp_config += f"{(level + 1) * indent}{key} {val}\n"
132 self._vpp_config += f"{(level + 1) * indent}{val}\n"
134 self._vpp_config += f"{level * indent}}}\n"
136 def add_unix_log(self, value=None):
137 """Add UNIX log configuration.
139 :param value: Log file.
142 path = [u"unix", u"log"]
144 value = self._vpp_logfile
145 self.add_config_item(self._nodeconfig, value, path)
147 def add_unix_cli_listen(self, value=u"/run/vpp/cli.sock"):
148 """Add UNIX cli-listen configuration.
150 :param value: CLI listen address and port or path to CLI socket.
153 path = [u"unix", u"cli-listen"]
154 self.add_config_item(self._nodeconfig, value, path)
156 def add_unix_cli_no_pager(self):
157 """Add UNIX cli-no-pager configuration."""
158 path = [u"unix", u"cli-no-pager"]
159 self.add_config_item(self._nodeconfig, u"", path)
161 def add_unix_gid(self, value=u"vpp"):
162 """Add UNIX gid configuration.
167 path = [u"unix", u"gid"]
168 self.add_config_item(self._nodeconfig, value, path)
170 def add_unix_nodaemon(self):
171 """Add UNIX nodaemon configuration."""
172 path = [u"unix", u"nodaemon"]
173 self.add_config_item(self._nodeconfig, u"", path)
175 def add_unix_coredump(self):
176 """Add UNIX full-coredump configuration."""
177 path = [u"unix", u"full-coredump"]
178 self.add_config_item(self._nodeconfig, u"", path)
180 def add_unix_exec(self, value):
181 """Add UNIX exec configuration."""
182 path = [u"unix", u"exec"]
183 self.add_config_item(self._nodeconfig, value, path)
185 def add_socksvr(self, socket=Constants.SOCKSVR_PATH):
186 """Add socksvr configuration."""
187 path = [u"socksvr", u"socket-name"]
188 self.add_config_item(self._nodeconfig, socket, path)
190 def add_graph_node_variant(self, variant=Constants.GRAPH_NODE_VARIANT):
191 """Add default graph node variant.
193 :param value: Graph node variant default value.
198 variant_list = [u"hsw", u"skx", u"icl"]
199 if variant not in variant_list:
200 raise ValueError("Invalid graph node variant value")
201 path = [u"node", u"default", u"variant"]
202 self.add_config_item(self._nodeconfig, variant, path)
204 def add_api_segment_gid(self, value=u"vpp"):
205 """Add API-SEGMENT gid configuration.
210 path = [u"api-segment", u"gid"]
211 self.add_config_item(self._nodeconfig, value, path)
213 def add_api_segment_global_size(self, value):
214 """Add API-SEGMENT global-size configuration.
216 :param value: Global size.
219 path = [u"api-segment", u"global-size"]
220 self.add_config_item(self._nodeconfig, value, path)
222 def add_api_segment_api_size(self, value):
223 """Add API-SEGMENT api-size configuration.
225 :param value: API size.
228 path = [u"api-segment", u"api-size"]
229 self.add_config_item(self._nodeconfig, value, path)
231 def add_buffers_per_numa(self, value):
232 """Increase number of buffers allocated.
234 :param value: Number of buffers allocated.
237 path = [u"buffers", u"buffers-per-numa"]
238 self.add_config_item(self._nodeconfig, value, path)
240 def add_buffers_default_data_size(self, value):
241 """Increase buffers data-size allocated.
243 :param value: Buffers data-size allocated.
246 path = [u"buffers", u"default data-size"]
247 self.add_config_item(self._nodeconfig, value, path)
249 def add_dpdk_dev(self, *devices):
250 """Add DPDK PCI device configuration.
252 :param devices: PCI device(s) (format xxxx:xx:xx.x)
255 for device in devices:
256 if pci_dev_check(device):
257 path = [u"dpdk", f"dev {device}"]
258 self.add_config_item(self._nodeconfig, u"", path)
260 def add_dpdk_dev_parameter(self, device, parameter, value):
261 """Add parameter for DPDK device.
263 :param device: PCI device (format xxxx:xx:xx.x).
264 :param parameter: Parameter name.
265 :param value: Parameter value.
270 if pci_dev_check(device):
271 path = [u"dpdk", f"dev {device}", parameter]
272 self.add_config_item(self._nodeconfig, value, path)
274 def add_dpdk_cryptodev(self, count):
275 """Add DPDK Crypto PCI device configuration.
277 :param count: Number of HW crypto devices to add.
280 cryptodev = Topology.get_cryptodev(self._node)
281 for i in range(count):
282 cryptodev_config = re.sub(r"\d.\d$", f"1.{str(i)}", cryptodev)
283 path = [u"dpdk", f"dev {cryptodev_config}"]
284 self.add_config_item(self._nodeconfig, u"", path)
285 self.add_dpdk_uio_driver(u"vfio-pci")
287 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
288 """Add DPDK SW Crypto device configuration.
290 :param sw_pmd_type: Type of SW crypto device PMD to add.
291 :param socket_id: Socket ID.
292 :param count: Number of SW crypto devices to add.
293 :type sw_pmd_type: str
297 for _ in range(count):
298 cryptodev_config = f"vdev cryptodev_{sw_pmd_type}_pmd," \
299 f"socket_id={str(socket_id)}"
300 path = [u"dpdk", cryptodev_config]
301 self.add_config_item(self._nodeconfig, u"", path)
303 def add_dpdk_dev_default_rxq(self, value):
304 """Add DPDK dev default rxq configuration.
306 :param value: Default number of rxqs.
309 path = [u"dpdk", u"dev default", u"num-rx-queues"]
310 self.add_config_item(self._nodeconfig, value, path)
312 def add_dpdk_dev_default_txq(self, value):
313 """Add DPDK dev default txq configuration.
315 :param value: Default number of txqs.
318 path = [u"dpdk", u"dev default", u"num-tx-queues"]
319 self.add_config_item(self._nodeconfig, value, path)
321 def add_dpdk_dev_default_rxd(self, value):
322 """Add DPDK dev default rxd configuration.
324 :param value: Default number of rxds.
327 path = [u"dpdk", u"dev default", u"num-rx-desc"]
328 self.add_config_item(self._nodeconfig, value, path)
330 def add_dpdk_dev_default_txd(self, value):
331 """Add DPDK dev default txd configuration.
333 :param value: Default number of txds.
336 path = [u"dpdk", u"dev default", u"num-tx-desc"]
337 self.add_config_item(self._nodeconfig, value, path)
339 def add_dpdk_log_level(self, value):
340 """Add DPDK log-level configuration.
342 :param value: Log level.
345 path = [u"dpdk", u"log-level"]
346 self.add_config_item(self._nodeconfig, value, path)
348 def add_dpdk_no_pci(self):
349 """Add DPDK no-pci."""
350 path = [u"dpdk", u"no-pci"]
351 self.add_config_item(self._nodeconfig, u"", path)
353 def add_dpdk_uio_driver(self, value=None):
354 """Add DPDK uio-driver configuration.
356 :param value: DPDK uio-driver configuration. By default, driver will be
357 loaded automatically from Topology file, still leaving option
358 to manually override by parameter.
362 value = Topology.get_uio_driver(self._node)
363 path = [u"dpdk", u"uio-driver"]
364 self.add_config_item(self._nodeconfig, value, path)
366 def add_dpdk_max_simd_bitwidth(self, variant=Constants.GRAPH_NODE_VARIANT):
367 """Add DPDK max-simd-bitwidth configuration.
369 :param value: Graph node variant default value.
372 if variant == u"icl":
374 elif variant in [u"skx", u"hsw"]:
379 path = [u"dpdk", u"max-simd-bitwidth"]
380 self.add_config_item(self._nodeconfig, value, path)
382 def add_cpu_main_core(self, value):
383 """Add CPU main core configuration.
385 :param value: Main core option.
388 path = [u"cpu", u"main-core"]
389 self.add_config_item(self._nodeconfig, value, path)
391 def add_cpu_corelist_workers(self, value):
392 """Add CPU corelist-workers configuration.
394 :param value: Corelist-workers option.
397 path = [u"cpu", u"corelist-workers"]
398 self.add_config_item(self._nodeconfig, value, path)
400 def add_main_heap_size(self, value):
401 """Add Main Heap Size configuration.
403 :param value: Amount of heap.
406 path = [u"memory", u"main-heap-size"]
407 self.add_config_item(self._nodeconfig, value, path)
409 def add_main_heap_page_size(self, value):
410 """Add Main Heap Page Size configuration.
412 :param value: Heap page size.
415 path = [u"memory", u"main-heap-page-size"]
416 self.add_config_item(self._nodeconfig, value, path)
418 def add_default_hugepage_size(self, value=Constants.DEFAULT_HUGEPAGE_SIZE):
419 """Add Default Hugepage Size configuration.
421 :param value: Hugepage size.
424 path = [u"memory", u"default-hugepage-size"]
425 self.add_config_item(self._nodeconfig, value, path)
427 def add_api_trace(self):
428 """Add API trace configuration."""
429 path = [u"api-trace", u"on"]
430 self.add_config_item(self._nodeconfig, u"", path)
432 def add_ip6_hash_buckets(self, value):
433 """Add IP6 hash buckets configuration.
435 :param value: Number of IP6 hash buckets.
438 path = [u"ip6", u"hash-buckets"]
439 self.add_config_item(self._nodeconfig, value, path)
441 def add_ip6_heap_size(self, value):
442 """Add IP6 heap-size configuration.
444 :param value: IP6 Heapsize amount.
447 path = [u"ip6", u"heap-size"]
448 self.add_config_item(self._nodeconfig, value, path)
450 def add_spd_flow_cache_ipv4_outbound(self):
451 """Add SPD flow cache for IP4 outbound traffic"""
452 path = [u"ipsec", u"ipv4-outbound-spd-flow-cache"]
453 self.add_config_item(self._nodeconfig, "on", path)
455 def add_statseg_size(self, value):
456 """Add Stats Heap Size configuration.
458 :param value: Stats heapsize amount.
461 path = [u"statseg", u"size"]
462 self.add_config_item(self._nodeconfig, value, path)
464 def add_statseg_page_size(self, value):
465 """Add Stats Heap Page Size configuration.
467 :param value: Stats heapsize amount.
470 path = [u"statseg", u"page-size"]
471 self.add_config_item(self._nodeconfig, value, path)
473 def add_statseg_per_node_counters(self, value):
474 """Add stats per-node-counters configuration.
476 :param value: "on" to switch the counters on.
479 path = [u"statseg", u"per-node-counters"]
480 self.add_config_item(self._nodeconfig, value, path)
482 def add_plugin(self, state, *plugins):
483 """Add plugin section for specific plugin(s).
485 :param state: State of plugin [enable|disable].
486 :param plugins: Plugin(s) to disable.
490 for plugin in plugins:
491 path = [u"plugins", f"plugin {plugin}", state]
492 self.add_config_item(self._nodeconfig, u" ", path)
494 def add_dpdk_no_multi_seg(self):
495 """Add DPDK no-multi-seg configuration."""
496 path = [u"dpdk", u"no-multi-seg"]
497 self.add_config_item(self._nodeconfig, u"", path)
499 def add_dpdk_no_tx_checksum_offload(self):
500 """Add DPDK no-tx-checksum-offload configuration."""
501 path = [u"dpdk", u"no-tx-checksum-offload"]
502 self.add_config_item(self._nodeconfig, u"", path)
504 def add_nat(self, value=u"deterministic"):
505 """Add NAT mode configuration.
507 :param value: NAT mode.
510 path = [u"nat", value]
511 self.add_config_item(self._nodeconfig, u"", path)
513 def add_nat_max_translations_per_thread(self, value):
514 """Add NAT max. translations per thread number configuration.
516 :param value: NAT mode.
519 path = [u"nat", u"max translations per thread"]
520 self.add_config_item(self._nodeconfig, value, path)
522 def add_nsim_poll_main_thread(self):
523 """Add NSIM poll-main-thread configuration."""
524 path = [u"nsim", u"poll-main-thread"]
525 self.add_config_item(self._nodeconfig, u"", path)
527 def add_tcp_congestion_control_algorithm(self, value=u"cubic"):
528 """Add TCP congestion control algorithm.
530 :param value: The congestion control algorithm to use. Example: cubic
533 path = [u"tcp", u"cc-algo"]
534 self.add_config_item(self._nodeconfig, value, path)
536 def add_tcp_preallocated_connections(self, value):
537 """Add TCP pre-allocated connections.
539 :param value: The number of pre-allocated connections.
542 path = [u"tcp", u"preallocated-connections"]
543 self.add_config_item(self._nodeconfig, value, path)
545 def add_tcp_preallocated_half_open_connections(self, value):
546 """Add TCP pre-allocated half open connections.
548 :param value: The number of pre-allocated half open connections.
551 path = [u"tcp", u"preallocated-half-open-connections"]
552 self.add_config_item(self._nodeconfig, value, path)
554 def add_session_enable(self):
555 """Add session enable."""
556 path = [u"session", u"enable"]
557 self.add_config_item(self._nodeconfig, u"", path)
559 def add_session_event_queues_memfd_segment(self):
560 """Add session event queue memfd segment."""
561 path = [u"session", u"evt_qs_memfd_seg"]
562 self.add_config_item(self._nodeconfig, u"", path)
564 def add_session_event_queue_length(self, value):
565 """Add session event queue length.
567 :param value: Session event queue length.
570 path = [u"session", u"event-queue-length"]
571 self.add_config_item(self._nodeconfig, value, path)
573 def add_session_event_queues_segment_size(self, value):
574 """Add session event queue length.
576 :param value: Session event queue segment size.
579 path = [u"session", u"evt_qs_seg_size"]
580 self.add_config_item(self._nodeconfig, value, path)
582 def add_session_preallocated_sessions(self, value):
583 """Add the number of pre-allocated sessions.
585 :param value: Number of pre-allocated sessions.
588 path = [u"session", u"preallocated-sessions"]
589 self.add_config_item(self._nodeconfig, value, path)
591 def add_session_v4_session_table_buckets(self, value):
592 """Add number of v4 session table buckets to the config.
594 :param value: Number of v4 session table buckets.
597 path = [u"session", u"v4-session-table-buckets"]
598 self.add_config_item(self._nodeconfig, value, path)
600 def add_session_v4_session_table_memory(self, value):
601 """Add the size of v4 session table memory.
603 :param value: Size of v4 session table memory.
606 path = [u"session", u"v4-session-table-memory"]
607 self.add_config_item(self._nodeconfig, value, path)
609 def add_session_v4_halfopen_table_buckets(self, value):
610 """Add the number of v4 halfopen table buckets.
612 :param value: Number of v4 halfopen table buckets.
615 path = [u"session", u"v4-halfopen-table-buckets"]
616 self.add_config_item(self._nodeconfig, value, path)
618 def add_session_v4_halfopen_table_memory(self, value):
619 """Add the size of v4 halfopen table memory.
621 :param value: Size of v4 halfopen table memory.
624 path = [u"session", u"v4-halfopen-table-memory"]
625 self.add_config_item(self._nodeconfig, value, path)
627 def add_session_local_endpoints_table_buckets(self, value):
628 """Add the number of local endpoints table buckets.
630 :param value: Number of local endpoints table buckets.
633 path = [u"session", u"local-endpoints-table-buckets"]
634 self.add_config_item(self._nodeconfig, value, path)
636 def add_session_local_endpoints_table_memory(self, value):
637 """Add the size of local endpoints table memory.
639 :param value: Size of local endpoints table memory.
642 path = [u"session", u"local-endpoints-table-memory"]
643 self.add_config_item(self._nodeconfig, value, path)
645 def write_config(self, filename=None):
646 """Generate and write VPP startup configuration to file.
648 Use data from calls to this class to form a startup.conf file and
649 replace /etc/vpp/startup.conf with it on topology node.
651 :param filename: Startup configuration file name.
654 self.dump_config(self._nodeconfig)
657 filename = self._vpp_startup_conf
659 cmd = f"echo \"{self._vpp_config}\" | sudo tee {filename}"
661 self._node, cmd, message=u"Writing config file failed!"
664 def apply_config(self, filename=None, verify_vpp=True):
665 """Generate and write VPP startup configuration to file and restart VPP.
667 Use data from calls to this class to form a startup.conf file and
668 replace /etc/vpp/startup.conf with it on topology node.
670 :param filename: Startup configuration file name.
671 :param verify_vpp: Verify VPP is running after restart.
673 :type verify_vpp: bool
675 self.write_config(filename=filename)
677 VPPUtil.restart_vpp_service(self._node, self._node_key)
679 VPPUtil.verify_vpp(self._node)