Convert Figma logo to code with AI

vishvananda logonetlink

Simple netlink library for go.

3,040
779
3,040
229

Top Related Projects

2,371

Some reference and example networking plugins, maintained by the CNI team.

networking for containers

22,159

eBPF-based Networking, Security, and Observability

12,469

CLI tool for spawning and running containers according to the OCI specification

5,437

Open Container Initiative-based implementation of Kubernetes Container Runtime Interface

9,167

flannel is a network fabric for containers, designed for Kubernetes

Quick Overview

Netlink is a Go package that provides low-level access to Linux networking interfaces and configurations. It allows developers to interact with network devices, manage routing tables, and configure network settings programmatically using native Go code.

Pros

  • High performance due to direct kernel communication
  • Comprehensive coverage of Linux networking features
  • Native Go implementation without CGo dependencies
  • Actively maintained and widely used in the Go ecosystem

Cons

  • Linux-specific, not portable to other operating systems
  • Requires root privileges for many operations
  • Steep learning curve due to low-level nature
  • API may change with kernel updates

Code Examples

  1. List network interfaces:
package main

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

func main() {
    links, err := netlink.LinkList()
    if err != nil {
        fmt.Println(err)
        return
    }
    for _, link := range links {
        fmt.Println(link.Attrs().Name)
    }
}
  1. Add a static route:
package main

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

func main() {
    route := &netlink.Route{
        Dst: &net.IPNet{
            IP:   net.IPv4(192, 168, 1, 0),
            Mask: net.CIDRMask(24, 32),
        },
        Gw: net.IPv4(192, 168, 0, 1),
    }
    err := netlink.RouteAdd(route)
    if err != nil {
        panic(err)
    }
}
  1. Create a VLAN interface:
package main

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

func main() {
    parent, _ := netlink.LinkByName("eth0")
    vlan := &netlink.Vlan{
        LinkAttrs: netlink.LinkAttrs{
            Name:        "vlan100",
            ParentIndex: parent.Attrs().Index,
        },
        VlanId: 100,
    }
    err := netlink.LinkAdd(vlan)
    if err != nil {
        panic(err)
    }
}

Getting Started

To use Netlink in your Go project:

  1. Install the package:

    go get github.com/vishvananda/netlink
    
  2. Import the package in your Go code:

    import "github.com/vishvananda/netlink"
    
  3. Use the package functions to interact with network interfaces and configurations. Most operations require root privileges, so run your program with sudo or as root.

  4. Refer to the package documentation and examples for specific use cases and API details.

Competitor Comparisons

2,371

Some reference and example networking plugins, maintained by the CNI team.

Pros of plugins

  • Specifically designed for container networking, offering a wide range of plugins for different network types
  • Implements the Container Network Interface (CNI) specification, ensuring compatibility with various container runtimes
  • Actively maintained with regular updates and contributions from the community

Cons of plugins

  • More complex to set up and configure compared to the simpler netlink library
  • Focused on container networking, which may be overkill for general network programming tasks
  • Requires understanding of CNI concepts and container orchestration

Code comparison

netlink:

link, err := netlink.LinkByName("eth0")
addr, err := netlink.AddrList(link, netlink.FAMILY_V4)

plugins:

conf := &types.NetConf{
    Name: "mynet",
    Type: "bridge",
}
result, err := ipam.ExecAdd("host-local", []byte(conf.IPAM.Marshal()))

Summary

netlink is a low-level Go library for interacting with Linux netlink sockets, providing direct access to network configuration. plugins is a collection of CNI-compatible networking plugins for container environments. While netlink offers more flexibility for general network programming, plugins is better suited for container-specific networking tasks and integration with container runtimes.

networking for containers

Pros of libnetwork

  • Designed specifically for container networking, offering higher-level abstractions
  • Provides a pluggable architecture for different network drivers
  • Includes built-in support for service discovery and load balancing

Cons of libnetwork

  • More complex and heavyweight compared to netlink's simpler approach
  • Primarily focused on Docker ecosystem, potentially limiting its general-purpose use
  • May have a steeper learning curve for developers not familiar with container networking concepts

Code Comparison

netlink:

link, err := netlink.LinkByName("eth0")
addr, _ := netlink.ParseAddr("192.168.1.1/24")
netlink.AddrAdd(link, addr)

libnetwork:

network, _ := controller.NewNetwork("bridge", "mynet", "")
endpoint, _ := network.CreateEndpoint("myendpoint")
container.Join(endpoint)

The netlink example demonstrates low-level network interface manipulation, while libnetwork showcases higher-level network and endpoint management for containers.

22,159

eBPF-based Networking, Security, and Observability

Pros of Cilium

  • Comprehensive container networking and security solution
  • Advanced features like L7 policy enforcement and Kubernetes integration
  • Active development with frequent updates and community support

Cons of Cilium

  • Higher complexity and steeper learning curve
  • Requires more system resources due to its extensive feature set
  • May be overkill for simpler networking tasks

Code Comparison

Netlink (Go):

link, err := netlink.LinkByName("eth0")
if err != nil {
    log.Fatal(err)
}

Cilium (C):

struct cilium_map *map = cilium_map_create("my_map", BPF_MAP_TYPE_HASH, 4, 4, 1024, 0);
if (!map) {
    fprintf(stderr, "Failed to create map\n");
    return 1;
}

Summary

Netlink is a lightweight Go library for interacting with the Linux netlink protocol, suitable for basic network configuration tasks. Cilium, on the other hand, is a comprehensive container networking and security solution with advanced features, but comes with increased complexity. Choose Netlink for simpler networking tasks and Cilium for more advanced container networking and security requirements in Kubernetes environments.

12,469

CLI tool for spawning and running containers according to the OCI specification

Pros of runc

  • Broader scope: runc is a complete container runtime, while netlink focuses solely on network operations
  • OCI-compliant: runc adheres to Open Container Initiative standards, ensuring compatibility with various container ecosystems
  • More active development: runc has a larger community and more frequent updates

Cons of runc

  • Higher complexity: runc's broader scope makes it more complex to use and understand compared to netlink's focused approach
  • Steeper learning curve: Due to its comprehensive feature set, runc requires more time to master than netlink

Code Comparison

netlink (Go):

link, err := netlink.LinkByName("eth0")
if err != nil {
    return err
}
netlink.LinkSetUp(link)

runc (Go):

spec, err := generate.New("linux")
if err != nil {
    return err
}
spec.Root = &specs.Root{Path: rootfsPath}
spec.Process = &specs.Process{Args: []string{"/bin/sh"}}

Both libraries use Go, but their purposes differ significantly. netlink focuses on network operations, while runc deals with container creation and management.

5,437

Open Container Initiative-based implementation of Kubernetes Container Runtime Interface

Pros of cri-o

  • Comprehensive container runtime implementation for Kubernetes
  • Supports multiple container image formats and registries
  • Actively maintained with regular updates and community support

Cons of cri-o

  • Larger codebase and more complex architecture
  • Specific to Kubernetes, less versatile for general networking tasks
  • Steeper learning curve for developers new to container runtimes

Code comparison

netlink:

link, err := netlink.LinkByName("eth0")
if err != nil {
    log.Fatal(err)
}

cri-o:

container, err := c.ContainerServer.GetContainerFromShortID(containerID)
if err != nil {
    return nil, err
}

Summary

netlink is a Go package for low-level network interface manipulation, while cri-o is a container runtime for Kubernetes. netlink offers more flexibility for general networking tasks, whereas cri-o provides a complete solution for container management in Kubernetes environments. The code examples demonstrate the different focus areas: netlink deals with network interfaces, while cri-o handles container operations. Choose netlink for network-specific tasks and cri-o for Kubernetes container runtime needs.

9,167

flannel is a network fabric for containers, designed for Kubernetes

Pros of Flannel

  • Provides a complete networking solution for container orchestration platforms like Kubernetes
  • Offers multiple backend options for network overlay, including VXLAN, host-gw, and UDP
  • Simplifies network configuration for multi-node container deployments

Cons of Flannel

  • More complex setup and configuration compared to Netlink
  • Limited to container networking use cases, less versatile for general network programming
  • May introduce additional overhead due to its overlay network approach

Code Comparison

Netlink (Go):

link, err := netlink.LinkByName("eth0")
if err != nil {
    log.Fatal(err)
}

Flannel (Go):

backend, err := vxlan.NewBackend(&vxlan.Config{
    Network:   config.Network,
    SubnetLen: config.SubnetLen,
    Port:      config.VNI,
})

Summary

Netlink is a low-level network interface library for Go, providing direct access to Linux networking primitives. It's versatile and suitable for various network programming tasks.

Flannel is a higher-level networking solution specifically designed for container orchestration. It simplifies network configuration for multi-node container deployments but is less flexible for general network programming compared to Netlink.

Choose Netlink for fine-grained control over network interfaces and general network programming. Opt for Flannel when setting up networking for container clusters, especially in Kubernetes environments.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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.