Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | /* * vsock_diag_test - vsock_diag.ko test suite * * Copyright (C) 2017 Red Hat, Inc. * * Author: Stefan Hajnoczi <stefanha@redhat.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. */ #include <getopt.h> #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <signal.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/types.h> #include <linux/list.h> #include <linux/net.h> #include <linux/netlink.h> #include <linux/sock_diag.h> #include <netinet/tcp.h> #include "../../../include/uapi/linux/vm_sockets.h" #include "../../../include/uapi/linux/vm_sockets_diag.h" #include "timeout.h" #include "control.h" enum test_mode { TEST_MODE_UNSET, TEST_MODE_CLIENT, TEST_MODE_SERVER }; /* Per-socket status */ struct vsock_stat { struct list_head list; struct vsock_diag_msg msg; }; static const char *sock_type_str(int type) { switch (type) { case SOCK_DGRAM: return "DGRAM"; case SOCK_STREAM: return "STREAM"; default: return "INVALID TYPE"; } } static const char *sock_state_str(int state) { switch (state) { case TCP_CLOSE: return "UNCONNECTED"; case TCP_SYN_SENT: return "CONNECTING"; case TCP_ESTABLISHED: return "CONNECTED"; case TCP_CLOSING: return "DISCONNECTING"; case TCP_LISTEN: return "LISTEN"; default: return "INVALID STATE"; } } static const char *sock_shutdown_str(int shutdown) { switch (shutdown) { case 1: return "RCV_SHUTDOWN"; case 2: return "SEND_SHUTDOWN"; case 3: return "RCV_SHUTDOWN | SEND_SHUTDOWN"; default: return "0"; } } static void print_vsock_addr(FILE *fp, unsigned int cid, unsigned int port) { if (cid == VMADDR_CID_ANY) fprintf(fp, "*:"); else fprintf(fp, "%u:", cid); if (port == VMADDR_PORT_ANY) fprintf(fp, "*"); else fprintf(fp, "%u", port); } static void print_vsock_stat(FILE *fp, struct vsock_stat *st) { print_vsock_addr(fp, st->msg.vdiag_src_cid, st->msg.vdiag_src_port); fprintf(fp, " "); print_vsock_addr(fp, st->msg.vdiag_dst_cid, st->msg.vdiag_dst_port); fprintf(fp, " %s %s %s %u\n", sock_type_str(st->msg.vdiag_type), sock_state_str(st->msg.vdiag_state), sock_shutdown_str(st->msg.vdiag_shutdown), st->msg.vdiag_ino); } static void print_vsock_stats(FILE *fp, struct list_head *head) { struct vsock_stat *st; list_for_each_entry(st, head, list) print_vsock_stat(fp, st); } static struct vsock_stat *find_vsock_stat(struct list_head *head, int fd) { struct vsock_stat *st; struct stat stat; if (fstat(fd, &stat) < 0) { perror("fstat"); exit(EXIT_FAILURE); } list_for_each_entry(st, head, list) if (st->msg.vdiag_ino == stat.st_ino) return st; fprintf(stderr, "cannot find fd %d\n", fd); exit(EXIT_FAILURE); } static void check_no_sockets(struct list_head *head) { if (!list_empty(head)) { fprintf(stderr, "expected no sockets\n"); print_vsock_stats(stderr, head); exit(1); } } static void check_num_sockets(struct list_head *head, int expected) { struct list_head *node; int n = 0; list_for_each(node, head) n++; if (n != expected) { fprintf(stderr, "expected %d sockets, found %d\n", expected, n); print_vsock_stats(stderr, head); exit(EXIT_FAILURE); } } static void check_socket_state(struct vsock_stat *st, __u8 state) { if (st->msg.vdiag_state != state) { fprintf(stderr, "expected socket state %#x, got %#x\n", state, st->msg.vdiag_state); exit(EXIT_FAILURE); } } static void send_req(int fd) { struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK, }; struct { struct nlmsghdr nlh; struct vsock_diag_req vreq; } req = { .nlh = { .nlmsg_len = sizeof(req), .nlmsg_type = SOCK_DIAG_BY_FAMILY, .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP, }, .vreq = { .sdiag_family = AF_VSOCK, .vdiag_states = ~(__u32)0, }, }; struct iovec iov = { .iov_base = &req, .iov_len = sizeof(req), }; struct msghdr msg = { .msg_name = &nladdr, .msg_namelen = sizeof(nladdr), .msg_iov = &iov, .msg_iovlen = 1, }; for (;;) { if (sendmsg(fd, &msg, 0) < 0) { if (errno == EINTR) continue; perror("sendmsg"); exit(EXIT_FAILURE); } return; } } static ssize_t recv_resp(int fd, void *buf, size_t len) { struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK, }; struct iovec iov = { .iov_base = buf, .iov_len = len, }; struct msghdr msg = { .msg_name = &nladdr, .msg_namelen = sizeof(nladdr), .msg_iov = &iov, .msg_iovlen = 1, }; ssize_t ret; do { ret = recvmsg(fd, &msg, 0); } while (ret < 0 && errno == EINTR); if (ret < 0) { perror("recvmsg"); exit(EXIT_FAILURE); } return ret; } static void add_vsock_stat(struct list_head *sockets, const struct vsock_diag_msg *resp) { struct vsock_stat *st; st = malloc(sizeof(*st)); if (!st) { perror("malloc"); exit(EXIT_FAILURE); } st->msg = *resp; list_add_tail(&st->list, sockets); } /* * Read vsock stats into a list. */ static void read_vsock_stat(struct list_head *sockets) { long buf[8192 / sizeof(long)]; int fd; fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); if (fd < 0) { perror("socket"); exit(EXIT_FAILURE); } send_req(fd); for (;;) { const struct nlmsghdr *h; ssize_t ret; ret = recv_resp(fd, buf, sizeof(buf)); if (ret == 0) goto done; if (ret < sizeof(*h)) { fprintf(stderr, "short read of %zd bytes\n", ret); exit(EXIT_FAILURE); } h = (struct nlmsghdr *)buf; while (NLMSG_OK(h, ret)) { if (h->nlmsg_type == NLMSG_DONE) goto done; if (h->nlmsg_type == NLMSG_ERROR) { const struct nlmsgerr *err = NLMSG_DATA(h); if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) fprintf(stderr, "NLMSG_ERROR\n"); else { errno = -err->error; perror("NLMSG_ERROR"); } exit(EXIT_FAILURE); } if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY) { fprintf(stderr, "unexpected nlmsg_type %#x\n", h->nlmsg_type); exit(EXIT_FAILURE); } if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct vsock_diag_msg))) { fprintf(stderr, "short vsock_diag_msg\n"); exit(EXIT_FAILURE); } add_vsock_stat(sockets, NLMSG_DATA(h)); h = NLMSG_NEXT(h, ret); } } done: close(fd); } static void free_sock_stat(struct list_head *sockets) { struct vsock_stat *st; struct vsock_stat *next; list_for_each_entry_safe(st, next, sockets, list) free(st); } static void test_no_sockets(unsigned int peer_cid) { LIST_HEAD(sockets); read_vsock_stat(&sockets); check_no_sockets(&sockets); free_sock_stat(&sockets); } static void test_listen_socket_server(unsigned int peer_cid) { union { struct sockaddr sa; struct sockaddr_vm svm; } addr = { .svm = { .svm_family = AF_VSOCK, .svm_port = 1234, .svm_cid = VMADDR_CID_ANY, }, }; LIST_HEAD(sockets); struct vsock_stat *st; int fd; fd = socket(AF_VSOCK, SOCK_STREAM, 0); if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { perror("bind"); exit(EXIT_FAILURE); } if (listen(fd, 1) < 0) { perror("listen"); exit(EXIT_FAILURE); } read_vsock_stat(&sockets); check_num_sockets(&sockets, 1); st = find_vsock_stat(&sockets, fd); check_socket_state(st, TCP_LISTEN); close(fd); free_sock_stat(&sockets); } static void test_connect_client(unsigned int peer_cid) { union { struct sockaddr sa; struct sockaddr_vm svm; } addr = { .svm = { .svm_family = AF_VSOCK, .svm_port = 1234, .svm_cid = peer_cid, }, }; int fd; int ret; LIST_HEAD(sockets); struct vsock_stat *st; control_expectln("LISTENING"); fd = socket(AF_VSOCK, SOCK_STREAM, 0); timeout_begin(TIMEOUT); do { ret = connect(fd, &addr.sa, sizeof(addr.svm)); timeout_check("connect"); } while (ret < 0 && errno == EINTR); timeout_end(); if (ret < 0) { perror("connect"); exit(EXIT_FAILURE); } read_vsock_stat(&sockets); check_num_sockets(&sockets, 1); st = find_vsock_stat(&sockets, fd); check_socket_state(st, TCP_ESTABLISHED); control_expectln("DONE"); control_writeln("DONE"); close(fd); free_sock_stat(&sockets); } static void test_connect_server(unsigned int peer_cid) { union { struct sockaddr sa; struct sockaddr_vm svm; } addr = { .svm = { .svm_family = AF_VSOCK, .svm_port = 1234, .svm_cid = VMADDR_CID_ANY, }, }; union { struct sockaddr sa; struct sockaddr_vm svm; } clientaddr; socklen_t clientaddr_len = sizeof(clientaddr.svm); LIST_HEAD(sockets); struct vsock_stat *st; int fd; int client_fd; fd = socket(AF_VSOCK, SOCK_STREAM, 0); if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { perror("bind"); exit(EXIT_FAILURE); } if (listen(fd, 1) < 0) { perror("listen"); exit(EXIT_FAILURE); } control_writeln("LISTENING"); timeout_begin(TIMEOUT); do { client_fd = accept(fd, &clientaddr.sa, &clientaddr_len); timeout_check("accept"); } while (client_fd < 0 && errno == EINTR); timeout_end(); if (client_fd < 0) { perror("accept"); exit(EXIT_FAILURE); } if (clientaddr.sa.sa_family != AF_VSOCK) { fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n", clientaddr.sa.sa_family); exit(EXIT_FAILURE); } if (clientaddr.svm.svm_cid != peer_cid) { fprintf(stderr, "expected peer CID %u from accept(2), got %u\n", peer_cid, clientaddr.svm.svm_cid); exit(EXIT_FAILURE); } read_vsock_stat(&sockets); check_num_sockets(&sockets, 2); find_vsock_stat(&sockets, fd); st = find_vsock_stat(&sockets, client_fd); check_socket_state(st, TCP_ESTABLISHED); control_writeln("DONE"); control_expectln("DONE"); close(client_fd); close(fd); free_sock_stat(&sockets); } static struct { const char *name; void (*run_client)(unsigned int peer_cid); void (*run_server)(unsigned int peer_cid); } test_cases[] = { { .name = "No sockets", .run_server = test_no_sockets, }, { .name = "Listen socket", .run_server = test_listen_socket_server, }, { .name = "Connect", .run_client = test_connect_client, .run_server = test_connect_server, }, {}, }; static void init_signals(void) { struct sigaction act = { .sa_handler = sigalrm, }; sigaction(SIGALRM, &act, NULL); signal(SIGPIPE, SIG_IGN); } static unsigned int parse_cid(const char *str) { char *endptr = NULL; unsigned long int n; errno = 0; n = strtoul(str, &endptr, 10); if (errno || *endptr != '\0') { fprintf(stderr, "malformed CID \"%s\"\n", str); exit(EXIT_FAILURE); } return n; } static const char optstring[] = ""; static const struct option longopts[] = { { .name = "control-host", .has_arg = required_argument, .val = 'H', }, { .name = "control-port", .has_arg = required_argument, .val = 'P', }, { .name = "mode", .has_arg = required_argument, .val = 'm', }, { .name = "peer-cid", .has_arg = required_argument, .val = 'p', }, { .name = "help", .has_arg = no_argument, .val = '?', }, {}, }; static void usage(void) { fprintf(stderr, "Usage: vsock_diag_test [--help] [--control-host=<host>] --control-port=<port> --mode=client|server --peer-cid=<cid>\n" "\n" " Server: vsock_diag_test --control-port=1234 --mode=server --peer-cid=3\n" " Client: vsock_diag_test --control-host=192.168.0.1 --control-port=1234 --mode=client --peer-cid=2\n" "\n" "Run vsock_diag.ko tests. Must be launched in both\n" "guest and host. One side must use --mode=client and\n" "the other side must use --mode=server.\n" "\n" "A TCP control socket connection is used to coordinate tests\n" "between the client and the server. The server requires a\n" "listen address and the client requires an address to\n" "connect to.\n" "\n" "The CID of the other side must be given with --peer-cid=<cid>.\n"); exit(EXIT_FAILURE); } int main(int argc, char **argv) { const char *control_host = NULL; const char *control_port = NULL; int mode = TEST_MODE_UNSET; unsigned int peer_cid = VMADDR_CID_ANY; int i; init_signals(); for (;;) { int opt = getopt_long(argc, argv, optstring, longopts, NULL); if (opt == -1) break; switch (opt) { case 'H': control_host = optarg; break; case 'm': if (strcmp(optarg, "client") == 0) mode = TEST_MODE_CLIENT; else if (strcmp(optarg, "server") == 0) mode = TEST_MODE_SERVER; else { fprintf(stderr, "--mode must be \"client\" or \"server\"\n"); return EXIT_FAILURE; } break; case 'p': peer_cid = parse_cid(optarg); break; case 'P': control_port = optarg; break; case '?': default: usage(); } } if (!control_port) usage(); if (mode == TEST_MODE_UNSET) usage(); if (peer_cid == VMADDR_CID_ANY) usage(); if (!control_host) { if (mode != TEST_MODE_SERVER) usage(); control_host = "0.0.0.0"; } control_init(control_host, control_port, mode == TEST_MODE_SERVER); for (i = 0; test_cases[i].name; i++) { void (*run)(unsigned int peer_cid); printf("%s...", test_cases[i].name); fflush(stdout); if (mode == TEST_MODE_CLIENT) run = test_cases[i].run_client; else run = test_cases[i].run_server; if (run) run(peer_cid); printf("ok\n"); } control_cleanup(); return EXIT_SUCCESS; } |