Skip to content

Commit

Permalink
Merge pull request #584 from stgraber/ovn
Browse files Browse the repository at this point in the history
Port more OVN functions to libovsdb
  • Loading branch information
hallyn authored Mar 5, 2024
2 parents f6c389c + e519d41 commit 6ee49ff
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 35 deletions.
42 changes: 27 additions & 15 deletions internal/server/network/driver_ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2112,17 +2112,21 @@ func (n *ovn) setup(update bool) error {
}

if !update {
revert.Add(func() { _ = n.state.OVNNB.LogicalRouterPortDelete(n.getRouterExtPortName()) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalRouterPort(context.TODO(), n.getRouterName(), n.getRouterExtPortName())
})
}

// Create external switch port and link to router port.
err = n.state.OVNNB.LogicalSwitchPortAdd(n.getExtSwitchName(), n.getExtSwitchRouterPortName(), nil, update)
err = n.state.OVNNB.CreateLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchRouterPortName(), nil, update)
if err != nil {
return fmt.Errorf("Failed adding external switch router port: %w", err)
}

if !update {
revert.Add(func() { _ = n.state.OVNNB.LogicalSwitchPortDelete(n.getExtSwitchRouterPortName()) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchRouterPortName())
})
}

err = n.state.OVNNB.LogicalSwitchPortLinkRouter(n.getExtSwitchRouterPortName(), n.getRouterExtPortName())
Expand All @@ -2131,13 +2135,15 @@ func (n *ovn) setup(update bool) error {
}

// Create external switch port and link to external provider network.
err = n.state.OVNNB.LogicalSwitchPortAdd(n.getExtSwitchName(), n.getExtSwitchProviderPortName(), nil, update)
err = n.state.OVNNB.CreateLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchProviderPortName(), nil, update)
if err != nil {
return fmt.Errorf("Failed adding external switch provider port: %w", err)
}

if !update {
revert.Add(func() { _ = n.state.OVNNB.LogicalSwitchPortDelete(n.getExtSwitchProviderPortName()) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchProviderPortName())
})
}

err = n.state.OVNNB.LogicalSwitchPortLinkProviderNetwork(n.getExtSwitchProviderPortName(), uplinkNet.extSwitchProviderName)
Expand Down Expand Up @@ -2340,7 +2346,9 @@ func (n *ovn) setup(update bool) error {
}

if !update {
revert.Add(func() { _ = n.state.OVNNB.LogicalRouterPortDelete(n.getRouterIntPortName()) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalRouterPort(context.TODO(), n.getRouterName(), n.getRouterIntPortName())
})
}

// Configure DHCP option sets.
Expand Down Expand Up @@ -2459,13 +2467,15 @@ func (n *ovn) setup(update bool) error {
}

// Create internal switch port and link to router port.
err = n.state.OVNNB.LogicalSwitchPortAdd(n.getIntSwitchName(), n.getIntSwitchRouterPortName(), nil, update)
err = n.state.OVNNB.CreateLogicalSwitchPort(context.TODO(), n.getIntSwitchName(), n.getIntSwitchRouterPortName(), nil, update)
if err != nil {
return fmt.Errorf("Failed adding internal switch router port: %w", err)
}

if !update {
revert.Add(func() { _ = n.state.OVNNB.LogicalSwitchPortDelete(n.getIntSwitchRouterPortName()) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getIntSwitchName(), n.getIntSwitchRouterPortName())
})
}

err = n.state.OVNNB.LogicalSwitchPortLinkRouter(n.getIntSwitchRouterPortName(), n.getRouterIntPortName())
Expand Down Expand Up @@ -2722,27 +2732,27 @@ func (n *ovn) Delete(clientType request.ClientType) error {
return err
}

err = n.state.OVNNB.LogicalRouterPortDelete(n.getRouterExtPortName())
err = n.state.OVNNB.DeleteLogicalRouterPort(context.TODO(), n.getRouterName(), n.getRouterExtPortName())
if err != nil {
return err
}

err = n.state.OVNNB.LogicalRouterPortDelete(n.getRouterIntPortName())
err = n.state.OVNNB.DeleteLogicalRouterPort(context.TODO(), n.getRouterName(), n.getRouterIntPortName())
if err != nil {
return err
}

err = n.state.OVNNB.LogicalSwitchPortDelete(n.getExtSwitchRouterPortName())
err = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchRouterPortName())
if err != nil {
return err
}

err = n.state.OVNNB.LogicalSwitchPortDelete(n.getExtSwitchProviderPortName())
err = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getExtSwitchName(), n.getExtSwitchProviderPortName())
if err != nil {
return err
}

err = n.state.OVNNB.LogicalSwitchPortDelete(n.getIntSwitchRouterPortName())
err = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getIntSwitchName(), n.getIntSwitchRouterPortName())
if err != nil {
return err
}
Expand Down Expand Up @@ -3634,7 +3644,7 @@ func (n *ovn) InstanceDevicePortStart(opts *OVNInstanceNICSetupOpts, securityACL
// to configure the port as needed. This is required in case the OVN northbound database was unavailable
// when the instance NIC was stopped and was unable to remove the port on last stop, which would otherwise
// prevent future NIC starts.
err = n.state.OVNNB.LogicalSwitchPortAdd(n.getIntSwitchName(), instancePortName, &networkOVN.OVNSwitchPortOpts{
err = n.state.OVNNB.CreateLogicalSwitchPort(context.TODO(), n.getIntSwitchName(), instancePortName, &networkOVN.OVNSwitchPortOpts{
DHCPv4OptsID: dhcpV4ID,
DHCPv6OptsID: dhcpv6ID,
MAC: mac,
Expand All @@ -3647,7 +3657,9 @@ func (n *ovn) InstanceDevicePortStart(opts *OVNInstanceNICSetupOpts, securityACL
return "", nil, err
}

revert.Add(func() { _ = n.state.OVNNB.LogicalSwitchPortDelete(instancePortName) })
revert.Add(func() {
_ = n.state.OVNNB.DeleteLogicalSwitchPort(context.TODO(), n.getIntSwitchName(), instancePortName)
})

// Add DNS records for port's IPs, and retrieve the IP addresses used.
var dnsIPv4, dnsIPv6 net.IP
Expand Down
190 changes: 170 additions & 20 deletions internal/server/network/ovn/ovn_nb_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,56 @@ func (o *NB) CreateLogicalRouterPort(ctx context.Context, routerName OVNRouter,
return nil
}

// LogicalRouterPortDelete deletes a named logical router port from a logical router.
func (o *NB) LogicalRouterPortDelete(portName OVNRouterPort) error {
_, err := o.nbctl("--if-exists", "lrp-del", string(portName))
// DeleteLogicalRouterPort deletes a named logical router port from a logical router.
func (o *NB) DeleteLogicalRouterPort(ctx context.Context, routerName OVNRouter, portName OVNRouterPort) error {
operations := []ovsdb.Operation{}

// Get the logical router port.
logicalRouterPort := ovnNB.LogicalRouterPort{
Name: string(portName),
}

err := o.get(ctx, &logicalRouterPort)
if err != nil {
// Logical router port is already gone.
if err == ErrNotFound {
return nil
}

return err
}

// Remove the port from the router.
logicalRouter := ovnNB.LogicalRouter{
Name: string(routerName),
}

updateOps, err := o.client.Where(&logicalRouter).Mutate(&logicalRouter, ovsModel.Mutation{
Field: &logicalRouter.Ports,
Mutator: ovsdb.MutateOperationDelete,
Value: []string{logicalRouterPort.UUID},
})
if err != nil {
return err
}

operations = append(operations, updateOps...)

// Delete the port itself.
deleteOps, err := o.client.Where(&logicalRouterPort).Delete()
if err != nil {
return err
}

operations = append(operations, deleteOps...)

// Apply the changes.
resp, err := o.client.Transact(ctx, operations...)
if err != nil {
return err
}

_, err = ovsdb.CheckOperationResults(resp, operations)
if err != nil {
return err
}
Expand Down Expand Up @@ -1127,23 +1174,35 @@ func (o *NB) LogicalSwitchPortUUID(portName OVNSwitchPort) (OVNSwitchPortUUID, e
return "", nil
}

// LogicalSwitchPortAdd adds a named logical switch port to a logical switch, and sets options if provided.
// CreateLogicalSwitchPort adds a named logical switch port to a logical switch, and sets options if provided.
// If mayExist is true, then an existing resource of the same name is not treated as an error.
func (o *NB) LogicalSwitchPortAdd(switchName OVNSwitch, portName OVNSwitchPort, opts *OVNSwitchPortOpts, mayExist bool) error {
args := []string{}
func (o *NB) CreateLogicalSwitchPort(ctx context.Context, switchName OVNSwitch, portName OVNSwitchPort, opts *OVNSwitchPortOpts, mayExist bool) error {
// Prepare the new switch port entry.
logicalSwitchPort := ovnNB.LogicalSwitchPort{
Name: string(portName),
UUID: "lsp",
ExternalIDs: map[string]string{},
}

if mayExist {
args = append(args, "--may-exist")
// Check if the entry already exists.
err := o.get(ctx, &logicalSwitchPort)
if err != nil && err != ErrNotFound {
return err
}

// Add switch port.
args = append(args, "lsp-add", string(switchName), string(portName))
if logicalSwitchPort.UUID != "lsp" && !mayExist {
return ErrExists
}

// Set switch port options if supplied.
if opts != nil {
// Created nested VLAN port if requested.
if opts.Parent != "" {
args = append(args, string(opts.Parent), fmt.Sprintf("%d", opts.VLAN))
parentName := string(opts.Parent)
tag := int(opts.VLAN)

logicalSwitchPort.ParentName = &parentName
logicalSwitchPort.Tag = &tag
}

ipStr := make([]string, 0, len(opts.IPs))
Expand All @@ -1160,24 +1219,68 @@ func (o *NB) LogicalSwitchPortAdd(switchName OVNSwitch, portName OVNSwitchPort,
addresses = "dynamic"
}

args = append(args, "--", "lsp-set-addresses", string(portName), addresses)
logicalSwitchPort.Addresses = []string{addresses}

if opts.DHCPv4OptsID != "" {
args = append(args, "--", "lsp-set-dhcpv4-options", string(portName), string(opts.DHCPv4OptsID))
dhcp4opts := string(opts.DHCPv4OptsID)
logicalSwitchPort.Dhcpv4Options = &dhcp4opts
}

if opts.DHCPv6OptsID != "" {
args = append(args, "--", "lsp-set-dhcpv6-options", string(portName), string(opts.DHCPv6OptsID))
dhcp6opts := string(opts.DHCPv6OptsID)
logicalSwitchPort.Dhcpv6Options = &dhcp6opts
}

if opts.Location != "" {
args = append(args, "--", "set", "logical_switch_port", string(portName), fmt.Sprintf("external_ids:%s=%s", ovnExtIDIncusLocation, opts.Location))
logicalSwitchPort.ExternalIDs[ovnExtIDIncusLocation] = opts.Location
}
}

args = append(args, "--", "set", "logical_switch_port", string(portName), fmt.Sprintf("external_ids:%s=%s", ovnExtIDIncusSwitch, switchName))
logicalSwitchPort.ExternalIDs[ovnExtIDIncusSwitch] = string(switchName)

_, err := o.nbctl(args...)
// Apply the changes.
operations := []ovsdb.Operation{}
if logicalSwitchPort.UUID != "lsp" {
// If it already exists, update it.
updateOps, err := o.client.Where(&logicalSwitchPort).Update(&logicalSwitchPort)
if err != nil {
return err
}

operations = append(operations, updateOps...)
} else {
// Else, create it.
createOps, err := o.client.Create(&logicalSwitchPort)
if err != nil {
return err
}

operations = append(operations, createOps...)

// And connect it to the switch.
logicalSwitch := ovnNB.LogicalSwitch{
Name: string(switchName),
}

updateOps, err := o.client.Where(&logicalSwitch).Mutate(&logicalSwitch, ovsModel.Mutation{
Field: &logicalSwitch.Ports,
Mutator: ovsdb.MutateOperationInsert,
Value: []string{logicalSwitchPort.UUID},
})
if err != nil {
return err
}

operations = append(operations, updateOps...)
}

// Apply the database changes.
resp, err := o.client.Transact(ctx, operations...)
if err != nil {
return err
}

_, err = ovsdb.CheckOperationResults(resp, operations)
if err != nil {
return err
}
Expand Down Expand Up @@ -1415,9 +1518,56 @@ func (o *NB) logicalSwitchPortDeleteAppendArgs(args []string, portName OVNSwitch
return args
}

// LogicalSwitchPortDelete deletes a named logical switch port.
func (o *NB) LogicalSwitchPortDelete(portName OVNSwitchPort) error {
_, err := o.nbctl(o.logicalSwitchPortDeleteAppendArgs(nil, portName)...)
// DeleteLogicalSwitchPort deletes a named logical switch port.
func (o *NB) DeleteLogicalSwitchPort(ctx context.Context, switchName OVNSwitch, portName OVNSwitchPort) error {
operations := []ovsdb.Operation{}

// Get the logical switch port.
logicalSwitchPort := ovnNB.LogicalSwitchPort{
Name: string(portName),
}

err := o.get(ctx, &logicalSwitchPort)
if err != nil {
// Logical switch port is already gone.
if err == ErrNotFound {
return nil
}

return err
}

// Remove the port from the switch.
logicalSwitch := ovnNB.LogicalSwitch{
Name: string(switchName),
}

updateOps, err := o.client.Where(&logicalSwitch).Mutate(&logicalSwitch, ovsModel.Mutation{
Field: &logicalSwitch.Ports,
Mutator: ovsdb.MutateOperationDelete,
Value: []string{logicalSwitchPort.UUID},
})
if err != nil {
return err
}

operations = append(operations, updateOps...)

// Delete the port itself.
deleteOps, err := o.client.Where(&logicalSwitchPort).Delete()
if err != nil {
return err
}

operations = append(operations, deleteOps...)

// Apply the changes.
resp, err := o.client.Transact(ctx, operations...)
if err != nil {
return err
}

_, err = ovsdb.CheckOperationResults(resp, operations)
if err != nil {
return err
}
Expand Down

0 comments on commit 6ee49ff

Please sign in to comment.