1 # Copyright (c) 2023 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__ = ["VppConfigGenerator", "VppInitConfig"]
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 Startup config location
62 self._vpp_startup_conf = u"/etc/vpp/startup.conf"
64 def set_node(self, node, node_key=None):
67 :param node: Node to store configuration on.
68 :param node_key: Topology node key.
71 :raises RuntimeError: If Node type is not DUT.
73 if node[u"type"] != NodeType.DUT:
75 u"Startup config can only be applied to DUTnode."
78 self._node_key = node_key
80 def get_config_str(self):
81 """Get dumped startup configuration in VPP config format.
83 :returns: Startup configuration in VPP config format.
86 self.dump_config(self._nodeconfig)
87 return self._vpp_config
89 def add_config_item(self, config, value, path):
90 """Add startup configuration item.
92 :param config: Startup configuration of node.
93 :param value: Value to insert.
94 :param path: Path where to insert item.
100 config[path[0]] = value
102 if path[0] not in config:
103 config[path[0]] = dict()
104 elif isinstance(config[path[0]], str):
105 config[path[0]] = dict() if config[path[0]] == u"" \
106 else {config[path[0]]: u""}
107 self.add_config_item(config[path[0]], value, path[1:])
109 def dump_config(self, obj, level=-1):
110 """Dump the startup configuration in VPP config format.
112 :param obj: Python Object to print.
113 :param level: Nested level for indentation.
120 self._vpp_config += f"{level * indent}{{\n"
121 if isinstance(obj, dict):
122 for key, val in obj.items():
123 if hasattr(val, u"__iter__") and not isinstance(val, str):
124 self._vpp_config += f"{(level + 1) * indent}{key}\n"
125 self.dump_config(val, level + 1)
127 self._vpp_config += f"{(level + 1) * indent}{key} {val}\n"
130 self._vpp_config += f"{(level + 1) * indent}{val}\n"
132 self._vpp_config += f"{level * indent}}}\n"
134 def add_unix_log(self, value="/var/log/vpp/vpp.log"):
135 """Add UNIX log configuration.
137 :param value: Log file.
140 path = [u"unix", u"log"]
141 self.add_config_item(self._nodeconfig, value, path)
143 def add_unix_cli_listen(self, value=u"/run/vpp/cli.sock"):
144 """Add UNIX cli-listen configuration.
146 :param value: CLI listen address and port or path to CLI socket.
149 path = [u"unix", u"cli-listen"]
150 self.add_config_item(self._nodeconfig, value, path)
152 def add_unix_cli_no_pager(self):
153 """Add UNIX cli-no-pager configuration."""
154 path = [u"unix", u"cli-no-pager"]
155 self.add_config_item(self._nodeconfig, u"", path)
157 def add_unix_gid(self, value=u"vpp"):
158 """Add UNIX gid configuration.
163 path = [u"unix", u"gid"]
164 self.add_config_item(self._nodeconfig, value, path)
166 def add_unix_nodaemon(self):
167 """Add UNIX nodaemon configuration."""
168 path = [u"unix", u"nodaemon"]
169 self.add_config_item(self._nodeconfig, u"", path)
171 def add_unix_coredump(self):
172 """Add UNIX full-coredump configuration."""
173 path = [u"unix", u"full-coredump"]
174 self.add_config_item(self._nodeconfig, u"", path)
176 def add_unix_exec(self, value):
177 """Add UNIX exec configuration."""
178 path = [u"unix", u"exec"]
179 self.add_config_item(self._nodeconfig, value, path)
181 def add_socksvr(self, socket=Constants.SOCKSVR_PATH):
182 """Add socksvr configuration."""
183 path = [u"socksvr", u"socket-name"]
184 self.add_config_item(self._nodeconfig, socket, path)
186 def add_graph_node_variant(self, variant=Constants.GRAPH_NODE_VARIANT):
187 """Add default graph node variant.
189 :param value: Graph node variant default value.
194 variant_list = [u"hsw", u"skx", u"icl"]
195 if variant not in variant_list:
196 raise ValueError("Invalid graph node variant value")
197 path = [u"node", u"default", u"variant"]
198 self.add_config_item(self._nodeconfig, variant, path)
200 def add_api_segment_gid(self, value=u"vpp"):
201 """Add API-SEGMENT gid configuration.
206 path = [u"api-segment", u"gid"]
207 self.add_config_item(self._nodeconfig, value, path)
209 def add_api_segment_global_size(self, value):
210 """Add API-SEGMENT global-size configuration.
212 :param value: Global size.
215 path = [u"api-segment", u"global-size"]
216 self.add_config_item(self._nodeconfig, value, path)
218 def add_api_segment_api_size(self, value):
219 """Add API-SEGMENT api-size configuration.
221 :param value: API size.
224 path = [u"api-segment", u"api-size"]
225 self.add_config_item(self._nodeconfig, value, path)
227 def add_buffers_per_numa(self, value):
228 """Increase number of buffers allocated.
230 :param value: Number of buffers allocated.
233 path = [u"buffers", u"buffers-per-numa"]
234 self.add_config_item(self._nodeconfig, value, path)
236 def add_buffers_default_data_size(self, value):
237 """Increase buffers data-size allocated.
239 :param value: Buffers data-size allocated.
242 path = [u"buffers", u"default data-size"]
243 self.add_config_item(self._nodeconfig, value, path)
245 def add_dpdk_dev(self, *devices):
246 """Add DPDK PCI device configuration.
248 :param devices: PCI device(s) (format xxxx:xx:xx.x)
251 for device in devices:
252 if pci_dev_check(device):
253 path = [u"dpdk", f"dev {device}"]
254 self.add_config_item(self._nodeconfig, u"", path)
256 def add_dpdk_cryptodev(self, count):
257 """Add DPDK Crypto PCI device configuration.
259 :param count: Number of HW crypto devices to add.
262 cryptodevs = Topology.get_cryptodev(self._node)
263 for device in cryptodevs.values():
264 for i in range(int(count/len(cryptodevs))):
265 addr = re.sub(r"\d.\d$", f"0.{i+1}", device["pci_address"])
266 path = ["dpdk", f"dev {addr}"]
267 self.add_config_item(self._nodeconfig, "", path)
268 self.add_dpdk_uio_driver("vfio-pci")
270 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
271 """Add DPDK SW Crypto device configuration.
273 :param sw_pmd_type: Type of SW crypto device PMD to add.
274 :param socket_id: Socket ID.
275 :param count: Number of SW crypto devices to add.
276 :type sw_pmd_type: str
280 for _ in range(count):
281 cryptodev_config = f"vdev cryptodev_{sw_pmd_type}_pmd," \
282 f"socket_id={str(socket_id)}"
283 path = [u"dpdk", cryptodev_config]
284 self.add_config_item(self._nodeconfig, u"", path)
286 def add_dpdk_dev_default_rxq(self, value):
287 """Add DPDK dev default rxq configuration.
289 :param value: Default number of rxqs.
292 path = [u"dpdk", u"dev default", u"num-rx-queues"]
293 self.add_config_item(self._nodeconfig, value, path)
295 def add_dpdk_dev_default_txq(self, value):
296 """Add DPDK dev default txq configuration.
298 :param value: Default number of txqs.
301 path = [u"dpdk", u"dev default", u"num-tx-queues"]
302 self.add_config_item(self._nodeconfig, value, path)
304 def add_dpdk_dev_default_rxd(self, value):
305 """Add DPDK dev default rxd configuration.
307 :param value: Default number of rxds.
310 path = [u"dpdk", u"dev default", u"num-rx-desc"]
311 self.add_config_item(self._nodeconfig, value, path)
313 def add_dpdk_dev_default_txd(self, value):
314 """Add DPDK dev default txd configuration.
316 :param value: Default number of txds.
319 path = [u"dpdk", u"dev default", u"num-tx-desc"]
320 self.add_config_item(self._nodeconfig, value, path)
322 def add_dpdk_log_level(self, value):
323 """Add DPDK log-level configuration.
325 :param value: Log level.
328 path = [u"dpdk", u"log-level"]
329 self.add_config_item(self._nodeconfig, value, path)
331 def add_dpdk_no_pci(self):
332 """Add DPDK no-pci."""
333 path = [u"dpdk", u"no-pci"]
334 self.add_config_item(self._nodeconfig, u"", path)
336 def add_dpdk_uio_driver(self, value=None):
337 """Add DPDK uio-driver configuration.
339 :param value: DPDK uio-driver configuration. By default, driver will be
340 loaded automatically from Topology file, still leaving option
341 to manually override by parameter.
345 value = Topology.get_uio_driver(self._node)
346 path = [u"dpdk", u"uio-driver"]
347 self.add_config_item(self._nodeconfig, value, path)
349 def add_dpdk_max_simd_bitwidth(self, variant=Constants.GRAPH_NODE_VARIANT):
350 """Add DPDK max-simd-bitwidth configuration.
352 :param value: Graph node variant default value.
355 if variant == u"icl":
357 elif variant in [u"skx", u"hsw"]:
362 path = [u"dpdk", u"max-simd-bitwidth"]
363 self.add_config_item(self._nodeconfig, value, path)
365 def add_cpu_main_core(self, value):
366 """Add CPU main core configuration.
368 :param value: Main core option.
371 path = [u"cpu", u"main-core"]
372 self.add_config_item(self._nodeconfig, value, path)
374 def add_cpu_corelist_workers(self, value):
375 """Add CPU corelist-workers configuration.
377 :param value: Corelist-workers option.
380 path = [u"cpu", u"corelist-workers"]
381 self.add_config_item(self._nodeconfig, value, path)
383 def add_main_heap_size(self, value):
384 """Add Main Heap Size configuration.
386 :param value: Amount of heap.
389 path = [u"memory", u"main-heap-size"]
390 self.add_config_item(self._nodeconfig, value, path)
392 def add_main_heap_page_size(self, value):
393 """Add Main Heap Page Size configuration.
395 :param value: Heap page size.
398 path = [u"memory", u"main-heap-page-size"]
399 self.add_config_item(self._nodeconfig, value, path)
401 def add_default_hugepage_size(self, value=Constants.DEFAULT_HUGEPAGE_SIZE):
402 """Add Default Hugepage Size configuration.
404 :param value: Hugepage size.
407 path = [u"memory", u"default-hugepage-size"]
408 self.add_config_item(self._nodeconfig, value, path)
410 def add_api_trace(self):
411 """Add API trace configuration."""
412 path = [u"api-trace", u"on"]
413 self.add_config_item(self._nodeconfig, u"", path)
415 def add_ip6_hash_buckets(self, value):
416 """Add IP6 hash buckets configuration.
418 :param value: Number of IP6 hash buckets.
421 path = [u"ip6", u"hash-buckets"]
422 self.add_config_item(self._nodeconfig, value, path)
424 def add_ip6_heap_size(self, value):
425 """Add IP6 heap-size configuration.
427 :param value: IP6 Heapsize amount.
430 path = [u"ip6", u"heap-size"]
431 self.add_config_item(self._nodeconfig, value, path)
433 def add_ipsec_spd_flow_cache_ipv4_inbound(self, value):
434 """Add IPsec spd flow cache for IP4 inbound.
436 :param value: "on" to enable spd flow cache.
439 path = [u"ipsec", u"ipv4-inbound-spd-flow-cache"]
440 self.add_config_item(self._nodeconfig, value, path)
442 def add_ipsec_spd_flow_cache_ipv4_outbound(self, value):
443 """Add IPsec spd flow cache for IP4 outbound.
445 :param value: "on" to enable spd flow cache.
448 path = [u"ipsec", u"ipv4-outbound-spd-flow-cache"]
449 self.add_config_item(self._nodeconfig, value, path)
451 def add_ipsec_spd_fast_path_ipv4_inbound(self, value):
452 """Add IPsec spd fast path for IP4 inbound.
454 :param value: "on" to enable spd fast path.
457 path = [u"ipsec", u"ipv4-inbound-spd-fast-path"]
458 self.add_config_item(self._nodeconfig, value, path)
460 def add_ipsec_spd_fast_path_ipv4_outbound(self, value):
461 """Add IPsec spd fast path for IP4 outbound.
463 :param value: "on" to enable spd fast path.
466 path = [u"ipsec", u"ipv4-outbound-spd-fast-path"]
467 self.add_config_item(self._nodeconfig, value, path)
469 def add_ipsec_spd_fast_path_num_buckets(self, value):
470 """Add num buckets for IPsec spd fast path.
472 :param value: Number of buckets.
475 path = [u"ipsec", u"spd-fast-path-num-buckets"]
476 self.add_config_item(self._nodeconfig, value, path)
478 def add_statseg_size(self, value):
479 """Add Stats Heap Size configuration.
481 :param value: Stats heapsize amount.
484 path = [u"statseg", u"size"]
485 self.add_config_item(self._nodeconfig, value, path)
487 def add_statseg_page_size(self, value):
488 """Add Stats Heap Page Size configuration.
490 :param value: Stats heapsize amount.
493 path = [u"statseg", u"page-size"]
494 self.add_config_item(self._nodeconfig, value, path)
496 def add_statseg_per_node_counters(self, value):
497 """Add stats per-node-counters configuration.
499 :param value: "on" to switch the counters on.
502 path = [u"statseg", u"per-node-counters"]
503 self.add_config_item(self._nodeconfig, value, path)
505 def add_plugin(self, state, *plugins):
506 """Add plugin section for specific plugin(s).
508 :param state: State of plugin [enable|disable].
509 :param plugins: Plugin(s) to disable.
513 for plugin in plugins:
514 path = [u"plugins", f"plugin {plugin}", state]
515 self.add_config_item(self._nodeconfig, u" ", path)
517 def add_dpdk_no_multi_seg(self):
518 """Add DPDK no-multi-seg configuration."""
519 path = [u"dpdk", u"no-multi-seg"]
520 self.add_config_item(self._nodeconfig, u"", path)
522 def add_dpdk_no_tx_checksum_offload(self):
523 """Add DPDK no-tx-checksum-offload configuration."""
524 path = [u"dpdk", u"no-tx-checksum-offload"]
525 self.add_config_item(self._nodeconfig, u"", path)
527 def add_nat(self, value=u"deterministic"):
528 """Add NAT mode configuration.
530 :param value: NAT mode.
533 path = [u"nat", value]
534 self.add_config_item(self._nodeconfig, u"", path)
536 def add_nat_max_translations_per_thread(self, value):
537 """Add NAT max. translations per thread number configuration.
539 :param value: NAT mode.
542 path = [u"nat", u"max translations per thread"]
543 self.add_config_item(self._nodeconfig, value, path)
545 def add_nsim_poll_main_thread(self):
546 """Add NSIM poll-main-thread configuration."""
547 path = [u"nsim", u"poll-main-thread"]
548 self.add_config_item(self._nodeconfig, u"", path)
550 def add_tcp_congestion_control_algorithm(self, value=u"cubic"):
551 """Add TCP congestion control algorithm.
553 :param value: The congestion control algorithm to use. Example: cubic
556 path = [u"tcp", u"cc-algo"]
557 self.add_config_item(self._nodeconfig, value, path)
559 def add_tcp_preallocated_connections(self, value):
560 """Add TCP pre-allocated connections.
562 :param value: The number of pre-allocated connections.
565 path = [u"tcp", u"preallocated-connections"]
566 self.add_config_item(self._nodeconfig, value, path)
568 def add_tcp_preallocated_half_open_connections(self, value):
569 """Add TCP pre-allocated half open connections.
571 :param value: The number of pre-allocated half open connections.
574 path = [u"tcp", u"preallocated-half-open-connections"]
575 self.add_config_item(self._nodeconfig, value, path)
577 def add_session_enable(self):
578 """Add session enable."""
579 path = [u"session", u"enable"]
580 self.add_config_item(self._nodeconfig, u"", path)
582 def add_session_app_socket_api(self):
583 """Use session app socket api."""
584 path = [u"session", u"use-app-socket-api"]
585 self.add_config_item(self._nodeconfig, u"", path)
587 def add_session_event_queues_memfd_segment(self):
588 """Add session event queue memfd segment."""
589 path = [u"session", u"evt_qs_memfd_seg"]
590 self.add_config_item(self._nodeconfig, u"", path)
592 def add_session_event_queue_length(self, value):
593 """Add session event queue length.
595 :param value: Session event queue length.
598 path = [u"session", u"event-queue-length"]
599 self.add_config_item(self._nodeconfig, value, path)
601 def add_session_event_queues_segment_size(self, value):
602 """Add session event queue length.
604 :param value: Session event queue segment size.
607 path = [u"session", u"evt_qs_seg_size"]
608 self.add_config_item(self._nodeconfig, value, path)
610 def add_session_preallocated_sessions(self, value):
611 """Add the number of pre-allocated sessions.
613 :param value: Number of pre-allocated sessions.
616 path = [u"session", u"preallocated-sessions"]
617 self.add_config_item(self._nodeconfig, value, path)
619 def add_session_v4_session_table_buckets(self, value):
620 """Add number of v4 session table buckets to the config.
622 :param value: Number of v4 session table buckets.
625 path = [u"session", u"v4-session-table-buckets"]
626 self.add_config_item(self._nodeconfig, value, path)
628 def add_session_v4_session_table_memory(self, value):
629 """Add the size of v4 session table memory.
631 :param value: Size of v4 session table memory.
634 path = [u"session", u"v4-session-table-memory"]
635 self.add_config_item(self._nodeconfig, value, path)
637 def add_session_v4_halfopen_table_buckets(self, value):
638 """Add the number of v4 halfopen table buckets.
640 :param value: Number of v4 halfopen table buckets.
643 path = [u"session", u"v4-halfopen-table-buckets"]
644 self.add_config_item(self._nodeconfig, value, path)
646 def add_session_v4_halfopen_table_memory(self, value):
647 """Add the size of v4 halfopen table memory.
649 :param value: Size of v4 halfopen table memory.
652 path = [u"session", u"v4-halfopen-table-memory"]
653 self.add_config_item(self._nodeconfig, value, path)
655 def add_session_local_endpoints_table_buckets(self, value):
656 """Add the number of local endpoints table buckets.
658 :param value: Number of local endpoints table buckets.
661 path = [u"session", u"local-endpoints-table-buckets"]
662 self.add_config_item(self._nodeconfig, value, path)
664 def add_session_local_endpoints_table_memory(self, value):
665 """Add the size of local endpoints table memory.
667 :param value: Size of local endpoints table memory.
670 path = [u"session", u"local-endpoints-table-memory"]
671 self.add_config_item(self._nodeconfig, value, path)
673 def write_config(self, filename=None):
674 """Generate and write VPP startup configuration to file.
676 Use data from calls to this class to form a startup.conf file and
677 replace /etc/vpp/startup.conf with it on topology node.
679 :param filename: Startup configuration file name.
682 self.dump_config(self._nodeconfig)
685 filename = self._vpp_startup_conf
687 cmd = f"echo \"{self._vpp_config}\" | sudo tee {filename}"
689 self._node, cmd, message=u"Writing config file failed!"
692 def apply_config(self, filename=None, verify_vpp=True):
693 """Generate and write VPP startup configuration to file and restart VPP.
695 Use data from calls to this class to form a startup.conf file and
696 replace /etc/vpp/startup.conf with it on topology node.
698 :param filename: Startup configuration file name.
699 :param verify_vpp: Verify VPP is running after restart.
701 :type verify_vpp: bool
703 self.write_config(filename=filename)
705 VPPUtil.restart_vpp_service(self._node, self._node_key)
707 VPPUtil.verify_vpp(self._node)
711 """VPP Initial Configuration."""
713 def init_vpp_startup_configuration_on_all_duts(nodes):
714 """Apply initial VPP startup configuration on all DUTs.
716 :param nodes: Nodes in the topology.
719 huge_size = Constants.DEFAULT_HUGEPAGE_SIZE
720 for node in nodes.values():
721 if node[u"type"] == NodeType.DUT:
722 vpp_config = VppConfigGenerator()
723 vpp_config.set_node(node)
724 vpp_config.add_unix_log()
725 vpp_config.add_unix_cli_listen()
726 vpp_config.add_unix_cli_no_pager()
727 vpp_config.add_unix_gid()
728 vpp_config.add_unix_coredump()
729 vpp_config.add_socksvr(socket=Constants.SOCKSVR_PATH)
730 vpp_config.add_main_heap_size("2G")
731 vpp_config.add_main_heap_page_size(huge_size)
732 vpp_config.add_default_hugepage_size(huge_size)
733 vpp_config.add_statseg_size("2G")
734 vpp_config.add_statseg_page_size(huge_size)
735 vpp_config.add_statseg_per_node_counters("on")
736 vpp_config.add_plugin("disable", "default")
737 vpp_config.add_plugin("enable", "dpdk_plugin.so")
738 vpp_config.add_dpdk_dev(
739 *[node["interfaces"][interface].get("pci_address") \
740 for interface in node[u"interfaces"]]
742 vpp_config.add_ip6_hash_buckets(2000000)
743 vpp_config.add_ip6_heap_size("4G")
744 vpp_config.apply_config()