-
Notifications
You must be signed in to change notification settings - Fork 9
/
conn.go
316 lines (273 loc) · 7.25 KB
/
conn.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
package multipath
import (
"net"
"sort"
"sync"
"sync/atomic"
"time"
)
type mpConn struct {
cid connectionID
remoteAddr net.Addr
lastFN uint64
subflows []*subflow
muSubflows sync.RWMutex
recvQueue *receiveQueue
closed uint32 // 1 == true, 0 == false
writerMaybeReady chan bool
tryRetransmit chan bool
pendingAckMap map[uint64]*pendingAck
pendingAckMu *sync.RWMutex
}
func newMPConn(cid connectionID, remoteAddr net.Addr) *mpConn {
mpc := &mpConn{
cid: cid,
remoteAddr: remoteAddr,
lastFN: minFrameNumber - 1,
recvQueue: newReceiveQueue(recieveQueueLength),
writerMaybeReady: make(chan bool, 1),
tryRetransmit: make(chan bool, 1),
pendingAckMap: make(map[uint64]*pendingAck),
pendingAckMu: &sync.RWMutex{},
}
go mpc.retransmitLoop()
return mpc
}
func (bc *mpConn) Read(b []byte) (n int, err error) {
return bc.recvQueue.read(b)
}
func (bc *mpConn) Write(b []byte) (n int, err error) {
frame := composeFrame(atomic.AddUint64(&bc.lastFN, 1), b)
for {
bc.pendingAckMu.RLock()
inflight := len(bc.pendingAckMap)
bc.pendingAckMu.RUnlock()
if inflight > 500 {
time.Sleep(time.Millisecond * 100)
log.Tracef("too many inflights")
continue
}
for _, sf := range bc.sortedSubflows() {
if atomic.LoadUint64(&sf.actuallyBusyOnWrite) == 1 {
// Avoid a possibly blocked writer for a retransmit
continue
}
select {
case sf.sendQueue <- frame:
return len(b), nil
default:
}
}
if len(bc.sortedSubflows()) == 0 {
return 0, ErrClosed
}
<-bc.writerMaybeReady
}
}
func (bc *mpConn) Close() error {
bc.close()
for _, sf := range bc.sortedSubflows() {
sf.close()
}
return nil
}
func (bc *mpConn) close() {
atomic.StoreUint32(&bc.closed, 1)
bc.recvQueue.close()
}
type fakeAddr struct{}
func (fakeAddr) Network() string { return "multipath" }
func (fakeAddr) String() string { return "multipath" }
func (bc *mpConn) LocalAddr() net.Addr {
return fakeAddr{}
}
func (bc *mpConn) RemoteAddr() net.Addr {
return bc.remoteAddr
}
func (bc *mpConn) SetDeadline(t time.Time) error {
bc.SetReadDeadline(t)
return bc.SetWriteDeadline(t)
}
func (bc *mpConn) SetReadDeadline(t time.Time) error {
bc.recvQueue.setReadDeadline(t)
return nil
}
func (bc *mpConn) SetWriteDeadline(t time.Time) error {
bc.muSubflows.RLock()
defer bc.muSubflows.RUnlock()
for _, sf := range bc.subflows {
if err := sf.conn.SetWriteDeadline(t); err != nil {
return err
}
}
return nil
}
func (bc *mpConn) retransmit(frame *sendFrame) {
frame.changeLock.Lock()
defer frame.changeLock.Unlock()
if atomic.LoadUint64(&frame.beingRetransmitted) == 1 {
return
}
atomic.StoreUint64(&frame.beingRetransmitted, 1)
defer func() {
atomic.StoreUint64(&frame.beingRetransmitted, 0)
}()
subflows := bc.sortedSubflows()
alreadyTransmittedOnAllSubflows := false
for {
abort := false
if bc.closed == 1 {
return
}
var selectedSubflow *subflow
abort, alreadyTransmittedOnAllSubflows, selectedSubflow = selectSubflowForRetransmit(subflows, frame, false)
if selectedSubflow == nil {
abort, alreadyTransmittedOnAllSubflows, selectedSubflow = selectSubflowForRetransmit(subflows, frame, true)
if selectedSubflow == nil {
abort = true
alreadyTransmittedOnAllSubflows = true
break
}
}
select {
case <-selectedSubflow.chClose:
continue
case selectedSubflow.sendQueue <- frame:
frame.retransmissions++
log.Debugf("retransmitted frame %d via %s", frame.fn, selectedSubflow.to)
if frame.sentVia == nil {
frame.sentVia = make([]transmissionDatapoint, 0)
}
frame.sentVia = append(frame.sentVia, transmissionDatapoint{selectedSubflow, time.Now()})
return
default:
}
if abort {
break
}
<-bc.tryRetransmit
}
if !alreadyTransmittedOnAllSubflows {
log.Debugf("frame %d is being retransmitted on all subflows of %x", frame.fn, bc.cid)
}
return
}
func selectSubflowForRetransmit(subflows []*subflow, frame *sendFrame, timeFallback bool) (bool, bool, *subflow) {
var selectedSubflow *subflow
for _, sf := range subflows {
if atomic.LoadUint64(&sf.actuallyBusyOnWrite) == 1 {
// Avoid a possibly blocked writer for a retransmit
// let's avoid re-sending a frame down the same socket twice.
// Since at best, it just double sends a frame into the send buffer
// and at worst it blocks other frames from entering a send buffer.
continue
}
// Have we used this subflow before for this frame?
usedBefore := false
var avoidTime time.Time
for _, avoidSF := range frame.sentVia {
if sf == avoidSF.sf {
usedBefore = true
avoidTime = avoidSF.txTime
}
}
// It may be acceptable to use a subflow that has been used before
// if we are in timeFallback mode
if usedBefore {
if timeFallback {
if time.Since(avoidTime) > time.Second {
usedBefore = false
}
} else {
continue
}
}
if !usedBefore {
// frame.sentVia
return false, false, sf
}
}
return true, true, selectedSubflow
}
func (bc *mpConn) sortedSubflows() []*subflow {
bc.muSubflows.RLock()
subflows := make([]*subflow, len(bc.subflows))
copy(subflows, bc.subflows)
bc.muSubflows.RUnlock()
sort.Slice(subflows, func(i, j int) bool {
return subflows[i].getRTT() < subflows[j].getRTT()
})
return subflows
}
func (bc *mpConn) add(to string, c net.Conn, clientSide bool, probeStart time.Time, tracker StatsTracker) {
bc.muSubflows.Lock()
defer bc.muSubflows.Unlock()
bc.subflows = append(bc.subflows, startSubflow(to, c, bc, clientSide, probeStart, tracker))
}
func (bc *mpConn) remove(theSubflow *subflow) {
bc.muSubflows.Lock()
var remains []*subflow
for _, sf := range bc.subflows {
if sf != theSubflow {
remains = append(remains, sf)
}
}
bc.subflows = remains
left := len(remains)
bc.muSubflows.Unlock()
if left == 0 {
bc.close()
}
}
func (bc *mpConn) retransmitLoop() {
evalTick := time.NewTicker(time.Millisecond * 100)
for {
select {
case <-evalTick.C:
}
if atomic.LoadUint32(&bc.closed) == 1 {
return
}
bc.pendingAckMu.RLock()
RetransmitFrames := make([]pendingAck, 0)
for fn, frame := range bc.pendingAckMap {
if time.Since(frame.sentAt) > frame.outboundSf.retransTimer() {
if bc.pendingAckMap[fn] != nil {
RetransmitFrames = append(RetransmitFrames, *frame)
}
}
}
bc.pendingAckMu.RUnlock()
sort.Slice(RetransmitFrames, func(i, j int) bool {
return RetransmitFrames[i].fn < RetransmitFrames[j].fn
})
for _, frame := range RetransmitFrames {
sendframe := frame.framePtr
sendframe.changeLock.Lock()
if bc.isPendingAck(frame.fn) {
// No ack means the subflow fails or has a longer RTT
// log.Errorf("Retransmitting! %#v", frame.fn)
if sendframe.beingRetransmitted == 0 {
go bc.retransmit(sendframe)
}
sendframe.changeLock.Unlock()
} else {
// It is ok to release buffer here as the frame will never
// be retransmitted again.
sendframe.release()
sendframe.changeLock.Unlock()
bc.pendingAckMu.Lock()
delete(bc.pendingAckMap, frame.fn)
bc.pendingAckMu.Unlock()
}
}
}
}
func (bc *mpConn) isPendingAck(fn uint64) bool {
if fn > minFrameNumber {
bc.pendingAckMu.RLock()
defer bc.pendingAckMu.RUnlock()
return bc.pendingAckMap[fn] != nil
}
return false
}