Skip to content

Commit aa9f6f3

Browse files
Konstantin Shkolnyygregkh
authored andcommitted
vsock/test: verify socket options after setting them
commit 86814d8 upstream. Replace setsockopt() calls with calls to functions that follow setsockopt() with getsockopt() and check that the returned value and its size are the same as have been set. (Except in vsock_perf.) Signed-off-by: Konstantin Shkolnyy <kshk@linux.ibm.com> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Paolo Abeni <pabeni@redhat.com> [Stefano: patch needed to avoid vsock test build failure reported by Johan Korsnes after backporting commit 0a98de8 ("vsock/test: fix seqpacket message bounds test") in 6.12-stable tree] Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Tested-by: Johan Korsnes <johan.korsnes@remarkable.no> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ceab30f commit aa9f6f3

9 files changed

Lines changed: 181 additions & 53 deletions

File tree

tools/testing/vsock/control.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "timeout.h"
2929
#include "control.h"
30+
#include "util.h"
3031

3132
static int control_fd = -1;
3233

@@ -50,7 +51,6 @@ void control_init(const char *control_host,
5051

5152
for (ai = result; ai; ai = ai->ai_next) {
5253
int fd;
53-
int val = 1;
5454

5555
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
5656
if (fd < 0)
@@ -65,11 +65,8 @@ void control_init(const char *control_host,
6565
break;
6666
}
6767

68-
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
69-
&val, sizeof(val)) < 0) {
70-
perror("setsockopt");
71-
exit(EXIT_FAILURE);
72-
}
68+
setsockopt_int_check(fd, SOL_SOCKET, SO_REUSEADDR, 1,
69+
"setsockopt SO_REUSEADDR");
7370

7471
if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
7572
goto next;

tools/testing/vsock/msg_zerocopy_common.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414

1515
#include "msg_zerocopy_common.h"
1616

17-
void enable_so_zerocopy(int fd)
18-
{
19-
int val = 1;
20-
21-
if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
22-
perror("setsockopt");
23-
exit(EXIT_FAILURE);
24-
}
25-
}
26-
2717
void vsock_recv_completion(int fd, const bool *zerocopied)
2818
{
2919
struct sock_extended_err *serr;

tools/testing/vsock/msg_zerocopy_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define VSOCK_RECVERR 1
1313
#endif
1414

15-
void enable_so_zerocopy(int fd);
1615
void vsock_recv_completion(int fd, const bool *zerocopied);
1716

1817
#endif /* MSG_ZEROCOPY_COMMON_H */

tools/testing/vsock/util.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,145 @@ void free_test_iovec(const struct iovec *test_iovec,
663663

664664
free(iovec);
665665
}
666+
667+
/* Set "unsigned long long" socket option and check that it's indeed set */
668+
void setsockopt_ull_check(int fd, int level, int optname,
669+
unsigned long long val, char const *errmsg)
670+
{
671+
unsigned long long chkval;
672+
socklen_t chklen;
673+
int err;
674+
675+
err = setsockopt(fd, level, optname, &val, sizeof(val));
676+
if (err) {
677+
fprintf(stderr, "setsockopt err: %s (%d)\n",
678+
strerror(errno), errno);
679+
goto fail;
680+
}
681+
682+
chkval = ~val; /* just make storage != val */
683+
chklen = sizeof(chkval);
684+
685+
err = getsockopt(fd, level, optname, &chkval, &chklen);
686+
if (err) {
687+
fprintf(stderr, "getsockopt err: %s (%d)\n",
688+
strerror(errno), errno);
689+
goto fail;
690+
}
691+
692+
if (chklen != sizeof(chkval)) {
693+
fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
694+
chklen);
695+
goto fail;
696+
}
697+
698+
if (chkval != val) {
699+
fprintf(stderr, "value mismatch: set %llu got %llu\n", val,
700+
chkval);
701+
goto fail;
702+
}
703+
return;
704+
fail:
705+
fprintf(stderr, "%s val %llu\n", errmsg, val);
706+
exit(EXIT_FAILURE);
707+
;
708+
}
709+
710+
/* Set "int" socket option and check that it's indeed set */
711+
void setsockopt_int_check(int fd, int level, int optname, int val,
712+
char const *errmsg)
713+
{
714+
int chkval;
715+
socklen_t chklen;
716+
int err;
717+
718+
err = setsockopt(fd, level, optname, &val, sizeof(val));
719+
if (err) {
720+
fprintf(stderr, "setsockopt err: %s (%d)\n",
721+
strerror(errno), errno);
722+
goto fail;
723+
}
724+
725+
chkval = ~val; /* just make storage != val */
726+
chklen = sizeof(chkval);
727+
728+
err = getsockopt(fd, level, optname, &chkval, &chklen);
729+
if (err) {
730+
fprintf(stderr, "getsockopt err: %s (%d)\n",
731+
strerror(errno), errno);
732+
goto fail;
733+
}
734+
735+
if (chklen != sizeof(chkval)) {
736+
fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
737+
chklen);
738+
goto fail;
739+
}
740+
741+
if (chkval != val) {
742+
fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval);
743+
goto fail;
744+
}
745+
return;
746+
fail:
747+
fprintf(stderr, "%s val %d\n", errmsg, val);
748+
exit(EXIT_FAILURE);
749+
}
750+
751+
static void mem_invert(unsigned char *mem, size_t size)
752+
{
753+
size_t i;
754+
755+
for (i = 0; i < size; i++)
756+
mem[i] = ~mem[i];
757+
}
758+
759+
/* Set "timeval" socket option and check that it's indeed set */
760+
void setsockopt_timeval_check(int fd, int level, int optname,
761+
struct timeval val, char const *errmsg)
762+
{
763+
struct timeval chkval;
764+
socklen_t chklen;
765+
int err;
766+
767+
err = setsockopt(fd, level, optname, &val, sizeof(val));
768+
if (err) {
769+
fprintf(stderr, "setsockopt err: %s (%d)\n",
770+
strerror(errno), errno);
771+
goto fail;
772+
}
773+
774+
/* just make storage != val */
775+
chkval = val;
776+
mem_invert((unsigned char *)&chkval, sizeof(chkval));
777+
chklen = sizeof(chkval);
778+
779+
err = getsockopt(fd, level, optname, &chkval, &chklen);
780+
if (err) {
781+
fprintf(stderr, "getsockopt err: %s (%d)\n",
782+
strerror(errno), errno);
783+
goto fail;
784+
}
785+
786+
if (chklen != sizeof(chkval)) {
787+
fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
788+
chklen);
789+
goto fail;
790+
}
791+
792+
if (memcmp(&chkval, &val, sizeof(val)) != 0) {
793+
fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n",
794+
val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec);
795+
goto fail;
796+
}
797+
return;
798+
fail:
799+
fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec);
800+
exit(EXIT_FAILURE);
801+
}
802+
803+
void enable_so_zerocopy_check(int fd)
804+
{
805+
setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
806+
"setsockopt SO_ZEROCOPY");
807+
}

tools/testing/vsock/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum);
6868
struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum);
6969
void free_test_iovec(const struct iovec *test_iovec,
7070
struct iovec *iovec, int iovnum);
71+
void setsockopt_ull_check(int fd, int level, int optname,
72+
unsigned long long val, char const *errmsg);
73+
void setsockopt_int_check(int fd, int level, int optname, int val,
74+
char const *errmsg);
75+
void setsockopt_timeval_check(int fd, int level, int optname,
76+
struct timeval val, char const *errmsg);
77+
void enable_so_zerocopy_check(int fd);
7178
#endif /* UTIL_H */

tools/testing/vsock/vsock_perf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ static void run_receiver(int rcvlowat_bytes)
251251
close(fd);
252252
}
253253

254+
static void enable_so_zerocopy(int fd)
255+
{
256+
int val = 1;
257+
258+
if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
259+
perror("setsockopt");
260+
exit(EXIT_FAILURE);
261+
}
262+
}
263+
254264
static void run_sender(int peer_cid, unsigned long to_send_bytes)
255265
{
256266
time_t tx_begin_ns;

tools/testing/vsock/vsock_test.c

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,13 @@ static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
455455

456456
sock_buf_size = SOCK_BUF_SIZE;
457457

458-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
459-
&sock_buf_size, sizeof(sock_buf_size))) {
460-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
461-
exit(EXIT_FAILURE);
462-
}
458+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
459+
sock_buf_size,
460+
"setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
463461

464-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
465-
&sock_buf_size, sizeof(sock_buf_size))) {
466-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
467-
exit(EXIT_FAILURE);
468-
}
462+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
463+
sock_buf_size,
464+
"setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
469465

470466
/* Ready to receive data. */
471467
control_writeln("SRVREADY");
@@ -597,10 +593,8 @@ static void test_seqpacket_timeout_client(const struct test_opts *opts)
597593
tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
598594
tv.tv_usec = 0;
599595

600-
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) {
601-
perror("setsockopt(SO_RCVTIMEO)");
602-
exit(EXIT_FAILURE);
603-
}
596+
setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
597+
"setsockopt(SO_RCVTIMEO)");
604598

605599
read_enter_ns = current_nsec();
606600

@@ -866,11 +860,8 @@ static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
866860
exit(EXIT_FAILURE);
867861
}
868862

869-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
870-
&lowat_val, sizeof(lowat_val))) {
871-
perror("setsockopt(SO_RCVLOWAT)");
872-
exit(EXIT_FAILURE);
873-
}
863+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
864+
lowat_val, "setsockopt(SO_RCVLOWAT)");
874865

875866
control_expectln("SRVSENT");
876867

@@ -1398,11 +1389,9 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
13981389
/* size_t can be < unsigned long long */
13991390
sock_buf_size = buf_size;
14001391

1401-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1402-
&sock_buf_size, sizeof(sock_buf_size))) {
1403-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1404-
exit(EXIT_FAILURE);
1405-
}
1392+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1393+
sock_buf_size,
1394+
"setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
14061395

14071396
if (low_rx_bytes_test) {
14081397
/* Set new SO_RCVLOWAT here. This enables sending credit
@@ -1411,11 +1400,8 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
14111400
*/
14121401
recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
14131402

1414-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1415-
&recv_buf_size, sizeof(recv_buf_size))) {
1416-
perror("setsockopt(SO_RCVLOWAT)");
1417-
exit(EXIT_FAILURE);
1418-
}
1403+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1404+
recv_buf_size, "setsockopt(SO_RCVLOWAT)");
14191405
}
14201406

14211407
/* Send one dummy byte here, because 'setsockopt()' above also
@@ -1457,11 +1443,8 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
14571443
recv_buf_size++;
14581444

14591445
/* Updating SO_RCVLOWAT will send credit update. */
1460-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1461-
&recv_buf_size, sizeof(recv_buf_size))) {
1462-
perror("setsockopt(SO_RCVLOWAT)");
1463-
exit(EXIT_FAILURE);
1464-
}
1446+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1447+
recv_buf_size, "setsockopt(SO_RCVLOWAT)");
14651448
}
14661449

14671450
fds.fd = fd;

tools/testing/vsock/vsock_test_zerocopy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void test_client(const struct test_opts *opts,
162162
}
163163

164164
if (test_data->so_zerocopy)
165-
enable_so_zerocopy(fd);
165+
enable_so_zerocopy_check(fd);
166166

167167
iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);
168168

tools/testing/vsock/vsock_uring_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void vsock_io_uring_client(const struct test_opts *opts,
7373
}
7474

7575
if (msg_zerocopy)
76-
enable_so_zerocopy(fd);
76+
enable_so_zerocopy_check(fd);
7777

7878
iovec = alloc_test_iovec(test_data->vecs, test_data->vecs_cnt);
7979

0 commit comments

Comments
 (0)