Categorygithub.com/willfantom/neslink
modulepackage
0.2.0
Repository: https://github.com/willfantom/neslink.git
Documentation: pkg.go.dev

# README

netneslink    GitHub release (latest SemVer)

Go Reference

NESlink is go package that allows for interaction with netlink. NESlink is simply a quality of life wrapper around these great netlink and netns packages. Not all the functionality of these packages remains in NESlink, but the interactions that are included are better suited to the NES platform implementation.

The main objective of this package is to make link interaction easier when dealing with many links across many network namespaces. This includes safe parallel operations in multiple namespaces.


Usage

At the core of this package are the Do functions, one for network namespaces, the other for links. These allow for low-code, go routine safe interaction with both links and namespaces (and links in namespaces).

Namespace Interaction

Any interaction with a netns should be done via a call to Do. As an example, to list the links in a network namespace, you would simply need to provide Do with a NsProvider for the target namespace, and the NsAction for listing links. So if there was a network namespace called example, then the following snippet would perform the action safely:

links := make([]netlink.Link, 0)
err := neslink.Do(neslink.NPName("example"), neslink.NALinks(&links))
if err != nil {
  ...

💡 Any number of NSActions can be provided to a single Do call, and they will be executed in order.

Here err would contain any error that occurred either in switching namespaces or within the function. If for any reason the system thread used for the action executing go routine fails to be returned to the netns of the caller, the thread is marked as dirty and can not be accessed again.

Custom NsActions can be easily created too, see this example.

Link Interaction

To manage links, any operation should be a LinkAction set in a call to Do. Do will execute a set of functions in a given netns. As an example, the below snippet will create a new bridge called br0 in a pre-existing named netns called example, then set its MAC address to 12:23:34:45:56:67 and set its state to UP:

if err := neslink.Do(
  neslink.NPName("example"),
  neslink.LANewBridge("br0"),
  neslink.LASetHw(neslink.LPName("br0"), "12:23:34:45:56:67"),
  neslink.LASetUp(neslink.LPName("br0")),
  ); err != nil {
  ...

📝 Setting a link's netns is not a LinkAction but instead a NsAction, since after moving the link to another netns, the netns of the Do goroutine should also be changed to the netns to complete any further actions on the link.

Via the LinkProviders, new links can be created, or already created links can be obtained via their name, index, or alias.

NEScript Integration

Using this package, NEScripts can be executed on any specific netns, making it easy to specify custom actions to execute via the NsAction system.


Motivation

Whilst the 2 packages referenced at the top of this doc for netlink and netns provide all this functionality and more, they are still somewhat low-level packages. This can result in programs that use them extensively needing a lot of wrapper code to make the provided functionality easier and safer to use. This package is that wrapper code.


🚧 WIP

  • Add tests
  • Run tests via actions

# Packages

No description provided by the author
No description provided by the author

# Functions

Do executes a given set of actions in a specified network namespace.
No description provided by the author
No description provided by the author
LADelete will simply delete the link when the action is executed.
LAGeneric allows for a custom LinkAction to be created and then used in a LinkDo call.
LANewBridge creates a new bridge with the given name.
LANewDummy creates a new dummy link with the given name.
LANewGRETap creates a new gretap device with the given name, local IP, and remoteIP.
LANewVeth will create a new veth pair.
LANewVxlan creates a new vxlan link with the given configuration.
LANewWireguard creates a new wireguard link with the given name.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LPAlias creates a link provider that when called, will provide the pre-existing link with the given alias (in the namespace this is called in).
LPIndex creates a link provider that when called, will provide the pre-existing link with the given index (in the namespace this is called in).
LPName creates a link provider that when called, will provide the pre-existing link with the given name (in the namespace this is called in).
NADeleteNamed when executed removes the named netns if it exists.
NADeleteNamedAt when executed removes the named netns if it exists.
NAExecNescript will execute a NEScript in the netns it is called in, most likely the netns of the wrapping NsDo.
NAGeneric allows for a custom action (function) to be performed in a given network namespace.
NAGetLink gets a specific link from the given link provider when the action is called.
NAGetNsFd provides an open file descriptor for the network namespace it is called in.
NALinks returns a list of all the links in the namespace obtained via the given provider.
NANewNs will create a new network namespace and bind it to a named file.
NANewNsAt will create a new network namespace and bind it to a named file in a given directory.
NASetLinkNs moves a link provided by the given link provider to the namespace provided by the ns provider.
NPGeneric provides the means to create custom providers.
NPName returns a netns provider that provides the netns path for a named (mounted) netns.
NPNameAt returns a netns provider that provides the netns path for a named (mounted) netns.
NPNow returns a netns provider that provides the netns path for the process/thread that calls the Provide function.
NPPath returns a netns provider that provides the netns path based on the path given.
NPProcess returns a netns provider that provides the netns path for the process associated with the given process ID.
NPThread returns a netns provider that provides the netns path for the process associated with the given process and thread ID.

# Constants

No description provided by the author
No description provided by the author

# Structs

LinkAction is a singular operation that can be performed on a generic netlink link.
No description provided by the author
NsAction represents an action that should be executed in a namespace via NsDo.
NsProvider offers a approach to obtaining network namespace paths based on given conditions.

# Interfaces

Action represents an entity that has a name and some function (act) that can return an error.

# Type aliases

Namespace is a path to a file associated with a network namespace.
NsFd is a file descriptor for an open Namespace file.