netlink

repository·main·Indexed 25 days ago

https://github.com/vishvananda/netlink

A 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.

Tokens
27.9K
Snippets
62
Records
291
Agent score
85%

What's inside netlink

  1. Run tests for netlink

    main

    Testing 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/netlink
  2. Manage IP sets with the netlink package

    main

    The 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.
  3. Create a new bridge and attach an interface

    main

    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)
    }
  4. Add an IP address to an interface

    main

    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)
    }
  5. Configure HandleOptions

    main

    When creating a handle via NewHandleWithOptions, you can provide HandleOptions to customize behavior:

    FieldTypeDescription
    DisableVFInfoCollectionboolIf true, skips fetching VF (Virtual Function) information for links. This improves performance if VF info is not needed.
    RetryInterruptedboolIf true, automatically retries dump operations that fail with EINTR before returning ErrDumpInterrupted.
    NetNS*netns.NsHandleSpecifies the network namespace to operate on. If nil, the current namespace is used.
  6. Configure VDPANewDevParams

    main

    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.HardwareAddr
    • MaxVQP: uint16
    • MTU: uint16
    • Features: uint64 (bitmask of features)
  7. Handle LinkNotFoundError

    main
    When attempting to retrieve or read links, the library may return a LinkNotFoundError. This error type is used to distinguish 'not found' scenarios from other netlink errors, allowing for more robust error handling in dependent code.
  8. Use FOU tunnel operations on non-Linux platforms

    main
    The FOU (Flexible Overlay) tunnel operations 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.
  9. Create a MirredAction

    main

    Use NewMirredAction(redirIndex int) to create a mirroring or redirection action. The MirredAction field uses MirredAct constants:

    • TCA_EGRESS_REDIR: Redirect to EGRESS
    • TCA_EGRESS_MIRROR: Mirror to EGRESS
    • TCA_INGRESS_REDIR: Redirect to INGRESS
    • TCA_INGRESS_MIRROR: Mirror to INGRESS