Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding custom bridge support to docker run #6704

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding custom bridge support to network driver.
Docker-DCO-1.1-Signed-off-by: Vishnu Kannan <vishnuk@google.com> (github: vishh)
  • Loading branch information
vishh committed Jul 11, 2014
commit 3f4837d6a1cc3bcec65002a04bf0ae3376ad33c2
3 changes: 3 additions & 0 deletions daemon/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func populateCommand(c *Container, env []string) error {
case "host":
en.HostNetworking = true
case "bridge", "": // empty string to support existing containers
// TODO(vishh): Handle custom bridge here.
if !c.Config.NetworkDisabled {
network := c.NetworkSettings
en.Interface = &execdriver.NetworkInterface{
Expand Down Expand Up @@ -411,6 +412,7 @@ func (container *Container) allocateNetwork() error {
eng = container.daemon.eng
)

// TODO(vishh): Handle custom bridge.
job := eng.Job("allocate_interface", container.ID)
if env, err = job.Stdout.AddEnv(); err != nil {
return err
Expand Down Expand Up @@ -945,6 +947,7 @@ func (container *Container) initializeNetworking() error {
container.Config.NetworkDisabled = true
return container.buildHostnameAndHostsFiles("127.0.1.1")
} else {
// TODO(vishh): Handle custom bridge.
if err := container.allocateNetwork(); err != nil {
return err
}
Expand Down
39 changes: 26 additions & 13 deletions daemon/networkdriver/bridge/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
// Network interface represents the networking stack of a container
type networkInterface struct {
IP net.IP
Bridge net.IPNet
PortMappings []net.Addr // there are mappings to the host interfaces
}

Expand Down Expand Up @@ -317,32 +318,44 @@ func createBridgeIface(name string) error {
// Allocate a network interface
func Allocate(job *engine.Job) engine.Status {
var (
ip *net.IP
err error
id = job.Args[0]
requestedIP = net.ParseIP(job.Getenv("RequestedIP"))
ip *net.IP
err error
id = job.Args[0]
requestedIP = net.ParseIP(job.Getenv("RequestedIP"))
requestedBridge = job.Getenv("RequestedBridge")
)

bridgeNetworkInUse := bridgeNetwork
bridgeIfaceInUse := bridgeIface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please those 2 vars in the var block above ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if requestedBridge != "" && requestedBridge != bridgeIfaceInUse {
addr, err := networkdriver.GetIfaceAddr(requestedBridge)
if err != nil {
return job.Error(fmt.Errorf("Could not obtain details about bridge %s. Error: %s",
requestedBridge, err))
}
bridgeNetworkInUse = addr.(*net.IPNet)
bridgeIfaceInUse = requestedBridge
}
if requestedIP != nil {
ip, err = ipallocator.RequestIP(bridgeNetwork, &requestedIP)
ip, err = ipallocator.RequestIP(bridgeNetworkInUse, &requestedIP)
} else {
ip, err = ipallocator.RequestIP(bridgeNetwork, nil)
ip, err = ipallocator.RequestIP(bridgeNetworkInUse, nil)
}
if err != nil {
return job.Error(err)
}

out := engine.Env{}
out.Set("IP", ip.String())
out.Set("Mask", bridgeNetwork.Mask.String())
out.Set("Gateway", bridgeNetwork.IP.String())
out.Set("Bridge", bridgeIface)
out.Set("Mask", bridgeNetworkInUse.Mask.String())
out.Set("Gateway", bridgeNetworkInUse.IP.String())
out.Set("Bridge", bridgeIfaceInUse)

size, _ := bridgeNetwork.Mask.Size()
size, _ := bridgeNetworkInUse.Mask.Size()
out.SetInt("IPPrefixLen", size)

currentInterfaces.Set(id, &networkInterface{
IP: *ip,
IP: *ip,
Bridge: *bridgeNetworkInUse,
})

out.WriteTo(job.Stdout)
Expand All @@ -367,7 +380,7 @@ func Release(job *engine.Job) engine.Status {
}
}

if err := ipallocator.ReleaseIP(bridgeNetwork, &containerInterface.IP); err != nil {
if err := ipallocator.ReleaseIP(&containerInterface.Bridge, &containerInterface.IP); err != nil {
log.Printf("Unable to release ip %s\n", err)
}
return engine.StatusOK
Expand Down
13 changes: 13 additions & 0 deletions runconfig/hostconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ type DeviceMapping struct {
CgroupPermissions string
}

func (n NetworkMode) IsNonDefaultBridge() bool {
parts := strings.SplitN(string(n), ":", 2)
return len(parts) > 1 && parts[0] == "bridge"
}

func (n NetworkMode) GetNonDefaultBridge() string {
parts := strings.SplitN(string(n), ":", 2)
if (len(parts) > 1 && parts[0] == "bridge") {
return parts[1]
}
return ""
}

type HostConfig struct {
Binds []string
ContainerIDFile string
Expand Down