forked from contiv/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 16
/
instruction.go
322 lines (260 loc) · 7.56 KB
/
instruction.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
package openflow15
// This file contains OFP 1.5 instruction defenitions
import (
"encoding/binary"
"errors"
"fmt"
"antrea.io/libOpenflow/util"
"k8s.io/klog/v2"
)
// ofp_instruction_type 1.5
const (
InstrType_GOTO_TABLE = 1 /* Setup the next table in the lookup pipeline */
InstrType_WRITE_METADATA = 2 /* Setup the metadata field for use later in pipeline */
InstrType_WRITE_ACTIONS = 3 /* Write the action(s) onto the datapath action set */
InstrType_APPLY_ACTIONS = 4 /* Applies the action(s) immediately */
InstrType_CLEAR_ACTIONS = 5 /* Clears all actions from the datapath action set */
InstrType_DEPRECATED = 6 /* Was Apply meter (rate limiter) */
InstrType_STAT_TRIGGER = 7 /* Statistics triggers */
InstrType_EXPERIMENTER = 0xFFFF /* Experimenter instruction */
)
// Generic instruction header
// ofp_instruction_header
type InstrHeader struct {
Type uint16
Length uint16
}
type Instruction interface {
util.Message
AddAction(act Action, prepend bool) error
}
func (a *InstrHeader) Len() (n uint16) {
return 4
}
func (a *InstrHeader) MarshalBinary() (data []byte, err error) {
data = make([]byte, a.Len())
binary.BigEndian.PutUint16(data[:2], a.Type)
binary.BigEndian.PutUint16(data[2:4], a.Length)
return
}
func (a *InstrHeader) UnmarshalBinary(data []byte) error {
if len(data) != 4 {
return errors.New("Wrong size to unmarshal an InstrHeader message.")
}
a.Type = binary.BigEndian.Uint16(data[:2])
a.Length = binary.BigEndian.Uint16(data[2:4])
return nil
}
func DecodeInstr(data []byte) (Instruction, error) {
if len(data) < 2 {
return nil, errors.New("data too short to decode Instruction")
}
t := binary.BigEndian.Uint16(data[:2])
var a Instruction
switch t {
case InstrType_GOTO_TABLE:
a = new(InstrGotoTable)
case InstrType_WRITE_METADATA:
a = new(InstrWriteMetadata)
case InstrType_WRITE_ACTIONS:
a = new(InstrActions)
case InstrType_APPLY_ACTIONS:
a = new(InstrActions)
case InstrType_CLEAR_ACTIONS:
a = new(InstrActions)
case InstrType_DEPRECATED:
case InstrType_STAT_TRIGGER:
case InstrType_EXPERIMENTER:
default:
return nil, fmt.Errorf("unknown Instrheader type: %v", t)
}
err := a.UnmarshalBinary(data)
if err != nil {
klog.ErrorS(err, "Failed to unmarshal Instruction", "data", data)
return nil, err
}
return a, nil
}
type InstrGotoTable struct {
InstrHeader
TableId uint8
pad []byte // 3 bytes
}
func (instr *InstrGotoTable) Len() (n uint16) {
return 8
}
func (instr *InstrGotoTable) MarshalBinary() (data []byte, err error) {
data, err = instr.InstrHeader.MarshalBinary()
b := make([]byte, 4)
b[0] = instr.TableId
copy(b[3:], instr.pad)
data = append(data, b...)
return
}
func (instr *InstrGotoTable) UnmarshalBinary(data []byte) error {
instr.InstrHeader.UnmarshalBinary(data[:4])
instr.TableId = data[4]
copy(instr.pad, data[5:8])
return nil
}
func NewInstrGotoTable(tableId uint8) *InstrGotoTable {
instr := new(InstrGotoTable)
instr.Type = InstrType_GOTO_TABLE
instr.TableId = tableId
instr.pad = make([]byte, 3)
instr.Length = instr.Len()
return instr
}
func (instr *InstrGotoTable) AddAction(act Action, prepend bool) error {
return errors.New("Not supported on this instrction")
}
type InstrWriteMetadata struct {
InstrHeader
pad []byte // 4 bytes
Metadata uint64 /* Metadata value to write */
MetadataMask uint64 /* Metadata write bitmask */
}
// FIXME: we need marshall/unmarshall/len/new functions for write metadata instr
func (instr *InstrWriteMetadata) Len() (n uint16) {
return 24
}
func (instr *InstrWriteMetadata) MarshalBinary() (data []byte, err error) {
data, err = instr.InstrHeader.MarshalBinary()
b := make([]byte, 20)
copy(b, instr.pad)
binary.BigEndian.PutUint64(b[4:], instr.Metadata)
binary.BigEndian.PutUint64(b[12:], instr.MetadataMask)
data = append(data, b...)
return
}
func (instr *InstrWriteMetadata) UnmarshalBinary(data []byte) error {
instr.InstrHeader.UnmarshalBinary(data[:4])
copy(instr.pad, data[4:8])
instr.Metadata = binary.BigEndian.Uint64(data[8:16])
instr.MetadataMask = binary.BigEndian.Uint64(data[16:24])
return nil
}
func NewInstrWriteMetadata(metadata, metadataMask uint64) *InstrWriteMetadata {
instr := new(InstrWriteMetadata)
instr.Type = InstrType_WRITE_METADATA
instr.pad = make([]byte, 4)
instr.Metadata = metadata
instr.MetadataMask = metadataMask
instr.Length = instr.Len()
return instr
}
func (instr *InstrWriteMetadata) AddAction(act Action, prepend bool) error {
return errors.New("Not supported on this instrction")
}
// *_ACTION instructions
type InstrActions struct {
InstrHeader
pad []byte // 4 bytes
Actions []Action /* 0 or more actions associated with OFPIT_WRITE_ACTIONS and OFPIT_APPLY_ACTIONS */
}
func (instr *InstrActions) Len() (n uint16) {
n = 8
for _, act := range instr.Actions {
n += act.Len()
}
return
}
func (instr *InstrActions) MarshalBinary() (data []byte, err error) {
data, err = instr.InstrHeader.MarshalBinary()
b := make([]byte, 4)
copy(b, instr.pad)
data = append(data, b...)
for _, act := range instr.Actions {
b, err = act.MarshalBinary()
data = append(data, b...)
}
return
}
func (instr *InstrActions) UnmarshalBinary(data []byte) error {
instr.InstrHeader.UnmarshalBinary(data[:4])
n := 8
for n < int(instr.Length) {
act, err := DecodeAction(data[n:])
if err != nil {
klog.ErrorS(err, "Failed to decode InstrActions's Actions", "data", data[n:])
return err
}
instr.Actions = append(instr.Actions, act)
n += int(act.Len())
}
return nil
}
func (instr *InstrActions) AddAction(act Action, prepend bool) error {
// Append or prepend to the list
if prepend {
instr.Actions = append([]Action{act}, instr.Actions...)
} else {
instr.Actions = append(instr.Actions, act)
}
instr.Length = instr.Len()
return nil
}
func NewInstrWriteActions() *InstrActions {
instr := new(InstrActions)
instr.Type = InstrType_WRITE_ACTIONS
instr.pad = make([]byte, 4)
instr.Actions = make([]Action, 0)
instr.Length = instr.Len()
return instr
}
func NewInstrApplyActions() *InstrActions {
instr := new(InstrActions)
instr.Type = InstrType_APPLY_ACTIONS
instr.pad = make([]byte, 4)
instr.Actions = make([]Action, 0)
instr.Length = instr.Len()
return instr
}
// ofp_instruction_stat_trigger
type InstrStatTrigger struct {
InstrHeader
Flags uint32
Thresholds Stats
}
// ofp_stat_trigger_flags
const (
STF_PERIODIC = 1 << 0 /* Trigger for all multiples of thresholds. */
STF_ONLY_FIRST = 1 << 1 /* Trigger on only first reach threshold. */
)
func NewInstrStatTrigger(flags uint32) *InstrStatTrigger {
instr := new(InstrStatTrigger)
instr.Type = InstrType_STAT_TRIGGER
instr.Flags = flags
instr.Length = instr.Len()
return instr
}
func (instr *InstrStatTrigger) Len() (n uint16) {
n = 8
n += instr.Thresholds.Len()
return
}
func (instr *InstrStatTrigger) MarshalBinary() (data []byte, err error) {
data, err = instr.InstrHeader.MarshalBinary()
if err != nil {
return
}
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, instr.Flags)
data = append(data, b...)
b, err = instr.Thresholds.MarshalBinary()
data = append(data, b...)
return
}
func (instr *InstrStatTrigger) UnmarshalBinary(data []byte) error {
instr.InstrHeader.UnmarshalBinary(data[:4])
instr.Flags = binary.BigEndian.Uint32(data[4:8])
err := instr.Thresholds.UnmarshalBinary(data[8:])
if err != nil {
klog.ErrorS(err, "Failed to marshal InstrStatTrigger's Thresholds", "data", data[8:])
return err
}
return nil
}
func (instr *InstrStatTrigger) AddAction(act Action, prepend bool) error {
return errors.New("Not supported on this instrction")
}