-
Notifications
You must be signed in to change notification settings - Fork 51
/
lookup.go
305 lines (245 loc) · 7.54 KB
/
lookup.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
package lookup
import (
"errors"
"fmt"
"sort"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
// ForwardingPolicy is an instance of the forwarding policy
type ForwardingPolicy struct {
tags []policy.KeyValueOperator
count int
index int
actions interface{}
}
// intList is a list of integeres
type intList []int
//PolicyDB is the structure of a policy
type PolicyDB struct {
// rules []policy
numberOfPolicies int
equalPrefixes map[string]intList
equalMapTable map[string]map[string][]*ForwardingPolicy
equalIDMapTable map[string][]*ForwardingPolicy
notEqualMapTable map[string]map[string][]*ForwardingPolicy
notStarTable map[string][]*ForwardingPolicy
defaultNotExistsPolicy *ForwardingPolicy
}
//NewPolicyDB creates a new PolicyDB for efficient search of policies
func NewPolicyDB() (m *PolicyDB) {
m = &PolicyDB{
numberOfPolicies: 0,
equalPrefixes: map[string]intList{},
equalMapTable: map[string]map[string][]*ForwardingPolicy{},
equalIDMapTable: map[string][]*ForwardingPolicy{},
notEqualMapTable: map[string]map[string][]*ForwardingPolicy{},
notStarTable: map[string][]*ForwardingPolicy{},
defaultNotExistsPolicy: nil,
}
return m
}
func (array intList) sortedInsert(value int) intList {
l := len(array)
if l == 0 {
array = append(array, value)
return array
}
i := sort.Search(l, func(i int) bool {
return array[i] <= value
})
if i == 0 { // new value is the largest
array = append([]int{value}, array...)
return array
}
if i == l-1 { // new value is the smallest
array = append(array, value)
return array
}
inserted := append(array[0:i], value)
return append(inserted, array[i:]...)
}
//AddPolicy adds a policy to the database
func (m *PolicyDB) AddPolicy(selector policy.TagSelector) (policyID int) {
// Create a new policy object
e := ForwardingPolicy{
count: 0,
tags: selector.Clause,
actions: selector.Policy,
}
// For each tag of the incoming policy add a mapping between the map tables
// and the structure that represents the policy
for _, keyValueOp := range selector.Clause {
switch keyValueOp.Operator {
case policy.KeyExists:
m.equalPrefixes[keyValueOp.Key] = m.equalPrefixes[keyValueOp.Key].sortedInsert(0)
if _, ok := m.equalMapTable[keyValueOp.Key]; !ok {
m.equalMapTable[keyValueOp.Key] = map[string][]*ForwardingPolicy{}
}
m.equalMapTable[keyValueOp.Key][""] = append(m.equalMapTable[keyValueOp.Key][""], &e)
e.count++
case policy.KeyNotExists:
m.notStarTable[keyValueOp.Key] = append(m.notStarTable[keyValueOp.Key], &e)
if len(selector.Clause) == 1 {
m.defaultNotExistsPolicy = &e
}
case policy.Equal:
if _, ok := m.equalMapTable[keyValueOp.Key]; !ok {
m.equalMapTable[keyValueOp.Key] = map[string][]*ForwardingPolicy{}
}
for _, v := range keyValueOp.Value {
if end := len(v) - 1; v[end] == '*' {
m.equalPrefixes[keyValueOp.Key] = m.equalPrefixes[keyValueOp.Key].sortedInsert(end)
m.equalMapTable[keyValueOp.Key][v[:end]] = append(m.equalMapTable[keyValueOp.Key][v[:end]], &e)
} else {
m.equalMapTable[keyValueOp.Key][v] = append(m.equalMapTable[keyValueOp.Key][v], &e)
}
}
if keyValueOp.ID != "" {
if _, ok := m.equalIDMapTable[keyValueOp.ID]; !ok {
m.equalIDMapTable[keyValueOp.ID] = []*ForwardingPolicy{}
}
m.equalIDMapTable[keyValueOp.ID] = append(m.equalIDMapTable[keyValueOp.ID], &e)
}
e.count++
default: // policy.NotEqual
if _, ok := m.notEqualMapTable[keyValueOp.Key]; !ok {
m.notEqualMapTable[keyValueOp.Key] = map[string][]*ForwardingPolicy{}
}
for _, v := range keyValueOp.Value {
m.notEqualMapTable[keyValueOp.Key][v] = append(m.notEqualMapTable[keyValueOp.Key][v], &e)
e.count++
}
}
}
// Increase the number of policies
m.numberOfPolicies++
// Give the policy an index
e.index = m.numberOfPolicies
// Return the ID
return e.index
}
var (
errInvalidTag = errors.New("tag must be k=v")
)
// Custom implementation for splitting strings. Gives significant performance
// improvement. Do not allocate new strings
func (m *PolicyDB) tagSplit(tag string, k *string, v *string) error {
l := len(tag)
if l < 3 {
return errInvalidTag
}
if tag[0] == '=' {
return errInvalidTag
}
for i := 0; i < l; i++ {
if tag[i] == '=' {
if i+1 >= l {
return errInvalidTag
}
*k = tag[:i]
*v = tag[i+1:]
return nil
}
}
return errInvalidTag
}
// Search searches for a set of tags in the database to find a policy match
func (m *PolicyDB) Search(tags *policy.TagStore) (int, interface{}) {
count := make([]int, m.numberOfPolicies+1)
skip := make([]bool, m.numberOfPolicies+1)
// Disable all policies that fail the not key exists
copiedTags := tags.GetSlice()
var k, v string
for _, t := range copiedTags {
if err := m.tagSplit(t, &k, &v); err != nil {
continue
}
for _, policy := range m.notStarTable[k] {
skip[policy.index] = true
}
}
// Go through the list of tags
for _, t := range copiedTags {
// Search for matches of t (tag id)
if index, action := searchInMapTable(m.equalIDMapTable[t], count, skip); index >= 0 {
return index, action
}
if err := m.tagSplit(t, &k, &v); err != nil {
continue
}
// Search for matches of k=v
if index, action := searchInMapTable(m.equalMapTable[k][v], count, skip); index >= 0 {
return index, action
}
// Search for matches in prefixes
for _, i := range m.equalPrefixes[k] {
if i <= len(v) {
if index, action := searchInMapTable(m.equalMapTable[k][v[:i]], count, skip); index >= 0 {
return index, action
}
}
}
// Parse all of the policies that have a key that matches the incoming tag key
// and a not equal operator and that has a not match rule
for value, policies := range m.notEqualMapTable[k] {
if v == value {
continue
}
if index, action := searchInMapTable(policies, count, skip); index >= 0 {
return index, action
}
}
}
if m.defaultNotExistsPolicy != nil && !skip[m.defaultNotExistsPolicy.index] {
return m.defaultNotExistsPolicy.index, m.defaultNotExistsPolicy.actions
}
return -1, nil
}
func searchInMapTable(table []*ForwardingPolicy, count []int, skip []bool) (int, interface{}) {
for _, policy := range table {
// Skip the policy if we have marked it
if skip[policy.index] {
continue
}
// Since a policy is hit, the count of remaining tags is reduced by one
count[policy.index]++
// If all tags of the policy have been hit, there is a match
if count[policy.index] == policy.count {
return policy.index, policy.actions
}
}
return -1, nil
}
// PrintPolicyDB is a debugging function to dump the map
func (m *PolicyDB) PrintPolicyDB() {
zap.L().Debug("Print Policy DB: equal table")
for key, values := range m.equalMapTable {
for value, policies := range values {
zap.L().Debug("Print Policy DB",
zap.String("policies", fmt.Sprintf("%#v", policies)),
zap.String("key", key),
zap.String("value", value),
)
}
}
zap.L().Debug("Print Policy DB: equal id table")
for key, values := range m.equalIDMapTable {
for _, policies := range values {
zap.L().Debug("Print Policy DB",
zap.String("policies", fmt.Sprintf("%#v", policies)),
zap.String("key", key),
)
}
}
zap.L().Debug("Print Policy DB - not equal table")
for key, values := range m.notEqualMapTable {
for value, policies := range values {
zap.L().Debug("Print Policy DB",
zap.String("policies", fmt.Sprintf("%#v", policies)),
zap.String("key", key),
zap.String("value", value),
)
}
}
}