当前位置: 首页 > article >正文

usb重定向qemu前端处理

1、qemu添加spicevmc前端时会创建vmc通道。
-chardev 'spicevmc,id=usbredirchardev0,name=usbredir'
red::shared_ptr<RedCharDevice>
spicevmc_device_connect(RedsState *reds, SpiceCharDeviceInstance *sin, uint8_t channel_type)
{auto channel(red_vmc_channel_new(reds, channel_type)); //创建RedVmcChannel通道if (!channel) {return red::shared_ptr<RedCharDevice>();}auto dev = red::make_shared<RedCharDeviceSpiceVmc>(sin, reds, channel.get()); //设备与通道绑定channel->chardev_sin = sin;return dev;
}
2、RedVmcChannel中的rcc成员变量说明Channel与client是1对1的关系。
struct RedVmcChannel: public RedChannel
{RedVmcChannel(RedsState *reds, uint32_t type, uint32_t id);~RedVmcChannel() override;void on_connect(RedClient *client, RedStream *stream, int migration, RedChannelCapabilities *caps) override;VmcChannelClient *rcc;RedCharDevice *chardev; /* weak */SpiceCharDeviceInstance *chardev_sin;red::shared_ptr<RedVmcPipeItem> pipe_item;RedCharDeviceWriteBuffer *recv_from_client_buf;uint8_t port_opened;uint32_t queued_data;RedStatCounter in_data;RedStatCounter in_compressed;RedStatCounter in_decompressed;RedStatCounter out_data;RedStatCounter out_compressed;RedStatCounter out_uncompressed;
};
3、usbredir客户端连接上,创建 VmcChannelClient。
void RedVmcChannel::on_connect(RedClient *client, RedStream *stream, int migration,RedChannelCapabilities *caps)
{RedVmcChannel *vmc_channel;SpiceCharDeviceInstance *sin;SpiceCharDeviceInterface *sif;vmc_channel = this;sin = vmc_channel->chardev_sin;if (rcc) {red_channel_warning(this, "channel client (%p) already connected, refusing second connection", rcc);// TODO: notify client in advance about the in use channel using// SPICE_MSG_MAIN_CHANNEL_IN_USE (for example)red_stream_free(stream);return;}rcc = vmc_channel_client_create(this, client, stream, caps);if (!rcc) {return;}vmc_channel->queued_data = 0;rcc->ack_zero_messages_window();if (strcmp(sin->subtype, "port") == 0) {spicevmc_port_send_init(rcc);}if (!vmc_channel->chardev->client_add(reinterpret_cast<RedCharDeviceClientOpaque *>(client), FALSE, 0, ~0, ~0, rcc->is_waiting_for_migrate_data())) {spice_warning("failed to add client to spicevmc");rcc->disconnect();return;}sif = spice_char_device_get_interface(sin);if (sif->state) {sif->state(sin, 1);}
}
4、处理VmcChannelClient客户端消息,写入chardev设备。
bool VmcChannelClient::handle_message(uint16_t type, uint32_t size, void *msg)
{/* NOTE: *msg free by g_free() (when cb to VmcChannelClient::release_recv_buf* with the compressed msg type) */RedVmcChannel *channel;SpiceCharDeviceInterface *sif;channel = get_channel();sif = spice_char_device_get_interface(channel->chardev_sin);switch (type) {case SPICE_MSGC_SPICEVMC_DATA:spice_assert(channel->recv_from_client_buf->buf == msg);stat_inc_counter(channel->in_data, size);channel->recv_from_client_buf->buf_used = size;channel->chardev->write_buffer_add(channel->recv_from_client_buf);channel->recv_from_client_buf = nullptr;break;case SPICE_MSGC_SPICEVMC_COMPRESSED_DATA:return handle_compressed_msg(channel, this, static_cast<SpiceMsgCompressedData *>(msg));break;case SPICE_MSGC_PORT_EVENT:if (size != sizeof(uint8_t)) {spice_warning("bad port event message size");return FALSE;}if (sif->base.minor_version >= 2 && sif->event != nullptr)sif->event(channel->chardev_sin, *static_cast<uint8_t *>(msg));break;default:return RedChannelClient::handle_message(type, size, msg);}return TRUE;
}
5、数据添加到队列中。
void RedCharDevice::write_buffer_add(RedCharDeviceWriteBuffer *write_buf)
{/* caller shouldn't add buffers for client that was removed */if (write_buf->priv->origin == WRITE_BUFFER_ORIGIN_CLIENT &&!red_char_device_client_find(this, write_buf->priv->client)) {g_warning("client not found: this %p client %p", this, write_buf->priv->client);red_char_device_write_buffer_unref(write_buf);return;}g_queue_push_head(&priv->write_queue, write_buf);write_to_device();
}
6、最终写入qemu的spicevmc前端。
int RedCharDevice::write_to_device()
{SpiceCharDeviceInterface *sif;int total = 0;int n;if (!priv->running || priv->wait_for_migrate_data || !priv->sin) {return 0;}/* protect against recursion with red_char_device_wakeup */if (priv->during_write_to_device++ > 0) {return 0;}red::shared_ptr<RedCharDevice> hold_dev(this);if (priv->write_to_dev_timer) {red_timer_cancel(priv->write_to_dev_timer);}sif = spice_char_device_get_interface(priv->sin);while (priv->running) {uint32_t write_len;if (!priv->cur_write_buf) {priv->cur_write_buf =static_cast<RedCharDeviceWriteBuffer *>(g_queue_pop_tail(&priv->write_queue));if (!priv->cur_write_buf)break;priv->cur_write_buf_pos = priv->cur_write_buf->buf;}write_len = priv->cur_write_buf->buf + priv->cur_write_buf->buf_used - priv->cur_write_buf_pos;n = sif->write(priv->sin, priv->cur_write_buf_pos, write_len);if (n <= 0) {if (priv->during_write_to_device > 1) {priv->during_write_to_device = 1;continue; /* a wakeup might have been called during the write - make sure it doesn't get lost */}break;}total += n;write_len -= n;if (!write_len) {write_buffer_release(&priv->cur_write_buf);continue;}priv->cur_write_buf_pos += n;}/* retry writing as long as the write queue is not empty */if (priv->running) {if (priv->cur_write_buf) {if (priv->write_to_dev_timer) {red_timer_start(priv->write_to_dev_timer,CHAR_DEVICE_WRITE_TO_TIMEOUT);}} else {spice_assert(g_queue_is_empty(&priv->write_queue));}priv->active = priv->active || total;}priv->during_write_to_device = 0;return total;
}
7、spice vmc前端注册。
static SpiceCharDeviceInterface vmc_interface = {.base.type          = SPICE_INTERFACE_CHAR_DEVICE,.base.description   = "spice virtual channel char device",.base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,.base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,.state              = vmc_state,.write              = vmc_write,.read               = vmc_read,.event              = vmc_event,.flags              = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
};
8、spice vmc前端写函数实现。
static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
{SpiceChardev *scd = container_of(sin, SpiceChardev, sin);Chardev *chr = CHARDEV(scd);ssize_t out = 0;ssize_t last_out;uint8_t* p = (uint8_t*)buf;while (len > 0) {int can_write = qemu_chr_be_can_write(chr);last_out = MIN(len, can_write);if (last_out <= 0) {break;}qemu_chr_be_write(chr, p, last_out);out += last_out;len -= last_out;p += last_out;}trace_spice_vmc_write(out, len + out);return out;
}
9、调用qemu char前端模块的写。
void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len)
{if (qemu_chr_replay(s)) {if (replay_mode == REPLAY_MODE_PLAY) {return;}replay_chr_be_write(s, buf, len);} else {qemu_chr_be_write_impl(s, buf, len);}
}

10、判断是否有后端设备连接并发送给后端设备

void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len)
{CharBackend *be = s->be;if (be && be->chr_read) {be->chr_read(be->opaque, buf, len);}
}

相关文章:

usb重定向qemu前端处理

1、qemu添加spicevmc前端时会创建vmc通道。 -chardev spicevmc,idusbredirchardev0,nameusbredir red::shared_ptr<RedCharDevice> spicevmc_device_connect(RedsState *reds, SpiceCharDeviceInstance *sin, uint8_t channel_type) {auto channel(red_vmc_channel_new(r…...

OpenHarmony - 小型系统内核(LiteOS-A)(五)

OpenHarmony - 小型系统内核&#xff08;LiteOS-A&#xff09;&#xff08;五&#xff09; 六、文件系统 虚拟文件系统 基本概念 VFS&#xff08;Virtual File System&#xff09;是文件系统的虚拟层&#xff0c;它不是一个实际的文件系统&#xff0c;而是一个异构文件系统之…...

PyTorch进阶学习笔记[长期更新]

第一章 PyTorch简介和安装 PyTorch是一个很强大的深度学习库&#xff0c;在学术中使用占比很大。 我这里是Mac系统的安装&#xff0c;相比起教程中的win/linux安装感觉还是简单不少&#xff08;之前就已经安好啦&#xff09;&#xff0c;有需要指导的小伙伴可以评论。 第二章…...

proteus8.17 环境配置

Proteus介绍 Proteus 8.17 是一款功能强大的电子设计自动化&#xff08;EDA&#xff09;软件&#xff0c;广泛应用于电子电路设计、仿真和分析。以下是其主要特点和新功能&#xff1a; ### 主要功能 - **电路仿真**&#xff1a;支持数字和模拟电路的仿真&#xff0c;包括静态…...

Microsoft SQL Server Management 一键删除数据库所有外键

DECLARE ESQL VARCHAR(1000); DECLARE FCursor CURSOR --定义游标 FOR (SELECT ALTER TABLE O.name DROP CONSTRAINT F.name; AS CommandSQL from SYS.FOREIGN_KEYS F JOIN SYS.ALL_OBJECTS O ON F.PARENT_OBJECT_ID O.OBJECT_ID WHERE O.TYPE U AND F.TYPE …...

【JAVAFX】自定义FXML 文件存放的位置以及使用

情况 1&#xff1a;FXML 文件与调用类在同一个包中&#xff08;推荐&#xff09; 假设类 MainApp 的包是 com.example&#xff0c;且 FXML 文件放在 resources/com/example 下&#xff1a; 项目根目录 ├── src │ └── sample │ └── Main.java ├── src/s…...

Oracle 如何停止正在运行的 Job

Oracle 如何停止正在运行的 Job 先了解是dbms_job 还是 dbms_scheduler&#xff0c;再确定操作命令。 一 使用 DBMS_JOB 包停止作业&#xff08;适用于旧版 Job&#xff09; 1.1 查看正在运行的 Job SELECT job, what, this_date, this_sec, failures, broken FROM user_j…...

高级语言调用C接口(四)结构体(2)-Python

这个专栏好久没有更新了&#xff0c;主要是坑开的有点大&#xff0c;也不知道怎么填&#xff0c;涉及到的开发语言比较多&#xff0c;写起来比较累&#xff0c;需要看的人其实并不多&#xff0c;只能说&#xff0c;慢慢填吧&#xff0c;中间肯定还会插很多别的东西&#xff0c;…...

Java对接Dify API接口完整指南

Java对接Dify API接口完整指南 一、Dify API简介 Dify是一款AI应用开发平台&#xff0c;提供多种自然语言处理能力。通过调用Dify开放API&#xff0c;开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中。 二、准备工作 获取API密钥 登录Dify平台控制台在「API密…...

极狐GitLab GEO 功能介绍

极狐GitLab 是 GitLab 在中国的发行版&#xff0c;关于中文参考文档和资料有&#xff1a; 极狐GitLab 中文文档极狐GitLab 中文论坛极狐GitLab 官网 Geo (PREMIUM SELF) Geo 是广泛分布的开发团队的解决方案&#xff0c;可作为灾难恢复策略的一部分提供热备份。Geo 不是 开箱…...

Nginx-前言

nginx是什么&#xff1f; 轻量级&#xff0c;开源免费的web服务器软件&#xff0c;服务器安装nginx&#xff0c;服务器则成为web服务器 nginx的稳定版版本号&#xff1a; 偶数版本 nginx的相关目录&#xff1a; /etc/nginx/nginx.conf nginx的主配置文件 /etc/nginx/ngi…...

LFI to RCE

LFI不止可以来读取文件&#xff0c;还能用来RCE 在多道CTF题目中都有LFItoRCE的非预期解&#xff0c;下面总结一下LFI的利用姿势 1. /proc/self/environ 利用 条件&#xff1a;目标能读取 /proc/self/environ&#xff0c;并且网页中存在LFI点 利用方式&#xff1a; 修改请…...

云原生(Cloud Native)的详解、开发流程及同类软件对比

以下是云原生&#xff08;Cloud Native&#xff09;的详解、开发流程及同类软件对比&#xff1a; 一、云原生核心概念 定义&#xff1a; 云原生&#xff08;Cloud Native&#xff09;是基于云环境设计和运行应用程序的方法论&#xff0c;强调利用云平台的弹性、分布式和自动化…...

全局唯一标识符(UID)生成策略

目录 一、UUID 二、雪花算法 三、时间戳 随机数 四、利用数据库的自增字段 五、 基于 Redis 的原子操作 总结 在信息系统中&#xff0c;生成唯一ID是非常常见的需求&#xff0c;尤其是在分布式系统或高并发场景下。以下是几种常见的生成唯一ID的算法或方式&#xff1a; …...

学习笔记:减速机工作原理

学习笔记&#xff1a;减速机工作原理 一、减速机图片二、减速比概念三、减速机的速比与扭矩之间的关系四、题外内容--电机扭矩 一、减速机图片 二、减速比概念 即减速装置的传动比&#xff0c;是传动比的一种&#xff0c;是指减速机构中&#xff0c;驱动轴与被驱动轴瞬时输入速…...

《UE5_C++多人TPS完整教程》学习笔记36 ——《P37 拾取组件(Pickup Widget)》

本文为B站系列教学视频 《UE5_C多人TPS完整教程》 —— 《P37 拾取组件&#xff08;Pickup Widget&#xff09;》 的学习笔记&#xff0c;该系列教学视频为计算机工程师、程序员、游戏开发者、作家&#xff08;Engineer, Programmer, Game Developer, Author&#xff09; Steph…...

《空间复杂度(C语言)》

文章目录 前言一、什么是空间复杂度&#xff1f;通俗理解&#xff1a; 二、空间复杂度的数学定义三、常见空间复杂度举例&#xff08;含C语言代码&#xff09;&#x1f539; O(1)&#xff1a;常数空间&#x1f539; O(n)&#xff1a;线性空间&#x1f539; O(n^2)&#xff1a;平…...

Kaggle-Store Sales-(回归+多表合并+xgboost模型)

Store Sales 题意&#xff1a; 给出很多商店&#xff0c;给出商店的类型&#xff0c;某时某刻卖了多少销售额。 给出了油价表&#xff0c;假期表&#xff0c;进货表。 让你求出测试集合中每个商店的销售额是多少。 数据处理: 1.由于是多表&#xff0c;所以要先把其他表与tr…...

在 Tailwind CSS 中优雅地隐藏滚动条

在开发中&#xff0c;我们经常需要隐藏滚动条但保持滚动功能&#xff0c;这在构建现代化的用户界面时很常见。 本文将介绍两种在 Tailwind CSS 项目中实现这一目标的方法&#xff0c;方便同学们记录和查阅。 方法一&#xff1a;使用 tailwind-scrollbar-hide 插件 这是一种更…...

智能合约安全审计平台——以太坊虚拟机安全沙箱

目录 以太坊虚拟机安全沙箱 —— 理论、设计与实战1. 引言2. 理论背景与安全原理2.1 以太坊虚拟机(EVM)概述2.2 安全沙箱的基本概念2.3 安全证明与形式化验证3. 系统架构与模块设计3.1 模块功能说明3.2 模块之间的数据流与安全性4. 安全性与密码学考量4.1 密码学保障在沙箱中…...

std::unordered_map(C++)

std::unordered_map 1. 概述2. 内部实现3. 性能特征4. 常用 API5. 使用示例6. 自定义哈希与相等比较7. 注意事项与优化8. 使用建议9. emplace和insert异同相同点不同点例子对比何时优先使用哪种&#xff1f; 1. 概述 定义&#xff1a;std::unordered_map<Key, T, Hash, KeyE…...

【MCP教程】Claude Desktop 如何连接部署在远程的remote mcp server服务器(remote host)

前言 最近MCP特别火热&#xff0c;笔者自己也根据官方文档尝试了下。 官方文档给的Demo是在本地部署一个weather.py&#xff0c;然后用本地的Claude Desktop去访问该mcp服务器&#xff0c;从而完成工具的调用&#xff1a; 但是&#xff0c;问题来了&#xff0c;Claude Deskto…...

Android Input——输入事件回调完成(十四)

前面几篇文章介绍了事件回调的相关流程,以及回调事件处理函数的相关内容,最后我们再来看一下事件处理完后,如何通知 InputDispatcher 去回调 Callback。 一、客户端回调 在 Android 的事件分发机制中,当客户端(即应用层)完成事件处理后,最终会调用 ViewRootImpl 的 fin…...

数据通信学习笔记之OSPF配置命令

华为 [huawei]ospf 10 router-id 1.1.1.1 //创建ospf进程&#xff0c;本地有效area 1 // 进入区域1network 192.168.1.0 0.0.0.255 // 宣告网段&#xff0c;使用反掩码stub // 配置为stub区域stub no-summary // 配置为Totally Stub 完全末节区域。在ABR上配置&#xff0…...

Python -yield 在python 中什么意思

在 Python 中&#xff0c;yield 是一个关键字&#xff0c;用于定义生成器函数&#xff08;generator function&#xff09;。它的作用是将一个普通函数转变为可迭代的生成器&#xff0c;具有惰性计算的特性。以下是关键要点&#xff1a; 核心概念 生成器函数&#xff1a; 当函数…...

多个路由器互通(静态路由)无单臂路由(简单版)

多个路由器互通&#xff08;静态路由&#xff09;无单臂路由&#xff08;简单版&#xff09; 开启端口并配ip地址 维护1 Router>en Router#conf t Router(config)#int g0/0 Router(config-if)#no shutdown Router(config-if)#ip address 192.168.10.254 255.255.255.0 Ro…...

OpenCV 图形API(38)图像滤波-----Sobel 算子操作函数Sobel()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 cv::gapi::Sobel 函数是 OpenCV 的 G-API 模块中用于执行 Sobel 算子操作的一个函数&#xff0c;主要用于图像的边缘检测。Sobel 算子通过计算图…...

3DS 转 STL 全攻略:传统工具与迪威模型网在线转换深度解析

在 3D 建模与 3D 打印的技术领域中&#xff0c;常常会遇到需要将不同格式的文件进行转换的情况。其中&#xff0c;把 3DS 文件转换为 STL 格式是较为常见的操作。3DS 文件作为一种旧版 Autodesk 3D Studio 使用的 3D 图像格式&#xff0c;存储着丰富的信息&#xff0c;包括网格…...

windows系统安装驱动、cuda和cudnn

一、首先在自己的电脑里安装了nvidia的独立显卡 显卡的查找方式&#xff1a; CtrlShiftEsc打开任务管理器&#xff0c;点击性能&#xff0c;点击GPU 0查看显卡型号&#xff0c;如下图所示&#xff1a; 只要电脑中有nvidia的独立显卡&#xff0c;就可以暗转显卡驱动、cuda和cu…...

嵌入式开发--STM32软件和硬件CRC的使用--续篇

本文是《嵌入式开发–STM32软件和硬件CRC的使用》的续篇&#xff0c;又踩到一个坑&#xff0c;发出来让大家避一下坑。 按照G0系列的设置&#xff0c;得出错误的结果 前文对应的是STM32G0系列&#xff0c;今天在用STM32G4系列时&#xff0c;按照前文的设置&#xff0c;用硬件…...