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

相关推荐
探索云原生1 小时前
终于搞懂 Kueue:5 个核心对象一次讲透
linux·docker·ai·云原生·kubernetes
An_s2 小时前
机器学习python之识别图中物品信息
java·linux·开发语言
Championship.23.243 小时前
Linux 3.0 LVDS驱动开发详解
linux·运维·驱动开发·lvds
风向决定发型丶3 小时前
Shell中的特殊变量
linux·运维·bash
mounter6254 小时前
走向长时运行:引入协程(Coroutines),打破 BPF 程序的“一堂到底”限制
linux·ebpf·kernel
运维大师4 小时前
【Linux运维极简教程】05-软件包管理
linux·运维·服务器
六点_dn5 小时前
Linux学习笔记-shell运算符
linux·笔记·学习
hehelm5 小时前
IO 多路复用 — Reactor
linux·服务器·网络·数据库·c++
浅止菌6 小时前
嵌入式项目Makefile越写越乱?一张万能模板+5个技巧,告别重复劳动
linux
刘某的Cloud7 小时前
手工配置nginx的systemd服务
linux·运维·网络·nginx·systemd