-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from fromanirh/snapshot-pci-tree
snapshot: gather full pci tree, handling bridges
- Loading branch information
Showing
9 changed files
with
310 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package address | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
regexAddress *regexp.Regexp = regexp.MustCompile( | ||
`^(([0-9a-f]{0,4}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-9a-f]{1})$`, | ||
) | ||
) | ||
|
||
type Address struct { | ||
Domain string | ||
Bus string | ||
Slot string | ||
Function string | ||
} | ||
|
||
// String() returns the canonical [D]BSF representation of this Address | ||
func (addr *Address) String() string { | ||
return addr.Domain + ":" + addr.Bus + ":" + addr.Slot + "." + addr.Function | ||
} | ||
|
||
// Given a string address, returns a complete Address struct, filled in with | ||
// domain, bus, slot and function components. The address string may either | ||
// be in $BUS:$SLOT.$FUNCTION (BSF) format or it can be a full PCI address | ||
// that includes the 4-digit $DOMAIN information as well: | ||
// $DOMAIN:$BUS:$SLOT.$FUNCTION. | ||
// | ||
// Returns "" if the address string wasn't a valid PCI address. | ||
func FromString(address string) *Address { | ||
addrLowered := strings.ToLower(address) | ||
matches := regexAddress.FindStringSubmatch(addrLowered) | ||
if len(matches) == 6 { | ||
dom := "0000" | ||
if matches[1] != "" { | ||
dom = matches[2] | ||
} | ||
return &Address{ | ||
Domain: dom, | ||
Bus: matches[3], | ||
Slot: matches[4], | ||
Function: matches[5], | ||
} | ||
} | ||
return nil | ||
} |
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,82 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package address_test | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
pciaddr "github.com/jaypipes/ghw/pkg/pci/address" | ||
) | ||
|
||
func TestPCIAddressFromString(t *testing.T) { | ||
|
||
tests := []struct { | ||
addrStr string | ||
expected *pciaddr.Address | ||
// AddressFromString is more flexible than String() and wants | ||
// to accept addresses not in full canonical form, as long as | ||
// it can do the right thing - e.g. a sane default Domain exists. | ||
// Thus we need to sometimes skip the Address -> string check. | ||
skipStringTest bool | ||
}{ | ||
{ | ||
addrStr: "00:00.0", | ||
expected: &pciaddr.Address{ | ||
Domain: "0000", | ||
Bus: "00", | ||
Slot: "00", | ||
Function: "0", | ||
}, | ||
skipStringTest: true, | ||
}, | ||
{ | ||
addrStr: "0000:00:00.0", | ||
expected: &pciaddr.Address{ | ||
Domain: "0000", | ||
Bus: "00", | ||
Slot: "00", | ||
Function: "0", | ||
}, | ||
}, | ||
{ | ||
addrStr: "0000:03:00.0", | ||
expected: &pciaddr.Address{ | ||
Domain: "0000", | ||
Bus: "03", | ||
Slot: "00", | ||
Function: "0", | ||
}, | ||
}, | ||
{ | ||
addrStr: "0000:03:00.A", | ||
expected: &pciaddr.Address{ | ||
Domain: "0000", | ||
Bus: "03", | ||
Slot: "00", | ||
Function: "a", | ||
}, | ||
}, | ||
} | ||
for x, test := range tests { | ||
got := pciaddr.FromString(test.addrStr) | ||
if !reflect.DeepEqual(got, test.expected) { | ||
t.Fatalf("Test #%d failed. Expected %v but got %v", x, test.expected, got) | ||
} | ||
|
||
if test.skipStringTest { | ||
continue | ||
} | ||
|
||
addrStr := got.String() | ||
// addresses are case insensitive | ||
if !strings.EqualFold(addrStr, test.addrStr) { | ||
t.Fatalf("Test #%d failed. Expected %q but got %q (case insensitive match)", x, test.addrStr, addrStr) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.