2015-02-23 01:24:22 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2015-07-30 13:02:23 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-07-30 00:23:55 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-02-23 01:24:22 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2015-07-30 13:02:23 +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-22 17:56:36 +00:00
|
|
|
func setupDevice(config *networkConfiguration, i *bridgeInterface) error {
|
2015-06-18 22:59:49 +00:00
|
|
|
var setMac bool
|
|
|
|
|
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-09-25 16:02:18 +00:00
|
|
|
if config.BridgeName != DefaultBridgeName && config.DefaultBridge {
|
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()
|
2015-07-30 00:23:55 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to check kernel versions: %v. Will not assign a MAC address to the bridge interface", err)
|
|
|
|
} else {
|
|
|
|
setMac = kv.Kernel > 3 || (kv.Kernel == 3 && kv.Major >= 3)
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 13:02:23 +00:00
|
|
|
if err = netlink.LinkAdd(i.Link); err != nil {
|
|
|
|
logrus.Debugf("Failed to create bridge %s via netlink. Trying ioctl", config.BridgeName)
|
|
|
|
return ioctlCreateBridge(config.BridgeName, setMac)
|
|
|
|
}
|
|
|
|
|
|
|
|
if setMac {
|
|
|
|
hwAddr := netutils.GenerateRandomMAC()
|
|
|
|
if err = netlink.LinkSetHardwareAddr(i.Link, hwAddr); err != nil {
|
|
|
|
return fmt.Errorf("failed to set bridge mac-address %s : %s", hwAddr, err.Error())
|
|
|
|
}
|
|
|
|
logrus.Debugf("Setting bridge mac address to %s", hwAddr)
|
|
|
|
}
|
|
|
|
return err
|
2015-02-23 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 05:11:12 +00:00
|
|
|
// SetupDeviceUp ups the given bridge interface.
|
2015-05-22 17:56:36 +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
|
|
|
}
|