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
|
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#define MAX_EPOLL_EVENTS (1024)
#define MAX_SERVERS (4)
#if defined(HAVE_LIB_RT) && defined(__linux__) && NEED_GLIBC(2,3,2)
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(AF_INET6)
#include <netinet/in.h>
#endif
#if defined(AF_UNIX)
#include <sys/un.h>
#endif
#include <netdb.h>
typedef void (epoll_func_t)(
const int child,
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t ppid);
static timer_t epoll_timerid;
#endif
static int max_servers = 1;
static int opt_epoll_domain = AF_UNIX;
static int opt_epoll_port = DEFAULT_EPOLL_PORT;
/*
* stress_set_epoll_port()
* set the default port base
*/
void stress_set_epoll_port(const char *optarg)
{
stress_set_net_port("epoll-port", optarg,
MIN_EPOLL_PORT,
MAX_EPOLL_PORT - (STRESS_PROCS_MAX * MAX_SERVERS),
&opt_epoll_port);
}
/*
* stress_set_epoll_domain()
* set the socket domain option
*/
int stress_set_epoll_domain(const char *name)
{
int ret;
ret = stress_set_net_domain(DOMAIN_ALL, "epoll-domain",
name, &opt_epoll_domain);
switch (opt_epoll_domain) {
case AF_INET:
case AF_INET6:
max_servers = 4;
break;
case AF_UNIX:
default:
max_servers = 1;
}
return ret;
}
#if defined(HAVE_LIB_RT) && defined(__linux__) && NEED_GLIBC(2,3,2)
/*
* epoll_timer_handler()
* catch timer signal and cancel if no more runs flagged
*/
static void MLOCKED epoll_timer_handler(int sig)
{
(void)sig;
/* Cancel timer if we detect no more runs */
if (!opt_do_run) {
struct itimerspec timer;
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = timer.it_value.tv_sec;
timer.it_interval.tv_nsec = timer.it_value.tv_nsec;
(void)timer_settime(epoll_timerid, 0, &timer, NULL);
}
}
/*
* handle_socket_sigalrm()
* catch SIGALRM
*/
static MLOCKED void handle_socket_sigalrm(int dummy)
{
(void)dummy;
opt_do_run = false;
}
/*
* epoll_spawn()
* spawn a process
*/
static pid_t epoll_spawn(
epoll_func_t func,
const int child,
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t ppid)
{
pid_t pid;
again:
pid = fork();
if (pid < 0) {
if (opt_do_run && (errno == EAGAIN))
goto again;
return -1;
}
if (pid == 0) {
(void)setpgid(0, pgrp);
stress_parent_died_alarm();
func(child, counter, instance, max_ops, name, ppid);
exit(EXIT_SUCCESS);
}
(void)setpgid(pid, pgrp);
return pid;
}
/*
* epoll_set_fd_nonblock()
* set non-blocking mode on fd
*/
static int epoll_set_fd_nonblock(const int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
return -1;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
return -1;
return 0;
}
/*
* epoll_recv_data()
* receive data on fd
*/
static void epoll_recv_data(const int fd)
{
while (opt_do_run) {
char buf[8192];
ssize_t n;
n = recv(fd, buf, sizeof(buf), 0);
if (n == -1) {
if (errno != EAGAIN)
(void)close(fd);
break;
} else if (n == 0) {
(void)close(fd);
break;
}
}
}
/*
* epoll_ctl_add()
* add fd to epoll list
*/
static int epoll_ctl_add(const int efd, const int fd)
{
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &event) < 0)
return -1;
return 0;
}
/*
* epoll_notification()
* handle accept notification on sfd, add
* fd's to epoll event list
*/
static int epoll_notification(
const char *name,
const int efd,
const int sfd)
{
for (;;) {
struct sockaddr saddr;
socklen_t slen = sizeof(saddr);
int fd;
if ((fd = accept(sfd, &saddr, &slen)) < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
/* all incoming connections handled so finish */
return 0;
}
if ((errno == EMFILE) || (errno == ENFILE)) {
/* out of file descriptors! */
return 0;
}
pr_fail_err(name, "accept");
return -1;
}
/*
* Add non-blocking fd to epoll event list
*/
if (epoll_set_fd_nonblock(fd) < 0) {
pr_fail_err(name, "setting socket to non-blocking");
(void)close(fd);
return -1;
}
if (epoll_ctl_add(efd, fd) < 0) {
pr_fail_err(name, "epoll ctl add");
(void)close(fd);
return -1;
}
}
return 0;
}
/*
* epoll_client()
* rapidly try to connect to server(s) and
* send a relatively short message
*/
static int epoll_client(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t ppid)
{
int port_counter = 0;
uint64_t connect_timeouts = 0;
struct sigevent sev;
struct itimerspec timer;
struct sockaddr *addr = NULL;
if (stress_sighandler(name, SIGRTMIN, epoll_timer_handler, NULL) < 0)
return -1;
do {
char buf[4096];
int fd, saved_errno;
int retries = 0;
int ret = -1;
int port = opt_epoll_port + port_counter +
(max_servers * instance);
socklen_t addr_len = 0;
/* Cycle through the servers */
port_counter = (port_counter + 1) % max_servers;
retry:
if (!opt_do_run)
break;
if ((fd = socket(opt_epoll_domain, SOCK_STREAM, 0)) < 0) {
pr_fail_dbg(name, "socket");
return -1;
}
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &epoll_timerid;
if (timer_create(CLOCK_REALTIME, &sev, &epoll_timerid) < 0) {
pr_fail_err(name, "timer_create");
(void)close(fd);
return -1;
}
/*
* Allow 1 second for connection to occur,
* connect can block if the connection table
* fills up because we're waiting for TIME-OUTs
* to occur on previously closed connections
*/
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 250000000;
timer.it_interval.tv_sec = timer.it_value.tv_sec;
timer.it_interval.tv_nsec = timer.it_value.tv_nsec;
if (timer_settime(epoll_timerid, 0, &timer, NULL) < 0) {
pr_fail_err(name, "timer_settime");
(void)close(fd);
return -1;
}
stress_set_sockaddr(name, instance, ppid,
opt_epoll_domain, port, &addr, &addr_len, NET_ADDR_ANY);
errno = 0;
ret = connect(fd, addr, addr_len);
saved_errno = errno;
/* No longer need timer */
if (timer_delete(epoll_timerid) < 0) {
pr_fail_err(name, "timer_delete");
(void)close(fd);
return -1;
}
if (ret < 0) {
switch (saved_errno) {
case EINTR:
connect_timeouts++;
break;
case ECONNREFUSED: /* No servers yet running */
case ENOENT: /* unix domain not yet created */
break;
default:
pr_dbg(stderr, "%s: connect failed: %d (%s)\n",
name, saved_errno, strerror(saved_errno));
break;
}
(void)close(fd);
(void)shim_usleep(100000); /* Twiddle fingers for a moment */
retries++;
if (retries > 1000) {
/* Sigh, give up.. */
errno = saved_errno;
pr_fail_dbg(name, "too many connects");
return -1;
}
goto retry;
}
memset(buf, 'A' + (*counter % 26), sizeof(buf));
if (send(fd, buf, sizeof(buf), 0) < 0) {
(void)close(fd);
pr_fail_dbg(name, "send");
break;
}
(void)close(fd);
(void)shim_sched_yield();
(*counter)++;
} while (opt_do_run && (!max_ops || *counter < max_ops));
#if defined(AF_UNIX)
if (addr && (opt_epoll_domain == AF_UNIX)) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
if (connect_timeouts)
pr_dbg(stderr, "%s: %" PRIu64 " x 0.25 second "
"connect timeouts, connection table full "
"(instance %" PRIu32 ")\n",
name, connect_timeouts, instance);
return EXIT_SUCCESS;
}
/*
* epoll_server()
* wait on connections and read data
*/
static void epoll_server(
const int child,
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t ppid)
{
int efd = -1, sfd = -1, rc = EXIT_SUCCESS;
int so_reuseaddr = 1;
int port = opt_epoll_port + child + (max_servers * instance);
struct epoll_event *events = NULL;
struct sockaddr *addr = NULL;
socklen_t addr_len = 0;
if (stress_sighandler(name, SIGALRM, handle_socket_sigalrm, NULL) < 0) {
rc = EXIT_FAILURE;
goto die;
}
if ((sfd = socket(opt_epoll_domain, SOCK_STREAM, 0)) < 0) {
pr_fail_err(name, "socket");
rc = EXIT_FAILURE;
goto die;
}
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
&so_reuseaddr, sizeof(so_reuseaddr)) < 0) {
pr_fail_err(name, "setsockopt");
rc = EXIT_FAILURE;
goto die_close;
}
stress_set_sockaddr(name, instance, ppid,
opt_epoll_domain, port, &addr, &addr_len, NET_ADDR_ANY);
if (bind(sfd, addr, addr_len) < 0) {
pr_fail_err(name, "bind");
rc = EXIT_FAILURE;
goto die_close;
}
if (epoll_set_fd_nonblock(sfd) < 0) {
pr_fail_err(name, "setting socket to non-blocking");
rc = EXIT_FAILURE;
goto die_close;
}
if (listen(sfd, SOMAXCONN) < 0) {
pr_fail_err(name, "listen");
rc = EXIT_FAILURE;
goto die_close;
}
if ((efd = epoll_create1(0)) < 0) {
pr_fail_err(name, "epoll_create1");
rc = EXIT_FAILURE;
goto die_close;
}
if (epoll_ctl_add(efd, sfd) < 0) {
pr_fail_err(name, "epoll ctl add");
rc = EXIT_FAILURE;
goto die_close;
}
if ((events = calloc(MAX_EPOLL_EVENTS,
sizeof(struct epoll_event))) == NULL) {
pr_fail_err(name, "epoll ctl add");
rc = EXIT_FAILURE;
goto die_close;
}
do {
int n, i;
memset(events, 0, MAX_EPOLL_EVENTS * sizeof(struct epoll_event));
errno = 0;
/*
* Wait for 100ms for an event, allowing us to
* to break out if opt_do_run has been changed
*/
n = epoll_wait(efd, events, MAX_EPOLL_EVENTS, 100);
if (n < 0) {
if (errno != EINTR) {
pr_fail_err(name, "epoll_wait");
rc = EXIT_FAILURE;
goto die_close;
}
break;
}
for (i = 0; i < n; i++) {
if ((events[i].events & EPOLLERR) ||
(events[i].events & EPOLLHUP) ||
(!(events[i].events & EPOLLIN))) {
/*
* Error has occurred or fd is not
* for reading anymore.. so reap fd
*/
(void)close(events[i].data.fd);
} else if (sfd == events[i].data.fd) {
/*
* The listening socket has notification(s)
* pending, so handle incoming connections
*/
if (epoll_notification(name, efd, sfd) < 0)
break;
} else {
/*
* The fd has data available, so read it
*/
epoll_recv_data(events[i].data.fd);
}
}
} while (opt_do_run && (!max_ops || *counter < max_ops));
die_close:
if (efd != -1)
(void)close(efd);
if (sfd != -1)
(void)close(sfd);
die:
#if defined(AF_UNIX)
if (addr && (opt_epoll_domain == AF_UNIX)) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
free(events);
exit(rc);
}
/*
* stress_epoll
* stress by heavy socket I/O
*/
int stress_epoll(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
pid_t pids[MAX_SERVERS], ppid = getppid();
int i, rc = EXIT_SUCCESS;
if (max_servers == 1) {
pr_dbg(stderr, "%s: process [%d] using socket port %d\n",
name, getpid(),
opt_epoll_port + instance);
} else {
pr_dbg(stderr, "%s: process [%d] using socket ports %d..%d\n",
name, getpid(),
opt_epoll_port + (max_servers * instance),
opt_epoll_port + (max_servers * (instance + 1)) - 1);
}
/*
* Spawn off servers to handle multi port connections.
* The (src address, src port, dst address, dst port) tuple
* is kept in the connection table for a default of 60 seconds
* which means for many fast short connections we can
* fill this table up and new connections get blocked until
* this table empties. One strategy is to reduce TIME_WAIT (not
* good) so the easiest way forward is to just increase the
* number of ports being listened to to increase the tuple
* range and hence allow more connections. See
* http://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux.html
* Typically, we are limited to ~500 connections per second
* on a default Linux configuration.
*/
memset(pids, 0, sizeof(pids));
for (i = 0; i < max_servers; i++) {
pids[i] = epoll_spawn(epoll_server, i,
counter, instance, max_ops, name, ppid);
if (pids[i] < 0) {
pr_fail_dbg(name, "fork");
goto reap;
}
}
epoll_client(counter, instance, max_ops, name, ppid);
reap:
for (i = 0; i < max_servers; i++) {
int status;
if (pids[i] > 0) {
(void)kill(pids[i], SIGKILL);
if (waitpid(pids[i], &status, 0) < 0) {
pr_fail_dbg(name, "waitpid");
}
}
}
return rc;
}
#else
int stress_epoll(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
return stress_not_implemented(counter, instance, max_ops, name);
}
#endif
|