0001 ======
0002 usbmon
0003 ======
0004
0005 Introduction
0006 ============
0007
0008 The name "usbmon" in lowercase refers to a facility in kernel which is
0009 used to collect traces of I/O on the USB bus. This function is analogous
0010 to a packet socket used by network monitoring tools such as tcpdump(1)
0011 or Ethereal. Similarly, it is expected that a tool such as usbdump or
0012 USBMon (with uppercase letters) is used to examine raw traces produced
0013 by usbmon.
0014
0015 The usbmon reports requests made by peripheral-specific drivers to Host
0016 Controller Drivers (HCD). So, if HCD is buggy, the traces reported by
0017 usbmon may not correspond to bus transactions precisely. This is the same
0018 situation as with tcpdump.
0019
0020 Two APIs are currently implemented: "text" and "binary". The binary API
0021 is available through a character device in /dev namespace and is an ABI.
0022 The text API is deprecated since 2.6.35, but available for convenience.
0023
0024 How to use usbmon to collect raw text traces
0025 ============================================
0026
0027 Unlike the packet socket, usbmon has an interface which provides traces
0028 in a text format. This is used for two purposes. First, it serves as a
0029 common trace exchange format for tools while more sophisticated formats
0030 are finalized. Second, humans can read it in case tools are not available.
0031
0032 To collect a raw text trace, execute following steps.
0033
0034 1. Prepare
0035 ----------
0036
0037 Mount debugfs (it has to be enabled in your kernel configuration), and
0038 load the usbmon module (if built as module). The second step is skipped
0039 if usbmon is built into the kernel::
0040
0041 # mount -t debugfs none_debugs /sys/kernel/debug
0042 # modprobe usbmon
0043 #
0044
0045 Verify that bus sockets are present::
0046
0047 # ls /sys/kernel/debug/usb/usbmon
0048 0s 0u 1s 1t 1u 2s 2t 2u 3s 3t 3u 4s 4t 4u
0049 #
0050
0051 Now you can choose to either use the socket '0u' (to capture packets on all
0052 buses), and skip to step #3, or find the bus used by your device with step #2.
0053 This allows to filter away annoying devices that talk continuously.
0054
0055 2. Find which bus connects to the desired device
0056 ------------------------------------------------
0057
0058 Run "cat /sys/kernel/debug/usb/devices", and find the T-line which corresponds
0059 to the device. Usually you do it by looking for the vendor string. If you have
0060 many similar devices, unplug one and compare the two
0061 /sys/kernel/debug/usb/devices outputs. The T-line will have a bus number.
0062
0063 Example::
0064
0065 T: Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 0
0066 D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
0067 P: Vendor=0557 ProdID=2004 Rev= 1.00
0068 S: Manufacturer=ATEN
0069 S: Product=UC100KM V2.00
0070
0071 "Bus=03" means it's bus 3. Alternatively, you can look at the output from
0072 "lsusb" and get the bus number from the appropriate line. Example:
0073
0074 Bus 003 Device 002: ID 0557:2004 ATEN UC100KM V2.00
0075
0076 3. Start 'cat'
0077 --------------
0078
0079 ::
0080
0081 # cat /sys/kernel/debug/usb/usbmon/3u > /tmp/1.mon.out
0082
0083 to listen on a single bus, otherwise, to listen on all buses, type::
0084
0085 # cat /sys/kernel/debug/usb/usbmon/0u > /tmp/1.mon.out
0086
0087 This process will read until it is killed. Naturally, the output can be
0088 redirected to a desirable location. This is preferred, because it is going
0089 to be quite long.
0090
0091 4. Perform the desired operation on the USB bus
0092 -----------------------------------------------
0093
0094 This is where you do something that creates the traffic: plug in a flash key,
0095 copy files, control a webcam, etc.
0096
0097 5. Kill cat
0098 -----------
0099
0100 Usually it's done with a keyboard interrupt (Control-C).
0101
0102 At this point the output file (/tmp/1.mon.out in this example) can be saved,
0103 sent by e-mail, or inspected with a text editor. In the last case make sure
0104 that the file size is not excessive for your favourite editor.
0105
0106 Raw text data format
0107 ====================
0108
0109 Two formats are supported currently: the original, or '1t' format, and
0110 the '1u' format. The '1t' format is deprecated in kernel 2.6.21. The '1u'
0111 format adds a few fields, such as ISO frame descriptors, interval, etc.
0112 It produces slightly longer lines, but otherwise is a perfect superset
0113 of '1t' format.
0114
0115 If it is desired to recognize one from the other in a program, look at the
0116 "address" word (see below), where '1u' format adds a bus number. If 2 colons
0117 are present, it's the '1t' format, otherwise '1u'.
0118
0119 Any text format data consists of a stream of events, such as URB submission,
0120 URB callback, submission error. Every event is a text line, which consists
0121 of whitespace separated words. The number or position of words may depend
0122 on the event type, but there is a set of words, common for all types.
0123
0124 Here is the list of words, from left to right:
0125
0126 - URB Tag. This is used to identify URBs, and is normally an in-kernel address
0127 of the URB structure in hexadecimal, but can be a sequence number or any
0128 other unique string, within reason.
0129
0130 - Timestamp in microseconds, a decimal number. The timestamp's resolution
0131 depends on available clock, and so it can be much worse than a microsecond
0132 (if the implementation uses jiffies, for example).
0133
0134 - Event Type. This type refers to the format of the event, not URB type.
0135 Available types are: S - submission, C - callback, E - submission error.
0136
0137 - "Address" word (formerly a "pipe"). It consists of four fields, separated by
0138 colons: URB type and direction, Bus number, Device address, Endpoint number.
0139 Type and direction are encoded with two bytes in the following manner:
0140
0141 == == =============================
0142 Ci Co Control input and output
0143 Zi Zo Isochronous input and output
0144 Ii Io Interrupt input and output
0145 Bi Bo Bulk input and output
0146 == == =============================
0147
0148 Bus number, Device address, and Endpoint are decimal numbers, but they may
0149 have leading zeros, for the sake of human readers.
0150
0151 - URB Status word. This is either a letter, or several numbers separated
0152 by colons: URB status, interval, start frame, and error count. Unlike the
0153 "address" word, all fields save the status are optional. Interval is printed
0154 only for interrupt and isochronous URBs. Start frame is printed only for
0155 isochronous URBs. Error count is printed only for isochronous callback
0156 events.
0157
0158 The status field is a decimal number, sometimes negative, which represents
0159 a "status" field of the URB. This field makes no sense for submissions, but
0160 is present anyway to help scripts with parsing. When an error occurs, the
0161 field contains the error code.
0162
0163 In case of a submission of a Control packet, this field contains a Setup Tag
0164 instead of an group of numbers. It is easy to tell whether the Setup Tag is
0165 present because it is never a number. Thus if scripts find a set of numbers
0166 in this word, they proceed to read Data Length (except for isochronous URBs).
0167 If they find something else, like a letter, they read the setup packet before
0168 reading the Data Length or isochronous descriptors.
0169
0170 - Setup packet, if present, consists of 5 words: one of each for bmRequestType,
0171 bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0.
0172 These words are safe to decode if Setup Tag was 's'. Otherwise, the setup
0173 packet was present, but not captured, and the fields contain filler.
0174
0175 - Number of isochronous frame descriptors and descriptors themselves.
0176 If an Isochronous transfer event has a set of descriptors, a total number
0177 of them in an URB is printed first, then a word per descriptor, up to a
0178 total of 5. The word consists of 3 colon-separated decimal numbers for
0179 status, offset, and length respectively. For submissions, initial length
0180 is reported. For callbacks, actual length is reported.
0181
0182 - Data Length. For submissions, this is the requested length. For callbacks,
0183 this is the actual length.
0184
0185 - Data tag. The usbmon may not always capture data, even if length is nonzero.
0186 The data words are present only if this tag is '='.
0187
0188 - Data words follow, in big endian hexadecimal format. Notice that they are
0189 not machine words, but really just a byte stream split into words to make
0190 it easier to read. Thus, the last word may contain from one to four bytes.
0191 The length of collected data is limited and can be less than the data length
0192 reported in the Data Length word. In the case of an Isochronous input (Zi)
0193 completion where the received data is sparse in the buffer, the length of
0194 the collected data can be greater than the Data Length value (because Data
0195 Length counts only the bytes that were received whereas the Data words
0196 contain the entire transfer buffer).
0197
0198 Examples:
0199
0200 An input control transfer to get a port status::
0201
0202 d5ea89a0 3575914555 S Ci:1:001:0 s a3 00 0000 0003 0004 4 <
0203 d5ea89a0 3575914560 C Ci:1:001:0 0 4 = 01050000
0204
0205 An output bulk transfer to send a SCSI command 0x28 (READ_10) in a 31-byte
0206 Bulk wrapper to a storage device at address 5::
0207
0208 dd65f0e8 4128379752 S Bo:1:005:2 -115 31 = 55534243 ad000000 00800000 80010a28 20000000 20000040 00000000 000000
0209 dd65f0e8 4128379808 C Bo:1:005:2 0 31 >
0210
0211 Raw binary format and API
0212 =========================
0213
0214 The overall architecture of the API is about the same as the one above,
0215 only the events are delivered in binary format. Each event is sent in
0216 the following structure (its name is made up, so that we can refer to it)::
0217
0218 struct usbmon_packet {
0219 u64 id; /* 0: URB ID - from submission to callback */
0220 unsigned char type; /* 8: Same as text; extensible. */
0221 unsigned char xfer_type; /* ISO (0), Intr, Control, Bulk (3) */
0222 unsigned char epnum; /* Endpoint number and transfer direction */
0223 unsigned char devnum; /* Device address */
0224 u16 busnum; /* 12: Bus number */
0225 char flag_setup; /* 14: Same as text */
0226 char flag_data; /* 15: Same as text; Binary zero is OK. */
0227 s64 ts_sec; /* 16: gettimeofday */
0228 s32 ts_usec; /* 24: gettimeofday */
0229 int status; /* 28: */
0230 unsigned int length; /* 32: Length of data (submitted or actual) */
0231 unsigned int len_cap; /* 36: Delivered length */
0232 union { /* 40: */
0233 unsigned char setup[SETUP_LEN]; /* Only for Control S-type */
0234 struct iso_rec { /* Only for ISO */
0235 int error_count;
0236 int numdesc;
0237 } iso;
0238 } s;
0239 int interval; /* 48: Only for Interrupt and ISO */
0240 int start_frame; /* 52: For ISO */
0241 unsigned int xfer_flags; /* 56: copy of URB's transfer_flags */
0242 unsigned int ndesc; /* 60: Actual number of ISO descriptors */
0243 }; /* 64 total length */
0244
0245 These events can be received from a character device by reading with read(2),
0246 with an ioctl(2), or by accessing the buffer with mmap. However, read(2)
0247 only returns first 48 bytes for compatibility reasons.
0248
0249 The character device is usually called /dev/usbmonN, where N is the USB bus
0250 number. Number zero (/dev/usbmon0) is special and means "all buses".
0251 Note that specific naming policy is set by your Linux distribution.
0252
0253 If you create /dev/usbmon0 by hand, make sure that it is owned by root
0254 and has mode 0600. Otherwise, unprivileged users will be able to snoop
0255 keyboard traffic.
0256
0257 The following ioctl calls are available, with MON_IOC_MAGIC 0x92:
0258
0259 MON_IOCQ_URB_LEN, defined as _IO(MON_IOC_MAGIC, 1)
0260
0261 This call returns the length of data in the next event. Note that majority of
0262 events contain no data, so if this call returns zero, it does not mean that
0263 no events are available.
0264
0265 MON_IOCG_STATS, defined as _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
0266
0267 The argument is a pointer to the following structure::
0268
0269 struct mon_bin_stats {
0270 u32 queued;
0271 u32 dropped;
0272 };
0273
0274 The member "queued" refers to the number of events currently queued in the
0275 buffer (and not to the number of events processed since the last reset).
0276
0277 The member "dropped" is the number of events lost since the last call
0278 to MON_IOCG_STATS.
0279
0280 MON_IOCT_RING_SIZE, defined as _IO(MON_IOC_MAGIC, 4)
0281
0282 This call sets the buffer size. The argument is the size in bytes.
0283 The size may be rounded down to the next chunk (or page). If the requested
0284 size is out of [unspecified] bounds for this kernel, the call fails with
0285 -EINVAL.
0286
0287 MON_IOCQ_RING_SIZE, defined as _IO(MON_IOC_MAGIC, 5)
0288
0289 This call returns the current size of the buffer in bytes.
0290
0291 MON_IOCX_GET, defined as _IOW(MON_IOC_MAGIC, 6, struct mon_get_arg)
0292 MON_IOCX_GETX, defined as _IOW(MON_IOC_MAGIC, 10, struct mon_get_arg)
0293
0294 These calls wait for events to arrive if none were in the kernel buffer,
0295 then return the first event. The argument is a pointer to the following
0296 structure::
0297
0298 struct mon_get_arg {
0299 struct usbmon_packet *hdr;
0300 void *data;
0301 size_t alloc; /* Length of data (can be zero) */
0302 };
0303
0304 Before the call, hdr, data, and alloc should be filled. Upon return, the area
0305 pointed by hdr contains the next event structure, and the data buffer contains
0306 the data, if any. The event is removed from the kernel buffer.
0307
0308 The MON_IOCX_GET copies 48 bytes to hdr area, MON_IOCX_GETX copies 64 bytes.
0309
0310 MON_IOCX_MFETCH, defined as _IOWR(MON_IOC_MAGIC, 7, struct mon_mfetch_arg)
0311
0312 This ioctl is primarily used when the application accesses the buffer
0313 with mmap(2). Its argument is a pointer to the following structure::
0314
0315 struct mon_mfetch_arg {
0316 uint32_t *offvec; /* Vector of events fetched */
0317 uint32_t nfetch; /* Number of events to fetch (out: fetched) */
0318 uint32_t nflush; /* Number of events to flush */
0319 };
0320
0321 The ioctl operates in 3 stages.
0322
0323 First, it removes and discards up to nflush events from the kernel buffer.
0324 The actual number of events discarded is returned in nflush.
0325
0326 Second, it waits for an event to be present in the buffer, unless the pseudo-
0327 device is open with O_NONBLOCK.
0328
0329 Third, it extracts up to nfetch offsets into the mmap buffer, and stores
0330 them into the offvec. The actual number of event offsets is stored into
0331 the nfetch.
0332
0333 MON_IOCH_MFLUSH, defined as _IO(MON_IOC_MAGIC, 8)
0334
0335 This call removes a number of events from the kernel buffer. Its argument
0336 is the number of events to remove. If the buffer contains fewer events
0337 than requested, all events present are removed, and no error is reported.
0338 This works when no events are available too.
0339
0340 FIONBIO
0341
0342 The ioctl FIONBIO may be implemented in the future, if there's a need.
0343
0344 In addition to ioctl(2) and read(2), the special file of binary API can
0345 be polled with select(2) and poll(2). But lseek(2) does not work.
0346
0347 * Memory-mapped access of the kernel buffer for the binary API
0348
0349 The basic idea is simple:
0350
0351 To prepare, map the buffer by getting the current size, then using mmap(2).
0352 Then, execute a loop similar to the one written in pseudo-code below::
0353
0354 struct mon_mfetch_arg fetch;
0355 struct usbmon_packet *hdr;
0356 int nflush = 0;
0357 for (;;) {
0358 fetch.offvec = vec; // Has N 32-bit words
0359 fetch.nfetch = N; // Or less than N
0360 fetch.nflush = nflush;
0361 ioctl(fd, MON_IOCX_MFETCH, &fetch); // Process errors, too
0362 nflush = fetch.nfetch; // This many packets to flush when done
0363 for (i = 0; i < nflush; i++) {
0364 hdr = (struct ubsmon_packet *) &mmap_area[vec[i]];
0365 if (hdr->type == '@') // Filler packet
0366 continue;
0367 caddr_t data = &mmap_area[vec[i]] + 64;
0368 process_packet(hdr, data);
0369 }
0370 }
0371
0372 Thus, the main idea is to execute only one ioctl per N events.
0373
0374 Although the buffer is circular, the returned headers and data do not cross
0375 the end of the buffer, so the above pseudo-code does not need any gathering.