physnet

package
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package physnet connects VirtoMir to the physical network (D16, ADR-0075): SR-IOV virtual functions on the host's NICs, BGP (and EVPN for the VXLAN overlays) through FRR, IP address pools that hand machines fixed addresses, and dynamic DNS (RFC 2136 with TSIG) for them. Commands run through a Runner and sysfs through a root directory, so everything is tested without the hardware. SR-IOV and BGP are experimental: not tested on hardware.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid  = errors.New("invalid")
	ErrNotFound = errors.New("not found")
	ErrExists   = errors.New("exists")
	ErrBusy     = errors.New("in use")
	ErrConflict = errors.New("conflict")
)

Errors for the API.

View Source
var FRRFile = "/etc/frr/virtomir-bgp.conf"

FRRFile is where the section is kept for people to read; FRR's own saved configuration holds it too after write memory.

View Source
var Host = SRIOV{Root: "/", RulesDir: "/etc/udev/rules.d"}

Host is the real host.

Functions

func FRRConfig

func FRRConfig(c BGPConfig) (string, error)

FRRConfig is the section VirtoMir gives FRR.

func Register

func Register(ctx context.Context, c DNSConfig, host string, ips []netip.Addr) error

Register points host's name at ips (replacing what it had) and each address back at the name.

func ReverseName

func ReverseName(ip netip.Addr) string

ReverseName is the PTR name of an address: 50.0.20.10.in-addr.arpa.

func Unregister

func Unregister(ctx context.Context, c DNSConfig, host string, ips []netip.Addr) error

Unregister removes host's addresses and the PTR records of ips.

Types

type Allocation

type Allocation struct {
	IP     string `json:"ip"`
	MAC    string `json:"mac,omitempty"`
	VMUUID string `json:"vmUuid,omitempty"`
	VMName string `json:"vmName,omitempty"`
	// Hostname is the machine's DNS name in the zone ("" = not registered).
	Hostname string    `json:"hostname,omitempty"`
	Host     string    `json:"host,omitempty"` // the member the machine was created on
	Created  time.Time `json:"created"`
}

Allocation is an address a machine holds.

type BGPConfig

type BGPConfig struct {
	Enabled   bool          `json:"enabled"`
	LocalAS   uint32        `json:"localAs"`
	RouterID  string        `json:"routerId"`
	Neighbors []BGPNeighbor `json:"neighbors"`
	// Announce are the networks this host announces (its NAT or routed subnets).
	Announce []string `json:"announce"`
	// EVPN announces the host's VXLAN overlays (advertise-all-vni), so leaf switches and
	// other hosts learn where machines are instead of flooding.
	EVPN bool `json:"evpn"`
	// AppliedAS is the AS VirtoMir last configured, to remove that section on a change.
	AppliedAS uint32 `json:"appliedAs,omitempty"`
}

BGPConfig is this host's BGP speaker.

func ApplyBGP

func ApplyBGP(ctx context.Context, r Runner, c BGPConfig) (BGPConfig, error)

ApplyBGP replaces VirtoMir's BGP section in the running FRR and saves FRR's configuration; it returns the config with AppliedAS set. Neighbors reconnect.

func (BGPConfig) Validate

func (c BGPConfig) Validate() error

Validate checks every value that ends up in FRR's configuration.

type BGPNeighbor

type BGPNeighbor struct {
	Address     string `json:"address"`
	RemoteAS    uint32 `json:"remoteAs"`
	Description string `json:"description,omitempty"`
	Password    string `json:"password,omitempty"`
	BFD         bool   `json:"bfd,omitempty"`
}

BGPNeighbor is a router this host peers with.

type BGPSession

type BGPSession struct {
	Address     string   `json:"address"`
	RemoteAS    uint32   `json:"remoteAs"`
	Description string   `json:"description,omitempty"`
	State       string   `json:"state"` // Established, Active, Connect, Idle, …
	Uptime      string   `json:"uptime,omitempty"`
	Received    int      `json:"prefixesReceived"`
	Sent        int      `json:"prefixesSent"`
	Families    []string `json:"families"` // ipv4Unicast, ipv6Unicast, l2VpnEvpn
}

BGPSession is a neighbor as FRR sees it.

func Sessions

func Sessions(ctx context.Context, r Runner) ([]BGPSession, error)

Sessions reads `show bgp summary json` (all address families).

type DNSConfig

type DNSConfig struct {
	Enabled bool   `json:"enabled"`
	Server  string `json:"server"` // host or host:port of the primary server
	Zone    string `json:"zone"`   // corp.example.
	// ReverseZones get the PTR records: 20.10.in-addr.arpa., …; none = no PTR records.
	ReverseZones []string `json:"reverseZones,omitempty"`
	KeyName      string   `json:"keyName,omitempty"`   // the TSIG key, e.g. virtomir.
	Algorithm    string   `json:"algorithm,omitempty"` // hmac-sha256 (default), hmac-sha512 or hmac-md5
	Secret       string   `json:"secret,omitempty"`    // base64, as in the server's key file
	TTL          uint32   `json:"ttl,omitempty"`
}

DNSConfig is where updates go and how they are signed.

func (DNSConfig) Name

func (c DNSConfig) Name(host string) (string, error)

Name is a machine's name in the zone.

func (*DNSConfig) Normalize

func (c *DNSConfig) Normalize() error

Normalize fills defaults (port 53, TTL 300, hmac-sha256) and checks the rest.

type Exec

type Exec struct{}

Exec runs commands on this host.

func (Exec) Run

func (Exec) Run(ctx context.Context, name string, args ...string) ([]byte, error)

Run runs name with args; an error carries the command's standard error.

type FRRStatus

type FRRStatus struct {
	Installed bool   `json:"installed"`
	Version   string `json:"version,omitempty"`
	BGPD      bool   `json:"bgpd"`
}

FRRStatus says whether FRR runs here with bgpd.

func DetectFRR

func DetectFRR(ctx context.Context, r Runner) FRRStatus

DetectFRR asks vtysh for FRR's version and running daemons.

type IPPool

type IPPool struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Description string   `json:"description,omitempty"`
	Network     string   `json:"network,omitempty"` // the libvirt network machines join for it
	CIDR        string   `json:"cidr"`
	Gateway     string   `json:"gateway,omitempty"`
	RangeStart  string   `json:"rangeStart"`
	RangeEnd    string   `json:"rangeEnd"`
	DNSServers  []string `json:"dnsServers,omitempty"`
	// Domain is the DNS search domain the machines get.
	Domain      string       `json:"domain,omitempty"`
	Reserved    []string     `json:"reserved,omitempty"`
	Allocations []Allocation `json:"allocations"`
	Created     time.Time    `json:"created"`
	Updated     time.Time    `json:"updated"`
}

IPPool is a range of addresses of one network.

func (IPPool) Prefix

func (p IPPool) Prefix() netip.Prefix

Prefix is the pool's network.

type Released

type Released struct {
	Pool string `json:"pool"`
	Allocation
}

Released is an allocation that was given back, with its pool.

type Runner

type Runner interface {
	Run(ctx context.Context, name string, args ...string) ([]byte, error)
}

Runner runs a command and returns its standard output.

type SRIOV

type SRIOV struct {
	Root     string
	RulesDir string
}

SRIOV reads the host's SR-IOV NICs from sysfs under root ("/" on a host) and the udev rules VirtoMir wrote in rulesDir.

func (SRIOV) Devices

func (s SRIOV) Devices() ([]SRIOVDevice, error)

Devices lists the SR-IOV capable NICs.

func (SRIOV) SetVFs

func (s SRIOV) SetVFs(iface string, n int, persistent bool) error

SetVFs gives a NIC n virtual functions (0 removes them). The kernel only changes a non-zero count through 0, so machines using its VFs lose them: callers check first. With persistent, a udev rule sets the count again at boot.

type SRIOVDevice

type SRIOVDevice struct {
	Interface  string `json:"interface"`
	PCI        string `json:"pci"`
	Driver     string `json:"driver"`
	TotalVFs   int    `json:"totalVfs"`
	CurrentVFs int    `json:"currentVfs"`
	VFs        []VF   `json:"vfs"`
	// Persistent: a udev rule sets the VF count again at boot.
	Persistent bool `json:"persistent"`
}

SRIOVDevice is an SR-IOV capable NIC.

type Shared

type Shared struct {
	Version int       `json:"version"`
	Pools   []IPPool  `json:"pools"`
	DNS     DNSConfig `json:"dns"`
}

Shared is what members exchange (newest version wins): the IP pools with their allocations and the dynamic DNS settings.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store keeps -state-dir/physnet.json.

func Open

func Open(path string) (*Store, error)

Open loads path; a missing file is an empty store.

func (*Store) Adopt

func (s *Store) Adopt(sh Shared) (bool, error)

Adopt takes over a member's shared state when it is newer; it reports whether it did.

func (*Store) Allocate

func (s *Store) Allocate(id string, a Allocation, want string) (Allocation, error)

Allocate gives a machine an address of pool id: want if free, else the first free one. A machine that holds an address of the pool keeps it.

func (*Store) BGP

func (s *Store) BGP() BGPConfig

BGP returns this host's BGP settings.

func (*Store) Check

func (s *Store) Check(id, ip string) (string, error)

Check says whether ip is free in pool id ("" = yes).

func (*Store) Claim

func (s *Store) Claim(id, ip, uuid string) error

Claim binds an address that was handed out before the machine existed to its UUID.

func (*Store) CreatePool

func (s *Store) CreatePool(p IPPool) (IPPool, error)

CreatePool adds a pool; it must not overlap another pool's range.

func (*Store) DNS

func (s *Store) DNS() DNSConfig

DNS returns the dynamic DNS settings.

func (*Store) DeletePool

func (s *Store) DeletePool(id string) error

DeletePool removes a pool nobody holds addresses of.

func (*Store) Pool

func (s *Store) Pool(id string) (IPPool, bool)

Pool returns one pool.

func (*Store) Pools

func (s *Store) Pools() []IPPool

Pools lists the IP pools by name.

func (*Store) Release

func (s *Store) Release(id, ip string) error

Release frees an address of pool id.

func (*Store) ReleaseVM

func (s *Store) ReleaseVM(uuid string) ([]Released, error)

ReleaseVM frees every address a machine holds.

func (*Store) SetBGP

func (s *Store) SetBGP(c BGPConfig) error

SetBGP saves this host's BGP settings (not shared: router IDs differ).

func (*Store) SetDNS

func (s *Store) SetDNS(c DNSConfig) (DNSConfig, error)

SetDNS saves the dynamic DNS settings; an empty secret keeps the stored one.

func (*Store) Shared

func (s *Store) Shared() Shared

Shared returns the pools and DNS settings with their version.

func (*Store) UpdatePool

func (s *Store) UpdatePool(p IPPool) (IPPool, error)

UpdatePool changes a pool; addresses machines hold must stay inside it.

type VF

type VF struct {
	Index  int    `json:"index"`
	PCI    string `json:"pci"`              // 0000:04:10.1
	Netdev string `json:"netdev,omitempty"` // only while the host's driver has it
	MAC    string `json:"mac,omitempty"`
}

VF is one virtual function of a NIC.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL