1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
"""VPP util library"""
import logging
import re
import subprocess
import platform
import requests
from collections import Counter
ubuntu_pkgs = {'release': ['vpp', 'vpp-plugins', 'vpp-api-java', 'vpp-api-lua', 'vpp-api-python',
'vpp-dbg', 'vpp-dev'],
'master': ['vpp', 'vpp-plugin-core', 'vpp-ext-deps', 'vpp-api-python',
'vpp-dbg', 'vpp-dev', 'vpp-plugin-dpdk']}
centos_pkgs = {'release': ['vpp', 'vpp-plugins', 'vpp-api-java', 'vpp-api-lua',
'vpp-api-python', 'vpp-debuginfo', 'vpp-devel', 'libvpp0'],
'master': ['vpp', 'vpp-plugins', 'vpp-ext-deps', 'vpp-api-java', 'vpp-api-lua',
'vpp-api-python', 'vpp-debuginfo', 'vpp-devel', 'libvpp0']}
class VPPUtil(object):
"""General class for any VPP related methods/functions."""
@staticmethod
def exec_command(cmd, timeout=None):
"""Execute a command on the local node.
:param cmd: Command to run locally.
:param timeout: Timeout value
:type cmd: str
:type timeout: int
:return return_code, stdout, stderr
:rtype: tuple(int, str, str)
"""
logging.info(" Local Command: {}".format(cmd))
out = ''
err = ''
prc = subprocess.Popen(cmd, shell=True, bufsize=1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
with prc.stdout:
lines = prc.stdout.readlines()
for line in lines:
if type(line) != str:
line = line.decode()
logging.info(" {}".format(line.strip('\n')))
out += line
with prc.stderr:
lines = prc.stderr.readlines()
for line in lines:
if type(line) != str:
line = line.decode()
logging.warning(" {}".format(line.strip('\n')))
err += line
ret = prc.wait()
return ret, out, err
def _autoconfig_backup_file(self, filename):
"""
Create a backup file.
:param filename: The file to backup
:type filename: str
"""
# Does a copy of the file exist, if not create one
ofile = filename + '.orig'
(ret, stdout, stderr) = self.exec_command('ls {}'.format(ofile))
if ret != 0:
logging.debug(stderr)
if stdout.strip('\n') != ofile:
cmd = 'sudo cp {} {}'.format(filename, ofile)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug(stderr)
def _install_vpp_ubuntu(self, node, branch, ubuntu_version='xenial'):
"""
Install the VPP packages
:param node: Node dictionary with cpuinfo.
:param branch: VPP branch
:param ubuntu_version: Ubuntu Version
:type node: dict
:type branch: string
:type ubuntu_version: string
"""
# Modify the sources list
sfile = '/etc/apt/sources.list.d/99fd.io.list'
# Backup the sources list
self._autoconfig_backup_file(sfile)
reps = 'deb [trusted=yes] https://packagecloud.io/fdio/'
reps += '{}/ubuntu {} main\n'.format(branch, ubuntu_version)
with open(sfile, 'w') as sfd:
sfd.write(reps)
sfd.close()
# Add the key
key = requests.get(
'https://packagecloud.io/fdio/{}/gpgkey'.format(branch))
cmd = 'echo "{}" | apt-key add -'.format(key.content.decode(key.encoding))
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
# Install the package
cmd = 'apt-get -y update'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} apt-get update failed on node {} {}'.format(
cmd,
node['host'],
stderr))
# Get the package list
pkgstr = ''
for ps in ubuntu_pkgs[branch]:
pkgstr += ps + ' '
cmd = 'apt-get -y install {}'.format(pkgstr)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.format(
cmd, node['host'], stdout, stderr))
def _install_vpp_centos(self, node, branch):
"""
Install the VPP packages
:param node: Node dictionary with cpuinfo.
:param branch: The branch name release or master
:type node: dict
:type branch: string
"""
# Be sure the correct system packages are installed
cmd = 'yum -y update'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
cmd = 'yum -y install pygpgme yum-utils'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
# Modify the sources list
sfile = '/etc/yum.repos.d/fdio-release.repo'
# Backup the sources list
self._autoconfig_backup_file(sfile)
# Remove the current file
cmd = 'rm {}'.format(sfile)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
# Get the file contents
reps = '\n'.join([
'[fdio_{}]'.format(branch),
'name=fdio_{}'.format(branch),
'baseurl=https://packagecloud.io/fdio/{}/el/7/$basearch'.format(
branch),
'repo_gpgcheck=1',
'gpgcheck=0',
'enabled=1',
'gpgkey=https://packagecloud.io/fdio/{}/gpgkey'.format(branch),
'sslverify=1',
'sslcacert=/etc/pki/tls/certs/ca-bundle.crt',
'metadata_expire=300\n',
'[fdio_{}-source]'.format(branch),
'name=fdio_release-{}'.format(branch),
'baseurl=https://packagecloud.io/fdio/{}/el/7/SRPMS'.format(
branch),
'repo_gpgcheck=1',
'gpgcheck=0',
'enabled=1',
'gpgkey=https://packagecloud.io/fdio/{}/gpgkey'.format(branch),
'sslverify =1',
'sslcacert=/etc/pki/tls/certs/ca-bundle.crt',
'metadata_expire=300\n'
])
with open(sfile, 'w') as sfd:
sfd.write(reps)
sfd.close()
# Update the fdio repo
cmd = 'yum clean all'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
cmd = "yum -q makecache -y --disablerepo='*' " \
"--enablerepo='fdio_{}'".format(branch)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {}'.format(
cmd,
node['host'],
stderr))
# Get the package list
pkgstr = ''
for ps in centos_pkgs[branch]:
pkgstr += ps + ' '
cmd = 'yum -y install {}'.format(pkgstr)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.format(
cmd, node['host'], stdout, stderr))
def install_vpp(self, node, branch):
"""
Install the VPP packages
:param node: Node dictionary with cpuinfo.
:param branch: The branch name
:type node: dict
:type branch: string
"""
distro = self.get_linux_distro()
logging.info(" {}".format(distro[0]))
if distro[0] == 'Ubuntu':
logging.info("Install Ubuntu")
self._install_vpp_ubuntu(node, branch, ubuntu_version=distro[2])
elif distro[0] == 'CentOS Linux':
logging.info("Install CentOS")
self._install_vpp_centos(node, branch)
else:
logging.info("Install CentOS (default)")
self._install_vpp_centos(node, branch)
return
def _uninstall_vpp_ubuntu(self, node):
"""
Uninstall the VPP packages
:param node: Node dictionary with cpuinfo.
:type node: dict
"""
# get the package list
pkgstr = ''
pkgs = self.get_installed_vpp_pkgs()
for pkg in pkgs:
pkgname = pkg['name']
pkgstr += pkgname + ' '
cmd = 'dpkg --purge {}'.format(pkgstr)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.format(
cmd, node['host'], stdout, stderr))
def _uninstall_vpp_centos(self, node):
"""
Uninstall the VPP packages
:param node: Node dictionary with cpuinfo.
:type node: dict
"""
pkgstr = ''
pkgs = self.get_installed_vpp_pkgs()
for pkg in pkgs:
pkgname = pkg['name']
pkgstr += pkgname + ' '
logging.info("Uninstalling {}".format(pkgstr))
cmd = 'yum -y remove {}'.format(pkgstr)
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.format(
cmd, node['host'], stdout, stderr))
def uninstall_vpp(self, node):
"""
Uninstall the VPP packages
:param node: Node dictionary with cpuinfo.
:type node: dict
"""
# First stop VPP
self.stop(node)
distro = self.get_linux_distro()
if distro[0] == 'Ubuntu':
logging.info("Uninstall Ubuntu")
self._uninstall_vpp_ubuntu(node)
elif distro[0] == 'CentOS Linux':
logging.info("Uninstall CentOS")
self._uninstall_vpp_centos(node)
else:
logging.info("Uninstall CentOS (Default)")
self._uninstall_vpp_centos(node)
return
def show_vpp_settings(self, *additional_cmds):
"""
Print default VPP settings. In case others are needed, can be
accepted as next parameters (each setting one parameter), preferably
in form of a string.
:param additional_cmds: Additional commands that the vpp should print
settings for.
:type additional_cmds: tuple
"""
def_setting_tb_displayed = {
'IPv6 FIB': 'ip6 fib',
'IPv4 FIB': 'ip fib',
'Interface IP': 'int addr',
'Interfaces': 'int',
'ARP': 'ip arp',
'Errors': 'err'
}
if additional_cmds:
for cmd in additional_cmds:
def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] \
= cmd
for _, value in def_setting_tb_displayed.items():
self.exec_command('vppctl sh {}'.format(value))
@staticmethod
def get_vms(node):
"""
Get a list of VMs that are connected to VPP interfaces
:param node: VPP node.
:type node: dict
:returns: Dictionary containing a list of VMs and the interfaces
that are connected to VPP
:rtype: dictionary
"""
vmdict = {}
print("Need to implement get vms")
return vmdict
@staticmethod
def get_int_ip(node):
"""
Get the VPP interfaces and IP addresses
:param node: VPP node.
:type node: dict
:returns: Dictionary containing VPP interfaces and IP addresses
:rtype: dictionary
"""
interfaces = {}
cmd = 'vppctl show int addr'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
return interfaces
lines = stdout.split('\n')
if len(lines[0]) is not 0:
if lines[0].split(' ')[0] == 'FileNotFoundError':
return interfaces
name = ''
for line in lines:
if len(line) is 0:
continue
# If the first character is not whitespace
# create a new interface
if len(re.findall(r'\s', line[0])) is 0:
spl = line.split()
name = spl[0]
if name == 'local0':
continue
interfaces[name] = {}
interfaces[name]['state'] = spl[1].lstrip('(').rstrip('):\r')
else:
interfaces[name]['address'] = line.lstrip(' ').rstrip('\r')
return interfaces
@staticmethod
def get_hardware(node):
"""
Get the VPP hardware information and return it in a
dictionary
:param node: VPP node.
:type node: dict
:returns: Dictionary containing VPP hardware information
:rtype: dictionary
"""
interfaces = {}
cmd = 'vppctl show hard'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
return interfaces
lines = stdout.split('\n')
if len(lines[0]) is not 0:
if lines[0].split(' ')[0] == 'FileNotFoundError':
return interfaces
for line in lines:
if len(line) is 0:
continue
# If the first character is not whitespace
# create a new interface
if len(re.findall(r'\s', line[0])) is 0:
spl = line.split()
name = spl[0]
interfaces[name] = {}
interfaces[name]['index'] = spl[1]
interfaces[name]['state'] = spl[2]
# Ethernet address
rfall = re.findall(r'Ethernet address', line)
if rfall:
spl = line.split()
interfaces[name]['mac'] = spl[2]
# Carrier
rfall = re.findall(r'carrier', line)
if rfall:
spl = line.split('carrier ')
interfaces[name]['carrier'] = spl[1]
# Socket
spl = ''
rfall = re.findall(r'numa \d+', line)
if rfall:
spl = rfall[0].split()
interfaces[name]['numa'] = rfall[0].split()[1]
# Queues and Descriptors
rfall = re.findall(r'rx\: queues \d+', line)
if rfall:
interfaces[name]['rx queues'] = rfall[0].split()[2]
rdesc = re.findall(r'desc \d+', line)
if rdesc:
interfaces[name]['rx descs'] = rdesc[0].split()[1]
rfall = re.findall(r'tx\: queues \d+', line)
if rfall:
interfaces[name]['tx queues'] = rfall[0].split()[2]
rdesc = re.findall(r'desc \d+', line)
if rdesc:
interfaces[name]['tx descs'] = rdesc[0].split()[1]
return interfaces
def _get_installed_vpp_pkgs_ubuntu(self):
"""
Get the VPP hardware information and return it in a
dictionary
:returns: List of the packages installed
:rtype: list
"""
pkgs = []
cmd = 'dpkg -l | grep vpp'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
return pkgs
lines = stdout.split('\n')
for line in lines:
items = line.split()
if len(items) < 2:
continue
pkg = {'name': items[1], 'version': items[2]}
pkgs.append(pkg)
return pkgs
def _get_installed_vpp_pkgs_centos(self):
"""
Get the VPP hardware information and return it in a
dictionary
:returns: List of the packages installed
:rtype: list
"""
pkgs = []
cmd = 'rpm -qa | grep vpp'
(ret, stdout, stderr) = self.exec_command(cmd)
if ret != 0:
return pkgs
lines = stdout.split('\n')
for line in lines:
if len(line) == 0:
continue
items = line.split()
if len(items) < 2:
pkg = {'name': items[0]}
else:
pkg = {'name': items[1], 'version': items[2]}
pkgs.append(pkg)
return pkgs
def get_installed_vpp_pkgs(self):
"""
Get the VPP hardware information and return it in a
dictionary
:returns: List of the packages installed
:rtype: list
"""
distro = self.get_linux_distro()
if distro[0] == 'Ubuntu':
pkgs = self._get_installed_vpp_pkgs_ubuntu()
elif distro[0] == 'CentOS Linux':
pkgs = self._get_installed_vpp_pkgs_centos()
else:
pkgs = self._get_installed_vpp_pkgs_centos()
return []
return pkgs
@staticmethod
def get_interfaces_numa_node(node, *iface_keys):
"""Get numa node on which are located most of the interfaces.
Return numa node with highest count of interfaces provided as
arguments.
Return 0 if the interface does not have numa_node information
available.
If all interfaces have unknown location (-1), then return 0.
If most of interfaces have unknown location (-1), but there are
some interfaces with known location, then return the second most
location of the provided interfaces.
:param node: Node from DICT__nodes.
:param iface_keys: Interface keys for lookup.
:type node: dict
:type iface_keys: strings
"""
numa_list = []
for if_key in iface_keys:
try:
numa_list.append(node['interfaces'][if_key].get('numa_node'))
except KeyError:
pass
numa_cnt_mc = Counter(numa_list).most_common()
numa_cnt_mc_len = len(numa_cnt_mc)
if numa_cnt_mc_len > 0 and numa_cnt_mc[0][0] != -1:
return numa_cnt_mc[0][0]
elif numa_cnt_mc_len > 1 and numa_cnt_mc[0][0] == -1:
return numa_cnt_mc[1][0]
return 0
@staticmethod
def restart(node):
"""
Starts vpp for a given node
:param node: VPP node.
:type node: dict
"""
cmd = 'service vpp restart'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.
format(cmd, node['host'],
stdout, stderr))
@staticmethod
def start(node):
"""
Starts vpp for a given node
:param node: VPP node.
:type node: dict
"""
cmd = 'service vpp start'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.
format(cmd, node['host'],
stdout, stderr))
@staticmethod
def stop(node):
"""
Stops vpp for a given node
:param node: VPP node.
:type node: dict
"""
cmd = 'service vpp stop'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
logging.debug('{} failed on node {} {} {}'.
format(cmd, node['host'],
stdout, stderr))
# noinspection RegExpRedundantEscape
@staticmethod
def status(node):
"""
Gets VPP status
:param: node
:type node: dict
:returns: status, errors
:rtype: tuple(str, list)
"""
errors = []
vutil = VPPUtil()
pkgs = vutil.get_installed_vpp_pkgs()
if len(pkgs) == 0:
return "Not Installed", errors
cmd = 'service vpp status'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
# Get the active status
state = re.findall(r'Active:[\w (\)]+', stdout)[0].split(' ')
if len(state) > 2:
statestr = "{} {}".format(state[1], state[2])
else:
statestr = "Invalid"
# For now we won't look for DPDK errors
# lines = stdout.split('\n')
# for line in lines:
# if 'EAL' in line or \
# 'FAILURE' in line or \
# 'failed' in line or \
# 'Failed' in line:
# errors.append(line.lstrip(' '))
return statestr, errors
@staticmethod
def get_linux_distro():
"""
Get the linux distribution and check if it is supported
:returns: linux distro, None if the distro is not supported
:rtype: list
"""
distro = platform.linux_distribution()
if distro[0] == 'Ubuntu' or \
distro[0] == 'CentOS Linux' or \
distro[:7] == 'Red Hat':
return distro
else:
raise RuntimeError(
'Linux Distribution {} is not supported'.format(distro[0]))
@staticmethod
def version():
"""
Gets VPP Version information
:returns: version
:rtype: dict
"""
version = {}
cmd = 'vppctl show version verbose'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
return version
lines = stdout.split('\n')
if len(lines[0]) is not 0:
if lines[0].split(' ')[0] == 'FileNotFoundError':
return version
for line in lines:
if len(line) is 0:
continue
dct = line.split(':')
version[dct[0]] = dct[1].lstrip(' ')
return version
@staticmethod
def show_bridge(node):
"""
Shows the current bridge configuration
:param node: VPP node.
:type node: dict
:returns: A list of interfaces
"""
ifaces = []
cmd = 'vppctl show bridge'
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.
format(cmd, node['host'],
stdout, stderr))
lines = stdout.split('\r\n')
bridges = []
for line in lines:
if line == 'no bridge-domains in use':
print(line)
return ifaces
if len(line) == 0:
continue
lspl = line.lstrip(' ').split()
if lspl[0] != 'BD-ID':
bridges.append(lspl[0])
for bridge in bridges:
cmd = 'vppctl show bridge {} detail'.format(bridge)
(ret, stdout, stderr) = VPPUtil.exec_command(cmd)
if ret != 0:
raise RuntimeError('{} failed on node {} {} {}'.
format(cmd, node['host'],
stdout, stderr))
lines = stdout.split('\r\n')
for line in lines:
iface = re.findall(r'[a-zA-z]+\d+/\d+/\d+', line)
if len(iface):
ifcidx = {'name': iface[0], 'index': line.split()[1]}
ifaces.append(ifcidx)
print(stdout)
return ifaces
|