-
Notifications
You must be signed in to change notification settings - Fork 51
/
servicecache.go
292 lines (240 loc) · 6.21 KB
/
servicecache.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
package servicecache
import (
"fmt"
"net"
"sync"
"go.aporeto.io/enforcerd/trireme-lib/common"
"go.aporeto.io/enforcerd/trireme-lib/utils/ipprefix"
"go.aporeto.io/enforcerd/trireme-lib/utils/portspec"
)
type entry struct {
id string
ports *portspec.PortSpec
data interface{}
}
type entryList []*entry
func (e entryList) Delete(i int) entryList {
if i >= len(e) || i < 0 {
return e
}
return append(e[:i], e[i+1:]...)
}
// ServiceCache is a new service cache
type ServiceCache struct {
// ipprefixs is map[prefixlength][prefix] -> array of entries indexed by port
local ipprefix.IPcache
remote ipprefix.IPcache
// hostcaches is map[host] -> array of entries indexed by port.
remoteHosts map[string]entryList
localHosts map[string]entryList
// portCaches is list of all ports where we can retrieve a service based on the port.
remotePorts entryList
localPorts entryList
sync.RWMutex
}
// NewTable creates a new table
func NewTable() *ServiceCache {
return &ServiceCache{
local: ipprefix.NewIPCache(),
remote: ipprefix.NewIPCache(),
remoteHosts: map[string]entryList{},
localHosts: map[string]entryList{},
}
}
// Add adds a service into the cache. Returns error of if any overlap has been detected.
func (s *ServiceCache) Add(e *common.Service, id string, data interface{}, local bool) error {
s.Lock()
defer s.Unlock()
record := &entry{
ports: e.Ports,
data: data,
id: id,
}
if err := s.addPorts(e, record, local); err != nil {
return err
}
if err := s.addHostService(e, record, local); err != nil {
return err
}
return s.addIPService(e, record, local)
}
// Find searches for a matching service, given an IP and port. Caller must specify
// the local or remote context.
func (s *ServiceCache) Find(ip net.IP, port int, host string, local bool) interface{} {
s.RLock()
defer s.RUnlock()
if host != "" {
if data := s.findHost(host, port, local); data != nil {
return data
}
}
return s.findIP(ip, port, local)
}
// FindListeningServicesForPU returns a service that is found and the associated
// portSpecifications that refer to this service.
func (s *ServiceCache) FindListeningServicesForPU(id string) (interface{}, *portspec.PortSpec) {
s.RLock()
defer s.RUnlock()
for _, spec := range s.localPorts {
if spec.id == id {
return spec.data, spec.ports
}
}
return nil, nil
}
// DeleteByID will delete all entries related to this ID from all references.
func (s *ServiceCache) DeleteByID(id string, local bool) {
s.Lock()
defer s.Unlock()
hosts := s.remoteHosts
cache := s.remote
if local {
hosts = s.localHosts
cache = s.local
}
if local {
s.localPorts = deleteMatchingPorts(s.localPorts, id)
} else {
s.remotePorts = deleteMatchingPorts(s.remotePorts, id)
}
for host, ports := range hosts {
hosts[host] = deleteMatchingPorts(ports, id)
if len(hosts[host]) == 0 {
delete(hosts, host)
}
}
deleteMatching := func(val interface{}) interface{} {
if val == nil {
return nil
}
entryL := val.(entryList)
r := deleteMatchingPorts(entryL, id)
if len(r) == 0 {
return nil
}
return r
}
cache.RunFuncOnVals(deleteMatching)
}
func deleteMatchingPorts(list entryList, id string) entryList {
remainingPorts := entryList{}
for _, spec := range list {
if spec.id != id {
remainingPorts = append(remainingPorts, spec)
}
}
return remainingPorts
}
func (s *ServiceCache) addIPService(e *common.Service, record *entry, local bool) error {
cache := s.remote
if local {
cache = s.local
}
addresses := e.Addresses
// If addresses are nil, I only care about ports.
if len(e.Addresses) == 0 && len(e.FQDNs) == 0 {
addresses = map[string]struct{}{}
addresses["0.0.0.0/0"] = struct{}{}
addresses["::/0"] = struct{}{}
}
for addrS := range addresses {
var records entryList
var addr *net.IPNet
var err error
if _, addr, err = net.ParseCIDR(addrS); err != nil {
continue
}
mask, _ := addr.Mask.Size()
v, exists := cache.Get(addr.IP, mask)
if !exists {
records = entryList{}
} else {
records = v.(entryList)
for _, spec := range records {
if spec.ports.Overlaps(e.Ports) {
return fmt.Errorf("service port overlap for a given IP not allowed: ip %s, port %s", addr.String(), e.Ports.String())
}
}
}
records = append(records, record)
cache.Put(addr.IP, mask, records)
}
return nil
}
func (s *ServiceCache) addHostService(e *common.Service, record *entry, local bool) error {
hostCache := s.remoteHosts
if local {
hostCache = s.localHosts
}
// If addresses are nil, I only care about ports.
if len(e.FQDNs) == 0 {
return nil
}
for _, host := range e.FQDNs {
if _, ok := hostCache[host]; !ok {
hostCache[host] = entryList{}
}
for _, spec := range hostCache[host] {
if spec.ports.Overlaps(e.Ports) {
return fmt.Errorf("service port overlap for a given host not allowed: host %s, port %s", host, e.Ports.String())
}
}
hostCache[host] = append(hostCache[host], record)
}
return nil
}
// findIP searches for a matching service, given an IP and port
func (s *ServiceCache) findIP(ip net.IP, port int, local bool) interface{} {
cache := s.remote
if local {
cache = s.local
}
if ip == nil {
return nil
}
var data interface{}
findMatch := func(val interface{}) bool {
if val != nil {
records := val.(entryList)
for _, e := range records {
if e.ports.IsIncluded(port) {
data = e.data
return true
}
}
}
return false
}
cache.RunFuncOnLpmIP(ip, findMatch)
return data
}
// findIP searches for a matching service, given an IP and port
func (s *ServiceCache) findHost(host string, port int, local bool) interface{} {
hostCache := s.remoteHosts
if local {
hostCache = s.localHosts
}
entries, ok := hostCache[host]
if !ok {
return nil
}
for _, e := range entries {
if e.ports.IsIncluded(port) {
return e.data
}
}
return nil
}
// addPorts will only work for local ports.
func (s *ServiceCache) addPorts(e *common.Service, record *entry, local bool) error {
if !local {
return nil
}
for _, spec := range s.localPorts {
if spec.ports.Overlaps(e.Ports) {
return fmt.Errorf("service port overlap in the global port list: %+v %s", e.Addresses, e.Ports.String())
}
}
s.localPorts = append(s.localPorts, record)
return nil
}