forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace contiv/libovsdb with ovn-org/libovsdb #76
Open
antoninbas
wants to merge
1
commit into
antrea-io:main
Choose a base branch
from
antoninbas:replace-contiv/libovsdb-with-ovn-org/libovsdb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe use
instead of matching on rootUUID, this way we don't need to maintain the rootUUID.