1 # Copyright (c) 2018 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."""
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.constants import Constants
21 from resources.libraries.python.DUTSetup import DUTSetup
22 from resources.libraries.python.topology import NodeType
23 from resources.libraries.python.topology import Topology
25 __all__ = ['VppConfigGenerator']
28 class VppConfigGenerator(object):
29 """VPP Configuration File Generator."""
32 """Initialize library."""
33 # VPP Node to apply configuration on
39 # Serialized VPP Configuration
42 self._vpp_service_name = 'vpp'
43 # VPP Logfile location
44 self._vpp_logfile = '/tmp/vpe.log'
45 # VPP Startup config location
46 self._vpp_startup_conf = '/etc/vpp/startup.conf'
47 # VPP Startup config backup location
48 self._vpp_startup_conf_backup = None
50 def set_node(self, node):
53 :param node: Node to store configuration on.
55 :raises RuntimeError: If Node type is not DUT.
57 if node['type'] != NodeType.DUT:
58 raise RuntimeError('Startup config can only be applied to DUT'
61 self._hostname = Topology.get_node_hostname(node)
63 def set_vpp_logfile(self, logfile):
64 """Set VPP logfile location.
66 :param logfile: VPP logfile location.
69 self._vpp_logfile = logfile
71 def set_vpp_startup_conf_backup(self, backup='/etc/vpp/startup.backup'):
72 """Set VPP startup configuration backup.
74 :param backup: VPP logfile location.
77 self._vpp_startup_conf_backup = backup
79 def get_config_str(self):
80 """Get dumped startup configuration in VPP config format.
82 :returns: Startup configuration in VPP config format.
85 self.dump_config(self._nodeconfig)
86 return self._vpp_config
88 def add_config_item(self, config, value, path):
89 """Add startup configuration item.
91 :param config: Startup configuration of node.
92 :param value: Value to insert.
93 :param path: Path where to insert item.
99 config[path[0]] = value
101 if path[0] not in config:
103 self.add_config_item(config[path[0]], value, path[1:])
105 def dump_config(self, obj, level=-1):
106 """Dump the startup configuration in VPP config format.
108 :param obj: Python Object to print.
109 :param level: Nested level for indentation.
116 self._vpp_config += '{}{{\n'.format((level) * indent)
117 if isinstance(obj, dict):
118 for key, val in obj.items():
119 if hasattr(val, '__iter__'):
120 self._vpp_config += '{}{}\n'.format((level + 1) * indent,
122 self.dump_config(val, level + 1)
124 self._vpp_config += '{}{} {}\n'.format((level + 1) * indent,
128 self._vpp_config += '{}{}\n'.format((level + 1) * indent, val)
130 self._vpp_config += '{}}}\n'.format(level * indent)
132 def add_unix_log(self, value=None):
133 """Add UNIX log configuration.
135 :param value: Log file.
138 path = ['unix', 'log']
140 value = self._vpp_logfile
141 self.add_config_item(self._nodeconfig, value, path)
143 def add_unix_cli_listen(self, value='localhost:5002'):
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_gid(self, value='vpp'):
153 """Add UNIX gid configuration.
158 path = ['unix', 'gid']
159 self.add_config_item(self._nodeconfig, value, path)
161 def add_unix_nodaemon(self):
162 """Add UNIX nodaemon configuration."""
163 path = ['unix', 'nodaemon']
164 self.add_config_item(self._nodeconfig, '', path)
166 def add_unix_coredump(self):
167 """Add UNIX full-coredump configuration."""
168 path = ['unix', 'full-coredump']
169 self.add_config_item(self._nodeconfig, '', path)
171 def add_unix_exec(self, value):
172 """Add UNIX exec configuration."""
173 path = ['unix', 'exec']
174 self.add_config_item(self._nodeconfig, value, path)
176 def add_api_segment_gid(self, value='vpp'):
177 """Add API-SEGMENT gid configuration.
182 path = ['api-segment', 'gid']
183 self.add_config_item(self._nodeconfig, value, path)
185 def add_api_segment_global_size(self, value):
186 """Add API-SEGMENT global-size configuration.
188 :param value: Global size.
191 path = ['api-segment', 'global-size']
192 self.add_config_item(self._nodeconfig, value, path)
194 def add_api_segment_api_size(self, value):
195 """Add API-SEGMENT api-size configuration.
197 :param value: API size.
200 path = ['api-segment', 'api-size']
201 self.add_config_item(self._nodeconfig, value, path)
203 def add_dpdk_dev(self, *devices):
204 """Add DPDK PCI device configuration.
206 :param devices: PCI device(s) (format xxxx:xx:xx.x)
208 :raises ValueError: If PCI address format is not valid.
210 pattern = re.compile("^[0-9A-Fa-f]{4}:[0-9A-Fa-f]{2}:"
211 "[0-9A-Fa-f]{2}\\.[0-9A-Fa-f]$")
212 for device in devices:
213 if not pattern.match(device):
214 raise ValueError('PCI address {} to be added to host {} '
215 'is not in valid format xxxx:xx:xx.x'.
216 format(device, self._hostname))
217 path = ['dpdk', 'dev {0}'.format(device)]
218 self.add_config_item(self._nodeconfig, '', path)
220 def add_dpdk_cryptodev(self, count):
221 """Add DPDK Crypto PCI device configuration.
223 :param count: Number of HW crypto devices to add.
226 cryptodev = Topology.get_cryptodev(self._node)
227 for i in range(count):
228 cryptodev_config = 'dev {0}'.format(
229 re.sub(r'\d.\d$', '1.'+str(i), cryptodev))
230 path = ['dpdk', cryptodev_config]
231 self.add_config_item(self._nodeconfig, '', path)
232 self.add_dpdk_uio_driver('igb_uio')
234 def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
235 """Add DPDK SW Crypto device configuration.
237 :param sw_pmd_type: Type of SW crypto device PMD to add.
238 :param socket_id: Socket ID.
239 :param count: Number of SW crypto devices to add.
240 :type sw_pmd_type: str
244 for _ in range(count):
245 cryptodev_config = 'vdev cryptodev_{0}_pmd,socket_id={1}'.\
246 format(sw_pmd_type, str(socket_id))
247 path = ['dpdk', cryptodev_config]
248 self.add_config_item(self._nodeconfig, '', path)
250 def add_dpdk_dev_default_rxq(self, value):
251 """Add DPDK dev default rxq configuration.
253 :param value: Default number of rxqs.
256 path = ['dpdk', 'dev default', 'num-rx-queues']
257 self.add_config_item(self._nodeconfig, value, path)
259 def add_dpdk_dev_default_txq(self, value):
260 """Add DPDK dev default txq configuration.
262 :param value: Default number of txqs.
265 path = ['dpdk', 'dev default', 'num-tx-queues']
266 self.add_config_item(self._nodeconfig, value, path)
268 def add_dpdk_dev_default_rxd(self, value):
269 """Add DPDK dev default rxd configuration.
271 :param value: Default number of rxds.
274 path = ['dpdk', 'dev default', 'num-rx-desc']
275 self.add_config_item(self._nodeconfig, value, path)
277 def add_dpdk_dev_default_txd(self, value):
278 """Add DPDK dev default txd configuration.
280 :param value: Default number of txds.
283 path = ['dpdk', 'dev default', 'num-tx-desc']
284 self.add_config_item(self._nodeconfig, value, path)
286 def add_dpdk_socketmem(self, value):
287 """Add DPDK socket memory configuration.
289 :param value: Socket memory size.
292 path = ['dpdk', 'socket-mem']
293 self.add_config_item(self._nodeconfig, value, path)
295 def add_dpdk_uio_driver(self, value):
296 """Add DPDK uio-driver configuration.
298 :param value: DPDK uio-driver configuration.
301 path = ['dpdk', 'uio-driver']
302 self.add_config_item(self._nodeconfig, value, path)
304 def add_cpu_main_core(self, value):
305 """Add CPU main core configuration.
307 :param value: Main core option.
310 path = ['cpu', 'main-core']
311 self.add_config_item(self._nodeconfig, value, path)
313 def add_cpu_corelist_workers(self, value):
314 """Add CPU corelist-workers configuration.
316 :param value: Corelist-workers option.
319 path = ['cpu', 'corelist-workers']
320 self.add_config_item(self._nodeconfig, value, path)
322 def add_heapsize(self, value):
323 """Add Heapsize configuration.
325 :param value: Amount of heapsize.
329 self.add_config_item(self._nodeconfig, value, path)
331 def add_api_trace(self):
332 """Add API trace configuration."""
333 path = ['api-trace', 'on']
334 self.add_config_item(self._nodeconfig, '', path)
336 def add_ip6_hash_buckets(self, value):
337 """Add IP6 hash buckets configuration.
339 :param value: Number of IP6 hash buckets.
342 path = ['ip6', 'hash-buckets']
343 self.add_config_item(self._nodeconfig, value, path)
345 def add_ip6_heap_size(self, value):
346 """Add IP6 heap-size configuration.
348 :param value: IP6 Heapsize amount.
351 path = ['ip6', 'heap-size']
352 self.add_config_item(self._nodeconfig, value, path)
354 def add_ip_heap_size(self, value):
355 """Add IP heap-size configuration.
357 :param value: IP Heapsize amount.
360 path = ['ip', 'heap-size']
361 self.add_config_item(self._nodeconfig, value, path)
363 def add_plugin(self, state, *plugins):
364 """Add plugin section for specific plugin(s).
366 :param state: State of plugin [enable|disable].
367 :param plugins: Plugin(s) to disable.
371 for plugin in plugins:
372 path = ['plugins', 'plugin {0}'.format(plugin), state]
373 self.add_config_item(self._nodeconfig, ' ', path)
375 def add_dpdk_no_multi_seg(self):
376 """Add DPDK no-multi-seg configuration."""
377 path = ['dpdk', 'no-multi-seg']
378 self.add_config_item(self._nodeconfig, '', path)
380 def add_dpdk_no_tx_checksum_offload(self):
381 """Add DPDK no-tx-checksum-offload configuration."""
382 path = ['dpdk', 'no-tx-checksum-offload']
383 self.add_config_item(self._nodeconfig, '', path)
385 def add_nat(self, value='deterministic'):
386 """Add NAT configuration.
388 :param value: NAT mode.
392 self.add_config_item(self._nodeconfig, value, path)
394 def add_tcp_preallocated_connections(self, value):
395 """Add TCP pre-allocated connections.
397 :param value: The number of pre-allocated connections.
400 path = ['tcp', 'preallocated-connections']
401 self.add_config_item(self._nodeconfig, value, path)
403 def add_tcp_preallocated_half_open_connections(self, value):
404 """Add TCP pre-allocated half open connections.
406 :param value: The number of pre-allocated half open connections.
409 path = ['tcp', 'preallocated-half-open-connections']
410 self.add_config_item(self._nodeconfig, value, path)
412 def add_session_event_queue_length(self, value):
413 """Add session event queue length.
415 :param value: Session event queue length.
418 path = ['session', 'event-queue-length']
419 self.add_config_item(self._nodeconfig, value, path)
421 def add_session_preallocated_sessions(self, value):
422 """Add the number of pre-allocated sessions.
424 :param value: Number of pre-allocated sessions.
427 path = ['session', 'preallocated-sessions']
428 self.add_config_item(self._nodeconfig, value, path)
430 def add_session_v4_session_table_buckets(self, value):
431 """Add number of v4 session table buckets to the config.
433 :param value: Number of v4 session table buckets.
436 path = ['session', 'v4-session-table-buckets']
437 self.add_config_item(self._nodeconfig, value, path)
439 def add_session_v4_session_table_memory(self, value):
440 """Add the size of v4 session table memory.
442 :param value: Size of v4 session table memory.
445 path = ['session', 'v4-session-table-memory']
446 self.add_config_item(self._nodeconfig, value, path)
448 def add_session_v4_halfopen_table_buckets(self, value):
449 """Add the number of v4 halfopen table buckets.
451 :param value: Number of v4 halfopen table buckets.
454 path = ['session', 'v4-halfopen-table-buckets']
455 self.add_config_item(self._nodeconfig, value, path)
457 def add_session_v4_halfopen_table_memory(self, value):
458 """Add the size of v4 halfopen table memory.
460 :param value: Size of v4 halfopen table memory.
463 path = ['session', 'v4-halfopen-table-memory']
464 self.add_config_item(self._nodeconfig, value, path)
466 def add_session_local_endpoints_table_buckets(self, value):
467 """Add the number of local endpoints table buckets.
469 :param value: Number of local endpoints table buckets.
472 path = ['session', 'local-endpoints-table-buckets']
473 self.add_config_item(self._nodeconfig, value, path)
475 def add_session_local_endpoints_table_memory(self, value):
476 """Add the size of local endpoints table memory.
478 :param value: Size of local endpoints table memory.
481 path = ['session', 'local-endpoints-table-memory']
482 self.add_config_item(self._nodeconfig, value, path)
484 def apply_config(self, filename=None, retries=60, restart_vpp=True):
485 """Generate and apply VPP configuration for node.
487 Use data from calls to this class to form a startup.conf file and
488 replace /etc/vpp/startup.conf with it on node.
490 :param filename: Startup configuration file name.
491 :param retries: Number of times (default 60) to re-try waiting.
492 :param restart_vpp: Whether to restart VPP.
495 :type restart_vpp: bool.
496 :raises RuntimeError: If writing config file failed or restart of VPP
497 failed or backup of VPP startup.conf failed.
499 self.dump_config(self._nodeconfig)
502 ssh.connect(self._node)
505 filename = self._vpp_startup_conf
507 if self._vpp_startup_conf_backup is not None:
509 ssh.exec_command('sudo cp {src} {dest}'.
510 format(src=self._vpp_startup_conf,
511 dest=self._vpp_startup_conf_backup))
513 raise RuntimeError('Backup of config file failed on node '
514 '{name}'.format(name=self._hostname))
517 ssh.exec_command('echo "{config}" | sudo tee {filename}'.
518 format(config=self._vpp_config,
522 raise RuntimeError('Writing config file failed to node {name}'.
523 format(name=self._hostname))
526 DUTSetup.start_service(self._node, Constants.VPP_UNIT)
528 # Sleep <waittime> seconds, up to <retry> times,
529 # and verify if VPP is running.
530 for _ in range(retries):
533 ssh.exec_command('echo show hardware-interfaces | '
534 'nc 0 5002 || echo "VPP not yet running"')
535 if ret == 0 and 'VPP not yet running' not in stdout:
538 raise RuntimeError('VPP failed to restart on node {name}'.
539 format(name=self._hostname))
541 def restore_config(self):
542 """Restore VPP startup.conf from backup.
544 :raises RuntimeError: When restoration of startup.conf file failed.
548 ssh.connect(self._node)
550 ret, _, _ = ssh.exec_command('sudo cp {src} {dest}'.
551 format(src=self._vpp_startup_conf_backup,
552 dest=self._vpp_startup_conf))
554 raise RuntimeError('Restoration of config file failed on node '
555 '{name}'.format(name=self._hostname))