Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding errors stack #492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/networkservice/common/mechanisms/vfio/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 All @@ -26,6 +28,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
"google.golang.org/grpc"

Expand Down Expand Up @@ -89,7 +92,7 @@ func (c *vfioClient) Request(ctx context.Context, request *networkservice.Networ
if mech := vfio.ToMechanism(conn.GetMechanism()); mech != nil {
if err := os.Mkdir(c.vfioDir, mkdirPerm); err != nil && !os.IsExist(err) {
logger.Error("failed to create vfio directory")
return nil, err
return nil, errors.Wrapf(err, "failed to create vfio directory %s", c.vfioDir)
}

if err := unix.Mknod(
Expand All @@ -98,7 +101,7 @@ func (c *vfioClient) Request(ctx context.Context, request *networkservice.Networ
int(unix.Mkdev(mech.GetVfioMajor(), mech.GetVfioMinor())),
); err != nil && !os.IsExist(err) {
logger.Errorf("failed to mknod device: %v", vfioDevice)
return nil, err
return nil, errors.Wrapf(err, "failed to mknod device: %v", vfioDevice)
}

igid := mech.GetParameters()[vfio.IommuGroupKey]
Expand All @@ -108,7 +111,7 @@ func (c *vfioClient) Request(ctx context.Context, request *networkservice.Networ
int(unix.Mkdev(mech.GetDeviceMajor(), mech.GetDeviceMinor())),
); err != nil && !os.IsExist(err) {
logger.Errorf("failed to mknod device: %v", vfioDevice)
return nil, err
return nil, errors.Wrapf(err, "failed to mknod device: %v", vfioDevice)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/networkservice/common/mechanisms/vfio/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 @@ -114,7 +116,7 @@ func (s *vfioServer) Request(ctx context.Context, request *networkservice.Networ
func (s *vfioServer) getDeviceNumbers(deviceFile string) (major, minor uint32, err error) {
info := new(unix.Stat_t)
if err := unix.Stat(deviceFile, info); err != nil {
return 0, 0, err
return 0, 0, errors.Wrapf(err, "failed to check %s file status", deviceFile)
}
return Major(info.Rdev), Minor(info.Rdev), nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/networkservice/common/resetmechanism/mechanism_map.gen.go

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

6 changes: 4 additions & 2 deletions pkg/sriov/pci/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 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 @@ -190,7 +192,7 @@ func (p *Pool) waitDriverGettingBound(ctx context.Context, pcif pciFunction, dri

select {
case <-ctx.Done():
return ctx.Err()
return errors.Wrap(ctx.Err(), "provided context is done")
case <-timeoutCh:
return errors.Errorf("time for binding kernel driver exceeded: %s, cause: %v", pcif.GetPCIAddress(), err)
case <-time.After(driverBindCheck):
Expand Down Expand Up @@ -218,5 +220,5 @@ func (p *Pool) vfioDriverCheck(pcif pciFunction) error {
}

_, err = os.Stat(filepath.Join(p.vfioDir, strconv.FormatUint(uint64(iommuGroup), 10)))
return err
return errors.Wrapf(err, "failed to join path elements: %s, %s", p.vfioDir, strconv.FormatUint(uint64(iommuGroup), 10))
}
6 changes: 4 additions & 2 deletions pkg/sriov/pcifunction/function.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 @@ -71,7 +73,7 @@ func (f *Function) GetNetInterfaceName() (string, error) {
func (f *Function) GetIOMMUGroup() (uint, error) {
stringIOMMUGroup, err := evalSymlinkAndGetBaseName(f.withDevicePath(iommuGroup))
if err != nil {
return 0, errors.Wrapf(err, "error evaluating IOMMU group id for the device: %v", f.address)
return 0, err
}

iommuGroup, _ := strconv.Atoi(stringIOMMUGroup)
Expand All @@ -87,7 +89,7 @@ func (f *Function) GetBoundDriver() (string, error) {

driver, err := evalSymlinkAndGetBaseName(f.withDevicePath(boundDriverPath))
if err != nil {
return "", errors.Wrapf(err, "error evaluating bound driver for the device: %v", f.address)
return "", err
}

return driver, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/sriov/pcifunction/physical_function.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 @@ -97,7 +99,7 @@ func (pf *PhysicalFunction) GetVirtualFunctions() []*Function {
func (pf *PhysicalFunction) createVirtualFunctions() error {
switch vfsCount, err := readUintFromFile(pf.withDevicePath(configuredVFFile)); {
case err != nil:
return errors.Wrapf(err, "failed to get configured VFs number for the PCI device: %v", pf.address)
return err
case vfsCount > 0:
return nil
}
Expand Down
28 changes: 22 additions & 6 deletions pkg/tools/cgroup/cgroup.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 All @@ -25,6 +27,8 @@ import (
"os"
"path/filepath"
"reflect"

"github.com/pkg/errors"
)

const (
Expand All @@ -41,9 +45,10 @@ type Cgroup struct {
// NewCgroups returns all cgroups matching pathPattern
func NewCgroups(pathPattern string) (cgroups []*Cgroup, err error) {
var filePaths []string
filePaths, err = filepath.Glob(filepath.Join(pathPattern, deviceListFileName))
pattern := filepath.Join(pathPattern, deviceListFileName)
filePaths, err = filepath.Glob(pattern)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to get filepaths %s", pattern)
}

for _, filePath := range filePaths {
Expand All @@ -57,14 +62,24 @@ func NewCgroups(pathPattern string) (cgroups []*Cgroup, err error) {
func (c *Cgroup) Allow(major, minor uint32) error {
dev := newDevice(major, minor, 'r', 'w', 'm')

return ioutil.WriteFile(filepath.Join(c.Path, deviceAllowFileName), []byte(dev.String()), 0)
filePath := filepath.Join(c.Path, deviceAllowFileName)
if err := ioutil.WriteFile(filePath, []byte(dev.String()), 0); err != nil {
return errors.Wrapf(err, "failed to write to a %s", filePath)
}

return nil
}

// Deny denies "c major:minor rw" for cgroup
func (c *Cgroup) Deny(major, minor uint32) error {
dev := newDevice(major, minor, 'r', 'w')

return ioutil.WriteFile(filepath.Join(c.Path, deviceDenyFileName), []byte(dev.String()), 0)
filePath := filepath.Join(c.Path, deviceAllowFileName)
if err := ioutil.WriteFile(filePath, []byte(dev.String()), 0); err != nil {
return errors.Wrapf(err, "failed to write to a %s", filePath)
}

return nil
}

// IsAllowed returns if "c major:minor rwm" is allowed for cgroup
Expand All @@ -86,9 +101,10 @@ func (c *Cgroup) IsWiderThan(major, minor uint32) (bool, error) {
}

func (c *Cgroup) compareTo(dev *device) (isAllowed, isWider bool, err error) {
file, err := os.Open(filepath.Join(c.Path, deviceListFileName))
filePath := filepath.Clean(filepath.Join(c.Path, deviceListFileName))
file, err := os.Open(filePath)
if err != nil {
return false, false, err
return false, false, errors.Wrapf(err, "failed to open file %s", filePath)
}
defer func() { _ = file.Close() }()

Expand Down
4 changes: 3 additions & 1 deletion pkg/tools/cgroup/fake_cgroup.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 @@ -70,7 +72,7 @@ func NewFakeWideCgroup(ctx context.Context, path string) (*Cgroup, error) {

func newFakeCgroup(ctx context.Context, path string) (*Cgroup, supplierFunc, error) {
if err := os.MkdirAll(path, mkdirPerm); err != nil {
return nil, nil, err
return nil, nil, errors.Wrapf(err, "failed to create directory path %s", path)
}
go func() {
<-ctx.Done()
Expand Down
12 changes: 9 additions & 3 deletions pkg/tools/cgroup/file_api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-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 All @@ -25,6 +27,7 @@ import (
"io/ioutil"
"os"

"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand All @@ -36,12 +39,12 @@ func inputFileAPI(ctx context.Context, filePath string, consumer func(string)) e
_ = os.Remove(filePath)
err := unix.Mkfifo(filePath, createPerm)
if err != nil {
return err
return errors.Wrapf(err, "failed to make FIFO special file %s", filePath)
}

fd, err := unix.Open(filePath, unix.O_RDWR|unix.O_NONBLOCK, 0)
if err != nil {
return err
return errors.Wrapf(err, "failed to open file %s", filePath)
}
file := os.NewFile(uintptr(fd), filePath)

Expand Down Expand Up @@ -79,7 +82,10 @@ type supplierFunc func(s string) error

func outputFileAPI(filePath string) (supplier supplierFunc) {
supplier = func(data string) error {
return ioutil.WriteFile(filePath, []byte(data), createPerm)
if err := ioutil.WriteFile(filePath, []byte(data), createPerm); err != nil {
return errors.Wrapf(err, "failed to write to a %s", filePath)
}
return nil
}
return supplier
}