Skip to content

Commit

Permalink
incusd/network/ovs: Port BridgePortAdd to OVSDB
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Dec 22, 2023
1 parent f589625 commit 1ac37dd
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions internal/server/network/ovs/ovs_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,69 @@ func (o *VSwitch) BridgeDelete(bridgeName string) error {

// BridgePortAdd adds a port to the bridge (if already attached does nothing).
func (o *VSwitch) BridgePortAdd(bridgeName string, portName string, mayExist bool) error {
args := []string{}
ctx := context.TODO()

if mayExist {
args = append(args, "--may-exist")
// Get the bridge.
bridge := ovsSwitch.Bridge{
Name: bridgeName,
}

err := o.client.Get(ctx, &bridge)
if err != nil {
return err
}

// Create the interface.
iface := ovsSwitch.Interface{
UUID: "interface",
Name: portName,
}

args = append(args, "add-port", bridgeName, portName)
interfaceOps, err := o.client.Create(&iface)
if err != nil {
return err
}

_, err := subprocess.RunCommand("ovs-vsctl", args...)
// Create the port.
port := ovsSwitch.Port{
Name: portName,
}

err = o.client.Get(ctx, &port)
if err != nil && err != ovsdbClient.ErrNotFound {
return err
}

if port.UUID != "" {
if mayExist {
// Already exists.
return nil
}

return fmt.Errorf("OVS port %q already exists on %q", portName, bridgeName)
}

port.UUID = "port"
port.Interfaces = []string{iface.UUID}
portOps, err := o.client.Create(&port)
if err != nil {
return err
}

// Create the bridge port entry.
mutateOps, err := o.client.Where(&bridge).Mutate(&bridge, ovsdbModel.Mutation{
Field: &bridge.Ports,
Mutator: ovsdb.MutateOperationInsert,
Value: []string{port.UUID},
})
if err != nil {
return err
}

operations := append(interfaceOps, portOps...)
operations = append(operations, mutateOps...)

_, err = o.client.Transact(ctx, operations...)
if err != nil {
return err
}
Expand Down

0 comments on commit 1ac37dd

Please sign in to comment.