-
Notifications
You must be signed in to change notification settings - Fork 5
/
ovsclient.go
278 lines (251 loc) · 7.04 KB
/
ovsclient.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
package goovs
import (
"fmt"
"net"
"reflect"
"strconv"
"sync"
"github.com/socketplane/libovsdb"
)
const (
defaultTCPHost = "127.0.0.1"
defaultTCPPort = 6640
defaultUnixEndpoint = "/var/run/openvswitch/db.sock"
)
const (
defaultOvsDB = "Open_vSwitch"
)
const (
ovsTableName = "Open_vSwitch"
bridgeTableName = "Bridge"
portTableName = "Port"
interfaceTableName = "Interface"
controllerTableName = "Controller"
//flowTableName = ""
)
const (
deleteOperation = "delete"
insertOperation = "insert"
mutateOperation = "mutate"
selectOperation = "select"
updateOperation = "update"
)
// OvsObject is the main interface represent an ovs object
type OvsObject interface {
ReadFromDBRow(row *libovsdb.Row) error
}
// OvsClient is the interface towards outside user
type OvsClient interface {
BridgeExists(brname string) (bool, error)
CreateBridge(brname string) error
DeleteBridge(brname string) error
UpdateBridgeController(brname, controller string) error
CreateInternalPort(brname, portname string, vlantag int) error
CreateVethPort(brname, portname string, vlantag int) error
CreatePatchPort(brname, portname, peername string) error
DeletePort(brname, porname string) error
UpdatePortTagByName(brname, portname string, vlantag int) error
FindAllPortsOnBridge(brname string) ([]string, error)
PortExistsOnBridge(portname, brname string) (bool, error)
RemoveInterfaceFromPort(portname, interfaceUUID string) error
Disconnect()
}
type ovsClient struct {
dbClient *libovsdb.OvsdbClient
bridgeCache map[string]*OvsBridge
portCache map[string]*OvsPort
interfaceCache map[string]*OvsInterface
}
var client *ovsClient
var cache map[string]map[string]libovsdb.Row
var ovsclient *ovsClient
var bridgeUpdateLock sync.RWMutex
var portUpdateLock sync.RWMutex
var intfUpdateLock sync.RWMutex
var bridgeCacheUpdateLock sync.RWMutex
var portCacheUpdateLock sync.RWMutex
var intfCacheUpdateLock sync.RWMutex
var populateCacheLock sync.RWMutex
// GetOVSClient is used for
func GetOVSClient(contype, endpoint string) (OvsClient, error) {
if client != nil {
return client, nil
}
var dbclient *libovsdb.OvsdbClient
var err error
switch contype {
case "tcp":
if endpoint == "" {
dbclient, err = libovsdb.Connect(defaultTCPHost, defaultTCPPort)
} else {
var host, port string
host, port, err = net.SplitHostPort(endpoint)
if err != nil {
return nil, err
}
var portInt int
if portInt, err = strconv.Atoi(port); err != nil {
return nil, err
}
dbclient, err = libovsdb.Connect(host, portInt)
}
case "unix":
if endpoint == "" {
endpoint = defaultUnixEndpoint
}
dbclient, err = libovsdb.ConnectWithUnixSocket(endpoint)
default:
return nil, fmt.Errorf("GetOVSClient: Unsupported connection type %q.", contype)
}
if err != nil {
return nil, err
}
var notfr notifier
dbclient.Register(notfr)
cache = make(map[string]map[string]libovsdb.Row)
client = &ovsClient{dbClient: dbclient}
if client.bridgeCache == nil {
client.bridgeCache = make(map[string]*OvsBridge)
}
if client.portCache == nil {
client.portCache = make(map[string]*OvsPort)
}
if client.interfaceCache == nil {
client.interfaceCache = make(map[string]*OvsInterface)
}
var initial *libovsdb.TableUpdates
initial, err = dbclient.MonitorAll(defaultOvsDB, "")
if err != nil {
return nil, err
}
populateCache(*initial)
return client, nil
}
func (client *ovsClient) Disconnect() {
client.dbClient.Disconnect()
}
func (client *ovsClient) transact(operations []libovsdb.Operation, action string) error {
reply, _ := client.dbClient.Transact(defaultOvsDB, operations...)
if len(reply) < len(operations) {
return fmt.Errorf("%s failed due to Number of Replies should be at least equal to number of Operations", action)
}
//ok := true
for i, o := range reply {
if o.Error != "" {
//ok = false
if i < len(operations) {
return fmt.Errorf("%s transaction Failed due to an error : %s details: %s in %+v", action, o.Error, o.Details, operations[i])
}
return fmt.Errorf("%s transaction Failed due to an error :%s", action, o.Error)
}
}
//if ok {
// log.Println(action, "successful: ", reply[0].UUID.GoUUID)
//}
return nil
}
type notifier struct {
}
func (n notifier) Update(context interface{}, tableUpdates libovsdb.TableUpdates) {
populateCache(tableUpdates)
}
func (n notifier) Locked([]interface{}) {
}
func (n notifier) Stolen([]interface{}) {
}
func (n notifier) Echo([]interface{}) {
}
func (n notifier) Disconnect([]interface{}) {
}
func (n notifier) Disconnected(*libovsdb.OvsdbClient) {
}
func (client *ovsClient) updateOvsObjCacheByRow(objtype, uuid string, row *libovsdb.Row) (err error) {
switch objtype {
case bridgeTableName:
brObj := &OvsBridge{UUID: uuid}
if err = brObj.ReadFromDBRow(row); err != nil {
return
}
bridgeCacheUpdateLock.Lock()
client.bridgeCache[uuid] = brObj
bridgeCacheUpdateLock.Unlock()
//data, _ := json.MarshalIndent(brObj, "", " ")
//fmt.Println(string(data))
case portTableName:
portObj := &OvsPort{UUID: uuid}
if err = portObj.ReadFromDBRow(row); err != nil {
return
}
portCacheUpdateLock.Lock()
client.portCache[uuid] = portObj
portCacheUpdateLock.Unlock()
//data, _ := json.MarshalIndent(portObj, "", " ")
//fmt.Println(string(data))
case interfaceTableName:
intfObj := &OvsInterface{UUID: uuid}
if err = intfObj.ReadFromDBRow(row); err != nil {
return
}
intfCacheUpdateLock.Lock()
client.interfaceCache[uuid] = intfObj
intfCacheUpdateLock.Unlock()
//data, _ := json.MarshalIndent(intfObj, "", " ")
//fmt.Println(string(data))
}
return
}
func (client *ovsClient) removeOvsObjCacheByRow(objtype, uuid string) error {
switch objtype {
case bridgeTableName:
if _, ok := client.bridgeCache[uuid]; ok {
bridgeCacheUpdateLock.Lock()
delete(client.bridgeCache, uuid)
bridgeCacheUpdateLock.Unlock()
}
case portTableName:
if _, ok := client.portCache[uuid]; ok {
portCacheUpdateLock.Lock()
delete(client.portCache, uuid)
portCacheUpdateLock.Unlock()
}
case interfaceTableName:
if _, ok := client.interfaceCache[uuid]; ok {
intfCacheUpdateLock.Lock()
delete(client.interfaceCache, uuid)
intfCacheUpdateLock.Unlock()
}
}
return nil
}
func populateCache(updates libovsdb.TableUpdates) (err error) {
populateCacheLock.Lock()
defer populateCacheLock.Unlock()
for table, tableUpdate := range updates.Updates {
if _, ok := cache[table]; !ok {
cache[table] = make(map[string]libovsdb.Row)
}
for uuid, row := range tableUpdate.Rows {
empty := libovsdb.Row{}
if !reflect.DeepEqual(row.New, empty) {
// fmt.Println(table + " with uuid " + uuid + "is updated")
cache[table][uuid] = row.New
if err = client.updateOvsObjCacheByRow(table, uuid, &row.New); err != nil {
return
}
} else {
delete(cache[table], uuid)
// fmt.Println(table + " with uuid " + uuid + "is removed")
if err = client.removeOvsObjCacheByRow(table, uuid); err != nil {
return
}
}
}
}
return
}
func getRootUUID() string {
for uuid := range cache[defaultOvsDB] {
return uuid
}
return ""
}