ifaces

package
v1.9.1-community Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPsFromInterface

func IPsFromInterface(ifaceName string) ([]netip.Addr, error)

Default function for getting the list of IPs configured for a specific network interface

Types

type Event

type Event struct {
	Type      EventType
	Interface Interface
}

Event of a network interface, given the type (added, removed) and the interface name

type EventType

type EventType int

EventType for an interface: added, deleted

const (
	EventAdded EventType = iota
	EventDeleted
)

func (EventType) String

func (e EventType) String() string

type Filter

type Filter interface {
	Allowed(iface string) (bool, error)
}

func FromConfig

func FromConfig(cfg *config.Agent) (Filter, error)

type Informer

type Informer interface {
	// Subscribe returns a channel that sends Event instances.
	Subscribe(ctx context.Context) (<-chan Event, error)
}

Informer provides notifications about each network interface that is added or removed from the host. Production implementations: Poller and Watcher.

type Interface added in v0.2.0

type Interface struct {
	InterfaceKey
	MAC   [6]uint8
	NetNS netns.NsHandle
}

func NewInterface

func NewInterface(index int, name string, mac [6]uint8, netNS netns.NsHandle, nsname string) Interface

type InterfaceKey

type InterfaceKey struct {
	Index  int
	Name   string
	NSName string
}

type Poller

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

Poller periodically looks for the network interfaces in the system and forwards Event notifications when interfaces are added or deleted.

func NewPoller

func NewPoller(period time.Duration, bufLen int) *Poller

func (*Poller) Subscribe

func (np *Poller) Subscribe(ctx context.Context) (<-chan Event, error)

type Registerer added in v0.2.0

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

Registerer is an informer that wraps another informer implementation, and keeps track of the currently existing interfaces in the system, accessible through the IfaceNameForIndex method.

func NewRegisterer added in v0.2.0

func NewRegisterer(inner Informer, cfg *config.Agent, m *metrics.Metrics) (*Registerer, error)

func (*Registerer) IfaceNameForIndexAndMAC

func (r *Registerer) IfaceNameForIndexAndMAC(idx int, mac [6]uint8) (string, bool)

IfaceNameForIndexAndMAC returns the interface name for a given interface index and MAC address, using the cached data from prior events observed by the underlying interfaces informer. It returns the interface name and true if a match is found, or an empty string and false if not.

If multiple MACs are associated with the same index, this function attempts to disambiguate using the provided MAC. If no exact match is found, it applies heuristics (e.g., preferred MAC prefix rules) to choose a preferred interface name. As a last resort, if no MAC match is possible, it returns the first name associated with the index to avoid falling back to a syscall.

A fallback to net.InterfaceByIndex is performed only if the index is not present in the cache, or the MAC does not match any known entry and no heuristic rule applies.

Concurrency note:

Without double-checked locking, the following sequence may occur:

  1. Goroutine A acquires RLock, sees r.ifaces[idx][mac] is missing
  2. Goroutine B does the same (also sees entry missing)
  3. Both release RLock and call net.InterfaceByIndex(idx)
  4. Both prepare to insert iface.Name into r.ifaces[idx][mac]
  5. Goroutine A acquires Lock and writes to the map
  6. Goroutine B acquires Lock and overwrites A's result

This results in a lost update. To prevent this, the function uses double-checked locking: it re-checks under Lock before inserting, ensuring that only one goroutine updates the cache.

func (*Registerer) Subscribe added in v0.2.0

func (r *Registerer) Subscribe(ctx context.Context) (<-chan Event, error)

type Watcher

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

Watcher uses system's netlink to get real-time information events about network interfaces' addition or removal.

func NewWatcher

func NewWatcher(bufLen int, m *metrics.Metrics) *Watcher

func (*Watcher) Subscribe

func (w *Watcher) Subscribe(ctx context.Context) (<-chan Event, error)

Jump to

Keyboard shortcuts

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