Skip to content

Commit

Permalink
assignableadapters: check usb bundles
Browse files Browse the repository at this point in the history
for collisions and report the error to the user

Signed-off-by: Christoph Ostarek <[email protected]>
  • Loading branch information
christoph-zededa committed Nov 24, 2023
1 parent 3a5d003 commit 26379c8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/pillar/cmd/domainmgr/domainmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2834,7 +2834,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string,
log.Functionf("handlePhysicalIOAdapterListImpl: initialized to get len %d",
len(aa.IoBundleList))

aa.CheckBadUSBBundles(log)
aa.CheckBadUSBBundles()

Check warning on line 2837 in pkg/pillar/cmd/domainmgr/domainmgr.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/domainmgr/domainmgr.go#L2837

Added line #L2837 was not covered by tests
// check for mismatched PCI-ids and assignment groups and mark as errors
aa.CheckBadAssignmentGroups(log, hyper.PCISameController)
for i := range aa.IoBundleList {
Expand Down Expand Up @@ -2880,7 +2880,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string,
"add/modify: %+v", phyAdapter.Phylabel, ib)
aa.AddOrUpdateIoBundle(log, *ib)

aa.CheckBadUSBBundles(log)
aa.CheckBadUSBBundles()

Check warning on line 2883 in pkg/pillar/cmd/domainmgr/domainmgr.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/domainmgr/domainmgr.go#L2883

Added line #L2883 was not covered by tests
// check for mismatched PCI-ids and assignment groups and mark as errors
aa.CheckBadAssignmentGroups(log, hyper.PCISameController)
// Lookup since it could have changed
Expand Down
33 changes: 31 additions & 2 deletions pkg/pillar/types/assignableadapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,37 @@ func (aa *AssignableAdapters) LookupIoBundleIfName(ifname string) *IoBundle {
return nil
}

// CheckBadUSBBundles checks for errors
func (aa *AssignableAdapters) CheckBadUSBBundles(log *base.LogObject) {
// CheckBadUSBBundles sets ib.Error/ErrorTime if bundle collides in regards of USB
func (aa *AssignableAdapters) CheckBadUSBBundles() {
usbProductsAddressMap := make(map[[2]string][]*IoBundle)
for i := range aa.IoBundleList {
ioBundle := &aa.IoBundleList[i]
if ioBundle.UsbAddr == "" && ioBundle.UsbProduct == "" {
continue
}

id := [2]string{ioBundle.UsbAddr, ioBundle.UsbProduct}
if usbProductsAddressMap[id] == nil {
usbProductsAddressMap[id] = make([]*IoBundle, 0)
}
usbProductsAddressMap[id] = append(usbProductsAddressMap[id], ioBundle)
}

for _, bundles := range usbProductsAddressMap {
if len(bundles) <= 1 {
continue
}

errStr := "ioBundle collision:||"

for _, bundle := range bundles {
errStr += fmt.Sprintf("phylabel %s - usbaddr: %s usbproduct: %s||", bundle.Phylabel, bundle.UsbAddr, bundle.UsbProduct)
}
for _, bundle := range bundles {
bundle.Error = errStr
bundle.ErrorTime = time.Now()
}
}
}

// CheckBadAssignmentGroups sets ib.Error/ErrorTime if two IoBundles in different
Expand Down
95 changes: 95 additions & 0 deletions pkg/pillar/types/assignableadapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,98 @@ func TestNVMEIsUsed(t *testing.T) {
}
}
}

func TestCheckBadUSBBundles(t *testing.T) {
t.Parallel()
aa := AssignableAdapters{}

type bundleWithError struct {
bundle IoBundle
expectedError string
}
bundleTestCases := []struct {
bundleWithError []bundleWithError
}{
{
bundleWithError: []bundleWithError{
{
bundle: IoBundle{Phylabel: "1", UsbAddr: "1:1", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 1 - usbaddr: 1:1 usbproduct: a:a||phylabel 2 - usbaddr: 1:1 usbproduct: a:a||",
},
{
bundle: IoBundle{Phylabel: "2", UsbAddr: "1:1", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 1 - usbaddr: 1:1 usbproduct: a:a||phylabel 2 - usbaddr: 1:1 usbproduct: a:a||",
},
},
},
{
bundleWithError: []bundleWithError{
{
bundle: IoBundle{Phylabel: "3", UsbAddr: "1:1", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 3 - usbaddr: 1:1 usbproduct: a:a||phylabel 4 - usbaddr: 1:1 usbproduct: a:a||",
},
{
bundle: IoBundle{Phylabel: "4", UsbAddr: "1:1", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 3 - usbaddr: 1:1 usbproduct: a:a||phylabel 4 - usbaddr: 1:1 usbproduct: a:a||",
},
{
bundle: IoBundle{Phylabel: "5", UsbAddr: "1:1", UsbProduct: ""},
expectedError: "",
},
},
},
{
bundleWithError: []bundleWithError{
{
bundle: IoBundle{Phylabel: "6", UsbAddr: "1:1", UsbProduct: ""},
expectedError: "ioBundle collision:||phylabel 6 - usbaddr: 1:1 usbproduct: ||phylabel 7 - usbaddr: 1:1 usbproduct: ||",
},
{
bundle: IoBundle{Phylabel: "7", UsbAddr: "1:1", UsbProduct: ""},
expectedError: "ioBundle collision:||phylabel 6 - usbaddr: 1:1 usbproduct: ||phylabel 7 - usbaddr: 1:1 usbproduct: ||",
},
},
},
{
bundleWithError: []bundleWithError{
{
bundle: IoBundle{Phylabel: "8", UsbAddr: "", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 8 - usbaddr: usbproduct: a:a||phylabel 9 - usbaddr: usbproduct: a:a||",
},
{
bundle: IoBundle{Phylabel: "9", UsbAddr: "", UsbProduct: "a:a"},
expectedError: "ioBundle collision:||phylabel 8 - usbaddr: usbproduct: a:a||phylabel 9 - usbaddr: usbproduct: a:a||",
},
},
},
{
bundleWithError: []bundleWithError{
{
bundle: IoBundle{Phylabel: "10", UsbAddr: "", UsbProduct: ""},
},
{
bundle: IoBundle{Phylabel: "11", UsbAddr: "", UsbProduct: ""},
},
},
},
}

for _, testCase := range bundleTestCases {
bundles := make([]IoBundle, 0)

for _, bundle := range testCase.bundleWithError {
bundles = append(bundles, bundle.bundle)
}
aa.IoBundleList = bundles

aa.CheckBadUSBBundles()

for i, bundleWithErr := range testCase.bundleWithError {
if bundles[i].Error != bundleWithErr.expectedError {
t.Logf("bundle %s expected error \n'%s', got error \n'%s'",
bundleWithErr.bundle.Phylabel, bundleWithErr.expectedError, bundles[i].Error)

}
}
}
}

0 comments on commit 26379c8

Please sign in to comment.