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 # BASED ON https://github.com/openvswitch/ovs/blob/master/utilities/ovs-docker
20 #set -e #Exit script if a command fails
22 # Check for programs we'll need.
28 if test -x "$dir/$1"; then
33 echo >&2 "$0: $1 not found in \$PATH, please install and try again"
38 sudo ovs-vsctl --timeout=60 "$@"
44 sudo docker exec "$CONTAINER" ovs-vsctl --timeout=60 "$@"
47 create_netns_link () {
48 sudo mkdir -p /var/run/netns
49 if [ ! -e /var/run/netns/"$PID" ]; then
50 sudo ln -s /proc/"$PID"/ns/net /var/run/netns/"$PID"
51 trap 'delete_netns_link' 0
52 for signal in 1 2 3 13 14 15; do
53 trap 'delete_netns_link; trap - $signal; kill -$signal $$' $signal
58 delete_netns_link () {
59 sudo rm -f /var/run/netns/"$PID"
62 connect_namespace_to_container () {
67 if [ -z "$NAMESPACE" ] || [ -z "$CONTAINER" ]; then
68 echo >&2 "$UTIL add-port: not enough arguments (use --help for help)"
73 while [ $# -ne 0 ]; do
76 ADDRESS=`expr X"$1" : 'X[^=]*=\(.*\)'`
80 MACADDRESS=`expr X"$1" : 'X[^=]*=\(.*\)'`
84 echo >&2 "$UTIL add-port: unknown option \"$1\""
90 if PID=`sudo docker inspect -f '{{.State.Pid}}' "$CONTAINER"`; then :; else
91 echo >&2 "$UTIL: Failed to get the PID of the container"
97 CONTAINER_IF="v-$NAMESPACE"
98 NAMESPACE_IF="v-${CONTAINER:0:12}"
101 if [ -z `sudo ip netns list | grep "$NAMESPACE"` ]; then
102 sudo ip netns add "$NAMESPACE"
105 # Create a veth pair in namespace.
106 sudo ip netns exec "$NAMESPACE" ip link add "$NAMESPACE_IF" type veth peer \
108 sudo ip netns exec "$NAMESPACE" ip link set dev "$NAMESPACE_IF" up
110 # Move one side to container namespace.
111 sudo ip netns exec "$NAMESPACE" ip link set dev "$CONTAINER_IF" netns "$PID"
112 sudo ip netns exec "$PID" ip link set dev "$CONTAINER_IF" up
114 # And put it in integration bridge
115 d_ovs_vsctl "$CONTAINER" add-port br-int "$CONTAINER_IF"
117 if [ -n "$ADDRESS" ]; then
118 sudo ip netns exec "$NAMESPACE" ip addr add "$ADDRESS" dev "$NAMESPACE_IF"
121 if [ -n "$MACADDRESS" ]; then
122 sudo ip netns exec "$NAMESPACE" ip link set dev "$NAMESPACE_IF" \
123 address "$MACADDRESS"
133 if [ -z `sudo docker images | awk '/^ovs-docker/ {print $1}'` ]; then
134 echo "$UTIL: Docker image ovs-docker does not exist, creating..."
135 sudo docker build -t ovs-docker .
138 CONTAINER=`sudo docker run -itd --privileged --cap-add ALL --name=ovs-node-"$NODE" ovs-docker`
140 if [ $? -ne 0 ]; then
141 echo >&2 "$UTIL: Failed to start container $NODE"
146 while [ "$STATUS" != "EXITED" ]; do
147 STATUS=`sudo docker exec "$CONTAINER" supervisorctl status configure-ovs |\
150 CONTAINER_GW=`sudo docker inspect -f '{{ .NetworkSettings.Gateway }}' "$CONTAINER"`
151 CONTAINER_IP=`sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' "$CONTAINER"`
153 # Create a container bridge as integration for all guests
154 if d_ovs_vsctl "$CONTAINER" br-exists br-int; then :; else
155 d_ovs_vsctl "$CONTAINER" add-br br-int
156 d_ovs_vsctl "$CONTAINER" add-port br-int patch-tun -- \
157 set interface patch-tun type=patch option:peer=patch-int
161 # Create a container bridge as endpoint for all tunnels
162 if d_ovs_vsctl "$CONTAINER" br-exists br-tun; then :; else
163 d_ovs_vsctl "$CONTAINER" add-br br-tun
164 d_ovs_vsctl "$CONTAINER" add-port br-tun patch-int -- \
165 set interface patch-int type=patch option:peer=patch-tun
169 if [ "$TUN" == "vxlan" ]; then
171 elif [ "$TUN" == "vxlan-gpe" ]; then
172 TUN_OPT="type=vxlan option:exts=gpe"
177 if [ -z "$TUN" ]; then :; else
178 ovs_vsctl add-port br-tun vtep-node-"$NODE" -- \
179 set interface vtep-node-"$NODE" $TUN_OPT \
180 option:remote_ip="$CONTAINER_IP" ofport_request="$NODE"
181 d_ovs_vsctl "$CONTAINER" add-port br-tun vtep -- \
182 set interface vtep $TUN_OPT \
183 option:remote_ip="$CONTAINER_GW" ofport_request=10
186 if [ -z "$ODL" ]; then :; else
187 d_ovs_vsctl "$CONTAINER" set-manager "tcp:${ODL}:6640"
188 d_ovs_vsctl "$CONTAINER" set-controller br-tun "tcp:${ODL}:6633"
189 d_ovs_vsctl "$CONTAINER" set-controller br-int "tcp:${ODL}:6633"
193 until [ $DO_GUEST -eq 0 ]; do
194 ADDRESS="10.0.${NODE}.${DO_GUEST}/16"
195 connect_namespace_to_container "ovsnsn${NODE}g$DO_GUEST" "$CONTAINER" \
196 --ipaddress="$ADDRESS"
201 spawn_nodes_and_guests () {
203 while [ $# -ne 0 ]; do
206 NODES=`expr X"$1" : 'X[^=]*=\(.*\)'`
210 GUESTS=`expr X"$1" : 'X[^=]*=\(.*\)'`
214 TUN=`expr X"$1" : 'X[^=]*=\(.*\)'`
218 ODL=`expr X"$1" : 'X[^=]*=\(.*\)'`
222 echo >&2 "$UTIL spawn: unknown option \"$1\""
229 TUN_REGEX="vxlan|vxlan-gpe|^$"
230 IP_REGEX="^$|^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
232 if [[ $NODES =~ $NUM_REGEX ]]; then :; else
233 echo >&2 "$UTIL: NODES has to be a number"
237 if [ $NODES -gt 256 ]; then
238 echo >&2 "$UTIL: NODES has to be less than 256"
242 if [[ $GUESTS =~ $NUM_REGEX ]]; then :; else
243 echo >&2 "$UTIL: GUESTS has to be a number"
247 if [ $GUESTS -gt 256 ]; then
248 echo >&2 "$UTIL: GUESTS has to be less than 256"
252 if [[ $TUN =~ $TUN_REGEX ]]; then :; else
253 echo >&2 "$UTIL: TYPE has to be vxlan or vxlan-gpe"
257 if [[ $ODL =~ $IP_REGEX ]]; then :; else
258 echo >&2 "$UTIL: IP has to be a valid ip address"
262 # Create a host bridge as end point for all tunnels
263 if ovs_vsctl br-exists br-tun; then :; else
264 ovs_vsctl add-br br-tun
265 if [ -z "$ODL" ]; then :; else
266 ovs_vsctl set-manager "tcp:${ODL}:6640"
267 ovs_vsctl set-controller br-tun "tcp:${ODL}:6633"
272 until [ $DO_NODE -eq 0 ]; do
273 spawn_node "$DO_NODE" "$TUN"
280 for ID in `sudo docker ps -a | awk '/ovs-node-[0-9]+$/ {print $1}'`; do
281 sudo docker stop "$ID"
285 for NS in `sudo ip netns list | grep ovsns`; do
286 sudo ip netns del "$NS"
289 ovs_vsctl del-br br-tun
290 ovs_vsctl del-manager
296 ${UTIL}: Perform various tasks related with docker-ovs container.
297 usage: ${UTIL} COMMAND
300 spawn --nodes=NODES --guests=GUESTS --tun=TYPE --odl=IP
301 Runs NODES number of docker-ovs instances and attaches
302 GUESTS number of namespaces to each instance. If tun
303 option is specified, tunnel of such type will be configured
304 between the nodes and a host bridge. Types supported are
307 Stops containers and deletes namespaces
309 -h, --help display this help message.
314 search_path ovs-vsctl
317 #if [[ $EUID -ne 0 ]]; then
318 # echo "This script must be run as root" 1>&2
322 if (sudo ip netns) > /dev/null 2>&1; then :; else
323 echo >&2 "$UTIL: ip utility not found (or it does not support netns),"\
328 if [ $# -eq 0 ]; then
336 spawn_nodes_and_guests "$@"
349 echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"