Architecture Overview
The network stack is built around several core components:Network Task
TheNetworkTask (Kernel/Net/NetworkTask.cpp) is a dedicated kernel process that:
- Processes incoming packets from all network adapters
- Dispatches packets to protocol handlers
- Manages delayed TCP ACKs
- Handles TCP retransmission
- Runs the main packet processing loop
Kernel/Net/NetworkTask.h
Packet processing flow:
- Network adapter receives packet → triggers interrupt
on_receivecallback increments pending packets counter- NetworkTask wakes up and reads packet from adapter
- Ethernet frame header parsed to determine EtherType
- Dispatch to appropriate handler (ARP, IPv4, IPv6)
Kernel/Net/NetworkTask.cpp:95
Socket Abstraction
TheSocket class (Kernel/Net/Socket.h) provides the base for all network sockets:
Socket Base Class
Socket Base Class
Core properties:Reference:
- Domain (AF_INET, AF_INET6, AF_LOCAL)
- Type (SOCK_STREAM, SOCK_DGRAM, SOCK_RAW)
- Protocol (IPPROTO_TCP, IPPROTO_UDP, etc.)
Kernel/Net/Socket.h:33bind()- Bind to local addressconnect()- Establish connectionlisten()/accept()- Server operationssendto()/recvfrom()- Data transfersetsockopt()/getsockopt()- Socket optionsshutdown()- Close one or both directions
Kernel/Net/Socket.h:74
Protocol Implementations
TCP (Transmission Control Protocol)
TCPSocket (Kernel/Net/TCPSocket.h) implements reliable, connection-oriented communication:
TCP State Machine
TCP State Machine
Kernel/Net/TCPSocket.h:54- Three-way handshake (SYN, SYN-ACK, ACK)
- Sequence number tracking
- Window-based flow control
- Delayed ACK optimization
- Retransmission on timeout
- Congestion control
- Graceful connection termination (FIN)
Kernel/Net/NetworkTask.cpp:39, Kernel/Net/TCP.h
UDP (User Datagram Protocol)
UDPSocket (Kernel/Net/UDPSocket.h) provides connectionless, unreliable datagram service:
- No connection establishment required
- Best-effort delivery
- Checksum validation
- Multicast support
- Broadcast support (when enabled)
Kernel/Net/UDPSocket.h, Kernel/Net/UDP.h
IPv4 (Internet Protocol version 4)
IPv4 packet handling includes:- Header validation and checksum verification
- Fragmentation and reassembly
- Time-to-live (TTL) processing
- Routing decision making
- Protocol demultiplexing (TCP, UDP, ICMP)
Kernel/Net/IP/IPv4.h
IPv6 (Internet Protocol version 6)
IPv6 support includes:- 128-bit addressing
- Simplified header format
- Extension headers
- ICMPv6 integration
- Neighbor Discovery Protocol (NDP)
Kernel/Net/IP/IPv6.h, Kernel/Net/ICMPv6.h
ICMP (Internet Control Message Protocol)
ICMPv4:- Echo request/reply (ping)
- Destination unreachable
- Time exceeded
- Redirect messages
Kernel/Net/ICMP.h
ICMPv6:
- Echo request/reply
- Neighbor Solicitation/Advertisement
- Router Solicitation/Advertisement
- Packet Too Big messages
Kernel/Net/ICMPv6.h
Local Sockets (Unix Domain)
LocalSocket (Kernel/Net/LocalSocket.h) implements AF_LOCAL/AF_UNIX sockets:
- Stream sockets (SOCK_STREAM)
- Datagram sockets (SOCK_DGRAM)
- File system path binding
- SCM_RIGHTS (file descriptor passing)
- Credential passing
Kernel/Net/LocalSocket.h
Network Adapters
TheNetworkAdapter (Kernel/Net/NetworkAdapter.h) class abstracts network interfaces:
Supported Drivers
Loopback Adapter- Virtual adapter for local communication
- Automatically configured with 127.0.0.1/8
- No hardware dependencies
Kernel/Net/LoopbackAdapter.h, Kernel/Net/NetworkTask.cpp:70
Intel Network Cards (Kernel/Net/Intel/)
- E1000 family support
- PCI-based adapters
- Hardware offload capabilities
Kernel/Net/Realtek/)
- RTL8139 and compatible
- Common in virtual environments
Kernel/Net/VirtIO/)
- Virtio-net para-virtualized driver
- Optimized for virtual machines
- High performance with minimal overhead
Adapter Configuration
NetworkAdapter provides:- MAC address management
- IPv4 address/netmask configuration
- IPv6 address configuration
- MTU (Maximum Transmission Unit) settings
- Packet receive callback registration
- Link state monitoring
Kernel/Net/NetworkAdapter.h
Routing and ARP
Routing Table
Route structure (Kernel/Net/Routing.h) defines routing entries:
route_to()- Find route for destinationupdate_routing_table()- Add/remove routes- Gateway routing support
- Broadcast routing
Kernel/Net/Routing.h:18, Kernel/Net/Routing.cpp
ARP (Address Resolution Protocol)
ARP maps IPv4 addresses to MAC addresses:- ARP request/reply handling
- ARP cache (table) management
- Automatic cache updates
- ARP cache aging and expiration
Kernel/Net/IP/ARP.h, Kernel/Net/Routing.h:60
Network Management
NetworkingManagement (Kernel/Net/NetworkingManagement.h) provides system-wide coordination:
- Network adapter registration and enumeration
- Adapter lifecycle management
- Global network subsystem initialization
- Adapter iteration for packet processing
Kernel/Net/NetworkingManagement.h, Kernel/Net/NetworkTask.cpp:67
Socket Options
Supported socket options viasetsockopt() / getsockopt():
SOL_SOCKET Level
SO_RCVTIMEO/SO_SNDTIMEO- Receive/send timeoutsSO_ERROR- Get and clear error statusSO_REUSEADDR- Allow address reuseSO_KEEPALIVE- TCP keepaliveSO_TIMESTAMP- Packet timestampsSO_BINDTODEVICE- Bind to specific interface
IPPROTO_IP Level
IP_TTL- Time-to-liveIP_MULTICAST_LOOP- Multicast loopbackIP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP- Multicast groups
IPPROTO_TCP Level
TCP_NODELAY- Disable Nagle algorithm
Kernel/Net/Socket.h:84
Packet Buffer Management
The NetworkTask allocates a 64 KiB kernel buffer for packet processing:Kernel/Net/NetworkTask.cpp:81
Performance Optimizations
Delayed TCP ACK
The stack implements delayed ACK to reduce overhead:- ACKs batched when possible
- Separate hash table tracks sockets needing ACKs
- Periodic flush (every 500ms) ensures timely acknowledgment
Kernel/Net/NetworkTask.cpp:46, Kernel/Net/NetworkTask.cpp:96
TCP Retransmission
Automatic retransmission of lost packets:- Timer-based retransmit queue
- Exponential backoff
- Configurable retransmit limits
Kernel/Net/NetworkTask.cpp:43
Implementation Notes
All network operations use the
SOCKET_TRY() macro which automatically updates the SO_ERROR field on failure, providing proper error reporting to userspace.- Low latency for packet handling
- Efficient interrupt-driven processing
- Centralized protocol implementation
- Simplified synchronization
Kernel/Net/Socket.h:187