Beyond Callbacks: A Developer-friendly Programming Paradigm for Network Simulation
Network simulation plays a crucial role in both networking research and industry. Existing commonly-used Discrete Event Simulations (DES) are based on the callback mechanism for discrete events. However, as network functions grow in complexity, callbacks reveal a fundamental structural limitation: a callback can only represent a single point in a blocking operation and cannot capture the full operation as a coherent whole. This forces developers to manually decompose sequential network logic into scattered callback fragments, leading to stack ripping, callback hell, and heavy development and debugging burdens. We propose CoDES (\textbf{Co}routine-based \textbf{DES}), a coroutine-based programming paradigm for network simulation that addresses this structural limitation. CoDES targets development productivity rather than execution performance. Realizing CoDES in existing DES simulators is non-trivial, as coroutine suspension must be coordinated with the event scheduler, introducing challenges in coroutine resource conflicts, operation-level computation requirements, compatibility with existing callback-based simulators, and overhead. We implement a CoDES-based framework in ns-3, address all four challenges, and release it as open source. We evaluate CoDES by implementing Message Passing Interface (MPI), High Precision Congestion Control (HPCC), and Routing Information Protocol (RIP) in ns-3, spanning the transport, congestion control, and routing layers. Compared with callback-based counterparts, CoDES reduces lines of code by up to 51.4% and nesting depth by up to 50%, with no accuracy loss and negligible runtime and memory overhead.