Merge lp:~saviq/charms/trusty/neutron-contrail/config-vhost-interface into lp:~sdn-charmers/charms/trusty/neutron-contrail/trunk

Proposed by Michał Sawicz on 2015-06-26
Status: Merged
Merged at revision: 28
Proposed branch: lp:~saviq/charms/trusty/neutron-contrail/config-vhost-interface
Merge into: lp:~sdn-charmers/charms/trusty/neutron-contrail/trunk
Diff against target: 312 lines (+178/-31)
7 files modified
config.yaml (+14/-0)
hooks/neutron_contrail_hooks.py (+12/-0)
hooks/neutron_contrail_utils.py (+11/-2)
scripts/br-interfaces.awk (+35/-0)
scripts/create-vrouter.sh (+93/-28)
scripts/vhost-phys.awk (+12/-0)
scripts/vhost-phys.sh (+1/-1)
To merge this branch: bzr merge lp:~saviq/charms/trusty/neutron-contrail/config-vhost-interface
Reviewer Review Type Date Requested Status
Robert Ayres (community) 2015-06-26 Approve on 2015-07-10
Review via email: mp+263129@code.launchpad.net

Commit message

Add fold-juju-bridge and optional vhost-interface config options to deal with more complex network configurations

To post a comment you must log in.
45. By Michał Sawicz on 2015-06-29

Clarify the warning

46. By Michał Sawicz on 2015-06-30

Do not back up our own configs

47. By Michał Sawicz on 2015-06-30

Fix config var name

48. By Michał Sawicz on 2015-06-30

Change ifdown order, otherwise vif tries to delete interfaces that are already down

49. By Michał Sawicz on 2015-06-30

Fix vhost-phys.sh script

50. By Michał Sawicz on 2015-06-30

Take down vhost0, agent and toggle module before configuring vrouter

Otherwise `vif --add` has no effect and networking is gone

51. By Michał Sawicz on 2015-07-01

Merge trunk

52. By Michał Sawicz on 2015-07-01

Need to start the service in case the physical interface does not change

Robert Ayres (robert-ayres) wrote :

Merged with some reworkings.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2015-06-02 12:50:23 +0000
3+++ config.yaml 2015-07-01 11:55:34 +0000
4@@ -35,3 +35,17 @@
5 discovery-server-ip:
6 type: string
7 description: Specify discovery server ip manually
8+ vhost-interface:
9+ type: string
10+ description: |
11+ Specify the interface to install vhost0 on. If left empty, vhost0 will
12+ be installed on the default gateway interface.
13+ fold-juju-bridge:
14+ type: boolean
15+ default: False
16+ description: |
17+ Juju on MAAS creates a juju-br0 bridge for deploying LXC and KVM
18+ workloads. Enable this to remove this bridge if you want to install
19+ vhost0 directly on the underlying interface. WARNING: this will
20+ break current and future juju-deployed LXC or KVM workloads on all
21+ machines where this is set to True.
22
23=== modified file 'hooks/neutron_contrail_hooks.py'
24--- hooks/neutron_contrail_hooks.py 2015-05-21 16:05:07 +0000
25+++ hooks/neutron_contrail_hooks.py 2015-07-01 11:55:34 +0000
26@@ -14,6 +14,8 @@
27
28 from charmhelpers.core.host import (
29 restart_on_change,
30+ service_stop,
31+ service_start,
32 service_restart
33 )
34
35@@ -32,6 +34,7 @@
36 ifdown,
37 ifup,
38 modprobe,
39+ rmmod,
40 provision_vrouter,
41 units,
42 unprovision_vrouter,
43@@ -59,8 +62,17 @@
44
45 @hooks.hook("config-changed")
46 def config_changed():
47+ # Need to take down vhost0, the agent and toggle the module
48+ # in case we change physical interfaces, otherwise vif --add
49+ # does not work
50+ ifdown(["vhost0"])
51+ service_stop("contrail-vrouter-agent")
52+ rmmod("vrouter")
53+ modprobe("vrouter")
54+ configure_vrouter()
55 configure_virtual_gateways()
56 write_config()
57+ service_start("contrail-vrouter-agent")
58 if not units("contrail-discovery") and not units("control-node"):
59 config["control-node-ready"] = True if config.get("discovery-server-ip") \
60 else False
61
62=== modified file 'hooks/neutron_contrail_utils.py'
63--- hooks/neutron_contrail_utils.py 2015-06-30 18:42:39 +0000
64+++ hooks/neutron_contrail_utils.py 2015-07-01 11:55:34 +0000
65@@ -28,7 +28,12 @@
66 return 8082
67
68 def configure_vrouter():
69- check_call(["./create-vrouter.sh"], cwd="scripts")
70+ args = ["./create-vrouter.sh"]
71+ if config.get("fold-juju-bridge", False):
72+ args.append("-j")
73+ if config.get("vhost-interface", ""):
74+ args.append(config["vhost-interface"])
75+ check_call(args, cwd="scripts")
76
77 def contrail_api_ctx():
78 ip = config.get("contrail-api-ip")
79@@ -117,6 +122,10 @@
80 modules.write(module)
81 modules.write("\n")
82
83+def rmmod(module):
84+ log("Unloading kernel module {}".format(module))
85+ check_call(["rmmod", module])
86+
87 def neutron_metadata_ctx():
88 ctxs = [ { "metadata_secret": relation_get("shared-secret", unit, rid) }
89 for rid in relation_ids("neutron-metadata")
90@@ -208,7 +217,7 @@
91 return ip + "/" + str(cidr)
92
93 def vhost_phys():
94- return check_output(["scripts/vhost-phys.sh"]).rstrip()
95+ return check_output(["./vhost-phys.sh"], cwd="scripts").rstrip()
96
97 def vrouter_ctx():
98 return { "hostname": vrouter_hostname(),
99
100=== added file 'scripts/br-interfaces.awk'
101--- scripts/br-interfaces.awk 1970-01-01 00:00:00 +0000
102+++ scripts/br-interfaces.awk 2015-07-01 11:55:34 +0000
103@@ -0,0 +1,35 @@
104+function strip(s)
105+{
106+ sub(/^[[:blank:]]+/, "", s)
107+ sub(/[[:blank:]]+$/, "", s)
108+ return s
109+}
110+
111+/^[[:blank:]]*(iface|mapping|auto|allow-[^ ]+|source) / {
112+ iface = 0
113+}
114+
115+$0 ~ "^[[:blank:]]*(iface|auto) (" interface "|juju-br0)" {
116+ if ($1 == "auto" && $2 == "juju-br0") {
117+ print "auto", interface > interface_head
118+ }
119+ if ($1 == "iface") {
120+ iface = 1
121+ if ($2 == "juju-br0") {
122+ print "iface", interface, $3, $4 > interface_head
123+ }
124+ }
125+
126+ print "#" $0
127+ next
128+}
129+
130+iface == 1 {
131+ if (!match($0, "^[[:blank:]]*(bridge_|#|$)")) {
132+ print " " strip($0) > interface_cfg
133+ }
134+ print "#" $0
135+ next
136+}
137+
138+{ print $0 }
139
140=== modified file 'scripts/create-vrouter.sh'
141--- scripts/create-vrouter.sh 2014-12-03 16:51:47 +0000
142+++ scripts/create-vrouter.sh 2015-07-01 11:55:34 +0000
143@@ -1,5 +1,12 @@
144 #!/bin/sh -e
145
146+configJujuBr()
147+{
148+ cat juju-header
149+ cat "$1"
150+ cat "$2" 2> /dev/null || true
151+}
152+
153 configVRouter()
154 {
155 cat juju-header
156@@ -39,43 +46,101 @@
157
158 restoreIfaces()
159 {
160- cat $TMP/$1.ifaces | xargs -n 1 brctl addif $1 || true
161+ [ -e $TMP/$1.ifaces ] && cat $TMP/$1.ifaces | xargs -n 1 brctl addif $1 || true
162 }
163
164 saveIfaces()
165 {
166- find /sys/class/net/$1/brif | tail -n +2 | xargs -n 1 basename \
167- > $TMP/$1.ifaces
168-}
169-
170+ find /sys/class/net/$1/brif | tail -n +2 | xargs -n 1 basename > $TMP/$1.ifaces
171+}
172+
173+mangleConfigs()
174+{
175+ for cfg in /etc/network/interfaces /etc/network/interfaces.d/*.cfg \
176+ /etc/network/*.config; do
177+ [ -e "$cfg" ] || continue
178+ awk $@ "$cfg" > $TMP/interfaces.cfg
179+ if ! diff $TMP/interfaces.cfg "$cfg" > /dev/null; then
180+ if [ "$cfg" != "$JUJUBR_CFG" -a "$cfg" != "$VROUTER_CFG" ]; then
181+ [ -e "$cfg.save" ] || mv "$cfg" "$cfg.save"
182+ fi
183+ cat juju-header $TMP/interfaces.cfg > "$cfg"
184+ fi
185+ done
186+}
187+
188+restoreConfigs()
189+{
190+ rm $JUJUBR_CFG $VROUTER_CFG 2> /dev/null || true
191+ for file in `find /etc/network -name '*.save'`
192+ do
193+ mv "$file" "${file%.save}"
194+ done
195+}
196+
197+JUJUBR_CFG=/etc/network/interfaces.d/jujubr.cfg
198+VROUTER_CFG=/etc/network/interfaces.d/vrouter.cfg
199 TMP=$(mktemp -d /tmp/create-vrouter.XXX)
200
201-if [ $# -ne 0 ]; then
202- interface=$1
203-else
204- interface=$(route -n | awk '$1 == "0.0.0.0" { print $8 }')
205-fi
206-
207-ifacedown $interface vhost0; sleep 5
208+# determine base juju-br0 and vhost0 interfaces
209+vhost_interface=$(grep -e "iface.*inet manual" $VROUTER_CFG | awk '{ print $2 }') 2> /dev/null || true
210+br_interface=$(ls /sys/class/net/juju-br0/brif | head -n1) 2> /dev/null || true
211+
212+# restore original configs
213+ifacedown juju-br0 $br_interface $vhost_interface; sleep 5
214+restoreConfigs
215+ifaceup $br_interface $vhost_interface juju-br0; sleep 5
216+
217+# determine restored juju-br0 interface
218+br_interface=$(ls /sys/class/net/juju-br0/brif | head -n1) 2> /dev/null || true
219+
220+# ensure interfaces.d support
221 if ! grep -q '^[[:blank:]]*source /etc/network/interfaces\.d/\*\.cfg[[:blank:]]*$' \
222- /etc/network/interfaces; then
223+ /etc/network/interfaces; then
224+ [ -e /etc/network/interfaces.save ] || cp /etc/network/interfaces /etc/network/interfaces.save
225 printf "\n%s\n" "source /etc/network/interfaces.d/*.cfg" \
226- >> /etc/network/interfaces
227+ >> /etc/network/interfaces
228 fi
229 mkdir -p /etc/network/interfaces.d
230-for cfg in /etc/network/interfaces /etc/network/interfaces.d/*.cfg \
231- /etc/network/*.config; do
232- [ -e "$cfg" ] || continue
233- awk -v interface=$interface -v interface_cfg=$TMP/interface.cfg \
234- -v vrouter_cfg=$TMP/vrouter.cfg -f vrouter-interfaces.awk "$cfg" \
235- > $TMP/interfaces.cfg
236- if ! diff $TMP/interfaces.cfg "$cfg" > /dev/null; then
237- mv "$cfg" "$cfg.save"
238- cat juju-header $TMP/interfaces.cfg > "$cfg"
239+
240+# drop juju bridge
241+if [ "$1" = "-j" ]; then
242+ shift
243+ if [ ! -e "/sys/class/net/juju-br0" ]; then
244+ echo "WARNING: did not find juju-br0, ignoring \"-j\""
245+ else
246+ ifacedown juju-br0 $br_interface
247+ mangleConfigs -v interface=$br_interface -v interface_cfg=$TMP/interface.cfg \
248+ -v interface_head=$TMP/interface.head -f br-interfaces.awk
249+ configJujuBr $TMP/interface.head $TMP/interface.cfg \
250+ > $JUJUBR_CFG
251+ ifaceup $br_interface
252 fi
253-done
254-configVRouter $interface $TMP/interface.cfg $TMP/vrouter.cfg \
255- > /etc/network/interfaces.d/vrouter.cfg
256-ifaceup $interface vhost0
257-
258+ rm -f $TMP/*
259+fi
260+
261+# determine/validate vhost interface
262+if [ -n "$1" ]; then
263+ interface=$1
264+else
265+ interface=$(route -n | awk '$1 == "0.0.0.0" { print $8 }')
266+fi
267+
268+if [ -z "$interface" -o ! -e "/sys/class/net/$interface" ]; then
269+ echo "WARNING: invalid interface for vhost0: \"$interface\"," >&2
270+ echo "skipping vhost0 configuration" >&2
271+ interface=""
272+fi
273+
274+# configure vhost0
275+if [ -n "$interface" ]; then
276+ ifacedown vhost0 $interface $vhost_interface; sleep 5
277+ mangleConfigs -v interface=$interface -v interface_cfg=$TMP/interface.cfg \
278+ -v vrouter_cfg=$TMP/vrouter.cfg -f vrouter-interfaces.awk
279+
280+ configVRouter $interface $TMP/interface.cfg $TMP/vrouter.cfg \
281+ > $VROUTER_CFG
282+ ifaceup $interface $vhost_interface vhost0
283+ rm -f $TMP/*
284+fi
285 rm -rf $TMP
286
287=== added file 'scripts/vhost-phys.awk'
288--- scripts/vhost-phys.awk 1970-01-01 00:00:00 +0000
289+++ scripts/vhost-phys.awk 2015-07-01 11:55:34 +0000
290@@ -0,0 +1,12 @@
291+/^vif/ {
292+ if ($3 != "vhost0") {
293+ iface = $3
294+ }
295+ next
296+}
297+
298+$2 == "HWaddr:" mac {
299+ print iface
300+ exit
301+}
302+
303
304=== modified file 'scripts/vhost-phys.sh'
305--- scripts/vhost-phys.sh 2014-10-31 18:30:28 +0000
306+++ scripts/vhost-phys.sh 2015-07-01 11:55:34 +0000
307@@ -1,4 +1,4 @@
308 #!/bin/sh -e
309
310 mac=$(cat /sys/class/net/vhost0/address)
311-vif --list | awk -v mac=$mac 'BEGIN { RS="\n\n" }; $3 != "vhost0" && $5 == "HWaddr:" mac { print $3; exit 0 }'
312+vif --list | awk -v mac=$mac -f vhost-phys.awk

Subscribers

People subscribed via source and target branches