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 PCI Utility libraries"""
18 from vpplib.VPPUtil import VPPUtil
20 DPDK_SCRIPT = "/vpp/vpp-config/scripts/dpdk-devbind.py"
22 # PCI Device id regular expresssion
23 PCI_DEV_ID_REGEX = '[0-9A-Fa-f]+:[0-9A-Fa-f]+:[0-9A-Fa-f]+.[0-9A-Fa-f]+'
26 class VppPCIUtil(object):
33 def _create_device_list(device_string):
35 Returns a list of PCI devices
37 :param device_string: The devices string from dpdk_devbind
38 :returns: The device list
44 ids = re.findall(PCI_DEV_ID_REGEX, device_string)
45 descriptions = re.findall(r'\'([\s\S]*?)\'', device_string)
46 unused = re.findall(r'unused=[\w,]+', device_string)
48 for i, j in enumerate(ids):
49 device = {'description': descriptions[i]}
51 device['unused'] = unused[i].split('=')[1].split(',')
53 cmd = 'ls /sys/bus/pci/devices/{}/driver/module/drivers'. \
55 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
57 device['driver'] = stdout.split(':')[1].rstrip('\n')
59 cmd = 'cat /sys/bus/pci/devices/{}/numa_node'.format(ids[i])
60 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
62 raise RuntimeError('{} failed {} {}'.
63 format(cmd, stderr, stdout))
64 numa_node = stdout.rstrip('\n')
66 device['numa_node'] = '0'
68 device['numa_node'] = numa_node
71 device['interfaces'] = []
72 cmd = 'ls /sys/bus/pci/devices/{}/net'.format(ids[i])
73 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
75 interfaces = stdout.rstrip('\n').split()
76 device['interfaces'] = interfaces
79 for intf in interfaces:
80 cmd = 'cat /sys/bus/pci/devices/{}/net/{}/address'.format(
82 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
84 raise RuntimeError('{} failed {} {}'.
85 format(cmd, stderr, stdout))
87 l2_addrs.append(stdout.rstrip('\n'))
89 device['l2addr'] = l2_addrs
91 devices[ids[i]] = device
95 def __init__(self, node):
97 self._dpdk_devices = {}
98 self._kernel_devices = {}
99 self._other_devices = {}
100 self._crypto_dpdk_devices = {}
101 self._crypto_kernel_devices = {}
102 self._crypto_other_devices = {}
103 self._link_up_devices = {}
105 def get_all_devices(self):
107 Returns a list of all the devices
112 rootdir = node['rootdir']
113 dpdk_script = rootdir + DPDK_SCRIPT
114 cmd = dpdk_script + ' --status'
115 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
117 raise RuntimeError('{} failed on node {} {}'.format(
122 # Get the network devices using the DPDK
123 # First get everything after using DPDK
124 stda = stdout.split('Network devices using DPDK-compatible driver')[1]
125 # Then get everything before using kernel driver
126 using_dpdk = stda.split('Network devices using kernel driver')[0]
127 self._dpdk_devices = self._create_device_list(using_dpdk)
129 # Get the network devices using the kernel
130 stda = stdout.split('Network devices using kernel driver')[1]
131 using_kernel = stda.split('Other network devices')[0]
132 self._kernel_devices = self._create_device_list(using_kernel)
134 # Get the other network devices
135 stda = stdout.split('Other network devices')[1]
136 other = stda.split('Crypto devices using DPDK-compatible driver')[0]
137 self._other_devices = self._create_device_list(other)
139 # Get the crypto devices using the DPDK
140 stda = stdout.split('Crypto devices using DPDK-compatible driver')[1]
141 crypto_using_dpdk = stda.split('Crypto devices using kernel driver')[0]
142 self._crypto_dpdk_devices = self._create_device_list(
145 # Get the network devices using the kernel
146 stda = stdout.split('Crypto devices using kernel driver')[1]
147 crypto_using_kernel = stda.split('Other crypto devices')[0]
148 self._crypto_kernel_devices = self._create_device_list(
151 # Get the other network devices
152 crypto_other = stdout.split('Other crypto devices')[1]
153 self._crypto_other_devices = self._create_device_list(crypto_other)
155 # Get the devices used by the kernel
156 for devk in self._kernel_devices.items():
159 for i in device['interfaces']:
160 cmd = "ip addr show " + i
161 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
163 raise RuntimeError('{} failed on node {} {}'.format(
167 lstate = re.findall(r'state \w+', stdout)[0].split(' ')[1]
169 # Take care of the links that are UP
171 device['linkup'] = True
172 self._link_up_devices[dvid] = device
174 for devl in self._link_up_devices.items():
176 del self._kernel_devices[dvid]
178 def get_dpdk_devices(self):
180 Returns a list the dpdk devices
183 return self._dpdk_devices
185 def get_kernel_devices(self):
187 Returns a list the kernel devices
190 return self._kernel_devices
192 def get_other_devices(self):
194 Returns a list the other devices
197 return self._other_devices
199 def get_crypto_dpdk_devices(self):
201 Returns a list the crypto dpdk devices
204 return self._crypto_dpdk_devices
206 def get_crypto_kernel_devices(self):
208 Returns a list the crypto kernel devices
211 return self._crypto_kernel_devices
213 def get_crypto_other_devices(self):
215 Returns a list the crypto other devices
218 return self._crypto_other_devices
220 def get_link_up_devices(self):
222 Returns a list the link up devices
225 return self._link_up_devices
228 def vpp_create_interface(interfaces, device_id, device):
230 Create an interface using the device is and device
234 name = 'port' + str(len(interfaces))
235 interfaces[name] = {}
236 interfaces[name]['pci_address'] = device_id
237 interfaces[name]['numa_node'] = device['numa_node']
238 if 'l2addr' in device:
239 l2_addrs = device['l2addr']
240 for i, j in enumerate(l2_addrs):
242 mname = 'mac_address' + str(i + 1)
243 interfaces[name][mname] = l2_addrs[i]
245 interfaces[name]['mac_address'] = l2_addrs[i]
248 def show_vpp_devices(devices, show_interfaces=True, show_header=True):
250 show the vpp devices specified in the argument
252 :param devices: A list of devices
253 :param show_interfaces: show the kernel information
254 :param show_header: Display the header if true
256 :type show_interfaces: bool
257 :type show_header: bool
261 header = "{:15} {:25} {:50}".format("PCI ID",
262 "Kernel Interface(s)",
265 header = "{:15} {:50}".format("PCI ID",
267 dashseparator = ("-" * (len(header) - 2))
269 if show_header is True:
272 for dit in devices.items():
276 interfaces = device['interfaces']
278 for i, j in enumerate(interfaces):
280 interface += ',' + interfaces[i]
282 interface = interfaces[i]
284 print "{:15} {:25} {:50}".format(
285 dvid, interface, device['description'])
287 print "{:15} {:50}".format(
288 dvid, device['description'])
291 def unbind_vpp_device(node, device_id):
293 unbind the device specified
295 :param node: Node dictionary with cpuinfo.
296 :param device_id: The device id
298 :type device_id: string
301 rootdir = node['rootdir']
302 dpdk_script = rootdir + DPDK_SCRIPT
303 cmd = dpdk_script + ' -u ' + ' ' + device_id
304 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
306 raise RuntimeError('{} failed on node {} {} {}'.format(
311 def bind_vpp_device(node, driver, device_id):
313 bind the device specified
315 :param node: Node dictionary with cpuinfo.
316 :param driver: The driver
317 :param device_id: The device id
320 :type device_id: string
323 rootdir = node['rootdir']
324 dpdk_script = rootdir + DPDK_SCRIPT
325 cmd = dpdk_script + ' -b ' + driver + ' ' + device_id
326 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
328 raise RuntimeError('{} failed on node {} {} {}'.format(
329 cmd, node['host'], stdout, stderr))