| 1 | |
| 2 | |
| 3 | * vsock test utilities
|
| 4 | *
|
| 5 | * Copyright (C) 2017 Red Hat, Inc.
|
| 6 | *
|
| 7 | * Author: Stefan Hajnoczi <[email protected]>
|
| 8 |
|
| 9 |
|
| 10 | #include <ctype.h>
|
| 11 | #include <errno.h>
|
| 12 | #include <stdio.h>
|
| 13 | #include <stdint.h>
|
| 14 | #include <stdlib.h>
|
| 15 | #include <string.h>
|
| 16 | #include <signal.h>
|
| 17 | #include <unistd.h>
|
| 18 | #include <assert.h>
|
| 19 | #include <sys/epoll.h>
|
| 20 | #include <sys/ioctl.h>
|
| 21 | #include <sys/mman.h>
|
| 22 | #include <linux/sockios.h>
|
| 23 |
|
| 24 | #include "timeout.h"
|
| 25 | #include "control.h"
|
| 26 | #include "util.h"
|
| 27 |
|
| 28 | #define KALLSYMS_PATH "/proc/kallsyms"
|
| 29 | #define KALLSYMS_LINE_LEN 512
|
| 30 |
|
| 31 |
|
| 32 | void init_signals(void)
|
| 33 | {
|
| 34 | struct sigaction act = {
|
| 35 | .sa_handler = sigalrm,
|
| 36 | };
|
| 37 |
|
| 38 | sigaction(SIGALRM, &act, NULL);
|
| 39 | signal(SIGPIPE, SIG_IGN);
|
| 40 | }
|
| 41 |
|
| 42 | static unsigned int parse_uint(const char *str, const char *err_str)
|
| 43 | {
|
| 44 | char *endptr = NULL;
|
| 45 | unsigned long n;
|
| 46 |
|
| 47 | errno = 0;
|
| 48 | n = strtoul(str, &endptr, 10);
|
| 49 | if (errno || *endptr != '\0') {
|
| 50 | fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
|
| 51 | exit(EXIT_FAILURE);
|
| 52 | }
|
| 53 | return n;
|
| 54 | }
|
| 55 |
|
| 56 |
|
| 57 | unsigned int parse_cid(const char *str)
|
| 58 | {
|
| 59 | return parse_uint(str, "CID");
|
| 60 | }
|
| 61 |
|
| 62 |
|
| 63 | unsigned int parse_port(const char *str)
|
| 64 | {
|
| 65 | return parse_uint(str, "port");
|
| 66 | }
|
| 67 |
|
| 68 |
|
| 69 | void vsock_wait_remote_close(int fd)
|
| 70 | {
|
| 71 | struct epoll_event ev;
|
| 72 | int epollfd, nfds;
|
| 73 |
|
| 74 | epollfd = epoll_create1(0);
|
| 75 | if (epollfd == -1) {
|
| 76 | perror("epoll_create1");
|
| 77 | exit(EXIT_FAILURE);
|
| 78 | }
|
| 79 |
|
| 80 | ev.events = EPOLLRDHUP | EPOLLHUP;
|
| 81 | ev.data.fd = fd;
|
| 82 | if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
|
| 83 | perror("epoll_ctl");
|
| 84 | exit(EXIT_FAILURE);
|
| 85 | }
|
| 86 |
|
| 87 | nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
|
| 88 | if (nfds == -1) {
|
| 89 | perror("epoll_wait");
|
| 90 | exit(EXIT_FAILURE);
|
| 91 | }
|
| 92 |
|
| 93 | if (nfds == 0) {
|
| 94 | fprintf(stderr, "epoll_wait timed out\n");
|
| 95 | exit(EXIT_FAILURE);
|
| 96 | }
|
| 97 |
|
| 98 | assert(nfds == 1);
|
| 99 | assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
|
| 100 | assert(ev.data.fd == fd);
|
| 101 |
|
| 102 | close(epollfd);
|
| 103 | }
|
| 104 |
|
| 105 | |
| 106 | * Return false if the op is not supported.
|
| 107 |
|
| 108 | bool vsock_ioctl_int(int fd, unsigned long op, int expected)
|
| 109 | {
|
| 110 | int actual, ret;
|
| 111 | char name[32];
|
| 112 |
|
| 113 | snprintf(name, sizeof(name), "ioctl(%lu)", op);
|
| 114 |
|
| 115 | timeout_begin(TIMEOUT);
|
| 116 | do {
|
| 117 | ret = ioctl(fd, op, &actual);
|
| 118 | if (ret < 0) {
|
| 119 | if (errno == EOPNOTSUPP || errno == ENOTTY)
|
| 120 | break;
|
| 121 |
|
| 122 | perror(name);
|
| 123 | exit(EXIT_FAILURE);
|
| 124 | }
|
| 125 | timeout_check(name);
|
| 126 | } while (actual != expected);
|
| 127 | timeout_end();
|
| 128 |
|
| 129 | return ret >= 0;
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | * Return false if transport does not implement the unsent_bytes() callback.
|
| 134 |
|
| 135 | bool vsock_wait_sent(int fd)
|
| 136 | {
|
| 137 | return vsock_ioctl_int(fd, SIOCOUTQ, 0);
|
| 138 | }
|
| 139 |
|
| 140 | |
| 141 | * Return the file descriptor, or -1 on error.
|
| 142 |
|
| 143 | int vsock_bind_try(unsigned int cid, unsigned int port, int type)
|
| 144 | {
|
| 145 | struct sockaddr_vm sa = {
|
| 146 | .svm_family = AF_VSOCK,
|
| 147 | .svm_cid = cid,
|
| 148 | .svm_port = port,
|
| 149 | };
|
| 150 | int fd, saved_errno;
|
| 151 |
|
| 152 | fd = socket(AF_VSOCK, type, 0);
|
| 153 | if (fd < 0) {
|
| 154 | perror("socket");
|
| 155 | exit(EXIT_FAILURE);
|
| 156 | }
|
| 157 |
|
| 158 | if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
|
| 159 | saved_errno = errno;
|
| 160 | close(fd);
|
| 161 | errno = saved_errno;
|
| 162 | fd = -1;
|
| 163 | }
|
| 164 |
|
| 165 | return fd;
|
| 166 | }
|
| 167 |
|
| 168 |
|
| 169 | int vsock_bind(unsigned int cid, unsigned int port, int type)
|
| 170 | {
|
| 171 | int fd;
|
| 172 |
|
| 173 | fd = vsock_bind_try(cid, port, type);
|
| 174 | if (fd < 0) {
|
| 175 | perror("bind");
|
| 176 | exit(EXIT_FAILURE);
|
| 177 | }
|
| 178 |
|
| 179 | return fd;
|
| 180 | }
|
| 181 |
|
| 182 | int vsock_connect_fd(int fd, unsigned int cid, unsigned int port)
|
| 183 | {
|
| 184 | struct sockaddr_vm sa = {
|
| 185 | .svm_family = AF_VSOCK,
|
| 186 | .svm_cid = cid,
|
| 187 | .svm_port = port,
|
| 188 | };
|
| 189 | int ret;
|
| 190 |
|
| 191 | timeout_begin(TIMEOUT);
|
| 192 | do {
|
| 193 | ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
|
| 194 | timeout_check("connect");
|
| 195 | } while (ret < 0 && errno == EINTR);
|
| 196 | timeout_end();
|
| 197 |
|
| 198 | return ret;
|
| 199 | }
|
| 200 |
|
| 201 |
|
| 202 | int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
|
| 203 | {
|
| 204 | int client_fd;
|
| 205 |
|
| 206 | client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
|
| 207 |
|
| 208 | if (vsock_connect_fd(client_fd, cid, port)) {
|
| 209 | perror("connect");
|
| 210 | exit(EXIT_FAILURE);
|
| 211 | }
|
| 212 |
|
| 213 | return client_fd;
|
| 214 | }
|
| 215 |
|
| 216 |
|
| 217 | int vsock_connect(unsigned int cid, unsigned int port, int type)
|
| 218 | {
|
| 219 | int fd;
|
| 220 |
|
| 221 | control_expectln("LISTENING");
|
| 222 |
|
| 223 | fd = socket(AF_VSOCK, type, 0);
|
| 224 | if (fd < 0) {
|
| 225 | perror("socket");
|
| 226 | exit(EXIT_FAILURE);
|
| 227 | }
|
| 228 |
|
| 229 | if (vsock_connect_fd(fd, cid, port)) {
|
| 230 | int old_errno = errno;
|
| 231 |
|
| 232 | close(fd);
|
| 233 | fd = -1;
|
| 234 | errno = old_errno;
|
| 235 | }
|
| 236 |
|
| 237 | return fd;
|
| 238 | }
|
| 239 |
|
| 240 | int vsock_stream_connect(unsigned int cid, unsigned int port)
|
| 241 | {
|
| 242 | return vsock_connect(cid, port, SOCK_STREAM);
|
| 243 | }
|
| 244 |
|
| 245 | int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
|
| 246 | {
|
| 247 | return vsock_connect(cid, port, SOCK_SEQPACKET);
|
| 248 | }
|
| 249 |
|
| 250 |
|
| 251 | static int vsock_listen(unsigned int cid, unsigned int port, int type)
|
| 252 | {
|
| 253 | int fd;
|
| 254 |
|
| 255 | fd = vsock_bind(cid, port, type);
|
| 256 |
|
| 257 | if (listen(fd, 1) < 0) {
|
| 258 | perror("listen");
|
| 259 | exit(EXIT_FAILURE);
|
| 260 | }
|
| 261 |
|
| 262 | return fd;
|
| 263 | }
|
| 264 |
|
| 265 | |
| 266 | * address is stored to clientaddrp. clientaddrp may be NULL.
|
| 267 |
|
| 268 | int vsock_accept(unsigned int cid, unsigned int port,
|
| 269 | struct sockaddr_vm *clientaddrp, int type)
|
| 270 | {
|
| 271 | union {
|
| 272 | struct sockaddr sa;
|
| 273 | struct sockaddr_vm svm;
|
| 274 | } clientaddr;
|
| 275 | socklen_t clientaddr_len = sizeof(clientaddr.svm);
|
| 276 | int fd, client_fd, old_errno;
|
| 277 |
|
| 278 | fd = vsock_listen(cid, port, type);
|
| 279 |
|
| 280 | control_writeln("LISTENING");
|
| 281 |
|
| 282 | timeout_begin(TIMEOUT);
|
| 283 | do {
|
| 284 | client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
|
| 285 | timeout_check("accept");
|
| 286 | } while (client_fd < 0 && errno == EINTR);
|
| 287 | timeout_end();
|
| 288 |
|
| 289 | old_errno = errno;
|
| 290 | close(fd);
|
| 291 | errno = old_errno;
|
| 292 |
|
| 293 | if (client_fd < 0)
|
| 294 | return client_fd;
|
| 295 |
|
| 296 | if (clientaddr_len != sizeof(clientaddr.svm)) {
|
| 297 | fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
|
| 298 | (size_t)clientaddr_len);
|
| 299 | exit(EXIT_FAILURE);
|
| 300 | }
|
| 301 | if (clientaddr.sa.sa_family != AF_VSOCK) {
|
| 302 | fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
|
| 303 | clientaddr.sa.sa_family);
|
| 304 | exit(EXIT_FAILURE);
|
| 305 | }
|
| 306 |
|
| 307 | if (clientaddrp)
|
| 308 | *clientaddrp = clientaddr.svm;
|
| 309 | return client_fd;
|
| 310 | }
|
| 311 |
|
| 312 | int vsock_stream_accept(unsigned int cid, unsigned int port,
|
| 313 | struct sockaddr_vm *clientaddrp)
|
| 314 | {
|
| 315 | return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
|
| 316 | }
|
| 317 |
|
| 318 | int vsock_stream_listen(unsigned int cid, unsigned int port)
|
| 319 | {
|
| 320 | return vsock_listen(cid, port, SOCK_STREAM);
|
| 321 | }
|
| 322 |
|
| 323 | int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
|
| 324 | struct sockaddr_vm *clientaddrp)
|
| 325 | {
|
| 326 | return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
|
| 327 | }
|
| 328 |
|
| 329 | |
| 330 | *
|
| 331 | * expected_ret:
|
| 332 | * <0 Negative errno (for testing errors)
|
| 333 | * 0 End-of-file
|
| 334 | * >0 Success (bytes successfully written)
|
| 335 |
|
| 336 | void send_buf(int fd, const void *buf, size_t len, int flags,
|
| 337 | ssize_t expected_ret)
|
| 338 | {
|
| 339 | ssize_t nwritten = 0;
|
| 340 | ssize_t ret;
|
| 341 |
|
| 342 | timeout_begin(TIMEOUT);
|
| 343 | do {
|
| 344 | ret = send(fd, buf + nwritten, len - nwritten, flags);
|
| 345 | timeout_check("send");
|
| 346 |
|
| 347 | if (ret < 0 && errno == EINTR)
|
| 348 | continue;
|
| 349 | if (ret <= 0)
|
| 350 | break;
|
| 351 |
|
| 352 | nwritten += ret;
|
| 353 | } while (nwritten < len);
|
| 354 | timeout_end();
|
| 355 |
|
| 356 | if (expected_ret < 0) {
|
| 357 | if (ret != -1) {
|
| 358 | fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
|
| 359 | ret, expected_ret);
|
| 360 | exit(EXIT_FAILURE);
|
| 361 | }
|
| 362 | if (errno != -expected_ret) {
|
| 363 | perror("send");
|
| 364 | exit(EXIT_FAILURE);
|
| 365 | }
|
| 366 | return;
|
| 367 | }
|
| 368 |
|
| 369 | if (ret < 0) {
|
| 370 | perror("send");
|
| 371 | exit(EXIT_FAILURE);
|
| 372 | }
|
| 373 |
|
| 374 | if (nwritten != expected_ret) {
|
| 375 | if (ret == 0)
|
| 376 | fprintf(stderr, "unexpected EOF while sending bytes\n");
|
| 377 |
|
| 378 | fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
|
| 379 | nwritten, expected_ret);
|
| 380 | exit(EXIT_FAILURE);
|
| 381 | }
|
| 382 | }
|
| 383 |
|
| 384 | #define RECV_PEEK_RETRY_USEC (10 * 1000)
|
| 385 |
|
| 386 | |
| 387 | *
|
| 388 | * When MSG_PEEK is set, recv() is retried until it returns at least
|
| 389 | * expected_ret bytes. The function returns on error, EOF, or timeout
|
| 390 | * as usual.
|
| 391 | *
|
| 392 | * expected_ret:
|
| 393 | * <0 Negative errno (for testing errors)
|
| 394 | * 0 End-of-file
|
| 395 | * >0 Success (bytes successfully read)
|
| 396 |
|
| 397 | void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
|
| 398 | {
|
| 399 | ssize_t nread = 0;
|
| 400 | ssize_t ret;
|
| 401 |
|
| 402 | timeout_begin(TIMEOUT);
|
| 403 | do {
|
| 404 | ret = recv(fd, buf + nread, len - nread, flags);
|
| 405 | timeout_check("recv");
|
| 406 |
|
| 407 | if (ret < 0 && errno == EINTR)
|
| 408 | continue;
|
| 409 | if (ret <= 0)
|
| 410 | break;
|
| 411 |
|
| 412 | if (flags & MSG_PEEK) {
|
| 413 | if (ret >= expected_ret) {
|
| 414 | nread = ret;
|
| 415 | break;
|
| 416 | }
|
| 417 | timeout_usleep(RECV_PEEK_RETRY_USEC);
|
| 418 | continue;
|
| 419 | }
|
| 420 |
|
| 421 | nread += ret;
|
| 422 | } while (nread < len);
|
| 423 | timeout_end();
|
| 424 |
|
| 425 | if (expected_ret < 0) {
|
| 426 | if (ret != -1) {
|
| 427 | fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
|
| 428 | ret, expected_ret);
|
| 429 | exit(EXIT_FAILURE);
|
| 430 | }
|
| 431 | if (errno != -expected_ret) {
|
| 432 | perror("recv");
|
| 433 | exit(EXIT_FAILURE);
|
| 434 | }
|
| 435 | return;
|
| 436 | }
|
| 437 |
|
| 438 | if (ret < 0) {
|
| 439 | perror("recv");
|
| 440 | exit(EXIT_FAILURE);
|
| 441 | }
|
| 442 |
|
| 443 | if (nread != expected_ret) {
|
| 444 | if (ret == 0)
|
| 445 | fprintf(stderr, "unexpected EOF while receiving bytes\n");
|
| 446 |
|
| 447 | fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
|
| 448 | nread, expected_ret);
|
| 449 | exit(EXIT_FAILURE);
|
| 450 | }
|
| 451 | }
|
| 452 |
|
| 453 | |
| 454 | *
|
| 455 | * expected_ret:
|
| 456 | * <0 Negative errno (for testing errors)
|
| 457 | * 0 End-of-file
|
| 458 | * 1 Success
|
| 459 |
|
| 460 | void send_byte(int fd, int expected_ret, int flags)
|
| 461 | {
|
| 462 | static const uint8_t byte = 'A';
|
| 463 |
|
| 464 | send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
|
| 465 | }
|
| 466 |
|
| 467 | |
| 468 | *
|
| 469 | * expected_ret:
|
| 470 | * <0 Negative errno (for testing errors)
|
| 471 | * 0 End-of-file
|
| 472 | * 1 Success
|
| 473 |
|
| 474 | void recv_byte(int fd, int expected_ret, int flags)
|
| 475 | {
|
| 476 | uint8_t byte;
|
| 477 |
|
| 478 | recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
|
| 479 |
|
| 480 | if (byte != 'A') {
|
| 481 | fprintf(stderr, "unexpected byte read 0x%02x\n", byte);
|
| 482 | exit(EXIT_FAILURE);
|
| 483 | }
|
| 484 | }
|
| 485 |
|
| 486 |
|
| 487 | void run_tests(const struct test_case *test_cases,
|
| 488 | const struct test_opts *opts)
|
| 489 | {
|
| 490 | int i;
|
| 491 |
|
| 492 | for (i = 0; test_cases[i].name; i++) {
|
| 493 | void (*run)(const struct test_opts *opts);
|
| 494 | char *line;
|
| 495 |
|
| 496 | printf("%d - %s...", i, test_cases[i].name);
|
| 497 | fflush(stdout);
|
| 498 |
|
| 499 | |
| 500 | * ensures that client and server are executing the
|
| 501 | * same test case. In particular, it means whoever is
|
| 502 | * faster will not see the peer still executing the
|
| 503 | * last test. This is important because port numbers
|
| 504 | * can be used by multiple test cases.
|
| 505 |
|
| 506 | if (test_cases[i].skip)
|
| 507 | control_writeln("SKIP");
|
| 508 | else
|
| 509 | control_writeln("NEXT");
|
| 510 |
|
| 511 | line = control_readln();
|
| 512 | if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
|
| 513 |
|
| 514 | printf("skipped\n");
|
| 515 |
|
| 516 | free(line);
|
| 517 | continue;
|
| 518 | }
|
| 519 |
|
| 520 | control_cmpln(line, "NEXT", true);
|
| 521 | free(line);
|
| 522 |
|
| 523 | if (opts->mode == TEST_MODE_CLIENT)
|
| 524 | run = test_cases[i].run_client;
|
| 525 | else
|
| 526 | run = test_cases[i].run_server;
|
| 527 |
|
| 528 | if (run)
|
| 529 | run(opts);
|
| 530 |
|
| 531 | printf("ok\n");
|
| 532 | }
|
| 533 |
|
| 534 | printf("All tests have been executed. Waiting other peer...");
|
| 535 | fflush(stdout);
|
| 536 |
|
| 537 | |
| 538 | * Final full barrier, to ensure that all tests have been run and
|
| 539 | * that even the last one has been successful on both sides.
|
| 540 |
|
| 541 | control_writeln("COMPLETED");
|
| 542 | control_expectln("COMPLETED");
|
| 543 |
|
| 544 | printf("ok\n");
|
| 545 | }
|
| 546 |
|
| 547 | void list_tests(const struct test_case *test_cases)
|
| 548 | {
|
| 549 | int i;
|
| 550 |
|
| 551 | printf("ID\tTest name\n");
|
| 552 |
|
| 553 | for (i = 0; test_cases[i].name; i++)
|
| 554 | printf("%d\t%s\n", i, test_cases[i].name);
|
| 555 |
|
| 556 | exit(EXIT_FAILURE);
|
| 557 | }
|
| 558 |
|
| 559 | static unsigned long parse_test_id(const char *test_id_str, size_t test_cases_len)
|
| 560 | {
|
| 561 | unsigned long test_id;
|
| 562 | char *endptr = NULL;
|
| 563 |
|
| 564 | errno = 0;
|
| 565 | test_id = strtoul(test_id_str, &endptr, 10);
|
| 566 | if (errno || *endptr != '\0') {
|
| 567 | fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
|
| 568 | exit(EXIT_FAILURE);
|
| 569 | }
|
| 570 |
|
| 571 | if (test_id >= test_cases_len) {
|
| 572 | fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
|
| 573 | test_id, test_cases_len - 1);
|
| 574 | exit(EXIT_FAILURE);
|
| 575 | }
|
| 576 |
|
| 577 | return test_id;
|
| 578 | }
|
| 579 |
|
| 580 | void skip_test(struct test_case *test_cases, size_t test_cases_len,
|
| 581 | const char *test_id_str)
|
| 582 | {
|
| 583 | unsigned long test_id = parse_test_id(test_id_str, test_cases_len);
|
| 584 | test_cases[test_id].skip = true;
|
| 585 | }
|
| 586 |
|
| 587 | void pick_test(struct test_case *test_cases, size_t test_cases_len,
|
| 588 | const char *test_id_str)
|
| 589 | {
|
| 590 | static bool skip_all = true;
|
| 591 | unsigned long test_id;
|
| 592 |
|
| 593 | if (skip_all) {
|
| 594 | unsigned long i;
|
| 595 |
|
| 596 | for (i = 0; i < test_cases_len; ++i)
|
| 597 | test_cases[i].skip = true;
|
| 598 |
|
| 599 | skip_all = false;
|
| 600 | }
|
| 601 |
|
| 602 | test_id = parse_test_id(test_id_str, test_cases_len);
|
| 603 | test_cases[test_id].skip = false;
|
| 604 | }
|
| 605 |
|
| 606 | unsigned long hash_djb2(const void *data, size_t len)
|
| 607 | {
|
| 608 | unsigned long hash = 5381;
|
| 609 | int i = 0;
|
| 610 |
|
| 611 | while (i < len) {
|
| 612 | hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
|
| 613 | i++;
|
| 614 | }
|
| 615 |
|
| 616 | return hash;
|
| 617 | }
|
| 618 |
|
| 619 | size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
|
| 620 | {
|
| 621 | size_t bytes;
|
| 622 | int i;
|
| 623 |
|
| 624 | for (bytes = 0, i = 0; i < iovnum; i++)
|
| 625 | bytes += iov[i].iov_len;
|
| 626 |
|
| 627 | return bytes;
|
| 628 | }
|
| 629 |
|
| 630 | unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
|
| 631 | {
|
| 632 | unsigned long hash;
|
| 633 | size_t iov_bytes;
|
| 634 | size_t offs;
|
| 635 | void *tmp;
|
| 636 | int i;
|
| 637 |
|
| 638 | iov_bytes = iovec_bytes(iov, iovnum);
|
| 639 |
|
| 640 | tmp = malloc(iov_bytes);
|
| 641 | if (!tmp) {
|
| 642 | perror("malloc");
|
| 643 | exit(EXIT_FAILURE);
|
| 644 | }
|
| 645 |
|
| 646 | for (offs = 0, i = 0; i < iovnum; i++) {
|
| 647 | memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
|
| 648 | offs += iov[i].iov_len;
|
| 649 | }
|
| 650 |
|
| 651 | hash = hash_djb2(tmp, iov_bytes);
|
| 652 | free(tmp);
|
| 653 |
|
| 654 | return hash;
|
| 655 | }
|
| 656 |
|
| 657 | |
| 658 | * in the 'test_iovec'. For each element in the 'test_iovec' it
|
| 659 | * allocates new element in the resulting 'iovec'. 'iov_len'
|
| 660 | * of the new element is copied from 'test_iovec'. 'iov_base' is
|
| 661 | * allocated depending on the 'iov_base' of 'test_iovec':
|
| 662 | *
|
| 663 | * 'iov_base' == NULL -> valid buf: mmap('iov_len').
|
| 664 | *
|
| 665 | * 'iov_base' == MAP_FAILED -> invalid buf:
|
| 666 | * mmap('iov_len'), then munmap('iov_len').
|
| 667 | * 'iov_base' still contains result of
|
| 668 | * mmap().
|
| 669 | *
|
| 670 | * 'iov_base' == number -> unaligned valid buf:
|
| 671 | * mmap('iov_len') + number.
|
| 672 | *
|
| 673 | * 'iovnum' is number of elements in 'test_iovec'.
|
| 674 | *
|
| 675 | * Returns new 'iovec' or calls 'exit()' on error.
|
| 676 |
|
| 677 | struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
|
| 678 | {
|
| 679 | struct iovec *iovec;
|
| 680 | int i;
|
| 681 |
|
| 682 | iovec = malloc(sizeof(*iovec) * iovnum);
|
| 683 | if (!iovec) {
|
| 684 | perror("malloc");
|
| 685 | exit(EXIT_FAILURE);
|
| 686 | }
|
| 687 |
|
| 688 | for (i = 0; i < iovnum; i++) {
|
| 689 | iovec[i].iov_len = test_iovec[i].iov_len;
|
| 690 |
|
| 691 | iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
|
| 692 | PROT_READ | PROT_WRITE,
|
| 693 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
|
| 694 | -1, 0);
|
| 695 | if (iovec[i].iov_base == MAP_FAILED) {
|
| 696 | perror("mmap");
|
| 697 | exit(EXIT_FAILURE);
|
| 698 | }
|
| 699 |
|
| 700 | if (test_iovec[i].iov_base != MAP_FAILED)
|
| 701 | iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
|
| 702 | }
|
| 703 |
|
| 704 |
|
| 705 | for (i = 0; i < iovnum; i++) {
|
| 706 | if (test_iovec[i].iov_base == MAP_FAILED) {
|
| 707 | if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
|
| 708 | perror("munmap");
|
| 709 | exit(EXIT_FAILURE);
|
| 710 | }
|
| 711 | }
|
| 712 | }
|
| 713 |
|
| 714 | for (i = 0; i < iovnum; i++) {
|
| 715 | int j;
|
| 716 |
|
| 717 | if (test_iovec[i].iov_base == MAP_FAILED)
|
| 718 | continue;
|
| 719 |
|
| 720 | for (j = 0; j < iovec[i].iov_len; j++)
|
| 721 | ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
|
| 722 | }
|
| 723 |
|
| 724 | return iovec;
|
| 725 | }
|
| 726 |
|
| 727 | |
| 728 | * On error calls 'exit()'.
|
| 729 |
|
| 730 | void free_test_iovec(const struct iovec *test_iovec,
|
| 731 | struct iovec *iovec, int iovnum)
|
| 732 | {
|
| 733 | int i;
|
| 734 |
|
| 735 | for (i = 0; i < iovnum; i++) {
|
| 736 | if (test_iovec[i].iov_base != MAP_FAILED) {
|
| 737 | if (test_iovec[i].iov_base)
|
| 738 | iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
|
| 739 |
|
| 740 | if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
|
| 741 | perror("munmap");
|
| 742 | exit(EXIT_FAILURE);
|
| 743 | }
|
| 744 | }
|
| 745 | }
|
| 746 |
|
| 747 | free(iovec);
|
| 748 | }
|
| 749 |
|
| 750 |
|
| 751 | void setsockopt_ull_check(int fd, int level, int optname,
|
| 752 | unsigned long long val, char const *errmsg)
|
| 753 | {
|
| 754 | unsigned long long chkval;
|
| 755 | socklen_t chklen;
|
| 756 | int err;
|
| 757 |
|
| 758 | err = setsockopt(fd, level, optname, &val, sizeof(val));
|
| 759 | if (err) {
|
| 760 | fprintf(stderr, "setsockopt err: %s (%d)\n",
|
| 761 | strerror(errno), errno);
|
| 762 | goto fail;
|
| 763 | }
|
| 764 |
|
| 765 | chkval = ~val;
|
| 766 | chklen = sizeof(chkval);
|
| 767 |
|
| 768 | err = getsockopt(fd, level, optname, &chkval, &chklen);
|
| 769 | if (err) {
|
| 770 | fprintf(stderr, "getsockopt err: %s (%d)\n",
|
| 771 | strerror(errno), errno);
|
| 772 | goto fail;
|
| 773 | }
|
| 774 |
|
| 775 | if (chklen != sizeof(chkval)) {
|
| 776 | fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
|
| 777 | chklen);
|
| 778 | goto fail;
|
| 779 | }
|
| 780 |
|
| 781 | if (chkval != val) {
|
| 782 | fprintf(stderr, "value mismatch: set %llu got %llu\n", val,
|
| 783 | chkval);
|
| 784 | goto fail;
|
| 785 | }
|
| 786 | return;
|
| 787 | fail:
|
| 788 | fprintf(stderr, "%s val %llu\n", errmsg, val);
|
| 789 | exit(EXIT_FAILURE);
|
| 790 | }
|
| 791 |
|
| 792 |
|
| 793 | void setsockopt_int_check(int fd, int level, int optname, int val,
|
| 794 | char const *errmsg)
|
| 795 | {
|
| 796 | int chkval;
|
| 797 | socklen_t chklen;
|
| 798 | int err;
|
| 799 |
|
| 800 | err = setsockopt(fd, level, optname, &val, sizeof(val));
|
| 801 | if (err) {
|
| 802 | fprintf(stderr, "setsockopt err: %s (%d)\n",
|
| 803 | strerror(errno), errno);
|
| 804 | goto fail;
|
| 805 | }
|
| 806 |
|
| 807 | chkval = ~val;
|
| 808 | chklen = sizeof(chkval);
|
| 809 |
|
| 810 | err = getsockopt(fd, level, optname, &chkval, &chklen);
|
| 811 | if (err) {
|
| 812 | fprintf(stderr, "getsockopt err: %s (%d)\n",
|
| 813 | strerror(errno), errno);
|
| 814 | goto fail;
|
| 815 | }
|
| 816 |
|
| 817 | if (chklen != sizeof(chkval)) {
|
| 818 | fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
|
| 819 | chklen);
|
| 820 | goto fail;
|
| 821 | }
|
| 822 |
|
| 823 | if (chkval != val) {
|
| 824 | fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval);
|
| 825 | goto fail;
|
| 826 | }
|
| 827 | return;
|
| 828 | fail:
|
| 829 | fprintf(stderr, "%s val %d\n", errmsg, val);
|
| 830 | exit(EXIT_FAILURE);
|
| 831 | }
|
| 832 |
|
| 833 | static void mem_invert(unsigned char *mem, size_t size)
|
| 834 | {
|
| 835 | size_t i;
|
| 836 |
|
| 837 | for (i = 0; i < size; i++)
|
| 838 | mem[i] = ~mem[i];
|
| 839 | }
|
| 840 |
|
| 841 |
|
| 842 | void setsockopt_timeval_check(int fd, int level, int optname,
|
| 843 | struct timeval val, char const *errmsg)
|
| 844 | {
|
| 845 | struct timeval chkval;
|
| 846 | socklen_t chklen;
|
| 847 | int err;
|
| 848 |
|
| 849 | err = setsockopt(fd, level, optname, &val, sizeof(val));
|
| 850 | if (err) {
|
| 851 | fprintf(stderr, "setsockopt err: %s (%d)\n",
|
| 852 | strerror(errno), errno);
|
| 853 | goto fail;
|
| 854 | }
|
| 855 |
|
| 856 |
|
| 857 | chkval = val;
|
| 858 | mem_invert((unsigned char *)&chkval, sizeof(chkval));
|
| 859 | chklen = sizeof(chkval);
|
| 860 |
|
| 861 | err = getsockopt(fd, level, optname, &chkval, &chklen);
|
| 862 | if (err) {
|
| 863 | fprintf(stderr, "getsockopt err: %s (%d)\n",
|
| 864 | strerror(errno), errno);
|
| 865 | goto fail;
|
| 866 | }
|
| 867 |
|
| 868 | if (chklen != sizeof(chkval)) {
|
| 869 | fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
|
| 870 | chklen);
|
| 871 | goto fail;
|
| 872 | }
|
| 873 |
|
| 874 | if (memcmp(&chkval, &val, sizeof(val)) != 0) {
|
| 875 | fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n",
|
| 876 | val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec);
|
| 877 | goto fail;
|
| 878 | }
|
| 879 | return;
|
| 880 | fail:
|
| 881 | fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec);
|
| 882 | exit(EXIT_FAILURE);
|
| 883 | }
|
| 884 |
|
| 885 | void enable_so_zerocopy_check(int fd)
|
| 886 | {
|
| 887 | setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
|
| 888 | "setsockopt SO_ZEROCOPY");
|
| 889 | }
|
| 890 |
|
| 891 | void enable_so_linger(int fd, int timeout)
|
| 892 | {
|
| 893 | struct linger optval = {
|
| 894 | .l_onoff = 1,
|
| 895 | .l_linger = timeout
|
| 896 | };
|
| 897 |
|
| 898 | if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
|
| 899 | perror("setsockopt(SO_LINGER)");
|
| 900 | exit(EXIT_FAILURE);
|
| 901 | }
|
| 902 | }
|
| 903 |
|
| 904 | static int __get_transports(void)
|
| 905 | {
|
| 906 | char buf[KALLSYMS_LINE_LEN];
|
| 907 | const char *ksym;
|
| 908 | int ret = 0;
|
| 909 | FILE *f;
|
| 910 |
|
| 911 | f = fopen(KALLSYMS_PATH, "r");
|
| 912 | if (!f) {
|
| 913 | perror("Can't open " KALLSYMS_PATH);
|
| 914 | exit(EXIT_FAILURE);
|
| 915 | }
|
| 916 |
|
| 917 | while (fgets(buf, sizeof(buf), f)) {
|
| 918 | char *match;
|
| 919 | int i;
|
| 920 |
|
| 921 | assert(buf[strlen(buf) - 1] == '\n');
|
| 922 |
|
| 923 | for (i = 0; i < TRANSPORT_NUM; ++i) {
|
| 924 | if (ret & BIT(i))
|
| 925 | continue;
|
| 926 |
|
| 927 | |
| 928 | * See kallsyms.c:s_show().
|
| 929 |
|
| 930 | ksym = transport_ksyms[i];
|
| 931 | match = strstr(buf, ksym);
|
| 932 | if (match && isspace(match[strlen(ksym)])) {
|
| 933 | ret |= BIT(i);
|
| 934 | break;
|
| 935 | }
|
| 936 | }
|
| 937 | }
|
| 938 |
|
| 939 | fclose(f);
|
| 940 | return ret;
|
| 941 | }
|
| 942 |
|
| 943 | |
| 944 | * transport.
|
| 945 |
|
| 946 | int get_transports(void)
|
| 947 | {
|
| 948 | static int tr = -1;
|
| 949 |
|
| 950 | if (tr == -1)
|
| 951 | tr = __get_transports();
|
| 952 |
|
| 953 | return tr;
|
| 954 | }
|