7ee3e2ceb05aa1d82dcb6fd7f1dc2dcb55a1c73e
[csit.git] / resources / libraries / python / VppConfigGenerator.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """VPP Configuration File Generator library.
15
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.
19 """
20
21 import re
22
23 from resources.libraries.python.ssh import exec_cmd_no_error
24 from resources.libraries.python.topology import NodeType
25 from resources.libraries.python.topology import Topology
26 from resources.libraries.python.VPPUtil import VPPUtil
27
28 __all__ = ['VppConfigGenerator']
29
30
31 def pci_dev_check(pci_dev):
32     """Check if provided PCI address is in correct format.
33
34     :param pci_dev: PCI address (expected format: xxxx:xx:xx.x).
35     :type pci_dev: str
36     :returns: True if PCI address is in correct format.
37     :rtype: bool
38     :raises ValueError: If PCI address is in incorrect format.
39     """
40     pattern = re.compile("^[0-9A-Fa-f]{4}:[0-9A-Fa-f]{2}:"
41                          "[0-9A-Fa-f]{2}\\.[0-9A-Fa-f]$")
42     if not pattern.match(pci_dev):
43         raise ValueError('PCI address {addr} is not in valid format '
44                          'xxxx:xx:xx.x'.format(addr=pci_dev))
45     return True
46
47
48 class VppConfigGenerator(object):
49     """VPP Configuration File Generator."""
50
51     def __init__(self):
52         """Initialize library."""
53         # VPP Node to apply configuration on
54         self._node = ''
55         # VPP Hostname
56         self._hostname = ''
57         # VPP Configuration
58         self._nodeconfig = {}
59         # Serialized VPP Configuration
60         self._vpp_config = ''
61         # VPP Service name
62         self._vpp_service_name = 'vpp'
63         # VPP Logfile location
64         self._vpp_logfile = '/tmp/vpe.log'
65         # VPP Startup config location
66         self._vpp_startup_conf = '/etc/vpp/startup.conf'
67         # VPP Startup config backup location
68         self._vpp_startup_conf_backup = None
69
70     def set_node(self, node):
71         """Set DUT node.
72
73         :param node: Node to store configuration on.
74         :type node: dict
75         :raises RuntimeError: If Node type is not DUT.
76         """
77         if node['type'] != NodeType.DUT:
78             raise RuntimeError('Startup config can only be applied to DUT'
79                                'node.')
80         self._node = node
81         self._hostname = Topology.get_node_hostname(node)
82
83     def set_vpp_logfile(self, logfile):
84         """Set VPP logfile location.
85
86         :param logfile: VPP logfile location.
87         :type logfile: str
88         """
89         self._vpp_logfile = logfile
90
91     def set_vpp_startup_conf_backup(self, backup='/etc/vpp/startup.backup'):
92         """Set VPP startup configuration backup.
93
94         :param backup: VPP logfile location.
95         :type backup: str
96         """
97         self._vpp_startup_conf_backup = backup
98
99     def get_config_str(self):
100         """Get dumped startup configuration in VPP config format.
101
102         :returns: Startup configuration in VPP config format.
103         :rtype: str
104         """
105         self.dump_config(self._nodeconfig)
106         return self._vpp_config
107
108     def add_config_item(self, config, value, path):
109         """Add startup configuration item.
110
111         :param config: Startup configuration of node.
112         :param value: Value to insert.
113         :param path: Path where to insert item.
114         :type config: dict
115         :type value: str
116         :type path: list
117         """
118         if len(path) == 1:
119             config[path[0]] = value
120             return
121         if path[0] not in config:
122             config[path[0]] = {}
123         elif isinstance(config[path[0]], str):
124             config[path[0]] = {} if config[path[0]] == '' \
125                 else {config[path[0]]: ''}
126         self.add_config_item(config[path[0]], value, path[1:])
127
128     def dump_config(self, obj, level=-1):
129         """Dump the startup configuration in VPP config format.
130
131         :param obj: Python Object to print.
132         :param level: Nested level for indentation.
133         :type obj: Obj
134         :type level: int
135         :returns: nothing
136         """
137         indent = '  '
138         if level >= 0:
139             self._vpp_config += '{}{{\n'.format((level) * indent)
140         if isinstance(obj, dict):
141             for key, val in obj.items():
142                 if hasattr(val, '__iter__'):
143                     self._vpp_config += '{}{}\n'.format((level + 1) * indent,
144                                                         key)
145                     self.dump_config(val, level + 1)
146                 else:
147                     self._vpp_config += '{}{} {}\n'.format((level + 1) * indent,
148                                                            key, val)
149         else:
150             for val in obj:
151                 self._vpp_config += '{}{}\n'.format((level + 1) * indent, val)
152         if level >= 0:
153             self._vpp_config += '{}}}\n'.format(level * indent)
154
155     def add_unix_log(self, value=None):
156         """Add UNIX log configuration.
157
158         :param value: Log file.
159         :type value: str
160         """
161         path = ['unix', 'log']
162         if value is None:
163             value = self._vpp_logfile
164         self.add_config_item(self._nodeconfig, value, path)
165
166     def add_unix_cli_listen(self, value='/run/vpp/cli.sock'):
167         """Add UNIX cli-listen configuration.
168
169         :param value: CLI listen address and port or path to CLI socket.
170         :type value: str
171         """
172         path = ['unix', 'cli-listen']
173         self.add_config_item(self._nodeconfig, value, path)
174
175     def add_unix_gid(self, value='vpp'):
176         """Add UNIX gid configuration.
177
178         :param value: Gid.
179         :type value: str
180         """
181         path = ['unix', 'gid']
182         self.add_config_item(self._nodeconfig, value, path)
183
184     def add_unix_nodaemon(self):
185         """Add UNIX nodaemon configuration."""
186         path = ['unix', 'nodaemon']
187         self.add_config_item(self._nodeconfig, '', path)
188
189     def add_unix_coredump(self):
190         """Add UNIX full-coredump configuration."""
191         path = ['unix', 'full-coredump']
192         self.add_config_item(self._nodeconfig, '', path)
193
194     def add_unix_exec(self, value):
195         """Add UNIX exec configuration."""
196         path = ['unix', 'exec']
197         self.add_config_item(self._nodeconfig, value, path)
198
199     def add_socksvr(self, socket="default"):
200         """Add socksvr configuration."""
201         path = ['socksvr', socket]
202         self.add_config_item(self._nodeconfig, '', path)
203
204     def add_api_segment_gid(self, value='vpp'):
205         """Add API-SEGMENT gid configuration.
206
207         :param value: Gid.
208         :type value: str
209         """
210         path = ['api-segment', 'gid']
211         self.add_config_item(self._nodeconfig, value, path)
212
213     def add_api_segment_global_size(self, value):
214         """Add API-SEGMENT global-size configuration.
215
216         :param value: Global size.
217         :type value: str
218         """
219         path = ['api-segment', 'global-size']
220         self.add_config_item(self._nodeconfig, value, path)
221
222     def add_api_segment_api_size(self, value):
223         """Add API-SEGMENT api-size configuration.
224
225         :param value: API size.
226         :type value: str
227         """
228         path = ['api-segment', 'api-size']
229         self.add_config_item(self._nodeconfig, value, path)
230
231     def add_buffers_per_numa(self, value):
232         """Increase number of buffers allocated.
233
234         :param value: Number of buffers allocated.
235         :type value: int
236         """
237         path = ['buffers', 'buffers-per-numa']
238         self.add_config_item(self._nodeconfig, value, path)
239
240     def add_dpdk_dev(self, *devices):
241         """Add DPDK PCI device configuration.
242
243         :param devices: PCI device(s) (format xxxx:xx:xx.x)
244         :type devices: tuple
245         """
246         for device in devices:
247             if pci_dev_check(device):
248                 path = ['dpdk', 'dev {0}'.format(device)]
249                 self.add_config_item(self._nodeconfig, '', path)
250
251     def add_dpdk_dev_parameter(self, device, parameter, value):
252         """Add parameter for DPDK device.
253
254         :param device: PCI device (format xxxx:xx:xx.x).
255         :param parameter: Parameter name.
256         :param value: Parameter value.
257         :type device: str
258         :type parameter: str
259         :type value: str
260         """
261         if pci_dev_check(device):
262             path = ['dpdk', 'dev {0}'.format(device), parameter]
263             self.add_config_item(self._nodeconfig, value, path)
264
265     def add_dpdk_cryptodev(self, count):
266         """Add DPDK Crypto PCI device configuration.
267
268         :param count: Number of HW crypto devices to add.
269         :type count: int
270         """
271         cryptodev = Topology.get_cryptodev(self._node)
272         for i in range(count):
273             cryptodev_config = 'dev {0}'.format(
274                 re.sub(r'\d.\d$', '1.'+str(i), cryptodev))
275             path = ['dpdk', cryptodev_config]
276             self.add_config_item(self._nodeconfig, '', path)
277         self.add_dpdk_uio_driver('vfio-pci')
278
279     def add_dpdk_sw_cryptodev(self, sw_pmd_type, socket_id, count):
280         """Add DPDK SW Crypto device configuration.
281
282         :param sw_pmd_type: Type of SW crypto device PMD to add.
283         :param socket_id: Socket ID.
284         :param count: Number of SW crypto devices to add.
285         :type sw_pmd_type: str
286         :type socket_id: int
287         :type count: int
288         """
289         for _ in range(count):
290             cryptodev_config = 'vdev cryptodev_{0}_pmd,socket_id={1}'.\
291                 format(sw_pmd_type, str(socket_id))
292             path = ['dpdk', cryptodev_config]
293             self.add_config_item(self._nodeconfig, '', path)
294
295     def add_dpdk_eth_bond_dev(self, ethbond_id, mode, xmit_policy, *slaves):
296         """Add DPDK Eth_bond device configuration.
297
298         :param ethbond_id: Eth_bond device ID.
299         :param mode: Link bonding mode.
300         :param xmit_policy: Transmission policy.
301         :param slaves: PCI device(s) to be bonded (format xxxx:xx:xx.x).
302         :type ethbond_id: str or int
303         :type mode: str or int
304         :type xmit_policy: str
305         :type slaves: list
306         """
307         slaves_config = ',slave=' + \
308                         ',slave='.join(slave if pci_dev_check(slave) else ''
309                                        for slave in slaves)
310         ethbond_config = 'vdev eth_bond{id},mode={mode}{slaves},' \
311                          'xmit_policy={xmit_pol}'.format(id=ethbond_id,
312                                                          mode=mode,
313                                                          slaves=slaves_config,
314                                                          xmit_pol=xmit_policy)
315         path = ['dpdk', ethbond_config]
316         self.add_config_item(self._nodeconfig, '', path)
317
318     def add_dpdk_dev_default_rxq(self, value):
319         """Add DPDK dev default rxq configuration.
320
321         :param value: Default number of rxqs.
322         :type value: str
323         """
324         path = ['dpdk', 'dev default', 'num-rx-queues']
325         self.add_config_item(self._nodeconfig, value, path)
326
327     def add_dpdk_dev_default_txq(self, value):
328         """Add DPDK dev default txq configuration.
329
330         :param value: Default number of txqs.
331         :type value: str
332         """
333         path = ['dpdk', 'dev default', 'num-tx-queues']
334         self.add_config_item(self._nodeconfig, value, path)
335
336     def add_dpdk_dev_default_rxd(self, value):
337         """Add DPDK dev default rxd configuration.
338
339         :param value: Default number of rxds.
340         :type value: str
341         """
342         path = ['dpdk', 'dev default', 'num-rx-desc']
343         self.add_config_item(self._nodeconfig, value, path)
344
345     def add_dpdk_dev_default_txd(self, value):
346         """Add DPDK dev default txd configuration.
347
348         :param value: Default number of txds.
349         :type value: str
350         """
351         path = ['dpdk', 'dev default', 'num-tx-desc']
352         self.add_config_item(self._nodeconfig, value, path)
353
354     def add_dpdk_log_level(self, value):
355         """Add DPDK log-level configuration.
356
357         :param value: Log level.
358         :type value: str
359         """
360         path = ['dpdk', 'log-level']
361         self.add_config_item(self._nodeconfig, value, path)
362
363     def add_dpdk_no_pci(self):
364         """Add DPDK no-pci."""
365         path = ['dpdk', 'no-pci']
366         self.add_config_item(self._nodeconfig, '', path)
367
368     def add_dpdk_uio_driver(self, value=None):
369         """Add DPDK uio-driver configuration.
370
371         :param value: DPDK uio-driver configuration. By default, driver will be
372                       loaded automatically from Topology file, still leaving
373                       option to manually override by parameter.
374         :type value: str
375         """
376         if value is None:
377             value = Topology.get_uio_driver(self._node)
378         path = ['dpdk', 'uio-driver']
379         self.add_config_item(self._nodeconfig, value, path)
380
381     def add_cpu_main_core(self, value):
382         """Add CPU main core configuration.
383
384         :param value: Main core option.
385         :type value: str
386         """
387         path = ['cpu', 'main-core']
388         self.add_config_item(self._nodeconfig, value, path)
389
390     def add_cpu_corelist_workers(self, value):
391         """Add CPU corelist-workers configuration.
392
393         :param value: Corelist-workers option.
394         :type value: str
395         """
396         path = ['cpu', 'corelist-workers']
397         self.add_config_item(self._nodeconfig, value, path)
398
399     def add_heapsize(self, value):
400         """Add Heapsize configuration.
401
402         :param value: Amount of heapsize.
403         :type value: str
404         """
405         path = ['heapsize']
406         self.add_config_item(self._nodeconfig, value, path)
407
408     def add_api_trace(self):
409         """Add API trace configuration."""
410         path = ['api-trace', 'on']
411         self.add_config_item(self._nodeconfig, '', path)
412
413     def add_ip6_hash_buckets(self, value):
414         """Add IP6 hash buckets configuration.
415
416         :param value: Number of IP6 hash buckets.
417         :type value: str
418         """
419         path = ['ip6', 'hash-buckets']
420         self.add_config_item(self._nodeconfig, value, path)
421
422     def add_ip6_heap_size(self, value):
423         """Add IP6 heap-size configuration.
424
425         :param value: IP6 Heapsize amount.
426         :type value: str
427         """
428         path = ['ip6', 'heap-size']
429         self.add_config_item(self._nodeconfig, value, path)
430
431     def add_ip_heap_size(self, value):
432         """Add IP heap-size configuration.
433
434         :param value: IP Heapsize amount.
435         :type value: str
436         """
437         path = ['ip', 'heap-size']
438         self.add_config_item(self._nodeconfig, value, path)
439
440     def add_statseg_size(self, value):
441         """Add stats segment heap size configuration.
442
443         :param value: Stats heapsize amount.
444         :type value: str
445         """
446         path = ['statseg', 'size']
447         self.add_config_item(self._nodeconfig, value, path)
448
449     def add_statseg_per_node_counters(self, value):
450         """Add stats per-node-counters configuration.
451
452         :param value: "on" to switch the counters on.
453         :type value: str
454         """
455         path = ['statseg', 'per-node-counters']
456         self.add_config_item(self._nodeconfig, value, path)
457
458     def add_plugin(self, state, *plugins):
459         """Add plugin section for specific plugin(s).
460
461         :param state: State of plugin [enable|disable].
462         :param plugins: Plugin(s) to disable.
463         :type state: str
464         :type plugins: list
465         """
466         for plugin in plugins:
467             path = ['plugins', 'plugin {0}'.format(plugin), state]
468             self.add_config_item(self._nodeconfig, ' ', path)
469
470     def add_dpdk_no_multi_seg(self):
471         """Add DPDK no-multi-seg configuration."""
472         path = ['dpdk', 'no-multi-seg']
473         self.add_config_item(self._nodeconfig, '', path)
474
475     def add_dpdk_no_tx_checksum_offload(self):
476         """Add DPDK no-tx-checksum-offload configuration."""
477         path = ['dpdk', 'no-tx-checksum-offload']
478         self.add_config_item(self._nodeconfig, '', path)
479
480     def add_nat(self, value='deterministic'):
481         """Add NAT configuration.
482
483         :param value: NAT mode.
484         :type value: str
485         """
486         path = ['nat']
487         self.add_config_item(self._nodeconfig, value, path)
488
489     def add_tcp_preallocated_connections(self, value):
490         """Add TCP pre-allocated connections.
491
492         :param value: The number of pre-allocated connections.
493         :type value: int
494         """
495         path = ['tcp', 'preallocated-connections']
496         self.add_config_item(self._nodeconfig, value, path)
497
498     def add_tcp_preallocated_half_open_connections(self, value):
499         """Add TCP pre-allocated half open connections.
500
501         :param value: The number of pre-allocated half open connections.
502         :type value: int
503         """
504         path = ['tcp', 'preallocated-half-open-connections']
505         self.add_config_item(self._nodeconfig, value, path)
506
507     def add_session_event_queue_length(self, value):
508         """Add session event queue length.
509
510         :param value: Session event queue length.
511         :type value: int
512         """
513         path = ['session', 'event-queue-length']
514         self.add_config_item(self._nodeconfig, value, path)
515
516     def add_session_preallocated_sessions(self, value):
517         """Add the number of pre-allocated sessions.
518
519         :param value: Number of pre-allocated sessions.
520         :type value: int
521         """
522         path = ['session', 'preallocated-sessions']
523         self.add_config_item(self._nodeconfig, value, path)
524
525     def add_session_v4_session_table_buckets(self, value):
526         """Add number of v4 session table buckets to the config.
527
528         :param value: Number of v4 session table buckets.
529         :type value: int
530         """
531         path = ['session', 'v4-session-table-buckets']
532         self.add_config_item(self._nodeconfig, value, path)
533
534     def add_session_v4_session_table_memory(self, value):
535         """Add the size of v4 session table memory.
536
537         :param value: Size of v4 session table memory.
538         :type value: str
539         """
540         path = ['session', 'v4-session-table-memory']
541         self.add_config_item(self._nodeconfig, value, path)
542
543     def add_session_v4_halfopen_table_buckets(self, value):
544         """Add the number of v4 halfopen table buckets.
545
546         :param value: Number of v4 halfopen table buckets.
547         :type value: int
548         """
549         path = ['session', 'v4-halfopen-table-buckets']
550         self.add_config_item(self._nodeconfig, value, path)
551
552     def add_session_v4_halfopen_table_memory(self, value):
553         """Add the size of v4 halfopen table memory.
554
555         :param value: Size of v4 halfopen table memory.
556         :type value: str
557         """
558         path = ['session', 'v4-halfopen-table-memory']
559         self.add_config_item(self._nodeconfig, value, path)
560
561     def add_session_local_endpoints_table_buckets(self, value):
562         """Add the number of local endpoints table buckets.
563
564         :param value: Number of local endpoints table buckets.
565         :type value: int
566         """
567         path = ['session', 'local-endpoints-table-buckets']
568         self.add_config_item(self._nodeconfig, value, path)
569
570     def add_session_local_endpoints_table_memory(self, value):
571         """Add the size of local endpoints table memory.
572
573         :param value: Size of local endpoints table memory.
574         :type value: str
575         """
576         path = ['session', 'local-endpoints-table-memory']
577         self.add_config_item(self._nodeconfig, value, path)
578
579     def write_config(self, filename=None):
580         """Generate and write VPP startup configuration to file.
581
582         Use data from calls to this class to form a startup.conf file and
583         replace /etc/vpp/startup.conf with it on topology node.
584
585         :param filename: Startup configuration file name.
586         :type filename: str
587         """
588         self.dump_config(self._nodeconfig)
589
590         if filename is None:
591             filename = self._vpp_startup_conf
592
593         if self._vpp_startup_conf_backup is not None:
594             cmd = ('cp {src} {dest}'.format(
595                 src=self._vpp_startup_conf, dest=self._vpp_startup_conf_backup))
596             exec_cmd_no_error(
597                 self._node, cmd, sudo=True, message='Copy config file failed!')
598
599         cmd = ('echo "{config}" | sudo tee {filename}'.format(
600             config=self._vpp_config, filename=filename))
601         exec_cmd_no_error(
602             self._node, cmd, message='Writing config file failed!')
603
604     def apply_config(self, filename=None, verify_vpp=True):
605         """Generate and write VPP startup configuration to file and restart VPP.
606
607         Use data from calls to this class to form a startup.conf file and
608         replace /etc/vpp/startup.conf with it on topology node.
609
610         :param filename: Startup configuration file name.
611         :param verify_vpp: Verify VPP is running after restart.
612         :type filename: str
613         :type verify_vpp: bool
614         """
615         self.write_config(filename=filename)
616
617         VPPUtil.restart_vpp_service(self._node)
618         if verify_vpp:
619             VPPUtil.verify_vpp(self._node)
620
621     def restore_config(self):
622         """Restore VPP startup.conf from backup."""
623         cmd = ('cp {src} {dest}'.format(
624             src=self._vpp_startup_conf_backup, dest=self._vpp_startup_conf))
625         exec_cmd_no_error(
626             self._node, cmd, sudo=True, message='Copy config file failed!')