forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsession.go
453 lines (377 loc) · 9.83 KB
/
session.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
447
448
449
450
451
452
453
//-----------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license.
// See LICENSE.txt file in the project root for full license information.
//-----------------------------------------------------------------------------
package main
import (
"container/list"
"encoding/gob"
"net"
"os"
"sync"
"sync/atomic"
"time"
)
// EthrTestType represents the test type.
type EthrTestType uint32
const (
// All represents all tests - For now only applicable for servers
All EthrTestType = iota
// Bandwidth represents the bandwidth test.
Bandwidth
// Cps represents connections/s test.
Cps
// Pps represents packets/s test.
Pps
// Latency represents the latency test.
Latency
// ConnLatency represents connection setup latency.
ConnLatency
)
// EthrProtocol represents the network protocol.
type EthrProtocol uint32
const (
// TCP represents the tcp protocol.
TCP EthrProtocol = iota
// UDP represents the udp protocol.
UDP
// HTTP represents using http protocol.
HTTP
// HTTPS represents using https protocol.
HTTPS
// ICMP represents the icmp protocol.
ICMP
)
// EthrTestID represents the test id.
type EthrTestID struct {
// Protocol represents the protocol this test uses.
Protocol EthrProtocol
// Type represents the test type this test uses.
Type EthrTestType
}
// EthrMsgType represents the message type.
type EthrMsgType uint32
const (
// EthrInv represents the Inv message.
EthrInv EthrMsgType = iota
// EthrSyn represents the Syn message.
EthrSyn
// EthrAck represents the Ack message.
EthrAck
// EthrFin represents the Fin message.
EthrFin
// EthrBgn represents the Bgn message.
EthrBgn
// EthrEnd represents the End message.
EthrEnd
)
// EthrMsgVer represents the message version.
type EthrMsgVer uint32
// EthrMsg represents the message entity.
type EthrMsg struct {
// Version represents the message version.
Version EthrMsgVer
// Type represents the message type.
Type EthrMsgType
// Syn represents the Syn value.
Syn *EthrMsgSyn
// Ack represents the Ack value.
Ack *EthrMsgAck
// Fin represents the Fin value.
Fin *EthrMsgFin
// Bgn represents the Bgn value.
Bgn *EthrMsgBgn
// End represents the End value.
End *EthrMsgEnd
}
// EthrMsgSyn represents the Syn entity.
type EthrMsgSyn struct {
// TestParam represents the test parameters.
TestParam EthrTestParam
}
// EthrMsgAck represents the Ack entity.
type EthrMsgAck struct {
Cert []byte
NapDuration time.Duration
}
// EthrMsgFin represents the Fin entity.
type EthrMsgFin struct {
// Message represents the message body.
Message string
}
// EthrMsgBgn represents the Bgn entity.
type EthrMsgBgn struct {
// UDPPort represents the udp port.
UDPPort string
}
// EthrMsgEnd represents the End entity.
type EthrMsgEnd struct {
// Message represents the message body.
Message string
}
// EthrTestParam represents the parameters used for the test.
type EthrTestParam struct {
// TestID represents the test id of this test.
TestID EthrTestID
// NumThreads represents how many threads are used for the test.
NumThreads uint32
// BufferSize represents the buffer size.
BufferSize uint32
// RttCount represents the rtt count.
RttCount uint32
// Reverse mode for bandwidth tests.
Reverse bool
}
type ethrTestResult struct {
data uint64
}
type ethrTest struct {
isActive bool
session *ethrSession
ctrlConn net.Conn
refCount int32
enc *gob.Encoder
dec *gob.Decoder
rcvdMsgs chan *EthrMsg
testParam EthrTestParam
testResult ethrTestResult
done chan struct{}
connList *list.List
}
type ethrMode uint32
const (
ethrModeInv ethrMode = iota
ethrModeServer
ethrModeExtServer
ethrModeClient
ethrModeExtClient
)
type ethrIPVer uint32
const (
ethrIPAny ethrIPVer = iota
ethrIPv4
ethrIPv6
)
type ethrClientParam struct {
duration time.Duration
gap time.Duration
}
type ethrServerParam struct {
showUI bool
showWEBUI bool
}
var ipVer ethrIPVer = ethrIPAny
type ethrConn struct {
data uint64
test *ethrTest
conn net.Conn
elem *list.Element
fd uintptr
retrans uint64
}
type ethrSession struct {
remoteAddr string
testCount uint32
tests map[EthrTestID]*ethrTest
}
var gSessions = make(map[string]*ethrSession)
var gSessionKeys = make([]string, 0)
var gSessionLock sync.RWMutex
func deleteKey(key string) {
i := 0
for _, x := range gSessionKeys {
if x != key {
gSessionKeys[i] = x
i++
}
}
gSessionKeys = gSessionKeys[:i]
}
func newTest(remoteAddr string, conn net.Conn, testParam EthrTestParam, enc *gob.Encoder, dec *gob.Decoder) (*ethrTest, error) {
gSessionLock.Lock()
defer gSessionLock.Unlock()
return newTestInternal(remoteAddr, conn, testParam, enc, dec)
}
func newTestInternal(remoteAddr string, conn net.Conn, testParam EthrTestParam, enc *gob.Encoder, dec *gob.Decoder) (*ethrTest, error) {
var session *ethrSession
session, found := gSessions[remoteAddr]
if !found {
session = ðrSession{}
session.remoteAddr = remoteAddr
session.tests = make(map[EthrTestID]*ethrTest)
gSessions[remoteAddr] = session
gSessionKeys = append(gSessionKeys, remoteAddr)
}
test, found := session.tests[testParam.TestID]
if found {
return test, os.ErrExist
}
session.testCount++
test = ðrTest{}
test.session = session
test.ctrlConn = conn
test.refCount = 0
test.enc = enc
test.dec = dec
test.rcvdMsgs = make(chan *EthrMsg)
test.testParam = testParam
test.done = make(chan struct{})
test.connList = list.New()
session.tests[testParam.TestID] = test
return test, nil
}
func deleteTest(test *ethrTest) {
gSessionLock.Lock()
defer gSessionLock.Unlock()
deleteTestInternal(test)
}
func deleteTestInternal(test *ethrTest) {
session := test.session
testID := test.testParam.TestID
//
// TODO fix this, we need to decide where to close this, inside this
// function or by the caller. The reason we may need it to be done by
// the caller is, because done is used for test done notification and
// there may be some time after done that consumers are still accessing it
//
// Since we have not added any refCounting on test object, we are doing
// hacky timeout based solution by closing "done" outside and sleeping
// for sufficient time. ugh!
//
// close(test.done)
// test.ctrlConn.Close()
// test.session = nil
// test.connList = test.connList.Init()
//
delete(session.tests, testID)
session.testCount--
if session.testCount == 0 {
deleteKey(session.remoteAddr)
delete(gSessions, session.remoteAddr)
}
}
func getTest(remoteAddr string, proto EthrProtocol, testType EthrTestType) (test *ethrTest) {
gSessionLock.RLock()
defer gSessionLock.RUnlock()
return getTestInternal(remoteAddr, proto, testType)
}
func getTestInternal(remoteAddr string, proto EthrProtocol, testType EthrTestType) (test *ethrTest) {
test = nil
session, found := gSessions[remoteAddr]
if !found {
return
}
test, _ = session.tests[EthrTestID{proto, testType}]
return
}
func createOrGetTest(remoteAddr string, proto EthrProtocol, testType EthrTestType) (test *ethrTest, isNew bool) {
gSessionLock.Lock()
defer gSessionLock.Unlock()
isNew = false
test = getTestInternal(remoteAddr, proto, testType)
if test == nil {
isNew = true
testParam := EthrTestParam{TestID: EthrTestID{proto, testType}}
test, _ = newTestInternal(remoteAddr, nil, testParam, nil, nil)
test.isActive = true
}
atomic.AddInt32(&test.refCount, 1)
return
}
func safeDeleteTest(test *ethrTest) bool {
gSessionLock.Lock()
defer gSessionLock.Unlock()
if atomic.AddInt32(&test.refCount, -1) == 0 {
deleteTestInternal(test)
return true
}
return false
}
func addRef(test *ethrTest) {
gSessionLock.Lock()
defer gSessionLock.Unlock()
// TODO: Since we already take lock, atomic is not needed. Fix this later.
atomic.AddInt32(&test.refCount, 1)
}
func (test *ethrTest) newConn(conn net.Conn) (ec *ethrConn) {
gSessionLock.Lock()
defer gSessionLock.Unlock()
ec = ðrConn{}
ec.test = test
ec.conn = conn
ec.fd = getFd(conn)
ec.elem = test.connList.PushBack(ec)
return
}
func (test *ethrTest) delConn(conn net.Conn) {
for e := test.connList.Front(); e != nil; e = e.Next() {
ec := e.Value.(*ethrConn)
if ec.conn == conn {
test.connList.Remove(e)
break
}
}
}
func (test *ethrTest) connListDo(f func(*ethrConn)) {
gSessionLock.RLock()
defer gSessionLock.RUnlock()
for e := test.connList.Front(); e != nil; e = e.Next() {
ec := e.Value.(*ethrConn)
f(ec)
}
}
func watchControlChannel(test *ethrTest, waitForChannelStop chan bool) {
go func() {
for {
ethrMsg := recvSessionMsg(test.dec)
if ethrMsg.Type == EthrInv {
break
}
test.rcvdMsgs <- ethrMsg
ui.printDbg("%v", ethrMsg)
}
waitForChannelStop <- true
}()
}
func recvSessionMsg(dec *gob.Decoder) (ethrMsg *EthrMsg) {
ethrMsg = &EthrMsg{}
err := dec.Decode(ethrMsg)
if err != nil {
ui.printDbg("Error receiving message on control channel: %v", err)
ethrMsg.Type = EthrInv
}
return
}
func sendSessionMsg(enc *gob.Encoder, ethrMsg *EthrMsg) error {
err := enc.Encode(ethrMsg)
if err != nil {
ui.printDbg("Error sending message on control channel. Message: %v, Error: %v", ethrMsg, err)
}
return err
}
func createAckMsg(cert []byte, d time.Duration) (ethrMsg *EthrMsg) {
ethrMsg = &EthrMsg{Version: 0, Type: EthrAck}
ethrMsg.Ack = &EthrMsgAck{}
ethrMsg.Ack.Cert = cert
ethrMsg.Ack.NapDuration = d
return
}
func createFinMsg(message string) (ethrMsg *EthrMsg) {
ethrMsg = &EthrMsg{Version: 0, Type: EthrFin}
ethrMsg.Fin = &EthrMsgFin{}
ethrMsg.Fin.Message = message
return
}
func createSynMsg(testParam EthrTestParam) (ethrMsg *EthrMsg) {
ethrMsg = &EthrMsg{Version: 0, Type: EthrSyn}
ethrMsg.Syn = &EthrMsgSyn{}
ethrMsg.Syn.TestParam = testParam
return
}
func createBgnMsg(port string) (ethrMsg *EthrMsg) {
ethrMsg = &EthrMsg{Version: 0, Type: EthrBgn}
ethrMsg.Bgn = &EthrMsgBgn{}
ethrMsg.Bgn.UDPPort = port
return
}