forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedulerImplementation.go
380 lines (343 loc) · 9.28 KB
/
schedulerImplementation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package quic
import (
"errors"
"fmt"
"github.com/VividCortex/ewma"
"github.com/boisjacques/golang-utils"
"github.com/boisjacques/qed/internal/wire"
"github.com/tylerwince/godbg"
"hash/crc32"
"math/rand"
"net"
"time"
)
type SchedulerImplementation struct {
paths map[uint32]*Path
session Session
referenceRTT uint16
pathZero *Path
pathIds []uint32
lastPath uint32
addressHelper *AddressHelper
addrChan chan map[uint32]net.Addr
localAddrs map[uint32]net.Addr
remoteAddrs map[uint32]net.Addr
sockets map[uint32]net.PacketConn
isInitialized bool
totalPathWeight int
isActive bool
mode schedulerOperation
}
func NewScheduler(session Session, pconn net.PacketConn, remote net.Addr) *SchedulerImplementation {
pathZero := &Path{
isPathZero: true,
pathID: 0,
weight: 1000,
local: pconn,
remote: remote,
owd: 0,
vewma: ewma.NewMovingAverage(5),
}
paths := make(map[uint32]*Path)
paths[pathZero.pathID] = pathZero
pathIds := make([]uint32, 0)
pathIds = append(pathIds, pathZero.pathID)
scheduler := &SchedulerImplementation{
paths: paths,
session: session,
referenceRTT: 0,
pathZero: pathZero,
pathIds: pathIds,
lastPath: 0,
addressHelper: NewAddressHelper(),
addrChan: make(chan map[uint32]net.Addr, 1000),
localAddrs: make(map[uint32]net.Addr),
remoteAddrs: make(map[uint32]net.Addr),
sockets: make(map[uint32]net.PacketConn),
isInitialized: false,
totalPathWeight: 1000,
isActive: false,
mode: roundRobin,
}
scheduler.sockets[CRC(pconn.LocalAddr())] = pconn
godbg.Dbg("initialising scheduler")
for !scheduler.addressHelper.isInitalised {
time.Sleep(1 * time.Millisecond)
}
scheduler.localAddrs = scheduler.addressHelper.GetAddresses()
go scheduler.announceAddresses()
go scheduler.modeSelectionTimer()
go scheduler.measurePathsRunner()
go scheduler.weighPathsRunner()
godbg.Dbg("Scheduler up and running")
return scheduler
}
func (s *SchedulerImplementation) IsInitialized() bool {
return s.isInitialized
}
func (s *SchedulerImplementation) SetIsInitialized(isInitialized bool) {
s.isInitialized = isInitialized
}
func (s *SchedulerImplementation) IsActive() bool {
return s.isActive
}
func (s *SchedulerImplementation) Activate(isActive bool) {
s.isActive = isActive
}
func (s *SchedulerImplementation) Write(p []byte) error {
var path *Path
var err error
for {
path, err = s.getPath()
if err != nil {
}
if path != nil && path.local != nil {
break
}
s.session.(*session).logger.Errorf("nil path selected")
}
_, err = path.local.WriteTo(p, path.remote)
if err != nil {
fmt.Println(err, util.Tracer())
return err
}
return nil
}
func (s *SchedulerImplementation) Read([]byte) (int, net.Addr, error) {
return 0, nil, errors.New("Not implemented yet")
}
func (s *SchedulerImplementation) Close() error {
// TODO: Mock close
return errors.New("not implemented yet")
}
func (s *SchedulerImplementation) LocalAddr() net.Addr { return nil }
func (s *SchedulerImplementation) RemoteAddr() net.Addr { return s.paths[s.lastPath].remote }
func (s *SchedulerImplementation) SetCurrentRemoteAddr(net.Addr) {}
func (s *SchedulerImplementation) getPath() (*Path, error) {
if s.mode == roundRobin {
return s.getRoundRobinPath()
} else if s.mode == weightBased {
return s.getWeightedPath()
} else {
return nil, errors.New("invalid mode of scheduler operation")
}
}
func (s *SchedulerImplementation) getWeightedPath() (*Path, error) {
rand.Seed(time.Now().UnixNano())
r := rand.Intn(s.totalPathWeight)
for _, path := range s.paths {
r -= path.weight
if r <= 0 {
return path, nil
}
}
return nil, errors.New("no path selected")
}
func (s *SchedulerImplementation) getRoundRobinPath() (*Path, error) {
s.lastPath = (s.lastPath + 1) % uint32(len(s.pathIds))
path := s.paths[s.pathIds[s.lastPath]]
return path, nil
}
func (s *SchedulerImplementation) newPath(local, remote net.Addr) error {
usock, err := s.openSocket(local)
if err != nil {
return err
}
if usock == nil {
err := errors.New("no socket returned")
return err
}
checksum := crc32.ChecksumIEEE(xor([]byte(local.String()), []byte(remote.String())))
p := NewPath(checksum, usock, remote, 1000)
s.paths[p.pathID] = p
s.pathIds = append(s.pathIds, p.pathID)
return nil
}
func (s *SchedulerImplementation) addLocalAddress(local net.Addr) {
for _, remote := range s.remoteAddrs {
if isSameVersion(local, remote) {
err := s.newPath(local, remote)
if err != nil {
s.session.(*session).logger.Errorf("Path could not be created: %s", err)
}
}
}
}
func (s *SchedulerImplementation) addRemoteAddress(remoteAddress net.Addr) {
checksum := CRC(remoteAddress)
if !s.containsBlocking(checksum, remote) {
s.remoteAddrs[checksum] = remoteAddress
for _, localAddress := range s.localAddrs {
if isSameVersion(localAddress, remoteAddress) {
err := s.newPath(localAddress, remoteAddress)
if err != nil {
s.session.(*session).logger.Errorf("Path could not be created: %s", err)
}
}
}
}
}
func (s *SchedulerImplementation) removeAddress(address net.Addr) {
if s.containsBlocking(CRC(address), remote) {
s.delete(address, remote)
}
if s.containsBlocking(CRC(address), local) {
s.delete(address, local)
}
for k, v := range s.paths {
if v.contains(address) {
s.removePath(k)
}
}
}
func (s *SchedulerImplementation) GetPathZero() *Path {
return s.pathZero
}
func (s *SchedulerImplementation) removePath(pathId uint32) {
delete(s.paths, pathId)
}
func (s *SchedulerImplementation) announceAddresses() {
for s.session == nil {
time.Sleep(1 * time.Millisecond)
}
for !s.isActive {
time.Sleep(1 * time.Millisecond)
}
for _, addr := range s.localAddrs {
if addr != s.pathZero.local.LocalAddr() {
s.session.(*session).queueControlFrame(s.assembleAddrModFrame(wire.AddFrame, addr))
s.session.(*session).logger.Debugf("Queued addition frame for address %s", addr.String())
}
}
}
func (s *SchedulerImplementation) assembleAddrModFrame(operation wire.AddressModificationOperation, addr net.Addr) *wire.AddrModFrame {
var version wire.IpVersion
if addr.(*net.UDPAddr).IP.To4() != nil {
version = wire.IPv4
} else {
version = wire.IPv6
}
f := wire.NewAddrModFrame(operation, version, addr)
return f
}
func (s *SchedulerImplementation) assembleOwdFrame(pathId uint32) *wire.OwdFrame {
f := wire.NewOwdFrame(pathId)
return f
}
func (s *SchedulerImplementation) containsBlocking(key uint32, direcion direcionAddr) bool {
var contains bool
if direcion == local {
_, contains = s.localAddrs[key]
} else if direcion == remote {
_, contains = s.remoteAddrs[key]
}
return contains
}
func (s *SchedulerImplementation) delete(addr net.Addr, direction direcionAddr) {
for key, path := range s.paths {
if path.contains(addr) {
s.deletePath(key)
}
}
if direction == local {
delete(s.localAddrs, CRC(addr))
}
if direction == remote {
delete(s.remoteAddrs, CRC(addr))
}
}
func (s *SchedulerImplementation) deletePath(pathId uint32) {
delete(s.paths, pathId)
}
func (s *SchedulerImplementation) setOwd(pathId uint32, owd uint64) error {
s.paths[pathId].setOwd(float64(owd))
return nil
}
func (s *SchedulerImplementation) openSocket(local net.Addr) (net.PacketConn, error) {
var err error = nil
usock, contains := s.sockets[CRC(local)]
if !contains {
usock, err = net.ListenUDP("udp", local.(*net.UDPAddr))
if err != nil {
panic(err)
}
if usock != nil {
s.sockets[CRC(local)] = usock
s.session.(*session).handlerManager.AddConn(usock)
} else {
panic("nilsocket returned")
}
}
return usock, err
}
func (s *SchedulerImplementation) measurePathsRunner() {
go func() {
for {
if s.isActive {
s.measurePaths()
}
time.Sleep(500 * time.Millisecond)
}
}()
}
func (s *SchedulerImplementation) measurePaths() {
for _, path := range s.paths {
s.measurePath(path)
}
}
//TODO: Time has to move further down the path
func (s *SchedulerImplementation) measurePath(path *Path) {
s.session.(*session).queueControlFrame(s.assembleOwdFrame(path.pathID))
}
func (s *SchedulerImplementation) sumUpWeights() {
s.totalPathWeight = 0
for _, path := range s.paths {
s.totalPathWeight += path.weight
time.Sleep(10 * time.Millisecond)
}
}
func (s *SchedulerImplementation) weighPathsRunner() {
go func() {
for {
if s.isActive && s.mode == weightBased {
s.weighPaths()
}
time.Sleep(500 * time.Millisecond)
}
}()
}
func (s *SchedulerImplementation) weighPaths() {
for _, path := range s.paths {
s.weighPath(path)
}
s.sumUpWeights()
}
func (s *SchedulerImplementation) weighPath(path *Path) {
if path.owd < s.calculateAverageOwd() {
if path.weight < 1000 {
path.weight = path.weight + 1
}
} else {
if path.weight > 1 {
path.weight = path.weight - 1
}
}
}
func (s *SchedulerImplementation) calculateAverageOwd() float64 {
var aOwd float64
for _, path := range s.paths {
aOwd += path.MovingAverage()
}
aOwd = aOwd / float64(len(s.paths))
return aOwd
}
func (s *SchedulerImplementation) modeSelectionTimer() {
for s.session == nil {
time.Sleep(1 * time.Millisecond)
}
for !s.isActive {
time.Sleep(1 * time.Millisecond)
}
time.Sleep(500 * time.Millisecond)
s.mode = weightBased
}