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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <sys/note.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <libgen.h>
#include <kstat.h>
#include <ofmt.h>
#include <libilb.h>
#include "ilbadm.h"
#define ILBST_TIMESTAMP_HEADER 0x01 /* a timestamp w. every header */
#define ILBST_DELTA_INTERVAL 0x02 /* delta over specified interval */
#define ILBST_ABS_NUMBERS 0x04 /* print absolute numbers, no d's */
#define ILBST_ITEMIZE 0x08 /* itemize */
#define ILBST_VERBOSE 0x10 /* verbose error info */
#define ILBST_OLD_VALUES 0x20 /* for internal processing */
#define ILBST_RULES_CHANGED 0x40
typedef struct {
char is_name[KSTAT_STRLEN];
uint64_t is_value;
} ilbst_stat_t;
static ilbst_stat_t rulestats[] = {
{"num_servers", 0},
{"bytes_not_processed", 0},
{"pkt_not_processed", 0},
{"bytes_dropped", 0},
{"pkt_dropped", 0},
{"nomem_bytes_dropped", 0},
{"nomem_pkt_dropped", 0},
{"noport_bytes_dropped", 0},
{"noport_pkt_dropped", 0},
{"icmp_echo_processed", 0},
{"icmp_dropped", 0},
{"icmp_too_big_processed", 0},
{"icmp_too_big_dropped", 0}
};
/* indices into array above, to avoid searching */
#define RLSTA_NUM_SRV 0
#define RLSTA_BYTES_U 1
#define RLSTA_PKT_U 2
#define RLSTA_BYTES_D 3
#define RLSTA_PKT_D 4
#define RLSTA_NOMEMBYTES_D 5
#define RLSTA_NOMEMPKT_D 6
#define RLSTA_NOPORTBYTES_D 7
#define RLSTA_NOPORTPKT_D 8
#define RLSTA_ICMP_P 9
#define RLSTA_ICMP_D 10
#define RLSTA_ICMP2BIG_P 11
#define RLSTA_ICMP2BIG_D 12
static ilbst_stat_t servstats[] = {
{"bytes_processed", 0},
{"pkt_processed", 0}
};
/* indices into array above, to avoid searching */
#define SRVST_BYTES_P 0
#define SRVST_PKT_P 1
/* values used for of_* commands as id */
#define ILBST_PKT_P 0
#define ILBST_BYTES_P 1
#define ILBST_PKT_U 2
#define ILBST_BYTES_U 3
#define ILBST_PKT_D 4
#define ILBST_BYTES_D 5
#define ILBST_ICMP_P 6
#define ILBST_ICMP_D 7
#define ILBST_ICMP2BIG_P 8
#define ILBST_ICMP2BIG_D 9
#define ILBST_NOMEMP_D 10
#define ILBST_NOPORTP_D 11
#define ILBST_NOMEMB_D 12
#define ILBST_NOPORTB_D 13
#define ILBST_ITEMIZE_SNAME 97
#define ILBST_ITEMIZE_RNAME 98
#define ILBST_TIMESTAMP 99
/* approx field widths */
#define ILBST_PKTCTR_W 8
#define ILBST_BYTECTR_W 10
#define ILBST_TIME_W 15
static boolean_t of_rule_stats(ofmt_arg_t *, char *, uint_t);
static boolean_t of_server_stats(ofmt_arg_t *, char *, uint_t);
static boolean_t of_itemize_stats(ofmt_arg_t *, char *, uint_t);
static boolean_t of_timestamp(ofmt_arg_t *, char *, uint_t);
static ofmt_field_t stat_itemize_fields[] = {
{"RULENAME", ILB_NAMESZ, ILBST_ITEMIZE_RNAME, of_itemize_stats},
{"SERVERNAME", ILB_NAMESZ, ILBST_ITEMIZE_SNAME, of_itemize_stats},
{"PKT_P", ILBST_PKTCTR_W, ILBST_PKT_P, of_itemize_stats},
{"BYTES_P", ILBST_BYTECTR_W, ILBST_BYTES_P, of_itemize_stats},
{"TIME", ILBST_TIME_W, ILBST_TIMESTAMP, of_timestamp},
{NULL, 0, 0, NULL}
};
static ofmt_field_t stat_stdfields[] = {
{"PKT_P", ILBST_PKTCTR_W, ILBST_PKT_P, of_server_stats},
{"BYTES_P", ILBST_BYTECTR_W, ILBST_BYTES_P, of_server_stats},
{"PKT_U", ILBST_PKTCTR_W, ILBST_PKT_U, of_rule_stats},
{"BYTES_U", ILBST_BYTECTR_W, ILBST_BYTES_U, of_rule_stats},
{"PKT_D", ILBST_PKTCTR_W, ILBST_PKT_D, of_rule_stats},
{"BYTES_D", ILBST_BYTECTR_W, ILBST_BYTES_D, of_rule_stats},
{"ICMP_P", ILBST_PKTCTR_W, ILBST_ICMP_P, of_rule_stats},
{"ICMP_D", ILBST_PKTCTR_W, ILBST_ICMP_D, of_rule_stats},
{"ICMP2BIG_P", 11, ILBST_ICMP2BIG_P, of_rule_stats},
{"ICMP2BIG_D", 11, ILBST_ICMP2BIG_D, of_rule_stats},
{"NOMEMP_D", ILBST_PKTCTR_W, ILBST_NOMEMP_D, of_rule_stats},
{"NOPORTP_D", ILBST_PKTCTR_W, ILBST_NOPORTP_D, of_rule_stats},
{"NOMEMB_D", ILBST_PKTCTR_W, ILBST_NOMEMB_D, of_rule_stats},
{"NOPORTB_D", ILBST_PKTCTR_W, ILBST_NOPORTB_D, of_rule_stats},
{"TIME", ILBST_TIME_W, ILBST_TIMESTAMP, of_timestamp},
{NULL, 0, 0, NULL}
};
static char stat_stdhdrs[] = "PKT_P,BYTES_P,PKT_U,BYTES_U,PKT_D,BYTES_D";
static char stat_stdv_hdrs[] = "PKT_P,BYTES_P,PKT_U,BYTES_U,PKT_D,BYTES_D,"
"ICMP_P,ICMP_D,ICMP2BIG_P,ICMP2BIG_D,NOMEMP_D,NOPORTP_D";
static char stat_itemize_rule_hdrs[] = "SERVERNAME,PKT_P,BYTES_P";
static char stat_itemize_server_hdrs[] = "RULENAME,PKT_P,BYTES_P";
#define RSTAT_SZ (sizeof (rulestats)/sizeof (rulestats[0]))
#define SSTAT_SZ (sizeof (servstats)/sizeof (servstats[0]))
typedef struct {
char isd_servername[KSTAT_STRLEN]; /* serverID */
ilbst_stat_t isd_serverstats[SSTAT_SZ];
hrtime_t isd_crtime; /* save for comparison purpose */
} ilbst_srv_desc_t;
/*
* this data structure stores statistics for a rule - both an old set
* and a current/new set. we use pointers to the actual stores and switch
* the pointers for every round. old_is_old in ilbst_arg_t indicates
* which pointer points to the "old" data struct (ie, if true, _o pointer
* points to old)
*/
typedef struct {
char ird_rulename[KSTAT_STRLEN];
int ird_num_servers;
int ird_num_servers_o;
int ird_srv_ind;
hrtime_t ird_crtime; /* save for comparison */
hrtime_t ird_crtime_o; /* save for comparison */
ilbst_srv_desc_t *ird_srvlist;
ilbst_srv_desc_t *ird_srvlist_o;
ilbst_stat_t ird_rstats[RSTAT_SZ];
ilbst_stat_t ird_rstats_o[RSTAT_SZ];
ilbst_stat_t *ird_rulestats;
ilbst_stat_t *ird_rulestats_o;
} ilbst_rule_desc_t;
/*
* overall "container" for information pertaining to statistics, and
* how to display them.
*/
typedef struct {
int ilbst_flags;
/* fields representing user input */
char *ilbst_rulename; /* optional */
char *ilbst_server; /* optional */
int ilbst_interval;
int ilbst_count;
/* "internal" fields for data and data presentation */
ofmt_handle_t ilbst_oh;
boolean_t ilbst_old_is_old;
ilbst_rule_desc_t *ilbst_rlist;
int ilbst_rcount; /* current list count */
int ilbst_rcount_prev; /* prev (different) count */
int ilbst_rlist_sz; /* number of alloc'ed rules */
int ilbst_rule_index; /* for itemizes display */
} ilbst_arg_t;
/* ARGSUSED */
static boolean_t
of_timestamp(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
{
time_t now;
struct tm *now_tm;
now = time(NULL);
now_tm = localtime(&now);
(void) strftime(buf, bufsize, "%F:%H.%M.%S", now_tm);
return (B_TRUE);
}
static boolean_t
i_sum_per_rule_processed(ilbst_rule_desc_t *rp, uint64_t *resp, int index,
int flags)
{
int i, num_servers;
ilbst_srv_desc_t *srv, *o_srv, *n_srv;
uint64_t res = 0;
boolean_t valid = B_TRUE;
boolean_t old = flags & ILBST_OLD_VALUES;
boolean_t check_valid;
/* if we do abs. numbers, we never look at the _o fields */
assert((old && (flags & ILBST_ABS_NUMBERS)) == B_FALSE);
/* we only check for validity under certain conditions */
check_valid = !(old || (flags & ILBST_ABS_NUMBERS));
if (check_valid && rp->ird_num_servers != rp->ird_num_servers_o)
valid = B_FALSE;
num_servers = old ? rp->ird_num_servers_o : rp->ird_num_servers;
for (i = 0; i < num_servers; i++) {
n_srv = &rp->ird_srvlist[i];
o_srv = &rp->ird_srvlist_o[i];
if (old)
srv = o_srv;
else
srv = n_srv;
res += srv->isd_serverstats[index].is_value;
/*
* if creation times don't match, comparison is wrong; if
* if we already know something is invalid, we don't
* need to compare again.
*/
if (check_valid && valid == B_TRUE &&
o_srv->isd_crtime != n_srv->isd_crtime) {
valid = B_FALSE;
break;
}
}
/*
* save the result even though it may be imprecise - let the
* caller decide what to do
*/
*resp = res;
return (valid);
}
typedef boolean_t (*sumfunc_t)(ilbst_rule_desc_t *, uint64_t *, int);
static boolean_t
i_sum_per_rule_pkt_p(ilbst_rule_desc_t *rp, uint64_t *resp, int flags)
{
return (i_sum_per_rule_processed(rp, resp, SRVST_PKT_P, flags));
}
static boolean_t
i_sum_per_rule_bytes_p(ilbst_rule_desc_t *rp, uint64_t *resp, int flags)
{
return (i_sum_per_rule_processed(rp, resp, SRVST_BYTES_P, flags));
}
static boolean_t
of_server_stats(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
{
ilbst_arg_t *sta = (ilbst_arg_t *)of_arg->ofmt_cbarg;
uint64_t count = 0, val;
int i;
boolean_t valid = B_TRUE;
sumfunc_t sumfunc;
switch (of_arg->ofmt_id) {
case ILBST_PKT_P: sumfunc = i_sum_per_rule_pkt_p;
break;
case ILBST_BYTES_P: sumfunc = i_sum_per_rule_bytes_p;
break;
}
for (i = 0; i < sta->ilbst_rcount; i++) {
valid = sumfunc(&sta->ilbst_rlist[i], &val, sta->ilbst_flags);
if (!valid)
return (valid);
count += val;
}
if ((sta->ilbst_flags & ILBST_ABS_NUMBERS) != 0)
goto out;
for (i = 0; i < sta->ilbst_rcount; i++) {
(void) sumfunc(&sta->ilbst_rlist[i], &val,
sta->ilbst_flags | ILBST_OLD_VALUES);
count -= val;
}
out:
/*
* normally, we print "change per second", which we calculate
* here. otherwise, we print "change over interval"
*/
if ((sta->ilbst_flags & (ILBST_DELTA_INTERVAL|ILBST_ABS_NUMBERS)) == 0)
count /= sta->ilbst_interval;
(void) snprintf(buf, bufsize, "%llu", count);
return (B_TRUE);
}
/*
* this function is called when user wants itemized stats of every
* server for a named rule, or vice vera.
* i_do_print sets sta->rule_index and the proper ird_srv_ind so
* we don't have to differentiate between these two cases here.
*/
static boolean_t
of_itemize_stats(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
{
ilbst_arg_t *sta = (ilbst_arg_t *)of_arg->ofmt_cbarg;
int stat_ind;
uint64_t count;
int rule_index = sta->ilbst_rule_index;
int srv_ind = sta->ilbst_rlist[rule_index].ird_srv_ind;
boolean_t ret = B_TRUE;
ilbst_srv_desc_t *srv, *osrv;
srv = &sta->ilbst_rlist[rule_index].ird_srvlist[srv_ind];
switch (of_arg->ofmt_id) {
case ILBST_PKT_P: stat_ind = SRVST_PKT_P;
break;
case ILBST_BYTES_P: stat_ind = SRVST_BYTES_P;
break;
case ILBST_ITEMIZE_RNAME:
(void) snprintf(buf, bufsize, "%s",
sta->ilbst_rlist[rule_index].ird_rulename);
return (B_TRUE);
case ILBST_ITEMIZE_SNAME:
(void) snprintf(buf, bufsize, "%s", srv->isd_servername);
return (B_TRUE);
}
count = srv->isd_serverstats[stat_ind].is_value;
if ((sta->ilbst_flags & ILBST_ABS_NUMBERS) != 0)
goto out;
osrv = &sta->ilbst_rlist[rule_index].ird_srvlist_o[srv_ind];
if (srv->isd_crtime != osrv->isd_crtime)
ret = B_FALSE;
count -= osrv->isd_serverstats[stat_ind].is_value;
out:
/*
* normally, we print "change per second", which we calculate
* here. otherwise, we print "change over interval" or absolute
* values.
*/
if ((sta->ilbst_flags & (ILBST_DELTA_INTERVAL|ILBST_ABS_NUMBERS)) == 0)
count /= sta->ilbst_interval;
(void) snprintf(buf, bufsize, "%llu", count);
return (ret);
}
static boolean_t
of_rule_stats(ofmt_arg_t *of_arg, char *buf, uint_t bufsize)
{
ilbst_arg_t *sta = (ilbst_arg_t *)of_arg->ofmt_cbarg;
int i, ind;
uint64_t count = 0;
switch (of_arg->ofmt_id) {
case ILBST_PKT_U: ind = RLSTA_PKT_U;
break;
case ILBST_BYTES_U: ind = RLSTA_BYTES_U;
break;
case ILBST_PKT_D: ind = RLSTA_PKT_D;
break;
case ILBST_BYTES_D: ind = RLSTA_BYTES_D;
break;
case ILBST_ICMP_P: ind = RLSTA_ICMP_P;
break;
case ILBST_ICMP_D: ind = RLSTA_ICMP_D;
break;
case ILBST_ICMP2BIG_P: ind = RLSTA_ICMP2BIG_P;
break;
case ILBST_ICMP2BIG_D: ind = RLSTA_ICMP2BIG_D;
break;
case ILBST_NOMEMP_D: ind = RLSTA_NOMEMPKT_D;
break;
case ILBST_NOPORTP_D: ind = RLSTA_NOPORTPKT_D;
break;
case ILBST_NOMEMB_D: ind = RLSTA_NOMEMBYTES_D;
break;
case ILBST_NOPORTB_D: ind = RLSTA_NOPORTBYTES_D;
break;
}
for (i = 0; i < sta->ilbst_rcount; i++)
count += sta->ilbst_rlist[i].ird_rulestats[ind].is_value;
if ((sta->ilbst_flags & ILBST_ABS_NUMBERS) != 0)
goto out;
/*
* the purist approach: if we can't say 100% that what we
* calculate is correct, don't.
*/
if (sta->ilbst_flags & ILBST_RULES_CHANGED)
return (B_FALSE);
for (i = 0; i < sta->ilbst_rcount; i++) {
if (sta->ilbst_rlist[i].ird_crtime_o != 0 &&
sta->ilbst_rlist[i].ird_crtime !=
sta->ilbst_rlist[i].ird_crtime_o)
return (B_FALSE);
count -= sta->ilbst_rlist[i].ird_rulestats_o[ind].is_value;
}
out:
/*
* normally, we print "change per second", which we calculate
* here. otherwise, we print "change over interval"
*/
if ((sta->ilbst_flags & (ILBST_DELTA_INTERVAL|ILBST_ABS_NUMBERS)) == 0)
count /= sta->ilbst_interval;
(void) snprintf(buf, bufsize, "%llu", count);
return (B_TRUE);
}
/*
* Get the number of kstat instances. Note that when rules are being
* drained the number of kstats instances may be different than the
* kstat counter num_rules (ilb:0:global:num_rules").
*
* Also there can be multiple instances of a rule in the following
* scenario:
*
* A rule named rule A has been deleted but remains in kstats because
* its undergoing connection draining. During this time, the user adds
* a new rule with the same name(rule A). In this case, there would
* be two kstats instances for rule A. Currently ilbadm's aggregate
* results will include data from both instances of rule A. In,
* future we should have ilbadm stats only consider the latest instance
* of the rule (ie only consider the the instance that corresponds
* to the rule that was just added).
*
*/
static int
i_get_num_kinstances(kstat_ctl_t *kctl)
{
kstat_t *kp;
int num_instances = 0; /* nothing found, 0 rules */
for (kp = kctl->kc_chain; kp != NULL; kp = kp->ks_next) {
if (strncmp("rulestat", kp->ks_class, 8) == 0 &&
strncmp("ilb", kp->ks_module, 3) == 0) {
num_instances++;
}
}
return (num_instances);
}
/*
* since server stat's classname is made up of <rulename>-sstat,
* we walk the rule list to construct the comparison
* Return: pointer to rule whose name matches the class
* NULL if no match
*/
static ilbst_rule_desc_t *
match_2_rnames(char *class, ilbst_rule_desc_t *rlist, int rcount)
{
int i;
char classname[KSTAT_STRLEN];
for (i = 0; i < rcount; i++) {
(void) snprintf(classname, sizeof (classname), "%s-sstat",
rlist[i].ird_rulename);
if (strncmp(classname, class, sizeof (classname)) == 0)
return (&rlist[i]);
}
return (NULL);
}
static int
i_stat_index(kstat_named_t *knp, ilbst_stat_t *stats, int count)
{
int i;
for (i = 0; i < count; i++) {
if (strcasecmp(stats[i].is_name, knp->name) == 0)
return (i);
}
return (-1);
}
static void
i_copy_sstats(ilbst_srv_desc_t *sp, kstat_t *kp)
{
kstat_named_t *knp;
int i, ind;
knp = KSTAT_NAMED_PTR(kp);
for (i = 0; i < kp->ks_ndata; i++, knp++) {
ind = i_stat_index(knp, servstats, SSTAT_SZ);
if (ind == -1)
continue;
(void) strlcpy(sp->isd_serverstats[ind].is_name, knp->name,
sizeof (sp->isd_serverstats[ind].is_name));
sp->isd_serverstats[ind].is_value = knp->value.ui64;
sp->isd_crtime = kp->ks_crtime;
}
}
static ilbadm_status_t
i_get_server_descs(ilbst_arg_t *sta, kstat_ctl_t *kctl)
{
ilbadm_status_t rc = ILBADM_OK;
kstat_t *kp;
int i = -1;
ilbst_rule_desc_t *rp;
ilbst_rule_desc_t *rlist = sta->ilbst_rlist;
int rcount = sta->ilbst_rcount;
/*
* find all "server" kstats, or the one specified in
* sta->server
*/
for (kp = kctl->kc_chain; kp != NULL; kp = kp->ks_next) {
if (strncmp("ilb", kp->ks_module, 3) != 0)
continue;
if (sta->ilbst_server != NULL &&
strcasecmp(sta->ilbst_server, kp->ks_name) != 0)
continue;
rp = match_2_rnames(kp->ks_class, rlist, rcount);
if (rp == NULL)
continue;
(void) kstat_read(kctl, kp, NULL);
i = rp->ird_srv_ind++;
rc = ILBADM_OK;
/*
* This means that a server is added after we check last
* time... Just make the array bigger.
*/
if (i+1 > rp->ird_num_servers) {
ilbst_srv_desc_t *srvlist;
if ((srvlist = realloc(rp->ird_srvlist, (i+1) *
sizeof (*srvlist))) == NULL) {
rc = ILBADM_ENOMEM;
break;
}
rp->ird_srvlist = srvlist;
rp->ird_num_servers = i;
}
(void) strlcpy(rp->ird_srvlist[i].isd_servername, kp->ks_name,
sizeof (rp->ird_srvlist[i].isd_servername));
i_copy_sstats(&rp->ird_srvlist[i], kp);
}
for (i = 0; i < rcount; i++)
rlist[i].ird_srv_ind = 0;
if (sta->ilbst_server != NULL && i == -1)
rc = ILBADM_ENOSERVER;
return (rc);
}
static void
i_copy_rstats(ilbst_rule_desc_t *rp, kstat_t *kp)
{
kstat_named_t *knp;
int i, ind;
knp = KSTAT_NAMED_PTR(kp);
for (i = 0; i < kp->ks_ndata; i++, knp++) {
ind = i_stat_index(knp, rulestats, RSTAT_SZ);
if (ind == -1)
continue;
(void) strlcpy(rp->ird_rulestats[ind].is_name, knp->name,
sizeof (rp->ird_rulestats[ind].is_name));
rp->ird_rulestats[ind].is_value = knp->value.ui64;
}
}
static void
i_set_rlstats_ptr(ilbst_rule_desc_t *rp, boolean_t old_is_old)
{
if (old_is_old) {
rp->ird_rulestats = rp->ird_rstats;
rp->ird_rulestats_o = rp->ird_rstats_o;
} else {
rp->ird_rulestats = rp->ird_rstats_o;
rp->ird_rulestats_o = rp->ird_rstats;
}
}
/*
* this function walks the array of rules and switches pointer to old
* and new stats as well as serverlists.
*/
static void
i_swap_rl_pointers(ilbst_arg_t *sta, int rcount)
{
int i, tmp_num;
ilbst_rule_desc_t *rlist = sta->ilbst_rlist;
ilbst_srv_desc_t *tmp_srv;
for (i = 0; i < rcount; i++) {
/* swap srvlist pointers */
tmp_srv = rlist[i].ird_srvlist;
rlist[i].ird_srvlist = rlist[i].ird_srvlist_o;
rlist[i].ird_srvlist_o = tmp_srv;
/*
* swap server counts - we need the old one to
* save reallocation calls
*/
tmp_num = rlist[i].ird_num_servers_o;
rlist[i].ird_num_servers_o = rlist[i].ird_num_servers;
rlist[i].ird_num_servers = tmp_num;
/* preserve creation time */
rlist[i].ird_crtime_o = rlist[i].ird_crtime;
i_set_rlstats_ptr(&rlist[i], sta->ilbst_old_is_old);
rlist[i].ird_srv_ind = 0;
}
}
static void
i_init_rulelist(ilbst_arg_t *sta, int rcount)
{
int i;
ilbst_rule_desc_t *rlist = sta->ilbst_rlist;
for (i = 0; i < rcount; i++) {
rlist[i].ird_rulestats = rlist[i].ird_rstats;
rlist[i].ird_rulestats_o = rlist[i].ird_rstats_o;
rlist[i].ird_srv_ind = 0;
}
}
/*
* this function searches for kstats describing individual rules and
* saves name, # of servers, and the kstat_t * describing them (this is
* for sta->rulename == NULL);
* if sta->rulename != NULL, it names the rule we're looking for
* and this function will fill in the other data (like the all_rules case)
* Returns: ILBADM_ENORULE named rule not found
* ILBADM_ENOMEM no mem. available
*/
static ilbadm_status_t
i_get_rule_descs(ilbst_arg_t *sta, kstat_ctl_t *kctl)
{
ilbadm_status_t rc = ILBADM_OK;
kstat_t *kp;
kstat_named_t *knp;
int i;
int num_servers;
ilbst_rule_desc_t *rlist = sta->ilbst_rlist;
int rcount = sta->ilbst_rcount;
/*
* find all "rule" kstats, or the one specified in
* sta->ilbst_rulename.
*/
for (i = 0, kp = kctl->kc_chain; i < rcount && kp != NULL;
kp = kp->ks_next) {
if (strncmp("rulestat", kp->ks_class, 8) != 0 ||
strncmp("ilb", kp->ks_module, 3) != 0)
continue;
(void) kstat_read(kctl, kp, NULL);
knp = kstat_data_lookup(kp, "num_servers");
if (knp == NULL) {
ilbadm_err(gettext("kstat_data_lookup() failed: %s"),
strerror(errno));
rc = ILBADM_LIBERR;
break;
}
if (sta->ilbst_rulename != NULL) {
if (strcasecmp(kp->ks_name, sta->ilbst_rulename)
!= 0)
continue;
}
(void) strlcpy(rlist[i].ird_rulename, kp->ks_name,
sizeof (rlist[i].ird_rulename));
/* only alloc the space we need, set counter here ... */
if (sta->ilbst_server != NULL)
num_servers = 1;
else
num_servers = (int)knp->value.ui64;
/* ... furthermore, only reallocate if necessary */
if (num_servers != rlist[i].ird_num_servers) {
ilbst_srv_desc_t *srvlist;
rlist[i].ird_num_servers = num_servers;
if (rlist[i].ird_srvlist == NULL)
srvlist = calloc(num_servers,
sizeof (*srvlist));
else
srvlist = realloc(rlist[i].ird_srvlist,
sizeof (*srvlist) * num_servers);
if (srvlist == NULL) {
rc = ILBADM_ENOMEM;
break;
}
rlist[i].ird_srvlist = srvlist;
}
rlist[i].ird_srv_ind = 0;
rlist[i].ird_crtime = kp->ks_crtime;
i_copy_rstats(&rlist[i], kp);
i++;
/* if we know we're done, return */
if (sta->ilbst_rulename != NULL || i == rcount) {
rc = ILBADM_OK;
break;
}
}
if (sta->ilbst_rulename != NULL && i == 0)
rc = ILBADM_ENORULE;
return (rc);
}
static void
i_do_print(ilbst_arg_t *sta)
{
int i;
/* non-itemized display can go right ahead */
if ((sta->ilbst_flags & ILBST_ITEMIZE) == 0) {
ofmt_print(sta->ilbst_oh, sta);
return;
}
/*
* rulename is given, list a line per server
* here's how we do it:
* the _ITEMIZE flag indicates to the print function (called
* from ofmt_print()) to look at server [ird_srv_ind] only.
*/
if (sta->ilbst_rulename != NULL) {
sta->ilbst_rule_index = 0;
for (i = 0; i < sta->ilbst_rlist->ird_num_servers; i++) {
sta->ilbst_rlist->ird_srv_ind = i;
ofmt_print(sta->ilbst_oh, sta);
}
sta->ilbst_rlist->ird_srv_ind = 0;
return;
}
/* list one line for every rule for a given server */
for (i = 0; i < sta->ilbst_rcount; i++) {
/*
* if a rule doesn't contain a given server, there's no
* need to print it. Luckily, we can check that
* fairly easily
*/
if (sta->ilbst_rlist[i].ird_srvlist[0].isd_servername[0] ==
'\0')
continue;
sta->ilbst_rule_index = i;
sta->ilbst_rlist[i].ird_srv_ind = 0;
ofmt_print(sta->ilbst_oh, sta);
}
sta->ilbst_rule_index = 0;
}
static ilbadm_status_t
i_do_show_stats(ilbst_arg_t *sta)
{
kstat_ctl_t *kctl;
kid_t nkid;
int rcount = 1, i;
ilbadm_status_t rc = ILBADM_OK;
ilbst_rule_desc_t *rlist, *rp;
boolean_t pseudo_abs = B_FALSE; /* for first pass */
if ((kctl = kstat_open()) == NULL) {
ilbadm_err(gettext("kstat_open() failed: %s"), strerror(errno));
return (ILBADM_LIBERR);
}
if (sta->ilbst_rulename == NULL)
rcount = i_get_num_kinstances(kctl);
rlist = calloc(sizeof (*rlist), rcount);
if (rlist == NULL) {
rc = ILBADM_ENOMEM;
goto out;
}
sta->ilbst_old_is_old = B_TRUE;
sta->ilbst_rlist = rlist;
sta->ilbst_rcount = sta->ilbst_rcount_prev = rcount;
sta->ilbst_rlist_sz = rcount;
/*
* in the first pass, we always print absolute numbers. We
* need to remember whether we wanted abs. numbers for
* other samples as well
*/
if ((sta->ilbst_flags & ILBST_ABS_NUMBERS) == 0) {
sta->ilbst_flags |= ILBST_ABS_NUMBERS;
pseudo_abs = B_TRUE;
}
i_init_rulelist(sta, rcount);
do {
rc = i_get_rule_descs(sta, kctl);
if (rc != ILBADM_OK)
goto out;
rc = i_get_server_descs(sta, kctl);
if (rc != ILBADM_OK)
goto out;
i_do_print(sta);
if (sta->ilbst_count == -1 || --(sta->ilbst_count) > 0)
(void) sleep(sta->ilbst_interval);
else
break;
nkid = kstat_chain_update(kctl);
sta->ilbst_flags &= ~ILBST_RULES_CHANGED;
/*
* we only need to continue with most of the rest of this if
* the kstat chain id has changed
*/
if (nkid == 0)
goto swap_old_new;
if (nkid == -1) {
ilbadm_err(gettext("kstat_chain_update() failed: %s"),
strerror(errno));
rc = ILBADM_LIBERR;
break;
}
/*
* find out whether the number of rules has changed.
* if so, adjust rcount and _o; if number has increased,
* expand array to hold all rules.
* we only shrink if rlist_sz is larger than both rcount and
* rcount_prev;
*/
if (sta->ilbst_rulename == NULL)
rcount = i_get_num_kinstances(kctl);
if (rcount != sta->ilbst_rcount) {
sta->ilbst_flags |= ILBST_RULES_CHANGED;
sta->ilbst_rcount_prev = sta->ilbst_rcount;
sta->ilbst_rcount = rcount;
if (rcount > sta->ilbst_rcount_prev) {
rlist = realloc(sta->ilbst_rlist,
sizeof (*sta->ilbst_rlist) * rcount);
if (rlist == NULL) {
rc = ILBADM_ENOMEM;
break;
}
sta->ilbst_rlist = rlist;
/* realloc doesn't zero out memory */
for (i = sta->ilbst_rcount_prev;
i < rcount; i++) {
rp = &sta->ilbst_rlist[i];
bzero(rp, sizeof (*rp));
i_set_rlstats_ptr(rp,
sta->ilbst_old_is_old);
}
/*
* even if rlist_sz was > rcount, it's now
* shrunk to rcount
*/
sta->ilbst_rlist_sz = sta->ilbst_rcount;
}
}
/*
* we may need to shrink the allocated slots down to the
* actually required number - we need to make sure we
* don't delete old or new stats.
*/
if (sta->ilbst_rlist_sz > MAX(sta->ilbst_rcount,
sta->ilbst_rcount_prev)) {
sta->ilbst_rlist_sz =
MAX(sta->ilbst_rcount, sta->ilbst_rcount_prev);
rlist = realloc(sta->ilbst_rlist,
sizeof (*sta->ilbst_rlist) * sta->ilbst_rlist_sz);
if (rlist == NULL) {
rc = ILBADM_ENOMEM;
break;
}
sta->ilbst_rlist = rlist;
}
/*
* move pointers around so what used to point to "old"
* stats now points to new, and vice versa
* if we're printing absolute numbers, this rigmarole is
* not necessary.
*/
swap_old_new:
if (pseudo_abs)
sta->ilbst_flags &= ~ILBST_ABS_NUMBERS;
if ((sta->ilbst_flags & ILBST_ABS_NUMBERS) == 0) {
sta->ilbst_old_is_old = !sta->ilbst_old_is_old;
i_swap_rl_pointers(sta, rcount);
}
_NOTE(CONSTCOND)
} while (B_TRUE);
out:
(void) kstat_close(kctl);
if ((rc != ILBADM_OK) && (rc != ILBADM_LIBERR))
ilbadm_err(ilbadm_errstr(rc));
if (sta->ilbst_rlist != NULL)
free(sta->ilbst_rlist);
return (rc);
}
/*
* read ilb's kernel statistics and (periodically) display
* them.
*/
/* ARGSUSED */
ilbadm_status_t
ilbadm_show_stats(int argc, char *argv[])
{
ilbadm_status_t rc;
int c;
ilbst_arg_t sta;
int oflags = 0;
char *fieldnames = stat_stdhdrs;
ofmt_field_t *fields = stat_stdfields;
boolean_t r_opt = B_FALSE, s_opt = B_FALSE, i_opt = B_FALSE;
boolean_t o_opt = B_FALSE, p_opt = B_FALSE, t_opt = B_FALSE;
boolean_t v_opt = B_FALSE, A_opt = B_FALSE, d_opt = B_FALSE;
ofmt_status_t oerr;
ofmt_handle_t oh = NULL;
bzero(&sta, sizeof (sta));
sta.ilbst_interval = 1;
sta.ilbst_count = 1;
while ((c = getopt(argc, argv, ":tdAr:s:ivo:p")) != -1) {
switch ((char)c) {
case 't': sta.ilbst_flags |= ILBST_TIMESTAMP_HEADER;
t_opt = B_TRUE;
break;
case 'd': sta.ilbst_flags |= ILBST_DELTA_INTERVAL;
d_opt = B_TRUE;
break;
case 'A': sta.ilbst_flags |= ILBST_ABS_NUMBERS;
A_opt = B_TRUE;
break;
case 'r': sta.ilbst_rulename = optarg;
r_opt = B_TRUE;
break;
case 's': sta.ilbst_server = optarg;
s_opt = B_TRUE;
break;
case 'i': sta.ilbst_flags |= ILBST_ITEMIZE;
i_opt = B_TRUE;
break;
case 'o': fieldnames = optarg;
o_opt = B_TRUE;
break;
case 'p': oflags |= OFMT_PARSABLE;
p_opt = B_TRUE;
break;
case 'v': sta.ilbst_flags |= ILBST_VERBOSE;
v_opt = B_TRUE;
fieldnames = stat_stdv_hdrs;
break;
case ':': ilbadm_err(gettext("missing option-argument"
" detected for %c"), (char)optopt);
exit(1);
/* not reached */
break;
case '?': /* fallthrough */
default:
unknown_opt(argv, optind-1);
/* not reached */
break;
}
}
if (s_opt && r_opt) {
ilbadm_err(gettext("options -s and -r are mutually exclusive"));
exit(1);
}
if (i_opt) {
if (!(s_opt || r_opt)) {
ilbadm_err(gettext("option -i requires"
" either -r or -s"));
exit(1);
}
if (v_opt) {
ilbadm_err(gettext("option -i and -v are mutually"
" exclusive"));
exit(1);
}
/* only use "std" headers if none are specified */
if (!o_opt)
if (r_opt)
fieldnames = stat_itemize_rule_hdrs;
else /* must be s_opt */
fieldnames = stat_itemize_server_hdrs;
fields = stat_itemize_fields;
}
if (p_opt) {
if (!o_opt) {
ilbadm_err(gettext("option -p requires -o"));
exit(1);
}
if (v_opt) {
ilbadm_err(gettext("option -o and -v are mutually"
" exclusive"));
exit(1);
}
if (strcasecmp(fieldnames, "all") == 0) {
ilbadm_err(gettext("option -p requires"
" explicit field names"));
exit(1);
}
}
if (t_opt) {
if (v_opt) {
fieldnames = "all";
} else {
int len = strlen(fieldnames) + 6;
char *fnames;
fnames = malloc(len);
if (fnames == NULL) {
rc = ILBADM_ENOMEM;
return (rc);
}
(void) snprintf(fnames, len, "%s,TIME", fieldnames);
fieldnames = fnames;
}
}
if (A_opt && d_opt) {
ilbadm_err(gettext("options -d and -A are mutually exclusive"));
exit(1);
}
/* find and parse interval and count arguments if present */
if (optind < argc) {
sta.ilbst_interval = atoi(argv[optind]);
if (sta.ilbst_interval < 1) {
ilbadm_err(gettext("illegal interval spec %s"),
argv[optind]);
exit(1);
}
sta.ilbst_count = -1;
if (++optind < argc) {
sta.ilbst_count = atoi(argv[optind]);
if (sta.ilbst_count < 1) {
ilbadm_err(gettext("illegal count spec %s"),
argv[optind]);
exit(1);
}
}
}
oerr = ofmt_open(fieldnames, fields, oflags, 80, &oh);
if (oerr != OFMT_SUCCESS) {
char e[80];
ilbadm_err(gettext("ofmt_open failed: %s"),
ofmt_strerror(oh, oerr, e, sizeof (e)));
return (ILBADM_LIBERR);
}
sta.ilbst_oh = oh;
rc = i_do_show_stats(&sta);
ofmt_close(oh);
return (rc);
}
|