Documentation ¶
Overview ¶
Package tenus allows to configure and manage Linux network devices programmatically.
You can create, configure and manage various advanced Linux network setups directly from your Go code. tenus also allows you to configure advanced network setups with Linux containers including Docker. It leverages Linux Kernenl's netlink facility and exposes easier to work with programming API than the one provided by netlink.
Actual implementations are in: link_linux.go, bridge_linux.go, veth_linux.go, vlan_linux.go and macvlan_linux.go
Index ¶
- Variables
- func AddToBridge(netIfc, netBridge *net.Interface) error
- func DeleteLink(name string) error
- func DockerPidByName(name string, dockerHost string) (int, error)
- func FindInterfaceByMacAddress(macaddr string) (*net.Interface, error)
- func MakeNetInterfaceName(base string) string
- func NetInterfaceNameValid(name string) (bool, error)
- func NetNsHandle(nspid int) (uintptr, error)
- func RemoveFromBridge(netIfc *net.Interface) error
- func RenameInterfaceByName(old string, newName string) error
- func SetNetNsToPid(nspid int) error
- type Bridge
- type Bridger
- type Link
- func (l *Link) DeleteLink() error
- func (l *Link) NetInterface() *net.Interface
- func (l *Link) SetLinkDefaultGw(gw *net.IP) error
- func (l *Link) SetLinkDown() error
- func (l *Link) SetLinkIp(ip net.IP, network *net.IPNet) error
- func (l *Link) SetLinkMTU(mtu int) error
- func (l *Link) SetLinkMacAddress(macaddr string) error
- func (l *Link) SetLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error
- func (l *Link) SetLinkNetNsPid(nspid int) error
- func (l *Link) SetLinkNsFd(nspath string) error
- func (l *Link) SetLinkNsToDocker(name string, dockerHost string) error
- func (l *Link) SetLinkUp() error
- func (l *Link) UnsetLinkIp(ip net.IP, network *net.IPNet) error
- type LinkOptions
- type Linker
- type MacVlanLink
- type MacVlanOptions
- type MacVlaner
- type MacVtapLink
- type MacVtaper
- type NetworkOptions
- type VethOptions
- type VethPair
- func (veth *VethPair) DeletePeerLink() error
- func (veth *VethPair) NetInterface() *net.Interface
- func (veth *VethPair) PeerNetInterface() *net.Interface
- func (veth *VethPair) SetPeerLinkIp(ip net.IP, nw *net.IPNet) error
- func (veth *VethPair) SetPeerLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error
- func (veth *VethPair) SetPeerLinkNsFd(nspath string) error
- func (veth *VethPair) SetPeerLinkNsPid(nspid int) error
- func (veth *VethPair) SetPeerLinkNsToDocker(name string, dockerHost string) error
- func (veth *VethPair) SetPeerLinkUp() error
- type Vether
- type VlanLink
- type VlanOptions
- type Vlaner
Constants ¶
This section is empty.
Variables ¶
Supported macvlan modes by tenus package
Functions ¶
func AddToBridge ¶
AddToBridge adds network interfaces to network bridge. It is equivalent of running: ip link set ${netIfc name} master ${netBridge name} It returns error when it fails to add the network interface to bridge.
func DeleteLink ¶
DeleteLink deletes netowrk link from Linux Host It is equivalent of running: ip link delete dev ${name}
func DockerPidByName ¶
DockerPidByName returns PID of the running docker container. It accepts Docker container name and Docker host as parameters and queries Docker API via HTTP. Docker host passed as an argument can be either full path to Docker UNIX socket or HOST:PORT address string. It returns error if Docker container can not be found or if an error occurs when querying Docker API.
func FindInterfaceByMacAddress ¶
FindInterfaceByMacAddress returns *net.Interface which has a given MAC address assigned. It returns nil and error if invalid MAC address is supplied or if there is no network interface with the given MAC address assigned on Linux host.
func MakeNetInterfaceName ¶
func NetInterfaceNameValid ¶
NetInterfaceNameValid checks if the network interface name is valid. It accepts interface name as a string. It returns error if invalid interface name is supplied.
func NetNsHandle ¶
NetNsHandle returns a file descriptor handle for network namespace specified by PID. It returns error if network namespace could not be found or if network namespace path could not be opened.
func RemoveFromBridge ¶
AddToBridge adds network interfaces to network bridge. It is equivalent of running: ip link set dev ${netIfc name} nomaster It returns error when it fails to remove the network interface from the bridge.
func RenameInterfaceByName ¶
RenameInterfaceByName renames an interface of given name.
func SetNetNsToPid ¶
SetNetNsToPid sets network namespace to the one specied by PID. It returns error if the network namespace could not be set.
Types ¶
type Bridge ¶
type Bridge struct { Link // contains filtered or unexported fields }
Bridge is Link which has zero or more slave network interfaces. Bridge implements Bridger interface.
func (*Bridge) AddSlaveIfc ¶
AddSlaveIfc adds network interface to network bridge. It is equivalent of running: ip link set ${ifc name} master ${bridge name} It returns error if the network interface could not be added to the bridge.
func (*Bridge) RemoveSlaveIfc ¶
RemoveSlaveIfc removes network interface from the network bridge. It is equivalent of running: ip link set dev ${netIfc name} nomaster It returns error if the network interface is not in the bridge or it could not be removed from the bridge.
type Bridger ¶
type Bridger interface { // Linker interface Linker // AddSlaveIfc adds network interface to the network bridge AddSlaveIfc(*net.Interface) error //RemoveSlaveIfc removes network interface from the network bridge RemoveSlaveIfc(*net.Interface) error }
Bridger embeds Linker interface and adds one extra function.
func BridgeFromName ¶
BridgeFromName returns a tenus network bridge from an existing bridge of given name on the Linux host. It returns error if the bridge of the given name cannot be found.
func NewBridge ¶
NewBridge creates new network bridge on Linux host.
It is equivalent of running: ip link add name br${RANDOM STRING} type bridge NewBridge returns Bridger which is initialized to a pointer of type Bridge if the bridge was created successfully on the Linux host. Newly created bridge is assigned a random name starting with "br". It returns error if the bridge could not be created.
func NewBridgeWithName ¶
NewBridge creates new network bridge on Linux host with the name passed as a parameter. It is equivalent of running: ip link add name ${ifcName} type bridge It returns error if the bridge can not be created.
type Link ¶
type Link struct {
// contains filtered or unexported fields
}
Link has a logical network interface
func (*Link) DeleteLink ¶
DeleteLink deletes link interface on Linux host. It is equivalent of running: ip link delete dev ${interface name}
func (*Link) NetInterface ¶
NetInterface returns link's logical network interface.
func (*Link) SetLinkDefaultGw ¶
SetLinkDefaultGw configures the link's default Gateway. It is equivalent of running: ip route add default via ${ip address}
func (*Link) SetLinkDown ¶
SetLinkDown brings the link down. It is equivalent of running: ip link set dev ${interface name} down
func (*Link) SetLinkIp ¶
SetLinkIp configures the link's IP address. It is equivalent of running: ip address add ${address}/${mask} dev ${interface name}
func (*Link) SetLinkMTU ¶
SetLinkMTU sets link's MTU. It is equivalent of running: ip link set dev ${interface name} mtu ${MTU value}
func (*Link) SetLinkMacAddress ¶
SetLinkMacAddress sets link's MAC address. It is equivalent of running: ip link set dev ${interface name} address ${address}
func (*Link) SetLinkNetInNs ¶
SetLinkNetInNs configures network settings of the link in network namespace specified by PID.
func (*Link) SetLinkNetNsPid ¶
SetLinkNetNsPid moves the link to Network namespace specified by PID.
func (*Link) SetLinkNsFd ¶
SetLinkNsFd sets the link's Linux namespace to the one specified by filesystem path.
func (*Link) SetLinkNsToDocker ¶
SetLinkNsToDocker sets the link's Linux namespace to a running Docker one specified by Docker name.
type LinkOptions ¶
type LinkOptions struct { // MAC address MacAddr string // Maximum Transmission Unit MTU int // Link network flags i.e. FlagUp, FlagLoopback, FlagMulticast Flags net.Flags // Network namespace in which the network link should be created Ns int }
LinkOptions allows you to specify network link options.
type Linker ¶
type Linker interface { // NetInterface returns the link's logical network interface NetInterface() *net.Interface // DeleteLink deletes the link from Linux host DeleteLink() error // SetLinkMTU sets the link's MTU. SetLinkMTU(int) error // SetLinkMacAddress sets the link's MAC address. SetLinkMacAddress(string) error // SetLinkUp brings the link up SetLinkUp() error // SetLinkDown brings the link down SetLinkDown() error // SetLinkIp configures the link's IP address SetLinkIp(net.IP, *net.IPNet) error // UnsetLinkIp remove and IP address from the link UnsetLinkIp(net.IP, *net.IPNet) error // SetLinkDefaultGw configures the link's default gateway SetLinkDefaultGw(*net.IP) error // SetLinkNetNsPid moves the link to network namespace specified by PID SetLinkNetNsPid(int) error // SetLinkNetInNs configures network settings of the link in network namespace SetLinkNetInNs(int, net.IP, *net.IPNet, *net.IP) error }
Linker is a generic Linux network link
func NewLink ¶
NewLink creates new network link on Linux host.
It is equivalent of running: ip link add name ${ifcName} type dummy NewLink returns Linker which is initialized to a pointer of type Link if the link was created successfully on the Linux host. It returns error if the network link could not be created on Linux host.
func NewLinkFrom ¶
NewLinkFrom creates new tenus link on Linux host from an existing interface of given name
func NewLinkWithOptions ¶
func NewLinkWithOptions(ifcName string, opts LinkOptions) (Linker, error)
NewLinkWithOptions creates new network link on Linux host and sets some of its network parameters passed in as LinkOptions
Calling NewLinkWithOptions is equivalent of running following commands one after another if particular option is passed in as a parameter:
ip link add name ${ifcName} type dummy ip link set dev ${ifcName} address ${MAC address} ip link set dev ${ifcName} mtu ${MTU value} ip link set dev ${ifcName} up
NewLinkWithOptions returns Linker which is initialized to a pointer of type Link if the network link with given LinkOptions was created successfully on the Linux host. It attempts to delete the link if any of the LinkOptions are incorrect or if setting the options failed and returns error.
type MacVlanLink ¶
type MacVlanLink struct { Link // contains filtered or unexported fields }
MacVlanLink is Link which has a master network device and operates in a given network mode. It implements MacVlaner interface.
func (*MacVlanLink) MasterNetInterface ¶
func (macvln *MacVlanLink) MasterNetInterface() *net.Interface
MasterNetInterface returns macvlan link's master network interface
func (*MacVlanLink) Mode ¶
func (macvln *MacVlanLink) Mode() string
Mode returns macvlan link's network operation mode
func (*MacVlanLink) NetInterface ¶
func (macvln *MacVlanLink) NetInterface() *net.Interface
NetInterface returns macvlan link's network interface
type MacVlanOptions ¶
type MacVlanOptions struct { // macvlan device name Dev string // macvlan mode Mode string // MAC address MacAddr string }
MacVlanOptions allows you to specify some options for macvlan link.
type MacVlaner ¶
type MacVlaner interface { // Linker interface Linker // MasterNetInterface returns macvlan master network device MasterNetInterface() *net.Interface // Mode returns macvlan link's network mode Mode() string }
MacVlaner embeds Linker interface and adds few more functions.
func NewMacVlanLink ¶
NewMacVlanLink creates macvlan network link
It is equivalent of running:
ip link add name mc${RANDOM STRING} link ${master interface} type macvlan
NewMacVlanLink returns MacVlaner which is initialized to a pointer of type MacVlanLink if the macvlan link was created successfully on the Linux host. Newly created link is assigned a random name starting with "mc". It sets the macvlan mode to "bridge" mode which is a default. It returns error if the link could not be created.
func NewMacVlanLinkWithOptions ¶
func NewMacVlanLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVlaner, error)
NewMacVlanLinkWithOptions creates macvlan network link and sets som of its network parameters passed in as MacVlanOptions.
It is equivalent of running:
ip link add name ${macvlan name} link ${master interface} address ${macaddress} type macvlan mode ${mode}
NewMacVlanLinkWithOptions returns MacVlaner which is initialized to a pointer of type MacVlanLink if the macvlan link was created successfully on the Linux host. If particular option is empty, it sets default value if possible. It returns error if the macvlan link could not be created or if incorrect options have been passed.
type MacVtapLink ¶
type MacVtapLink struct {
*MacVlanLink
}
MacVtapLink is MacVlanLink. It implements MacVtaper interface
type MacVtaper ¶
type MacVtaper interface { MacVlaner }
MacVtaper embeds MacVlaner interface
func NewMacVtapLink ¶
NewMacVtapLink creates macvtap network link
It is equivalent of running:
ip link add name mvt${RANDOM STRING} link ${master interface} type macvtap
NewMacVtapLink returns MacVtaper which is initialized to a pointer of type MacVtapLink if the macvtap link was created successfully on the Linux host. Newly created link is assigned a random name starting with "mvt". It sets the macvlan mode to "bridge" which is a default. It returns error if the link could not be created.
func NewMacVtapLinkWithOptions ¶
func NewMacVtapLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVtaper, error)
NewMacVtapLinkWithOptions creates macvtap network link and can set some of its network parameters passed in as MacVlanOptions.
It is equivalent of running:
ip link add name ${macvlan name} link ${master interface} address ${macaddress} type macvtap mode ${mode}
NewMacVtapLinkWithOptions returns MacVtaper which is initialized to a pointer of type MacVtapLink if the macvtap link was created successfully on the Linux host. It returns error if the macvtap link could not be created.
type VethOptions ¶
type VethOptions struct { // Veth pair's peer interface name PeerName string // TX queue length TxQueueLen int }
VethOptions allows you to specify options for veth link.
type VethPair ¶
type VethPair struct { Link // contains filtered or unexported fields }
VethPair is a Link. Veth links are created in pairs called peers.
func (*VethPair) DeletePeerLink ¶
DeletePeerLink deletes peer link. It also deletes the other peer interface in VethPair
func (*VethPair) NetInterface ¶
NetInterface returns veth link's primary network interface
func (*VethPair) PeerNetInterface ¶
NetInterface returns veth link's peer network interface
func (*VethPair) SetPeerLinkIp ¶
SetPeerLinkIp configures peer link's IP address
func (*VethPair) SetPeerLinkNetInNs ¶
func (veth *VethPair) SetPeerLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error
SetPeerLinkNetInNs configures peer link's IP network in network namespace specified by PID
func (*VethPair) SetPeerLinkNsFd ¶
SetPeerLinkNsFd sends peer link into container specified by path
func (*VethPair) SetPeerLinkNsPid ¶
SetPeerLinkNsPid sends peer link into container specified by PID
func (*VethPair) SetPeerLinkNsToDocker ¶
SetPeerLinkNsToDocker sends peer link into Docker
func (*VethPair) SetPeerLinkUp ¶
SetPeerLinkUp sets peer link up
type Vether ¶
type Vether interface { // Linker interface Linker // PeerNetInterface returns peer network interface PeerNetInterface() *net.Interface // SetPeerLinkUp sets peer link up - which also brings up the other peer in VethPair SetPeerLinkUp() error // DeletePeerLink deletes peer link - this also deletes the other peer in VethPair DeletePeerLink() error // SetPeerLinkIp configures peer link's IP address SetPeerLinkIp(net.IP, *net.IPNet) error // SetPeerLinkNsToDocker sends peer link into Docker SetPeerLinkNsToDocker(string, string) error // SetPeerLinkNsPid sends peer link into container specified by PID SetPeerLinkNsPid(int) error // SetPeerLinkNsFd sends peer link into container specified by path SetPeerLinkNsFd(string) error // SetPeerLinkNetInNs configures peer link's IP network in network namespace specified by PID SetPeerLinkNetInNs(int, net.IP, *net.IPNet, *net.IP) error }
Vether embeds Linker interface and adds few more functions mostly to handle peer link interface
func NewVethPair ¶
NewVethPair creates a pair of veth network links.
It is equivalent of running:
ip link add name veth${RANDOM STRING} type veth peer name veth${RANDOM STRING}.
NewVethPair returns Vether which is initialized to a pointer of type VethPair if the veth link was successfully created on Linux host. Newly created pair of veth links are assigned random names starting with "veth". NewVethPair returns error if the veth pair could not be created.
func NewVethPairWithOptions ¶
func NewVethPairWithOptions(ifcName string, opts VethOptions) (Vether, error)
NewVethPairWithOptions creates a pair of veth network links.
It is equivalent of running:
ip link add name ${first device name} type veth peer name ${second device name}
NewVethPairWithOptions returns Vether which is initialized to a pointer of type VethPair if the veth link was successfully created on the Linux host. It accepts VethOptions which allow you to set peer interface name. It returns error if the veth pair could not be created.
type VlanLink ¶
type VlanLink struct { Link // contains filtered or unexported fields }
VlanLink is a Link which has a master network device. Each VlanLink has a VLAN tag id
func (*VlanLink) MasterNetInterface ¶
MasterNetInterface returns vlan link's master network interface
func (*VlanLink) NetInterface ¶
NetInterface returns vlan link's network interface
type VlanOptions ¶
type VlanOptions struct { // Name of the vlan device Dev string // VLAN tag id Id uint16 // MAC address MacAddr string }
VlanOptions allows you to specify options for vlan link.
type Vlaner ¶
type Vlaner interface { // Linker interface Linker // MasterNetInterface returns vlan master network interface MasterNetInterface() *net.Interface // Id returns VLAN tag Id() uint16 }
Vlaner is interface which embeds Linker interface and adds few more functions.
func NewVlanLink ¶
NewVlanLink creates vlan network link.
It is equivalent of running:
ip link add name vlan${RANDOM STRING} link ${master interface name} type vlan id ${tag}
NewVlanLink returns Vlaner which is initialized to a pointer of type VlanLink if the vlan link was successfully created on the Linux host. Newly created link is assigned a random name starting with "vlan". It returns error if the link can not be created.
func NewVlanLinkWithOptions ¶
func NewVlanLinkWithOptions(masterDev string, opts VlanOptions) (Vlaner, error)
NewVlanLinkWithOptions creates vlan network link and sets some of its network parameters to values passed in as VlanOptions
It is equivalent of running:
ip link add name ${vlan name} link ${master interface} address ${macaddress} type vlan id ${tag}
NewVlanLinkWithOptions returns Vlaner which is initialized to a pointer of type VlanLink if the vlan link was created successfully on the Linux host. It accepts VlanOptions which allow you to set link's options. It returns error if the link could not be created.