etherconn

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// XDPSendingModeSingle is the TX mode where the forwarder sends a packet a time, this is the default mode
	XDPSendingModeSingle xdpSendingMode = "single"
	// XDPSendingModeBatch is the TX mode where the forwarder sends a batch of packets a time,
	// only use this mode when there is a high number of TX
	XDPSendingModeBatch xdpSendingMode = "batch"
)

Variables

View Source
var (
	// ErrTimeOut is the error returned when opeartion timeout
	ErrTimeOut = errors.New("timeout")
	// ErrRelayStopped is the error returned when relay already stopped
	ErrRelayStopped = errors.New("relay stopped")
)
View Source
var BroadCastMAC = net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

Functions

This section is empty.

Types

type EtherConn

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

EtherConn sends/receives Ethernet payload like IP packet with Ethernet encapsulation, without provisioning them in OS. it needs to be registered with a PacketRelay instance to work.

func NewEtherConn

func NewEtherConn(mac net.HardwareAddr, relay PacketRelay, ethernetTypes []uint16, options ...EthernetOption) *EtherConn

NewEtherConn creates a new EtherConn instance, mac is used as part of EtherConn's L2Endpoint; Relay is the PacketRelay forwarder where the Ethernet packets are sent and received . Ethernet types are the supported types for RX, if empty, default list contains ARP, IPv4 and IPv6.

func (*EtherConn) Close

func (ec *EtherConn) Close() error

func (*EtherConn) LocalAddr

func (ec *EtherConn) LocalAddr() net.Addr

func (*EtherConn) ReadPacket added in v1.1.0

func (ec *EtherConn) ReadPacket() ([]byte, *L2Endpoint, error)

ReadPacket returns received Ethernet payload bytes, along with the remote L2Endpoint. ReadPacket only returns payload that matches one of underlying PacketRelay's configured EtherTypes.

func (*EtherConn) ReadPacketFrom added in v1.1.0

func (ec *EtherConn) ReadPacketFrom(p []byte) (int, *L2Endpoint, error)

ReadPacketFrom copies the received Ethernet payload to p, this is a wrapper of ReadPacket.

func (*EtherConn) SetDeadline

func (ec *EtherConn) SetDeadline(t time.Time) error

func (*EtherConn) SetReadDeadline

func (ec *EtherConn) SetReadDeadline(t time.Time) error

func (*EtherConn) SetWriteDeadline

func (ec *EtherConn) SetWriteDeadline(t time.Time) error

func (*EtherConn) WriteIPData added in v1.1.0

func (ec *EtherConn) WriteIPData(p []byte, destMac net.HardwareAddr) (int, error)

WriteIPData sends an IPv4/IPv6 packet to destMac.

func (*EtherConn) WritePacketTo added in v1.1.0

func (ec *EtherConn) WritePacketTo(p []byte, ethernetType uint16, destMac net.HardwareAddr) (int, error)

WritePacketTo sends an Ethernet payload, along with specified EtherType, to destMac.

type EthernetOption added in v1.1.0

type EthernetOption func(ec *EtherConn)

EthernetOption is a function use to provide customized option when creating EtherConn

func WithReceiveMulticast added in v1.1.0

func WithReceiveMulticast(multicast bool) EthernetOption

WithReceiveMulticast allow/disallow EtherConn to receive multicast/broadcast Ethernet traffic

type EthernetResponse added in v1.1.0

type EthernetResponse struct {
	//LocalEndpoint/RemoteEndpoint is the local/remote L2Endpoint
	LocalEndpoint, RemoteEndpoint *L2Endpoint
	// EtherBytes is the Ethernet frame bytes
	EtherBytes []byte
	// EtherPayloadBytes is the Ethernet payload bytes within the EtherBytes,
	// where payload belongs to the specified EtherTypes,
	// default are 0x0800 (IPv4) and 0x86dd (IPv6),
	// nil if there is no payload with specified EtherTypes;
	EtherPayloadBytes []byte
	// TransportPayloadBytes is the transport layer(UDP/TCP/SCTP) payload bytes within the IPBytes,nil for unsupport transport layer
	TransportPayloadBytes []byte
	// RemoteIP is the remote IP address
	RemoteIP net.IP
	// RemotePort is the remote transport layer port, 0 for unsupport transport layer
	RemotePort uint16
	// Protocol is the IP protocol number
	Protocol uint8
	// LocalIP is the local IP address
	LocalIP net.IP
	// LocalPort is the local transport layer port, 0 for unsupport transport layer
	LocalPort uint16
}

EthernetResponse is what PacketRelay received and parsed

type L2Endpoint

type L2Endpoint struct {
	HwAddr       net.HardwareAddr
	EthernetType uint16 // inner most EtherType (e.g payload type)
}

L2Endpoint represents a layer 2 MAC address that send/receives Ethernet frames

func (*L2Endpoint) GetKey

func (l2e *L2Endpoint) GetKey() (key L2EndpointKey)

func (*L2Endpoint) Network

func (l2e *L2Endpoint) Network() string

func (*L2Endpoint) String

func (l2e *L2Endpoint) String() (s string)

String implements net.Addr interface, return a string with format: l2ep&<l2EndpointKey_str>, see L2EndpointKey.String for format of <l2EndpointKey_str>

type L2EndpointKey

type L2EndpointKey [8]byte

L2EndpointKey is key identify a L2 EndPoint 1. First 6 bytes are MAC address 2. Last 2 bytes are innermost EtherType

func (L2EndpointKey) String

func (l2epkey L2EndpointKey) String() string

type L4HashKey added in v1.1.0

type L4HashKey [19]byte

L4HashKey represents a Layer4 (transport) endpoint hashed key. [0:15] bytes is the IP address, [16] is the IP protocol, [17:18] is the port number, in big endian

func NewL4HashKeyWithEthernet added in v1.1.0

func NewL4HashKeyWithEthernet(eth *EthernetResponse) (r L4HashKey)

NewL4HashKeyWithEthernet returns a L4HashKey from a Ethernet response

func NewL4HashKeyWithUDPAddr added in v1.1.0

func NewL4HashKeyWithUDPAddr(addr *net.UDPAddr) (r L4HashKey)

NewL4HashKeyWithUDPAddr returns a L4HashKey from a net.UDPAddr

type NextHopResolver added in v1.1.0

type NextHopResolver func(net.IP) net.HardwareAddr

NextHopResolver resolves the target MAC address for the destination given its IP.

type PacketRelay

type PacketRelay interface {
	// Register registers a list of L2EndpointKey of a EtherConn, PacketRely send/receive packet on its behalf,
	// it returns following channels:
	// receive is the channel used to receive;
	// send is the channel used to send;
	// stop is a channel that will be closed when PacketRelay stops sending;
	// if multicastSupport is true, then multicast ethernet traffic will be received as well;
	// if one of key is already registered, then existing key will be overridden;
	Register(ks []L2EndpointKey, multicast bool) (recv chan *EthernetResponse, send chan []byte, stop chan struct{}, registrationID int)
	// Unregister removes L2EndpointKey from registration
	Unregister(registrationID int)
	// Close stops the forwarding of Ethernet packets
	Close() error
	// InterfaceName returns binding interface name
	InterfaceName() string
}

PacketRelay represents the Ethernet packet forwarder implementation.

type RUDPConn

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

RUDPConn implement net.PacketConn interface; it's used to send/receive UDP payload, using an underlying EtherConn for packet forwarding.

func NewRUDPConn

func NewRUDPConn(src *net.UDPAddr, c *EtherConn, options ...RUDPConnOption) (*RUDPConn, error)

NewRUDPConn creates a new RUDPConn, with specified EtherConn, and, optionally RUDPConnOption(s). src is the source UDP Address, it could be any IP address, even address not provisioned in OS, like 0.0.0.0.

func (*RUDPConn) Close

func (ruc *RUDPConn) Close() error

Close closes underlying EtherConn

func (*RUDPConn) LocalAddr

func (ruc *RUDPConn) LocalAddr() net.Addr

LocalAddr implements net.PacketConn interface, it returns its UDPAddr

func (*RUDPConn) ReadFrom

func (ruc *RUDPConn) ReadFrom(p []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn interface, and copies UDP payload to p. Note: the underlying EtherConn will send all received packets as *EthernetResponse to RUDPConn, RUDPConn will ignore packets that aren't destined to its UDPAddr, unless WithAcceptAny(true) is specified when creating the RUDPConn. In that case, RUDPConn will accept any UDP packet.

func (*RUDPConn) SetDeadline

func (ruc *RUDPConn) SetDeadline(t time.Time) error

func (*RUDPConn) SetReadDeadline

func (ruc *RUDPConn) SetReadDeadline(t time.Time) error

func (*RUDPConn) SetWriteDeadline

func (ruc *RUDPConn) SetWriteDeadline(t time.Time) error

func (*RUDPConn) WriteTo

func (ruc *RUDPConn) WriteTo(p []byte, dstAddr net.Addr) (int, error)

WriteTo sends UDP payload to the specified target. This function adds UDP and IP headers, and uses RUDPConn's resolve function to obtain next hop's MAC address, and use underlying EtherConn to send IP packet, with EtherConn's Ethernet encapsulation.

type RUDPConnOption

type RUDPConnOption func(rudpc *RUDPConn)

RUDPConnOption is a function use to provide customized option when creating RUDPConn

func WithAcceptAny

func WithAcceptAny(accept bool) RUDPConnOption

WithAcceptAny allows RUDPConn to accept any UDP packet, even if this isn't the destination address

func WithResolveNextHopMacFunc

func WithResolveNextHopMacFunc(f NextHopResolver) RUDPConnOption

WithResolveNextHopMacFunc specifies a function to resolve a destination IP address to next-hop MAC address. By default, resolveNextHopMACWithBroadcast is used.

type SharedEthernetConn added in v1.1.0

type SharedEthernetConn interface {
	Register(key L4HashKey) chan *EthernetResponse
	Unregister(k L4HashKey)
	WriteIPPktTo(p []byte, dstMac net.HardwareAddr) (int, error)
	SetWriteDeadline(t time.Time) error
}

SharedEthernetConn is a shared connection where multiple parallel connections can be opened simultaneously over it.

type SharedRUDPConn added in v1.1.0

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

SharedRUDPConn is the UDP connection could share same SharedEtherConn;

func NewSharingRUDPConn

func NewSharingRUDPConn(src *net.UDPAddr, c SharedEthernetConn, roptions ...RUDPConnOption) (*SharedRUDPConn, error)

NewSharingRUDPConn creates a new SharedRUDPConn, to run a UDP Conn over a custom SharedEtherConn network. SharedRUDPConn doesn't support accepting packets for other MAC addresses, and only WithResolveNextHopMacFunc option is supported.

func (*SharedRUDPConn) Close added in v1.1.0

func (shared *SharedRUDPConn) Close() error

func (*SharedRUDPConn) LocalAddr added in v1.1.0

func (shared *SharedRUDPConn) LocalAddr() net.Addr

func (*SharedRUDPConn) ReadFrom added in v1.1.0

func (shared *SharedRUDPConn) ReadFrom(p []byte) (int, net.Addr, error)

func (*SharedRUDPConn) SetDeadline added in v1.1.0

func (shared *SharedRUDPConn) SetDeadline(t time.Time) error

func (*SharedRUDPConn) SetReadDeadline added in v1.1.0

func (shared *SharedRUDPConn) SetReadDeadline(t time.Time) error

func (*SharedRUDPConn) SetWriteDeadline added in v1.1.0

func (shared *SharedRUDPConn) SetWriteDeadline(t time.Time) error

func (*SharedRUDPConn) WriteTo added in v1.1.0

func (shared *SharedRUDPConn) WriteTo(p []byte, addr net.Addr) (int, error)

WriteTo sends UDP payload to the specified target. This function adds UDP and IP headers, and uses RUDPConn's resolve function to obtain next hop's MAC address, and use underlying EtherConn to send IP packet, with EtherConn's Ethernet encapsulation.

type XDPRelay

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

XDPRelay uses Linux AF_XDP socket as the underlying forwarding mechanism, so it achieves higher performance than RawSocketRelay in multicore setup, XDPRelay usage notes:

  1. for virtio interface, the number of queues provisioned needs to be 2x of number CPU cores VM has, binding will fail otherwise.
  2. AF_XDP is still relative new, see XDP kernel&driver support status: https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md#xdp
  3. For best performance: a) use NIC multiple queues and multiple routine(with runtime.LockOSThread()) to drive the traffic b) the number of routines >= number of NIC queues

func NewXDPRelay

func NewXDPRelay(logger *zerolog.Logger, ifname string, ethernetTypes []uint16, options ...XDPRelayOption) (*XDPRelay, error)

NewXDPRelay creates a new instance of XDPRelay, by default, the XDPRelay binds to all queues of the specified interface

func (*XDPRelay) Close added in v1.1.0

func (xr *XDPRelay) Close() error

func (*XDPRelay) InterfaceName added in v1.1.0

func (xr *XDPRelay) InterfaceName() string

func (*XDPRelay) NumSocket

func (xr *XDPRelay) NumSocket() int

NumSocket returns number of XDP socket

func (*XDPRelay) Register

func (xr *XDPRelay) Register(ks []L2EndpointKey, recvMulticast bool) (chan *EthernetResponse, chan []byte, chan struct{}, int)

func (*XDPRelay) Unregister added in v1.1.0

func (xr *XDPRelay) Unregister(registrationID int)

type XDPRelayOption

type XDPRelayOption func(xr *XDPRelay)

XDPRelayOption could be used in NewXDPRelay to customize XDPRelay upon creation

func WithQueueID

func WithQueueID(qidlist []int) XDPRelayOption

WithQueueID specifies a list of interface queue id (start from 0) that the XDPRelay binds to; by default, XDPRelay will use all queues. note: only use this option if you know what you are doing, since this could cause lower performance or XDPRelay unable to receive some of packets.

func WithSendingMode

func WithSendingMode(m xdpSendingMode) XDPRelayOption

WithSendingMode set the XDPRelay's sending mode to m

func WithXDPPerClntRecvChanDepth

func WithXDPPerClntRecvChanDepth(depth uint) XDPRelayOption

WithXDPPerClntRecvChanDepth set the depth in receiving channel for each registered

func WithXDPSendChanDepth

func WithXDPSendChanDepth(depth uint) XDPRelayOption

WithXDPSendChanDepth set the dep th in sending channel

func WithXDPUMEMChunkSize

func WithXDPUMEMChunkSize(fsize uint) XDPRelayOption

WithXDPUMEMChunkSize specifies the XDP UMEM size, which implicitly set the max packet size could be handled by XDPRelay, must be either 4096 or 2048 (kernel XDP limitation)

func WithXDPUMEMNumOfTrunk

func WithXDPUMEMNumOfTrunk(num uint) XDPRelayOption

WithXDPUMEMNumOfTrunk specifies the number of UMEM trunks, must be power of 2. the Fill/Completion/TX/RX ring size is half of specified value;

Jump to

Keyboard shortcuts

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