netns

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dialer

func Dialer(ns Handle) func(context.Context, string, string) (net.Conn, error)

Dialer dials TCP or UDP IP literals in ns, pinning a thread only until the socket is created.

func Do

func Do(ctx context.Context, targetNS Handle, do func(context.Context) error) error

Do runs do() with the OS thread switched into targetNS, then restores it.

func Listen

func Listen(ctx context.Context, ns Handle, ports []uint16) (_ []net.Listener, retErr error)

Listen opens wildcard TCP listeners inside ns. Sockets retain their namespace and can be served from another namespace.

func RemoveNamed

func RemoveNamed(name string) error

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

func SetSysctls(sysctls ...Sysctl) error

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

type Handle = vishnetns.NsHandle

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

func CreateNamed(name string) (Handle, error)

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

func GetFromName(name string) (Handle, error)

GetFromName opens the namespace of that name under /run/netns.

type Sysctl

type Sysctl struct {
	Key   string
	Value string
}

Sysctl is a key-value sysctl setting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL