2015-04-13 14:40:42 -04:00
|
|
|
package sandbox
|
2015-02-19 20:21:42 -05:00
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-13 21:36:58 -04:00
|
|
|
"net"
|
2015-03-11 19:37:43 -04:00
|
|
|
"os"
|
2015-05-26 17:14:01 -04:00
|
|
|
"os/exec"
|
2015-03-11 19:37:43 -04:00
|
|
|
"runtime"
|
2015-04-28 01:57:36 -04:00
|
|
|
"sync"
|
2015-03-11 19:37:43 -04:00
|
|
|
"syscall"
|
2015-05-26 17:14:01 -04:00
|
|
|
"time"
|
2015-03-11 19:37:43 -04:00
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-05-19 20:08:56 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-03-11 19:37:43 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
2015-02-24 14:19:00 -05:00
|
|
|
|
2015-05-18 19:05:10 -04:00
|
|
|
const prefix = "/var/run/docker/netns"
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
garbagePathMap = make(map[string]bool)
|
|
|
|
gpmLock sync.Mutex
|
|
|
|
gpmWg sync.WaitGroup
|
2015-05-27 16:20:24 -04:00
|
|
|
gpmCleanupPeriod = 60 * time.Second
|
2015-05-26 17:14:01 -04:00
|
|
|
)
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
// The networkNamespace type is the linux implementation of the Sandbox
|
|
|
|
// interface. It represents a linux network namespace, and moves an interface
|
|
|
|
// into it when called on method AddInterface or sets the gateway etc.
|
2015-02-19 20:21:42 -05:00
|
|
|
type networkNamespace struct {
|
2015-05-21 14:04:49 -04:00
|
|
|
path string
|
|
|
|
sinfo *Info
|
|
|
|
nextIfIndex int
|
|
|
|
sync.Mutex
|
2015-02-19 20:21:42 -05:00
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
func init() {
|
|
|
|
reexec.Register("netns-create", reexecCreateNamespace)
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
func createBasePath() {
|
2015-04-28 01:57:36 -04:00
|
|
|
err := os.MkdirAll(prefix, 0644)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
panic("Could not create net namespace path directory")
|
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
|
|
|
|
// Start the garbage collection go routine
|
|
|
|
go removeUnusedPaths()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeUnusedPaths() {
|
2015-05-27 17:40:02 -04:00
|
|
|
for range time.Tick(gpmCleanupPeriod) {
|
2015-05-26 17:14:01 -04:00
|
|
|
gpmLock.Lock()
|
|
|
|
pathList := make([]string, 0, len(garbagePathMap))
|
|
|
|
for path := range garbagePathMap {
|
|
|
|
pathList = append(pathList, path)
|
|
|
|
}
|
|
|
|
garbagePathMap = make(map[string]bool)
|
|
|
|
gpmWg.Add(1)
|
|
|
|
gpmLock.Unlock()
|
|
|
|
|
|
|
|
for _, path := range pathList {
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
gpmWg.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToGarbagePaths(path string) {
|
|
|
|
gpmLock.Lock()
|
|
|
|
garbagePathMap[path] = true
|
2015-05-27 17:40:02 -04:00
|
|
|
gpmLock.Unlock()
|
2015-05-26 17:14:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromGarbagePaths(path string) {
|
|
|
|
gpmLock.Lock()
|
|
|
|
delete(garbagePathMap, path)
|
2015-05-27 17:40:02 -04:00
|
|
|
gpmLock.Unlock()
|
2015-04-28 01:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateKey generates a sandbox key based on the passed
|
|
|
|
// container id.
|
|
|
|
func GenerateKey(containerID string) string {
|
|
|
|
maxLen := 12
|
|
|
|
if len(containerID) < maxLen {
|
|
|
|
maxLen = len(containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + "/" + containerID[:maxLen]
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
// NewSandbox provides a new sandbox instance created in an os specific way
|
|
|
|
// provided a key which uniquely identifies the sandbox
|
2015-05-05 00:20:45 -04:00
|
|
|
func NewSandbox(key string, osCreate bool) (Sandbox, error) {
|
|
|
|
info, err := createNetworkNamespace(key, osCreate)
|
2015-05-03 16:29:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &networkNamespace{path: key, sinfo: info}, nil
|
2015-04-13 14:40:42 -04:00
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
func reexecCreateNamespace() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
log.Fatal("no namespace path provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mount("/proc/self/ns/net", os.Args[1], "bind", syscall.MS_BIND, ""); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-05-27 16:20:24 -04:00
|
|
|
|
|
|
|
if err := loopbackUp(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:20:45 -04:00
|
|
|
func createNetworkNamespace(path string, osCreate bool) (*Info, error) {
|
2015-03-11 19:37:43 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
origns, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer origns.Close()
|
|
|
|
|
|
|
|
if err := createNamespaceFile(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: reexec.Self(),
|
|
|
|
Args: append([]string{"netns-create"}, path),
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
2015-05-05 00:20:45 -04:00
|
|
|
if osCreate {
|
2015-05-26 17:14:01 -04:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWNET
|
2015-02-19 20:21:42 -05:00
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, fmt.Errorf("namespace creation reexec command failed: %v", err)
|
2015-03-11 19:37:43 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 20:37:19 -04:00
|
|
|
interfaces := []*Interface{}
|
2015-05-03 16:29:43 -04:00
|
|
|
info := &Info{Interfaces: interfaces}
|
|
|
|
return info, nil
|
2015-02-19 20:21:42 -05:00
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
func unmountNamespaceFile(path string) {
|
2015-05-19 18:08:58 -04:00
|
|
|
if _, err := os.Stat(path); err == nil {
|
2015-05-26 17:14:01 -04:00
|
|
|
syscall.Unmount(path, syscall.MNT_DETACH)
|
2015-05-19 18:08:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
func createNamespaceFile(path string) (err error) {
|
|
|
|
var f *os.File
|
2015-04-28 01:57:36 -04:00
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
once.Do(createBasePath)
|
2015-05-26 17:14:01 -04:00
|
|
|
// Remove it from garbage collection list if present
|
|
|
|
removeFromGarbagePaths(path)
|
|
|
|
|
|
|
|
// If the path is there unmount it first
|
|
|
|
unmountNamespaceFile(path)
|
|
|
|
|
|
|
|
// wait for garbage collection to complete if it is in progress
|
|
|
|
// before trying to create the file.
|
|
|
|
gpmWg.Wait()
|
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
if f, err = os.Create(path); err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
2015-05-26 17:14:01 -04:00
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func loopbackUp() error {
|
|
|
|
iface, err := netlink.LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return netlink.LinkSetUp(iface)
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:38 -04:00
|
|
|
func (n *networkNamespace) RemoveInterface(i *Interface) error {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
origns, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer origns.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(n.path, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed get network namespace %q: %v", n.path, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
nsFD := f.Fd()
|
|
|
|
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer netns.Set(origns)
|
|
|
|
|
|
|
|
// Find the network inteerface identified by the DstName attribute.
|
|
|
|
iface, err := netlink.LinkByName(i.DstName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Down the interface before configuring
|
|
|
|
if err := netlink.LinkSetDown(iface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netlink.LinkSetName(iface, i.SrcName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("LinkSetName failed: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
// Move the network interface to caller namespace.
|
|
|
|
if err := netlink.LinkSetNsFd(iface, int(origns)); err != nil {
|
2015-05-05 20:32:38 -04:00
|
|
|
fmt.Println("LinkSetNsPid failed: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-18 23:02:57 -04:00
|
|
|
n.Lock()
|
|
|
|
for index, intf := range n.sinfo.Interfaces {
|
|
|
|
if intf == i {
|
|
|
|
n.sinfo.Interfaces = append(n.sinfo.Interfaces[:index], n.sinfo.Interfaces[index+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
|
2015-05-05 20:32:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:44:06 -04:00
|
|
|
func (n *networkNamespace) AddInterface(i *Interface) error {
|
2015-05-21 14:04:49 -04:00
|
|
|
n.Lock()
|
|
|
|
i.DstName = fmt.Sprintf("%s%d", i.DstName, n.nextIfIndex)
|
|
|
|
n.nextIfIndex++
|
|
|
|
n.Unlock()
|
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
origns, err := netns.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer origns.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(n.path, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed get network namespace %q: %v", n.path, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-05-04 05:54:19 -04:00
|
|
|
// Find the network interface identified by the SrcName attribute.
|
2015-03-11 19:37:43 -04:00
|
|
|
iface, err := netlink.LinkByName(i.SrcName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the network interface to the destination namespace.
|
|
|
|
nsFD := f.Fd()
|
|
|
|
if err := netlink.LinkSetNsFd(iface, int(nsFD)); err != nil {
|
2015-02-19 20:21:42 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-03-11 19:37:43 -04:00
|
|
|
|
|
|
|
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer netns.Set(origns)
|
|
|
|
|
2015-04-23 20:37:19 -04:00
|
|
|
// Down the interface before configuring
|
|
|
|
if err := netlink.LinkSetDown(iface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:37:43 -04:00
|
|
|
// Configure the interface now this is moved in the proper namespace.
|
|
|
|
if err := configureInterface(iface, i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Up the interface.
|
|
|
|
if err := netlink.LinkSetUp(iface); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:04:49 -04:00
|
|
|
n.Lock()
|
2015-04-13 14:40:42 -04:00
|
|
|
n.sinfo.Interfaces = append(n.sinfo.Interfaces, i)
|
2015-05-21 14:04:49 -04:00
|
|
|
n.Unlock()
|
|
|
|
|
2015-02-19 20:21:42 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-13 21:36:58 -04:00
|
|
|
func (n *networkNamespace) SetGateway(gw net.IP) error {
|
2015-04-28 01:57:36 -04:00
|
|
|
if len(gw) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:08:50 -04:00
|
|
|
err := programGateway(n.path, gw)
|
2015-04-13 14:40:42 -04:00
|
|
|
if err == nil {
|
|
|
|
n.sinfo.Gateway = gw
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 21:36:58 -04:00
|
|
|
func (n *networkNamespace) SetGatewayIPv6(gw net.IP) error {
|
2015-04-23 20:37:19 -04:00
|
|
|
if len(gw) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:08:50 -04:00
|
|
|
err := programGateway(n.path, gw)
|
2015-04-13 14:40:42 -04:00
|
|
|
if err == nil {
|
|
|
|
n.sinfo.GatewayIPv6 = gw
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:08:56 -04:00
|
|
|
func (n *networkNamespace) AddStaticRoute(r *types.StaticRoute) error {
|
|
|
|
err := programRoute(n.path, r.Destination, r.NextHop)
|
|
|
|
if err == nil {
|
|
|
|
n.Lock()
|
|
|
|
n.sinfo.StaticRoutes = append(n.sinfo.StaticRoutes, r)
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) RemoveStaticRoute(r *types.StaticRoute) error {
|
|
|
|
err := removeRoute(n.path, r.Destination, r.NextHop)
|
|
|
|
if err == nil {
|
|
|
|
n.Lock()
|
|
|
|
lastIndex := len(n.sinfo.StaticRoutes) - 1
|
|
|
|
for i, v := range n.sinfo.StaticRoutes {
|
|
|
|
if v == r {
|
|
|
|
// Overwrite the route we're removing with the last element
|
|
|
|
n.sinfo.StaticRoutes[i] = n.sinfo.StaticRoutes[lastIndex]
|
|
|
|
// Shorten the slice to trim the extra element
|
|
|
|
n.sinfo.StaticRoutes = n.sinfo.StaticRoutes[:lastIndex]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-20 11:44:06 -04:00
|
|
|
func (n *networkNamespace) Interfaces() []*Interface {
|
2015-05-18 23:02:57 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-04-13 14:40:42 -04:00
|
|
|
return n.sinfo.Interfaces
|
2015-02-19 20:21:42 -05:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
func (n *networkNamespace) Key() string {
|
2015-02-19 20:21:42 -05:00
|
|
|
return n.path
|
|
|
|
}
|
2015-02-24 14:19:00 -05:00
|
|
|
|
|
|
|
func (n *networkNamespace) Destroy() error {
|
|
|
|
// Assuming no running process is executing in this network namespace,
|
|
|
|
// unmounting is sufficient to destroy it.
|
2015-04-28 01:57:36 -04:00
|
|
|
if err := syscall.Unmount(n.path, syscall.MNT_DETACH); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:14:01 -04:00
|
|
|
// Stash it into the garbage collection list
|
|
|
|
addToGarbagePaths(n.path)
|
|
|
|
return nil
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|