Merge pull request #1126 from milosgajdos83/metric-update
Update route metric when receiving Sync routes
This commit is contained in:
		| @@ -935,6 +935,20 @@ func (n *network) processNetChan(listener tunnel.Listener) { | |||||||
| 						log.Debugf("Network node %s skipping route addition: route already present", n.id) | 						log.Debugf("Network node %s skipping route addition: route already present", n.id) | ||||||
| 						continue | 						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 | 					// lookup best routes for the services in the just received route | ||||||
| 					q := []router.QueryOption{ | 					q := []router.QueryOption{ | ||||||
| 						router.QueryService(route.Service), | 						router.QueryService(route.Service), | ||||||
| @@ -972,6 +986,8 @@ func (n *network) processNetChan(listener tunnel.Listener) { | |||||||
| 					if bestRoute.Metric <= route.Metric { | 					if bestRoute.Metric <= route.Metric { | ||||||
| 						continue | 						continue | ||||||
| 					} | 					} | ||||||
|  | 					/////////////////////////////////////////////////////////////////////// | ||||||
|  | 					/////////////////////////////////////////////////////////////////////// | ||||||
|  |  | ||||||
| 					// add route to the routing table | 					// add route to the routing table | ||||||
| 					if err := n.router.Table().Create(route); err != nil && err != router.ErrDuplicateRoute { | 					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 | 	// encode the routes to protobuf | ||||||
| 	pbRoutes := make([]*pbRtr.Route, 0, len(routes)) | 	pbRoutes := make([]*pbRtr.Route, 0, len(routes)) | ||||||
| 	for _, route := range 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 | 		// generate new route proto | ||||||
| 		pbRoute := pbUtil.RouteToProto(route) | 		pbRoute := pbUtil.RouteToProto(route) | ||||||
| 		// mask the route before outbounding | 		// mask the route before outbounding | ||||||
| @@ -1314,6 +1316,7 @@ func (n *network) getProtoRoutes() ([]*pbRtr.Route, error) { | |||||||
| 		// add to list of routes | 		// add to list of routes | ||||||
| 		pbRoutes = append(pbRoutes, pbRoute) | 		pbRoutes = append(pbRoutes, pbRoute) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return pbRoutes, nil | 	return pbRoutes, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user