forked from contiv/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 16
/
group.go
213 lines (176 loc) · 4.6 KB
/
group.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
package openflow13
// This file has all group related defs
import (
"encoding/binary"
log "github.com/sirupsen/logrus"
"antrea.io/libOpenflow/common"
)
const (
OFPG_MAX = 0xffffff00 /* Last usable group number. */
/* Fake groups. */
OFPG_ALL = 0xfffffffc /* Represents all groups for group delete commands. */
OFPG_ANY = 0xffffffff /* Wildcard group used only for flow stats requests. Selects all flows regardless of group (including flows with no group).
*/
)
const (
OFPGC_ADD = 0 /* New group. */
OFPGC_MODIFY = 1 /* Modify all matching groups. */
OFPGC_DELETE = 2 /* Delete all matching groups. */
OFPGC_INSERT_BUCKET = 3 /* Insert action buckets to the already available
list of action buckets in a matching group */
)
const (
OFPGT_ALL = 0 /* All (multicast/broadcast) group. */
OFPGT_SELECT = 1 /* Select group. */
OFPGT_INDIRECT = 2 /* Indirect group. */
OFPGT_FF = 3 /* Fast failover group. */
)
// GroupMod message
type GroupMod struct {
common.Header
Command uint16 /* One of OFPGC_*. */
Type uint8 /* One of OFPGT_*. */
pad uint8 /* Pad to 64 bits. */
GroupId uint32 /* Group identifier. */
Buckets []Bucket /* List of buckets */
}
// Create a new group mode message
func NewGroupMod() *GroupMod {
g := new(GroupMod)
g.Header = NewOfp13Header()
g.Header.Type = Type_GroupMod
g.Command = OFPGC_ADD
g.Type = OFPGT_ALL
g.GroupId = 0
g.Buckets = make([]Bucket, 0)
return g
}
// Add a bucket to group mod
func (g *GroupMod) AddBucket(bkt Bucket) {
g.Buckets = append(g.Buckets, bkt)
}
func (g *GroupMod) Len() (n uint16) {
n = g.Header.Len()
n += 8
if g.Command == OFPGC_DELETE {
return
}
for _, b := range g.Buckets {
n += b.Len()
}
return
}
func (g *GroupMod) MarshalBinary() (data []byte, err error) {
g.Header.Length = g.Len()
data, err = g.Header.MarshalBinary()
bytes := make([]byte, 8)
n := 0
binary.BigEndian.PutUint16(bytes[n:], g.Command)
n += 2
bytes[n] = g.Type
n += 1
bytes[n] = g.pad
n += 1
binary.BigEndian.PutUint32(bytes[n:], g.GroupId)
n += 4
data = append(data, bytes...)
for _, bkt := range g.Buckets {
bytes, err = bkt.MarshalBinary()
data = append(data, bytes...)
log.Debugf("Groupmod bucket: %v", bytes)
}
log.Debugf("GroupMod(%d): %v", len(data), data)
return
}
func (g *GroupMod) UnmarshalBinary(data []byte) error {
n := 0
g.Header.UnmarshalBinary(data[n:])
n += int(g.Header.Len())
g.Command = binary.BigEndian.Uint16(data[n:])
n += 2
g.Type = data[n]
n += 1
g.pad = data[n]
n += 1
g.GroupId = binary.BigEndian.Uint32(data[n:])
n += 4
for n < int(g.Header.Length) {
bkt := new(Bucket)
bkt.UnmarshalBinary(data[n:])
g.Buckets = append(g.Buckets, *bkt)
n += int(bkt.Len())
}
return nil
}
type Bucket struct {
Length uint16 /* Length the bucket in bytes, including this header and any padding to make it 64-bit aligned. */
Weight uint16 /* Relative weight of bucket. Only defined for select groups. */
WatchPort uint32 /* Used for FRR groups */
WatchGroup uint32 /* Used for FRR groups */
pad []byte /* 4 bytes */
Actions []Action /* zero or more actions */
}
// Create a new Bucket
func NewBucket() *Bucket {
bkt := new(Bucket)
bkt.Weight = 0
bkt.pad = make([]byte, 4)
bkt.Actions = make([]Action, 0)
bkt.WatchPort = P_ANY
bkt.WatchGroup = OFPG_ANY
bkt.Length = bkt.Len()
return bkt
}
// Add an action to the bucket
func (b *Bucket) AddAction(act Action) {
b.Actions = append(b.Actions, act)
}
func (b *Bucket) Len() (n uint16) {
n = 16
for _, a := range b.Actions {
n += a.Len()
}
// Round it to closest multiple of 8
n = ((n + 7) / 8) * 8
return
}
func (b *Bucket) MarshalBinary() (data []byte, err error) {
bytes := make([]byte, 16)
n := 0
b.Length = b.Len() // Calculate length first
binary.BigEndian.PutUint16(bytes[n:], b.Length)
n += 2
binary.BigEndian.PutUint16(bytes[n:], b.Weight)
n += 2
binary.BigEndian.PutUint32(bytes[n:], b.WatchPort)
n += 4
binary.BigEndian.PutUint32(bytes[n:], b.WatchGroup)
n += 4
data = append(data, bytes...)
for _, a := range b.Actions {
bytes, err = a.MarshalBinary()
data = append(data, bytes...)
}
return
}
func (b *Bucket) UnmarshalBinary(data []byte) error {
n := 0
b.Length = binary.BigEndian.Uint16(data[n:])
n += 2
b.Weight = binary.BigEndian.Uint16(data[n:])
n += 2
b.WatchPort = binary.BigEndian.Uint32(data[n:])
n += 4
b.WatchGroup = binary.BigEndian.Uint32(data[n:])
n += 4
n += 4 // for padding
for n < int(b.Length) {
a, err := DecodeAction(data[n:])
if err != nil {
return err
}
b.Actions = append(b.Actions, a)
n += int(a.Len())
}
return nil
}