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

Add support for USB hot plugging #686

Merged
merged 1 commit into from
Aug 28, 2024
Merged

Conversation

nesteroff
Copy link
Contributor

@nesteroff nesteroff commented Jul 11, 2024

Description of changes

This adds a service that runs on the host and listens for device add and remove events using libudev. When a new USB device is attached, it is automatically assigned to the designated virtual machine using the official qemu.qmp library."

Checklist for things done

  • Summary of the proposed changes in the PR description
  • More detailed description in the commit message(s)
  • Commits are squashed into relevant entities - avoid a lot of minimal dev time commits in the PR
  • Contribution guidelines followed
  • Ghaf documentation updated with the commit - https://tiiuae.github.io/ghaf/
  • PR linked to architecture documentation and requirement(s) (ticket id)
  • Test procedure described (or includes tests). Select one or more:
    • Tested on Lenovo X1 x86_64
    • Tested on Jetson Orin NX or AGX aarch64
    • Tested on Polarfire riscv64
  • Author has run nix flake check --accept-flake-config and it passes
  • All automatic Github Action checks pass - see actions
  • Author has added reviewers and removed PR draft status

Testing

Input Devices:

  • Connect a USB keyboard or mouse to the Lenovo X1 and verify that it is working in the GUI VM.

Audio Devices:

  • Connect a USB headset to the Lenovo X1 and ssh into the audio-vm.
  • Run aplay -l and find the headset in the list. It should be listed as either card 0 or card 1.
  • To test the sound, execute speaker-test -D hw:1,0 -c 2 -t sine. If the headset appears as card 0, change hw:1,0 to hw:0,0.

If a bug is found or if something is not working as expected, please collect the vhotplug service logs from journalctl on the host.

@nesteroff nesteroff temporarily deployed to internal-build-workflow July 11, 2024 13:09 — with GitHub Actions Inactive
packages/qemuqmp/default.nix Outdated Show resolved Hide resolved
packages/qemuqmp/default.nix Outdated Show resolved Hide resolved
@mbssrc
Copy link
Collaborator

mbssrc commented Jul 30, 2024

This is great. Tested this briefly and worked fine. Let me know what your test results with the other usb devices are!
If you could add ID_INPUT_TOUCHSCREEN etc. (https://github.com/tiiuae/ghaf/blob/main/packages/hardware-scan/hardware-scan.sh#L242) as well, it would also support touchscreens.

Generally (maybe not as part of this PR) we could use a static configuration file as general policy based on device type and VM, and later add-on user defined passthrough for switcheroo between VMs.

@nesteroff
Copy link
Contributor Author

This is great. Tested this briefly and worked fine. Let me know what your test results with the other usb devices are! If you could add ID_INPUT_TOUCHSCREEN etc. (https://github.com/tiiuae/ghaf/blob/main/packages/hardware-scan/hardware-scan.sh#L242) as well, it would also support touchscreens.

Thanks. I added touchscreens to the list as well.

Generally (maybe not as part of this PR) we could use a static configuration file as general policy based on device type and VM, and later add-on user defined passthrough for switcheroo between VMs.

Sure. I tried to keep this app simple but in future versions we will need to improve the configuration to implement more complicated rules, add exclusions, support for the cloud hypervisor and so on.

I’ve tested this with input devices, audio headsets, removable disks, and ethernet adapters. It seems to work fine but I left network devices disabled by default because attaching them to the net-vm breaks remote nixos-rebuild with target-host. Also hot-plugging of disk devices is disabled until we have a storage vm or something like that. Do you know if there are any other device types that we need to support?

@mbssrc
Copy link
Collaborator

mbssrc commented Aug 5, 2024

Perhaps the yubikey and gps devices, so we can get rid of the static external device definition?

@nesteroff
Copy link
Contributor Author

Perhaps the yubikey and gps devices, so we can get rid of the static external device definition?

Sure, I'll look into it.

@nesteroff
Copy link
Contributor Author

Recent updates:

  • Introduced a configuration file for hot-plugging settings.
  • Updated the passthrough algorithm to use USB interface information (class, subclass, protocol). This allows us to passthrough USB devices to VMs without requiring drivers on the host. It should also work when the host is configured not to authorize USB devices by default.
  • When defining a rule for a USB interface class, it is possible to add certain devices to the ignore list so that they remain connected to the host.
  • Unfortunately, some devices do not provide correct USB interface information. For example, our GPS receiver returns 0xFF (Vendor Defined). In such cases we can passthrough the device by VID/PID using either ghaf.hardware.usb.external or ghaf.hardware.usb.vhotplug.

@nesteroff nesteroff temporarily deployed to internal-build-workflow August 15, 2024 14:16 — with GitHub Actions Inactive
Signed-off-by: Yuri Nesterov <yuriy.nesterov@unikie.com>
@nesteroff
Copy link
Contributor Author

Updated to make hot-plugged devices survive suspend and resume.

@nesteroff nesteroff marked this pull request as ready for review August 23, 2024 15:16
@brianmcgillion brianmcgillion added the Needs Testing CI Team to pre-verify label Aug 27, 2024
@milva-unikie
Copy link

Tested on Lenovo-X1 (lenovo-x1-carbon-gen11-debug on USB SSD)

No issues found!

  • USB keyboard and mouse work
  • Both can be connected and disconnected multiple times in a row with no issues
  • Both can be connected before boot with no issues
  • Both work with Lock and Log In screens
  • Audio-vm detects USB headset
  • Boot times and shutdown times are not affected
  • Yubikey works
  • GPS receiver works
  • nixos-rebuild switch works from host
  • Test-automation passes

@milva-unikie milva-unikie added Tested on Lenovo X1 Carbon This PR has been tested on Lenovo X1 Carbon and removed Needs Testing CI Team to pre-verify labels Aug 28, 2024
@brianmcgillion brianmcgillion merged commit 4c568da into tiiuae:main Aug 28, 2024
14 checks passed
@nesteroff nesteroff deleted the vhotplug branch August 29, 2024 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tested on Lenovo X1 Carbon This PR has been tested on Lenovo X1 Carbon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants