Initial commit of libcontainer
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
f8923d8060
commit
81d2c67492
20 changed files with 1531 additions and 0 deletions
104
libcontainer/network/network.go
Normal file
104
libcontainer/network/network.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/dotcloud/docker/pkg/netlink"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoDefaultRoute = errors.New("no default network route found")
|
||||
)
|
||||
|
||||
func InterfaceUp(name string) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkLinkUp(iface)
|
||||
}
|
||||
|
||||
func InterfaceDown(name string) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkLinkDown(iface)
|
||||
}
|
||||
|
||||
func ChangeInterfaceName(old, newName string) error {
|
||||
iface, err := net.InterfaceByName(old)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkChangeName(iface, newName)
|
||||
}
|
||||
|
||||
func CreateVethPair(name1, name2 string) error {
|
||||
return netlink.NetworkCreateVethPair(name1, name2)
|
||||
}
|
||||
|
||||
func SetInterfaceInNamespacePid(name string, nsPid int) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkSetNsPid(iface, nsPid)
|
||||
}
|
||||
|
||||
func SetInterfaceInNamespaceFd(name string, fd int) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkSetNsFd(iface, fd)
|
||||
}
|
||||
|
||||
func SetInterfaceMaster(name, master string) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
masterIface, err := net.InterfaceByName(master)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkSetMaster(iface, masterIface)
|
||||
}
|
||||
|
||||
func SetDefaultGateway(ip string) error {
|
||||
return netlink.AddDefaultGw(net.ParseIP(ip))
|
||||
}
|
||||
|
||||
func SetInterfaceIp(name string, rawIp string) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ip, ipNet, err := net.ParseCIDR(rawIp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkLinkAddIp(iface, ip, ipNet)
|
||||
}
|
||||
|
||||
func SetMtu(name string, mtu int) error {
|
||||
iface, err := net.InterfaceByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.NetworkSetMTU(iface, mtu)
|
||||
}
|
||||
|
||||
func GetDefaultMtu() (int, error) {
|
||||
routes, err := netlink.NetworkGetRoutes()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
for _, r := range routes {
|
||||
if r.Default {
|
||||
return r.Iface.MTU, nil
|
||||
}
|
||||
}
|
||||
return -1, ErrNoDefaultRoute
|
||||
}
|
85
libcontainer/network/veth.go
Normal file
85
libcontainer/network/veth.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dotcloud/docker/pkg/libcontainer"
|
||||
"github.com/dotcloud/docker/pkg/libcontainer/namespaces"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// SetupVeth sets up an existing network namespace with the specified
|
||||
// network configuration.
|
||||
func SetupVeth(config *libcontainer.Network) error {
|
||||
if err := InterfaceDown(config.TempVethName); err != nil {
|
||||
return fmt.Errorf("interface down %s %s", config.TempVethName, err)
|
||||
}
|
||||
if err := ChangeInterfaceName(config.TempVethName, "eth0"); err != nil {
|
||||
return fmt.Errorf("change %s to eth0 %s", config.TempVethName, err)
|
||||
}
|
||||
if err := SetInterfaceIp("eth0", config.IP); err != nil {
|
||||
return fmt.Errorf("set eth0 ip %s", err)
|
||||
}
|
||||
|
||||
if err := SetMtu("eth0", config.Mtu); err != nil {
|
||||
return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
|
||||
}
|
||||
if err := InterfaceUp("eth0"); err != nil {
|
||||
return fmt.Errorf("eth0 up %s", err)
|
||||
}
|
||||
|
||||
if err := SetMtu("lo", config.Mtu); err != nil {
|
||||
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
|
||||
}
|
||||
if err := InterfaceUp("lo"); err != nil {
|
||||
return fmt.Errorf("lo up %s", err)
|
||||
}
|
||||
|
||||
if config.Gateway != "" {
|
||||
if err := SetDefaultGateway(config.Gateway); err != nil {
|
||||
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetupNamespaceMountDir prepares a new root for use as a mount
|
||||
// source for bind mounting namespace fd to an outside path
|
||||
func SetupNamespaceMountDir(root string) error {
|
||||
if err := os.MkdirAll(root, 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
// make sure mounts are not unmounted by other mnt namespaces
|
||||
if err := syscall.Mount("", root, "none", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil && err != syscall.EINVAL {
|
||||
return err
|
||||
}
|
||||
if err := syscall.Mount(root, root, "none", syscall.MS_BIND, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateNetworkNamespace creates a new network namespace and binds it's fd
|
||||
// at the binding path
|
||||
func CreateNetworkNamespace(bindingPath string) error {
|
||||
f, err := os.OpenFile(bindingPath, os.O_RDONLY|os.O_CREATE|os.O_EXCL, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Close()
|
||||
|
||||
if err := namespaces.CreateNewNamespace(libcontainer.CLONE_NEWNET, bindingPath); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteNetworkNamespace unmounts the binding path and removes the
|
||||
// file so that no references to the fd are present and the network
|
||||
// namespace is automatically cleaned up
|
||||
func DeleteNetworkNamespace(bindingPath string) error {
|
||||
if err := syscall.Unmount(bindingPath, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Remove(bindingPath)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue