-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
491 lines (445 loc) · 13.2 KB
/
config.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2019 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"time"
log "github.com/inconshreveable/log15"
"github.com/scionproto/scion/pkg/snet"
)
type HerculesGeneralConfig struct {
Direction string
DumpInterval time.Duration
Interfaces []string
Mode string
MTU int
Queue int
NumThreads int
Verbosity string
LocalAddress string
PerPathStatsFile string
PCCBenchMarkDuration time.Duration
}
type SiteConfig struct {
HostAddr string
NumPaths int
PathSpec []PathSpec
}
type HerculesReceiverConfig struct {
HerculesGeneralConfig
OutputFile string
ConfigureQueues bool
AcceptTimeout int
ExpectNumPaths int
}
type HerculesSenderConfig struct {
HerculesGeneralConfig
TransmitFile string
FileOffset int
FileLength int
EnablePCC bool
RateLimit int
NumPathsPerDest int
Destinations []SiteConfig
}
var (
localAddrRegexp = regexp.MustCompile(`^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):([0-9]{1,5})$`)
configurableInterfaceRegexp = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
)
// receiver related
func (config *HerculesReceiverConfig) initializeDefaults() {
config.HerculesGeneralConfig.initializeDefaults()
config.OutputFile = ""
config.ConfigureQueues = false
config.AcceptTimeout = 0
config.ExpectNumPaths = 1
}
// Validates configuration parameters that have been provided, does not validate for presence of mandatory arguments.
func (config *HerculesReceiverConfig) validateLoose() error {
if config.Direction != "" && config.Direction != "download" {
return errors.New("field Direction must either be empty or 'download'")
}
if err := config.HerculesGeneralConfig.validateLoose(); err != nil {
return err
}
// check if output file exists (or folder)
if config.OutputFile != "" {
if stat, err := os.Stat(config.OutputFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else if stat.IsDir() {
return fmt.Errorf("output file %s is a directory", config.OutputFile)
} else {
log.Info(fmt.Sprintf("output file %s exists: will be overwritten", config.OutputFile))
}
dir := filepath.Dir(config.OutputFile)
stat, err := os.Stat(dir)
if err != nil {
return err
}
if !stat.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}
}
if config.ConfigureQueues {
for _, ifName := range config.Interfaces {
if !configurableInterfaceRegexp.MatchString(ifName) {
return fmt.Errorf("cannot configure interface '%s' - escaping not implemented", ifName)
}
}
}
return nil
}
// Validates all configuration parameters, also checks presence of mandatory parameters.
func (config *HerculesReceiverConfig) validateStrict() error {
if err := config.HerculesGeneralConfig.validateStrict(); err != nil {
return err
}
if err := config.validateLoose(); err != nil {
return err
}
if config.OutputFile == "" {
return errors.New("no output file specified")
}
return nil
}
// Merge commandline arguments into the current configuration.
func (config *HerculesReceiverConfig) mergeFlags(flags *Flags) error {
if err := forbidFlags([]string{"pcc", "p", "d", "t", "np", "be", "resv"}, "receiving"); err != nil {
return err
}
if err := config.HerculesGeneralConfig.mergeFlags(flags); err != nil {
return nil
}
if isFlagPassed("o") {
config.OutputFile = flags.outputFilename
}
if isFlagPassed("timeout") {
config.AcceptTimeout = flags.acceptTimeout
}
if isFlagPassed("ep") {
config.ExpectNumPaths = flags.expectPaths
}
return nil
}
// sender related
func (config *HerculesSenderConfig) initializeDefaults() {
config.HerculesGeneralConfig.initializeDefaults()
config.TransmitFile = ""
config.FileOffset = -1 // no offset
config.FileLength = -1 // use the whole file
config.EnablePCC = true
config.RateLimit = 3333333
config.NumPathsPerDest = 1
config.Destinations = nil
}
// Validates configuration parameters that have been provided, does not validate for presence of mandatory arguments.
func (config *HerculesSenderConfig) validateLoose() error {
if config.Direction != "" && config.Direction != "upload" {
return errors.New("field Direction must either be empty or 'upload'")
}
if err := config.HerculesGeneralConfig.validateLoose(); err != nil {
return err
}
// check that the file exists
if config.TransmitFile != "" {
stat, err := os.Stat(config.TransmitFile)
if err != nil {
return err
}
if stat.IsDir() {
return errors.New("file to transmit is a directory")
}
}
if config.FileOffset > 0 && config.FileLength < 0 {
return errors.New("must provide a valid file length")
}
if config.RateLimit < 100 {
log.Warn(fmt.Sprintf("rate limit is really low (%d packets per second)", config.RateLimit))
}
if config.NumPathsPerDest > maxPathsPerReceiver {
return fmt.Errorf("can use at most %d paths per destination; configured limit (%d) too large", maxPathsPerReceiver, config.NumPathsPerDest)
}
// validate destinations
for d := range config.Destinations {
if config.Destinations[d].NumPaths > maxPathsPerReceiver {
return fmt.Errorf("can use at most %d paths per destination; max for destination %d is too large (%d)", maxPathsPerReceiver, d, config.Destinations[d].NumPaths)
}
udpAddress, err := snet.ParseUDPAddr(config.Destinations[d].HostAddr)
if err != nil {
return err
}
if udpAddress.Host.Port == 0 {
return errors.New("must specify a destination port")
}
if udpAddress.IA == 0 {
return errors.New("must provide IA for destination address")
}
}
return nil
}
// Validates all configuration parameters and checks the presence of mandatory parameters
func (config *HerculesSenderConfig) validateStrict() error {
if err := config.HerculesGeneralConfig.validateStrict(); err != nil {
return err
}
if err := config.validateLoose(); err != nil {
return err
}
if config.TransmitFile == "" {
return errors.New("you must specify a file to send")
}
if len(config.Destinations) == 0 {
return errors.New("you must specify at least one destination")
}
return nil
}
// Merge commandline arguments into the current configuration.
func (config *HerculesSenderConfig) mergeFlags(flags *Flags) error {
if err := forbidFlags([]string{"o", "timeout", "ep"}, "sending"); err != nil {
return err
}
if err := config.HerculesGeneralConfig.mergeFlags(flags); err != nil {
return nil
}
if isFlagPassed("pcc") {
config.EnablePCC = flags.enablePCC
}
if isFlagPassed("p") {
config.RateLimit = flags.maxRateLimit
}
if isFlagPassed("d") {
sites := make([]SiteConfig, 0)
for _, remoteAddr := range flags.remoteAddrs {
sites = append(sites, SiteConfig{
HostAddr: remoteAddr,
})
}
config.Destinations = sites
}
if isFlagPassed("t") {
config.TransmitFile = flags.transmitFilename
}
if isFlagPassed("foffset") {
config.FileOffset = flags.fileOffset
}
if isFlagPassed(("flength")) {
config.FileLength = flags.fileLength
}
if isFlagPassed("np") {
config.NumPathsPerDest = flags.numPaths
}
return nil
}
// Converts config.Destinations into []*Destination for use by herculesTx.
// Assumes config (strictly) is valid.
func (config *HerculesSenderConfig) destinations() []*Destination {
var dests []*Destination
for d, dst := range config.Destinations {
// since config is valid, there can be no error
hostAddr, _ := snet.ParseUDPAddr(dst.HostAddr)
dest := &Destination{
hostAddr: hostAddr,
pathSpec: &config.Destinations[d].PathSpec,
numPaths: config.NumPathsPerDest,
}
if config.Destinations[d].NumPaths > 0 {
dest.numPaths = config.Destinations[d].NumPaths
}
dests = append(dests, dest)
}
return dests
}
// for both, sender and receiver
func (config *HerculesGeneralConfig) initializeDefaults() {
config.Direction = ""
config.DumpInterval = 1 * time.Second
config.Mode = ""
config.MTU = 1500
config.NumThreads = 1
config.Queue = 0
config.Verbosity = ""
config.LocalAddress = ""
config.PerPathStatsFile = ""
config.PCCBenchMarkDuration = 0
}
func (config *HerculesGeneralConfig) validateLoose() error {
var ifaces []*net.Interface
if config.Direction != "" && config.Direction != "upload" && config.Direction != "download" {
return errors.New("field Direction must either be 'upload', 'download' or empty")
}
if config.DumpInterval <= 0 {
return errors.New("field DumpInterval must be strictly positive")
}
if len(config.Interfaces) != 0 {
for _, ifName := range config.Interfaces {
var err error
iface, err := net.InterfaceByName(ifName)
if err != nil {
return err
}
if iface.Flags&net.FlagUp == 0 {
return fmt.Errorf("interface %s is not up", iface.Name)
}
ifaces = append(ifaces, iface)
}
}
if config.Mode != "z" && config.Mode != "c" && config.Mode != "" {
return fmt.Errorf("unknown mode %s", config.Mode)
}
// check LocalAddress
if config.LocalAddress != "" {
udpAddress, err := snet.ParseUDPAddr(config.LocalAddress)
if err != nil {
return err
}
if udpAddress.Host.Port == 0 {
return errors.New("must specify a source port")
}
if udpAddress.IA == 0 {
return errors.New("must provide IA for local address")
}
for _, iface := range ifaces {
if err := checkAssignedIP(iface, udpAddress.Host.IP); err != nil {
return err
}
}
}
if config.MTU < minFrameSize {
return fmt.Errorf("MTU too small: %d < %d", config.MTU, minFrameSize)
}
if config.MTU > 9038 {
return fmt.Errorf("can not use jumbo frames of size %d > 9038", config.MTU)
}
if config.Queue < 0 {
return errors.New("queue number must be non-negative")
}
if config.NumThreads < 1 {
return errors.New("must at least use 1 worker thread")
}
if config.Verbosity != "" && config.Verbosity != "v" && config.Verbosity != "vv" {
return errors.New("verbosity must be empty or one of 'v', 'vv'")
}
return nil
}
// Check that the mandatory general configuration has been set.
//
// WARNING: this function does not validate the contents of the options to avoid duplicate calls to validateLoose(),
// as this function is called within Hercules(Sender|Receiver)Config.validateLoose() already.
func (config *HerculesGeneralConfig) validateStrict() error {
if len(config.Interfaces) == 0 {
return errors.New("you must specify at least one network interface to use")
}
if config.LocalAddress == "" {
return errors.New("you must specify a local address")
}
if config.MTU > 8015 {
log.Warn(fmt.Sprintf("using frame size %d > 8015 (IEEE 802.11)", config.MTU))
}
return nil
}
func (config *HerculesGeneralConfig) mergeFlags(flags *Flags) error {
if isFlagPassed("n") {
config.DumpInterval = flags.dumpInterval * time.Second
}
if isFlagPassed("i") {
config.Interfaces = flags.ifNames
}
if isFlagPassed("m") {
config.Mode = flags.mode
}
if isFlagPassed("l") {
config.LocalAddress = flags.localAddr
}
if isFlagPassed("q") {
config.Queue = flags.queue
}
if isFlagPassed("nt") {
config.NumThreads = flags.numThreads
}
if isFlagPassed("v") {
config.Verbosity = flags.verbose
}
if isFlagPassed("mtu") {
config.MTU = flags.mtu
}
if isFlagPassed("ps") {
config.PerPathStatsFile = flags.perPathStats
}
if isFlagPassed("pccbd") {
config.PCCBenchMarkDuration = time.Duration(flags.pccBenchmarkDuration) * time.Second
}
return nil
}
func (config *HerculesGeneralConfig) getXDPMode() (mode int) {
switch config.Mode {
case "z":
mode = XDP_ZEROCOPY
case "c":
mode = XDP_COPY
default:
mode = XDP_COPY
}
return mode
}
func (config *HerculesGeneralConfig) interfaces() ([]*net.Interface, error) {
var interfaces []*net.Interface
for _, ifName := range config.Interfaces {
iface, err := net.InterfaceByName(ifName)
if err != nil {
return nil, err
}
interfaces = append(interfaces, iface)
}
return interfaces, nil
}
// helpers
// Checks that none of flags are passed by the command line.
// mode should either be "sending" or "receiving" and is only used in errors
//
// Returns an error if any of the provided flags was passed by the command line, nil otherwise
func forbidFlags(flags []string, mode string) error {
var illegalFlags []string
for _, f := range flags {
if isFlagPassed(f) {
illegalFlags = append(illegalFlags, f)
}
}
if len(illegalFlags) > 0 {
return fmt.Errorf("-%s not permitted for %s", strings.Join(illegalFlags, ", -"), mode)
} else {
return nil
}
}
func checkAssignedIP(iface *net.Interface, localAddr net.IP) (err error) {
// Determine src IP matches information on Interface
interfaceAddrs, err := iface.Addrs()
if err != nil {
return
}
for _, ifAddr := range interfaceAddrs {
ip, ok := ifAddr.(*net.IPNet)
if ok && ip.IP.To4() != nil && ip.IP.To4().Equal(localAddr) {
return nil
}
}
return fmt.Errorf("interface '%s' does not have the IP address '%s'", iface.Name, localAddr)
}