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:
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.
16 TODO: Support initialization with default values,
17 so that we do not need to have block of 6 "Add Unix" commands
18 in 7 various places of CSIT code.
23 from resources.libraries.python.Constants import Constants
24 from resources.libraries.python.ssh import exec_cmd_no_error
25 from resources.libraries.python.topology import NodeType
26 from resources.libraries.python.topology import Topology
27 from resources.libraries.python.VPPUtil import VPPUtil
29 __all__ = [u"VppConfigGenerator"]
32 def pci_dev_check(pci_dev):
33 """Check if provided PCI address is in correct format.
35 :param pci_dev: PCI address (expected format: xxxx:xx:xx.x).
37 :returns: True if PCI address is in correct format.
39 :raises ValueError: If PCI address is in incorrect format.
42 r"^[0-9A-Fa-f]{4}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}\.[0-9A-Fa-f]$"
44 if not re.match(pattern, pci_dev):
46 f"PCI address {pci_dev} is not in valid format xxxx:xx:xx.x"
51 class VppConfigGenerator:
52 """VPP Configuration File Generator."""
55 """Initialize library."""
56 # VPP Node to apply configuration on
61 self._nodeconfig = dict()
62 # Serialized VPP Configuration
63 self._vpp_config = u""
65 self._vpp_service_name = u"vpp"
66 # VPP Logfile location
67 self._vpp_logfile = u"/tmp/vpe.log"
68 # VPP Startup config location
69 self._vpp_startup_conf = u"/etc/vpp/startup.conf"
70 # VPP Startup config backup location
71 self._vpp_startup_conf_backup = None
73 def set_node(self, node, node_key=None):
76 :param node: Node to store configuration on.
77 :param node_key: Topology node key.
80 :raises RuntimeError: If Node type is not DUT.
82 if node[u"type"] != NodeType.DUT:
84 u"Startup config can only be applied to DUTnode."
87 self._node_key = node_key
89 def set_vpp_logfile(self, logfile):
90 """Set VPP logfile location.
92 :param logfile: VPP logfile location.
95 self._vpp_logfile = logfile
97 def set_vpp_startup_conf_backup(self, backup=u"/etc/vpp/startup.backup"):
98 """Set VPP startup configuration backup.
100 :param backup: VPP logfile location.
103 self._vpp_startup_conf_backup = backup
105 def get_config_str(self):
106 """Get dumped startup configuration in VPP config format.
108 :returns: Startup configuration in VPP config format.
111 self.dump_config(self._nodeconfig)
112 return self._vpp_config
114 def add_config_item(self, config, value, path):
115 """Add startup configuration item.
117 :param config: Startup configuration of node.
118 :param value: Value to insert.
119 :param path: Path where to insert item.
125 config[path[0]] = value
127 if path[0] not in config:
128 config[path[0]] = dict()
129 elif isinstance(config[path[0]], str):
130 config[path[0]] = dict() if config[path[0]] == u"" \
131 else {config[path[0]]: u""}
132 self.add_config_item(config[path[0]], value, path[1:])
134 def dump_config(self, obj, level=-1):
135 """Dump the startup configuration in VPP config format.
137 :param obj: Python Object to print.
138 :param level: Nested level for indentation.
145 self._vpp_config += f"{level * indent}{{\n"
146 if isinstance(obj, dict):
147 for key, val in obj.items():
148 if hasattr(val, u"__iter__") and not isinstance(val, str):
149 self._vpp_config += f"{(level + 1) * indent}{key}\n"
150 self.dump_config(val, level + 1)
152 self._vpp_config += f"{(level + 1) * indent}{key} {val}\n"
155 self._vpp_config += f"{(level + 1) * indent}{val}\n"
157 self._vpp_config += f"{level * indent}}}\n"
159 def add_unix_log(self, value=None):
160 """Add UNIX log configuration.
162 :param value: Log file.
165 path = [u"unix", u"log"]
167 value = self._vpp_logfile
168 self.add_config_item(self._nodeconfig, value, path)
170 def add_unix_cli_listen(self, value=u"/run/vpp/cli.sock"):
171 """Add UNIX cli-listen configuration.
173 :param value: CLI listen address and port or path to CLI socket.
176 path = [u"unix", u"cli-listen"]
177 self.add_config_item(self._nodeconfig, value, path)
179 def add_unix_gid(self, value=u"vpp"):
180 """Add UNIX gid configuration.
185 path = [u"unix", u"gid"]
186 self.add_config_item(self._nodeconfig, value, path)
188 def add_unix_nodaemon(self):
189 """Add UNIX nodaemon configuration."""
190 path = [u"unix", u"nodaemon"]
191 self.add_config_item(self._nodeconfig, u"", path)
193 def add_unix_coredump(self):
194 """Add UNIX full-coredump configuration."""
195 path = [u"unix", u"full-coredump"]
196 self.add_config_item(self._nodeconfig, u"", path)
198 def add_unix_exec(self, value):
199 """Add UNIX exec configuration."""
200 path = [u"unix", u"exec"]
201 self.add_config_item(self._nodeconfig, value, path)
203 def add_socksvr(self, socket=Constants.SOCKSVR_PATH):
204 """Add socksvr configuration."""
205 path = [u"socksvr", u"socket-name"]
206 self.add_config_item(self._nodeconfig, socket, path)
208 def add_api_segment_gid(self, value=u"vpp"):
209 """Add API-SEGMENT gid configuration.
214 path = [u"api-segment", u"gid"]
215 self.add_config_item(self._nodeconfig, value, path)
217 def add_api_segment_global_size(self, value):
218 """Add API-SEGMENT global-size configuration.
220 :param value: Global size.
223 path = [u"api-segment", u"global-size"]
224 self.add_config_item(self._nodeconfig, value, path)
226 def add_api_segment_api_size(self, value):
227 """Add API-SEGMENT api-size configuration.
229 :param value: API size.
232 path = [u"api-segment", u"api-size"]
233 self.add_config_item(self._nodeconfig, value, path)
235 def add_buffers_per_numa(self, value):
236 """Increase number of buffers allocated.
238 :param value: Number of buffers allocated.
241 path = [u"buffers", u"buffers-per-numa"]
242 self.add_config_item(self._nodeconfig, value, path)
244 def add_buffers_default_data_size(self, value):
245 """Increase buffers data-size allocated.
247 :param value: Buffers data-size allocated.
250 path = [u"buffers", u"default data-size"]
251 self.add_config_item(self._nodeconfig, value, path)
253 def add_dpdk_dev(self, *devices):
254 """Add DPDK PCI device configuration.
256 :param devices: PCI device(s) (format xxxx:xx:xx.x)
259 for device in devices:
260 if pci_dev_check(device):
261 path = [u"dpdk", f"dev {device}"]
262 self.add_config_item(self._nodeconfig, u"", path)
264 def add_dpdk_dev_parameter(self, device, parameter, value):
265 """Add parameter for DPDK device.
267 :param device: PCI device (format xxxx:xx:xx.x).
268 :param parameter: Parameter name.
269 :param value: Parameter value.
274 if pci_dev_check(device):
275 path = [u"dpdk", f"dev {device}", parameter]
276 self.add_config_item(self._nodeconfig, value, path)
278 def add_dpdk_cryptodev(self, count):
279 """Add DPDK Crypto PCI device configuration.
281 :param count: Number of HW crypto devices to add.
284 cryptodev = Topology.get_cryptodev(self._node)
285 for i in range(count):
286 cryptodev_config = re.sub(r"\d.\d$", f"1.{str(i)}", cryptodev)
287 path = [u"dpdk", f"dev {cryptodev_config}"]
288 self.add_config_item(self._nodeconfig, u"", path)
289 self.add_dpdk_uio_driver(u"vfio-pci")
291 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
292 """Add DPDK SW Crypto device configuration.
294 :param sw_pmd_type: Type of SW crypto device PMD to add.
295 :param socket_id: Socket ID.
296 :param count: Number of SW crypto devices to add.
297 :type sw_pmd_type: str
301 for _ in range(count):
302 cryptodev_config = f"vdev cryptodev_{sw_pmd_type}_pmd," \
303 f"socket_id={str(socket_id)}"
304 path = [u"dpdk", cryptodev_config]
305 self.add_config_item(self._nodeconfig, u"", path)
307 def add_dpdk_eth_bond_dev(self, ethbond_id, mode, xmit_policy, *slaves):
308 """Add DPDK Eth_bond device configuration.
310 :param ethbond_id: Eth_bond device ID.
311 :param mode: Link bonding mode.
312 :param xmit_policy: Transmission policy.
313 :param slaves: PCI device(s) to be bonded (format xxxx:xx:xx.x).
314 :type ethbond_id: str or int
315 :type mode: str or int
316 :type xmit_policy: str
319 slaves_config = u"slave=" + u",slave=".join(
320 slave if pci_dev_check(slave) else u"" for slave in slaves
322 ethbond_config = f"vdev eth_bond{ethbond_id}," \
323 f"mode={mode}{slaves_config},xmit_policy={xmit_policy}"
324 path = [u"dpdk", ethbond_config]
325 self.add_config_item(self._nodeconfig, u"", path)
327 def add_dpdk_dev_default_rxq(self, value):
328 """Add DPDK dev default rxq configuration.
330 :param value: Default number of rxqs.
333 path = [u"dpdk", u"dev default", u"num-rx-queues"]
334 self.add_config_item(self._nodeconfig, value, path)
336 def add_dpdk_dev_default_txq(self, value):
337 """Add DPDK dev default txq configuration.
339 :param value: Default number of txqs.
342 path = [u"dpdk", u"dev default", u"num-tx-queues"]
343 self.add_config_item(self._nodeconfig, value, path)
345 def add_dpdk_dev_default_rxd(self, value):
346 """Add DPDK dev default rxd configuration.
348 :param value: Default number of rxds.
351 path = [u"dpdk", u"dev default", u"num-rx-desc"]
352 self.add_config_item(self._nodeconfig, value, path)
354 def add_dpdk_dev_default_txd(self, value):
355 """Add DPDK dev default txd configuration.
357 :param value: Default number of txds.
360 path = [u"dpdk", u"dev default", u"num-tx-desc"]
361 self.add_config_item(self._nodeconfig, value, path)
363 def add_dpdk_log_level(self, value):
364 """Add DPDK log-level configuration.
366 :param value: Log level.
369 path = [u"dpdk", u"log-level"]
370 self.add_config_item(self._nodeconfig, value, path)
372 def add_dpdk_no_pci(self):
373 """Add DPDK no-pci."""
374 path = [u"dpdk", u"no-pci"]
375 self.add_config_item(self._nodeconfig, u"", path)
377 def add_dpdk_uio_driver(self, value=None):
378 """Add DPDK uio-driver configuration.
380 :param value: DPDK uio-driver configuration. By default, driver will be
381 loaded automatically from Topology file, still leaving option
382 to manually override by parameter.
386 value = Topology.get_uio_driver(self._node)
387 path = [u"dpdk", u"uio-driver"]
388 self.add_config_item(self._nodeconfig, value, path)
390 def add_cpu_main_core(self, value):
391 """Add CPU main core configuration.
393 :param value: Main core option.
396 path = [u"cpu", u"main-core"]
397 self.add_config_item(self._nodeconfig, value, path)
399 def add_cpu_corelist_workers(self, value):
400 """Add CPU corelist-workers configuration.
402 :param value: Corelist-workers option.
405 path = [u"cpu", u"corelist-workers"]
406 self.add_config_item(self._nodeconfig, value, path)
408 def add_heapsize(self, value):
409 """Add Heapsize configuration.
411 :param value: Amount of heapsize.
415 self.add_config_item(self._nodeconfig, value, path)
417 def add_api_trace(self):
418 """Add API trace configuration."""
419 path = [u"api-trace", u"on"]
420 self.add_config_item(self._nodeconfig, u"", path)
422 def add_ip6_hash_buckets(self, value):
423 """Add IP6 hash buckets configuration.
425 :param value: Number of IP6 hash buckets.
428 path = [u"ip6", u"hash-buckets"]
429 self.add_config_item(self._nodeconfig, value, path)
431 def add_ip6_heap_size(self, value):
432 """Add IP6 heap-size configuration.
434 :param value: IP6 Heapsize amount.
437 path = [u"ip6", u"heap-size"]
438 self.add_config_item(self._nodeconfig, value, path)
440 def add_ip_heap_size(self, value):
441 """Add IP heap-size configuration.
443 :param value: IP Heapsize amount.
446 path = [u"ip", u"heap-size"]
447 self.add_config_item(self._nodeconfig, value, path)
449 def add_statseg_size(self, value):
450 """Add stats segment heap size configuration.
452 :param value: Stats heapsize amount.
455 path = [u"statseg", u"size"]
456 self.add_config_item(self._nodeconfig, value, path)
458 def add_statseg_per_node_counters(self, value):
459 """Add stats per-node-counters configuration.
461 :param value: "on" to switch the counters on.
464 path = [u"statseg", u"per-node-counters"]
465 self.add_config_item(self._nodeconfig, value, path)
467 def add_plugin(self, state, *plugins):
468 """Add plugin section for specific plugin(s).
470 :param state: State of plugin [enable|disable].
471 :param plugins: Plugin(s) to disable.
475 for plugin in plugins:
476 path = [u"plugins", f"plugin {plugin}", state]
477 self.add_config_item(self._nodeconfig, u" ", path)
479 def add_dpdk_no_multi_seg(self):
480 """Add DPDK no-multi-seg configuration."""
481 path = [u"dpdk", u"no-multi-seg"]
482 self.add_config_item(self._nodeconfig, u"", path)
484 def add_dpdk_no_tx_checksum_offload(self):
485 """Add DPDK no-tx-checksum-offload configuration."""
486 path = [u"dpdk", u"no-tx-checksum-offload"]
487 self.add_config_item(self._nodeconfig, u"", path)
489 def add_nat(self, value=u"deterministic"):
490 """Add NAT configuration.
492 :param value: NAT mode.
496 self.add_config_item(self._nodeconfig, value, path)
498 def add_nsim_poll_main_thread(self):
499 """Add NSIM poll-main-thread configuration."""
500 path = [u"nsim", u"poll-main-thread"]
501 self.add_config_item(self._nodeconfig, u"", path)
503 def add_tcp_congestion_control_algorithm(self, value=u"cubic"):
504 """Add TCP congestion control algorithm.
506 :param value: The congestion control algorithm to use. Example: cubic
509 path = [u"tcp", u"cc-algo"]
510 self.add_config_item(self._nodeconfig, value, path)
512 def add_tcp_preallocated_connections(self, value):
513 """Add TCP pre-allocated connections.
515 :param value: The number of pre-allocated connections.
518 path = [u"tcp", u"preallocated-connections"]
519 self.add_config_item(self._nodeconfig, value, path)
521 def add_tcp_preallocated_half_open_connections(self, value):
522 """Add TCP pre-allocated half open connections.
524 :param value: The number of pre-allocated half open connections.
527 path = [u"tcp", u"preallocated-half-open-connections"]
528 self.add_config_item(self._nodeconfig, value, path)
530 def add_session_enable(self):
531 """Add session enable."""
532 path = [u"session", u"enable"]
533 self.add_config_item(self._nodeconfig, u"", path)
535 def add_session_event_queues_memfd_segment(self):
536 """Add session event queue memfd segment."""
537 path = [u"session", u"evt_qs_memfd_seg"]
538 self.add_config_item(self._nodeconfig, u"", path)
540 def add_session_event_queue_length(self, value):
541 """Add session event queue length.
543 :param value: Session event queue length.
546 path = [u"session", u"event-queue-length"]
547 self.add_config_item(self._nodeconfig, value, path)
549 def add_session_event_queues_segment_size(self, value):
550 """Add session event queue length.
552 :param value: Session event queue segment size.
555 path = [u"session", u"evt_qs_seg_size"]
556 self.add_config_item(self._nodeconfig, value, path)
558 def add_session_preallocated_sessions(self, value):
559 """Add the number of pre-allocated sessions.
561 :param value: Number of pre-allocated sessions.
564 path = [u"session", u"preallocated-sessions"]
565 self.add_config_item(self._nodeconfig, value, path)
567 def add_session_v4_session_table_buckets(self, value):
568 """Add number of v4 session table buckets to the config.
570 :param value: Number of v4 session table buckets.
573 path = [u"session", u"v4-session-table-buckets"]
574 self.add_config_item(self._nodeconfig, value, path)
576 def add_session_v4_session_table_memory(self, value):
577 """Add the size of v4 session table memory.
579 :param value: Size of v4 session table memory.
582 path = [u"session", u"v4-session-table-memory"]
583 self.add_config_item(self._nodeconfig, value, path)
585 def add_session_v4_halfopen_table_buckets(self, value):
586 """Add the number of v4 halfopen table buckets.
588 :param value: Number of v4 halfopen table buckets.
591 path = [u"session", u"v4-halfopen-table-buckets"]
592 self.add_config_item(self._nodeconfig, value, path)
594 def add_session_v4_halfopen_table_memory(self, value):
595 """Add the size of v4 halfopen table memory.
597 :param value: Size of v4 halfopen table memory.
600 path = [u"session", u"v4-halfopen-table-memory"]
601 self.add_config_item(self._nodeconfig, value, path)
603 def add_session_local_endpoints_table_buckets(self, value):
604 """Add the number of local endpoints table buckets.
606 :param value: Number of local endpoints table buckets.
609 path = [u"session", u"local-endpoints-table-buckets"]
610 self.add_config_item(self._nodeconfig, value, path)
612 def add_session_local_endpoints_table_memory(self, value):
613 """Add the size of local endpoints table memory.
615 :param value: Size of local endpoints table memory.
618 path = [u"session", u"local-endpoints-table-memory"]
619 self.add_config_item(self._nodeconfig, value, path)
621 def write_config(self, filename=None):
622 """Generate and write VPP startup configuration to file.
624 Use data from calls to this class to form a startup.conf file and
625 replace /etc/vpp/startup.conf with it on topology node.
627 :param filename: Startup configuration file name.
630 self.dump_config(self._nodeconfig)
633 filename = self._vpp_startup_conf
635 if self._vpp_startup_conf_backup is not None:
636 cmd = f"cp {self._vpp_startup_conf} {self._vpp_startup_conf_backup}"
638 self._node, cmd, sudo=True, message=u"Copy config file failed!"
641 cmd = f"echo \"{self._vpp_config}\" | sudo tee {filename}"
643 self._node, cmd, message=u"Writing config file failed!"
646 def apply_config(self, filename=None, verify_vpp=True):
647 """Generate and write VPP startup configuration to file and restart VPP.
649 Use data from calls to this class to form a startup.conf file and
650 replace /etc/vpp/startup.conf with it on topology node.
652 :param filename: Startup configuration file name.
653 :param verify_vpp: Verify VPP is running after restart.
655 :type verify_vpp: bool
657 self.write_config(filename=filename)
659 VPPUtil.restart_vpp_service(self._node, self._node_key)
661 VPPUtil.verify_vpp(self._node)
663 def restore_config(self):
664 """Restore VPP startup.conf from backup."""
665 cmd = f"cp {self._vpp_startup_conf_backup} {self._vpp_startup_conf}"
667 self._node, cmd, sudo=True, message=u"Copy config file failed!"