forked from digitalocean/go-openvswitch
/
flow.go
492 lines (421 loc) · 11.3 KB
/
flow.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright 2017 DigitalOcean.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ovs
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
// PortLOCAL is a special in_port value which refers to the local port
// of an OVS bridge.
PortLOCAL = -1
)
// Possible errors which may be encountered while marshaling or unmarshaling
// a Flow.
var (
errActionsWithDrop = errors.New("Flow actions include drop, but multiple actions specified")
errInvalidActions = errors.New("invalid actions for Flow")
errNoActions = errors.New("no actions defined for Flow")
errNotEnoughElements = errors.New("not enough elements for valid Flow")
errInvalidLearnedActions = errors.New("invalid actions for LearnedFlow")
)
// A Protocol is an OpenFlow protocol designation accepted by Open vSwitch.
type Protocol string
// Protocol constants which can be used in OVS flow configurations.
const (
ProtocolARP Protocol = "arp"
ProtocolICMPv4 Protocol = "icmp"
ProtocolICMPv6 Protocol = "icmp6"
ProtocolIPv4 Protocol = "ip"
ProtocolIPv6 Protocol = "ipv6"
ProtocolTCPv4 Protocol = "tcp"
ProtocolTCPv6 Protocol = "tcp6"
ProtocolUDPv4 Protocol = "udp"
ProtocolUDPv6 Protocol = "udp6"
)
// A Flow is an OpenFlow flow meant for adding flows to a software bridge. It can be marshaled
// to and from its textual form for use with Open vSwitch.
type Flow struct {
Priority int
Protocol Protocol
InPort int
Matches []Match
Table int
IdleTimeout int
Cookie uint64
Actions []Action
}
// A LearnedFlow is defined as part of the Learn action.
type LearnedFlow struct {
Priority int
InPort int
Matches []Match
Table int
IdleTimeout int
Cookie uint64
Actions []Action
DeleteLearned bool
FinHardTimeout int
HardTimeout int
Limit int
}
var _ error = &FlowError{}
// A FlowError is an error encountered while marshaling or unmarshaling
// a Flow.
type FlowError struct {
// Str indicates the string, if any, that caused the flow to
// fail while unmarshaling.
Str string
// Err indicates the error that halted flow marshaling or unmarshaling.
Err error
}
// Error returns the string representation of a FlowError.
func (e *FlowError) Error() string {
if e.Str == "" {
return e.Err.Error()
}
return fmt.Sprintf("flow error due to string %q: %v",
e.Str, e.Err)
}
// Constants and variables used repeatedly to reduce errors in code.
const (
priorityString = "priority="
priority = "priority"
cookie = "cookie"
keyActions = "actions"
idleTimeout = "idle_timeout"
inPort = "in_port"
table = "table"
duration = "duration"
nPackets = "n_packets"
nBytes = "n_bytes"
hardAge = "hard_age"
idleAge = "idle_age"
// Variables used in LearnedFlows only.
deleteLearned = "delete_learned"
finHardTimeout = "fin_hard_timeout"
hardTimeout = "hard_timeout"
limit = "limit"
portLOCAL = "LOCAL"
)
var (
priorityBytes = []byte(priorityString)
)
// MarshalText marshals a Flow into its textual form.
func (f *Flow) MarshalText() ([]byte, error) {
if len(f.Actions) == 0 {
return nil, &FlowError{
Err: errNoActions,
}
}
actions, err := marshalActions(f.Actions)
if err != nil {
return nil, err
}
matches, err := marshalMatches(f.Matches)
if err != nil {
return nil, err
}
// Action "drop" must only be specified by itself
if len(actions) > 1 {
for _, a := range actions {
if a == actionDrop {
return nil, &FlowError{
Err: errActionsWithDrop,
}
}
}
}
b := make([]byte, len(priorityBytes))
copy(b, priorityBytes)
b = strconv.AppendInt(b, int64(f.Priority), 10)
if f.Protocol != "" {
b = append(b, ',')
b = append(b, f.Protocol...)
}
if f.InPort != 0 {
b = append(b, ","+inPort+"="...)
// Special case, InPortLOCAL is converted to the literal string LOCAL
if f.InPort == PortLOCAL {
b = append(b, portLOCAL...)
} else {
b = strconv.AppendInt(b, int64(f.InPort), 10)
}
}
if len(matches) > 0 {
b = append(b, ","+strings.Join(matches, ",")...)
}
b = append(b, ","+table+"="...)
b = strconv.AppendInt(b, int64(f.Table), 10)
b = append(b, ","+idleTimeout+"="...)
b = strconv.AppendInt(b, int64(f.IdleTimeout), 10)
if f.Cookie > 0 {
// Hexadecimal cookies are much easier to read.
b = append(b, ","+cookie+"="...)
b = append(b, paddedHexUint64(f.Cookie)...)
}
b = append(b, ","+keyActions+"="+strings.Join(actions, ",")...)
return b, nil
}
// MarshalText marshals a LearnedFlow into its textual form.
func (f *LearnedFlow) MarshalText() ([]byte, error) {
if len(f.Actions) == 0 {
return nil, &FlowError{
Err: errNoActions,
}
}
// A learned flow can have a limited set of actions, namely `load` and `output:field`.
for _, a := range f.Actions {
switch a := a.(type) {
case *loadSetFieldAction:
if a.typ != actionLoad {
return nil, errInvalidLearnedActions
}
case *outputFieldAction:
default:
return nil, errInvalidLearnedActions
}
}
actions, err := marshalActions(f.Actions)
if err != nil {
return nil, err
}
matches, err := marshalMatches(f.Matches)
if err != nil {
return nil, err
}
b := make([]byte, len(priorityBytes))
copy(b, priorityBytes)
b = strconv.AppendInt(b, int64(f.Priority), 10)
if f.InPort != 0 {
b = append(b, ","+inPort+"="...)
// Special case, InPortLOCAL is converted to the literal string LOCAL
if f.InPort == PortLOCAL {
b = append(b, portLOCAL...)
} else {
b = strconv.AppendInt(b, int64(f.InPort), 10)
}
}
if len(matches) > 0 {
b = append(b, ","+strings.Join(matches, ",")...)
}
b = append(b, ","+table+"="...)
b = strconv.AppendInt(b, int64(f.Table), 10)
b = append(b, ","+idleTimeout+"="...)
b = strconv.AppendInt(b, int64(f.IdleTimeout), 10)
b = append(b, ","+finHardTimeout+"="...)
b = strconv.AppendInt(b, int64(f.FinHardTimeout), 10)
b = append(b, ","+hardTimeout+"="...)
b = strconv.AppendInt(b, int64(f.HardTimeout), 10)
if f.Limit > 0 {
// On older version of OpenVSwitch, the limit option doesn't exist.
// Make sure we don't use it if the value is not set.
b = append(b, ","+limit+"="...)
b = strconv.AppendInt(b, int64(f.Limit), 10)
}
if f.DeleteLearned {
b = append(b, ","+deleteLearned...)
}
if f.Cookie > 0 {
// Hexadecimal cookies are much easier to read.
b = append(b, ","+cookie+"="...)
b = append(b, paddedHexUint64(f.Cookie)...)
}
b = append(b, ","+strings.Join(actions, ",")...)
return b, nil
}
// UnmarshalText unmarshals flow text into a Flow.
func (f *Flow) UnmarshalText(b []byte) error {
// Make a copy per documentation for encoding.TextUnmarshaler.
// A string is easier to work with in this case.
s := string(b)
// Must have one and only one actions=... field in the flow.
ss := strings.Split(s, keyActions+"=")
if len(ss) != 2 || ss[1] == "" {
return &FlowError{
Err: errNoActions,
}
}
if len(ss) < 2 {
return &FlowError{
Err: errNotEnoughElements,
}
}
matchers, actions := strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
// Handle matchers first.
ss = strings.Split(matchers, ",")
f.Matches = make([]Match, 0)
for i := 0; i < len(ss); i++ {
if !strings.Contains(ss[i], "=") {
// that means this will be a protocol field.
if ss[i] != "" {
f.Protocol = Protocol(ss[i])
}
continue
}
// All remaining comma-separated values should be in key=value format,
// but parsing "actions" is done later because actions can use the form:
// actions=foo,bar,baz
kv := strings.Split(ss[i], "=")
if len(kv) != 2 {
continue
}
kv[1] = strings.TrimSpace(kv[1])
switch strings.TrimSpace(kv[0]) {
case priority:
// Parse priority into struct field.
pri, err := strconv.ParseInt(kv[1], 10, 0)
if err != nil {
return &FlowError{
Str: kv[1],
Err: err,
}
}
f.Priority = int(pri)
continue
case cookie:
// Parse cookie into struct field.
cookie, err := strconv.ParseUint(kv[1], 0, 64)
if err != nil {
return &FlowError{
Str: kv[1],
Err: err,
}
}
f.Cookie = cookie
continue
case keyActions:
// Parse actions outside this loop.
continue
case inPort:
// Parse in_port into struct field.
s := kv[1]
if strings.TrimSpace(s) == portLOCAL {
f.InPort = PortLOCAL
continue
}
port, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return &FlowError{
Str: s,
Err: err,
}
}
f.InPort = int(port)
continue
case idleTimeout:
// Parse idle_timeout into struct field.
timeout, err := strconv.ParseInt(kv[1], 10, 0)
if err != nil {
return &FlowError{
Str: kv[1],
Err: err,
}
}
f.IdleTimeout = int(timeout)
continue
case table:
// Parse table into struct field.
table, err := strconv.ParseInt(kv[1], 10, 0)
if err != nil {
return &FlowError{
Str: kv[1],
Err: err,
}
}
f.Table = int(table)
continue
case duration, nPackets, nBytes, hardAge, idleAge:
// ignore those fields.
continue
}
// All arbitrary key/value pairs that
// don't match the case above.
match, err := parseMatch(kv[0], kv[1])
if err != nil {
return err
}
// The keyword will be skipped if unknown,
// don't add a nil value
if match != nil {
f.Matches = append(f.Matches, match)
}
}
// Parse all actions from the flow.
p := newActionParser(strings.NewReader(actions))
out, raw, err := p.Parse()
if err != nil {
return &FlowError{
Str: actions,
Err: errInvalidActions,
}
}
f.Actions = out
// Action "drop" must only be specified by itself.
if len(raw) > 1 {
for _, a := range raw {
if a == actionDrop {
return &FlowError{
Err: errActionsWithDrop,
}
}
}
}
return nil
}
// MatchFlow converts Flow into MatchFlow.
func (f *Flow) MatchFlow() *MatchFlow {
return &MatchFlow{
Protocol: f.Protocol,
InPort: f.InPort,
Matches: f.Matches,
Table: f.Table,
Cookie: f.Cookie,
}
}
// marshalActions marshals all provided Actions to their text form.
func marshalActions(aa []Action) ([]string, error) {
fns := make([]func() ([]byte, error), 0, len(aa))
for _, fn := range aa {
fns = append(fns, fn.MarshalText)
}
return marshalFunctions(fns)
}
// marshalMatches marshals all provided Matches to their text form.
func marshalMatches(mm []Match) ([]string, error) {
fns := make([]func() ([]byte, error), 0, len(mm))
for _, fn := range mm {
fns = append(fns, fn.MarshalText)
}
return marshalFunctions(fns)
}
// marshalFunctions marshals a slice of functions to their text form.
func marshalFunctions(fns []func() ([]byte, error)) ([]string, error) {
out := make([]string, 0, len(fns))
for _, fn := range fns {
o, err := fn()
if err != nil {
return nil, err
}
out = append(out, string(o))
}
return out, nil
}
// paddedHexUint64 formats and displays a uint64 as a hexadecimal string
// prefixed with 0x and zero-padded up to 16 characters in length.
func paddedHexUint64(i uint64) string {
return fmt.Sprintf("%#016x", i)
}