-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlx4vf-helper
executable file
·134 lines (119 loc) · 3.98 KB
/
mlx4vf-helper
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
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2015-2016 StorPool Storage AD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#---------------------------------------------------------------------------
# This file should be placed in /sbin/
#
# It should be called after the master interface is up.
# The CentOS way is via /sbin/ifup-local by adding this line:
# [ -x /sbin/mlx4vf-helper ] && /sbin/mlx4vf-helper "$1"
#
# The debian way is to add post-up directive to master config:
# post-up /sbin/mlx4vf-helper $IFACE
#
set -e
me=${0##*/}
if [ "$1" = "" ]; then
echo "Usage: $me <interfaceName>"
echo
exit 1
fi
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
# Enable VF interface renaming
#
# Default is enabled
VFRENAME=1
# Before renamig the interface is set down.
# Uncomment the following to bring the interface back up after rename
#VF_UP_AFTER_RENAME=1
function splog()
{
logger -t "${0##*/}" "$*"
}
function createVFNAME()
{
VFNAME_TEMPLATE=${VFNAME_TEMPLATE:-_PFNAME__vf_VFID_}
VFNAME="${VFNAME_TEMPLATE//_PFNAME_/$PFNAME}"
VFNAME="${VFNAME//_VFID_/$VFID}"
VFNAME="${VFNAME//_VFIDX_/$VFIDX}"
}
function tweakVFMAC()
{
if [ -n "$SP_OURID" ]; then
for i in ${!INTERFACES[@]}; do
if [ "${INTERFACES[i]}" = "$VFNAME" ]; then
VFOLDMAC=$VFMAC
printf -v VFMAC "02:42:%02x:%02x:%02x:%02x" "$((224+i))" "$VFID" "$((SP_OURID/256))" "$((SP_OURID%256))"
break
fi
done
fi
}
if [ -f "/etc/${me}.conf" ]; then
source "/etc/${me}.conf"
fi
if [ -x /usr/sbin/storpool_confget ]; then
eval `/usr/sbin/storpool_confget -S`
fi
INTERFACES=(${SP_IFACE//,/ })
PFNAME="$1"
VFID=0
if [ -d "/sys/class/net/${PFNAME}/device/virtfn0/net" ]; then
PHYS_PORT_ID=$(</sys/class/net/${PFNAME}/phys_port_id)
for VFIDX in {0..63}; do
if [ -L "/sys/class/net/${PFNAME}/device/virtfn${VFIDX}" ]; then
if [ -d "/sys/class/net/${PFNAME}/device/virtfn${VFIDX}/net" ]; then
for phys in $(cd "/sys/class/net/${PFNAME}/device/virtfn${VFIDX}/net" && grep -l $PHYS_PORT_ID */phys_port_id 2>/dev/null); do
VFOLD="${phys%/*}"
VFMAC="$(</sys/class/net/${VFOLD}/address)"
createVFNAME
tweakVFMAC
if [ "$DEBUG" = "yes" ]; then
splog "$PFNAME>>>$PHYS_PORT_ID VFIDX:$VFIDX VFID:$VFID VFOLD:$VFOLD VFNAME:$VFNAME VFOLDMAC:$VFOLDMAC VFMAC:$VFMAC"
fi
eval "VFVLAN=\${${VFNAME}_VLAN}"
ip link set "$PFNAME" vf "$VFIDX" mac ${VFMAC}${VFVLAN:+ vlan $VFVLAN}
if [ -n "$VFRENAME" ] && [ "$VFOLD" != "$VFNAME" ]; then
ip link set "$VFOLD" down
ip link set "$VFOLD" name "${VFNAME}"
if [ "$VFOLDMAC" != "$VFMAC" ]; then
ip link set "$VFNAME" address "$VFMAC"
if [ $? -ne 0 ]; then
splog "$VFNAME MAC $VFMAC change failed! old MAC:$VFOLDMAC"
fi
fi
if [ -n "$VF_UP_AFTER_RENAME" ]; then
ip link set "${VFNAME}" up
fi
splog "$VFNAME MAC ${VFMAC}${VFVLAN:+ VLAN $VFVLAN}"
else
splog "$VFOLD MAC ${VFMAC}${VFVLAN:+ VLAN $VFVLAN}"
fi
VFID=$((VFID+1))
done
fi
else
break
fi
done
fi