-
Notifications
You must be signed in to change notification settings - Fork 162
/
config.go
142 lines (129 loc) · 4.07 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
// Copyright 2016 ETH Zurich
// Copyright 2019 ETH Zurich, Anapaya Systems
//
// 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 config defines the router's configuration file.
package config
import (
"io"
"runtime"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/private/serrors"
"github.com/scionproto/scion/private/config"
"github.com/scionproto/scion/private/env"
api "github.com/scionproto/scion/private/mgmtapi"
)
const idSample = "router-1"
type Config struct {
General env.General `toml:"general,omitempty"`
Features env.Features `toml:"features,omitempty"`
Logging log.Config `toml:"log,omitempty"`
Metrics env.Metrics `toml:"metrics,omitempty"`
API api.Config `toml:"api,omitempty"`
Router RouterConfig `toml:"router,omitempty"`
}
type RouterConfig struct {
ReceiveBufferSize int `toml:"receive_buffer_size,omitempty"`
SendBufferSize int `toml:"send_buffer_size,omitempty"`
NumProcessors int `toml:"num_processors,omitempty"`
NumSlowPathProcessors int `toml:"num_slow_processors,omitempty"`
BatchSize int `toml:"batch_size,omitempty"`
EndhostStartPort *int `toml:"endhost_start_port,omitempty"`
EndhostEndPort *int `toml:"endhost_end_port,omitempty"`
}
func (cfg *RouterConfig) ConfigName() string {
return "router"
}
func (cfg *RouterConfig) Validate() error {
if cfg.ReceiveBufferSize < 0 {
return serrors.New("Provided router config is invalid. ReceiveBufferSize < 0")
}
if cfg.SendBufferSize < 0 {
return serrors.New("Provided router config is invalid. SendBufferSize < 0")
}
if cfg.BatchSize < 1 {
return serrors.New("Provided router config is invalid. BatchSize < 1")
}
if cfg.NumProcessors < 0 {
return serrors.New("Provided router config is invalid. NumProcessors < 0")
}
if cfg.NumSlowPathProcessors < 1 {
return serrors.New("Provided router config is invalid. NumSlowPathProcessors < 1")
}
if cfg.EndhostStartPort != nil {
if cfg.EndhostEndPort == nil {
return serrors.New("Provided router config is invalid. " +
"EndHostEndPort is nil; EndHostStartPort isn't")
}
if *cfg.EndhostStartPort < 0 {
return serrors.New("Provided router config is invalid. EndHostStartPort < 0")
}
if *cfg.EndhostEndPort >= (1 << 16) {
return serrors.New("Provided router config is invalid. EndHostEndPort > 2**16 -1")
}
if *cfg.EndhostStartPort > *cfg.EndhostEndPort {
return serrors.New("Provided router config is invalid. " +
"EndHostStartPort > EndhostEndPort")
}
} else {
if cfg.EndhostEndPort != nil {
return serrors.New("Provided router config is invalid. " +
"EndHostStartPort is nil; EndHostEndPort isn't")
}
}
return nil
}
func (cfg *RouterConfig) InitDefaults() {
if cfg.NumProcessors == 0 {
cfg.NumProcessors = runtime.GOMAXPROCS(0)
}
if cfg.NumSlowPathProcessors == 0 {
cfg.NumSlowPathProcessors = 1
}
if cfg.BatchSize == 0 {
cfg.BatchSize = 256
}
}
func (cfg *RouterConfig) Sample(dst io.Writer, path config.Path, ctx config.CtxMap) {
config.WriteString(dst, routerConfigSample)
}
func (cfg *Config) InitDefaults() {
config.InitAll(
&cfg.General,
&cfg.Features,
&cfg.Logging,
&cfg.Metrics,
&cfg.API,
&cfg.Router,
)
}
func (cfg *Config) Validate() error {
return config.ValidateAll(
&cfg.General,
&cfg.Features,
&cfg.Logging,
&cfg.Metrics,
&cfg.API,
&cfg.Router,
)
}
func (cfg *Config) Sample(dst io.Writer, path config.Path, _ config.CtxMap) {
config.WriteSample(dst, path, config.CtxMap{config.ID: idSample},
&cfg.General,
&cfg.Features,
&cfg.Logging,
&cfg.Metrics,
&cfg.API,
&cfg.Router,
)
}