Documentation
¶
Overview ¶
Package netns contains Linux network namespace-related functionality.
Package netns creates Linux network namespaces and runs code, opens sockets and dials inside them. Entering a namespace is a property of the OS thread, so everything here locks a thread for as long as it is in one and restores the caller's namespace before returning.
Index ¶
- func Dialer(ns Handle) func(context.Context, string, string) (net.Conn, error)
- func Do(ctx context.Context, targetNS Handle, do func(context.Context) error) error
- func Listen(ctx context.Context, ns Handle, ports []uint16) (_ []net.Listener, retErr error)
- func RemoveNamed(name string) error
- func SetSysctls(sysctls ...Sysctl) error
- type Handle
- type Sysctl
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dialer ¶
Dialer dials TCP or UDP IP literals in ns, pinning a thread only until the socket is created.
func Listen ¶
Listen opens wildcard TCP listeners inside ns. Sockets retain their namespace and can be served from another namespace.
func RemoveNamed ¶
RemoveNamed unmounts and unlinks a name under /run/netns, without following it if it is a symlink. A name that is already gone is not an error, and the namespace itself survives for as long as something holds it open.
func SetSysctls ¶
SetSysctls writes each sysctl, in order, in the current network namespace, under a single lock hold and at most one /proc/sys read-write remount. Every key is written even if it already reads that way, because writing one key can change another: turning net/ipv4/conf/all/forwarding off sets net/ipv4/conf/all/accept_redirects. Missing net/ipv6/ keys are skipped.
Types ¶
type Handle ¶
Handle is a descriptor for a network namespace. Callers name it through this package so that nothing else has to import the one underneath.
func CreateNamed ¶
CreateNamed creates a named netns and returns its handle, restoring the caller's current netns before returning.
The caller owns the name exclusively, so a name still present when this runs was left behind by an earlier incarnation and is removed first. The kernel creates the name with O_EXCL, so without that removal a single failed teardown would wedge the name for good: nothing could ever create it again. Removal only unmounts and unlinks the name. Anything still holding the namespace keeps it alive, and existing handles stay usable.
func GetFromName ¶
GetFromName opens the namespace of that name under /run/netns.