tenus

package module
v0.0.0-...-1f3ed00 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

README

Linux networking in Golang

GoDoc License

tenus is a Golang package which allows you to configure and manage Linux network devices programmatically. It communicates with Linux Kernel via netlink to facilitate creation and configuration of network devices on the Linux host. The package also allows for more advanced network setups with Linux containers including Docker.

tenus uses runc's implementation of netlink protocol. The package only works with newer Linux Kernels (3.10+) which are shipping reasonably new netlink protocol implementation, so if you are running older kernel this package won't be of much use to you I'm afraid. I have developed this package on Ubuntu Trusty Tahr which ships with 3.13+ and verified its functionality on Precise Pangolin with upgraded kernel to version 3.10. I could worked around the netlink issues by using ioctl syscalls, but I decided to prefer "pure netlink" implementation, so suck it old Kernels.

At the moment only functional tests are available, but the interface design should hopefully allow for easy (ish) unit testing in the future. I do appreciate that the package's test coverage is not great at the moment, but the core functionality should be covered. I would massively welcome PRs.

Get started

There is a Vagrantfile available in the repo so using vagrant is the easiest way to get started:

milosgajdos@bimbonet ~ $ git clone https://github.com/milosgajdos83/tenus.git
milosgajdos@bimbonet ~ $ vagrant up

Note using the provided Vagrantfile will take quite a long time to spin the VM as vagrant will setup Ubuntu Trusty VM with all the prerequisities:

  • it will install golang and docker onto the VM
  • it will export GOPATH and go get the tenus package onto the VM
  • it will also "pull" Docker ubuntu image so that you can run the tests once the VM is set up

At the moment running the tests require Docker to be installed, but in the future I'd love to separate tests per interface so that you can run only chosen test sets.

Once the VM is running, cd into particular repo directory and you can run the tests:

milosgajdos@bimbonet ~ $ cd $GOPATH/src/github.com/milosgajdos83/tenus
milosgajdos@bimbonet ~ $ sudo go test

If you don't want to use the provided Vagrantfile, you can simply run your own Linux VM (with 3.10+ kernel) and follow the regular golang development flow:

milosgajdos@bimbonet ~ $ go get github.com/milosgajdos83/tenus
milosgajdos@bimbonet ~ $ cd $GOPATH/src/github.com/milosgajdos83/tenus
milosgajdos@bimbonet ~ $ sudo go test

Once you've got the package and ran the tests (you don't need to run the tests!), you can start hacking. Below you can find simple code samples to get started with the package.

Examples

Below you can find a few code snippets which can help you get started writing your own programs.

The example below shows a simple program example which creates a new network bridge, a new dummy network link and adds it into the bridge.

package main

import (
	"fmt"
	"log"

	"github.com/milosgajdos83/tenus"
)

func main() {
	// Create a new network bridge
	br, err := tenus.NewBridgeWithName("mybridge")
	if err != nil {
		log.Fatal(err)
	}

	// Bring the bridge up
	if err = br.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// Create a dummy link
	dl, err := tenus.NewLink("mydummylink")
	if err != nil {
		log.Fatal(err)
	}

	// Add the dummy link into bridge
	if err = br.AddSlaveIfc(dl.NetInterface()); err != nil {
		log.Fatal(err)
	}

	// Bring the dummy link up
	if err = dl.SetLinkUp(); err != nil {
		fmt.Println(err)
	}
}
New network bridge, veth pair, one peer in Docker

The example below shows how you can create a new network bride, configure its IP address, add a new veth pair and send one of the veth peers into Docker with a given name.

!! You must make sure that particular Docker is runnig if you want the code sample below to work properly !! So before you compile and run the program below you should create a particular docker with the below used name:

milosgajdos@bimbonet ~ $ docker run -i -t --rm --privileged -h vethdckr --name vethdckr ubuntu:14.04 /bin/bash
package main

import (
	"fmt"
	"log"
	"net"

	"github.com/milosgajdos83/tenus"
)

func main() {
	// CREATE BRIDGE AND BRING IT UP
	br, err := tenus.NewBridgeWithName("vethbridge")
	if err != nil {
		log.Fatal(err)
	}

	brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := br.SetLinkIp(brIp, brIpNet); err != nil {
		fmt.Println(err)
	}

	if err = br.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// CREATE VETH PAIR
	veth, err := tenus.NewVethPairWithOptions("myveth01", tenus.VethOptions{PeerName: "myveth02"})
	if err != nil {
		log.Fatal(err)
	}

	// ASSIGN IP ADDRESS TO THE HOST VETH INTERFACE
	vethHostIp, vethHostIpNet, err := net.ParseCIDR("10.0.41.2/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetLinkIp(vethHostIp, vethHostIpNet); err != nil {
		fmt.Println(err)
	}

	// ADD MYVETH01 INTERFACE TO THE MYBRIDGE BRIDGE
	myveth01, err := net.InterfaceByName("myveth01")
	if err != nil {
		log.Fatal(err)
	}

	if err = br.AddSlaveIfc(myveth01); err != nil {
		fmt.Println(err)
	}

	if err = veth.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// PASS VETH PEER INTERFACE TO A RUNNING DOCKER BY PID
	pid, err := tenus.DockerPidByName("vethdckr", "/var/run/docker.sock")
	if err != nil {
		fmt.Println(err)
	}

	if err := veth.SetPeerLinkNsPid(pid); err != nil {
		log.Fatal(err)
	}

	// ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE
	vethGuestIp, vethGuestIpNet, err := net.ParseCIDR("10.0.41.5/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetPeerLinkNetInNs(pid, vethGuestIp, vethGuestIpNet, nil); err != nil {
		log.Fatal(err)
	}
}
Working with existing bridges and interfaces

The following examples show how to retrieve exisiting interfaces as a tenus link and bridge

package main

import (
	"fmt"
	"log"
	"net"

	"github.com/milosgajdos83/tenus"
)

func main() {
	// RETRIEVE EXISTING BRIDGE
	br, err := tenus.BridgeFromName("bridge0")
	if err != nil {
		log.Fatal(err)
	}

	// REMOVING AN IP FROM A BRIDGE INTERFACE (BEFORE RECONFIGURATION)
	brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
	if err != nil {
		log.Fatal(err)
	}
	if err := br.UnsetLinkIp(brIp, brIpNet); err != nil {
		log.Fatal(err)
	}

	// RETRIEVE EXISTING INTERFACE
	dl, err := tenus.NewLinkFrom("eth0")
	if err != nil {
		log.Fatal(err)
	}

	// RENAMING AN INTERFACE BY NAME
	if err := tenus.RenameInterfaceByName("vethPSQSEl", "vethNEWNAME"); err != nil {
		log.Fatal(err)
	}

}
VLAN and MAC VLAN interfaces

You can check out VLAN and Mac VLAN examples, too.

More examples

Repo contains few more code sample in examples folder so make sure to check them out if you're interested.

TODO

This is just a rough beginning of the project which I put together over couple of weeks in my free time. I'd like to integrate this into my own Docker fork and test the advanced netowrking functionality with the core of Docker as oppose to configuring network interfaces from a separate golang program, because advanced networking in Docker was the main motivation for writing this package.

Documentation

More in depth package documentation is available via godoc

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

Constants

This section is empty.

Variables

View Source
var MacVlanModes = map[string]bool{
	"private": true,
	"vepa":    true,
	"bridge":  true,
}

Supported macvlan modes by tenus package

Functions

func AddToBridge

func AddToBridge(netIfc, netBridge *net.Interface) error

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(name string) error

DeleteLink deletes netowrk link from Linux Host It is equivalent of running: ip link delete dev ${name}

func DockerPidByName

func DockerPidByName(name string, dockerHost string) (int, error)

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

func FindInterfaceByMacAddress(macaddr string) (*net.Interface, error)

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 MakeNetInterfaceName(base string) string

func NetInterfaceNameValid

func NetInterfaceNameValid(name string) (bool, error)

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

func NetNsHandle(nspid int) (uintptr, error)

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

func RemoveFromBridge(netIfc *net.Interface) error

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

func RenameInterfaceByName(old string, newName string) error

RenameInterfaceByName renames an interface of given name.

func SetNetNsToPid

func SetNetNsToPid(nspid int) error

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

func (br *Bridge) AddSlaveIfc(ifc *net.Interface) error

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

func (br *Bridge) RemoveSlaveIfc(ifc *net.Interface) error

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

func BridgeFromName(ifcName string) (Bridger, error)

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

func NewBridge() (Bridger, error)

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

func NewBridgeWithName(ifcName string) (Bridger, error)

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 struct {
	// contains filtered or unexported fields
}

Link has a logical network interface

func (l *Link) DeleteLink() error

DeleteLink deletes link interface on Linux host. It is equivalent of running: ip link delete dev ${interface name}

func (*Link) NetInterface

func (l *Link) NetInterface() *net.Interface

NetInterface returns link's logical network interface.

func (*Link) SetLinkDefaultGw

func (l *Link) SetLinkDefaultGw(gw *net.IP) error

SetLinkDefaultGw configures the link's default Gateway. It is equivalent of running: ip route add default via ${ip address}

func (*Link) SetLinkDown

func (l *Link) SetLinkDown() error

SetLinkDown brings the link down. It is equivalent of running: ip link set dev ${interface name} down

func (*Link) SetLinkIp

func (l *Link) SetLinkIp(ip net.IP, network *net.IPNet) error

SetLinkIp configures the link's IP address. It is equivalent of running: ip address add ${address}/${mask} dev ${interface name}

func (*Link) SetLinkMTU

func (l *Link) SetLinkMTU(mtu int) error

SetLinkMTU sets link's MTU. It is equivalent of running: ip link set dev ${interface name} mtu ${MTU value}

func (*Link) SetLinkMacAddress

func (l *Link) SetLinkMacAddress(macaddr string) error

SetLinkMacAddress sets link's MAC address. It is equivalent of running: ip link set dev ${interface name} address ${address}

func (*Link) SetLinkNetInNs

func (l *Link) SetLinkNetInNs(nspid int, ip net.IP, network *net.IPNet, gw *net.IP) error

SetLinkNetInNs configures network settings of the link in network namespace specified by PID.

func (*Link) SetLinkNetNsPid

func (l *Link) SetLinkNetNsPid(nspid int) error

SetLinkNetNsPid moves the link to Network namespace specified by PID.

func (*Link) SetLinkNsFd

func (l *Link) SetLinkNsFd(nspath string) error

SetLinkNsFd sets the link's Linux namespace to the one specified by filesystem path.

func (*Link) SetLinkNsToDocker

func (l *Link) SetLinkNsToDocker(name string, dockerHost string) error

SetLinkNsToDocker sets the link's Linux namespace to a running Docker one specified by Docker name.

func (*Link) SetLinkUp

func (l *Link) SetLinkUp() error

SetLinkUp brings the link up. It is equivalent of running: ip link set dev ${interface name} up

func (*Link) UnsetLinkIp

func (l *Link) UnsetLinkIp(ip net.IP, network *net.IPNet) error

UnsetLinkIp configures the link's IP address. It is equivalent of running: ip address del ${address}/${mask} dev ${interface 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(ifcName string) (Linker, error)

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

func NewLinkFrom(ifcName string) (Linker, error)

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 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(masterDev string) (MacVlaner, error)

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 struct {
	*MacVlanLink
}

MacVtapLink is MacVlanLink. It implements MacVtaper interface

type MacVtaper

type MacVtaper interface {
	MacVlaner
}

MacVtaper embeds MacVlaner interface

func NewMacVtapLink(masterDev string) (MacVtaper, error)

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 NetworkOptions

type NetworkOptions struct {
	IpAddr string
	Gw     string
	Routes []netlink.Route
}

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 (veth *VethPair) DeletePeerLink() error

DeletePeerLink deletes peer link. It also deletes the other peer interface in VethPair

func (*VethPair) NetInterface

func (veth *VethPair) NetInterface() *net.Interface

NetInterface returns veth link's primary network interface

func (*VethPair) PeerNetInterface

func (veth *VethPair) PeerNetInterface() *net.Interface

NetInterface returns veth link's peer network interface

func (*VethPair) SetPeerLinkIp

func (veth *VethPair) SetPeerLinkIp(ip net.IP, nw *net.IPNet) error

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

func (veth *VethPair) SetPeerLinkNsFd(nspath string) error

SetPeerLinkNsFd sends peer link into container specified by path

func (*VethPair) SetPeerLinkNsPid

func (veth *VethPair) SetPeerLinkNsPid(nspid int) error

SetPeerLinkNsPid sends peer link into container specified by PID

func (*VethPair) SetPeerLinkNsToDocker

func (veth *VethPair) SetPeerLinkNsToDocker(name string, dockerHost string) error

SetPeerLinkNsToDocker sends peer link into Docker

func (*VethPair) SetPeerLinkUp

func (veth *VethPair) SetPeerLinkUp() error

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

func NewVethPair() (Vether, error)

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 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) Id

func (vln *VlanLink) Id() uint16

Id returns vlan link's vlan tag id

func (*VlanLink) MasterNetInterface

func (vln *VlanLink) MasterNetInterface() *net.Interface

MasterNetInterface returns vlan link's master network interface

func (*VlanLink) NetInterface

func (vln *VlanLink) NetInterface() *net.Interface

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(masterDev string, id uint16) (Vlaner, error)

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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