2015-02-23 01:24:22 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-02-23 01:24:22 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2015-02-23 05:11:12 +00:00
|
|
|
// SetupDevice create a new bridge interface/
|
2015-05-06 05:51:26 +00:00
|
|
|
func setupDevice(config *NetworkConfiguration, i *bridgeInterface) error {
|
2015-02-23 01:24:22 +00:00
|
|
|
// We only attempt to create the bridge when the requested device name is
|
|
|
|
// the default one.
|
2015-04-15 05:25:42 +00:00
|
|
|
if config.BridgeName != DefaultBridgeName && !config.AllowNonDefaultBridge {
|
2015-04-17 02:47:12 +00:00
|
|
|
return NonDefaultBridgeExistError(config.BridgeName)
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 21:25:43 +00:00
|
|
|
// Set the bridgeInterface netlink.Bridge.
|
2015-02-23 01:58:52 +00:00
|
|
|
i.Link = &netlink.Bridge{
|
2015-02-23 01:24:22 +00:00
|
|
|
LinkAttrs: netlink.LinkAttrs{
|
2015-04-15 05:25:42 +00:00
|
|
|
Name: config.BridgeName,
|
2015-02-23 01:24:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only set the bridge's MAC address if the kernel version is > 3.3, as it
|
|
|
|
// was not supported before that.
|
|
|
|
kv, err := kernel.GetKernelVersion()
|
|
|
|
if err == nil && (kv.Kernel >= 3 && kv.Major >= 3) {
|
2015-04-13 18:40:42 +00:00
|
|
|
i.Link.Attrs().HardwareAddr = netutils.GenerateRandomMAC()
|
2015-02-23 01:58:52 +00:00
|
|
|
log.Debugf("Setting bridge mac address to %s", i.Link.Attrs().HardwareAddr)
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 05:11:12 +00:00
|
|
|
// Call out to netlink to create the device.
|
2015-02-23 01:58:52 +00:00
|
|
|
return netlink.LinkAdd(i.Link)
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 05:11:12 +00:00
|
|
|
// SetupDeviceUp ups the given bridge interface.
|
2015-05-06 05:51:26 +00:00
|
|
|
func setupDeviceUp(config *NetworkConfiguration, i *bridgeInterface) error {
|
2015-02-24 19:19:00 +00:00
|
|
|
err := netlink.LinkSetUp(i.Link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to update the bridge interface to refresh the flags status,
|
|
|
|
// ignoring any failure to do so.
|
2015-04-15 05:25:42 +00:00
|
|
|
if lnk, err := netlink.LinkByName(config.BridgeName); err == nil {
|
2015-02-24 19:19:00 +00:00
|
|
|
i.Link = lnk
|
|
|
|
}
|
|
|
|
return nil
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|