diff --git a/libnetwork/namespace.go b/libnetwork/namespace.go index 4cc53e5909..56dc20086b 100644 --- a/libnetwork/namespace.go +++ b/libnetwork/namespace.go @@ -2,14 +2,16 @@ package libnetwork import "syscall" +// 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. type networkNamespace struct { path string interfaces []*Interface } -// Create a new network namespace mounted on the provided path. -func NewNamespace(path string) (Namespace, error) { - if err := reexec(reexecCreateNamespace, path); err != nil { +func createNetworkNamespace(path string) (Namespace, error) { + if err := reexec(cmdReexecCreateNamespace, path); err != nil { return nil, err } return &networkNamespace{path: path}, nil @@ -17,7 +19,7 @@ func NewNamespace(path string) (Namespace, error) { func (n *networkNamespace) AddInterface(i *Interface) error { // TODO Open pipe, pass fd to child and write serialized Interface on it. - if err := reexec(reexecMoveInterface, i.SrcName, i.DstName); err != nil { + if err := reexec(cmdReexecMoveInterface, i.SrcName, i.DstName); err != nil { return err } n.interfaces = append(n.interfaces, i) diff --git a/libnetwork/network.go b/libnetwork/network.go index 60e9cd0b0c..544e854820 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -9,7 +9,7 @@ // // // For a new container: create network namespace (providing the path). // networkPath := "/var/lib/docker/.../4d23e" -// networkNamespace, err := libnetwork.NewNamespace(networkPath) +// networkNamespace, err := libnetwork.NewNetworkNamespace(networkPath) // if err != nil { // return err // } @@ -35,10 +35,19 @@ package libnetwork // ulteriorly join using the Link method. A Network is managed by a specific // driver. type Network interface { + // The type of network, which corresponds to its managing driver. Type() string + + // Create a new link to this network symbolically identified by the + // specified unique name. Link(name string) ([]*Interface, error) } +// Interface represents the settings and identity of a network device. It is +// used as a return type for Network.Link, and it is common practice for the +// caller to use this information when moving interface SrcName from host +// namespace to DstName in a different net namespace with the appropriate +// network settings. type Interface struct { // The name of the interface in the origin network namespace. SrcName string @@ -47,20 +56,50 @@ type Interface struct { // network namespace. DstName string - MacAddress string - Address string + // MAC address for the interface. + MacAddress string + + // IPv4 address for the interface. + Address string + + // IPv6 address for the interface. AddressIPv6 string - Gateway string + + // IPv4 gateway for the interface. + Gateway string + + // IPv6 gateway for the interface. GatewayIPv6 string - MTU int + + // Network MTU. + MTU int } +// Namespace represents a network namespace, mounted on a specific Path. It +// holds a list of Interface, and more can be added dynamically. type Namespace interface { + // The path where the network namespace is mounted. Path() string + + // The collection of Interface previously added with the AddInterface + // method. Note that this doesn't incude network interfaces added in any + // other way (such as the default loopback interface existing in any newly + // created network namespace). Interfaces() []*Interface + + // Add an existing Interface to this namespace. The operation will rename + // from the Interface SrcName to DstName as it moves, and reconfigure the + // interface according to the specified settings. AddInterface(*Interface) error } +// Create a new network of the specified networkType. The options are driver +// specific and modeled in a generic way. func NewNetwork(networkType string, options DriverParams) (Network, error) { return createNetwork(networkType, options) } + +// Create a new network namespace mounted on the specified path. +func NewNetworkNamespace(path string) (Namespace, error) { + return createNetworkNamespace(path) +} diff --git a/libnetwork/reexec.go b/libnetwork/reexec.go index 0da21c62e6..4f9ab6bf70 100644 --- a/libnetwork/reexec.go +++ b/libnetwork/reexec.go @@ -11,16 +11,16 @@ import ( type reexecCommand int const ( - reexecCreateNamespace reexecCommand = iota - reexecMoveInterface + cmdReexecCreateNamespace reexecCommand = iota + cmdReexecMoveInterface ) var reexecCommands = map[reexecCommand]struct { Key string Entrypoint func() }{ - reexecCreateNamespace: {"netns-create", createNetworkNamespace}, - reexecMoveInterface: {"netns-moveif", namespaceMoveInterface}, + cmdReexecCreateNamespace: {"netns-create", reexecCreateNamespace}, + cmdReexecMoveInterface: {"netns-moveif", reexecMoveInterface}, } func init() { diff --git a/libnetwork/reexec_move_interface.go b/libnetwork/reexec_move_interface.go index 10706033b2..707fd4f54a 100644 --- a/libnetwork/reexec_move_interface.go +++ b/libnetwork/reexec_move_interface.go @@ -19,7 +19,7 @@ func (s setupError) Error() string { return s.Message } -func namespaceMoveInterface() { +func reexecMoveInterface() { runtime.LockOSThread() var ( diff --git a/libnetwork/reexec_netns_create.go b/libnetwork/reexec_netns_create.go index 96c63bc7c3..827669f5b7 100644 --- a/libnetwork/reexec_netns_create.go +++ b/libnetwork/reexec_netns_create.go @@ -9,7 +9,7 @@ import ( "github.com/vishvananda/netlink" ) -func createNetworkNamespace() { +func reexecCreateNamespace() { runtime.LockOSThread() if len(os.Args) < 2 {