forked from antrea-io/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace contiv/libovsdb with ovn-org/libovsdb
contiv/libovsdb is abandoned and we were using a version from 2017 Instead we use github.com/ovn-org/libovsdb, which is still under active development and is used by ovn-kubernetes. We also move the ovsdbDriver code to an internal package: it is only used for integration tests and does not need to be part of the public API. All the functions that were not used anywhere are also removed. Bump up version to v0.13.0. Fixes antrea-io#62 Signed-off-by: Antonin Bas <[email protected]>
- Loading branch information
1 parent
a73fa6b
commit ebd672b
Showing
11 changed files
with
831 additions
and
740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.12.0 | ||
v0.13.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ovn-org/libovsdb/client" | ||
"github.com/ovn-org/libovsdb/model" | ||
"github.com/ovn-org/libovsdb/ovsdb" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"antrea.io/ofnet/ofctrl/internal/vswitchd" | ||
) | ||
|
||
// OVS driver state | ||
type OvsDriver struct { | ||
// OVS client | ||
client client.Client | ||
|
||
// Name of the OVS bridge | ||
brName string | ||
|
||
rootUUID string | ||
} | ||
|
||
func newOvsDriver(ctx context.Context, bridgeName string) (*OvsDriver, error) { | ||
dbModel, err := vswitchd.Model() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ovs, err := client.NewOVSDBClient(dbModel, client.WithEndpoint("tcp:localhost:6640")) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create client: %w", err) | ||
} | ||
if err := ovs.Connect(ctx); err != nil { | ||
return nil, fmt.Errorf("failed to connect to ovsdb: %w", err) | ||
} | ||
|
||
if _, err := ovs.Monitor( | ||
ctx, | ||
ovs.NewMonitor( | ||
client.WithTable(&vswitchd.OpenvSwitch{}), | ||
client.WithTable(&vswitchd.Bridge{}), | ||
), | ||
); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get root UUID | ||
var rootUUID string | ||
for uuid := range ovs.Cache().Table("Open_vSwitch").Rows() { | ||
rootUUID = uuid | ||
} | ||
|
||
ovsDriver := &OvsDriver{ | ||
client: ovs, | ||
brName: bridgeName, | ||
rootUUID: rootUUID, | ||
} | ||
if err := ovsDriver.createBridge(ctx); err != nil { | ||
return nil, fmt.Errorf("failed to create bridge: %w", err) | ||
} | ||
return ovsDriver, nil | ||
} | ||
|
||
// Create a new OVS driver | ||
func NewOvsDriver(bridgeName string) *OvsDriver { | ||
ovsDriver, err := newOvsDriver(context.Background(), bridgeName) | ||
if err != nil { | ||
log.Fatalf("fail to create OVS driver: %v", err) | ||
} | ||
return ovsDriver | ||
} | ||
|
||
// Delete removes the bridge and disconnects the client | ||
func (d *OvsDriver) Delete() error { | ||
if d.client != nil { | ||
if err := d.deleteBridge(context.Background()); err != nil { | ||
return fmt.Errorf("error when deleting bridge %s: %w", d.brName, err) | ||
} | ||
log.Infof("Deleting OVS bridge: %s", d.brName) | ||
d.client.Disconnect() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Wrapper for ovsDB transaction | ||
func (d *OvsDriver) ovsdbTransact(ctx context.Context, ops []ovsdb.Operation) error { | ||
// Print out what we are sending | ||
log.Debugf("Transaction: %+v\n", ops) | ||
|
||
// Perform OVSDB transaction | ||
reply, err := d.client.Transact(ctx, ops...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := ovsdb.CheckOperationResults(reply, ops); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *OvsDriver) createBridge(ctx context.Context) error { | ||
// If the bridge already exists, just return | ||
// FIXME: should we delete the old bridge and create new one? | ||
if _, ok := d.getBridgeUUID(); ok { | ||
return nil | ||
} | ||
|
||
const brNamedUUID = "testbr" | ||
|
||
protocols := []vswitchd.BridgeProtocols{ | ||
vswitchd.BridgeProtocolsOpenflow10, | ||
vswitchd.BridgeProtocolsOpenflow11, | ||
vswitchd.BridgeProtocolsOpenflow12, | ||
vswitchd.BridgeProtocolsOpenflow13, | ||
vswitchd.BridgeProtocolsOpenflow14, | ||
vswitchd.BridgeProtocolsOpenflow15, | ||
} | ||
br := &vswitchd.Bridge{ | ||
UUID: brNamedUUID, | ||
FailMode: &vswitchd.BridgeFailModeSecure, | ||
Name: d.brName, | ||
Protocols: protocols, | ||
} | ||
|
||
insertOps, err := d.client.Create(br) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Inserting/Deleting a Bridge row in Bridge table requires mutating | ||
// the open_vswitch table. | ||
ovsRow := vswitchd.OpenvSwitch{ | ||
UUID: d.rootUUID, | ||
} | ||
mutateOps, err := d.client.Where(&ovsRow).Mutate(&ovsRow, model.Mutation{ | ||
Field: &ovsRow.Bridges, | ||
Mutator: "insert", | ||
Value: []string{br.UUID}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ops := append(insertOps, mutateOps...) | ||
|
||
return d.ovsdbTransact(ctx, ops) | ||
} | ||
|
||
func (d *OvsDriver) deleteBridge(ctx context.Context) error { | ||
uuid, ok := d.getBridgeUUID() | ||
if !ok { | ||
return nil | ||
} | ||
br := &vswitchd.Bridge{ | ||
UUID: uuid, | ||
} | ||
|
||
deleteOps, err := d.client.Where(br).Delete() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Inserting/Deleting a Bridge row in Bridge table requires mutating | ||
// the open_vswitch table. | ||
ovsRow := vswitchd.OpenvSwitch{ | ||
UUID: d.rootUUID, | ||
} | ||
mutateOps, err := d.client.Where(&ovsRow).Mutate(&ovsRow, model.Mutation{ | ||
Field: &ovsRow.Bridges, | ||
Mutator: "delete", | ||
Value: []string{br.UUID}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ops := append(deleteOps, mutateOps...) | ||
|
||
return d.ovsdbTransact(ctx, ops) | ||
} | ||
|
||
func (d *OvsDriver) getBridgeUUID() (string, bool) { | ||
rows := d.client.Cache().Table("Bridge").Rows() | ||
for uuid, m := range rows { | ||
br := m.(*vswitchd.Bridge) | ||
if br.Name == d.brName { | ||
return uuid, true | ||
} | ||
} | ||
return "", false | ||
} | ||
|
||
func (d *OvsDriver) BridgeName() string { | ||
return d.brName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package vswitchd | ||
|
||
type ( | ||
BridgeFailMode = string | ||
BridgeProtocols = string | ||
) | ||
|
||
var ( | ||
BridgeFailModeStandalone BridgeFailMode = "standalone" | ||
BridgeFailModeSecure BridgeFailMode = "secure" | ||
BridgeProtocolsOpenflow10 BridgeProtocols = "OpenFlow10" | ||
BridgeProtocolsOpenflow11 BridgeProtocols = "OpenFlow11" | ||
BridgeProtocolsOpenflow12 BridgeProtocols = "OpenFlow12" | ||
BridgeProtocolsOpenflow13 BridgeProtocols = "OpenFlow13" | ||
BridgeProtocolsOpenflow14 BridgeProtocols = "OpenFlow14" | ||
BridgeProtocolsOpenflow15 BridgeProtocols = "OpenFlow15" | ||
) | ||
|
||
// Bridge defines an object in Bridge table | ||
// We only include the fields we need | ||
type Bridge struct { | ||
UUID string `ovsdb:"_uuid"` | ||
// Include these fields (DatapathID, DatapathVersion) so that libovsdb | ||
// does not complain about missing model fields. | ||
DatapathID *string `ovsdb:"datapath_id"` | ||
DatapathType string `ovsdb:"datapath_type"` | ||
DatapathVersion string `ovsdb:"datapath_version"` | ||
FailMode *BridgeFailMode `ovsdb:"fail_mode"` | ||
Name string `ovsdb:"name"` | ||
Protocols []BridgeProtocols `ovsdb:"protocols"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package vswitchd | ||
|
||
import ( | ||
"github.com/ovn-org/libovsdb/model" | ||
) | ||
|
||
func Model() (model.ClientDBModel, error) { | ||
return model.NewClientDBModel("Open_vSwitch", map[string]model.Model{ | ||
"Bridge": &Bridge{}, | ||
"Open_vSwitch": &OpenvSwitch{}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package vswitchd | ||
|
||
// OpenvSwitch defines an object in Open_vSwitch table | ||
// We only include the fields we need | ||
type OpenvSwitch struct { | ||
UUID string `ovsdb:"_uuid"` | ||
Bridges []string `ovsdb:"bridges"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.