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"
39 sudo iptables -t nat -F
43 sudo ovs-vsctl --timeout=60 "$@"
49 sudo docker exec "$CONTAINER" ovs-vsctl --timeout=60 "$@"
52 create_netns_link () {
53 sudo mkdir -p /var/run/netns
54 if [ ! -e /var/run/netns/"$PID" ]; then
55 sudo ln -s /proc/"$PID"/ns/net /var/run/netns/"$PID"
56 trap 'delete_netns_link' 0
57 for signal in 1 2 3 13 14 15; do
58 trap 'delete_netns_link; trap - $signal; kill -$signal $$' $signal
63 delete_netns_link () {
64 sudo rm -f /var/run/netns/"$PID"
67 connect_namespace_to_container () {
72 if [ -z "$NAMESPACE" ] || [ -z "$CONTAINER" ]; then
73 echo >&2 "$UTIL add-port: not enough arguments (use --help for help)"
78 while [ $# -ne 0 ]; do
81 ADDRESS=`expr X"$1" : 'X[^=]*=\(.*\)'`
85 MACADDRESS=`expr X"$1" : 'X[^=]*=\(.*\)'`
89 echo >&2 "$UTIL add-port: unknown option \"$1\""
95 if PID=`sudo docker inspect -f '{{.State.Pid}}' "$CONTAINER"`; then :; else
96 echo >&2 "$UTIL: Failed to get the PID of the container"
102 CONTAINER_IF="v-$NAMESPACE"
103 NAMESPACE_IF="v-${CONTAINER:0:12}"
106 if [ -z `sudo ip netns list | grep "$NAMESPACE"` ]; then
107 sudo ip netns add "$NAMESPACE"
110 # Create a veth pair in namespace.
111 sudo ip netns exec "$NAMESPACE" ip link add "$NAMESPACE_IF" type veth peer \
113 sudo ip netns exec "$NAMESPACE" ip link set dev "$NAMESPACE_IF" up
115 # Move one side to container namespace.
116 sudo ip netns exec "$NAMESPACE" ip link set dev "$CONTAINER_IF" netns "$PID"
117 sudo ip netns exec "$PID" ip link set dev "$CONTAINER_IF" up
119 # And put it in integration bridge
120 d_ovs_vsctl "$CONTAINER" add-port br-int "$CONTAINER_IF"
122 if [ -n "$ADDRESS" ]; then
123 sudo ip netns exec "$NAMESPACE" ip addr add "$ADDRESS" dev "$NAMESPACE_IF"
126 if [ -n "$MACADDRESS" ]; then
127 sudo ip netns exec "$NAMESPACE" ip link set dev "$NAMESPACE_IF" \
128 address "$MACADDRESS"
138 if [ -z `sudo docker images | awk '/^ovs-docker/ {print $1}'` ]; then
139 echo "$UTIL: Docker image ovs-docker does not exist, creating..."
140 sudo docker build -t ovs-docker .
143 CONTAINER=`sudo docker run -itd --privileged --cap-add ALL --name=ovs-node-"$NODE" ovs-docker`
145 if [ $? -ne 0 ]; then
146 echo >&2 "$UTIL: Failed to start container $NODE"
151 while [ "$STATUS" != "EXITED" ]; do
152 STATUS=`sudo docker exec "$CONTAINER" supervisorctl status configure-ovs |\
155 CONTAINER_GW=`sudo docker inspect -f '{{ .NetworkSettings.Gateway }}' "$CONTAINER"`
156 CONTAINER_IP=`sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' "$CONTAINER"`
158 # Create a container bridge as integration for all guests
159 if d_ovs_vsctl "$CONTAINER" br-exists br-int; then :; else
160 d_ovs_vsctl "$CONTAINER" add-br br-int
161 d_ovs_vsctl "$CONTAINER" add-port br-int patch-tun -- \
162 set interface patch-tun type=patch option:peer=patch-int
166 # Create a container bridge as endpoint for all tunnels
167 if d_ovs_vsctl "$CONTAINER" br-exists br-tun; then :; else
168 d_ovs_vsctl "$CONTAINER" add-br br-tun
169 d_ovs_vsctl "$CONTAINER" add-port br-tun patch-int -- \
170 set interface patch-int type=patch option:peer=patch-tun
174 if [ "$TUN" == "vxlan" ]; then
176 elif [ "$TUN" == "vxlan-gpe" ]; then
177 TUN_OPT="type=vxlan option:exts=gpe"
182 if [ -z "$TUN" ]; then :; else
183 ovs_vsctl add-port br-tun vtep-node-"$NODE" -- \
184 set interface vtep-node-"$NODE" $TUN_OPT \
185 option:remote_ip="$CONTAINER_IP" ofport_request="$NODE"
186 d_ovs_vsctl "$CONTAINER" add-port br-tun vtep -- \
187 set interface vtep $TUN_OPT \
188 option:remote_ip="$CONTAINER_GW" ofport_request=10
191 if [ -z "$ODL" ]; then :; else
192 d_ovs_vsctl "$CONTAINER" set-manager "tcp:${ODL}:6640"
196 until [ $DO_GUEST -eq 0 ]; do
197 ADDRESS="10.0.${NODE}.${DO_GUEST}/16"
198 connect_namespace_to_container "ovsnsn${NODE}g$DO_GUEST" "$CONTAINER" \
199 --ipaddress="$ADDRESS"
204 spawn_nodes_and_guests () {
206 while [ $# -ne 0 ]; do
209 NODES=`expr X"$1" : 'X[^=]*=\(.*\)'`
213 GUESTS=`expr X"$1" : 'X[^=]*=\(.*\)'`
217 TUN=`expr X"$1" : 'X[^=]*=\(.*\)'`
221 ODL=`expr X"$1" : 'X[^=]*=\(.*\)'`
225 echo >&2 "$UTIL spawn: unknown option \"$1\""
232 TUN_REGEX="vxlan|vxlan-gpe|^$"
233 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])$"
235 if [[ $NODES =~ $NUM_REGEX ]]; then :; else
236 echo >&2 "$UTIL: NODES has to be a number"
240 if [ $NODES -gt 256 ]; then
241 echo >&2 "$UTIL: NODES has to be less than 256"
245 if [[ $GUESTS =~ $NUM_REGEX ]]; then :; else
246 echo >&2 "$UTIL: GUESTS has to be a number"
250 if [ $GUESTS -gt 256 ]; then
251 echo >&2 "$UTIL: GUESTS has to be less than 256"
255 if [[ $TUN =~ $TUN_REGEX ]]; then :; else
256 echo >&2 "$UTIL: TYPE has to be vxlan or vxlan-gpe"
260 if [[ $ODL =~ $IP_REGEX ]]; then :; else
261 echo >&2 "$UTIL: IP has to be a valid ip address"
265 # Create a host bridge as end point for all tunnels
266 if ovs_vsctl br-exists br-tun; then :; else
267 ovs_vsctl add-br br-tun
268 if [ -z "$ODL" ]; then :; else
269 ovs_vsctl set-manager "tcp:${ODL}:6640"
270 ovs_vsctl set-controller br-tun "tcp:${ODL}:6633"
275 until [ $DO_NODE -eq 0 ]; do
276 spawn_node "$DO_NODE" "$TUN"
283 for ID in `sudo docker ps -a | awk '/ovs-node-[0-9]+$/ {print $1}'`; do
284 sudo docker stop "$ID"
288 for NS in `sudo ip netns list | grep ovsns`; do
289 sudo ip netns del "$NS"
292 ovs_vsctl del-br br-tun
293 ovs_vsctl del-manager
299 ${UTIL}: Perform various tasks related with docker-ovs container.
300 usage: ${UTIL} COMMAND
303 spawn --nodes=NODES --guests=GUESTS --tun=TYPE --odl=IP
304 Runs NODES number of docker-ovs instances and attaches
305 GUESTS number of namespaces to each instance. If tun
306 option is specified, tunnel of such type will be configured
307 between the nodes and a host bridge. Types supported are
310 Stops containers and deletes namespaces
312 -h, --help display this help message.
317 search_path ovs-vsctl
321 #if [[ $EUID -ne 0 ]]; then
322 # echo "This script must be run as root" 1>&2
326 if (sudo ip netns) > /dev/null 2>&1; then :; else
327 echo >&2 "$UTIL: ip utility not found (or it does not support netns),"\
332 if [ $# -eq 0 ]; then
340 spawn_nodes_and_guests "$@"
353 echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"