summary refs log tree commit diff stats
path: root/hw/usb/bus.c
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2021-01-19 20:44:51 +0100
committerGerd Hoffmann <kraxel@redhat.com>2021-01-22 14:51:35 +0100
commit0f6dba145a4be998e0e5e4ccd94d9df8609eb327 (patch)
treee9b82396f97243e698446634dbc65ac83089da48 /hw/usb/bus.c
parentd755cb9696e8aa16e850ac5f0b908015520cd395 (diff)
downloadfocaccia-qemu-0f6dba145a4be998e0e5e4ccd94d9df8609eb327.tar.gz
focaccia-qemu-0f6dba145a4be998e0e5e4ccd94d9df8609eb327.zip
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

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20210119194452.2148048-1-kraxel@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Diffstat (limited to 'hw/usb/bus.c')
-rw-r--r--hw/usb/bus.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index 2b11041451..064f94e9c3 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -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);
     }