Documentation
¶
Overview ¶
Package netem provides network emulation wrappers for the net.PacketConn and net.Conn interfaces; it allows for dynamic thread-safe configuration of connection bandwidth, latency, jitter, and packet loss via indirection.
Index ¶
Constants ¶
const ( // IPv4HeaderSize is the min size of an IPv4 header in bytes. IPv4HeaderSize = 20 // IPv6HeaderSize is the fixed size of an IPv6 header in bytes. IPv6HeaderSize = 40 )
const ( // EthernetDefaultMTU is the standard MTU for most WANs and Internet traffic (1500 bytes). EthernetDefaultMTU = 1_500 // EthernetJumboFrameMTU is used in data center environments to reduce CPU overhead (9000 bytes). EthernetJumboFrameMTU = 9_000 // IPMaximumMTU represents the maximum possible size of an IP packet. // This is commonly the default MTU for Linux loopback devices. IPMaximumMTU = 65_536 )
Variables ¶
This section is empty.
Functions ¶
func NewConn ¶
func NewConn(c net.Conn, p StreamProfile) net.Conn
NewConn wraps an existing net.Conn to emulate network conditions for stream-oriented protocols like TCP. It ensures that data order is strictly preserved even when jitter or latency is applied.
func NewPacketConn ¶
func NewPacketConn(c net.PacketConn, p PacketProfile) net.PacketConn
NewPacketConn wraps an existing net.PacketConn to emulate network conditions for packet-oriented protocols like UDP.
Types ¶
type Bandwidth ¶
type Bandwidth interface {
// Limit returns the allowed throughput in bits per second.
Limit() uint64
}
Bandwidth models the capacity of the link.
type Conn ¶
Conn wraps an existing net.Conn to emulate network conditions for stream-oriented protocols.
To prevent stream corruption, Conn uses an internal FIFO queue (writeCh) to ensure that data is written to the underlying socket in the exact order it was received from the application, even in the presence of latency and jitter.
func (*Conn) SetDeadline ¶
SetDeadline implements net.Conn.
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements net.Conn.
type Fault ¶
type Fault interface {
// ShouldClose returns true if the connection should be severed abruptly.
ShouldClose() bool
}
Fault models the stability of a connection.
type Jitter ¶
type Jitter interface {
// Duration returns the random variance to add to the latency.
Duration() time.Duration
}
Jitter models the variance in transmission delay.
type Latency ¶
type Latency interface {
// Duration returns the delay for the current operation.
Duration() time.Duration
}
Latency models the delay of a network transmission.
type LinkProfile ¶
LinkProfile defines the shared physical properties of a network link.
type Loss ¶
type Loss interface {
// Drop returns true if the current datagram should be discarded.
Drop() bool
}
Loss models the unreliability of a datagram link.
type PacketConn ¶
type PacketConn struct {
net.PacketConn
// contains filtered or unexported fields
}
PacketConn wraps an existing net.PacketConn to emulate network conditions for packet-oriented protocols.
Unlike Conn, PacketConn allows for natural packet reordering if jitter configurations cause a later packet to be scheduled for delivery earlier than a previous one.
func (*PacketConn) SetDeadline ¶
func (c *PacketConn) SetDeadline(t time.Time) error
SetDeadline implements net.PacketConn.
func (*PacketConn) SetWriteDeadline ¶
func (c *PacketConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements net.PacketConn.
type PacketProfile ¶
type PacketProfile struct {
// MTU (Maximum Transmission Unit) is the largest packet size allowed.
// This value includes L3/L4 headers.
//
// Defaults to [EthernetDefaultMTU] if 0.
MTU uint
Latency Latency
Jitter Jitter
Bandwidth Bandwidth
Loss Loss
}
PacketProfile extends the link with datagram-specific behaviors.
type StreamProfile ¶
type StreamProfile struct {
// MTU (Maximum Transmission Unit) is the largest packet size allowed.
// This value includes L3/L4 headers.
//
// Defaults to [EthernetDefaultMTU] if 0.
MTU uint
Latency Latency
Jitter Jitter
Bandwidth Bandwidth
Fault Fault
}
StreamProfile extends the link with stream-specific behaviors.