From 36303270c6accbed3aa73e43417262af06ae7749 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Tue, 2 Jun 2015 13:20:15 -0700 Subject: [PATCH] Introduce UnsetGateway(IPv6) methods Sandbox needs unset gateway methods to cleanup gateway settings to enable smooth transition of the sandbox between endpoints. Signed-off-by: Jana Radhakrishnan --- libnetwork/sandbox/configure_linux.go | 12 +++++-- libnetwork/sandbox/namespace_linux.go | 50 +++++++++++++++++++++++++-- libnetwork/sandbox/sandbox.go | 6 ++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/libnetwork/sandbox/configure_linux.go b/libnetwork/sandbox/configure_linux.go index 6f106e8647..4022170820 100644 --- a/libnetwork/sandbox/configure_linux.go +++ b/libnetwork/sandbox/configure_linux.go @@ -30,7 +30,7 @@ func configureInterface(iface netlink.Link, settings *Interface) error { return nil } -func programGateway(path string, gw net.IP) error { +func programGateway(path string, gw net.IP, isAdd bool) error { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -57,7 +57,15 @@ func programGateway(path string, gw net.IP) error { return fmt.Errorf("route for the gateway could not be found: %v", err) } - return netlink.RouteAdd(&netlink.Route{ + if isAdd { + return netlink.RouteAdd(&netlink.Route{ + Scope: netlink.SCOPE_UNIVERSE, + LinkIndex: gwRoutes[0].LinkIndex, + Gw: gw, + }) + } + + return netlink.RouteDel(&netlink.Route{ Scope: netlink.SCOPE_UNIVERSE, LinkIndex: gwRoutes[0].LinkIndex, Gw: gw, diff --git a/libnetwork/sandbox/namespace_linux.go b/libnetwork/sandbox/namespace_linux.go index 3db3a8ee0f..c368d54f41 100644 --- a/libnetwork/sandbox/namespace_linux.go +++ b/libnetwork/sandbox/namespace_linux.go @@ -308,26 +308,72 @@ func (n *networkNamespace) AddInterface(i *Interface) error { } func (n *networkNamespace) SetGateway(gw net.IP) error { + // Silently return if the gateway is empty if len(gw) == 0 { return nil } - err := programGateway(n.path, gw) + err := programGateway(n.path, gw, true) if err == nil { + n.Lock() n.sinfo.Gateway = gw + n.Unlock() + } + + return err +} + +func (n *networkNamespace) UnsetGateway() error { + n.Lock() + gw := n.sinfo.Gateway + n.Unlock() + + // Silently return if the gateway is empty + if len(gw) == 0 { + return nil + } + + err := programGateway(n.path, gw, false) + if err == nil { + n.Lock() + n.sinfo.Gateway = net.IP{} + n.Unlock() } return err } func (n *networkNamespace) SetGatewayIPv6(gw net.IP) error { + // Silently return if the gateway is empty if len(gw) == 0 { return nil } - err := programGateway(n.path, gw) + err := programGateway(n.path, gw, true) if err == nil { + n.Lock() n.sinfo.GatewayIPv6 = gw + n.Unlock() + } + + return err +} + +func (n *networkNamespace) UnsetGatewayIPv6() error { + n.Lock() + gw := n.sinfo.GatewayIPv6 + n.Unlock() + + // Silently return if the gateway is empty + if len(gw) == 0 { + return nil + } + + err := programGateway(n.path, gw, false) + if err == nil { + n.Lock() + n.sinfo.GatewayIPv6 = net.IP{} + n.Unlock() } return err diff --git a/libnetwork/sandbox/sandbox.go b/libnetwork/sandbox/sandbox.go index 0f036034ab..e52fba031c 100644 --- a/libnetwork/sandbox/sandbox.go +++ b/libnetwork/sandbox/sandbox.go @@ -35,6 +35,12 @@ type Sandbox interface { // Set default IPv6 gateway for the sandbox SetGatewayIPv6(gw net.IP) error + // Unset the previously set default IPv4 gateway in the sandbox + UnsetGateway() error + + // Unset the previously set default IPv6 gateway in the sandbox + UnsetGatewayIPv6() error + // Add a static route to the sandbox. AddStaticRoute(*types.StaticRoute) error