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

相关推荐
未济2 天前
linux 配置环境变量
linux
傲世仙尊2 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.2 天前
进程间通信
linux
Liuqy-052 天前
Linux IO编程——静态库、动态库
linux
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK2 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖2 天前
SimlpeHttp
linux·服务器
qeen872 天前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程
Zenova EdgeOS2 天前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关