Skip to content

Commit

Permalink
Merge pull request networkservicemesh#383 from edwarnicke/routesfix
Browse files Browse the repository at this point in the history
Fix bug for non-/32 routes
  • Loading branch information
denis-tingaikin authored Nov 22, 2021
2 parents c1558da + 5110db5 commit 6faea43
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
}
}
for _, route := range routes {
if err := routeAdd(ctx, netlinkHandle, l, netlink.SCOPE_LINK, route); err != nil {
if err := routeAdd(ctx, netlinkHandle, l, netlink.SCOPE_UNIVERSE, route); err != nil {
return err
}
}
Expand All @@ -80,24 +80,39 @@ func routeAdd(ctx context.Context, handle *netlink.Handle, l netlink.Link, scope
if route.GetPrefixIPNet() == nil {
return errors.New("kernelRoute prefix must not be nil")
}
dst := route.GetPrefixIPNet()
dst.IP = dst.IP.Mask(dst.Mask)
kernelRoute := &netlink.Route{
LinkIndex: l.Attrs().Index,
Scope: scope,
Dst: route.GetPrefixIPNet(),
Dst: dst,
}
gw := route.GetNextHopIP()
if gw != nil {
kernelRoute.Gw = gw
if scope != netlink.SCOPE_LINK {
kernelRoute.SetFlag(netlink.FLAG_ONLINK)
}
}

now := time.Now()
if err := handle.RouteReplace(kernelRoute); err != nil {
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
WithField("Dst", kernelRoute.Dst).
WithField("Gw", kernelRoute.Gw).
WithField("Scope", kernelRoute.Scope).
WithField("Flags", kernelRoute.Flags).
WithField("duration", time.Since(now)).
WithField("netlink", "RouteReplace").Errorf("error %+v", err)
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
WithField("Dst", kernelRoute.Dst).
WithField("Gw", kernelRoute.Gw).
WithField("Scope", kernelRoute.Scope).
WithField("Flags", kernelRoute.Flags).
WithField("duration", time.Since(now)).
WithField("netlink", "RouteReplace").Debug("completed")
return nil
Expand Down

0 comments on commit 6faea43

Please sign in to comment.