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
23 # VPP_VERSION = '1707'
24 # VPP_VERSION = '1710'
28 class VPPUtil(object):
29 """General class for any VPP related methods/functions."""
32 def exec_command(cmd, timeout=None):
33 """Execute a command on the local node.
35 :param cmd: Command to run locally.
36 :param timeout: Timeout value
39 :return return_code, stdout, stderr
40 :rtype: tuple(int, str, str)
43 logging.info(" Local Command: {}".format(cmd))
46 prc = subprocess.Popen(cmd, shell=True, bufsize=1,
47 stdin=subprocess.PIPE,
48 stdout=subprocess.PIPE,
49 stderr=subprocess.PIPE)
52 for line in iter(prc.stdout.readline, b''):
53 logging.info(" {}".format(line.strip('\n')))
57 for line in iter(prc.stderr.readline, b''):
58 logging.warn(" {}".format(line.strip('\n')))
65 def _autoconfig_backup_file(self, filename):
69 :param filename: The file to backup
73 # Does a copy of the file exist, if not create one
74 ofile = filename + '.orig'
75 (ret, stdout, stderr) = self.exec_command('ls {}'.format(ofile))
78 if stdout.strip('\n') != ofile:
79 cmd = 'sudo cp {} {}'.format(filename, ofile)
80 (ret, stdout, stderr) = self.exec_command(cmd)
84 def _install_vpp_pkg_ubuntu(self, node, pkg):
86 Install the VPP packages
88 :param node: Node dictionary
89 :param pkg: The vpp packages
94 cmd = 'apt-get -y install {}'.format(pkg)
95 (ret, stdout, stderr) = self.exec_command(cmd)
97 raise RuntimeError('{} failed on node {} {} {}'.format(
98 cmd, node['host'], stdout, stderr))
100 def _install_vpp_pkg_centos(self, node, pkg):
102 Install the VPP packages
104 :param node: Node dictionary
105 :param pkg: The vpp packages
110 cmd = 'yum -y install {}'.format(pkg)
111 (ret, stdout, stderr) = self.exec_command(cmd)
113 raise RuntimeError('{} failed on node {} {} {}'.format(
114 cmd, node['host'], stdout, stderr))
116 def _install_vpp_ubuntu(self, node, fdio_release=VPP_VERSION,
117 ubuntu_version='xenial'):
119 Install the VPP packages
121 :param node: Node dictionary with cpuinfo.
122 :param fdio_release: VPP release number
123 :param ubuntu_version: Ubuntu Version
125 :type fdio_release: string
126 :type ubuntu_version: string
129 # Modify the sources list
130 sfile = '/etc/apt/sources.list.d/99fd.io.list'
132 # Backup the sources list
133 self._autoconfig_backup_file(sfile)
135 reps = 'deb [trusted=yes] https://packagecloud.io/fdio/'
136 # When using a stable branch
137 # reps += '{}/ubuntu {} main ./\n'.format(fdio_release, ubuntu_version)
139 reps += 'release/ubuntu {} main ./\n'.format(ubuntu_version)
141 # reps += 'master/ubuntu {} main ./\n'.format(ubuntu_version)
143 # reps += '1807/ubuntu {} main ./\n'.format(ubuntu_version)
145 with open(sfile, 'w') as sfd:
150 key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('release'))
151 # key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('master'))
152 # key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('1807'))
153 cmd = 'echo "{}" | apt-key add -'.format(key.content)
154 (ret, stdout, stderr) = self.exec_command(cmd)
156 raise RuntimeError('{} failed on node {} {}'.format(
161 # Install the package
162 cmd = 'apt-get -y update'
163 (ret, stdout, stderr) = self.exec_command(cmd)
165 raise RuntimeError('{} apt-get update failed on node {} {}'.format(
170 self._install_vpp_pkg_ubuntu(node, 'vpp-lib')
171 self._install_vpp_pkg_ubuntu(node, 'vpp')
172 self._install_vpp_pkg_ubuntu(node, 'vpp-plugins')
173 self._install_vpp_pkg_ubuntu(node, 'vpp-api-python')
174 self._install_vpp_pkg_ubuntu(node, 'vpp-api-java')
175 self._install_vpp_pkg_ubuntu(node, 'vpp-api-lua')
176 self._install_vpp_pkg_ubuntu(node, 'vpp-dev')
177 self._install_vpp_pkg_ubuntu(node, 'vpp-dbg')
179 def _install_vpp_centos(self, node, fdio_release=VPP_VERSION,
180 centos_version='centos7'):
182 Install the VPP packages
184 :param node: Node dictionary with cpuinfo.
185 :param fdio_release: VPP release number
186 :param centos_version: Ubuntu Version
188 :type fdio_release: string
189 :type centos_version: string
192 # Be sure the correct system packages are installed
193 cmd = 'yum -y update'
194 (ret, stdout, stderr) = self.exec_command(cmd)
196 logging.debug('{} failed on node {} {}'.format(
201 cmd = 'yum -y install pygpgme yum-utils'
202 (ret, stdout, stderr) = self.exec_command(cmd)
204 logging.debug('{} failed on node {} {}'.format(
209 # Modify the sources list
210 sfile = '/etc/yum.repos.d/fdio-release.repo'
212 # Backup the sources list
213 self._autoconfig_backup_file(sfile)
215 # Remove the current file
216 cmd = 'rm {}'.format(sfile)
217 (ret, stdout, stderr) = self.exec_command(cmd)
219 logging.debug('{} failed on node {} {}'.format(
229 # Get the file contents
230 reps = '[fdio_{}]\n'.format(bname)
231 reps += 'name=fdio_{}\n'.format(bname)
232 reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/$basearch\n'.format(bname)
233 reps += 'repo_gpgcheck=1\n'
234 reps += 'gpgcheck=0\n'
235 reps += 'enabled=1\n'
236 reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(bname)
237 reps += 'sslverify=1\n'
238 reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
239 reps += 'metadata_expire=300\n'
241 reps += '[fdio_{}-source]\n'.format(bname)
242 reps += 'name=fdio_release-{}\n'.format(bname)
243 reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/SRPMS\n'.format(bname)
244 reps += 'repo_gpgcheck=1\n'
245 reps += 'gpgcheck=0\n'
246 reps += 'enabled=1\n'
247 reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(bname)
248 reps += 'sslverify =1\n'
249 reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
250 reps += 'metadata_expire=300\n'
252 with open(sfile, 'w') as sfd:
256 # Update the fdio repo
257 cmd = 'yum clean all'
258 (ret, stdout, stderr) = self.exec_command(cmd)
260 logging.debug('{} failed on node {} {}'.format(
265 cmd = "yum -q makecache -y --disablerepo='*' --enablerepo='fdio_{}'".format(bname)
266 (ret, stdout, stderr) = self.exec_command(cmd)
268 logging.debug('{} failed on node {} {}'.format(
273 # Install the packages
274 self._install_vpp_pkg_centos(node, 'vpp-selinux-policy')
275 self._install_vpp_pkg_centos(node, 'vpp-lib')
276 self._install_vpp_pkg_centos(node, 'vpp')
277 self._install_vpp_pkg_centos(node, 'vpp-plugins')
278 self._install_vpp_pkg_centos(node, 'vpp-api-python')
279 self._install_vpp_pkg_centos(node, 'vpp-api-java')
280 self._install_vpp_pkg_centos(node, 'vpp-api-lua')
281 self._install_vpp_pkg_centos(node, 'vpp-devel')
282 self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
284 def install_vpp(self, node):
286 Install the VPP packages
288 :param node: Node dictionary with cpuinfo.
291 distro = self.get_linux_distro()
292 logging.info(" {}".format(distro[0]))
293 if distro[0] == 'Ubuntu':
294 logging.info("Install Ubuntu")
295 self._install_vpp_ubuntu(node)
296 elif distro[0] == 'CentOS Linux':
297 logging.info("Install CentOS")
298 self._install_vpp_centos(node)
300 logging.info("Install CentOS (default)")
301 self._install_vpp_centos(node)
304 def _uninstall_vpp_pkg_ubuntu(self, node, pkg):
306 Uninstall the VPP packages
308 :param node: Node dictionary
309 :param pkg: The vpp packages
313 cmd = 'dpkg --purge {}'.format(pkg)
314 (ret, stdout, stderr) = self.exec_command(cmd)
316 raise RuntimeError('{} failed on node {} {} {}'.format(
317 cmd, node['host'], stdout, stderr))
319 def _uninstall_vpp_pkg_centos(self, node, pkg):
321 Uninstall the VPP packages
323 :param node: Node dictionary
324 :param pkg: The vpp packages
328 cmd = 'yum -y remove {}'.format(pkg)
329 (ret, stdout, stderr) = self.exec_command(cmd)
331 raise RuntimeError('{} failed on node {} {} {}'.format(
332 cmd, node['host'], stdout, stderr))
334 def _uninstall_vpp_ubuntu(self, node):
336 Uninstall the VPP packages
338 :param node: Node dictionary with cpuinfo.
341 pkgs = self.get_installed_vpp_pkgs()
344 if 'version' in pkgs[0]:
345 logging.info("Uninstall Ubuntu Packages")
346 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dbg')
347 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dev')
348 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-python')
349 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-java')
350 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-lua')
351 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-plugins')
352 self._uninstall_vpp_pkg_ubuntu(node, 'vpp')
353 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-lib')
355 logging.info("Uninstall locally installed Ubuntu Packages")
357 self._uninstall_vpp_pkg_ubuntu(node, pkg['name'])
359 logging.error("There are no Ubuntu packages installed")
361 def _uninstall_vpp_centos(self, node):
363 Uninstall the VPP packages
365 :param node: Node dictionary with cpuinfo.
369 pkgs = self.get_installed_vpp_pkgs()
372 if 'version' in pkgs[0]:
373 logging.info("Uninstall CentOS Packages")
374 self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
375 self._uninstall_vpp_pkg_centos(node, 'vpp-devel')
376 self._uninstall_vpp_pkg_centos(node, 'vpp-api-python')
377 self._uninstall_vpp_pkg_centos(node, 'vpp-api-java')
378 self._uninstall_vpp_pkg_centos(node, 'vpp-api-lua')
379 self._uninstall_vpp_pkg_centos(node, 'vpp-plugins')
380 self._uninstall_vpp_pkg_centos(node, 'vpp')
381 self._uninstall_vpp_pkg_centos(node, 'vpp-lib')
382 self._uninstall_vpp_pkg_centos(node, 'vpp-selinux-policy')
384 logging.info("Uninstall locally installed CentOS Packages")
386 self._uninstall_vpp_pkg_centos(node, pkg['name'])
388 logging.error("There are no CentOS packages installed")
390 def uninstall_vpp(self, node):
392 Uninstall the VPP packages
394 :param node: Node dictionary with cpuinfo.
401 distro = self.get_linux_distro()
402 if distro[0] == 'Ubuntu':
403 logging.info("Uninstall Ubuntu")
404 self._uninstall_vpp_ubuntu(node)
405 elif distro[0] == 'CentOS Linux':
406 logging.info("Uninstall CentOS")
407 self._uninstall_vpp_centos(node)
409 logging.info("Uninstall CentOS (Default)")
410 self._uninstall_vpp_centos(node)
413 def show_vpp_settings(self, *additional_cmds):
415 Print default VPP settings. In case others are needed, can be
416 accepted as next parameters (each setting one parameter), preferably
419 :param additional_cmds: Additional commands that the vpp should print
421 :type additional_cmds: tuple
423 def_setting_tb_displayed = {
424 'IPv6 FIB': 'ip6 fib',
425 'IPv4 FIB': 'ip fib',
426 'Interface IP': 'int addr',
433 for cmd in additional_cmds:
434 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] \
437 for _, value in def_setting_tb_displayed.items():
438 self.exec_command('vppctl sh {}'.format(value))
443 Get a list of VMs that are connected to VPP interfaces
445 :param node: VPP node.
447 :returns: Dictionary containing a list of VMs and the interfaces that are connected to VPP
453 print "Need to implement get vms"
458 def get_int_ip(node):
460 Get the VPP interfaces and IP addresses
462 :param node: VPP node.
464 :returns: Dictionary containing VPP interfaces and IP addresses
468 cmd = 'vppctl show int addr'
469 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
473 lines = stdout.split('\n')
474 if len(lines[0]) is not 0:
475 if lines[0].split(' ')[0] == 'FileNotFoundError':
483 # If the first character is not whitespace
484 # create a new interface
485 if len(re.findall(r'\s', line[0])) is 0:
490 interfaces[name] = {}
491 interfaces[name]['state'] = spl[1].lstrip('(').rstrip('):\r')
493 interfaces[name]['address'] = line.lstrip(' ').rstrip('\r')
498 def get_hardware(node):
500 Get the VPP hardware information and return it in a
503 :param node: VPP node.
505 :returns: Dictionary containing VPP hardware information
510 cmd = 'vppctl show hard'
511 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
515 lines = stdout.split('\n')
516 if len(lines[0]) is not 0:
517 if lines[0].split(' ')[0] == 'FileNotFoundError':
524 # If the first character is not whitespace
525 # create a new interface
526 if len(re.findall(r'\s', line[0])) is 0:
529 interfaces[name] = {}
530 interfaces[name]['index'] = spl[1]
531 interfaces[name]['state'] = spl[2]
534 rfall = re.findall(r'Ethernet address', line)
537 interfaces[name]['mac'] = spl[2]
540 rfall = re.findall(r'carrier', line)
542 spl = line.split('carrier ')
543 interfaces[name]['carrier'] = spl[1]
546 rfall = re.findall(r'numa \d+', line)
548 spl = rfall[0].split()
549 interfaces[name]['numa'] = rfall[0].split()[1]
551 # Queues and Descriptors
552 rfall = re.findall(r'rx\: queues \d+', line)
554 interfaces[name]['rx queues'] = rfall[0].split()[2]
555 rdesc = re.findall(r'desc \d+', line)
557 interfaces[name]['rx descs'] = rdesc[0].split()[1]
559 rfall = re.findall(r'tx\: queues \d+', line)
561 interfaces[name]['tx queues'] = rfall[0].split()[2]
562 rdesc = re.findall(r'desc \d+', line)
564 interfaces[name]['tx descs'] = rdesc[0].split()[1]
568 def _get_installed_vpp_pkgs_ubuntu(self):
570 Get the VPP hardware information and return it in a
573 :returns: List of the packages installed
578 cmd = 'dpkg -l | grep vpp'
579 (ret, stdout, stderr) = self.exec_command(cmd)
583 lines = stdout.split('\n')
588 pkg = {'name': items[1], 'version': items[2]}
593 def _get_installed_vpp_pkgs_centos(self):
595 Get the VPP hardware information and return it in a
598 :returns: List of the packages installed
603 cmd = 'rpm -qa | grep vpp'
604 (ret, stdout, stderr) = self.exec_command(cmd)
608 lines = stdout.split('\n')
615 pkg = {'name': items[0]}
617 pkg = {'name': items[1], 'version': items[2]}
623 def get_installed_vpp_pkgs(self):
625 Get the VPP hardware information and return it in a
628 :returns: List of the packages installed
632 distro = self.get_linux_distro()
633 if distro[0] == 'Ubuntu':
634 pkgs = self._get_installed_vpp_pkgs_ubuntu()
635 elif distro[0] == 'CentOS Linux':
636 pkgs = self._get_installed_vpp_pkgs_centos()
638 pkgs = self._get_installed_vpp_pkgs_centos()
644 def get_interfaces_numa_node(node, *iface_keys):
645 """Get numa node on which are located most of the interfaces.
647 Return numa node with highest count of interfaces provided as arguments.
648 Return 0 if the interface does not have numa_node information available.
649 If all interfaces have unknown location (-1), then return 0.
650 If most of interfaces have unknown location (-1), but there are
651 some interfaces with known location, then return the second most
652 location of the provided interfaces.
654 :param node: Node from DICT__nodes.
655 :param iface_keys: Interface keys for lookup.
657 :type iface_keys: strings
660 for if_key in iface_keys:
662 numa_list.append(node['interfaces'][if_key].get('numa_node'))
666 numa_cnt_mc = Counter(numa_list).most_common()
667 numa_cnt_mc_len = len(numa_cnt_mc)
668 if numa_cnt_mc_len > 0 and numa_cnt_mc[0][0] != -1:
669 return numa_cnt_mc[0][0]
670 elif numa_cnt_mc_len > 1 and numa_cnt_mc[0][0] == -1:
671 return numa_cnt_mc[1][0]
679 Starts vpp for a given node
681 :param node: VPP node.
685 cmd = 'service vpp restart'
686 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
688 raise RuntimeError('{} failed on node {} {} {}'.
689 format(cmd, node['host'],
696 Starts vpp for a given node
698 :param node: VPP node.
702 cmd = 'service vpp start'
703 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
705 raise RuntimeError('{} failed on node {} {} {}'.
706 format(cmd, node['host'],
713 Stops vpp for a given node
715 :param node: VPP node.
719 cmd = 'service vpp stop'
720 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
722 logging.debug('{} failed on node {} {} {}'.
723 format(cmd, node['host'],
726 # noinspection RegExpRedundantEscape
735 :returns: status, errors
736 :rtype: tuple(str, list)
740 pkgs = vutil.get_installed_vpp_pkgs()
742 return "Not Installed", errors
744 cmd = 'service vpp status'
745 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
747 # Get the active status
748 state = re.findall(r'Active:[\w (\)]+', stdout)[0].split(' ')
750 statestr = "{} {}".format(state[1], state[2])
754 # For now we won't look for DPDK errors
755 # lines = stdout.split('\n')
757 # if 'EAL' in line or \
758 # 'FAILURE' in line or \
759 # 'failed' in line or \
761 # errors.append(line.lstrip(' '))
763 return statestr, errors
766 def get_linux_distro():
768 Get the linux distribution and check if it is supported
770 :returns: linux distro, None if the distro is not supported
774 distro = platform.linux_distribution()
775 if distro[0] == 'Ubuntu' or \
776 distro[0] == 'CentOS Linux' or \
777 distro[:7] == 'Red Hat':
780 raise RuntimeError('Linux Distribution {} is not supported'.format(distro[0]))
786 Gets VPP Version information
793 cmd = 'vppctl show version verbose'
794 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
798 lines = stdout.split('\n')
799 if len(lines[0]) is not 0:
800 if lines[0].split(' ')[0] == 'FileNotFoundError':
806 dct = line.split(':')
807 version[dct[0]] = dct[1].lstrip(' ')
812 def show_bridge(node):
814 Shows the current bridge configuration
816 :param node: VPP node.
818 :returns: A list of interfaces
822 cmd = 'vppctl show bridge'
823 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
825 raise RuntimeError('{} failed on node {} {} {}'.
826 format(cmd, node['host'],
828 lines = stdout.split('\r\n')
831 if line == 'no bridge-domains in use':
837 lspl = line.lstrip(' ').split()
838 if lspl[0] != 'BD-ID':
839 bridges.append(lspl[0])
841 for bridge in bridges:
842 cmd = 'vppctl show bridge {} detail'.format(bridge)
843 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
845 raise RuntimeError('{} failed on node {} {} {}'.
846 format(cmd, node['host'],
849 lines = stdout.split('\r\n')
851 iface = re.findall(r'[a-zA-z]+\d+/\d+/\d+', line)
853 ifcidx ={'name': iface[0], 'index': line.split()[1] }
854 ifaces.append(ifcidx)