usb: add pcap support.
PATCH 1/1 usb: add pcap support.
Log all traffic of a specific usb device to a pcap file for later inspection. File format is compatible with linux usb monitor.
Usage:
qemu -device usb-${somedevice},pcap=file.pcap
wireshark file.pcap
--- include/hw/usb.h | 8 ++ hw/usb/bus.c | 16 +++ hw/usb/core.c | 17 ++++ hw/usb/pcap.c | 242 +++++++++++++++++++++++++++++++++++++++++++++ hw/usb/meson.build | 1 + 5 files changed, 284 insertions(+) create mode 100644 hw/usb/pcap.c
include/hw/usb.h +8 -0
@@ -231,6 +231,9 @@ struct USBDevice {void *opaque;uint32_t flags; +char *pcap_filename; +FILE *pcap; +/* Actual connected speed */ int speed;/* Supported speeds, not in info because it may be variable (hostdevs) */
@@ -570,4 +573,9 @@ int usb_get_quirks(uint16_t vendor_id, uint16_t product_id, uint8_t interface_class, uint8_t interface_subclass, uint8_t interface_protocol); +/* pcap.c */ +void usb_pcap_init (FILE *fp); +void usb_pcap_ctrl (USBPacket *p,bool setup); +void usb_pcap_data (USBPacket *p,bool setup); +#endif
hw/usb/bus.c +16 -0
@@ -23,6 +23,7 @@ static Property usb_props[] = { USB_DEV_FLAG_FULL_PATH, true),DEFINE_PROP_BIT ("msos-desc" , USBDevice, flags,USB_DEV_FLAG_MSOS_DESC_ENABLE , true), +DEFINE_PROP_STRING ("pcap" , USBDevice, pcap_filename), DEFINE_PROP_END_OF_LIST() };
@@ -270,6 +271,17 @@ static void usb_qdev_realize(DeviceState *qdev, Error **errp)return ; } } + +if (dev->pcap_filename) { +int fd =qemu_open_old (dev->pcap_filename,O_CREAT |O_WRONLY |O_TRUNC ,0666 ); +if (fd <0 ) { +error_setg (errp,"open %s failed" , dev->pcap_filename); +usb_qdev_unrealize (qdev); +return ; + } + dev->pcap =fdopen (fd,"w" ); +usb_pcap_init (dev->pcap); + } }static void usb_qdev_unrealize (DeviceState *qdev)
@@ -283,6 +295,10 @@ static void usb_qdev_unrealize(DeviceState *qdev)g_free (s); } +if (dev->pcap) { +fclose (dev->pcap); + } +if (dev->attached) {usb_device_detach (dev); }
hw/usb/core.c +17 -0
@@ -154,6 +154,7 @@ static void do_token_setup(USBDevice *s, USBPacket *p) index = (s->setup_buf[5 ] <<8 ) | s->setup_buf[4 ];if (s->setup_buf[0 ] &USB_DIR_IN ) { +usb_pcap_ctrl (p, true);usb_device_handle_control (s, p, request, value, index, s->setup_len, s->data_buf);if (p->status ==USB_RET_ASYNC ) {
@@ -190,6 +191,7 @@ static void do_token_in(USBDevice *s, USBPacket *p)switch (s->setup_state) {case SETUP_STATE_ACK :if (!(s->setup_buf[0 ] &USB_DIR_IN )) { +usb_pcap_ctrl (p, true);usb_device_handle_control (s, p, request, value, index, s->setup_len, s->data_buf);if (p->status ==USB_RET_ASYNC ) {
@@ -197,6 +199,7 @@ static void do_token_in(USBDevice *s, USBPacket *p) } s->setup_state =SETUP_STATE_IDLE ; p->actual_length =0 ; +usb_pcap_ctrl (p, false); }break ;
@@ -215,6 +218,7 @@ static void do_token_in(USBDevice *s, USBPacket *p) } s->setup_state =SETUP_STATE_IDLE ; p->status =USB_RET_STALL ; +usb_pcap_ctrl (p, false);break ;default :
@@ -230,6 +234,7 @@ static void do_token_out(USBDevice *s, USBPacket *p)case SETUP_STATE_ACK :if (s->setup_buf[0 ] &USB_DIR_IN ) { s->setup_state =SETUP_STATE_IDLE ; +usb_pcap_ctrl (p, false);/* transfer OK */ }else {/* ignore additional output */
@@ -251,6 +256,7 @@ static void do_token_out(USBDevice *s, USBPacket *p) } s->setup_state =SETUP_STATE_IDLE ; p->status =USB_RET_STALL ; +usb_pcap_ctrl (p, false);break ;default :
@@ -288,6 +294,7 @@ static void do_parameter(USBDevice *s, USBPacket *p)usb_packet_copy (p, s->data_buf, s->setup_len); } +usb_pcap_ctrl (p, true);usb_device_handle_control (s, p, request, value, index, s->setup_len, s->data_buf);if (p->status ==USB_RET_ASYNC ) {
@@ -301,6 +308,7 @@ static void do_parameter(USBDevice *s, USBPacket *p) p->actual_length =0 ;usb_packet_copy (p, s->data_buf, s->setup_len); } +usb_pcap_ctrl (p, false); }/* ctrl complete function for devices which use usb_generic_handle_packet and
@@ -311,6 +319,7 @@ void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p) {if (p->status <0 ) { s->setup_state =SETUP_STATE_IDLE ; +usb_pcap_ctrl (p, false); }switch (s->setup_state) {
@@ -325,6 +334,7 @@ void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)case SETUP_STATE_ACK : s->setup_state =SETUP_STATE_IDLE ; p->actual_length =0 ; +usb_pcap_ctrl (p, false);break ;case SETUP_STATE_PARAM :
@@ -359,12 +369,14 @@ USBDevice *usb_find_device(USBPort *port, uint8_t addr)static void usb_process_one (USBPacket *p) {USBDevice *dev = p->ep->dev; +bool nak;/* * Handlers expect status to be initialized to USB_RET_SUCCESS, but it * can be USB_RET_NAK here from a previous usb_process_one() call, * or USB_RET_ASYNC from going through usb_queue_one(). */ + nak = (p->status ==USB_RET_NAK ); p->status =USB_RET_SUCCESS ;if (p->ep->nr ==0 ) {
@@ -388,6 +400,9 @@ static void usb_process_one(USBPacket *p) } }else {/* data pipe */ +if (!nak) { +usb_pcap_data (p, true); + }usb_device_handle_data (dev, p); } }
@@ -439,6 +454,7 @@ void usb_handle_packet(USBDevice *dev, USBPacket *p)assert (p->stream || !p->ep->pipeline ||QTAILQ_EMPTY (&p->ep->queue));if (p->status !=USB_RET_NAK ) { +usb_pcap_data (p, false);usb_packet_set_state (p,USB_PACKET_COMPLETE ); } }
@@ -458,6 +474,7 @@ void usb_packet_complete_one(USBDevice *dev, USBPacket *p) (p->short_not_ok && (p->actual_length < p->iov.size))) { ep->halted = true; } +usb_pcap_data (p, false);usb_packet_set_state (p,USB_PACKET_COMPLETE );QTAILQ_REMOVE (&ep->queue, p, queue); dev->port->ops->complete (dev->port, p);
hw/usb/pcap.c new file +242 -0
242 lines not on this page · raw message
hw/usb/meson.build +1 -0
@@ -5,6 +5,7 @@ softmmu_ss.add(files( 'bus.c', 'combined-packet.c', 'core.c', + 'pcap.c', 'libhw.c' ))
Signature · 1 line
2.29.2
Quoted 6 lines · Gerd Hoffmann
Log all traffic of a specific usb device to a pcap file for later inspection. File format is compatible with linux usb monitor.
Usage:
qemu -device usb-${somedevice},pcap=file.pcap
wireshark file.pcap
Great!
Quoted 9 lines
Signed-off-by Gerd Hoffmann <[email protected]>--- include/hw/usb.h | 8 ++ hw/usb/bus.c | 16 +++ hw/usb/core.c | 17 ++++ hw/usb/pcap.c | 242 +++++++++++++++++++++++++++++++++++++++++++++ hw/usb/meson.build | 1 + 5 files changed, 284 insertions(+) create mode 100644 hw/usb/pcap.c
...
hw/usb/pcap.c
Quoted 6 lines
hw/usb/pcap.c new file +0
-0@@ -0,0 +1,242 @@
Missing license.
Quoted 8 lines
hw/usb/pcap.c new file +8
-0+#include "qemu/osdep.h" +#include "hw/usb.h" + +#define PCAP_MAGIC 0xa1b2c3d4 +#define PCAP_MAJOR 2 +#define PCAP_MINOR 4 + +/* https://wiki.wireshark.org/Development/LibpcapFileFormat */
hw/usb/pcap.c line 18
Quoted 10 lines
hw/usb/pcap.c new file +10
-0+ +struct pcap_hdr { +uint32_t magic_number;/* magic number */ +uint16_t version_major;/* major version number */ +uint16_t version_minor;/* minor version number */ +int32_t thiszone;/* GMT to local correction */ +uint32_t sigfigs;/* accuracy of timestamps */ +uint32_t snaplen;/* max length of captured packets, in octets */ +uint32_t network;/* data link type */ +};
QEMU_PACKED?
hw/usb/pcap.c line 25
Quoted 7 lines
hw/usb/pcap.c new file +7
-0+ +struct pcaprec_hdr { +uint32_t ts_sec;/* timestamp seconds */ +uint32_t ts_usec;/* timestamp microseconds */ +uint32_t incl_len;/* number of octets of packet saved in file */ +uint32_t orig_len;/* actual length of packet */ +};
QEMU_PACKED?
Quoted 24 lines
hw/usb/pcap.c new file +24
-0+ +/* https://www.tcpdump.org/linktypes.html */ +/* linux: Documentation/usb/usbmon.rst */ +/* linux: drivers/usb/mon/mon_bin.c */ + +#define LINKTYPE_USB_LINUX 189/* first 48 bytes only */ +#define LINKTYPE_USB_LINUX_MMAPPED 220/* full 64 byte header */ + +struct usbmon_packet { +uint64_t id;/* 0: URB ID - from submission to callback */ +unsigned char type;/* 8: Same as text; extensible. */ +unsigned char xfer_type;/* ISO (0), Intr, Control, Bulk (3) */ +unsigned char epnum;/* Endpoint number and transfer direction */ +unsigned char devnum;/* Device address */ +uint16_t busnum;/* 12: Bus number */ +char flag_setup;/* 14: Same as text */ +char flag_data;/* 15: Same as text; Binary zero is OK. */ +int64_t ts_sec;/* 16: gettimeofday */ +int32_t ts_usec;/* 24: gettimeofday */ +int32_t status;/* 28: */ +unsigned int length;/* 32: Length of data (submitted or actual) */ +unsigned int len_cap;/* 36: Delivered length */ +union {/* 40: */ +unsigned char setup[8 ];/* Only for Control S-type */
hw/usb/pcap.c line 59
Quoted 10 lines
hw/usb/pcap.c new file +10
-0+struct iso_rec {/* Only for ISO */ +int32_t error_count; +int32_t numdesc; + } iso; + } s; +int32_t interval;/* 48: Only for Interrupt and ISO */ +int32_t start_frame;/* 52: For ISO */ +uint32_t xfer_flags;/* 56: copy of URB's transfer_flags */ +uint32_t ndesc;/* 60: Actual number of ISO descriptors */ +};/* 64 total length */
QEMU_PACKED?
Otherwise:
Hi,
Quoted 8 lines
hw/usb/pcap.c
Quoted 6 lines
hw/usb/pcap.c new file +0
-0@@ -0,0 +1,242 @@Missing license.
Good point.
Quoted 11 lines
hw/usb/pcap.c line 10
Quoted 1 line
hw/usb/pcap.c new file +1
-0+struct pcap_hdr {
QEMU_PACKED?hw/usb/pcap.c line 20
Quoted 1 line
hw/usb/pcap.c new file +1
-0+struct pcaprec_hdr {
QEMU_PACKED?hw/usb/pcap.c line 34
Quoted 1 line
hw/usb/pcap.c new file +1
-0+struct usbmon_packet {
QEMU_PACKED?
Doesn't hurt, but also doesn't make a difference.
The structs are carefully layout-ed to have all
fields properly aligned.
take care,
Gerd