/
common.py
382 lines (311 loc) · 11.5 KB
/
common.py
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
# -*- coding: utf-8 -*-
# Copyright 2014 Red Hat, Inc.
#
# 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.
#
# Common functions and variables meant to be shared across various modules
# As opposed to utils, this is meant to be imported from anywhere. We can't
# import anything from os_net_config here.
import logging
import logging.handlers
import os
from oslo_concurrency import processutils
import sys
import yaml
# File to contain the DPDK mapped nics, as nic name will not be available after
# binding driver, which is required for correct nic numbering.
# Format of the file (list mapped nic's details):
# -
# name: eth1
# pci_address: 0000:02:00.0
# mac_address: 01:02:03:04:05:06
# driver: vfio-pci
DPDK_MAPPING_FILE = '/var/lib/os-net-config/dpdk_mapping.yaml'
# File to contain the list of SR-IOV PF, VF and their configurations
# Format of the file shall be
# - device_type: pf
# name: <pf name>
# numvfs: <number of VFs>
# promisc: "on"/"off"
# - device_type: vf
# device:
# name: <pf name>
# vfid: <VF id>
# name: <vf name>
# vlan_id: <vlan>
# qos: <qos>
# spoofcheck: "on"/"off"
# trust: "on"/"off"
# state: "auto"/"enable"/"disable"
# macaddr: <mac address>
# promisc: "on"/"off"
SRIOV_CONFIG_FILE = '/var/lib/os-net-config/sriov_config.yaml'
# File to contain the list of DCB configurations
# Format of the file shall be
# - name: <pf name>
# dscp2prio:
# - protocol: 44
# selector: 5
# priority: 6
# - protocol: 42
# selector: 5
# priority: 3
DCB_CONFIG_FILE = '/var/lib/os-net-config/dcb_config.yaml'
_SYS_BUS_PCI_DEV = '/sys/bus/pci/devices'
SYS_CLASS_NET = '/sys/class/net'
_LOG_FILE = '/var/log/os-net-config.log'
MLNX_VENDOR_ID = "0x15b3"
logger = logging.getLogger(__name__)
class OvsDpdkBindException(ValueError):
pass
def configure_logger(log_file=False, verbose=False, debug=False):
LOG_FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)s '
'%(name)s.%(funcName)s %(message)s')
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
logger = logging.getLogger("os_net_config")
logger.handlers.clear()
logger_level(logger, verbose, debug)
logger.propagate = True
formatter = logging.Formatter(fmt=LOG_FORMAT, datefmt=DATE_FORMAT)
if log_file:
file_handler = logging.handlers.RotatingFileHandler(
_LOG_FILE, maxBytes=10485760, backupCount=7
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
def logger_level(logger, verbose=False, debug=False):
log_level = logging.WARN
if debug:
log_level = logging.DEBUG
elif verbose:
log_level = logging.INFO
logger.setLevel(log_level)
def get_dev_path(ifname, path=None):
if not path:
path = ""
elif path.startswith("_"):
path = path[1:]
else:
path = f"device/{path}"
return os.path.join(SYS_CLASS_NET, ifname, path)
def get_pci_dev_path(pci_address, path=None):
if not path:
path = ""
elif path.startswith("_"):
path = path[1:]
return os.path.join(_SYS_BUS_PCI_DEV, pci_address, path)
def get_vendor_id(ifname):
try:
with open(get_dev_path(ifname, "vendor"), 'r') as f:
out = f.read().strip()
return out
except IOError:
return
def get_device_id(ifname):
try:
with open(get_dev_path(ifname, 'device'), 'r') as f:
out = f.read().strip()
return out
except IOError:
return
def get_file_data(filename):
if not os.path.exists(filename):
return ''
try:
with open(filename, 'r') as f:
return f.read()
except IOError:
logger.error(f"Error reading file: {filename}")
return ''
def write_yaml_config(filepath, data):
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w') as f:
yaml.safe_dump(data, f, default_flow_style=False)
def update_dcb_map(ifname, pci_addr, driver, noop, dscp2prio=None):
if not noop:
dcb_map = get_dcb_config_map()
for item in dcb_map:
if item['pci_addr'] == pci_addr:
item['name'] = ifname
item['driver'] = driver
item['dscp2prio'] = dscp2prio
break
else:
new_item = {}
new_item['pci_addr'] = pci_addr
new_item['driver'] = driver
new_item['name'] = ifname
new_item['dscp2prio'] = dscp2prio
dcb_map.append(new_item)
write_yaml_config(DCB_CONFIG_FILE, dcb_map)
def write_dcb_map(dcb_map):
write_yaml_config(DCB_CONFIG_FILE, dcb_map)
def get_dcb_config_map():
contents = get_file_data(DCB_CONFIG_FILE)
dcb_config_map = yaml.safe_load(contents) if contents else []
return dcb_config_map
def get_empty_dcb_map():
contents = get_file_data(DCB_CONFIG_FILE)
dcb_config_map = yaml.safe_load(contents) if contents else []
for entry in dcb_config_map:
entry['dscp2prio'] = []
return dcb_config_map
def add_dcb_entry(dcb_config_map, data):
for entry in dcb_config_map:
if entry['pci_addr'] == data.pci_addr:
entry['dscp2prio'] = data.dscp2prio
entry['name'] = data.name
entry['driver'] = data.driver
break
else:
new_entry = {}
new_entry['name'] = data.name
new_entry['pci_addr'] = data.pci_addr
new_entry['driver'] = data.driver
new_entry['dscp2prio'] = data.dscp2prio
dcb_config_map.append(new_entry)
return dcb_config_map
def reset_dcb_map():
dcb_map = get_empty_dcb_map()
if dcb_map != []:
write_dcb_map(dcb_map)
def get_sriov_map(pf_name=None):
contents = get_file_data(SRIOV_CONFIG_FILE)
sriov_map = yaml.safe_load(contents) if contents else []
if len(sriov_map) and pf_name:
return [pf for pf in sriov_map if pf['name'] == pf_name]
return sriov_map
def get_dpdk_map():
contents = get_file_data(DPDK_MAPPING_FILE)
dpdk_map = yaml.safe_load(contents) if contents else []
return dpdk_map
def _get_dpdk_mac_address(name):
contents = get_file_data(DPDK_MAPPING_FILE)
dpdk_map = yaml.safe_load(contents) if contents else []
for item in dpdk_map:
if item['name'] == name:
return item['mac_address']
def interface_mac(name):
try: # If the iface is part of a Linux bond, the real MAC is only here.
with open(get_dev_path(name, 'bonding_slave/perm_hwaddr'),
'r') as f:
return f.read().rstrip()
except IOError:
pass # Iface is not part of a bond, continue
try:
with open(get_dev_path(name, '_address'), 'r') as f:
return f.read().rstrip()
except IOError:
# If the interface is bound to a DPDK driver, get the mac address from
# the DPDK mapping file as /sys files will be removed after binding.
dpdk_mac_address = _get_dpdk_mac_address(name)
if dpdk_mac_address:
return dpdk_mac_address
logger.error("Unable to read mac address: %s" % name)
raise
def get_interface_driver_by_pci_address(pci_address):
try:
uevent = get_pci_dev_path(pci_address, 'uevent')
with open(uevent, 'r') as f:
out = f.read().strip()
for line in out.split('\n'):
if 'DRIVER' in line:
driver = line.split('=')
if len(driver) == 2:
return driver[1]
except IOError:
return
def is_mellanox_interface(ifname):
vendor_id = get_vendor_id(ifname)
return vendor_id == MLNX_VENDOR_ID
def is_vf(pci_address):
# If DPDK drivers are bound on a VF, then the path common.SYS_CLASS_NET
# wouldn't exist. Instead we look for the path
# /sys/bus/pci/devices/<PCI addr>/physfn to understand if the device
# is actually a VF. This path could be used by VFs not bound with
# DPDK drivers as well
vf_path_check = _SYS_BUS_PCI_DEV + '/%s/physfn' % pci_address
is_sriov_vf = os.path.isdir(vf_path_check)
return is_sriov_vf
def is_vf_by_name(interface_name, check_mapping_file=False):
vf_path_check = get_dev_path(interface_name, 'physfn')
is_sriov_vf = os.path.isdir(vf_path_check)
if not is_sriov_vf and check_mapping_file:
sriov_map = get_sriov_map()
for item in sriov_map:
if (item['name'] == interface_name and
item['device_type'] == 'vf'):
is_sriov_vf = True
return is_sriov_vf
def set_driverctl_override(pci_address, driver):
if driver is None:
logger.info(f"Driver override is not required for device"
"{pci_address}")
return False
iface_driver = get_interface_driver_by_pci_address(pci_address)
if iface_driver == driver:
logger.info(f"Driver {driver} is already bound to the device"
"{pci_address}")
return False
try:
if is_vf(pci_address):
out, err = processutils.execute('driverctl', '--nosave',
'set-override', pci_address,
driver)
else:
out, err = processutils.execute('driverctl', 'set-override',
pci_address, driver)
if err:
msg = f"Failed to bind dpdk interface {pci_address} err - {err}"
raise OvsDpdkBindException(msg)
except processutils.ProcessExecutionError:
msg = f"Failed to bind interface {pci_address} with dpdk"
raise OvsDpdkBindException(msg)
return err
def list_kmods(mods: list) -> list:
"""Listing Kernel Modules
Checks in currently loaded modules for a list
of modules and returns the ones that are not loaded
"""
try:
stdout, stderr = processutils.execute('lsmod')
except processutils.ProcessExecutionError as exc:
logger.error(f"Failed to get lsmod: {exc}")
raise
modules = set([line.split()[0] for line in stdout.strip().split('\n')])
return list(set(mods) - set(modules))
def load_kmods(mods: list):
"""Loading Kernel Modules
Loads modules from list that are not already loaded
"""
needed = list_kmods(mods)
for mod in needed:
try:
stdout, stderr = processutils.execute('modprobe', mod)
except processutils.ProcessExecutionError as exc:
logger.error(f"Failed to modprobe {mod}: {exc}")
raise
def restorecon(path: str):
"""Executes restorecon on a path"""
logger.info(f"Restoring selinux context on {path}")
try:
stdout, stderr = processutils.execute('restorecon', '-R', '-F', '-v',
path)
except processutils.ProcessExecutionError as exc:
logger.error(f"Failed to restorecon on {path}: {exc}")
raise
logger.debug(f"Restorecon completed: {stdout}")