2015-02-22 20:24:22 -05:00
|
|
|
package bridge
|
|
|
|
|
2015-04-10 12:02:25 -04:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
2015-02-22 20:24:22 -05:00
|
|
|
|
2015-02-22 20:58:52 -05:00
|
|
|
const (
|
2015-03-04 16:25:43 -05:00
|
|
|
// DefaultBridgeName is the default name for the bridge interface managed
|
|
|
|
// by the driver when unspecified by the caller.
|
2015-02-22 20:58:52 -05:00
|
|
|
DefaultBridgeName = "docker0"
|
|
|
|
)
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
// Interface models the bridge network device.
|
|
|
|
type bridgeInterface struct {
|
2015-04-10 12:02:25 -04:00
|
|
|
Link netlink.Link
|
|
|
|
bridgeIPv4 *net.IPNet
|
|
|
|
bridgeIPv6 *net.IPNet
|
2015-02-22 20:24:22 -05:00
|
|
|
}
|
|
|
|
|
2015-04-20 13:08:09 -04:00
|
|
|
// newInterface creates a new bridge interface structure. It attempts to find
|
2015-03-04 16:25:43 -05:00
|
|
|
// an already existing device identified by the Configuration BridgeName field,
|
|
|
|
// or the default bridge name when unspecified), but doesn't attempt to create
|
2015-04-20 13:08:09 -04:00
|
|
|
// one when missing
|
2015-03-04 16:25:43 -05:00
|
|
|
func newInterface(config *Configuration) *bridgeInterface {
|
2015-04-15 01:25:42 -04:00
|
|
|
i := &bridgeInterface{}
|
2015-02-22 20:24:22 -05:00
|
|
|
|
2015-02-22 20:58:52 -05:00
|
|
|
// Initialize the bridge name to the default if unspecified.
|
2015-04-15 01:25:42 -04:00
|
|
|
if config.BridgeName == "" {
|
|
|
|
config.BridgeName = DefaultBridgeName
|
2015-02-22 20:58:52 -05:00
|
|
|
}
|
|
|
|
|
2015-02-22 20:24:22 -05:00
|
|
|
// Attempt to find an existing bridge named with the specified name.
|
2015-04-15 01:25:42 -04:00
|
|
|
i.Link, _ = netlink.LinkByName(config.BridgeName)
|
2015-02-22 20:24:22 -05:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-04-20 13:08:09 -04:00
|
|
|
// exists indicates if the existing bridge interface exists on the system.
|
2015-03-04 16:25:43 -05:00
|
|
|
func (i *bridgeInterface) exists() bool {
|
2015-02-22 20:24:22 -05:00
|
|
|
return i.Link != nil
|
|
|
|
}
|
2015-02-23 00:32:48 -05:00
|
|
|
|
2015-04-20 13:08:09 -04:00
|
|
|
// addresses returns a single IPv4 address and all IPv6 addresses for the
|
2015-02-23 00:32:48 -05:00
|
|
|
// bridge interface.
|
2015-03-04 16:25:43 -05:00
|
|
|
func (i *bridgeInterface) addresses() (netlink.Addr, []netlink.Addr, error) {
|
2015-02-23 00:32:48 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:41:17 -05:00
|
|
|
if len(v4addr) == 0 {
|
|
|
|
return netlink.Addr{}, v6addr, nil
|
|
|
|
}
|
2015-02-23 00:32:48 -05:00
|
|
|
return v4addr[0], v6addr, nil
|
|
|
|
}
|