-
Notifications
You must be signed in to change notification settings - Fork 177
/
vrouter.go
425 lines (347 loc) · 11.7 KB
/
vrouter.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
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 ofnet
// This file implements the virtual router functionality using Vxlan overlay
// VXLAN tables are structured as follows
//
// +-------+ +-------------------+
// | Valid +---------------------------------------->| ARP to Controller |
// | Pkts +-->+-------+ +-------------------+
// +-------+ | Vlan | +---------+
// | Table +------->| IP Dst | +--------------+
// +-------+ | Lookup +--------->| Ucast Output |
// +---------- +--------------+
//
//
import (
//"fmt"
"errors"
"net"
"net/rpc"
"github.com/contiv/ofnet/ofctrl"
"github.com/shaleman/libOpenflow/openflow13"
"github.com/shaleman/libOpenflow/protocol"
log "github.com/Sirupsen/logrus"
)
// Vrouter state.
// One Vrouter instance exists on each host
type Vrouter struct {
agent *OfnetAgent // Pointer back to ofnet agent that owns this
ofSwitch *ofctrl.OFSwitch // openflow switch we are talking to
// Fgraph tables
inputTable *ofctrl.Table // Packet lookup starts here
vlanTable *ofctrl.Table // Vlan Table. map port or VNI to vlan
ipTable *ofctrl.Table // IP lookup table
// Flow Database
flowDb map[string]*ofctrl.Flow // Database of flow entries
portVlanFlowDb map[uint32]*ofctrl.Flow // Database of flow entries
// Router Mac to be used
myRouterMac net.HardwareAddr
}
// Create a new vrouter instance
func NewVrouter(agent *OfnetAgent, rpcServ *rpc.Server) *Vrouter {
vrouter := new(Vrouter)
// Keep a reference to the agent
vrouter.agent = agent
// Create a flow dbs and my router mac
vrouter.flowDb = make(map[string]*ofctrl.Flow)
vrouter.portVlanFlowDb = make(map[uint32]*ofctrl.Flow)
vrouter.myRouterMac, _ = net.ParseMAC("00:00:11:11:11:11")
return vrouter
}
// Handle new master added event
func (self *Vrouter) MasterAdded(master *OfnetNode) error {
return nil
}
// Handle switch connected notification
func (self *Vrouter) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
self.ofSwitch = sw
log.Infof("Switch connected(vrouter). installing flows")
// Init the Fgraph
self.initFgraph()
}
// Handle switch disconnected notification
func (self *Vrouter) SwitchDisconnected(sw *ofctrl.OFSwitch) {
// FIXME: ??
}
// Handle incoming packet
func (self *Vrouter) PacketRcvd(sw *ofctrl.OFSwitch, pkt *ofctrl.PacketIn) {
switch pkt.Data.Ethertype {
case 0x0806:
if (pkt.Match.Type == openflow13.MatchType_OXM) &&
(pkt.Match.Fields[0].Class == openflow13.OXM_CLASS_OPENFLOW_BASIC) &&
(pkt.Match.Fields[0].Field == openflow13.OXM_FIELD_IN_PORT) {
// Get the input port number
switch t := pkt.Match.Fields[0].Value.(type) {
case *openflow13.InPortField:
var inPortFld openflow13.InPortField
inPortFld = *t
self.processArp(pkt.Data, inPortFld.InPort)
}
}
case 0x0800:
// FIXME: We dont expect IP packets. Use this for statefull policies.
default:
log.Errorf("Received unknown ethertype: %x", pkt.Data.Ethertype)
}
}
// Add a local endpoint and install associated local route
func (self *Vrouter) AddLocalEndpoint(endpoint OfnetEndpoint) error {
// Install a flow entry for vlan mapping and point it to IP table
portVlanFlow, err := self.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: endpoint.PortNo,
})
if err != nil {
log.Errorf("Error creating portvlan entry. Err: %v", err)
return err
}
// Set the vlan and install it
portVlanFlow.SetVlan(endpoint.Vlan)
err = portVlanFlow.Next(self.ipTable)
if err != nil {
log.Errorf("Error installing portvlan entry. Err: %v", err)
return err
}
// save the flow entry
self.portVlanFlowDb[endpoint.PortNo] = portVlanFlow
// Create the output port
outPort, err := self.ofSwitch.OutputPort(endpoint.PortNo)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", endpoint.PortNo, err)
return err
}
// Install the IP address
ipFlow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
destMacAddr, _ := net.ParseMAC(endpoint.MacAddrStr)
// Set Mac addresses
ipFlow.SetMacDa(destMacAddr)
ipFlow.SetMacSa(self.myRouterMac)
// Point the route at output port
err = ipFlow.Next(outPort)
if err != nil {
log.Errorf("Error installing flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Store the flow
self.flowDb[endpoint.IpAddr.String()] = ipFlow
return nil
}
// Remove local endpoint
func (self *Vrouter) RemoveLocalEndpoint(endpoint OfnetEndpoint) error {
// Remove the port vlan flow.
portVlanFlow := self.portVlanFlowDb[endpoint.PortNo]
if portVlanFlow != nil {
err := portVlanFlow.Delete()
if err != nil {
log.Errorf("Error deleting portvlan flow. Err: %v", err)
}
}
// Find the flow entry
ipFlow := self.flowDb[endpoint.IpAddr.String()]
if ipFlow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipFlow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
return nil
}
// Add virtual tunnel end point. This is mainly used for mapping remote vtep IP
// to ofp port number.
func (self *Vrouter) AddVtepPort(portNo uint32, remoteIp net.IP) error {
// Install a flow entry for default VNI/vlan and point it to IP table
portVlanFlow, _ := self.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
InputPort: portNo,
})
// FIXME: Need to match on tunnelId and set vlan-id per VRF
// FIXME: not needed till multi-vrf support
// portVlanFlow.SetVlan(1)
portVlanFlow.Next(self.ipTable)
// FIXME: walk all the routes and see if we can install it
// This could happen if a route made it to us before VTEP
return nil
}
// Remove a VTEP port
func (self *Vrouter) RemoveVtepPort(portNo uint32, remoteIp net.IP) error {
return nil
}
// Add a vlan.
// This is mainly used for mapping vlan id to Vxlan VNI
func (self *Vrouter) AddVlan(vlanId uint16, vni uint32) error {
// FIXME: Add this for multiple VRF support
return nil
}
// Remove a vlan
func (self *Vrouter) RemoveVlan(vlanId uint16, vni uint32) error {
// FIXME: Add this for multiple VRF support
return nil
}
// AddEndpoint Add an endpoint to the datapath
func (self *Vrouter) AddEndpoint(endpoint *OfnetEndpoint) error {
log.Infof("AddEndpoint call for endpoint: %+v", endpoint)
// Lookup the VTEP for the endpoint
vtepPort := self.agent.vtepTable[endpoint.OriginatorIp.String()]
if vtepPort == nil {
log.Errorf("Could not find the VTEP for endpoint: %+v", endpoint)
return errors.New("VTEP not found")
}
// Install the endpoint in OVS
// Create an output port for the vtep
outPort, err := self.ofSwitch.OutputPort(*vtepPort)
if err != nil {
log.Errorf("Error creating output port %d. Err: %v", *vtepPort, err)
return err
}
// Install the IP address
ipFlow, err := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
})
if err != nil {
log.Errorf("Error creating flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Set Mac addresses
ipFlow.SetMacDa(self.myRouterMac)
// This is strictly not required at the source OVS. Source mac will be
// overwritten by the dest OVS anyway. We keep the source mac for debugging purposes..
// ipFlow.SetMacSa(self.myRouterMac)
// Set VNI
// FIXME: hardcode VNI for default VRF.
// FIXME: We need to use fabric VNI per VRF
ipFlow.SetTunnelId(1)
// Point it to output port
err = ipFlow.Next(outPort)
if err != nil {
log.Errorf("Error installing flow for endpoint: %+v. Err: %v", endpoint, err)
return err
}
// Store it in flow db
self.flowDb[endpoint.IpAddr.String()] = ipFlow
return nil
}
// RemoveEndpoint removes an endpoint from the datapath
func (self *Vrouter) RemoveEndpoint(endpoint *OfnetEndpoint) error {
// Find the flow entry
ipFlow := self.flowDb[endpoint.IpAddr.String()]
if ipFlow == nil {
log.Errorf("Error finding the flow for endpoint: %+v", endpoint)
return errors.New("Flow not found")
}
// Delete the Fgraph entry
err := ipFlow.Delete()
if err != nil {
log.Errorf("Error deleting the endpoint: %+v. Err: %v", endpoint, err)
}
return nil
}
const VLAN_TBL_ID = 1
const IP_TBL_ID = 4
// initialize Fgraph on the switch
func (self *Vrouter) initFgraph() error {
sw := self.ofSwitch
// Create all tables
self.inputTable = sw.DefaultTable()
self.vlanTable, _ = sw.NewTable(VLAN_TBL_ID)
self.ipTable, _ = sw.NewTable(IP_TBL_ID)
//Create all drop entries
// Drop mcast source mac
bcastMac, _ := net.ParseMAC("01:00:00:00:00:00")
bcastSrcFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
MacSa: &bcastMac,
MacSaMask: &bcastMac,
})
bcastSrcFlow.Next(sw.DropAction())
// Redirect ARP packets to controller
arpFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0806,
})
arpFlow.Next(sw.SendToController())
// Send all valid packets to vlan table
// This is installed at lower priority so that all packets that miss above
// flows will match entry
validPktFlow, _ := self.inputTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
validPktFlow.Next(self.vlanTable)
// Drop all packets that miss Vlan lookup
vlanMissFlow, _ := self.vlanTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
vlanMissFlow.Next(sw.DropAction())
// Drop all packets that miss IP lookup
ipMissFlow, _ := self.ipTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
ipMissFlow.Next(sw.DropAction())
return nil
}
// Process incoming ARP packets
func (self *Vrouter) processArp(pkt protocol.Ethernet, inPort uint32) {
log.Debugf("processing ARP packet on port %d", inPort)
switch t := pkt.Data.(type) {
case *protocol.ARP:
log.Debugf("ARP packet: %+v", *t)
var arpHdr protocol.ARP = *t
switch arpHdr.Operation {
case protocol.Type_Request:
// Lookup the Dest IP in the endpoint table
endpoint := self.agent.getEndpointByIp(arpHdr.IPDst)
if endpoint == nil {
// If we dont know the IP address, dont send an ARP response
log.Infof("Received ARP request for unknown IP: %v", arpHdr.IPDst)
return
}
// Form an ARP response
arpResp, _ := protocol.NewARP(protocol.Type_Reply)
arpResp.HWSrc = self.myRouterMac
arpResp.IPSrc = arpHdr.IPDst
arpResp.HWDst = arpHdr.HWSrc
arpResp.IPDst = arpHdr.IPSrc
log.Infof("Sending ARP response: %+v", arpResp)
// build the ethernet packet
ethPkt := protocol.NewEthernet()
ethPkt.HWDst = arpResp.HWDst
ethPkt.HWSrc = arpResp.HWSrc
ethPkt.Ethertype = 0x0806
ethPkt.Data = arpResp
log.Infof("Sending ARP response Ethernet: %+v", ethPkt)
// Packet out
pktOut := openflow13.NewPacketOut()
pktOut.Data = ethPkt
pktOut.AddAction(openflow13.NewActionOutput(inPort))
log.Infof("Sending ARP response packet: %+v", pktOut)
// Send it out
self.ofSwitch.Send(pktOut)
default:
log.Infof("Dropping ARP response packet from port %d", inPort)
}
}
}