用户态工具、握手、Sysfs 与部署流程

用户态工具、握手、Sysfs 与部署流程

1. 工具组成

tools/usb/usbip使用Autotools构建,依赖libudev,可选TCP wrappers和hwdata。主要命令:

命令 作用
usbip list -l 列出本机USB及driver状态
usbip bind/unbind -b BUSID 绑定/解除usbip-host
usbip list -r HOST 查询服务端DEVLIST
usbip attach -r HOST -b BUSID 导入设备
usbip port 查看VHCI端口
usbip detach -p PORT 断开虚拟端口
usbipd 监听并响应导入请求
usbipd --device 导出VUDC Gadget

2. 管理协议

用户态协议公共头包含version、opcode和status。当前主要使用:

  • OP_REQ/REP_DEVLIST
  • OP_REQ/REP_IMPORT

源码仍定义DEVINFO、EXPORT、UNEXPORT、CRYPKEY等opcode,但usbipd主请求分发没有实现CRYPKEY;不能把定义存在视为已支持IPsec密钥协商。

3. 物理设备服务端

1
2
3
4
5
modprobe usbip-core
modprobe usbip-host
usbip list -l
usbip bind --busid 1-2
usbipd -D

绑定后原本的usb-storageusbhid等本地driver会被解除,该设备在服务端本地不可继续正常使用。

导出状态可检查:

1
2
3
readlink /sys/bus/usb/devices/1-2/driver
cat /sys/bus/usb/drivers/usbip-host/match_busid
cat /sys/bus/usb/devices/1-2/usbip_status

4. 客户端

1
2
3
4
5
6
modprobe usbip-core
modprobe vhci-hcd
usbip list --remote SERVER
usbip attach --remote SERVER --busid 1-2
usbip port
lsusb -t

Detach:

1
usbip detach --port 0

5. Import握手与FD移交

1
2
3
4
5
6
7
8
9
client usbip                 server usbipd
| OP_REQ_IMPORT(busid) |
|--------------------------->|
| OP_REP_IMPORT |
|<---------------------------|
| write "port fd devid speed"
| to vhci attach sysfs |
| write fd
| to usbip_sockfd

usbipd把accepted socket写给stub/VUDC;client工具把connected socket写给VHCI。内核通过sockfd_lookup()增加socket引用,用户态随后可关闭自己的fd。

这种设计把地址解析、监听和访问策略留在用户态,将URB数据转发留在内核。

6. 关键Sysfs

Stub driver:

1
2
/sys/bus/usb/drivers/usbip-host/match_busid
/sys/bus/usb/drivers/usbip-host/rebind

Stub device:

1
2
3
usbip_status
usbip_sockfd
usbip_debug

VHCI:

1
2
3
4
/sys/devices/platform/vhci_hcd.0/nports
/sys/devices/platform/vhci_hcd.0/attach
/sys/devices/platform/vhci_hcd.0/detach
/sys/devices/platform/vhci_hcd.0/status

VUDC:

1
2
3
/sys/devices/platform/usbip-vudc.0/usbip_status
/sys/devices/platform/usbip-vudc.0/usbip_sockfd
/sys/devices/platform/usbip-vudc.0/dev_desc

这些store属性要求管理权限。不要向它们手工写入来源不明的fd或未验证参数。

7. 服务管理

生产环境至少应:

  • 让usbipd使用受限service account和capability;
  • 只监听管理网络地址;
  • firewall限制TCP 3240源地址;
  • 禁止自动导出新插入设备;
  • 明确设备allowlist;
  • 网络中断时自动清理VHCI端口;
  • 记录bind、attach和detach审计日志。

README中的“关闭防火墙、禁用SELinux”仅是历史调试建议,不应作为产品部署方案。

8. VUDC流程

VUDC服务端不是usbip bind

  1. 创建ConfigFS Gadget;
  2. 绑定到usbip-vudc.N
  3. 启动usbipd --device
  4. 客户端按busid usbip-vudc.N导入。

未绑定Gadget时,VUDC没有可供远端枚举的有效设备descriptor。

文章互动

阅读 --

留言

0 条留言

正在加载留言…