Skip to content

Commit

Permalink
Merge pull request #560 from wazsone/feature/adding-errors-stack
Browse files Browse the repository at this point in the history
 Adding stack to errors
  • Loading branch information
denis-tingaikin authored Feb 18, 2023
2 parents ebf5e6e + 5e663a2 commit a321f02
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 84 deletions.
10 changes: 5 additions & 5 deletions pkg/kernel/link.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// Copyright (c) 2020-2022 Intel Corporation. All Rights Reserved.
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -290,19 +290,19 @@ func searchByName(ns netns.NsHandle, name, pciAddress string) (netlink.Link, err
func GetNetlinkHandle(urlString string) (*netlink.Handle, error) {
curNSHandle, err := nshandle.Current()
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
defer func() { _ = curNSHandle.Close() }()

nsHandle, err := nshandle.FromURL(urlString)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
defer func() { _ = nsHandle.Close() }()

handle, err := netlink.NewHandleAtFrom(nsHandle, curNSHandle)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to create netlink NS handle")
}
return handle, nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -55,32 +55,31 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)

netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()

ifName := mechanism.GetInterfaceName()

l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to find link %s", ifName)
}

if err = netlinkHandle.LinkSetUp(l); err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to setup link for the interface %v", l)
}

var forwarderNetNS netns.NsHandle
forwarderNetNS, err = nshandle.Current()
if err != nil {
return errors.WithStack(err)
return err
}
defer func() { _ = forwarderNetNS.Close() }()

var targetNetNS netns.NsHandle
targetNetNS, err = nshandle.FromURL(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer func() { _ = targetNetNS.Close() }()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -46,19 +46,18 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil && mechanism.GetVLAN() == 0 {
netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()

ifName := mechanism.GetInterfaceName()

l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to find link %s", ifName)
}

if err := setIPContextNeighbors(ctx, netlinkHandle, conn.GetContext().GetIpContext().GetIpNeighbors(), l); err != nil {
return errors.WithStack(err)
return err
}

// If payload is IP - we need to add additional neighbor
Expand Down Expand Up @@ -140,7 +139,7 @@ func setPeerNeighbor(ctx context.Context, handle *netlink.Handle, l, peerLink ne
WithField("hardwareAddr", neigh.HardwareAddr).
WithField("duration", time.Since(now)).
WithField("netlink", "NeighSet").Error("setPeerNeighbor failed")
return errors.WithStack(err)
return errors.Wrapf(err, "failed to update ARP table with %s %s", neigh.IP.String(), neigh.HardwareAddr.String())
}
log.FromContext(ctx).
WithField("linkIndex", neigh.LinkIndex).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -43,13 +45,14 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *Map)
// Construct the netlink handle for the target namespace for this kernel interface
netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()

l, err := netlinkHandle.LinkByName(mechanism.GetInterfaceName())
ifName := mechanism.GetInterfaceName()
l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to find link %s", ifName)
}

ps, ok := tableIDs.Load(conn.GetId())
Expand All @@ -75,7 +78,7 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *Map)
for _, policy := range toAdd {
tableID, err := getFreeTableID(ctx, netlinkHandle)
if err != nil {
return errors.Wrapf(err, "failed to get free tableId")
return err
}
// If policy doesn't contain any route - add default
if len(policy.Routes) == 0 {
Expand All @@ -84,11 +87,11 @@ func create(ctx context.Context, conn *networkservice.Connection, tableIDs *Map)

for _, route := range policy.Routes {
if err := routeAdd(ctx, netlinkHandle, l, route, tableID); err != nil {
return errors.Wrapf(err, "failed to add route")
return err
}
}
if err := ruleAdd(ctx, netlinkHandle, policy, tableID); err != nil {
return errors.Wrapf(err, "failed to add rule")
return err
}
ps[tableID] = policy
tableIDs.Store(conn.GetId(), ps)
Expand Down Expand Up @@ -131,27 +134,27 @@ func policyToRule(policy *networkservice.PolicyRoute) (*netlink.Rule, error) {
if policy.From != "" {
src, err := netlink.ParseIPNet(policy.From)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse string %s in ip/net format", policy.From)
}
rule.Src = src
}
if policy.Proto != "" {
protocol, err := strconv.Atoi(policy.Proto)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse ip protocol number %s", policy.Proto)
}
rule.IPProto = protocol
}
dstPortRange, err := networkservice.ParsePortRange(policy.DstPort)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse port range %s", policy.DstPort)
}
if dstPortRange != nil {
rule.Dport = netlink.NewRulePortRange(dstPortRange.Start, dstPortRange.End)
}
srcPortRange, err := networkservice.ParsePortRange(policy.SrcPort)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse port range %s", policy.DstPort)
}
if srcPortRange != nil {
rule.Sport = netlink.NewRulePortRange(srcPortRange.Start, srcPortRange.End)
Expand All @@ -162,7 +165,7 @@ func policyToRule(policy *networkservice.PolicyRoute) (*netlink.Rule, error) {
func ruleAdd(ctx context.Context, handle *netlink.Handle, policy *networkservice.PolicyRoute, tableID int) error {
rule, err := policyToRule(policy)
if err != nil {
return errors.WithStack(err)
return err
}
rule.Table = tableID

Expand All @@ -176,7 +179,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.WithStack(err)
return errors.Wrap(err, "failed to add rule")
}
log.FromContext(ctx).
WithField("From", policy.From).
Expand Down Expand Up @@ -225,7 +228,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.WithStack(err)
return errors.Wrap(err, "failed to add route")
}
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
Expand All @@ -243,14 +246,14 @@ func del(ctx context.Context, conn *networkservice.Connection, tableIDs *Map) er
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil && mechanism.GetVLAN() == 0 {
netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()
ps, ok := tableIDs.LoadAndDelete(conn.GetId())
if ok {
for tableID, policy := range ps {
if err := delRule(ctx, netlinkHandle, policy, tableID); err != nil {
return errors.WithStack(err)
return err
}
}
}
Expand All @@ -261,7 +264,7 @@ func del(ctx context.Context, conn *networkservice.Connection, tableIDs *Map) er
func delRule(ctx context.Context, handle *netlink.Handle, policy *networkservice.PolicyRoute, tableID int) error {
rule, err := policyToRule(policy)
if err != nil {
return errors.WithStack(err)
return err
}

if err := flushTable(ctx, handle, tableID); err != nil {
Expand All @@ -277,7 +280,7 @@ func delRule(ctx context.Context, handle *netlink.Handle, policy *networkservice
WithField("SrcPort", policy.SrcPort).
WithField("duration", time.Since(now)).
WithField("netlink", "RuleDel").Errorf("error %+v", err)
return errors.Wrapf(errors.WithStack(err), "failed to delete rule")
return errors.Wrapf(err, "failed to delete rule")
}
log.FromContext(ctx).
WithField("From", policy.From).
Expand All @@ -296,12 +299,12 @@ func flushTable(ctx context.Context, handle *netlink.Handle, tableID int) error
},
netlink.RT_FILTER_TABLE)
if err != nil {
return errors.Wrapf(errors.WithStack(err), "failed to list routes")
return errors.Wrapf(err, "failed to list routes")
}
for i := 0; i < len(routes); i++ {
err := handle.RouteDel(&routes[i])
if err != nil {
return errors.Wrapf(errors.WithStack(err), "failed to delete route")
return errors.Wrapf(err, "failed to delete route")
}
}
log.FromContext(ctx).
Expand All @@ -317,7 +320,7 @@ func getFreeTableID(ctx context.Context, handle *netlink.Handle) (int, error) {
},
netlink.RT_FILTER_TABLE)
if err != nil {
return 0, errors.Wrapf(errors.WithStack(err), "getFreeTableID: failed to list routes")
return 0, errors.Wrapf(err, "getFreeTableID: failed to list routes")
}

// tableID = 0 is reserved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -46,20 +48,20 @@ func recoverTableIDs(ctx context.Context, conn *networkservice.Connection, table

netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()

podRules, err := netlinkHandle.RuleList(netlink.FAMILY_ALL)
if err != nil {
return errors.WithStack(err)
return errors.Wrap(err, "failed to get list of rules")
}

// try to find the corresponding missing policies in the network namespace of the pod
for _, policy := range conn.Context.IpContext.Policies {
policyRule, err := policyToRule(policy)
if err != nil {
return errors.WithStack(err)
return err
}
for i := range podRules {
if ruleEquals(&podRules[i], policyRule) {
Expand All @@ -71,7 +73,7 @@ func recoverTableIDs(ctx context.Context, conn *networkservice.Connection, table
WithField("Table", podRules[i].Table).Debug("policy recovered")
err := delRule(ctx, netlinkHandle, policy, podRules[i].Table)
if err != nil {
return errors.WithStack(err)
return err
}
break
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2022 Nordix Foundation.
//
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -39,19 +39,18 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil && mechanism.GetVLAN() == 0 {
netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
return err
}
defer netlinkHandle.Close()

ifName := mechanism.GetInterfaceName()

l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to find link %s", ifName)
}

if err = netlinkHandle.LinkSetUp(l); err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to setup link for the interface %v", l)
}

var linkRoutes []*networkservice.Route
Expand Down Expand Up @@ -106,7 +105,7 @@ func routeAdd(ctx context.Context, handle *netlink.Handle, l netlink.Link, scope
WithField("Flags", kernelRoute.Flags).
WithField("duration", time.Since(now)).
WithField("netlink", "RouteReplace").Errorf("error %+v", err)
return errors.WithStack(err)
return errors.Wrap(err, "failed to add route")
}
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
Expand Down
Loading

0 comments on commit a321f02

Please sign in to comment.