Virtio-vsock: 沟通VM内外的桥梁

Take-away

  • 定义:virtio-vsock是一种专门用于Guest VM和Host OS交互的网络设备。他一端是Guest VM中的用户态应用,另一端是Host OS上的用户态应用

  • 应用场景:目前在kata-container场景中频繁使用,本文中会时常使用kata-container作为应用背景。

  • 实现方式:本文以vhost-kernel方案为例进行说明

使用方式

给一个简单的python使用范例

Server code

Python 复制代码
#!/usr/bin/env python3
import socket
CID = socket.VMADDR_CID_HOST
PORT = 9999
s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
s.bind((CID, PORT))
s.listen()
(conn, (remote_cid, remote_port)) = s.accept()

print(f"Connection opened by cid={remote_cid} port={remote_port}")

while True:
    buf = conn.recv(64)
    if not buf:
        breakprint(f"Received bytes: {buf}")

Client code

Python 复制代码
#!/usr/bin/env python3
import socket
CID = socket.VMADDR_CID_HOST
PORT = 9999
s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
s.connect((CID, PORT))
s.sendall(b"Hello, world!")
s.close()

其中值得说明的是:

  • AF_VSOCK,是一个特殊的socket family,需要指定使用AF_VSOCK创建socket接口

  • <cid, port>: vsock的网络地址用<cid, port> 来表示,其中cid唯一指定一个VM,每个VM都有固定的cid,port是user app自己指定

QEMU启动方法

使用QEMU启动VM时,需要额外的参数配置

Python 复制代码
qemu-system-x86_64 -device vhost-vsock-pci,guest-cid=123

其中

  • guest-cid指定当前启动VM的cid,也就是上文中提到的vsock网络地址的<cid, port>中的cid

Refs

相关推荐
其实防守也摸鱼4 分钟前
CTF Web 6 题全解 Writeup
linux·sql·学习·算法·安全·ctf·xss
qq_4017004111 分钟前
CAN 开源库大全(嵌入式/上位机,适配STM32、Linux、Qt,附带Github地址)
linux·stm32·开源
AAA代码批发商1 小时前
DAYS 56【Linux 驱动深度】SPI 子系统驱动开发全指南:从架构、核心数据结构到设备驱动实战
linux·驱动开发·架构·内核开发·spi 子系统
听雨笑1 小时前
终端窗口关掉了,正在跑的活会不会死:三种起法各自的下场
linux
程序员-Benothing1 小时前
Linux CPU性能分析:top、mpstat、pidstat、perf实战
linux·运维·服务器
小诗懂技术1 小时前
Linux设备驱动并发控制总结_CSDN
linux·运维·服务器
北风toto2 小时前
银河麒麟 V10 内网 YUM 源修复实战教程(U盘中转版)
linux·服务器
小白鸽i2 小时前
【linux】麒麟桌面版安装人大金仓V8
linux·运维
Ruiery2 小时前
Linux 6.6内核内存管理深度解析(三):页表与缺页处理 — 虚拟地址怎么按需映射到物理页
linux·运维·服务器
ouynagda2 小时前
iMX6ull Linux 嵌入式驱动开发笔记(LCD / ADXL345 / SPI 子系统)
linux·arm开发·驱动开发·笔记·嵌入式硬件