-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexcludes.go
223 lines (188 loc) · 4.49 KB
/
excludes.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
package splitrt
import (
"context"
"net/netip"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
// excludesTimer is the timer for periodic exclude cleanup in seconds.
excludesTimer = 300
)
// dynExclude is a dynamic split excludes entry.
type dynExclude struct {
ttl uint32
updated bool
}
// Excludes contains split Excludes.
type Excludes struct {
sync.Mutex
s map[string]netip.Prefix
d map[netip.Addr]*dynExclude
done chan struct{}
closed chan struct{}
}
// setFilter resets the excludes in netfilter.
func (e *Excludes) setFilter(ctx context.Context) {
log.Debug("SplitRouting resetting excludes in netfilter")
addresses := []netip.Prefix{}
for _, v := range e.s {
addresses = append(addresses, v)
}
for k := range e.d {
prefix := netip.PrefixFrom(k, k.BitLen())
addresses = append(addresses, prefix)
}
setExcludes(ctx, addresses)
}
// AddStatic adds a static entry to the split excludes.
func (e *Excludes) AddStatic(ctx context.Context, address netip.Prefix) {
log.WithField("address", address).Debug("SplitRouting adding static exclude")
e.Lock()
defer e.Unlock()
// make sure new prefix in address does not overlap with existing
// prefixes in static excludes
removed := false
for k, v := range e.s {
if !v.Overlaps(address) {
// no overlap
continue
}
if v.Bits() <= address.Bits() {
// new prefix is already in existing prefix,
// do not add it
return
}
// new prefix contains old prefix, remove old prefix
delete(e.s, k)
removed = true
}
// add new prefix to static excludes
key := address.String()
e.s[key] = address
// add to netfilter
if removed {
// existing entries removed, we need to reset all excludes
e.setFilter(ctx)
return
}
// single new entry, add it
addExclude(ctx, address)
}
// AddDynamic adds a dynamic entry to the split excludes.
func (e *Excludes) AddDynamic(ctx context.Context, address netip.Prefix, ttl uint32) {
log.WithFields(log.Fields{
"address": address,
"ttl": ttl,
}).Debug("SplitRouting adding dynamic exclude")
if !address.IsSingleIP() {
log.Error("SplitRouting error adding dynamic exclude with multiple IPs")
return
}
a := address.Addr()
e.Lock()
defer e.Unlock()
// make sure new ip address is not in existing static excludes
for _, v := range e.s {
if v.Contains(a) {
return
}
}
// update existing entry in dynamic excludes
old := e.d[a]
if old != nil {
old.ttl = ttl
old.updated = true
return
}
// create new entry in dynamic excludes
e.d[a] = &dynExclude{
ttl: ttl,
updated: true,
}
// add to netfilter
addExclude(ctx, address)
}
// RemoveStatic removes a static entry from the split excludes.
func (e *Excludes) RemoveStatic(ctx context.Context, address netip.Prefix) {
e.Lock()
defer e.Unlock()
delete(e.s, address.String())
e.setFilter(ctx)
}
// cleanup cleans up the dynamic split excludes.
func (e *Excludes) cleanup(ctx context.Context) {
e.Lock()
defer e.Unlock()
changed := false
for k, v := range e.d {
// skip recently updated entries
if v.updated {
v.updated = false
continue
}
// exclude expired entries
if v.ttl < excludesTimer {
delete(e.d, k)
changed = true
continue
}
// reduce ttl
v.ttl -= excludesTimer
}
// if entries were changed, reset netfilter
if changed {
e.setFilter(ctx)
}
}
// start starts periodic cleanup of the split excludes.
func (e *Excludes) start() {
defer close(e.closed)
ctx := context.Background()
timer := time.NewTimer(excludesTimer * time.Second)
for {
select {
case <-timer.C:
e.cleanup(ctx)
timer.Reset(excludesTimer * time.Second)
case <-e.done:
if !timer.Stop() {
<-timer.C
}
return
}
}
}
// Start starts periodic cleanup of the split excludes.
func (e *Excludes) Start() {
log.Debug("SplitRouting starting periodic cleanup of excludes")
go e.start()
}
// Stop stops periodic cleanup of the split excludes.
func (e *Excludes) Stop() {
close(e.done)
<-e.closed
log.Debug("SplitRouting stopped periodic cleanup of excludes")
}
// List returns the list of static and dynamic excludes.
func (e *Excludes) List() (static, dynamic []string) {
e.Lock()
defer e.Unlock()
for k := range e.s {
static = append(static, k)
}
for k := range e.d {
dynamic = append(dynamic, k.String())
}
return
}
// NewExcludes returns new split excludes.
func NewExcludes() *Excludes {
return &Excludes{
s: make(map[string]netip.Prefix),
d: make(map[netip.Addr]*dynExclude),
done: make(chan struct{}),
closed: make(chan struct{}),
}
}