summaryrefslogtreecommitdiff
path: root/network/openvswitch/xen/network-openvswitch
blob: 45cda0b1954eee7b932125c87aa1225b7bce7d1c (plain)
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
#!/bin/bash
#============================================================================
# Default Xen network start/stop script.
# Xend calls a network script when it starts.
# The script name to use is defined in ${XEN_CONFIG_DIR}/xend-config.sxp
# in the network-script field.
#
# This script creates a virtual switch (default ${netdev}) and adds a
# device (defaults to eth0) to it.  The interface that this Open vSwitch
# is created on should not have a working IP address and will be used as
# a switch for Xen domU's.
#
# Usage:
# network-openvswitch (start|stop|status) {VAR=VAL}*
#
# Vars:
# bridge     The bridge to use (default xenvs0).
# netdev     The interface to add to the bridge (default eth0).  This
#            device should not be configured with an IP address.  If so
#            this script will tear down the interface and bring it back up
#            without an IP address.
#
# start:
# Creates the bridge as bridge
# Enslaves netdev to bridge
#
# stop:
# Removes netdev from the bridge
# Deletes bridge
#
# status:
# Print addresses, interfaces
#
#============================================================================

dir=$(dirname "$0")
. "$dir/logging.sh"
. "$dir/xen-script-common.sh"
. "$dir/xen-network-common.sh"
. "$dir/locking.sh"

findCommand "$@"
evalVariables "$@"

netdev=${netdev:-eth0}
bridge=${bridge:-ovs0}

addr=`ip addr show dev ${netdev} | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
if [ -n "$addr" ]; then
    echo "Invalid device: ${netdev} is up and has a valid IP address!" >&2
    exit 1
fi

show_status () {
    local dev=$1
    local bridge=$2
    
    echo '============================================================'
    echo 'vSwitch interfaces'
    ovs-vsctl list-ifaces ${bridge}
    echo ' '
    echo 'vSwitch ports'
    ovs-vsctl list-ports ${bridge}
    echo '============================================================'
}

op_start () {
    if [ "${bridge}" = "null" ] ; then
	return
    fi

    ifconfig "${netdev}" down
    ifconfig "${netdev}" 0.0.0.0 up
    ovs-vsctl -- --may-exist add-br ${bridge}
    ifconfig "${bridge}" 0.0.0.0 up
    ovs-vsctl -- --may-exist add-port ${bridge} ${netdev}

    # Remove any stale ports from last time virtual switch was running.
    # Open vSwitch has the habit of remembering port settings between
    # runs.
    for port in $(ovs-vsctl list-ports ${bridge})
    do
	if [ "${port}" != "${netdev}" ]
	then
	    ifconfig "${port}" down
	    ovs-vsctl del-port ${port}
	fi
    done
}

op_stop () {
    if [ "${bridge}" = "null" ]; then
	return
    fi

    # Remove all ports from virtual switch.
    for port in $(ovs-vsctl list-ports ${bridge})
    do
	ifconfig "${port}" down
	ovs-vsctl del-port ${port}
    done

    ifconfig "${bridge}" down
    ovs-vsctl -- --if-exists del-br ${bridge}
}

case "$command" in
    start)
	op_start
	;;
    
    stop)
	op_stop
	;;

    status)
	show_status ${netdev} ${bridge}
	;;

    *)
	echo "Unknown command: $command" >&2
	echo 'Valid commands are: start, stop, status' >&2
	exit 1
esac