-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
qemu: add usb host passthrough #20540
Merged
openshift-merge-bot
merged 1 commit into
containers:main
from
victortoso:usb-host-passthrough
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |||||
"io/fs" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"strconv" | ||||||
"strings" | ||||||
"time" | ||||||
|
||||||
|
@@ -59,6 +60,78 @@ func (v *MachineVM) setNewMachineCMD(qemuBinary string, cmdOpts *setNewMachineCM | |||||
v.CmdLine.SetQmpMonitor(v.QMPMonitor) | ||||||
v.CmdLine.SetNetwork() | ||||||
v.CmdLine.SetSerialPort(v.ReadySocket, v.VMPidFilePath, v.Name) | ||||||
v.CmdLine.SetUSBHostPassthrough(v.USBs) | ||||||
} | ||||||
|
||||||
func parseUSBs(usbs []string) ([]machine.USBConfig, error) { | ||||||
configs := []machine.USBConfig{} | ||||||
for _, str := range usbs { | ||||||
if str == "" { | ||||||
// Ignore --usb="" as it can be used to reset USBConfigs | ||||||
continue | ||||||
} | ||||||
|
||||||
vals := strings.Split(str, ",") | ||||||
if len(vals) != 2 { | ||||||
return configs, fmt.Errorf("usb: fail to parse: missing ',': %s", str) | ||||||
} | ||||||
|
||||||
left := strings.Split(vals[0], "=") | ||||||
if len(left) != 2 { | ||||||
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str) | ||||||
} | ||||||
|
||||||
right := strings.Split(vals[1], "=") | ||||||
if len(right) != 2 { | ||||||
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str) | ||||||
} | ||||||
|
||||||
option := "" | ||||||
if (left[0] == "bus" && right[0] == "devnum") || | ||||||
(right[0] == "bus" && left[0] == "devnum") { | ||||||
option = "bus_devnum" | ||||||
} | ||||||
if (left[0] == "vendor" && right[0] == "product") || | ||||||
(right[0] == "vendor" && left[0] == "product") { | ||||||
option = "vendor_product" | ||||||
} | ||||||
|
||||||
switch option { | ||||||
case "bus_devnum": | ||||||
bus, devnumber := left[1], right[1] | ||||||
if right[0] == "bus" { | ||||||
bus, devnumber = devnumber, bus | ||||||
} | ||||||
|
||||||
configs = append(configs, machine.USBConfig{ | ||||||
Bus: bus, | ||||||
DevNumber: devnumber, | ||||||
}) | ||||||
case "vendor_product": | ||||||
vendorStr, productStr := left[1], right[1] | ||||||
if right[0] == "vendor" { | ||||||
vendorStr, productStr = productStr, vendorStr | ||||||
} | ||||||
|
||||||
vendor, err := strconv.ParseInt(vendorStr, 16, 0) | ||||||
if err != nil { | ||||||
return configs, fmt.Errorf("fail to convert vendor of %s: %s", str, err) | ||||||
Comment on lines
+89
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be simplified option := left[0] +"_"+left[1] // and switch can be match against "vendor_product" and "product_vendor" both. switch |
||||||
} | ||||||
|
||||||
product, err := strconv.ParseInt(productStr, 16, 0) | ||||||
if err != nil { | ||||||
return configs, fmt.Errorf("fail to convert product of %s: %s", str, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same
Suggested change
|
||||||
} | ||||||
|
||||||
configs = append(configs, machine.USBConfig{ | ||||||
Vendor: int(vendor), | ||||||
Product: int(product), | ||||||
}) | ||||||
default: | ||||||
return configs, fmt.Errorf("usb: fail to parse: %s", str) | ||||||
} | ||||||
} | ||||||
return configs, nil | ||||||
} | ||||||
|
||||||
// NewMachine initializes an instance of a virtual machine based on the qemu | ||||||
|
@@ -104,6 +177,9 @@ func (p *QEMUVirtualization) NewMachine(opts machine.InitOptions) (machine.VM, e | |||||
vm.CPUs = opts.CPUS | ||||||
vm.Memory = opts.Memory | ||||||
vm.DiskSize = opts.DiskSize | ||||||
if vm.USBs, err = parseUSBs(opts.USBs); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
vm.Created = time.Now() | ||||||
|
||||||
|
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,79 @@ | ||
package qemu | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/containers/podman/v4/pkg/machine" | ||
) | ||
|
||
func TestUSBParsing(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
result []machine.USBConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Good vendor and product", | ||
args: []string{"vendor=13d3,product=5406", "vendor=08ec,product=0016"}, | ||
result: []machine.USBConfig{ | ||
{ | ||
Vendor: 5075, | ||
Product: 21510, | ||
}, | ||
{ | ||
Vendor: 2284, | ||
Product: 22, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Good bus and device number", | ||
args: []string{"bus=1,devnum=4", "bus=1,devnum=3"}, | ||
result: []machine.USBConfig{ | ||
{ | ||
Bus: "1", | ||
DevNumber: "4", | ||
}, | ||
{ | ||
Bus: "1", | ||
DevNumber: "3", | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Bad vendor and product, not hexa", | ||
args: []string{"vendor=13dk,product=5406"}, | ||
result: []machine.USBConfig{}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Bad vendor and product, bad separator", | ||
args: []string{"vendor=13d3:product=5406"}, | ||
result: []machine.USBConfig{}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Bad vendor and product, missing equal", | ||
args: []string{"vendor=13d3:product-5406"}, | ||
result: []machine.USBConfig{}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got, err := parseUSBs(test.args) | ||
if (err != nil) != test.wantErr { | ||
t.Errorf("parseUUBs error = %v, wantErr %v", err, test.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, test.result) { | ||
t.Errorf("parseUUBs got %v, want %v", got, test.result) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
usb:
adds more context and consistent with other error message.