forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathethr-kai.go
309 lines (278 loc) · 7.56 KB
/
ethr-kai.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
//-----------------------------------------------------------------------------
// 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 (
"flag"
"fmt"
"runtime"
"strings"
"time"
"github.com/microsoft/ethr-kai/internal/cmd"
"github.com/microsoft/ethr-kai/internal/ethrLog"
)
const defaultLogFileName = "./ethrs.log for server, ./ethrc.log for client"
const latencyDefaultBufferLenStr = "1B"
const defaultBufferLenStr = "16KB"
var (
gVersion string
loggingLevel ethrLog.LogLevel = ethrLog.LogLevelInfo
)
func main() {
//
// If version is not set via ldflags, then default to UNKNOWN
//
if gVersion == "" {
gVersion = "[VERSION: UNKNOWN]"
}
//
// Set GOMAXPROCS to 1024 as running large number of goroutines in a loop
// to send network traffic results in timer starvation, as well as unfair
// processing time across goroutines resulting in starvation of many TCP
// connections. Using a higher number of threads via GOMAXPROCS solves this
// problem.
//
runtime.GOMAXPROCS(1024)
flag.Usage = func() { cmd.EthrUsage(gVersion) }
isServer := flag.Bool("s", false, "")
clientDest := flag.String("c", "", "")
testTypePtr := flag.String("t", "", "")
thCount := flag.Int("n", 1, "")
bufLenStr := flag.String("l", "", "")
protocol := flag.String("p", "tcp", "")
outputFile := flag.String("o", defaultLogFileName, "")
debug := flag.Bool("debug", false, "")
noOutput := flag.Bool("no", false, "")
duration := flag.Duration("d", 10*time.Second, "")
showUI := flag.Bool("ui", false, "")
showWEBUI := flag.Bool("web", false, "")
rttCount := flag.Int("i", 1000, "")
portStr := flag.String("ports", "", "")
modeStr := flag.String("m", "", "")
use4 := flag.Bool("4", false, "")
use6 := flag.Bool("6", false, "")
gap := flag.Duration("g", 0, "")
reverse := flag.Bool("r", false, "")
ncs := flag.Bool("ncs", false, "")
ic := flag.Bool("ic", false, "")
flag.Parse()
//
// TODO: Handle the case if there are incorrect arguments
// fmt.Println("Number of incorrect arguments: " + strconv.Itoa(flag.NArg()))
//
//
// Only used in client mode, to control whether to display per connection
// statistics or not.
//
gNoConnectionStats = *ncs
//
// Only used in client mode to ignore HTTPS cert errors.
//
gIgnoreCert = *ic
if *debug {
loggingLevel = ethrLog.LogLevelDebug
}
xMode := false
switch *modeStr {
case "":
case "x":
xMode = true
default:
cmd.PrintUsageError("Invalid value for execution mode (-m).")
}
mode := ethrModeInv
if *isServer {
if *clientDest != "" {
cmd.PrintUsageError("Invalid arguments, \"-c\" cannot be used with \"-s\".")
}
if xMode {
mode = ethrModeExtServer
} else {
mode = ethrModeServer
}
} else if *clientDest != "" {
if xMode {
mode = ethrModeExtClient
} else {
mode = ethrModeClient
}
} else {
cmd.PrintUsageError("Invalid arguments, use either \"-s\" or \"-c\".")
}
if *reverse && mode != ethrModeClient {
cmd.PrintUsageError("Invalid arguments, \"-r\" can only be used in client mode.")
}
if *use4 && !*use6 {
ipVer = ethrIPv4
} else if *use6 && !*use4 {
ipVer = ethrIPv6
}
//Default latency test to 1KB if length is not specified
switch *bufLenStr {
case "":
*bufLenStr = getDefaultBufferLenStr(*testTypePtr)
}
bufLen := unitToNumber(*bufLenStr)
if bufLen == 0 {
cmd.PrintUsageError(fmt.Sprintf("Invalid length specified: %s" + *bufLenStr))
}
if *rttCount <= 0 {
cmd.PrintUsageError(fmt.Sprintf("Invalid RTT count for latency test: %d", *rttCount))
}
var testType EthrTestType
switch *testTypePtr {
case "":
switch mode {
case ethrModeServer:
testType = All
case ethrModeExtServer:
testType = All
case ethrModeClient:
testType = Bandwidth
case ethrModeExtClient:
testType = ConnLatency
}
case "b":
testType = Bandwidth
case "c":
testType = Cps
case "p":
testType = Pps
case "l":
testType = Latency
case "cl":
testType = ConnLatency
default:
cmd.PrintUsageError(fmt.Sprintf("Invalid value \"%s\" specified for parameter \"-t\".\n"+
"Valid parameters and values are:\n", *testTypePtr))
}
p := strings.ToUpper(*protocol)
proto := TCP
switch p {
case "TCP":
proto = TCP
case "UDP":
proto = UDP
case "HTTP":
proto = HTTP
case "HTTPS":
proto = HTTPS
case "ICMP":
proto = ICMP
default:
cmd.PrintUsageError(fmt.Sprintf("Invalid value \"%s\" specified for parameter \"-p\".\n"+
"Valid parameters and values are:\n", *protocol))
}
if *thCount <= 0 {
*thCount = runtime.NumCPU()
}
//
// For Pkt/s, we always override the buffer size to be just 1 byte.
// TODO: Evaluate in future, if we need to support > 1 byte packets for
// Pkt/s testing.
//
if testType == Pps {
bufLen = 1
}
testParam := EthrTestParam{EthrTestID{EthrProtocol(proto), testType},
uint32(*thCount),
uint32(bufLen),
uint32(*rttCount),
*reverse}
validateTestParam(mode, testParam)
generatePortNumbers(*portStr)
logFileName := *outputFile
if !*noOutput {
if logFileName == defaultLogFileName {
switch mode {
case ethrModeServer:
logFileName = "ethrs.log"
case ethrModeExtServer:
logFileName = "ethrxs.log"
case ethrModeClient:
logFileName = "ethrc.log"
case ethrModeExtClient:
logFileName = "ethrxc.log"
}
}
ethrLog.LogInit(logFileName)
}
clientParam := ethrClientParam{*duration, *gap}
serverParam := ethrServerParam{*showUI, *showWEBUI}
switch mode {
case ethrModeServer:
runServer(testParam, serverParam)
case ethrModeExtServer:
runXServer(testParam, serverParam)
case ethrModeClient:
runClient(testParam, clientParam, *clientDest)
case ethrModeExtClient:
runXClient(testParam, clientParam, *clientDest)
}
}
func getDefaultBufferLenStr(testTypePtr string) string {
if testTypePtr == "l" {
return latencyDefaultBufferLenStr
}
return defaultBufferLenStr
}
func emitUnsupportedTest(testParam EthrTestParam) {
cmd.PrintUsageError(fmt.Sprintf("\"%s\" test for \"%s\" is not supported.\n",
testToString(testParam.TestID.Type), protoToString(testParam.TestID.Protocol)))
}
func printReverseModeError() {
cmd.PrintUsageError("Reverse mode (-r) is only supported for TCP Bandwidth tests.")
}
func validateTestParam(mode ethrMode, testParam EthrTestParam) {
testType := testParam.TestID.Type
protocol := testParam.TestID.Protocol
if mode == ethrModeServer {
if testType != All || protocol != TCP {
emitUnsupportedTest(testParam)
}
} else if mode == ethrModeClient {
switch protocol {
case TCP:
if testType != Bandwidth && testType != Cps && testType != Latency {
emitUnsupportedTest(testParam)
}
if testParam.Reverse && testType != Bandwidth {
printReverseModeError()
}
case UDP:
if testType != Bandwidth && testType != Pps {
emitUnsupportedTest(testParam)
}
if testType == Bandwidth {
if testParam.BufferSize > (64 * 1024) {
cmd.PrintUsageError("Maximum supported buffer size for UDP is 64K\n")
}
}
if testParam.Reverse {
printReverseModeError()
}
case HTTP:
if testType != Bandwidth && testType != Latency {
emitUnsupportedTest(testParam)
}
if testParam.Reverse {
printReverseModeError()
}
case HTTPS:
if testType != Bandwidth {
emitUnsupportedTest(testParam)
}
if testParam.Reverse {
printReverseModeError()
}
default:
emitUnsupportedTest(testParam)
}
} else if mode == ethrModeExtClient {
if (protocol != TCP) || (testType != ConnLatency && testType != Bandwidth) {
emitUnsupportedTest(testParam)
}
}
}