-
Notifications
You must be signed in to change notification settings - Fork 186
/
netlib.c
4968 lines (4225 loc) · 133 KB
/
netlib.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
char netlib_id[]="\
@(#)netlib.c (c) Copyright 1993-2012 Hewlett-Packard Company. Version 2.6.0";
/****************************************************************/
/* */
/* netlib.c */
/* */
/* the common utility routines available to all... */
/* */
/* establish_control() establish the control socket */
/* calibrate_local_cpu() do local cpu calibration */
/* calibrate_remote_cpu() do remote cpu calibration */
/* send_request() send a request to the remote */
/* recv_response() receive a response from remote */
/* send_response() send a response to the remote */
/* recv_request() recv a request from the remote */
/* dump_request() dump request contents */
/* dump_response() dump response contents */
/* cpu_start() start measuring cpu */
/* cpu_stop() stop measuring cpu */
/* calc_cpu_util() calculate the cpu utilization */
/* calc_service_demand() calculate the service demand */
/* calc_thruput() calulate the tput in units */
/* calibrate() really calibrate local cpu */
/* identify_local() print local host information */
/* identify_remote() print remote host information */
/* format_number() format the number (KB, MB,etc) */
/* format_units() return the format in english */
/* msec_sleep() sleep for some msecs */
/* start_timer() start a timer */
/* random_ip_address() select a random IP address from */
/* specified range */
/* */
/* the routines you get when WANT_DLPI is defined... */
/* ...all moved to src/nettest_dlpi.c */
/* */
/* dl_open() open a file descriptor and */
/* attach to the card */
/* dl_mtu() find the MTU of the card */
/* dl_bind() bind the sap do the card */
/* dl_connect() sender's have of connect */
/* dl_accpet() receiver's half of connect */
/* dl_set_window() set the window size */
/* dl_stats() retrieve statistics */
/* dl_send_disc() initiate disconnect (sender) */
/* dl_recv_disc() accept disconnect (receiver) */
/****************************************************************/
/****************************************************************/
/* */
/* Global include files */
/* */
/****************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* It would seem that most of the includes being done here from
"sys/" actually have higher-level wrappers at just /usr/include.
This is based on a spot-check of a couple systems at my disposal.
If you have trouble compiling you may want to add "sys/" raj
10/95 */
#include <limits.h>
#include <signal.h>
#ifdef HAVE_SYSCALL_H
#include <syscall.h>
#endif
#ifdef MPE
# define NSIG _NSIG
#endif /* MPE */
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#ifndef WIN32
/* at some point, I would like to get rid of all these "sys/"
includes where appropriate. if you have a system that requires/
them, speak now, or your system may not compile later revisions of
netperf. raj 1/96 */
#include <unistd.h>
#include <sys/stat.h>
#include <sys/times.h>
#ifndef MPE
#include <time.h>
#include <sys/time.h>
#endif /* MPE */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <sys/utsname.h>
#if !defined(MPE) && !defined(__VMS)
#include <sys/param.h>
#endif /* MPE */
#else /* WIN32 */
#include <process.h>
#include <time.h>
#include <winsock2.h>
#define netperf_socklen_t socklen_t
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <mmsystem.h>
/* the only time someone should need to define DONT_IPV6 in the
"sources" file is if they are trying to compile on Windows 2000 or
NT4 and I suspect this may not be their only problem :) */
#ifndef DONT_IPV6
#include <ws2tcpip.h>
#endif
#define SIGALRM (14)
#define sleep(x) Sleep((x)*1000)
#endif /* WIN32 */
#ifdef HAVE_UNAME
#include <sys/utsname.h>
#endif
#ifdef _AIX
#include <sys/select.h>
#include <sys/sched.h>
#include <sys/pri.h>
#define PRIORITY PRI_LOW
#else/* _AIX */
#ifdef __sgi
#include <sys/prctl.h>
#include <sys/schedctl.h>
#define PRIORITY NDPLOMIN
#endif /* __sgi */
#endif /* _AIX */
#ifdef HAVE_MPCTL
#include <sys/mpctl.h>
#endif
#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
# include "missing/getaddrinfo.h"
#endif
#include "hist.h"
/****************************************************************/
/* */
/* Local Include Files */
/* */
/****************************************************************/
#define NETLIB
#include "netlib.h"
#include "netsh.h"
#include "netcpu.h"
#include "netperf_version.h"
/****************************************************************/
/* */
/* Global constants, macros and variables */
/* */
/****************************************************************/
#if defined(WIN32) || defined(__VMS)
struct timezone {
int dummy ;
} ;
#ifndef __VMS
SOCKET win_kludge_socket = INVALID_SOCKET;
SOCKET win_kludge_socket2 = INVALID_SOCKET;
#endif /* __VMS */
#endif /* WIN32 || __VMS */
#ifndef LONG_LONG_MAX
#define LONG_LONG_MAX 9223372036854775807LL
#endif /* LONG_LONG_MAX */
/* older versions of netperf knew about the HP kernel IDLE counter.
this is now obsolete - in favor of either pstat(), times, or a
process-level looper process. we also now require support for the
"long" integer type. raj 4/95. */
int
lib_num_loc_cpus, /* the number of cpus in the system */
lib_num_rem_cpus; /* how many we think are in the remote */
struct cpu_stats_struct
lib_local_cpu_stats,
lib_remote_cpu_stats;
#define PAGES_PER_CHILD 2
int lib_use_idle;
int cpu_method;
struct timeval time1, time2;
struct timezone tz;
float lib_elapsed,
lib_local_maxrate,
lib_remote_maxrate;
float lib_local_per_cpu_util[MAXCPUS];
int lib_cpu_map[MAXCPUS];
int *request_array;
int *response_array;
/* INVALID_SOCKET == INVALID_HANDLE_VALUE == (unsigned int)(~0) == -1 */
SOCKET netlib_control = INVALID_SOCKET;
SOCKET server_sock = INVALID_SOCKET;
int control_family = AF_UNSPEC;
/* global variables to hold the value for processor affinity */
int local_proc_affinity = -1,remote_proc_affinity = -1;
/* these are to allow netperf to be run easily through those evil,
end-to-end breaking things known as firewalls */
char local_data_port[10];
char remote_data_port[10];
char *local_data_address=NULL;
char *remote_data_address=NULL;
char *local_sysname, *remote_sysname;
char *local_release, *remote_release;
char *local_version, *remote_version;
char *local_machine, *remote_machine;
int local_data_family=AF_UNSPEC;
int remote_data_family=AF_UNSPEC;
char *netperf_version;
enum netperf_output_modes netperf_output_mode = HUMAN;
/* in the past, I was overlaying a structure on an array of ints. now
I am going to have a "real" structure, and point an array of ints
at it. the real structure will be forced to the same alignment as
the type "double." this change will mean that pre-2.1 netperfs
cannot be mixed with 2.1 and later. raj 11/95 */
union netperf_request_struct netperf_request;
union netperf_response_struct netperf_response;
FILE *where;
char libfmt = '?';
#ifdef WIN32
HANDLE hAlarm = INVALID_HANDLE_VALUE;
int timed_out=0;
#endif
int times_up;
#ifdef WIN32
/* we use a getopt implementation from net.sources */
/*
* get option letter from argument vector
*/
int
opterr = 1, /* should error messages be printed? */
optind = 1, /* index into parent argv vector */
optopt; /* character checked for validity */
char
*optarg; /* argument associated with option */
#define EMSG ""
#endif /* WIN32 */
static int measuring_cpu;
int
netlib_get_page_size(void) {
/* not all systems seem to have the sysconf for page size. for
those which do not, we will assume that the page size is 8192
bytes. this should be more than enough to be sure that there is
no page or cache thrashing by looper processes on MP
systems. otherwise that's really just too bad - such systems
should define _SC_PAGE_SIZE - raj 4/95 */
#ifndef _SC_PAGE_SIZE
#ifdef WIN32
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
return SystemInfo.dwPageSize;
#else
return(8192L);
#endif /* WIN32 */
#else
return(sysconf(_SC_PAGE_SIZE));
#endif /* _SC_PAGE_SIZE */
}
#ifdef WANT_INTERVALS
#ifdef WIN32
HANDLE WinTimer;
UINT timerRes;
void stop_itimer()
{
CancelWaitableTimer(WinTimer);
CloseHandle(WinTimer);
timeEndPeriod(timerRes);
}
#else
static unsigned int usec_per_itvl;
void
stop_itimer()
{
struct itimerval new_interval;
struct itimerval old_interval;
new_interval.it_interval.tv_sec = 0;
new_interval.it_interval.tv_usec = 0;
new_interval.it_value.tv_sec = 0;
new_interval.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL,&new_interval,&old_interval) != 0) {
/* there was a problem arming the interval timer */
perror("netperf: setitimer");
exit(1);
}
return;
}
#endif /* WIN32 */
#endif /* WANT_INTERVALS */
#ifdef WIN32
static void
error(char *pch)
{
if (!opterr) {
return; /* without printing */
}
fprintf(stderr, "%s: %s: %c\n",
(NULL != program) ? program : "getopt", pch, optopt);
}
int
getopt(int argc, char **argv, char *ostr)
{
static char *place = EMSG; /* option letter processing */
register char *oli; /* option letter list index */
if (!*place) {
/* update scanning pointer */
if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
return EOF;
}
if (*place == '-') {
/* found "--" */
++optind;
place = EMSG ; /* Added by shiva for Netperf */
return EOF;
}
}
/* option letter okay? */
if ((optopt = (int)*place++) == (int)':'
|| !(oli = strchr(ostr, optopt))) {
if (!*place) {
++optind;
}
error("illegal option");
return BADCH;
}
if (*++oli != ':') {
/* don't need argument */
optarg = NULL;
if (!*place)
++optind;
} else {
/* need an argument */
if (*place) {
optarg = place; /* no white space */
} else if (argc <= ++optind) {
/* no arg */
place = EMSG;
error("option requires an argument");
return BADCH;
} else {
optarg = argv[optind]; /* white space */
}
place = EMSG;
++optind;
}
return optopt; /* return option letter */
}
#endif /* WIN32 */
/*----------------------------------------------------------------------------
* WIN32 implementation of perror, does not deal very well with WSA errors
* The stdlib.h version of perror only deals with the ancient XENIX error codes.
*
* +*+SAF Why can't all WSA errors go through GetLastError? Most seem to...
*--------------------------------------------------------------------------*/
#ifdef WIN32
void PrintWin32Error(FILE *stream, LPSTR text)
{
LPSTR szTemp;
DWORD dwResult;
DWORD dwError;
dwError = GetLastError();
dwResult = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
NULL,
dwError,
LANG_NEUTRAL,
(LPTSTR)&szTemp,
0,
NULL );
if (dwResult)
fprintf(stream, "%s: %s\n", text, szTemp);
else
fprintf(stream, "%s: error 0x%x\n", text, dwError);
fflush(stream);
if (szTemp)
LocalFree((HLOCAL)szTemp);
}
#endif /* WIN32 */
char *
nsec_enabled_to_str(int enabled) {
switch (enabled) {
case NSEC_UNKNOWN:
return("Unknown");
case NSEC_DISABLED:
return("Disabled");
case NSEC_PERMISSIVE:
return("Permissive");
case NSEC_ENFORCING:
return("Enforcing");
default:
return("UNKNOWN MODE");
}
}
char * nsec_type_to_str(int type) {
switch (type) {
case NSEC_TYPE_UNKNOWN:
return("Unknown");
case NSEC_TYPE_SELINUX:
return("SELinux");
default:
return("UNKNOWN TYPE");
}
}
char *
inet_ttos(int type)
{
switch (type) {
case SOCK_DGRAM:
return("SOCK_DGRAM");
break;
case SOCK_STREAM:
return("SOCK_STREAM");
break;
#ifdef SOCK_DCCP
case SOCK_DCCP:
return("SOCK_DCCP");
#endif
#ifdef SOCK_SEQPACKET
case SOCK_SEQPACKET:
return("SOCK_SEQPACKET");
#endif
default:
return("SOCK_UNKNOWN");
}
}
char unknown[32];
char *
inet_ptos(int protocol) {
switch (protocol) {
case IPPROTO_TCP:
return("IPPROTO_TCP");
break;
case IPPROTO_UDP:
return("IPPROTO_UDP");
break;
#if defined(IPPROTO_SCTP)
case IPPROTO_SCTP:
return("IPPROTO_SCTP");
break;
#endif
#if defined(IPPROTO_DCCP)
case IPPROTO_DCCP:
return "IPPROTO_DCCP";
break;
#endif
#if defined(IPPROTO_UDPLITE)
case IPPROTO_UDPLITE:
return "IPPROTO_UDPLITE";
break;
#endif
default:
snprintf(unknown,sizeof(unknown),"IPPROTO_UNKNOWN(%d)",protocol);
return(unknown);
}
}
/* one of these days, this should not be required */
#ifndef AF_INET_SDP
#define AF_INET_SDP 27
#define PF_INET_SDP AF_INET_SDP
#endif
char *
inet_ftos(int family)
{
switch(family) {
case AF_INET:
return("AF_INET");
#if defined(AF_INET6)
case AF_INET6:
return("AF_INET6");
#endif
#if defined(AF_INET_SDP)
case AF_INET_SDP:
return("AF_INET_SDP");
#endif
#if defined(AF_RDS)
case AF_RDS:
return("AF_RDS");
#endif
default:
return("AF_UNSPEC");
}
}
int
inet_nton(int af, const void *src, char *dst, int cnt)
{
switch (af) {
case AF_INET:
/* magic constants again... :) */
if (cnt >= 4) {
memcpy(dst,src,4);
return 4;
}
else {
Set_errno(ENOSPC);
return(-1);
}
break;
#if defined(AF_INET6)
case AF_INET6:
if (cnt >= 16) {
memcpy(dst,src,16);
return(16);
}
else {
Set_errno(ENOSPC);
return(-1);
}
break;
#endif
#if defined(AF_RDS)
case AF_RDS:
if (cnt >= 4) {
memcpy(dst,src,4);
return 4;
}
#endif
default:
Set_errno(EAFNOSUPPORT);
return(-1);
}
}
double
ntohd(double net_double)
{
/* we rely on things being nicely packed */
union {
double whole_thing;
unsigned int words[2];
unsigned char bytes[8];
} conv_rec;
unsigned char scratch;
int i;
/* on those systems where ntohl is a no-op, we want to return the
original value, unchanged */
if (ntohl(1L) == 1L) {
return(net_double);
}
conv_rec.whole_thing = net_double;
/* we know that in the message passing routines that ntohl will have
been called on the 32 bit quantities. we need to put those back
the way they belong before we swap */
conv_rec.words[0] = htonl(conv_rec.words[0]);
conv_rec.words[1] = htonl(conv_rec.words[1]);
/* now swap */
for (i=0; i<= 3; i++) {
scratch = conv_rec.bytes[i];
conv_rec.bytes[i] = conv_rec.bytes[7-i];
conv_rec.bytes[7-i] = scratch;
}
#if defined(__FLOAT_WORD_ORDER) && defined(__BYTE_ORDER)
if (__FLOAT_WORD_ORDER != __BYTE_ORDER) {
/* Fixup mixed endian floating point machines */
unsigned int scratch = conv_rec.words[0];
conv_rec.words[0] = conv_rec.words[1];
conv_rec.words[1] = scratch;
}
#endif
return(conv_rec.whole_thing);
}
double
htond(double host_double)
{
/* we rely on things being nicely packed */
union {
double whole_thing;
unsigned int words[2];
unsigned char bytes[8];
} conv_rec;
unsigned char scratch;
int i;
/* on those systems where ntohl is a no-op, we want to return the
original value, unchanged */
if (ntohl(1L) == 1L) {
return(host_double);
}
conv_rec.whole_thing = host_double;
/* now swap */
for (i=0; i<= 3; i++) {
scratch = conv_rec.bytes[i];
conv_rec.bytes[i] = conv_rec.bytes[7-i];
conv_rec.bytes[7-i] = scratch;
}
#if defined(__FLOAT_WORD_ORDER) && defined(__BYTE_ORDER)
if (__FLOAT_WORD_ORDER != __BYTE_ORDER) {
/* Fixup mixed endian floating point machines */
unsigned int scratch = conv_rec.words[0];
conv_rec.words[0] = conv_rec.words[1];
conv_rec.words[1] = scratch;
}
#endif
/* we know that in the message passing routines htonl will be called
on the 32 bit quantities. we need to set things up so that when
this happens, the proper order will go out on the network */
conv_rec.words[0] = htonl(conv_rec.words[0]);
conv_rec.words[1] = htonl(conv_rec.words[1]);
return(conv_rec.whole_thing);
}
/* The original patch from Google used lrand48, but I have been
informed that is not easily available under Windows. So, rather
than have some #ifdefs here I'll just simplistically replace
lrand48 with rand(), which should be "good enough" at some point it
may be sufficient to just call rand() directly rather than call
this raj 20101130 */
unsigned int
rand32(){
return (unsigned int)rand() * 2 + rand() % 2;
}
/* this routine will set the ip address of the sockaddr in the
addrinfo to a random number in range, based on the address
family. for grins, we will sanity check the value of mask_len
against the address family. initial version from google,
enhancements by raj 20101129 */
void
random_ip_address(struct addrinfo *res, int mask_len)
{
switch(res->ai_family) {
case AF_INET: {
struct sockaddr_in *foo = (struct sockaddr_in *)res->ai_addr;
unsigned int addr = ntohl(foo->sin_addr.s_addr);
unsigned int mask = ((unsigned int)1 << (32 - mask_len)) - 1;
if ((mask_len < 0) || (mask_len > 32)) {
fprintf(where,
"Mask length must be between 0 and 32 inclusive for AF_INET\n");
fflush(where);
exit(-1);
}
addr = ntohl(foo->sin_addr.s_addr);
do {
addr = (addr & ~mask) | (rand32() & mask);
} while ((addr & 0xff) == 0xff);
foo->sin_addr.s_addr = htonl(addr);
break;
}
#if defined(AF_INET6)
case AF_INET6: {
struct sockaddr_in6 *foo = (struct sockaddr_in6 *)res->ai_addr;
unsigned int i, len;
unsigned int *addr = (unsigned int *)&(foo->sin6_addr.s6_addr);
unsigned int mask;
if ((mask_len < 0) || (mask_len > 128)) {
fprintf(where,
"Mask length must be between 0 and 128 inclusive for AF_INET\n");
fflush(where);
exit(-1);
}
for (i = 0; i < 4; i ++){
addr[i] = ntohl(addr[i]);
len = mask_len - i * 32;
len = ((len < 32) ? len : 32);
len = ((len > 0) ? len : 0);
mask = ((unsigned int)1 << (32 - len)) - 1;
addr[i] = (addr[i] & ~mask) | (rand32() & mask);
addr[i] = htonl(addr[i]);
}
break;
}
#endif
default:
fprintf(where,
"Unexpected Address Family of %u\n",res->ai_family);
fflush(where);
exit(-1);
}
}
#if defined(HAVE_SENDFILE)
int netperf_sendfile(SOCKET send_socket, struct ring_elt *send_ring) {
int len;
int ret = 0;
#if defined(__linux) || defined(__sun)
off_t scratch_offset; /* the linux sendfile() call will update
the offset variable, which is
something we do _not_ want to happen
to the value in the send_ring! so, we
have to use a scratch variable. */
#endif /* __linux || defined(__sun) */
#if defined (__sun)
size_t scratch_len; /* the sun sendfilev() needs a place to
tell us how many bytes were written,
even though it also returns the value */
sendfilevec_t sv;
#endif /* __sun */
/* you can look at netlib.h for a description of the fields we
are passing to sendfile(). 08/2000 */
#if defined(__linux)
scratch_offset = send_ring->offset;
len=sendfile(send_socket,
send_ring->fildes,
&scratch_offset, /* modified after the call! */
send_ring->length);
#elif defined (__sun)
/* We must call with SFV_NOWAIT and a large file size (>= 16MB)
to get zero-copy, as well as compiling with
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 */
sv.sfv_fd = send_ring->fildes;
sv.sfv_flag = SFV_NOWAIT;
sv.sfv_off = send_ring->offset;
sv.sfv_len = send_ring->length;
len = sendfilev(send_socket, &sv, 1, &scratch_len);
#elif defined(__FreeBSD__)
/* so close to HP-UX and yet so far away... :) */
ret = sendfile(send_ring->fildes,
send_socket,
send_ring->offset,
send_ring->length,
NULL,
(off_t *)&len,
send_ring->flags);
#elif defined(USE_OSX)
len = send_ring->length;
ret = sendfile(send_ring->fildes,
send_socket,
send_ring->offset,
(off_t *)&len,
NULL,
send_ring->flags);
#else /* original sendile HP-UX */
len=sendfile(send_socket,
send_ring->fildes,
send_ring->offset,
send_ring->length,
send_ring->hdtrl,
send_ring->flags);
#endif
/* for OSX and FreeBSD, a non-zero ret means something failed.
I would hope that the length fields are set to -1 or the
like, but at the moment I do not know I can count on
that. for other platforms, ret will be set to zero and we can
rely directly on len. raj 2013-05-01 */
if (ret != 0)
return -1;
else
return len;
}
#endif
/* one of these days, this should be abstracted-out just like the CPU
util stuff. raj 2005-01-27 */
int
get_num_cpus()
{
/* on HP-UX, even when we use the looper procs we need the pstat */
/* call */
int temp_cpus;
#ifdef __hpux
#include <sys/pstat.h>
struct pst_dynamic psd;
if (pstat_getdynamic((struct pst_dynamic *)&psd,
(size_t)sizeof(psd), (size_t)1, 0) != -1) {
temp_cpus = psd.psd_proc_cnt;
}
else {
temp_cpus = 1;
}
#else
/* MW: <unistd.h> was included for non-Windows systems above. */
/* Thus if _SC_NPROC_ONLN is defined, we should be able to use sysconf. */
#ifdef _SC_NPROCESSORS_ONLN
temp_cpus = sysconf(_SC_NPROCESSORS_ONLN);
#ifdef USE_PERFSTAT
temp_cpus = perfstat_cpu(NULL,NULL, sizeof(perfstat_cpu_t), 0);
#endif /* USE_PERFSTAT */
#else /* no _SC_NPROCESSORS_ONLN */
#ifdef WIN32
SYSTEM_INFO SystemInfo;
GetSystemInfo(&SystemInfo);
temp_cpus = SystemInfo.dwNumberOfProcessors;
#else
/* we need to know some other ways to do this, or just fall-back on
a global command line option - raj 4/95 */
temp_cpus = shell_num_cpus;
#endif /* WIN32 */
#endif /* _SC_NPROCESSORS_ONLN */
#endif /* __hpux */
if (temp_cpus > MAXCPUS) {
fprintf(where,
"Sorry, this system has more CPUs (%d) than I can handle (%d).\n"
"Please alter MAXCPUS in netlib.h and recompile.\n",
temp_cpus,
MAXCPUS);
fflush(where);
exit(1);
}
return(temp_cpus);
}
#ifdef WIN32
#ifdef __GNUC__
#define S64_SUFFIX(x) x##LL
#else
#define S64_SUFFIX(x) x##i64
#endif
/*
* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
*/
#define EPOCH_BIAS S64_SUFFIX(116444736000000000)
/*
* Union to facilitate converting from FILETIME to unsigned __int64
*/
typedef union {
unsigned __int64 ft_scalar;
FILETIME ft_struct;
} FT;
void
gettimeofday( struct timeval *tv , struct timezone *not_used )
{
FT nt_time;
__int64 UnixTime; /* microseconds since 1/1/1970 */
GetSystemTimeAsFileTime( &(nt_time.ft_struct) );
UnixTime = ((nt_time.ft_scalar - EPOCH_BIAS) / S64_SUFFIX(10));
tv->tv_sec = (long)(time_t)(UnixTime / S64_SUFFIX(1000000));
tv->tv_usec = (unsigned long)(UnixTime % S64_SUFFIX(1000000));
}
#endif /* WIN32 */
/* this routine will disable any running timer */
void
stop_timer()
{
#ifndef WIN32
alarm(0);
#else
/* at some point we may need some win32 equivalent */
if (hAlarm != (HANDLE) INVALID_HANDLE_VALUE) {
SetEvent(hAlarm);
}
#endif /* WIN32 */
}
/************************************************************************/
/* */
/* signal catcher */
/* */
/************************************************************************/
#ifndef WIN32
void
#if defined(__hpux)
catcher(sig, code, scp)
int sig;
int code;
struct sigcontext *scp;
#else
catcher(int sig)
#endif /* __hpux || __VMS */
{
#ifdef __hpux
if (debug > 2) {
fprintf(where,"caught signal %d ",sig);
if (scp) {
fprintf(where,"while in syscall %d\n",
scp->sc_syscall);
}
else {
fprintf(where,"null scp\n");
}