This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
nicstat.c
2905 lines (2665 loc) · 68.3 KB
/
nicstat.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
/*
* nicstat - print network traffic, Kb/s read and written.
*
* Copyright (c) 2005-2014, [email protected] and [email protected]
*
* nicstat is licensed under the Artistic License 2.0. You can find
* a copy of this license as LICENSE.txt included with the nicstat
* distribution, or at http://www.perlfoundation.org/artistic_license_2_0
*/
#define NICSTAT_VERSION "1.95"
/* Is this GNU/Linux? */
#if defined(__linux__) || defined(__linux) || defined(linux)
#define OS_LINUX 1
#endif
/* Is this Solaris? */
#if defined(sun) || defined(__sun)
#if defined(__SVR4) || defined(__svr4__)
#define OS_SOLARIS 1
#endif
#endif
#if ! defined(OS_SOLARIS) && ! defined(OS_LINUX)
#error "nicstat is not supported on your OS yet"
#endif
#ifndef DEBUG
#define DEBUG 0
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#ifdef OS_SOLARIS
#include <sys/sockio.h>
#include <kstat.h>
#include <libgen.h>
#ifdef USE_DLADM
#include <libdladm.h>
#include <libdllink.h>
#include <dlfcn.h>
#include <link.h>
#ifdef HAVE_LIBNETCFG
#include <libnetcfg.h>
#endif
#endif
#ifndef LIFC_ALLZONES /* Comes from <net/if.h> in 5.10 & later */
#define LIFC_ALLZONES 0x08
#endif
#ifndef LIFC_UNDER_IPMP /* <net.if.h> in 5.11 */
#define LIFC_UNDER_IPMP 0x0
#endif
#ifndef LIFC_ENABLED /* <net.if.h> in 5.11 */
#define LIFC_ENABLED 0x0
#endif
#ifndef MAXLINKNAMELEN /* <net/if.h> in 5.11 */
#define MAXLINKNAMELEN LIFNAMSIZ
#endif
#define LIFR_FLAGS_TYPE uint64_t
#else /* OS_SOLARIS */
#include <poll.h>
#endif /* OS_SOLARIS */
#ifdef OS_LINUX
/* #include <linux/if.h> */
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <linux/ethtool.h>
#define PROC_NET_DEV_PATH "/proc/net/dev"
#define PROC_NET_SNMP_PATH "/proc/net/snmp"
#define PROC_NET_NETSTAT_PATH "/proc/net/netstat"
#define PROC_NET_BUFSIZ (128 * 1024)
#define PROC_UPTIME "/proc/uptime"
#define UINT32_MAX (4294967295U)
/* Needs to be fixed if not built under ILP32 */
typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
extern char *optarg;
extern int optind, opterr, optopt;
#endif /* OS_LINUX */
#ifdef OS_LINUX
typedef __u8 duplex_t;
/* This may be defined by <linux/ethtool.h> */
#ifndef DUPLEX_UNKNOWN
#define DUPLEX_UNKNOWN 0xFF
#endif /* DUPLEX_UNKNOWN */
#else
typedef uint32_t duplex_t;
#define DUPLEX_UNKNOWN 0
#define DUPLEX_HALF 1
#define DUPLEX_FULL 2
#endif /* OS_LINUX */
#ifndef B_TRUE
#define B_TRUE 1
#define B_FALSE 0
#endif
#ifndef streql
#define streql(a, b) (strcmp((a), (b)) == 0)
#endif
#define PAGE_SIZE 20
#define INTERVAL 1
#define LOOP_MAX 1
#ifdef OS_LINUX
#define GETOPT_OPTIONS "hi:sS:znplvxtuaMmU"
#else
#define GETOPT_OPTIONS "hi:sznpklvxtuaMmU"
#endif
/*
* UDP stats
*/
typedef struct udp_stats {
struct timeval tv; /* tv_sec, tv_usec */
uint64_t inDatagrams;
uint64_t outDatagrams;
uint64_t inErrors;
uint64_t outErrors;
} udpstats_t;
static udpstats_t *g_udp_old, *g_udp_new;
typedef struct tcp_stats {
struct timeval tv; /* tv_sec, tv_usec */
uint64_t inDataInorderSegs;
uint64_t outDataSegs;
uint64_t inDataInorderBytes;
uint64_t inDataUnorderSegs;
uint64_t inDataUnorderBytes;
uint64_t outDataBytes;
uint64_t estabResets;
uint64_t outRsts;
uint64_t attemptFails;
uint64_t retransBytes;
uint64_t passiveOpens;
uint64_t activeOpens;
uint64_t halfOpenDrop;
uint64_t listenDrop;
uint64_t listenDropQ0;
} tcpstats_t;
static tcpstats_t *g_tcp_old, *g_tcp_new;
#ifdef OS_SOLARIS
static kstat_t *g_tcp_ksp, *g_udp_ksp;
#endif
/*
* Interface stats
*/
typedef struct nic_stats {
struct timeval tv; /* tv_sec, tv_usec */
uint64_t rbytes; /* total read bytes */
uint64_t wbytes; /* total written bytes */
uint64_t rpackets; /* total read packets */
uint64_t wpackets; /* total written packets */
uint64_t ierr; /* total input errors */
uint64_t oerr; /* total output errors */
uint64_t coll; /* total collisions */
uint64_t nocp; /* total nocanput */
uint64_t defer; /* total defers */
uint64_t sat; /* saturation value */
} nicstats_t;
typedef struct nicdata {
struct nicdata *next; /* pointer to next */
char *name; /* interface name (e.g. "lo0") */
uint32_t flags;
#ifdef OS_LINUX
int report; /* non-zero means we intend to print */
#endif
#ifdef OS_SOLARIS
kstat_t *ls_ksp;
kstat_t *op_ksp;
uint32_t ls_types;
uint32_t op_types;
LIFR_FLAGS_TYPE if_flags;
#endif
uint64_t speed; /* speed of interface */
duplex_t duplex;
struct nic_stats old; /* stats from previous lookup */
struct nic_stats new; /* stats from current lookup */
} nicdata_t;
typedef struct if_list {
struct if_list *next;
char *name;
#ifdef OS_LINUX
struct nicdata *nicp;
#endif
} if_list_t;
/*
* kstat type flags
*
* These are in decreasing order of preference; i.e, the highest order
* bit will be chosen as the preferred source. These bits have been
* chosen to allow addition above, below and in-between the existing
* choices.
*/
#define KS_LINK 0x40000 /* link:<n>:<if>:<stat> */
#define KS_DRV_MAC 0x10000 /* <drv>:<n>:mac:<stat> */
#define KS_DIN 0x04000 /* <drv>:<n>:<if>:<stat> */
#define KS_DRV 0x01000 /* <drv>:<n>:*:<stat> */
#define KS_NAME 0x00400 /* <if>:*:*:<stat> */
/*
* Other interface flags - for nicdata_t.flags
*/
#define NIC_LIF_UP 0x00000001 /* IFF_UP */
#define NIC_KS_UP 0x00000002 /* kstat link_state = 1 */
#define NIC_LOOPBACK 0x00000010 /* Is a IFF_LOOPBACK */
#define NIC_NO_GLIFFLAGS 0x00000100 /* no ioctl(,SIOCGLIFFLAGS,) */
#define NIC_NO_KSTATS 0x00000200 /* Can't even get packets */
#define NIC_NO_LINKSTATE 0x00000400 /* No :::link_state */
#define NIC_NO_GSET 0x00000800 /* ETHTOOL_GSET fails */
#define NIC_NO_SFLAG 0x00000200 /* No -S for this i'face */
#define NIC_UP (NIC_KS_UP | NIC_LIF_UP)
#define NIC_LK_IS_OK 0x00001000 /* ls_ksp == op_ksp */
#define NIC_LK_UPDATED 0x00010000 /* ls_ksp up to date */
#define NIC_OK_UPDATED 0x00020000 /* op_ksp up to date */
#define NIC_KU_UPDATED 0x00040000 /* NIC_KS_UP up to date */
#define NIC_LU_UPDATED 0x00080000 /* NIC_LIF_UP up to date */
/* These bits indicate we have updated some data */
#define NIC_UPDATED_FLAGS (NIC_LK_UPDATED | NIC_OK_UPDATED | \
NIC_KU_UPDATED | NIC_LU_UPDATED)
/* These bits are capabilities - should be static */
#define NIC_CAPAB (NIC_CAN_GLIFFLAGS | NIC_HAVE_KSTATS | NIC_LOOPBACK)
#ifdef OS_LINUX
struct if_speed_list {
struct if_speed_list *next;
char *name;
uint64_t speed;
int duplex;
};
static struct if_speed_list *g_if_speed_list = NULL;
#endif /* OS_LINUX */
/*
* This will contain everything we need to know about each interface, and
* will be dynamically allocated.
*/
static struct nicdata *g_nicdatap = NULL;
/* Print style for NICs */
enum { STYLE_FULL = 0, STYLE_FULL_UTIL, STYLE_SUMMARY, STYLE_PARSEABLE,
STYLE_EXTENDED, STYLE_EXTENDED_UTIL,
STYLE_EXTENDED_PARSEABLE, STYLE_NONE };
static int g_nicdata_count = 0; /* number of if's we are tracking */
static int g_style; /* output style */
static int g_skipzero; /* skip zero value lines */
static int g_nonlocal; /* list only non-local (exclude lo0) */
static int g_someif; /* trace some interfaces only */
static int g_list;
static int g_udp; /* show UDP stats */
static int g_tcp; /* show TCP stats */
static int g_opt_x;
static int g_opt_p;
static int g_verbose;
static int g_forever; /* run forever */
static char **g_tracked; /* tracked interfaces */
static int g_line; /* output line counter */
static char *g_progname; /* ptr to argv[0] */
static int g_caught_cont; /* caught SIGCONT - were suspended */
static int g_opt_m; /* show results in Mbps (megabits) */
static int g_opt_U; /* show in and out %Util */
/* Used in display headers - default is when displaying KB/s */
static char *g_runit_1 = "rKB/s";
static char *g_wunit_1 = "wKB/s";
static char *g_runit_2 = "RdKB";
static char *g_wunit_2 = "WrKB";
static int g_sock; /* Socket for interface ioctl's */
#ifdef OS_SOLARIS
static int g_opt_k;
static kstat_ctl_t *g_kc; /* kstat chain pointer */
static int g_new_kstat_chain = B_TRUE; /* kstat chain updated */
#ifdef USE_DLADM
/* This is set to TRUE if we can load the libdladm function we need */
static int g_use_dladm;
static dladm_handle_t g_handle = NULL;
#endif
#endif /* OS_SOLARIS */
#ifdef OS_LINUX
static unsigned long g_boot_time; /* when we booted */
static FILE *g_snmp = NULL;
static FILE *g_netstat = NULL;
#endif /* OS_LINUX */
/*
* diag - print stderr message.
*
* This subroutine prints an error message, possibly including the meaning
* of errno.
*/
static void
diag(int use_errno, char *format, ...)
{
va_list ap;
char *error_str;
(void) fprintf(stderr, "%s: ", g_progname);
if (use_errno) {
error_str = strerror(errno);
if (! error_str)
error_str = strerror(0);
}
va_start(ap, format);
(void) vfprintf(stderr, format, ap);
va_end(ap);
if (use_errno)
(void) fprintf(stderr, ": %s\n", error_str);
else
(void) fputc('\n', stderr);
}
#define die(...) \
do { \
diag(__VA_ARGS__); \
exit(2); \
} while (0)
/*
* usage - print a usage message and exit.
*/
static void
usage(void)
{
(void) fprintf(stderr,
"USAGE: nicstat [-hvnsxpztualMU] [-i int[,int...]]\n "
#ifdef OS_LINUX
"[-S int:mbps[,int:mbps...]] "
#endif
"[interval [count]]\n"
"\n"
" -h # help\n"
" -v # show version (" NICSTAT_VERSION ")\n"
" -i interface # track interface only\n"
" -n # show non-local interfaces only"
" (exclude lo0)\n"
" -s # summary output\n"
" -x # extended output\n"
" -p # parseable output\n"
" -z # skip zero value lines\n"
" -t # show TCP statistics\n"
" -u # show UDP statistics\n"
" -a # equivalent to \"-x -u -t\"\n"
" -l # list interface(s)\n"
" -M # output in Mbits/sec\n"
" -U # separate %%rUtil and %%wUtil\n"
#ifdef OS_LINUX
" -S int:mbps[fd|hd] # tell nicstat the interface\n"
" # speed (Mbits/sec) and duplex\n"
#endif
" eg,\n");
(void) fprintf(stderr,
" nicstat # print summary since boot only\n"
" nicstat 1 # print every 1 second\n"
" nicstat 1 5 # print 5 times only\n"
" nicstat -z 1 # print every 1 second, skip zero"
" lines\n"
" nicstat -i hme0 1 # print hme0 only every 1 second\n");
exit(1);
}
/*
* new_string - simply strdup(3), but terminate on failure
*/
static char *
new_string(char *s)
{
char *new;
new = strdup(s);
if (! new)
die(1, "strdup", g_progname);
return (new);
}
/*
* allocate() - calloc(3) - for zeroing, plus error handling
*/
static inline void *
allocate(size_t bytes)
{
void *p;
p = calloc(1, bytes);
if (p == NULL)
die(1, "calloc");
return (p);
}
/*
* Return floating difference in timevals
*/
static double
tv_diff(struct timeval *new, struct timeval *old)
{
double new_d, old_d;
new_d = (double)new->tv_sec;
new_d += new->tv_usec / 1000000.0;
old_d = (double)old->tv_sec;
old_d += old->tv_usec / 1000000.0;
return (new_d - old_d);
}
/*
* if_is_ignored - return true if interface is to be ignored
*/
static int
if_is_ignored(char *if_name)
{
char **p;
if (! g_someif)
return (B_FALSE);
for (p = g_tracked; *p; p++)
if (streql(if_name, *p))
return (B_FALSE);
return (B_TRUE);
}
#ifdef OS_SOLARIS
/*
* Check interface list to see if an interface is in it
*/
static int
interface_in_list(char *interface, nicdata_t *nicp)
{
while (nicp) {
if (streql(interface, nicp->name))
return (B_TRUE);
nicp = nicp->next;
}
return (B_FALSE);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* reclaim_nicdata - reclaim's a struct nicdata * from our global list
*
* Return a struct nicdata pointer; if it is found in the global list; and
* also remove it from the list (we are in the process of re-building the
* list). Modifies g_nicdatap.
*/
static struct nicdata *
reclaim_nicdata(char *if_name)
{
struct nicdata *matchp, *prevp;
prevp = NULL;
for (matchp = g_nicdatap; matchp; matchp = matchp->next) {
if (streql(matchp->name, if_name)) {
/* Got a match */
if (prevp)
/* Splice head of list to tail of list */
prevp->next = matchp->next;
else
/* We are at the head */
g_nicdatap = matchp->next;
/* Disassociate match with the tail of the list */
matchp->next = NULL;
return (matchp);
}
prevp = matchp;
}
return (NULL);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* fetch64 - return a uint64_t value from kstat.
*
* The arguments are a kstat pointer, the value name,
* and a default value in case the lookup fails.
*/
static uint64_t
fetch64(kstat_t *ksp, char *value64, uint64_t def)
{
kstat_named_t *knp; /* Kstat named pointer */
/* try a lookup and return */
if ((knp = kstat_data_lookup(ksp, value64)) != NULL)
/* Rely on C type conversion to promote smaller size values */
switch (knp->data_type) {
case KSTAT_DATA_INT32:
return (knp->value.i32);
/*NOTREACHED*/
case KSTAT_DATA_UINT32:
return (knp->value.ui32);
/*NOTREACHED*/
case KSTAT_DATA_INT64:
return (knp->value.i64);
/*NOTREACHED*/
case KSTAT_DATA_UINT64:
return (knp->value.ui64);
/*NOTREACHED*/
}
return (def);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* fetch32 - return a uint32_t value from kstat.
*
* The arguments are a kstat pointer, the value name,
* and a default value in case the lookup fails.
*/
static uint32_t
fetch32(kstat_t *ksp, char *value, uint32_t def)
{
kstat_named_t *knp; /* Kstat named pointer */
/* try a lookup and return */
if ((knp = kstat_data_lookup(ksp, value)) != NULL)
return (knp->value.ui32);
return (def);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* fetch6432 - return a uint64_t or a uint32_t value from kstat.
*
* The arguments are a kstat pointer, a potential ui64 value name,
* a potential ui32 value name, and a default value in case both
* lookup fails. The ui64 value is attempted first.
*/
static uint64_t
fetch6432(kstat_t *ksp, char *value64, char *value, uint64_t def)
{
kstat_named_t *knp; /* Kstat named pointer */
/* try lookups and return */
if ((knp = kstat_data_lookup(ksp, value64)) != NULL)
return (knp->value.ui64);
if ((knp = kstat_data_lookup(ksp, value)) != NULL)
return (knp->value.ui32);
return (def);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* fetch_nocanput - return nocanput value, whose name(s) are driver-dependent.
*
* Most drivers have a kstat "nocanput", but the ce driver
* at least has "rx_nocanput" and "tx_nocanput"
*/
static uint32_t
fetch_nocanput(kstat_t *ksp, uint32_t def)
{
kstat_named_t *knp; /* Kstat named pointer */
uint32_t sum;
/* These should go in order of decreasing prevalence */
if ((knp = kstat_data_lookup(ksp, "norcvbuf")) != NULL)
return (knp->value.ui32);
if ((knp = kstat_data_lookup(ksp, "nocanput")) != NULL)
return (knp->value.ui32);
if ((knp = kstat_data_lookup(ksp, "rx_nocanput")) != NULL) {
sum = knp->value.ui32;
if ((knp = kstat_data_lookup(ksp, "tx_nocanput"))
!= NULL) {
sum += knp->value.ui32;
return (sum);
}
}
return (def);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
/*
* fetch_boot_time - return the boot time in secs.
*
* This takes a kstat control pointer and looks up the boot time
* from unix:0:system_misc:boot:time. If found, this is returned,
* else 0.
*/
static time_t
fetch_boot_time()
{
kstat_t *ksp; /* Kstat struct pointer */
kstat_named_t *knp; /* Kstat named pointer */
static time_t boot_time = 0; /* Cache it if we can */
if (boot_time != 0)
return (boot_time);
if ((ksp = kstat_lookup(g_kc, "unix", 0, "system_misc")) == NULL)
die(1, "kstat_lookup: unix:0:system_misc");
if ((kstat_read(g_kc, ksp, NULL) != -1) &&
((knp = kstat_data_lookup(ksp, "boot_time")) != NULL))
/* summary since boot */
boot_time = knp->value.ui32;
/* This will be zero if kstat_data_lookup() failed */
return (boot_time);
}
#endif /* OS_SOLARIS */
#ifdef OS_LINUX
/*
* fetch_boot_time - return the boot time in secs.
*
* Gets the boot time from /proc.
*/
static unsigned long
fetch_boot_time()
{
char buf[64];
int uptime_fd, bufsiz, scanned;
unsigned long uptime;
uptime_fd = open(PROC_UPTIME, O_RDONLY, 0);
if (uptime_fd < 0)
die(1, "error opening %s for read", PROC_UPTIME);
bufsiz = read(uptime_fd, buf, sizeof (buf) - 1);
if (bufsiz < 0)
die(1, "read: %s", PROC_UPTIME);
buf[bufsiz] = '\0';
scanned = sscanf(buf, "%lu.", &uptime);
if (scanned != 1)
die(0, "cannot get uptime from %s", PROC_UPTIME);
return (time(0) - uptime);
}
#endif /* OS_LINUX */
#ifdef OS_SOLARIS
static if_list_t *g_getif_list = NULL; /* Used by the lifc & dladm routines */
static if_list_t *
get_if_list_lifc(if_list_t *p)
{
if_list_t *newp, *headp;
struct lifnum if_num; /* Includes # of if's */
struct lifconf if_conf; /* Includes ptr to list of names */
static struct ifreq *current_lif = (struct ifreq *)NULL;
struct lifreq *if_reqp, req;
int lif_size, lif_count, i;
headp = p;
/* Get number of interfaces on system */
if_num.lifn_family = AF_UNSPEC;
if_num.lifn_flags = LIFC_NOXMIT | LIFC_ALLZONES | LIFC_UNDER_IPMP
| LIFC_ENABLED;
if (ioctl(g_sock, SIOCGLIFNUM, &if_num) < 0)
die(1, "ioctl(IFNUM)");
/* Allocate my struct ifreq array buffer */
lif_size = (if_num.lifn_count + 1) * sizeof (struct lifreq);
current_lif = realloc(current_lif, lif_size);
if (! current_lif)
die(1, "realloc");
/* Get the current interface list via the ioctl() */
if_conf.lifc_family = AF_UNSPEC;
if_conf.lifc_flags = if_num.lifn_flags;
if_conf.lifc_len = lif_size;
if_conf.lifc_buf = (caddr_t)current_lif;
if (ioctl(g_sock, SIOCGLIFCONF, &if_conf) < 0)
die(1, "ioctl(IFCONF)");
lif_size = if_conf.lifc_len;
lif_count = if_conf.lifc_len / sizeof (struct lifreq);
/*
* Loop through entries in lifc_req, making a list of interfaces
*/
if_reqp = if_conf.lifc_req;
(void) memset((void *) &req, 0, sizeof (struct lifreq));
for (i = lif_count; i; i--, if_reqp++) {
/* Skip virtual IP's */
if (strchr(if_reqp->lifr_name, ':'))
continue;
(void) strlcpy(req.lifr_name, if_reqp->lifr_name, LIFNAMSIZ);
/*
* Skip interface if "-i" was used, and it is not
* a matching interface
*/
if (if_is_ignored(if_reqp->lifr_name))
continue;
/* Add to list */
if (p->name) {
/* Need new tail */
newp = allocate(sizeof (if_list_t));
p->next = newp;
p = newp;
}
p->name = new_string(if_reqp->lifr_name);
}
return (headp);
}
#endif /* OS_SOLARIS */
#ifdef USE_DLADM
/*
* dladm_callback - Function called by dladm_walk_datalink_id() for each
* link.
*/
/* ARGSUSED */
static int
dladm_callback(dladm_handle_t dh, datalink_id_t linkid, void *arg)
{
dladm_status_t status;
char link[MAXLINKNAMELEN];
datalink_class_t class;
uint_t mtu;
uint32_t flags;
struct if_list *p, *newp;
if ((status = dladm_datalink_id2info(g_handle, linkid, &flags, &class,
NULL, link, sizeof (link))) != DLADM_STATUS_OK) {
return (status);
}
/*
* Skip interface if "-i" was used, and it is not
* a matching interface
*/
if (if_is_ignored(link))
return (DLADM_WALK_CONTINUE);
p = g_getif_list;
if (p->name) {
/* Need new tail */
newp = allocate(sizeof (struct if_list));
p->next = newp;
p = newp;
g_getif_list = newp;
}
p->name = new_string(link);
return (DLADM_WALK_CONTINUE);
}
/*
* Get the current list of interfaces
*/
static struct if_list *
get_if_list_dl(if_list_t *p)
{
uint32_t flags = DLADM_OPT_ACTIVE;
/* Start with "lo0" unless it is ignored */
if (! g_nonlocal && (! if_is_ignored("lo0"))) {
p->name = new_string("lo0");
p->next = (struct if_list *)NULL;
}
/* dladm_callback() will append entries to g_getif_list */
g_getif_list = p;
(void) dladm_walk_datalink_id(dladm_callback, g_handle,
NULL, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
flags);
return (g_getif_list = p);
}
#endif /* USE_DLADM */
#ifdef OS_SOLARIS
/*
* Get the list from kstats, looking for one of:
*
* Class 4-tuple
* ===== =======
* net link::<ifname>:link_state
* net <drv>:<inst>:mac:link_state
* mac <ifname>::<ifname>/xx:link_state
*
* with a value of "1".
*
* This is only useful on S10 or newer; where interfaces given
* exclusively to non-global zones may not be visible via
* get_if_list_lifc(); and where USE_DLADM is not available.
*/
static if_list_t *
get_if_list_kstat(if_list_t *p)
{
if_list_t *newp, *headp;
kstat_t *ksp;
kstat_named_t *knp;
char ifname[MAXLINKNAMELEN];
char *namep;
headp = p;
/* Start with "lo0" unless it is ignored */
if (! g_nonlocal && (! if_is_ignored("lo0"))) {
p->name = new_string("lo0");
p->next = (struct if_list *)NULL;
}
for (ksp = g_kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (ksp->ks_type != KSTAT_TYPE_NAMED)
continue;
if (streql(ksp->ks_class, "net")) {
if (streql(ksp->ks_module, "link")) {
namep = ksp->ks_name;
goto lookup;
}
if (streql(ksp->ks_name, "mac")) {
(void) sprintf(ifname, "%s%u", ksp->ks_module,
ksp->ks_instance);
namep = ifname;
goto lookup;
}
continue;
}
if (streql(ksp->ks_class, "mac")) {
namep = ksp->ks_module;
goto lookup;
}
continue;
lookup:
if (kstat_read(g_kc, ksp, NULL) < 0)
die(1, "kstat_read");
if (! (knp = kstat_data_lookup(ksp, "link_state")))
continue;
/* We have a "link_state" */
if (knp->data_type != KSTAT_DATA_UINT32)
continue;
if (knp->value.ui32 != 1)
continue;
/* We have a value of 1 - link is UP */
if (if_is_ignored(ifname))
continue;
/* Add to list */
if (p->name) {
/* Need new tail */
newp = allocate(sizeof (if_list_t));
p->next = newp;
p = newp;
}
p->name = new_string(namep);
}
return (headp);
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
static if_list_t *
get_if_list()
{
/* Free g_getif_list if needed */
if (g_getif_list) {
struct if_list *p, *next;
for (p = g_getif_list; p; ) {
next = p->next;
if (p->name)
free(p->name);
free(p);
p = next;
}
}
/* Allocate new g_getif_list */
g_getif_list = allocate(sizeof (if_list_t));
if (g_opt_k)
return (get_if_list_kstat(g_getif_list));
#ifdef USE_DLADM
if (g_use_dladm)
return (get_if_list_dl(g_getif_list));
#endif
return (get_if_list_lifc(g_getif_list));
}
#endif /* OS_SOLARIS */
#ifdef OS_SOLARIS
static LIFR_FLAGS_TYPE
get_lif_flags(char *if_name)
{
struct lifreq req;
(void) strlcpy(req.lifr_name, if_name, LIFNAMSIZ);
if (ioctl(g_sock, SIOCGLIFINDEX, &req) == -1) {
return (0);
}
if (ioctl(g_sock, SIOCGLIFFLAGS, &req) == -1) {
return (0);
}
return (req.lifr_flags);
}
/*
* split_ifname()
*
* Splits interface names like "bge0", "e1000g7001" into driver/module name
* and instance number. The instance number is the largest set of trailing
* digits.
*/
static int
split_ifname(char *if_name, char *drv, uint32_t *instance)
{
char *p;
int n, m;
n = 0;
for (p = if_name; *p; p++)
n++;
if (n <= 1)
return (B_FALSE);
m = n;
for (p--; isdigit(*p); p--)
n--;
if (m == n || n == 0)
return (B_FALSE);
(void) strncpy(drv, if_name, n);
drv[n] = '\0';
*instance = (uint32_t)atol(++p);
return (B_TRUE);
}
/*
* OUTPUTS
* nic->ls_ksp
* nic->op_ksp
* nic->flags (NIC_KS_UP bit)
*/
static nicdata_t *
discover_kstats(char *if_name, nicdata_t *nic)
{
uint32_t if_instance;
kstat_t *ksp;
kstat_named_t *knp;
int ks_link_state; /* :::link_state */
int ks_opackets; /* :::opackets */
int ks_link_module; /* link::: */
int ks_drv_module; /* <drv>::: */
int ks_ifname_module; /* <ifname>::: */
uint32_t n;
uint32_t ttype;
char if_drv[MAXLINKNAMELEN];
if (! split_ifname(if_name, if_drv, &if_instance))
die(0, "%s: %s: invalid interface name\n", g_progname,
if_name);
nic->ls_ksp = NULL;
nic->op_ksp = NULL;
for (ksp = g_kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
if (ksp->ks_type != KSTAT_TYPE_NAMED)
continue;
if (strcmp(ksp->ks_class, "net") != 0)
continue;
ks_link_module = ks_drv_module = ks_ifname_module = B_FALSE;
if (streql(ksp->ks_module, "link"))
ks_link_module = B_TRUE;
else if (streql(ksp->ks_module, if_drv) &&
(ksp->ks_instance == if_instance))
ks_drv_module = B_TRUE;
else if (streql(ksp->ks_module, if_name))
ks_ifname_module = B_TRUE;
else
continue;
/* We have [link:::], [<drv>:<instance>::] or [<ifname>:::] */
(void) kstat_read(g_kc, ksp, NULL);