forked from digitalocean/go-openvswitch
/
actionparser.go
393 lines (336 loc) · 9.11 KB
/
actionparser.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
// 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 (
"bufio"
"bytes"
"fmt"
"io"
"net"
"regexp"
"strconv"
"strings"
)
// An actionParser is a parser for OVS flow actions.
type actionParser struct {
r *bufio.Reader
s stack
}
// newActionParser creates a new actionParser which wraps the input
// io.Reader.
func newActionParser(r io.Reader) *actionParser {
return &actionParser{
r: bufio.NewReader(r),
s: make(stack, 0),
}
}
// eof is a sentinel rune for end of file.
var eof = rune(0)
// read reads a single rune from the wrapped io.Reader. It returns eof
// if no more runes are present.
func (p *actionParser) read() rune {
ch, _, err := p.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// Parse parses a slice of Actions using the wrapped io.Reader. The raw
// action strings are also returned for inspection if needed.
func (p *actionParser) Parse() ([]Action, []string, error) {
var actions []Action
var raw []string
for {
a, r, err := p.parseAction()
if err != nil {
// No more actions remain
if err == io.EOF {
break
}
return nil, nil, err
}
actions = append(actions, a)
raw = append(raw, r)
}
return actions, raw, nil
}
// parseAction parses a single Action and its raw text from the wrapped
// io.Reader.
func (p *actionParser) parseAction() (Action, string, error) {
// Track runes encountered
var buf bytes.Buffer
for {
ch := p.read()
// If comma encountered and no open parentheses, at end of this
// action string
if ch == ',' && p.s.len() == 0 {
break
}
// If EOF encountered, at end of string
if ch == eof {
// If no items in buffer, end of this action
if buf.Len() == 0 {
return nil, "", io.EOF
}
// Parse action from buffer
break
}
// Track open and closing parentheses using a stack to ensure
// that they are appropriately matched
switch ch {
case '(':
p.s.push()
case ')':
p.s.pop()
}
_, _ = buf.WriteRune(ch)
}
// Found an unmatched set of parentheses
if p.s.len() > 0 {
return nil, "", fmt.Errorf("invalid action: %q", buf.String())
}
s := buf.String()
act, err := parseAction(s)
return act, s, err
}
// A stack is a basic stack with elements that have no value.
type stack []struct{}
// len returns the current length of the stack.
func (s *stack) len() int {
return len(*s)
}
// push adds an element to the stack.
func (s *stack) push() {
*s = append(*s, struct{}{})
}
// pop removes an element from the stack.
func (s *stack) pop() {
*s = (*s)[:s.len()-1]
}
var (
// resubmitRe is the regex used to match the resubmit action
// with port and table specified
resubmitRe = regexp.MustCompile(`resubmit\((\d*),(\d*)\)`)
// resubmitPortRe is the regex used to match the resubmit action
// when only a port is specified
resubmitPortRe = regexp.MustCompile(`resubmit:(\d+)`)
// ctRe is the regex used to match the ct action with its
// parameter list.
ctRe = regexp.MustCompile(`ct\((\S+)\)`)
// loadRe is the regex used to match the load action
// with its parameters.
loadRe = regexp.MustCompile(`load:(\S+)->(\S+)`)
// moveRe is the regex used to match the move action
// with its parameters.
moveRe = regexp.MustCompile(`move:(\S+)->(\S+)`)
// setFieldRe is the regex used to match the set_field action
// with its parameters.
setFieldRe = regexp.MustCompile(`set_field:(\S+)->(\S+)`)
)
// TODO(mdlayher): replace parsing regex with arguments parsers
// parseAction creates an Action function from the input string.
func parseAction(s string) (Action, error) {
// Simple actions which match a basic string
switch strings.ToLower(s) {
case actionDrop:
return Drop(), nil
case actionFlood:
return Flood(), nil
case actionInPort:
return InPort(), nil
case actionLocal:
return Local(), nil
case actionNormal:
return Normal(), nil
case actionStripVLAN:
return StripVLAN(), nil
}
// ActionCT, with its arguments
if ss := ctRe.FindAllStringSubmatch(s, 1); len(ss) > 0 && len(ss[0]) == 2 {
// Results are:
// - full string
// - arguments list
return ConnectionTracking(ss[0][1]), nil
}
// ActionModDataLinkDestination, with its hardware address.
if strings.HasPrefix(s, patModDataLinkDestination[:len(patModDataLinkDestination)-2]) {
var addr string
n, err := fmt.Sscanf(s, patModDataLinkDestination, &addr)
if err != nil {
return nil, err
}
if n > 0 {
mac, err := net.ParseMAC(addr)
if err != nil {
return nil, err
}
return ModDataLinkDestination(mac), nil
}
}
// ActionModDataLinkSource, with its hardware address.
if strings.HasPrefix(s, patModDataLinkSource[:len(patModDataLinkSource)-2]) {
var addr string
n, err := fmt.Sscanf(s, patModDataLinkSource, &addr)
if err != nil {
return nil, err
}
if n > 0 {
mac, err := net.ParseMAC(addr)
if err != nil {
return nil, err
}
return ModDataLinkSource(mac), nil
}
}
// ActionModNetworkDestination, with it hardware address
if strings.HasPrefix(s, patModNetworkDestination[:len(patModNetworkDestination)-2]) {
var ip string
n, err := fmt.Sscanf(s, patModNetworkDestination, &ip)
if err != nil {
return nil, err
}
if n > 0 {
ip4 := net.ParseIP(ip).To4()
if ip4 == nil {
return nil, fmt.Errorf("invalid IPv4 address: %s", ip)
}
return ModNetworkDestination(ip4), nil
}
}
// ActionModNetworkSource, with it hardware address
if strings.HasPrefix(s, patModNetworkSource[:len(patModNetworkSource)-2]) {
var ip string
n, err := fmt.Sscanf(s, patModNetworkSource, &ip)
if err != nil {
return nil, err
}
if n > 0 {
ip4 := net.ParseIP(ip).To4()
if ip4 == nil {
return nil, fmt.Errorf("invalid IPv4 address: %s", ip)
}
return ModNetworkSource(ip4), nil
}
}
// ActionModTransportDestinationPort, with its port.
if strings.HasPrefix(s, patModTransportDestinationPort[:len(patModTransportDestinationPort)-2]) {
var port uint16
n, err := fmt.Sscanf(s, patModTransportDestinationPort, &port)
if err != nil {
return nil, err
}
if n > 0 {
return ModTransportDestinationPort(port), nil
}
}
// ActionModTransportSourcePort, with its port.
if strings.HasPrefix(s, patModTransportSourcePort[:len(patModTransportSourcePort)-2]) {
var port uint16
n, err := fmt.Sscanf(s, patModTransportSourcePort, &port)
if err != nil {
return nil, err
}
if n > 0 {
return ModTransportSourcePort(port), nil
}
}
// ActionModVLANVID, with its VLAN ID
if strings.HasPrefix(s, patModVLANVID[:len(patModVLANVID)-2]) {
var vlan int
n, err := fmt.Sscanf(s, patModVLANVID, &vlan)
if err != nil {
return nil, err
}
if n > 0 {
return ModVLANVID(vlan), nil
}
}
// ActionConjunction, with it's id, dimension number, and dimension size
if strings.HasPrefix(s, patConjunction[:len(patConjunction)-10]) {
var id, dimensionNumber, dimensionSize int
n, err := fmt.Sscanf(s, patConjunction, &id, &dimensionNumber, &dimensionSize)
if err != nil {
return nil, err
}
if n > 0 {
return Conjunction(id, dimensionNumber, dimensionSize), nil
}
}
// ActionOutput, with its port number
if strings.HasPrefix(s, patOutput[:len(patOutput)-2]) {
var port int
n, err := fmt.Sscanf(s, patOutput, &port)
if err != nil {
return nil, err
}
if n > 0 {
return Output(port), nil
}
}
// ActionResubmit, with both port number and table number
if ss := resubmitRe.FindAllStringSubmatch(s, 1); len(ss) > 0 && len(ss[0]) == 3 {
var (
port int
table int
err error
)
// Results are:
// - full string
// - port in parenthesis
// - table in parenthesis
if s := ss[0][1]; s != "" {
port, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
}
if s := ss[0][2]; s != "" {
table, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
}
return Resubmit(port, table), nil
}
// ActionResubmitPort, with only a port number
if ss := resubmitPortRe.FindAllStringSubmatch(s, 1); len(ss) > 0 && len(ss[0]) == 2 {
port, err := strconv.Atoi(ss[0][1])
if err != nil {
return nil, err
}
return ResubmitPort(port), nil
}
if ss := loadRe.FindAllStringSubmatch(s, 2); len(ss) > 0 && len(ss[0]) == 3 {
// Results are:
// - full string
// - value
// - field
return Load(ss[0][1], ss[0][2]), nil
}
if ss := moveRe.FindAllStringSubmatch(s, 2); len(ss) > 0 && len(ss[0]) == 3 {
// Results are:
// - full string
// - value
// - field
return Move(ss[0][1], ss[0][2]), nil
}
if ss := setFieldRe.FindAllStringSubmatch(s, 2); len(ss) > 0 && len(ss[0]) == 3 {
// Results are:
// - full string
// - value
// - field
return SetField(ss[0][1], ss[0][2]), nil
}
return nil, fmt.Errorf("no action matched for %q", s)
}