-
Notifications
You must be signed in to change notification settings - Fork 346
/
ofctrl_flow.go
188 lines (165 loc) · 4.94 KB
/
ofctrl_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
// Copyright 2022 Antrea Authors.
//
// 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 openflow
import (
"fmt"
"strings"
"antrea.io/libOpenflow/openflow13"
"antrea.io/ofnet/ofctrl"
)
type FlowStates struct {
TableID uint8
PacketCount uint64
DurationNSecond uint32
}
type ofFlow struct {
table *ofTable
// The Flow.Table field can be updated by Reset(), which can be called by
// ReplayFlows() when replaying the Flow to OVS. For thread safety, any access
// to Flow.Table should hold the replayMutex read lock.
*ofctrl.Flow
// matchers is string slice, it is used to generate a readable match string of the Flow.
matchers []string
// protocol adds a readable protocol type in the match string of ofFlow.
protocol Protocol
// ctStateString is a temporary variable for the readable ct_state configuration. Its value is changed when the client
// updates the matching condition of "ct_states". When FlowBuilder.Done is called, its value is added into the matchers.
ctStateString string
// ctStates is a temporary variable to maintain openflow13.CTStates. When FlowBuilder.Done is called, it is used to
// set the CtStates field in ofctrl.Flow.Match.
ctStates *openflow13.CTStates
// isDropFlow is true if this flow actions contain "drop"
isDropFlow bool
}
func (f *ofFlow) String() string {
s := strings.Join([]string{`&ofFlow{`,
`table:` + fmt.Sprintf("%v", f.table.id) + `,`,
`matchers:` + fmt.Sprintf("%v", f.matchers) + `,`,
`protocol:` + fmt.Sprintf("%v", f.protocol) + `,`,
`flow:` + fmt.Sprintf("%v", f.Flow) + `,`,
`}`,
}, "")
return s
}
// Reset updates the ofFlow.Flow.Table field with ofFlow.table.Table.
// In the case of reconnecting to OVS, the ofnet library creates new OFTable
// objects. Reset() can be called to reset ofFlow.Flow.Table to the right value,
// before replaying the Flow to OVS.
func (f *ofFlow) Reset() {
f.Flow.Table = f.table.Table
}
func (f *ofFlow) Add() error {
err := f.Flow.Send(openflow13.FC_ADD)
if err != nil {
return err
}
f.table.UpdateStatus(1)
return nil
}
func (f *ofFlow) Modify() error {
err := f.Flow.Send(openflow13.FC_MODIFY_STRICT)
if err != nil {
return err
}
f.table.UpdateStatus(0)
return nil
}
func (f *ofFlow) Delete() error {
f.Flow.UpdateInstallStatus(true)
err := f.Flow.Send(openflow13.FC_DELETE_STRICT)
if err != nil {
return err
}
f.table.UpdateStatus(-1)
return nil
}
func (f *ofFlow) Type() EntryType {
return FlowEntry
}
func (f *ofFlow) KeyString() string {
return f.MatchString()
}
func (f *ofFlow) MatchString() string {
repr := fmt.Sprintf("table=%d", f.table.id)
if f.protocol != "" {
repr = fmt.Sprintf("%s,%s", repr, f.protocol)
}
if len(f.matchers) > 0 {
repr = fmt.Sprintf("%s,%s", repr, strings.Join(f.matchers, ","))
}
return repr
}
func (f *ofFlow) FlowPriority() uint16 {
return f.Match.Priority
}
func (f *ofFlow) FlowProtocol() Protocol {
return f.protocol
}
func (f *ofFlow) GetBundleMessage(entryOper OFOperation) (ofctrl.OpenFlowModMessage, error) {
var operation int
switch entryOper {
case AddMessage:
operation = openflow13.FC_ADD
case ModifyMessage:
operation = openflow13.FC_MODIFY_STRICT
case DeleteMessage:
operation = openflow13.FC_DELETE_STRICT
}
message, err := f.Flow.GetBundleMessage(operation)
if err != nil {
return nil, err
}
return message, nil
}
// CopyToBuilder returns a new FlowBuilder that copies the table, protocols,
// matches, and CookieID of the Flow, but does not copy private status fields
// of the ofctrl.Flow, e.g. "realized" and "isInstalled". It copies the
// original actions of the Flow only if copyActions is set to true, and
// resets the priority in the new FlowBuilder if it is provided.
func (f *ofFlow) CopyToBuilder(priority uint16, copyActions bool) FlowBuilder {
flow := &ofctrl.Flow{
Table: f.Flow.Table,
CookieID: f.Flow.CookieID,
CookieMask: f.Flow.CookieMask,
Match: f.Flow.Match,
}
if copyActions {
f.Flow.CopyActionsToNewFlow(flow)
}
if priority > 0 {
flow.Match.Priority = priority
}
newFlow := ofFlow{
table: f.table,
Flow: flow,
matchers: f.matchers,
protocol: f.protocol,
}
if copyActions {
newFlow.isDropFlow = f.isDropFlow
}
return &ofFlowBuilder{newFlow}
}
func (f *ofFlow) IsDropFlow() bool {
return f.isDropFlow
}
func (r *Range) ToNXRange() *openflow13.NXRange {
return openflow13.NewNXRange(int(r[0]), int(r[1]))
}
func (r *Range) Length() uint32 {
return r[1] - r[0] + 1
}
func (r *Range) Offset() uint32 {
return r[0]
}