netlink

package module
v0.0.0-...-d36f557 Latest Latest
Warning

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

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

README

Build Status GoDoc

The netlink package provides a simple netlink library for go. Netlink is the interface a user-space program in linux uses to communicate with the kernel. It can be used to add and remove interfaces, set ip addresses and routes, and configure ipsec. Netlink communication requires elevated privileges, so in most cases this code needs to be run as root. Since low-level netlink messages are inscrutable at best, the library attempts to provide an api that is loosely modeled on the CLI provided by iproute2. Actions like ip link add will be accomplished via a similarly named function like AddLink(). This library began its life as a fork of the netlink functionality in docker/libcontainer but was heavily rewritten to improve testability, performance, and to add new functionality like ipsec xfrm handling.

Local Build and Test

You can use go get command:

go get github.com/vishvananda/netlink

Testing dependencies:

go get github.com/vishvananda/netns

Testing (requires root):

sudo -E go test github.com/vishvananda/netlink

Examples

Add a new bridge and add eth1 into it:

package main

import (
    "net"
    "github.com/vishvananda/netlink"
)

func main() {
    la := netlink.NewLinkAttrs()
    la.Name = "foo"
    mybridge := &netlink.Bridge{la}}
    _ := netlink.LinkAdd(mybridge)
    eth1, _ := netlink.LinkByName("eth1")
    netlink.LinkSetMaster(eth1, mybridge)
}

Note NewLinkAttrs constructor, it sets default values in structure. For now it sets only TxQLen to -1, so kernel will set default by itself. If you're using simple initialization(LinkAttrs{Name: "foo"}) TxQLen will be set to 0 unless you specify it like LinkAttrs{Name: "foo", TxQLen: 1000}.

Add a new ip address to loopback:

package main

import (
    "net"
    "github.com/vishvananda/netlink"
)

func main() {
    lo, _ := netlink.LinkByName("lo")
    addr, _ := netlink.ParseAddr("169.254.169.254/32")
    netlink.AddrAdd(lo, addr)
}

Future Work

Many pieces of netlink are not yet fully supported in the high-level interface. Aspects of virtually all of the high-level objects don't exist. Many of the underlying primitives are there, so its a matter of putting the right fields into the high-level objects and making sure that they are serialized and deserialized correctly in the Add and List methods.

There are also a few pieces of low level netlink functionality that still need to be implemented. Routing rules are not in place and some of the more advanced link types. Hopefully there is decent structure and testing in place to make these fairly straightforward to add.

Documentation

Overview

Package netlink provides a simple library for netlink. Netlink is the interface a user-space program in linux uses to communicate with the kernel. It can be used to add and remove interfaces, set up ip addresses and routes, and confiugre ipsec. Netlink communication requires elevated privileges, so in most cases this code needs to be run as root. The low level primitives for netlink are contained in the nl subpackage. This package attempts to provide a high-level interface that is loosly modeled on the iproute2 cli.

Index

Constants

View Source
const (
	BOND_MODE_MASK uint64 = 1 << (1 + iota)
	BOND_ACTIVE_SLAVE_MASK
	BOND_MIIMON_MASK
	BOND_UPDELAY_MASK
	BOND_DOWNDELAY_MASK
	BOND_USE_CARRIER_MASK
	BOND_ARP_INTERVAL_MASK
	BOND_ARP_VALIDATE_MASK
	BOND_ARP_ALL_TARGETS_MASK
	BOND_PRIMARY_MASK
	BOND_PRIMARY_RESELECT_MASK
	BOND_FAIL_OVER_MAC_MASK
	BOND_XMIT_HASH_POLICY_MASK
	BOND_RESEND_IGMP_MASK
	BOND_NUM_PEER_NOTIF_MASK
	BOND_ALL_SLAVES_ACTIVE_MASK
	BOND_MIN_LINKS_MASK
	BOND_LP_INTERVAL_MASK
	BOND_PACKETS_PER_SLAVE_MASK
	BOND_LACP_RATE_MASK
	BOND_AD_SELECT_MASK
)

Flag mask for bond options. Bond.Flagmask must be set to on for option to work.

View Source
const (
	SizeOfIfReq = 40
	IFNAMSIZ    = 16
)

ideally golang.org/x/sys/unix would define IfReq but it only has IFNAMSIZ, hence this minimalistic implementation

View Source
const (
	NDA_UNSPEC = iota
	NDA_DST
	NDA_LLADDR
	NDA_CACHEINFO
	NDA_PROBES
	NDA_VLAN
	NDA_PORT
	NDA_VNI
	NDA_IFINDEX
	NDA_MAX = NDA_IFINDEX
)
View Source
const (
	NUD_NONE       = 0x00
	NUD_INCOMPLETE = 0x01
	NUD_REACHABLE  = 0x02
	NUD_STALE      = 0x04
	NUD_DELAY      = 0x08
	NUD_PROBE      = 0x10
	NUD_FAILED     = 0x20
	NUD_NOARP      = 0x40
	NUD_PERMANENT  = 0x80
)

Neighbor Cache Entry States.

View Source
const (
	NTF_USE    = 0x01
	NTF_SELF   = 0x02
	NTF_MASTER = 0x04
	NTF_PROXY  = 0x08
	NTF_ROUTER = 0x80
)

Neighbor Flags

View Source
const (
	FAMILY_ALL = nl.FAMILY_ALL
	FAMILY_V4  = nl.FAMILY_V4
	FAMILY_V6  = nl.FAMILY_V6
)

Family type definitions

View Source
const (
	HANDLE_NONE      = 0
	HANDLE_INGRESS   = 0xFFFFFFF1
	HANDLE_CLSACT    = HANDLE_INGRESS
	HANDLE_ROOT      = 0xFFFFFFFF
	PRIORITY_MAP_LEN = 16
)
View Source
const (
	HANDLE_MIN_INGRESS = 0xFFFFFFF2
	HANDLE_MIN_EGRESS  = 0xFFFFFFF3
)
View Source
const (
	RT_FILTER_PROTOCOL uint64 = 1 << (1 + iota)
	RT_FILTER_SCOPE
	RT_FILTER_TYPE
	RT_FILTER_TOS
	RT_FILTER_IIF
	RT_FILTER_OIF
	RT_FILTER_DST
	RT_FILTER_SRC
	RT_FILTER_GW
	RT_FILTER_TABLE
)
View Source
const IFA_FLAGS = 0x8

IFA_FLAGS is a u32 attribute.

View Source
const SizeofLinkStats = 0x5c
View Source
const (
	TIME_UNITS_PER_SEC = 1000000
)

Variables

View Source
var StringToBondLacpRateMap = map[string]BondLacpRate{
	"slow": BOND_LACP_RATE_SLOW,
	"fast": BOND_LACP_RATE_FAST,
}
View Source
var StringToBondModeMap = map[string]BondMode{
	"802.3ad":       BOND_MODE_802_3AD,
	"balance-rr":    BOND_MODE_BALANCE_RR,
	"active-backup": BOND_MODE_ACTIVE_BACKUP,
	"balance-xor":   BOND_MODE_BALANCE_XOR,
	"broadcast":     BOND_MODE_BROADCAST,
	"balance-tlb":   BOND_MODE_BALANCE_TLB,
	"balance-alb":   BOND_MODE_BALANCE_ALB,
}

Functions

func AddrAdd

func AddrAdd(link Link, addr *Addr) error

AddrAdd will add an IP address to a link device. Equivalent to: `ip addr add $addr dev $link`

func AddrDel

func AddrDel(link Link, addr *Addr) error

AddrDel will delete an IP address from a link device. Equivalent to: `ip addr del $addr dev $link`

func AddrSubscribe

func AddrSubscribe(ch chan<- AddrUpdate, done <-chan struct{}) error

AddrSubscribe takes a chan down which notifications will be sent when addresses change. Close the 'done' chan to stop subscription.

func AddrSubscribeAt

func AddrSubscribeAt(ns netns.NsHandle, ch chan<- AddrUpdate, done <-chan struct{}) error

AddrSubscribeAt works like AddrSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).

func AdjustSize

func AdjustSize(sz uint, mpu uint, linklayer int) uint

func AlignToAtm

func AlignToAtm(size uint) uint

func CalcRtable

func CalcRtable(rate *nl.TcRateSpec, rtab [256]uint32, cellLog int, mtu uint32, linklayer int) int

func ClassAdd

func ClassAdd(class Class) error

ClassAdd will add a class to the system. Equivalent to: `tc class add $class`

func ClassChange

func ClassChange(class Class) error

ClassChange will change a class in place Equivalent to: `tc class change $class` The parent and handle MUST NOT be changed.

func ClassDel

func ClassDel(class Class) error

ClassDel will delete a class from the system. Equivalent to: `tc class del $class`

func ClassReplace

func ClassReplace(class Class) error

ClassReplace will replace a class to the system. quivalent to: `tc class replace $class` The handle MAY be changed. If a class already exist with this parent/handle pair, the class is changed. If a class does not already exist with this parent/handle, a new class is created.

func ClockFactor

func ClockFactor() float64

func DeserializeRtab

func DeserializeRtab(b []byte) [256]uint32

func EncodeActions

func EncodeActions(attr *nl.RtAttr, actions []Action) error

func FilterAdd

func FilterAdd(filter Filter) error

FilterAdd will add a filter to the system. Equivalent to: `tc filter add $filter`

func FilterDel

func FilterDel(filter Filter) error

FilterDel will delete a filter from the system. Equivalent to: `tc filter del $filter`

func HandleStr

func HandleStr(handle uint32) string

func Hz

func Hz() float64

func LinkAdd

func LinkAdd(link Link) error

LinkAdd adds a new link device. The type and features of the device are taken from the parameters in the link object. Equivalent to: `ip link add $link`

func LinkDel

func LinkDel(link Link) error

LinkDel deletes link device. Either Index or Name must be set in the link object for it to be deleted. The other values are ignored. Equivalent to: `ip link del $link`

func LinkSetAlias

func LinkSetAlias(link Link, name string) error

LinkSetAlias sets the alias of the link device. Equivalent to: `ip link set dev $link alias $name`

func LinkSetDown

func LinkSetDown(link Link) error

LinkSetDown disables link device. Equivalent to: `ip link set $link down`

func LinkSetFastLeave

func LinkSetFastLeave(link Link, mode bool) error

func LinkSetFlood

func LinkSetFlood(link Link, mode bool) error

func LinkSetGuard

func LinkSetGuard(link Link, mode bool) error

func LinkSetHairpin

func LinkSetHairpin(link Link, mode bool) error

func LinkSetHardwareAddr

func LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error

LinkSetHardwareAddr sets the hardware address of the link device. Equivalent to: `ip link set $link address $hwaddr`

func LinkSetLearning

func LinkSetLearning(link Link, mode bool) error

func LinkSetMTU

func LinkSetMTU(link Link, mtu int) error

LinkSetMTU sets the mtu of the link device. Equivalent to: `ip link set $link mtu $mtu`

func LinkSetMaster

func LinkSetMaster(link Link, master *Bridge) error

LinkSetMaster sets the master of the link device. Equivalent to: `ip link set $link master $master`

func LinkSetMasterByIndex

func LinkSetMasterByIndex(link Link, masterIndex int) error

LinkSetMasterByIndex sets the master of the link device. Equivalent to: `ip link set $link master $master`

func LinkSetName

func LinkSetName(link Link, name string) error

LinkSetName sets the name of the link device. Equivalent to: `ip link set $link name $name`

func LinkSetNoMaster

func LinkSetNoMaster(link Link) error

LinkSetNoMaster removes the master of the link device. Equivalent to: `ip link set $link nomaster`

func LinkSetNsFd

func LinkSetNsFd(link Link, fd int) error

LinkSetNsFd puts the device into a new network namespace. The fd must be an open file descriptor to a network namespace. Similar to: `ip link set $link netns $ns`

func LinkSetNsPid

func LinkSetNsPid(link Link, nspid int) error

LinkSetNsPid puts the device into a new network namespace. The pid must be a pid of a running process. Equivalent to: `ip link set $link netns $pid`

func LinkSetRootBlock

func LinkSetRootBlock(link Link, mode bool) error

func LinkSetUp

func LinkSetUp(link Link) error

LinkSetUp enables the link device. Equivalent to: `ip link set $link up`

func LinkSetVfHardwareAddr

func LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error

LinkSetVfHardwareAddr sets the hardware address of a vf for the link. Equivalent to: `ip link set $link vf $vf mac $hwaddr`

func LinkSetVfVlan

func LinkSetVfVlan(link Link, vf, vlan int) error

LinkSetVfVlan sets the vlan of a vf for the link. Equivalent to: `ip link set $link vf $vf vlan $vlan`

func LinkSubscribe

func LinkSubscribe(ch chan<- LinkUpdate, done <-chan struct{}) error

LinkSubscribe takes a chan down which notifications will be sent when links change. Close the 'done' chan to stop subscription.

func LinkSubscribeAt

func LinkSubscribeAt(ns netns.NsHandle, ch chan<- LinkUpdate, done <-chan struct{}) error

LinkSubscribeAt works like LinkSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).

func MajorMinor

func MajorMinor(handle uint32) (uint16, uint16)

func MakeHandle

func MakeHandle(major, minor uint16) uint32

func NeighAdd

func NeighAdd(neigh *Neigh) error

NeighAdd will add an IP to MAC mapping to the ARP table Equivalent to: `ip neigh add ....`

func NeighAppend

func NeighAppend(neigh *Neigh) error

NeighAppend will append an entry to FDB Equivalent to: `bridge fdb append...`

func NeighDel

func NeighDel(neigh *Neigh) error

NeighDel will delete an IP address from a link device. Equivalent to: `ip addr del $addr dev $link`

func NeighSet

func NeighSet(neigh *Neigh) error

NeighSet will add or replace an IP to MAC mapping to the ARP table Equivalent to: `ip neigh replace....`

func NewIPNet

func NewIPNet(ip net.IP) *net.IPNet

NewIPNet generates an IPNet from an ip address using a netmask of 32 or 128.

func ParseIPNet

func ParseIPNet(s string) (*net.IPNet, error)

ParseIPNet parses a string in ip/net format and returns a net.IPNet. This is valuable because addresses in netlink are often IPNets and ParseCIDR returns an IPNet with the IP part set to the base IP of the range.

func Percentage2u32

func Percentage2u32(percentage float32) uint32

func QdiscAdd

func QdiscAdd(qdisc Qdisc) error

QdiscAdd will add a qdisc to the system. Equivalent to: `tc qdisc add $qdisc`

func QdiscChange

func QdiscChange(qdisc Qdisc) error

QdiscChange will change a qdisc in place Equivalent to: `tc qdisc change $qdisc` The parent and handle MUST NOT be changed.

func QdiscDel

func QdiscDel(qdisc Qdisc) error

QdiscDel will delete a qdisc from the system. Equivalent to: `tc qdisc del $qdisc`

func QdiscReplace

func QdiscReplace(qdisc Qdisc) error

QdiscReplace will replace a qdisc to the system. Equivalent to: `tc qdisc replace $qdisc` The handle MUST change.

func RouteAdd

func RouteAdd(route *Route) error

RouteAdd will add a route to the system. Equivalent to: `ip route add $route`

func RouteDel

func RouteDel(route *Route) error

RouteDel will delete a route from the system. Equivalent to: `ip route del $route`

func RouteSubscribe

func RouteSubscribe(ch chan<- RouteUpdate, done <-chan struct{}) error

RouteSubscribe takes a chan down which notifications will be sent when routes are added or deleted. Close the 'done' chan to stop subscription.

func RouteSubscribeAt

func RouteSubscribeAt(ns netns.NsHandle, ch chan<- RouteUpdate, done <-chan struct{}) error

RouteSubscribeAt works like RouteSubscribe plus it allows the caller to choose the network namespace in which to subscribe (ns).

func RuleAdd

func RuleAdd(rule *Rule) error

RuleAdd adds a rule to the system. Equivalent to: ip rule add

func RuleDel

func RuleDel(rule *Rule) error

RuleDel deletes a rule from the system. Equivalent to: ip rule del

func SerializeRtab

func SerializeRtab(rtab [256]uint32) []byte

func TickInUsec

func TickInUsec() float64

func XfrmPolicyAdd

func XfrmPolicyAdd(policy *XfrmPolicy) error

XfrmPolicyAdd will add an xfrm policy to the system. Equivalent to: `ip xfrm policy add $policy`

func XfrmPolicyDel

func XfrmPolicyDel(policy *XfrmPolicy) error

XfrmPolicyDel will delete an xfrm policy from the system. Note that the Tmpls are ignored when matching the policy to delete. Equivalent to: `ip xfrm policy del $policy`

func XfrmPolicyFlush

func XfrmPolicyFlush() error

XfrmPolicyFlush will flush the policies on the system. Equivalent to: `ip xfrm policy flush`

func XfrmPolicyUpdate

func XfrmPolicyUpdate(policy *XfrmPolicy) error

XfrmPolicyUpdate will update an xfrm policy to the system. Equivalent to: `ip xfrm policy update $policy`

func XfrmStateAdd

func XfrmStateAdd(state *XfrmState) error

XfrmStateAdd will add an xfrm state to the system. Equivalent to: `ip xfrm state add $state`

func XfrmStateDel

func XfrmStateDel(state *XfrmState) error

XfrmStateDel will delete an xfrm state from the system. Note that the Algos are ignored when matching the state to delete. Equivalent to: `ip xfrm state del $state`

func XfrmStateFlush

func XfrmStateFlush(proto Proto) error

XfrmStateFlush will flush the xfrm state on the system. proto = 0 means any transformation protocols Equivalent to: `ip xfrm state flush [ proto XFRM-PROTO ]`

func XfrmStateUpdate

func XfrmStateUpdate(state *XfrmState) error

XfrmStateUpdate will update an xfrm state to the system. Equivalent to: `ip xfrm state update $state`

func Xmittime

func Xmittime(rate uint64, size uint32) float64

Types

type Action

type Action interface {
	Attrs() *ActionAttrs
	Type() string
}

Action represents an action in any supported filter.

type ActionAttrs

type ActionAttrs struct {
	Index   int
	Capab   int
	Action  TcAct
	Refcnt  int
	Bindcnt int
}

func (ActionAttrs) String

func (q ActionAttrs) String() string

type Addr

type Addr struct {
	*net.IPNet
	Label string
	Flags int
	Scope int
}

Addr represents an IP address from netlink. Netlink ip addresses include a mask, so it stores the address as a net.IPNet.

func AddrList

func AddrList(link Link, family int) ([]Addr, error)

AddrList gets a list of IP addresses in the system. Equivalent to: `ip addr show`. The list can be filtered by link and ip family.

func ParseAddr

func ParseAddr(s string) (*Addr, error)

ParseAddr parses the string representation of an address in the form $ip/$netmask $label. The label portion is optional

func (Addr) Equal

func (a Addr) Equal(x Addr) bool

Equal returns true if both Addrs have the same net.IPNet value.

func (Addr) String

func (a Addr) String() string

String returns $ip/$netmask $label

type AddrUpdate

type AddrUpdate struct {
	LinkAddress net.IPNet
	LinkIndex   int
	NewAddr     bool // true=added false=deleted
}

type Bond

type Bond struct {
	LinkAttrs
	Mode            BondMode
	ActiveSlave     int
	Miimon          int
	UpDelay         int
	DownDelay       int
	UseCarrier      int
	ArpInterval     int
	ArpIpTargets    []net.IP
	ArpValidate     BondArpValidate
	ArpAllTargets   BondArpAllTargets
	Primary         int
	PrimaryReselect BondPrimaryReselect
	FailOverMac     BondFailOverMac
	XmitHashPolicy  BondXmitHashPolicy
	ResendIgmp      int
	NumPeerNotif    int
	AllSlavesActive int
	MinLinks        int
	LpInterval      int
	PackersPerSlave int
	LacpRate        BondLacpRate
	AdSelect        BondAdSelect
	// looking at iproute tool AdInfo can only be retrived. It can't be set.
	AdInfo *BondAdInfo
}

Bond representation

func NewLinkBond

func NewLinkBond(atr LinkAttrs) *Bond

func (*Bond) Attrs

func (bond *Bond) Attrs() *LinkAttrs

Attrs implementation.

func (*Bond) Type

func (bond *Bond) Type() string

Type implementation fro Vxlan.

type BondAdInfo

type BondAdInfo struct {
	AggregatorId int
	NumPorts     int
	ActorKey     int
	PartnerKey   int
	PartnerMac   net.HardwareAddr
}

BondAdInfo represents ad info for bond

type BondAdSelect

type BondAdSelect int

BondAdSelect type

const (
	BOND_AD_SELECT_STABLE BondAdSelect = iota
	BOND_AD_SELECT_BANDWIDTH
	BOND_AD_SELECT_COUNT
)

Possible BondAdSelect value

type BondArpAllTargets

type BondArpAllTargets int

BondArpAllTargets type

const (
	BOND_ARP_ALL_TARGETS_ANY BondArpAllTargets = iota
	BOND_ARP_ALL_TARGETS_ALL
)

Possible BondArpAllTargets value

type BondArpValidate

type BondArpValidate int

BondArpValidate type

const (
	BOND_ARP_VALIDATE_NONE BondArpValidate = iota
	BOND_ARP_VALIDATE_ACTIVE
	BOND_ARP_VALIDATE_BACKUP
	BOND_ARP_VALIDATE_ALL
)

Possible BondArpValidate value

type BondFailOverMac

type BondFailOverMac int

BondFailOverMac type

const (
	BOND_FAIL_OVER_MAC_NONE BondFailOverMac = iota
	BOND_FAIL_OVER_MAC_ACTIVE
	BOND_FAIL_OVER_MAC_FOLLOW
)

Possible BondFailOverMac value

type BondLacpRate

type BondLacpRate int

BondLacpRate type

const (
	BOND_LACP_RATE_SLOW BondLacpRate = iota
	BOND_LACP_RATE_FAST
	BOND_LACP_RATE_UNKNOWN
)

Possible BondLacpRate value

func StringToBondLacpRate

func StringToBondLacpRate(s string) BondLacpRate

StringToBondLacpRate returns bond lacp arte, or uknonw is the s is invalid.

func (BondLacpRate) String

func (b BondLacpRate) String() string

type BondMode

type BondMode int

BondMode type

const (
	BOND_MODE_802_3AD BondMode = iota
	BOND_MODE_BALANCE_RR
	BOND_MODE_ACTIVE_BACKUP
	BOND_MODE_BALANCE_XOR
	BOND_MODE_BROADCAST
	BOND_MODE_BALANCE_TLB
	BOND_MODE_BALANCE_ALB
	BOND_MODE_UNKNOWN
)

Possible BondMode

func StringToBondMode

func StringToBondMode(s string) BondMode

StringToBondMode returns bond mode, or uknonw is the s is invalid.

func (BondMode) String

func (b BondMode) String() string

type BondPrimaryReselect

type BondPrimaryReselect int

BondPrimaryReselect type

const (
	BOND_PRIMARY_RESELECT_ALWAYS BondPrimaryReselect = iota
	BOND_PRIMARY_RESELECT_BETTER
	BOND_PRIMARY_RESELECT_FAILURE
)

Possible BondPrimaryReselect value

type BondXmitHashPolicy

type BondXmitHashPolicy int

BondXmitHashPolicy type

const (
	BOND_XMIT_HASH_POLICY_LAYER2 BondXmitHashPolicy = iota
	BOND_XMIT_HASH_POLICY_LAYER3_4
	BOND_XMIT_HASH_POLICY_LAYER2_3
	BOND_XMIT_HASH_POLICY_ENCAP2_3
	BOND_XMIT_HASH_POLICY_ENCAP3_4
	BOND_XMIT_HASH_POLICY_UNKNOWN
)

Possible BondXmitHashPolicy value

func StringToBondXmitHashPolicy

func StringToBondXmitHashPolicy(s string) BondXmitHashPolicy

StringToBondXmitHashPolicy returns bond lacp arte, or uknonw is the s is invalid.

func (BondXmitHashPolicy) String

func (b BondXmitHashPolicy) String() string

type BpfAction

type BpfAction struct {
	ActionAttrs
	Fd   int
	Name string
}

func (*BpfAction) Attrs

func (action *BpfAction) Attrs() *ActionAttrs

func (*BpfAction) Type

func (action *BpfAction) Type() string

type BpfFilter

type BpfFilter struct {
	FilterAttrs
	ClassId      uint32
	Fd           int
	Name         string
	DirectAction bool
}

func (*BpfFilter) Attrs

func (filter *BpfFilter) Attrs() *FilterAttrs

func (*BpfFilter) Type

func (filter *BpfFilter) Type() string

type BpfProgType

type BpfProgType C.int
const (
	BPF_PROG_TYPE_UNSPEC BpfProgType = iota
	BPF_PROG_TYPE_SOCKET_FILTER
	BPF_PROG_TYPE_KPROBE
	BPF_PROG_TYPE_SCHED_CLS
	BPF_PROG_TYPE_SCHED_ACT
)

type Bridge

type Bridge struct {
	LinkAttrs
}

Bridge links are simple linux bridges

func (*Bridge) Attrs

func (bridge *Bridge) Attrs() *LinkAttrs

func (*Bridge) Type

func (bridge *Bridge) Type() string

type Class

type Class interface {
	Attrs() *ClassAttrs
	Type() string
}

func ClassList

func ClassList(link Link, parent uint32) ([]Class, error)

ClassList gets a list of classes in the system. Equivalent to: `tc class show`. Generally returns nothing if link and parent are not specified.

type ClassAttrs

type ClassAttrs struct {
	LinkIndex int
	Handle    uint32
	Parent    uint32
	Leaf      uint32
}

ClassAttrs represents a netlink class. A filter is associated with a link, has a handle and a parent. The root filter of a device should have a parent == HANDLE_ROOT.

func (ClassAttrs) String

func (q ClassAttrs) String() string

type Device

type Device struct {
	LinkAttrs
}

Device links cannot be created via netlink. These links are links created by udev like 'lo' and 'etho0'

func (*Device) Attrs

func (device *Device) Attrs() *LinkAttrs

func (*Device) Type

func (device *Device) Type() string

type Dir

type Dir uint8

Dir is an enum representing an ipsec template direction.

const (
	XFRM_DIR_IN Dir = iota
	XFRM_DIR_OUT
	XFRM_DIR_FWD
	XFRM_SOCKET_IN
	XFRM_SOCKET_OUT
	XFRM_SOCKET_FWD
)

func (Dir) String

func (d Dir) String() string

type Dummy

type Dummy struct {
	LinkAttrs
}

Dummy links are dummy ethernet devices

func (*Dummy) Attrs

func (dummy *Dummy) Attrs() *LinkAttrs

func (*Dummy) Type

func (dummy *Dummy) Type() string

type EncapType

type EncapType uint8

EncapType is an enum representing the optional packet encapsulation.

const (
	XFRM_ENCAP_ESPINUDP_NONIKE EncapType = iota + 1
	XFRM_ENCAP_ESPINUDP
)

func (EncapType) String

func (e EncapType) String() string

type Filter

type Filter interface {
	Attrs() *FilterAttrs
	Type() string
}

func FilterList

func FilterList(link Link, parent uint32) ([]Filter, error)

FilterList gets a list of filters in the system. Equivalent to: `tc filter show`. Generally returns nothing if link and parent are not specified.

type FilterAttrs

type FilterAttrs struct {
	LinkIndex int
	Handle    uint32
	Parent    uint32
	Priority  uint16 // lower is higher priority
	Protocol  uint16 // syscall.ETH_P_*
}

FilterAttrs represents a netlink filter. A filter is associated with a link, has a handle and a parent. The root filter of a device should have a parent == HANDLE_ROOT.

func (FilterAttrs) String

func (q FilterAttrs) String() string

type FilterFwAttrs

type FilterFwAttrs struct {
	ClassId   uint32
	InDev     string
	Mask      uint32
	Index     uint32
	Buffer    uint32
	Mtu       uint32
	Mpu       uint16
	Rate      uint32
	AvRate    uint32
	PeakRate  uint32
	Action    TcPolAct
	Overhead  uint16
	LinkLayer int
}

type Fw

type Fw struct {
	FilterAttrs
	ClassId uint32
	// TODO remove nl type from interface
	Police nl.TcPolice
	InDev  string
	// TODO Action
	Mask   uint32
	AvRate uint32
	Rtab   [256]uint32
	Ptab   [256]uint32
}

Fw filter filters on firewall marks NOTE: this is in filter_linux because it refers to nl.TcPolice which

is defined in nl/tc_linux.go

func NewFw

func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error)

func (*Fw) Attrs

func (filter *Fw) Attrs() *FilterAttrs

func (*Fw) Type

func (filter *Fw) Type() string

type GenericAction

type GenericAction struct {
	ActionAttrs
}

func (*GenericAction) Attrs

func (action *GenericAction) Attrs() *ActionAttrs

func (*GenericAction) Type

func (action *GenericAction) Type() string

type GenericClass

type GenericClass struct {
	ClassAttrs
	ClassType string
}

GenericClass classes represent types that are not currently understood by this netlink library.

func (*GenericClass) Attrs

func (class *GenericClass) Attrs() *ClassAttrs

func (*GenericClass) Type

func (class *GenericClass) Type() string

type GenericFilter

type GenericFilter struct {
	FilterAttrs
	FilterType string
}

GenericFilter filters represent types that are not currently understood by this netlink library.

func (*GenericFilter) Attrs

func (filter *GenericFilter) Attrs() *FilterAttrs

func (*GenericFilter) Type

func (filter *GenericFilter) Type() string
type GenericLink struct {
	LinkAttrs
	LinkType string
}

GenericLink links represent types that are not currently understood by this netlink library.

func (*GenericLink) Attrs

func (generic *GenericLink) Attrs() *LinkAttrs

func (*GenericLink) Type

func (generic *GenericLink) Type() string

type GenericQdisc

type GenericQdisc struct {
	QdiscAttrs
	QdiscType string
}

GenericQdisc qdiscs represent types that are not currently understood by this netlink library.

func (*GenericQdisc) Attrs

func (qdisc *GenericQdisc) Attrs() *QdiscAttrs

func (*GenericQdisc) Type

func (qdisc *GenericQdisc) Type() string

type Gretap

type Gretap struct {
	LinkAttrs
	IKey       uint32
	OKey       uint32
	EncapSport uint16
	EncapDport uint16
	Local      net.IP
	Remote     net.IP
	IFlags     uint16
	OFlags     uint16
	PMtuDisc   uint8
	Ttl        uint8
	Tos        uint8
	EncapType  uint16
	EncapFlags uint16
	Link       uint32
}

Gretap devices must specify LocalIP and RemoteIP on create

func (*Gretap) Attrs

func (gretap *Gretap) Attrs() *LinkAttrs

func (*Gretap) Type

func (gretap *Gretap) Type() string

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle is an handle for the netlink requests on a specific network namespace. All the requests on the same netlink family share the same netlink socket, which gets released when the handle is deleted.

func NewHandle

func NewHandle(nlFamilies ...int) (*Handle, error)

NewHandle returns a netlink handle on the current network namespace. Caller may specify the netlink families the handle should support. If no families are specified, all the families the netlink package supports will be automatically added.

func NewHandleAt

func NewHandleAt(ns netns.NsHandle, nlFamilies ...int) (*Handle, error)

NewHandle returns a netlink handle on the network namespace specified by ns. If ns=netns.None(), current network namespace will be assumed

func NewHandleAtFrom

func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error)

NewHandleAtFrom works as NewHandle but allows client to specify the new and the origin netns Handle.

func (*Handle) AddrAdd

func (h *Handle) AddrAdd(link Link, addr *Addr) error

AddrAdd will add an IP address to a link device. Equivalent to: `ip addr add $addr dev $link`

func (*Handle) AddrDel

func (h *Handle) AddrDel(link Link, addr *Addr) error

AddrDel will delete an IP address from a link device. Equivalent to: `ip addr del $addr dev $link`

func (*Handle) AddrList

func (h *Handle) AddrList(link Link, family int) ([]Addr, error)

AddrList gets a list of IP addresses in the system. Equivalent to: `ip addr show`. The list can be filtered by link and ip family.

func (*Handle) ClassAdd

func (h *Handle) ClassAdd(class Class) error

ClassAdd will add a class to the system. Equivalent to: `tc class add $class`

func (*Handle) ClassChange

func (h *Handle) ClassChange(class Class) error

ClassChange will change a class in place Equivalent to: `tc class change $class` The parent and handle MUST NOT be changed.

func (*Handle) ClassDel

func (h *Handle) ClassDel(class Class) error

ClassDel will delete a class from the system. Equivalent to: `tc class del $class`

func (*Handle) ClassList

func (h *Handle) ClassList(link Link, parent uint32) ([]Class, error)

ClassList gets a list of classes in the system. Equivalent to: `tc class show`. Generally returns nothing if link and parent are not specified.

func (*Handle) ClassReplace

func (h *Handle) ClassReplace(class Class) error

ClassReplace will replace a class to the system. quivalent to: `tc class replace $class` The handle MAY be changed. If a class already exist with this parent/handle pair, the class is changed. If a class does not already exist with this parent/handle, a new class is created.

func (*Handle) Delete

func (h *Handle) Delete()

Delete releases the resources allocated to this handle

func (*Handle) FilterAdd

func (h *Handle) FilterAdd(filter Filter) error

FilterAdd will add a filter to the system. Equivalent to: `tc filter add $filter`

func (*Handle) FilterDel

func (h *Handle) FilterDel(filter Filter) error

FilterDel will delete a filter from the system. Equivalent to: `tc filter del $filter`

func (*Handle) FilterList

func (h *Handle) FilterList(link Link, parent uint32) ([]Filter, error)

FilterList gets a list of filters in the system. Equivalent to: `tc filter show`. Generally returns nothing if link and parent are not specified.

func (*Handle) LinkAdd

func (h *Handle) LinkAdd(link Link) error

LinkAdd adds a new link device. The type and features of the device are taken fromt the parameters in the link object. Equivalent to: `ip link add $link`

func (*Handle) LinkByAlias

func (h *Handle) LinkByAlias(alias string) (Link, error)

LinkByAlias finds a link by its alias and returns a pointer to the object. If there are multiple links with the alias it returns the first one

func (*Handle) LinkByIndex

func (h *Handle) LinkByIndex(index int) (Link, error)

LinkByIndex finds a link by index and returns a pointer to the object.

func (*Handle) LinkByName

func (h *Handle) LinkByName(name string) (Link, error)

LinkByName finds a link by name and returns a pointer to the object.

func (*Handle) LinkDel

func (h *Handle) LinkDel(link Link) error

LinkDel deletes link device. Either Index or Name must be set in the link object for it to be deleted. The other values are ignored. Equivalent to: `ip link del $link`

func (*Handle) LinkGetProtinfo

func (h *Handle) LinkGetProtinfo(link Link) (Protinfo, error)
func (h *Handle) LinkList() ([]Link, error)

LinkList gets a list of link devices. Equivalent to: `ip link show`

func (*Handle) LinkSetAlias

func (h *Handle) LinkSetAlias(link Link, name string) error

LinkSetAlias sets the alias of the link device. Equivalent to: `ip link set dev $link alias $name`

func (*Handle) LinkSetDown

func (h *Handle) LinkSetDown(link Link) error

LinkSetDown disables link device. Equivalent to: `ip link set $link down`

func (*Handle) LinkSetFastLeave

func (h *Handle) LinkSetFastLeave(link Link, mode bool) error

func (*Handle) LinkSetFlood

func (h *Handle) LinkSetFlood(link Link, mode bool) error

func (*Handle) LinkSetGuard

func (h *Handle) LinkSetGuard(link Link, mode bool) error

func (*Handle) LinkSetHairpin

func (h *Handle) LinkSetHairpin(link Link, mode bool) error

func (*Handle) LinkSetHardwareAddr

func (h *Handle) LinkSetHardwareAddr(link Link, hwaddr net.HardwareAddr) error

LinkSetHardwareAddr sets the hardware address of the link device. Equivalent to: `ip link set $link address $hwaddr`

func (*Handle) LinkSetLearning

func (h *Handle) LinkSetLearning(link Link, mode bool) error

func (*Handle) LinkSetMTU

func (h *Handle) LinkSetMTU(link Link, mtu int) error

LinkSetMTU sets the mtu of the link device. Equivalent to: `ip link set $link mtu $mtu`

func (*Handle) LinkSetMaster

func (h *Handle) LinkSetMaster(link Link, master *Bridge) error

LinkSetMaster sets the master of the link device. Equivalent to: `ip link set $link master $master`

func (*Handle) LinkSetMasterByIndex

func (h *Handle) LinkSetMasterByIndex(link Link, masterIndex int) error

LinkSetMasterByIndex sets the master of the link device. Equivalent to: `ip link set $link master $master`

func (*Handle) LinkSetName

func (h *Handle) LinkSetName(link Link, name string) error

LinkSetName sets the name of the link device. Equivalent to: `ip link set $link name $name`

func (*Handle) LinkSetNoMaster

func (h *Handle) LinkSetNoMaster(link Link) error

LinkSetNoMaster removes the master of the link device. Equivalent to: `ip link set $link nomaster`

func (*Handle) LinkSetNsFd

func (h *Handle) LinkSetNsFd(link Link, fd int) error

LinkSetNsFd puts the device into a new network namespace. The fd must be an open file descriptor to a network namespace. Similar to: `ip link set $link netns $ns`

func (*Handle) LinkSetNsPid

func (h *Handle) LinkSetNsPid(link Link, nspid int) error

LinkSetNsPid puts the device into a new network namespace. The pid must be a pid of a running process. Equivalent to: `ip link set $link netns $pid`

func (*Handle) LinkSetRootBlock

func (h *Handle) LinkSetRootBlock(link Link, mode bool) error

func (*Handle) LinkSetUp

func (h *Handle) LinkSetUp(link Link) error

LinkSetUp enables the link device. Equivalent to: `ip link set $link up`

func (*Handle) LinkSetVfHardwareAddr

func (h *Handle) LinkSetVfHardwareAddr(link Link, vf int, hwaddr net.HardwareAddr) error

LinkSetVfHardwareAddr sets the hardware address of a vf for the link. Equivalent to: `ip link set $link vf $vf mac $hwaddr`

func (*Handle) LinkSetVfVlan

func (h *Handle) LinkSetVfVlan(link Link, vf, vlan int) error

LinkSetVfVlan sets the vlan of a vf for the link. Equivalent to: `ip link set $link vf $vf vlan $vlan`

func (*Handle) NeighAdd

func (h *Handle) NeighAdd(neigh *Neigh) error

NeighAdd will add an IP to MAC mapping to the ARP table Equivalent to: `ip neigh add ....`

func (*Handle) NeighAppend

func (h *Handle) NeighAppend(neigh *Neigh) error

NeighAppend will append an entry to FDB Equivalent to: `bridge fdb append...`

func (*Handle) NeighDel

func (h *Handle) NeighDel(neigh *Neigh) error

NeighDel will delete an IP address from a link device. Equivalent to: `ip addr del $addr dev $link`

func (*Handle) NeighList

func (h *Handle) NeighList(linkIndex, family int) ([]Neigh, error)

NeighList gets a list of IP-MAC mappings in the system (ARP table). Equivalent to: `ip neighbor show`. The list can be filtered by link and ip family.

func (*Handle) NeighSet

func (h *Handle) NeighSet(neigh *Neigh) error

NeighSet will add or replace an IP to MAC mapping to the ARP table Equivalent to: `ip neigh replace....`

func (*Handle) QdiscAdd

func (h *Handle) QdiscAdd(qdisc Qdisc) error

QdiscAdd will add a qdisc to the system. Equivalent to: `tc qdisc add $qdisc`

func (*Handle) QdiscChange

func (h *Handle) QdiscChange(qdisc Qdisc) error

QdiscChange will change a qdisc in place Equivalent to: `tc qdisc change $qdisc` The parent and handle MUST NOT be changed.

func (*Handle) QdiscDel

func (h *Handle) QdiscDel(qdisc Qdisc) error

QdiscDel will delete a qdisc from the system. Equivalent to: `tc qdisc del $qdisc`

func (*Handle) QdiscList

func (h *Handle) QdiscList(link Link) ([]Qdisc, error)

QdiscList gets a list of qdiscs in the system. Equivalent to: `tc qdisc show`. The list can be filtered by link.

func (*Handle) QdiscReplace

func (h *Handle) QdiscReplace(qdisc Qdisc) error

QdiscReplace will replace a qdisc to the system. Equivalent to: `tc qdisc replace $qdisc` The handle MUST change.

func (*Handle) RouteAdd

func (h *Handle) RouteAdd(route *Route) error

RouteAdd will add a route to the system. Equivalent to: `ip route add $route`

func (*Handle) RouteDel

func (h *Handle) RouteDel(route *Route) error

RouteDel will delete a route from the system. Equivalent to: `ip route del $route`

func (*Handle) RouteGet

func (h *Handle) RouteGet(destination net.IP) ([]Route, error)

RouteGet gets a route to a specific destination from the host system. Equivalent to: 'ip route get'.

func (*Handle) RouteList

func (h *Handle) RouteList(link Link, family int) ([]Route, error)

RouteList gets a list of routes in the system. Equivalent to: `ip route show`. The list can be filtered by link and ip family.

func (*Handle) RouteListFiltered

func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error)

RouteListFiltered gets a list of routes in the system filtered with specified rules. All rules must be defined in RouteFilter struct

func (*Handle) RuleAdd

func (h *Handle) RuleAdd(rule *Rule) error

RuleAdd adds a rule to the system. Equivalent to: ip rule add

func (*Handle) RuleDel

func (h *Handle) RuleDel(rule *Rule) error

RuleDel deletes a rule from the system. Equivalent to: ip rule del

func (*Handle) RuleList

func (h *Handle) RuleList(family int) ([]Rule, error)

RuleList lists rules in the system. Equivalent to: ip rule list

func (*Handle) SupportsNetlinkFamily

func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool

SupportsNetlinkFamily reports whether the passed netlink family is supported by this Handle

func (*Handle) XfrmPolicyAdd

func (h *Handle) XfrmPolicyAdd(policy *XfrmPolicy) error

XfrmPolicyAdd will add an xfrm policy to the system. Equivalent to: `ip xfrm policy add $policy`

func (*Handle) XfrmPolicyDel

func (h *Handle) XfrmPolicyDel(policy *XfrmPolicy) error

XfrmPolicyDel will delete an xfrm policy from the system. Note that the Tmpls are ignored when matching the policy to delete. Equivalent to: `ip xfrm policy del $policy`

func (*Handle) XfrmPolicyFlush

func (h *Handle) XfrmPolicyFlush() error

XfrmPolicyFlush will flush the policies on the system. Equivalent to: `ip xfrm policy flush`

func (*Handle) XfrmPolicyGet

func (h *Handle) XfrmPolicyGet(policy *XfrmPolicy) (*XfrmPolicy, error)

XfrmPolicyGet gets a the policy described by the index or selector, if found. Equivalent to: `ip xfrm policy get { SELECTOR | index INDEX } dir DIR [ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]`.

func (*Handle) XfrmPolicyList

func (h *Handle) XfrmPolicyList(family int) ([]XfrmPolicy, error)

XfrmPolicyList gets a list of xfrm policies in the system. Equivalent to: `ip xfrm policy show`. The list can be filtered by ip family.

func (*Handle) XfrmPolicyUpdate

func (h *Handle) XfrmPolicyUpdate(policy *XfrmPolicy) error

XfrmPolicyUpdate will update an xfrm policy to the system. Equivalent to: `ip xfrm policy update $policy`

func (*Handle) XfrmStateAdd

func (h *Handle) XfrmStateAdd(state *XfrmState) error

XfrmStateAdd will add an xfrm state to the system. Equivalent to: `ip xfrm state add $state`

func (*Handle) XfrmStateDel

func (h *Handle) XfrmStateDel(state *XfrmState) error

XfrmStateDel will delete an xfrm state from the system. Note that the Algos are ignored when matching the state to delete. Equivalent to: `ip xfrm state del $state`

func (*Handle) XfrmStateFlush

func (h *Handle) XfrmStateFlush(proto Proto) error

XfrmStateFlush will flush the xfrm state on the system. proto = 0 means any transformation protocols Equivalent to: `ip xfrm state flush [ proto XFRM-PROTO ]`

func (*Handle) XfrmStateGet

func (h *Handle) XfrmStateGet(state *XfrmState) (*XfrmState, error)

XfrmStateGet gets the xfrm state described by the ID, if found. Equivalent to: `ip xfrm state get ID [ mark MARK [ mask MASK ] ]`. Only the fields which constitue the SA ID must be filled in: ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ] mark is optional

func (*Handle) XfrmStateList

func (h *Handle) XfrmStateList(family int) ([]XfrmState, error)

XfrmStateList gets a list of xfrm states in the system. Equivalent to: `ip xfrm state show`. The list can be filtered by ip family.

func (*Handle) XfrmStateUpdate

func (h *Handle) XfrmStateUpdate(state *XfrmState) error

XfrmStateUpdate will update an xfrm state to the system. Equivalent to: `ip xfrm state update $state`

type Htb

type Htb struct {
	QdiscAttrs
	Version      uint32
	Rate2Quantum uint32
	Defcls       uint32
	Debug        uint32
	DirectPkts   uint32
}

Htb is a classful qdisc that rate limits based on tokens

func NewHtb

func NewHtb(attrs QdiscAttrs) *Htb

func (*Htb) Attrs

func (qdisc *Htb) Attrs() *QdiscAttrs

func (*Htb) Type

func (qdisc *Htb) Type() string

type HtbClass

type HtbClass struct {
	ClassAttrs
	Rate    uint64
	Ceil    uint64
	Buffer  uint32
	Cbuffer uint32
	Quantum uint32
	Level   uint32
	Prio    uint32
}

HtbClass represents an Htb class

func NewHtbClass

func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass

NOTE: function is in here because it uses other linux functions

func (*HtbClass) Attrs

func (q *HtbClass) Attrs() *ClassAttrs

func (HtbClass) String

func (q HtbClass) String() string

func (*HtbClass) Type

func (q *HtbClass) Type() string

type HtbClassAttrs

type HtbClassAttrs struct {
	// TODO handle all attributes
	Rate    uint64
	Ceil    uint64
	Buffer  uint32
	Cbuffer uint32
	Quantum uint32
	Level   uint32
	Prio    uint32
}

func (HtbClassAttrs) String

func (q HtbClassAttrs) String() string

type IPVlan

type IPVlan struct {
	LinkAttrs
	Mode IPVlanMode
}

func (*IPVlan) Attrs

func (ipvlan *IPVlan) Attrs() *LinkAttrs

func (*IPVlan) Type

func (ipvlan *IPVlan) Type() string

type IPVlanMode

type IPVlanMode uint16
const (
	IPVLAN_MODE_L2 IPVlanMode = iota
	IPVLAN_MODE_L3
	IPVLAN_MODE_MAX
)

type Ifb

type Ifb struct {
	LinkAttrs
}

Ifb links are advanced dummy devices for packet filtering

func (*Ifb) Attrs

func (ifb *Ifb) Attrs() *LinkAttrs

func (*Ifb) Type

func (ifb *Ifb) Type() string

type Ingress

type Ingress struct {
	QdiscAttrs
}

Ingress is a qdisc for adding ingress filters

func (*Ingress) Attrs

func (qdisc *Ingress) Attrs() *QdiscAttrs

func (*Ingress) Type

func (qdisc *Ingress) Type() string
type Link interface {
	Attrs() *LinkAttrs
	Type() string
}

Link represents a link device from netlink. Shared link attributes like name may be retrieved using the Attrs() method. Unique data can be retrieved by casting the object to the proper type.

func LinkByAlias

func LinkByAlias(alias string) (Link, error)

LinkByAlias finds a link by its alias and returns a pointer to the object. If there are multiple links with the alias it returns the first one

func LinkByIndex

func LinkByIndex(index int) (Link, error)

LinkByIndex finds a link by index and returns a pointer to the object.

func LinkByName

func LinkByName(name string) (Link, error)

LinkByName finds a link by name and returns a pointer to the object.

func LinkList() ([]Link, error)

LinkList gets a list of link devices. Equivalent to: `ip link show`

type LinkAttrs

type LinkAttrs struct {
	Index        int
	MTU          int
	TxQLen       int // Transmit Queue Length
	Name         string
	HardwareAddr net.HardwareAddr
	Flags        net.Flags
	ParentIndex  int         // index of the parent link device
	MasterIndex  int         // must be the index of a bridge
	Namespace    interface{} // nil | NsPid | NsFd
	Alias        string
	Statistics   *LinkStatistics
}

LinkAttrs represents data shared by most link types

func NewLinkAttrs

func NewLinkAttrs() LinkAttrs

NewLinkAttrs returns LinkAttrs structure filled with default values

type LinkStatistics

type LinkStatistics struct {
	RxPackets         uint32
	TxPackets         uint32
	RxBytes           uint32
	TxBytes           uint32
	RxErrors          uint32
	TxErrors          uint32
	RxDropped         uint32
	TxDropped         uint32
	Multicast         uint32
	Collisions        uint32
	RxLengthErrors    uint32
	RxOverErrors      uint32
	RxCrcErrors       uint32
	RxFrameErrors     uint32
	RxFifoErrors      uint32
	RxMissedErrors    uint32
	TxAbortedErrors   uint32
	TxCarrierErrors   uint32
	TxFifoErrors      uint32
	TxHeartbeatErrors uint32
	TxWindowErrors    uint32
	RxCompressed      uint32
	TxCompressed      uint32
}

Ref: struct rtnl_link_stats {...}

type LinkUpdate

type LinkUpdate struct {
	nl.IfInfomsg
	Header syscall.NlMsghdr
	Link
}

LinkUpdate is used to pass information back from LinkSubscribe()

type Macvlan

type Macvlan struct {
	LinkAttrs
	Mode MacvlanMode
}

Macvlan links have ParentIndex set in their Attrs()

func (*Macvlan) Attrs

func (macvlan *Macvlan) Attrs() *LinkAttrs

func (*Macvlan) Type

func (macvlan *Macvlan) Type() string

type MacvlanMode

type MacvlanMode uint16
const (
	MACVLAN_MODE_DEFAULT MacvlanMode = iota
	MACVLAN_MODE_PRIVATE
	MACVLAN_MODE_VEPA
	MACVLAN_MODE_BRIDGE
	MACVLAN_MODE_PASSTHRU
	MACVLAN_MODE_SOURCE
)

type Macvtap

type Macvtap struct {
	Macvlan
}

Macvtap - macvtap is a virtual interfaces based on macvlan

func (Macvtap) Type

func (macvtap Macvtap) Type() string

type MirredAct

type MirredAct uint8
const (
	TCA_EGRESS_REDIR   MirredAct = 1 /* packet redirect to EGRESS*/
	TCA_EGRESS_MIRROR  MirredAct = 2 /* mirror packet to EGRESS */
	TCA_INGRESS_REDIR  MirredAct = 3 /* packet redirect to INGRESS*/
	TCA_INGRESS_MIRROR MirredAct = 4 /* mirror packet to INGRESS */
)

func (MirredAct) String

func (a MirredAct) String() string

type MirredAction

type MirredAction struct {
	ActionAttrs
	MirredAction MirredAct
	Ifindex      int
}

func NewMirredAction

func NewMirredAction(redirIndex int) *MirredAction

func (*MirredAction) Attrs

func (action *MirredAction) Attrs() *ActionAttrs

func (*MirredAction) Type

func (action *MirredAction) Type() string

type Mode

type Mode uint8

Mode is an enum representing an ipsec transport.

const (
	XFRM_MODE_TRANSPORT Mode = iota
	XFRM_MODE_TUNNEL
	XFRM_MODE_ROUTEOPTIMIZATION
	XFRM_MODE_IN_TRIGGER
	XFRM_MODE_BEET
	XFRM_MODE_MAX
)

func (Mode) String

func (m Mode) String() string

type Ndmsg

type Ndmsg struct {
	Family uint8
	Index  uint32
	State  uint16
	Flags  uint8
	Type   uint8
}

func (*Ndmsg) Len

func (msg *Ndmsg) Len() int

func (*Ndmsg) Serialize

func (msg *Ndmsg) Serialize() []byte

type Neigh

type Neigh struct {
	LinkIndex    int
	Family       int
	State        int
	Type         int
	Flags        int
	IP           net.IP
	HardwareAddr net.HardwareAddr
}

Neigh represents a link layer neighbor from netlink.

func NeighDeserialize

func NeighDeserialize(m []byte) (*Neigh, error)

func NeighList

func NeighList(linkIndex, family int) ([]Neigh, error)

NeighList gets a list of IP-MAC mappings in the system (ARP table). Equivalent to: `ip neighbor show`. The list can be filtered by link and ip family.

func (*Neigh) String

func (neigh *Neigh) String() string

String returns $ip/$hwaddr $label

type Netem

type Netem struct {
	QdiscAttrs
	Latency       uint32
	DelayCorr     uint32
	Limit         uint32
	Loss          uint32
	LossCorr      uint32
	Gap           uint32
	Duplicate     uint32
	DuplicateCorr uint32
	Jitter        uint32
	ReorderProb   uint32
	ReorderCorr   uint32
	CorruptProb   uint32
	CorruptCorr   uint32
}

func NewNetem

func NewNetem(attrs QdiscAttrs, nattrs NetemQdiscAttrs) *Netem

NOTE function is here because it uses other linux functions

func (*Netem) Attrs

func (qdisc *Netem) Attrs() *QdiscAttrs

func (*Netem) Type

func (qdisc *Netem) Type() string

type NetemQdiscAttrs

type NetemQdiscAttrs struct {
	Latency       uint32  // in us
	DelayCorr     float32 // in %
	Limit         uint32
	Loss          float32 // in %
	LossCorr      float32 // in %
	Gap           uint32
	Duplicate     float32 // in %
	DuplicateCorr float32 // in %
	Jitter        uint32  // in us
	ReorderProb   float32 // in %
	ReorderCorr   float32 // in %
	CorruptProb   float32 // in %
	CorruptCorr   float32 // in %
}

func (NetemQdiscAttrs) String

func (q NetemQdiscAttrs) String() string

type NextHopFlag

type NextHopFlag int
const (
	FLAG_ONLINK    NextHopFlag = syscall.RTNH_F_ONLINK
	FLAG_PERVASIVE NextHopFlag = syscall.RTNH_F_PERVASIVE
)

type NexthopInfo

type NexthopInfo struct {
	LinkIndex int
	Hops      int
	Gw        net.IP
}

func (*NexthopInfo) String

func (n *NexthopInfo) String() string

type NsFd

type NsFd int

type NsPid

type NsPid int

type PfifoFast

type PfifoFast struct {
	QdiscAttrs
	Bands       uint8
	PriorityMap [PRIORITY_MAP_LEN]uint8
}

PfifoFast is the default qdisc created by the kernel if one has not been defined for the interface

func (*PfifoFast) Attrs

func (qdisc *PfifoFast) Attrs() *QdiscAttrs

func (*PfifoFast) Type

func (qdisc *PfifoFast) Type() string

type Prio

type Prio struct {
	QdiscAttrs
	Bands       uint8
	PriorityMap [PRIORITY_MAP_LEN]uint8
}

Prio is a basic qdisc that works just like PfifoFast

func NewPrio

func NewPrio(attrs QdiscAttrs) *Prio

func (*Prio) Attrs

func (qdisc *Prio) Attrs() *QdiscAttrs

func (*Prio) Type

func (qdisc *Prio) Type() string

type Protinfo

type Protinfo struct {
	Hairpin   bool
	Guard     bool
	FastLeave bool
	RootBlock bool
	Learning  bool
	Flood     bool
}

Protinfo represents bridge flags from netlink.

func LinkGetProtinfo

func LinkGetProtinfo(link Link) (Protinfo, error)

func (*Protinfo) String

func (prot *Protinfo) String() string

String returns a list of enabled flags

type Proto

type Proto uint8

Proto is an enum representing an ipsec protocol.

const (
	XFRM_PROTO_ROUTE2    Proto = syscall.IPPROTO_ROUTING
	XFRM_PROTO_ESP       Proto = syscall.IPPROTO_ESP
	XFRM_PROTO_AH        Proto = syscall.IPPROTO_AH
	XFRM_PROTO_HAO       Proto = syscall.IPPROTO_DSTOPTS
	XFRM_PROTO_COMP      Proto = 0x6c // NOTE not defined on darwin
	XFRM_PROTO_IPSEC_ANY Proto = syscall.IPPROTO_RAW
)

func (Proto) String

func (p Proto) String() string

type Qdisc

type Qdisc interface {
	Attrs() *QdiscAttrs
	Type() string
}

func QdiscList

func QdiscList(link Link) ([]Qdisc, error)

QdiscList gets a list of qdiscs in the system. Equivalent to: `tc qdisc show`. The list can be filtered by link.

type QdiscAttrs

type QdiscAttrs struct {
	LinkIndex int
	Handle    uint32
	Parent    uint32
	Refcnt    uint32 // read only
}

QdiscAttrs represents a netlink qdisc. A qdisc is associated with a link, has a handle, a parent and a refcnt. The root qdisc of a device should have parent == HANDLE_ROOT.

func (QdiscAttrs) String

func (q QdiscAttrs) String() string

type Route

type Route struct {
	LinkIndex  int
	ILinkIndex int
	Scope      Scope
	Dst        *net.IPNet
	Src        net.IP
	Gw         net.IP
	MultiPath  []*NexthopInfo
	Protocol   int
	Priority   int
	Table      int
	Type       int
	Tos        int
	Flags      int
}

Route represents a netlink route.

func RouteGet

func RouteGet(destination net.IP) ([]Route, error)

RouteGet gets a route to a specific destination from the host system. Equivalent to: 'ip route get'.

func RouteList

func RouteList(link Link, family int) ([]Route, error)

RouteList gets a list of routes in the system. Equivalent to: `ip route show`. The list can be filtered by link and ip family.

func RouteListFiltered

func RouteListFiltered(family int, filter *Route, filterMask uint64) ([]Route, error)

RouteListFiltered gets a list of routes in the system filtered with specified rules. All rules must be defined in RouteFilter struct

func (*Route) ClearFlag

func (r *Route) ClearFlag(flag NextHopFlag)

func (*Route) ListFlags

func (r *Route) ListFlags() []string

func (*Route) SetFlag

func (r *Route) SetFlag(flag NextHopFlag)

func (Route) String

func (r Route) String() string

type RouteUpdate

type RouteUpdate struct {
	Type uint16
	Route
}

RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE

type Rule

type Rule struct {
	Priority          int
	Table             int
	Mark              int
	Mask              int
	TunID             uint
	Goto              int
	Src               *net.IPNet
	Dst               *net.IPNet
	Flow              int
	IifName           string
	OifName           string
	SuppressIfgroup   int
	SuppressPrefixlen int
}

Rule represents a netlink rule.

func NewRule

func NewRule() *Rule

NewRule return empty rules.

func RuleList

func RuleList(family int) ([]Rule, error)

RuleList lists rules in the system. Equivalent to: ip rule list

func (Rule) String

func (r Rule) String() string

type Scope

type Scope uint8

Scope is an enum representing a route scope.

const (
	SCOPE_UNIVERSE Scope = syscall.RT_SCOPE_UNIVERSE
	SCOPE_SITE     Scope = syscall.RT_SCOPE_SITE
	SCOPE_LINK     Scope = syscall.RT_SCOPE_LINK
	SCOPE_HOST     Scope = syscall.RT_SCOPE_HOST
	SCOPE_NOWHERE  Scope = syscall.RT_SCOPE_NOWHERE
)

type Tbf

type Tbf struct {
	QdiscAttrs
	// TODO: handle 64bit rate properly
	Rate   uint64
	Limit  uint32
	Buffer uint32
}

Tbf is a classless qdisc that rate limits based on tokens

func (*Tbf) Attrs

func (qdisc *Tbf) Attrs() *QdiscAttrs

func (*Tbf) Type

func (qdisc *Tbf) Type() string

type TcAct

type TcAct int32
const (
	TC_ACT_UNSPEC     TcAct = -1
	TC_ACT_OK         TcAct = 0
	TC_ACT_RECLASSIFY TcAct = 1
	TC_ACT_SHOT       TcAct = 2
	TC_ACT_PIPE       TcAct = 3
	TC_ACT_STOLEN     TcAct = 4
	TC_ACT_QUEUED     TcAct = 5
	TC_ACT_REPEAT     TcAct = 6
	TC_ACT_REDIRECT   TcAct = 7
	TC_ACT_JUMP       TcAct = 0x10000000
)

func (TcAct) String

func (a TcAct) String() string

type TcPolAct

type TcPolAct int32
const (
	TC_POLICE_UNSPEC     TcPolAct = TcPolAct(TC_ACT_UNSPEC)
	TC_POLICE_OK         TcPolAct = TcPolAct(TC_ACT_OK)
	TC_POLICE_RECLASSIFY TcPolAct = TcPolAct(TC_ACT_RECLASSIFY)
	TC_POLICE_SHOT       TcPolAct = TcPolAct(TC_ACT_SHOT)
	TC_POLICE_PIPE       TcPolAct = TcPolAct(TC_ACT_PIPE)
)

func (TcPolAct) String

func (a TcPolAct) String() string

type Tuntap

type Tuntap struct {
	LinkAttrs
	Mode TuntapMode
}

Tuntap links created via /dev/tun/tap, but can be destroyed via netlink

func (*Tuntap) Attrs

func (tuntap *Tuntap) Attrs() *LinkAttrs

func (*Tuntap) Type

func (tuntap *Tuntap) Type() string

type TuntapMode

type TuntapMode uint16
const (
	TUNTAP_MODE_TUN TuntapMode = syscall.IFF_TUN
	TUNTAP_MODE_TAP TuntapMode = syscall.IFF_TAP
)

type U32

type U32 struct {
	FilterAttrs
	ClassId    uint32
	RedirIndex int
	Actions    []Action
}

U32 filters on many packet related properties

func (*U32) Attrs

func (filter *U32) Attrs() *FilterAttrs

func (*U32) Type

func (filter *U32) Type() string

type Veth

type Veth struct {
	LinkAttrs
	PeerName string // veth on create only
}

Veth devices must specify PeerName on create

func (*Veth) Attrs

func (veth *Veth) Attrs() *LinkAttrs

func (*Veth) Type

func (veth *Veth) Type() string

type Vlan

type Vlan struct {
	LinkAttrs
	VlanId int
}

Vlan links have ParentIndex set in their Attrs()

func (*Vlan) Attrs

func (vlan *Vlan) Attrs() *LinkAttrs

func (*Vlan) Type

func (vlan *Vlan) Type() string

type Vxlan

type Vxlan struct {
	LinkAttrs
	VxlanId      int
	VtepDevIndex int
	SrcAddr      net.IP
	Group        net.IP
	TTL          int
	TOS          int
	Learning     bool
	Proxy        bool
	RSC          bool
	L2miss       bool
	L3miss       bool
	UDPCSum      bool
	NoAge        bool
	GBP          bool
	Age          int
	Limit        int
	Port         int
	PortLow      int
	PortHigh     int
}

func (*Vxlan) Attrs

func (vxlan *Vxlan) Attrs() *LinkAttrs

func (*Vxlan) Type

func (vxlan *Vxlan) Type() string

type XfrmMark

type XfrmMark struct {
	Value uint32
	Mask  uint32
}

XfrmMark represents the mark associated to the state or policy

func (*XfrmMark) String

func (m *XfrmMark) String() string

type XfrmPolicy

type XfrmPolicy struct {
	Dst      *net.IPNet
	Src      *net.IPNet
	Proto    Proto
	DstPort  int
	SrcPort  int
	Dir      Dir
	Priority int
	Index    int
	Mark     *XfrmMark
	Tmpls    []XfrmPolicyTmpl
}

XfrmPolicy represents an ipsec policy. It represents the overlay network and has a list of XfrmPolicyTmpls representing the base addresses of the policy.

func XfrmPolicyGet

func XfrmPolicyGet(policy *XfrmPolicy) (*XfrmPolicy, error)

XfrmPolicyGet gets a the policy described by the index or selector, if found. Equivalent to: `ip xfrm policy get { SELECTOR | index INDEX } dir DIR [ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]`.

func XfrmPolicyList

func XfrmPolicyList(family int) ([]XfrmPolicy, error)

XfrmPolicyList gets a list of xfrm policies in the system. Equivalent to: `ip xfrm policy show`. The list can be filtered by ip family.

func (XfrmPolicy) String

func (p XfrmPolicy) String() string

type XfrmPolicyTmpl

type XfrmPolicyTmpl struct {
	Dst   net.IP
	Src   net.IP
	Proto Proto
	Mode  Mode
	Spi   int
	Reqid int
}

XfrmPolicyTmpl encapsulates a rule for the base addresses of an ipsec policy. These rules are matched with XfrmState to determine encryption and authentication algorithms.

func (XfrmPolicyTmpl) String

func (t XfrmPolicyTmpl) String() string

type XfrmState

type XfrmState struct {
	Dst          net.IP
	Src          net.IP
	Proto        Proto
	Mode         Mode
	Spi          int
	Reqid        int
	ReplayWindow int
	Limits       XfrmStateLimits
	Mark         *XfrmMark
	Auth         *XfrmStateAlgo
	Crypt        *XfrmStateAlgo
	Aead         *XfrmStateAlgo
	Encap        *XfrmStateEncap
}

XfrmState represents the state of an ipsec policy. It optionally contains an XfrmStateAlgo for encryption and one for authentication.

func XfrmStateGet

func XfrmStateGet(state *XfrmState) (*XfrmState, error)

XfrmStateGet gets the xfrm state described by the ID, if found. Equivalent to: `ip xfrm state get ID [ mark MARK [ mask MASK ] ]`. Only the fields which constitue the SA ID must be filled in: ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ] mark is optional

func XfrmStateList

func XfrmStateList(family int) ([]XfrmState, error)

XfrmStateList gets a list of xfrm states in the system. Equivalent to: `ip [-4|-6] xfrm state show`. The list can be filtered by ip family.

func (XfrmState) Print

func (sa XfrmState) Print(stats bool) string

func (XfrmState) String

func (sa XfrmState) String() string

type XfrmStateAlgo

type XfrmStateAlgo struct {
	Name        string
	Key         []byte
	TruncateLen int // Auth only
	ICVLen      int // AEAD only
}

XfrmStateAlgo represents the algorithm to use for the ipsec encryption.

func (XfrmStateAlgo) String

func (a XfrmStateAlgo) String() string

type XfrmStateEncap

type XfrmStateEncap struct {
	Type            EncapType
	SrcPort         int
	DstPort         int
	OriginalAddress net.IP
}

XfrmStateEncap represents the encapsulation to use for the ipsec encryption.

func (XfrmStateEncap) String

func (e XfrmStateEncap) String() string

type XfrmStateLimits

type XfrmStateLimits struct {
	ByteSoft    uint64
	ByteHard    uint64
	PacketSoft  uint64
	PacketHard  uint64
	TimeSoft    uint64
	TimeHard    uint64
	TimeUseSoft uint64
	TimeUseHard uint64
}

XfrmStateLimits represents the configured limits for the state.

Directories

Path Synopsis
Package nl has low level primitives for making Netlink calls.
Package nl has low level primitives for making Netlink calls.

Jump to

Keyboard shortcuts

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