From e1c14b3ca245fd06ef538005cd3a250904be5b4c Mon Sep 17 00:00:00 2001 From: Arnaud Porterie Date: Wed, 11 Mar 2015 13:42:37 -0700 Subject: [PATCH] Hairpin NAT network configuration Offer the ability to enable hairpin NAT on a per network basis, while keeping it disable by default as it is unsupported by older kernel. Signed-off-by: Arnaud Porterie --- configs/network.go | 6 ++++++ network_linux.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/configs/network.go b/configs/network.go index 55443988..9d5ed7a6 100644 --- a/configs/network.go +++ b/configs/network.go @@ -42,6 +42,12 @@ type Network struct { // HostInterfaceName is a unique name of a veth pair that resides on in the host interface of the // container. HostInterfaceName string `json:"host_interface_name"` + + // HairpinMode specifies if hairpin NAT should be enabled on the virtual interface + // bridge port in the case of type veth + // Note: This is unsupported on some systems. + // Note: This does not apply to loopback interfaces. + HairpinMode bool `json:"hairpin_mode"` } // Routes can be specified to create entries in the route table as the container is started diff --git a/network_linux.go b/network_linux.go index 687c5e8f..46c606a2 100644 --- a/network_linux.go +++ b/network_linux.go @@ -135,6 +135,11 @@ func (v *veth) create(n *network, nspid int) (err error) { if err := netlink.NetworkSetMTU(host, n.Mtu); err != nil { return err } + if n.HairpinMode { + if err := netlink.SetHairpinMode(host, true); err != nil { + return err + } + } if err := netlink.NetworkLinkUp(host); err != nil { return err }