Skip to content

Commit

Permalink
Merge branch 'main' of github.com:networkservicemesh/sdk-kernel into …
Browse files Browse the repository at this point in the history
…update-go-1.23.1
  • Loading branch information
Vladislav Byrgazov committed Nov 19, 2024
2 parents 8d64488 + 3e1b97b commit b11075a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-ping/ping v1.0.0
github.com/golang/protobuf v1.5.3
github.com/networkservicemesh/api v1.13.4-0.20240815101554-fdbfcd84fd0e
github.com/networkservicemesh/sdk v0.5.1-0.20241015095655-7ebf92ea74d7
github.com/networkservicemesh/sdk v0.5.1-0.20241106141620-b53ccb0ebe20
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.3.1-0.20240922070040-084abd93d350
Expand Down Expand Up @@ -47,7 +47,7 @@ require (
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/networkservicemesh/api v1.13.4-0.20240815101554-fdbfcd84fd0e h1:Fs34ZZN+bujq5OQEQUpxm0gYQ3g7uSCkcKcoUt70Be0=
github.com/networkservicemesh/api v1.13.4-0.20240815101554-fdbfcd84fd0e/go.mod h1:B8FmS3XZ7NZY7ZEtdcNg2NHYppDHlr4kl4eecdZN9eI=
github.com/networkservicemesh/sdk v0.5.1-0.20241015095655-7ebf92ea74d7 h1:KC1ivupPzRmk9WbmlU0OedWzZRjsML4x2qm4kOXtELk=
github.com/networkservicemesh/sdk v0.5.1-0.20241015095655-7ebf92ea74d7/go.mod h1:nz2VJ3l/UDvsEusdIZf7OMxC7zLrRCV1PuziPjxJeVE=
github.com/networkservicemesh/sdk v0.5.1-0.20241106141620-b53ccb0ebe20 h1:MB62uePZ8UGYTFCJqdiFnLeakQx/m0BPwVp6tJRFWRs=
github.com/networkservicemesh/sdk v0.5.1-0.20241106141620-b53ccb0ebe20/go.mod h1:lV8RBwf6tHkNFwHLFkzh4Tb/XezMP5IHGx5lW04YjQE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -101,8 +101,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// Copyright (c) 2023 Nordix Foundation.
// Copyright (c) 2023-2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -56,7 +56,7 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *gene
ifName := mechanism.GetInterfaceName()
l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.Wrapf(err, "failed to find link %s", ifName)
return errors.Wrapf(err, "iprule: failed to create policy rules for interface %s", ifName)
}
connID := conn.GetId()
ps, ok := tableIDs.Load(connID)
Expand All @@ -71,7 +71,7 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *gene
// Get netns for key to namespace to routing tableID map
netNS, err := nshandle.FromURL(mechanism.GetNetNSURL())
if err != nil {
return err
return errors.Wrapf(err, "iprule: failed to create policy rules in namespace: %s", mechanism.GetNetNSURL())
}

// Get policies to add and to remove
Expand Down Expand Up @@ -100,7 +100,7 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *gene
log.FromContext(ctx).
WithField("nsrtid", nsrtid).
WithField("ConnID", storedConnID).
Debug("storedTableID")
Debug("iprule:createNetnsRTableNextID")
if connID == storedConnID {
// No other connection adding policy using this free routing table ID
break
Expand Down Expand Up @@ -187,7 +187,7 @@ func policyToRule(policy *networkservice.PolicyRoute) (*netlink.Rule, error) {
}
srcPortRange, err := networkservice.ParsePortRange(policy.SrcPort)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse port range %s", policy.DstPort)
return nil, errors.Wrapf(err, "failed to parse port range %s", policy.SrcPort)
}
if srcPortRange != nil {
rule.Sport = netlink.NewRulePortRange(srcPortRange.Start, srcPortRange.End)
Expand All @@ -212,7 +212,7 @@ func ruleAdd(ctx context.Context, handle *netlink.Handle, policy *networkservice
WithField("Table", tableID).
WithField("duration", time.Since(now)).
WithField("netlink", "RuleAdd").Errorf("error %+v", err)
return errors.Wrap(err, "failed to add rule")
return errors.Wrap(err, "iprule: failed to add rule")
}
log.FromContext(ctx).
WithField("From", policy.From).
Expand All @@ -233,7 +233,7 @@ func defaultRoute() *networkservice.Route {

func routeAdd(ctx context.Context, handle *netlink.Handle, l netlink.Link, route *networkservice.Route, tableID int) error {
if route.GetPrefixIPNet() == nil {
return errors.New("kernelRoute prefix must not be nil")
return errors.New("iprule: kernelRoute prefix must not be nil")
}
dst := route.GetPrefixIPNet()
dst.IP = dst.IP.Mask(dst.Mask)
Expand Down Expand Up @@ -261,7 +261,7 @@ func routeAdd(ctx context.Context, handle *netlink.Handle, l netlink.Link, route
WithField("Table", tableID).
WithField("duration", time.Since(now)).
WithField("netlink", "RouteReplace").Errorf("error %+v", err)
return errors.Wrap(err, "failed to add route")
return errors.Wrap(err, "iprule: failed to add route")
}
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
Expand All @@ -285,13 +285,13 @@ func del(ctx context.Context, conn *networkservice.Connection, tableIDs *generic
ifName := mechanism.GetInterfaceName()
l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.Wrapf(err, "failed to find link %s", ifName)
return errors.Wrapf(err, "iprule: failed to delete policy rules for interface %s", ifName)
}
ps, ok := tableIDs.LoadAndDelete(conn.GetId())
if ok {
netNS, err := nshandle.FromURL(mechanism.GetNetNSURL())
if err != nil {
return err
return errors.Wrapf(err, "iprule: failed to delete policy rules in namespace: %s", mechanism.GetNetNSURL())
}
for tableID, policy := range ps {
if err := delRule(ctx, netlinkHandle, policy, tableID, l.Attrs().Index, createNetnsRTableNextID(netNS.UniqueId(), tableID), nsRTableNextIDToConnID); err != nil {
Expand All @@ -317,7 +317,7 @@ func delRuleOnly(ctx context.Context, handle *netlink.Handle, policy *networkser
WithField("SrcPort", policy.SrcPort).
WithField("duration", time.Since(now)).
WithField("netlink", "RuleDel").Errorf("error %+v", err)
return errors.Wrapf(err, "failed to delete rule")
return errors.Wrapf(err, "iprule: failed to delete rule")
}
log.FromContext(ctx).
WithField("From", policy.From).
Expand All @@ -329,14 +329,16 @@ func delRuleOnly(ctx context.Context, handle *netlink.Handle, policy *networkser
return nil
}

func delRule(ctx context.Context, handle *netlink.Handle, policy *networkservice.PolicyRoute, tableID, linkIndex int, nsRTableKey netnsRTableNextID, nsRTableNextIDToConnID *genericsync.Map[netnsRTableNextID, string]) error {
if err := flushTable(ctx, handle, tableID, linkIndex); err != nil {
return err
func delRule(ctx context.Context, handle *netlink.Handle, policy *networkservice.PolicyRoute, tableID, linkIndex int, nsRTableKey netnsRTableNextID, nsRTableNextIDToConnID *genericsync.Map[netnsRTableNextID, string]) (err error) {
if err = flushTable(ctx, handle, tableID, linkIndex); err == nil {
nsRTableNextIDToConnID.Delete(nsRTableKey)
}
nsRTableNextIDToConnID.Delete(nsRTableKey)

return delRuleOnly(ctx, handle, policy)
if errDelRule := delRuleOnly(ctx, handle, policy); errDelRule != nil {
return errDelRule
}
return err
}

func flushTable(ctx context.Context, handle *netlink.Handle, tableID, linkIndex int) error {
routes, err := handle.RouteListFiltered(netlink.FAMILY_ALL,
&netlink.Route{
Expand All @@ -345,7 +347,7 @@ func flushTable(ctx context.Context, handle *netlink.Handle, tableID, linkIndex
},
netlink.RT_FILTER_TABLE)
if err != nil {
return errors.Wrapf(err, "failed to list routes")
return errors.Wrapf(err, "iprule: failed to flush routing for tableID:%d, linkID %d", tableID, linkIndex)
}
for i := 0; i < len(routes); i++ {
// This conditions means the default route. We should delete it properly
Expand All @@ -358,7 +360,7 @@ func flushTable(ctx context.Context, handle *netlink.Handle, tableID, linkIndex
}
err := handle.RouteDel(&routes[i])
if err != nil {
return errors.Wrapf(err, "failed to delete route: %v", routes[i].String())
return errors.Wrapf(err, "iprule: failed to delete route: %v", routes[i].String())
}

log.FromContext(ctx).
Expand All @@ -367,7 +369,7 @@ func flushTable(ctx context.Context, handle *netlink.Handle, tableID, linkIndex
}
log.FromContext(ctx).
WithField("tableID", tableID).
WithField("netlink", "flushTable").Debug("completed")
WithField("iprule", "flushTable").Debug("completed")
return nil
}

Expand All @@ -378,12 +380,12 @@ func getFreeTableID(ctx context.Context, handle *netlink.Handle) (int, error) {
},
netlink.RT_FILTER_TABLE)
if err != nil {
return 0, errors.Wrapf(err, "getFreeTableID: failed to list routes")
return 0, errors.Wrapf(err, "iprule: failed to get free routing table ID, no routes")
}

rules, err := handle.RuleList(netlink.FAMILY_ALL)
if err != nil {
return 0, errors.Wrapf(err, "getFreeTableID: failed to list rules")
return 0, errors.Wrapf(err, "iprule: failed to get free routing table ID, no rules")
}

// tableID = 0 is reserved
Expand All @@ -405,7 +407,7 @@ func getFreeTableID(ctx context.Context, handle *netlink.Handle) (int, error) {
}
log.FromContext(ctx).
WithField("tableID", tableID).
WithField("netlink", "getFreeTableID").Debug("completed")
WithField("iprule", "getFreeTableID").Debug("completed")

return tableID, nil
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2024 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -57,12 +57,12 @@ func recoverTableIDs(ctx context.Context, conn *networkservice.Connection, table
ifName := mechanism.GetInterfaceName()
l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.Wrapf(err, "failed to find link %s", ifName)
return errors.Wrapf(err, "iprule: failed to recover table IDs for interface: %s", ifName)
}

podRules, err := netlinkHandle.RuleList(netlink.FAMILY_ALL)
if err != nil {
return errors.Wrap(err, "failed to get list of rules")
return errors.Wrapf(err, "iprule: failed to recover table IDs in namespace: %s", mechanism.GetNetNSURL())
}

tableIDtoPolicyMap := make(map[int]*networkservice.PolicyRoute)
Expand Down

0 comments on commit b11075a

Please sign in to comment.