Skip to content

Commit

Permalink
incusd/network/ovn: Port GetLogicalRouterRoutes to libovsdb
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
Sponsored-by: Luizalabs (https://luizalabs.com)
  • Loading branch information
stgraber committed Jun 20, 2024
1 parent 3e199a1 commit 2f99b19
Showing 1 changed file with 30 additions and 44 deletions.
74 changes: 30 additions & 44 deletions internal/server/network/ovn/ovn_nb_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3257,73 +3257,59 @@ func (o *NB) UpdateLogicalRouterPolicy(ctx context.Context, routerName OVNRouter
return nil
}

// LogicalRouterRoutes returns a list of static routes in the main route table of the logical router.
func (o *NB) LogicalRouterRoutes(routerName OVNRouter) ([]OVNRouterRoute, error) {
output, err := o.nbctl("lr-route-list", string(routerName))
// GetLogicalRouterRoutes returns a list of static routes in the main route table of the logical router.
func (o *NB) GetLogicalRouterRoutes(ctx context.Context, routerName OVNRouter) ([]OVNRouterRoute, error) {
// Get the logical router.
lr, err := o.GetLogicalRouter(ctx, routerName)
if err != nil {
return nil, err
}

lines := util.SplitNTrimSpace(strings.TrimSpace(output), "\n", -1, true)
routes := make([]OVNRouterRoute, 0)

mainTable := true // Assume output starts with main table (supports ovn versions without multiple tables).
for i, line := range lines {
if line == "IPv4 Routes" || line == "IPv6 Routes" {
continue // Ignore heading category lines.
}

// Keep track of which route table we are looking at.
if strings.HasPrefix(line, "Route Table") {
if line == "Route Table <main>:" {
mainTable = true
} else {
mainTable = false
}

continue
// Get all the routes.
routes := []OVNRouterRoute{}
for _, routeUUID := range lr.StaticRoutes {
// Get the static entry.
route := ovnNB.LogicalRouterStaticRoute{
UUID: routeUUID,
}

if !mainTable {
continue // We don't currently consider routes in other route tables.
err = o.get(ctx, &route)
if err != nil {
return nil, err
}

// E.g. "10.97.31.0/24 10.97.31.1 dst-ip [optional-some-router-port-name]"
fields := strings.Fields(line)
fieldsLen := len(fields)

if fieldsLen <= 0 {
continue // Ignore empty lines.
} else if fieldsLen < 3 || fieldsLen > 4 {
return nil, fmt.Errorf("Unrecognised static route item output on line %d: %q", i, line)
// Only use the main table.
if route.RouteTable != "" {
continue
}

var route OVNRouterRoute
// Create the route entry.
var routerRoute OVNRouterRoute

// ovn-nbctl doesn't output single-host route prefixes in CIDR format, so do the conversion here.
ip := net.ParseIP(fields[0])
// Add CIDR if missing.
ip := net.ParseIP(route.IPPrefix)
if ip != nil {
subnetSize := 32
if ip.To4() == nil {
subnetSize = 128
}

fields[0] = fmt.Sprintf("%s/%d", ip.String(), subnetSize)
route.IPPrefix = fmt.Sprintf("%s/%d", ip.String(), subnetSize)
}

_, prefix, err := net.ParseCIDR(fields[0])
_, prefix, err := net.ParseCIDR(route.IPPrefix)
if err != nil {
return nil, fmt.Errorf("Invalid static route prefix on line %d: %q", i, fields[0])
return nil, fmt.Errorf("Invalid static route prefix %q", route.IPPrefix)
}

route.Prefix = *prefix
route.NextHop = net.ParseIP(fields[1])
routerRoute.Prefix = *prefix
routerRoute.NextHop = net.ParseIP(route.Nexthop)

if fieldsLen > 3 {
route.Port = OVNRouterPort(fields[3])
if route.OutputPort != nil {
routerRoute.Port = OVNRouterPort(*route.OutputPort)
}

routes = append(routes, route)
routes = append(routes, routerRoute)
}

return routes, nil
Expand Down Expand Up @@ -3438,7 +3424,7 @@ func (o *NB) LogicalRouterPeeringDelete(opts OVNRouterPeering) error {
}

// Remove static routes from both routers that use the respective peering router ports.
staticRoutes, err := o.LogicalRouterRoutes(opts.LocalRouter)
staticRoutes, err := o.GetLogicalRouterRoutes(context.TODO(), opts.LocalRouter)
if err != nil {
return fmt.Errorf("Failed getting static routes for local peer router %q: %w", opts.LocalRouter, err)
}
Expand All @@ -3449,7 +3435,7 @@ func (o *NB) LogicalRouterPeeringDelete(opts OVNRouterPeering) error {
}
}

staticRoutes, err = o.LogicalRouterRoutes(opts.TargetRouter)
staticRoutes, err = o.GetLogicalRouterRoutes(context.TODO(), opts.TargetRouter)
if err != nil {
return fmt.Errorf("Failed getting static routes for target peer router %q: %w", opts.TargetRouter, err)
}
Expand Down

0 comments on commit 2f99b19

Please sign in to comment.