Skip to content

Network & Systems I/O Expert

Xây dựng hệ thống mạng từ tầng OS — Không library, chỉ có syscalls

Triết lý Module

"Don't use libraries; build them."

Module này deep-dive vào cách hệ thống mạng hoạt động ở mức kernelhardware. Đây không phải hướng dẫn sử dụng Tokio hay Hyper — mà là hiểu cách chúng được xây dựng từ đầu.

⚠️ TARGET AUDIENCE

Module này dành cho Systems ArchitectsHFT (High-Frequency Trading) Engineers — những người cần hiểu từng microsecond latency và mỗi byte trong packet.

Kiến trúc Module

Cấu trúc Nội dung

📦 Raw Sockets & Packets

Tạo SOCK_RAW với libc, xây dựng Ethernet/IP/TCP headers bằng bitwise operations. Blueprint cho high-performance packet sniffer với zero-copy networking.

📡 Custom Protocols

Thiết kế binary protocol trên TCP. Framing strategies, endianness handling. Deep comparison: Protocol Buffers vs Cap'n Proto vs FlatBuffers với zero-copy deserialization.

⚙️ OS Event Queues

Phân tích epoll (Linux), kqueue (BSD/macOS), IOCP (Windows). Rust FFI với OS-level APIs. Mathematical breakdown của C10K Problem.

🔄 Async Runtime Internals

Xây dựng minimal async runtime từ scratch. Hiểu Waker, Executor, Reactor — những building blocks của Tokio.

Tại sao cần hiểu Low-Level Networking?

Use CaseVấn đềGiải pháp Low-Level
HFT SystemsMỗi μs đều quan trọngBypass kernel với AF_XDP, custom protocol
Network SecurityPhân tích traffic rawRaw sockets, packet inspection
Custom Load BalancersHTTP overheadBinary protocol, zero-copy forwarding
Game ServersLow latency multiplayerUDP với custom reliability layer
IoT/EmbeddedResource constraintsMinimal TCP stack, no allocations

Prerequisites

Trước khi vào module này, đảm bảo đã hiểu:

  1. Rust Core: Memory Layout — Stack vs Heap
  2. Unsafe Rust: Unsafe — Raw pointers, FFI basics
  3. Async Basics: Async Runtime — Future trait, Pin

Learning Path

┌─────────────────────────────────────────────────────────────────────┐
│                                                                     │
│   1. Raw Sockets      2. Custom Protocol     3. OS Event Queue     │
│   ─────────────       ───────────────────    ─────────────────     │
│   • SOCK_RAW          • Binary framing       • epoll/kqueue        │
│   • Packet headers    • Serialization        • Rust FFI            │
│   • Zero-copy         • Protobuf comparison  • C10K math           │
│                                                                     │
│                              ↓                                      │
│                                                                     │
│                    4. Async Runtime Internals                       │
│                    ──────────────────────────                       │
│                    • Waker mechanism                                │
│                    • Executor loop                                  │
│                    • Build your own runtime                         │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Công cụ & Crates

CrateMục đíchTrade-off
libcRaw syscall bindingsManual, unsafe, full control
socket2Socket abstractionSafer, still low-level
mioCross-platform event loopAbstraction over epoll/kqueue/IOCP
bytesZero-copy byte manipulationEfficient buffer management

💡 PHILOSOPHY

Trong module này, chúng ta ưu tiên sử dụng libc trực tiếp để hiểu OS-level operations. Các crates như mio sẽ được giải thích như abstraction layers.


"Any sufficiently advanced networking library is indistinguishable from an operating system."
— Hiểu OS để viết library tốt hơn.