From 891af703be776b5666e8898616a4812d3f8ad318 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 17 Jan 2020 18:24:36 +0000 Subject: [PATCH] Update route metric when receiving Sync routes --- network/default.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/network/default.go b/network/default.go index 5b961537..cee65e16 100644 --- a/network/default.go +++ b/network/default.go @@ -935,6 +935,20 @@ func (n *network) processNetChan(listener tunnel.Listener) { log.Debugf("Network node %s skipping route addition: route already present", n.id) continue } + + metric := n.getRouteMetric(route.Router, route.Gateway, route.Link) + // check we don't overflow max int 64 + if d := route.Metric + metric; d <= 0 { + // set to max int64 if we overflow + route.Metric = math.MaxInt64 + } else { + // set the combined value of metrics otherwise + route.Metric = d + } + + ///////////////////////////////////////////////////////////////////// + // maybe we should not be this clever ¯\_(ツ)_/¯ // + ///////////////////////////////////////////////////////////////////// // lookup best routes for the services in the just received route q := []router.QueryOption{ router.QueryService(route.Service), @@ -972,6 +986,8 @@ func (n *network) processNetChan(listener tunnel.Listener) { if bestRoute.Metric <= route.Metric { continue } + /////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////// // add route to the routing table if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute { @@ -1293,20 +1309,6 @@ func (n *network) getProtoRoutes() ([]*pbRtr.Route, error) { // encode the routes to protobuf pbRoutes := make([]*pbRtr.Route, 0, len(routes)) for _, route := range routes { - // calculate route metric and add to the advertised metric - // we need to make sure we do not overflow math.MaxInt64 - metric := n.getRouteMetric(route.Router, route.Gateway, route.Link) - log.Tracef("Network metric for router %s and gateway %s: %v", route.Router, route.Gateway, metric) - - // check we don't overflow max int 64 - if d := route.Metric + metric; d <= 0 { - // set to max int64 if we overflow - route.Metric = math.MaxInt64 - } else { - // set the combined value of metrics otherwise - route.Metric = d - } - // generate new route proto pbRoute := pbUtil.RouteToProto(route) // mask the route before outbounding @@ -1314,6 +1316,7 @@ func (n *network) getProtoRoutes() ([]*pbRtr.Route, error) { // add to list of routes pbRoutes = append(pbRoutes, pbRoute) } + return pbRoutes, nil }