mac

package
v1.21.0-pre.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 3 Imported by: 54

Documentation

Index

Constants

View Source
const EthHdrLen = 14

Untagged ethernet (IEEE 802.3) frame header len

Variables

This section is empty.

Functions

This section is empty.

Types

type MAC

type MAC [6]byte

MAC is an IEEE 802 MAC-48 address.

It is a comparable value type: two MACs may be compared with == and a MAC may be used as a map key. Its zero value means "unset", which is how a device carrying no layer 2 address, such as an L3/NOARP device, is represented.

MAC can also be used in CRD-embedded structs. It is serialized as a string and validated by the API server on admission.

+kubebuilder:validation:Type=string +kubebuilder:validation:Format=mac

func FromHardwareAddr

func FromHardwareAddr(ha net.HardwareAddr) (MAC, error)

FromHardwareAddr converts ha to a MAC. Like ParseMAC it only accepts an IEEE 802 MAC-48 address, so a device carrying no layer 2 address, such as an L3/NOARP device, yields an error rather than an unset MAC.

Prefer this over a plain MAC(ha) conversion, which panics at run time on a net.HardwareAddr shorter than 6 bytes.

func GenerateRandMAC added in v0.15.7

func GenerateRandMAC() (MAC, error)

GenerateRandMAC generates a random unicast and locally administered MAC address.

func MustParseMAC added in v1.19.0

func MustParseMAC(s string) MAC

MustParseMAC calls ParseMAC and panics on error. It is intended for use in tests with hard-coded strings.

func ParseMAC

func ParseMAC(s string) (MAC, error)

ParseMAC parses s only as an IEEE 802 MAC-48.

func ParseMACOrUnset

func ParseMACOrUnset(s string) (MAC, error)

ParseMACOrUnset is ParseMAC with the empty string parsed as an unset MAC rather than as an error, matching MAC.UnmarshalText.

It is meant for the sources which report a MAC address optionally, such as the cloud provider interface and statuses in a CiliumNode: an absent value is legitimate there, and only a malformed one is rejected. Consumers which do require a MAC must reject the unset one themselves.

func (MAC) HardwareAddr

func (m MAC) HardwareAddr() net.HardwareAddr

HardwareAddr returns m as a net.HardwareAddr, or nil if m is unset. The result is a copy and does not alias m.

Unset must map to nil rather than to six zero bytes: consumers such as netlink treat a nil net.HardwareAddr as "no address requested" and a non-nil one as an explicit address to set, which the kernel rejects for devices that carry no layer 2 address.

func (MAC) IsValid

func (m MAC) IsValid() bool

IsValid reports whether m is set. Devices without a layer 2 address, such as L3/NOARP devices, carry an invalid MAC.

func (MAC) MarshalText added in v1.20.0

func (m MAC) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as the one returned by MAC.String.

func (MAC) String

func (m MAC) String() string

String returns the string representation of m, or the empty string if m is unset.

func (*MAC) UnmarshalText added in v1.20.0

func (m *MAC) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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