2 * Copyright (c) 2014-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/param.h>
62 #include <sys/queue.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/sys_domain.h>
67 #include <sys/kern_control.h>
68 #include <sys/kern_event.h>
69 #include <net/ntstat.h>
80 #define ROUNDUP64(a) \
81 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint64_t) - 1))) : sizeof(uint64_t))
82 #define ADVANCE64(x, n) (((char *)x) += ROUNDUP64(n))
85 u_int32_t xgn_len
; /* length of this structure */
86 u_int32_t xgn_kind
; /* number of PCBs at this time */
89 #define ALL_XGN_KIND_KCREG (XSO_KCREG)
90 #define ALL_XGN_KIND_EVT (XSO_SOCKET | XSO_RCVBUF | XSO_SNDBUF | XSO_STATS | XSO_EVT)
91 #define ALL_XGN_KIND_KCB (XSO_SOCKET | XSO_RCVBUF | XSO_SNDBUF | XSO_STATS | XSO_KCB)
94 systmpr(uint32_t proto
,
100 struct xsystmgen
*xig
, *oxig
;
103 struct xsocket_n
*so
= NULL
;
104 struct xsockbuf_n
*so_rcv
= NULL
;
105 struct xsockbuf_n
*so_snd
= NULL
;
106 struct xsockstat_n
*so_stat
= NULL
;
107 struct xkctl_reg
*kctl
= NULL
;
108 struct xkctlpcb
*kcb
= NULL
;
109 struct xkevtpcb
*kevb
= NULL
;
114 mibvar
= "net.systm.kevt.pcblist";
116 case SYSPROTO_CONTROL
:
117 mibvar
= "net.systm.kctl.pcblist";
120 mibvar
= "net.systm.kctl.reg_list";
129 if (sysctlbyname(mibvar
, 0, &len
, 0, 0) < 0) {
131 warn("sysctl: %s", mibvar
);
134 if ((buf
= malloc(len
)) == 0) {
135 warn("malloc %lu bytes", (u_long
)len
);
138 if (sysctlbyname(mibvar
, buf
, &len
, 0, 0) < 0) {
139 warn("sysctl: %s", mibvar
);
144 * Bail-out to avoid logic error in the loop below when
145 * there is in fact no more control block to process
147 if (len
<= sizeof(struct xsystmgen
)) {
151 oxig
= xig
= (struct xsystmgen
*)buf
;
152 for (next
= buf
+ ROUNDUP64(xig
->xg_len
); next
< buf
+ len
;
153 next
+= ROUNDUP64(xgn
->xgn_len
)) {
154 xgn
= (struct xgen_n
*)next
;
155 if (xgn
->xgn_len
<= sizeof(struct xsystmgen
))
158 if ((which
& xgn
->xgn_kind
) == 0) {
159 which
|= xgn
->xgn_kind
;
160 switch (xgn
->xgn_kind
) {
162 so
= (struct xsocket_n
*)xgn
;
165 so_rcv
= (struct xsockbuf_n
*)xgn
;
168 so_snd
= (struct xsockbuf_n
*)xgn
;
171 so_stat
= (struct xsockstat_n
*)xgn
;
174 kctl
= (struct xkctl_reg
*)xgn
;
177 kcb
= (struct xkctlpcb
*)xgn
;
180 kevb
= (struct xkevtpcb
*)xgn
;
183 printf("unexpected kind %d\n", xgn
->xgn_kind
);
188 printf("got %d twice\n", xgn
->xgn_kind
);
191 if (which
== ALL_XGN_KIND_KCREG
) {
195 printf("Registered kernel control modules\n");
197 printf("%-16.16s ", "kctlref");
198 printf("%-8.8s ", "id");
200 printf("%-8.8s ", "unit");
201 printf("%-8.8s ", "flags");
202 printf("%-8.8s ", "pcbcount");
203 printf("%-8.8s ", "rcvbuf");
204 printf("%-8.8s ", "sndbuf");
205 printf("%s ", "name");
210 printf("%16llx ", kctl
->xkr_kctlref
);
211 printf("%8x ", kctl
->xkr_id
);
213 printf("%8d ", kctl
->xkr_reg_unit
);
214 printf("%8x ", kctl
->xkr_flags
);
215 printf("%8d ", kctl
->xkr_pcbcount
);
216 printf("%8d ", kctl
->xkr_recvbufsize
);
217 printf("%8d ", kctl
->xkr_sendbufsize
);
218 printf("%s ", kctl
->xkr_name
);
220 } else if (which
== ALL_XGN_KIND_KCB
) {
224 printf("Active kernel control sockets\n");
226 printf("%16.16s ", "pcb");
227 printf("%-5.5s %-6.6s %-6.6s ",
228 "Proto", "Recv-Q", "Send-Q");
230 printf("%10.10s %10.10s ",
231 "rxbytes", "txbytes");
233 printf("%6.6s %6.6s %6.6s %6.6s ",
234 "rhiwat", "shiwat", "pid", "epid");
235 printf("%6.6s ", "unit");
236 printf("%6.6s ", "id");
237 printf("%s", "name");
242 printf("%16llx ", kcb
->xkp_kctpcb
);
243 printf("%-5.5s %6u %6u ", name
,
248 u_int64_t rxbytes
= 0;
249 u_int64_t txbytes
= 0;
251 for (i
= 0; i
< SO_TC_STATS_MAX
; i
++) {
252 rxbytes
+= so_stat
->xst_tc_stats
[i
].rxbytes
;
253 txbytes
+= so_stat
->xst_tc_stats
[i
].txbytes
;
255 printf("%10llu %10llu ", rxbytes
, txbytes
);
258 printf("%6u %6u %6u %6u ",
264 printf("%6d ", kcb
->xkp_unit
);
265 printf("%6d ", kcb
->xkp_kctlid
);
266 printf("%s", kcb
->xkp_kctlname
);
269 } else if (which
== ALL_XGN_KIND_EVT
) {
272 printf("Active kernel event sockets\n");
274 printf("%16.16s ", "pcb");
275 printf("%-5.5s %-6.6s %-6.6s ",
276 "Proto", "Recv-Q", "Send-Q");
277 printf("%6.6s ", "vendor");
278 printf("%6.6s ", "class");
279 printf("%6.6s", "subclass");
281 printf("%10.10s %10.10s ",
282 "rxbytes", "txbytes");
284 printf("%6.6s %6.6s %6.6s %6.6s",
285 "rhiwat", "shiwat", "pid", "epid");
290 printf("%16llx ", kevb
->kep_evtpcb
);
291 printf("%-5.5s %6u %6u ", name
,
294 printf("%6d ", kevb
->kep_vendor_code_filter
);
295 printf("%6d ", kevb
->kep_class_filter
);
296 printf("%6d", kevb
->kep_subclass_filter
);
299 u_int64_t rxbytes
= 0;
300 u_int64_t txbytes
= 0;
302 for (i
= 0; i
< SO_TC_STATS_MAX
; i
++) {
303 rxbytes
+= so_stat
->xst_tc_stats
[i
].rxbytes
;
304 txbytes
+= so_stat
->xst_tc_stats
[i
].txbytes
;
306 printf("%10llu %10llu ", rxbytes
, txbytes
);
309 printf("%6u %6u %6u %6u",
319 if (xig
!= oxig
&& xig
->xg_gen
!= oxig
->xg_gen
) {
320 if (oxig
->xg_count
> xig
->xg_count
) {
321 printf("Some %s sockets may have been deleted.\n",
323 } else if (oxig
->xg_count
< xig
->xg_count
) {
324 printf("Some %s sockets may have been created.\n",
327 printf("Some %s sockets may have been created or deleted",
335 kctl_stats(uint32_t off __unused
, char *name
, int af __unused
)
337 static struct kctlstat pkctlstat
;
338 struct kctlstat kctlstat
;
339 size_t len
= sizeof(struct kctlstat
);
340 const char *mibvar
= "net.systm.kctl.stats";
342 if (sysctlbyname(mibvar
, &kctlstat
, &len
, 0, 0) < 0) {
343 warn("sysctl: %s", mibvar
);
346 if (interval
&& vflag
> 0)
348 printf ("%s:\n", name
);
350 #define STATDIFF(f) (kctlstat.f - pkctlstat.f)
351 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
352 printf(m, STATDIFF(f), plural(STATDIFF(f)))
353 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
354 printf(m, STATDIFF(f))
356 p(kcs_reg_total
, "\t%llu total kernel control module%s registered\n");
357 p(kcs_reg_count
, "\t%llu current kernel control module%s registered\n");
358 p(kcs_pcbcount
, "\t%llu current kernel control socket%s\n");
359 p1a(kcs_gencnt
, "\t%llu kernel control generation count\n");
360 p(kcs_connections
, "\t%llu connection attempt%s\n");
361 p(kcs_conn_fail
, "\t%llu connection failure%s\n");
362 p(kcs_send_fail
, "\t%llu send failure%s\n");
363 p(kcs_send_list_fail
, "\t%llu send list failure%s\n");
364 p(kcs_enqueue_fail
, "\t%llu enqueue failure%s\n");
365 p(kcs_enqueue_fullsock
, "\t%llu packet%s dropped due to full socket buffers\n");
366 p(kcs_bad_kctlref
, "\t%llu failure%s with bad kern_ctl_ref\n");
367 p(kcs_tbl_size_too_big
, "\t%llu register failure%s because of too many kern_ctl_ref\n");
368 p(kcs_enqdata_mb_alloc_fail
, "\t%llu enqueuedata failure%s because could not allocate a packet\n");
369 p(kcs_enqdata_sbappend_fail
, "\t%llu enqueuedata failure%s due to full socket buffers\n");
376 bcopy(&kctlstat
, &pkctlstat
, len
);
380 kevt_stats(uint32_t off __unused
, char *name
, int af __unused
)
382 static struct kevtstat pkevtstat
;
383 struct kevtstat kevtstat
;
384 size_t len
= sizeof(struct kevtstat
);
385 const char *mibvar
= "net.systm.kevt.stats";
387 if (sysctlbyname(mibvar
, &kevtstat
, &len
, 0, 0) < 0) {
388 warn("sysctl: %s", mibvar
);
391 if (interval
&& vflag
> 0)
393 printf ("%s:\n", name
);
395 #define STATDIFF(f) (kevtstat.f - pkevtstat.f)
396 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
397 printf(m, STATDIFF(f), plural(STATDIFF(f)))
398 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
399 printf(m, STATDIFF(f))
401 p(kes_pcbcount
, "\t%llu current kernel control socket%s\n");
402 p1a(kes_gencnt
, "\t%llu kernel control generation count\n");
403 p(kes_badvendor
, "\t%llu bad vendor failure%s\n");
404 p(kes_toobig
, "\t%llu message too big failure%s\n");
405 p(kes_nomem
, "\t%llu out of memory failure%s\n");
406 p(kes_fullsock
, "\t%llu message%s dropped due to full socket buffers\n");
407 p(kes_posted
, "\t%llu message%s posted\n");
414 bcopy(&kevtstat
, &pkevtstat
, len
);
418 print_extbkidle_stats(uint32_t off __unused
, char *name
, int af __unused
)
420 static struct soextbkidlestat psoextbkidlestat
;
421 struct soextbkidlestat soextbkidlestat
;
422 size_t len
= sizeof(struct soextbkidlestat
);
423 const char *mibvar
= "kern.ipc.extbkidlestat";
425 if (sysctlbyname(mibvar
, &soextbkidlestat
, &len
, 0, 0) < 0) {
426 warn("sysctl: %s", mibvar
);
430 #define STATDIFF(f) (soextbkidlestat.f - psoextbkidlestat.f)
431 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
432 printf(m, STATDIFF(f), plural(STATDIFF(f)))
433 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
434 printf(m, STATDIFF(f))
436 if (interval
&& vflag
> 0)
438 printf ("%s:\n", name
);
440 p1a(so_xbkidle_maxperproc
, "\t%u max per process\n");
441 p1a(so_xbkidle_time
, "\t%u maximum time (seconds)\n");
442 p1a(so_xbkidle_rcvhiwat
, "\t%u high water mark\n");
443 p(so_xbkidle_notsupp
, "\t%u socket option not supported failure%s\n");
444 p(so_xbkidle_toomany
, "\t%u too many sockets failure%s\n");
445 p(so_xbkidle_wantok
, "\t%u total socket%s requested OK\n");
446 p(so_xbkidle_active
, "\t%u extended bk idle socket%s\n");
447 p(so_xbkidle_nocell
, "\t%u no cellular failure%s\n");
448 p(so_xbkidle_notime
, "\t%u no time failures%s\n");
449 p(so_xbkidle_forced
, "\t%u forced defunct socket%s\n");
450 p(so_xbkidle_resumed
, "\t%u resumed socket%s\n");
451 p(so_xbkidle_expired
, "\t%u timeout expired failure%s\n");
452 p1a(so_xbkidle_expired
, "\t%u timer rescheduled\n");
453 p(so_xbkidle_nodlgtd
, "\t%u no delegated failure%s\n");
461 print_nstat_stats(uint32_t off __unused
, char *name
, int af __unused
)
463 static struct nstat_stats pnstat_stats
;
464 struct nstat_stats nstat_stats
;
465 size_t len
= sizeof(struct nstat_stats
);
466 const char *mibvar
= "net.stats.stats";
468 if (sysctlbyname(mibvar
, &nstat_stats
, &len
, 0, 0) < 0) {
469 warn("sysctl: %s", mibvar
);
473 #define STATDIFF(f) (nstat_stats.f - pnstat_stats.f)
474 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
475 printf(m, STATDIFF(f), plural(STATDIFF(f)))
476 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
477 printf(m, STATDIFF(f))
479 if (interval
&& vflag
> 0)
481 printf ("%s:\n", name
);
483 p(nstat_successmsgfailures
, "\t%u enqueue success message failure%s\n");
484 p(nstat_sendcountfailures
, "\t%u enqueue source counts message failure%s\n");
485 p(nstat_sysinfofailures
, "\t%u enqueue sysinfo message failure%s\n");
486 p(nstat_srcupatefailures
, "\t%u enqueue source udpate message failure%s\n");
487 p(nstat_descriptionfailures
, "\t%u enqueue description message failure%s\n");
488 p(nstat_msgremovedfailures
, "\t%u enqueue remove message failure%s\n");
489 p(nstat_srcaddedfailures
, "\t%u enqueue source added message failure%s\n");
490 p(nstat_msgerrorfailures
, "\t%u enqueue error message failure%s\n");
491 p(nstat_copy_descriptor_failures
, "\t%u copy descriptor failure%s\n");
492 p(nstat_provider_counts_failures
, "\t%u provider counts failure%s\n");
493 p(nstat_control_send_description_failures
, "\t%u control send description failure%s\n");
494 p(nstat_control_send_goodbye_failures
, "\t%u control send goodbye failure%s\n");
495 p(nstat_flush_accumulated_msgs_failures
, "\t%u flush accumulated messages failure%s\n");
496 p(nstat_accumulate_msg_failures
, "\t%u accumulated message failure%s\n");
497 p(nstat_control_cleanup_source_failures
, "\t%u control cleanup source failure%s\n");
498 p(nstat_handle_msg_failures
, "\t%u handle message failure%s\n");