123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- package sandbox
- import (
- "fmt"
- "net"
- "os"
- "os/exec"
- "runtime"
- "sync"
- "syscall"
- "time"
- log "github.com/Sirupsen/logrus"
- "github.com/docker/docker/pkg/reexec"
- "github.com/docker/libnetwork/types"
- "github.com/vishvananda/netlink"
- "github.com/vishvananda/netns"
- )
- const prefix = "/var/run/docker/netns"
- var (
- once sync.Once
- garbagePathMap = make(map[string]bool)
- gpmLock sync.Mutex
- gpmWg sync.WaitGroup
- gpmCleanupPeriod = 60 * time.Second
- )
- // 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.
- type networkNamespace struct {
- path string
- sinfo *Info
- nextIfIndex int
- sync.Mutex
- }
- func init() {
- reexec.Register("netns-create", reexecCreateNamespace)
- }
- func createBasePath() {
- err := os.MkdirAll(prefix, 0644)
- if err != nil && !os.IsExist(err) {
- panic("Could not create net namespace path directory")
- }
- // Start the garbage collection go routine
- go removeUnusedPaths()
- }
- func removeUnusedPaths() {
- gpmLock.Lock()
- period := gpmCleanupPeriod
- gpmLock.Unlock()
- for range time.Tick(period) {
- 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
- gpmLock.Unlock()
- }
- func removeFromGarbagePaths(path string) {
- gpmLock.Lock()
- delete(garbagePathMap, path)
- gpmLock.Unlock()
- }
- // 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]
- }
- // NewSandbox provides a new sandbox instance created in an os specific way
- // provided a key which uniquely identifies the sandbox
- func NewSandbox(key string, osCreate bool) (Sandbox, error) {
- info, err := createNetworkNamespace(key, osCreate)
- if err != nil {
- return nil, err
- }
- return &networkNamespace{path: key, sinfo: info}, nil
- }
- 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)
- }
- if err := loopbackUp(); err != nil {
- log.Fatal(err)
- }
- }
- func createNetworkNamespace(path string, osCreate bool) (*Info, error) {
- 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
- }
- cmd := &exec.Cmd{
- Path: reexec.Self(),
- Args: append([]string{"netns-create"}, path),
- Stdout: os.Stdout,
- Stderr: os.Stderr,
- }
- if osCreate {
- cmd.SysProcAttr = &syscall.SysProcAttr{}
- cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWNET
- }
- if err := cmd.Run(); err != nil {
- return nil, fmt.Errorf("namespace creation reexec command failed: %v", err)
- }
- interfaces := []*Interface{}
- info := &Info{Interfaces: interfaces}
- return info, nil
- }
- func unmountNamespaceFile(path string) {
- if _, err := os.Stat(path); err == nil {
- syscall.Unmount(path, syscall.MNT_DETACH)
- }
- }
- func createNamespaceFile(path string) (err error) {
- var f *os.File
- once.Do(createBasePath)
- // 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()
- if f, err = os.Create(path); err == nil {
- f.Close()
- }
- return err
- }
- func loopbackUp() error {
- iface, err := netlink.LinkByName("lo")
- if err != nil {
- return err
- }
- return netlink.LinkSetUp(iface)
- }
- 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
- }
- // Move the network interface to caller namespace.
- if err := netlink.LinkSetNsFd(iface, int(origns)); err != nil {
- fmt.Println("LinkSetNsPid failed: ", err)
- return err
- }
- 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()
- return nil
- }
- func (n *networkNamespace) AddInterface(i *Interface) error {
- n.Lock()
- i.DstName = fmt.Sprintf("%s%d", i.DstName, n.nextIfIndex)
- n.nextIfIndex++
- n.Unlock()
- 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()
- // Find the network interface identified by the SrcName attribute.
- 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 {
- return err
- }
- if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
- return err
- }
- defer netns.Set(origns)
- // Down the interface before configuring
- if err := netlink.LinkSetDown(iface); err != nil {
- return err
- }
- // 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
- }
- n.Lock()
- n.sinfo.Interfaces = append(n.sinfo.Interfaces, i)
- n.Unlock()
- return nil
- }
- func (n *networkNamespace) SetGateway(gw net.IP) error {
- // Silently return if the gateway is empty
- if len(gw) == 0 {
- return nil
- }
- err := programGateway(n.path, gw, true)
- if err == nil {
- n.Lock()
- n.sinfo.Gateway = gw
- n.Unlock()
- }
- return err
- }
- func (n *networkNamespace) UnsetGateway() error {
- n.Lock()
- gw := n.sinfo.Gateway
- n.Unlock()
- // Silently return if the gateway is empty
- if len(gw) == 0 {
- return nil
- }
- err := programGateway(n.path, gw, false)
- if err == nil {
- n.Lock()
- n.sinfo.Gateway = net.IP{}
- n.Unlock()
- }
- return err
- }
- func (n *networkNamespace) SetGatewayIPv6(gw net.IP) error {
- // Silently return if the gateway is empty
- if len(gw) == 0 {
- return nil
- }
- err := programGateway(n.path, gw, true)
- if err == nil {
- n.Lock()
- n.sinfo.GatewayIPv6 = gw
- n.Unlock()
- }
- return err
- }
- func (n *networkNamespace) UnsetGatewayIPv6() error {
- n.Lock()
- gw := n.sinfo.GatewayIPv6
- n.Unlock()
- // Silently return if the gateway is empty
- if len(gw) == 0 {
- return nil
- }
- err := programGateway(n.path, gw, false)
- if err == nil {
- n.Lock()
- n.sinfo.GatewayIPv6 = net.IP{}
- n.Unlock()
- }
- return err
- }
- 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
- }
- func (n *networkNamespace) Interfaces() []*Interface {
- n.Lock()
- defer n.Unlock()
- return n.sinfo.Interfaces
- }
- func (n *networkNamespace) Key() string {
- return n.path
- }
- func (n *networkNamespace) Destroy() error {
- // Assuming no running process is executing in this network namespace,
- // unmounting is sufficient to destroy it.
- if err := syscall.Unmount(n.path, syscall.MNT_DETACH); err != nil {
- return err
- }
- // Stash it into the garbage collection list
- addToGarbagePaths(n.path)
- return nil
- }
|