Skip to content

Commit

Permalink
Adapt to multi-ip shift in API networkservicemesh/api#47
Browse files Browse the repository at this point in the history
networkservicemesh/api#47

Signed-off-by: Ed Warnicke <hagbard@gmail.com>
  • Loading branch information
edwarnicke authored and ljkiraly committed May 7, 2021
1 parent 4220bbd commit 7cc6b49
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.16
require (
github.com/golang/protobuf v1.4.3
github.com/google/uuid v1.1.2
github.com/networkservicemesh/api v0.0.0-20210417193417-dd329f8d6b7a
github.com/networkservicemesh/sdk v0.0.0-20210430112713-a9edf1f61b2d
github.com/networkservicemesh/api v0.0.0-20210502014900-961930476ee1
github.com/networkservicemesh/sdk v0.0.0-20210502020015-89486603aedb
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/vishvananda/netlink v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nats-io/stan.go v0.6.0/go.mod h1:eIcD5bi3pqbHT/xIIvXMwvzXYElgouBvaVRftaE+eac=
github.com/networkservicemesh/api v0.0.0-20210417193417-dd329f8d6b7a h1:BakXE3Zu+KLMYEgwcfL3zsff5J1DxCt/rI60x6KU5Q4=
github.com/networkservicemesh/api v0.0.0-20210417193417-dd329f8d6b7a/go.mod h1:B6meq/SWjWR6bGXZdXPfbOeaBK+T1JayLdtEJQCsXKU=
github.com/networkservicemesh/sdk v0.0.0-20210430112713-a9edf1f61b2d h1:A3T9UA26EgAz2XzQHQXSLr5B1bZqpmkUweDymkgwmB8=
github.com/networkservicemesh/sdk v0.0.0-20210430112713-a9edf1f61b2d/go.mod h1:wV3wbgR7d7BaHJ8yPHtkXEj9jZ0WvZWsfHJMarV4VQ0=
github.com/networkservicemesh/api v0.0.0-20210502014900-961930476ee1 h1:HNm4SS79Bx14cAXoLkIs/GB2KSJI+zT19ttDIu6Ofuc=
github.com/networkservicemesh/api v0.0.0-20210502014900-961930476ee1/go.mod h1:B6meq/SWjWR6bGXZdXPfbOeaBK+T1JayLdtEJQCsXKU=
github.com/networkservicemesh/sdk v0.0.0-20210502020015-89486603aedb h1:JH9vbQvQtaEZnmuZpxYKbcQ8uC9llznPn0DnmFEaENc=
github.com/networkservicemesh/sdk v0.0.0-20210502020015-89486603aedb/go.mod h1:uKeG+m5riQr0jllVp/vyeYJP2ruu1kyM8ejLWAKJwUo=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
Expand Down
30 changes: 17 additions & 13 deletions pkg/kernel/networkservice/ipcontext/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,23 @@ func (s *ipContextServer) Request(ctx context.Context, request *networkservice.N
return nil, errors.Wrapf(err, "failed to get net interface: %v", ifName)
}

ipContext := request.GetConnection().GetContext().GetIpContext()
ipAddr, err := netlink.ParseAddr(ipContext.GetSrcIpAddr())
if err != nil {
return nil, errors.Wrapf(err, "invalid IP address: %v", ipContext.GetSrcIpAddr())
}

if link.Attrs().OperState != netlink.OperUp {
if err = netlink.LinkSetUp(link); err != nil {
return nil, errors.Wrapf(err, "failed to set up net interface: %v", ifName)
}
}

if err := setIPAddr(ipAddr, link); err != nil {
return nil, err
ipContext := request.GetConnection().GetContext().GetIpContext()
for _, ipNet := range ipContext.GetSrcIpAddrs() {
ipAddr, err := netlink.ParseAddr(ipNet)
if err != nil {
return nil, errors.Wrapf(err, "invalid IP address: %v", ipNet)
}
if err := setIPAddr(ipAddr, link); err != nil {
return nil, err
}
}
if err := setRoutes(ipContext.GetSrcRoutes(), ipAddr, link); err != nil {
if err := setRoutes(ipContext.GetSrcRoutes(), link); err != nil {
return nil, err
}
if err := setIPNeighbors(ipContext.GetIpNeighbors(), link); err != nil {
Expand Down Expand Up @@ -92,20 +93,23 @@ func setIPAddr(ipAddr *netlink.Addr, link netlink.Link) error {
return nil
}

func setRoutes(routes []*networkservice.Route, ipAddr *netlink.Addr, link netlink.Link) error {
func setRoutes(routes []*networkservice.Route, link netlink.Link) error {
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,
},
Src: ipAddr.IP,
}); 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 7cc6b49

Please sign in to comment.