聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

Ubuntu 20.04上测试vfio-mediated-device(mtty)

2022-02-01 19:16 浏览: 1802764 次 我要评论(0 条) 字号:

参考https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/driver-api/vfio-mediated-device.rst,在Ubuntu 20.04上实验下vfio-mediated-device(mtty)。

1,环境
$ uname -a
Linux lenky-HP 5.10.0-1057-oem #61-Ubuntu SMP Thu Jan 13 15:06:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/issue
Ubuntu 20.04.2 LTS n l

2,准备测试代码
根据环境显示,下载5.10版本的mtty测试代码到本地:

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/plain/samples/vfio-mdev/mtty.c?h=v5.10

$ pwd
/data/lenky
$ ls vfio-mdev-mtty/mtty.c 

在同一目录vfio-mdev-mtty下准备Makefile,如下:

$ cat vfio-mdev-mtty/Makefile 
obj-m:=mtty.o
	KERNELBUILD :=/lib/modules/$(shell uname -r)/build

default:
	make -C $(KERNELBUILD) M=$(shell pwd) modules
	echo insmod ./mtty.ko to turn it on

clean:
	rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions

make编译,生成ko文件,并加载:

$ cd vfio-mdev-mtty/
$ make
y$ sudo insmod mtty.ko 
$ lsmod | grep mtty
mtty                   24576  0
mdev                   28672  2 vfio_mdev,mtty
$ ls /sys/devices/virtual/mtty/mtty/
mdev_supported_types  mtty_dev  power  subsystem  uevent

创建一个mediated device:

$ uuid
b1c6b7d6-8305-11ec-9e57-3b10f7a7b608
$ sudo sh -c 'echo "b1c6b7d6-8305-11ec-9e57-3b10f7a7b608" > "/sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create"'

添加到虚拟机:

$ virsh edit ubuntu20.04 

其中ubuntu20.04是在虚拟系统管理器里看到的虚拟机名称,在编辑器里的里增加如下内容:

    <hostdev mode='subsystem' type='mdev' managed='no' model='vfio-pci' display='off'>
      <source>
        <address uuid='b1c6b7d6-8305-11ec-9e57-3b10f7a7b608'/>
      </source>
    </hostdev>

只需对应的修改uuid后面的值即可。
保存后,再执行virsh edit ubuntu20.04打开会自动在hostdev里增加一行:

      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'>

表示该mdev设备在ubuntu20.04虚拟机里的pci总线位置为0x06。

运行虚拟机后,在虚拟机里可以看到如下信息:

$ lspci | grep 06
06:00.0 Serial controller: WCH.CN CH352 PCI Dual Serial Port Controller (rev 10)
$ dmesg | grep 06:00.0
...
[    1.231102] 0000:06:00.0: ttyS4 at I/O 0xc000 (irq = 22, base_baud = 115200) is a 16550A
[    1.231303] 0000:06:00.0: ttyS5 at I/O 0xc008 (irq = 22, base_baud = 115200) is a 16550A

表示设置的mdev设备在虚拟机里正常运行识别了。

在虚拟机里用minicom操作ttyS4,输入啥,回车后echo啥,就这??待看源码后修改再测。

$ sudo minicom -s
$ sudo minicom

参考:

https://wiki.archlinux.org/title/Intel_GVT-g



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复