From: Arseniy Krasnov <avkrasnov@salutedevices.com>
To: Stefano Garzarella <sgarzare@redhat.com>, <netdev@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>,
<virtualization@lists.linux-foundation.org>, <oxffffaa@gmail.com>,
Bobby Eshleman <bobby.eshleman@bytedance.com>
Subject: Re: [PATCH net-next 3/5] vsock/test: add send_buf() utility function
Date: Fri, 15 Sep 2023 22:25:28 +0300 [thread overview]
Message-ID: <ee1382e7-1e0e-a8f5-3703-11d28939df4f@salutedevices.com> (raw)
In-Reply-To: <20230915121452.87192-4-sgarzare@redhat.com>
On 15.09.2023 15:14, Stefano Garzarella wrote:
> Move the code of send_byte() out in a new utility function that
> can be used to send a generic buffer.
>
> This new function can be used when we need to send a custom
> buffer and not just a single 'A' byte.
>
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
> tools/testing/vsock/util.h | 2 +
> tools/testing/vsock/util.c | 90 +++++++++++++++++++++++---------------
> 2 files changed, 56 insertions(+), 36 deletions(-)
Reviewed-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
>
> diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h
> index fe31f267e67e..e5407677ce05 100644
> --- a/tools/testing/vsock/util.h
> +++ b/tools/testing/vsock/util.h
> @@ -42,6 +42,8 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
> int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
> struct sockaddr_vm *clientaddrp);
> void vsock_wait_remote_close(int fd);
> +void send_buf(int fd, const void *buf, size_t len, int flags,
> + ssize_t expected_ret);
> void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
> void send_byte(int fd, int expected_ret, int flags);
> void recv_byte(int fd, int expected_ret, int flags);
> diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c
> index 2826902706e8..6779d5008b27 100644
> --- a/tools/testing/vsock/util.c
> +++ b/tools/testing/vsock/util.c
> @@ -211,6 +211,59 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
> return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
> }
>
> +/* Transmit bytes from a buffer and check the return value.
> + *
> + * expected_ret:
> + * <0 Negative errno (for testing errors)
> + * 0 End-of-file
> + * >0 Success (bytes successfully written)
> + */
> +void send_buf(int fd, const void *buf, size_t len, int flags,
> + ssize_t expected_ret)
> +{
> + ssize_t nwritten = 0;
> + ssize_t ret;
> +
> + timeout_begin(TIMEOUT);
> + do {
> + ret = send(fd, buf + nwritten, len - nwritten, flags);
> + timeout_check("send");
> +
> + if (ret == 0 || (ret < 0 && errno != EINTR))
> + break;
> +
> + nwritten += ret;
> + } while (nwritten < len);
> + timeout_end();
> +
> + if (expected_ret < 0) {
> + if (ret != -1) {
> + fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
> + ret, expected_ret);
> + exit(EXIT_FAILURE);
> + }
> + if (errno != -expected_ret) {
> + perror("send");
> + exit(EXIT_FAILURE);
> + }
> + return;
> + }
> +
> + if (ret < 0) {
> + perror("send");
> + exit(EXIT_FAILURE);
> + }
> +
> + if (nwritten != expected_ret) {
> + if (ret == 0)
> + fprintf(stderr, "unexpected EOF while sending bytes\n");
> +
> + fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
> + nwritten, expected_ret);
> + exit(EXIT_FAILURE);
> + }
> +}
> +
> /* Receive bytes in a buffer and check the return value.
> *
> * expected_ret:
> @@ -273,43 +326,8 @@ void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
> void send_byte(int fd, int expected_ret, int flags)
> {
> const uint8_t byte = 'A';
> - ssize_t nwritten;
> -
> - timeout_begin(TIMEOUT);
> - do {
> - nwritten = send(fd, &byte, sizeof(byte), flags);
> - timeout_check("write");
> - } while (nwritten < 0 && errno == EINTR);
> - timeout_end();
> -
> - if (expected_ret < 0) {
> - if (nwritten != -1) {
> - fprintf(stderr, "bogus send(2) return value %zd\n",
> - nwritten);
> - exit(EXIT_FAILURE);
> - }
> - if (errno != -expected_ret) {
> - perror("write");
> - exit(EXIT_FAILURE);
> - }
> - return;
> - }
>
> - if (nwritten < 0) {
> - perror("write");
> - exit(EXIT_FAILURE);
> - }
> - if (nwritten == 0) {
> - if (expected_ret == 0)
> - return;
> -
> - fprintf(stderr, "unexpected EOF while sending byte\n");
> - exit(EXIT_FAILURE);
> - }
> - if (nwritten != sizeof(byte)) {
> - fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
> - exit(EXIT_FAILURE);
> - }
> + send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
> }
>
> /* Receive one byte and check the return value.
next prev parent reply other threads:[~2023-09-15 19:33 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-15 12:14 [PATCH net-next 0/5] vsock/test: add recv_buf()/send_buf() utility functions and some improvements Stefano Garzarella
2023-09-15 12:14 ` [PATCH net-next 1/5] vsock/test: add recv_buf() utility function Stefano Garzarella
2023-09-15 19:23 ` Arseniy Krasnov
2023-09-15 12:14 ` [PATCH net-next 2/5] vsock/test: use recv_buf() in vsock_test.c Stefano Garzarella
2023-09-15 19:24 ` Arseniy Krasnov
2023-09-15 12:14 ` [PATCH net-next 3/5] vsock/test: add send_buf() utility function Stefano Garzarella
2023-09-15 19:25 ` Arseniy Krasnov [this message]
2023-09-15 12:14 ` [PATCH net-next 4/5] vsock/test: use send_buf() in vsock_test.c Stefano Garzarella
2023-09-15 19:26 ` Arseniy Krasnov
2023-09-15 12:14 ` [PATCH net-next 5/5] vsock/test: track bytes in MSG_PEEK test for SOCK_SEQPACKET Stefano Garzarella
2023-09-15 19:29 ` Arseniy Krasnov
2023-09-15 19:30 ` [PATCH net-next 0/5] vsock/test: add recv_buf()/send_buf() utility functions and some improvements Arseniy Krasnov
2023-09-17 16:50 ` patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ee1382e7-1e0e-a8f5-3703-11d28939df4f@salutedevices.com \
--to=avkrasnov@salutedevices.com \
--cc=bobby.eshleman@bytedance.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oxffffaa@gmail.com \
--cc=sgarzare@redhat.com \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®