Debian Codegraph @

vnet/README.md

raw symbols SCIP
1 # vnet
2 A virtual network layer for pion.
3
4 ## Overview
5
6 ### Goals
7 * To make NAT traversal tests easy.
8 * To emulate packet impairment at application level for testing.
9 * To monitor packets at specified arbitrary interfaces.
10
11 ### Features
12 * Configurable virtual LAN and WAN
13 * Virtually hosted ICE servers
14
15 ### Virtual network components
16
17 #### Top View
18 ```
19 ......................................
20 : Virtual Network (vnet) :
21 : :
22 +-------+ * 1 +----+ +--------+ :
23 | :App |------------>|:Net|--o<-----|:Router | :
24 +-------+ +----+ | | :
25 +-----------+ * 1 +----+ | | :
26 |:STUNServer|-------->|:Net|--o<-----| | :
27 +-----------+ +----+ | | :
28 +-----------+ * 1 +----+ | | :
29 |:TURNServer|-------->|:Net|--o<-----| | :
30 +-----------+ +----+ [1] | | :
31 : 1 | | 1 <<has>> :
32 : +---<>| |<>----+ [2] :
33 : | +--------+ | :
34 To form | *| v 0..1 :
35 a subnet tree | o [3] +-----+ :
36 : | ^ |:NAT | :
37 : | | +-----+ :
38 : +-------+ :
39 ......................................
40 Note:
41 o: NIC (Network Interface Controller)
42 [1]: Net implements NIC interface.
43 [2]: Root router has no NAT. All child routers have a NAT always.
44 [3]: Router implements NIC interface for accesses from the
45 parent router.
46 ```
47
48 #### Net
49 Net provides 3 interfaces:
50 * Configuration API (direct)
51 * Network API via Net (equivalent to net.Xxx())
52 * Router access via NIC interface
53 ```
54 (Pion module/app, ICE servers, etc.)
55 +-----------+
56 | :App |
57 +-----------+
58 * |
59 | <<uses>>
60 1 v
61 +---------+ 1 * +-----------+ 1 * +-----------+ 1 * +------+
62 ..| :Router |----+------>o--| :Net |<>------|:Interface |<>------|:Addr |
63 +---------+ | NIC +-----------+ +-----------+ +------+
64 | <<interface>> (transport.Interface) (net.Addr)
65 |
66 | * +-----------+ 1 * +-----------+ 1 * +------+
67 +------>o--| :Router |<>------|:Interface |<>------|:Addr |
68 NIC +-----------+ +-----------+ +------+
69 <<interface>> (transport.Interface) (net.Addr)
70 ```
71
72 > The instance of `Net` will be the one passed around the project.
73 > Net class has public methods for configuration and for application use.
74
75
76 ## Implementation
77
78 ### Design Policy
79 * Each pion package should have config object which has `Net` (of type `transport.Net`) property.
80 - Just like how we distribute `LoggerFactory` throughout the pion project.
81 * DNS => a simple dictionary (global)?
82 * Each Net has routing capability (a goroutine)
83 * Use interface provided net package as much as possible
84 * Routers are connected in a tree structure (no loop is allowed)
85 - To simplify routing
86 - Easy to control / monitor (stats, etc)
87 * Root router has no NAT (== Internet / WAN)
88 * Non-root router has a NAT always
89 * When a Net is instantiated, it will automatically add `lo0` and `eth0` interface, and `lo0` will have one IP address, 127.0.0.1. (this is not used in pion/ice, however)
90 * When a Net is added to a router, the router automatically assign an IP address for `eth0` interface.
91 - For simplicity
92 * User data won't fragment, but optionally drop chunk larger than MTU
93 * IPv6 is not supported
94
95 ### Basic steps for setting up virtual network
96 1. Create a root router (WAN)
97 1. Create child routers and add to its parent (forms a tree, don't create a loop!)
98 1. Add instances of Net to each routers
99 1. Call Stop(), or Stop(), on the top router, which propagates all other routers
100
101 #### Example: WAN with one endpoint (vnet)
102 ```go
103 import (
104 "net"
105
106 "github.com/pion/transport"
107 "github.com/pion/transport/vnet"
108 "github.com/pion/logging"
109 )
110
111 // Create WAN (a root router).
112 wan, err := vnet.NewRouter(&RouterConfig{
113 CIDR: "0.0.0.0/0",
114 LoggerFactory: logging.NewDefaultLoggerFactory(),
115 })
116
117 // Create a network.
118 // You can specify a static IP for the instance of Net to use. If not specified,
119 // router will assign an IP address that is contained in the router's CIDR.
120 nw := vnet.NewNet(&vnet.NetConfig{
121 StaticIP: "27.1.2.3",
122 })
123
124 // Add the network to the router.
125 // The router will assign an IP address to `nw`.
126 if err = wan.AddNet(nw); err != nil {
127 // handle error
128 }
129
130 // Start router.
131 // This will start internal goroutine to route packets.
132 // If you set child routers (using AddRouter), the call on the root router
133 // will start the rest of routers for you.
134 if err = wan.Start(); err != nil {
135 // handle error
136 }
137
138 //
139 // Your application runs here using `nw`.
140 //
141
142 // Stop the router.
143 // This will stop all internal Go routines in the router tree.
144 // (No need to call Stop() on child routers)
145 if err = wan.Stop(); err != nil {
146 // handle error
147 }
148 ```
149
150 #### Example of how to pass around the instance of vnet.Net
151 The instance of vnet.Net wraps a subset of net package to enable operations
152 on the virtual network. Your project must be able to pass the instance to
153 all your routines that do network operation with net package. A typical way
154 is to use a config param to create your instances with the virtual network
155 instance (`nw` in the above example) like this:
156
157 ```go
158 type AgentConfig struct {
159 :
160 Net: transport.Net,
161 }
162
163 type Agent struct {
164 :
165 net: transport.Net,
166 }
167
168 func NetAgent(config *AgentConfig) *Agent {
169 if config.Net == nil {
170 config.Net = vnet.NewNet()
171 }
172
173 return &Agent {
174 :
175 net: config.Net,
176 }
177 }
178 ```
179
180 ```go
181 // a.net is the instance of vnet.Net class
182 func (a *Agent) listenUDP(...) error {
183 conn, err := a.net.ListenPacket(udpString, ...)
184 if err != nil {
185 return nil, err
186 }
187 :
188 }
189 ```
190
191 ### Compatibility and Support Status
192
193 |`net`<br>(built-in) |`vnet` |Note |
194 |:--- |:--- |:--- |
195 | net.Interfaces() | a.net.Interfaces() | |
196 | net.InterfaceByName() | a.net.InterfaceByName() | |
197 | net.ResolveUDPAddr() | a.net.ResolveUDPAddr() | |
198 | net.ListenPacket() | a.net.ListenPacket() | |
199 | net.ListenUDP() | a.net.ListenUDP() | ListenPacket() is recommended |
200 | net.Listen() | a.net.Listen() | TODO) |
201 | net.ListenTCP() | (not supported) | Listen() would be recommended |
202 | net.Dial() | a.net.Dial() | |
203 | net.DialUDP() | a.net.DialUDP() | |
204 | net.DialTCP() | (not supported) | |
205 | net.Interface | transport.Interface | |
206 | net.PacketConn | (use it as-is) | |
207 | net.UDPConn | transport.UDPConn | |
208 | net.TCPConn | transport.TCPConn | TODO: Use net.Conn in your code |
209 | net.Dialer | transport.Dialer | Use a.net.CreateDialer() to create it.<br>The use of vnet.Dialer is currently experimental. |
210
211 > `a.net` is an instance of Net class, and types are defined under the package name `vnet`
212
213 > Most of other `interface` types in net package can be used as is.
214
215 > Please post a github issue when other types/methods need to be added to vnet/vnet.Net.
216
217 ## TODO / Next Step
218 * Implement TCP (TCPConn, Listen)
219 * Support of IPv6
220 * Write a bunch of examples for building virtual networks.
221 * Add network impairment features (on Router)
222 - Introduce latency / jitter
223 - Packet filtering handler (allow selectively drop packets, etc.)
224 * Add statistics data retrieval
225 - Total number of packets forward by each router
226 - Total number of packet loss
227 - Total number of connection failure (TCP)
228
229 ## References
230 * [Comparing Simulated Packet Loss and RealWorld Network Congestion](https://www.riverbed.com/document/fpo/WhitePaper-Riverbed-SimulatedPacketLoss.pdf)
231 * [wireguard-go using GVisor's netstack](https://github.com/WireGuard/wireguard-go/tree/master/tun/netstack)