Statistics
| Branch: | Tag: | Revision:

root / tools / net-common.in @ 4fe43605

History | View | Annotate | Download (2.8 kB)

1
#!/bin/bash
2
#
3

    
4
# Copyright (C) 2011, 2012, 2013 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
readonly GANETI_TAP="gnt.com"
24

    
25
function check {
26
  if [ -z "$INTERFACE" ]; then
27
    echo "No network interface specified"
28
    exit 1
29
  fi
30

    
31
  if [ -z "$MODE" ]; then
32
    echo "MODE not specified"
33
    exit 1
34
  fi
35
}
36

    
37
function is_instance_communication_tap {
38
  COMMUNICATION=$(echo "$INTERFACE" | cut -d "." -f 1-2)
39

    
40
  if [ "$MODE" = "routed" -a "$COMMUNICATION" = "$GANETI_TAP" ]
41
  then
42
    return 0
43
  else
44
    return 1
45
  fi
46
}
47

    
48
function fix_mac {
49
  # Fix the autogenerated MAC to have the first octet set to "fe"
50
  # to discourage the bridge from using the TAP dev's MAC
51
  FIXED_MAC=$(ip link show $INTERFACE | \
52
    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
53
  # in case of a vif (xen_netback device) this action is not allowed
54
  ip link set $INTERFACE address $FIXED_MAC || true
55
}
56

    
57
function setup_bridge {
58
  if [ "$MODE" = "bridged" ]; then
59
    fix_mac
60
    ip link set $INTERFACE up
61
    ip link set $INTERFACE mtu $(</sys/class/net/${LINK}/mtu)
62

    
63
    # Connect the interface to the bridge
64
    brctl addif $LINK $INTERFACE
65
  fi
66
}
67

    
68
function setup_ovs {
69
  if [ "$MODE" = "openvswitch" ]; then
70
    ovs-vsctl add-port ${LINK} $INTERFACE
71
  fi
72
}
73

    
74
function setup_route {
75
  if [ "$MODE" = "routed" ]; then
76
    ip link set $INTERFACE up
77

    
78
    if [ -z "$IP" ]; then
79
      echo "Routed NIC but no IP address specified"
80
      exit 1
81
    fi
82

    
83
    # Route traffic targeted at the IP to the interface
84
    if [ -n "$LINK" ]; then
85
      while ip rule del dev $INTERFACE; do :; done
86
      ip rule add dev $INTERFACE table $LINK
87
      ip route replace $IP table $LINK proto static dev $INTERFACE
88

    
89
    else
90
      ip route replace $IP proto static dev $INTERFACE
91
    fi
92

    
93
    # Allow routing and arp proxying, or ndp proxying (IPv6)
94
    if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
95
      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
96
      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
97
    fi
98

    
99
    if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
100
      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
101
      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
102
    fi
103
  fi
104
}