2014-06-24 07:23:51 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-03-04 06:41:38 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
"github.com/docker/libcontainer/system"
|
2014-03-04 06:41:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// crosbymichael: could make a network strategy that instead of returning veth pair names it returns a pid to an existing network namespace
|
|
|
|
type NetNS struct {
|
|
|
|
}
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
func (v *NetNS) Create(n *Network, nspid int, networkState *NetworkState) error {
|
|
|
|
networkState.NsPath = n.NsPath
|
2014-03-04 06:41:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
func (v *NetNS) Initialize(config *Network, networkState *NetworkState) error {
|
|
|
|
if networkState.NsPath == "" {
|
|
|
|
return fmt.Errorf("nspath does is not specified in NetworkState")
|
2014-03-04 06:41:38 +08:00
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
f, err := os.OpenFile(networkState.NsPath, os.O_RDONLY, 0)
|
2014-03-04 06:41:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed get network namespace fd: %v", err)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-03-04 06:41:38 +08:00
|
|
|
if err := system.Setns(f.Fd(), syscall.CLONE_NEWNET); err != nil {
|
2014-09-26 01:04:50 +08:00
|
|
|
f.Close()
|
2014-03-04 06:41:38 +08:00
|
|
|
return fmt.Errorf("failed to setns current network namespace: %v", err)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-09-26 01:04:50 +08:00
|
|
|
f.Close()
|
2014-03-04 06:41:38 +08:00
|
|
|
return nil
|
|
|
|
}
|