-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from icecrime/bridge_refactoring
Bridge refactoring - Step 1
- Loading branch information
Showing
13 changed files
with
441 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package bridge | ||
|
||
import "github.com/vishvananda/netlink" | ||
|
||
const ( | ||
DefaultBridgeName = "docker0" | ||
) | ||
|
||
type Interface struct { | ||
Config *Configuration | ||
Link netlink.Link | ||
} | ||
|
||
func NewInterface(config *Configuration) *Interface { | ||
i := &Interface{ | ||
Config: config, | ||
} | ||
|
||
// Initialize the bridge name to the default if unspecified. | ||
if i.Config.BridgeName == "" { | ||
i.Config.BridgeName = DefaultBridgeName | ||
} | ||
|
||
// Attempt to find an existing bridge named with the specified name. | ||
i.Link, _ = netlink.LinkByName(i.Config.BridgeName) | ||
return i | ||
} | ||
|
||
// Exists indicates if the existing bridge interface exists on the system. | ||
func (i *Interface) Exists() bool { | ||
return i.Link != nil | ||
} | ||
|
||
// Addresses returns a single IPv4 address and all IPv6 addresses for the | ||
// bridge interface. | ||
func (i *Interface) Addresses() (netlink.Addr, []netlink.Addr, error) { | ||
v4addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V4) | ||
if err != nil { | ||
return netlink.Addr{}, nil, err | ||
} | ||
|
||
v6addr, err := netlink.AddrList(i.Link, netlink.FAMILY_V6) | ||
if err != nil { | ||
return netlink.Addr{}, nil, err | ||
} | ||
|
||
return v4addr[0], v6addr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package bridge | ||
|
||
import "github.com/docker/libnetwork" | ||
|
||
type bridgeNetwork struct { | ||
Config Configuration | ||
} | ||
|
||
func (b *bridgeNetwork) Type() string { | ||
return NetworkType | ||
} | ||
|
||
func (b *bridgeNetwork) Link(name string) ([]*libnetwork.Interface, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package bridge | ||
|
||
type SetupStep func(*Interface) error | ||
|
||
type BridgeSetup struct { | ||
bridge *Interface | ||
steps []SetupStep | ||
} | ||
|
||
func NewBridgeSetup(i *Interface) *BridgeSetup { | ||
return &BridgeSetup{bridge: i} | ||
} | ||
|
||
func (b *BridgeSetup) Apply() error { | ||
for _, fn := range b.steps { | ||
if err := fn(b.bridge); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *BridgeSetup) QueueStep(step SetupStep) { | ||
b.steps = append(b.steps, step) | ||
} | ||
|
||
//---------------------------------------------------------------------------// | ||
|
||
func SetupIPTables(i *Interface) error { | ||
return nil | ||
} | ||
|
||
func SetupIPForwarding(i *Interface) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bridge | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/docker/docker/pkg/parsers/kernel" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// SetupDevice create a new bridge interface/ | ||
func SetupDevice(i *Interface) error { | ||
// We only attempt to create the bridge when the requested device name is | ||
// the default one. | ||
if i.Config.BridgeName != DefaultBridgeName { | ||
return fmt.Errorf("bridge device with non default name %q must be created manually", i.Config.BridgeName) | ||
} | ||
|
||
// Set the Interface netlink.Bridge. | ||
i.Link = &netlink.Bridge{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: i.Config.BridgeName, | ||
}, | ||
} | ||
|
||
// 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) { | ||
i.Link.Attrs().HardwareAddr = generateRandomMAC() | ||
log.Debugf("Setting bridge mac address to %s", i.Link.Attrs().HardwareAddr) | ||
} | ||
|
||
// Call out to netlink to create the device. | ||
return netlink.LinkAdd(i.Link) | ||
} | ||
|
||
// SetupDeviceUp ups the given bridge interface. | ||
func SetupDeviceUp(i *Interface) error { | ||
return netlink.LinkSetUp(i.Link) | ||
} | ||
|
||
func generateRandomMAC() net.HardwareAddr { | ||
hw := make(net.HardwareAddr, 6) | ||
for i := 0; i < 6; i++ { | ||
hw[i] = byte(rand.Intn(255)) | ||
} | ||
hw[0] &^= 0x1 // clear multicast bit | ||
hw[0] |= 0x2 // set local assignment bit (IEEE802) | ||
return hw | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package bridge | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
"github.com/docker/docker/daemon/networkdriver/ipallocator" | ||
) | ||
|
||
func SetupFixedCIDRv4(i *Interface) error { | ||
addrv4, _, err := i.Addresses() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debugf("Using IPv4 subnet: %v", i.Config.FixedCIDR) | ||
return ipallocator.RegisterSubnet(addrv4.IPNet, i.Config.FixedCIDR) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package bridge | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
"github.com/docker/docker/daemon/networkdriver/ipallocator" | ||
) | ||
|
||
func SetupFixedCIDRv6(i *Interface) error { | ||
log.Debugf("Using IPv6 subnet: %v", i.Config.FixedCIDRv6) | ||
return ipallocator.RegisterSubnet(i.Config.FixedCIDRv6, i.Config.FixedCIDRv6) | ||
} |
Oops, something went wrong.