ユーザ用ツール

サイト用ツール


サイドバー

最新の10件
一覧
linux:usbip

USB/IP

USBデバイスをネットワーク経由で共有する.今回はPCのUSB portの調子が悪いのでリモートのport経由で利用するのに使った.USBは最大でも5mなので長い距離を転送するのにも良さそう.

linux 3.17からメインラインkernel に入った.Windows Client(ReactOSでも)の実装もある(動作未確認)

source

Config

$ grep -i usbip /boot/config-`uname -r`
CONFIG_USBIP_CORE=m
CONFIG_USBIP_VHCI_HCD=m
CONFIG_USBIP_VHCI_HC_PORTS=15
CONFIG_USBIP_VHCI_NR_HCS=8
CONFIG_USBIP_HOST=m
CONFIG_USBIP_VUDC=m
# CONFIG_USBIP_DEBUG is not set

Install

Debian

Debian sid amd64

$ sudo apt install usbip

Ubuntu

Ubuntu 21.04 Hirsute Hippo (development branch) amd64

$ sudo apt install linux-tools-common

設定

manのEXAMPLESに利用例が載っている

$ man usbipd | grep ^EXAMPLES$ -A 12
EXAMPLES
           server:# modprobe usbip-host

           server:# usbipd -D
               - Start usbip daemon.

           server:# usbip list --local
               - List driver assignments for usb devices.

           server:# usbip bind --busid=1-2
               - Bind usbip-host.ko to the device of busid 1-2.
               - A usb device 1-2 is now exportable to other hosts!
               - Use 'usbip unbind --busid=1-2' when you want to shutdown exporting and use the device locally.
$ man usbip | grep ^EXAMPLES$ -A 10
EXAMPLES
           client:# usbip list --remote=server
               - List devices exported by remote server.

           client:# modprobe vhci-hcd

           client:# usbip attach --remote=server --busid=1-2
               - Connect the remote USB device.

           client:# usbip detach --port=0
               - Detach the usb device.

Server側の設定

USB/IP host moduleの読み込み

$ sudo modprobe -v usbip_host
insmod /lib/modules/5.8.0-36-generic/kernel/drivers/usb/usbip/usbip-core.ko 
insmod /lib/modules/5.8.0-36-generic/kernel/drivers/usb/usbip/usbip-host.ko

usbipd の起動

$ sudo usbipd -h
usage: usbipd [options]

        -4, --ipv4
                Bind to IPv4. Default is both.

        -6, --ipv6
                Bind to IPv6. Default is both.

        -e, --device
                Run in device mode.
                Rather than drive an attached device, create
                a virtual UDC to bind gadgets to.

        -D, --daemon
                Run as a daemon process.

        -d, --debug
                Print debugging information.

        -PFILE, --pid FILE
                Write process id to FILE.
                If no FILE specified, use /var/run/usbipd.pid

        -tPORT, --tcp-port PORT
                Listen on TCP/IP port PORT.

        -h, --help
                Print this help.

        -v, --version
                Show version.
$ sudo usbipd -D

usbip usage

$ usbip 
usage: usbip [--debug] [--log] [--tcp-port PORT] [version]
             [help] <command> <args>

  attach     Attach a remote USB device
  detach     Detach a remote USB device
  list       List exportable or local USB devices
  bind       Bind device to usbip-host.ko
  unbind     Unbind device from usbip-host.ko
  port       Show imported USB devices

ローカルマシンのUSBデバイスを確認.今回は busid 1-1.1 の Webcam を共有

$ usbip list 
usage: usbip list [-p|--parsable] <args>
    -p, --parsable         Parsable list format
    -r, --remote=<host>    List the exportable USB devices on <host>
    -l, --local            List the local USB devices
$ usbip list -l
 - busid 1-1.1 (046d:0825)
   Logitech, Inc. : Webcam C270 (046d:0825)

 - busid 1-1.4 (0a5c:217f)
   Broadcom Corp. : BCM2045B (BDC-2.1) (0a5c:217f)

 - busid 1-1.5.3 (2109:0715)
   VIA Labs, Inc. : VL817 SATA Adaptor (2109:0715)

busidを指定してbind

$ usbip bind
usage: usbip bind <args>
    -b, --busid=<busid>    Bind usbip-host.ko to device on <busid>
$ sudo usbip bind -b 1-1.1
usbip: info: bind device on busid 1-1.1: complete
$ ls -A /sys/bus/usb/drivers/usbip-host
1-1.1  bind  match_busid  module  rebind  uevent  unbind
$ cat /sys/bus/usb/drivers/usbip-host/match_busid
1-1.1

USB busidではなくUSB deviceのVID/PIDを指定してbindしたい場合(※usbip list -pを利用する場合-lの前に指定しないと効かない)

VID=046d
PID=0825
/usr/sbin/usbip bind --"$(/usr/sbin/usbip list -p -l | /usr/bin/grep \#usbid=${VID}:${PID}\# | /usr/bin/cut '-d#' -f1)"

Clientの設定をして利用する.

デバイスを切断する.

$ usbip unbind
usage: usbip unbind <args>
    -b, --busid=<busid>    Unbind usbip-host.ko from device on <busid>
$ sudo usbip unbind -b 1-1.1
usbip: info: unbind device on busid 1-1.1: complete

daemonをkillする

$ pgrep usbipd
1904196
$ sudo killall usbipd
$ pgrep usbipd

moduleを開放する

$ lsmod | grep usbip
usbip_host             36864  0
usbip_core             40960  1 usbip_host
$ sudo rmmod -v usbip_host usbip_core
$ lsmod | grep usbip

ネットワーク確認

TCP:3240が通るようにしておく

$ sudo iptables -L 2>&1 | grep 3240
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:3240

TCPWRAPPERも設定してあれば設定

Client側の設定

リモートのデバイスを確認

$ usbip list -r 192.168.1.180
Exportable USB devices
======================
 - 192.168.1.180
      1-1.1: Logitech, Inc. : Webcam C270 (046d:0825)
           : /sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1
           : Miscellaneous Device / ? / Interface Association (ef/02/01)

デバイスをattach

$ sudo usbip attach -r 192.168.1.180 -b 1-1.1
$ sudo dmesg
  :
[354830.095734] usb 5-1: SetAddress Request (5) to port 0
[354830.332251] usb 5-1: New USB device found, idVendor=046d, idProduct=0825, bcdDevice= 0.10
[354830.332254] usb 5-1: New USB device strings: Mfr=0, Product=0, SerialNumber=2
[354830.332256] usb 5-1: SerialNumber: 7680CBD0
[354830.333594] uvcvideo: Found UVC 1.00 device <unnamed> (046d:0825)
[354830.446675] usb 5-1: set resolution quirk: cval->res = 384

attachしたデバイスの確認

$ usbip port
Imported USB devices
====================
libusbip: error: fopen
libusbip: error: read_record
Port 00: <Port in Use> at High Speed(480Mbps)
       Logitech, Inc. : Webcam C270 (046d:0825)
       5-1 -> unknown host, remote port and remote busid
           -> remote bus/dev 001/018
$ sudo usbip port
Imported USB devices
====================
Port 00: <Port in Use> at High Speed(480Mbps)
       Logitech, Inc. : Webcam C270 (046d:0825)
       5-1 -> usbip://192.168.1.180:3240/1-1.1
           -> remote bus/dev 001/018
$ ls -A /sys/bus/platform/drivers/vhci_hcd/vhci_hcd.0
attach  driver           modalias  power   status.1  status.3  status.5  status.7   uevent  usb6
detach  driver_override  nports    status  status.2  status.4  status.6  subsystem  usb5    usbip_debug
$ cat /sys/bus/platform/drivers/vhci_hcd/vhci_hcd.0/status 
hub port sta spd dev      sockfd local_busid
hs  0000 004 000 00000000 000000 0-0
hs  0001 004 000 00000000 000000 0-0
hs  0002 004 000 00000000 000000 0-0
hs  0003 004 000 00000000 000000 0-0
hs  0004 004 000 00000000 000000 0-0
hs  0005 004 000 00000000 000000 0-0
hs  0006 004 000 00000000 000000 0-0
hs  0007 004 000 00000000 000000 0-0
hs  0008 004 000 00000000 000000 0-0
hs  0009 004 000 00000000 000000 0-0
hs  0010 004 000 00000000 000000 0-0
hs  0011 004 000 00000000 000000 0-0
hs  0012 004 000 00000000 000000 0-0
hs  0013 004 000 00000000 000000 0-0
hs  0014 004 000 00000000 000000 0-0
ss  0015 004 000 00000000 000000 0-0
ss  0016 004 000 00000000 000000 0-0
ss  0017 004 000 00000000 000000 0-0
ss  0018 004 000 00000000 000000 0-0
ss  0019 004 000 00000000 000000 0-0
ss  0020 004 000 00000000 000000 0-0
ss  0021 004 000 00000000 000000 0-0
ss  0022 004 000 00000000 000000 0-0
ss  0023 004 000 00000000 000000 0-0
ss  0024 004 000 00000000 000000 0-0
ss  0025 004 000 00000000 000000 0-0
ss  0026 004 000 00000000 000000 0-0
ss  0027 004 000 00000000 000000 0-0
ss  0028 004 000 00000000 000000 0-0
ss  0029 004 000 00000000 000000 0-0

リモートUSBデバイスを切断する. <port>usbip port かattach時のdmesgを確認する.

$ sudo usbip detach
usage: usbip detach <args>
    -p, --port=<port>    vhci_hcd port the device is on
$ sudo usbip detach -p 00
usbip: info: Port 0 is now detached!

moduleの開放

$ sudo rmmod -v vhci_hcd usbip_core
$ lsmod | grep vhci_hcd

54MbpsのWi-Fi経由ではVGAは映ったり映らなかったり.QVGAだと安定していそう. 1Gbpsの有線で試すと最高画質でも問題なかった.

画像をプレビューしたまま30時間以上動かし続けて問題なかった.

dhcp環境でipが変わって切断されるパターンがあった.

USB2.0が480MbpsなのでGbEだと大丈夫そう.

USB3.xは大変そう.

永続化

Server

moduleの自動読み込み

$ echo 'usbip_host' | sudo tee -a /etc/modules-load.d/modules.conf
usbip_host

systemd環境でのデーモンの自動起動

/lib/systemd/system/usbipd.service を用意する.

[Unit]
Description=USB-IP Daemon
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/sbin/usbipd --pid /var/run/usbipd.pid
#ExecStop=/usr/bin/pkill -u root,daemon usbipd
ExecStop=/usr/bin/kill `cat /var/run/usbipd.pid`

[Install]
WantedBy=multi-user.target

有効化

$ sudo systemctl enable usbipd.service
Created symlink /etc/systemd/system/multi-user.target.wants/usbipd.service → /lib/systemd/system/usbipd.service.

手動起動

$ sudo systemctl start usbipd.service

USBポートの自動バインド

[Unit]
Description=USB-IP Binding on bus id %I
After=network-online.target usbipd.service
Wants=network-online.target
Requires=usbipd.service

[Service]
Type=simple
ExecStart=/usr/bin/usbip bind -b %i
RemainAfterExit=yes
ExecStop=/usr/bin/usbip unbind -b %i  
Restart=on-failure

[Install]
WantedBy=multi-user.target

有効化と起動

$ sudo systemctl enable usbip-bind@1-1.service
$ sudo systemctl start usbip-bind@1-1.service

※該当ポートにデバイスがない場合エラーとなる

$ sudo usbip bind -b 1-1.5
usbip: error: device with the specified bus ID does not exist

client

moduleの自動読み込み

$ echo vhci-hcd | sudo tee -a /etc/modules-load.d/usbipclient.conf
vhci-hcd

ip制限

tcpwrapperに対応しているよう

$ apt show usbip 2>&1 | grep Depends:
Depends: usb.ids, libc6 (>= 2.7), libudev1 (>= 183), libwrap0 (>= 7.6-4~)
$ which usbipd 
/usr/sbin/usbipd
$ ldd /usr/sbin/usbipd | grep -i libwrap
        libwrap.so.0 => /lib/x86_64-linux-gnu/libwrap.so.0 (0x00007f60ce19b000)

問題

client側でdetachせずにシャットダウンした後USB/IPが利用できない

client側でdetachせずにシャットダウンした後busyになっている

USBIP_CLIENT$ sudo usbip attach -r 192.168.1.188 -b 1-1.2
usbip: error: Attach Request for 1-1.2 failed - Device busy (exported)

USBIP_CLIENT$ usbip list -r 192.168.1.188
usbip: info: no exportable devices found on 192.168.1.188

host側でunbind&bindして

USBIP_HOST$ sudo usbip unbind -b 1-1.2
usbip: info: unbind device on busid 1-1.2: complete
USBIP_HOST$ sudo usbip bind -b 1-1.2
usbip: info: bind device on busid 1-1.2: complete

使えるようになる

USBIP_CLIENT$ usbip list -r 192.168.1.188
Exportable USB devices
======================
 - 192.168.1.188
      1-1.2: Logitech, Inc. : Webcam C270 (046d:0825)
           : /sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2
           : Miscellaneous Device / ? / Interface Association (ef/02/01)

USBIP_CLIENT$ sudo usbip attach -r 192.168.1.188 -b 1-1.2

大量にログが出力される

UVC/UACなウェブカメラをattachするとこういうログが出続ける.UVCを利用し始めると止まる.

$ sudo dmesg | tail
[363364.549049] vhci_hcd: unlink->seqnum 8028
[363364.549053] vhci_hcd: urb->status -104
[363364.553001] vhci_hcd: unlink->seqnum 8030
[363364.553005] vhci_hcd: urb->status -104
[363364.557118] vhci_hcd: unlink->seqnum 8032
[363364.557122] vhci_hcd: urb->status -104
[363364.560898] vhci_hcd: unlink->seqnum 8034
[363364.560901] vhci_hcd: urb->status -104
[363364.564900] vhci_hcd: unlink->seqnum 8036
[363364.564905] vhci_hcd: urb->status -104
$ sudo dmesg | pee "wc -l" "grep vhci_hcd | wc -l"
3146
2984

UVCを使い続けているとこういうメッセージが断続的に出力される

[  +0.000024] usb usb5: Not yet implemented
[  +4.999398] vhci_get_frame_number: 9793 callbacks suppressed
[  +0.000006] usb usb5: Not yet implemented

USB Hubをbind出来ない

USB HUBをbind出来たら便利そうと思ったけどlistに出てこないので無理そう

 lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 4: Dev 23, If 2, Class=Vendor Specific Class, Driver=, 12M
        |__ Port 4: Dev 23, If 0, Class=Wireless, Driver=btusb, 12M
        |__ Port 4: Dev 23, If 3, Class=Application Specific Interface, Driver=, 12M
        |__ Port 4: Dev 23, If 1, Class=Wireless, Driver=btusb, 12M
        |__ Port 5: Dev 5, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 3: Dev 21, If 0, Class=Mass Storage, Driver=uas, 480M
            |__ Port 4: Dev 22, If 0, Class=Vendor Specific Class, Driver=pl2303, 12M
$ usbip list -l
 - busid 1-1.4 (0a5c:217f)
   Broadcom Corp. : BCM2045B (BDC-2.1) (0a5c:217f)

 - busid 1-1.5.3 (2109:0715)
   VIA Labs, Inc. : VL817 SATA Adaptor (2109:0715)

 - busid 1-1.5.4 (067b:2303)
   Prolific Technology, Inc. : PL2303 Serial Port / Mobile Action MA-8910P (067b:2303)

Raspberry Pi OS armhfで共有したデバイスをリモートマシンから利用できない.

$ usbip list -r 10.42.0.253
usbip: error: Exported Device List Request failed - Request Completed Successfully

usbip: error: failed to get device list from 10.42.0.253
$ usbip --debug list -r 10.42.0.253 2>&1 | grep -v 'usbip: debug: names.c:'
usbip: debug: usbip.c:129:[run_command] running command: `list'
usbip: debug: usbip_list.c:138:[list_exported_devices] connected to 10.42.0.253:3240
usbip: debug: usbip_network.c:171:[usbip_net_recv_op_common] usbip_net_recv failed: -1
usbip: error: Exported Device List Request failed - Request Completed Successfully

usbip: error: failed to get device list from 10.42.0.253
$ sudo usbip --debug list -r 10.42.0.253 -b 1-1.3 2>&1 | grep -v 'usbip: debug: names.c:'
usbip: debug: usbip.c:129:[run_command] running command: `list'
usbip: debug: usbip_list.c:138:[list_exported_devices] connected to 10.42.0.253:3240
usbip: debug: usbip_network.c:171:[usbip_net_recv_op_common] usbip_net_recv failed: -1
usbip: error: Exported Device List Request failed - Request Completed Successfully

usbip: error: failed to get device list from 10.42.0.253
$ usbip port
Imported USB devices
====================

Raspberry Pi OSのlocalhostでも見えない

$ usbip list -r localhost
usbip: error: Exported Device List Request failed - Unknown Op Common Status

usbip: error: failed to get device list from localhost

ちなみに, usbip attach でRaspberry Pi OSからリモートのデバイスを利用することは可能

色々いじった環境なので新たに 2021-01-11-raspios-buster-armhf-lite.zipapt update && apt upgrade して usbip を導入した状態で試すと普通に動作した. 元の環境がどこかおかしくなっているよう.

$ sudo modprobe -v usbip-host 
insmod /lib/modules/5.4.83-v7+/kernel/drivers/usb/usbip/usbip-core.ko 
insmod /lib/modules/5.4.83-v7+/kernel/drivers/usb/usbip/usbip-host.ko
$ sudo usbipd -D
$ sudo usbip bind -b 1-1.2
usbip: info: bind device on busid 1-1.2: complete
$ sudo usbip list -r localhost
Exportable USB devices
======================
 - localhost
      1-1.2: Holtek Semiconductor, Inc. : unknown product (04d9:3113)
           : /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2
           : (Defined at Interface level) (00/00/00)
$ dpkg-query -W usbip raspberrypi-kernel
raspberrypi-kernel      1.20210108-1
usbip   2.0+4.18.20-2+rpi1
$ raspinfo | head -25
System Information
------------------

Raspberry Pi 3 Model B Rev 1.2
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"

Raspberry Pi reference 2021-01-11
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 21090519d85bdaa1615d5d5057d37b09368ea5d2, stage2

Linux raspberrypi 5.4.83-v7+ #1379 SMP Mon Dec 14 13:08:57 GMT 2020 armv7l GNU/Linux
Revision        : a32082
Serial          : 000000001f466013
Model           : Raspberry Pi 3 Model B Rev 1.2
Throttled flag  : throttled=0x0
Camera          : supported=0 detected=0

Videocore information
---------------------

Jan  8 2021 14:33:35 
Copyright (c) 2012 Broadcom
version 194a85abd768c7334bbadc3f1911c10a7d18ed14 (clean) (release) (start)
$ lsb_release -dr
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
$ uname -m
armv7l

動かない環境でsourceからusbipをbuildして出来上がった usbipd を利用すると動作した

$ apt source linux
$ cd linux-4.18.20/tools/usb/usbip
$ ./autogen.sh
$ ./configure
$ make
$ sudo modprobe -v usbip_host
$ lsmod | grep usbip
usbip_host             24576  0
usbip_core             16384  1 usbip_host
$ usbip list -l
 - busid 1-1.1 (0424:ec00)
   Standard Microsystems Corp. : SMSC9512/9514 Fast Ethernet Adapter (0424:ec00)

 - busid 1-1.3 (18d1:4ee1)
   Google Inc. : Nexus Device (MTP) (18d1:4ee1)

 - busid 1-1.5 (046d:0825)
   Logitech, Inc. : Webcam C270 (046d:0825)

$ sudo usbipd -D
$ sudo usbip bind -b 1-1.5
usbip: info: bind device on busid 1-1.5: complete
$ usbip list -r localhost
usbip: error: Exported Device List Request failed - Unknown Op Common Status

usbip: error: failed to get device list from localhost
$ pgrep usbipd
1006
$ sudo kill 1006
$ sudo ~/src/linux-4.18.20/tools/usb/usbip/src/usbipd -D
$ usbip list -r localhost
Exportable USB devices
======================
 - localhost
      1-1.5: Logitech, Inc. : Webcam C270 (046d:0825)
           : /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5
           : Miscellaneous Device / ? / Interface Association (ef/02/01)

経路

未確認だが恐らく暗号化などはされていないので経路を暗号化するなどしたほうが安心そう

関連URL

コメント

コメントを入力. Wiki文法が有効です:
   ____  _   __   ___    ____   ___ 
  / __/ | | / /  / _ \  / __/  / _ \
 _\ \   | |/ /  / // / _\ \   / // /
/___/   |___/  /____/ /___/  /____/
 
linux/usbip.txt · 最終更新: 2021/02/13 18:53 by matoken