Skip to content

Commit

Permalink
Use sysfs to set hairpin mode
Browse files Browse the repository at this point in the history
Set the hairpin mode using the sysfs interface which
looks like it is working all the way to the oldest
of RHEL6.6 kernels.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
  • Loading branch information
mrjana committed Jul 23, 2015
1 parent 759cf09 commit 66889d3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ check-format:

run-tests:
@echo "Running tests... "
@sudo mount -o rw,remount -t sysfs sysfs /sys
@echo "mode: count" > coverage.coverprofile
@for dir in $$(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d); do \
if ls $$dir/*.go &> /dev/null; then \
Expand All @@ -49,15 +50,15 @@ run-tests:
ret=$$? ;\
if [ $$ret -ne 0 ]; then exit $$ret; fi ;\
popd &> /dev/null; \
if [ -f $$dir/profile.tmp ]; then \
cat $$dir/profile.tmp | tail -n +2 >> coverage.coverprofile ; \
if [ -f $$dir/profile.tmp ]; then \
cat $$dir/profile.tmp | tail -n +2 >> coverage.coverprofile ; \
rm $$dir/profile.tmp ; \
fi ; \
fi ; \
fi ; \
fi ; \
done
@echo "Done running tests"

check-local: check-format check-code run-tests
check-local: check-format check-code run-tests

install-deps:
apt-get update && apt-get -y install iptables
Expand Down
7 changes: 4 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
machine:
services:
- docker

post:
- sudo mount -o rw,remount -t sysfs sysfs /sys

dependencies:
override:
- echo "Nothing to install"

test:
override:
- make circle-ci

- sudo make circle-ci
21 changes: 20 additions & 1 deletion drivers/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package bridge
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os/exec"
"path/filepath"
"strconv"
"sync"

Expand Down Expand Up @@ -772,6 +774,23 @@ func addToBridge(ifaceName, bridgeName string) error {
return ioctlAddToBridge(iface, master)
}

func setHairpinMode(ifaceName string, enable bool) error {
path := filepath.Join("/sys/class/net", ifaceName, "brport/hairpin_mode")

var val []byte
if enable {
val = []byte{'1', '\n'}
} else {
val = []byte{'0', '\n'}
}

if err := ioutil.WriteFile(path, val, 0644); err != nil {
return fmt.Errorf("unable to set hairpin mode on %s: %v", ifaceName, err)
}

return nil
}

func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
var (
ipv6Addr *net.IPNet
Expand Down Expand Up @@ -902,7 +921,7 @@ func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointIn
}

if !config.EnableUserlandProxy {
err = netlink.LinkSetHairpin(host, true)
err = setHairpinMode(hostIfName, true)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions netutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func SetupTestNetNS(t *testing.T) func() {
t.Fatal("Failed to open netns file")
}

if err := syscall.Unmount("/sys", syscall.MNT_DETACH); err != nil {
t.Fatalf("unmount /sys failed: %v", err)
}

if err := syscall.Mount("sysfs", "/sys", "sysfs", 0, ""); err != nil {
t.Fatalf("remount sysfs within netns failed: %v", err)
}

return func() {
if err := syscall.Close(fd); err != nil {
t.Logf("Warning: netns closing failed (%v)", err)
Expand Down

0 comments on commit 66889d3

Please sign in to comment.