Skip to content

Commit 86814d8

Browse files
Konstantin ShkolnyyPaolo Abeni
authored andcommitted
vsock/test: verify socket options after setting them
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 <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 3f36ee2 commit 86814d8

File tree

9 files changed

+181
-53
lines changed

9 files changed

+181
-53
lines changed

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
@@ -651,3 +651,145 @@ void free_test_iovec(const struct iovec *test_iovec,
651651

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

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
@@ -444,17 +444,13 @@ static void test_seqpacket_msg_bounds_server(const struct test_opts *opts)
444444

445445
sock_buf_size = SOCK_BUF_SIZE;
446446

447-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
448-
&sock_buf_size, sizeof(sock_buf_size))) {
449-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
450-
exit(EXIT_FAILURE);
451-
}
447+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
448+
sock_buf_size,
449+
"setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
452450

453-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
454-
&sock_buf_size, sizeof(sock_buf_size))) {
455-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
456-
exit(EXIT_FAILURE);
457-
}
451+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
452+
sock_buf_size,
453+
"setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
458454

459455
/* Ready to receive data. */
460456
control_writeln("SRVREADY");
@@ -586,10 +582,8 @@ static void test_seqpacket_timeout_client(const struct test_opts *opts)
586582
tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
587583
tv.tv_usec = 0;
588584

589-
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) {
590-
perror("setsockopt(SO_RCVTIMEO)");
591-
exit(EXIT_FAILURE);
592-
}
585+
setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
586+
"setsockopt(SO_RCVTIMEO)");
593587

594588
read_enter_ns = current_nsec();
595589

@@ -855,11 +849,8 @@ static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
855849
exit(EXIT_FAILURE);
856850
}
857851

858-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
859-
&lowat_val, sizeof(lowat_val))) {
860-
perror("setsockopt(SO_RCVLOWAT)");
861-
exit(EXIT_FAILURE);
862-
}
852+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
853+
lowat_val, "setsockopt(SO_RCVLOWAT)");
863854

864855
control_expectln("SRVSENT");
865856

@@ -1383,11 +1374,9 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
13831374
/* size_t can be < unsigned long long */
13841375
sock_buf_size = buf_size;
13851376

1386-
if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1387-
&sock_buf_size, sizeof(sock_buf_size))) {
1388-
perror("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
1389-
exit(EXIT_FAILURE);
1390-
}
1377+
setsockopt_ull_check(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
1378+
sock_buf_size,
1379+
"setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
13911380

13921381
if (low_rx_bytes_test) {
13931382
/* Set new SO_RCVLOWAT here. This enables sending credit
@@ -1396,11 +1385,8 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
13961385
*/
13971386
recv_buf_size = 1 + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
13981387

1399-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1400-
&recv_buf_size, sizeof(recv_buf_size))) {
1401-
perror("setsockopt(SO_RCVLOWAT)");
1402-
exit(EXIT_FAILURE);
1403-
}
1388+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1389+
recv_buf_size, "setsockopt(SO_RCVLOWAT)");
14041390
}
14051391

14061392
/* Send one dummy byte here, because 'setsockopt()' above also
@@ -1442,11 +1428,8 @@ static void test_stream_credit_update_test(const struct test_opts *opts,
14421428
recv_buf_size++;
14431429

14441430
/* Updating SO_RCVLOWAT will send credit update. */
1445-
if (setsockopt(fd, SOL_SOCKET, SO_RCVLOWAT,
1446-
&recv_buf_size, sizeof(recv_buf_size))) {
1447-
perror("setsockopt(SO_RCVLOWAT)");
1448-
exit(EXIT_FAILURE);
1449-
}
1431+
setsockopt_int_check(fd, SOL_SOCKET, SO_RCVLOWAT,
1432+
recv_buf_size, "setsockopt(SO_RCVLOWAT)");
14501433
}
14511434

14521435
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)