forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore-net.c
296 lines (267 loc) · 6.34 KB
/
core-net.c
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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "core-net.h"
#if defined(HAVE_SYS_UN_H)
#include <sys/un.h>
#else
UNEXPECTED
#endif
#include <netinet/in.h>
#if defined(HAVE_IFADDRS_H)
#include <ifaddrs.h>
#endif
#if defined(HAVE_NET_IF_H)
#include <net/if.h>
#endif
typedef struct {
const char *name;
const int domain;
const int domain_flags;
} stress_domain_t;
static const stress_domain_t domains[] = {
{ "ipv4", AF_INET, DOMAIN_INET },
{ "ipv6", AF_INET6, DOMAIN_INET6 },
{ "unix", AF_UNIX, DOMAIN_UNIX },
};
/*
* stress_net_interface_exists()
* check if interface exists, returns -1 if failed / not found
* 0 if interface exists
*/
int stress_net_interface_exists(const char *interface, const int domain, struct sockaddr *addr)
{
#if defined(HAVE_IFADDRS_H)
struct ifaddrs *ifaddr, *ifa;
int ret = -1;
if (!interface)
return -1;
if (!addr)
return -1;
if (getifaddrs(&ifaddr) < 0)
return -1;
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
if (!ifa->ifa_name)
continue;
if (ifa->ifa_addr->sa_family != domain)
continue;
if (strcmp(ifa->ifa_name, interface) == 0) {
(void)memcpy(addr, ifa->ifa_addr, sizeof(*addr));
ret = 0;
break;
}
}
freeifaddrs(ifaddr);
return ret;
#else
return -1;
#endif
}
/*
* stress_set_net_port()
* set up port number from opt
*/
void stress_set_net_port(
const char *optname,
const char *opt,
const int min_port,
const int max_port,
int *port)
{
const uint64_t val = stress_get_uint64(opt);
stress_check_range(optname, val,
(uint64_t)min_port,
(uint64_t)(max_port - STRESS_PROCS_MAX));
*port = (int)val;
}
/*
* stress_net_domain()
* return human readable domain name from domain number
*/
const char *stress_net_domain(const int domain)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(domains); i++) {
if (domains[i].domain == domain)
return domains[i].name;
}
return "unknown";
}
/*
* stress_set_net_domain()
* set the domain option
*/
int stress_set_net_domain(
const int domain_mask,
const char *name,
const char *domain_name,
int *domain)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(domains); i++) {
if ((domain_mask & domains[i].domain_flags) &&
!strcmp(domain_name, domains[i].name)) {
*domain = domains[i].domain;
return 0;
}
}
(void)fprintf(stderr, "%s: domain must be one of:", name);
for (i = 0; i < SIZEOF_ARRAY(domains); i++)
if (domain_mask & domains[i].domain_flags)
(void)fprintf(stderr, " %s", domains[i].name);
(void)fprintf(stderr, "\n");
*domain = 0;
return -1;
}
/*
* setup socket address
*/
void stress_set_sockaddr_if(
const char *name,
const uint32_t instance,
const pid_t ppid,
const int domain,
const int port,
const char *ifname,
struct sockaddr **sockaddr,
socklen_t *len,
const int net_addr)
{
(void)ppid;
uint16_t sin_port = (uint16_t)port + (uint16_t)instance;
*sockaddr = NULL;
*len = 0;
/* Handle overflow to omit ports 0..1023 */
if ((port > 1024) && (sin_port < 1024))
sin_port += 1024;
switch (domain) {
#if defined(AF_INET)
case AF_INET: {
static struct sockaddr_in addr;
(void)memset(&addr, 0, sizeof(addr));
if ((!ifname) || (!stress_net_interface_exists(ifname, domain, (struct sockaddr *)&addr))) {
switch (net_addr) {
case NET_ADDR_LOOPBACK:
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
break;
case NET_ADDR_ANY:
default:
addr.sin_addr.s_addr = htonl(INADDR_ANY);
break;
}
}
addr.sin_family = (sa_family_t)domain;
addr.sin_port = htons(sin_port);
*sockaddr = (struct sockaddr *)&addr;
*len = sizeof(addr);
break;
}
#endif
#if defined(AF_INET6)
case AF_INET6: {
static struct sockaddr_in6 addr;
#if defined(__minix__)
static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
static const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
#endif
(void)memset(&addr, 0, sizeof(addr));
if ((!ifname) || (!stress_net_interface_exists(ifname, domain, (struct sockaddr *)&addr))) {
switch (net_addr) {
case NET_ADDR_LOOPBACK:
addr.sin6_addr = in6addr_loopback;
break;
case NET_ADDR_ANY:
default:
addr.sin6_addr = in6addr_any;
break;
}
}
addr.sin6_family = (sa_family_t)domain;
addr.sin6_port = htons(sin_port);
*sockaddr = (struct sockaddr *)&addr;
*len = sizeof(addr);
break;
}
#endif
#if defined(AF_UNIX) && \
defined(HAVE_SOCKADDR_UN)
case AF_UNIX: {
static struct sockaddr_un addr;
(void)memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
(void)snprintf(addr.sun_path, sizeof(addr.sun_path),
"/tmp/stress-ng-%d-%" PRIu32,
(int)ppid, instance);
*sockaddr = (struct sockaddr *)&addr;
*len = sizeof(addr);
break;
}
#endif
default:
pr_fail("%s: unknown domain %d\n", name, domain);
(void)kill(getppid(), SIGALRM);
_exit(EXIT_FAILURE);
}
}
void stress_set_sockaddr(
const char *name,
const uint32_t instance,
const pid_t ppid,
const int domain,
const int port,
struct sockaddr **sockaddr,
socklen_t *len,
const int net_addr)
{
stress_set_sockaddr_if(name, instance, ppid, domain, port, NULL, sockaddr, len, net_addr);
}
/*
* setup just the socket address port
*/
void HOT stress_set_sockaddr_port(
const int domain,
const int port,
struct sockaddr *sockaddr)
{
switch (domain) {
#if defined(AF_INET)
case AF_INET: {
struct sockaddr_in *addr = (struct sockaddr_in *)sockaddr;
addr->sin_port = htons(port);
break;
}
#endif
#if defined(AF_INET6)
case AF_INET6: {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)sockaddr;
addr->sin6_port = htons(port);
break;
}
#endif
#if defined(AF_UNIX)
case AF_UNIX:
break;
#endif
default:
break;
}
}