aboutsummaryrefslogtreecommitdiffstats
path: root/qemu
blob: c1b0206fd592daf9155c9b260a02391281f65fb8 (plain) (blame)
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
#!/bin/sh

# routed subnet qemu hook for libvirt (L2 over L3)
#
# https://2e8.dk/libvirt-routed-subnet

# Example XML:
# <domain>
#   <metadata>
#     <routed-subnet xmlns="https://2e8.dk/libvirt-routed-subnet">
#       <interface mac="xx:xx:xx:xx:xx:xx" mtu="9000">
#         <gateway address="192.0.2.0/24"/>
#         <host address="192.0.2.42"/>
#       </interface>
#     </routed-subnet>
#   </metadata>
# </domain>

set -e

routingxml=""
fullxml=""
device=""
dry_run=0

IP="$(which ip)"
ip(){
	if [ $dry_run -eq 0 ] ; then
		"$IP" "$@"
	else
		echo ip "$@"
	fi
}

SYSCTL="/usr/sbin/sysctl"
sysctl(){
	if [ $dry_run -eq 0 ] ; then
		"$SYSCTL" "$@"
	else
		echo sysctl "$@"
	fi
}

xpath(){
	echo "$2" | xmllint --xpath "$1" - 2>/dev/null
}

get_address_family(){ [ -z "${1##*:*}" ] && echo ipv6 || echo ipv4; }

get_config(){
	local configfile="$1"
	local ns="https://2e8.dk/libvirt-routed-subnet"
	fullxml="$(cat "$configfile")"
	routingxml="$(xpath "//*[local-name()='routed-subnet' and namespace-uri()='$ns']" "$fullxml")"
}

get_device(){
	xpath "string(//interface[@type = 'ethernet']/mac[@address='$1']/../target/@dev)" "$fullxml"
}

get_element(){
	xpath "//*[local-name()='$1'][$2]" "$3"
}

get_attr(){
	xpath "string(*/@$1)" "$2"
}

node_loop(){
	local handler="$1"
	shift
	local i=1
	local ret=1
	while [ $i -lt 1000 ]; do
		$handler $i "$@" || return $ret
		i=$(( $i + 1 ))
		ret=0
	done
	return 2
}

process_gateway(){
	local i=$1
	local ifacexml="$2"
	local nodexml="$(get_element 'gateway' "$i" "$ifacexml")"
	if [ "$nodexml" = "" ] ; then
		return 1
	fi

	if [ $dry_run -ne 0 ] ; then
		echo "$nodexml"
	fi

	local address="$(get_attr 'address' "$nodexml")"

	ip addr add "$address" dev "$device"

	return 0
}

process_host(){
	local i=$1
	local ifacexml="$2"
	local nodexml="$(get_element 'host' "$i" "$ifacexml")"
	if [ "$nodexml" = "" ] ; then
		return 1
	fi

	if [ $dry_run -ne 0 ] ; then
		echo "$nodexml"
	fi

	local address="$(get_attr 'address' "$nodexml")"
	local family="$(get_address_family "$address")"

	if [ "$family" = "ipv4" ] ; then
		ip route add proto static "$address/32" dev "$device"
	fi
	return 0
}

process_route(){
	local i=$1
	local ifacexml="$2"
	local nodexml="$(get_element 'route' "$i" "$ifacexml")"
	if [ "$nodexml" = "" ] ; then
		return 1
	fi

	if [ $dry_run -ne 0 ] ; then
		echo "$nodexml"
	fi

	local prefix="$(get_attr 'prefix' "$nodexml")"
	local nexthop="$(get_attr 'nexthop' "$nodexml")"

	ip route add proto static "$prefix" via "$nexthop"
	return 0
}

process_iface(){
	local i=$1
	local ifacexml="$(get_element 'interface' "$1" "$routingxml")"
	if [ "$ifacexml" = "" ] ; then
		return 1
	fi

	local mac="$(xpath "string(/interface/@mac)" "$ifacexml")"
	if [ "$mac" = "" ] ; then
		return 1
	fi

	device="$(get_device "$mac")"
	if [ "$device" = "" ] ; then
		return 1
	fi

	local mtu="$(xpath "string(/interface/@mtu)" "$ifacexml")"
	if [ "$mtu" != "" ] ; then
		ip link set mtu "$mtu" dev "$device"
	fi

	prepare_device
	node_loop process_gateway "$ifacexml" || true
	node_loop process_host "$ifacexml" || true
	node_loop process_route "$ifacexml" || true

	return 0
}

prepare_device(){
	# IPv4
	sysctl -q -w "net.ipv4.conf.$device.proxy_arp=1"
	sysctl -q -w "net.ipv4.conf.$device.rp_filter=1"
	sysctl -q -w "net.ipv4.conf.$device.forwarding=1"

	# IPv6
	sysctl -q -w "net.ipv6.conf.$device.forwarding=1"
	sysctl -q -w "net.ipv6.conf.$device.accept_ra=0"

	# link up
	ip link set up dev "$device"
}

start_network(){
	node_loop process_iface || true
}

usage(){
	echo "Usage: $0 <guest_name> start begin <xmlfile>"
	echo "config: https://2e8.dk/libvirt-routed-subnet"
	echo "libvirt docs: https://www.libvirt.org/hooks.html"
}

test_main(){
	dry_run=1
	main foobar start begin test-in.xml
}

main(){
	if [ $# -eq 1 ] && [ "$1" = 'test' ] ; then
		test_main
		exit $?
	elif [ $# -ne 4 ] ; then
		echo 'too few arguments' >&2
		usage >&2
		exit 1
	fi
	local guest="$1"
	local event="$2"
	local evtype="$3"
	local configfile="$4"
	local state="${event}_${evtype}"
	get_config "$configfile"

	case "$state" in
	start_begin)
		start_network "$@"
		;;
	esac
}

main "$@"