Hi Marc-André Lureau

> Subject: Re: [PATCH v7 7/8] hw/usb/redirect-server: Stream interrupt IN
> endpoints
> 
> > Interrupt IN is not a request and an answer in usbredir. The host asks
> > once with start_interrupt_receiving. After that it expects a packet
> > every time the device has data.
> >
> > Park one IN packet on the device for each streaming endpoint. A device
> > that answers USB_RET_ASYNC holds the packet and completes it when it
> > has data. A device that answers USB_RET_NAK gives the packet back at
> > once, so park a new one when the bus reports a wakeup on that endpoint.
> > A slow timer parks one too, for a device that does not report a wakeup.
> > A streamed request does not wait on the endpoint's list the way a host
> > request does, because this timer already asks again.
> >
> > Wait for the endpoint's bInterval before parking the next one. Some
> > devices answer every time. Without the wait we would ask as fast as
> > the main loop runs, and the guest would get no time.
> >
> > On an error other than a stall the stream stays up, but back off to
> > the slow timer. At a high speed bInterval of 125us an endpoint that
> > keeps failing would otherwise send thousands of status messages a second.
> >
> > Give every endpoint its own timer and bottom half. We ask each
> > endpoint at its own rate. Drop the timer when the bottom half has
> > already asked, so it does not fire for nothing.
> >
> > Signed-off-by: Jamin Lin <[email protected]>
> > Message-ID: <[email protected]>
> > ---
> >  hw/usb/redirect-server.c | 298
> ++++++++++++++++++++++++++++++++++++++-
> >  hw/usb/trace-events      |   4 +
> >  2 files changed, 298 insertions(+), 4 deletions(-)
> >
> > diff --git a/hw/usb/redirect-server.c b/hw/usb/redirect-server.c index
> > 7eeb08ecd7f1..fecb7119ddd4 100644
> > --- a/hw/usb/redirect-server.c
> > +++ b/hw/usb/redirect-server.c
> > @@ -96,9 +96,13 @@ OBJECT_DECLARE_SIMPLE_TYPE(USBRedirServer,
> USB_REDIR_SERVER)
> >   */
> >  #define USBREDIR_SERVER_MAX_BULK (1 * MiB)
> >
> > +/* Buffer size for an interrupt IN endpoint before its descriptor is
> > +seen. */ #define USBREDIR_SERVER_INTR_STREAM_DEFAULT_LEN 64
> > +
> >  #define USBREDIR_SERVER_CTRL 0
> >  #define USBREDIR_SERVER_BULK 1
> >  #define USBREDIR_SERVER_INTERRUPT 2
> > +#define USBREDIR_SERVER_INTR_STREAM 3
> >
> >  /* Which message answers the host when a control transfer ends. */
> > typedef enum { @@ -156,6 +160,15 @@ typedef struct USBRedirServerEp {
> >      /* Asks again for a device that does not wake the endpoint. */
> >      QEMUTimer *retry;
> >
> > +    /*
> > +     * Interrupt IN streaming. The timer asks the device again at the
> > +     * endpoint's own rate, and more slowly after a NAK or an error.
> > +     * The bottom half asks at once when the device says it has data.
> > +     */
> > +    QEMUTimer *stream_timer;
> > +    QEMUBH *stream_bh;
> > +    bool streaming;
> > +
> >      uint16_t max_packet;
> >      uint8_t interval;
> >      uint8_t iface;
> > @@ -616,6 +629,97 @@ static void
> usbredir_server_interrupt_complete(USBRedirServer *s,
> >                                      rp->data, len);  }
> >
> > +/*
> > + * How long to wait before asking the device again. bInterval means
> > + * different things at different speeds: a number of frames at low
> > +and
> > + * full speed, an exponent at high speed.
> > + */
> > +static int64_t usbredir_server_intr_stream_period_ns(USBRedirServer *s,
> > +                                                     int ep_nr)
> {
> > +    int idx = usbredir_server_ep_index(ep_nr | USB_DIR_IN);
> > +    USBDevice *device = usbredir_server_device(s);
> > +    uint8_t interval = s->ep[idx].interval;
> > +
> > +    if (interval == 0) {
> > +        interval = 1;
> > +    }
> > +
> > +    if (device && device->speed == USB_SPEED_HIGH) {
> > +        interval = MIN(interval, 16);
> > +        return (1LL << (interval - 1)) * 125 * SCALE_US;
> > +    }
> > +
> > +    return (int64_t)interval * SCALE_MS; }
> > +
> > +/*
> > + * The device answered a streamed request. Send the answer to the
> > +host, then
> > + * ask again, because the host still wants more. Do not ask from
> > +here: the
> > + * device may answer at once, and this function would call itself
> > +over and
> > + * over. Arm the timer instead. It also waits for the endpoint's bInterval.
> > + */
> > +static void usbredir_server_intr_stream_complete(USBRedirServer *s,
> > +
> USBRedirServerPkt
> > +*rp) {
> > +    struct usb_redir_interrupt_packet_header resp = rp->intr_hdr;
> > +    struct usb_redir_interrupt_receiving_status_header st;
> > +    int idx = usbredir_server_ep_index(resp.endpoint);
> > +    int ep_nr = resp.endpoint & 0x0f;
> > +    USBRedirServerEp *ep = &s->ep[idx];
> > +    USBPacket *p = &rp->pkt;
> > +    int actual = p->actual_length;
> > +    bool retry = false;
> > +    int64_t delay_ns;
> > +
> > +    trace_usbredir_server_intr_stream_complete(ep_nr, p->status,
> > + actual);
> > +
> > +    switch (p->status) {
> > +    case USB_RET_SUCCESS:
> > +        resp.status = usb_redir_success;
> > +        resp.length = actual;
> > +        usbredirparser_send_interrupt_packet(s->parser, 0, &resp,
> > +                                             actual ? rp->data :
> NULL,
> > +                                             actual);
> > +        usbredirparser_do_write(s->parser);
> > +        break;
> > +    case USB_RET_NAK:
> > +        /* no data yet; wait, unless the device wakes us first */
> > +        retry = true;
> > +        break;
> > +    default:
> > +        /*
> > +         * The endpoint failed. There is no data, so send a status
> > +         * message instead. The host gives it to its guest.
> > +         *
> > +         * redirect.c ends the stream only on a stall, so do the same
> > +         * here. If we end it on any other error, the host still
> > +         * thinks the stream runs, and it never asks again.
> > +         */
> > +        st.endpoint = resp.endpoint;
> > +        st.status = usbredir_server_status(p->status);
> > +        usbredirparser_send_interrupt_receiving_status(s->parser, 0, &st);
> > +        usbredirparser_do_write(s->parser);
> > +        if (p->status == USB_RET_STALL) {
> > +            ep->streaming = false;
> > +        } else {
> > +            /* the stream stays up, but do not ask at the endpoint's rate
> */
> > +            retry = true;
> > +        }
> > +        break;
> > +    }
> > +
> > +    if (ep->streaming) {
> > +        if (retry) {
> > +            delay_ns = (int64_t)USBREDIR_SERVER_RETRY_MS *
> SCALE_MS;
> > +        } else {
> > +            delay_ns = usbredir_server_intr_stream_period_ns(s, ep_nr);
> > +        }
> > +        timer_mod(ep->stream_timer,
> > +                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
> delay_ns);
> > +    }
> > +}
> > +
> >  /*
> >   * USB port ops
> >   */
> > @@ -688,6 +792,9 @@ static void
> usbredir_server_packet_complete(USBPort *port, USBPacket *p)
> >      case USBREDIR_SERVER_INTERRUPT:
> >          usbredir_server_interrupt_complete(s, rp);
> >          break;
> > +    case USBREDIR_SERVER_INTR_STREAM:
> > +        usbredir_server_intr_stream_complete(s, rp);
> > +        break;
> >      default:
> >          g_assert_not_reached();
> >      }
> > @@ -727,9 +834,11 @@ static USBRedirServerAsk
> > usbredir_server_ask_device(USBRedirServer *s,
> >
> >      /*
> >       * Keep the request and send it again when the device wakes the
> > -     * endpoint.
> > +     * endpoint. A streamed request asks again by itself, with its own
> > +     * timer.
> >       */
> > -    if (rp->pkt.status == USB_RET_NAK) {
> > +    if (rp->pkt.status == USB_RET_NAK &&
> > +        rp->type != USBREDIR_SERVER_INTR_STREAM) {
> >          trace_usbredir_server_nak_park(rp->redir_id, rp->ep_idx);
> >          QTAILQ_INSERT_HEAD(&ep->waiting, rp, waiting_link);
> >          timer_mod(ep->retry,
> qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
> > @@ -775,8 +884,18 @@ static void usbredir_server_wakeup_ep(USBBus
> > *bus, USBEndpoint *ep,  {
> >      USBRedirServer *s = container_of(bus, USBRedirServer, bus);
> >      uint8_t addr = ep->nr | (ep->pid == USB_TOKEN_IN ? USB_DIR_IN :
> > 0);
> > +    int idx = usbredir_server_ep_index(addr);
> >
> > -    usbredir_server_kick_ep(s, usbredir_server_ep_index(addr));
> > +    /*
> > +     * The device has data. A streaming interrupt IN endpoint may have
> > +     * answered NAK. Ask now instead of waiting for the retry timer.
> > +     */
> > +    if (s->ep[idx].streaming) {
> > +        qemu_bh_schedule(s->ep[idx].stream_bh);
> > +    }
> > +
> > +    /* The host may also have a request stopped on this endpoint. */
> > +    usbredir_server_kick_ep(s, idx);
> >  }
> >
> >  static USBBusOps usbredir_server_bus_ops = { @@ -830,6 +949,95 @@
> > static void usbredir_server_pkt_free(USBRedirServer *s, USBRedirServerPkt
> *rp)
> >      g_free(rp);
> >  }
> >
> > +/*
> > + * Interrupt IN streaming
> > + */
> > +
> > +static bool usbredir_server_intr_stream_inflight(USBRedirServer *s,
> > +int ep_nr) {
> > +    USBRedirServerPkt *rp;
> > +
> > +    QTAILQ_FOREACH(rp, &s->inflight, inflight_link) {
> > +        if (rp->type == USBREDIR_SERVER_INTR_STREAM &&
> > +            (rp->intr_hdr.endpoint & 0x0f) == ep_nr) {
> > +            return true;
> > +        }
> > +    }
> > +    return false;
> > +}
> > +
> > +/*
> > + * Ask the device for data on @ep_nr and leave the request waiting. A
> > +device
> > + * sends data only when asked. Do nothing if a request on @ep_nr is
> > +still
> > + * unanswered.
> > + */
> > +static void usbredir_server_intr_stream_park(USBRedirServer *s, int
> > +ep_nr) {
> > +    USBDevice *device = usbredir_server_device(s);
> > +    int idx = usbredir_server_ep_index(ep_nr | USB_DIR_IN);
> > +    USBRedirServerPkt *rp;
> > +    USBEndpoint *ep;
> > +    int len;
> > +
> > +    /* No device to ask. */
> > +    if (!device || !device->attached) {
> > +        return;
> > +    }
> > +
> > +    /* The host did not ask for this endpoint, or a request is unanswered.
> */
> > +    if (!s->ep[idx].streaming ||
> > +        usbredir_server_intr_stream_inflight(s, ep_nr)) {
> > +        return;
> > +    }
> > +
> > +    len = s->ep[idx].max_packet;
> > +    if (len == 0) {
> > +        len = USBREDIR_SERVER_INTR_STREAM_DEFAULT_LEN;
> > +    }
> > +    ep = usb_ep_get(device, USB_TOKEN_IN, ep_nr);
> > +    rp = usbredir_server_pkt_alloc(len, idx);
> > +    rp->type = USBREDIR_SERVER_INTR_STREAM;
> > +    /* streamed data carries no host id */
> > +    rp->redir_id = 0;
> 
> This puts a packet with id 0 on s->inflight, but
> usbredir_server_cancel_data_packet() matches any inflight request by id.
> 
> A client cancel for a packet id == 0 can therefore hit this packet, send a 
> bogus
> cancelled interrupt packet. Skip USBREDIR_SERVER_INTR_STREAM in the
> cancel lookup?
> 


Thanks for the review and suggestion.

The streaming packet is used internally by the redirect server to communicate 
with its USB device, so it should not be cancelled by a client.
If the client cancels a packet with ID0, the streaming packet could be 
cancelled unexpectedly.
Will skip streaming packets in usbredir_server_cancel_data_packet.

static void usbredir_server_cancel_data_packet(void *priv, uint64_t id)
     QTAILQ_FOREACH(rp, &s->inflight, inflight_link) {
       
+        if (rp->type == USBREDIR_SERVER_INTR_STREAM) {
+            continue;
+        }
+
         if (rp->redir_id == id) {
             idx = rp->ep_idx; > 

Thanks,
Jamin
--> Marc-AndrĂ© Lureau <[email protected]>

Reply via email to