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

相关推荐
轻松Ai享生活1 天前
5 节课深入学习Linux Cgroups
linux
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5551 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
獭.獭.1 天前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya1 天前
centos配置环境变量jdk
linux·运维·centos
hashiqimiya1 天前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
逆小舟2 天前
【Linux】人事档案——用户及组管理
linux·c++
青草地溪水旁2 天前
pthread_mutex_lock函数深度解析
linux·多线程·pthread