forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fake_ovs.go
459 lines (404 loc) · 11.7 KB
/
fake_ovs.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package ovs
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
)
// ovsFake implements a fake ovs.Interface for testing purposes
//
// Note that the code here is *not* expected to be 100% equivalent to ovsExec, as
// that would require porting over huge amounts of ovs-ofctl source code. It needs
// to support enough features to make the SDN unit tests pass, and should do enough
// error checking to catch bugs that have tripped us up in the past (eg,
// specifying "nw_dst" without "ip").
type ovsFake struct {
bridge string
ports map[string]int
flows []ovsFlow
}
// ovsFlow represents an OVS flow
type ovsFlow struct {
table int
priority int
created time.Time
cookie string
fields []ovsField
actions string
}
type ovsField struct {
name string
value string
}
const (
minPriority = 0
defaultPriority = 32768
maxPriority = 65535
)
// NewFake returns a new ovs.Interface
func NewFake(bridge string) Interface {
return &ovsFake{bridge: bridge}
}
func (fake *ovsFake) AddBridge(properties ...string) error {
fake.ports = make(map[string]int)
fake.flows = make([]ovsFlow, 0)
return nil
}
func (fake *ovsFake) DeleteBridge() error {
fake.ports = nil
fake.flows = nil
return nil
}
func (fake *ovsFake) ensureExists() error {
if fake.ports == nil {
return fmt.Errorf("no bridge named %s", fake.bridge)
}
return nil
}
func (fake *ovsFake) GetOFPort(port string) (int, error) {
if err := fake.ensureExists(); err != nil {
return -1, err
}
if ofport, exists := fake.ports[port]; exists {
return ofport, nil
} else {
return -1, fmt.Errorf("no row %q in table Interface", port)
}
}
func (fake *ovsFake) AddPort(port string, ofportRequest int, properties ...string) (int, error) {
if err := fake.ensureExists(); err != nil {
return -1, err
}
ofport, exists := fake.ports[port]
if exists {
if ofport != ofportRequest && ofportRequest != -1 {
return -1, fmt.Errorf("allocated ofport (%d) did not match request (%d)", ofport, ofportRequest)
}
} else {
if ofportRequest == -1 {
ofport := 1
for _, existingPort := range fake.ports {
if existingPort >= ofport {
ofport = existingPort + 1
}
}
} else {
if ofportRequest < 1 || ofportRequest > 65535 {
return -1, fmt.Errorf("requested ofport (%d) out of range", ofportRequest)
}
ofport = ofportRequest
}
fake.ports[port] = ofport
}
return ofport, nil
}
func (fake *ovsFake) DeletePort(port string) error {
if err := fake.ensureExists(); err != nil {
return err
}
delete(fake.ports, port)
return nil
}
func (fake *ovsFake) SetFrags(mode string) error {
return nil
}
func (ovsif *ovsFake) Create(table string, values ...string) (string, error) {
return "fake-UUID", nil
}
func (fake *ovsFake) Destroy(table, record string) error {
return nil
}
func (fake *ovsFake) Get(table, record, column string) (string, error) {
return "", nil
}
func (fake *ovsFake) Set(table, record string, values ...string) error {
return nil
}
func (fake *ovsFake) Clear(table, record string, columns ...string) error {
return nil
}
type ovsFakeTx struct {
fake *ovsFake
err error
}
func (fake *ovsFake) NewTransaction() Transaction {
return &ovsFakeTx{fake: fake, err: fake.ensureExists()}
}
type parseCmd string
const (
parseForAdd parseCmd = "add-flow"
parseForDelete parseCmd = "del-flows"
)
func fieldSet(parsed *ovsFlow, field string) bool {
for _, f := range parsed.fields {
if f.name == field {
return true
}
}
return false
}
func checkNotAllowedField(flow string, parsed *ovsFlow, field string, cmd parseCmd) error {
if fieldSet(parsed, field) {
return fmt.Errorf("bad flow %q (field %q not allowed in %s)", flow, field, cmd)
}
return nil
}
func checkUnsupportedField(flow string, parsed *ovsFlow, field string) error {
if fieldSet(parsed, field) {
return fmt.Errorf("bad flow %q (field %q not supported)", flow, field)
}
return nil
}
func parseFlow(cmd parseCmd, flow string, args ...interface{}) (*ovsFlow, error) {
if len(args) > 0 {
flow = fmt.Sprintf(flow, args...)
}
// According to the man page, "flow descriptions comprise a series of field=value
// assignments, separated by commas or white space." However, you can also have
// fields with no value (eg, "ip"), and the "actions" field can also have internal
// commas, whitespace, and equals signs (but if it is present, it must be the
// last field specified).
parsed := &ovsFlow{
table: 0,
priority: defaultPriority,
fields: make([]ovsField, 0),
created: time.Now(),
cookie: "0",
}
flow = strings.Trim(flow, " ")
origFlow := flow
for flow != "" {
field := ""
value := ""
end := strings.IndexAny(flow, ", ")
if end == -1 {
end = len(flow)
}
eq := strings.Index(flow, "=")
if eq == -1 || eq > end {
field = flow[:end]
} else {
field = flow[:eq]
if field == "actions" {
end = len(flow)
value = flow[eq+1:]
} else {
valueEnd := end - 1
for flow[valueEnd] == ' ' || flow[valueEnd] == ',' {
valueEnd--
}
value = strings.Trim(flow[eq+1:end], ", ")
}
if value == "" {
return nil, fmt.Errorf("bad flow definition %q (empty field %q)", origFlow, field)
}
}
switch field {
case "table":
table, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("bad flow %q (bad table number %q)", origFlow, value)
} else if table < 0 || table > 255 {
return nil, fmt.Errorf("bad flow %q (table number %q out of range)", origFlow, value)
}
parsed.table = table
case "priority":
priority, err := strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("bad flow %q (bad priority %q)", origFlow, value)
} else if priority < minPriority || priority > maxPriority {
return nil, fmt.Errorf("bad flow %q (priority %q out of range)", origFlow, value)
}
parsed.priority = priority
case "actions":
parsed.actions = value
case "cookie":
parsed.cookie = value
default:
parsed.fields = append(parsed.fields, ovsField{field, value})
}
for end < len(flow) && (flow[end] == ',' || flow[end] == ' ') {
end++
}
flow = flow[end:]
}
// Sanity-checking
switch cmd {
case parseForAdd:
if err := checkNotAllowedField(flow, parsed, "out_port", cmd); err != nil {
return nil, err
}
if err := checkNotAllowedField(flow, parsed, "out_group", cmd); err != nil {
return nil, err
}
if parsed.actions == "" {
return nil, fmt.Errorf("bad flow %q (empty actions)", flow)
}
case parseForDelete:
if err := checkNotAllowedField(flow, parsed, "priority", cmd); err != nil {
return nil, err
}
if err := checkNotAllowedField(flow, parsed, "actions", cmd); err != nil {
return nil, err
}
if err := checkUnsupportedField(flow, parsed, "out_port"); err != nil {
return nil, err
}
if err := checkUnsupportedField(flow, parsed, "out_group"); err != nil {
return nil, err
}
}
if (fieldSet(parsed, "nw_src") || fieldSet(parsed, "nw_dst")) &&
!(fieldSet(parsed, "ip") || fieldSet(parsed, "arp") || fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified nw_src/nw_dst without ip/arp/tcp/udp)", flow)
}
if (fieldSet(parsed, "arp_spa") || fieldSet(parsed, "arp_tpa") || fieldSet(parsed, "arp_sha") || fieldSet(parsed, "arp_tha")) && !fieldSet(parsed, "arp") {
return nil, fmt.Errorf("bad flow %q (specified arp_spa/arp_tpa/arp_sha/arp_tpa without arp)", flow)
}
if (fieldSet(parsed, "tcp_src") || fieldSet(parsed, "tcp_dst")) && !fieldSet(parsed, "tcp") {
return nil, fmt.Errorf("bad flow %q (specified tcp_src/tcp_dst without tcp)", flow)
}
if (fieldSet(parsed, "udp_src") || fieldSet(parsed, "udp_dst")) && !fieldSet(parsed, "udp") {
return nil, fmt.Errorf("bad flow %q (specified udp_src/udp_dst without udp)", flow)
}
if (fieldSet(parsed, "tp_src") || fieldSet(parsed, "tp_dst")) && !(fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified tp_src/tp_dst without tcp/udp)", flow)
}
if fieldSet(parsed, "ip_frag") && (fieldSet(parsed, "tcp") || fieldSet(parsed, "udp")) {
return nil, fmt.Errorf("bad flow %q (specified ip_frag with tcp/udp)", flow)
}
return parsed, nil
}
// flowMatches tests if flow matches match. If exact is true, then the table, priority,
// and all fields much match. If exact is false, then the table and any fields specified
// in match must match, but priority is not checked, and there can be additional fields
// in flow that are not in match.
func flowMatches(flow, match *ovsFlow, exact bool) bool {
if flow.table != match.table && (exact || match.table != 0) {
return false
}
if exact && flow.priority != match.priority {
return false
}
if exact && len(flow.fields) != len(match.fields) {
return false
}
if match.cookie != "" && !fieldMatches(flow.cookie, match.cookie, exact) {
return false
}
for _, matchField := range match.fields {
var flowValue *string
for _, flowField := range flow.fields {
if flowField.name == matchField.name {
flowValue = &flowField.value
break
}
}
if flowValue == nil || !fieldMatches(*flowValue, matchField.value, exact) {
return false
}
}
return true
}
func fieldMatches(val, match string, exact bool) bool {
if val == match {
return true
}
if exact {
return false
}
// Handle bitfield/mask matches. (Some other syntax like "10.128.0.0/14" might
// get examined here, but it will fail the first ParseUint call and so not
// reach the final check.)
split := strings.Split(match, "/")
if len(split) == 2 {
matchNum, err1 := strconv.ParseUint(split[0], 0, 32)
mask, err2 := strconv.ParseUint(split[1], 0, 32)
valNum, err3 := strconv.ParseUint(val, 0, 32)
if err1 == nil && err2 == nil && err3 == nil {
if (matchNum & mask) == (valNum & mask) {
return true
}
}
}
return false
}
// sort.Interface support
type ovsFlows []ovsFlow
func (f ovsFlows) Len() int { return len(f) }
func (f ovsFlows) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ovsFlows) Less(i, j int) bool {
if f[i].table != f[j].table {
return f[i].table < f[j].table
}
if f[i].priority != f[j].priority {
return f[i].priority > f[j].priority
}
return f[i].created.Before(f[j].created)
}
func (tx *ovsFakeTx) AddFlow(flow string, args ...interface{}) {
if tx.err != nil {
return
}
parsed, err := parseFlow(parseForAdd, flow, args...)
if err != nil {
tx.err = err
return
}
// If there is already an exact match for this flow, then the new flow replaces it.
for i := range tx.fake.flows {
if flowMatches(&tx.fake.flows[i], parsed, true) {
tx.fake.flows[i] = *parsed
return
}
}
tx.fake.flows = append(tx.fake.flows, *parsed)
sort.Sort(ovsFlows(tx.fake.flows))
}
func (tx *ovsFakeTx) DeleteFlows(flow string, args ...interface{}) {
if tx.err != nil {
return
}
parsed, err := parseFlow(parseForDelete, flow, args...)
if err != nil {
tx.err = err
return
}
newFlows := make([]ovsFlow, 0, len(tx.fake.flows))
for _, flow := range tx.fake.flows {
if !flowMatches(&flow, parsed, false) {
newFlows = append(newFlows, flow)
}
}
tx.fake.flows = newFlows
}
func (tx *ovsFakeTx) EndTransaction() error {
err := tx.err
tx.err = nil
return err
}
func (fake *ovsFake) DumpFlows() ([]string, error) {
if err := fake.ensureExists(); err != nil {
return nil, err
}
flows := make([]string, 0, len(fake.flows))
for _, flow := range fake.flows {
str := fmt.Sprintf(" cookie=%s, table=%d", flow.cookie, flow.table)
if flow.priority != defaultPriority {
str += fmt.Sprintf(", priority=%d", flow.priority)
}
for _, field := range flow.fields {
if field.value == "" {
str += fmt.Sprintf(", %s", field.name)
} else {
str += fmt.Sprintf(", %s=%s", field.name, field.value)
}
}
if flow.actions != "" {
str += fmt.Sprintf(", actions=%s", flow.actions)
}
flows = append(flows, str)
}
return flows, nil
}