diff mbox

[04/12] char: introduce backend tx queue

Message ID 1312208590-25502-5-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori Aug. 1, 2011, 2:23 p.m. UTC
While the front tx queue has no flow control, the backend tx queue uses a
polling function to determine when the front end can receive data.

To convert this to the new queue model, we simply try to flush the backend tx
queue whenever we poll.  We then return the remaining space in the queue as
the value of the polling function.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 qemu-char.c |   49 ++++++++++++++++++++++++++++++++++++++++---------
 qemu-char.h |    3 ++-
 2 files changed, 42 insertions(+), 10 deletions(-)

Comments

Stefan Hajnoczi Aug. 1, 2011, 3:33 p.m. UTC | #1
On Mon, Aug 1, 2011 at 3:23 PM, Anthony Liguori <aliguori@us.ibm.com> wrote:
> While the front tx queue has no flow control, the backend tx queue uses a
> polling function to determine when the front end can receive data.
>
> To convert this to the new queue model, we simply try to flush the backend tx
> queue whenever we poll.  We then return the remaining space in the queue as
> the value of the polling function.
>
> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
> ---
>  qemu-char.c |   49 ++++++++++++++++++++++++++++++++++++++++---------
>  qemu-char.h |    3 ++-
>  2 files changed, 42 insertions(+), 10 deletions(-)
>
> diff --git a/qemu-char.c b/qemu-char.c
> index 3f9b32c..2746652 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -173,6 +173,11 @@ static size_t char_queue_read(CharQueue *q, void *data, size_t size)
>     return i;
>  }
>
> +static uint32_t char_queue_get_avail(CharQueue *q)
> +{
> +    return sizeof(q->ring) - (q->prod - q->cons);
> +}

"avail" confuses me.  How many bytes are available for the consumer?
How many bytes are available for the producer?

How about char_queue_get_space() or char_queue_get_nfree()?

Stefan
Anthony Liguori Aug. 1, 2011, 3:37 p.m. UTC | #2
On 08/01/2011 10:33 AM, Stefan Hajnoczi wrote:
> On Mon, Aug 1, 2011 at 3:23 PM, Anthony Liguori<aliguori@us.ibm.com>  wrote:
>> While the front tx queue has no flow control, the backend tx queue uses a
>> polling function to determine when the front end can receive data.
>>
>> To convert this to the new queue model, we simply try to flush the backend tx
>> queue whenever we poll.  We then return the remaining space in the queue as
>> the value of the polling function.
>>
>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>> ---
>>   qemu-char.c |   49 ++++++++++++++++++++++++++++++++++++++++---------
>>   qemu-char.h |    3 ++-
>>   2 files changed, 42 insertions(+), 10 deletions(-)
>>
>> diff --git a/qemu-char.c b/qemu-char.c
>> index 3f9b32c..2746652 100644
>> --- a/qemu-char.c
>> +++ b/qemu-char.c
>> @@ -173,6 +173,11 @@ static size_t char_queue_read(CharQueue *q, void *data, size_t size)
>>      return i;
>>   }
>>
>> +static uint32_t char_queue_get_avail(CharQueue *q)
>> +{
>> +    return sizeof(q->ring) - (q->prod - q->cons);
>> +}
>
> "avail" confuses me.  How many bytes are available for the consumer?
> How many bytes are available for the producer?
>
> How about char_queue_get_space() or char_queue_get_nfree()?

Sure.

Regards,

Anthony Liguori

>
> Stefan
>
diff mbox

Patch

diff --git a/qemu-char.c b/qemu-char.c
index 3f9b32c..2746652 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -173,6 +173,11 @@  static size_t char_queue_read(CharQueue *q, void *data, size_t size)
     return i;
 }
 
+static uint32_t char_queue_get_avail(CharQueue *q)
+{
+    return sizeof(q->ring) - (q->prod - q->cons);
+}
+
 static void qemu_chr_flush_fe_tx(CharDriverState *s)
 {
     uint8_t buf[MAX_CHAR_QUEUE_RING];
@@ -200,23 +205,49 @@  int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
     return ret;
 }
 
-int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
+static void qemu_chr_flush_be_tx(CharDriverState *s)
 {
-    if (!s->chr_ioctl)
-        return -ENOTSUP;
-    return s->chr_ioctl(s, cmd, arg);
+    uint8_t buf[MAX_CHAR_QUEUE_RING];
+    int len;
+
+    /* Only drain what the be can handle */
+    len = s->chr_can_read(s->handler_opaque);
+    if (len == 0) {
+        return;
+    }
+
+    len = char_queue_read(&s->be_tx, buf, len);
+
+    /* We only drained what we knew the be could handle so we don't need to
+     * requeue any data. */
+    s->chr_read(s, buf, len);
 }
 
 int qemu_chr_be_can_write(CharDriverState *s)
 {
-    if (!s->chr_can_read)
-        return 0;
-    return s->chr_can_read(s->handler_opaque);
+    /* Try to flush any queued data before returning how much data we can
+     * accept. */
+    qemu_chr_flush_be_tx(s);
+
+    return char_queue_get_avail(&s->be_tx);
 }
 
-void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
+int qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
 {
-    s->chr_read(s->handler_opaque, buf, len);
+    int ret;
+
+    ret = char_queue_write(&s->be_tx, buf, len);
+
+    qemu_chr_flush_be_tx(s);
+
+    return ret;
+}
+
+int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
+{
+    if (!s->chr_ioctl)
+        return -ENOTSUP;
+    return s->chr_ioctl(s, cmd, arg);
 }
 
 int qemu_chr_get_msgfd(CharDriverState *s)
diff --git a/qemu-char.h b/qemu-char.h
index bb9c1a7..85735b5 100644
--- a/qemu-char.h
+++ b/qemu-char.h
@@ -85,6 +85,7 @@  struct CharDriverState {
     int avail_connections;
 
     CharQueue fe_tx;
+    CharQueue be_tx;
 
     QTAILQ_ENTRY(CharDriverState) next;
 };
@@ -109,7 +110,7 @@  void qemu_chr_add_handlers(CharDriverState *s,
 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg);
 void qemu_chr_generic_open(CharDriverState *s);
 int qemu_chr_be_can_write(CharDriverState *s);
-void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len);
+int qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len);
 int qemu_chr_get_msgfd(CharDriverState *s);
 void qemu_chr_accept_input(CharDriverState *s);
 int qemu_chr_add_client(CharDriverState *s, int fd);