2 # Copyright (C) 2014 Nicira, Inc.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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"
27 ovs-vsctl --timeout=60 "$@"
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
41 delete_netns_link () {
42 rm -f /var/run/netns/"$PID"
45 get_port_for_container_interface () {
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"
66 if [ "$#" -lt 3 ]; then
71 if ovs_vsctl --may-exist add-br "$BRIDGE"; then :; else
72 echo >&2 "$UTIL: Failed to create bridge $BRIDGE"
76 if PID=`docker inspect -f '{{.State.Pid}}' "$CONTAINER"`; then :; else
77 echo >&2 "$UTIL: Failed to get the PID of the container"
84 ID=`uuidgen | sed 's/-//g'`
86 ip link add "${PORTNAME}_l" type veth peer name "${PORTNAME}_c"
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"
98 ip link set "${PORTNAME}_l" up
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
105 if [ -n "$ADDRESS" ]; then
106 ip netns exec "$PID" ip addr add "$ADDRESS" dev "$INTERFACE"
109 if [ -n "$GATEWAY" ]; then
110 ip netns exec "$PID" ip route add default via "$GATEWAY"
119 if [ "$#" -lt 3 ]; then
124 PORT=`get_port_for_container_interface "$CONTAINER" "$INTERFACE"`
125 if [ -z "$PORT" ]; then
129 ovs_vsctl --if-exists del-port "$PORT"
131 ip link delete "$PORT"
137 if [ "$#" -lt 2 ]; then
142 PORTS=`ovs_vsctl --data=bare --no-heading --columns=name find interface \
143 external_ids:container_id="$CONTAINER"`
144 if [ -z "$PORTS" ]; then
148 for PORT in $PORTS; do
149 ovs_vsctl --if-exists del-port "$PORT"
150 ip link delete "$PORT"
160 if [ "$#" -lt 4 ]; then
165 PORT=`get_port_for_container_interface "$CONTAINER_ID" "$INTERFACE"`
166 if [ -z "$PORT" ]; then
169 ovs_vsctl set port "$PORT" tag="$VLAN"
174 ${UTIL}: Performs integration of Open vSwitch with Docker.
175 usage: ${UTIL} COMMAND
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 \
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
198 -h, --help display this help message.
203 check_command_avail ovs-vsctl docker uuidgen
205 if (ip netns) > /dev/null 2>&1; then :; else
206 echo >&2 "$UTIL: ip utility not found (or it does not support netns),"\
211 if [ $# -eq 0 ]; then
242 echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"