-
Notifications
You must be signed in to change notification settings - Fork 19
/
fcoeutil.py
238 lines (191 loc) · 6.83 KB
/
fcoeutil.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
# SPDX-License-Identifier: GPL-2.0-only
import re, sys
import os.path
import constants
import util
import netutil
from util import dev_null
from xcp import logger
from disktools import *
import time
def start_lldpad():
util.runCmd2(['/sbin/lldpad', '-d'])
# Wait for lldpad to be ready
retries = 0
while True:
retries += 1
if util.runCmd2(['lldptool', '-p']) == 0:
break
if retries == 10:
raise Exception('Timed out waiting for lldpad to be ready')
time.sleep(1)
def hw_lldp_capable(intf):
return netutil.getDriver(intf) == 'bnx2x'
def start_fcoe(interfaces):
''' startFCoE takes a list of interfaces
and returns a dictonary {interface:result}
result could be either OK or error returned from fipvlan
'''
'''
modprobe sg (scsi generic)
modprobe bnx2fc if required.
'''
dcb_wait = True
result = {}
start_lldpad()
util.runCmd2(['/sbin/modprobe', 'sg'])
util.runCmd2(['/sbin/modprobe', 'libfc'])
util.runCmd2(['/sbin/modprobe', 'fcoe'])
util.runCmd2(['/sbin/modprobe', 'bnx2fc'])
for interface in interfaces:
if hw_lldp_capable(interface):
if dcb_wait:
# Wait for hardware to do dcb negotiation
dcb_wait = False
time.sleep(15)
util.runCmd2(['/sbin/lldptool', '-i', interface, '-L',
'adminStatus=disabled'])
else:
# Ideally this would use fcoemon to start FCoE but this doesn't
# fit the host-installer use case because it is possible to start
# one interface at a time.
util.runCmd2(['/sbin/dcbtool', 'sc', interface, 'dcb', 'on'])
util.runCmd2(['/sbin/dcbtool', 'sc', interface, 'app:fcoe',
'e:1'])
util.runCmd2(['/sbin/dcbtool', 'sc', interface, 'pfc',
'e:1', 'a:1', 'w:1'])
# wait for dcbtool changes to take effect
time.sleep(1)
logger.log('Starting fipvlan on %s' % interface)
rc, err = util.runCmd2(['/usr/sbin/fipvlan', '-s', '-c', interface],
with_stderr=True)
if rc != 0:
result[interface] = err
else:
result[interface] = 'OK'
logger.log(result)
# Wait for block devices to appear.
# Without being able to know how long this will take and because LUNs can
# appear before the block devices are created, just wait a constant number
# of seconds for FCoE to stabilize.
time.sleep(30)
util.runCmd2(util.udevsettleCmd())
for interface, status in result.items():
if status == 'OK':
logger.log(get_luns_on_intf(interface))
return result
def get_fcoe_capable_ifaces(check_lun):
''' Return all FCoE capable interfaces.
if checkLun is True, then this routine
will check if there are any LUNs associated
with an interface and will exclued them
from the list that is returned.
'''
start_lldpad()
dcb_nics = []
nics = netutil.scanConfiguration()
def get_dcb_capablity(interface):
''' checks if a NIC is dcb capable (in hardware or software).
If netdev for an interface has dcbnl_ops defined
then this interface is deemed dcb capable.
dcbtool gc ethX dcb will return Status = Successful if netdev
has dcbnl_ops defined.
'''
output = None
rc, output, err = util.runCmd2(['dcbtool', 'gc', interface, 'dcb'],
True, True)
if rc != 0:
return False
if output is not None:
outlist = output.split('\n')
outstr = outlist[3]
outdata = outstr.split(':')
return "Successful" in outdata[1]
for nic, conf in nics.items():
if get_dcb_capablity(nic):
if check_lun and len(get_luns_on_intf(nic)) > 0:
continue
dcb_nics.append(nic)
return dcb_nics
def get_fcoe_vlans(interface):
''' This routine return fcoe vlans associated with an interface.
returns the vlans as a list.
'''
vlans = []
rc, out, err = util.runCmd2(['fcoeadm', '-f'], True, True)
if rc != 0:
return vlans
for l in out.split('\n'):
line = l.strip()
if len(line) == 0 or ':' not in line:
continue
k, v = line.split(':', 1)
key = k.strip()
value = v.strip()
if key == 'Interface':
iface = value.split('.', 1)[0].strip()
if iface == interface:
vlans.append(value)
return vlans
lun_re = re.compile(r'(\d+)\s+(\S+)\s+(\S+ \S+)\s+(\d+)\s+(.+)')
def get_fcoe_luns():
''' returns a dictionary of fcoe luns
'''
d = {}
rc, out, err = util.runCmd2(['fcoeadm', '-t'], True, True)
if rc != 0:
return d
state = 'header'
for l in out.split('\n'):
line = l.lstrip()
if len(line) == 0 or line.startswith('--') or line.startswith('PCI') or line.startswith('No'):
continue
if state == 'header':
if ':' not in line:
# LUN banner
rport = header['OS Device Name']
if iface not in d:
d[iface] = {}
d[iface][rport] = header
state = 'luns'
continue
k, v = line.split(':', 1)
key = k.strip()
value = v.strip()
if key == 'Interface':
iface = value
header = {}
else:
header[key] = value
else:
# LUNs
m = lun_re.match(line)
if m:
if 'luns' not in d[iface][rport]:
d[iface][rport]['luns'] = {}
d[iface][rport]['luns'][m.group(1)] = {'device': m.group(2), 'capacity': m.group(3),
'bsize': m.group(4), 'description': m.group(5)}
else:
if not line.startswith('Interface:'):
# Skip LUNs which do not yet have a block device.
continue
# New header, starts with Interface:
state = 'header'
_, v = line.split(':', 1)
iface = v.strip()
header = {}
return d
def get_luns_on_intf(interface):
''' this routine get all the luns/block devices
available through interface and returns them
as a list.
'''
fcoedisks = get_fcoe_luns()
vlans = get_fcoe_vlans(interface)
lluns = []
for vlan in vlans:
if vlan in fcoedisks:
for rport, val in fcoedisks[vlan].items():
for lun in val['luns'].values():
lluns.append(lun['device'])
return lluns