summaryrefslogtreecommitdiff
path: root/utils/iscsi_offload.sh
blob: 1869fe1ec48436cd343e467972cc34518564786e (plain)
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
#!/bin/bash
#
# iscsi_offload
#
# Configure iSCSI offload engines for use with open-iscsi
# Usage:
#    iscsi_offload [-d | -f | -i <ipaddr> | -t ] <nic>
#
# Copyright (c) 2011 Hannes Reinecke, SUSE Labs
# This script is licensed under the GPL.
#
# The script creates an open-iscsi interface definition
# in the style <nic>-<module>, where <nic> matches the
# network interface passed on the commandline.
# If '-t' (test mode) is passed as an option, the script
# will not create nor modify any setting but just print
# the currently active ones.
#
# Currently the script works with Broadcom (bnx2i) and
# Chelsio T3 (cxgbi) iSCSI offload engines.
# Should work with Chelsio T4, but has not been tested.
# ServerEngines (be2iscsi) and QLogic (qla4xxx) can only
# be configured via BIOS, open-iscsi support is still in
# development.
#

#
# Return codes:
#    0: Success
#    1: Invalid command line parameter
#    2: iSCSI offloading not supported
#    3: Error during module loading
#    4: Cannot configure interface via iscsiadm, use BIOS setup
#    5: internal error running iscsiadm
#
# Output:
#    <mac> [none|dhcp|ip <ipaddr>|ibft]
# where
#    <mac>: MAC Address of the iSCSI offload engine
#    none:  No IP configuration set for the iSCSI offload engine
#    dhcp:  iSCSI offload engine configured for DHCP
#    ip:    iSCSI offload engine configured with static IP address <ipaddr>
#    ibft:  iSCSI offload engine configured from iBFT values
#

#
# Figure out the MAC address of the iSCSI offload engine
# corresponding to a NIC from a given PCI device.
# bnx2 is using one PCI device per port for both network and iSCSI offloading
# cxgb3 is using one PCI device for everything.
#
iscsi_macaddress_from_pcidevice()
{
    local path=$1
    local if=$2
    local h
    local host

    for h in $path/host* ; do
	if [ -d "$h" ] ; then
	    host=${h##*/}
	    read netdev < /sys/class/iscsi_host/$host/netdev
	    if [ "$netdev" = "$IFNAME" ] ; then
		read mac < /sys/class/iscsi_host/$host/hwaddress
		if [ "$mac" != "00:00:00:00:00:00" ] ; then
		    echo "$mac"
		fi
		break;
	    fi
	fi
    done
}

#
# Figure out the MAC address of the iSCSI offload engine
# corresponding to a NIC from a given PCI function.
# It is assumed that the MAC address of the iSCSI offload
# engine is equal of the MAC address of the NIC plus one.
# Suitable for be2iscsi and qla4xxx
#
iscsi_macaddress_from_pcifn()
{
    local path=$1
    local if=$2
    local h
    local host
    local ifmac
    local olemacoffset=$3

    ifmac=$(ip addr show dev $if | sed -n 's/ *link\/ether \(.*\) brd.*/\1/p')
    m5=$(( 0x${ifmac##*:} ))
    m5=$(( $m5 + $olemacoffset ))
    ifmac=$(printf "%s:%02x" ${ifmac%:*} $m5)
    for host in /sys/class/iscsi_host/host* ; do
	if [ -L "$host" ] ; then
	    read mac < $host/hwaddress
	    if [ "$mac" = "$ifmac" ] ; then
		echo "$mac"
		break;
	    fi
	fi
    done
}

update_iface_setting() {
    local iface="$1"
    local name="$2"
    local value="$3"

    iface_value=$(iscsiadm -m iface -I $iface | sed -n "s/$name = \(.*\)/\1/p")
    if [ "$iface_value" = "<empty>" ] ; then
	iface_value=
    fi
    if [ "$iface_value" != "$value" ] ; then
	if ! iscsiadm -m iface -I $iface -o update -n "$name" -v "$value" ; then
	    return 1
	fi
    fi
    return 0
}

while getopts di:t options ; do
    case $options in
	d ) mode=dhcp;;
	i ) mode=static
	    optaddr=$OPTARG
	    ;;
	f ) mode=firmware;;
	t ) dry_run=1;;
	?)  printf "Usage: %s [-d|-t|-i ipaddr|-f] ifname\n" $0
	    exit 1;;
    esac
done
shift $(($OPTIND - 1))

IFNAME=$1
ibft_mode="none"

if [ -z "$IFNAME" ] ; then
    echo "No interface specified"
    exit 1
fi

if [ "$dry_run" ] ; then
    if [ "$mode" = "dhcp" ] ; then
	echo "'-t' specified, ignoring '-d'"
	mode=
    elif [ "$mode" = "static" ] ; then
	echo "'-t' specified, ignoring '-s'"
	mode=
    fi
fi

if [ ! -L /sys/class/net/$IFNAME ] ; then
    echo "Interface $IFNAME not found"
    exit 1
fi

if [ "$optaddr" ] && ! ip route get $optaddr ; then
    echo "Invalid IP address $optaddr"
    exit 1
fi
if [ "$dry_run" ] ; then
    mode=
fi


ifpath=$(cd -P /sys/class/net/$IFNAME; echo $PWD)
pcipath=$(cd -P $ifpath/device; echo $PWD)

if [ -d $pcipath ] ; then
    drvlink=$(readlink $pcipath/driver)
    driver=${drvlink##*/}
fi

if [ -z "$driver" ] ; then
    echo "No driver found for interface $IFNAME"
    exit 1
fi

case "$driver" in
    bnx2*)
	mod=bnx2i
	;;
    cxgb*)
	mod=cxgb3i
	;;
    be2*)
	mod=be2iscsi
	;;
    qla*)
	mod=qla4xxx
	;;
    qed*)
	mod=qedi
	;;
esac

if [ -z "$mod" ] ; then
    echo "iSCSI offloading not supported on interface $IFNAME"
    exit 2
fi

# Check if the required modules are already loaded
loaded=$(sed -n "/^$mod/p" /proc/modules)
if [ -z "$loaded" ] ; then
    modprobe $mod
fi

loaded=$(sed -n "/^$mod/p" /proc/modules)
if [ -z "$loaded" ] ; then
    echo "Loading of $mod.ko failed, please check dmesg"
    exit 3
fi

# Get the correct MAC address for the various devices
if [ "$mod" = "bnx2i" ] ; then
    mac=$(iscsi_macaddress_from_pcidevice $pcipath $IFNAME)
elif [ "$mod" = "cxgb3i" ] ; then
    mac=$(iscsi_macaddress_from_pcidevice $pcipath $IFNAME)
elif [ "$mod" = "be2iscsi" ] ; then
    mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 1)
elif [ "$mod" = "qla4xxx" ] ; then
    mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 1)
elif [ "$mod" = "qede" -o "$mod" = "qedi" ] ; then
    mac=$(iscsi_macaddress_from_pcifn $pcipath $IFNAME 4)
fi

if [ -z "$mac" ] ; then
    echo "iSCSI offloading not supported on interface $IFNAME"
    exit 2
fi

gen_iface="$mod.$mac"
ioe_iface="${IFNAME}-${mod}"

# Get existing settings
if iscsiadm -m iface -I $ioe_iface > /dev/null 2>&1 ; then
    ioe_mac=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.hwaddress = \(.*\)/\1/p")
    ioe_mod=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.transport_name = \(.*\)/\1/p")
    ipaddr=$(iscsiadm -m iface -I $ioe_iface 2> /dev/null| sed -n "s/iface\.ipaddress = \(.*\)/\1/p")
    if [ "$ipaddr" == "<empty>" ] ; then
	ipaddr=
    fi
elif [ "$mod" = "be2iscsi" ] ; then
    ioe_mac=$mac
    ioe_mod=$mod
else
    # Create new interface
    iscsiadm -m iface -I $ioe_iface --op=new 2> /dev/null
    ioe_mac=
    ioe_mod=
    ipaddr=
fi

if [ -z "$dry_run" ] ; then
    if [ "$ioe_mac" != "$mac" ] ; then
	if [ -n "$ioe_mac" ] ; then
	    echo "Warning: Updating MAC address on iface $ioe_iface"
	fi
	update_iface_setting $ioe_iface iface.hwaddress "$mac"
    fi

    if [ "$ioe_mod" != "$mod" ] ; then
	if [ -n "$ioe_mod" ] ; then
	    echo "Warning: Update transport on iface $ioe_iface"
	fi
	update_iface_setting $ioe_iface iface.transport_name "$mod"
    fi
elif [ -z "$ipaddr" ] ; then
    ipaddr=$(iscsiadm -m iface -I $gen_iface 2> /dev/null| sed -n "s/iface\.ipaddress = \(.*\)/\1/p")
    if [ "$ipaddr" = "<empty>" ] ; then
	ipaddr=
    fi
elif [ "$ioe_mod" != "$mod" ] ; then
    echo "Warning: Transport mismatch on iface $ioe_iface: $ioe_mod should be $mod"
fi

# Check iBFT setting
for d in /sys/firmware/* ; do
    [ -d $d ] || continue
    [ -d $d/ethernet0 ] || continue
    iboot_dir=$d
done
if [ -n "$iboot_dir" ] && [ -d "$iboot_dir" ] ; then
    for if in ${iboot_dir}/ethernet* ; do
	read ibft_mac < $if/mac
	[ "$ibft_mac" = "$mac" ] || continue
	ibft_origin=0
	[ -f ${if}/origin ] && read ibft_origin < $if/origin
	if [ "$ibft_origin" -eq 1 ] ; then
	    ibft_mode="static"
	elif [ "$ibft_origin" -eq 3 ] ; then
	    ibft_mode="dhcp"
	fi
	[ -f $if/dhcp ] && read ibft_dhcp < $if/dhcp
	if [ -n "$ibft_dhcp" -a "$ibft_mode" != "dhcp" ] ; then
	    ibft_mode=dhcp
	fi
	if [ "$ibft_mode" = "dhcp" ] ; then
	    ibft_ipaddr="0.0.0.0"
	    ibft_gateway=
	    ibft_mask=
	    break
	fi
	[ -f $if/ip-addr ] && read ibft_ipaddr < $if/ip-addr
	[ -f $if/gateway ] && read ibft_gateway < $if/gateway
	[ -f $if/subnet-mask ] && read ibft_mask < $if/subnet-mask
	break
    done
fi

if [ -z "$optaddr" ] && [ "$ibft_ipaddr" ] ; then
    optaddr=$ibft_ipaddr
fi

# Check if the interface needs to be configured
if [ -z "$mode" ] ; then
    if [ "$ibft_mode" != "none" ] ; then
	echo "$mac ibft"
	mode="ibft"
    elif [ -z "$ipaddr" ] ; then
	echo "$mac none"
	mode="none"
    elif [ "$ipaddr" = "0.0.0.0" ] ; then
	echo "$mac dhcp"
	ipaddr=
	mode="dhcp"
    else
	echo "$mac ip $ipaddr"
	mode="static"
    fi
    [ "$dry_run" ] && exit 0
elif [ "$mode" = "dhcp" ] ; then
    if [ "$ipaddr" = "0.0.0.0" ] ; then
	echo "$mac dhcp"
	exit 0
    fi
    optaddr="0.0.0.0"
elif [ "$mode" = "static" ] && [ "$ipaddr" = "$optaddr" ] ; then
    echo "$mac ip $ipaddr"
    exit 0
fi

if [ "$mod" = "be2iscsi" ] ; then
    exit 4
fi

if ! update_iface_setting $ioe_iface iface.ipaddress "$optaddr" ; then
    echo "Failed to set IP address: $?"
    exit 1
fi
if ! update_iface_setting $gen_iface iface.ipaddress "$optaddr" ; then
    echo "Failed to set IP address for generic interface: $?"
    exit 1
fi

if ! update_iface_setting $ioe_iface iface.gateway "$ibft_gateway" ; then
    echo "Failed to set gateway address: $?"
    exit 1
fi

if ! update_iface_setting $gen_iface iface.gateway "$ibft_gateway" ; then
    echo "Failed to set gateway address for generic interface: $?"
    exit 1
fi

if ! update_iface_setting $ioe_iface iface.subnet_mask "$ibft_mask" ; then
    echo "Failed to set subnet mask: $?"
    exit 1
fi

if ! update_iface_setting $gen_iface iface.subnet_mask "$ibft_mask" ; then
    echo "Failed to set subnet mask for generic interface: $?"
    exit 1
fi

if [ "$mod" = "qla4xxx" ] ; then
    iscsiadm -m iface -H $mac -o applyall
fi
ip link set dev $IFNAME up

exit 0