2015-02-19 17:21:42 -08:00
|
|
|
package libnetwork
|
|
|
|
|
2015-02-24 11:19:00 -08:00
|
|
|
import "syscall"
|
|
|
|
|
2015-02-27 09:34:30 -08:00
|
|
|
// The networkNamespace type is the default implementation of the Namespace
|
|
|
|
// interface. It simply creates a new network namespace, and moves an interface
|
|
|
|
// into it when called on method AddInterface.
|
2015-02-19 17:21:42 -08:00
|
|
|
type networkNamespace struct {
|
|
|
|
path string
|
|
|
|
interfaces []*Interface
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:34:30 -08:00
|
|
|
func createNetworkNamespace(path string) (Namespace, error) {
|
|
|
|
if err := reexec(cmdReexecCreateNamespace, path); err != nil {
|
2015-02-19 17:21:42 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &networkNamespace{path: path}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) AddInterface(i *Interface) error {
|
2015-02-22 17:24:22 -08:00
|
|
|
// TODO Open pipe, pass fd to child and write serialized Interface on it.
|
2015-02-27 09:34:30 -08:00
|
|
|
if err := reexec(cmdReexecMoveInterface, i.SrcName, i.DstName); err != nil {
|
2015-02-19 17:21:42 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.interfaces = append(n.interfaces, i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) Interfaces() []*Interface {
|
|
|
|
return n.interfaces
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) Path() string {
|
|
|
|
return n.path
|
|
|
|
}
|
2015-02-24 11:19:00 -08:00
|
|
|
|
|
|
|
func (n *networkNamespace) Destroy() error {
|
|
|
|
// Assuming no running process is executing in this network namespace,
|
|
|
|
// unmounting is sufficient to destroy it.
|
|
|
|
return syscall.Unmount(n.path, syscall.MNT_DETACH)
|
|
|
|
}
|