Simple netlink library for go.
Clone or download
Permalink
Failed to load latest commit information.
nl Make AddChild more generic Oct 18, 2018
.travis.yml Implementation of HFSC Jul 16, 2018
CHANGELOG.md Add changelog file, initial release tagging Mar 16, 2018
LICENSE Initial commit of netlink package Sep 1, 2014
Makefile Properly use Skip() function, add -test.v when running tests Nov 8, 2017
README.md replace syscall with golang.org/x/sys/unix Oct 26, 2017
addr.go addr: Parse address lifetime information from IFA_CACHEINFO attr Apr 12, 2017
addr_linux.go fix prefixlen/local IP, incl. PtP addresses Jun 17, 2018
addr_test.go fix prefixlen/local IP, incl. PtP addresses Jun 17, 2018
bpf_linux.go Update bpf_linux.go Jan 5, 2018
bridge_linux.go Use IFLA_* constants from x/sys/unix Feb 23, 2018
bridge_linux_test.go Allow to skip tests based on min kernel version required Nov 8, 2017
class.go Implementation of HFSC Jul 16, 2018
class_linux.go Implementation of HFSC Jul 16, 2018
class_test.go Implementation of HFSC Jul 16, 2018
conntrack_linux.go Read conntrack flow statistics Mar 16, 2018
conntrack_test.go Read conntrack flow statistics Mar 16, 2018
conntrack_unspecified.go Added conntrack_unspecified Apr 5, 2017
filter.go Add Hash to U32 Apr 19, 2018
filter_linux.go Add Hash to U32 Apr 19, 2018
filter_test.go Add Hash to U32 Apr 19, 2018
fou.go Netlink: Fix Darwin build Jul 30, 2018
fou_linux.go Fix go build issue for fou code Nov 27, 2017
fou_test.go Fix go build issue for fou code Nov 27, 2017
fou_unspecified.go added support for Foo-over-UDP netlink calls Nov 13, 2017
genetlink_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
genetlink_unspecified.go Add support for generic netlink May 7, 2017
gtp_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
gtp_test.go Add support for GPRS Tunnelling Protocol(GTP) May 7, 2017
handle_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
handle_test.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
handle_unspecified.go netlink: allow non linux builds to pass. Dec 24, 2017
ioctl_linux.go Two new functions: LinkSetBondSlave and VethPeerIndex Dec 14, 2017
link.go Initial support for vlan aware bridges. Jul 23, 2018
link_linux.go Initial support for vlan aware bridges. Jul 23, 2018
link_test.go Add a test for Vlan filtering support for bridges. Jul 23, 2018
link_tuntap_linux.go Drop cgo usage for ifReq structure. Oct 2, 2015
neigh.go Add support for neighbor subscription Oct 16, 2018
neigh_linux.go Add support for neighbor subscription Oct 16, 2018
neigh_test.go Add support for neighbor subscription Oct 16, 2018
netlink.go Add support for tcp diags. Feb 5, 2017
netlink_linux.go support MPLS Feb 20, 2017
netlink_test.go Run both Inline/Encap mode in TestSEG6RouteAddDel May 1, 2018
netlink_unspecified.go Add LinkSetTxQLen Sep 6, 2017
netns_linux.go Add network namespace ID management. Jul 19, 2018
netns_test.go Add network namespace ID management. Jul 19, 2018
netns_unspecified.go Add network namespace ID management. Jul 19, 2018
order.go Add support for tcp diags. Feb 5, 2017
protinfo.go protinfo: Check if object is nil Jul 6, 2018
protinfo_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
protinfo_test.go Allow to skip tests based on min kernel version required Nov 8, 2017
qdisc.go Implementation of HFSC Jul 16, 2018
qdisc_linux.go Avoid serializing empty TCA_OPTIONS in qdisc messages Oct 16, 2018
qdisc_test.go Ingress qdisc add/del Test case Jul 17, 2018
rdma_link_linux.go Add RDMA netlink socket for RDMA device information Apr 29, 2018
rdma_link_test.go Add RDMA netlink socket for RDMA device information Apr 29, 2018
route.go Add support for hoplimit metric in routes Oct 16, 2018
route_linux.go Add support for hoplimit metric in routes Oct 16, 2018
route_test.go Add support for hoplimit metric in routes Oct 16, 2018
route_unspecified.go Fix compilation errors for non linux platforms. Feb 20, 2017
rule.go Support invert in ip rules Nov 8, 2017
rule_linux.go Support invert in ip rules Nov 8, 2017
rule_test.go Properly tear down netns at the end of test Nov 28, 2017
socket.go Add support for tcp diags. Feb 5, 2017
socket_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
socket_test.go Run TestSocketGet in dedicated netns Nov 28, 2017
xfrm.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
xfrm_monitor_linux.go replace syscall with golang.org/x/sys/unix Oct 26, 2017
xfrm_monitor_test.go Properly tear down netns at the end of test Nov 28, 2017
xfrm_policy.go Add support for action and ifindex in XFRM policy Aug 14, 2018
xfrm_policy_linux.go Add support for action and ifindex in XFRM policy Aug 14, 2018
xfrm_policy_test.go Add support for action and ifindex in XFRM policy Aug 14, 2018
xfrm_state.go Expose xfrm state's current and window statistics Oct 30, 2017
xfrm_state_linux.go Clarify ESN bitmap length construction logic Feb 6, 2018
xfrm_state_test.go Properly tear down netns at the end of test Nov 28, 2017

README.md

netlink - netlink library for go

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 (
    "fmt"
    "github.com/vishvananda/netlink"
)

func main() {
    la := netlink.NewLinkAttrs()
    la.Name = "foo"
    mybridge := &netlink.Bridge{LinkAttrs: la}
    err := netlink.LinkAdd(mybridge)
    if err != nil  {
        fmt.Printf("could not add %s: %v\n", la.Name, err)
    }
    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 (
    "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.