diff --git a/go.mod b/go.mod index 5d91eb81..ed706013 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ module github.com/networkservicemesh/sdk-kernel go 1.16 - require ( + github.com/ghodss/yaml v1.0.0 // indirect github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.1.2 github.com/networkservicemesh/api v0.5.1-0.20210618184350-e3fc29dc170b diff --git a/go.sum b/go.sum index 2bbe62bd..10307709 100644 --- a/go.sum +++ b/go.sum @@ -279,6 +279,7 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/square/go-jose.v2 v2.4.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/kernel/networkservice/netnsconnectioncontext/common.go b/pkg/kernel/networkservice/netnsconnectioncontext/common.go index 26fb2d0e..ca8b9165 100644 --- a/pkg/kernel/networkservice/netnsconnectioncontext/common.go +++ b/pkg/kernel/networkservice/netnsconnectioncontext/common.go @@ -45,24 +45,22 @@ func AddIPs(ctx context.Context, conn *networkservice.Connection, isClient bool) defer func() { _ = currNetNS.Close() }() ipContext := conn.GetContext().GetIpContext() - ipAddr, err := netlink.ParseAddr(ipContext.GetDstIpAddr()) + + ipNet := ipContext.GetDstIpAddrs() routes := ipContext.GetDstRoutes() if isClient { - ipAddr, err = netlink.ParseAddr(ipContext.GetSrcIpAddr()) + ipNet = ipContext.GetSrcIpAddrs() routes = ipContext.GetSrcRoutes() } - if err != nil { - return errors.Wrapf(err, "invalid IP address: %v", ipContext.GetSrcIpAddr()) - } - logger.Debugf("Is to set IP: %v and routes: %+v", ipAddr, routes) - return setIPandRoutes(hostIfName, routes, ipAddr, currNetNS, clientNetNS) + logger.Debugf("Is to set IPs: %v and routes: %+v", ipNet, routes) + return setIPandRoutes(hostIfName, routes, ipNet, currNetNS, clientNetNS) } return nil } -func setIPandRoutes(ifName string, routes []*networkservice.Route, ipAddr *netlink.Addr, currNetNS, toNetNS netns.NsHandle) error { +func setIPandRoutes(ifName string, routes []*networkservice.Route, ipNet []string, currNetNS, toNetNS netns.NsHandle) error { return nshandle.RunIn(currNetNS, toNetNS, func() error { link, err := netlink.LinkByName(ifName) if err != nil { @@ -70,19 +68,26 @@ func setIPandRoutes(ifName string, routes []*networkservice.Route, ipAddr *netli //return errors.Wrapf(err, "no link created with name %s", ifName) } - ipAddrs, err := netlink.AddrList(link, kernel.FamilyAll) - if err != nil { - return errors.Wrapf(err, "failed to get the net interface IP addresses: %v", link.Attrs().Name) - } + for _, ipN := range ipNet { + ipAddr, err := netlink.ParseAddr(ipN) + if err != nil { + return errors.Wrapf(err, "invalid IP address: %v", ipNet) + } - for i := range ipAddrs { - if ipAddr.Equal(ipAddrs[i]) { - return nil + ipAddrs, err := netlink.AddrList(link, kernel.FamilyAll) + if err != nil { + return errors.Wrapf(err, "failed to get the net interface IP addresses: %v", link.Attrs().Name) + } + + for i := range ipAddrs { + if ipAddr.Equal(ipAddrs[i]) { + return nil + } } - } - if err := netlink.AddrAdd(link, ipAddr); err != nil { - return errors.Wrapf(err, "failed to add IP address to the net interface: %v %v", link.Attrs().Name, ipAddr) + if err := netlink.AddrAdd(link, ipAddr); err != nil { + return errors.Wrapf(err, "failed to add IP address to the net interface: %v %v", link.Attrs().Name, ipAddr) + } } for _, route := range routes { @@ -90,13 +95,17 @@ func setIPandRoutes(ifName string, routes []*networkservice.Route, ipAddr *netli if err != nil { return errors.Wrapf(err, "invalid route CIDR: %v", route.GetPrefix()) } - if err = netlink.RouteAdd(&netlink.Route{ + kernelRoute := &netlink.Route{ LinkIndex: link.Attrs().Index, Dst: &net.IPNet{ IP: routeNet.IP, Mask: routeNet.Mask, }, - }); err != nil && !os.IsExist(err) { + } + if route.GetNextHopIP() != nil { + kernelRoute.Gw = route.GetNextHopIP() + } + if err = netlink.RouteAdd(kernelRoute); err != nil && !os.IsExist(err) { return errors.Wrapf(err, "failed to add route: %v", route.GetPrefix()) } }