~vmkononenko/+junk/uperf

« back to all changes in this revision

Viewing changes to src/generic.c

  • Committer: Volodymyr Kononenko
  • Date: 2017-05-02 11:55:13 UTC
  • Revision ID: vmkononenko@gmail.com-20170502115513-0xg7uvymc02nu0sp
Tags: upstream-1.0.5
ImportĀ upstreamĀ versionĀ 1.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Sun Microsystems
 
3
 *
 
4
 * This file is part of uperf.
 
5
 *
 
6
 * uperf is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License version 3
 
8
 * as published by the Free Software Foundation.
 
9
 *
 
10
 * uperf is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with uperf.  If not, see http://www.gnu.org/licenses/.
 
17
 */
 
18
 
 
19
/*
 
20
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved. Use is
 
21
 * subject to license terms.
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include "../config.h"
 
26
#endif /* HAVE_CONFIG_H */
 
27
#ifdef HAVE_STRING_H
 
28
#include <string.h>
 
29
#endif /* HAVE_STRING_H */
 
30
 
 
31
#include <strings.h>
 
32
#include <errno.h>
 
33
 
 
34
#include <stdio.h>
 
35
#include <stdlib.h>
 
36
#include <sys/types.h>
 
37
#include <sys/socket.h>
 
38
#include <netdb.h>
 
39
#include <netinet/in.h>
 
40
#include <netinet/tcp.h>
 
41
#include <arpa/inet.h>
 
42
#include <unistd.h>
 
43
#include <assert.h>
 
44
#include <sys/types.h>
 
45
#include <unistd.h>
 
46
#include <fcntl.h>
 
47
#include <sys/socket.h>
 
48
#include <arpa/inet.h>
 
49
 
 
50
#ifdef  HAVE_SYS_POLL_H
 
51
#include <sys/poll.h>
 
52
#endif /*  HAVE_SYS_POLL_H */
 
53
 
 
54
#include "logging.h"
 
55
#include "uperf.h"
 
56
#include "flowops.h"
 
57
#include "workorder.h"
 
58
#include "protocol.h"
 
59
 
 
60
#define USE_POLL_ACCEPT 1
 
61
#define LISTENQ         10240   /* 2nd argument to listen() */
 
62
#define TIMEOUT         1200000 /* Argument to poll */
 
63
 
 
64
int
 
65
name_to_addr(const char *address, struct sockaddr_storage *saddr)
 
66
{
 
67
        struct addrinfo *res, *res0, hints;
 
68
        int error;
 
69
 
 
70
        memset(&hints, 0, sizeof(hints));
 
71
        hints.ai_family = PF_UNSPEC;
 
72
        hints.ai_socktype = SOCK_STREAM;
 
73
        if ((error = getaddrinfo(address, NULL, NULL, &res0)) == 0) {
 
74
                for (res = res0; res; res = res->ai_next) {
 
75
                        if ((res->ai_addr->sa_family != AF_INET) &&
 
76
                            (res->ai_addr->sa_family != AF_INET6)) {
 
77
                                continue;
 
78
                        }
 
79
                        memcpy(saddr, res->ai_addr, res->ai_addrlen);
 
80
                        break;
 
81
                }
 
82
                freeaddrinfo(res0);
 
83
                if (res != NULL) {
 
84
                        return (UPERF_SUCCESS);
 
85
                } else {
 
86
                        return (UPERF_FAILURE);
 
87
                }
 
88
        } else {
 
89
                ulog_err("name_to_addr(%s): %s\n", address, gai_strerror(error));
 
90
                return (UPERF_FAILURE);
 
91
        }
 
92
}
 
93
 
 
94
int
 
95
generic_socket(protocol_t *p, int domain, int protocol)
 
96
{
 
97
        if ((p->fd = socket(domain, SOCK_STREAM, protocol)) < 0) {
 
98
                ulog_err("%s: Cannot create socket", protocol_to_str(p->type));
 
99
                return (UPERF_FAILURE);
 
100
        }
 
101
        return (UPERF_SUCCESS);
 
102
}
 
103
 
 
104
/* ARGSUSED */
 
105
int
 
106
generic_connect(protocol_t *p, int protocol)
 
107
{
 
108
        struct sockaddr_storage serv;
 
109
        socklen_t len;
 
110
        const int off = 0;
 
111
 
 
112
        uperf_debug("Connecting to %s:%d\n", p->host, p->port);
 
113
 
 
114
        (void) memset(&serv, 0, sizeof(struct sockaddr_storage));
 
115
        if (name_to_addr(p->host, &serv)) {
 
116
                /* Error is already reported by name_to_addr, so just return */
 
117
                return (UPERF_FAILURE);
 
118
        }
 
119
 
 
120
        if (generic_socket(p, serv.ss_family, protocol) < 0) {
 
121
                return (UPERF_FAILURE);
 
122
        }
 
123
        switch (serv.ss_family) {
 
124
        case AF_INET:
 
125
                ((struct sockaddr_in *)&serv)->sin_port = htons(p->port);
 
126
                len = (socklen_t)sizeof(struct sockaddr_in);
 
127
                break;
 
128
        case AF_INET6:
 
129
                ((struct sockaddr_in6 *)&serv)->sin6_port = htons(p->port);
 
130
                len = (socklen_t)sizeof(struct sockaddr_in6);
 
131
                if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int)) < 0) {
 
132
                        return (UPERF_FAILURE);
 
133
                }
 
134
                break;
 
135
        default:
 
136
                uperf_debug("Unsupported protocol family: %d\n", serv.ss_family);
 
137
                return (UPERF_FAILURE);
 
138
                break;
 
139
        }
 
140
        if (connect(p->fd, (const struct sockaddr *)&serv, len) < 0) {
 
141
                ulog_err("%s: Cannot connect to %s:%d",
 
142
                         protocol_to_str(p->type), p->host, p->port);
 
143
                return (UPERF_FAILURE);
 
144
        }
 
145
 
 
146
        return (UPERF_SUCCESS);
 
147
}
 
148
 
 
149
int
 
150
generic_setfd_nonblock(int fd)
 
151
{
 
152
        return (fcntl(fd, F_SETFL, O_NONBLOCK));
 
153
}
 
154
 
 
155
int
 
156
generic_set_socket_buffer(int fd, int size)
 
157
{
 
158
        int w = size;
 
159
 
 
160
        if (w == 0)
 
161
                return (UPERF_SUCCESS);
 
162
        if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&w, sizeof (w))) {
 
163
                ulog_warn("Cannot set SO_SNDBUF");
 
164
        }
 
165
        if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&w, sizeof (w))
 
166
                != 0) {
 
167
                ulog_warn(" Cannot set SO_RCVBUF");
 
168
        }
 
169
 
 
170
        return (UPERF_SUCCESS);
 
171
}
 
172
 
 
173
int
 
174
generic_verify_socket_buffer(int fd, int wndsz)
 
175
{
 
176
        int nwsz;
 
177
        socklen_t len;
 
178
        float diff;
 
179
 
 
180
        if (wndsz == 0)
 
181
                return (UPERF_SUCCESS);
 
182
 
 
183
        /* Now verify */
 
184
        len = sizeof (wndsz);
 
185
        nwsz = -1;
 
186
        if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&nwsz, &len) != 0) {
 
187
                ulog_warn("Cannot get SO_SNDBUF");
 
188
        }
 
189
        diff = 1.0*nwsz/wndsz;
 
190
        if (diff < 0.9 || diff > 1.1) {
 
191
                ulog_warn("%s: %.2fKB (Requested:%.2fKB)",
 
192
                          "Send buffer", nwsz/1024.0, wndsz/1024.0);
 
193
        } else {
 
194
                uperf_info("Set Send buffer size to %.2fKB\n", nwsz/1024.0);
 
195
        }
 
196
 
 
197
        len = sizeof (wndsz);
 
198
        if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&nwsz, &len) != 0) {
 
199
                ulog_warn("Cannot get SO_RCVBUF");
 
200
        }
 
201
 
 
202
        diff = 1.0*nwsz/wndsz;
 
203
        if (diff < 0.9 || diff > 1.1) {
 
204
                ulog_warn("%s: %.2fKB (Requested:%.2fKB)",
 
205
                          "Recv buffer", nwsz/1024.0, wndsz/1024.0);
 
206
        } else {
 
207
                uperf_info("Set Recv buffer size to %.2fKB\n", nwsz/1024.0);
 
208
        }
 
209
 
 
210
        return (UPERF_SUCCESS);
 
211
}
 
212
 
 
213
/* ARGSUSED */
 
214
int
 
215
generic_listen(protocol_t *p, int pflag, void* options)
 
216
{
 
217
        const int on = 1;
 
218
        const int off = 0;
 
219
        int use_ipv6_socket;
 
220
        socklen_t len;
 
221
 
 
222
        if (setsockopt(p->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) {
 
223
                ulog_err("%s: Cannot set SO_REUSEADDR",
 
224
                        protocol_to_str(p->type));
 
225
        }
 
226
        if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(int)) < 0) {
 
227
                use_ipv6_socket = 0;
 
228
        } else {
 
229
                use_ipv6_socket = 1;
 
230
        }
 
231
        if (use_ipv6_socket) {
 
232
                struct sockaddr_in6 sin6;
 
233
 
 
234
                memset(&sin6, 0, sizeof(struct sockaddr_in6));
 
235
                sin6.sin6_family = AF_INET6;
 
236
                sin6.sin6_port = htons(p->port);
 
237
                sin6.sin6_addr = in6addr_any;
 
238
                if (bind(p->fd, (const struct sockaddr *)&sin6, sizeof(struct sockaddr_in6)) < 0) {
 
239
                        ulog_err("%s: Cannot bind to port %d",
 
240
                                 protocol_to_str(p->type), p->port);
 
241
                        return (UPERF_FAILURE);
 
242
                } else {
 
243
                        if (p->port == ANY_PORT) {
 
244
                                memset(&sin6, 0, sizeof(struct sockaddr_in6));
 
245
                                len = (socklen_t)sizeof(struct sockaddr_in6);
 
246
                                if ((getsockname(p->fd, (struct sockaddr *)&sin6, &len)) < 0) {
 
247
                                        ulog_err("%s: Cannot getsockname",
 
248
                                                 protocol_to_str(p->type));
 
249
                                        return (UPERF_FAILURE);
 
250
                                } else {
 
251
                                        p->port = ntohs(sin6.sin6_port);
 
252
                                }
 
253
                        }
 
254
                }
 
255
        } else {
 
256
                struct sockaddr_in sin;
 
257
 
 
258
                memset(&sin, 0, sizeof(struct sockaddr_in));
 
259
                sin.sin_family = AF_INET;
 
260
                sin.sin_port = htons(p->port);
 
261
                sin.sin_addr.s_addr = htonl(INADDR_ANY);
 
262
                if (bind(p->fd, (const struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
 
263
                        ulog_err("%s: Cannot bind to port %d",
 
264
                                 protocol_to_str(p->type), p->port);
 
265
                        return (UPERF_FAILURE);
 
266
                } else {
 
267
                        if (p->port == ANY_PORT) {
 
268
                                memset(&sin, 0, sizeof(struct sockaddr_in));
 
269
                                len = (socklen_t)sizeof(struct sockaddr_in);
 
270
                                if ((getsockname(p->fd, (struct sockaddr *)&sin, &len)) < 0) {
 
271
                                        ulog_err("%s: Cannot getsockname",
 
272
                                                 protocol_to_str(p->type));
 
273
                                        return (UPERF_FAILURE);
 
274
                                } else {
 
275
                                        p->port = ntohs(sin.sin_port);
 
276
                                }
 
277
                        }
 
278
                }
 
279
        }
 
280
        listen(p->fd, LISTENQ);
 
281
        uperf_debug("Listening on port %d\n", p->port);
 
282
 
 
283
        return (p->port);
 
284
}
 
285
 
 
286
/*
 
287
 * On success, a non negative value is returned. 0 is returned to
 
288
 * indicate timeout. timeout is in millisecs
 
289
 */
 
290
int
 
291
generic_poll(int fd, int timeout, short poll_type)
 
292
{
 
293
        struct pollfd pfd[2];
 
294
 
 
295
        if (timeout <= 0)
 
296
                return (0);
 
297
 
 
298
        pfd[0].fd = fd;
 
299
        pfd[0].events = poll_type;
 
300
        pfd[0].revents = 0;
 
301
 
 
302
        return (poll(pfd, 1, timeout));
 
303
}
 
304
 
 
305
int
 
306
generic_disconnect(protocol_t *p)
 
307
{
 
308
        if (p && p->fd >= 0) {
 
309
                (void) close(p->fd);
 
310
                p->fd = -1;
 
311
        }
 
312
 
 
313
        return (UPERF_SUCCESS);
 
314
}
 
315
 
 
316
int
 
317
generic_read(protocol_t *p, void *buffer, int size, void *options)
 
318
{
 
319
        flowop_options_t *fo = (flowop_options_t *)options;
 
320
        int timeout = (fo ? fo->poll_timeout/1.0e+6 : 0);
 
321
 
 
322
        if (timeout > 0) {
 
323
                if ((generic_poll(p->fd, timeout, POLLIN)) <= 0)
 
324
                        return (-1);
 
325
        }
 
326
        return (read(p->fd, buffer, size));
 
327
}
 
328
 
 
329
/* ARGSUSED */
 
330
int
 
331
generic_write(protocol_t *p, void *buffer, int size, void *options)
 
332
{
 
333
        return (write(p->fd, buffer, size));
 
334
}
 
335
 
 
336
/* ARGSUSED */
 
337
int
 
338
generic_recv(protocol_t *p, void *buffer, int size, void *options)
 
339
{
 
340
        return (recv(p->fd, buffer, size, 0));
 
341
}
 
342
 
 
343
/* ARGSUSED */
 
344
int
 
345
generic_send(protocol_t *p, void *buffer, int size, void *options)
 
346
{
 
347
        return (send(p->fd, buffer, size, 0));
 
348
}
 
349
 
 
350
 
 
351
/* ARGSUSED */
 
352
int
 
353
generic_undefined(protocol_t *p, void *options)
 
354
{
 
355
        uperf_error("Undefined function in protocol called\n");
 
356
        return (UPERF_FAILURE);
 
357
}
 
358
 
 
359
/* ARGSUSED2 */
 
360
int
 
361
generic_accept(protocol_t *oldp, protocol_t *newp, void *options)
 
362
{
 
363
        socklen_t addrlen;
 
364
        int timeout;
 
365
        char hostname[NI_MAXHOST];
 
366
        struct sockaddr_storage remote;
 
367
 
 
368
        assert(oldp);
 
369
        assert(newp);
 
370
 
 
371
        addrlen = (socklen_t)sizeof(struct sockaddr_storage);
 
372
        timeout = 10000;
 
373
 
 
374
        if ((generic_poll(oldp->fd, timeout, POLLIN)) <= 0)
 
375
                return (-1);
 
376
 
 
377
        if ((newp->fd = accept(oldp->fd, (struct sockaddr *)&remote,
 
378
            &addrlen)) < 0) {
 
379
                ulog_err("accept:");
 
380
                return (UPERF_FAILURE);
 
381
        }
 
382
        switch (remote.ss_family) {
 
383
        case AF_INET:
 
384
        {
 
385
                struct sockaddr_in *sin;
 
386
 
 
387
                sin = (struct sockaddr_in *)&remote;
 
388
                inet_ntop(AF_INET, &sin->sin_addr, hostname, sizeof(hostname));
 
389
                newp->port = ntohs(sin->sin_port);
 
390
                break;
 
391
        }
 
392
        case AF_INET6:
 
393
        {
 
394
                struct sockaddr_in6 *sin6;
 
395
 
 
396
                sin6 = (struct sockaddr_in6 *)&remote;
 
397
                inet_ntop(AF_INET6, &sin6->sin6_addr, hostname, sizeof(hostname));
 
398
                newp->port = ntohs(sin6->sin6_port);
 
399
                break;
 
400
        }
 
401
        default:
 
402
                return (UPERF_FAILURE);
 
403
                break;
 
404
        }
 
405
        (void) strlcpy(newp->host, hostname, sizeof(newp->host));
 
406
        uperf_info("Accepted connection from %s:%d\n", newp->host, newp->port);
 
407
 
 
408
#if 0
 
409
        if ((error = getnameinfo((const struct sockaddr *)&remote, addrlen,
 
410
                                 hostname, sizeof(hostname), NULL, 0, 0)) == 0) {
 
411
                if (remote.ss_family == AF_INET) {
 
412
                        newp->port = ntohs(((struct sockaddr_in *)&remote)->sin_port);
 
413
                } else {
 
414
                        newp->port = ntohs(((struct sockaddr_in6 *)&remote)->sin6_port);
 
415
                }
 
416
                (void) strlcpy(newp->host, hostname, MAXHOSTNAME);
 
417
                uperf_info("Accepted connection from %s:%d\n",
 
418
                           newp->host, newp->port);
 
419
        } else {
 
420
                char msg[128];
 
421
                (void) snprintf(msg, sizeof (msg),
 
422
                    "getnameinfo failed after accept: %s", gai_strerror(error));
 
423
                uperf_log_msg(UPERF_LOG_ERROR, errno, msg);
 
424
                return (errno);
 
425
        }
 
426
#endif
 
427
        return (UPERF_SUCCESS);
 
428
}
 
429
 
 
430
void
 
431
generic_fini(protocol_t *p)
 
432
{
 
433
        if (p && p->fd > 0)
 
434
                (void) close(p->fd);
 
435
        if (p)
 
436
                free(p);
 
437
}
 
438
 
 
439
void
 
440
set_tcp_options(int fd, flowop_options_t *f)
 
441
{
 
442
        /*
 
443
         * It is necessary that we set the send and recv buffer
 
444
         * sizes *before* connect() and bind(), else it will
 
445
         * not work
 
446
         *
 
447
         * We also need to make sure that we verify the buffer
 
448
         * sizes after the connect and listen
 
449
         */
 
450
        int wndsz;
 
451
 
 
452
        if (!f) {
 
453
                /*
 
454
                 * We used to se buffer size to 1M in the past
 
455
                 * But on GNU/linux if you do not set the window
 
456
                 * size, it autotunes.
 
457
                 */
 
458
                wndsz = 0;
 
459
        } else {
 
460
                wndsz = f->wndsz;
 
461
        }
 
462
        (void) generic_set_socket_buffer(fd, wndsz);
 
463
        (void) generic_verify_socket_buffer(fd, wndsz);
 
464
 
 
465
        if (f && (FO_TCP_NODELAY(f))) {
 
466
                int nodelay = 1;
 
467
                if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
 
468
                        (char *)&nodelay, sizeof (nodelay))) {
 
469
                        ulog_warn("Cannot set TCP_NODELAY");
 
470
                }
 
471
        }
 
472
}