2015-02-19 17:21:42 -08:00
|
|
|
package bridge
|
|
|
|
|
2015-02-19 22:21:05 -08:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
)
|
2015-02-19 17:21:42 -08:00
|
|
|
|
2015-02-22 17:24:22 -08:00
|
|
|
const (
|
|
|
|
NetworkType = "simplebridge"
|
|
|
|
VethPrefix = "veth"
|
|
|
|
)
|
2015-02-19 17:21:42 -08:00
|
|
|
|
2015-02-22 17:24:22 -08:00
|
|
|
type Configuration struct {
|
|
|
|
BridgeName string
|
|
|
|
AddressIPv4 *net.IPNet
|
2015-02-22 21:32:48 -08:00
|
|
|
FixedCIDR *net.IPNet
|
|
|
|
FixedCIDRv6 *net.IPNet
|
2015-02-22 17:24:22 -08:00
|
|
|
EnableIPv6 bool
|
|
|
|
EnableIPTables bool
|
|
|
|
EnableIPForwarding bool
|
2015-02-19 17:21:42 -08:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:21:05 -08:00
|
|
|
func init() {
|
2015-02-22 17:24:22 -08:00
|
|
|
libnetwork.RegisterNetworkType(NetworkType, Create, &Configuration{})
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:17:12 -08:00
|
|
|
func Create(name string, config *Configuration) (libnetwork.Network, error) {
|
2015-02-22 17:24:22 -08:00
|
|
|
bridgeIntfc := NewInterface(config)
|
|
|
|
bridgeSetup := NewBridgeSetup(bridgeIntfc)
|
|
|
|
|
|
|
|
// If the bridge interface doesn't exist, we need to start the setup steps
|
|
|
|
// by creating a new device and assigning it an IPv4 address.
|
|
|
|
bridgeAlreadyExists := bridgeIntfc.Exists()
|
|
|
|
if !bridgeAlreadyExists {
|
|
|
|
bridgeSetup.QueueStep(SetupDevice)
|
|
|
|
bridgeSetup.QueueStep(SetupBridgeIPv4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conditionnally queue setup steps depending on configuration values.
|
2015-02-22 17:58:52 -08:00
|
|
|
for _, step := range []struct {
|
2015-02-22 17:24:22 -08:00
|
|
|
Condition bool
|
|
|
|
Fn SetupStep
|
|
|
|
}{
|
|
|
|
// Enable IPv6 on the bridge if required. We do this even for a
|
|
|
|
// previously existing bridge, as it may be here from a previous
|
|
|
|
// installation where IPv6 wasn't supported yet and needs to be
|
|
|
|
// assigned an IPv6 link-local address.
|
|
|
|
{config.EnableIPv6, SetupBridgeIPv6},
|
|
|
|
|
|
|
|
// We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
|
|
|
|
// the case of a previously existing device.
|
|
|
|
{bridgeAlreadyExists, SetupVerifyConfiguredAddresses},
|
|
|
|
|
|
|
|
// Setup the bridge to allocate containers IPv4 addresses in the
|
|
|
|
// specified subnet.
|
2015-02-22 21:32:48 -08:00
|
|
|
{config.FixedCIDR != nil, SetupFixedCIDRv4},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// Setup the bridge to allocate containers global IPv6 addresses in the
|
|
|
|
// specified subnet.
|
2015-02-22 21:32:48 -08:00
|
|
|
{config.FixedCIDRv6 != nil, SetupFixedCIDRv6},
|
2015-02-22 17:24:22 -08:00
|
|
|
|
|
|
|
// Setup IPTables.
|
|
|
|
{config.EnableIPTables, SetupIPTables},
|
|
|
|
|
|
|
|
// Setup IP forwarding.
|
|
|
|
{config.EnableIPForwarding, SetupIPForwarding},
|
2015-02-22 17:58:52 -08:00
|
|
|
} {
|
2015-02-22 17:24:22 -08:00
|
|
|
if step.Condition {
|
|
|
|
bridgeSetup.QueueStep(step.Fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the prepared list of steps, and abort at the first error.
|
|
|
|
bridgeSetup.QueueStep(SetupDeviceUp)
|
|
|
|
if err := bridgeSetup.Apply(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:17:12 -08:00
|
|
|
return &bridgeNetwork{NetworkName: name, Config: *config}, nil
|
2015-02-22 17:24:22 -08:00
|
|
|
}
|