forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
203 lines (192 loc) · 4.61 KB
/
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
/*
* 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 <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.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
typedef struct {
const char *name;
const int domain;
const int domain_flags;
} domain_t;
static const domain_t domains[] = {
{ "ipv4", AF_INET, DOMAIN_INET },
{ "ipv6", AF_INET6, DOMAIN_INET6 },
{ "unix", AF_UNIX, DOMAIN_UNIX },
};
/*
* 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)
{
*port = get_uint64(opt);
check_range(optname, *port,
min_port, max_port - STRESS_PROCS_MAX);
}
/*
* 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(
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)
{
switch (domain) {
#if defined(AF_INET)
case AF_INET: {
static struct sockaddr_in addr;
(void)memset(&addr, 0, sizeof(addr));
addr.sin_family = domain;
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_port = htons(port + instance);
*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));
addr.sin6_family = domain;
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_port = htons(port + instance);
*sockaddr = (struct sockaddr *)&addr;
*len = sizeof(addr);
break;
}
#endif
#if defined(AF_UNIX)
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);
}
}
/*
* 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;
}
}