-
Notifications
You must be signed in to change notification settings - Fork 153
/
staticinfo_config.go
257 lines (228 loc) · 8.25 KB
/
staticinfo_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
// Copyright 2020 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 beaconing
import (
"encoding/json"
"io/ioutil"
"strings"
"time"
"github.com/scionproto/scion/go/cs/ifstate"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/seg/extensions/staticinfo"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/topology"
"github.com/scionproto/scion/go/lib/util"
)
type InterfaceLatencies struct {
Inter util.DurWrap `json:"Inter"`
Intra map[common.IFIDType]util.DurWrap `json:"Intra"`
}
type InterfaceBandwidths struct {
Inter uint64 `json:"Inter"`
Intra map[common.IFIDType]uint64 `json:"Intra"`
}
type InterfaceGeodata struct {
Longitude float32 `json:"Longitude"`
Latitude float32 `json:"Latitude"`
Address string `json:"Address"`
}
type InterfaceHops struct {
Intra map[common.IFIDType]uint32 `json:"Intra"`
}
type LinkType staticinfo.LinkType
func (l *LinkType) MarshalText() ([]byte, error) {
switch *l {
case staticinfo.LinkTypeDirect:
return []byte("direct"), nil
case staticinfo.LinkTypeMultihop:
return []byte("multihop"), nil
case staticinfo.LinkTypeOpennet:
return []byte("opennet"), nil
default:
return nil, serrors.New("invalid link type value")
}
}
func (l *LinkType) UnmarshalText(text []byte) error {
switch strings.ToLower(string(text)) {
case "direct":
*l = staticinfo.LinkTypeDirect
case "multihop":
*l = staticinfo.LinkTypeMultihop
case "opennet":
*l = staticinfo.LinkTypeOpennet
default:
return serrors.New("invalid link type", "link type", text)
}
return nil
}
// StaticInfoCfg is used to parse data from config.json.
type StaticInfoCfg struct {
Latency map[common.IFIDType]InterfaceLatencies `json:"Latency"`
Bandwidth map[common.IFIDType]InterfaceBandwidths `json:"Bandwidth"`
LinkType map[common.IFIDType]LinkType `json:"LinkType"`
Geo map[common.IFIDType]InterfaceGeodata `json:"Geo"`
Hops map[common.IFIDType]InterfaceHops `json:"Hops"`
Note string `json:"Note"`
}
// ParseStaticInfoCfg parses data from a config file into a StaticInfoCfg struct.
func ParseStaticInfoCfg(file string) (*StaticInfoCfg, error) {
raw, err := ioutil.ReadFile(file)
if err != nil {
return nil, serrors.WrapStr("failed to read static info config: ",
err, "file", file)
}
var cfg StaticInfoCfg
if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, serrors.WrapStr("failed to parse static info config: ",
err, "file ", file)
}
// TODO(matzf): validate that there are no entries for 0 the interface ID.
// TODO(matzf): the interface-to-interface ("Intra") entries in the json file
// are (expected to be!) symmetric. Check & fill in the symmetric entries.
return &cfg, nil
}
// Generate creates a StaticInfoExtn struct and
// populates it with data extracted from the configuration.
func (cfg StaticInfoCfg) Generate(intfs *ifstate.Interfaces,
ingress, egress common.IFIDType) *staticinfo.Extension {
ifType := interfaceTypeTable(intfs)
return cfg.generate(ifType, ingress, egress)
}
func (cfg StaticInfoCfg) generate(ifType map[common.IFIDType]topology.LinkType,
ingress, egress common.IFIDType) *staticinfo.Extension {
return &staticinfo.Extension{
Latency: cfg.generateLatency(ifType, ingress, egress),
Bandwidth: cfg.generateBandwidth(ifType, ingress, egress),
Geo: cfg.generateGeo(ifType, ingress, egress),
LinkType: cfg.generateLinkType(ifType, egress),
InternalHops: cfg.generateInternalHops(ifType, ingress, egress),
Note: cfg.Note,
}
}
// generateLatency creates the LatencyInfo by extracting the relevant values from
// the config.
func (cfg StaticInfoCfg) generateLatency(ifType map[common.IFIDType]topology.LinkType,
ingress, egress common.IFIDType) staticinfo.LatencyInfo {
l := staticinfo.LatencyInfo{
Intra: make(map[common.IFIDType]time.Duration),
Inter: make(map[common.IFIDType]time.Duration),
}
for ifid, v := range cfg.Latency[egress].Intra {
if includeIntraInfo(ifType, ifid, ingress, egress) {
l.Intra[ifid] = v.Duration
}
}
for ifid, v := range cfg.Latency {
t := ifType[ifid]
if ifid == egress || t == topology.Peer {
l.Inter[ifid] = v.Inter.Duration
}
}
return l
}
// generateBandwidth creates the BandwidthInfo by extracting the relevant values
// from the config.
func (cfg StaticInfoCfg) generateBandwidth(ifType map[common.IFIDType]topology.LinkType,
ingress, egress common.IFIDType) staticinfo.BandwidthInfo {
bw := staticinfo.BandwidthInfo{
Intra: make(map[common.IFIDType]uint64),
Inter: make(map[common.IFIDType]uint64),
}
for ifid, v := range cfg.Bandwidth[egress].Intra {
if includeIntraInfo(ifType, ifid, ingress, egress) {
bw.Intra[ifid] = v
}
}
for ifid, v := range cfg.Bandwidth {
t := ifType[ifid]
if ifid == egress || t == topology.Peer {
bw.Inter[ifid] = v.Inter
}
}
return bw
}
// generateLinkType creates the LinkTypeInfo by extracting the relevant values from
// the config.
func (cfg StaticInfoCfg) generateLinkType(ifType map[common.IFIDType]topology.LinkType,
egress common.IFIDType) staticinfo.LinkTypeInfo {
lt := make(staticinfo.LinkTypeInfo)
for ifid, intfLT := range cfg.LinkType {
t := ifType[ifid]
if ifid == egress || t == topology.Peer {
lt[ifid] = staticinfo.LinkType(intfLT)
}
}
return lt
}
// generateInternalHops creates the InternalHopsInfo by extracting the relevant
// values from the config.
func (cfg StaticInfoCfg) generateInternalHops(ifType map[common.IFIDType]topology.LinkType,
ingress, egress common.IFIDType) staticinfo.InternalHopsInfo {
ihi := make(staticinfo.InternalHopsInfo)
for ifid, v := range cfg.Hops[egress].Intra {
if includeIntraInfo(ifType, ifid, ingress, egress) {
ihi[ifid] = v
}
}
return ihi
}
// generateGeo creates the GeoInfo by extracting the relevant values from
// the config.
func (cfg StaticInfoCfg) generateGeo(ifType map[common.IFIDType]topology.LinkType,
ingress, egress common.IFIDType) staticinfo.GeoInfo {
gi := staticinfo.GeoInfo{}
for ifid, loc := range cfg.Geo {
t := ifType[ifid]
if ifid == egress || ifid == ingress || t == topology.Peer {
gi[ifid] = staticinfo.GeoCoordinates{
Longitude: loc.Longitude,
Latitude: loc.Latitude,
Address: loc.Address,
}
}
}
return gi
}
// includeIntraInfo determines if the intra-AS metadata info for the interface
// pair (ifid, egress) should be included in this beacon:
// Include information between the egress interface and
// - ingress interface
// - sibling child interfaces,
// - core interfaces, at start or end of a segment
// - peer interfaces
// For core/sibling child interfaces, we can skip some entries to avoid
// redundancy: by consistently only including latency to interfaces with
// ifid > egress, we ensure that for each cross-over, the latency from
// this AS Entry's egress interface to the other AS Entry's egress
// interface will be available in exactly one of the two AS Entries.
// Note that the choice of < or > is arbitrary. At least each separate
// AS needs to pick one consistently (or decide to just include the full
// information all the time), otherwise information for cross-overs may
// be missing.
func includeIntraInfo(ifType map[common.IFIDType]topology.LinkType,
ifid, ingress, egress common.IFIDType) bool {
t := ifType[ifid]
return ifid == ingress ||
t == topology.Child && ifid > egress ||
t == topology.Core && (egress == 0 || ingress == 0) && ifid > egress ||
t == topology.Peer
}
func interfaceTypeTable(intfs *ifstate.Interfaces) map[common.IFIDType]topology.LinkType {
ifMap := intfs.All()
ifTypes := make(map[common.IFIDType]topology.LinkType, len(ifMap))
for ifID, ifInfo := range ifMap {
ifTypes[ifID] = ifInfo.TopoInfo().LinkType
}
return ifTypes
}