-
Notifications
You must be signed in to change notification settings - Fork 155
/
util.go
130 lines (114 loc) · 3.15 KB
/
util.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
// Copyright 2019 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 (
"context"
"sort"
"strconv"
"strings"
"sync"
"github.com/scionproto/scion/control/ifstate"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/log"
seg "github.com/scionproto/scion/pkg/segment"
"github.com/scionproto/scion/private/topology"
)
// sortedIntfs returns all interfaces of the given link type sorted by interface
// ID.
func sortedIntfs(intfs *ifstate.Interfaces, linkType topology.LinkType) []uint16 {
var result []uint16
for ifid, intf := range intfs.All() {
topoInfo := intf.TopoInfo()
if topoInfo.LinkType != linkType {
continue
}
result = append(result, ifid)
}
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
return result
}
type summary struct {
mu sync.Mutex
srcs map[addr.IA]struct{}
ifIds map[uint16]struct{}
count int
}
func newSummary() *summary {
return &summary{
srcs: make(map[addr.IA]struct{}),
ifIds: make(map[uint16]struct{}),
}
}
func (s *summary) AddSrc(ia addr.IA) {
s.mu.Lock()
defer s.mu.Unlock()
s.srcs[ia] = struct{}{}
}
func (s *summary) AddIfid(ifid uint16) {
s.mu.Lock()
defer s.mu.Unlock()
s.ifIds[ifid] = struct{}{}
}
func (s *summary) Inc() {
s.mu.Lock()
defer s.mu.Unlock()
s.count++
}
func (s *summary) IfIds() []uint16 {
s.mu.Lock()
defer s.mu.Unlock()
list := make([]uint16, 0, len(s.ifIds))
for ifId := range s.ifIds {
list = append(list, ifId)
}
sort.Slice(list, func(i, j int) bool { return list[i] < list[j] })
return list
}
// hopDescription creates a human readable description of a AS entry list by
// describing the hops only.
func hopsDescription(entries []seg.ASEntry) string {
var desc strings.Builder
for _, entry := range entries {
hop := entry.HopEntry.HopField
if hop.ConsIngress != 0 {
desc.WriteString(strconv.Itoa(int(hop.ConsIngress)))
desc.WriteString(" ")
}
desc.WriteString(entry.Local.String())
if hop.ConsEgress != 0 {
desc.WriteString(" ")
desc.WriteString(strconv.Itoa(int(hop.ConsIngress)))
desc.WriteString(">")
}
}
return desc.String()
}
// withSilent creates a logger based on the logger in the context that only logs
// at debug level if silent is set. Otherwise, the logger in the context is
// returned.
func withSilent(ctx context.Context, silent bool) log.Logger {
if silent {
return silentLogger{Logger: log.FromCtx(ctx)}
}
return log.FromCtx(ctx)
}
type silentLogger struct {
log.Logger
}
func (s silentLogger) Info(msg string, ctx ...interface{}) {
s.Logger.Debug(msg, ctx...)
}
func (s silentLogger) Error(msg string, ctx ...interface{}) {
s.Logger.Debug(msg, ctx...)
}