[Qemu-devel] RFC: usb redirection protocol v0.2

2011-02-03 Thread Hans de Goede

Hi All,

Attached is version 0.2 of the protocol for the usb redirection stuff
I've been working on. This contains a number of changes based on Gerd
Hoffmann's recommendations and some things which I noticed while
actually implementing things.

Note that this version still is missing a packet type for interrupt
transfers. I've not gotten around to thinking much about those yet,
so rather then adding something adhoc, I've just left it out for now.

Note you can also access this draft here:
http://people.fedoraproject.org/~jwrdegoede/usb-redirection-protocol-0.2.txt

Regards,

Hans
USB Network Redirection protocol description version 0.2 (unreleased/wip)

Revisions
-

Version 0.1
-Initial version (released as initial RFC without a version number)

Version 0.2
-Remove usb_redir_report_descriptor packet, as it is not possible to get
 the cached descriptors from the OS on all platforms and we can do without
-Replace vm-host with usb-guest
-Replace the synchroneous / asynchroneous commands nomenclature with
 control / data packets
-Move the packet id to the main packet header shared by all packets
-Add note: All integers in the protocol are send over the pipe in least
 significant byte first order.
-Add note: All structs are packed
-s/data_size/length/
-Add a usb_redir_cancel_data_packet packet
-Add usb_redir_reset and usb_redir_reset_status packets


USB redirerection protocol (draft)
--

The protocol described in this document is meant for tunneling usb transfers
to a single usb device. Note: not an entire hub, only a single device.

The most significant use case for this is taking a usb device attached to
some machine a which acts as a client / viewer to a virtual machine v
hosted on another machine b, and make the usb device show up inside the
virtual machine as if it were attached directly to the virtual machine v.

The described protocol assumes a reliable ordered bidirectional transport is
available, for example a tcp socket. All integers in the protocol are send
over the pipe in least significant byte first order. All structs send over
the pipe are packed (no padding).

Definitions:
usb-device: The usb-device whose usb transfers are being tunneled.
usb-guest: The entity connecting to the usb-device and using it as if
connected directly to it. For example a virtual machine running a guest
os which accesses a usb-device over the network as if it is part of the
virtual machine.
usb-host: The entity making the usb-device available for use by a usb-guest.
For example a deamon on a machine which exports the usb-device over the
network which then appears inside a virtual machine on another machine.


Basic packet structure / communication
--

Each packet exchanged between the usb-guest and the usb-host starts with a
usb_redir_header, followed by an optional packet type specific header
follow by optional additional data.

The usb_redir_header each packet starts with looks as follows:

struct usb_redir_header {
uint32_t type;
uint32_t length;
uint32_t id;
}

type:This identifies the type of packet, from the type enum
length:  Length of the optional type specific packet header + the optional
 additional data. Can be 0.
id:  A unique id, generated by the usb-guest when sending a packet,
 the usb-host will use the same id in its response packet, allowing
 the usb-guest to match responses to its original requests.

There are 2 types of packets:

1) control packets
2) data packets

Control packets are handled synchroneously inside the usb-host, it will hand
the request over to the host os and then *wait* for a response. The usb-host
will thus stop processing further packets. Where as for data packets the
usb-host hands them over to the host os with the request to let the usb-host
process know when there is a respone from the usb-device.

Note that control packets should only be send to the usb-host when no data
packets are pending on the device / interface / endpoint affected by the
control packet. Any pending data packets will get dropped, and any active
iso streams / allocated bulk streams will get stopped / free-ed.


Packet type list


control packets:
usb_redir_hello
usb_redir_reset
usb_redir_reset_status
usb_redir_set_configuration
usb_redir_get_configuration
usb_redir_configuration_status
usb_redir_set_alt_setting
usb_redir_get_alt_setting
usb_redir_alt_setting_status
usb_redir_start_iso_stream
usb_redir_stop_iso_stream
usb_redir_iso_stream_status
usb_redir_alloc_bulk_streams
usb_redir_free_bulk_streams
usb_redir_bulk_streams_status
usb_redir_cancel_data_packet

data packets:
usb_redir_control_packet
usb_redir_bulk_packet
usb_redir_iso_packet


usb_redir_hello
---

usb_redir_header.type:usb_redir_hello
usb_redir_header.length:  see description

struct usb_redir_hello_header {
char version[64];
uint32_t capabilities[0];
}

No 

[Qemu-devel] RFC; usb redirection protocol

2010-12-10 Thread Hans de Goede

Hi All,

Here is what I have in mind as usb redirection protocol:

USB redirerection protocol (draft)
--

The protocol described in this document is meant for tunneling usb transfers
to a single usb device. Note: not an entire hub, only a single device.

The most significant use case for this is taking a usb device attached to
some machine a which acts as a client / viewer to a virtual machine v
hosted on another machine b, and make the usb device show up inside the
virtual machine as if it were attached directly to the virtual machine v.

The described protocol assumes a reliable ordered bidirectional transport is
available, for example a tcp socket.

Definitions:
vm-host:  machine running the virtual machine with the guest os accessing
  the usb device
usb-host: machine which has the usb device which appears inside the vm


Basic packet structure / communication
--

Each packet exchanged between the vm-host and the usb-host starts with a
usb_redir_header, followed by an optional command specific header
follow by optional additional data.

The usb_redir_header each packet starts with looks as follows:

struct usb_redir_header {
uint32_t command;
uint32_t length;
}

command: A command code, from the command enum
length:  length of the optional command specific header + the optional
 additional data. Can be 0.

There are 3 types of commands:

1) Connection setup commands
2) Synchroneous commands
3) Asynchroneous commands

Given that everything is done over a potentially slow transport in practice
the diferentiating between synchroneous and asynchroneous commands may seem
odd. The difference is how the usb-host will handle them once received. For
synchroneous commands the usb-host will hand the request over to the host os
and then *wait* for a response. This means that the vm-host is guaranteed to
get an immediate response. Where as for asynchroneous commands to usb-host
hands the request over to the host os with the request to let the usb-host
process know when the request is done.

Also note that all synchroneous commands should only be executed when no
asynchroneous commands are pending on the device / interface / endpoint
affected by the synchroneous command. Any pending commands will get
dropped, and any active iso streams / allocated bulk streams will get
stopped / free-ed.


Command list


Connection setup commands:
usb_redir_hello
usb_redir_report_descriptor

Synchroneous commands:
usb_redir_set_configuration
usb_redir_get_configuration
usb_redir_configuration_status
usb_redir_set_alt_setting
usb_redir_get_alt_setting
usb_redir_alt_setting_status
usb_redir_start_iso_stream
usb_redir_stop_iso_stream
usb_redir_iso_stream_status
usb_redir_alloc_bulk_streams
usb_redir_free_bulk_streams
usb_redir_bulk_streams_status

Asynchroneous commands:
usb_redir_control_packet
usb_redir_bulk_packet
usb_redir_iso_packet


usb_redir_hello
---

usb_redir_header.command: usb_redir_hello
usb_redir_header.length:  see description

struct usb_redir_hello_header {
char version[64];
uint32_t capabilities[0];
}

No command specific additional data.

A packet of this type is send by both sides as soon as a connection is
establised. This commands consists of:
version:   A free form 0 terminated version string, useful for logging
   should not be parsed! Suggested format: qemu 0.13,
   usb-redir-daemon 0.1, etc.
capabilities:  A variable length array for announcing capabilities.

The value of the length field depends on the size of the capabilities array.
If we cross the 32 capabilities count, it will go from 1 uint32_t to 2,
etc. the value is 64 + capabilities-array-size * sizeof(uint32_t).

Currently the following capabilities are defined:
usb_redir_cap_bulk_streams: USB 3 bulk streams are supported


usb_redir_report_descriptor
---

usb_redir_header.command: usb_redir_report_desciptor
usb_redir_header.length:  sizeof usb device descriptors

No command specific header.

The command specific additional data contains the entire descriptors for
the usb device.

A packet of this type is send by the usb-host directly after the hello packet
it contains the usb descriptor tables for the usb device.


usb_redir_set_configuration
---

usb_redir_header.command: usb_redir_set_configuration
usb_redir_header.length:  sizeof(usb_redir_set_configuration_header)

struct usb_redir_set_configuration_header {
uint8_t configuration;
}

No command specific additional data.

This command can be send by the vm-host to set (change) the active
configuration of the usb device.

usb_redir_get_configuration
---

usb_redir_header.command: usb_redir_get_configuration
usb_redir_header.length:  0

No command specific header.

No command specific additional data.

This command can be send by the vm-host to get (query) the active