Skip to content

Commit 808aafc

Browse files
authored
Merge pull request #81 from ecbaldwin/capture-actions
Capture actions from the trace output into a slice of strings
2 parents 9d885f6 + 46ef48c commit 808aafc

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

ovs/proto_trace.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var (
3030
datapathActionsRegexp = regexp.MustCompile(`Datapath actions: (.*)`)
3131
initialFlowRegexp = regexp.MustCompile(`Flow: (.*)`)
3232
finalFlowRegexp = regexp.MustCompile(`Final flow: (.*)`)
33+
megaFlowRegexp = regexp.MustCompile(`Megaflow: (.*)`)
34+
traceStartRegexp = regexp.MustCompile(`bridge\("(.*)"\)`)
35+
traceFlowRegexp = regexp.MustCompile(` *[[:digit:]]+[.] ([[:alpha:]].*)`)
36+
traceActionRegexp = regexp.MustCompile(` +([[:alpha:]].*)`)
3337

3438
pushVLANPattern = `push_vlan(vid=[0-9]+,pcp=[0-9]+)`
3539
)
@@ -113,14 +117,15 @@ type ProtoTrace struct {
113117
InputFlow *DataPathFlows
114118
FinalFlow *DataPathFlows
115119
DataPathActions DataPathActions
120+
FlowActions []string
116121
}
117122

118123
// UnmarshalText unmarshals ProtoTrace text into a ProtoTrace type.
119124
// Not implemented yet.
120125
func (pt *ProtoTrace) UnmarshalText(b []byte) error {
121126
lines := strings.Split(string(b), "\n")
122127
for _, line := range lines {
123-
if matches, matched := checkForDataPathActions(line); matched {
128+
if matches, matched := checkMatch(datapathActionsRegexp, line); matched {
124129
// first index is always the left most match, following
125130
// are the actual matches
126131
pt.DataPathActions = &dataPathActions{
@@ -130,7 +135,7 @@ func (pt *ProtoTrace) UnmarshalText(b []byte) error {
130135
continue
131136
}
132137

133-
if matches, matched := checkForInputFlow(line); matched {
138+
if matches, matched := checkMatch(initialFlowRegexp, line); matched {
134139
flow := &DataPathFlows{}
135140
err := flow.UnmarshalText([]byte(matches[1]))
136141
if err != nil {
@@ -141,7 +146,7 @@ func (pt *ProtoTrace) UnmarshalText(b []byte) error {
141146
continue
142147
}
143148

144-
if matches, matched := checkForFinalFlow(line); matched {
149+
if matches, matched := checkMatch(finalFlowRegexp, line); matched {
145150
flow := &DataPathFlows{}
146151
err := flow.UnmarshalText([]byte(matches[1]))
147152
if err != nil {
@@ -151,31 +156,30 @@ func (pt *ProtoTrace) UnmarshalText(b []byte) error {
151156
pt.FinalFlow = flow
152157
continue
153158
}
154-
}
155159

156-
return nil
157-
}
160+
if _, matched := checkMatch(megaFlowRegexp, line); matched {
161+
continue
162+
}
158163

159-
func checkForDataPathActions(s string) ([]string, bool) {
160-
matches := datapathActionsRegexp.FindStringSubmatch(s)
161-
if len(matches) == 0 {
162-
return matches, false
163-
}
164+
if _, matched := checkMatch(traceStartRegexp, line); matched {
165+
continue
166+
}
164167

165-
return matches, true
166-
}
168+
if _, matched := checkMatch(traceFlowRegexp, line); matched {
169+
continue
170+
}
167171

168-
func checkForInputFlow(s string) ([]string, bool) {
169-
matches := initialFlowRegexp.FindStringSubmatch(s)
170-
if len(matches) == 0 {
171-
return matches, false
172+
if matches, matched := checkMatch(traceActionRegexp, line); matched {
173+
pt.FlowActions = append(pt.FlowActions, matches[1])
174+
continue
175+
}
172176
}
173177

174-
return matches, true
178+
return nil
175179
}
176180

177-
func checkForFinalFlow(s string) ([]string, bool) {
178-
matches := finalFlowRegexp.FindStringSubmatch(s)
181+
func checkMatch(re *regexp.Regexp, s string) ([]string, bool) {
182+
matches := re.FindStringSubmatch(s)
179183
if len(matches) == 0 {
180184
return matches, false
181185
}

ovs/proto_trace_test.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121

2222
func Test_UnmarshalText(t *testing.T) {
2323
testcases := []struct {
24-
name string
25-
output string
26-
actions DataPathActions
24+
name string
25+
output string
26+
datapathActions DataPathActions
27+
flowActions []string
2728
}{
2829
{
2930
name: "action output port",
@@ -39,7 +40,11 @@ bridge("br0")
3940
Final flow: unchanged
4041
Megaflow: recirc_id=0,tcp,in_port=3,nw_src=192.0.2.0/24,nw_frag=no,tp_dst=22
4142
Datapath actions: 1`,
42-
actions: NewDataPathActions("1"),
43+
datapathActions: NewDataPathActions("1"),
44+
flowActions: []string{
45+
"resubmit(,2)",
46+
"output:1",
47+
},
4348
},
4449
{
4550
name: "in_port is LOCAL",
@@ -55,7 +60,11 @@ bridge("br0")
5560
Final flow: unchanged
5661
Megaflow: recirc_id=0,tcp,in_port=LOCAL,nw_src=192.0.2.0/24,nw_frag=no,tp_dst=22
5762
Datapath actions: 1`,
58-
actions: NewDataPathActions("1"),
63+
datapathActions: NewDataPathActions("1"),
64+
flowActions: []string{
65+
"resubmit(,2)",
66+
"output:1",
67+
},
5968
},
6069
{
6170
name: "popvlan and output port",
@@ -71,7 +80,11 @@ bridge("br0")
7180
Final flow: unchanged
7281
Megaflow: recirc_id=0,tcp,in_port=3,nw_src=192.0.2.0/24,nw_frag=no,tp_dst=22
7382
Datapath actions: popvlan,1`,
74-
actions: NewDataPathActions("popvlan,1"),
83+
datapathActions: NewDataPathActions("popvlan,1"),
84+
flowActions: []string{
85+
"resubmit(,2)",
86+
"output:1",
87+
},
7588
},
7689
{
7790
name: "pushvlan and output port",
@@ -87,7 +100,11 @@ bridge("br0")
87100
Final flow: unchanged
88101
Megaflow: recirc_id=0,tcp,in_port=3,nw_src=192.0.2.0/24,nw_frag=no,tp_dst=22
89102
Datapath actions: push_vlan(vid=20,pcp=0),4`,
90-
actions: NewDataPathActions("push_vlan(vid=20,pcp=0),4"),
103+
datapathActions: NewDataPathActions("push_vlan(vid=20,pcp=0),4"),
104+
flowActions: []string{
105+
"resubmit(,2)",
106+
"output:1",
107+
},
91108
},
92109
{
93110
name: "drop",
@@ -103,7 +120,11 @@ bridge("br0")
103120
Final flow: unchanged
104121
Megaflow: recirc_id=0,tcp,in_port=3,nw_src=192.0.2.0/24,nw_frag=no,tp_dst=22
105122
Datapath actions: drop`,
106-
actions: NewDataPathActions("drop"),
123+
datapathActions: NewDataPathActions("drop"),
124+
flowActions: []string{
125+
"resubmit(,2)",
126+
"output:1",
127+
},
107128
},
108129
}
109130

@@ -115,11 +136,17 @@ Datapath actions: drop`,
115136
t.Errorf("error unmarshalling tests: %q", err)
116137
}
117138

118-
if !reflect.DeepEqual(testcase.actions, pt.DataPathActions) {
119-
t.Logf("expected: %v", testcase.actions)
139+
if !reflect.DeepEqual(testcase.datapathActions, pt.DataPathActions) {
140+
t.Logf("expected: %v", testcase.datapathActions)
120141
t.Logf("actual: %v", pt.DataPathActions)
121142
t.Error("unexpected datapath actions")
122143
}
144+
145+
if !reflect.DeepEqual(testcase.flowActions, pt.FlowActions) {
146+
t.Logf("expected: %v", testcase.flowActions)
147+
t.Logf("actual: %v", pt.FlowActions)
148+
t.Error("unexpected trace actions")
149+
}
123150
})
124151
}
125152
}

0 commit comments

Comments
 (0)