Documentation
¶
Overview ¶
Package netns provides a utility function that allows a user to perform actions in a different network namespace
Index ¶
- func Do(nsName string, cb Callback) error
- func NewNSListener(nsName string, addr *net.TCPAddr, tos byte, logger logger.Logger) (net.Listener, error)
- func NewNSListenerWithCustomListener(nsName string, addr *net.TCPAddr, logger logger.Logger, ...) (net.Listener, error)
- func ParseAddress(address string) (nsName string, addr string, err error)
- func VRFToNetNS(vrf string) string
- type Callback
- type ListenerCreator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do takes a function which it will call in the network namespace specified by nsName. The goroutine that calls this will lock itself to its current OS thread, hop namespaces, call the given function, hop back to its original namespace, and then unlock itself from its current OS thread. Do returns an error if an error occurs at any point besides in the invocation of the given function, or if the given function itself returns an error.
The callback function is expected to do something simple such as just creating a socket / opening a connection, as it's not desirable to start complex logic in a goroutine that is pinned to the current OS thread. Also any goroutine started from the callback function may or may not execute in the desired namespace.
Example (HttpClient) ¶
package main import ( "net" "net/http" "time" "github.com/aristanetworks/goarista/netns" ) func main() { vrf := "management" vrf = netns.VRFToNetNS(vrf) // vrf is now "ns-management" dial := func(network, address string) (net.Conn, error) { var conn net.Conn err := netns.Do(vrf, func() error { var err error conn, err = (&net.Dialer{ Timeout: 30 * time.Second, // This is the connection timeout KeepAlive: 30 * time.Second, }).Dial(network, address) return err }) return conn, err } client := &http.Client{ Transport: &http.Transport{ //TLSClientConfig: ..., <- if you need SSL/TLS. Dial: dial, }, Timeout: 30 * time.Second, // This is the request timeout } resp, err := client.Get("http://example.com") _ = resp _ = err }
Output:
func NewNSListener ¶
func NewNSListener(nsName string, addr *net.TCPAddr, tos byte, logger logger.Logger) (net.Listener, error)
NewNSListener creates a new net.Listener bound to a network namespace. The listening socket will be bound to the specified local address and will have the specified tos.
func NewNSListenerWithCustomListener ¶
func NewNSListenerWithCustomListener(nsName string, addr *net.TCPAddr, logger logger.Logger, listenerCreator ListenerCreator) (net.Listener, error)
NewNSListenerWithCustomListener creates a new net.Listener bound to a network namespace. The listener is created using listenerCreator. listenerCreator should create a listener that binds to addr. listenerCreator may be called multiple times if the vrf is deleted and recreated.
func ParseAddress ¶
ParseAddress takes in an address string, parsing out the address and an optional VRF name. It returns the namespace corresponding to the VRF. The expected form is [<vrf-name>/]address:port. However, ParseAddress will not actually check to see if the VRF name or address are valid. Presumably, when those values are used later, they will fail if they are malformed
func VRFToNetNS ¶
VRFToNetNS converts a VRF name to network namespace's name corresponding to that VRF.
Types ¶
type Callback ¶
type Callback func() error
Callback is a function that gets called in a given network namespace. The user needs to check any errors from any calls inside this function.
type ListenerCreator ¶
ListenerCreator is the signature of a function which creates a listener, for use in functions where custom listeners can be generated