-
Notifications
You must be signed in to change notification settings - Fork 9
/
ovn-integrate
executable file
·227 lines (198 loc) · 5.98 KB
/
ovn-integrate
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
#! /bin/sh
OVSLIB="/usr/share/openvswitch/scripts/ovs-lib"
if [ ! -e "${OVSLIB}" ]; then
echo "$0 needs ${OVSLIB} to work." 1>&2
exit 1
fi
if (ovs-vsctl --version) > /dev/null 2>&1; then :; else
log_failure_msg "missing ovs-vsctl, cannot proceed."
exit 1
fi
ovs_vsctl () {
ovs-vsctl --timeout=5 "$@"
}
. ${OVSLIB} || exit 1
save_ip_address() {
if [ -z "$1" ] || [ -z "$2" ]; then
return
fi
port="$1"
bridge="$2"
echo "ip addr flush dev ${port} 2>/dev/null"
ip addr show dev ${port} | while read addr; do
set -- $addr
# Check and trim family.
family=$1
shift
case $family in
inet | inet6) ;;
*) continue ;;
esac
addrcmd=
while test $# != 0; do
case $1 in
dynamic)
continue 2
;;
scope)
if test "$2" = link; then
continue 2
fi
;;
"${port}"|"${port}:"*)
shift
continue
;;
esac
addrcmd="$addrcmd $1"
shift
done
echo ip -f $family addr add $addrcmd dev ${bridge}
echo ip link set ${bridge} up
done
}
save_ip_route() {
if [ -z "$1" ] || [ -z "$2" ]; then
return
fi
port="$1"
bridge="$2"
echo "ip route flush dev ${port} proto boot 2>/dev/null" # Suppresses "Nothing to flush".
ip route show dev ${port} | while read route; do
# "proto kernel" routes are installed by the kernel automatically.
case ${route} in
*" proto kernel "*) continue ;;
esac
echo "ip route add ${route} dev ${bridge}"
done
}
nics_to_bridge() {
if (ip -V) > /dev/null 2>&1; then :; else
log_failure_msg "missing ip command, cannot proceed."
exit 1
fi
if (mktemp --version) > /dev/null 2>&1; then :; else
log_failure_msg "missing mktemp command, cannot proceed."
exit 1
fi
for iface in "$@"
do
if [ -d /sys/class/net/"${iface}" ]; then :; else
log_failure_msg "interface ${iface} does not exist, skipping."
continue
fi
if [ -e /sys/class/net/"${iface}/address" ]; then
mac_address=`cat /sys/class/net/"${iface}"/address`
else
log_failure_msg "interface ${iface} does not have a mac address."
continue
fi
OVSBR="br${iface}"
if (ovs_vsctl add-br "${OVSBR}" \
-- br-set-external-id "${OVSBR}" bridge-id "${OVSBR}"\
-- set bridge "${OVSBR}" fail-mode=standalone \
other_config:hwaddr="$mac_address" \
-- add-port "${OVSBR}" "${iface}") ; then
log_success_msg "successfully created OVS bridge ${OVSBR}"
#Move the ip address and ip route of the added port to the bridge.
#To do this, save the current configuration of the NIC interface
#as commands in a temp file and then re-execute it for the bridge.
script=`mktemp`
echo "#! /bin/sh" > ${script}
save_ip_address "${iface}" "${OVSBR}" >> ${script}
save_ip_route "${iface}" "${OVSBR}" >> ${script}
chmod +x ${script}
if (${script}) then :; else
log_failure_msg "failed to transfer IP address or routes from\
${iface} to ${OVSBR}. Do it manually."
fi
rm -f ${script}
else
log_failure_msg "failed to create OVS bridge ${OVSBR}"
fi
done
}
create_integration_bridge() {
if (ovs_vsctl -- --may-exist add-br br-int\
-- br-set-external-id br-int bridge-id br-int\
-- set bridge br-int other-config:disable-in-band=true\
-- set bridge br-int fail-mode=secure) ; then
log_success_msg "successfully created the integration bridge."
else
log_failure_msg "failed to create the integration bridge."
fi
}
set_ipam() {
if (ovs_vsctl -- set Open_vSwitch . external_ids:ipam="$1" \
external_ids:ovn-remote="tcp:$1:6640") ; then
log_success_msg "successfully set the ipam server ip address"
else
log_failure_msg "failed to set the ipam server ip address"
fi
}
set_tep() {
if (ovs_vsctl -- set Open_vSwitch . external_ids:ovn-encap-ip="$1"); then
log_success_msg "successfully set the local tunnel endpoint"
else
log_failure_msg "failed to set the local tunnel endpoint"
fi
}
set_encap_type() {
if (ovs_vsctl -- set Open_vSwitch . external_ids:ovn-encap-type="$1"); then
log_success_msg "successfully set the encap type"
else
log_failure_msg "failed to set the encap type"
fi
}
usage() {
UTIL=$(basename $0)
cat << EOF
${UTIL}: Utilities for OVN integration in a host.
usage: ${UTIL} COMMAND
Commands:
nics-to-bridge creates an OVS bridge for each of the nic interfaces
provided after this command as arguments.
ex: ${UTIL} nics-to-bridge eth0 eth1 eth2
create-integration-bridge
creates a OVS integration bridge.
set-ipam set the IP address of the ipam host
set-tep set the local tunnel-endpoint
set-encap-type set the tunnel encapsulation type
Options:
-h, --help display this help message.
EOF
exit 0
}
if [ $# -eq 0 ]; then
usage
fi
case $1 in
"nics-to-bridge")
shift
nics_to_bridge "$@"
exit 0
;;
"create-integration-bridge")
create_integration_bridge
exit 0
;;
"set-ipam")
shift
set_ipam "$1"
exit 0
;;
"set-tep")
shift
set_tep "$1"
exit 0
;;
"set-encap-type")
shift
set_encap_type "$1"
exit 0
;;
-h | --help)
usage
exit 0
;;
esac