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 util library"""
21 from collections import Counter
24 class VPPUtil(object):
25 """General class for any VPP related methods/functions."""
28 def exec_command(cmd, timeout=None):
29 """Execute a command on the local node.
31 :param cmd: Command to run locally.
32 :param timeout: Timeout value
35 :return return_code, stdout, stderr
36 :rtype: tuple(int, str, str)
39 logging.info(" Local Command: {}".format(cmd))
42 prc = subprocess.Popen(cmd, shell=True, bufsize=1,
43 stdin=subprocess.PIPE,
44 stdout=subprocess.PIPE,
45 stderr=subprocess.PIPE)
48 for line in iter(prc.stdout.readline, b''):
49 logging.info(" {}".format(line.strip('\n')))
53 for line in iter(prc.stderr.readline, b''):
54 logging.warn(" {}".format(line.strip('\n')))
61 def _autoconfig_backup_file(self, filename):
65 :param filename: The file to backup
69 # Does a copy of the file exist, if not create one
70 ofile = filename + '.orig'
71 (ret, stdout, stderr) = self.exec_command('ls {}'.format(ofile))
74 if stdout.strip('\n') != ofile:
75 cmd = 'sudo cp {} {}'.format(filename, ofile)
76 (ret, stdout, stderr) = self.exec_command(cmd)
80 def _install_vpp_pkg_ubuntu(self, node, pkg):
82 Install the VPP packages
84 :param node: Node dictionary
85 :param pkg: The vpp packages
90 cmd = 'apt-get -y install {}'.format(pkg)
91 (ret, stdout, stderr) = self.exec_command(cmd)
93 raise RuntimeError('{} failed on node {} {} {}'.format(
94 cmd, node['host'], stdout, stderr))
96 def _install_vpp_pkg_centos(self, node, pkg):
98 Install the VPP packages
100 :param node: Node dictionary
101 :param pkg: The vpp packages
106 cmd = 'yum -y install {}'.format(pkg)
107 (ret, stdout, stderr) = self.exec_command(cmd)
109 raise RuntimeError('{} failed on node {} {} {}'.format(
110 cmd, node['host'], stdout, stderr))
112 def _install_vpp_ubuntu(self, node, branch, ubuntu_version='xenial'):
114 Install the VPP packages
116 :param node: Node dictionary with cpuinfo.
117 :param branch: VPP branch
118 :param ubuntu_version: Ubuntu Version
121 :type ubuntu_version: string
124 # Modify the sources list
125 sfile = '/etc/apt/sources.list.d/99fd.io.list'
127 # Backup the sources list
128 self._autoconfig_backup_file(sfile)
130 reps = 'deb [trusted=yes] https://packagecloud.io/fdio/'
131 reps += '{}/ubuntu {} main ./\n'.format(branch, ubuntu_version)
133 with open(sfile, 'w') as sfd:
138 key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format(branch))
139 cmd = 'echo "{}" | apt-key add -'.format(key.content)
140 (ret, stdout, stderr) = self.exec_command(cmd)
142 raise RuntimeError('{} failed on node {} {}'.format(
147 # Install the package
148 cmd = 'apt-get -y update'
149 (ret, stdout, stderr) = self.exec_command(cmd)
151 raise RuntimeError('{} apt-get update failed on node {} {}'.format(
156 self._install_vpp_pkg_ubuntu(node, 'vpp-lib')
157 self._install_vpp_pkg_ubuntu(node, 'vpp')
158 self._install_vpp_pkg_ubuntu(node, 'vpp-plugins')
159 self._install_vpp_pkg_ubuntu(node, 'vpp-api-python')
160 self._install_vpp_pkg_ubuntu(node, 'vpp-api-java')
161 self._install_vpp_pkg_ubuntu(node, 'vpp-api-lua')
162 self._install_vpp_pkg_ubuntu(node, 'vpp-dev')
163 self._install_vpp_pkg_ubuntu(node, 'vpp-dbg')
165 def _install_vpp_centos(self, node, branch):
167 Install the VPP packages
169 :param node: Node dictionary with cpuinfo.
170 :param branch: The branch name release or master
175 # Be sure the correct system packages are installed
176 cmd = 'yum -y update'
177 (ret, stdout, stderr) = self.exec_command(cmd)
179 logging.debug('{} failed on node {} {}'.format(
184 cmd = 'yum -y install pygpgme yum-utils'
185 (ret, stdout, stderr) = self.exec_command(cmd)
187 logging.debug('{} failed on node {} {}'.format(
192 # Modify the sources list
193 sfile = '/etc/yum.repos.d/fdio-release.repo'
195 # Backup the sources list
196 self._autoconfig_backup_file(sfile)
198 # Remove the current file
199 cmd = 'rm {}'.format(sfile)
200 (ret, stdout, stderr) = self.exec_command(cmd)
202 logging.debug('{} failed on node {} {}'.format(
207 # Get the file contents
208 reps = '[fdio_{}]\n'.format(branch)
209 reps += 'name=fdio_{}\n'.format(branch)
210 reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/$basearch\n'.format(branch)
211 reps += 'repo_gpgcheck=1\n'
212 reps += 'gpgcheck=0\n'
213 reps += 'enabled=1\n'
214 reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(branch)
215 reps += 'sslverify=1\n'
216 reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
217 reps += 'metadata_expire=300\n'
219 reps += '[fdio_{}-source]\n'.format(branch)
220 reps += 'name=fdio_{}-source\n'.format(branch)
221 reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/SRPMS\n'.format(branch)
222 reps += 'repo_gpgcheck=1\n'
223 reps += 'gpgcheck=0\n'
224 reps += 'enabled=1\n'
225 reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(branch)
226 reps += 'sslverify =1\n'
227 reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
228 reps += 'metadata_expire=300\n'
230 with open(sfile, 'w') as sfd:
234 # Update the fdio repo
235 cmd = 'yum clean all'
236 (ret, stdout, stderr) = self.exec_command(cmd)
238 logging.debug('{} failed on node {} {}'.format(
243 cmd = "yum -q makecache -y --disablerepo='*' --enablerepo='fdio_{}'".format(branch)
244 (ret, stdout, stderr) = self.exec_command(cmd)
246 logging.debug('{} failed on node {} {}'.format(
251 # Install the packages
252 self._install_vpp_pkg_centos(node, 'vpp-selinux-policy')
253 self._install_vpp_pkg_centos(node, 'vpp-lib')
254 self._install_vpp_pkg_centos(node, 'vpp')
255 self._install_vpp_pkg_centos(node, 'vpp-plugins')
256 self._install_vpp_pkg_centos(node, 'vpp-api-python')
257 self._install_vpp_pkg_centos(node, 'vpp-api-java')
258 self._install_vpp_pkg_centos(node, 'vpp-api-lua')
259 self._install_vpp_pkg_centos(node, 'vpp-devel')
260 self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
262 def install_vpp(self, node, branch):
264 Install the VPP packages
266 :param node: Node dictionary with cpuinfo.
267 :param branch: The branch name
272 distro = self.get_linux_distro()
273 logging.info(" {}".format(distro[0]))
274 if distro[0] == 'Ubuntu':
275 logging.info("Install Ubuntu")
276 self._install_vpp_ubuntu(node, branch, ubuntu_version=distro[2])
277 elif distro[0] == 'CentOS Linux':
278 logging.info("Install CentOS")
279 self._install_vpp_centos(node, branch)
281 logging.info("Install CentOS (default)")
282 self._install_vpp_centos(node, branch)
285 def _uninstall_vpp_pkg_ubuntu(self, node, pkg):
287 Uninstall the VPP packages
289 :param node: Node dictionary
290 :param pkg: The vpp packages
294 cmd = 'dpkg --purge {}'.format(pkg)
295 (ret, stdout, stderr) = self.exec_command(cmd)
297 raise RuntimeError('{} failed on node {} {} {}'.format(
298 cmd, node['host'], stdout, stderr))
300 def _uninstall_vpp_pkg_centos(self, node, pkg):
302 Uninstall the VPP packages
304 :param node: Node dictionary
305 :param pkg: The vpp packages
309 cmd = 'yum -y remove {}'.format(pkg)
310 (ret, stdout, stderr) = self.exec_command(cmd)
312 raise RuntimeError('{} failed on node {} {} {}'.format(
313 cmd, node['host'], stdout, stderr))
315 def _uninstall_vpp_ubuntu(self, node):
317 Uninstall the VPP packages
319 :param node: Node dictionary with cpuinfo.
322 pkgs = self.get_installed_vpp_pkgs()
325 if 'version' in pkgs[0]:
326 logging.info("Uninstall Ubuntu Packages")
327 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dbg')
328 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dev')
329 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-python')
330 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-java')
331 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-lua')
332 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-plugins')
333 self._uninstall_vpp_pkg_ubuntu(node, 'vpp')
334 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-lib')
336 logging.info("Uninstall locally installed Ubuntu Packages")
338 self._uninstall_vpp_pkg_ubuntu(node, pkg['name'])
340 logging.error("There are no Ubuntu packages installed")
342 def _uninstall_vpp_centos(self, node):
344 Uninstall the VPP packages
346 :param node: Node dictionary with cpuinfo.
350 pkgs = self.get_installed_vpp_pkgs()
353 if 'version' in pkgs[0]:
354 logging.info("Uninstall CentOS Packages")
355 self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
356 self._uninstall_vpp_pkg_centos(node, 'vpp-devel')
357 self._uninstall_vpp_pkg_centos(node, 'vpp-api-python')
358 self._uninstall_vpp_pkg_centos(node, 'vpp-api-java')
359 self._uninstall_vpp_pkg_centos(node, 'vpp-api-lua')
360 self._uninstall_vpp_pkg_centos(node, 'vpp-plugins')
361 self._uninstall_vpp_pkg_centos(node, 'vpp')
362 self._uninstall_vpp_pkg_centos(node, 'vpp-lib')
363 self._uninstall_vpp_pkg_centos(node, 'vpp-selinux-policy')
365 logging.info("Uninstall locally installed CentOS Packages")
367 self._uninstall_vpp_pkg_centos(node, pkg['name'])
369 logging.error("There are no CentOS packages installed")
371 def uninstall_vpp(self, node):
373 Uninstall the VPP packages
375 :param node: Node dictionary with cpuinfo.
382 distro = self.get_linux_distro()
383 if distro[0] == 'Ubuntu':
384 logging.info("Uninstall Ubuntu")
385 self._uninstall_vpp_ubuntu(node)
386 elif distro[0] == 'CentOS Linux':
387 logging.info("Uninstall CentOS")
388 self._uninstall_vpp_centos(node)
390 logging.info("Uninstall CentOS (Default)")
391 self._uninstall_vpp_centos(node)
394 def show_vpp_settings(self, *additional_cmds):
396 Print default VPP settings. In case others are needed, can be
397 accepted as next parameters (each setting one parameter), preferably
400 :param additional_cmds: Additional commands that the vpp should print
402 :type additional_cmds: tuple
404 def_setting_tb_displayed = {
405 'IPv6 FIB': 'ip6 fib',
406 'IPv4 FIB': 'ip fib',
407 'Interface IP': 'int addr',
414 for cmd in additional_cmds:
415 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] \
418 for _, value in def_setting_tb_displayed.items():
419 self.exec_command('vppctl sh {}'.format(value))
424 Get a list of VMs that are connected to VPP interfaces
426 :param node: VPP node.
428 :returns: Dictionary containing a list of VMs and the interfaces that are connected to VPP
434 print "Need to implement get vms"
439 def get_int_ip(node):
441 Get the VPP interfaces and IP addresses
443 :param node: VPP node.
445 :returns: Dictionary containing VPP interfaces and IP addresses
449 cmd = 'vppctl show int addr'
450 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
454 lines = stdout.split('\n')
455 if len(lines[0]) is not 0:
456 if lines[0].split(' ')[0] == 'FileNotFoundError':
464 # If the first character is not whitespace
465 # create a new interface
466 if len(re.findall(r'\s', line[0])) is 0:
471 interfaces[name] = {}
472 interfaces[name]['state'] = spl[1].lstrip('(').rstrip('):\r')
474 interfaces[name]['address'] = line.lstrip(' ').rstrip('\r')
479 def get_hardware(node):
481 Get the VPP hardware information and return it in a
484 :param node: VPP node.
486 :returns: Dictionary containing VPP hardware information
491 cmd = 'vppctl show hard'
492 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
496 lines = stdout.split('\n')
497 if len(lines[0]) is not 0:
498 if lines[0].split(' ')[0] == 'FileNotFoundError':
505 # If the first character is not whitespace
506 # create a new interface
507 if len(re.findall(r'\s', line[0])) is 0:
510 interfaces[name] = {}
511 interfaces[name]['index'] = spl[1]
512 interfaces[name]['state'] = spl[2]
515 rfall = re.findall(r'Ethernet address', line)
518 interfaces[name]['mac'] = spl[2]
521 rfall = re.findall(r'carrier', line)
523 spl = line.split('carrier ')
524 interfaces[name]['carrier'] = spl[1]
527 rfall = re.findall(r'numa \d+', line)
529 spl = rfall[0].split()
530 interfaces[name]['numa'] = rfall[0].split()[1]
532 # Queues and Descriptors
533 rfall = re.findall(r'rx\: queues \d+', line)
535 interfaces[name]['rx queues'] = rfall[0].split()[2]
536 rdesc = re.findall(r'desc \d+', line)
538 interfaces[name]['rx descs'] = rdesc[0].split()[1]
540 rfall = re.findall(r'tx\: queues \d+', line)
542 interfaces[name]['tx queues'] = rfall[0].split()[2]
543 rdesc = re.findall(r'desc \d+', line)
545 interfaces[name]['tx descs'] = rdesc[0].split()[1]
549 def _get_installed_vpp_pkgs_ubuntu(self, distro):
551 Get the VPP hardware information and return it in a
554 :param distro: The linux distro
556 :returns: List of the packages installed
561 cmd = 'dpkg -l | grep vpp'
562 (ret, stdout, stderr) = self.exec_command(cmd)
566 lines = stdout.split('\n')
571 pkg = {'name': items[1], 'version': items[2]}
576 def _get_installed_vpp_pkgs_centos(self):
578 Get the VPP hardware information and return it in a
581 :returns: List of the packages installed
586 cmd = 'rpm -qa | grep vpp'
587 (ret, stdout, stderr) = self.exec_command(cmd)
591 lines = stdout.split('\n')
598 pkg = {'name': items[0]}
600 pkg = {'name': items[1], 'version': items[2]}
606 def get_installed_vpp_pkgs(self):
608 Get the VPP hardware information and return it in a
611 :returns: List of the packages installed
615 distro = self.get_linux_distro()
616 if distro[0] == 'Ubuntu':
617 pkgs = self._get_installed_vpp_pkgs_ubuntu(distro)
618 elif distro[0] == 'CentOS Linux':
619 pkgs = self._get_installed_vpp_pkgs_centos()
621 pkgs = self._get_installed_vpp_pkgs_centos()
627 def get_interfaces_numa_node(node, *iface_keys):
628 """Get numa node on which are located most of the interfaces.
630 Return numa node with highest count of interfaces provided as arguments.
631 Return 0 if the interface does not have numa_node information available.
632 If all interfaces have unknown location (-1), then return 0.
633 If most of interfaces have unknown location (-1), but there are
634 some interfaces with known location, then return the second most
635 location of the provided interfaces.
637 :param node: Node from DICT__nodes.
638 :param iface_keys: Interface keys for lookup.
640 :type iface_keys: strings
643 for if_key in iface_keys:
645 numa_list.append(node['interfaces'][if_key].get('numa_node'))
649 numa_cnt_mc = Counter(numa_list).most_common()
650 numa_cnt_mc_len = len(numa_cnt_mc)
651 if numa_cnt_mc_len > 0 and numa_cnt_mc[0][0] != -1:
652 return numa_cnt_mc[0][0]
653 elif numa_cnt_mc_len > 1 and numa_cnt_mc[0][0] == -1:
654 return numa_cnt_mc[1][0]
662 Starts vpp for a given node
664 :param node: VPP node.
668 cmd = 'service vpp restart'
669 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
671 raise RuntimeError('{} failed on node {} {} {}'.
672 format(cmd, node['host'],
679 Starts vpp for a given node
681 :param node: VPP node.
685 cmd = 'service vpp start'
686 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
688 raise RuntimeError('{} failed on node {} {} {}'.
689 format(cmd, node['host'],
696 Stops vpp for a given node
698 :param node: VPP node.
702 cmd = 'service vpp stop'
703 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
705 logging.debug('{} failed on node {} {} {}'.format(cmd, node['host'], stdout, stderr))
707 # noinspection RegExpRedundantEscape
716 :returns: status, errors
717 :rtype: tuple(str, list)
721 pkgs = vutil.get_installed_vpp_pkgs()
723 return "Not Installed", errors
725 cmd = 'service vpp status'
726 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
728 # Get the active status
729 state = re.findall(r'Active:[\w (\)]+', stdout)[0].split(' ')
731 statestr = "{} {}".format(state[1], state[2])
735 # For now we won't look for DPDK errors
736 # lines = stdout.split('\n')
738 # if 'EAL' in line or \
739 # 'FAILURE' in line or \
740 # 'failed' in line or \
742 # errors.append(line.lstrip(' '))
744 return statestr, errors
747 def get_linux_distro():
749 Get the linux distribution and check if it is supported
751 :returns: linux distro, None if the distro is not supported
755 distro = platform.linux_distribution()
756 if distro[0] == 'Ubuntu' or \
757 distro[0] == 'CentOS Linux' or \
758 distro[:7] == 'Red Hat':
761 raise RuntimeError('Linux Distribution {} is not supported'.format(distro[0]))
767 Gets VPP Version information
774 cmd = 'vppctl show version verbose'
775 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
779 lines = stdout.split('\n')
780 if len(lines[0]) is not 0:
781 if lines[0].split(' ')[0] == 'FileNotFoundError':
787 dct = line.split(':')
788 version[dct[0]] = dct[1].lstrip(' ')
793 def show_bridge(node):
795 Shows the current bridge configuration
797 :param node: VPP node.
799 :returns: A list of interfaces
803 cmd = 'vppctl show bridge'
804 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
806 raise RuntimeError('{} failed on node {} {} {}'.
807 format(cmd, node['host'],
809 lines = stdout.split('\r\n')
812 if line == 'no bridge-domains in use':
818 lspl = line.lstrip(' ').split()
819 if lspl[0] != 'BD-ID':
820 bridges.append(lspl[0])
822 for bridge in bridges:
823 cmd = 'vppctl show bridge {} detail'.format(bridge)
824 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
826 raise RuntimeError('{} failed on node {} {} {}'.
827 format(cmd, node['host'],
830 lines = stdout.split('\r\n')
832 iface = re.findall(r'[a-zA-z]+\d+/\d+/\d+', line)
834 ifcidx = {'name': iface[0], 'index': line.split()[1]}
835 ifaces.append(ifcidx)