Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | * Introduction |
| 2 | |
| 3 | The name "usbmon" in lowercase refers to a facility in kernel which is |
| 4 | used to collect traces of I/O on the USB bus. This function is analogous |
| 5 | to a packet socket used by network monitoring tools such as tcpdump(1) |
| 6 | or Ethereal. Similarly, it is expected that a tool such as usbdump or |
| 7 | USBMon (with uppercase letters) is used to examine raw traces produced |
| 8 | by usbmon. |
| 9 | |
| 10 | The usbmon reports requests made by peripheral-specific drivers to Host |
| 11 | Controller Drivers (HCD). So, if HCD is buggy, the traces reported by |
| 12 | usbmon may not correspond to bus transactions precisely. This is the same |
| 13 | situation as with tcpdump. |
| 14 | |
Pete Zaitcev | d25bc4d | 2011-02-03 22:01:36 -0700 | [diff] [blame] | 15 | Two APIs are currently implemented: "text" and "binary". The binary API |
| 16 | is available through a character device in /dev namespace and is an ABI. |
| 17 | The text API is deprecated since 2.6.35, but available for convenience. |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * How to use usbmon to collect raw text traces |
| 20 | |
| 21 | Unlike the packet socket, usbmon has an interface which provides traces |
| 22 | in a text format. This is used for two purposes. First, it serves as a |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 23 | common trace exchange format for tools while more sophisticated formats |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | are finalized. Second, humans can read it in case tools are not available. |
| 25 | |
| 26 | To collect a raw text trace, execute following steps. |
| 27 | |
| 28 | 1. Prepare |
| 29 | |
| 30 | Mount debugfs (it has to be enabled in your kernel configuration), and |
| 31 | load the usbmon module (if built as module). The second step is skipped |
| 32 | if usbmon is built into the kernel. |
| 33 | |
| 34 | # mount -t debugfs none_debugs /sys/kernel/debug |
| 35 | # modprobe usbmon |
Pete Zaitcev | d9ac2cf | 2006-06-12 20:09:39 -0700 | [diff] [blame] | 36 | # |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | Verify that bus sockets are present. |
| 39 | |
Rogério Brito | f0cc82a | 2009-08-22 17:33:53 -0300 | [diff] [blame] | 40 | # ls /sys/kernel/debug/usb/usbmon |
Pete Zaitcev | aacf4a0 | 2008-12-04 16:17:00 -0700 | [diff] [blame] | 41 | 0s 0u 1s 1t 1u 2s 2t 2u 3s 3t 3u 4s 4t 4u |
Pete Zaitcev | d9ac2cf | 2006-06-12 20:09:39 -0700 | [diff] [blame] | 42 | # |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Pete Zaitcev | aacf4a0 | 2008-12-04 16:17:00 -0700 | [diff] [blame] | 44 | Now you can choose to either use the socket '0u' (to capture packets on all |
| 45 | buses), and skip to step #3, or find the bus used by your device with step #2. |
| 46 | This allows to filter away annoying devices that talk continuously. |
Paolo 'Blaisorblade' Giarrusso | 092a212 | 2007-08-24 12:19:22 +0200 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 2. Find which bus connects to the desired device |
| 49 | |
| 50 | Run "cat /proc/bus/usb/devices", and find the T-line which corresponds to |
| 51 | the device. Usually you do it by looking for the vendor string. If you have |
| 52 | many similar devices, unplug one and compare two /proc/bus/usb/devices outputs. |
| 53 | The T-line will have a bus number. Example: |
| 54 | |
| 55 | T: Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 0 |
| 56 | D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1 |
| 57 | P: Vendor=0557 ProdID=2004 Rev= 1.00 |
| 58 | S: Manufacturer=ATEN |
| 59 | S: Product=UC100KM V2.00 |
| 60 | |
| 61 | Bus=03 means it's bus 3. |
| 62 | |
| 63 | 3. Start 'cat' |
| 64 | |
Rogério Brito | f0cc82a | 2009-08-22 17:33:53 -0300 | [diff] [blame] | 65 | # cat /sys/kernel/debug/usb/usbmon/3u > /tmp/1.mon.out |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Paolo 'Blaisorblade' Giarrusso | 092a212 | 2007-08-24 12:19:22 +0200 | [diff] [blame] | 67 | to listen on a single bus, otherwise, to listen on all buses, type: |
| 68 | |
Rogério Brito | f0cc82a | 2009-08-22 17:33:53 -0300 | [diff] [blame] | 69 | # cat /sys/kernel/debug/usb/usbmon/0u > /tmp/1.mon.out |
Paolo 'Blaisorblade' Giarrusso | 092a212 | 2007-08-24 12:19:22 +0200 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | This process will be reading until killed. Naturally, the output can be |
| 72 | redirected to a desirable location. This is preferred, because it is going |
| 73 | to be quite long. |
| 74 | |
| 75 | 4. Perform the desired operation on the USB bus |
| 76 | |
| 77 | This is where you do something that creates the traffic: plug in a flash key, |
| 78 | copy files, control a webcam, etc. |
| 79 | |
| 80 | 5. Kill cat |
| 81 | |
| 82 | Usually it's done with a keyboard interrupt (Control-C). |
| 83 | |
| 84 | At this point the output file (/tmp/1.mon.out in this example) can be saved, |
| 85 | sent by e-mail, or inspected with a text editor. In the last case make sure |
| 86 | that the file size is not excessive for your favourite editor. |
| 87 | |
| 88 | * Raw text data format |
| 89 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 90 | Two formats are supported currently: the original, or '1t' format, and |
| 91 | the '1u' format. The '1t' format is deprecated in kernel 2.6.21. The '1u' |
| 92 | format adds a few fields, such as ISO frame descriptors, interval, etc. |
| 93 | It produces slightly longer lines, but otherwise is a perfect superset |
| 94 | of '1t' format. |
| 95 | |
| 96 | If it is desired to recognize one from the other in a program, look at the |
| 97 | "address" word (see below), where '1u' format adds a bus number. If 2 colons |
| 98 | are present, it's the '1t' format, otherwise '1u'. |
| 99 | |
| 100 | Any text format data consists of a stream of events, such as URB submission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | URB callback, submission error. Every event is a text line, which consists |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 102 | of whitespace separated words. The number or position of words may depend |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | on the event type, but there is a set of words, common for all types. |
| 104 | |
| 105 | Here is the list of words, from left to right: |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 106 | |
Pete Zaitcev | aacf4a0 | 2008-12-04 16:17:00 -0700 | [diff] [blame] | 107 | - URB Tag. This is used to identify URBs, and is normally an in-kernel address |
| 108 | of the URB structure in hexadecimal, but can be a sequence number or any |
| 109 | other unique string, within reason. |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | - Timestamp in microseconds, a decimal number. The timestamp's resolution |
| 112 | depends on available clock, and so it can be much worse than a microsecond |
| 113 | (if the implementation uses jiffies, for example). |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | - Event Type. This type refers to the format of the event, not URB type. |
| 116 | Available types are: S - submission, C - callback, E - submission error. |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 117 | |
| 118 | - "Address" word (formerly a "pipe"). It consists of four fields, separated by |
| 119 | colons: URB type and direction, Bus number, Device address, Endpoint number. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | Type and direction are encoded with two bytes in the following manner: |
| 121 | Ci Co Control input and output |
| 122 | Zi Zo Isochronous input and output |
| 123 | Ii Io Interrupt input and output |
| 124 | Bi Bo Bulk input and output |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 125 | Bus number, Device address, and Endpoint are decimal numbers, but they may |
| 126 | have leading zeros, for the sake of human readers. |
| 127 | |
| 128 | - URB Status word. This is either a letter, or several numbers separated |
| 129 | by colons: URB status, interval, start frame, and error count. Unlike the |
| 130 | "address" word, all fields save the status are optional. Interval is printed |
| 131 | only for interrupt and isochronous URBs. Start frame is printed only for |
| 132 | isochronous URBs. Error count is printed only for isochronous callback |
| 133 | events. |
| 134 | |
| 135 | The status field is a decimal number, sometimes negative, which represents |
| 136 | a "status" field of the URB. This field makes no sense for submissions, but |
| 137 | is present anyway to help scripts with parsing. When an error occurs, the |
| 138 | field contains the error code. |
| 139 | |
| 140 | In case of a submission of a Control packet, this field contains a Setup Tag |
| 141 | instead of an group of numbers. It is easy to tell whether the Setup Tag is |
| 142 | present because it is never a number. Thus if scripts find a set of numbers |
| 143 | in this word, they proceed to read Data Length (except for isochronous URBs). |
| 144 | If they find something else, like a letter, they read the setup packet before |
| 145 | reading the Data Length or isochronous descriptors. |
| 146 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 147 | - Setup packet, if present, consists of 5 words: one of each for bmRequestType, |
| 148 | bRequest, wValue, wIndex, wLength, as specified by the USB Specification 2.0. |
| 149 | These words are safe to decode if Setup Tag was 's'. Otherwise, the setup |
| 150 | packet was present, but not captured, and the fields contain filler. |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 151 | |
| 152 | - Number of isochronous frame descriptors and descriptors themselves. |
| 153 | If an Isochronous transfer event has a set of descriptors, a total number |
| 154 | of them in an URB is printed first, then a word per descriptor, up to a |
| 155 | total of 5. The word consists of 3 colon-separated decimal numbers for |
| 156 | status, offset, and length respectively. For submissions, initial length |
| 157 | is reported. For callbacks, actual length is reported. |
| 158 | |
Pete Zaitcev | d9ac2cf | 2006-06-12 20:09:39 -0700 | [diff] [blame] | 159 | - Data Length. For submissions, this is the requested length. For callbacks, |
| 160 | this is the actual length. |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | - Data tag. The usbmon may not always capture data, even if length is nonzero. |
Pete Zaitcev | d9ac2cf | 2006-06-12 20:09:39 -0700 | [diff] [blame] | 163 | The data words are present only if this tag is '='. |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | - Data words follow, in big endian hexadecimal format. Notice that they are |
| 166 | not machine words, but really just a byte stream split into words to make |
| 167 | it easier to read. Thus, the last word may contain from one to four bytes. |
| 168 | The length of collected data is limited and can be less than the data length |
Pete Zaitcev | d25bc4d | 2011-02-03 22:01:36 -0700 | [diff] [blame] | 169 | reported in the Data Length word. In the case of an Isochronous input (Zi) |
| 170 | completion where the received data is sparse in the buffer, the length of |
| 171 | the collected data can be greater than the Data Length value (because Data |
| 172 | Length counts only the bytes that were received whereas the Data words |
| 173 | contain the entire transfer buffer). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | Examples: |
| 176 | |
Pete Zaitcev | ae0d6cc | 2005-06-25 14:32:59 -0700 | [diff] [blame] | 177 | An input control transfer to get a port status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 179 | d5ea89a0 3575914555 S Ci:1:001:0 s a3 00 0000 0003 0004 4 < |
| 180 | d5ea89a0 3575914560 C Ci:1:001:0 0 4 = 01050000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | An output bulk transfer to send a SCSI command 0x5E in a 31-byte Bulk wrapper |
| 183 | to a storage device at address 5: |
| 184 | |
Pete Zaitcev | f1c9e30 | 2007-02-24 19:27:33 -0800 | [diff] [blame] | 185 | dd65f0e8 4128379752 S Bo:1:005:2 -115 31 = 55534243 5e000000 00000000 00000600 00000000 00000000 00000000 000000 |
| 186 | dd65f0e8 4128379808 C Bo:1:005:2 0 31 > |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | * Raw binary format and API |
| 189 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 190 | The overall architecture of the API is about the same as the one above, |
| 191 | only the events are delivered in binary format. Each event is sent in |
| 192 | the following structure (its name is made up, so that we can refer to it): |
| 193 | |
| 194 | struct usbmon_packet { |
| 195 | u64 id; /* 0: URB ID - from submission to callback */ |
| 196 | unsigned char type; /* 8: Same as text; extensible. */ |
| 197 | unsigned char xfer_type; /* ISO (0), Intr, Control, Bulk (3) */ |
| 198 | unsigned char epnum; /* Endpoint number and transfer direction */ |
| 199 | unsigned char devnum; /* Device address */ |
| 200 | u16 busnum; /* 12: Bus number */ |
| 201 | char flag_setup; /* 14: Same as text */ |
| 202 | char flag_data; /* 15: Same as text; Binary zero is OK. */ |
| 203 | s64 ts_sec; /* 16: gettimeofday */ |
| 204 | s32 ts_usec; /* 24: gettimeofday */ |
| 205 | int status; /* 28: */ |
| 206 | unsigned int length; /* 32: Length of data (submitted or actual) */ |
| 207 | unsigned int len_cap; /* 36: Delivered length */ |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 208 | union { /* 40: */ |
| 209 | unsigned char setup[SETUP_LEN]; /* Only for Control S-type */ |
| 210 | struct iso_rec { /* Only for ISO */ |
| 211 | int error_count; |
| 212 | int numdesc; |
| 213 | } iso; |
| 214 | } s; |
| 215 | int interval; /* 48: Only for Interrupt and ISO */ |
| 216 | int start_frame; /* 52: For ISO */ |
| 217 | unsigned int xfer_flags; /* 56: copy of URB's transfer_flags */ |
| 218 | unsigned int ndesc; /* 60: Actual number of ISO descriptors */ |
| 219 | }; /* 64 total length */ |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 220 | |
| 221 | These events can be received from a character device by reading with read(2), |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 222 | with an ioctl(2), or by accessing the buffer with mmap. However, read(2) |
| 223 | only returns first 48 bytes for compatibility reasons. |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 224 | |
| 225 | The character device is usually called /dev/usbmonN, where N is the USB bus |
| 226 | number. Number zero (/dev/usbmon0) is special and means "all buses". |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 227 | Note that specific naming policy is set by your Linux distribution. |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 228 | |
| 229 | If you create /dev/usbmon0 by hand, make sure that it is owned by root |
| 230 | and has mode 0600. Otherwise, unpriviledged users will be able to snoop |
| 231 | keyboard traffic. |
| 232 | |
| 233 | The following ioctl calls are available, with MON_IOC_MAGIC 0x92: |
| 234 | |
| 235 | MON_IOCQ_URB_LEN, defined as _IO(MON_IOC_MAGIC, 1) |
| 236 | |
| 237 | This call returns the length of data in the next event. Note that majority of |
| 238 | events contain no data, so if this call returns zero, it does not mean that |
| 239 | no events are available. |
| 240 | |
| 241 | MON_IOCG_STATS, defined as _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats) |
| 242 | |
| 243 | The argument is a pointer to the following structure: |
| 244 | |
| 245 | struct mon_bin_stats { |
| 246 | u32 queued; |
| 247 | u32 dropped; |
| 248 | }; |
| 249 | |
| 250 | The member "queued" refers to the number of events currently queued in the |
| 251 | buffer (and not to the number of events processed since the last reset). |
| 252 | |
| 253 | The member "dropped" is the number of events lost since the last call |
| 254 | to MON_IOCG_STATS. |
| 255 | |
| 256 | MON_IOCT_RING_SIZE, defined as _IO(MON_IOC_MAGIC, 4) |
| 257 | |
| 258 | This call sets the buffer size. The argument is the size in bytes. |
| 259 | The size may be rounded down to the next chunk (or page). If the requested |
| 260 | size is out of [unspecified] bounds for this kernel, the call fails with |
| 261 | -EINVAL. |
| 262 | |
| 263 | MON_IOCQ_RING_SIZE, defined as _IO(MON_IOC_MAGIC, 5) |
| 264 | |
| 265 | This call returns the current size of the buffer in bytes. |
| 266 | |
| 267 | MON_IOCX_GET, defined as _IOW(MON_IOC_MAGIC, 6, struct mon_get_arg) |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 268 | MON_IOCX_GETX, defined as _IOW(MON_IOC_MAGIC, 10, struct mon_get_arg) |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 269 | |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 270 | These calls wait for events to arrive if none were in the kernel buffer, |
| 271 | then return the first event. The argument is a pointer to the following |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 272 | structure: |
| 273 | |
| 274 | struct mon_get_arg { |
| 275 | struct usbmon_packet *hdr; |
| 276 | void *data; |
| 277 | size_t alloc; /* Length of data (can be zero) */ |
| 278 | }; |
| 279 | |
| 280 | Before the call, hdr, data, and alloc should be filled. Upon return, the area |
| 281 | pointed by hdr contains the next event structure, and the data buffer contains |
| 282 | the data, if any. The event is removed from the kernel buffer. |
| 283 | |
Pete Zaitcev | f4e2332 | 2009-06-10 15:23:52 -0600 | [diff] [blame] | 284 | The MON_IOCX_GET copies 48 bytes to hdr area, MON_IOCX_GETX copies 64 bytes. |
Pete Zaitcev | 471c604 | 2009-02-19 22:54:45 -0700 | [diff] [blame] | 285 | |
Pete Zaitcev | 6f23ee1 | 2006-12-30 22:43:10 -0800 | [diff] [blame] | 286 | MON_IOCX_MFETCH, defined as _IOWR(MON_IOC_MAGIC, 7, struct mon_mfetch_arg) |
| 287 | |
| 288 | This ioctl is primarily used when the application accesses the buffer |
| 289 | with mmap(2). Its argument is a pointer to the following structure: |
| 290 | |
| 291 | struct mon_mfetch_arg { |
| 292 | uint32_t *offvec; /* Vector of events fetched */ |
| 293 | uint32_t nfetch; /* Number of events to fetch (out: fetched) */ |
| 294 | uint32_t nflush; /* Number of events to flush */ |
| 295 | }; |
| 296 | |
| 297 | The ioctl operates in 3 stages. |
| 298 | |
| 299 | First, it removes and discards up to nflush events from the kernel buffer. |
| 300 | The actual number of events discarded is returned in nflush. |
| 301 | |
| 302 | Second, it waits for an event to be present in the buffer, unless the pseudo- |
| 303 | device is open with O_NONBLOCK. |
| 304 | |
| 305 | Third, it extracts up to nfetch offsets into the mmap buffer, and stores |
| 306 | them into the offvec. The actual number of event offsets is stored into |
| 307 | the nfetch. |
| 308 | |
| 309 | MON_IOCH_MFLUSH, defined as _IO(MON_IOC_MAGIC, 8) |
| 310 | |
| 311 | This call removes a number of events from the kernel buffer. Its argument |
| 312 | is the number of events to remove. If the buffer contains fewer events |
| 313 | than requested, all events present are removed, and no error is reported. |
| 314 | This works when no events are available too. |
| 315 | |
| 316 | FIONBIO |
| 317 | |
| 318 | The ioctl FIONBIO may be implemented in the future, if there's a need. |
| 319 | |
| 320 | In addition to ioctl(2) and read(2), the special file of binary API can |
| 321 | be polled with select(2) and poll(2). But lseek(2) does not work. |
| 322 | |
| 323 | * Memory-mapped access of the kernel buffer for the binary API |
| 324 | |
| 325 | The basic idea is simple: |
| 326 | |
| 327 | To prepare, map the buffer by getting the current size, then using mmap(2). |
| 328 | Then, execute a loop similar to the one written in pseudo-code below: |
| 329 | |
| 330 | struct mon_mfetch_arg fetch; |
| 331 | struct usbmon_packet *hdr; |
| 332 | int nflush = 0; |
| 333 | for (;;) { |
| 334 | fetch.offvec = vec; // Has N 32-bit words |
| 335 | fetch.nfetch = N; // Or less than N |
| 336 | fetch.nflush = nflush; |
| 337 | ioctl(fd, MON_IOCX_MFETCH, &fetch); // Process errors, too |
| 338 | nflush = fetch.nfetch; // This many packets to flush when done |
| 339 | for (i = 0; i < nflush; i++) { |
| 340 | hdr = (struct ubsmon_packet *) &mmap_area[vec[i]]; |
| 341 | if (hdr->type == '@') // Filler packet |
| 342 | continue; |
| 343 | caddr_t data = &mmap_area[vec[i]] + 64; |
| 344 | process_packet(hdr, data); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | Thus, the main idea is to execute only one ioctl per N events. |
| 349 | |
| 350 | Although the buffer is circular, the returned headers and data do not cross |
| 351 | the end of the buffer, so the above pseudo-code does not need any gathering. |