ovs-docker: Add the ability to set VLANs.
[cascardo/ovs.git] / utilities / ovs-docker
1 #!/bin/bash
2 # Copyright (C) 2014 Nicira, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 check_command_avail () {
17     while [ $# -ne 0 ]; do
18         if ("$1" --version) > /dev/null 2>&1; then :; else
19             echo >&2 "$UTIL: missing $1, cannot proceed"
20             exit 1
21         fi
22         shift
23     done
24 }
25
26 ovs_vsctl () {
27     ovs-vsctl --timeout=60 "$@"
28 }
29
30 create_netns_link () {
31     mkdir -p /var/run/netns
32     if [ ! -e /var/run/netns/"$PID" ]; then
33         ln -s /proc/"$PID"/ns/net /var/run/netns/"$PID"
34         trap 'delete_netns_link' 0
35         for signal in 1 2 3 13 14 15; do
36             trap 'delete_netns_link; trap - $signal; kill -$signal $$' $signal
37         done
38     fi
39 }
40
41 delete_netns_link () {
42     rm -f /var/run/netns/"$PID"
43 }
44
45 get_port_for_container_interface () {
46     CONTAINER="$1"
47     INTERFACE="$2"
48
49     PORT=`ovs_vsctl --data=bare --no-heading --columns=name find interface \
50              external_ids:container_id="$CONTAINER"  \
51              external_ids:container_iface="$INTERFACE"`
52     if [ -z "$PORT" ]; then
53         echo >&2 "$UTIL: Failed to find any attached port" \
54                  "for CONTAINER=$CONTAINER and INTERFACE=$INTERFACE"
55     fi
56     echo "$PORT"
57 }
58
59 add_port () {
60     BRIDGE="$1"
61     INTERFACE="$2"
62     CONTAINER="$3"
63     ADDRESS="$4"
64     GATEWAY="$5"
65
66     if [ "$#" -lt 3 ]; then
67         usage
68         exit 1
69     fi
70
71     if ovs_vsctl --may-exist add-br "$BRIDGE"; then :; else
72         echo >&2 "$UTIL: Failed to create bridge $BRIDGE"
73         exit 1
74     fi
75
76     if PID=`docker inspect -f '{{.State.Pid}}' "$CONTAINER"`; then :; else
77         echo >&2 "$UTIL: Failed to get the PID of the container"
78         exit 1
79     fi
80
81     create_netns_link
82
83     # Create a veth pair.
84     ID=`uuidgen | sed 's/-//g'`
85     PORTNAME="${ID:0:13}"
86     ip link add "${PORTNAME}_l" type veth peer name "${PORTNAME}_c"
87
88     # Add one end of veth to OVS bridge.
89     if ovs_vsctl --may-exist add-port "$BRIDGE" "${PORTNAME}_l" \
90        -- set interface "${PORTNAME}_l" \
91        external_ids:container_id="$CONTAINER" \
92        external_ids:container_iface="$INTERFACE"; then :; else
93         echo >&2 "$UTIL: Failed to add "${PORTNAME}_l" port to bridge $BRIDGE"
94         ip link delete "${PORTNAME}_l"
95         exit 1
96     fi
97
98     ip link set "${PORTNAME}_l" up
99
100     # Move "${PORTNAME}_c" inside the container and changes its name.
101     ip link set "${PORTNAME}_c" netns "$PID"
102     ip netns exec "$PID" ip link set dev "${PORTNAME}_c" name "$INTERFACE"
103     ip netns exec "$PID" ip link set "$INTERFACE" up
104
105     if [ -n "$ADDRESS" ]; then
106         ip netns exec "$PID" ip addr add "$ADDRESS" dev "$INTERFACE"
107     fi
108
109     if [ -n "$GATEWAY" ]; then
110         ip netns exec "$PID" ip route add default via "$GATEWAY"
111     fi
112 }
113
114 del_port () {
115     BRIDGE="$1"
116     INTERFACE="$2"
117     CONTAINER="$3"
118
119     if [ "$#" -lt 3 ]; then
120         usage
121         exit 1
122     fi
123
124     PORT=`get_port_for_container_interface "$CONTAINER" "$INTERFACE"`
125     if [ -z "$PORT" ]; then
126         exit 1
127     fi
128
129     ovs_vsctl --if-exists del-port "$PORT"
130
131     ip link delete "$PORT"
132 }
133
134 del_ports () {
135     BRIDGE="$1"
136     CONTAINER="$2"
137     if [ "$#" -lt 2 ]; then
138         usage
139         exit 1
140     fi
141
142     PORTS=`ovs_vsctl --data=bare --no-heading --columns=name find interface \
143              external_ids:container_id="$CONTAINER"`
144     if [ -z "$PORTS" ]; then
145         exit 0
146     fi
147
148     for PORT in $PORTS; do
149         ovs_vsctl --if-exists del-port "$PORT"
150         ip link delete "$PORT"
151     done
152 }
153
154 set_vlan () {
155     BRIDGE="$1"
156     INTERFACE="$2"
157     CONTAINER_ID="$3"
158     VLAN="$4"
159
160     if [ "$#" -lt 4 ]; then
161         usage
162         exit 1
163     fi
164
165     PORT=`get_port_for_container_interface "$CONTAINER_ID" "$INTERFACE"`
166     if [ -z "$PORT" ]; then
167         exit 1
168     fi
169     ovs_vsctl set port "$PORT" tag="$VLAN"
170 }
171
172 usage() {
173     cat << EOF
174 ${UTIL}: Performs integration of Open vSwitch with Docker.
175 usage: ${UTIL} COMMAND
176
177 Commands:
178   add-port BRIDGE INTERFACE CONTAINER [ADDRESS [GATEWAY]]
179                     Adds INTERFACE inside CONTAINER and connects it as a port
180                     in Open vSwitch BRIDGE. Optionally, sets ADDRESS on
181                     INTERFACE. ADDRESS can include a '/' to represent network
182                     prefix length. Along with ADDRESS, optionally set the
183                     default gateway for the container. e.g.:
184                     ${UTIL} add-port br-int eth1 c474a0e2830e 192.168.1.2/24 \
185                         192.168.1.1
186   del-port BRIDGE INTERFACE CONTAINER
187                     Deletes INTERFACE inside CONTAINER and removes its
188                     connection to Open vSwitch BRIDGE. e.g.:
189                     ${UTIL} del-port br-int eth1 c474a0e2830e
190   del-ports BRIDGE CONTAINER
191                     Removes all Open vSwitch interfaces from CONTAINER. e.g.:
192                     ${UTIL} del-ports br-int c474a0e2830e
193   set-vlan BRIDGE INTERFACE CONTAINER VLAN
194                     Configures the INTERFACE of CONTAINER attached to BRIDGE
195                     to become an access port of VLAN. e.g.:
196                     ${UTIL} set-vlan br-int eth1 c474a0e2830e 5
197 Options:
198   -h, --help        display this help message.
199 EOF
200 }
201
202 UTIL=$(basename $0)
203 check_command_avail ovs-vsctl docker uuidgen
204
205 if (ip netns) > /dev/null 2>&1; then :; else
206     echo >&2 "$UTIL: ip utility not found (or it does not support netns),"\
207              "cannot proceed"
208     exit 1
209 fi
210
211 if [ $# -eq 0 ]; then
212     usage
213     exit 0
214 fi
215
216 case $1 in
217     "add-port")
218         shift
219         add_port "$@"
220         exit 0
221         ;;
222     "del-port")
223         shift
224         del_port "$@"
225         exit 0
226         ;;
227     "del-ports")
228         shift
229         del_ports "$@"
230         exit 0
231         ;;
232     "set-vlan")
233         shift
234         set_vlan "$@"
235         exit 0
236         ;;
237     -h | --help)
238         usage
239         exit 0
240         ;;
241     *)
242         echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"
243         exit 1
244         ;;
245 esac