From e435e3915800d95556644bab95618a6164f44644 Mon Sep 17 00:00:00 2001 From: Viktor Liu Date: Mon, 29 Apr 2024 18:43:14 +0200 Subject: [PATCH] Fix route selection IDs (#1890) --- client/internal/routemanager/client.go | 6 +++++- client/server/route.go | 24 +++++++++++++++++------- route/route.go | 6 ++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/client/internal/routemanager/client.go b/client/internal/routemanager/client.go index 3569d13ae67..a8f0c867708 100644 --- a/client/internal/routemanager/client.go +++ b/client/internal/routemanager/client.go @@ -156,7 +156,11 @@ func (c *clientNetwork) getBestRouteFromStatuses(routePeerStatuses map[string]ro if currScore != 0 && currScore < chosenScore+0.1 { return currID } else { - log.Infof("new chosen route is %s with peer %s with score %f for network %s", chosen, c.routes[chosen].Peer, chosenScore, c.network) + var peer string + if route := c.routes[chosen]; route != nil { + peer = route.Peer + } + log.Infof("new chosen route is %s with peer %s with score %f for network %s", chosen, peer, chosenScore, c.network) } } diff --git a/client/server/route.go b/client/server/route.go index 4aa37dbb78b..018fe23c64d 100644 --- a/client/server/route.go +++ b/client/server/route.go @@ -3,14 +3,20 @@ package server import ( "context" "fmt" + "net/netip" "sort" "golang.org/x/exp/maps" "github.com/netbirdio/netbird/client/proto" - "github.com/netbirdio/netbird/route" ) +type selectRoute struct { + NetID string + Network netip.Prefix + Selected bool +} + // ListRoutes returns a list of all available routes. func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) (*proto.ListRoutesResponse, error) { s.mutex.Lock() @@ -23,13 +29,17 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) ( routesMap := s.engine.GetClientRoutesWithNetID() routeSelector := s.engine.GetRouteManager().GetRouteSelector() - var routes []*route.Route + var routes []*selectRoute for id, rt := range routesMap { if len(rt) == 0 { continue } - rt[0].ID = id - routes = append(routes, rt[0]) + route := &selectRoute{ + NetID: id, + Network: rt[0].Network, + Selected: routeSelector.IsSelected(id), + } + routes = append(routes, route) } sort.Slice(routes, func(i, j int) bool { @@ -40,7 +50,7 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) ( iAddr := routes[i].Network.Addr() jAddr := routes[j].Network.Addr() if iAddr == jAddr { - return routes[i].ID < routes[j].ID + return routes[i].NetID < routes[j].NetID } return iAddr.String() < jAddr.String() } @@ -50,9 +60,9 @@ func (s *Server) ListRoutes(ctx context.Context, req *proto.ListRoutesRequest) ( var pbRoutes []*proto.Route for _, route := range routes { pbRoutes = append(pbRoutes, &proto.Route{ - ID: route.ID, + ID: route.NetID, Network: route.Network.String(), - Selected: routeSelector.IsSelected(route.ID), + Selected: route.Selected, }) } diff --git a/route/route.go b/route/route.go index 7e8a8377c41..97c75f3b492 100644 --- a/route/route.go +++ b/route/route.go @@ -107,6 +107,12 @@ func (r *Route) Copy() *Route { // IsEqual compares one route with the other func (r *Route) IsEqual(other *Route) bool { + if r == nil && other == nil { + return true + } else if r == nil || other == nil { + return false + } + return other.ID == r.ID && other.Description == r.Description && other.NetID == r.NetID &&