-
Notifications
You must be signed in to change notification settings - Fork 5
/
ovsbridge.go
260 lines (228 loc) · 7.23 KB
/
ovsbridge.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
package goovs
import (
"fmt"
"github.com/socketplane/libovsdb"
)
// OvsBridge is the structure represents the ovs bridge
type OvsBridge struct {
UUID string `json:"_uuid"`
Controller string `json:"controller"`
Name string `json:"name"`
PortUUIDs []string `json:"ports"`
DatapathID string `json:"datapath_id"`
}
// ReadFromDBRow is used to initialize the object from a row
func (bridge *OvsBridge) ReadFromDBRow(row *libovsdb.Row) error {
if bridge.PortUUIDs == nil {
bridge.PortUUIDs = make([]string, 0)
}
for field, value := range row.Fields {
switch field {
case "name":
bridge.Name = value.(string)
case "datapath_id":
switch value.(type) {
case string:
bridge.DatapathID = value.(string)
}
case "ports":
switch value.(type) {
case libovsdb.UUID:
bridge.PortUUIDs = append(bridge.PortUUIDs, value.(libovsdb.UUID).GoUUID)
case libovsdb.OvsSet:
for _, uuids := range value.(libovsdb.OvsSet).GoSet {
bridge.PortUUIDs = append(bridge.PortUUIDs, uuids.(libovsdb.UUID).GoUUID)
}
}
}
}
return nil
}
// CreateBridge is used to create a ovs bridge
func (client *ovsClient) CreateBridge(brname string) error {
bridgeUpdateLock.Lock()
defer bridgeUpdateLock.Unlock()
bridgeExists, err := client.BridgeExists(brname)
if err != nil {
return fmt.Errorf("Failed to retrieve the bridge info")
} else if bridgeExists {
return nil
}
namedBridgeUUID := "gobridge"
namedPortUUID := "goport"
namedInterfaceUUID := "gointerface"
// intf row to insert
intf := make(map[string]interface{})
intf["name"] = brname
intf["type"] = `internal`
insertInterfaceOp := libovsdb.Operation{
Op: "insert",
Table: "Interface",
Row: intf,
UUIDName: namedInterfaceUUID,
}
// port row to insert
port := make(map[string]interface{})
port["name"] = brname
port["interfaces"] = libovsdb.UUID{GoUUID: namedInterfaceUUID}
insertPortOp := libovsdb.Operation{
Op: insertOperation,
Table: portTableName,
Row: port,
UUIDName: namedPortUUID,
}
// bridge row to insert
bridge := make(map[string]interface{})
bridge["name"] = brname
bridge["stp_enable"] = false
bridge["ports"] = libovsdb.UUID{GoUUID: namedPortUUID}
// simple insert operation
insertBridgeOp := libovsdb.Operation{
Op: insertOperation,
Table: bridgeTableName,
Row: bridge,
UUIDName: namedBridgeUUID,
}
// Inserting a Bridge row in Bridge table requires mutating the open_vswitch table
mutateUUID := []libovsdb.UUID{libovsdb.UUID{GoUUID: namedBridgeUUID}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("bridges", insertOperation, mutateSet)
condition := libovsdb.NewCondition("_uuid", "==", libovsdb.UUID{GoUUID: getRootUUID()})
// simple mutate operation
mutateOp := libovsdb.Operation{
Op: mutateOperation,
Table: ovsTableName,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{insertInterfaceOp, insertPortOp, insertBridgeOp, mutateOp}
return client.transact(operations, "create bridge")
}
// DeleteBridge is used to delete a ovs bridge
func (client *ovsClient) DeleteBridge(brname string) error {
bridgeUpdateLock.Lock()
defer bridgeUpdateLock.Unlock()
bridgeExists, err := client.BridgeExists(brname)
if err != nil {
return fmt.Errorf("Failed to retrieve the bridge info")
} else if !bridgeExists {
return nil
}
bridgeUUID, err := client.getBridgeUUIDByName(brname)
if err != nil {
return err
}
delBridgeCondition := libovsdb.NewCondition("name", "==", brname)
deleteOp := libovsdb.Operation{
Op: deleteOperation,
Table: bridgeTableName,
Where: []interface{}{delBridgeCondition},
}
// Deleting a Bridge row in Bridge table requires mutating the open_vswitch table
mutateUUID := []libovsdb.UUID{libovsdb.UUID{GoUUID: bridgeUUID}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("bridges", deleteOperation, mutateSet)
condition := libovsdb.NewCondition("_uuid", "==", libovsdb.UUID{GoUUID: getRootUUID()})
// simple mutate operation
mutateOp := libovsdb.Operation{
Op: mutateOperation,
Table: ovsTableName,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{deleteOp, mutateOp}
return client.transact(operations, "delete bridge")
}
func (client *ovsClient) deleteAllPortsOnBridge(brname string) error {
bridgeExists, err := client.BridgeExists(brname)
if err != nil {
return fmt.Errorf("Failed to retrieve the bridge info")
} else if !bridgeExists {
return nil
}
portList, err := client.findAllPortUUIDsOnBridge(brname)
if err != nil {
return err
}
if len(portList) != 0 {
for _, portUUID := range portList {
err = client.deletePortByUUID(brname, portUUID)
if err != nil {
return err
}
}
}
return nil
}
// BridgeExists is used to check if a bridge exists or not
func (client *ovsClient) BridgeExists(brname string) (bool, error) {
// if bridge name is invalid, return false
if brname == "" {
return false, fmt.Errorf("The bridge name is invalid")
}
bridgeCacheUpdateLock.RLock()
defer bridgeCacheUpdateLock.RUnlock()
for _, br := range client.bridgeCache {
if br.Name == brname {
return true, nil
}
}
return false, nil
}
func (client *ovsClient) UpdateBridgeController(brname, controller string) error {
bridgeUpdateLock.Lock()
defer bridgeUpdateLock.Unlock()
bridgeExists, err := client.BridgeExists(brname)
if err != nil {
return fmt.Errorf("Failed to retrieve the bridge info")
} else if !bridgeExists {
return nil
}
bridgeUUID, err := client.getBridgeUUIDByName(brname)
if err != nil {
return err
}
namedControllerUUID := "gocontroller"
ctrler := make(map[string]interface{})
ctrler["target"] = controller
insertControllerOp := libovsdb.Operation{
Op: insertOperation,
Table: controllerTableName,
Row: ctrler,
UUIDName: namedControllerUUID,
}
bridge := make(map[string]interface{})
bridge["controller"] = libovsdb.UUID{GoUUID: namedControllerUUID}
updateBrCondition := libovsdb.NewCondition("name", "==", brname)
updateOp := libovsdb.Operation{
Op: updateOperation,
Table: bridgeTableName,
Row: bridge,
Where: []interface{}{updateBrCondition},
}
// Update a Bridge row in Bridge table requires mutating the open_vswitch table
mutateUUID := []libovsdb.UUID{libovsdb.UUID{GoUUID: namedControllerUUID}}
mutateSet, _ := libovsdb.NewOvsSet(mutateUUID)
mutation := libovsdb.NewMutation("controller", insertOperation, mutateSet)
condition := libovsdb.NewCondition("_uuid", "==", libovsdb.UUID{GoUUID: bridgeUUID})
// simple mutate operation
mutateOp := libovsdb.Operation{
Op: mutateOperation,
Table: bridgeTableName,
Mutations: []interface{}{mutation},
Where: []interface{}{condition},
}
operations := []libovsdb.Operation{insertControllerOp, updateOp, mutateOp}
return client.transact(operations, "update bridge controller")
}
func (client *ovsClient) getBridgeUUIDByName(brname string) (string, error) {
if brname == "" {
return "", fmt.Errorf("The bridge name is invalid")
}
for uuid, bridge := range client.bridgeCache {
if bridge.Name == brname {
return uuid, nil
}
}
return "", fmt.Errorf("The brdige name %s doesn't exist", brname)
}