Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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
}
type InterfaceKey ¶
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.
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 (*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:
- Goroutine A acquires RLock, sees r.ifaces[idx][mac] is missing
- Goroutine B does the same (also sees entry missing)
- Both release RLock and call net.InterfaceByIndex(idx)
- Both prepare to insert iface.Name into r.ifaces[idx][mac]
- Goroutine A acquires Lock and writes to the map
- 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.