github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/drivers/bridge/setup_device.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package bridge
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/docker/docker/libnetwork/netutils"
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/vishvananda/netlink"
    14  )
    15  
    16  // SetupDevice create a new bridge interface/
    17  func setupDevice(config *networkConfiguration, i *bridgeInterface) error {
    18  	// We only attempt to create the bridge when the requested device name is
    19  	// the default one. The default bridge name can be overridden with the
    20  	// DOCKER_TEST_CREATE_DEFAULT_BRIDGE env var. It should be used only for
    21  	// test purpose.
    22  	var defaultBridgeName string
    23  	if defaultBridgeName = os.Getenv("DOCKER_TEST_CREATE_DEFAULT_BRIDGE"); defaultBridgeName == "" {
    24  		defaultBridgeName = DefaultBridgeName
    25  	}
    26  	if config.BridgeName != defaultBridgeName && config.DefaultBridge {
    27  		return NonDefaultBridgeExistError(config.BridgeName)
    28  	}
    29  
    30  	// Set the bridgeInterface netlink.Bridge.
    31  	i.Link = &netlink.Bridge{
    32  		LinkAttrs: netlink.LinkAttrs{
    33  			Name: config.BridgeName,
    34  		},
    35  	}
    36  
    37  	// Set the bridge's MAC address. Requires kernel version 3.3 or up.
    38  	hwAddr := netutils.GenerateRandomMAC()
    39  	i.Link.Attrs().HardwareAddr = hwAddr
    40  	logrus.Debugf("Setting bridge mac address to %s", hwAddr)
    41  
    42  	if err := i.nlh.LinkAdd(i.Link); err != nil {
    43  		logrus.WithError(err).Errorf("Failed to create bridge %s via netlink", config.BridgeName)
    44  		return err
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func setupDefaultSysctl(config *networkConfiguration, i *bridgeInterface) error {
    51  	// Disable IPv6 router advertisements originating on the bridge
    52  	sysPath := filepath.Join("/proc/sys/net/ipv6/conf/", config.BridgeName, "accept_ra")
    53  	if _, err := os.Stat(sysPath); err != nil {
    54  		logrus.
    55  			WithField("bridge", config.BridgeName).
    56  			WithField("syspath", sysPath).
    57  			Info("failed to read ipv6 net.ipv6.conf.<bridge>.accept_ra")
    58  		return nil
    59  	}
    60  	if err := os.WriteFile(sysPath, []byte{'0', '\n'}, 0644); err != nil {
    61  		logrus.WithError(err).Warn("unable to disable IPv6 router advertisement")
    62  	}
    63  	return nil
    64  }
    65  
    66  // SetupDeviceUp ups the given bridge interface.
    67  func setupDeviceUp(config *networkConfiguration, i *bridgeInterface) error {
    68  	err := i.nlh.LinkSetUp(i.Link)
    69  	if err != nil {
    70  		return fmt.Errorf("Failed to set link up for %s: %v", config.BridgeName, err)
    71  	}
    72  
    73  	// Attempt to update the bridge interface to refresh the flags status,
    74  	// ignoring any failure to do so.
    75  	if lnk, err := i.nlh.LinkByName(config.BridgeName); err == nil {
    76  		i.Link = lnk
    77  	} else {
    78  		logrus.Warnf("Failed to retrieve link for interface (%s): %v", config.BridgeName, err)
    79  	}
    80  	return nil
    81  }