From 46b0cc35f1e30ad67ab88cc46d7c3ef40e7aaa38 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 10:11:43 +0100 Subject: [PATCH 1/9] drop IP/network from 'weave launch' plus a whole bunch of other small changes to weave script which I'll eventually extract into separate commits. --- weave | 96 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/weave b/weave index e83d11e4..fa6378df 100755 --- a/weave +++ b/weave @@ -3,7 +3,7 @@ set -e usage() { echo "Usage:" - echo "weave launch [-password ] ..." + echo "weave launch [-password ] ..." echo "weave run ..." echo "weave attach " echo "weave detach " @@ -72,8 +72,14 @@ add_iptables_rule() { fi } -add_nat_rule() { - add_iptables_rule nat "$@" +# Delete a rule from iptables, if it exist +delete_iptables_rule() { + IPTABLES_TABLE="$1" + shift 1 + if run_iptables -t $IPTABLES_TABLE -C "$@" >/dev/null 2>&1 + then + run_iptables -t $IPTABLES_TABLE -D "$@" >/dev/null + fi } create_bridge() { @@ -92,6 +98,9 @@ create_bridge() { # Work around the situation where there are no rules allowing traffic # across our bridge. E.g. ufw add_iptables_rule filter FORWARD -i $BRIDGE -o $BRIDGE -j ACCEPT + # create a chain for masquerading + run_iptables -t nat -N WEAVE >/dev/null 2>&1 || true + add_iptables_rule nat POSTROUTING -j WEAVE } ip link set dev $BRIDGE up } @@ -101,16 +110,6 @@ destroy_bridge() { ip link del dev $BRIDGE fi run_iptables -t filter -D FORWARD -i $BRIDGE -o $BRIDGE -j ACCEPT 2>/dev/null || true -} - -setup_masquerading() { - run_iptables -t nat -N WEAVE >/dev/null 2>&1 || true - add_nat_rule POSTROUTING -j WEAVE - add_nat_rule WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE - add_nat_rule WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE -} - -stop_masquerading() { run_iptables -t nat -F WEAVE >/dev/null 2>&1 || true run_iptables -t nat -D POSTROUTING -j WEAVE >/dev/null 2>&1 || true run_iptables -t nat -X WEAVE >/dev/null 2>&1 || true @@ -118,7 +117,7 @@ stop_masquerading() { # the following borrows from https://github.com/jpetazzo/pipework with_container_netns () { - CONTAINER="$2" + CONTAINER="$1" CONTAINER_PID=$(docker inspect --format='{{ .State.Pid }}' $CONTAINER) if [ "$CONTAINER_PID" = 0 ] ; then @@ -143,6 +142,7 @@ with_container_netns () { # Run the wrapped command STATUS=0 + shift 1 if ! "$@" >$IP_TMPFILE 2>&1 ; then STATUS=1 if [ "$(docker inspect --format='{{ .State.Pid }}' $CONTAINER)" != "$CONTAINER_PID" ] ; then @@ -157,19 +157,34 @@ with_container_netns () { return $STATUS } -# Attach Container-ID $1 to the weave network at CIDR address $2, -# creating a virtual ethernet device if necessary. -# Also turn off tx offload on device $3 inside the namespace. +launch() { + if ! ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU ; then + return 1 + fi + if ! ip link set $GUEST_IFNAME netns $NETNS ; then + # failed before we assigned the veth to the container's + # namespace - delete it + ip link del $LOCAL_IFNAME type veth || true + return 1 + fi + + ip link set $LOCAL_IFNAME master $BRIDGE + ip link set $LOCAL_IFNAME up + ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME + ip netns exec $NETNS ip link set $CONTAINER_IFNAME up + check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null +} + attach() { if ip link show $LOCAL_IFNAME >/dev/null 2>&1 ; then # veth name already exists, so assume we set it up already; # just add the IP address. - if ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $2 >/dev/null ; then + if ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $1 >/dev/null ; then # address was there already return 0 fi - if ! ip netns exec $NETNS ip addr add $2 dev $CONTAINER_IFNAME ; then + if ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME ; then return 1 fi @@ -190,9 +205,9 @@ attach() { fi if ! ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME || - ! ip netns exec $NETNS ip addr add $2 dev $CONTAINER_IFNAME || + ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME || ! ip netns exec $NETNS ip link set $CONTAINER_IFNAME up || - ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $3 tx off >/dev/null ; then + ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $CONTAINER_IFNAME tx off >/dev/null ; then return 1 fi @@ -205,15 +220,13 @@ attach() { ip netns exec $NETNS ip route add 224.0.0.0/4 dev $CONTAINER_IFNAME } -# Detach Container-ID $1 at CIDR address $2 from the weave network -# removing the virtual ethernet device if there are no more addresses detach() { - if ! ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $2 >/dev/null ; then + if ! ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $1 >/dev/null ; then # address is not there, leave the device alone return 0 fi - if ! ip netns exec $NETNS ip addr del $2 dev $CONTAINER_IFNAME ; then + if ! ip netns exec $NETNS ip addr del $1 dev $CONTAINER_IFNAME ; then return 1 fi @@ -228,9 +241,6 @@ detach() { case "$COMMAND" in launch) - [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 case $(docker inspect --format '{{ .State.Running }} {{ .Config.Image }}' weave 2>/dev/null) in "true $IMAGE") echo "Weave is already running." >&2 @@ -249,8 +259,6 @@ case "$COMMAND" in ;; esac create_bridge - stop_masquerading - setup_masquerading $IPADDR # We set the router name to the bridge mac since that is # stable across re-creations of the containers. # @@ -262,7 +270,7 @@ case "$COMMAND" in # additional parameters, such as resource limits, to docker # when launching the weave container. CONTAINER=$(docker run --privileged -d --name=weave -p 6783:6783/tcp -p 6783:6783/udp $WEAVE_DOCKER_ARGS $IMAGE -name $MACADDR "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR eth0 + with_container_netns $CONTAINER launch echo $CONTAINER ;; status) @@ -295,40 +303,40 @@ case "$COMMAND" in ;; run) [ $# -gt 0 ] || usage - IPADDR=$1 + CIDR=$1 shift 1 create_bridge CONTAINER=$(docker run -d "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR $CONTAINER_IFNAME + with_container_netns $CONTAINER attach $CIDR echo $CONTAINER ;; attach) [ $# -gt 1 ] || usage create_bridge - with_container_netns attach $2 $1 $CONTAINER_IFNAME + with_container_netns $2 attach $1 ;; detach) [ $# -gt 1 ] || usage - with_container_netns detach $2 $1 + with_container_netns $2 detach $1 ;; expose) [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 create_bridge - if ! ip addr show dev $BRIDGE | grep -qF $IPADDR + if ! ip addr show dev $BRIDGE | grep -qF $1 then - ip addr add dev $BRIDGE $IPADDR + ip addr add dev $BRIDGE $1 + add_iptables_rule nat WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE + add_iptables_rule nat WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE fi ;; hide) [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 create_bridge - if ip addr show dev $BRIDGE | grep -qF $IPADDR + if ip addr show dev $BRIDGE | grep -qF $1 then - ip addr del dev $BRIDGE $IPADDR + ip addr del dev $BRIDGE $1 + delete_iptables_rule nat WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE + delete_iptables_rule nat WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE fi ;; stop) @@ -337,13 +345,11 @@ case "$COMMAND" in fi docker rm -f weave >/dev/null 2>&1 || true check_command_and_run conntrack conntrack -D -p udp --dport 6783 >/dev/null 2>&1 || true - stop_masquerading ;; reset) docker kill weave >/dev/null 2>&1 || true docker rm -f weave >/dev/null 2>&1 || true check_command_and_run conntrack conntrack -D -p udp --dport 6783 >/dev/null 2>&1 || true - stop_masquerading destroy_bridge ;; *) From 0adda9eae90dd57193cf00d35effd9e9250505ed Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 10:11:43 +0100 Subject: [PATCH 2/9] drop IP/network from 'weave launch' plus a whole bunch of other small changes to weave script which I'll eventually extract into separate commits. --- weave | 96 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/weave b/weave index ea6d9a9f..a3231e67 100755 --- a/weave +++ b/weave @@ -3,7 +3,7 @@ set -e usage() { echo "Usage:" - echo "weave launch [-password ] ..." + echo "weave launch [-password ] ..." echo "weave run ..." echo "weave start " echo "weave attach " @@ -73,8 +73,14 @@ add_iptables_rule() { fi } -add_nat_rule() { - add_iptables_rule nat "$@" +# Delete a rule from iptables, if it exist +delete_iptables_rule() { + IPTABLES_TABLE="$1" + shift 1 + if run_iptables -t $IPTABLES_TABLE -C "$@" >/dev/null 2>&1 + then + run_iptables -t $IPTABLES_TABLE -D "$@" >/dev/null + fi } create_bridge() { @@ -93,6 +99,9 @@ create_bridge() { # Work around the situation where there are no rules allowing traffic # across our bridge. E.g. ufw add_iptables_rule filter FORWARD -i $BRIDGE -o $BRIDGE -j ACCEPT + # create a chain for masquerading + run_iptables -t nat -N WEAVE >/dev/null 2>&1 || true + add_iptables_rule nat POSTROUTING -j WEAVE } ip link set dev $BRIDGE up } @@ -102,16 +111,6 @@ destroy_bridge() { ip link del dev $BRIDGE fi run_iptables -t filter -D FORWARD -i $BRIDGE -o $BRIDGE -j ACCEPT 2>/dev/null || true -} - -setup_masquerading() { - run_iptables -t nat -N WEAVE >/dev/null 2>&1 || true - add_nat_rule POSTROUTING -j WEAVE - add_nat_rule WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE - add_nat_rule WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE -} - -stop_masquerading() { run_iptables -t nat -F WEAVE >/dev/null 2>&1 || true run_iptables -t nat -D POSTROUTING -j WEAVE >/dev/null 2>&1 || true run_iptables -t nat -X WEAVE >/dev/null 2>&1 || true @@ -119,7 +118,7 @@ stop_masquerading() { # the following borrows from https://github.com/jpetazzo/pipework with_container_netns () { - CONTAINER="$2" + CONTAINER="$1" CONTAINER_PID=$(docker inspect --format='{{ .State.Pid }}' $CONTAINER) if [ "$CONTAINER_PID" = 0 ] ; then @@ -144,6 +143,7 @@ with_container_netns () { # Run the wrapped command STATUS=0 + shift 1 if ! "$@" >$IP_TMPFILE 2>&1 ; then STATUS=1 if [ "$(docker inspect --format='{{ .State.Pid }}' $CONTAINER)" != "$CONTAINER_PID" ] ; then @@ -158,19 +158,34 @@ with_container_netns () { return $STATUS } -# Attach Container-ID $1 to the weave network at CIDR address $2, -# creating a virtual ethernet device if necessary. -# Also turn off tx offload on device $3 inside the namespace. +launch() { + if ! ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU ; then + return 1 + fi + if ! ip link set $GUEST_IFNAME netns $NETNS ; then + # failed before we assigned the veth to the container's + # namespace - delete it + ip link del $LOCAL_IFNAME type veth || true + return 1 + fi + + ip link set $LOCAL_IFNAME master $BRIDGE + ip link set $LOCAL_IFNAME up + ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME + ip netns exec $NETNS ip link set $CONTAINER_IFNAME up + check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null +} + attach() { if ip link show $LOCAL_IFNAME >/dev/null 2>&1 ; then # veth name already exists, so assume we set it up already; # just add the IP address. - if ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $2 >/dev/null ; then + if ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $1 >/dev/null ; then # address was there already return 0 fi - if ! ip netns exec $NETNS ip addr add $2 dev $CONTAINER_IFNAME ; then + if ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME ; then return 1 fi @@ -191,9 +206,9 @@ attach() { fi if ! ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME || - ! ip netns exec $NETNS ip addr add $2 dev $CONTAINER_IFNAME || + ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME || ! ip netns exec $NETNS ip link set $CONTAINER_IFNAME up || - ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $3 tx off >/dev/null ; then + ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $CONTAINER_IFNAME tx off >/dev/null ; then return 1 fi @@ -206,15 +221,13 @@ attach() { ip netns exec $NETNS ip route add 224.0.0.0/4 dev $CONTAINER_IFNAME } -# Detach Container-ID $1 at CIDR address $2 from the weave network -# removing the virtual ethernet device if there are no more addresses detach() { - if ! ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $2 >/dev/null ; then + if ! ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME | grep -F $1 >/dev/null ; then # address is not there, leave the device alone return 0 fi - if ! ip netns exec $NETNS ip addr del $2 dev $CONTAINER_IFNAME ; then + if ! ip netns exec $NETNS ip addr del $1 dev $CONTAINER_IFNAME ; then return 1 fi @@ -229,9 +242,6 @@ detach() { case "$COMMAND" in launch) - [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 case $(docker inspect --format '{{ .State.Running }} {{ .Config.Image }}' weave 2>/dev/null) in "true $IMAGE") echo "Weave is already running." >&2 @@ -250,8 +260,6 @@ case "$COMMAND" in ;; esac create_bridge - stop_masquerading - setup_masquerading $IPADDR # We set the router name to the bridge mac since that is # stable across re-creations of the containers. # @@ -263,7 +271,7 @@ case "$COMMAND" in # additional parameters, such as resource limits, to docker # when launching the weave container. CONTAINER=$(docker run --privileged -d --name=weave -p 6783:6783/tcp -p 6783:6783/udp $WEAVE_DOCKER_ARGS $IMAGE -name $MACADDR "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR eth0 + with_container_netns $CONTAINER launch echo $CONTAINER ;; status) @@ -296,11 +304,11 @@ case "$COMMAND" in ;; run) [ $# -gt 0 ] || usage - IPADDR=$1 + CIDR=$1 shift 1 create_bridge CONTAINER=$(docker run -d "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR $CONTAINER_IFNAME + with_container_netns $CONTAINER attach $CIDR echo $CONTAINER ;; start) @@ -314,30 +322,30 @@ case "$COMMAND" in attach) [ $# -gt 1 ] || usage create_bridge - with_container_netns attach $2 $1 $CONTAINER_IFNAME + with_container_netns $2 attach $1 ;; detach) [ $# -gt 1 ] || usage - with_container_netns detach $2 $1 + with_container_netns $2 detach $1 ;; expose) [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 create_bridge - if ! ip addr show dev $BRIDGE | grep -qF $IPADDR + if ! ip addr show dev $BRIDGE | grep -qF $1 then - ip addr add dev $BRIDGE $IPADDR + ip addr add dev $BRIDGE $1 + add_iptables_rule nat WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE + add_iptables_rule nat WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE fi ;; hide) [ $# -gt 0 ] || usage - IPADDR=$1 - shift 1 create_bridge - if ip addr show dev $BRIDGE | grep -qF $IPADDR + if ip addr show dev $BRIDGE | grep -qF $1 then - ip addr del dev $BRIDGE $IPADDR + ip addr del dev $BRIDGE $1 + delete_iptables_rule nat WEAVE -o $BRIDGE ! -s $1 -j MASQUERADE + delete_iptables_rule nat WEAVE -s $1 ! -o $BRIDGE -j MASQUERADE fi ;; stop) @@ -346,13 +354,11 @@ case "$COMMAND" in fi docker rm -f weave >/dev/null 2>&1 || true check_command_and_run conntrack conntrack -D -p udp --dport 6783 >/dev/null 2>&1 || true - stop_masquerading ;; reset) docker kill weave >/dev/null 2>&1 || true docker rm -f weave >/dev/null 2>&1 || true check_command_and_run conntrack conntrack -D -p udp --dport 6783 >/dev/null 2>&1 || true - stop_masquerading destroy_bridge ;; *) From 85787f2d4e266bfd8aa32ceb2f8dd286c4698882 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 10:21:02 +0100 Subject: [PATCH 3/9] fix 'weave start' post rebase --- weave | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/weave b/weave index a3231e67..1df5e49b 100755 --- a/weave +++ b/weave @@ -313,10 +313,9 @@ case "$COMMAND" in ;; start) [ $# -gt 1 ] || usage - IPADDR=$1 create_bridge CONTAINER=$(docker start $2) - with_container_netns attach $CONTAINER $IPADDR $CONTAINER_IFNAME + with_container_netns $CONTAINER attach $1 echo $CONTAINER ;; attach) From 7a53efe66c4be087142906c593ff4e131f792d95 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 11:27:32 +0100 Subject: [PATCH 4/9] get error handling right --- weave | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/weave b/weave index 1df5e49b..eb62ad64 100755 --- a/weave +++ b/weave @@ -169,10 +169,10 @@ launch() { return 1 fi - ip link set $LOCAL_IFNAME master $BRIDGE - ip link set $LOCAL_IFNAME up - ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME - ip netns exec $NETNS ip link set $CONTAINER_IFNAME up + ip link set $LOCAL_IFNAME master $BRIDGE && + ip link set $LOCAL_IFNAME up && + ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME && + ip netns exec $NETNS ip link set $CONTAINER_IFNAME up && check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null } From 579f3469c8d6cad7db75db4d5b2e9614eec3dec3 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 12:30:42 +0100 Subject: [PATCH 5/9] simplify code for attach --- weave | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/weave b/weave index eb62ad64..796effef 100755 --- a/weave +++ b/weave @@ -196,29 +196,26 @@ attach() { return 1 fi - if ! ip link set $LOCAL_IFNAME master $BRIDGE || - ! ip link set $LOCAL_IFNAME up || - ! ip link set $GUEST_IFNAME netns $NETNS ; then + if ! ip link set $GUEST_IFNAME netns $NETNS ; then # failed before we assigned the veth to the container's # namespace - delete it ip link del $LOCAL_IFNAME type veth || true return 1 fi - if ! ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME || + if ! ip link set $LOCAL_IFNAME master $BRIDGE || + ! ip link set $LOCAL_IFNAME up || + ! ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME || ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME || ! ip netns exec $NETNS ip link set $CONTAINER_IFNAME up || ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $CONTAINER_IFNAME tx off >/dev/null ; then return 1 fi - if ip netns exec $NETNS ip route show | grep '^224\.0\.0\.0/4' >/dev/null ; then - # There's a multicast route already present, leave it alone. - return 0 - fi - # Route multicast packets across the weave network. - ip netns exec $NETNS ip route add 224.0.0.0/4 dev $CONTAINER_IFNAME + if ! ip netns exec $NETNS ip route show | grep '^224\.0\.0\.0/4' >/dev/null ; then + ip netns exec $NETNS ip route add 224.0.0.0/4 dev $CONTAINER_IFNAME + fi } detach() { From dba87601872fbefa02fe0bdf7c239ffc7b373f85 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 13:43:15 +0100 Subject: [PATCH 6/9] update README --- README.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c0560220..9886e4e7 100644 --- a/README.md +++ b/README.md @@ -55,20 +55,20 @@ containers, one on each host. On $HOST1 run (as root) - host1# weave launch 10.0.0.1/16 + host1# weave launch host1# C=$(weave run 10.0.1.1/24 -t -i ubuntu) The first line starts the weave router, in a container. This needs to -be done once on each host. We tell weave that its IP address should -be 10.0.0.1, and that the weave network is on 10.0.0.0/16. +be done once on each host. The second line runs our application container. We give it an IP -address and network (a subnet of the weave network). `weave run` -invokes `docker run -d` with all the parameters following the IP -address and network. So we could be launching any container this way; -here we just take a stock ubuntu container and launch a shell in -it. There's also a `weave start` command, which invokes `docker start` -for starting existing containers. +address and network, in +[CIDR notation](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#CIDR_notation). +`weave run` invokes `docker run -d` with all the parameters following +the IP address and network. So we could be launching any container +this way; here we just take a stock ubuntu container and launch a +shell in it. There's also a `weave start` command, which invokes +`docker start` for starting existing containers. If our application consists of more than one container on this host we simply launch them with a variation on that second line. @@ -81,15 +81,14 @@ unique. We repeat similar steps on $HOST2... - host2# weave launch 10.0.0.2/16 $HOST1 + host2# weave launch $HOST1 host2# C=$(weave run 10.0.1.2/24 -t -i ubuntu) -The only difference, apart from the choice of IP addresses for the -weave router and the application container, is that we tell our weave -that it should peer with the weave on $HOST1 (specified as the IP -address or hostname by which $HOST2 can reach it). NB: if there is a -firewall between $HOST1 and $HOST2, you must open port 6783 for TCP -and UDP. +The only difference, apart from the choice of IP address for the +application container, is that we tell our weave that it should peer +with the weave on $HOST1 (specified as the IP address or hostname by +which $HOST2 can reach it). NB: if there is a firewall between $HOST1 +and $HOST2, you must open port 6783 for TCP and UDP. Note that we could instead have told the weave on $HOST1 to connect to $HOST2, or told both about each other. Order does not matter here; From d3ba6746a56c236a585caab110925ef6a39d69ea Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 24 Oct 2014 15:36:54 +0100 Subject: [PATCH 7/9] backward compatibility --- weave | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/weave b/weave index 796effef..cc8f9986 100755 --- a/weave +++ b/weave @@ -264,6 +264,12 @@ case "$COMMAND" in # (eth0, wlan0, etc) so the name becomes stable across host # restarts. MACADDR=`cat /sys/class/net/$BRIDGE/address` + # Backward compatibility... Note that the regexp here is far + # from precise, but good enough. + if echo "$1" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$' >/dev/null ; then + echo "WARNING: $1 parameter swallowed; 'weave launch' no longer takes a CIDR as the first parameter" >&2 + shift 1 + fi # Set WEAVE_DOCKER_ARGS in the environment in order to supply # additional parameters, such as resource limits, to docker # when launching the weave container. From fad584694629ef553f845c13ed159b97f2797a3c Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sun, 26 Oct 2014 12:13:28 +0000 Subject: [PATCH 8/9] refactor: extract basic container/bridge connection establishment Note that we do not mark the interface in the container as 'up' here since some callers, in particular 'attach', add CIDRs to the interface, and some programs rely on 'up' implying 'has IP address'. --- weave | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/weave b/weave index a4994867..8a1bbcf0 100755 --- a/weave +++ b/weave @@ -175,7 +175,7 @@ with_container_netns () { return $STATUS } -launch() { +connect_container_to_bridge() { if ! ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU ; then return 1 fi @@ -188,7 +188,12 @@ launch() { ip link set $LOCAL_IFNAME master $BRIDGE && ip link set $LOCAL_IFNAME up && - ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME && + ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME + } + + +launch() { + connect_container_to_bridge && ip netns exec $NETNS ip link set $CONTAINER_IFNAME up && check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null } @@ -209,20 +214,7 @@ attach() { return 0 fi - if ! ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU ; then - return 1 - fi - - if ! ip link set $GUEST_IFNAME netns $NETNS ; then - # failed before we assigned the veth to the container's - # namespace - delete it - ip link del $LOCAL_IFNAME type veth || true - return 1 - fi - - if ! ip link set $LOCAL_IFNAME master $BRIDGE || - ! ip link set $LOCAL_IFNAME up || - ! ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME || + if ! connect_container_to_bridge || ! ip netns exec $NETNS ip addr add $1 dev $CONTAINER_IFNAME || ! ip netns exec $NETNS ip link set $CONTAINER_IFNAME up || ! check_command_and_run ethtool ip netns exec $NETNS ethtool -K $CONTAINER_IFNAME tx off >/dev/null ; then From 4b8988346fdae9c7a7c461b8bc58daeb0944c715 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 27 Oct 2014 14:08:54 +0000 Subject: [PATCH 9/9] cosmetic --- weave | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/weave b/weave index 8a1bbcf0..b0841021 100755 --- a/weave +++ b/weave @@ -187,15 +187,15 @@ connect_container_to_bridge() { fi ip link set $LOCAL_IFNAME master $BRIDGE && - ip link set $LOCAL_IFNAME up && - ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME - } + ip link set $LOCAL_IFNAME up && + ip netns exec $NETNS ip link set $GUEST_IFNAME name $CONTAINER_IFNAME +} launch() { connect_container_to_bridge && - ip netns exec $NETNS ip link set $CONTAINER_IFNAME up && - check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null + ip netns exec $NETNS ip link set $CONTAINER_IFNAME up && + check_command_and_run ethtool ip netns exec $NETNS ethtool -K eth0 tx off >/dev/null } attach() {