forked from Kmotiko/gofc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapath.go
226 lines (205 loc) · 5.29 KB
/
datapath.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
package gofc
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"github.com/Kmotiko/gofc/ofprotocol/ofp13"
)
// datapath
type Datapath struct {
buffer chan *bytes.Buffer
conn *net.TCPConn
datapathId uint64
sendBuffer chan *ofp13.OFMessage
ofpversion string
ports int
}
/**
* ctor
*/
func NewDatapath(conn *net.TCPConn) *Datapath {
dp := new(Datapath)
dp.sendBuffer = make(chan *ofp13.OFMessage, 10)
dp.conn = conn
return dp
}
func (dp *Datapath) sendLoop() {
for {
// wait channel
msg := <-(dp.sendBuffer)
// serialize data
byteData := (*msg).Serialize()
_, err := dp.conn.Write(byteData)
if err != nil {
fmt.Println("failed to write conn")
fmt.Println(err)
return
}
}
}
func (dp *Datapath) recvLoop() {
buf := make([]byte, 1024*64)
for {
// read
size, err := dp.conn.Read(buf)
if err != nil {
fmt.Println("failed to read conn")
fmt.Println(err)
return
}
// tmp := make([]byte, 2048)
for i := 0; i < size; {
msgLen := binary.BigEndian.Uint16(buf[i+2:])
dp.handlePacket(buf[i : i+(int)(msgLen)])
i += (int)(msgLen)
}
}
}
func (dp *Datapath) handlePacket(buf []byte) {
// parse data
msg := ofp13.Parse(buf[0:])
if _, ok := msg.(*ofp13.OfpHello); ok {
// handle hello
featureReq := ofp13.NewOfpFeaturesRequest()
dp.Send(featureReq)
} else {
// dispatch handler
dp.dispatchHandler(msg)
}
}
func (dp *Datapath) dispatchHandler(msg ofp13.OFMessage) {
apps := GetAppManager().GetApplications()
for _, app := range apps {
switch msgi := msg.(type) {
// if message is OfpHeader
case *ofp13.OfpHeader:
switch msgi.Type {
// handle echo request
case ofp13.OFPT_ECHO_REQUEST:
if obj, ok := app.(Of13EchoRequestHandler); ok {
obj.HandleEchoRequest(msgi, dp)
}
// handle echo reply
case ofp13.OFPT_ECHO_REPLY:
if obj, ok := app.(Of13EchoReplyHandler); ok {
obj.HandleEchoReply(msgi, dp)
}
// handle Barrier reply
case ofp13.OFPT_BARRIER_REPLY:
if obj, ok := app.(Of13BarrierReplyHandler); ok {
obj.HandleBarrierReply(msgi, dp)
}
default:
}
// Recv Error
case *ofp13.OfpErrorMsg:
if obj, ok := app.(Of13ErrorMsgHandler); ok {
obj.HandleErrorMsg(msgi, dp)
}
// Recv RoleReply
case *ofp13.OfpRole:
if obj, ok := app.(Of13RoleReplyHandler); ok {
obj.HandleRoleReply(msgi, dp)
}
// Recv GetAsyncReply
case *ofp13.OfpAsyncConfig:
if obj, ok := app.(Of13AsyncConfigHandler); ok {
obj.HandleAsyncConfig(msgi, dp)
}
// case SwitchFeatures
case *ofp13.OfpSwitchFeatures:
if obj, ok := app.(Of13SwitchFeaturesHandler); ok {
obj.HandleSwitchFeatures(msgi, dp)
}
// case GetConfigReply
case *ofp13.OfpSwitchConfig:
if obj, ok := app.(Of13SwitchConfigHandler); ok {
obj.HandleSwitchConfig(msgi, dp)
}
// case PacketIn
case *ofp13.OfpPacketIn:
if obj, ok := app.(Of13PacketInHandler); ok {
obj.HandlePacketIn(msgi, dp)
}
// case FlowRemoved
case *ofp13.OfpFlowRemoved:
if obj, ok := app.(Of13FlowRemovedHandler); ok {
obj.HandleFlowRemoved(msgi, dp)
}
// case MultipartReply
case *ofp13.OfpMultipartReply:
switch msgi.Type {
case ofp13.OFPMP_DESC:
if obj, ok := app.(Of13DescStatsReplyHandler); ok {
obj.HandleDescStatsReply(msgi, dp)
}
case ofp13.OFPMP_FLOW:
if obj, ok := app.(Of13FlowStatsReplyHandler); ok {
obj.HandleFlowStatsReply(msgi, dp)
}
case ofp13.OFPMP_AGGREGATE:
if obj, ok := app.(Of13AggregateStatsReplyHandler); ok {
obj.HandleAggregateStatsReply(msgi, dp)
}
case ofp13.OFPMP_TABLE:
if obj, ok := app.(Of13TableStatsReplyHandler); ok {
obj.HandleTableStatsReply(msgi, dp)
}
case ofp13.OFPMP_PORT_STATS:
if obj, ok := app.(Of13PortStatsReplyHandler); ok {
obj.HandlePortStatsReply(msgi, dp)
}
case ofp13.OFPMP_QUEUE:
if obj, ok := app.(Of13QueueStatsReplyHandler); ok {
obj.HandleQueueStatsReply(msgi, dp)
}
case ofp13.OFPMP_GROUP:
if obj, ok := app.(Of13GroupStatsReplyHandler); ok {
obj.HandleGroupStatsReply(msgi, dp)
}
case ofp13.OFPMP_GROUP_DESC:
if obj, ok := app.(Of13GroupDescStatsReplyHandler); ok {
obj.HandleGroupDescStatsReply(msgi, dp)
}
case ofp13.OFPMP_GROUP_FEATURES:
if obj, ok := app.(Of13GroupFeaturesStatsReplyHandler); ok {
obj.HandleGroupFeaturesStatsReply(msgi, dp)
}
case ofp13.OFPMP_METER:
if obj, ok := app.(Of13MeterStatsReplyHandler); ok {
obj.HandleMeterStatsReply(msgi, dp)
}
case ofp13.OFPMP_METER_CONFIG:
if obj, ok := app.(Of13MeterConfigStatsReplyHandler); ok {
obj.HandleMeterConfigStatsReply(msgi, dp)
}
case ofp13.OFPMP_METER_FEATURES:
if obj, ok := app.(Of13MeterFeaturesStatsReplyHandler); ok {
obj.HandleMeterFeaturesStatsReply(msgi, dp)
}
case ofp13.OFPMP_TABLE_FEATURES:
if obj, ok := app.(Of13TableFeaturesStatsReplyHandler); ok {
obj.HandleTableFeaturesStatsReply(msgi, dp)
}
case ofp13.OFPMP_PORT_DESC:
if obj, ok := app.(Of13PortDescStatsReplyHandler); ok {
obj.HandlePortDescStatsReply(msgi, dp)
}
case ofp13.OFPMP_EXPERIMENTER:
// TODO: implement
default:
}
default:
fmt.Println("UnSupport Message")
}
}
}
/**
*
*/
func (dp *Datapath) Send(message ofp13.OFMessage) bool {
// push data
(dp.sendBuffer) <- &message
return true
}