Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ovs: ovs-ofctl --strict support #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ovs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ func Pipe(fn PipeFunc) OptionFunc {
}
}

// Strict deactivates wildcards for matching purposes when shelling to
// 'ovs-ofctl'.
func Strict() OptionFunc {
return func(c *Client) {
c.ofctlFlags = append(c.ofctlFlags, "--strict")
}
}

const (
// FlowFormatNXMTableID is a flow format which allows Nicira Extended match
// with the ability to place a flow in a specific table.
Expand Down
8 changes: 8 additions & 0 deletions ovs/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,14 @@ func (f *Flow) MatchFlow() *MatchFlow {
}
}

// MatchFlowStrict converts Flow into a strict-matching-aware MatchFlow
func (f *Flow) MatchFlowStrict() *MatchFlow {
mf := f.MatchFlow()
mf.Priority = f.Priority
mf.Strict = true
return mf
}

// marshalActions marshals all provided Actions to their text form.
func marshalActions(aa []Action) ([]string, error) {
fns := make([]func() ([]byte, error), 0, len(aa))
Expand Down
12 changes: 12 additions & 0 deletions ovs/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,18 @@ func TestFlowMatchFlow(t *testing.T) {
}
})
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
want, got := tt.m, tt.f.MatchFlowStrict()
wantStrict := &(*want)
wantStrict.Strict = true
wantStrict.Priority = tt.f.Priority
if !reflect.DeepEqual(wantStrict, got) {
t.Fatalf("unexpected MatchFlowStrict:\n- want: %#v\n- got: %#v",
wantStrict, got)
}
})
}
}

// flowsEqual determines if two possible Flows are equal.
Expand Down
9 changes: 8 additions & 1 deletion ovs/matchflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ var (
// A MatchFlow is an OpenFlow flow intended for flow deletion. It can be marshaled to its textual
// form for use with Open vSwitch.
type MatchFlow struct {
Protocol Protocol
Strict bool
InPort int
Priority int
Protocol Protocol
Matches []Match
Table int

Expand Down Expand Up @@ -83,6 +85,11 @@ func (f *MatchFlow) MarshalText() ([]byte, error) {

var b []byte

if f.Strict {
b = append(b, priority+"="...)
b = strconv.AppendInt(b, int64(f.Priority), 10)
b = append(b, ',')
}
if f.Protocol != "" {
b = append(b, f.Protocol...)
b = append(b, ',')
Expand Down
20 changes: 18 additions & 2 deletions ovs/openflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ type flowDirective struct {

// Possible flowDirective directive values.
const (
dirAdd = "add"
dirDelete = "delete"
dirAdd = "add"
dirDelete = "delete"
dirDeleteStrict = "delete_strict"
)

// Add pushes zero or more Flows on to the transaction, to be added by
Expand Down Expand Up @@ -107,6 +108,21 @@ func (tx *FlowTransaction) Delete(flows ...*MatchFlow) {
tx.push(dirDelete, tms...)
}

// DeleteStrict is almost the same as Delete, except that the matching process
// will be strict.
func (tx *FlowTransaction) DeleteStrict(flows ...*MatchFlow) {
if tx.err != nil {
return
}

tms := make([]encoding.TextMarshaler, 0, len(flows))
for _, f := range flows {
tms = append(tms, f)
}

tx.push(dirDeleteStrict, tms...)
}

// push pushes zero or more encoding.TextMarshalers on to the transaction
// (typically a Flow or MatchFlow).
func (tx *FlowTransaction) push(directive string, flows ...encoding.TextMarshaler) {
Expand Down
14 changes: 10 additions & 4 deletions ovs/openflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,15 @@ func TestClientOpenFlowAddFlowBundleOK(t *testing.T) {
}

// Flows for deletion
matchFlows := []*MatchFlow{{
Cookie: 0xdeadbeef,
}}
matchFlows := []*MatchFlow{
{
Cookie: 0xdeadbeef,
},
{
Strict: true,
Priority: 0,
},
}

pipe := Pipe(func(stdin io.Reader, cmd string, args ...string) ([]byte, error) {
if want, got := "ovs-ofctl", cmd; want != got {
Expand Down Expand Up @@ -1079,7 +1085,7 @@ func mustVerifyFlowBundle(t *testing.T, stdin io.Reader, flows []*Flow, matchFlo
}

gotFlows = append(gotFlows, flow)
case dirDelete:
case dirDelete, dirDeleteStrict:
gotMatchFlows = append(gotMatchFlows, string(bb[1]))
default:
t.Fatalf("unexpected directive in flow bundle: %q", keyword)
Expand Down