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
|
/*
* 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"
#if defined(__linux__)
#define SOCKET_NODELAY
#endif
#if defined(SOCKET_NODELAY)
#include <netinet/tcp.h>
#endif
#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
#if defined(__linux__) && defined(__NR_sendmmsg) && NEED_GLIBC(2,14,0)
#define HAVE_SENDMMSG
#endif
#define SOCKET_OPT_SEND 0x01
#define SOCKET_OPT_SENDMSG 0x02
#define SOCKET_OPT_SENDMMSG 0x03
#define MSGVEC_SIZE (4)
typedef struct {
const char *optname;
int opt;
} socket_opts_t;
typedef struct {
const char *typename;
int type;
} socket_type_t;
static int opt_socket_domain = AF_INET;
static int opt_socket_port = DEFAULT_SOCKET_PORT;
static int opt_socket_opts = SOCKET_OPT_SEND;
static int opt_socket_type = SOCK_STREAM;
static const socket_opts_t socket_opts[] = {
{ "send", SOCKET_OPT_SEND },
{ "sendmsg", SOCKET_OPT_SENDMSG },
#if defined(HAVE_SENDMMSG)
{ "sendmmsg", SOCKET_OPT_SENDMMSG },
#endif
{ NULL, 0 }
};
static const socket_type_t socket_type[] = {
#if defined(SOCK_STREAM)
{ "stream", SOCK_STREAM },
#endif
#if defined(SOCK_SEQPACKET)
{ "seqpacket", SOCK_SEQPACKET },
#endif
{ NULL, 0 }
};
/*
* stress_set_socket_opts()
* parse --sock-opts
*/
int stress_set_socket_opts(const char *optarg)
{
int i;
for (i = 0; socket_opts[i].optname; i++) {
if (!strcmp(optarg, socket_opts[i].optname)) {
opt_socket_opts = socket_opts[i].opt;
return 0;
}
}
fprintf(stderr, "sock-opts option '%s' not known, options are:", optarg);
for (i = 0; socket_opts[i].optname; i++) {
fprintf(stderr, "%s %s",
i == 0 ? "" : ",", socket_opts[i].optname);
}
fprintf(stderr, "\n");
return -1;
}
/*
* stress_set_socket_type()
* parse --sock-type
*/
int stress_set_socket_type(const char *optarg)
{
int i;
for (i = 0; socket_type[i].typename; i++) {
if (!strcmp(optarg, socket_type[i].typename)) {
opt_socket_type = socket_type[i].type;
return 0;
}
}
fprintf(stderr, "sock-type option '%s' not known, options are:", optarg);
for (i = 0; socket_type[i].typename; i++) {
fprintf(stderr, "%s %s",
i == 0 ? "" : ",", socket_type[i].typename);
}
fprintf(stderr, "\n");
return -1;
}
/*
* stress_set_socket_port()
* set port to use
*/
void stress_set_socket_port(const char *optarg)
{
stress_set_net_port("sock-port", optarg,
MIN_SOCKET_PORT, MAX_SOCKET_PORT - STRESS_PROCS_MAX,
&opt_socket_port);
}
/*
* stress_set_socket_domain()
* set the socket domain option
*/
int stress_set_socket_domain(const char *name)
{
return stress_set_net_domain(DOMAIN_ALL, "sock-domain",
name, &opt_socket_domain);
}
/*
* stress_sctp_client()
* client reader
*/
static void stress_sctp_client(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t ppid)
{
struct sockaddr *addr;
(void)setpgid(0, pgrp);
stress_parent_died_alarm();
do {
char buf[SOCKET_BUF];
int fd;
int retries = 0;
socklen_t addr_len = 0;
retry:
if (!opt_do_run) {
(void)kill(getppid(), SIGALRM);
exit(EXIT_FAILURE);
}
if ((fd = socket(opt_socket_domain, opt_socket_type, 0)) < 0) {
pr_fail_dbg(name, "socket");
/* failed, kick parent to finish */
(void)kill(getppid(), SIGALRM);
exit(EXIT_FAILURE);
}
stress_set_sockaddr(name, instance, ppid,
opt_socket_domain, opt_socket_port,
&addr, &addr_len, NET_ADDR_ANY);
if (connect(fd, addr, addr_len) < 0) {
(void)close(fd);
(void)shim_usleep(10000);
retries++;
if (retries > 100) {
/* Give up.. */
pr_fail_dbg(name, "connect");
(void)kill(getppid(), SIGALRM);
exit(EXIT_FAILURE);
}
goto retry;
}
do {
ssize_t n = recv(fd, buf, sizeof(buf), 0);
if (n == 0)
break;
if (n < 0) {
if (errno != EINTR)
pr_fail_dbg(name, "recv");
break;
}
} while (opt_do_run && (!max_ops || *counter < max_ops));
(void)shutdown(fd, SHUT_RDWR);
(void)close(fd);
} while (opt_do_run && (!max_ops || *counter < max_ops));
#if defined(AF_UNIX)
if (opt_socket_domain == AF_UNIX) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
/* Inform parent we're all done */
(void)kill(getppid(), SIGALRM);
}
/*
* handle_socket_sigalrm()
* catch SIGALRM
* stress_sctp_client()
* client reader
*/
static void MLOCKED handle_socket_sigalrm(int dummy)
{
(void)dummy;
opt_do_run = false;
}
/*
* stress_sctp_server()
* server writer
*/
static int stress_sctp_server(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name,
const pid_t pid,
const pid_t ppid)
{
char buf[SOCKET_BUF];
int fd, status;
int so_reuseaddr = 1;
socklen_t addr_len = 0;
struct sockaddr *addr;
uint64_t msgs = 0;
int rc = EXIT_SUCCESS;
(void)setpgid(pid, pgrp);
if (stress_sighandler(name, SIGALRM, handle_socket_sigalrm, NULL) < 0) {
rc = EXIT_FAILURE;
goto die;
}
if ((fd = socket(opt_socket_domain, opt_socket_type, 0)) < 0) {
rc = exit_status(errno);
pr_fail_dbg(name, "socket");
goto die;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
&so_reuseaddr, sizeof(so_reuseaddr)) < 0) {
pr_fail_dbg(name, "setsockopt");
rc = EXIT_FAILURE;
goto die_close;
}
stress_set_sockaddr(name, instance, ppid,
opt_socket_domain, opt_socket_port,
&addr, &addr_len, NET_ADDR_ANY);
if (bind(fd, addr, addr_len) < 0) {
rc = exit_status(errno);
pr_fail_dbg(name, "bind");
goto die_close;
}
if (listen(fd, 10) < 0) {
pr_fail_dbg(name, "listen");
rc = EXIT_FAILURE;
goto die_close;
}
do {
int sfd = accept(fd, (struct sockaddr *)NULL, NULL);
if (sfd >= 0) {
size_t i, j;
struct sockaddr saddr;
socklen_t len;
int sndbuf;
struct msghdr msg;
struct iovec vec[sizeof(buf)/16];
#if defined(HAVE_SENDMMSG)
struct mmsghdr msgvec[MSGVEC_SIZE];
unsigned int msg_len = 0;
#endif
#if defined(SOCKET_NODELAY)
int one = 1;
#endif
len = sizeof(saddr);
if (getsockname(fd, &saddr, &len) < 0) {
pr_fail_dbg(name, "getsockname");
(void)close(sfd);
break;
}
len = sizeof(sndbuf);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &len) < 0) {
pr_fail_dbg(name, "getsockopt");
(void)close(sfd);
break;
}
#if defined(SOCKET_NODELAY)
if (opt_flags & OPT_FLAGS_SOCKET_NODELAY) {
if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one)) < 0) {
pr_inf(stdout, "%s: setsockopt TCP_NODELAY "
"failed and disabled, errno=%d (%s)\n",
name, errno, strerror(errno));
opt_flags &= ~OPT_FLAGS_SOCKET_NODELAY;
}
}
#endif
memset(buf, 'A' + (*counter % 26), sizeof(buf));
switch (opt_socket_opts) {
case SOCKET_OPT_SEND:
for (i = 16; i < sizeof(buf); i += 16) {
ssize_t ret = send(sfd, buf, i, 0);
if (ret < 0) {
if (errno != EINTR)
pr_fail_dbg(name, "send");
break;
} else
msgs++;
}
break;
case SOCKET_OPT_SENDMSG:
for (j = 0, i = 16; i < sizeof(buf); i += 16, j++) {
vec[j].iov_base = buf;
vec[j].iov_len = i;
}
memset(&msg, 0, sizeof(msg));
msg.msg_iov = vec;
msg.msg_iovlen = j;
if (sendmsg(sfd, &msg, 0) < 0) {
if (errno != EINTR)
pr_fail_dbg(name, "sendmsg");
} else
msgs += j;
break;
#if defined(HAVE_SENDMMSG)
case SOCKET_OPT_SENDMMSG:
memset(msgvec, 0, sizeof(msgvec));
for (j = 0, i = 16; i < sizeof(buf); i += 16, j++) {
vec[j].iov_base = buf;
vec[j].iov_len = i;
msg_len += i;
}
for (i = 0; i < MSGVEC_SIZE; i++) {
msgvec[i].msg_hdr.msg_iov = vec;
msgvec[i].msg_hdr.msg_iovlen = j;
}
if (sendmmsg(sfd, msgvec, MSGVEC_SIZE, 0) < 0) {
if (errno != EINTR)
pr_fail_dbg(name, "sendmmsg");
} else
msgs += (MSGVEC_SIZE * j);
break;
#endif
default:
/* Should never happen */
pr_err(stderr, "%s: bad option %d\n", name, opt_socket_opts);
(void)close(sfd);
goto die_close;
}
if (getpeername(sfd, &saddr, &len) < 0) {
pr_fail_dbg(name, "getpeername");
}
(void)close(sfd);
}
(*counter)++;
} while (opt_do_run && (!max_ops || *counter < max_ops));
die_close:
(void)close(fd);
die:
#if defined(AF_UNIX)
if (opt_socket_domain == AF_UNIX) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
if (pid) {
(void)kill(pid, SIGKILL);
(void)waitpid(pid, &status, 0);
}
pr_dbg(stderr, "%s: %" PRIu64 " messages sent\n", name, msgs);
return rc;
}
/*
* stress_sock
* stress by heavy socket I/O
*/
int stress_sock(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
pid_t pid, ppid = getppid();
pr_dbg(stderr, "%s: process [%d] using socket port %d\n",
name, (int)getpid(), opt_socket_port + instance);
again:
pid = fork();
if (pid < 0) {
if (opt_do_run && (errno == EAGAIN))
goto again;
pr_fail_dbg(name, "fork");
return EXIT_FAILURE;
} else if (pid == 0) {
stress_sctp_client(counter, instance, max_ops, name, ppid);
exit(EXIT_SUCCESS);
} else {
return stress_sctp_server(counter, instance, max_ops, name, pid, ppid);
}
}
|