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 for programs we'll need.
22 if test -x "$dir/$1"; then
27 echo >&2 "$0: $1 not found in \$PATH, please install and try again"
32 ovs-vsctl --timeout=60 "$@"
35 create_netns_link () {
36 mkdir -p /var/run/netns
37 if [ ! -e /var/run/netns/"$PID" ]; then
38 ln -s /proc/"$PID"/ns/net /var/run/netns/"$PID"
39 trap 'delete_netns_link' 0
40 for signal in 1 2 3 13 14 15; do
41 trap 'delete_netns_link; trap - $signal; kill -$signal $$' $signal
46 delete_netns_link () {
47 rm -f /var/run/netns/"$PID"
50 get_port_for_container_interface () {
54 PORT=`ovs_vsctl --data=bare --no-heading --columns=name find interface \
55 external_ids:container_id="$CONTAINER" \
56 external_ids:container_iface="$INTERFACE"`
57 if [ -z "$PORT" ]; then
58 echo >&2 "$UTIL: Failed to find any attached port" \
59 "for CONTAINER=$CONTAINER and INTERFACE=$INTERFACE"
71 if [ "$#" -lt 3 ]; then
76 # Check if a port is already attached for the given container and interface
77 PORT=`get_port_for_container_interface "$CONTAINER" "$INTERFACE" \
79 if [ -n "$PORT" ]; then
80 echo >&2 "$UTIL: Port already attached" \
81 "for CONTAINER=$CONTAINER and INTERFACE=$INTERFACE"
85 if ovs_vsctl br-exists "$BRIDGE" || \
86 ovs_vsctl add-br "$BRIDGE"; then :; else
87 echo >&2 "$UTIL: Failed to create bridge $BRIDGE"
91 if PID=`docker inspect -f '{{.State.Pid}}' "$CONTAINER"`; then :; else
92 echo >&2 "$UTIL: Failed to get the PID of the container"
99 ID=`uuidgen | sed 's/-//g'`
100 PORTNAME="${ID:0:13}"
101 ip link add "${PORTNAME}_l" type veth peer name "${PORTNAME}_c"
103 # Add one end of veth to OVS bridge.
104 if ovs_vsctl --may-exist add-port "$BRIDGE" "${PORTNAME}_l" \
105 -- set interface "${PORTNAME}_l" \
106 external_ids:container_id="$CONTAINER" \
107 external_ids:container_iface="$INTERFACE"; then :; else
108 echo >&2 "$UTIL: Failed to add "${PORTNAME}_l" port to bridge $BRIDGE"
109 ip link delete "${PORTNAME}_l"
113 ip link set "${PORTNAME}_l" up
115 # Move "${PORTNAME}_c" inside the container and changes its name.
116 ip link set "${PORTNAME}_c" netns "$PID"
117 ip netns exec "$PID" ip link set dev "${PORTNAME}_c" name "$INTERFACE"
118 ip netns exec "$PID" ip link set "$INTERFACE" up
120 if [ -n "$ADDRESS" ]; then
121 ip netns exec "$PID" ip addr add "$ADDRESS" dev "$INTERFACE"
124 if [ -n "$GATEWAY" ]; then
125 ip netns exec "$PID" ip route add default via "$GATEWAY"
134 if [ "$#" -lt 3 ]; then
139 PORT=`get_port_for_container_interface "$CONTAINER" "$INTERFACE"`
140 if [ -z "$PORT" ]; then
144 ovs_vsctl --if-exists del-port "$PORT"
146 ip link delete "$PORT"
152 if [ "$#" -lt 2 ]; then
157 PORTS=`ovs_vsctl --data=bare --no-heading --columns=name find interface \
158 external_ids:container_id="$CONTAINER"`
159 if [ -z "$PORTS" ]; then
163 for PORT in $PORTS; do
164 ovs_vsctl --if-exists del-port "$PORT"
165 ip link delete "$PORT"
175 if [ "$#" -lt 4 ]; then
180 PORT=`get_port_for_container_interface "$CONTAINER_ID" "$INTERFACE"`
181 if [ -z "$PORT" ]; then
184 ovs_vsctl set port "$PORT" tag="$VLAN"
189 ${UTIL}: Performs integration of Open vSwitch with Docker.
190 usage: ${UTIL} COMMAND
193 add-port BRIDGE INTERFACE CONTAINER [ADDRESS [GATEWAY]]
194 Adds INTERFACE inside CONTAINER and connects it as a port
195 in Open vSwitch BRIDGE. Optionally, sets ADDRESS on
196 INTERFACE. ADDRESS can include a '/' to represent network
197 prefix length. Along with ADDRESS, optionally set the
198 default gateway for the container. e.g.:
199 ${UTIL} add-port br-int eth1 c474a0e2830e 192.168.1.2/24 \
201 del-port BRIDGE INTERFACE CONTAINER
202 Deletes INTERFACE inside CONTAINER and removes its
203 connection to Open vSwitch BRIDGE. e.g.:
204 ${UTIL} del-port br-int eth1 c474a0e2830e
205 del-ports BRIDGE CONTAINER
206 Removes all Open vSwitch interfaces from CONTAINER. e.g.:
207 ${UTIL} del-ports br-int c474a0e2830e
208 set-vlan BRIDGE INTERFACE CONTAINER VLAN
209 Configures the INTERFACE of CONTAINER attached to BRIDGE
210 to become an access port of VLAN. e.g.:
211 ${UTIL} set-vlan br-int eth1 c474a0e2830e 5
213 -h, --help display this help message.
218 search_path ovs-vsctl
222 if (ip netns) > /dev/null 2>&1; then :; else
223 echo >&2 "$UTIL: ip utility not found (or it does not support netns),"\
228 if [ $# -eq 0 ]; then
259 echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"