Skip to content

Commit 43f71fb

Browse files
committed
Bridge - skip gateway allocation if no gateway is needed
An "--internal" bridge network will never set up a default route and, with "-o com.docker.network.bridge.inhibit_ipv4", no Gateway address will be assigned to the bridge. So, implement the SkipGwAlloc interface in the bridge driver, and use it to to indicate that no Gateway address is required in this specific case. Signed-off-by: Rob Murray <[email protected]>
1 parent 38e76eb commit 43f71fb

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

integration/network/bridge/bridge_linux_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
ctr "github.com/docker/docker/integration/internal/container"
1515
"github.com/docker/docker/integration/internal/network"
1616
"github.com/docker/docker/internal/testutils/networking"
17+
"github.com/docker/docker/libnetwork/drivers/bridge"
1718
"github.com/docker/docker/testutil"
1819
"github.com/docker/docker/testutil/daemon"
1920
"gotest.tools/v3/assert"
@@ -315,3 +316,37 @@ func TestFilterForwardPolicy(t *testing.T) {
315316
})
316317
}
317318
}
319+
320+
// TestPointToPoint checks that a "/31" --internal network with inhibit_ipv4
321+
// has two addresses available for containers (no address is reserved for a
322+
// gateway, because it won't be used).
323+
func TestPointToPoint(t *testing.T) {
324+
ctx := setupTest(t)
325+
apiClient := testEnv.APIClient()
326+
327+
const netName = "testp2pbridge"
328+
network.CreateNoError(ctx, t, apiClient, netName,
329+
network.WithIPAM("192.168.135.0/31", ""),
330+
network.WithInternal(),
331+
network.WithOption(bridge.InhibitIPv4, "true"),
332+
)
333+
defer network.RemoveNoError(ctx, t, apiClient, netName)
334+
335+
const ctrName = "ctr1"
336+
id := ctr.Run(ctx, t, apiClient,
337+
ctr.WithNetworkMode(netName),
338+
ctr.WithName(ctrName),
339+
)
340+
defer apiClient.ContainerRemove(ctx, id, containertypes.RemoveOptions{Force: true})
341+
342+
attachCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
343+
defer cancel()
344+
res := ctr.RunAttach(attachCtx, t, apiClient,
345+
ctr.WithCmd([]string{"ping", "-c1", "-W3", ctrName}...),
346+
ctr.WithNetworkMode(netName),
347+
)
348+
defer apiClient.ContainerRemove(ctx, res.ContainerID, containertypes.RemoveOptions{Force: true})
349+
assert.Check(t, is.Equal(res.ExitCode, 0))
350+
assert.Check(t, is.Equal(res.Stderr.Len(), 0))
351+
assert.Check(t, is.Contains(res.Stdout.String(), "1 packets transmitted, 1 packets received"))
352+
}

libnetwork/drivers/bridge/bridge_linux.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,19 @@ func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (s
745745
return "", nil
746746
}
747747

748+
func (d *driver) GetSkipGwAlloc(opts options.Generic) (ipv4, ipv6 bool, _ error) {
749+
// The network doesn't exist yet, so use a dummy id that's long enough to be
750+
// truncated to a short-id (12 characters) and used in the bridge device name.
751+
cfg, err := parseNetworkOptions("dummyNetworkId", opts)
752+
if err != nil {
753+
return false, false, err
754+
}
755+
// cfg.InhibitIPv4 means no gateway address will be assigned to the bridge, if
756+
// the network is also cfg.Internal, there will not be a default route to use
757+
// the gateway address either.
758+
return cfg.InhibitIPv4 && cfg.Internal, false, nil
759+
}
760+
748761
// CreateNetwork creates a new network using the bridge driver.
749762
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
750763
// Sanity checks

0 commit comments

Comments
 (0)