forked from jonstout/ogo
/
ofp10.go
282 lines (238 loc) · 5.87 KB
/
ofp10.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
// OpenFlow Wire Protocol 0x01
// Package ofp10 provides OpenFlow 1.0 structs along with Read
// and Write methods for each.
//
// Struct documentation is taken from the OpenFlow Switch
// Specification Version 1.0.0.
// https://www.opennetworking.org/images/stories/downloads/sdn-resources/onf-specifications/openflow/openflow-spec-v1.0.0.pdf
package ofp10
import (
"encoding/binary"
"errors"
"github.com/3d0c/ogo/protocol/eth"
"github.com/3d0c/ogo/protocol/ofpxx"
"github.com/3d0c/ogo/protocol/util"
)
const (
VERSION = 1
)
// Echo request/reply messages can be sent from either the
// switch or the controller, and must return an echo reply. They
// can be used to indicate the latency, bandwidth, and/or
// liveness of a controller-switch connection.
func NewEchoRequest() *ofpxx.Header {
h := ofpxx.NewOfp10Header()
h.Type = Type_EchoRequest
return &h
}
// Echo request/reply messages can be sent from either the
// switch or the controller, and must return an echo reply. They
// can be used to indicate the latency, bandwidth, and/or
// liveness of a controller-switch connection.
func NewEchoReply() *ofpxx.Header {
h := ofpxx.NewOfp10Header()
h.Type = Type_EchoReply
return &h
}
// ofp_type 1.0
const (
/* Immutable messages. */
Type_Hello = iota
Type_Error
Type_EchoRequest
Type_EchoReply
Type_Vendor
/* Switch configuration messages. */
Type_FeaturesRequest
Type_FeaturesReply
Type_GetConfigRequest
Type_GetConfigReply
Type_SetConfig
/* Asynchronous messages. */
Type_PacketIn
Type_FlowRemoved
Type_PortStatus
/* Controller command messages. */
Type_PacketOut
Type_FlowMod
Type_PortMod
/* Statistics messages. */
Type_StatsRequest
Type_StatsReply
/* Barrier messages. */
Type_BarrierRequest
Type_BarrierReply
/* Queue Configuration messages. */
Type_QueueGetConfigRequest
Type_QueueGetConfigReply
)
// When the controller wishes to send a packet out through the
// datapath, it uses the OFPT_PACKET_OUT message: The buffer_id
// is the same given in the ofp_packet_in message. If the
// buffer_id is -1, then the packet data is included in the data
// array. If OFPP_TABLE is specified as the output port of an
// action, the in_port in the packet_out message is used in the
// flow table lookup.
type PacketOut struct {
ofpxx.Header
BufferId uint32
InPort uint16
ActionsLen uint16
Actions []Action
Data util.Message
}
func NewPacketOut() *PacketOut {
p := new(PacketOut)
p.Header = ofpxx.NewOfp10Header()
p.Header.Type = Type_PacketOut
p.BufferId = 0xffffffff
p.InPort = P_NONE
p.ActionsLen = 0
p.Actions = make([]Action, 0)
return p
}
func (p *PacketOut) AddAction(act Action) {
p.Actions = append(p.Actions, act)
p.ActionsLen += act.Len()
}
func (p *PacketOut) Len() (n uint16) {
n += p.Header.Len()
n += 8
n += p.ActionsLen
for _, a := range p.Actions {
n += a.Len()
}
if p.Data != nil {
n += p.Data.Len()
}
//if n < 72 { return 72 }
return
}
func (p *PacketOut) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(p.Len()))
b := make([]byte, 0)
n := 0
p.Header.Length = p.Len()
b, err = p.Header.MarshalBinary()
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], p.BufferId)
n += 4
binary.BigEndian.PutUint16(data[n:], p.InPort)
n += 2
binary.BigEndian.PutUint16(data[n:], p.ActionsLen)
n += 2
for _, a := range p.Actions {
b, err = a.MarshalBinary()
copy(data[n:], b)
n += len(b)
}
if p.Data != nil {
b, err = p.Data.MarshalBinary()
copy(data[n:], b)
n += len(b)
}
return
}
func (p *PacketOut) UnmarshalBinary(data []byte) error {
err := p.Header.UnmarshalBinary(data)
n := p.Header.Len()
p.BufferId = binary.BigEndian.Uint32(data[n:])
n += 4
p.InPort = binary.BigEndian.Uint16(data[n:])
n += 2
p.ActionsLen = binary.BigEndian.Uint16(data[n:])
n += 2
for n < (n + p.ActionsLen) {
a := DecodeAction(data[n:])
p.Actions = append(p.Actions, a)
n += a.Len()
}
err = p.Data.UnmarshalBinary(data[n:])
return err
}
// ofp_packet_in 1.0
type PacketIn struct {
ofpxx.Header
BufferId uint32
TotalLen uint16
InPort uint16
Reason uint8
Data eth.Ethernet
}
func NewPacketIn() *PacketIn {
p := new(PacketIn)
p.Header = ofpxx.NewOfp10Header()
p.Header.Type = Type_PacketIn
p.BufferId = 0xffffffff
p.InPort = P_NONE
p.Reason = 0
return p
}
func (p *PacketIn) Len() (n uint16) {
n += p.Header.Len()
n += 9
n += p.Data.Len()
return
}
func (p *PacketIn) MarshalBinary() (data []byte, err error) {
data, err = p.Header.MarshalBinary()
b := make([]byte, 9)
n := 0
binary.BigEndian.PutUint32(b, p.BufferId)
n += 4
binary.BigEndian.PutUint16(b[n:], p.TotalLen)
n += 2
binary.BigEndian.PutUint16(b[n:], p.InPort)
n += 2
b[n] = p.Reason
n += 1
data = append(data, b...)
b, err = p.Data.MarshalBinary()
data = append(data, b...)
return
}
func (p *PacketIn) UnmarshalBinary(data []byte) error {
err := p.Header.UnmarshalBinary(data)
n := p.Header.Len()
p.BufferId = binary.BigEndian.Uint32(data[n:])
n += 4
p.TotalLen = binary.BigEndian.Uint16(data[n:])
n += 2
p.InPort = binary.BigEndian.Uint16(data[n:])
n += 2
p.Reason = data[n]
n += 1
err = p.Data.UnmarshalBinary(data[n:])
return err
}
// ofp_packet_in_reason 1.0
const (
R_NO_MATCH = iota
R_ACTION
)
// ofp_vendor_header 1.0
type VendorHeader struct {
Header ofpxx.Header /*Type OFPT_VENDOR*/
Vendor uint32
}
func (v *VendorHeader) Len() (n uint16) {
return v.Header.Len() + 4
}
func (v *VendorHeader) MarshalBinary() (data []byte, err error) {
data, err = v.Header.MarshalBinary()
b := make([]byte, 4)
binary.BigEndian.PutUint32(data[:4], v.Vendor)
data = append(data, b...)
return
}
func (v *VendorHeader) UnmarshalBinary(data []byte) error {
if len(data) < int(v.Len()) {
return errors.New("The []byte the wrong size to unmarshal an " +
"VendorHeader message.")
}
v.Header.UnmarshalBinary(data)
n := int(v.Header.Len())
v.Vendor = binary.BigEndian.Uint32(data[n:])
return nil
}