1 # Copyright (c) 2021 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_gid(self, value=u"vpp"):
157 """Add UNIX gid configuration.
162 path = [u"unix", u"gid"]
163 self.add_config_item(self._nodeconfig, value, path)
165 def add_unix_nodaemon(self):
166 """Add UNIX nodaemon configuration."""
167 path = [u"unix", u"nodaemon"]
168 self.add_config_item(self._nodeconfig, u"", path)
170 def add_unix_coredump(self):
171 """Add UNIX full-coredump configuration."""
172 path = [u"unix", u"full-coredump"]
173 self.add_config_item(self._nodeconfig, u"", path)
175 def add_unix_exec(self, value):
176 """Add UNIX exec configuration."""
177 path = [u"unix", u"exec"]
178 self.add_config_item(self._nodeconfig, value, path)
180 def add_socksvr(self, socket=Constants.SOCKSVR_PATH):
181 """Add socksvr configuration."""
182 path = [u"socksvr", u"socket-name"]
183 self.add_config_item(self._nodeconfig, socket, path)
185 def add_graph_node_variant(self, variant=Constants.GRAPH_NODE_VARIANT):
186 """Add default graph node variant.
188 :param value: Graph node variant default value.
193 variant_list = [u"hsw", u"skx", u"icl"]
194 if variant not in variant_list:
195 raise ValueError("Invalid graph node variant value")
196 path = [u"node", u"default", u"variant"]
197 self.add_config_item(self._nodeconfig, variant, path)
199 def add_api_segment_gid(self, value=u"vpp"):
200 """Add API-SEGMENT gid configuration.
205 path = [u"api-segment", u"gid"]
206 self.add_config_item(self._nodeconfig, value, path)
208 def add_api_segment_global_size(self, value):
209 """Add API-SEGMENT global-size configuration.
211 :param value: Global size.
214 path = [u"api-segment", u"global-size"]
215 self.add_config_item(self._nodeconfig, value, path)
217 def add_api_segment_api_size(self, value):
218 """Add API-SEGMENT api-size configuration.
220 :param value: API size.
223 path = [u"api-segment", u"api-size"]
224 self.add_config_item(self._nodeconfig, value, path)
226 def add_buffers_per_numa(self, value):
227 """Increase number of buffers allocated.
229 :param value: Number of buffers allocated.
232 path = [u"buffers", u"buffers-per-numa"]
233 self.add_config_item(self._nodeconfig, value, path)
235 def add_buffers_default_data_size(self, value):
236 """Increase buffers data-size allocated.
238 :param value: Buffers data-size allocated.
241 path = [u"buffers", u"default data-size"]
242 self.add_config_item(self._nodeconfig, value, path)
244 def add_dpdk_dev(self, *devices):
245 """Add DPDK PCI device configuration.
247 :param devices: PCI device(s) (format xxxx:xx:xx.x)
250 for device in devices:
251 if pci_dev_check(device):
252 path = [u"dpdk", f"dev {device}"]
253 self.add_config_item(self._nodeconfig, u"", path)
255 def add_dpdk_dev_parameter(self, device, parameter, value):
256 """Add parameter for DPDK device.
258 :param device: PCI device (format xxxx:xx:xx.x).
259 :param parameter: Parameter name.
260 :param value: Parameter value.
265 if pci_dev_check(device):
266 path = [u"dpdk", f"dev {device}", parameter]
267 self.add_config_item(self._nodeconfig, value, path)
269 def add_dpdk_cryptodev(self, count):
270 """Add DPDK Crypto PCI device configuration.
272 :param count: Number of HW crypto devices to add.
275 cryptodev = Topology.get_cryptodev(self._node)
276 for i in range(count):
277 cryptodev_config = re.sub(r"\d.\d$", f"1.{str(i)}", cryptodev)
278 path = [u"dpdk", f"dev {cryptodev_config}"]
279 self.add_config_item(self._nodeconfig, u"", path)
280 self.add_dpdk_uio_driver(u"vfio-pci")
282 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
283 """Add DPDK SW Crypto device configuration.
285 :param sw_pmd_type: Type of SW crypto device PMD to add.
286 :param socket_id: Socket ID.
287 :param count: Number of SW crypto devices to add.
288 :type sw_pmd_type: str
292 for _ in range(count):
293 cryptodev_config = f"vdev cryptodev_{sw_pmd_type}_pmd," \
294 f"socket_id={str(socket_id)}"
295 path = [u"dpdk", cryptodev_config]
296 self.add_config_item(self._nodeconfig, u"", path)
298 def add_dpdk_dev_default_rxq(self, value):
299 """Add DPDK dev default rxq configuration.
301 :param value: Default number of rxqs.
304 path = [u"dpdk", u"dev default", u"num-rx-queues"]
305 self.add_config_item(self._nodeconfig, value, path)
307 def add_dpdk_dev_default_txq(self, value):
308 """Add DPDK dev default txq configuration.
310 :param value: Default number of txqs.
313 path = [u"dpdk", u"dev default", u"num-tx-queues"]
314 self.add_config_item(self._nodeconfig, value, path)
316 def add_dpdk_dev_default_rxd(self, value):
317 """Add DPDK dev default rxd configuration.
319 :param value: Default number of rxds.
322 path = [u"dpdk", u"dev default", u"num-rx-desc"]
323 self.add_config_item(self._nodeconfig, value, path)
325 def add_dpdk_dev_default_txd(self, value):
326 """Add DPDK dev default txd configuration.
328 :param value: Default number of txds.
331 path = [u"dpdk", u"dev default", u"num-tx-desc"]
332 self.add_config_item(self._nodeconfig, value, path)
334 def add_dpdk_log_level(self, value):
335 """Add DPDK log-level configuration.
337 :param value: Log level.
340 path = [u"dpdk", u"log-level"]
341 self.add_config_item(self._nodeconfig, value, path)
343 def add_dpdk_no_pci(self):
344 """Add DPDK no-pci."""
345 path = [u"dpdk", u"no-pci"]
346 self.add_config_item(self._nodeconfig, u"", path)
348 def add_dpdk_uio_driver(self, value=None):
349 """Add DPDK uio-driver configuration.
351 :param value: DPDK uio-driver configuration. By default, driver will be
352 loaded automatically from Topology file, still leaving option
353 to manually override by parameter.
357 value = Topology.get_uio_driver(self._node)
358 path = [u"dpdk", u"uio-driver"]
359 self.add_config_item(self._nodeconfig, value, path)
361 def add_dpdk_max_simd_bitwidth(self, variant=Constants.GRAPH_NODE_VARIANT):
362 """Add DPDK max-simd-bitwidth configuration.
364 :param value: Graph node variant default value.
367 if variant == u"icl":
369 elif variant in [u"skx", u"hsw"]:
374 path = [u"dpdk", u"max-simd-bitwidth"]
375 self.add_config_item(self._nodeconfig, value, path)
377 def add_cpu_main_core(self, value):
378 """Add CPU main core configuration.
380 :param value: Main core option.
383 path = [u"cpu", u"main-core"]
384 self.add_config_item(self._nodeconfig, value, path)
386 def add_cpu_corelist_workers(self, value):
387 """Add CPU corelist-workers configuration.
389 :param value: Corelist-workers option.
392 path = [u"cpu", u"corelist-workers"]
393 self.add_config_item(self._nodeconfig, value, path)
395 def add_main_heap_size(self, value):
396 """Add Main Heap Size configuration.
398 :param value: Amount of heap.
401 path = [u"memory", u"main-heap-size"]
402 self.add_config_item(self._nodeconfig, value, path)
404 def add_main_heap_page_size(self, value):
405 """Add Main Heap Page Size configuration.
407 :param value: Heap page size.
410 path = [u"memory", u"main-heap-page-size"]
411 self.add_config_item(self._nodeconfig, value, path)
413 def add_default_hugepage_size(self, value=Constants.DEFAULT_HUGEPAGE_SIZE):
414 """Add Default Hugepage Size configuration.
416 :param value: Hugepage size.
419 path = [u"memory", u"default-hugepage-size"]
420 self.add_config_item(self._nodeconfig, value, path)
422 def add_api_trace(self):
423 """Add API trace configuration."""
424 path = [u"api-trace", u"on"]
425 self.add_config_item(self._nodeconfig, u"", path)
427 def add_ip6_hash_buckets(self, value):
428 """Add IP6 hash buckets configuration.
430 :param value: Number of IP6 hash buckets.
433 path = [u"ip6", u"hash-buckets"]
434 self.add_config_item(self._nodeconfig, value, path)
436 def add_ip6_heap_size(self, value):
437 """Add IP6 heap-size configuration.
439 :param value: IP6 Heapsize amount.
442 path = [u"ip6", u"heap-size"]
443 self.add_config_item(self._nodeconfig, value, path)
445 def add_spd_flow_cache_ipv4_outbound(self):
446 """Add SPD flow cache for IP4 outbound traffic"""
447 path = [u"ipsec", u"ipv4-outbound-spd-flow-cache"]
448 self.add_config_item(self._nodeconfig, "on", path)
450 def add_statseg_size(self, value):
451 """Add Stats Heap Size configuration.
453 :param value: Stats heapsize amount.
456 path = [u"statseg", u"size"]
457 self.add_config_item(self._nodeconfig, value, path)
459 def add_statseg_page_size(self, value):
460 """Add Stats Heap Page Size configuration.
462 :param value: Stats heapsize amount.
465 path = [u"statseg", u"page-size"]
466 self.add_config_item(self._nodeconfig, value, path)
468 def add_statseg_per_node_counters(self, value):
469 """Add stats per-node-counters configuration.
471 :param value: "on" to switch the counters on.
474 path = [u"statseg", u"per-node-counters"]
475 self.add_config_item(self._nodeconfig, value, path)
477 def add_plugin(self, state, *plugins):
478 """Add plugin section for specific plugin(s).
480 :param state: State of plugin [enable|disable].
481 :param plugins: Plugin(s) to disable.
485 for plugin in plugins:
486 path = [u"plugins", f"plugin {plugin}", state]
487 self.add_config_item(self._nodeconfig, u" ", path)
489 def add_dpdk_no_multi_seg(self):
490 """Add DPDK no-multi-seg configuration."""
491 path = [u"dpdk", u"no-multi-seg"]
492 self.add_config_item(self._nodeconfig, u"", path)
494 def add_dpdk_no_tx_checksum_offload(self):
495 """Add DPDK no-tx-checksum-offload configuration."""
496 path = [u"dpdk", u"no-tx-checksum-offload"]
497 self.add_config_item(self._nodeconfig, u"", path)
499 def add_nat(self, value=u"deterministic"):
500 """Add NAT mode configuration.
502 :param value: NAT mode.
505 path = [u"nat", value]
506 self.add_config_item(self._nodeconfig, u"", path)
508 def add_nat_max_translations_per_thread(self, value):
509 """Add NAT max. translations per thread number configuration.
511 :param value: NAT mode.
514 path = [u"nat", u"max translations per thread"]
515 self.add_config_item(self._nodeconfig, value, path)
517 def add_nsim_poll_main_thread(self):
518 """Add NSIM poll-main-thread configuration."""
519 path = [u"nsim", u"poll-main-thread"]
520 self.add_config_item(self._nodeconfig, u"", path)
522 def add_tcp_congestion_control_algorithm(self, value=u"cubic"):
523 """Add TCP congestion control algorithm.
525 :param value: The congestion control algorithm to use. Example: cubic
528 path = [u"tcp", u"cc-algo"]
529 self.add_config_item(self._nodeconfig, value, path)
531 def add_tcp_preallocated_connections(self, value):
532 """Add TCP pre-allocated connections.
534 :param value: The number of pre-allocated connections.
537 path = [u"tcp", u"preallocated-connections"]
538 self.add_config_item(self._nodeconfig, value, path)
540 def add_tcp_preallocated_half_open_connections(self, value):
541 """Add TCP pre-allocated half open connections.
543 :param value: The number of pre-allocated half open connections.
546 path = [u"tcp", u"preallocated-half-open-connections"]
547 self.add_config_item(self._nodeconfig, value, path)
549 def add_session_enable(self):
550 """Add session enable."""
551 path = [u"session", u"enable"]
552 self.add_config_item(self._nodeconfig, u"", path)
554 def add_session_event_queues_memfd_segment(self):
555 """Add session event queue memfd segment."""
556 path = [u"session", u"evt_qs_memfd_seg"]
557 self.add_config_item(self._nodeconfig, u"", path)
559 def add_session_event_queue_length(self, value):
560 """Add session event queue length.
562 :param value: Session event queue length.
565 path = [u"session", u"event-queue-length"]
566 self.add_config_item(self._nodeconfig, value, path)
568 def add_session_event_queues_segment_size(self, value):
569 """Add session event queue length.
571 :param value: Session event queue segment size.
574 path = [u"session", u"evt_qs_seg_size"]
575 self.add_config_item(self._nodeconfig, value, path)
577 def add_session_preallocated_sessions(self, value):
578 """Add the number of pre-allocated sessions.
580 :param value: Number of pre-allocated sessions.
583 path = [u"session", u"preallocated-sessions"]
584 self.add_config_item(self._nodeconfig, value, path)
586 def add_session_v4_session_table_buckets(self, value):
587 """Add number of v4 session table buckets to the config.
589 :param value: Number of v4 session table buckets.
592 path = [u"session", u"v4-session-table-buckets"]
593 self.add_config_item(self._nodeconfig, value, path)
595 def add_session_v4_session_table_memory(self, value):
596 """Add the size of v4 session table memory.
598 :param value: Size of v4 session table memory.
601 path = [u"session", u"v4-session-table-memory"]
602 self.add_config_item(self._nodeconfig, value, path)
604 def add_session_v4_halfopen_table_buckets(self, value):
605 """Add the number of v4 halfopen table buckets.
607 :param value: Number of v4 halfopen table buckets.
610 path = [u"session", u"v4-halfopen-table-buckets"]
611 self.add_config_item(self._nodeconfig, value, path)
613 def add_session_v4_halfopen_table_memory(self, value):
614 """Add the size of v4 halfopen table memory.
616 :param value: Size of v4 halfopen table memory.
619 path = [u"session", u"v4-halfopen-table-memory"]
620 self.add_config_item(self._nodeconfig, value, path)
622 def add_session_local_endpoints_table_buckets(self, value):
623 """Add the number of local endpoints table buckets.
625 :param value: Number of local endpoints table buckets.
628 path = [u"session", u"local-endpoints-table-buckets"]
629 self.add_config_item(self._nodeconfig, value, path)
631 def add_session_local_endpoints_table_memory(self, value):
632 """Add the size of local endpoints table memory.
634 :param value: Size of local endpoints table memory.
637 path = [u"session", u"local-endpoints-table-memory"]
638 self.add_config_item(self._nodeconfig, value, path)
640 def write_config(self, filename=None):
641 """Generate and write VPP startup configuration to file.
643 Use data from calls to this class to form a startup.conf file and
644 replace /etc/vpp/startup.conf with it on topology node.
646 :param filename: Startup configuration file name.
649 self.dump_config(self._nodeconfig)
652 filename = self._vpp_startup_conf
654 cmd = f"echo \"{self._vpp_config}\" | sudo tee {filename}"
656 self._node, cmd, message=u"Writing config file failed!"
659 def apply_config(self, filename=None, verify_vpp=True):
660 """Generate and write VPP startup configuration to file and restart VPP.
662 Use data from calls to this class to form a startup.conf file and
663 replace /etc/vpp/startup.conf with it on topology node.
665 :param filename: Startup configuration file name.
666 :param verify_vpp: Verify VPP is running after restart.
668 :type verify_vpp: bool
670 self.write_config(filename=filename)
672 VPPUtil.restart_vpp_service(self._node, self._node_key)
674 VPPUtil.verify_vpp(self._node)