-
Notifications
You must be signed in to change notification settings - Fork 0
/
prudp.go
446 lines (398 loc) · 11.6 KB
/
prudp.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package prudp
import (
"sync/atomic"
"time"
)
const (
PRUDP_RTO_NDL = 30 // no delay min rto
PRUDP_RTO_MIN = 100 // normal min rto
PRUDP_RTO_DEF = 200
PRUDP_RTO_MAX = 60000
PRUDP_CMD_PUSH = 81 // cmd: push data
PRUDP_CMD_ACK = 82 // cmd: ack
PRUDP_CMD_WASK = 83 // cmd: window probe (ask)
PRUDP_CMD_WINS = 84 // cmd: window size (tell)
PRUDP_ASK_SEND = 1 // need to send PRUDP_CMD_WASK
PRUDP_ASK_TELL = 2 // need to send PRUDP_CMD_WINS
PRUDP_WND_SND = 32
PRUDP_WND_RCV = 32
PRUDP_MTU_DEF = 1400
PRUDP_ACK_FAST = 3
PRUDP_INTERVAL = 100
PRUDP_OVERHEAD = 24
PRUDP_DEADLINK = 20
PRUDP_THRESH_INIT = 2
PRUDP_THRESH_MIN = 2
PRUDP_PROBE_INIT = 7000 // 7 secs to probe window size
PRUDP_PROBE_LIMIT = 120000 // up to 120 secs to probe window
PRUDP_SN_OFFSET = 12
)
// RUDP segment
type segment struct {
session uint32 // number
cmd uint8 // command
frg uint8 // fragment MSS segment
wnd uint16 // window size
ts uint32 // timestamp
sn uint32 // sequence number
una uint32 // unacknowledged
rto uint32 // retransmission timeout
xmit uint32 // transmit
resendts uint32 // resend timestamp
fastack uint32 // fast acknowledge
acked uint32 // mark if the seg has acked
data []byte // data textcont
}
// output_callback is a prototype which ought capture conn and call conn.Write
type output_callback func(buf []byte, size int)
// to be send ACK, includ ts and sn.
// ts -> Timestamp
// sn -> Sequence Number
type ackItem struct {
sn uint32
ts uint32
}
// single RPUDP connection
type PRUDP struct {
MTU, mss, state uint32
session uint32
send_una, send_next, recv_next uint32
ssthresh uint32
net_RTTvar, net_RTTs int32
net_rto, net_Minrto uint32
send_wind, revc_wind, remote_wind, c_wind, probe uint32
interval, ts_flush uint32
nodelay, updated uint32
ts_probe, probe_wait uint32
dead_link, incr uint32
fastReSend int32
flowCONwind, stream int32
// queue for index point
// buf save or delete data to network, can speak write and read.
send_queue []segment
recv_queue []segment
send_buf []segment
recv_buf []segment
// to be send ACK, includ ts and sn.
// ts -> Timestamp
// sn -> Sequence Number
acklist []ackItem
// interim buffer, but Doesn't seem to make much sense.
buffer []byte
reserved int
output output_callback
}
// Segment Struct Func TO
// encode a segment into buffer
func (seg *segment) encode(ptr []byte) []byte {
ptr = prudp_encode32u(ptr, seg.session)
ptr = prudp_encode8u(ptr, seg.cmd)
ptr = prudp_encode8u(ptr, seg.frg)
ptr = prudp_encode16u(ptr, seg.wnd)
ptr = prudp_encode32u(ptr, seg.ts)
ptr = prudp_encode32u(ptr, seg.sn)
ptr = prudp_encode32u(ptr, seg.una)
ptr = prudp_encode32u(ptr, uint32(len(seg.data)))
atomic.AddUint64(&DefaultSnmp.OutSegs, 1)
return ptr
}
// unpack a segment into buffer
func (seg *segment) decode(data []byte, seesion uint32, cmd uint8, frg uint8, wnd uint16, ts uint32, sn uint32, length uint32, una uint32, conv uint32) []byte {
data = prudp_decode32u(data, &seesion)
data = prudp_decode8u(data, &cmd)
data = prudp_decode8u(data, &frg)
data = prudp_decode16u(data, &wnd)
data = prudp_decode32u(data, &ts)
data = prudp_decode32u(data, &sn)
data = prudp_decode32u(data, &una)
data = prudp_decode32u(data, &length)
atomic.AddUint64(&DefaultSnmp.OutSegs, 1)
return data
}
func _imin_(a, b uint32) uint32 {
if a <= b {
return a
}
return b
}
func _imax_(a, b uint32) uint32 {
if a >= b {
return a
}
return b
}
func _ibound_(lower, middle, upper uint32) uint32 {
return _imin_(_imax_(lower, middle), upper)
}
func _itimediff(later, earlier uint32) int32 {
return (int32)(later - earlier)
}
// monotonic reference time point
var refTime time.Time = time.Now()
// currentMs returns current elapsed monotonic milliseconds since program startup
func currentMs() uint32 { return uint32(time.Since(refTime) / time.Millisecond) }
// CreatePRUDP create a new PRUDP state machine
//
// 'conv' must be equal in the connection peers, or else data will be silently rejected.
//
// 'output' function will be called whenever these is data to be sent on wire.
func CreatePRUDP(session uint32, output output_callback) *PRUDP {
rpudp := new(PRUDP)
rpudp.session = session
rpudp.send_wind = PRUDP_WND_SND
rpudp.revc_wind = PRUDP_WND_RCV
rpudp.remote_wind = PRUDP_WND_RCV
rpudp.MTU = PRUDP_MTU_DEF
rpudp.mss = rpudp.MTU - PRUDP_OVERHEAD
rpudp.buffer = make([]byte, rpudp.MTU)
rpudp.net_rto = PRUDP_RTO_DEF
rpudp.net_Minrto = PRUDP_RTO_MIN
rpudp.interval = PRUDP_INTERVAL
rpudp.ts_flush = PRUDP_INTERVAL
rpudp.ssthresh = PRUDP_THRESH_INIT
rpudp.dead_link = PRUDP_DEADLINK
rpudp.output = output
return rpudp
}
// newSegment creates a PRUDP segment
func (rpudp *PRUDP) newSegment(size int) (seg segment) {
seg.data = transmitBuff.Get().([]byte)[:size]
return
}
// if n >= mss
func (prudp *PRUDP) IsReserveMss(n int) bool {
if n >= int(prudp.MTU-PRUDP_OVERHEAD) || n < 0 {
return false
}
prudp.reserved = n
prudp.mss = prudp.MTU - PRUDP_OVERHEAD - uint32(n)
return true
}
// flush pending data
// TODO Optimization algorithm
func (prudp *PRUDP) flush(ackOnly bool) uint32 {
var seg segment
seg.session = prudp.session
seg.cmd = PRUDP_CMD_ACK
seg.wnd = prudp.wind_unused()
seg.una = prudp.recv_next
buffer := prudp.buffer
// keep n bytes untouched
ptr := buffer[prudp.reserved:]
// makeSpace makes room for writing
makeSpace := func(space int) {
size := len(buffer) - len(ptr)
if size+space > int(prudp.MTU) {
prudp.output(buffer, size)
ptr = buffer[prudp.reserved:]
}
}
// flush bytes in buffer if there is any
flushBuffer := func() {
size := len(buffer) - len(ptr)
if size > prudp.reserved {
prudp.output(buffer, size)
}
}
// flush acknowledges
for i, ack := range prudp.acklist {
makeSpace(PRUDP_OVERHEAD)
// filter jitters caused by bufferbloat
if _itimediff(ack.sn, prudp.recv_next) >= 0 || len(prudp.acklist)-1 == i {
seg.sn, seg.ts = ack.sn, ack.ts
ptr = seg.encode(ptr)
}
}
prudp.acklist = prudp.acklist[0:0]
if ackOnly { // flash remain ack segments
flushBuffer()
return prudp.interval
}
// probe window size (if remote window size equals zero)
if prudp.remote_wind == 0 {
current := currentMs()
if prudp.probe_wait == 0 {
prudp.probe_wait = PRUDP_PROBE_INIT
prudp.ts_probe = current + prudp.probe_wait
} else {
if _itimediff(current, prudp.ts_probe) >= 0 {
if prudp.probe_wait < PRUDP_PROBE_INIT {
prudp.probe_wait = PRUDP_PROBE_INIT
}
prudp.probe_wait += prudp.probe_wait / 2
if prudp.probe_wait > PRUDP_PROBE_LIMIT {
prudp.probe_wait = PRUDP_PROBE_LIMIT
}
prudp.ts_probe = current + prudp.probe_wait
prudp.probe |= PRUDP_ASK_SEND
}
}
} else {
prudp.ts_probe = 0
prudp.probe_wait = 0
}
// flush window probing commands
if (prudp.probe & PRUDP_ASK_SEND) != 0 {
seg.cmd = PRUDP_CMD_WASK
makeSpace(PRUDP_OVERHEAD)
ptr = seg.encode(ptr)
}
// flush window probing commands
if (prudp.probe & PRUDP_ASK_TELL) != 0 {
seg.cmd = PRUDP_CMD_WINS
makeSpace(PRUDP_OVERHEAD)
ptr = seg.encode(ptr)
}
prudp.probe = 0
// calculate window size
cwnd := _imin_(prudp.send_wind, prudp.remote_wind)
if prudp.flowCONwind == 0 {
cwnd = _imin_(prudp.c_wind, cwnd)
}
// sliding window, controlled by snd_nxt && sna_una+cwnd
newSegsCount := 0
for k := range prudp.send_queue {
if _itimediff(prudp.send_next, prudp.send_una+cwnd) >= 0 {
break
}
newseg := prudp.send_queue[k]
newseg.session = prudp.session
newseg.cmd = PRUDP_CMD_PUSH
newseg.sn = prudp.send_next
prudp.send_buf = append(prudp.send_buf, newseg)
prudp.send_next++
newSegsCount++
}
if newSegsCount > 0 {
prudp.send_queue = prudp.remove_front(prudp.send_queue, newSegsCount)
}
// calculate resent
resent := uint32(prudp.fastReSend)
if prudp.fastReSend <= 0 {
resent = 0xffffffff
}
// check for retransmissions
current := currentMs()
var change, lostSegs, fastRetransSegs, earlyRetransSegs uint64
minrto := int32(prudp.interval)
ref := prudp.send_buf[:len(prudp.send_buf)] // for bounds check elimination
for k := range ref {
segment := &ref[k]
needsend := false
if segment.acked == 1 {
continue
}
if segment.xmit == 0 { // initial transmit
needsend = true
segment.rto = prudp.net_rto
segment.resendts = current + segment.rto
} else if segment.fastack >= resent { // fast retransmit
needsend = true
segment.fastack = 0
segment.rto = prudp.net_rto
segment.resendts = current + segment.rto
change++
fastRetransSegs++
} else if segment.fastack > 0 && newSegsCount == 0 { // early retransmit
needsend = true
segment.fastack = 0
segment.rto = prudp.net_rto
segment.resendts = current + segment.rto
change++
earlyRetransSegs++
} else if _itimediff(current, segment.resendts) >= 0 { // RTO
needsend = true
if prudp.nodelay == 0 {
segment.rto += prudp.net_rto
} else {
segment.rto += prudp.net_rto / 2
}
segment.fastack = 0
segment.resendts = current + segment.rto
lostSegs++
}
if needsend {
current = currentMs()
segment.xmit++
segment.ts = current
segment.wnd = seg.wnd
segment.una = seg.una
need := PRUDP_OVERHEAD + len(segment.data)
makeSpace(need)
ptr = segment.encode(ptr)
copy(ptr, segment.data)
ptr = ptr[len(segment.data):]
if segment.xmit >= prudp.dead_link {
prudp.state = 0xFFFFFFFF
}
}
// get the nearest rto
if rto := _itimediff(segment.resendts, current); rto > 0 && rto < minrto {
minrto = rto
}
}
// flash remain segments
flushBuffer()
// counter updates
sum := lostSegs
if lostSegs > 0 {
atomic.AddUint64(&DefaultSnmp.LostSegs, lostSegs)
}
if fastRetransSegs > 0 {
atomic.AddUint64(&DefaultSnmp.FastRetransSegs, fastRetransSegs)
sum += fastRetransSegs
}
if earlyRetransSegs > 0 {
atomic.AddUint64(&DefaultSnmp.EarlyRetransSegs, earlyRetransSegs)
sum += earlyRetransSegs
}
if sum > 0 {
atomic.AddUint64(&DefaultSnmp.RetransSegs, sum)
}
// cwnd update
if prudp.flowCONwind == 0 {
// update ssthresh
// rate halving, https://tools.ietf.org/html/rfc6937
if change > 0 {
inflight := prudp.send_next - prudp.send_una
prudp.ssthresh = inflight / 2
if prudp.ssthresh < PRUDP_THRESH_MIN {
prudp.ssthresh = PRUDP_THRESH_MIN
}
prudp.c_wind = prudp.ssthresh + resent
prudp.incr = prudp.c_wind * prudp.mss
}
// congestion control, https://tools.ietf.org/html/rfc5681
if lostSegs > 0 {
prudp.ssthresh = cwnd / 2
if prudp.ssthresh < PRUDP_THRESH_MIN {
prudp.ssthresh = PRUDP_THRESH_MIN
}
prudp.c_wind = 1
prudp.incr = prudp.mss
}
if prudp.c_wind < 1 {
prudp.c_wind = 1
prudp.incr = prudp.mss
}
}
return uint32(minrto)
}
// window unused
func (prudp *PRUDP) wind_unused() uint16 {
if len(prudp.recv_queue) < int(prudp.revc_wind) {
return uint16(int(prudp.revc_wind) - len(prudp.recv_queue))
}
return 0
}
// remove front n elements from queue
// if the number of elements to remove is more than half of the size.
// just shift the rear elements to front, otherwise just reslice q to q[n:]
// then the cost of runtime.growslice can always be less than n/2
func (prudp *PRUDP) remove_front(q []segment, n int) []segment {
if n > cap(q)/2 {
newn := copy(q, q[n:])
return q[:newn]
}
return q[n:]
}