forked from MFALHI/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathovsSwitch.go
More file actions
523 lines (428 loc) · 12.8 KB
/
ovsSwitch.go
File metadata and controls
523 lines (428 loc) · 12.8 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/***
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 drivers
import (
"fmt"
"net"
"strings"
"time"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/netmaster/mastercfg"
"github.com/contiv/netplugin/utils/netutils"
"github.com/contiv/ofnet"
log "github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
)
const useVethPair = true
const vxlanEndpointMtu = 1450
// OvsSwitch represents on OVS bridge instance
type OvsSwitch struct {
bridgeName string
netType string
ovsdbDriver *OvsdbDriver
ofnetAgent *ofnet.OfnetAgent
}
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string) (*OvsSwitch, error) {
var err error
sw := new(OvsSwitch)
sw.bridgeName = bridgeName
sw.netType = netType
// Determine the failure mode
failMode := ""
if netType == "vxlan" {
failMode = "secure"
}
// Create OVS db driver
sw.ovsdbDriver, err = NewOvsdbDriver(bridgeName, failMode)
if err != nil {
log.Fatalf("Error creating ovsdb driver. Err: %v", err)
}
// For Vxlan, initialize ofnet. For VLAN mode, we use OVS normal forwarding
if netType == "vxlan" {
// Create an ofnet agent
sw.ofnetAgent, err = ofnet.NewOfnetAgent("vxlan", net.ParseIP(localIP),
ofnet.OFNET_AGENT_PORT, 6633)
if err != nil {
log.Fatalf("Error initializing ofnet")
return nil, err
}
// Add controller to the OVS
ctrlerIP := "127.0.0.1"
ctrlerPort := uint16(6633)
target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, ctrlerPort)
if !sw.ovsdbDriver.IsControllerPresent(target) {
err = sw.ovsdbDriver.AddController(ctrlerIP, ctrlerPort)
if err != nil {
log.Fatalf("Error adding controller to OVS. Err: %v", err)
return nil, err
}
}
log.Infof("Waiting for OVS switch to connect..")
// Wait for a while for OVS switch to connect to ofnet agent
sw.ofnetAgent.WaitForSwitchConnection()
log.Infof("Switch (vxlan) connected.")
}
return sw, nil
}
// Delete performs cleanup prior to destruction of the OvsDriver
func (sw *OvsSwitch) Delete() {
if sw.ofnetAgent != nil {
sw.ofnetAgent.Delete()
}
if sw.ovsdbDriver != nil {
sw.ovsdbDriver.Delete()
// Wait a little for OVS switch to be deleted
time.Sleep(300 * time.Millisecond)
}
}
// CreateNetwork creates a new network/vlan
func (sw *OvsSwitch) CreateNetwork(pktTag uint16, extPktTag uint32) error {
if sw.netType == "vxlan" {
// Add the vlan/vni to ofnet
err := sw.ofnetAgent.AddVlan(pktTag, extPktTag)
if err != nil {
log.Errorf("Error adding vlan/vni %d/%d. Err: %v", pktTag, extPktTag, err)
return err
}
}
return nil
}
// DeleteNetwork deletes a network/vlan
func (sw *OvsSwitch) DeleteNetwork(pktTag uint16, extPktTag uint32) error {
if sw.netType == "vxlan" {
// Delete vlan/vni mapping
err := sw.ofnetAgent.RemoveVlan(pktTag, extPktTag)
if err != nil {
log.Errorf("Error removing vlan/vni %d/%d. Err: %v", pktTag, extPktTag, err)
return err
}
}
return nil
}
// createVethPair creates veth interface pairs with specified name
func createVethPair(name1, name2 string) error {
log.Infof("Creating Veth pairs with name: %s, %s", name1, name2)
// Veth pair params
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: name1,
TxQLen: 0,
},
PeerName: name2,
}
// Create the veth pair
if err := netlink.LinkAdd(veth); err != nil {
log.Errorf("error creating veth pair: %v", err)
return err
}
return nil
}
// deleteVethPair deletes veth interface pairs
func deleteVethPair(name1, name2 string) error {
log.Infof("Deleting Veth pairs with name: %s, %s", name1, name2)
// Veth pair params
veth := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: name1,
TxQLen: 0,
},
PeerName: name2,
}
// Create the veth pair
if err := netlink.LinkDel(veth); err != nil {
log.Errorf("error deleting veth pair: %v", err)
return err
}
return nil
}
// setLinkUp sets the link up
func setLinkUp(name string) error {
iface, err := netlink.LinkByName(name)
if err != nil {
return err
}
return netlink.LinkSetUp(iface)
}
// Set the link mtu
func setLinkMtu(name string, mtu int) error {
iface, err := netlink.LinkByName(name)
if err != nil {
return err
}
return netlink.LinkSetMTU(iface, mtu)
}
// getOvsPostName returns OVS port name depending on if we use Veth pairs
func getOvsPostName(intfName string) string {
var ovsPortName string
if useVethPair {
ovsPortName = strings.Replace(intfName, "port", "vport", 1)
} else {
ovsPortName = intfName
}
return ovsPortName
}
// CreatePort creates a port in ovs switch
func (sw *OvsSwitch) CreatePort(intfName string, cfgEp *mastercfg.CfgEndpointState, pktTag int) error {
var ovsIntfType string
// Get OVS port name
ovsPortName := getOvsPostName(intfName)
// Create Veth pairs if required
if useVethPair {
ovsIntfType = ""
// Create a Veth pair
err := createVethPair(intfName, ovsPortName)
if err != nil {
log.Errorf("Error creating veth pairs. Err: %v", err)
return err
}
// Set the OVS side of the port as up
err = setLinkUp(ovsPortName)
if err != nil {
log.Errorf("Error setting link %s up. Err: %v", ovsPortName, err)
return err
}
} else {
ovsPortName = intfName
ovsIntfType = "internal"
}
// Set the link mtu to 1450 to allow for 50 bytes vxlan encap
// (inner eth header(14) + outer IP(20) outer UDP(8) + vxlan header(8))
err := setLinkMtu(intfName, vxlanEndpointMtu)
if err != nil {
log.Errorf("Error setting link %s mtu. Err: %v", intfName, err)
return err
}
// Ask OVSDB driver to add the port
err = sw.ovsdbDriver.CreatePort(ovsPortName, ovsIntfType, cfgEp.ID, pktTag)
if err != nil {
return err
}
defer func() {
if err != nil {
sw.ovsdbDriver.DeletePort(intfName)
}
}()
// Wait a little for OVS to create the interface
time.Sleep(300 * time.Millisecond)
// Set the interface mac address
err = netutils.SetInterfaceMac(intfName, cfgEp.MacAddress)
if err != nil {
log.Errorf("Error setting interface Mac %s on port %s", cfgEp.MacAddress, intfName)
return err
}
// Add the endpoint to ofnet
if sw.netType == "vxlan" {
// Get the openflow port number for the interface
ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(ovsPortName)
if err != nil {
log.Errorf("Could not find the OVS port %s. Err: %v", ovsPortName, err)
return err
}
macAddr, _ := net.ParseMAC(cfgEp.MacAddress)
// Build the endpoint info
endpoint := ofnet.EndpointInfo{
PortNo: ofpPort,
MacAddr: macAddr,
Vlan: uint16(pktTag),
IpAddr: net.ParseIP(cfgEp.IPAddress),
EndpointGroup: cfgEp.EndpointGroupID,
}
log.Infof("Adding local endpoint: {%+v}", endpoint)
// Add the local port to ofnet
err = sw.ofnetAgent.AddLocalEndpoint(endpoint)
if err != nil {
log.Errorf("Error adding local port %s to ofnet. Err: %v", ovsPortName, err)
return err
}
}
return nil
}
// UpdatePort updates an OVS port without creating it
func (sw *OvsSwitch) UpdatePort(intfName string, cfgEp *mastercfg.CfgEndpointState, pktTag int) error {
// Get OVS port name
ovsPortName := getOvsPostName(intfName)
// Add the endpoint to ofnet
if sw.netType == "vxlan" {
// Get the openflow port number for the interface
ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(ovsPortName)
if err != nil {
log.Errorf("Could not find the OVS port %s. Err: %v", ovsPortName, err)
return err
}
macAddr, _ := net.ParseMAC(cfgEp.MacAddress)
// Build the endpoint info
endpoint := ofnet.EndpointInfo{
PortNo: ofpPort,
MacAddr: macAddr,
Vlan: uint16(pktTag),
IpAddr: net.ParseIP(cfgEp.IPAddress),
EndpointGroup: cfgEp.EndpointGroupID,
}
// Add the local port to ofnet
err = sw.ofnetAgent.AddLocalEndpoint(endpoint)
if err != nil {
log.Errorf("Error adding local port %s to ofnet. Err: %v", ovsPortName, err)
return err
}
}
return nil
}
// DeletePort removes a port from OVS
func (sw *OvsSwitch) DeletePort(epOper *OvsOperEndpointState) error {
if epOper.VtepIP != "" {
return nil
}
// Get the OVS port name
ovsPortName := getOvsPostName(epOper.PortName)
// Delete the Veth pairs if required
if useVethPair {
// Delete a Veth pair
err := deleteVethPair(ovsPortName, epOper.PortName)
if err != nil {
log.Errorf("Error creating veth pairs. Err: %v", err)
// Continue to cleanup remaining state
}
} else {
ovsPortName = epOper.PortName
}
// Remove info from ofnet
if sw.netType == "vxlan" {
// Get the openflow port number for the interface
ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(ovsPortName)
if err != nil {
log.Errorf("Could not find the OVS port %s. Err: %v", ovsPortName, err)
return err
}
err = sw.ofnetAgent.RemoveLocalEndpoint(ofpPort)
if err != nil {
log.Errorf("Error removing port %s from ofnet. Err: %v", ovsPortName, err)
}
}
// Delete it from ovsdb
err := sw.ovsdbDriver.DeletePort(ovsPortName)
if err != nil {
return err
}
return nil
}
// vxlanIfName returns formatted vxlan interface name
func vxlanIfName(vtepIP string) string {
return fmt.Sprintf(vxlanIfNameFmt, strings.Replace(vtepIP, ".", "", -1))
}
// CreateVtep creates a VTEP interface
func (sw *OvsSwitch) CreateVtep(vtepIP string) error {
// Create interface name for VTEP
intfName := vxlanIfName(vtepIP)
log.Infof("Creating VTEP intf %s for IP %s", intfName, vtepIP)
// Check if it already exists
isPresent, vsifName := sw.ovsdbDriver.IsVtepPresent(vtepIP)
if !isPresent || (vsifName != intfName) {
// Ask ovsdb to create it
err := sw.ovsdbDriver.CreateVtep(intfName, vtepIP)
if err != nil {
log.Errorf("Error creating VTEP port %s. Err: %v", intfName, err)
}
}
// Wait a little for OVS to create the interface
time.Sleep(300 * time.Millisecond)
// Get the openflow port number for the interface
ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(intfName)
if err != nil {
log.Errorf("Could not find the OVS port %s. Err: %v", intfName, err)
return err
}
// Add info about VTEP port to ofnet
err = sw.ofnetAgent.AddVtepPort(ofpPort, net.ParseIP(vtepIP))
if err != nil {
log.Errorf("Error adding VTEP port %s to ofnet. Err: %v", intfName, err)
return err
}
return nil
}
// DeleteVtep deletes a VTEP
func (sw *OvsSwitch) DeleteVtep(vtepIP string) error {
// Build vtep interface name
intfName := vxlanIfName(vtepIP)
log.Infof("Deleting VTEP intf %s for IP %s", intfName, vtepIP)
// Get the openflow port number for the interface
ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(intfName)
if err != nil {
log.Errorf("Could not find the OVS port %s. Err: %v", intfName, err)
return err
}
// Add info about VTEP port to ofnet
err = sw.ofnetAgent.RemoveVtepPort(ofpPort, net.ParseIP(vtepIP))
if err != nil {
log.Errorf("Error deleting VTEP port %s to ofnet. Err: %v", intfName, err)
return err
}
// ask ovsdb to delete the VTEP
return sw.ovsdbDriver.DeleteVtep(intfName)
}
// AddUplinkPort adds uplink port to the OVS
func (sw *OvsSwitch) AddUplinkPort(intfName string) error {
var err error
// some error checking
if sw.netType != "vlan" {
log.Fatalf("Can not add uplink to OVS type %s.", sw.netType)
}
uplinkID := "uplink" + intfName
// Check if port is already part of the OVS and add it
if !sw.ovsdbDriver.IsPortNamePresent(intfName) {
// Ask OVSDB driver to add the port as a trunk port
err = sw.ovsdbDriver.CreatePort(intfName, "", uplinkID, 0)
if err != nil {
log.Errorf("Error adding uplink %s to OVS. Err: %v", intfName, err)
return err
}
}
log.Infof("Added uplink %s to OVS switch %s.", intfName, sw.bridgeName)
defer func() {
if err != nil {
sw.ovsdbDriver.DeletePort(intfName)
}
}()
return nil
}
// AddMaster adds master node
func (sw *OvsSwitch) AddMaster(node core.ServiceInfo) error {
var resp bool
// Build master info
masterInfo := ofnet.OfnetNode{
HostAddr: node.HostAddr,
HostPort: uint16(node.Port),
}
// Add the master
err := sw.ofnetAgent.AddMaster(&masterInfo, &resp)
if err != nil {
log.Errorf("Error adding ofnet master %+v. Err: %v", masterInfo, err)
return err
}
return nil
}
// DeleteMaster deletes master node
func (sw *OvsSwitch) DeleteMaster(node core.ServiceInfo) error {
// Build master info
masterInfo := ofnet.OfnetNode{
HostAddr: node.HostAddr,
HostPort: uint16(node.Port),
}
// remove the master
err := sw.ofnetAgent.RemoveMaster(&masterInfo)
if err != nil {
log.Errorf("Error deleting ofnet master %+v. Err: %v", masterInfo, err)
return err
}
return nil
}