virtio-nvgpu README

项目的 README,来自 github.com/nestrilabs/virtio-nvgpu。合并为一个 markdown 文件:README.md。

virtio-nvgpu

Share one NVIDIA GPU across several VMs, at bare-metal speed.

Run NVIDIA's own unmodified drivers (Vulkan, NVENC, and CUDA) inside multiple microVMs, while the host keeps the card. No vGPU licenses or VFIO passthrough needed.

Why

  1. Passthrough locks the whole GPU to one VM.
  2. vGPU costs real money and only works on datacenter cards.
  3. API forwarding (Venus, virgl) is hella slow, and doesn't support NVENC or CUDA.

virtio-nvgpu forwards the NVIDIA driver instead of the graphics API. The guest builds its own GPU commands and writes them straight to GPU memory, so a frame being drawn doesn't cross the VM boundary. Nothing gets serialized and replayed on the host the way Venus and virgl do it.

How fast

On an RTX 3060:

[!NOTE] 12 is what we tested, not a hard cap. Like containers, you're limited by:

  1. VRAM. A game needs gigabytes, so this usually runs out first. Cap each guest with --vram-limit-mib.
  2. Host CPU and RAM for each guest.
  3. NVENC's 12-session cap on GeForce cards. Our Nestri capture layer encodes with Vulkan Video, which doesn't count against it (16 at once worked fine).

Numbers and method →

Who it's for

People who stream games or GPU apps from headless VMs (cloud gaming, remote desktops, CI with a real GPU, selfhosted "serverless" AI) on consumer NVIDIA cards.

Before you use it

Learn more

Inspired by gVisor's nvproxy, and kayfabe by Reindert Pelsma.

virtio-nvgpu (nestrilabs/virtio-nvgpu)

正在搜索数据库…

这是什么?一个 virtio 设备,能把一块 NVIDIA GPU 以接近裸机的速度共享给多个 KVM 虚拟机。它的做法是转发内核驱动的 ioctl,而不是翻译图形 API。目标场景是无头(headless)推流:虚拟机在 GPU 上完成渲染和编码,离开的只有压缩后的视频。
它装在虚拟机里、宿主机上,还是两边都要装?两边都要:来宾侧是一个内核模块,注册 /dev/nvidia 并通过 virtqueue 转发 ioctl 和 mmap;宿主机侧是一个 Rust 设备 crate,在你的 VMM 里实现设备端。这个 crate 本身不依赖任何特定的 VMM。
怎么让虚拟机用上共享的 GPU?目前还没有一键安装的办法:你要在 Linux KVM 虚拟机里加载来宾内核模块,并让你的宿主机 VMM 通过实现一小套 trait(descriptor 链、事件队列、来宾与宿主机内存映射)来接入这个设备 crate。可选能力不支持时会降级而不是编译失败,所以 VMM 不必等所有功能都支持了才接入。
支持哪些 NVIDIA 驱动和显卡?内核驱动按 ABI 版本范围匹配:自带 535.129.03、580.178.04 和 595.71.05 三个 profile,更旧的驱动会被拒绝。版本号来自运行 595.99.02 驱动的 RTX 3060;运行 615.71.09 的 RTX A2000 能渲染,但没有做过基准测试。
有哪些功能不能用或还没做?没有显示输出(display scanout,画面以视频而非像素形式输出),不支持 MIG 和 SR-IOV,没有 cudaMallocManaged 统一内存,CUDA 除了枚举设备之外未做测试。一块卡上超过四个虚拟机的情况也没试过,面向每个虚拟机的沙箱化 isolate 目前只是设计构想,还不是代码。
代码用什么许可证?许可证是刻意分开的:来宾内核驱动用 GPL-2.0,virtio 设备 crate 用 Apache-2.0,共享的协议头文件则采用 BSD-3-Clause OR GPL-2.0+ 双许可,这样两部分都能包含它们。
虚拟机的性能离裸机到底有多近?对于 2 毫秒及以上的类游戏帧,虚拟机能达到裸机 2% 以内的差距,CPU 占用也相同——在 12 秒不限速的运行中,来宾用时 0.37 秒,宿主机为 0.40 秒。只有非常轻量的帧(0.5 毫秒及以下)会下降,为裸机的 93–71%。
多台虚拟机真的能同时共享一块卡吗?一块 RTX 3060 上四个来宾合计渲染出 103.7 fps,而单个来宾是 102.9 fps,帧时间分布精确到小数点后四位都完全均匀——而且四个来宾同时以 60 Hz 编码 H.264,也没有触发 NVENC 会话上限。四是实际测试过的数量,并不是发现的极限。
为什么不直接用 VFIO 直通或者带 Venus 的 virtio-gpu?VFIO 直通速度是原生的,但要把整块 GPU 独占给一台虚拟机。Venus 会把每一个 API 调用串行化——每帧成千上万次——而且没有实现来宾侧 NVENC 的可行路径。virtio-nvgpu 按每次 ioctl 跨越边界,而渲染循环一次都不会跨,因为提交只是对映射内存的一次写入。
虚拟机用这块共享 GPU 实际能做什么?Vulkan 渲染并在来宾内部向合成器呈现画面、无头 EGL 的 OpenGL、CUDA 设备内存分配以及零拷贝的 CUDA↔Vulkan/GL 互操作,另外还支持从 CUDA 设备指针进行 NVENC 编码和 NVDEC 解码。
大小
5.6 MB
星标
462
时长
8个月
提交 · 90 天