From 1ac37dd11eeb779286acaa6886097e82d22f284e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Fri, 22 Dec 2023 11:25:47 -0500 Subject: [PATCH] incusd/network/ovs: Port BridgePortAdd to OVSDB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- internal/server/network/ovs/ovs_actions.go | 64 ++++++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/internal/server/network/ovs/ovs_actions.go b/internal/server/network/ovs/ovs_actions.go index 5ac387a7bed..f4d29754780 100644 --- a/internal/server/network/ovs/ovs_actions.go +++ b/internal/server/network/ovs/ovs_actions.go @@ -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 }