-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexcludes.go
183 lines (155 loc) · 3.7 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
package splitrt
import (
"context"
"net"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
// excludesTimer is the timer for periodic exclude cleanup in seconds.
excludesTimer = 300
)
// exclude is a split excludes entry.
type exclude struct {
net *net.IPNet
static bool
ttl uint32
updated bool
}
// Excludes contains split Excludes.
type Excludes struct {
sync.Mutex
m map[string]*exclude
done chan struct{}
closed chan struct{}
}
// addFilter adds the exclude to netfilter.
func (e *Excludes) addFilter(ctx context.Context, exclude *exclude) {
log.WithField("address", exclude.net).Debug("SplitRouting adding exclude to netfilter")
addExclude(ctx, exclude.net)
}
// setFilter resets the excludes in netfilter.
func (e *Excludes) setFilter(ctx context.Context) {
log.Debug("SplitRouting resetting excludes in netfilter")
addresses := []*net.IPNet{}
for _, v := range e.m {
addresses = append(addresses, v.net)
}
setExcludes(ctx, addresses)
}
// add adds the exclude entry for ip to the split excludes.
func (e *Excludes) add(ctx context.Context, ip *net.IPNet, exclude *exclude) {
e.Lock()
defer e.Unlock()
key := ip.String()
old := e.m[key]
// new entry, just add it
if old == nil {
e.m[key] = exclude
e.addFilter(ctx, exclude)
return
}
// old entry exists, update values
if old.static {
// static entry is not updated
return
}
// update entry
old.static = exclude.static
old.ttl = exclude.ttl
old.updated = true
}
// AddStatic adds a static entry to the split excludes.
func (e *Excludes) AddStatic(ctx context.Context, address *net.IPNet) {
log.WithField("address", address).Debug("SplitRouting adding static exclude")
e.add(ctx, address, &exclude{
net: address,
static: true,
})
}
// AddDynamic adds a dynamic entry to the split excludes.
func (e *Excludes) AddDynamic(ctx context.Context, address *net.IPNet, ttl uint32) {
log.WithFields(log.Fields{
"address": address,
"ttl": ttl,
}).Debug("SplitRouting adding dynamic exclude")
e.add(ctx, address, &exclude{
net: address,
ttl: ttl,
updated: true,
})
}
// Remove removes an entry from the split excludes.
func (e *Excludes) Remove(ctx context.Context, address *net.IPNet) {
e.Lock()
defer e.Unlock()
delete(e.m, address.String())
e.setFilter(ctx)
}
// cleanup cleans up the split excludes.
func (e *Excludes) cleanup(ctx context.Context) {
e.Lock()
defer e.Unlock()
changed := false
for k, v := range e.m {
// skip static entries
if v.static {
continue
}
// skip recently updated entries
if v.updated {
v.updated = false
continue
}
// exclude expired entries
if v.ttl < excludesTimer {
delete(e.m, 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")
}
// NewExcludes returns new split excludes.
func NewExcludes() *Excludes {
return &Excludes{
m: make(map[string]*exclude),
done: make(chan struct{}),
closed: make(chan struct{}),
}
}