]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/systm.c
7c234763145153241ca5730900d2ba184626fd2e
2 * Copyright (c) 2014 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>
79 #define ROUNDUP64(a) \
80 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint64_t) - 1))) : sizeof(uint64_t))
81 #define ADVANCE64(x, n) (((char *)x) += ROUNDUP64(n))
84 u_int32_t xgn_len
; /* length of this structure */
85 u_int32_t xgn_kind
; /* number of PCBs at this time */
88 #define ALL_XGN_KIND_KCREG (XSO_KCREG)
89 #define ALL_XGN_KIND_EVT (XSO_SOCKET | XSO_RCVBUF | XSO_SNDBUF | XSO_STATS | XSO_EVT)
90 #define ALL_XGN_KIND_KCB (XSO_SOCKET | XSO_RCVBUF | XSO_SNDBUF | XSO_STATS | XSO_KCB)
93 systmpr(uint32_t proto
,
99 struct xsystmgen
*xig
, *oxig
;
102 struct xsocket_n
*so
= NULL
;
103 struct xsockbuf_n
*so_rcv
= NULL
;
104 struct xsockbuf_n
*so_snd
= NULL
;
105 struct xsockstat_n
*so_stat
= NULL
;
106 struct xkctl_reg
*kctl
= NULL
;
107 struct xkctlpcb
*kcb
= NULL
;
108 struct xkevtpcb
*kevb
= NULL
;
113 mibvar
= "net.systm.kevt.pcblist";
115 case SYSPROTO_CONTROL
:
116 mibvar
= "net.systm.kctl.pcblist";
119 mibvar
= "net.systm.kctl.reg_list";
128 if (sysctlbyname(mibvar
, 0, &len
, 0, 0) < 0) {
130 warn("sysctl: %s", mibvar
);
133 if ((buf
= malloc(len
)) == 0) {
134 warn("malloc %lu bytes", (u_long
)len
);
137 if (sysctlbyname(mibvar
, buf
, &len
, 0, 0) < 0) {
138 warn("sysctl: %s", mibvar
);
143 * Bail-out to avoid logic error in the loop below when
144 * there is in fact no more control block to process
146 if (len
<= sizeof(struct xsystmgen
)) {
150 oxig
= xig
= (struct xsystmgen
*)buf
;
151 for (next
= buf
+ ROUNDUP64(xig
->xg_len
); next
< buf
+ len
;
152 next
+= ROUNDUP64(xgn
->xgn_len
)) {
153 xgn
= (struct xgen_n
*)next
;
154 if (xgn
->xgn_len
<= sizeof(struct xsystmgen
))
157 if ((which
& xgn
->xgn_kind
) == 0) {
158 which
|= xgn
->xgn_kind
;
159 switch (xgn
->xgn_kind
) {
161 so
= (struct xsocket_n
*)xgn
;
164 so_rcv
= (struct xsockbuf_n
*)xgn
;
167 so_snd
= (struct xsockbuf_n
*)xgn
;
170 so_stat
= (struct xsockstat_n
*)xgn
;
173 kctl
= (struct xkctl_reg
*)xgn
;
176 kcb
= (struct xkctlpcb
*)xgn
;
179 kevb
= (struct xkevtpcb
*)xgn
;
182 printf("unexpected kind %d\n", xgn
->xgn_kind
);
187 printf("got %d twice\n", xgn
->xgn_kind
);
190 if (which
== ALL_XGN_KIND_KCREG
) {
194 printf("Registered kernel control modules\n");
196 printf("%-16.16s ", "kctlref");
197 printf("%-8.8s ", "id");
199 printf("%-8.8s ", "unit");
200 printf("%-8.8s ", "flags");
201 printf("%-8.8s ", "pcbcount");
202 printf("%-8.8s ", "rcvbuf");
203 printf("%-8.8s ", "sndbuf");
204 printf("%s ", "name");
209 printf("%16llx ", kctl
->xkr_kctlref
);
210 printf("%8x ", kctl
->xkr_id
);
212 printf("%8d ", kctl
->xkr_reg_unit
);
213 printf("%8x ", kctl
->xkr_flags
);
214 printf("%8d ", kctl
->xkr_pcbcount
);
215 printf("%8d ", kctl
->xkr_recvbufsize
);
216 printf("%8d ", kctl
->xkr_sendbufsize
);
217 printf("%s ", kctl
->xkr_name
);
219 } else if (which
== ALL_XGN_KIND_KCB
) {
223 printf("Active kernel control sockets\n");
225 printf("%16.16s ", "pcb");
226 printf("%-5.5s %-6.6s %-6.6s ",
227 "Proto", "Recv-Q", "Send-Q");
229 printf("%10.10s %10.10s ",
230 "rxbytes", "txbytes");
232 printf("%6.6s %6.6s %6.6s %6.6s ",
233 "rhiwat", "shiwat", "pid", "epid");
234 printf("%6.6s ", "unit");
235 printf("%6.6s ", "id");
236 printf("%s", "name");
241 printf("%16llx ", kcb
->xkp_kctpcb
);
242 printf("%-5.5s %6u %6u ", name
,
247 u_int64_t rxbytes
= 0;
248 u_int64_t txbytes
= 0;
250 for (i
= 0; i
< SO_TC_STATS_MAX
; i
++) {
251 rxbytes
+= so_stat
->xst_tc_stats
[i
].rxbytes
;
252 txbytes
+= so_stat
->xst_tc_stats
[i
].txbytes
;
254 printf("%10llu %10llu ", rxbytes
, txbytes
);
257 printf("%6u %6u %6u %6u ",
263 printf("%6d ", kcb
->xkp_unit
);
264 printf("%6d ", kcb
->xkp_kctlid
);
265 printf("%s", kcb
->xkp_kctlname
);
268 } else if (which
== ALL_XGN_KIND_EVT
) {
271 printf("Active kernel event sockets\n");
273 printf("%16.16s ", "pcb");
274 printf("%-5.5s %-6.6s %-6.6s ",
275 "Proto", "Recv-Q", "Send-Q");
276 printf("%6.6s ", "vendor");
277 printf("%6.6s ", "class");
278 printf("%6.6s", "subclass");
280 printf("%10.10s %10.10s ",
281 "rxbytes", "txbytes");
283 printf("%6.6s %6.6s %6.6s %6.6s",
284 "rhiwat", "shiwat", "pid", "epid");
289 printf("%16llx ", kevb
->kep_evtpcb
);
290 printf("%-5.5s %6u %6u ", name
,
293 printf("%6d ", kevb
->kep_vendor_code_filter
);
294 printf("%6d ", kevb
->kep_class_filter
);
295 printf("%6d", kevb
->kep_subclass_filter
);
298 u_int64_t rxbytes
= 0;
299 u_int64_t txbytes
= 0;
301 for (i
= 0; i
< SO_TC_STATS_MAX
; i
++) {
302 rxbytes
+= so_stat
->xst_tc_stats
[i
].rxbytes
;
303 txbytes
+= so_stat
->xst_tc_stats
[i
].txbytes
;
305 printf("%10llu %10llu ", rxbytes
, txbytes
);
308 printf("%6u %6u %6u %6u",
318 if (xig
!= oxig
&& xig
->xg_gen
!= oxig
->xg_gen
) {
319 if (oxig
->xg_count
> xig
->xg_count
) {
320 printf("Some %s sockets may have been deleted.\n",
322 } else if (oxig
->xg_count
< xig
->xg_count
) {
323 printf("Some %s sockets may have been created.\n",
326 printf("Some %s sockets may have been created or deleted",
334 kctl_stats(uint32_t off __unused
, char *name
, int af __unused
)
336 static struct kctlstat pkctlstat
;
337 struct kctlstat kctlstat
;
338 size_t len
= sizeof(struct kctlstat
);
339 const char *mibvar
= "net.systm.kctl.stats";
341 if (sysctlbyname(mibvar
, &kctlstat
, &len
, 0, 0) < 0) {
342 warn("sysctl: %s", mibvar
);
345 if (interval
&& vflag
> 0)
347 printf ("%s:\n", name
);
349 #define STATDIFF(f) (kctlstat.f - pkctlstat.f)
350 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
351 printf(m, STATDIFF(f), plural(STATDIFF(f)))
352 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
353 printf(m, STATDIFF(f))
355 p(kcs_reg_total
, "\t%llu total kernel control module%s registered\n");
356 p(kcs_reg_count
, "\t%llu current kernel control module%s registered\n");
357 p(kcs_pcbcount
, "\t%llu current kernel control socket%s\n");
358 p1a(kcs_gencnt
, "\t%llu kernel control generation count\n");
359 p(kcs_connections
, "\t%llu connection attempt%s\n");
360 p(kcs_conn_fail
, "\t%llu connection failure%s\n");
361 p(kcs_send_fail
, "\t%llu send failure%s\n");
362 p(kcs_send_list_fail
, "\t%llu send list failure%s\n");
363 p(kcs_enqueue_fail
, "\t%llu enqueus failure%s\n");
364 p(kcs_enqueue_fullsock
, "\t%llu packet%s dropped due to full socket buffers\n");
371 bcopy(&kctlstat
, &pkctlstat
, len
);
375 kevt_stats(uint32_t off __unused
, char *name
, int af __unused
)
377 static struct kevtstat pkevtstat
;
378 struct kevtstat kevtstat
;
379 size_t len
= sizeof(struct kctlstat
);
380 const char *mibvar
= "net.systm.kevt.stats";
382 if (sysctlbyname(mibvar
, &kevtstat
, &len
, 0, 0) < 0) {
383 warn("sysctl: %s", mibvar
);
386 if (interval
&& vflag
> 0)
388 printf ("%s:\n", name
);
390 #define STATDIFF(f) (kevtstat.f - pkevtstat.f)
391 #define p(f, m) if (STATDIFF(f) || sflag <= 1) \
392 printf(m, STATDIFF(f), plural(STATDIFF(f)))
393 #define p1a(f, m) if (STATDIFF(f) || sflag <= 1) \
394 printf(m, STATDIFF(f))
396 p(kes_pcbcount
, "\t%llu current kernel control socket%s\n");
397 p1a(kes_gencnt
, "\t%llu kernel control generation count\n");
398 p(kes_badvendor
, "\t%llu bad vendor failure%s\n");
399 p(kes_toobig
, "\t%llu message too big failure%s\n");
400 p(kes_nomem
, "\t%llu out of memeory failure%s\n");
401 p(kes_fullsock
, "\t%llu message%s dropped due to full socket buffers\n");
402 p(kes_posted
, "\t%llu message posted%s\n");
405 bcopy(&kevtstat
, &pkevtstat
, len
);