-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to report SRIOV devices. Much like GPU devices, support is added using a new top-level package. SRIOV devices are either Physical Functions or Virtual functions. The preferred representation for ghw is Physical Functions, whose dependent devices will be Virtual Functions; however, for the sake of practicality, the API also exposes soft references to Virtual Functions, so consumers of the API can access them directly and not navigating the parent devices. This patch also adds support in `ghwc`, to report the sriov information, and in the `snapshot` package, to make sure to capture all the files in sysfs that ghw cares about. Last but not least, lacking access to suitable non-linux systems, support is provided only on linux OS, even though the API tries hard not to be linux-specific. Resolves: #92 Signed-off-by: Francesco Romani <fromani@redhat.com>
- Loading branch information
Showing
11 changed files
with
636 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaypipes/ghw" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// sriovCmd represents the listing command | ||
var sriovCmd = &cobra.Command{ | ||
Use: "sriov", | ||
Short: "Show SRIOV devices information for the host system", | ||
RunE: showSRIOV, | ||
} | ||
|
||
// showSRIOV show SRIOV physical device information for the host system. | ||
func showSRIOV(cmd *cobra.Command, args []string) error { | ||
sriov, err := ghw.SRIOV() | ||
if err != nil { | ||
return errors.Wrap(err, "error getting SRIOV info") | ||
} | ||
|
||
switch outputFormat { | ||
case outputFormatHuman: | ||
fmt.Printf("%v\n", sriov) | ||
|
||
for _, dev := range sriov.PhysicalFunctions { | ||
fmt.Printf(" %v\n", dev) | ||
} | ||
case outputFormatJSON: | ||
fmt.Printf("%s\n", sriov.JSONString(pretty)) | ||
case outputFormatYAML: | ||
fmt.Printf("%s", sriov.YAMLString()) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(sriovCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package sriov | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaypipes/ghw/pkg/context" | ||
"github.com/jaypipes/ghw/pkg/marshal" | ||
"github.com/jaypipes/ghw/pkg/option" | ||
"github.com/jaypipes/ghw/pkg/pci" | ||
pciaddr "github.com/jaypipes/ghw/pkg/pci/address" | ||
) | ||
|
||
type Device struct { | ||
Interfaces []string `json:"interfaces"` | ||
// the PCI address where the SRIOV instance can be found | ||
Address *pciaddr.Address `json:"address"` | ||
PCI *pci.Device `json:"pci"` | ||
} | ||
|
||
func (d Device) ToString(devType string) string { | ||
deviceStr := d.Address.String() | ||
nodeStr := "" | ||
if d.PCI != nil { | ||
deviceStr = d.PCI.String() | ||
if d.PCI.Node != nil { | ||
nodeStr = fmt.Sprintf(" [affined to NUMA node %d]", d.PCI.Node.ID) | ||
} | ||
} | ||
return fmt.Sprintf("%s function %s@%s", devType, nodeStr, deviceStr) | ||
} | ||
|
||
type PhysicalFunction struct { | ||
Device | ||
MaxVFNum int `json:"max_vf_num,omitempty"` | ||
VFs []VirtualFunction `json:"vfs,omitempty"` | ||
} | ||
|
||
type VirtualFunction struct { | ||
Device | ||
ID int `json:"id"` | ||
// Address of the (parent) Physical Function this Virtual Function pertains to. | ||
ParentAddress *pciaddr.Address `json:"parent_address,omitempty"` | ||
} | ||
|
||
func (pf *PhysicalFunction) String() string { | ||
return fmt.Sprintf("%s with %d/%d virtual functions", | ||
pf.Device.ToString("physical"), | ||
len(pf.VFs), | ||
pf.MaxVFNum, | ||
) | ||
} | ||
|
||
func (vf *VirtualFunction) String() string { | ||
return fmt.Sprintf("%s index %d from %s", | ||
vf.Device.ToString("virtual"), | ||
vf.ID, | ||
vf.ParentAddress, | ||
) | ||
} | ||
|
||
type Info struct { | ||
ctx *context.Context | ||
// All the Physical Functions found in the host system, | ||
PhysicalFunctions []*PhysicalFunction `json:"physical_functions,omitempty"` | ||
// All the Virtual Functions found in the host system, | ||
// This is the very same data found navigating the `PhysicalFunctions`; | ||
// These pointers point back to the corresponding structs in the `PhysicalFunctions` | ||
// slice. | ||
VirtualFunctions []*VirtualFunction `json:"virtual_functions,omitempty"` | ||
} | ||
|
||
// New returns a pointer to an Info struct that contains information about the | ||
// SRIOV devices on the host system. | ||
func New(opts ...*option.Option) (*Info, error) { | ||
ctx := context.New(opts...) | ||
info := &Info{ctx: ctx} | ||
if err := ctx.Do(info.load); err != nil { | ||
return nil, err | ||
} | ||
return info, nil | ||
} | ||
|
||
func (i *Info) String() string { | ||
return fmt.Sprintf( | ||
"sriov (%d phsyical %d virtual devices)", | ||
len(i.PhysicalFunctions), | ||
len(i.VirtualFunctions), | ||
) | ||
} | ||
|
||
// simple private struct used to encapsulate SRIOV information in a top-level | ||
// "sriov" YAML/JSON map/object key | ||
type sriovPrinter struct { | ||
Info *Info `json:"sriov,omitempty"` | ||
} | ||
|
||
// YAMLString returns a string with the SRIOV information formatted as YAML | ||
// under a top-level "sriov:" key | ||
func (i *Info) YAMLString() string { | ||
return marshal.SafeYAML(i.ctx, sriovPrinter{i}) | ||
} | ||
|
||
// JSONString returns a string with the SRIOV information formatted as JSON | ||
// under a top-level "sriov:" key | ||
func (i *Info) JSONString(indent bool) string { | ||
return marshal.SafeJSON(i.ctx, sriovPrinter{i}, indent) | ||
} |
Oops, something went wrong.