aboutsummaryrefslogtreecommitdiffstats
path: root/contrail-agent/scripts/create-vrouter.sh
blob: cd3828f228a6aa7fa799487db5fceb6f662106d8 (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
#!/bin/sh -e
#
# Script used to configure vRouter interface

ARG_BRIDGE=b
ARG_HELP=h
OPTS=:${ARG_BRIDGE}${ARG_HELP}
USAGE="\
create-vrouter [-${ARG_BRIDGE}${ARG_HELP}] [interface]
Options:
  -$ARG_BRIDGE  remove bridge from interface if exists
  -$ARG_HELP  print this message"

configVRouter()
{
	cat juju-header
	if [ -s "$2" ]; then
		printf "\n%s\n" "auto $1"
		cat "$2"
	elif [ ! -e "$2" ]; then
		printf "\n%s\n%s\n" "auto $1" "iface $1 inet manual"
	fi
	printf "\n%s\n"	"auto vhost0"
	if [ -e "$3" ]; then
		cat "$3"
	else
		echo "iface vhost0 inet dhcp"
	fi
	cat <<-EOF
		    pre-up ip link add vhost0 address \$(cat /sys/class/net/$1/address) type vhost
		    pre-up vif --add $1 --mac \$(cat /sys/class/net/$1/address) --vrf 0 --vhost-phys --type physical
		    pre-up vif --add vhost0 --mac \$(cat /sys/class/net/$1/address) --vrf 0 --type vhost --xconnect $1
		    post-down vif --list | awk '/^vif.*OS: vhost0/ {split(\$1, arr, "\\/"); print arr[2];}' | xargs vif --delete
		    post-down vif --list | awk '/^vif.*OS: $1/ {split(\$1, arr, "\\/"); print arr[2];}' | xargs vif --delete
		    post-down ip link delete vhost0
		EOF
}

configureInterfaces()
{
	for cfg in /etc/network/interfaces /etc/network/interfaces.d/*.cfg \
	    /etc/network/*.config; do
		# for each network interfaces config, extract the config for
		# the chosen interface whilst commenting it out in the
		# subsequent replacement config
		[ -e "$cfg" ] || continue
		awk -v interface=$1 -v interface_cfg=$TMP/interface.cfg \
		    -v vrouter_cfg=$TMP/vrouter.cfg -f vrouter-interfaces.awk \
		    "$cfg" > $TMP/interfaces.cfg
		if ! diff $TMP/interfaces.cfg "$cfg" > /dev/null; then
			# create backup
			mv "$cfg" "$cfg.save"
			# substitute replacement config for original config
			{ cat juju-header; echo; cat $TMP/interfaces.cfg; } > "$cfg"
		fi
	done
	if [ -e $TMP/interface.cfg ]; then
		# strip whitespace
		sed -En -e '1h;1!H;${g;s/[[:space:]]+$//;p}' -i \
		    $TMP/interface.cfg
	fi
}

configureInterfacesDir()
{
	# add interfaces.d source line to /etc/network/interfaces
	if ! grep -q '^[[:blank:]]*source /etc/network/interfaces\.d/\*\.cfg[[:blank:]]*$' \
	    /etc/network/interfaces; then
		printf "\n%s\n" "source /etc/network/interfaces.d/*.cfg" \
		    >> /etc/network/interfaces
		# it's possible for conflicting network config to exist in
		# /etc/network/interfaces.d when we start sourcing it
		# so disable any config as a precautionary measure
		for cfg in /etc/network/interfaces.d/*.cfg; do
			[ -e "$cfg" ] || continue
			mv "$cfg" "$cfg.old"
		done
	fi
	mkdir -p /etc/network/interfaces.d
}

configureVRouter()
{
	if [ $# = 1 ]; then
		iface_down=$1
		iface_delete=$1
		iface_up=$1
		iface_cfg=$TMP/interface.cfg
	else
		iface_down="$1 $2"
		iface_delete=$2
		iface_up=$1
		iface_cfg=/dev/null
	fi
	ifacedown $iface_down vhost0; sleep 5
	configureInterfacesDir
	configureInterfaces $iface_delete
	configVRouter $iface_up $iface_cfg $TMP/vrouter.cfg \
	    > /etc/network/interfaces.d/vrouter.cfg
	ifaceup $iface_up vhost0
	restoreRoutes
}

ifacebridge()
{
	for cfg in /etc/network/interfaces /etc/network/interfaces.d/*.cfg \
	    /etc/network/*.config; do
		# extract all the bridges with interface as port
		# and all interfaces marked auto
		[ -e "$cfg" ] || continue
		awk -v interface=$1 \
		    -v auto_interfaces=$TMP/auto_interfaces \
		    -v bridge_interfaces=$TMP/bridge_interfaces \
		    -f bridges.awk "$cfg"
	done
	if [ -e $TMP/bridge_interfaces ]; then
		# output the bridge marked auto
		grep -m 1 -f $TMP/bridge_interfaces $TMP/auto_interfaces
	fi
}

ifacedown()
{
	for iface; do
		# ifdown interface
		# if bridge, save list of interfaces
		# if bond, save list of slaves
		if [ ! -e /sys/class/net/$iface ]; then
			continue
		fi
		[ -d /sys/class/net/$iface/bridge ] && saveIfaces $iface
		[ -d /sys/class/net/$iface/bonding ] && saveSlaves $iface
		ifdown -v --force $iface
	done
}

ifaceup()
{
	for iface; do
		# ifup interface
		# if bridge, restore list of interfaces
		# restore list of slaves if exists (bond)
		restoreSlaves $iface
		ifup -v $iface
		[ -d /sys/class/net/$iface/bridge ] && restoreIfaces $iface
	done
	return 0
}

restoreRoutes()
{
	if [ -e /etc/network/routes ]; then
		service networking-routes stop
		service networking-routes start
	fi
}

restoreIfaces()
{
	if [ -e $TMP/$1.ifaces ]; then
		cat $TMP/$1.ifaces | xargs -n 1 brctl addif $1 || true
	fi
}

restoreSlaves()
{
	if [ -e $TMP/$1.slaves ]; then
		cat $TMP/$1.slaves | xargs ifup
	fi
}

saveIfaces()
{
	if [ -z "$(find /sys/class/net/$1/brif -maxdepth 0 -empty)" ]; then
		find /sys/class/net/$1/brif | tail -n +2 | xargs -n 1 basename \
		    > $TMP/$1.ifaces
	fi
}

saveSlaves()
{
	if [ -s /sys/class/net/$1/bonding/slaves ]; then
		cat /sys/class/net/$1/bonding/slaves | tr " " "\n" \
		    > $TMP/$1.slaves
	fi
}

usage()
{
	if [ $# -gt 0 ]; then
		fd=$1
	else
		fd=1
	fi
	echo "$USAGE" >&$fd
}

usageError()
{
	echo "$1" >&2
	usage 2
	exit 1
}

while getopts $OPTS opt; do
	case $opt in
	$ARG_BRIDGE)
		remove_bridge=true
		;;
	$ARG_HELP)
		usage
		exit 0
		;;
	"?")
		usageError "Unknown argument: $OPTARG"
		;;
	esac
done
shift $(($OPTIND - 1))

if [ $# -gt 1 ]; then
	usageError "Too many arguments"
fi

TMP=$(mktemp -d /tmp/create-vrouter.XXX)

if [ $# -ne 0 ]; then
	bridge=$(ifacebridge $1)
	if [ -n "$bridge" ]; then
		if [ -n "$remove_bridge" ]; then
			configureVRouter $1 $bridge
		else
			configureVRouter $bridge
		fi
	else
		configureVRouter $1
	fi
else
	# use default gateway interface
	gateway=$(route -n | awk '$1 == "0.0.0.0" { print $8 }')
	if [ -d /sys/class/net/$gateway/bridge ] \
	    && [ -z "$(find /sys/class/net/$gateway/brif -maxdepth 0 -empty)" ] \
	    && [ -n "$remove_bridge" ]; then
		interface=$(find /sys/class/net/$gateway/brif | sed -n -e '2p' | xargs basename)
		configureVRouter $interface $gateway
	else
		configureVRouter $gateway
	fi
fi

rm -rf $TMP