Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'vsock-tests'

Stefano Garzarella says:

====================
vsock/test: add recv_buf()/send_buf() utility functions and some improvements

We recently found that some tests were failing [1].

The problem was that we were not waiting for all the bytes correctly,
so we had a partial read. I had initially suggested using MSG_WAITALL,
but this could have timeout problems.

Since we already had send_byte() and recv_byte() that handled the timeout,
but also the expected return value, I moved that code to two new functions
that we can now use to send/receive generic buffers.

The last commit is just an improvement to a test I found difficult to
understand while using the new functions.

@Arseniy a review and some testing are really appreciated :-)

[1] https://lore.kernel.org/netdev/63xflnwiohdfo6m3vnrrxgv2ulplencpwug5qqacugqh7xxpu3@tsczkuqgwurb/
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+144 -224
+107 -71
tools/testing/vsock/util.c
··· 211 211 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET); 212 212 } 213 213 214 + /* Transmit bytes from a buffer and check the return value. 215 + * 216 + * expected_ret: 217 + * <0 Negative errno (for testing errors) 218 + * 0 End-of-file 219 + * >0 Success (bytes successfully written) 220 + */ 221 + void send_buf(int fd, const void *buf, size_t len, int flags, 222 + ssize_t expected_ret) 223 + { 224 + ssize_t nwritten = 0; 225 + ssize_t ret; 226 + 227 + timeout_begin(TIMEOUT); 228 + do { 229 + ret = send(fd, buf + nwritten, len - nwritten, flags); 230 + timeout_check("send"); 231 + 232 + if (ret == 0 || (ret < 0 && errno != EINTR)) 233 + break; 234 + 235 + nwritten += ret; 236 + } while (nwritten < len); 237 + timeout_end(); 238 + 239 + if (expected_ret < 0) { 240 + if (ret != -1) { 241 + fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n", 242 + ret, expected_ret); 243 + exit(EXIT_FAILURE); 244 + } 245 + if (errno != -expected_ret) { 246 + perror("send"); 247 + exit(EXIT_FAILURE); 248 + } 249 + return; 250 + } 251 + 252 + if (ret < 0) { 253 + perror("send"); 254 + exit(EXIT_FAILURE); 255 + } 256 + 257 + if (nwritten != expected_ret) { 258 + if (ret == 0) 259 + fprintf(stderr, "unexpected EOF while sending bytes\n"); 260 + 261 + fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n", 262 + nwritten, expected_ret); 263 + exit(EXIT_FAILURE); 264 + } 265 + } 266 + 267 + /* Receive bytes in a buffer and check the return value. 268 + * 269 + * expected_ret: 270 + * <0 Negative errno (for testing errors) 271 + * 0 End-of-file 272 + * >0 Success (bytes successfully read) 273 + */ 274 + void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret) 275 + { 276 + ssize_t nread = 0; 277 + ssize_t ret; 278 + 279 + timeout_begin(TIMEOUT); 280 + do { 281 + ret = recv(fd, buf + nread, len - nread, flags); 282 + timeout_check("recv"); 283 + 284 + if (ret == 0 || (ret < 0 && errno != EINTR)) 285 + break; 286 + 287 + nread += ret; 288 + } while (nread < len); 289 + timeout_end(); 290 + 291 + if (expected_ret < 0) { 292 + if (ret != -1) { 293 + fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n", 294 + ret, expected_ret); 295 + exit(EXIT_FAILURE); 296 + } 297 + if (errno != -expected_ret) { 298 + perror("recv"); 299 + exit(EXIT_FAILURE); 300 + } 301 + return; 302 + } 303 + 304 + if (ret < 0) { 305 + perror("recv"); 306 + exit(EXIT_FAILURE); 307 + } 308 + 309 + if (nread != expected_ret) { 310 + if (ret == 0) 311 + fprintf(stderr, "unexpected EOF while receiving bytes\n"); 312 + 313 + fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n", 314 + nread, expected_ret); 315 + exit(EXIT_FAILURE); 316 + } 317 + } 318 + 214 319 /* Transmit one byte and check the return value. 215 320 * 216 321 * expected_ret: ··· 326 221 void send_byte(int fd, int expected_ret, int flags) 327 222 { 328 223 const uint8_t byte = 'A'; 329 - ssize_t nwritten; 330 224 331 - timeout_begin(TIMEOUT); 332 - do { 333 - nwritten = send(fd, &byte, sizeof(byte), flags); 334 - timeout_check("write"); 335 - } while (nwritten < 0 && errno == EINTR); 336 - timeout_end(); 337 - 338 - if (expected_ret < 0) { 339 - if (nwritten != -1) { 340 - fprintf(stderr, "bogus send(2) return value %zd\n", 341 - nwritten); 342 - exit(EXIT_FAILURE); 343 - } 344 - if (errno != -expected_ret) { 345 - perror("write"); 346 - exit(EXIT_FAILURE); 347 - } 348 - return; 349 - } 350 - 351 - if (nwritten < 0) { 352 - perror("write"); 353 - exit(EXIT_FAILURE); 354 - } 355 - if (nwritten == 0) { 356 - if (expected_ret == 0) 357 - return; 358 - 359 - fprintf(stderr, "unexpected EOF while sending byte\n"); 360 - exit(EXIT_FAILURE); 361 - } 362 - if (nwritten != sizeof(byte)) { 363 - fprintf(stderr, "bogus send(2) return value %zd\n", nwritten); 364 - exit(EXIT_FAILURE); 365 - } 225 + send_buf(fd, &byte, sizeof(byte), flags, expected_ret); 366 226 } 367 227 368 228 /* Receive one byte and check the return value. ··· 340 270 void recv_byte(int fd, int expected_ret, int flags) 341 271 { 342 272 uint8_t byte; 343 - ssize_t nread; 344 273 345 - timeout_begin(TIMEOUT); 346 - do { 347 - nread = recv(fd, &byte, sizeof(byte), flags); 348 - timeout_check("read"); 349 - } while (nread < 0 && errno == EINTR); 350 - timeout_end(); 274 + recv_buf(fd, &byte, sizeof(byte), flags, expected_ret); 351 275 352 - if (expected_ret < 0) { 353 - if (nread != -1) { 354 - fprintf(stderr, "bogus recv(2) return value %zd\n", 355 - nread); 356 - exit(EXIT_FAILURE); 357 - } 358 - if (errno != -expected_ret) { 359 - perror("read"); 360 - exit(EXIT_FAILURE); 361 - } 362 - return; 363 - } 364 - 365 - if (nread < 0) { 366 - perror("read"); 367 - exit(EXIT_FAILURE); 368 - } 369 - if (nread == 0) { 370 - if (expected_ret == 0) 371 - return; 372 - 373 - fprintf(stderr, "unexpected EOF while receiving byte\n"); 374 - exit(EXIT_FAILURE); 375 - } 376 - if (nread != sizeof(byte)) { 377 - fprintf(stderr, "bogus recv(2) return value %zd\n", nread); 378 - exit(EXIT_FAILURE); 379 - } 380 276 if (byte != 'A') { 381 277 fprintf(stderr, "unexpected byte read %c\n", byte); 382 278 exit(EXIT_FAILURE);
+3
tools/testing/vsock/util.h
··· 42 42 int vsock_seqpacket_accept(unsigned int cid, unsigned int port, 43 43 struct sockaddr_vm *clientaddrp); 44 44 void vsock_wait_remote_close(int fd); 45 + void send_buf(int fd, const void *buf, size_t len, int flags, 46 + ssize_t expected_ret); 47 + void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret); 45 48 void send_byte(int fd, int expected_ret, int flags); 46 49 void recv_byte(int fd, int expected_ret, int flags); 47 50 void run_tests(const struct test_case *test_cases,
+34 -153
tools/testing/vsock/vsock_test.c
··· 262 262 bool seqpacket) 263 263 { 264 264 unsigned char buf[MSG_PEEK_BUF_LEN]; 265 - ssize_t send_size; 266 265 int fd; 267 266 int i; 268 267 ··· 280 281 281 282 control_expectln("SRVREADY"); 282 283 283 - send_size = send(fd, buf, sizeof(buf), 0); 284 - 285 - if (send_size < 0) { 286 - perror("send"); 287 - exit(EXIT_FAILURE); 288 - } 289 - 290 - if (send_size != sizeof(buf)) { 291 - fprintf(stderr, "Invalid send size %zi\n", send_size); 292 - exit(EXIT_FAILURE); 293 - } 284 + send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 294 285 295 286 close(fd); 296 287 } ··· 291 302 unsigned char buf_half[MSG_PEEK_BUF_LEN / 2]; 292 303 unsigned char buf_normal[MSG_PEEK_BUF_LEN]; 293 304 unsigned char buf_peek[MSG_PEEK_BUF_LEN]; 294 - ssize_t res; 295 305 int fd; 296 306 297 307 if (seqpacket) ··· 304 316 } 305 317 306 318 /* Peek from empty socket. */ 307 - res = recv(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT); 308 - if (res != -1) { 309 - fprintf(stderr, "expected recv(2) failure, got %zi\n", res); 310 - exit(EXIT_FAILURE); 311 - } 312 - 313 - if (errno != EAGAIN) { 314 - perror("EAGAIN expected"); 315 - exit(EXIT_FAILURE); 316 - } 319 + recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK | MSG_DONTWAIT, 320 + -EAGAIN); 317 321 318 322 control_writeln("SRVREADY"); 319 323 320 324 /* Peek part of data. */ 321 - res = recv(fd, buf_half, sizeof(buf_half), MSG_PEEK); 322 - if (res != sizeof(buf_half)) { 323 - fprintf(stderr, "recv(2) + MSG_PEEK, expected %zu, got %zi\n", 324 - sizeof(buf_half), res); 325 - exit(EXIT_FAILURE); 326 - } 325 + recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK, sizeof(buf_half)); 327 326 328 327 /* Peek whole data. */ 329 - res = recv(fd, buf_peek, sizeof(buf_peek), MSG_PEEK); 330 - if (res != sizeof(buf_peek)) { 331 - fprintf(stderr, "recv(2) + MSG_PEEK, expected %zu, got %zi\n", 332 - sizeof(buf_peek), res); 333 - exit(EXIT_FAILURE); 334 - } 328 + recv_buf(fd, buf_peek, sizeof(buf_peek), MSG_PEEK, sizeof(buf_peek)); 335 329 336 330 /* Compare partial and full peek. */ 337 331 if (memcmp(buf_half, buf_peek, sizeof(buf_half))) { ··· 326 356 * so check it with MSG_PEEK. We must get length 327 357 * of the message. 328 358 */ 329 - res = recv(fd, buf_half, sizeof(buf_half), MSG_PEEK | 330 - MSG_TRUNC); 331 - if (res != sizeof(buf_peek)) { 332 - fprintf(stderr, 333 - "recv(2) + MSG_PEEK | MSG_TRUNC, exp %zu, got %zi\n", 334 - sizeof(buf_half), res); 335 - exit(EXIT_FAILURE); 336 - } 359 + recv_buf(fd, buf_half, sizeof(buf_half), MSG_PEEK | MSG_TRUNC, 360 + sizeof(buf_peek)); 337 361 } 338 362 339 - res = recv(fd, buf_normal, sizeof(buf_normal), 0); 340 - if (res != sizeof(buf_normal)) { 341 - fprintf(stderr, "recv(2), expected %zu, got %zi\n", 342 - sizeof(buf_normal), res); 343 - exit(EXIT_FAILURE); 344 - } 363 + recv_buf(fd, buf_normal, sizeof(buf_normal), 0, sizeof(buf_normal)); 345 364 346 365 /* Compare full peek and normal read. */ 347 366 if (memcmp(buf_peek, buf_normal, sizeof(buf_peek))) { ··· 375 416 msg_count = SOCK_BUF_SIZE / MAX_MSG_SIZE; 376 417 377 418 for (int i = 0; i < msg_count; i++) { 378 - ssize_t send_size; 379 419 size_t buf_size; 380 420 int flags; 381 421 void *buf; ··· 402 444 flags = 0; 403 445 } 404 446 405 - send_size = send(fd, buf, buf_size, flags); 406 - 407 - if (send_size < 0) { 408 - perror("send"); 409 - exit(EXIT_FAILURE); 410 - } 411 - 412 - if (send_size != buf_size) { 413 - fprintf(stderr, "Invalid send size\n"); 414 - exit(EXIT_FAILURE); 415 - } 447 + send_buf(fd, buf, buf_size, flags, buf_size); 416 448 417 449 /* 418 450 * Hash sum is computed at both client and server in ··· 503 555 exit(EXIT_FAILURE); 504 556 } 505 557 506 - if (send(fd, buf, sizeof(buf), 0) != sizeof(buf)) { 507 - perror("send failed"); 508 - exit(EXIT_FAILURE); 509 - } 558 + send_buf(fd, buf, sizeof(buf), 0, sizeof(buf)); 510 559 511 560 control_writeln("SENDDONE"); 512 561 close(fd); ··· 625 680 static void test_seqpacket_bigmsg_client(const struct test_opts *opts) 626 681 { 627 682 unsigned long sock_buf_size; 628 - ssize_t send_size; 629 683 socklen_t len; 630 684 void *data; 631 685 int fd; ··· 651 707 exit(EXIT_FAILURE); 652 708 } 653 709 654 - send_size = send(fd, data, sock_buf_size, 0); 655 - if (send_size != -1) { 656 - fprintf(stderr, "expected 'send(2)' failure, got %zi\n", 657 - send_size); 658 - exit(EXIT_FAILURE); 659 - } 660 - 661 - if (errno != EMSGSIZE) { 662 - fprintf(stderr, "expected EMSGSIZE in 'errno', got %i\n", 663 - errno); 664 - exit(EXIT_FAILURE); 665 - } 710 + send_buf(fd, data, sock_buf_size, 0, -EMSGSIZE); 666 711 667 712 control_writeln("CLISENT"); 668 713 ··· 705 772 memset(buf1, BUF_PATTERN_1, buf_size); 706 773 memset(buf2, BUF_PATTERN_2, buf_size); 707 774 708 - if (send(fd, buf1, buf_size, 0) != buf_size) { 709 - perror("send failed"); 710 - exit(EXIT_FAILURE); 711 - } 775 + send_buf(fd, buf1, buf_size, 0, buf_size); 712 776 713 - if (send(fd, buf2, buf_size, 0) != buf_size) { 714 - perror("send failed"); 715 - exit(EXIT_FAILURE); 716 - } 777 + send_buf(fd, buf2, buf_size, 0, buf_size); 717 778 718 779 close(fd); 719 780 } ··· 828 901 unsigned long lowat_val = RCVLOWAT_BUF_SIZE; 829 902 char buf[RCVLOWAT_BUF_SIZE]; 830 903 struct pollfd fds; 831 - ssize_t read_res; 832 904 short poll_flags; 833 905 int fd; 834 906 ··· 882 956 /* Use MSG_DONTWAIT, if call is going to wait, EAGAIN 883 957 * will be returned. 884 958 */ 885 - read_res = recv(fd, buf, sizeof(buf), MSG_DONTWAIT); 886 - if (read_res != RCVLOWAT_BUF_SIZE) { 887 - fprintf(stderr, "Unexpected recv result %zi\n", 888 - read_res); 889 - exit(EXIT_FAILURE); 890 - } 959 + recv_buf(fd, buf, sizeof(buf), MSG_DONTWAIT, RCVLOWAT_BUF_SIZE); 891 960 892 961 control_writeln("POLLDONE"); 893 962 ··· 894 973 static void test_inv_buf_client(const struct test_opts *opts, bool stream) 895 974 { 896 975 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 897 - ssize_t ret; 976 + ssize_t expected_ret; 898 977 int fd; 899 978 900 979 if (stream) ··· 910 989 control_expectln("SENDDONE"); 911 990 912 991 /* Use invalid buffer here. */ 913 - ret = recv(fd, NULL, sizeof(data), 0); 914 - if (ret != -1) { 915 - fprintf(stderr, "expected recv(2) failure, got %zi\n", ret); 916 - exit(EXIT_FAILURE); 917 - } 918 - 919 - if (errno != EFAULT) { 920 - fprintf(stderr, "unexpected recv(2) errno %d\n", errno); 921 - exit(EXIT_FAILURE); 922 - } 923 - 924 - ret = recv(fd, data, sizeof(data), MSG_DONTWAIT); 992 + recv_buf(fd, NULL, sizeof(data), 0, -EFAULT); 925 993 926 994 if (stream) { 927 995 /* For SOCK_STREAM we must continue reading. */ 928 - if (ret != sizeof(data)) { 929 - fprintf(stderr, "expected recv(2) success, got %zi\n", ret); 930 - exit(EXIT_FAILURE); 931 - } 932 - /* Don't check errno in case of success. */ 996 + expected_ret = sizeof(data); 933 997 } else { 934 998 /* For SOCK_SEQPACKET socket's queue must be empty. */ 935 - if (ret != -1) { 936 - fprintf(stderr, "expected recv(2) failure, got %zi\n", ret); 937 - exit(EXIT_FAILURE); 938 - } 939 - 940 - if (errno != EAGAIN) { 941 - fprintf(stderr, "unexpected recv(2) errno %d\n", errno); 942 - exit(EXIT_FAILURE); 943 - } 999 + expected_ret = -EAGAIN; 944 1000 } 1001 + 1002 + recv_buf(fd, data, sizeof(data), MSG_DONTWAIT, expected_ret); 945 1003 946 1004 control_writeln("DONE"); 947 1005 ··· 930 1030 static void test_inv_buf_server(const struct test_opts *opts, bool stream) 931 1031 { 932 1032 unsigned char data[INV_BUF_TEST_DATA_LEN] = {0}; 933 - ssize_t res; 934 1033 int fd; 935 1034 936 1035 if (stream) ··· 942 1043 exit(EXIT_FAILURE); 943 1044 } 944 1045 945 - res = send(fd, data, sizeof(data), 0); 946 - if (res != sizeof(data)) { 947 - fprintf(stderr, "unexpected send(2) result %zi\n", res); 948 - exit(EXIT_FAILURE); 949 - } 1046 + send_buf(fd, data, sizeof(data), 0, sizeof(data)); 950 1047 951 1048 control_writeln("SENDDONE"); 952 1049 ··· 976 1081 977 1082 static void test_stream_virtio_skb_merge_client(const struct test_opts *opts) 978 1083 { 979 - ssize_t res; 980 1084 int fd; 981 1085 982 1086 fd = vsock_stream_connect(opts->peer_cid, 1234); ··· 985 1091 } 986 1092 987 1093 /* Send first skbuff. */ 988 - res = send(fd, HELLO_STR, strlen(HELLO_STR), 0); 989 - if (res != strlen(HELLO_STR)) { 990 - fprintf(stderr, "unexpected send(2) result %zi\n", res); 991 - exit(EXIT_FAILURE); 992 - } 1094 + send_buf(fd, HELLO_STR, strlen(HELLO_STR), 0, strlen(HELLO_STR)); 993 1095 994 1096 control_writeln("SEND0"); 995 1097 /* Peer reads part of first skbuff. */ 996 1098 control_expectln("REPLY0"); 997 1099 998 1100 /* Send second skbuff, it will be appended to the first. */ 999 - res = send(fd, WORLD_STR, strlen(WORLD_STR), 0); 1000 - if (res != strlen(WORLD_STR)) { 1001 - fprintf(stderr, "unexpected send(2) result %zi\n", res); 1002 - exit(EXIT_FAILURE); 1003 - } 1101 + send_buf(fd, WORLD_STR, strlen(WORLD_STR), 0, strlen(WORLD_STR)); 1004 1102 1005 1103 control_writeln("SEND1"); 1006 1104 /* Peer reads merged skbuff packet. */ ··· 1003 1117 1004 1118 static void test_stream_virtio_skb_merge_server(const struct test_opts *opts) 1005 1119 { 1120 + size_t read = 0, to_read; 1006 1121 unsigned char buf[64]; 1007 - ssize_t res; 1008 1122 int fd; 1009 1123 1010 1124 fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL); ··· 1016 1130 control_expectln("SEND0"); 1017 1131 1018 1132 /* Read skbuff partially. */ 1019 - res = recv(fd, buf, 2, 0); 1020 - if (res != 2) { 1021 - fprintf(stderr, "expected recv(2) returns 2 bytes, got %zi\n", res); 1022 - exit(EXIT_FAILURE); 1023 - } 1133 + to_read = 2; 1134 + recv_buf(fd, buf + read, to_read, 0, to_read); 1135 + read += to_read; 1024 1136 1025 1137 control_writeln("REPLY0"); 1026 1138 control_expectln("SEND1"); 1027 1139 1028 - res = recv(fd, buf + 2, sizeof(buf) - 2, 0); 1029 - if (res != 8) { 1030 - fprintf(stderr, "expected recv(2) returns 8 bytes, got %zi\n", res); 1031 - exit(EXIT_FAILURE); 1032 - } 1140 + /* Read the rest of both buffers */ 1141 + to_read = strlen(HELLO_STR WORLD_STR) - read; 1142 + recv_buf(fd, buf + read, to_read, 0, to_read); 1143 + read += to_read; 1033 1144 1034 - res = recv(fd, buf, sizeof(buf) - 8 - 2, MSG_DONTWAIT); 1035 - if (res != -1) { 1036 - fprintf(stderr, "expected recv(2) failure, got %zi\n", res); 1037 - exit(EXIT_FAILURE); 1038 - } 1145 + /* No more bytes should be there */ 1146 + to_read = sizeof(buf) - read; 1147 + recv_buf(fd, buf + read, to_read, MSG_DONTWAIT, -EAGAIN); 1039 1148 1040 1149 if (memcmp(buf, HELLO_STR WORLD_STR, strlen(HELLO_STR WORLD_STR))) { 1041 1150 fprintf(stderr, "pattern mismatch\n");