Network Package Reference
pkg/network/— NetworkManager, CNI, VXLAN, IPAM, and Discovery.
Overview
Section titled “Overview”The pkg/network package manages networking for Firecracker VMs, including bridge creation, TAP device setup, VXLAN overlay networks, and IP allocation.
Package Structure:
pkg/network/├── manager.go # NetworkManager (orchestration)├── vxlan.go # VXLAN overlay (cross-node)├── cni.go # CNI plugin integration├── cni_client.go # CNI client wrapper├── netlink.go # Netlink operations└── tap_executor.go # TAP device creationTest utilities live in
test/testhelpers/(they were moved out of thepkg/networkpackage).
NetworkManager
Section titled “NetworkManager”File: manager.go
The NetworkManager orchestrates all network operations for VMs.
Type Definition
Section titled “Type Definition”type NetworkManager struct { config types.NetworkConfig bridges map[string]bool mu sync.RWMutex tapDevices map[string]*TapDevice ipAllocator *IPAllocator natSetup bool vxlanMgr *VXLANManager peerDiscovery bool nodeDiscovery types.NodeDiscovery cniClient *CNIClient pendingPeers []string dnsmasqMu sync.Mutex}Configuration
Section titled “Configuration”type NetworkConfig struct { BridgeName string // e.g., "swarm-br0" Subnet string // e.g., "192.168.127.0/24" BridgeIP string // e.g., "192.168.127.1/24" IPMode string // "static" or "dhcp" NATEnabled bool // Enable masquerading VXLANEnabled bool // Enable cross-node overlay VXLANPeers []string // Initial peer IPs EnableRateLimit bool // Packet rate limiting MaxPacketsPerSec int // Rate limit threshold DHCPRangeStart int // DHCP range start offset (default 50) DHCPRangeEnd int // DHCP range end offset (default 200) NetnsPath string // CNI netns path (default /tmp/firecracker-ns) VXLANID int // VXLAN VNI (default 100) VXLANTunnelIP string // Overlay IP for this node}Constructor
Section titled “Constructor”func NewNetworkManager(config types.NetworkConfig) types.NetworkManagerExample:
config := types.NetworkConfig{ BridgeName: "swarm-br0", Subnet: "192.168.127.0/24", BridgeIP: "192.168.127.1/24", IPMode: "static", NATEnabled: true, VXLANEnabled: true,}
nm := network.NewNetworkManager(config)Methods
Section titled “Methods”func (nm *NetworkManager) Init(ctx context.Context) errorPurpose: Initialize network infrastructure.
Steps:
- Create Linux bridge (
swarm-br0) - Assign bridge IP address
- Enable IP forwarding
- Setup NAT/masquerading (if enabled)
- Create VXLAN device (if enabled)
Example:
if err := nm.Init(ctx); err != nil { log.Fatal(err)}PrepareNetwork
Section titled “PrepareNetwork”func (nm *NetworkManager) PrepareNetwork(ctx context.Context, task *types.Task) errorPurpose: Create the TAP device(s), allocate IPs, and attach the VM’s
network(s) to the bridge. TAP creation/removal is handled by the unexported
helpers createTapDevice(ctx, network, index, taskID) and
removeTapDevice(tap).
Parameters:
ctx— Context for cancellationtask— Task whoseNetworksare configured (annotations are added in place)
CleanupNetwork
Section titled “CleanupNetwork”func (nm *NetworkManager) CleanupNetwork(ctx context.Context, task *types.Task) errorPurpose: Tear down the task’s TAP devices and release its IP allocations.
GetTapIP
Section titled “GetTapIP”func (nm *NetworkManager) GetTapIP(taskID string) (string, error)Purpose: Return the allocated IP for a task’s TAP device.
IP allocation for a task is deterministic (SHA-256 of the task ID mapped
into the subnet, linear probing for collisions); releasing an IP is done via
(*IPAllocator).Release (see below).
UpdateVXLANPeers
Section titled “UpdateVXLANPeers”func (nm *NetworkManager) UpdateVXLANPeers(peers []string) errorPurpose: Update VXLAN forwarding database with new peers.
Implementation:
# For each peer IP:bridge fdb append dev swarm-br0-vxlan dst <peer-ip> vni 100 port 4789SetNodeDiscovery
Section titled “SetNodeDiscovery”func (nm *NetworkManager) SetNodeDiscovery(discovery types.NodeDiscovery)Purpose: Set discovery provider (Consul) for dynamic peer updates.
IPAllocator
Section titled “IPAllocator”File: manager.go
Type Definition
Section titled “Type Definition”type IPAllocator struct { subnet *net.IPNet gateway net.IP allocated map[string]string // IP → VM ID mapping mu sync.Mutex}Constructor
Section titled “Constructor”func NewIPAllocator(subnetStr, gatewayStr string) (*IPAllocator, error)Methods
Section titled “Methods”Allocate
Section titled “Allocate”func (a *IPAllocator) Allocate(vmID string) (string, error)Purpose: Allocate deterministic IP for VM.
Algorithm:
func (a *IPAllocator) hashToIP(vmID string) net.IP { h := sha256.New() h.Write([]byte(vmID)) hash := h.Sum(nil)
// IPv4: use first 4 bytes of hash n := binary.BigEndian.Uint32(hash[:4]) % (size - 2) ipInt := subnetBase + n + 1
return net.IP(ipInt)}Collision Resolution:
// Linear probing - try next IP if collisionfor i := 0; i < 256; i++ { if !isGateway && !isAllocated { allocated[ipStr] = vmID return ipStr, nil } ip = incIP(ip)}Release
Section titled “Release”func (a *IPAllocator) Release(ip string)Purpose: Release an IP back to the pool.
VXLANManager
Section titled “VXLANManager”File: vxlan.go
The VXLANManager handles VXLAN overlay network setup and peer management.
Type Definition
Section titled “Type Definition”type VXLANManager struct { BridgeName string // Bridge to attach (e.g., "swarm-br0") VXLANID int // VXLAN Network Identifier (e.g., 100) OverlayIP string // Overlay IP of this node vxlanPort int // UDP port (default 4789) peerStore PeerStore // Source of peer IPs mu sync.RWMutex // + private context/netlink executor fields}Peers are supplied by a PeerStore (GetPeers, AddPeer, RemovePeer).
NewStaticPeerStore(initialPeers []string) *StaticPeerStore is the map-backed
implementation used for manually configured peers.
Constructor
Section titled “Constructor”func NewVXLANManager(bridgeName string, vxlanID int, overlayIP string, peerStore PeerStore) *VXLANManagerParameters:
bridgeName— bridge the VXLAN device is attached tovxlanID— VXLAN Network IdentifieroverlayIP— this node’s overlay IPpeerStore— peer source (a nil store is replaced by an emptyStaticPeerStore)
Methods
Section titled “Methods”SetupVXLAN
Section titled “SetupVXLAN”func (v *VXLANManager) SetupVXLAN(physInterface, localIP string) errorPurpose: Create the VXLAN device, attach it to the bridge, add the overlay IP, and program initial peer forwarding entries.
Implementation:
# Create VXLAN deviceip link add swarm-br0-vxlan type vxlan id 100 dstport 4789 local <local-ip>
# Attach to bridgeip link set swarm-br0-vxlan master swarm-br0
# Bring upip link set swarm-br0-vxlan upAddRouteToSubnet
Section titled “AddRouteToSubnet”func (v *VXLANManager) AddRouteToSubnet(remoteSubnet, remoteOverlayIP string) errorPurpose: Route a remote subnet through a peer’s overlay IP.
UpdatePeers
Section titled “UpdatePeers”func (v *VXLANManager) UpdatePeers(newPeers []string) errorPurpose: Reconcile the VXLAN forwarding database with the given peer set (adds and removes FDB entries as needed).
GetPeers
Section titled “GetPeers”func (v *VXLANManager) GetPeers() []stringPurpose: Return the current peer list.
StartPeerDiscovery / StopPeerDiscovery
Section titled “StartPeerDiscovery / StopPeerDiscovery”func (v *VXLANManager) StartPeerDiscovery(ctx context.Context, localIP string, port int) errorfunc (v *VXLANManager) StopPeerDiscovery()Purpose: Start/stop UDP announcement-based peer discovery.
CNIClient
Section titled “CNIClient”File: cni.go, cni_client.go
CNI integration for SwarmKit network attachments.
Type Definition
Section titled “Type Definition”type CNIClient struct { config CNIConfig}
type CNIConfig struct { BinDir string // Path to CNI binaries (default: /opt/cni/bin) ConfDir string // Path to CNI configs (default: /etc/cni/net.d) CacheDir string // Path to CNI cache (default: /var/lib/cni) NetworkName string // Default network name (default: swarmcracker)}Constructor
Section titled “Constructor”func NewCNIClient(config CNIConfig) *CNIClientEmpty CNIConfig fields are filled with the defaults shown above.
Methods
Section titled “Methods”AddNetwork
Section titled “AddNetwork”func (c *CNIClient) AddNetwork(ctx context.Context, containerID, netns, ipCIDR, networkName string) (*CNIResult, error)Purpose: Invoke the CNI plugin’s ADD operation for a container’s netns.
Returns:
type CNIResult struct { CNIVersion string Interfaces []CNIInterface IPs []CNIIP Routes []CNIRoute}DelNetwork
Section titled “DelNetwork”func (c *CNIClient) DelNetwork(ctx context.Context, containerID, netns, networkName string) errorPurpose: Invoke the CNI plugin’s DEL operation.
Discovery
Section titled “Discovery”Consul-based peer discovery for VXLAN lives in the separate pkg/discovery
package (consul.go), not in pkg/network. NetworkManager.SetNodeDiscovery
accepts a types.NodeDiscovery provider, which the SwarmKit integration wires
to the Consul client so that peer updates are programmed into the VXLAN
forwarding database.
Netlink Operations
Section titled “Netlink Operations”File: netlink.go
Low-level link/address/route programming goes through a NetlinkExecutor
interface; NewDefaultNetlinkExecutor() returns the real
vishvananda/netlink-backed implementation. The interface exposes
LinkByName, LinkAdd, LinkDel, LinkSetUp, LinkSetDown,
LinkSetMaster, LinkSetMTU, LinkSetNsFd, AddrAdd, AddrDel, AddrList,
RouteAdd, RouteDel, RouteList, BridgeVlanAdd, BridgeVlanDel,
NeighAdd, NeighDel, and NeighList. Tests substitute a mock implementation.
TAP helpers
Section titled “TAP helpers”File: cni.go
func CreateTAPDevice(name, bridge string) (*TAPDevice, error)func DeleteTAPDevice(name string) errorfunc SetupVXLANFDB(tapName string, peers []string) errorThese standalone helpers (usable by the CNI plugin) create/delete a TAP device,
attach it to a bridge, and program VXLAN FDB entries through a TAPExecutor
(NewDefaultTAPExecutor()), which has injectable *WithExecutor variants for
testing.
TAP Device
Section titled “TAP Device”File: manager.go, cni.go
pkg/network has two TAP representations:
TapDevice(manager.go) — the per-VM device tracked by the manager, with full network details.TAPDevice(cni.go) — the result of the standalone CNI/CreateTAPDevicehelper.
type TapDevice struct { Name string // e.g., "tap-abc123" Bridge string // e.g., "swarm-br0" IP string // e.g., "192.168.127.42" Netmask string // e.g., "255.255.255.0" Gateway string // e.g., "192.168.127.1" Subnet string // e.g., "192.168.127.0/24"}
type TAPDevice struct { Name string MAC string Bridge string IP string Netmask string}NAT/Masquerading
Section titled “NAT/Masquerading”Enabled by default for VM internet access.
Implementation:
# Enable IP forwardingsysctl -w net.ipv4.ip_forward=1
# Setup masqueradingiptables -t nat -A POSTROUTING -s 192.168.127.0/24 ! -o swarm-br0 -j MASQUERADEConfiguration:
network: nat_enabled: trueRate Limiting
Section titled “Rate Limiting”Optional packet rate limiting on TAP devices.
Configuration:
network: enable_rate_limit: true max_packets_per_sec: 10000Implementation:
# Using tc (traffic control)tc qdisc add dev <tap> root handle 1: htbtc class add dev <tap> parent 1: classid 1:1 htb rate <rate>tc filter add dev <tap> parent 1: protocol ip prio 1 u32 match u32 0 0 flowid 1:1Testing
Section titled “Testing”Mock NetworkManager
Section titled “Mock NetworkManager”pkg/network tests use mocks_test.go. A minimal stand-in for the
types.NetworkManager interface looks like:
type MockNetworkManager struct { Prepared map[string]bool}
func (m *MockNetworkManager) PrepareNetwork(ctx context.Context, task *types.Task) error { m.Prepared[task.ID] = true return nil}
func (m *MockNetworkManager) CleanupNetwork(ctx context.Context, task *types.Task) error { delete(m.Prepared, task.ID) return nil}
func (m *MockNetworkManager) GetTapIP(taskID string) (string, error) { return "192.168.127.42", nil}Error Handling
Section titled “Error Handling”Common Errors
Section titled “Common Errors”| Error | Cause | Resolution |
|---|---|---|
"invalid subnet" |
Bad CIDR notation | Use valid format (e.g., 192.168.127.0/24) |
"bridge creation failed" |
Permission denied | Run with root/capabilities |
"failed to allocate IP" |
Subnet exhausted | Increase subnet size or cleanup VMs |
"vxlan device creation failed" |
VXLAN module missing | Load vxlan kernel module |
Related Documentation
Section titled “Related Documentation”| Topic | Document |
|---|---|
| SwarmKit executor | SwarmKit Reference |
| User networking guide | Networking Guide |
| Architecture | Architecture Overview |