Install the netlink library
mainTo use the netlink library in your Go project, use the go get command to fetch the package from GitHub.
go get github.com/vishvananda/netlinkrepository·main·Indexed 25 days ago
https://github.com/vishvananda/netlinkA Go library providing a high-level API for communicating with the Linux kernel via netlink. It allows developers to manage network interfaces, IP addresses, routes, and IPsec configuration, with an API design loosely modeled after the iproute2 CLI.
To use the netlink library in your Go project, use the go get command to fetch the package from GitHub.
go get github.com/vishvananda/netlinkTesting the library requires root privileges and the netns dependency. To run tests, use sudo -E go test to ensure environment variables are preserved.
go get github.com/vishvananda/netns
sudo -E go test github.com/vishvananda/netlinkThe netlink package provides a Go API for managing Linux ipset structures via netlink. You can create, destroy, flush, swap, and list ipsets, as well as add, delete, or test for specific entries within a set.
Key operations include:
IpsetCreate: Create a new set with specific options.IpsetAdd / IpsetDel: Manage entries in a set.IpsetList / IpsetListAll: Retrieve set information and entries.IpsetTest: Check if an entry exists in a set.You can create a new bridge interface using netlink.LinkAdd() and attach an existing interface (like eth1) to it using netlink.LinkSetMaster().
When defining link attributes, it is recommended to use the netlink.NewLinkAttrs() constructor. This ensures that TxQLen is set to -1, allowing the kernel to apply its own default values. If you use a literal struct initialization like LinkAttrs{Name: "foo"}, TxQLen will default to 0 unless explicitly specified.
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)
}To add an IP address to a specific interface (e.g., lo), first retrieve the link using netlink.LinkByName(), parse the address string using netlink.ParseAddr(), and then apply it using netlink.AddrAdd().
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)
}When creating a handle via NewHandleWithOptions, you can provide HandleOptions to customize behavior:
| Field | Type | Description |
|---|---|---|
DisableVFInfoCollection | bool | If true, skips fetching VF (Virtual Function) information for links. This improves performance if VF info is not needed. |
RetryInterrupted | bool | If true, automatically retries dump operations that fail with EINTR before returning ErrDumpInterrupted. |
NetNS | *netns.NsHandle | Specifies the network namespace to operate on. If nil, the current namespace is used. |
When creating a new vDPA device using VDPANewDev, use the VDPANewDevParams struct to specify device parameters. You can use SetBits to configure required features.
Fields:
MACAddr: net.HardwareAddrMaxVQP: uint16MTU: uint16Features: uint64 (bitmask of features)LinkNotFoundError. This error type is used to distinguish 'not found' scenarios from other netlink errors, allowing for more robust error handling in dependent code.FouAdd, FouDel, and FouList are not implemented on non-Linux platforms. Attempting to use these functions on any operating system other than Linux will result in an ErrNotImplemented error.Use NewMirredAction(redirIndex int) to create a mirroring or redirection action. The MirredAction field uses MirredAct constants:
TCA_EGRESS_REDIR: Redirect to EGRESSTCA_EGRESS_MIRROR: Mirror to EGRESSTCA_INGRESS_REDIR: Redirect to INGRESSTCA_INGRESS_MIRROR: Mirror to INGRESSNewPoliceAction() to create a policing action. It defaults to LinkLayer: 1 (ETHERNET) and ExceedAction: TC_POLICE_RECLASSIFY. You can configure rates in bytes per second and burst in bytes.NewCsumAction() to create a checksum update action. You can configure UpdateFlags using CsumUpdateFlags constants like TCA_CSUM_UPDATE_FLAG_TCP or TCA_CSUM_UPDATE_FLAG_UDP.