enable hairpin mode on virtual interface bridge port
This is to support being able to DNAT/MASQ traffic from a container back into itself (dotcloud/docker#4442) Docker-DCO-1.1-Signed-off-by: Patrick Hemmer <patrick.hemmer@gmail.com> (github: phemmer)
This commit is contained in:
parent
ddb4f8a12f
commit
2d4a713602
|
@ -7,6 +7,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -1204,6 +1205,28 @@ func SetMacAddress(name, addr string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetHairpinMode(iface *net.Interface, enabled bool) error {
|
||||||
|
sysPath := filepath.Join("/sys/class/net", iface.Name, "brport/hairpin_mode")
|
||||||
|
|
||||||
|
sysFile, err := os.OpenFile(sysPath, os.O_WRONLY, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer sysFile.Close()
|
||||||
|
|
||||||
|
var writeVal []byte
|
||||||
|
if enabled {
|
||||||
|
writeVal = []byte("1")
|
||||||
|
} else {
|
||||||
|
writeVal = []byte("0")
|
||||||
|
}
|
||||||
|
if _, err := sysFile.Write(writeVal); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func ChangeName(iface *net.Interface, newName string) error {
|
func ChangeName(iface *net.Interface, newName string) error {
|
||||||
if len(newName) >= IFNAMSIZ {
|
if len(newName) >= IFNAMSIZ {
|
||||||
return fmt.Errorf("Interface name %s too long", newName)
|
return fmt.Errorf("Interface name %s too long", newName)
|
||||||
|
@ -1224,5 +1247,6 @@ func ChangeName(iface *net.Interface, newName string) error {
|
||||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&data[0]))); errno != 0 {
|
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&data[0]))); errno != 0 {
|
||||||
return errno
|
return errno
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,3 +95,11 @@ func SetMtu(name string, mtu int) error {
|
||||||
}
|
}
|
||||||
return netlink.NetworkSetMTU(iface, mtu)
|
return netlink.NetworkSetMTU(iface, mtu)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetHairpinMode(name string, enabled bool) error {
|
||||||
|
iface, err := net.InterfaceByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return netlink.SetHairpinMode(iface, enabled)
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ func (v *Veth) Create(n *Network, nspid int, networkState *NetworkState) error {
|
||||||
if err := SetMtu(name1, n.Mtu); err != nil {
|
if err := SetMtu(name1, n.Mtu); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := SetHairpinMode(name1, true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := InterfaceUp(name1); err != nil {
|
if err := InterfaceUp(name1); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue