Statistics
| Branch: | Tag: | Revision:

root / tools / kvm-ifup.in @ 57fb6fcb

History | View | Annotate | Download (2.4 kB)

1
#!/bin/bash
2
#
3

    
4
# Copyright (C) 2011, 2012 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21
@SHELL_ENV_INIT@
22

    
23
if [ -z "$INTERFACE" ]; then
24
  echo "No network interface specified"
25
  exit 1
26
fi
27

    
28
if [ -z "$MODE" ]; then
29
  echo "MODE not specified"
30
  exit 1
31
fi
32

    
33
# Execute the user-supplied network script, if applicable
34
if [ -x "$CONF_DIR/kvm-vif-bridge" ]; then
35
  exec $CONF_DIR/kvm-vif-bridge
36
fi
37

    
38
if [ "$MODE" = "bridged" ]; then
39
  # Fix the autogenerated MAC to have the first octet set to "fe"
40
  # to discourage the bridge from using the TAP dev's MAC
41
  FIXED_MAC=$(ip link show $INTERFACE | \
42
    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
43
  ip link set $INTERFACE address $FIXED_MAC
44

    
45
  ip link set $INTERFACE up
46
  ip link set $INTERFACE mtu $(</sys/class/net/${BRIDGE}/mtu)
47

    
48
  # Connect the interface to the bridge
49
  brctl addif $BRIDGE $INTERFACE
50

    
51
elif [ "$MODE" = "openvswitch" ]; then
52
  ovs-vsctl add-port ${LINK} $INTERFACE
53

    
54
else
55
  ip link set $INTERFACE up
56

    
57
  if [ -z "$IP" ]; then
58
    echo "Routed NIC but no IP address specified"
59
    exit 1
60
  fi
61

    
62
  # Route traffic targeted at the IP to the interface
63
  if [ -n "$LINK" ]; then
64
    while ip rule del dev $INTERFACE; do :; done
65
    ip rule add dev $INTERFACE table $LINK
66
    ip route replace $IP table $LINK proto static dev $INTERFACE
67

    
68
  else
69
    ip route replace $IP proto static dev $INTERFACE
70
  fi
71

    
72
  # Allow routing and arp proxying, or ndp proxying (IPv6)
73
  if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
74
    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
75
    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
76
  fi
77

    
78
  if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
79
    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
80
    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
81
  fi
82
fi