1 /*
2  * Copyright (C) 2013-2021 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * This code is a complete clean re-write of the stress tool by
19  * Colin Ian King <colin.king@canonical.com> and attempts to be
20  * backwardly compatible with the stress tool by Amos Waterland
21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
22  * functionality.
23  *
24  */
25 #include "stress-ng.h"
26 
27 static const stress_help_t help[] = {
28 	{ NULL,	"ping-sock N",		"start N workers that exercises a ping socket" },
29 	{ NULL,	"ping-sock-ops N",	"stop after N ping sendto messages" },
30 	{ NULL,	NULL,			NULL }
31 };
32 
33 #if defined(PF_INET) &&		\
34     defined(SOCK_DGRAM) &&	\
35     defined(IPPROTO_ICMP) &&	\
36     defined(HAVE_ICMPHDR) &&	\
37     defined(__linux__)
38 
39 /*
40  *  stress_ping_sock
41  *	UDP flood
42  */
stress_ping_sock(const stress_args_t * args)43 static int stress_ping_sock(const stress_args_t *args)
44 {
45 	int fd, rc = EXIT_SUCCESS, j = 0;
46 	struct sockaddr_in addr;
47 	struct icmphdr *icmp_hdr;
48 	const size_t sz = 4;
49 	int rand_port;
50 	char ALIGN64 buf[sizeof(*icmp_hdr) + sz];
51 
52 	static const char data[64] =
53 		"0123456789ABCDEFGHIJKLMNOPQRSTUV"
54 		"WXYZabcdefghijklmnopqrstuvwxyz@!";
55 
56 	if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)) < 0) {
57 		if (errno == EPROTONOSUPPORT) {
58 			pr_inf_skip("%s: skipping stressor, protocol not supported\n",
59 				args->name);
60 			return EXIT_NOT_IMPLEMENTED;
61 		}
62 		if ((errno == EPERM) || (errno == EACCES)) {
63 			pr_inf_skip("%s: skipping stressor, permission denied\n",
64 				args->name);
65 			return EXIT_NOT_IMPLEMENTED;
66 		}
67 		pr_fail("%s: socket failed, errno=%d (%s)\n",
68 			args->name, errno, strerror(errno));
69 		return EXIT_FAILURE;
70 	}
71 
72 	(void)memset(&addr, 0, sizeof(addr));
73 	addr.sin_family = AF_INET;
74 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
75 
76 	icmp_hdr = (struct icmphdr *)buf;
77 	(void)memset(icmp_hdr, 0, sizeof(*icmp_hdr));
78 	icmp_hdr->type = ICMP_ECHO;
79 	icmp_hdr->un.echo.id = (uint16_t)getpid();	/* some unique ID */
80 	icmp_hdr->un.echo.sequence = 1;
81 
82 	rand_port = 1024 + (stress_mwc16() % (65535 - 1024));
83 
84 	stress_set_proc_state(args->name, STRESS_STATE_RUN);
85 
86 	do {
87 		(void)memset(buf + sizeof(*icmp_hdr), data[j++ & 63], sz);
88 		addr.sin_port = htons(rand_port);
89 
90 		if (sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)) > 0)
91 			inc_counter(args);
92 
93 		icmp_hdr->un.echo.sequence++;
94 		rand_port++;
95 		if (rand_port > 65535)
96 			rand_port = 0;
97 	} while (keep_stressing(args));
98 
99 	stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
100 
101 	(void)close(fd);
102 
103 	return rc;
104 }
105 
106 stressor_info_t stress_ping_sock_info = {
107 	.stressor = stress_ping_sock,
108 	.class = CLASS_NETWORK | CLASS_OS,
109 	.help = help
110 };
111 #else
112 stressor_info_t stress_ping_sock_info = {
113 	.stressor = stress_not_implemented,
114 	.class = CLASS_NETWORK | CLASS_OS,
115 	.help = help
116 };
117 #endif
118