Documentation
¶
Index ¶
Constants ¶
const ( // HeaderLength is the Ethernet header length. HeaderLength = 14 // ChecksumLength is the frame check sequence (FCS) length (32-bit CRC). ChecksumLength = 4 // MTU (maximum transmission unit) is the maximum number of bytes that are // allowed on the payload of a frame (the link layer name for a packet). MTU = physical.MTU - HeaderLength - ChecksumLength )
Variables ¶
This section is empty.
Functions ¶
func BroadcastMACAddress ¶
func BroadcastMACAddress() net.HardwareAddr
BroadcastMACAddress is the IP address used for broadcast in a local network.
func BroadcastMACEndpoint ¶
BroadcastMACEndpoint is the IP address used for broadcast in a local network.
Types ¶
type EthernetPort ¶
type EthernetPort interface { Send(ctx context.Context, frame *gplayers.Ethernet) error Recv() <-chan *gplayers.Ethernet Close() error ForwardingMode() bool MACAddress() gopacket.Endpoint }
EthernetPort represents a hypothetical Ethernet network interface card, composed by a physical wire and a MAC address.
When sending a frame out it goes with the MAC address of the port as its src MAC address, unless if running on "forwarding mode".
Inbound frames with dst MAC address not matching the port's MAC address will be discarded, unless if running on "forwarding mode" or if the dst MAC address is the broadcast MAC address.
func NewEthernetPort ¶
func NewEthernetPort(ctx context.Context, conf EthernetPortConfig) (EthernetPort, error)
NewEthernetPort creates an EthernetPort from config.
type EthernetPortConfig ¶
type EthernetPortConfig struct { // ForwardingMode keeps inbound frames with wrong dst MAC address. ForwardingMode bool `yaml:"forwardingMode"` MACAddress string `yaml:"macAddress"` MetricLabels struct { StackName string `yaml:"stackName"` } `yaml:"metricLabels"` Medium physical.FullDuplexUnreliableWireConfig `yaml:"fullDuplexUnreliableWire"` }
EthernetPortConfig contains the configs for the concrete implementation of EthernetPort.
type SwitchConfig ¶
type SwitchConfig struct { MetricLabels struct { StackName string `yaml:"stackName"` } `yaml:"metricLabels"` Ports []EthernetPortConfig `yaml:"ethernetPorts"` }
SwitchConfig contains the configs for RunSwitch().
type SwitchWaitCloseFunc ¶
func RunSwitch ¶
func RunSwitch(ctx context.Context, conf SwitchConfig) (SwitchWaitCloseFunc, error)
RunSwitch runs a hypothetical L2 switch, which decaps and forwards ethernet frames based on a forwarding table constructed by learning L2 routes on the fly: frame comes in, a mapping from src MAC address to port number is cached. No mapping present on the table: frame is forwarded to all other ports. If dst MAC address matches the MAC address of one of the switch's ports, the frame is discarded.
The returned function blocks until the switch has stopped running, which happens upon the given ctx being cancelled. The function argument is another function, passed for consuming any potential frames that remained in the ports.