Skip to content

Commit

Permalink
Update/Add IPv6 networking support
Browse files Browse the repository at this point in the history
This is based on the work done in PR moby#2974

Docker-DCO-1.1-Signed-off-by: Andrew Williams <williams.andrew@gmail.com> (github: TheDude05)
  • Loading branch information
ajwdev committed Feb 18, 2014
1 parent 39ad2cb commit 9bfaf44
Show file tree
Hide file tree
Showing 19 changed files with 821 additions and 1,616 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -15,6 +15,7 @@ Andreas Tiefenthaler <at@an-ti.eu>
Andrew Duckworth <grillopress@gmail.com>
Andrew Macgregor <andrew.macgregor@agworld.com.au>
Andrew Munsell <andrew@wizardapps.net>
Andrew Williams <williams.andrew@gmail.com>
Andrews Medina <andrewsmedina@gmail.com>
Andy Chambers <anchambers@paypal.com>
andy diller <dillera@gmail.com>
Expand Down
26 changes: 17 additions & 9 deletions container.go
@@ -1,7 +1,6 @@
package docker

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -563,11 +562,14 @@ func populateCommand(c *Container) {
if !c.Config.NetworkDisabled {
network := c.NetworkSettings
en = &execdriver.Network{
Gateway: network.Gateway,
Bridge: network.Bridge,
IPAddress: network.IPAddress,
IPPrefixLen: network.IPPrefixLen,
Mtu: c.runtime.config.Mtu,
Gateway: network.Gateway,
Gateway6: network.Gateway6,
IPAddress: network.IPAddress,
IPAddress6: network.IPAddress6,
IPPrefixLen: network.IPPrefixLen,
IPPrefixLen6: network.IPPrefixLen6,
Bridge: network.Bridge,
Mtu: c.runtime.config.Mtu,
}
}

Expand Down Expand Up @@ -640,6 +642,10 @@ func (container *Container) Start() (err error) {
log.Printf("WARNING: IPv4 forwarding is disabled. Networking will not work")
}

if container.runtime.sysInfo.IPv6ForwardingDisabled {
log.Printf("WARNING: IPv6 forwarding is disabled. IPv6 networking will not work")
}

if container.Volumes == nil || len(container.Volumes) == 0 {
container.Volumes = make(map[string]string)
container.VolumesRW = make(map[string]bool)
Expand Down Expand Up @@ -1118,11 +1124,13 @@ func (container *Container) allocateNetwork() error {
if container.runtime.config.DisableNetwork {
env = &engine.Env{}
} else {
currentIP := container.NetworkSettings.IPAddress
currentIP := container.NetworkSettings.IPAddress
currentIP6 := container.NetworkSettings.IPAddress6

job := eng.Job("allocate_interface", container.ID)
if currentIP != "" {
job.Setenv("RequestIP", currentIP)
job.Setenv("RequestIP6", currentIP6)
}

env, err = job.Stdout.AddEnv()
Expand Down Expand Up @@ -1217,8 +1225,8 @@ func (container *Container) allocateNetwork() error {
container.NetworkSettings.IPAddress = env.Get("IP")
container.NetworkSettings.IPAddress6 = env.Get("IP6")
container.NetworkSettings.IPPrefixLen = env.GetInt("IPPrefixLen")
container.NetworkSettings.IPPrefixLen6 = env.GetInt("IPPrefixLen6")
container.NetworkSettings.Gateway = env.Get("Gateway")
container.NetworkSettings.IPPrefixLen6 = env.GetInt("IPPrefixLen6")
container.NetworkSettings.Gateway = env.Get("Gateway")
container.NetworkSettings.Gateway6 = env.Get("Gateway6")

return nil
Expand Down
15 changes: 10 additions & 5 deletions execdriver/driver.go
Expand Up @@ -42,7 +42,9 @@ func GetInitFunc(name string) (InitFunc, error) {
type InitArgs struct {
User string
Gateway string
Gateway6 string
Ip string
Ip6 string
WorkDir string
Privileged bool
Env []string
Expand All @@ -68,11 +70,14 @@ type Driver interface {

// Network settings of the container
type Network struct {
Gateway string `json:"gateway"`
IPAddress string `json:"ip"`
Bridge string `json:"bridge"`
IPPrefixLen int `json:"ip_prefix_len"`
Mtu int `json:"mtu"`
Gateway string `json:"gateway"`
Gateway6 string `json:"gateway6"`
IPAddress string `json:"ip"`
IPAddress6 string `json:"ip6"`
IPPrefixLen int `json:"ip_prefix_len"`
IPPrefixLen6 int `json:"ip_prefix_len6"`
Bridge string `json:"bridge"`
Mtu int `json:"mtu"`
}

type Resources struct {
Expand Down
2 changes: 2 additions & 0 deletions execdriver/lxc/driver.go
Expand Up @@ -94,7 +94,9 @@ func (d *driver) Run(c *execdriver.Command, startCallback execdriver.StartCallba
if c.Network != nil {
params = append(params,
"-g", c.Network.Gateway,
"-g6", c.Network.Gateway6,
"-i", fmt.Sprintf("%s/%d", c.Network.IPAddress, c.Network.IPPrefixLen),
"-i6", fmt.Sprintf("%s/%d", c.Network.IPAddress6, c.Network.IPPrefixLen6),
"-mtu", strconv.Itoa(c.Network.Mtu),
)
}
Expand Down
38 changes: 32 additions & 6 deletions execdriver/lxc/init.go
Expand Up @@ -23,19 +23,35 @@ func setupHostname(args *execdriver.InitArgs) error {

// Setup networking
func setupNetworking(args *execdriver.InitArgs) error {
if args.Ip != "" {
if args.Ip != "" || args.Ip6 != "" {
// eth0
iface, err := net.InterfaceByName("eth0")
if err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)
}
ip, ipNet, err := net.ParseCIDR(args.Ip)
if err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)

// IPv4
if args.Ip != "" {
ip, ipNet, err := net.ParseCIDR(args.Ip)
if err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)
}
if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
return fmt.Errorf("Unable to set up IPv4 networking: %v", err)
}
}
if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)

// IPv6
if args.Ip6 != "" {
ip, ipNet, err := net.ParseCIDR(args.Ip6)
if err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)
}
if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
return fmt.Errorf("Unable to set up IPv6 networking: %v", err)
}
}

if err := netlink.NetworkSetMTU(iface, args.Mtu); err != nil {
return fmt.Errorf("Unable to set MTU: %v", err)
}
Expand All @@ -62,6 +78,16 @@ func setupNetworking(args *execdriver.InitArgs) error {
return fmt.Errorf("Unable to set up networking: %v", err)
}
}
if args.Gateway6 != "" {
gw := net.ParseIP(args.Gateway6)
if gw == nil {
return fmt.Errorf("Unable to set up networking, %s is not a valid gateway IP", args.Gateway)
}

if err := netlink.AddDefaultGw(gw); err != nil {
return fmt.Errorf("Unable to set up networking: %v", err)
}
}

return nil
}
Expand Down

0 comments on commit 9bfaf44

Please sign in to comment.