1 # Copyright (c) 2016 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.topology import NodeType
21 from resources.libraries.python.topology import Topology
23 __all__ = ['VppConfigGenerator']
26 class VppConfigGenerator(object):
27 """VPP Configuration File Generator."""
30 """Initialize library."""
31 # VPP Node to apply configuration on
37 # Serialized VPP Configuration
40 self._vpp_service_name = 'vpp'
42 def set_node(self, node):
45 :param node: Node to store configuration on.
47 :raises RuntimeError: If Node type is not DUT.
49 if node['type'] != NodeType.DUT:
50 raise RuntimeError('Startup config can only be applied to DUT'
53 self._hostname = Topology.get_node_hostname(node)
55 def get_config_str(self):
56 """Get dumped startup configuration in VPP config format.
58 :returns: Startup configuration in VPP config format.
61 self.dump_config(self._nodeconfig)
62 return self._vpp_config
64 def add_config_item(self, config, value, path):
65 """Add startup configuration item.
67 :param config: Startup configuration of node.
68 :param value: Value to insert.
69 :param path: Path where to insert item.
75 config[path[0]] = value
77 if path[0] not in config:
79 self.add_config_item(config[path[0]], value, path[1:])
81 def dump_config(self, obj, level=-1):
82 """Dump the startup configuration in VPP config format.
84 :param obj: Python Object to print.
85 :param level: Nested level for indentation.
92 self._vpp_config += '{}{{\n'.format((level) * indent)
93 if isinstance(obj, dict):
94 for key, val in obj.items():
95 if hasattr(val, '__iter__'):
96 self._vpp_config += '{}{}\n'.format((level + 1) * indent,
98 self.dump_config(val, level + 1)
100 self._vpp_config += '{}{} {}\n'.format((level + 1) * indent,
104 self._vpp_config += '{}{}\n'.format((level + 1) * indent, val)
106 self._vpp_config += '{}}}\n'.format(level * indent)
108 def add_unix_log(self, value='/tmp/vpe.log'):
109 """Add UNIX log configuration.
111 :param value: Log file.
114 path = ['unix', 'log']
115 self.add_config_item(self._nodeconfig, value, path)
117 def add_unix_cli_listen(self, value='localhost:5002'):
118 """Add UNIX cli-listen configuration.
120 :param value: CLI listen address and port.
123 path = ['unix', 'cli-listen']
124 self.add_config_item(self._nodeconfig, value, path)
126 def add_unix_nodaemon(self):
127 """Add UNIX nodaemon configuration."""
128 path = ['unix', 'nodaemon']
129 self.add_config_item(self._nodeconfig, '', path)
131 def add_unix_exec(self, value):
132 """Add UNIX exec configuration."""
133 path = ['unix', 'exec']
134 self.add_config_item(self._nodeconfig, value, path)
136 def add_dpdk_dev(self, *devices):
137 """Add DPDK PCI device configuration.
139 :param devices: PCI device(s) (format xxxx:xx:xx.x)
141 :raises ValueError: If PCI address format is not valid.
143 pattern = re.compile("^[0-9A-Fa-f]{4}:[0-9A-Fa-f]{2}:"
144 "[0-9A-Fa-f]{2}\\.[0-9A-Fa-f]$")
145 for device in devices:
146 if not pattern.match(device):
147 raise ValueError('PCI address {} to be added to host {} '
148 'is not in valid format xxxx:xx:xx.x'.
149 format(device, self._hostname))
150 path = ['dpdk', 'dev {0}'.format(device)]
151 self.add_config_item(self._nodeconfig, '', path)
153 def add_dpdk_cryptodev(self, count):
154 """Add DPDK Crypto PCI device configuration.
156 :param count: Number of crypto devices to add.
159 cryptodev = Topology.get_cryptodev(self._node)
160 for i in range(count):
161 cryptodev_config = 'dev {0}'.format(
162 re.sub(r'\d.\d$', '1.'+str(i), cryptodev))
163 path = ['dpdk', cryptodev_config]
164 self.add_config_item(self._nodeconfig, '', path)
165 self.add_dpdk_uio_driver('igb_uio')
167 def add_dpdk_dev_default_rxq(self, value):
168 """Add DPDK dev default rxq configuration.
170 :param value: Default number of rxqs.
173 path = ['dpdk', 'dev default', 'num-rx-queues']
174 self.add_config_item(self._nodeconfig, value, path)
176 def add_dpdk_dev_default_txq(self, value):
177 """Add DPDK dev default txq configuration.
179 :param value: Default number of txqs.
182 path = ['dpdk', 'dev default', 'num-tx-queues']
183 self.add_config_item(self._nodeconfig, value, path)
185 def add_dpdk_dev_default_rxd(self, value):
186 """Add DPDK dev default rxd configuration.
188 :param value: Default number of rxds.
191 path = ['dpdk', 'dev default', 'num-rx-desc']
192 self.add_config_item(self._nodeconfig, value, path)
194 def add_dpdk_dev_default_txd(self, value):
195 """Add DPDK dev default txd configuration.
197 :param value: Default number of txds.
200 path = ['dpdk', 'dev default', 'num-tx-desc']
201 self.add_config_item(self._nodeconfig, value, path)
203 def add_dpdk_socketmem(self, value):
204 """Add DPDK socket memory configuration.
206 :param value: Socket memory size.
209 path = ['dpdk', 'socket-mem']
210 self.add_config_item(self._nodeconfig, value, path)
212 def add_dpdk_uio_driver(self, value):
213 """Add DPDK uio-driver configuration.
215 :param value: DPDK uio-driver configuration.
218 path = ['dpdk', 'uio-driver']
219 self.add_config_item(self._nodeconfig, value, path)
221 def add_cpu_main_core(self, value):
222 """Add CPU main core configuration.
224 :param value: Main core option.
227 path = ['cpu', 'main-core']
228 self.add_config_item(self._nodeconfig, value, path)
230 def add_cpu_corelist_workers(self, value):
231 """Add CPU corelist-workers configuration.
233 :param value: Corelist-workers option.
236 path = ['cpu', 'corelist-workers']
237 self.add_config_item(self._nodeconfig, value, path)
239 def add_heapsize(self, value):
240 """Add Heapsize configuration.
242 :param value: Amount of heapsize.
246 self.add_config_item(self._nodeconfig, value, path)
248 def add_api_trace(self):
249 """Add API trace configuration."""
250 path = ['api-trace', 'on']
251 self.add_config_item(self._nodeconfig, '', path)
253 def add_ip6_hash_buckets(self, value):
254 """Add IP6 hash buckets configuration.
256 :param value: Number of IP6 hash buckets.
259 path = ['ip6', 'hash-buckets']
260 self.add_config_item(self._nodeconfig, value, path)
262 def add_ip6_heap_size(self, value):
263 """Add IP6 heap-size configuration.
265 :param value: IP6 Heapsize amount.
268 path = ['ip6', 'heap-size']
269 self.add_config_item(self._nodeconfig, value, path)
271 def add_plugin_disable(self, *plugins):
272 """Add plugin disable for specific plugin.
274 :param plugins: Plugin(s) to disable.
277 for plugin in plugins:
278 path = ['plugins', 'plugin {0}'.format(plugin), 'disable']
279 self.add_config_item(self._nodeconfig, ' ', path)
281 def add_dpdk_no_multi_seg(self):
282 """Add DPDK no-multi-seg configuration."""
283 path = ['dpdk', 'no-multi-seg']
284 self.add_config_item(self._nodeconfig, '', path)
286 def add_nat(self, value='deterministic'):
287 """Add NAT configuration.
289 :param value: NAT mode.
293 self.add_config_item(self._nodeconfig, value, path)
295 def apply_config(self, filename='/etc/vpp/startup.conf', waittime=5,
296 retries=12, restart_vpp=True):
297 """Generate and apply VPP configuration for node.
299 Use data from calls to this class to form a startup.conf file and
300 replace /etc/vpp/startup.conf with it on node.
302 :param filename: Startup configuration file name.
303 :param waittime: Time to wait for VPP to restart (default 5 seconds).
304 :param retries: Number of times (default 12) to re-try waiting.
305 :param restart_vpp: Whether to restart VPP.
309 :type restart_vpp: bool.
310 :raises RuntimeError: If writing config file failed, or restarting of
313 self.dump_config(self._nodeconfig)
316 ssh.connect(self._node)
319 ssh.exec_command('echo "{config}" | sudo tee {filename}'.
320 format(config=self._vpp_config,
324 raise RuntimeError('Writing config file failed to node {}'.
325 format(self._hostname))
328 # Instead of restarting, we'll do separate start and stop
329 # actions. This way we don't care whether VPP was running
331 ssh.exec_command('sudo service {} stop'
332 .format(self._vpp_service_name))
334 ssh.exec_command('sudo service {} start'
335 .format(self._vpp_service_name))
337 raise RuntimeError('Restarting VPP failed on node {}'.
338 format(self._hostname))
340 # Sleep <waittime> seconds, up to <retry> times,
341 # and verify if VPP is running.
342 for _ in range(retries):
345 ssh.exec_command('echo show hardware-interfaces | '
346 'nc 0 5002 || echo "VPP not yet running"')
350 raise RuntimeError('VPP failed to restart on node {}'.
351 format(self._hostname))