VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBReadAhead.cpp@ 47631

Last change on this file since 47631 was 46806, checked in by vboxsync, 12 years ago

USB: respect the case that vusbDevGetRh() returns NULL

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* $Id: VUSBReadAhead.cpp 46806 2013-06-26 14:24:17Z vboxsync $ */
2/** @file
3 * Virtual USB - Read-ahead buffering for periodic endpoints.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_VUSB
23#include <VBox/vmm/pdm.h>
24#include <VBox/vmm/vmapi.h>
25#include <VBox/err.h>
26#include <VBox/log.h>
27#include <iprt/alloc.h>
28#include <iprt/time.h>
29#include <iprt/thread.h>
30#include <iprt/semaphore.h>
31#include <iprt/string.h>
32#include <iprt/assert.h>
33#include <iprt/asm.h>
34#include "VUSBInternal.h"
35
36
37/*******************************************************************************
38* Structures and Typedefs *
39*******************************************************************************/
40
41/**
42 * Argument package of vusbDevReadAheadThread().
43 */
44typedef struct vusb_read_ahead_args
45{
46 /** Pointer to the device which the thread is for. */
47 PVUSBDEV pDev;
48 /** Pointer to the pipe which the thread is servicing. */
49 PVUSBPIPE pPipe;
50 /** A flag indicating a high-speed (vs. low/full-speed) endpoint. */
51 bool fHighSpeed;
52 /** A flag telling the thread to terminate. */
53 bool fTerminate;
54} VUSBREADAHEADARGS, *PVUSBREADAHEADARGS;
55
56
57/*******************************************************************************
58* Implementation *
59*******************************************************************************/
60
61static PVUSBURB vusbDevNewIsocUrb(PVUSBDEV pDev, unsigned uEndPt, unsigned uInterval, unsigned uPktSize)
62{
63 PVUSBURB pUrb;
64 unsigned cPackets = 0;
65 uint32_t cbTotal = 0;
66 unsigned uNextIndex = 0;
67
68 Assert(pDev);
69 Assert(uEndPt);
70 Assert(uInterval);
71 Assert(uPktSize);
72
73 /* Calculate the amount of data needed, taking the endpoint's bInterval into account */
74 for (unsigned i = 0; i < 8; ++i)
75 {
76 if (i == uNextIndex)
77 {
78 cbTotal += uPktSize;
79 cPackets++;
80 uNextIndex += uInterval;
81 }
82 }
83 Assert(cbTotal <= 24576);
84
85 // @todo: What do we do if cPackets is 0?
86
87 /*
88 * Allocate and initialize the URB.
89 */
90 Assert(pDev->u8Address != VUSB_INVALID_ADDRESS);
91
92 PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
93 if (!pRh)
94 /* can happen during disconnect */
95 return NULL;
96
97 pUrb = vusbRhNewUrb(pRh, pDev->u8Address, cbTotal, 1);
98 if (!pUrb)
99 /* not much we can do here... */
100 return NULL;
101
102 pUrb->enmType = VUSBXFERTYPE_ISOC;
103 pUrb->EndPt = uEndPt;
104 pUrb->enmDir = VUSBDIRECTION_IN;
105 pUrb->fShortNotOk = false;
106 pUrb->enmStatus = VUSBSTATUS_OK;
107 pUrb->Hci.EdAddr = 0;
108 pUrb->Hci.fUnlinked = false;
109// @todo: fill in the rest? The Hci member is not relevant
110#ifdef LOG_ENABLED
111 static unsigned s_iSerial = 0;
112 s_iSerial = (s_iSerial + 1) % 10000;
113 RTStrAPrintf(&pUrb->pszDesc, "URB %p prab<%04d", pUrb, s_iSerial); // prab = Periodic Read-Ahead Buffer
114#endif
115
116 /* Set up the individual packets, again with bInterval in mind */
117 pUrb->cIsocPkts = 8;
118 unsigned off = 0;
119 uNextIndex = 0;
120 for (unsigned i = 0; i < 8; i++)
121 {
122 pUrb->aIsocPkts[i].enmStatus = VUSBSTATUS_NOT_ACCESSED;
123 pUrb->aIsocPkts[i].off = off;
124 if (i == uNextIndex) // skip unused packets
125 {
126 pUrb->aIsocPkts[i].cb = uPktSize;
127 off += uPktSize;
128 uNextIndex += uInterval;
129 }
130 else
131 pUrb->aIsocPkts[i].cb = 0;
132 }
133 Assert(off == cbTotal);
134 return pUrb;
135}
136
137/**
138 * Thread function for performing read-ahead buffering of periodic input.
139 *
140 * This thread keeps a buffer (queue) filled with data read from a periodic
141 * input endpoint.
142 *
143 * The problem: In the EHCI emulation, there is a very short period between the
144 * time when the guest can schedule a request and the time when it expects the results.
145 * This leads to many dropped URBs because by the time we get the data from the host,
146 * the guest already gave up and moved on.
147 *
148 * The solution: For periodic transfers, we know the worst-case bandwidth. We can
149 * read ahead and buffer a few milliseconds worth of data. That way data is available
150 * by the time the guest asks for it and we can deliver it immediately.
151 *
152 * @returns success indicator.
153 * @param Thread This thread.
154 * @param pvUser Pointer to a VUSBREADAHEADARGS structure.
155 */
156static DECLCALLBACK(int) vusbDevReadAheadThread(RTTHREAD Thread, void *pvUser)
157{
158 PVUSBPIPE pPipe;
159 PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pvUser;
160 PCVUSBDESCENDPOINT pDesc;
161 PVUSBURB pUrb;
162 int rc = VINF_SUCCESS;
163 unsigned max_pkt_size, mult, interval;
164
165 LogFlow(("vusb: periodic read-ahead buffer thread started\n"));
166 Assert(pArgs);
167 Assert(pArgs->pPipe && pArgs->pDev);
168
169 pPipe = pArgs->pPipe;
170 pDesc = &pPipe->in->Core;
171 Assert(pDesc);
172
173 /* The previous read-ahead thread could be still running (vusbReadAheadStop sets only
174 * fTerminate to true and returns immediately). Therefore we have to wait until the
175 * previous thread is done and all submitted URBs are completed. */
176 while (pPipe->cSubmitted > 0)
177 {
178 Log2(("vusbDevReadAheadThread: still %u packets submitted, waiting before starting...\n", pPipe->cSubmitted));
179 RTThreadSleep(1);
180 }
181 pPipe->pvReadAheadArgs = pArgs;
182 pPipe->cBuffered = 0;
183
184 /* Figure out the maximum bandwidth we might need */
185 if (pArgs->fHighSpeed)
186 {
187 /* High-speed endpoint */
188 Assert((pDesc->wMaxPacketSize & 0x1fff) == pDesc->wMaxPacketSize);
189 Assert(pDesc->bInterval <= 16);
190 interval = pDesc->bInterval ? 1 << (pDesc->bInterval - 1) : 1;
191 max_pkt_size = pDesc->wMaxPacketSize & 0x7ff;
192 mult = ((pDesc->wMaxPacketSize & 0x1800) >> 11) + 1;
193 }
194 else
195 {
196 /* Full- or low-speed endpoint */
197 Assert((pDesc->wMaxPacketSize & 0x7ff) == pDesc->wMaxPacketSize);
198 interval = pDesc->bInterval;
199 max_pkt_size = pDesc->wMaxPacketSize;
200 mult = 1;
201 }
202 Log(("vusb: interval=%u, max pkt size=%u, multiplier=%u\n", interval, max_pkt_size, mult));
203
204 /*
205 * Submit new URBs in a loop unless the buffer is too full (paused VM etc.). Note that we only
206 * queue the URBs here, they are reaped on a different thread.
207 */
208 while (pArgs->fTerminate == false)
209 {
210 while (pPipe->cSubmitted < 120 && pPipe->cBuffered < 120)
211 {
212 pUrb = vusbDevNewIsocUrb(pArgs->pDev, pDesc->bEndpointAddress & 0xF, interval, max_pkt_size * mult);
213 if (!pUrb) {
214 /* Happens if device was unplugged. */
215 Log(("vusb: read-ahead thread failed to allocate new URB; exiting\n"));
216 vusbReadAheadStop(pvUser);
217 break;
218 }
219
220 Assert(pUrb->enmState == VUSBURBSTATE_ALLOCATED);
221
222 // @todo: at the moment we abuse the Hci.pNext member (which is otherwise entirely unused!)
223 pUrb->Hci.pNext = (PVUSBURB)pvUser;
224
225 pUrb->enmState = VUSBURBSTATE_IN_FLIGHT;
226 rc = vusbUrbQueueAsyncRh(pUrb);
227 if (RT_FAILURE(rc))
228 {
229 /* Happens if device was unplugged. */
230 Log(("vusb: read-ahead thread failed to queue URB with %Rrc; exiting\n", rc));
231 vusbReadAheadStop(pvUser);
232 break;
233 }
234 ++pPipe->cSubmitted;
235 }
236 RTThreadSleep(1);
237 }
238 LogFlow(("vusb: periodic read-ahead buffer thread exiting\n"));
239 pPipe->pvReadAheadArgs = NULL;
240
241 /* wait until there are no more submitted packets */
242 while (pPipe->cSubmitted > 0)
243 {
244 Log2(("vusbDevReadAheadThread: still %u packets submitted, waiting before terminating...\n", pPipe->cSubmitted));
245 RTThreadSleep(1);
246 }
247
248 RTMemTmpFree(pArgs);
249
250 return rc;
251}
252
253/**
254 * Completes a read-ahead URB. This function does *not* free the URB but puts
255 * it on a queue instead. The URB is only freed when the guest asks for the data
256 * (by reading on the buffered pipe) or when the pipe/device is torn down.
257 */
258void vusbUrbCompletionReadAhead(PVUSBURB pUrb)
259{
260 Assert(pUrb);
261 Assert(pUrb->Hci.pNext);
262 PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pUrb->Hci.pNext;
263 PVUSBPIPE pPipe = pArgs->pPipe;
264 Assert(pPipe);
265
266 pUrb->Hci.pNext = NULL; // @todo: use a more suitable field
267 if (pPipe->pBuffUrbHead == NULL)
268 {
269 // The queue is empty, this is easy
270 Assert(!pPipe->pBuffUrbTail);
271 pPipe->pBuffUrbTail = pPipe->pBuffUrbHead = pUrb;
272 }
273 else
274 {
275 // Some URBs are queued already
276 Assert(pPipe->pBuffUrbTail);
277 Assert(!pPipe->pBuffUrbTail->Hci.pNext);
278 pPipe->pBuffUrbTail = pPipe->pBuffUrbTail->Hci.pNext = pUrb;
279 }
280 --pPipe->cSubmitted;
281 ++pPipe->cBuffered;
282}
283
284/**
285 * Process a submit of an input URB on a pipe with read-ahead buffering. Instead
286 * of passing the URB to the proxy, we use previously read data stored in the
287 * read-ahead buffer, immediately complete the input URB and free the buffered URB.
288 *
289 * @param pUrb The URB submitted by HC
290 * @param pPipe The pipe with read-ahead buffering
291 *
292 * @return int Status code
293 */
294int vusbUrbSubmitBufferedRead(PVUSBURB pUrb, PVUSBPIPE pPipe)
295{
296 PVUSBURB pBufferedUrb;
297 Assert(pUrb && pPipe);
298
299 pBufferedUrb = pPipe->pBuffUrbHead;
300 if (pBufferedUrb)
301 {
302 unsigned cbTotal;
303
304 // There's a URB available in the read-ahead buffer; use it
305 pPipe->pBuffUrbHead = pBufferedUrb->Hci.pNext;
306 if (pPipe->pBuffUrbHead == NULL)
307 pPipe->pBuffUrbTail = NULL;
308
309 --pPipe->cBuffered;
310
311 // Make sure the buffered URB is what we expect
312 Assert(pUrb->enmType == pBufferedUrb->enmType);
313 Assert(pUrb->EndPt == pBufferedUrb->EndPt);
314 Assert(pUrb->enmDir == pBufferedUrb->enmDir);
315
316 pUrb->enmState = VUSBURBSTATE_REAPED;
317 pUrb->enmStatus = pBufferedUrb->enmStatus;
318 cbTotal = 0;
319 // Copy status and data received from the device
320 for (unsigned i = 0; i < pUrb->cIsocPkts; ++i)
321 {
322 unsigned off, len;
323
324 off = pBufferedUrb->aIsocPkts[i].off;
325 len = pBufferedUrb->aIsocPkts[i].cb;
326 pUrb->aIsocPkts[i].cb = len;
327 pUrb->aIsocPkts[i].off = off;
328 pUrb->aIsocPkts[i].enmStatus = pBufferedUrb->aIsocPkts[i].enmStatus;
329 cbTotal += len;
330 Assert(pUrb->VUsb.cbDataAllocated >= cbTotal);
331 memcpy(&pUrb->abData[off], &pBufferedUrb->abData[off], len);
332 }
333 // Give back the data to the HC right away and then free the buffered URB
334 vusbUrbCompletionRh(pUrb);
335 // This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
336 // Assert(pUrb->enmState == VUSBURBSTATE_FREE);
337 Assert(pBufferedUrb->enmState == VUSBURBSTATE_REAPED);
338 LogFlow(("%s: vusbUrbSubmitBufferedRead: Freeing buffered URB\n", pBufferedUrb->pszDesc));
339 pBufferedUrb->VUsb.pfnFree(pBufferedUrb);
340 // This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
341 // Assert(pBufferedUrb->enmState == VUSBURBSTATE_FREE);
342 }
343 else
344 {
345 // No URB on hand. Either we exhausted the buffer (shouldn't happen!) or the guest simply
346 // asked for data too soon. Pretend that the device didn't deliver any data.
347 pUrb->enmState = VUSBURBSTATE_REAPED;
348 pUrb->enmStatus = VUSBSTATUS_DATA_UNDERRUN;
349 for (unsigned i = 0; i < pUrb->cIsocPkts; ++i)
350 {
351 pUrb->aIsocPkts[i].cb = 0;
352 pUrb->aIsocPkts[i].enmStatus = VUSBSTATUS_NOT_ACCESSED;
353 }
354 vusbUrbCompletionRh(pUrb);
355 // This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
356 // Assert(pUrb->enmState == VUSBURBSTATE_FREE);
357 LogFlow(("%s: vusbUrbSubmitBufferedRead: No buffered URB available!\n", pBufferedUrb->pszDesc));
358 }
359 return VINF_SUCCESS;
360}
361
362/* Read-ahead start/stop functions, used primarily to keep the PVUSBREADAHEADARGS struct private to this module. */
363
364void vusbReadAheadStart(PVUSBDEV pDev, PVUSBPIPE pPipe)
365{
366 int rc;
367 PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)RTMemTmpAlloc(sizeof(*pArgs));
368
369 if (pArgs)
370 {
371 PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
372 pArgs->pDev = pDev;
373 pArgs->pPipe = pPipe;
374 pArgs->fTerminate = false;
375 pArgs->fHighSpeed = pRh && ((pRh->fHcVersions & VUSB_STDVER_20) != 0);
376 if (pArgs->fHighSpeed)
377 rc = RTThreadCreate(&pPipe->ReadAheadThread, vusbDevReadAheadThread, pArgs, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "USBISOC");
378 else
379 rc = VERR_VUSB_DEVICE_NOT_ATTACHED; // No buffering for low/full-speed devices at the moment, needs testing.
380 if (RT_SUCCESS(rc))
381 {
382 Log(("vusb: created isochronous read-ahead thread\n"));
383 }
384 else
385 {
386 Log(("vusb: isochronous read-ahead thread creation failed, rc=%d\n", rc));
387 pPipe->ReadAheadThread = NIL_RTTHREAD;
388 RTMemTmpFree(pArgs);
389 }
390 }
391 /* If thread creation failed for any reason, simply fall back to standard processing. */
392}
393
394void vusbReadAheadStop(void *pvReadAheadArgs)
395{
396 PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pvReadAheadArgs;
397 Log(("vusb: terminating read-ahead thread for endpoint\n"));
398 pArgs->fTerminate = true;
399}
400
401/*
402 * Local Variables:
403 * mode: c
404 * c-file-style: "bsd"
405 * c-basic-offset: 4
406 * tab-width: 4
407 * indent-tabs-mode: s
408 * End:
409 */
410
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette