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

相关推荐
Christal_pyy6 分钟前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
csbDD1 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
Natsuagin3 小时前
轻松美化双系统启动界面与同步时间设置(Windows + Ubuntu)
linux·windows·ubuntu·grub
我们的五年4 小时前
【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)
linux·网络·http
我们的五年5 小时前
【Linux网络】TCP/IP地址的有机结合(有能力VS100%???),IP地址的介绍
linux·运维·网络·tcp/ip
davenian6 小时前
< OS 有关 > Ubuntu 24 SSH 服务器更换端口 in jp/us VPSs
linux·ubuntu·ssh
诚信爱国敬业友善7 小时前
GUI编程(window系统→Linux系统)
linux·python·gui
sekaii7 小时前
ReDistribution plan细节
linux·服务器·数据库
YH_DevJourney7 小时前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++
威哥爱编程8 小时前
Linux驱动开发13个实用案例
linux