1#!/bin/bash
2#============================================================================
3# ${XEN_SCRIPT_DIR}/vif-bridge
4#
5# Script for configuring a vif in bridged mode.
6#
7# Usage:
8# vif-bridge (add|remove|online|offline)
9#
10# Environment vars:
11# vif         vif interface name (required).
12# XENBUS_PATH path to this device's details in the XenStore (required).
13#
14# Read from the store:
15# bridge  bridge to add the vif to (optional).  Defaults to searching for the
16#         bridge itself.
17# ip      list of IP networks for the vif, space-separated (optional).
18#
19# up:
20# Enslaves the vif interface to the bridge and adds iptables rules
21# for its ip addresses (if any).
22#
23# down:
24# Removes the vif interface from the bridge and removes the iptables
25# rules for its ip addresses (if any).
26#============================================================================
27
28dir=$(dirname "$0")
29. "$dir/vif-common.sh"
30
31bridge=${bridge:-}
32bridge=$(xenstore_read_default "$XENBUS_PATH/bridge" "$bridge")
33
34if [ -z "$bridge" ]; then
35    if which brctl >&/dev/null; then
36        bridge=$(brctl show | awk 'NR==2{print$1}')
37    else
38        bridge=$(bridge link | cut -d" " -f7)
39    fi
40  if [ -z "$bridge" ]
41  then
42     fatal "Could not find bridge, and none was specified"
43  fi
44else
45  #
46  # Old style bridge setup with netloop, used to have a bridge name
47  # of xenbrX, enslaving pethX and vif0.X, and then configuring
48  # eth0.
49  #
50  # New style bridge setup does not use netloop, so the bridge name
51  # is ethX and the physical device is enslaved pethX
52  #
53  # So if...
54  #
55  #   - User asks for xenbrX
56  #   - AND xenbrX doesn't exist
57  #   - AND there is a ethX device which is a bridge
58  #
59  # ..then we translate xenbrX to ethX
60  #
61  # This lets old config files work without modification
62  #
63  if [ ! -e "/sys/class/net/$bridge" ] && [ -z "${bridge##xenbr*}" ]
64  then
65     if [ -e "/sys/class/net/eth${bridge#xenbr}/bridge" ]
66     then
67        bridge="eth${bridge#xenbr}"
68     fi
69  fi
70fi
71
72RET=0
73ip link show dev "$bridge" 1>/dev/null 2>&1 || RET=1
74if [ "$RET" -eq 1 ]
75then
76    fatal "Could not find bridge device $bridge"
77fi
78
79case "$command" in
80    online)
81        setup_virtual_bridge_port "$dev"
82        set_mtu "$bridge" "$dev"
83        add_to_bridge "$bridge" "$dev"
84        ;;
85
86    offline)
87        if which brctl >&/dev/null; then
88            do_without_error brctl delif "$bridge" "$dev"
89        else
90            do_without_error ip link set "$dev" nomaster
91        fi
92        do_without_error ifconfig "$dev" down
93        ;;
94
95    add)
96        setup_virtual_bridge_port "$dev"
97        set_mtu "$bridge" "$dev"
98        add_to_bridge "$bridge" "$dev"
99        ;;
100esac
101
102handle_iptable
103
104call_hooks vif post
105
106log debug "Successful vif-bridge $command for $dev, bridge $bridge."
107if [ "$type_if" = vif -a "$command" = "online" ]
108then
109  success
110fi
111