From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 166FAC54EE9 for ; Thu, 22 Sep 2022 19:54:55 +0000 (UTC) Received: from localhost ([::1]:39496 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1obSHe-0003qH-VH for qemu-devel@archiver.kernel.org; Thu, 22 Sep 2022 15:54:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1obSFw-0001W0-DZ for qemu-devel@nongnu.org; Thu, 22 Sep 2022 15:53:10 -0400 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]:33513) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1obSFu-000285-ED for qemu-devel@nongnu.org; Thu, 22 Sep 2022 15:53:08 -0400 Received: by mail-lj1-x235.google.com with SMTP id a10so12345200ljq.0 for ; Thu, 22 Sep 2022 12:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=ssyi/xZUxSH0lWcZWJwiKOutOPgLyCm5dN203B45ABM=; b=N0WUIpWvC+8lFPQhRWHMBYzTlvAMipqLnk6etpYMntj07Kf8dcQGvgCKvkNhK4LGuM osy8sdNioXElwPq405RO7AhKDkcjfvMf2yQoRwphEIbP6X6umDZDARfHaHdvsNpEy+gf jfI2FSwtVS0kfSy2Ek7i6wBDJgGch/+4Jck1Pzzz1To1Ni9jubtoAwQ/aTBO6oLS2XHG 2gQE9kPx+wOhq9ACsWgcauaQZy4DHgIOj9mV9wbNYiN0DE6VXKGY/On1o5ixpv7p/zwJ b63P9vXs/+t1mrcqMdXlEQHjrpYJIY8EuqstBKit7YSFg5huJC3yKxDM8n0kdY//vgt1 mZtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=ssyi/xZUxSH0lWcZWJwiKOutOPgLyCm5dN203B45ABM=; b=MqHaMfLRn0/Co4v3PLPd61R1vqbBryNchxOcFsWdeZSqYoL/iZzkaINDAAql3xZkGK tCBgUMuiKQl5CcDnzcnko08g1QznRDVqU8mT/nktIjh0zN6m6Oj1ajuLOvTK9eP8BwCN V3arq4P8mwv63xmkrimJKsrFLj6wxyQBSb0dKcWANdjW2kLBsM6rEHgeCu/1g03B++tf JwiFab1dvg6TBxjC1Un9NT3FXeigShiSRzWHjO25WKcCtzrcmn8uSjpIq3i4D2oNwO0P zeMizc8qr36CaQqPUxJkEPlOFJxjI6ymhweAe1cyXF5cSAZF8F0y70BK+EXmq33g/qKa wgPw== X-Gm-Message-State: ACrzQf0JKCytA65sA5xh7XJi3VMbQB0w294TZSDU25XXIjJeZabXixxP vLe1KjuU/pCjex+9HNi2rNDac+fCkdQu0E5TB5g= X-Google-Smtp-Source: AMsMyM7b84rjYfkaHjZ7I24IGg0styv/Yn4Emc8xB7FSS7JkRt1IdzVQXgdczDdegoMP2pfhNLirYk+Ai8r2Bz1IAUM= X-Received: by 2002:a2e:a44c:0:b0:26b:e70f:a026 with SMTP id v12-20020a2ea44c000000b0026be70fa026mr1664231ljn.94.1663876384361; Thu, 22 Sep 2022 12:53:04 -0700 (PDT) MIME-Version: 1.0 References: <20220920103159.1865256-1-bmeng.cn@gmail.com> <20220920103159.1865256-17-bmeng.cn@gmail.com> In-Reply-To: <20220920103159.1865256-17-bmeng.cn@gmail.com> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Thu, 22 Sep 2022 21:52:52 +0200 Message-ID: Subject: Re: [PATCH v2 16/39] tests/qtest: Use send/recv for socket communication To: Bin Meng Cc: qemu-devel@nongnu.org, Xuzhou Cheng , Bin Meng , =?UTF-8?Q?Daniel_P=2E_Berrang=C3=A9?= , Laurent Vivier , Paolo Bonzini , Thomas Huth Content-Type: multipart/alternative; boundary="000000000000d71e5b05e94968d2" Received-SPF: pass client-ip=2a00:1450:4864:20::235; envelope-from=marcandre.lureau@gmail.com; helo=mail-lj1-x235.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --000000000000d71e5b05e94968d2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Sep 20, 2022 at 2:23 PM Bin Meng wrote: > From: Xuzhou Cheng > > Socket communication in the libqtest and libqmp codes uses read() > and write() which work on any file descriptor on *nix, and sockets > in *nix are an example of a file descriptor. > > However sockets on Windows do not use *nix-style file descriptors, > so read() and write() cannot be used on sockets on Windows. > Switch over to use send() and recv() instead which work on both > Windows and *nix. > > Signed-off-by: Xuzhou Cheng > Signed-off-by: Bin Meng > Reviewed-by: Marc-Andr=C3=A9 Lureau > --- > > Changes in v2: > - Introduce qemu_send_full() and use it > > include/qemu/sockets.h | 2 ++ > tests/qtest/libqmp.c | 5 +++-- > tests/qtest/libqtest.c | 4 ++-- > util/osdep.c | 33 +++++++++++++++++++++++++++++++++ > 4 files changed, 40 insertions(+), 4 deletions(-) > > diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h > index 038faa157f..8ff7832eba 100644 > --- a/include/qemu/sockets.h > +++ b/include/qemu/sockets.h > @@ -15,6 +15,8 @@ int inet_aton(const char *cp, struct in_addr *ia); > bool fd_is_socket(int fd); > int qemu_socket(int domain, int type, int protocol); > int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen); > +ssize_t qemu_send_full(int s, const void *buf, size_t count) > + G_GNUC_WARN_UNUSED_RESULT; > int socket_set_cork(int fd, int v); > int socket_set_nodelay(int fd); > void qemu_socket_set_block(int fd); > diff --git a/tests/qtest/libqmp.c b/tests/qtest/libqmp.c > index ade26c15f0..2b08382e5d 100644 > --- a/tests/qtest/libqmp.c > +++ b/tests/qtest/libqmp.c > @@ -23,6 +23,7 @@ > #endif > > #include "qemu/cutils.h" > +#include "qemu/sockets.h" > #include "qapi/error.h" > #include "qapi/qmp/json-parser.h" > #include "qapi/qmp/qjson.h" > @@ -36,7 +37,7 @@ typedef struct { > > static void socket_send(int fd, const char *buf, size_t size) > { > - size_t res =3D qemu_write_full(fd, buf, size); > + ssize_t res =3D qemu_send_full(fd, buf, size); > > assert(res =3D=3D size); > } > @@ -69,7 +70,7 @@ QDict *qmp_fd_receive(int fd) > ssize_t len; > char c; > > - len =3D read(fd, &c, 1); > + len =3D recv(fd, &c, 1, 0); > if (len =3D=3D -1 && errno =3D=3D EINTR) { > continue; > } > diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c > index a0f28ddf8e..a25a8b47d0 100644 > --- a/tests/qtest/libqtest.c > +++ b/tests/qtest/libqtest.c > @@ -436,7 +436,7 @@ void qtest_quit(QTestState *s) > > static void socket_send(int fd, const char *buf, size_t size) > { > - size_t res =3D qemu_write_full(fd, buf, size); > + ssize_t res =3D qemu_send_full(fd, buf, size); > > assert(res =3D=3D size); > } > @@ -468,7 +468,7 @@ static GString > *qtest_client_socket_recv_line(QTestState *s) > ssize_t len; > char buffer[1024]; > > - len =3D read(s->fd, buffer, sizeof(buffer)); > + len =3D recv(s->fd, buffer, sizeof(buffer), 0); > if (len =3D=3D -1 && errno =3D=3D EINTR) { > continue; > } > diff --git a/util/osdep.c b/util/osdep.c > index 60fcbbaebe..0342e754e1 100644 > --- a/util/osdep.c > +++ b/util/osdep.c > @@ -502,6 +502,39 @@ int qemu_accept(int s, struct sockaddr *addr, > socklen_t *addrlen) > return ret; > } > > +/* > + * A variant of send(2) which handles partial send. > + * > + * Return the number of bytes transferred over the socket. > + * Set errno if fewer than `count' bytes are sent. > + * > + * This function don't work with non-blocking socket's. > + * Any of the possibilities with non-blocking socket's is bad: > + * - return a short write (then name is wrong) > + * - busy wait adding (errno =3D=3D EAGAIN) to the loop > + */ > +ssize_t qemu_send_full(int s, const void *buf, size_t count) > +{ > + ssize_t ret =3D 0; > + ssize_t total =3D 0; > + > + while (count) { > + ret =3D send(s, buf, count, 0); > + if (ret < 0) { > + if (errno =3D=3D EINTR) { > + continue; > + } > + break; > + } > + > + count -=3D ret; > + buf +=3D ret; > + total +=3D ret; > + } > + > + return total; > +} > + > void qemu_set_hw_version(const char *version) > { > hw_version =3D version; > -- > 2.34.1 > > > --=20 Marc-Andr=C3=A9 Lureau --000000000000d71e5b05e94968d2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Tue, Sep 20, 2022 at 2:23 PM Bin M= eng <bmeng.cn@gmail.com> wr= ote:
From: Xuzho= u Cheng <xuzhou.cheng@windriver.com>

Socket communication in the libqtest and libqmp codes uses read()
and write() which work on any file descriptor on *nix, and sockets
in *nix are an example of a file descriptor.

However sockets on Windows do not use *nix-style file descriptors,
so read() and write() cannot be used on sockets on Windows.
Switch over to use send() and recv() instead which work on both
Windows and *nix.

Signed-off-by: Xuzhou Cheng <xuzhou.cheng@windriver.com>
Signed-off-by: Bin Meng <bin.meng@windriver.com>

=
Reviewed-by: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com>
=C2=A0
---

Changes in v2:
- Introduce qemu_send_full() and use it

=C2=A0include/qemu/sockets.h |=C2=A0 2 ++
=C2=A0tests/qtest/libqmp.c=C2=A0 =C2=A0|=C2=A0 5 +++--
=C2=A0tests/qtest/libqtest.c |=C2=A0 4 ++--
=C2=A0util/osdep.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| 33 +++++++++++= ++++++++++++++++++++++
=C2=A04 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index 038faa157f..8ff7832eba 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -15,6 +15,8 @@ int inet_aton(const char *cp, struct in_addr *ia);
=C2=A0bool fd_is_socket(int fd);
=C2=A0int qemu_socket(int domain, int type, int protocol);
=C2=A0int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen); +ssize_t qemu_send_full(int s, const void *buf, size_t count)
+=C2=A0 =C2=A0 G_GNUC_WARN_UNUSED_RESULT;
=C2=A0int socket_set_cork(int fd, int v);
=C2=A0int socket_set_nodelay(int fd);
=C2=A0void qemu_socket_set_block(int fd);
diff --git a/tests/qtest/libqmp.c b/tests/qtest/libqmp.c
index ade26c15f0..2b08382e5d 100644
--- a/tests/qtest/libqmp.c
+++ b/tests/qtest/libqmp.c
@@ -23,6 +23,7 @@
=C2=A0#endif

=C2=A0#include "qemu/cutils.h"
+#include "qemu/sockets.h"
=C2=A0#include "qapi/error.h"
=C2=A0#include "qapi/qmp/json-parser.h"
=C2=A0#include "qapi/qmp/qjson.h"
@@ -36,7 +37,7 @@ typedef struct {

=C2=A0static void socket_send(int fd, const char *buf, size_t size)
=C2=A0{
-=C2=A0 =C2=A0 size_t res =3D qemu_write_full(fd, buf, size);
+=C2=A0 =C2=A0 ssize_t res =3D qemu_send_full(fd, buf, size);

=C2=A0 =C2=A0 =C2=A0assert(res =3D=3D size);
=C2=A0}
@@ -69,7 +70,7 @@ QDict *qmp_fd_receive(int fd)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ssize_t len;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0char c;

-=C2=A0 =C2=A0 =C2=A0 =C2=A0 len =3D read(fd, &c, 1);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 len =3D recv(fd, &c, 1, 0);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (len =3D=3D -1 && errno =3D=3D= EINTR) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0continue;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index a0f28ddf8e..a25a8b47d0 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -436,7 +436,7 @@ void qtest_quit(QTestState *s)

=C2=A0static void socket_send(int fd, const char *buf, size_t size)
=C2=A0{
-=C2=A0 =C2=A0 size_t res =3D qemu_write_full(fd, buf, size);
+=C2=A0 =C2=A0 ssize_t res =3D qemu_send_full(fd, buf, size);

=C2=A0 =C2=A0 =C2=A0assert(res =3D=3D size);
=C2=A0}
@@ -468,7 +468,7 @@ static GString *qtest_client_socket_recv_line(QTestStat= e *s)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ssize_t len;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0char buffer[1024];

-=C2=A0 =C2=A0 =C2=A0 =C2=A0 len =3D read(s->fd, buffer, sizeof(buffer))= ;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 len =3D recv(s->fd, buffer, sizeof(buffer),= 0);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (len =3D=3D -1 && errno =3D=3D= EINTR) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0continue;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
diff --git a/util/osdep.c b/util/osdep.c
index 60fcbbaebe..0342e754e1 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -502,6 +502,39 @@ int qemu_accept(int s, struct sockaddr *addr, socklen_= t *addrlen)
=C2=A0 =C2=A0 =C2=A0return ret;
=C2=A0}

+/*
+ * A variant of send(2) which handles partial send.
+ *
+ * Return the number of bytes transferred over the socket.
+ * Set errno if fewer than `count' bytes are sent.
+ *
+ * This function don't work with non-blocking socket's.
+ * Any of the possibilities with non-blocking socket's is bad:
+ *=C2=A0 =C2=A0- return a short write (then name is wrong)
+ *=C2=A0 =C2=A0- busy wait adding (errno =3D=3D EAGAIN) to the loop
+ */
+ssize_t qemu_send_full(int s, const void *buf, size_t count)
+{
+=C2=A0 =C2=A0 ssize_t ret =3D 0;
+=C2=A0 =C2=A0 ssize_t total =3D 0;
+
+=C2=A0 =C2=A0 while (count) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 ret =3D send(s, buf, count, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (ret < 0) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (errno =3D=3D EINTR) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 continue;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 count -=3D ret;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 buf +=3D ret;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 total +=3D ret;
+=C2=A0 =C2=A0 }
+
+=C2=A0 =C2=A0 return total;
+}
+
=C2=A0void qemu_set_hw_version(const char *version)
=C2=A0{
=C2=A0 =C2=A0 =C2=A0hw_version =3D version;
--
2.34.1




--
Marc-Andr=C3=A9 Lureau
--000000000000d71e5b05e94968d2--