root / contrib / ganeti-hooks / kvm-vif-bridge @ 84bee898
History | View | Annotate | Download (2.8 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
# This is an example of a Ganeti kvm ifup script that configures network |
4 |
# interfaces based on the initial deployment of the Okeanos project |
5 |
|
6 |
TAP_CONSTANT_MAC=cc:47:52:4e:45:54 # GRNET in hex :-) |
7 |
MAC2EUI64=/etc/ganeti/mac2eui64.py |
8 |
NFDHCPD_STATE_DIR=/var/lib/nfdhcpd |
9 |
|
10 |
function routed_setup_ipv4 { |
11 |
# get the link's default gateway |
12 |
gw=$(ip route list table $LINK | sed -n 's/default via \([^ ]\+\).*/\1/p' | head -1) |
13 |
|
14 |
# mangle ARPs to come from the gw's IP |
15 |
arptables -D OUTPUT -o $INTERFACE --opcode request -j mangle >/dev/null 2>&1 |
16 |
arptables -A OUTPUT -o $INTERFACE --opcode request -j mangle --mangle-ip-s "$gw" |
17 |
|
18 |
# route interface to the proper routing table |
19 |
while ip rule del dev $INTERFACE; do :; done |
20 |
ip rule add dev $INTERFACE table $LINK |
21 |
|
22 |
# static route mapping IP -> INTERFACE |
23 |
ip route replace $IP table $LINK proto static dev $INTERFACE |
24 |
|
25 |
# Enable proxy ARP |
26 |
echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp |
27 |
} |
28 |
|
29 |
function routed_setup_ipv6 { |
30 |
# Add a routing entry for the eui-64 |
31 |
prefix=$(ip -6 route list table $LINK | awk '/\/64/ {print $1; exit}') |
32 |
uplink=$(ip -6 route list table $LINK | sed -n 's/default via .* dev \([^ ]\+\).*/\1/p' | head -1) |
33 |
eui64=$($MAC2EUI64 $MAC $prefix) |
34 |
|
35 |
while ip -6 rule del dev $INTERFACE; do :; done |
36 |
ip -6 rule add dev $INTERFACE table $LINK |
37 |
ip -6 ro replace $eui64/128 dev $INTERFACE table $LINK |
38 |
ip -6 neigh add proxy $eui64 dev $uplink |
39 |
|
40 |
# disable proxy NDP since we're handling this on userspace |
41 |
# this should be the default, but better safe than sorry |
42 |
echo 0 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp |
43 |
} |
44 |
|
45 |
# pick a firewall profile per NIC, based on tags (and apply it) |
46 |
function routed_setup_firewall { |
47 |
ifprefix="synnefo:network:$INTERFACE_INDEX:" |
48 |
for tag in $TAGS; do |
49 |
case ${tag#$ifprefix} in |
50 |
protected:1) |
51 |
chain=PROTECTED-1 |
52 |
;; |
53 |
protected:2) |
54 |
chain=PROTECTED-2 |
55 |
;; |
56 |
esac |
57 |
done |
58 |
|
59 |
iptables -D FORWARD -o $INTERFACE -j $chain 2>/dev/null |
60 |
ip6tables -D FORWARD -o $INTERFACE -j $chain 2>/dev/null |
61 |
if [ "x$chain" != "x" ]; then |
62 |
iptables -A FORWARD -o $INTERFACE -j $chain |
63 |
ip6tables -A FORWARD -o $INTERFACE -j $chain |
64 |
fi |
65 |
} |
66 |
|
67 |
function routed_setup_nfdhcpd { |
68 |
umask 022 |
69 |
cat >$NFDHCPD_STATE_DIR/$INTERFACE <<EOF |
70 |
IP=$IP |
71 |
MAC=$MAC |
72 |
LINK=$LINK |
73 |
HOSTNAME=$INSTANCE |
74 |
TAGS="$TAGS" |
75 |
EOF |
76 |
} |
77 |
|
78 |
if [ "$MODE" = "routed" ]; then |
79 |
# special proxy-ARP/NDP routing mode |
80 |
|
81 |
# use a constant predefined MAC address for the tap |
82 |
ip link set $INTERFACE addr $TAP_CONSTANT_MAC |
83 |
# bring the tap up |
84 |
ifconfig $INTERFACE 0.0.0.0 up |
85 |
|
86 |
# Drop unicast BOOTP/DHCP packets |
87 |
iptables -D FORWARD -i $INTERFACE -p udp --dport 67 -j DROP 2>/dev/null |
88 |
iptables -A FORWARD -i $INTERFACE -p udp --dport 67 -j DROP |
89 |
|
90 |
routed_setup_ipv4 |
91 |
routed_setup_ipv6 |
92 |
routed_setup_firewall |
93 |
routed_setup_nfdhcpd |
94 |
elif [ "$MODE" = "bridged" ]; then |
95 |
ifconfig $INTERFACE 0.0.0.0 up |
96 |
brctl addif $BRIDGE $INTERFACE |
97 |
rm -f $NFDHCPD_STATE_DIR/$INTERFACE |
98 |
fi |