1 # Copyright (c) 2024 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
60 self._vpp_service_name = "vpp"
61 # VPP Startup config location
62 self._vpp_startup_conf = "/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["type"] != NodeType.DUT:
75 "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]] == "" \
106 else {config[path[0]]: ""}
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 = ["unix", "log"]
141 self.add_config_item(self._nodeconfig, value, path)
143 def add_unix_cli_listen(self, value="/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 = ["unix", "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 = ["unix", "cli-no-pager"]
155 self.add_config_item(self._nodeconfig, "", path)
157 def add_unix_gid(self, value="vpp"):
158 """Add UNIX gid configuration.
163 path = ["unix", "gid"]
164 self.add_config_item(self._nodeconfig, value, path)
166 def add_unix_nodaemon(self):
167 """Add UNIX nodaemon configuration."""
168 path = ["unix", "nodaemon"]
169 self.add_config_item(self._nodeconfig, "", path)
171 def add_unix_coredump(self):
172 """Add UNIX full-coredump configuration."""
173 path = ["unix", "full-coredump"]
174 self.add_config_item(self._nodeconfig, "", path)
176 def add_unix_exec(self, value):
177 """Add UNIX exec configuration."""
178 path = ["unix", "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 = ["socksvr", "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 = ["hsw", "skx", "icl"]
195 if variant not in variant_list:
196 raise ValueError("Invalid graph node variant value")
197 path = ["node", "default", "variant"]
198 self.add_config_item(self._nodeconfig, variant, path)
200 def add_api_segment_gid(self, value="vpp"):
201 """Add api-segment gid configuration.
206 path = ["api-segment", "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 = ["api-segment", "global-size"]
216 self.add_config_item(self._nodeconfig, value, path)
218 def add_api_segment_prefix(self, value="vpp"):
219 """Add api-segment prefix configuration.
224 path = ["api-segment", "prefix"]
225 self.add_config_item(self._nodeconfig, value, path)
227 def add_api_segment_api_size(self, value):
228 """Add api-segment api-size configuration.
230 :param value: API size.
233 path = ["api-segment", "api-size"]
234 self.add_config_item(self._nodeconfig, value, path)
236 def add_buffers_per_numa(self, value):
237 """Increase number of buffers allocated.
239 :param value: Number of buffers allocated.
242 path = ["buffers", "buffers-per-numa"]
243 self.add_config_item(self._nodeconfig, value, path)
245 def add_buffers_default_data_size(self, value):
246 """Increase buffers data-size allocated.
248 :param value: Buffers data-size allocated.
251 path = ["buffers", "default data-size"]
252 self.add_config_item(self._nodeconfig, value, path)
254 def add_dpdk_dev(self, *devices):
255 """Add DPDK PCI device configuration.
257 :param devices: PCI device(s) (format xxxx:xx:xx.x)
260 for device in devices:
261 if pci_dev_check(device):
262 path = ["dpdk", f"dev {device}"]
263 self.add_config_item(self._nodeconfig, "", path)
265 def add_dpdk_cryptodev(self, count, num_rx_queues=1):
266 """Add DPDK Crypto PCI device configuration.
268 :param count: Number of HW crypto devices to add.
269 :param num_rx_queues: Number of RX queues per QAT interface.
271 :type num_rx_queues: int
273 cryptodevs = Topology.get_cryptodev(self._node)
274 for device in cryptodevs.values():
275 for i in range(int(count/len(cryptodevs))):
276 numvfs = device["numvfs"]
277 computed = f"{(i+1)//numvfs}.{(i+1)%numvfs}"
278 addr = re.sub(r"\d.\d$", computed, device["pci_address"])
279 path = ["dpdk", f"dev {addr}", "num-rx-queues"]
280 self.add_config_item(self._nodeconfig, num_rx_queues, path)
281 self.add_dpdk_uio_driver("vfio-pci")
283 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
284 """Add DPDK SW Crypto device configuration.
286 :param sw_pmd_type: Type of SW crypto device PMD to add.
287 :param socket_id: Socket ID.
288 :param count: Number of SW crypto devices to add.
289 :type sw_pmd_type: str
293 for _ in range(count):
294 cryptodev_config = f"vdev cryptodev_{sw_pmd_type}_pmd," \
295 f"socket_id={str(socket_id)}"
296 path = ["dpdk", cryptodev_config]
297 self.add_config_item(self._nodeconfig, "", path)
299 def add_dpdk_dev_default_rxq(self, value):
300 """Add DPDK dev default rxq configuration.
302 :param value: Default number of rxqs.
305 path = ["dpdk", "dev default", "num-rx-queues"]
306 self.add_config_item(self._nodeconfig, value, path)
308 def add_dpdk_dev_default_txq(self, value):
309 """Add DPDK dev default txq configuration.
311 :param value: Default number of txqs.
314 path = ["dpdk", "dev default", "num-tx-queues"]
315 self.add_config_item(self._nodeconfig, value, path)
317 def add_dpdk_dev_default_rxd(self, value):
318 """Add DPDK dev default rxd configuration.
320 :param value: Default number of rxds.
323 path = ["dpdk", "dev default", "num-rx-desc"]
324 self.add_config_item(self._nodeconfig, value, path)
326 def add_dpdk_dev_default_txd(self, value):
327 """Add DPDK dev default txd configuration.
329 :param value: Default number of txds.
332 path = ["dpdk", "dev default", "num-tx-desc"]
333 self.add_config_item(self._nodeconfig, value, path)
335 def add_dpdk_dev_default_tso(self):
336 """Add DPDK dev default tso configuration."""
337 path = [u"dpdk", u"dev default", u"tso"]
338 self.add_config_item(self._nodeconfig, "on", path)
340 def add_dpdk_log_level(self, value):
341 """Add DPDK log-level configuration.
343 :param value: Log level.
346 path = ["dpdk", "log-level"]
347 self.add_config_item(self._nodeconfig, value, path)
349 def add_dpdk_no_pci(self):
350 """Add DPDK no-pci."""
351 path = ["dpdk", "no-pci"]
352 self.add_config_item(self._nodeconfig, "", path)
354 def add_dpdk_uio_driver(self, value=None):
355 """Add DPDK uio-driver configuration.
357 :param value: DPDK uio-driver configuration. By default, driver will be
358 loaded automatically from Topology file, still leaving option
359 to manually override by parameter.
363 value = Topology.get_uio_driver(self._node)
364 path = ["dpdk", "uio-driver"]
365 self.add_config_item(self._nodeconfig, value, path)
367 def add_dpdk_max_simd_bitwidth(self, variant=Constants.GRAPH_NODE_VARIANT):
368 """Add DPDK max-simd-bitwidth configuration.
370 :param value: Graph node variant default value.
375 elif variant in ["skx", "hsw"]:
380 path = ["dpdk", "max-simd-bitwidth"]
381 self.add_config_item(self._nodeconfig, value, path)
383 def add_dpdk_enable_tcp_udp_checksum(self):
384 """Add DPDK enable-tcp-udp-checksum configuration."""
385 path = [u"dpdk", u"enable-tcp-udp-checksum"]
386 self.add_config_item(self._nodeconfig, u"", path)
388 def add_cpu_main_core(self, value):
389 """Add CPU main core configuration.
391 :param value: Main core option.
394 path = ["cpu", "main-core"]
395 self.add_config_item(self._nodeconfig, value, path)
397 def add_cpu_corelist_workers(self, value):
398 """Add CPU corelist-workers configuration.
400 :param value: Corelist-workers option.
403 path = ["cpu", "corelist-workers"]
404 self.add_config_item(self._nodeconfig, value, path)
406 def add_main_heap_size(self, value):
407 """Add Main Heap Size configuration.
409 :param value: Amount of heap.
412 path = ["memory", "main-heap-size"]
413 self.add_config_item(self._nodeconfig, value, path)
415 def add_main_heap_page_size(self, value):
416 """Add Main Heap Page Size configuration.
418 :param value: Heap page size.
421 path = ["memory", "main-heap-page-size"]
422 self.add_config_item(self._nodeconfig, value, path)
424 def add_default_hugepage_size(self, value=Constants.DEFAULT_HUGEPAGE_SIZE):
425 """Add Default Hugepage Size configuration.
427 :param value: Hugepage size.
430 path = ["memory", "default-hugepage-size"]
431 self.add_config_item(self._nodeconfig, value, path)
433 def add_api_trace(self):
434 """Add API trace configuration."""
435 path = ["api-trace", "on"]
436 self.add_config_item(self._nodeconfig, "", path)
438 def add_ip6_hash_buckets(self, value):
439 """Add IP6 hash buckets configuration.
441 :param value: Number of IP6 hash buckets.
444 path = ["ip6", "hash-buckets"]
445 self.add_config_item(self._nodeconfig, value, path)
447 def add_ip6_heap_size(self, value):
448 """Add IP6 heap-size configuration.
450 :param value: IP6 Heapsize amount.
453 path = ["ip6", "heap-size"]
454 self.add_config_item(self._nodeconfig, value, path)
456 def add_ipsec_spd_flow_cache_ipv4_inbound(self, value):
457 """Add IPsec spd flow cache for IP4 inbound.
459 :param value: "on" to enable spd flow cache.
462 path = ["ipsec", "ipv4-inbound-spd-flow-cache"]
463 self.add_config_item(self._nodeconfig, value, path)
465 def add_ipsec_spd_flow_cache_ipv4_outbound(self, value):
466 """Add IPsec spd flow cache for IP4 outbound.
468 :param value: "on" to enable spd flow cache.
471 path = ["ipsec", "ipv4-outbound-spd-flow-cache"]
472 self.add_config_item(self._nodeconfig, value, path)
474 def add_ipsec_spd_fast_path_ipv4_inbound(self, value):
475 """Add IPsec spd fast path for IP4 inbound.
477 :param value: "on" to enable spd fast path.
480 path = [u"ipsec", u"ipv4-inbound-spd-fast-path"]
481 self.add_config_item(self._nodeconfig, value, path)
483 def add_ipsec_spd_fast_path_ipv4_outbound(self, value):
484 """Add IPsec spd fast path for IP4 outbound.
486 :param value: "on" to enable spd fast path.
489 path = ["ipsec", "ipv4-outbound-spd-fast-path"]
490 self.add_config_item(self._nodeconfig, value, path)
492 def add_ipsec_spd_fast_path_num_buckets(self, value):
493 """Add num buckets for IPsec spd fast path.
495 :param value: Number of buckets.
498 path = ["ipsec", "spd-fast-path-num-buckets"]
499 self.add_config_item(self._nodeconfig, value, path)
501 def add_statseg_size(self, value):
502 """Add Stats Heap Size configuration.
504 :param value: Stats heapsize amount.
507 path = ["statseg", "size"]
508 self.add_config_item(self._nodeconfig, value, path)
510 def add_statseg_page_size(self, value):
511 """Add Stats Heap Page Size configuration.
513 :param value: Stats heapsize amount.
516 path = ["statseg", "page-size"]
517 self.add_config_item(self._nodeconfig, value, path)
519 def add_statseg_per_node_counters(self, value):
520 """Add stats per-node-counters configuration.
522 :param value: "on" to switch the counters on.
525 path = ["statseg", "per-node-counters"]
526 self.add_config_item(self._nodeconfig, value, path)
528 def add_plugin(self, state, *plugins):
529 """Add plugin section for specific plugin(s).
531 :param state: State of plugin [enable|disable].
532 :param plugins: Plugin(s) to disable.
536 for plugin in plugins:
537 path = ["plugins", f"plugin {plugin}", state]
538 self.add_config_item(self._nodeconfig, " ", path)
540 def add_dpdk_no_multi_seg(self):
541 """Add DPDK no-multi-seg configuration."""
542 path = ["dpdk", "no-multi-seg"]
543 self.add_config_item(self._nodeconfig, "", path)
545 def add_dpdk_no_tx_checksum_offload(self):
546 """Add DPDK no-tx-checksum-offload configuration."""
547 path = ["dpdk", "no-tx-checksum-offload"]
548 self.add_config_item(self._nodeconfig, "", path)
550 def add_nat(self, value="deterministic"):
551 """Add NAT mode configuration.
553 :param value: NAT mode.
556 path = ["nat", value]
557 self.add_config_item(self._nodeconfig, "", path)
559 def add_nat_max_translations_per_thread(self, value):
560 """Add NAT max. translations per thread number configuration.
562 :param value: NAT mode.
565 path = ["nat", "max translations per thread"]
566 self.add_config_item(self._nodeconfig, value, path)
568 def add_nsim_poll_main_thread(self):
569 """Add NSIM poll-main-thread configuration."""
570 path = ["nsim", "poll-main-thread"]
571 self.add_config_item(self._nodeconfig, "", path)
573 def add_tcp_congestion_control_algorithm(self, value="cubic"):
574 """Add TCP congestion control algorithm.
576 :param value: The congestion control algorithm to use. Example: cubic
579 path = ["tcp", "cc-algo"]
580 self.add_config_item(self._nodeconfig, value, path)
582 def add_tcp_preallocated_connections(self, value):
583 """Add TCP pre-allocated connections.
585 :param value: The number of pre-allocated connections.
588 path = ["tcp", "preallocated-connections"]
589 self.add_config_item(self._nodeconfig, value, path)
591 def add_tcp_preallocated_half_open_connections(self, value):
592 """Add TCP pre-allocated half open connections.
594 :param value: The number of pre-allocated half open connections.
597 path = ["tcp", "preallocated-half-open-connections"]
598 self.add_config_item(self._nodeconfig, value, path)
600 def add_tcp_tso(self):
601 """Add TCP tso configuration."""
602 path = [u"tcp", u"tso"]
603 self.add_config_item(self._nodeconfig, u"", path)
605 def add_session_enable(self):
606 """Add session enable."""
607 path = ["session", "enable"]
608 self.add_config_item(self._nodeconfig, "", path)
610 def add_session_app_socket_api(self):
611 """Use session app socket api."""
612 path = ["session", "use-app-socket-api"]
613 self.add_config_item(self._nodeconfig, "", path)
615 def add_session_event_queues_memfd_segment(self):
616 """Add session event queue memfd segment."""
617 path = ["session", "evt_qs_memfd_seg"]
618 self.add_config_item(self._nodeconfig, "", path)
620 def add_session_event_queue_length(self, value):
621 """Add session event queue length.
623 :param value: Session event queue length.
626 path = ["session", "event-queue-length"]
627 self.add_config_item(self._nodeconfig, value, path)
629 def add_session_event_queues_segment_size(self, value):
630 """Add session event queue length.
632 :param value: Session event queue segment size.
635 path = ["session", "evt_qs_seg_size"]
636 self.add_config_item(self._nodeconfig, value, path)
638 def add_session_preallocated_sessions(self, value):
639 """Add the number of pre-allocated sessions.
641 :param value: Number of pre-allocated sessions.
644 path = ["session", "preallocated-sessions"]
645 self.add_config_item(self._nodeconfig, value, path)
647 def add_session_v4_session_table_buckets(self, value):
648 """Add number of v4 session table buckets to the config.
650 :param value: Number of v4 session table buckets.
653 path = ["session", "v4-session-table-buckets"]
654 self.add_config_item(self._nodeconfig, value, path)
656 def add_session_v4_session_table_memory(self, value):
657 """Add the size of v4 session table memory.
659 :param value: Size of v4 session table memory.
662 path = ["session", "v4-session-table-memory"]
663 self.add_config_item(self._nodeconfig, value, path)
665 def add_session_v4_halfopen_table_buckets(self, value):
666 """Add the number of v4 halfopen table buckets.
668 :param value: Number of v4 halfopen table buckets.
671 path = ["session", "v4-halfopen-table-buckets"]
672 self.add_config_item(self._nodeconfig, value, path)
674 def add_session_v4_halfopen_table_memory(self, value):
675 """Add the size of v4 halfopen table memory.
677 :param value: Size of v4 halfopen table memory.
680 path = ["session", "v4-halfopen-table-memory"]
681 self.add_config_item(self._nodeconfig, value, path)
683 def add_session_local_endpoints_table_buckets(self, value):
684 """Add the number of local endpoints table buckets.
686 :param value: Number of local endpoints table buckets.
689 path = ["session", "local-endpoints-table-buckets"]
690 self.add_config_item(self._nodeconfig, value, path)
692 def add_session_local_endpoints_table_memory(self, value):
693 """Add the size of local endpoints table memory.
695 :param value: Size of local endpoints table memory.
698 path = ["session", "local-endpoints-table-memory"]
699 self.add_config_item(self._nodeconfig, value, path)
701 def add_session_use_dma(self):
702 """Add session use-dma configuration."""
703 path = [u"session", u"use-dma"]
704 self.add_config_item(self._nodeconfig, u"", path)
706 def add_dma_dev(self, devices):
707 """Add DMA devices configuration.
709 :param devices: DMA devices or work queues.
712 for device in devices:
713 path = ["dsa", f"dev {device}"]
714 self.add_config_item(self._nodeconfig, "", path)
716 def add_logging_default_syslog_log_level(self, value="debug"):
717 """Add default logging level for syslog.
719 :param value: Log level.
722 path = ["logging", "default-syslog-log-level"]
723 self.add_config_item(self._nodeconfig, value, path)
725 def write_config(self, filename=None):
726 """Generate and write VPP startup configuration to file.
728 Use data from calls to this class to form a startup.conf file and
729 replace /etc/vpp/startup.conf with it on topology node.
731 :param filename: Startup configuration file name.
734 self.dump_config(self._nodeconfig)
737 filename = self._vpp_startup_conf
739 cmd = f"echo \"{self._vpp_config}\" | sudo tee {filename}"
741 self._node, cmd, message="Writing config file failed!"
744 def apply_config(self, filename=None, verify_vpp=True):
745 """Generate and write VPP startup configuration to file and restart VPP.
747 Use data from calls to this class to form a startup.conf file and
748 replace /etc/vpp/startup.conf with it on topology node.
750 :param filename: Startup configuration file name.
751 :param verify_vpp: Verify VPP is running after restart.
753 :type verify_vpp: bool
755 self.write_config(filename=filename)
757 VPPUtil.restart_vpp_service(self._node, self._node_key)
759 VPPUtil.verify_vpp(self._node)
763 """VPP Initial Configuration."""
765 def init_vpp_startup_configuration_on_all_duts(nodes):
766 """Apply initial VPP startup configuration on all DUTs.
768 :param nodes: Nodes in the topology.
771 huge_size = Constants.DEFAULT_HUGEPAGE_SIZE
772 for node in nodes.values():
773 if node["type"] == NodeType.DUT:
774 vpp_config = VppConfigGenerator()
775 vpp_config.set_node(node)
776 vpp_config.add_unix_log()
777 vpp_config.add_unix_cli_listen()
778 vpp_config.add_unix_cli_no_pager()
779 vpp_config.add_unix_gid()
780 vpp_config.add_unix_coredump()
781 vpp_config.add_socksvr(socket=Constants.SOCKSVR_PATH)
782 vpp_config.add_main_heap_size("2G")
783 vpp_config.add_main_heap_page_size(huge_size)
784 vpp_config.add_default_hugepage_size(huge_size)
785 vpp_config.add_statseg_size("2G")
786 vpp_config.add_statseg_page_size(huge_size)
787 vpp_config.add_statseg_per_node_counters("on")
788 vpp_config.add_plugin("disable", "default")
789 vpp_config.add_plugin("enable", "dpdk_plugin.so")
790 vpp_config.add_dpdk_dev(
791 *[node["interfaces"][interface].get("pci_address") \
792 for interface in node["interfaces"]]
794 vpp_config.add_ip6_hash_buckets(2000000)
795 vpp_config.add_ip6_heap_size("4G")
796 vpp_config.apply_config()