Skip to content

Commit e578e95

Browse files
committed
Merge pull request #281 from mavenugo/hairpin
enable hairpin mode on the bridge port & fix iptables rule
2 parents 90638ec + 9548cbe commit e578e95

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

drivers/bridge/bridge.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,13 @@ func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointIn
516516
return err
517517
}
518518

519+
if !config.EnableUserlandProxy {
520+
err = netlink.LinkSetHairpin(host, true)
521+
if err != nil {
522+
return err
523+
}
524+
}
525+
519526
// v4 address for the sandbox side pipe interface
520527
ip4, err := ipAllocator.RequestIP(n.bridge.bridgeIPv4, nil)
521528
if err != nil {

iptables/iptables.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ var (
4444

4545
// Chain defines the iptables chain.
4646
type Chain struct {
47-
Name string
48-
Bridge string
49-
Table Table
47+
Name string
48+
Bridge string
49+
Table Table
50+
HairpinMode bool
5051
}
5152

5253
// ChainError is returned to represent errors during ip table operation.
@@ -75,9 +76,10 @@ func initCheck() error {
7576
// NewChain adds a new chain to ip table.
7677
func NewChain(name, bridge string, table Table, hairpinMode bool) (*Chain, error) {
7778
c := &Chain{
78-
Name: name,
79-
Bridge: bridge,
80-
Table: table,
79+
Name: name,
80+
Bridge: bridge,
81+
Table: table,
82+
HairpinMode: hairpinMode,
8183
}
8284

8385
if string(c.Table) == "" {
@@ -151,12 +153,16 @@ func (c *Chain) Forward(action Action, ip net.IP, port int, proto, destAddr stri
151153
// value" by both iptables and ip6tables.
152154
daddr = "0/0"
153155
}
154-
if output, err := Raw("-t", string(Nat), string(action), c.Name,
156+
args := []string{"-t", string(Nat), string(action), c.Name,
155157
"-p", proto,
156158
"-d", daddr,
157159
"--dport", strconv.Itoa(port),
158160
"-j", "DNAT",
159-
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))); err != nil {
161+
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
162+
if !c.HairpinMode {
163+
args = append(args, "!", "-i", c.Bridge)
164+
}
165+
if output, err := Raw(args...); err != nil {
160166
return err
161167
} else if len(output) != 0 {
162168
return ChainError{Chain: "FORWARD", Output: output}

iptables/iptables_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestForward(t *testing.T) {
4848
"--dport", strconv.Itoa(port),
4949
"-j", "DNAT",
5050
"--to-destination", dstAddr + ":" + strconv.Itoa(dstPort),
51+
"!", "-i", natChain.Bridge,
5152
}
5253

5354
if !Exists(natChain.Table, natChain.Name, dnatRule...) {

0 commit comments

Comments
 (0)