1*d9f75844SAndroid Build Coastguard Worker# Network Emulation Framework 2*d9f75844SAndroid Build Coastguard Worker 3*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.reviewed = '2021-03-01' %?> 4*d9f75844SAndroid Build Coastguard Worker 5*d9f75844SAndroid Build Coastguard Worker[TOC] 6*d9f75844SAndroid Build Coastguard Worker 7*d9f75844SAndroid Build Coastguard Worker## Disclamer 8*d9f75844SAndroid Build Coastguard Worker 9*d9f75844SAndroid Build Coastguard WorkerThis documentation explain the implementation details of Network Emulation 10*d9f75844SAndroid Build Coastguard WorkerFramework. Framework's public APIs are located in: 11*d9f75844SAndroid Build Coastguard Worker 12*d9f75844SAndroid Build Coastguard Worker* [`/api/test/network_emulation_manager.h`](https://source.chromium.org/search?q=%2Fapi%2Ftest%2Fnetwork_emulation_manager.h) 13*d9f75844SAndroid Build Coastguard Worker* [`/api/test/create_network_emulation_manager.h`](https://source.chromium.org/search?q=%2Fapi%2Ftest%2Fcreate_network_emulation_manager.h) 14*d9f75844SAndroid Build Coastguard Worker* [`/api/test/network_emulation/network_emulation_interfaces.h`](https://source.chromium.org/search?q=%2Fapi%2Ftest%2Fnetwork_emulation%2Fnetwork_emulation_interfaces.h) 15*d9f75844SAndroid Build Coastguard Worker* [`/api/test/simulated_network.h`](https://source.chromium.org/search?q=%2Fapi%2Ftest%2Fsimulated_network.h) 16*d9f75844SAndroid Build Coastguard Worker 17*d9f75844SAndroid Build Coastguard Worker## Overview 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard WorkerNetwork Emulation Framework provides an ability to emulate network behavior 20*d9f75844SAndroid Build Coastguard Workerbetween different clients, including a WebRTC PeerConnection client. To 21*d9f75844SAndroid Build Coastguard Workerconfigure network behavior, the user can choose different options: 22*d9f75844SAndroid Build Coastguard Worker 23*d9f75844SAndroid Build Coastguard Worker* Use predefined implementation that can be configured with parameters such as 24*d9f75844SAndroid Build Coastguard Worker packet loss, bandwidth, delay, etc. 25*d9f75844SAndroid Build Coastguard Worker* Custom implementation 26*d9f75844SAndroid Build Coastguard Worker 27*d9f75844SAndroid Build Coastguard WorkerConceptually the framework provides the ability to define multiple endpoints and 28*d9f75844SAndroid Build Coastguard Workerroutes used to connect them. All network related entities are created and 29*d9f75844SAndroid Build Coastguard Workermanaged by single factory class `webrtc::NetworkEmulationManager` which is 30*d9f75844SAndroid Build Coastguard Workerimplemented by `webrtc::test::NetworkEmulationManagerImpl` and can work in two 31*d9f75844SAndroid Build Coastguard Workermodes: 32*d9f75844SAndroid Build Coastguard Worker 33*d9f75844SAndroid Build Coastguard Worker* Real time 34*d9f75844SAndroid Build Coastguard Worker* Simulated time 35*d9f75844SAndroid Build Coastguard Worker 36*d9f75844SAndroid Build Coastguard WorkerThe manager has a dedicated task queue which pipes all packets through all 37*d9f75844SAndroid Build Coastguard Workernetwork routes from senders to receivers. This task queue behaviour is 38*d9f75844SAndroid Build Coastguard Workerdetermined by `webrtc::TimeController`, which is based on either in real time or 39*d9f75844SAndroid Build Coastguard Workersimulated time mode. 40*d9f75844SAndroid Build Coastguard Worker 41*d9f75844SAndroid Build Coastguard WorkerThe network operates on IP level and supports only UDP for now. 42*d9f75844SAndroid Build Coastguard Worker 43*d9f75844SAndroid Build Coastguard Worker## Abstractions 44*d9f75844SAndroid Build Coastguard Worker 45*d9f75844SAndroid Build Coastguard WorkerThe framework contains the following public abstractions: 46*d9f75844SAndroid Build Coastguard Worker 47*d9f75844SAndroid Build Coastguard Worker* `webrtc::NetworkBehaviorInterface` - defines how emulated network should 48*d9f75844SAndroid Build Coastguard Worker behave. It operates on packets metadata level and is responsible for telling 49*d9f75844SAndroid Build Coastguard Worker which packet at which time have to be delivered to the next receiver. 50*d9f75844SAndroid Build Coastguard Worker 51*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedIpPacket` - represents a single packet that can be sent or 52*d9f75844SAndroid Build Coastguard Worker received via emulated network. It has source and destination address and 53*d9f75844SAndroid Build Coastguard Worker payload to transfer. 54*d9f75844SAndroid Build Coastguard Worker 55*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedNetworkReceiverInterface` - generic packet receiver 56*d9f75844SAndroid Build Coastguard Worker interface. 57*d9f75844SAndroid Build Coastguard Worker 58*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedEndpoint` - primary user facing abstraction of the 59*d9f75844SAndroid Build Coastguard Worker framework. It represents a network interface on client's machine. It has its 60*d9f75844SAndroid Build Coastguard Worker own unique IP address and can be used to send and receive packets. 61*d9f75844SAndroid Build Coastguard Worker 62*d9f75844SAndroid Build Coastguard Worker `EmulatedEndpoint` implements `EmulatedNetworkReceiverInterface` to receive 63*d9f75844SAndroid Build Coastguard Worker packets from the network and provides an API to send packets to the network 64*d9f75844SAndroid Build Coastguard Worker and API to bind other `EmulatedNetworkReceiverInterface` which will be able 65*d9f75844SAndroid Build Coastguard Worker to receive packets from the endpoint. `EmulatedEndpoint` interface has the 66*d9f75844SAndroid Build Coastguard Worker only implementation: `webrtc::test::EmulatedEndpointImpl`. 67*d9f75844SAndroid Build Coastguard Worker 68*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedNetworkNode` - represents single network in the real world, 69*d9f75844SAndroid Build Coastguard Worker like a 3G network between peers, or Wi-Fi for one peer and LTE for another. 70*d9f75844SAndroid Build Coastguard Worker Each `EmulatedNetworkNode` is a single direction connetion and to form 71*d9f75844SAndroid Build Coastguard Worker bidirectional connection between endpoints two nodes should be used. 72*d9f75844SAndroid Build Coastguard Worker Multiple nodes can be joined into chain emulating a network path from one 73*d9f75844SAndroid Build Coastguard Worker peer to another. 74*d9f75844SAndroid Build Coastguard Worker 75*d9f75844SAndroid Build Coastguard Worker In public API this class is forward declared and fully accessible only by 76*d9f75844SAndroid Build Coastguard Worker the framework implementation. 77*d9f75844SAndroid Build Coastguard Worker 78*d9f75844SAndroid Build Coastguard Worker Internally consist of two parts: `LinkEmulation`, which is responsible for 79*d9f75844SAndroid Build Coastguard Worker behavior of current `EmulatedNetworkNode` and `NetworkRouterNode` which is 80*d9f75844SAndroid Build Coastguard Worker responsible for routing packets to the next node or to the endpoint. 81*d9f75844SAndroid Build Coastguard Worker 82*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedRoute` - represents single route from one network interface 83*d9f75844SAndroid Build Coastguard Worker on one device to another network interface on another device. 84*d9f75844SAndroid Build Coastguard Worker 85*d9f75844SAndroid Build Coastguard Worker In public API this class is forward declared and fully accessible only by 86*d9f75844SAndroid Build Coastguard Worker the framework implementation. 87*d9f75844SAndroid Build Coastguard Worker 88*d9f75844SAndroid Build Coastguard Worker It contains start and end endpoint and ordered list of `EmulatedNetworkNode` 89*d9f75844SAndroid Build Coastguard Worker which forms the single directional route between those endpoints. 90*d9f75844SAndroid Build Coastguard Worker 91*d9f75844SAndroid Build Coastguard WorkerThe framework has also the following private abstractions: 92*d9f75844SAndroid Build Coastguard Worker 93*d9f75844SAndroid Build Coastguard Worker* `webrtc::test::NetworkRouterNode` - an `EmulatedNetworkReceiverInterface` 94*d9f75844SAndroid Build Coastguard Worker that can route incoming packets to the next receiver based on internal IP 95*d9f75844SAndroid Build Coastguard Worker routing table. 96*d9f75844SAndroid Build Coastguard Worker 97*d9f75844SAndroid Build Coastguard Worker* `webrtc::test::LinkEmulation` - an `EmulatedNetworkReceiverInterface` that 98*d9f75844SAndroid Build Coastguard Worker can emulate network leg behavior via `webrtc::NetworkBehaviorInterface` 99*d9f75844SAndroid Build Coastguard Worker interface. 100*d9f75844SAndroid Build Coastguard Worker 101*d9f75844SAndroid Build Coastguard WorkerFor integrating with `webrtc::PeerConnection` there are helper abstractions: 102*d9f75844SAndroid Build Coastguard Worker 103*d9f75844SAndroid Build Coastguard Worker* `webrtc::EmulatedNetworkManagerInterface` which is implemented by 104*d9f75844SAndroid Build Coastguard Worker `webrtc::test::EmulatedNetworkManager` and provides `rtc::Thread` and 105*d9f75844SAndroid Build Coastguard Worker `rtc::NetworkManager` for WebRTC to use as network thread for 106*d9f75844SAndroid Build Coastguard Worker `PeerConnection` and for `cricket::BasicPortAllocator`. 107*d9f75844SAndroid Build Coastguard Worker 108*d9f75844SAndroid Build Coastguard Worker Implementation represent framework endpoints as `rtc::Network` to WebRTC. 109*d9f75844SAndroid Build Coastguard Worker 110*d9f75844SAndroid Build Coastguard Worker## Architecture 111*d9f75844SAndroid Build Coastguard Worker 112*d9f75844SAndroid Build Coastguard WorkerLet's take a look on how framework's abstractions are connected to each other. 113*d9f75844SAndroid Build Coastguard Worker 114*d9f75844SAndroid Build Coastguard WorkerWhen the user wants to setup emulated network, first of all, they should create 115*d9f75844SAndroid Build Coastguard Workeran instance of `NetworkEmulationManager` using 116*d9f75844SAndroid Build Coastguard Worker`webrtc::CreateNetworkEmulationManager(...)` API. Then user should use a manager 117*d9f75844SAndroid Build Coastguard Workerto create at least one `EmulatedEndpoint` for each client. After endpoints, the 118*d9f75844SAndroid Build Coastguard Workeruser should create required `EmulatedNetworkNode`s and with help of manager 119*d9f75844SAndroid Build Coastguard Workerchain them into `EmulatedRoute`s conecting desired endpoints. 120*d9f75844SAndroid Build Coastguard Worker 121*d9f75844SAndroid Build Coastguard WorkerHere is a visual overview of the emulated network architecture: 122*d9f75844SAndroid Build Coastguard Worker 123*d9f75844SAndroid Build Coastguard Worker 124*d9f75844SAndroid Build Coastguard Worker 125*d9f75844SAndroid Build Coastguard WorkerWhen network is hooked into `PeerConnection` it is done through network thread 126*d9f75844SAndroid Build Coastguard Workerand `NetworkManager`. In the network thread the custom `rtc::SocketServer` is 127*d9f75844SAndroid Build Coastguard Workerprovided: `webrtc::test::FakeNetworkSocketServer`. This custom socket server 128*d9f75844SAndroid Build Coastguard Workerwill construct custom sockets (`webrtc::test::FakeNetworkSocket`), which 129*d9f75844SAndroid Build Coastguard Workerinternally bind themselves to the required endpoint. All packets processing 130*d9f75844SAndroid Build Coastguard Workerinside socket have to be done on the `PeerConnection`'s network thread. When 131*d9f75844SAndroid Build Coastguard Workerpacket is going from `PeerConnection` to the network it's already comming from 132*d9f75844SAndroid Build Coastguard Workerthe network thread and when it's comming from the emulated network switch from 133*d9f75844SAndroid Build Coastguard Workerthe Network Emulation Framework internal task queue and `PeerConnection`'s 134*d9f75844SAndroid Build Coastguard Workernetwork thread is done inside socket's `OnPacketReceived(...)` method. 135*d9f75844SAndroid Build Coastguard Worker 136*d9f75844SAndroid Build Coastguard Worker 137