-
Notifications
You must be signed in to change notification settings - Fork 156
/
watcher.go
491 lines (446 loc) · 14.8 KB
/
watcher.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 2020 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 control
import (
"context"
"fmt"
"net"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/metrics"
"github.com/scionproto/scion/pkg/private/serrors"
"github.com/scionproto/scion/pkg/snet"
)
const (
// defaultGatewayDiscoveryInterval is the default interval for discovering
// remote gateways.
defaultGatewayDiscoveryInterval = 5 * time.Second
// defaultGatewayDiscoveryTimeout is the default timeout for discovering
// remote gateways.
defaultGatewayDiscoveryTimeout = 5 * time.Second
// defaultGatewayPollInterval is the default interval for polling the remote
// gateway for prefixes.
defaultGatewayPollInterval = 5 * time.Second
// defaultGatewayPollTimeout is the default timeout for polling the remote
// gateway for prefixes.
defaultGatewayPollTimeout = 5 * time.Second
)
var (
// ErrAlreadyRunning is the error returned when attempting to run a task twice.
ErrAlreadyRunning = serrors.New("is running")
)
// Gateway represents a remote gateway instance.
type Gateway struct {
// Control contains the control-plane address of the remote gateway.
Control *net.UDPAddr
// Probe contains the probing address of the remote gateway.
Probe *net.UDPAddr
// Data contains the data-plane address of the remote gateway.
Data *net.UDPAddr
// Interfaces are the last-hop SCION interfaces that should be preferred.
Interfaces []uint64
}
func (g Gateway) Equal(other Gateway) bool {
return g.Control.String() == other.Control.String() &&
g.Probe.String() == other.Probe.String() &&
g.Data.String() == other.Data.String() &&
interfacesKey(g.Interfaces) == interfacesKey(other.Interfaces)
}
func interfacesKey(interfaces []uint64) string {
keyParts := make([]string, 0, len(interfaces))
for _, i := range interfaces {
keyParts = append(keyParts, strconv.FormatUint(i, 10))
}
sort.Strings(keyParts)
return strings.Join(keyParts, "-")
}
// Discoverer discovers gateway instances. It must be scoped to the intended
// remote for the GatewayWatcher.
type Discoverer interface {
Gateways(ctx context.Context) ([]Gateway, error)
}
// GatewayWatcherMetrics contains the metrics the GatewayWatcher reports.
type GatewayWatcherMetrics struct {
// Remotes is the number of remote gateways discovered in the remote AS.
Remotes metrics.Gauge
// RemotesChanges is the number of times the number of remotes changed.
RemotesChanges metrics.Counter
// DiscoveryErrors counts the errors when discovering gateway in a remote AS.
DiscoveryErrors metrics.Counter
// PrefixFetchErrors counts the error when fetching prefixes from gateways
// in the remote AS.
PrefixFetchErrors metrics.Counter
}
// GatewayWatcher watches gateways in a specific remote AS.
//
// Per discovered gateway, the watcher starts a PrefixWatcher that periodically
// polls the IP prefixes served by that gateway. The PrefixWatcher tasks are
// dynamically added and removed depending on the gateway discovery responses.
// The delta in the list of gateways is assumed to be empty, if a discovery
// attempt fails.
//
// When the GatewayWatcher is stopped, all spawaned PrefixWatcher tasks are
// stopped as well.
type GatewayWatcher struct {
// Remote is the remote AS to watch.
Remote addr.IA
// Discoverer is used for remote gateway discovery. It must not be nil.
Discoverer Discoverer
// DiscoverInterval is the time between consecutive gateway discovery
// attempts. If zero, this defaults to 5 seconds.
DiscoverInterval time.Duration
// DiscoverTimeout is the timout for an individual gateway discovery
// attempts. If zero, this defaults to 5 seconds.
DiscoverTimeout time.Duration
// Template serves as the template for the PrefixWatcher tasks that are
// spawned. For each discovered gateway, a PrefixWatcher task is started
// based on this template.
Template PrefixWatcherConfig
// Metrics can be used to report information about discovered remote gateways. If not
// initialized, no metrics will be reported.
Metrics GatewayWatcherMetrics
// stateMtx protects the state below from concurrent access.
stateMtx sync.RWMutex
gateways []Gateway
// currentWatchers is a map of all currently active prefix watchers.
currentWatchers map[string]watcherItem
runMarkerLock sync.Mutex
// runMarker is set to true the first time a Session runs. Subsequent calls use this value to
// return an error.
runMarker bool
}
type watcherItem struct {
*prefixWatcher
cancel func()
}
func (w *watcherItem) Close() error {
w.cancel()
return w.prefixWatcher.fetcher.Close()
}
// gatewayDiagnostics represents the gathered diagnostics from the prefixes.
type gatewayDiagnostics struct {
DataAddr string `json:"data_address"`
ProbeAddr string `json:"probe_address"`
Interfaces []uint64 `json:"interfaces"`
Prefixes []string `json:"prefixes"`
Timestamp time.Time `json:"timestamp"`
}
// Run watches the remote for gateways. This method blocks until the context
// expires, or an irrecoverable error is encountered.
func (w *GatewayWatcher) Run(ctx context.Context) error {
if err := w.runOnceCheck(); err != nil {
return err
}
if err := w.validateParameters(); err != nil {
return err
}
logger := log.FromCtx(ctx).New("debug_id", log.NewDebugID(), "remote", w.Remote)
ctx = log.CtxWith(ctx, logger)
logger.Info("Starting periodic gateway discovery")
defer logger.Info("Stopped periodic gateway discovery")
ticker := time.NewTicker(w.DiscoverInterval)
defer ticker.Stop()
w.run(ctx)
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
w.run(ctx)
}
}
}
func (w *GatewayWatcher) run(runCtx context.Context) {
ctx, cancel := context.WithTimeout(runCtx, w.DiscoverTimeout)
defer cancel()
logger := log.FromCtx(ctx)
logger.Debug("Discovering remote gateways")
discovered, err := w.Discoverer.Gateways(ctx)
if err != nil {
metrics.GaugeSet(w.Metrics.Remotes, 0)
metrics.CounterInc(w.Metrics.DiscoveryErrors)
logger.Info("Failed to discover remote gateways", "err", err)
return
}
w.stateMtx.Lock()
defer w.stateMtx.Unlock()
diff := computeDiff(w.gateways, discovered)
for _, gateway := range diff.Add {
w.currentWatchers[fmt.Sprint(gateway)] = w.watchPrefixes(runCtx, gateway)
}
for _, gateway := range diff.Remove {
key := fmt.Sprint(gateway)
if prefixWatcher, ok := w.currentWatchers[key]; ok {
if err := prefixWatcher.Close(); err != nil {
logger.Info("Error stopping prefix discovery", "gateway", prefixWatcher.gateway)
}
delete(w.currentWatchers, key)
}
}
if len(diff.Add) != 0 {
logger.Info("Start prefix discovery", "gateways", diff.Add)
}
if len(diff.Remove) != 0 {
logger.Info("Stop prefix discovery", "gateways", diff.Remove)
}
w.gateways = discovered
metrics.GaugeSet(w.Metrics.Remotes, float64(len(discovered)))
if len(diff.Add) > 0 || len(diff.Remove) > 0 {
metrics.CounterInc(w.Metrics.RemotesChanges)
}
}
func (w *GatewayWatcher) watchPrefixes(ctx context.Context, gateway Gateway) watcherItem {
ctx, cancel := context.WithCancel(ctx)
watcher := newPrefixWatcher(ctx, gateway, w.Remote, w.Template, w.Metrics.PrefixFetchErrors)
go func(ctx context.Context, watcher *prefixWatcher) {
defer log.HandlePanic()
if err := watcher.Run(ctx); err != nil {
addr := snet.UDPAddr{
IA: watcher.remote,
Host: watcher.gateway.Control,
}
log.FromCtx(ctx).Info("PrefixWatcher stopped with error", "remote", addr, "err", err)
}
}(ctx, watcher)
return watcherItem{
prefixWatcher: watcher,
cancel: cancel,
}
}
func (w *GatewayWatcher) runOnceCheck() error {
w.runMarkerLock.Lock()
defer w.runMarkerLock.Unlock()
if w.runMarker {
return ErrAlreadyRunning
}
w.runMarker = true
w.currentWatchers = map[string]watcherItem{}
return nil
}
// Diagnostics gives back a RemoteDiagnostics map
func (w *GatewayWatcher) diagnostics() (remoteDiagnostics, error) {
w.stateMtx.RLock()
defer w.stateMtx.RUnlock()
// assemble the diagnostics json output
diagnostics := struct {
Gateways map[string]gatewayDiagnostics `json:"gateways"`
}{
Gateways: make(map[string]gatewayDiagnostics),
}
for _, watcher := range w.currentWatchers {
watcher.stateMtx.RLock()
defer watcher.stateMtx.RUnlock()
interfaces := watcher.gateway.Interfaces
if watcher.gateway.Interfaces == nil {
interfaces = []uint64{}
}
diagnostics.Gateways[watcher.gateway.Control.String()] = gatewayDiagnostics{
DataAddr: watcher.gateway.Data.String(),
ProbeAddr: watcher.gateway.Probe.String(),
Interfaces: interfaces,
Prefixes: watcher.prefixes,
Timestamp: watcher.timestamp,
}
}
return diagnostics, nil
}
func (w *GatewayWatcher) validateParameters() error {
if w.Discoverer == nil {
return serrors.New("discoverer must not be nil")
}
if w.DiscoverInterval == 0 {
w.DiscoverInterval = defaultGatewayDiscoveryInterval
}
if w.DiscoverTimeout == 0 {
w.DiscoverTimeout = defaultGatewayDiscoveryTimeout
}
if err := w.Template.validateParameters(); err != nil {
return serrors.WrapStr("validating PrefixWatcher template", err)
}
return nil
}
// PrefixConsumer consumes the prefixes fetched by the PrefixWatcher.
type PrefixConsumer interface {
Prefixes(remote addr.IA, gateway Gateway, prefixes []*net.IPNet) error
}
// PrefixFetcher fetches the IP prefixes from a remote gateway.
type PrefixFetcher interface {
Prefixes(ctx context.Context, gateway *net.UDPAddr) ([]*net.IPNet, error)
Close() error
}
// PrefixFetcherFactory constructs a PrefixFetcher for a given remote gateway.
type PrefixFetcherFactory interface {
NewPrefixFetcher(ctx context.Context, gateway Gateway) PrefixFetcher
}
// PrefixWatcherConfig configures a prefix watcher that watches IP prefixes
// advertised by a remote gateway. The discovered IP prefixes are advertised to
// the prefix consumer. The watcher is stateless and does not keep track of
// previously discovered IP prefixes.
type PrefixWatcherConfig struct {
// Consumer consume the fetched prefixes. Its methods are called
// synchroniously and should return swiftly.
Consumer PrefixConsumer
// PrefixFetcherFactory is used to create a IP prefix fetcher for the remote gateway.
FetcherFactory PrefixFetcherFactory
// PollInterval is the time between consecutive poll attempts. If zero, this
// defaults to 5 seconds.
PollInterval time.Duration
// PollTimeout is the timout for an individual poll attempts. If zero, this
// defaults to 5 seconds.
PollTimeout time.Duration
}
func (c *PrefixWatcherConfig) validateParameters() error {
if c.Consumer == nil {
return serrors.New("consumer must not be nil")
}
if c.FetcherFactory == nil {
return serrors.New("fetcher factory must not be nil")
}
if c.PollInterval == 0 {
c.PollInterval = defaultGatewayPollInterval
}
if c.PollTimeout == 0 {
c.PollTimeout = defaultGatewayPollTimeout
}
return nil
}
// prefixWatcher watches IP prefixes advertised by a remote gateway. The
// discovered IP prefixes are advertised to the prefix consumer. The watcher is
// stateless and does not keep track of previously discovered IP prefixes.
type prefixWatcher struct {
PrefixWatcherConfig
gateway Gateway
remote addr.IA
fetcher PrefixFetcher
runMarkerLock sync.Mutex
// runMarker is set to true the first time a Session runs. Subsequent calls use this value to
// return an error.
runMarker bool
// stateMtx protects the state below from concurrent access.
stateMtx sync.RWMutex
// state of last fetched prefixes
prefixes []string
// timestamp of last fetched prefixes
timestamp time.Time
// fetchErrors counts the amount of errors while fetching prefixes.
fetchErrors metrics.Counter
}
func newPrefixWatcher(
ctx context.Context,
gateway Gateway,
remote addr.IA,
cfg PrefixWatcherConfig,
fetchErrors metrics.Counter,
) *prefixWatcher {
return &prefixWatcher{
PrefixWatcherConfig: cfg,
gateway: gateway,
remote: remote,
fetcher: cfg.FetcherFactory.NewPrefixFetcher(ctx, gateway),
fetchErrors: fetchErrors,
}
}
// Run periodically fetches the prefixes advertised by the remote gateway. This
// method blocks until the context is closed or an irrecoverable error is
// encountered.
func (w *prefixWatcher) Run(ctx context.Context) error {
if err := w.runOnceCheck(); err != nil {
return err
}
if err := w.validateParameters(); err != nil {
return err
}
logger := log.FromCtx(ctx).New("ctrl_addr", w.gateway.Control)
ctx = log.CtxWith(ctx, logger)
logger.Debug("Starting periodic IP prefix discovery")
defer logger.Debug("Stopped periodic IP prefix discovery")
ticker := time.NewTicker(w.PollInterval)
defer ticker.Stop()
w.run(ctx)
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
w.run(ctx)
}
}
}
func (w *prefixWatcher) run(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, w.PollTimeout)
defer cancel()
logger := log.FromCtx(ctx)
logger.Debug("Fetching IP prefixes from remote gateway")
prefixes, err := w.fetcher.Prefixes(ctx, w.gateway.Control)
if err != nil {
metrics.CounterInc(w.fetchErrors)
logger.Debug("Failed to fetch IP prefixes from remote gateway", "err", err)
return
}
logger.Debug("Fetched prefixes successfully", "prefixes", fmtPrefixes(prefixes))
snapshot := fmtPrefixes(prefixes)
if err := w.Consumer.Prefixes(w.remote, w.gateway, prefixes); err != nil {
logger.Error("Failed to process prefixes", "prefixes", fmtPrefixes(prefixes), "err", err)
}
w.stateMtx.Lock()
defer w.stateMtx.Unlock()
w.prefixes = snapshot
w.timestamp = time.Now()
}
func fmtPrefixes(prefixes []*net.IPNet) []string {
ret := []string{}
for _, p := range prefixes {
ret = append(ret, p.String())
}
return ret
}
func (w *prefixWatcher) runOnceCheck() error {
w.runMarkerLock.Lock()
defer w.runMarkerLock.Unlock()
if w.runMarker {
return ErrAlreadyRunning
}
w.runMarker = true
return nil
}
type diff struct {
Add []Gateway
Remove []Gateway
}
func computeDiff(prev, next []Gateway) diff {
return diff{
Add: subtract(next, prev),
Remove: subtract(prev, next),
}
}
// subtract subtracts all the gateways in b from the ones in a.
func subtract(a, b []Gateway) []Gateway {
set := map[string]Gateway{}
for _, gateway := range a {
set[fmt.Sprint(gateway)] = gateway
}
for _, gateway := range b {
delete(set, fmt.Sprint(gateway))
}
result := make([]Gateway, 0, len(set))
for _, gateway := range set {
result = append(result, gateway)
}
return result
}