Skip to content

Commit

Permalink
feature: add IPv6 network create inside BridgeMode
Browse files Browse the repository at this point in the history
Signed-off-by: Leno Hou <lenohou@gmail.com>
  • Loading branch information
houstar committed Nov 5, 2018
1 parent c4b9f7f commit 811645e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
69 changes: 50 additions & 19 deletions network/mode/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

// New is used to initialize bridge network.
func New(ctx context.Context, config network.BridgeConfig, manager mgr.NetworkMgr) error {
// TODO: support ipv6.

// clear exist bridge network
if err := manager.Remove(ctx, "bridge"); err != nil {
if !errtypes.IsNotfound(err) {
Expand All @@ -38,62 +36,95 @@ func New(ctx context.Context, config network.BridgeConfig, manager mgr.NetworkMg
}

// get bridge ip
bridgeIP := utils.StringDefault(config.IPv4, DefaultIPNet)
ipNet, err := netlink.ParseIPNet(bridgeIP)
bridgeIP := utils.StringDefault(config.IPv4, DefaultIPv4Net)
ipv4Net, err := netlink.ParseIPNet(bridgeIP)
if err != nil {
return fmt.Errorf("failed to parse ip %v", bridgeIP)
}
logrus.Debugf("initialize bridge network, bridge ip: %s.", ipNet)
logrus.Debugf("initialize bridge network, bridge ip: %s.", ipv4Net)

// init host bridge network.
_, err = initBridgeDevice(bridgeName, ipNet)
_, err = initBridgeDevice(bridgeName, ipv4Net)
if err != nil {
return err
}

// get bridge subnet
_, subnet, err := net.ParseCIDR(bridgeIP)
_, subnetv4, err := net.ParseCIDR(bridgeIP)
if err != nil {
return fmt.Errorf("failted to parse subnet %v", bridgeIP)
}
logrus.Debugf("initialize bridge network, bridge network: %s", subnet)
logrus.Debugf("initialize bridge network, bridge network: %s", subnetv4)

// get ip range
var ipRange string
var ipv4Range string
if config.FixedCIDRv4 != "" {
ipRange = config.FixedCIDRv4
ipv4Range = config.FixedCIDRv4
} else {
ipRange = subnet.String()
ipv4Range = subnetv4.String()
}
logrus.Debugf("initialize bridge network, bridge ip range in subnet: %s", ipRange)
logrus.Debugf("initialize bridge network, bridge ip range in subnet: %s", ipv4Range)

// get gateway
gateway := DefaultGateway
gatewayv4 := DefaultGatewayv4
if config.GatewayIPv4 != "" {
gateway = config.GatewayIPv4
gatewayv4 = config.GatewayIPv4
}
logrus.Debugf("initialize bridge network, gateway: %s", gateway)
logrus.Debugf("initialize bridge network, gateway: %s", gatewayv4)

ipamV4Conf := types.IPAMConfig{
AuxAddress: make(map[string]string),
Subnet: subnet.String(),
IPRange: ipRange,
Gateway: gateway,
Subnet: subnetv4.String(),
IPRange: ipv4Range,
Gateway: gatewayv4,
}

ipam := &types.IPAM{
Driver: "default",
Config: []types.IPAMConfig{ipamV4Conf},
}

if config.IPv6 {
// get ipv6 subnet
FixedCIDRv6 := utils.StringDefault(config.FixedCIDRv6, DefaultIPv6Net)
_, subnetv6, err := net.ParseCIDR(FixedCIDRv6)
if err != nil {
return fmt.Errorf("failted to parse subnet %v", FixedCIDRv6)
}
logrus.Debugf("initialize bridge network, bridge network: %s", subnetv6)

// get ipv6 range
var ipv6Range string
if config.FixedCIDRv6 != "" {
ipv6Range = config.FixedCIDRv6
} else {
ipv6Range = subnetv6.String()
}
logrus.Debugf("initialize bridge network, bridge ip range in subnet: %s", ipv6Range)

gatewayv6 := DefaultGatewayv6
if config.GatewayIPv6 != "" {
gatewayv6 = config.GatewayIPv6
}
logrus.Debugf("initialize bridge network, gateway: %s", gatewayv6)

ipamV6Conf := types.IPAMConfig{
AuxAddress: make(map[string]string),
Subnet: subnetv6.String(),
IPRange: ipv6Range,
Gateway: gatewayv6,
}
ipam.Config = append(ipam.Config, ipamV6Conf)
}

mtu := network.DefaultNetworkMtu
if config.Mtu != 0 {
mtu = config.Mtu
}

networkCreate := types.NetworkCreate{
Driver: "bridge",
EnableIPV6: false,
EnableIPV6: config.IPv6,
Internal: false,
Options: map[string]string{
bridge.BridgeName: bridgeName,
Expand Down
21 changes: 15 additions & 6 deletions network/mode/bridge/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ const (
// DefaultBridge defines the default bridge name.
DefaultBridge = "p0"

// DefaultIPNet defines the default bridge ip.
DefaultIPNet = "192.168.5.1/24"
// DefaultIPv4Net defines the default bridge ipv4.
DefaultIPv4Net = "192.168.5.1/24"

// DefaultIPRange defines the default bridge ip range in ipam.
DefaultIPRange = "192.168.5.1/24"
// DefaultIPv4Range defines the default bridge ipv4 range in ipam.
DefaultIPv4Range = "192.168.5.1/24"

// DefaultGateway defines the default bridge gateway.
DefaultGateway = "192.168.5.1"
// DefaultGatewayv4 defines the default bridge gateway ipv4.
DefaultGatewayv4 = "192.168.5.1"

// DefaultIPv6Net defines the default bridge ipv6.
DefaultIPv6Net = "2002:db8:1::1/64"

// DefaultIPv6Range defines the default bridge ipv6 range in ipam.
DefaultIPv6Range = "2002:db8:1::1/64"

// DefaultGatewayv6 defines the default bridge gateway ipv6.
DefaultGatewayv6 = "2002:db8:1::1"

// DefaultBindingIP defines the default binding ip.
DefaultBindingIP = "0.0.0.0"
Expand Down

0 comments on commit 811645e

Please sign in to comment.