Received: from eggs.gnu.org ([2001:4830:134:3::10]:46872) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abCbB-0002NI-Kp for qemu-devel@nongnu.org; Wed, 02 Mar 2016 14:38:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1abCb8-0002hf-AA for qemu-devel@nongnu.org; Wed, 02 Mar 2016 14:38:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52252) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abCb8-0002ha-20 for qemu-devel@nongnu.org; Wed, 02 Mar 2016 14:38:14 -0500 From: Markus Armbruster References: <1456771254-17511-1-git-send-email-armbru@redhat.com> <1456771254-17511-28-git-send-email-armbru@redhat.com> Date: Wed, 02 Mar 2016 20:38:11 +0100 In-Reply-To: (=?utf-8?Q?=22Marc-Andr=C3=A9?= Lureau"'s message of "Wed, 2 Mar 2016 19:41:49 +0100") Message-ID: <87bn6wd61o.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 27/38] ivshmem: Simplify how we cope with short reads from server List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Cc: Paolo Bonzini , cam , Claudio Fontana , QEMU , David Marchand Marc-Andr=C3=A9 Lureau writes: > Hi > > On Mon, Feb 29, 2016 at 7:40 PM, Markus Armbruster wr= ote: >> Short reads from a UNIX domain sockets are exceedingly unlikely when >> the other side always sends eight bytes and we always read eight >> bytes. We cope with them anyway. However, the code doing that is >> rather convoluted. Dumb it down radically. >> >> Replace the convoluted code > > agreed > >> >> Signed-off-by: Markus Armbruster >> --- >> hw/misc/ivshmem.c | 76 ++++++++++++------------------------------------= ------- >> 1 file changed, 16 insertions(+), 60 deletions(-) >> >> diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c >> index e578b8a..fb8a4f7 100644 >> --- a/hw/misc/ivshmem.c >> +++ b/hw/misc/ivshmem.c >> @@ -26,7 +26,6 @@ >> #include "migration/migration.h" >> #include "qemu/error-report.h" >> #include "qemu/event_notifier.h" >> -#include "qemu/fifo8.h" >> #include "sysemu/char.h" >> #include "sysemu/hostmem.h" >> #include "qapi/visitor.h" >> @@ -80,7 +79,6 @@ typedef struct IVShmemState { >> uint32_t intrstatus; >> >> CharDriverState *server_chr; >> - Fifo8 incoming_fifo; >> MemoryRegion ivshmem_mmio; >> >> /* We might need to register the BAR before we actually have the me= mory. >> @@ -99,6 +97,8 @@ typedef struct IVShmemState { >> uint32_t vectors; >> uint32_t features; >> MSIVector *msi_vectors; >> + uint64_t msg_buf; /* buffer for receiving server messages= */ >> + int msg_buffered_bytes; /* #bytes in @msg_buf */ >> >> Error *migration_blocker; >> >> @@ -255,11 +255,6 @@ static const MemoryRegionOps ivshmem_mmio_ops =3D { >> }, >> }; >> >> -static int ivshmem_can_receive(void * opaque) >> -{ >> - return sizeof(int64_t); >> -} >> - >> static void ivshmem_vector_notify(void *opaque) >> { >> MSIVector *entry =3D opaque; >> @@ -459,53 +454,6 @@ static void resize_peers(IVShmemState *s, int nb_pe= ers) >> } >> } >> >> -static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, in= t size, >> - void *data, size_t len) >> -{ >> - const uint8_t *p; >> - uint32_t num; >> - >> - assert(len <=3D sizeof(int64_t)); /* limitation of the fifo */ >> - if (fifo8_is_empty(&s->incoming_fifo) && size =3D=3D len) { >> - memcpy(data, buf, size); >> - return true; >> - } >> - >> - IVSHMEM_DPRINTF("short read of %d bytes\n", size); >> - >> - num =3D MIN(size, sizeof(int64_t) - fifo8_num_used(&s->incoming_fif= o)); >> - fifo8_push_all(&s->incoming_fifo, buf, num); >> - >> - if (fifo8_num_used(&s->incoming_fifo) < len) { >> - assert(num =3D=3D 0); >> - return false; >> - } >> - >> - size -=3D num; >> - buf +=3D num; >> - p =3D fifo8_pop_buf(&s->incoming_fifo, len, &num); >> - assert(num =3D=3D len); >> - >> - memcpy(data, p, len); >> - >> - if (size > 0) { >> - fifo8_push_all(&s->incoming_fifo, buf, size); >> - } >> - >> - return true; >> -} >> - >> -static bool fifo_update_and_get_i64(IVShmemState *s, >> - const uint8_t *buf, int size, int64= _t *i64) >> -{ >> - if (fifo_update_and_get(s, buf, size, i64, sizeof(*i64))) { >> - *i64 =3D GINT64_FROM_LE(*i64); >> - return true; >> - } >> - >> - return false; >> -} >> - >> static void ivshmem_add_kvm_msi_virq(IVShmemState *s, int vector, >> Error **errp) >> { >> @@ -658,6 +606,14 @@ static void process_msg(IVShmemState *s, int64_t ms= g, int fd, Error **errp) >> } >> } >> >> +static int ivshmem_can_receive(void *opaque) >> +{ >> + IVShmemState *s =3D opaque; >> + >> + assert(s->msg_buffered_bytes < sizeof(s->msg_buf)); >> + return sizeof(s->msg_buf) - s->msg_buffered_bytes; >> +} >> + >> static void ivshmem_read(void *opaque, const uint8_t *buf, int size) >> { >> IVShmemState *s =3D opaque; >> @@ -665,8 +621,12 @@ static void ivshmem_read(void *opaque, const uint8_= t *buf, int size) >> int incoming_fd; >> int64_t incoming_posn; >> >> - if (!fifo_update_and_get_i64(s, buf, size, &incoming_posn)) { >> - return; >> + assert(size >=3D 0 && s->msg_buffered_bytes + size <=3D sizeof(s->m= sg_buf)); >> + memcpy((unsigned char *)&s->msg_buf + s->msg_buffered_bytes, buf, s= ize); >> + s->msg_buffered_bytes +=3D size; >> + if (s->msg_buffered_bytes =3D=3D sizeof(s->msg_buf)) { >> + incoming_posn =3D le64_to_cpu(s->msg_buf); >> + s->msg_buffered_bytes =3D 0; >> } >> > > missing "else return" though. Indeed. Glad you caught my screwup. >> incoming_fd =3D qemu_chr_fe_get_msgfd(s->server_chr); >> @@ -1019,8 +979,6 @@ static void pci_ivshmem_realize(PCIDevice *dev, Err= or **errp) >> } >> } >> >> - fifo8_create(&s->incoming_fifo, sizeof(int64_t)); >> - >> if (s->role_val =3D=3D IVSHMEM_PEER) { >> error_setg(&s->migration_blocker, >> "Migration is disabled when using feature 'peer mode= ' in device 'ivshmem'"); >> @@ -1033,8 +991,6 @@ static void pci_ivshmem_exit(PCIDevice *dev) >> IVShmemState *s =3D IVSHMEM(dev); >> int i; >> >> - fifo8_destroy(&s->incoming_fifo); >> - >> if (s->migration_blocker) { >> migrate_del_blocker(s->migration_blocker); >> error_free(s->migration_blocker); >> -- >> 2.4.3 >> >>