Skip to content

Commit

Permalink
Adapt to merge of multi-ip shift in API networkservicemesh/api#47
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Jun 22, 2021
1 parent 97667f8 commit 6a28c86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
49 changes: 29 additions & 20 deletions pkg/kernel/networkservice/netnsconnectioncontext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,58 +45,67 @@ 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 {
return nil
//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 {
_, routeNet, err := net.ParseCIDR(route.GetPrefix())
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())
}
}
Expand Down

0 comments on commit 6a28c86

Please sign in to comment.