2 * Copyright (c) 2010-2017 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 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/kpi_mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/kern_control.h>
34 #include <sys/mcache.h>
35 #include <sys/socketvar.h>
36 #include <sys/sysctl.h>
37 #include <sys/queue.h>
39 #include <sys/protosw.h>
41 #include <kern/clock.h>
42 #include <kern/debug.h>
44 #include <libkern/libkern.h>
45 #include <libkern/OSMalloc.h>
46 #include <libkern/OSAtomic.h>
47 #include <libkern/locks.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
54 // These includes appear in ntstat.h but we include them here first so they won't trigger
55 // any clang diagnostic errors.
56 #include <netinet/in.h>
57 #include <netinet/in_stat.h>
58 #include <netinet/tcp.h>
60 #pragma clang diagnostic push
61 #pragma clang diagnostic error "-Wpadded"
62 #pragma clang diagnostic error "-Wpacked"
63 // This header defines structures shared with user space, so we need to ensure there is
64 // no compiler inserted padding in case the user space process isn't using the same
65 // architecture as the kernel (example: i386 process with x86_64 kernel).
66 #include <net/ntstat.h>
67 #pragma clang diagnostic pop
69 #include <netinet/ip_var.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/in_var.h>
72 #include <netinet/tcp_var.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_cc.h>
75 #include <netinet/udp.h>
76 #include <netinet/udp_var.h>
77 #include <netinet6/in6_pcb.h>
78 #include <netinet6/in6_var.h>
80 __private_extern__
int nstat_collect
= 1;
82 #if (DEBUG || DEVELOPMENT)
83 SYSCTL_INT(_net
, OID_AUTO
, statistics
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
84 &nstat_collect
, 0, "Collect detailed statistics");
85 #endif /* (DEBUG || DEVELOPMENT) */
88 static int nstat_privcheck
= 1;
90 static int nstat_privcheck
= 0;
92 SYSCTL_INT(_net
, OID_AUTO
, statistics_privcheck
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
93 &nstat_privcheck
, 0, "Entitlement check");
95 SYSCTL_NODE(_net
, OID_AUTO
, stats
,
96 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "network statistics");
98 static int nstat_debug
= 0;
99 SYSCTL_INT(_net_stats
, OID_AUTO
, debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
100 &nstat_debug
, 0, "");
102 static int nstat_sendspace
= 2048;
103 SYSCTL_INT(_net_stats
, OID_AUTO
, sendspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
104 &nstat_sendspace
, 0, "");
106 static int nstat_recvspace
= 8192;
107 SYSCTL_INT(_net_stats
, OID_AUTO
, recvspace
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
108 &nstat_recvspace
, 0, "");
110 static struct nstat_stats nstat_stats
;
111 SYSCTL_STRUCT(_net_stats
, OID_AUTO
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
112 &nstat_stats
, nstat_stats
, "");
114 static u_int32_t nstat_lim_interval
= 30 * 60; /* Report interval, seconds */
115 static u_int32_t nstat_lim_min_tx_pkts
= 100;
116 static u_int32_t nstat_lim_min_rx_pkts
= 100;
117 #if (DEBUG || DEVELOPMENT)
118 SYSCTL_INT(_net_stats
, OID_AUTO
, lim_report_interval
,
119 CTLFLAG_RW
| CTLFLAG_LOCKED
, &nstat_lim_interval
, 0,
120 "Low internet stat report interval");
122 SYSCTL_INT(_net_stats
, OID_AUTO
, lim_min_tx_pkts
,
123 CTLFLAG_RW
| CTLFLAG_LOCKED
, &nstat_lim_min_tx_pkts
, 0,
124 "Low Internet, min transmit packets threshold");
126 SYSCTL_INT(_net_stats
, OID_AUTO
, lim_min_rx_pkts
,
127 CTLFLAG_RW
| CTLFLAG_LOCKED
, &nstat_lim_min_rx_pkts
, 0,
128 "Low Internet, min receive packets threshold");
129 #endif /* DEBUG || DEVELOPMENT */
131 static struct net_api_stats net_api_stats_before
;
132 static u_int64_t net_api_stats_last_report_time
;
133 #define NET_API_STATS_REPORT_INTERVAL (12 * 60 * 60) /* 12 hours, in seconds */
134 static u_int32_t net_api_stats_report_interval
= NET_API_STATS_REPORT_INTERVAL
;
136 #if (DEBUG || DEVELOPMENT)
137 SYSCTL_UINT(_net_stats
, OID_AUTO
, api_report_interval
,
138 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_api_stats_report_interval
, 0, "");
139 #endif /* DEBUG || DEVELOPMENT */
142 NSTAT_FLAG_CLEANUP
= (1 << 0),
143 NSTAT_FLAG_REQCOUNTS
= (1 << 1),
144 NSTAT_FLAG_SUPPORTS_UPDATES
= (1 << 2),
145 NSTAT_FLAG_SYSINFO_SUBSCRIBED
= (1 << 3),
149 #define QUERY_CONTINUATION_SRC_COUNT 50
151 #define QUERY_CONTINUATION_SRC_COUNT 100
154 typedef TAILQ_HEAD(, nstat_src
) tailq_head_nstat_src
;
155 typedef TAILQ_ENTRY(nstat_src
) tailq_entry_nstat_src
;
157 typedef struct nstat_provider_filter
{
159 u_int64_t npf_events
;
162 } nstat_provider_filter
;
165 typedef struct nstat_control_state
{
166 struct nstat_control_state
*ncs_next
;
167 u_int32_t ncs_watching
;
168 decl_lck_mtx_data(, ncs_mtx
);
169 kern_ctl_ref ncs_kctl
;
171 nstat_src_ref_t ncs_next_srcref
;
172 tailq_head_nstat_src ncs_src_queue
;
173 mbuf_t ncs_accumulated
;
175 nstat_provider_filter ncs_provider_filters
[NSTAT_PROVIDER_COUNT
];
176 /* state maintained for partial query requests */
177 u_int64_t ncs_context
;
179 } nstat_control_state
;
181 typedef struct nstat_provider
{
182 struct nstat_provider
*next
;
183 nstat_provider_id_t nstat_provider_id
;
184 size_t nstat_descriptor_length
;
185 errno_t (*nstat_lookup
)(const void *data
, u_int32_t length
, nstat_provider_cookie_t
*out_cookie
);
186 int (*nstat_gone
)(nstat_provider_cookie_t cookie
);
187 errno_t (*nstat_counts
)(nstat_provider_cookie_t cookie
, struct nstat_counts
*out_counts
, int *out_gone
);
188 errno_t (*nstat_watcher_add
)(nstat_control_state
*state
, nstat_msg_add_all_srcs
*req
);
189 void (*nstat_watcher_remove
)(nstat_control_state
*state
);
190 errno_t (*nstat_copy_descriptor
)(nstat_provider_cookie_t cookie
, void *data
, u_int32_t len
);
191 void (*nstat_release
)(nstat_provider_cookie_t cookie
, boolean_t locked
);
192 bool (*nstat_reporting_allowed
)(nstat_provider_cookie_t cookie
, nstat_provider_filter
*filter
);
195 typedef STAILQ_HEAD(, nstat_src
) stailq_head_nstat_src
;
196 typedef STAILQ_ENTRY(nstat_src
) stailq_entry_nstat_src
;
198 typedef TAILQ_HEAD(, nstat_tu_shadow
) tailq_head_tu_shadow
;
199 typedef TAILQ_ENTRY(nstat_tu_shadow
) tailq_entry_tu_shadow
;
201 typedef TAILQ_HEAD(, nstat_procdetails
) tailq_head_procdetails
;
202 typedef TAILQ_ENTRY(nstat_procdetails
) tailq_entry_procdetails
;
204 typedef struct nstat_src
{
205 tailq_entry_nstat_src ns_control_link
; // All sources for the nstat_control_state, for iterating over.
206 nstat_control_state
*ns_control
; // The nstat_control_state that this is a source for
207 nstat_src_ref_t srcref
;
208 nstat_provider
*provider
;
209 nstat_provider_cookie_t cookie
;
214 static errno_t
nstat_control_send_counts(nstat_control_state
*,
215 nstat_src
*, unsigned long long, u_int16_t
, int *);
216 static int nstat_control_send_description(nstat_control_state
*state
, nstat_src
*src
, u_int64_t context
, u_int16_t hdr_flags
);
217 static int nstat_control_send_update(nstat_control_state
*state
, nstat_src
*src
, u_int64_t context
, u_int16_t hdr_flags
, int *gone
);
218 static errno_t
nstat_control_send_removed(nstat_control_state
*, nstat_src
*);
219 static errno_t
nstat_control_send_goodbye(nstat_control_state
*state
, nstat_src
*src
);
220 static void nstat_control_cleanup_source(nstat_control_state
*state
, nstat_src
*src
, boolean_t
);
221 static bool nstat_control_reporting_allowed(nstat_control_state
*state
, nstat_src
*src
);
222 static boolean_t
nstat_control_begin_query(nstat_control_state
*state
, const nstat_msg_hdr
*hdrp
);
223 static u_int16_t
nstat_control_end_query(nstat_control_state
*state
, nstat_src
*last_src
, boolean_t partial
);
224 static void nstat_ifnet_report_ecn_stats(void);
225 static void nstat_ifnet_report_lim_stats(void);
226 static void nstat_net_api_report_stats(void);
227 static errno_t
nstat_set_provider_filter( nstat_control_state
*state
, nstat_msg_add_all_srcs
*req
);
229 static u_int32_t nstat_udp_watchers
= 0;
230 static u_int32_t nstat_tcp_watchers
= 0;
232 static void nstat_control_register(void);
235 * The lock order is as follows:
237 * socket_lock (inpcb)
241 static volatile OSMallocTag nstat_malloc_tag
= NULL
;
242 static nstat_control_state
*nstat_controls
= NULL
;
243 static uint64_t nstat_idle_time
= 0;
244 static decl_lck_mtx_data(, nstat_mtx
);
246 /* some extern definitions */
247 extern void mbuf_report_peak_usage(void);
248 extern void tcp_report_stats(void);
252 const struct sockaddr
*src
,
253 struct sockaddr
*dst
,
256 if (src
->sa_len
> maxlen
) {
260 bcopy(src
, dst
, src
->sa_len
);
261 if (src
->sa_family
== AF_INET6
&&
262 src
->sa_len
>= sizeof(struct sockaddr_in6
)) {
263 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)dst
;
264 if (IN6_IS_SCOPE_EMBED(&sin6
->sin6_addr
)) {
265 if (sin6
->sin6_scope_id
== 0) {
266 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
268 sin6
->sin6_addr
.s6_addr16
[1] = 0;
274 nstat_ip_to_sockaddr(
275 const struct in_addr
*ip
,
277 struct sockaddr_in
*sin
,
280 if (maxlen
< sizeof(struct sockaddr_in
)) {
284 sin
->sin_family
= AF_INET
;
285 sin
->sin_len
= sizeof(*sin
);
286 sin
->sin_port
= port
;
291 nstat_ifnet_to_flags(
295 u_int32_t functional_type
= if_functional_type(ifp
, FALSE
);
297 /* Panic if someone adds a functional type without updating ntstat. */
298 VERIFY(0 <= functional_type
&& functional_type
<= IFRTYPE_FUNCTIONAL_LAST
);
300 switch (functional_type
) {
301 case IFRTYPE_FUNCTIONAL_UNKNOWN
:
302 flags
|= NSTAT_IFNET_IS_UNKNOWN_TYPE
;
304 case IFRTYPE_FUNCTIONAL_LOOPBACK
:
305 flags
|= NSTAT_IFNET_IS_LOOPBACK
;
307 case IFRTYPE_FUNCTIONAL_WIRED
:
308 case IFRTYPE_FUNCTIONAL_INTCOPROC
:
309 flags
|= NSTAT_IFNET_IS_WIRED
;
311 case IFRTYPE_FUNCTIONAL_WIFI_INFRA
:
312 flags
|= NSTAT_IFNET_IS_WIFI
;
314 case IFRTYPE_FUNCTIONAL_WIFI_AWDL
:
315 flags
|= NSTAT_IFNET_IS_WIFI
;
316 flags
|= NSTAT_IFNET_IS_AWDL
;
318 case IFRTYPE_FUNCTIONAL_CELLULAR
:
319 flags
|= NSTAT_IFNET_IS_CELLULAR
;
323 if (IFNET_IS_EXPENSIVE(ifp
)) {
324 flags
|= NSTAT_IFNET_IS_EXPENSIVE
;
331 nstat_inpcb_to_flags(
332 const struct inpcb
*inp
)
336 if ((inp
!= NULL
) && (inp
->inp_last_outifp
!= NULL
)) {
337 struct ifnet
*ifp
= inp
->inp_last_outifp
;
338 flags
= nstat_ifnet_to_flags(ifp
);
340 if (flags
& NSTAT_IFNET_IS_CELLULAR
) {
341 if (inp
->inp_socket
!= NULL
&&
342 (inp
->inp_socket
->so_flags1
& SOF1_CELLFALLBACK
)) {
343 flags
|= NSTAT_IFNET_VIA_CELLFALLBACK
;
347 flags
= NSTAT_IFNET_IS_UNKNOWN_TYPE
;
353 #pragma mark -- Network Statistic Providers --
355 static errno_t
nstat_control_source_add(u_int64_t context
, nstat_control_state
*state
, nstat_provider
*provider
, nstat_provider_cookie_t cookie
);
356 struct nstat_provider
*nstat_providers
= NULL
;
358 static struct nstat_provider
*
359 nstat_find_provider_by_id(
360 nstat_provider_id_t id
)
362 struct nstat_provider
*provider
;
364 for (provider
= nstat_providers
; provider
!= NULL
; provider
= provider
->next
) {
365 if (provider
->nstat_provider_id
== id
) {
375 nstat_provider_id_t id
,
378 nstat_provider
**out_provider
,
379 nstat_provider_cookie_t
*out_cookie
)
381 *out_provider
= nstat_find_provider_by_id(id
);
382 if (*out_provider
== NULL
) {
386 return (*out_provider
)->nstat_lookup(data
, length
, out_cookie
);
389 static void nstat_init_route_provider(void);
390 static void nstat_init_tcp_provider(void);
391 static void nstat_init_udp_provider(void);
392 static void nstat_init_ifnet_provider(void);
394 __private_extern__
void
397 if (nstat_malloc_tag
!= NULL
) {
401 OSMallocTag tag
= OSMalloc_Tagalloc(NET_STAT_CONTROL_NAME
, OSMT_DEFAULT
);
402 if (!OSCompareAndSwapPtr(NULL
, tag
, &nstat_malloc_tag
)) {
403 OSMalloc_Tagfree(tag
);
404 tag
= nstat_malloc_tag
;
406 // we need to initialize other things, we do it here as this code path will only be hit once;
407 nstat_init_route_provider();
408 nstat_init_tcp_provider();
409 nstat_init_udp_provider();
410 nstat_init_ifnet_provider();
411 nstat_control_register();
415 #pragma mark -- Aligned Buffer Allocation --
417 struct align_header
{
423 nstat_malloc_aligned(
428 struct align_header
*hdr
= NULL
;
429 u_int32_t size
= length
+ sizeof(*hdr
) + alignment
- 1;
431 u_int8_t
*buffer
= OSMalloc(size
, tag
);
432 if (buffer
== NULL
) {
436 u_int8_t
*aligned
= buffer
+ sizeof(*hdr
);
437 aligned
= (u_int8_t
*)P2ROUNDUP(aligned
, alignment
);
439 hdr
= (struct align_header
*)(void *)(aligned
- sizeof(*hdr
));
440 hdr
->offset
= aligned
- buffer
;
451 struct align_header
*hdr
= (struct align_header
*)(void *)((u_int8_t
*)buffer
- sizeof(*hdr
));
452 OSFree(((char*)buffer
) - hdr
->offset
, hdr
->length
, tag
);
455 #pragma mark -- Route Provider --
457 static nstat_provider nstat_route_provider
;
463 nstat_provider_cookie_t
*out_cookie
)
465 // rt_lookup doesn't take const params but it doesn't modify the parameters for
466 // the lookup. So...we use a union to eliminate the warning.
469 const struct sockaddr
*const_sa
;
472 const nstat_route_add_param
*param
= (const nstat_route_add_param
*)data
;
475 if (length
< sizeof(*param
)) {
479 if (param
->dst
.v4
.sin_family
== 0 ||
480 param
->dst
.v4
.sin_family
> AF_MAX
||
481 (param
->mask
.v4
.sin_family
!= 0 && param
->mask
.v4
.sin_family
!= param
->dst
.v4
.sin_family
)) {
485 if (param
->dst
.v4
.sin_len
> sizeof(param
->dst
) ||
486 (param
->mask
.v4
.sin_family
&& param
->mask
.v4
.sin_len
> sizeof(param
->mask
.v4
.sin_len
))) {
489 if ((param
->dst
.v4
.sin_family
== AF_INET
&&
490 param
->dst
.v4
.sin_len
< sizeof(struct sockaddr_in
)) ||
491 (param
->dst
.v6
.sin6_family
== AF_INET6
&&
492 param
->dst
.v6
.sin6_len
< sizeof(struct sockaddr_in6
))) {
496 dst
.const_sa
= (const struct sockaddr
*)¶m
->dst
;
497 mask
.const_sa
= param
->mask
.v4
.sin_family
? (const struct sockaddr
*)¶m
->mask
: NULL
;
499 struct radix_node_head
*rnh
= rt_tables
[dst
.sa
->sa_family
];
504 lck_mtx_lock(rnh_lock
);
505 struct rtentry
*rt
= rt_lookup(TRUE
, dst
.sa
, mask
.sa
, rnh
, param
->ifindex
);
506 lck_mtx_unlock(rnh_lock
);
509 *out_cookie
= (nstat_provider_cookie_t
)rt
;
512 return rt
? 0 : ENOENT
;
517 nstat_provider_cookie_t cookie
)
519 struct rtentry
*rt
= (struct rtentry
*)cookie
;
520 return ((rt
->rt_flags
& RTF_UP
) == 0) ? 1 : 0;
525 nstat_provider_cookie_t cookie
,
526 struct nstat_counts
*out_counts
,
529 struct rtentry
*rt
= (struct rtentry
*)cookie
;
530 struct nstat_counts
*rt_stats
= rt
->rt_stats
;
536 if (out_gone
&& (rt
->rt_flags
& RTF_UP
) == 0) {
541 atomic_get_64(out_counts
->nstat_rxpackets
, &rt_stats
->nstat_rxpackets
);
542 atomic_get_64(out_counts
->nstat_rxbytes
, &rt_stats
->nstat_rxbytes
);
543 atomic_get_64(out_counts
->nstat_txpackets
, &rt_stats
->nstat_txpackets
);
544 atomic_get_64(out_counts
->nstat_txbytes
, &rt_stats
->nstat_txbytes
);
545 out_counts
->nstat_rxduplicatebytes
= rt_stats
->nstat_rxduplicatebytes
;
546 out_counts
->nstat_rxoutoforderbytes
= rt_stats
->nstat_rxoutoforderbytes
;
547 out_counts
->nstat_txretransmit
= rt_stats
->nstat_txretransmit
;
548 out_counts
->nstat_connectattempts
= rt_stats
->nstat_connectattempts
;
549 out_counts
->nstat_connectsuccesses
= rt_stats
->nstat_connectsuccesses
;
550 out_counts
->nstat_min_rtt
= rt_stats
->nstat_min_rtt
;
551 out_counts
->nstat_avg_rtt
= rt_stats
->nstat_avg_rtt
;
552 out_counts
->nstat_var_rtt
= rt_stats
->nstat_var_rtt
;
553 out_counts
->nstat_cell_rxbytes
= out_counts
->nstat_cell_txbytes
= 0;
555 bzero(out_counts
, sizeof(*out_counts
));
563 nstat_provider_cookie_t cookie
,
566 rtfree((struct rtentry
*)cookie
);
569 static u_int32_t nstat_route_watchers
= 0;
572 nstat_route_walktree_add(
573 struct radix_node
*rn
,
577 struct rtentry
*rt
= (struct rtentry
*)rn
;
578 nstat_control_state
*state
= (nstat_control_state
*)context
;
580 LCK_MTX_ASSERT(rnh_lock
, LCK_MTX_ASSERT_OWNED
);
582 /* RTF_UP can't change while rnh_lock is held */
583 if ((rt
->rt_flags
& RTF_UP
) != 0) {
584 /* Clear RTPRF_OURS if the route is still usable */
586 if (rt_validate(rt
)) {
587 RT_ADDREF_LOCKED(rt
);
594 /* Otherwise if RTF_CONDEMNED, treat it as if it were down */
599 result
= nstat_control_source_add(0, state
, &nstat_route_provider
, rt
);
609 nstat_route_add_watcher(
610 nstat_control_state
*state
,
611 nstat_msg_add_all_srcs
*req
)
616 lck_mtx_lock(rnh_lock
);
618 result
= nstat_set_provider_filter(state
, req
);
620 OSIncrementAtomic(&nstat_route_watchers
);
622 for (i
= 1; i
< AF_MAX
; i
++) {
623 struct radix_node_head
*rnh
;
629 result
= rnh
->rnh_walktree(rnh
, nstat_route_walktree_add
, state
);
631 // This is probably resource exhaustion.
632 // There currently isn't a good way to recover from this.
633 // Least bad seems to be to give up on the add-all but leave
634 // the watcher in place.
639 lck_mtx_unlock(rnh_lock
);
644 __private_extern__
void
645 nstat_route_new_entry(
648 if (nstat_route_watchers
== 0) {
652 lck_mtx_lock(&nstat_mtx
);
653 if ((rt
->rt_flags
& RTF_UP
) != 0) {
654 nstat_control_state
*state
;
655 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
656 if ((state
->ncs_watching
& (1 << NSTAT_PROVIDER_ROUTE
)) != 0) {
657 // this client is watching routes
658 // acquire a reference for the route
661 // add the source, if that fails, release the reference
662 if (nstat_control_source_add(0, state
, &nstat_route_provider
, rt
) != 0) {
668 lck_mtx_unlock(&nstat_mtx
);
672 nstat_route_remove_watcher(
673 __unused nstat_control_state
*state
)
675 OSDecrementAtomic(&nstat_route_watchers
);
679 nstat_route_copy_descriptor(
680 nstat_provider_cookie_t cookie
,
684 nstat_route_descriptor
*desc
= (nstat_route_descriptor
*)data
;
685 if (len
< sizeof(*desc
)) {
688 bzero(desc
, sizeof(*desc
));
690 struct rtentry
*rt
= (struct rtentry
*)cookie
;
691 desc
->id
= (uint64_t)VM_KERNEL_ADDRPERM(rt
);
692 desc
->parent_id
= (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_parent
);
693 desc
->gateway_id
= (uint64_t)VM_KERNEL_ADDRPERM(rt
->rt_gwroute
);
698 if ((sa
= rt_key(rt
))) {
699 nstat_copy_sa_out(sa
, &desc
->dst
.sa
, sizeof(desc
->dst
));
703 if ((sa
= rt_mask(rt
)) && sa
->sa_len
<= sizeof(desc
->mask
)) {
704 memcpy(&desc
->mask
, sa
, sa
->sa_len
);
708 if ((sa
= rt
->rt_gateway
)) {
709 nstat_copy_sa_out(sa
, &desc
->gateway
.sa
, sizeof(desc
->gateway
));
713 desc
->ifindex
= rt
->rt_ifp
->if_index
;
716 desc
->flags
= rt
->rt_flags
;
722 nstat_route_reporting_allowed(nstat_provider_cookie_t cookie
, nstat_provider_filter
*filter
)
726 if ((filter
->npf_flags
& NSTAT_FILTER_IFNET_FLAGS
) != 0) {
727 struct rtentry
*rt
= (struct rtentry
*)cookie
;
728 struct ifnet
*ifp
= rt
->rt_ifp
;
731 uint16_t interface_properties
= nstat_ifnet_to_flags(ifp
);
733 if ((filter
->npf_flags
& interface_properties
) == 0) {
742 nstat_init_route_provider(void)
744 bzero(&nstat_route_provider
, sizeof(nstat_route_provider
));
745 nstat_route_provider
.nstat_descriptor_length
= sizeof(nstat_route_descriptor
);
746 nstat_route_provider
.nstat_provider_id
= NSTAT_PROVIDER_ROUTE
;
747 nstat_route_provider
.nstat_lookup
= nstat_route_lookup
;
748 nstat_route_provider
.nstat_gone
= nstat_route_gone
;
749 nstat_route_provider
.nstat_counts
= nstat_route_counts
;
750 nstat_route_provider
.nstat_release
= nstat_route_release
;
751 nstat_route_provider
.nstat_watcher_add
= nstat_route_add_watcher
;
752 nstat_route_provider
.nstat_watcher_remove
= nstat_route_remove_watcher
;
753 nstat_route_provider
.nstat_copy_descriptor
= nstat_route_copy_descriptor
;
754 nstat_route_provider
.nstat_reporting_allowed
= nstat_route_reporting_allowed
;
755 nstat_route_provider
.next
= nstat_providers
;
756 nstat_providers
= &nstat_route_provider
;
759 #pragma mark -- Route Collection --
761 __private_extern__
struct nstat_counts
*
765 struct nstat_counts
*result
= rte
->rt_stats
;
770 if (nstat_malloc_tag
== NULL
) {
774 result
= nstat_malloc_aligned(sizeof(*result
), sizeof(u_int64_t
), nstat_malloc_tag
);
779 bzero(result
, sizeof(*result
));
781 if (!OSCompareAndSwapPtr(NULL
, result
, &rte
->rt_stats
)) {
782 nstat_free_aligned(result
, nstat_malloc_tag
);
783 result
= rte
->rt_stats
;
789 __private_extern__
void
794 nstat_free_aligned(rte
->rt_stats
, nstat_malloc_tag
);
795 rte
->rt_stats
= NULL
;
799 __private_extern__
void
800 nstat_route_connect_attempt(
804 struct nstat_counts
* stats
= nstat_route_attach(rte
);
806 OSIncrementAtomic(&stats
->nstat_connectattempts
);
809 rte
= rte
->rt_parent
;
813 __private_extern__
void
814 nstat_route_connect_success(
819 struct nstat_counts
* stats
= nstat_route_attach(rte
);
821 OSIncrementAtomic(&stats
->nstat_connectsuccesses
);
824 rte
= rte
->rt_parent
;
828 __private_extern__
void
836 struct nstat_counts
* stats
= nstat_route_attach(rte
);
838 if ((flags
& NSTAT_TX_FLAG_RETRANSMIT
) != 0) {
839 OSAddAtomic(bytes
, &stats
->nstat_txretransmit
);
841 OSAddAtomic64((SInt64
)packets
, (SInt64
*)&stats
->nstat_txpackets
);
842 OSAddAtomic64((SInt64
)bytes
, (SInt64
*)&stats
->nstat_txbytes
);
846 rte
= rte
->rt_parent
;
850 __private_extern__
void
858 struct nstat_counts
* stats
= nstat_route_attach(rte
);
861 OSAddAtomic64((SInt64
)packets
, (SInt64
*)&stats
->nstat_rxpackets
);
862 OSAddAtomic64((SInt64
)bytes
, (SInt64
*)&stats
->nstat_rxbytes
);
864 if (flags
& NSTAT_RX_FLAG_OUT_OF_ORDER
) {
865 OSAddAtomic(bytes
, &stats
->nstat_rxoutoforderbytes
);
867 if (flags
& NSTAT_RX_FLAG_DUPLICATE
) {
868 OSAddAtomic(bytes
, &stats
->nstat_rxduplicatebytes
);
873 rte
= rte
->rt_parent
;
877 /* atomically average current value at _val_addr with _new_val and store */
878 #define NSTAT_EWMA_ATOMIC(_val_addr, _new_val, _decay) do { \
879 volatile uint32_t _old_val; \
880 volatile uint32_t _avg; \
882 _old_val = *_val_addr; \
889 _avg = _old_val - (_old_val >> _decay) + (_new_val >> _decay); \
891 if (_old_val == _avg) break; \
892 } while (!OSCompareAndSwap(_old_val, _avg, _val_addr)); \
895 /* atomically compute minimum of current value at _val_addr with _new_val and store */
896 #define NSTAT_MIN_ATOMIC(_val_addr, _new_val) do { \
897 volatile uint32_t _old_val; \
899 _old_val = *_val_addr; \
900 if (_old_val != 0 && _old_val < _new_val) \
904 } while (!OSCompareAndSwap(_old_val, _new_val, _val_addr)); \
907 __private_extern__
void
913 const uint32_t decay
= 3;
916 struct nstat_counts
* stats
= nstat_route_attach(rte
);
918 NSTAT_EWMA_ATOMIC(&stats
->nstat_avg_rtt
, rtt
, decay
);
919 NSTAT_MIN_ATOMIC(&stats
->nstat_min_rtt
, rtt
);
920 NSTAT_EWMA_ATOMIC(&stats
->nstat_var_rtt
, rtt_var
, decay
);
922 rte
= rte
->rt_parent
;
926 __private_extern__
void
929 uint32_t connect_attempts
,
930 uint32_t connect_successes
,
933 uint32_t rx_duplicatebytes
,
934 uint32_t rx_outoforderbytes
,
937 uint32_t tx_retransmit
,
941 const uint32_t decay
= 3;
944 struct nstat_counts
* stats
= nstat_route_attach(rte
);
946 OSAddAtomic(connect_attempts
, &stats
->nstat_connectattempts
);
947 OSAddAtomic(connect_successes
, &stats
->nstat_connectsuccesses
);
948 OSAddAtomic64((SInt64
)tx_packets
, (SInt64
*)&stats
->nstat_txpackets
);
949 OSAddAtomic64((SInt64
)tx_bytes
, (SInt64
*)&stats
->nstat_txbytes
);
950 OSAddAtomic(tx_retransmit
, &stats
->nstat_txretransmit
);
951 OSAddAtomic64((SInt64
)rx_packets
, (SInt64
*)&stats
->nstat_rxpackets
);
952 OSAddAtomic64((SInt64
)rx_bytes
, (SInt64
*)&stats
->nstat_rxbytes
);
953 OSAddAtomic(rx_outoforderbytes
, &stats
->nstat_rxoutoforderbytes
);
954 OSAddAtomic(rx_duplicatebytes
, &stats
->nstat_rxduplicatebytes
);
957 NSTAT_EWMA_ATOMIC(&stats
->nstat_avg_rtt
, rtt
, decay
);
958 NSTAT_MIN_ATOMIC(&stats
->nstat_min_rtt
, rtt
);
959 NSTAT_EWMA_ATOMIC(&stats
->nstat_var_rtt
, rtt_var
, decay
);
962 rte
= rte
->rt_parent
;
966 #pragma mark -- TCP Kernel Provider --
969 * Due to the way the kernel deallocates a process (the process structure
970 * might be gone by the time we get the PCB detach notification),
971 * we need to cache the process name. Without this, proc_name() would
972 * return null and the process name would never be sent to userland.
974 * For UDP sockets, we also store the cached the connection tuples along with
975 * the interface index. This is necessary because when UDP sockets are
976 * disconnected, the connection tuples are forever lost from the inpcb, thus
977 * we need to keep track of the last call to connect() in ntstat.
979 struct nstat_tucookie
{
981 char pname
[MAXCOMLEN
+ 1];
984 struct sockaddr_in v4
;
985 struct sockaddr_in6 v6
;
988 struct sockaddr_in v4
;
989 struct sockaddr_in6 v6
;
991 unsigned int if_index
;
992 uint16_t ifnet_properties
;
995 static struct nstat_tucookie
*
996 nstat_tucookie_alloc_internal(
1001 struct nstat_tucookie
*cookie
;
1003 cookie
= OSMalloc(sizeof(*cookie
), nstat_malloc_tag
);
1004 if (cookie
== NULL
) {
1008 LCK_MTX_ASSERT(&nstat_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1010 if (ref
&& in_pcb_checkstate(inp
, WNT_ACQUIRE
, locked
) == WNT_STOPUSING
) {
1011 OSFree(cookie
, sizeof(*cookie
), nstat_malloc_tag
);
1014 bzero(cookie
, sizeof(*cookie
));
1016 proc_name(inp
->inp_socket
->last_pid
, cookie
->pname
,
1017 sizeof(cookie
->pname
));
1019 * We only increment the reference count for UDP sockets because we
1020 * only cache UDP socket tuples.
1022 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_UDP
) {
1023 OSIncrementAtomic(&inp
->inp_nstat_refcnt
);
1029 static struct nstat_tucookie
*
1030 nstat_tucookie_alloc(
1033 return nstat_tucookie_alloc_internal(inp
, false, false);
1036 static struct nstat_tucookie
*
1037 nstat_tucookie_alloc_ref(
1040 return nstat_tucookie_alloc_internal(inp
, true, false);
1043 static struct nstat_tucookie
*
1044 nstat_tucookie_alloc_ref_locked(
1047 return nstat_tucookie_alloc_internal(inp
, true, true);
1051 nstat_tucookie_release_internal(
1052 struct nstat_tucookie
*cookie
,
1055 if (SOCK_PROTO(cookie
->inp
->inp_socket
) == IPPROTO_UDP
) {
1056 OSDecrementAtomic(&cookie
->inp
->inp_nstat_refcnt
);
1058 in_pcb_checkstate(cookie
->inp
, WNT_RELEASE
, inplock
);
1059 OSFree(cookie
, sizeof(*cookie
), nstat_malloc_tag
);
1063 nstat_tucookie_release(
1064 struct nstat_tucookie
*cookie
)
1066 nstat_tucookie_release_internal(cookie
, false);
1070 nstat_tucookie_release_locked(
1071 struct nstat_tucookie
*cookie
)
1073 nstat_tucookie_release_internal(cookie
, true);
1077 static nstat_provider nstat_tcp_provider
;
1080 nstat_tcpudp_lookup(
1081 struct inpcbinfo
*inpinfo
,
1084 nstat_provider_cookie_t
*out_cookie
)
1086 struct inpcb
*inp
= NULL
;
1088 // parameter validation
1089 const nstat_tcp_add_param
*param
= (const nstat_tcp_add_param
*)data
;
1090 if (length
< sizeof(*param
)) {
1094 // src and dst must match
1095 if (param
->remote
.v4
.sin_family
!= 0 &&
1096 param
->remote
.v4
.sin_family
!= param
->local
.v4
.sin_family
) {
1101 switch (param
->local
.v4
.sin_family
) {
1104 if (param
->local
.v4
.sin_len
!= sizeof(param
->local
.v4
) ||
1105 (param
->remote
.v4
.sin_family
!= 0 &&
1106 param
->remote
.v4
.sin_len
!= sizeof(param
->remote
.v4
))) {
1110 inp
= in_pcblookup_hash(inpinfo
, param
->remote
.v4
.sin_addr
, param
->remote
.v4
.sin_port
,
1111 param
->local
.v4
.sin_addr
, param
->local
.v4
.sin_port
, 1, NULL
);
1119 const struct in6_addr
*in6c
;
1120 struct in6_addr
*in6
;
1123 if (param
->local
.v6
.sin6_len
!= sizeof(param
->local
.v6
) ||
1124 (param
->remote
.v6
.sin6_family
!= 0 &&
1125 param
->remote
.v6
.sin6_len
!= sizeof(param
->remote
.v6
))) {
1129 local
.in6c
= ¶m
->local
.v6
.sin6_addr
;
1130 remote
.in6c
= ¶m
->remote
.v6
.sin6_addr
;
1132 inp
= in6_pcblookup_hash(inpinfo
, remote
.in6
, param
->remote
.v6
.sin6_port
,
1133 local
.in6
, param
->local
.v6
.sin6_port
, 1, NULL
);
1146 // At this point we have a ref to the inpcb
1147 *out_cookie
= nstat_tucookie_alloc(inp
);
1148 if (*out_cookie
== NULL
) {
1149 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1159 nstat_provider_cookie_t
*out_cookie
)
1161 return nstat_tcpudp_lookup(&tcbinfo
, data
, length
, out_cookie
);
1166 nstat_provider_cookie_t cookie
)
1168 struct nstat_tucookie
*tucookie
=
1169 (struct nstat_tucookie
*)cookie
;
1173 return (!(inp
= tucookie
->inp
) ||
1174 !(tp
= intotcpcb(inp
)) ||
1175 inp
->inp_state
== INPCB_STATE_DEAD
) ? 1 : 0;
1180 nstat_provider_cookie_t cookie
,
1181 struct nstat_counts
*out_counts
,
1184 struct nstat_tucookie
*tucookie
=
1185 (struct nstat_tucookie
*)cookie
;
1188 bzero(out_counts
, sizeof(*out_counts
));
1194 // if the pcb is in the dead state, we should stop using it
1195 if (nstat_tcp_gone(cookie
)) {
1199 if (!(inp
= tucookie
->inp
) || !intotcpcb(inp
)) {
1203 inp
= tucookie
->inp
;
1204 struct tcpcb
*tp
= intotcpcb(inp
);
1206 atomic_get_64(out_counts
->nstat_rxpackets
, &inp
->inp_stat
->rxpackets
);
1207 atomic_get_64(out_counts
->nstat_rxbytes
, &inp
->inp_stat
->rxbytes
);
1208 atomic_get_64(out_counts
->nstat_txpackets
, &inp
->inp_stat
->txpackets
);
1209 atomic_get_64(out_counts
->nstat_txbytes
, &inp
->inp_stat
->txbytes
);
1210 out_counts
->nstat_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1211 out_counts
->nstat_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1212 out_counts
->nstat_txretransmit
= tp
->t_stat
.txretransmitbytes
;
1213 out_counts
->nstat_connectattempts
= tp
->t_state
>= TCPS_SYN_SENT
? 1 : 0;
1214 out_counts
->nstat_connectsuccesses
= tp
->t_state
>= TCPS_ESTABLISHED
? 1 : 0;
1215 out_counts
->nstat_avg_rtt
= tp
->t_srtt
;
1216 out_counts
->nstat_min_rtt
= tp
->t_rttbest
;
1217 out_counts
->nstat_var_rtt
= tp
->t_rttvar
;
1218 if (out_counts
->nstat_avg_rtt
< out_counts
->nstat_min_rtt
) {
1219 out_counts
->nstat_min_rtt
= out_counts
->nstat_avg_rtt
;
1221 atomic_get_64(out_counts
->nstat_cell_rxbytes
, &inp
->inp_cstat
->rxbytes
);
1222 atomic_get_64(out_counts
->nstat_cell_txbytes
, &inp
->inp_cstat
->txbytes
);
1223 atomic_get_64(out_counts
->nstat_wifi_rxbytes
, &inp
->inp_wstat
->rxbytes
);
1224 atomic_get_64(out_counts
->nstat_wifi_txbytes
, &inp
->inp_wstat
->txbytes
);
1225 atomic_get_64(out_counts
->nstat_wired_rxbytes
, &inp
->inp_Wstat
->rxbytes
);
1226 atomic_get_64(out_counts
->nstat_wired_txbytes
, &inp
->inp_Wstat
->txbytes
);
1233 nstat_provider_cookie_t cookie
,
1236 struct nstat_tucookie
*tucookie
=
1237 (struct nstat_tucookie
*)cookie
;
1239 nstat_tucookie_release_internal(tucookie
, locked
);
1243 nstat_tcp_add_watcher(
1244 nstat_control_state
*state
,
1245 nstat_msg_add_all_srcs
*req
)
1247 // There is a tricky issue around getting all TCP sockets added once
1248 // and only once. nstat_tcp_new_pcb() is called prior to the new item
1249 // being placed on any lists where it might be found.
1250 // By locking the tcbinfo.ipi_lock prior to marking the state as a watcher,
1251 // it should be impossible for a new socket to be added twice.
1252 // On the other hand, there is still a timing issue where a new socket
1253 // results in a call to nstat_tcp_new_pcb() before this watcher
1254 // is instantiated and yet the socket doesn't make it into ipi_listhead
1255 // prior to the scan. <rdar://problem/30361716>
1259 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
1260 result
= nstat_set_provider_filter(state
, req
);
1262 OSIncrementAtomic(&nstat_tcp_watchers
);
1264 // Add all current tcp inpcbs. Ignore those in timewait
1266 struct nstat_tucookie
*cookie
;
1267 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
)
1269 cookie
= nstat_tucookie_alloc_ref(inp
);
1270 if (cookie
== NULL
) {
1273 if (nstat_control_source_add(0, state
, &nstat_tcp_provider
,
1275 nstat_tucookie_release(cookie
);
1281 lck_rw_done(tcbinfo
.ipi_lock
);
1287 nstat_tcp_remove_watcher(
1288 __unused nstat_control_state
*state
)
1290 OSDecrementAtomic(&nstat_tcp_watchers
);
1293 __private_extern__
void
1297 struct nstat_tucookie
*cookie
;
1299 inp
->inp_start_timestamp
= mach_continuous_time();
1301 if (nstat_tcp_watchers
== 0) {
1305 socket_lock(inp
->inp_socket
, 0);
1306 lck_mtx_lock(&nstat_mtx
);
1307 nstat_control_state
*state
;
1308 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1309 if ((state
->ncs_watching
& (1 << NSTAT_PROVIDER_TCP_KERNEL
)) != 0) {
1310 // this client is watching tcp
1311 // acquire a reference for it
1312 cookie
= nstat_tucookie_alloc_ref_locked(inp
);
1313 if (cookie
== NULL
) {
1316 // add the source, if that fails, release the reference
1317 if (nstat_control_source_add(0, state
,
1318 &nstat_tcp_provider
, cookie
) != 0) {
1319 nstat_tucookie_release_locked(cookie
);
1324 lck_mtx_unlock(&nstat_mtx
);
1325 socket_unlock(inp
->inp_socket
, 0);
1328 __private_extern__
void
1329 nstat_pcb_detach(struct inpcb
*inp
)
1331 nstat_control_state
*state
;
1333 tailq_head_nstat_src dead_list
;
1334 struct nstat_tucookie
*tucookie
;
1337 if (inp
== NULL
|| (nstat_tcp_watchers
== 0 && nstat_udp_watchers
== 0)) {
1341 TAILQ_INIT(&dead_list
);
1342 lck_mtx_lock(&nstat_mtx
);
1343 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1344 lck_mtx_lock(&state
->ncs_mtx
);
1345 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
1347 nstat_provider_id_t provider_id
= src
->provider
->nstat_provider_id
;
1348 if (provider_id
== NSTAT_PROVIDER_TCP_KERNEL
|| provider_id
== NSTAT_PROVIDER_UDP_KERNEL
) {
1349 tucookie
= (struct nstat_tucookie
*)src
->cookie
;
1350 if (tucookie
->inp
== inp
) {
1357 result
= nstat_control_send_goodbye(state
, src
);
1359 TAILQ_REMOVE(&state
->ncs_src_queue
, src
, ns_control_link
);
1360 TAILQ_INSERT_TAIL(&dead_list
, src
, ns_control_link
);
1362 lck_mtx_unlock(&state
->ncs_mtx
);
1364 lck_mtx_unlock(&nstat_mtx
);
1366 while ((src
= TAILQ_FIRST(&dead_list
))) {
1367 TAILQ_REMOVE(&dead_list
, src
, ns_control_link
);
1368 nstat_control_cleanup_source(NULL
, src
, TRUE
);
1372 __private_extern__
void
1373 nstat_pcb_cache(struct inpcb
*inp
)
1375 nstat_control_state
*state
;
1377 struct nstat_tucookie
*tucookie
;
1379 if (inp
== NULL
|| nstat_udp_watchers
== 0 ||
1380 inp
->inp_nstat_refcnt
== 0) {
1383 VERIFY(SOCK_PROTO(inp
->inp_socket
) == IPPROTO_UDP
);
1384 lck_mtx_lock(&nstat_mtx
);
1385 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1386 lck_mtx_lock(&state
->ncs_mtx
);
1387 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
1389 tucookie
= (struct nstat_tucookie
*)src
->cookie
;
1390 if (tucookie
->inp
== inp
) {
1391 if (inp
->inp_vflag
& INP_IPV6
) {
1392 in6_ip6_to_sockaddr(&inp
->in6p_laddr
,
1394 &tucookie
->local
.v6
,
1395 sizeof(tucookie
->local
));
1396 in6_ip6_to_sockaddr(&inp
->in6p_faddr
,
1398 &tucookie
->remote
.v6
,
1399 sizeof(tucookie
->remote
));
1400 } else if (inp
->inp_vflag
& INP_IPV4
) {
1401 nstat_ip_to_sockaddr(&inp
->inp_laddr
,
1403 &tucookie
->local
.v4
,
1404 sizeof(tucookie
->local
));
1405 nstat_ip_to_sockaddr(&inp
->inp_faddr
,
1407 &tucookie
->remote
.v4
,
1408 sizeof(tucookie
->remote
));
1410 if (inp
->inp_last_outifp
) {
1411 tucookie
->if_index
=
1412 inp
->inp_last_outifp
->if_index
;
1415 tucookie
->ifnet_properties
= nstat_inpcb_to_flags(inp
);
1416 tucookie
->cached
= true;
1420 lck_mtx_unlock(&state
->ncs_mtx
);
1422 lck_mtx_unlock(&nstat_mtx
);
1425 __private_extern__
void
1426 nstat_pcb_invalidate_cache(struct inpcb
*inp
)
1428 nstat_control_state
*state
;
1430 struct nstat_tucookie
*tucookie
;
1432 if (inp
== NULL
|| nstat_udp_watchers
== 0 ||
1433 inp
->inp_nstat_refcnt
== 0) {
1436 VERIFY(SOCK_PROTO(inp
->inp_socket
) == IPPROTO_UDP
);
1437 lck_mtx_lock(&nstat_mtx
);
1438 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1439 lck_mtx_lock(&state
->ncs_mtx
);
1440 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
1442 tucookie
= (struct nstat_tucookie
*)src
->cookie
;
1443 if (tucookie
->inp
== inp
) {
1444 tucookie
->cached
= false;
1448 lck_mtx_unlock(&state
->ncs_mtx
);
1450 lck_mtx_unlock(&nstat_mtx
);
1454 nstat_tcp_copy_descriptor(
1455 nstat_provider_cookie_t cookie
,
1459 if (len
< sizeof(nstat_tcp_descriptor
)) {
1463 if (nstat_tcp_gone(cookie
)) {
1467 nstat_tcp_descriptor
*desc
= (nstat_tcp_descriptor
*)data
;
1468 struct nstat_tucookie
*tucookie
=
1469 (struct nstat_tucookie
*)cookie
;
1470 struct inpcb
*inp
= tucookie
->inp
;
1471 struct tcpcb
*tp
= intotcpcb(inp
);
1472 bzero(desc
, sizeof(*desc
));
1474 if (inp
->inp_vflag
& INP_IPV6
) {
1475 in6_ip6_to_sockaddr(&inp
->in6p_laddr
, inp
->inp_lport
,
1476 &desc
->local
.v6
, sizeof(desc
->local
));
1477 in6_ip6_to_sockaddr(&inp
->in6p_faddr
, inp
->inp_fport
,
1478 &desc
->remote
.v6
, sizeof(desc
->remote
));
1479 } else if (inp
->inp_vflag
& INP_IPV4
) {
1480 nstat_ip_to_sockaddr(&inp
->inp_laddr
, inp
->inp_lport
,
1481 &desc
->local
.v4
, sizeof(desc
->local
));
1482 nstat_ip_to_sockaddr(&inp
->inp_faddr
, inp
->inp_fport
,
1483 &desc
->remote
.v4
, sizeof(desc
->remote
));
1486 desc
->state
= intotcpcb(inp
)->t_state
;
1487 desc
->ifindex
= (inp
->inp_last_outifp
== NULL
) ? 0 :
1488 inp
->inp_last_outifp
->if_index
;
1490 // danger - not locked, values could be bogus
1491 desc
->txunacked
= tp
->snd_max
- tp
->snd_una
;
1492 desc
->txwindow
= tp
->snd_wnd
;
1493 desc
->txcwindow
= tp
->snd_cwnd
;
1495 if (CC_ALGO(tp
)->name
!= NULL
) {
1496 strlcpy(desc
->cc_algo
, CC_ALGO(tp
)->name
,
1497 sizeof(desc
->cc_algo
));
1500 struct socket
*so
= inp
->inp_socket
;
1502 // TBD - take the socket lock around these to make sure
1504 desc
->upid
= so
->last_upid
;
1505 desc
->pid
= so
->last_pid
;
1506 desc
->traffic_class
= so
->so_traffic_class
;
1507 if ((so
->so_flags1
& SOF1_TRAFFIC_MGT_SO_BACKGROUND
)) {
1508 desc
->traffic_mgt_flags
|= TRAFFIC_MGT_SO_BACKGROUND
;
1510 if ((so
->so_flags1
& SOF1_TRAFFIC_MGT_TCP_RECVBG
)) {
1511 desc
->traffic_mgt_flags
|= TRAFFIC_MGT_TCP_RECVBG
;
1513 proc_name(desc
->pid
, desc
->pname
, sizeof(desc
->pname
));
1514 if (desc
->pname
[0] == 0) {
1515 strlcpy(desc
->pname
, tucookie
->pname
,
1516 sizeof(desc
->pname
));
1518 desc
->pname
[sizeof(desc
->pname
) - 1] = 0;
1519 strlcpy(tucookie
->pname
, desc
->pname
,
1520 sizeof(tucookie
->pname
));
1522 memcpy(desc
->uuid
, so
->last_uuid
, sizeof(so
->last_uuid
));
1523 memcpy(desc
->vuuid
, so
->so_vuuid
, sizeof(so
->so_vuuid
));
1524 if (so
->so_flags
& SOF_DELEGATED
) {
1525 desc
->eupid
= so
->e_upid
;
1526 desc
->epid
= so
->e_pid
;
1527 memcpy(desc
->euuid
, so
->e_uuid
, sizeof(so
->e_uuid
));
1529 desc
->eupid
= desc
->upid
;
1530 desc
->epid
= desc
->pid
;
1531 memcpy(desc
->euuid
, desc
->uuid
, sizeof(desc
->uuid
));
1533 desc
->sndbufsize
= so
->so_snd
.sb_hiwat
;
1534 desc
->sndbufused
= so
->so_snd
.sb_cc
;
1535 desc
->rcvbufsize
= so
->so_rcv
.sb_hiwat
;
1536 desc
->rcvbufused
= so
->so_rcv
.sb_cc
;
1539 tcp_get_connectivity_status(tp
, &desc
->connstatus
);
1540 desc
->ifnet_properties
= nstat_inpcb_to_flags(inp
);
1541 inp_get_activity_bitmap(inp
, &desc
->activity_bitmap
);
1542 desc
->start_timestamp
= inp
->inp_start_timestamp
;
1543 desc
->timestamp
= mach_continuous_time();
1548 nstat_tcpudp_reporting_allowed(nstat_provider_cookie_t cookie
, nstat_provider_filter
*filter
, bool is_UDP
)
1552 if ((filter
->npf_flags
& (NSTAT_FILTER_IFNET_FLAGS
| NSTAT_FILTER_SPECIFIC_USER
)) != 0) {
1553 struct nstat_tucookie
*tucookie
= (struct nstat_tucookie
*)cookie
;
1554 struct inpcb
*inp
= tucookie
->inp
;
1556 /* Only apply interface filter if at least one is allowed. */
1557 if ((filter
->npf_flags
& NSTAT_FILTER_IFNET_FLAGS
) != 0) {
1558 uint16_t interface_properties
= nstat_inpcb_to_flags(inp
);
1560 if ((filter
->npf_flags
& interface_properties
) == 0) {
1561 // For UDP, we could have an undefined interface and yet transfers may have occurred.
1562 // We allow reporting if there have been transfers of the requested kind.
1563 // This is imperfect as we cannot account for the expensive attribute over wifi.
1564 // We also assume that cellular is expensive and we have no way to select for AWDL
1567 if ((filter
->npf_flags
& (NSTAT_FILTER_ACCEPT_CELLULAR
| NSTAT_FILTER_ACCEPT_EXPENSIVE
)) &&
1568 (inp
->inp_cstat
->rxbytes
|| inp
->inp_cstat
->txbytes
)) {
1571 if ((filter
->npf_flags
& NSTAT_FILTER_ACCEPT_WIFI
) &&
1572 (inp
->inp_wstat
->rxbytes
|| inp
->inp_wstat
->txbytes
)) {
1575 if ((filter
->npf_flags
& NSTAT_FILTER_ACCEPT_WIRED
) &&
1576 (inp
->inp_Wstat
->rxbytes
|| inp
->inp_Wstat
->txbytes
)) {
1587 if (((filter
->npf_flags
& NSTAT_FILTER_SPECIFIC_USER
) != 0) && (retval
)) {
1588 struct socket
*so
= inp
->inp_socket
;
1592 if (((filter
->npf_flags
& NSTAT_FILTER_SPECIFIC_USER_BY_PID
) != 0) &&
1593 (filter
->npf_pid
== so
->last_pid
)) {
1595 } else if (((filter
->npf_flags
& NSTAT_FILTER_SPECIFIC_USER_BY_EPID
) != 0) &&
1596 (filter
->npf_pid
== (so
->so_flags
& SOF_DELEGATED
)? so
->e_upid
: so
->last_pid
)) {
1598 } else if (((filter
->npf_flags
& NSTAT_FILTER_SPECIFIC_USER_BY_UUID
) != 0) &&
1599 (memcmp(filter
->npf_uuid
, so
->last_uuid
, sizeof(so
->last_uuid
)) == 0)) {
1601 } else if (((filter
->npf_flags
& NSTAT_FILTER_SPECIFIC_USER_BY_EUUID
) != 0) &&
1602 (memcmp(filter
->npf_uuid
, (so
->so_flags
& SOF_DELEGATED
)? so
->e_uuid
: so
->last_uuid
,
1603 sizeof(so
->last_uuid
)) == 0)) {
1613 nstat_tcp_reporting_allowed(nstat_provider_cookie_t cookie
, nstat_provider_filter
*filter
)
1615 return nstat_tcpudp_reporting_allowed(cookie
, filter
, FALSE
);
1619 nstat_init_tcp_provider(void)
1621 bzero(&nstat_tcp_provider
, sizeof(nstat_tcp_provider
));
1622 nstat_tcp_provider
.nstat_descriptor_length
= sizeof(nstat_tcp_descriptor
);
1623 nstat_tcp_provider
.nstat_provider_id
= NSTAT_PROVIDER_TCP_KERNEL
;
1624 nstat_tcp_provider
.nstat_lookup
= nstat_tcp_lookup
;
1625 nstat_tcp_provider
.nstat_gone
= nstat_tcp_gone
;
1626 nstat_tcp_provider
.nstat_counts
= nstat_tcp_counts
;
1627 nstat_tcp_provider
.nstat_release
= nstat_tcp_release
;
1628 nstat_tcp_provider
.nstat_watcher_add
= nstat_tcp_add_watcher
;
1629 nstat_tcp_provider
.nstat_watcher_remove
= nstat_tcp_remove_watcher
;
1630 nstat_tcp_provider
.nstat_copy_descriptor
= nstat_tcp_copy_descriptor
;
1631 nstat_tcp_provider
.nstat_reporting_allowed
= nstat_tcp_reporting_allowed
;
1632 nstat_tcp_provider
.next
= nstat_providers
;
1633 nstat_providers
= &nstat_tcp_provider
;
1636 #pragma mark -- UDP Provider --
1638 static nstat_provider nstat_udp_provider
;
1644 nstat_provider_cookie_t
*out_cookie
)
1646 return nstat_tcpudp_lookup(&udbinfo
, data
, length
, out_cookie
);
1651 nstat_provider_cookie_t cookie
)
1653 struct nstat_tucookie
*tucookie
=
1654 (struct nstat_tucookie
*)cookie
;
1657 return (!(inp
= tucookie
->inp
) ||
1658 inp
->inp_state
== INPCB_STATE_DEAD
) ? 1 : 0;
1663 nstat_provider_cookie_t cookie
,
1664 struct nstat_counts
*out_counts
,
1667 struct nstat_tucookie
*tucookie
=
1668 (struct nstat_tucookie
*)cookie
;
1674 // if the pcb is in the dead state, we should stop using it
1675 if (nstat_udp_gone(cookie
)) {
1679 if (!tucookie
->inp
) {
1683 struct inpcb
*inp
= tucookie
->inp
;
1685 atomic_get_64(out_counts
->nstat_rxpackets
, &inp
->inp_stat
->rxpackets
);
1686 atomic_get_64(out_counts
->nstat_rxbytes
, &inp
->inp_stat
->rxbytes
);
1687 atomic_get_64(out_counts
->nstat_txpackets
, &inp
->inp_stat
->txpackets
);
1688 atomic_get_64(out_counts
->nstat_txbytes
, &inp
->inp_stat
->txbytes
);
1689 atomic_get_64(out_counts
->nstat_cell_rxbytes
, &inp
->inp_cstat
->rxbytes
);
1690 atomic_get_64(out_counts
->nstat_cell_txbytes
, &inp
->inp_cstat
->txbytes
);
1691 atomic_get_64(out_counts
->nstat_wifi_rxbytes
, &inp
->inp_wstat
->rxbytes
);
1692 atomic_get_64(out_counts
->nstat_wifi_txbytes
, &inp
->inp_wstat
->txbytes
);
1693 atomic_get_64(out_counts
->nstat_wired_rxbytes
, &inp
->inp_Wstat
->rxbytes
);
1694 atomic_get_64(out_counts
->nstat_wired_txbytes
, &inp
->inp_Wstat
->txbytes
);
1701 nstat_provider_cookie_t cookie
,
1704 struct nstat_tucookie
*tucookie
=
1705 (struct nstat_tucookie
*)cookie
;
1707 nstat_tucookie_release_internal(tucookie
, locked
);
1711 nstat_udp_add_watcher(
1712 nstat_control_state
*state
,
1713 nstat_msg_add_all_srcs
*req
)
1715 // There is a tricky issue around getting all UDP sockets added once
1716 // and only once. nstat_udp_new_pcb() is called prior to the new item
1717 // being placed on any lists where it might be found.
1718 // By locking the udpinfo.ipi_lock prior to marking the state as a watcher,
1719 // it should be impossible for a new socket to be added twice.
1720 // On the other hand, there is still a timing issue where a new socket
1721 // results in a call to nstat_udp_new_pcb() before this watcher
1722 // is instantiated and yet the socket doesn't make it into ipi_listhead
1723 // prior to the scan. <rdar://problem/30361716>
1727 lck_rw_lock_shared(udbinfo
.ipi_lock
);
1728 result
= nstat_set_provider_filter(state
, req
);
1732 struct nstat_tucookie
*cookie
;
1734 OSIncrementAtomic(&nstat_udp_watchers
);
1736 // Add all current UDP inpcbs.
1737 LIST_FOREACH(inp
, udbinfo
.ipi_listhead
, inp_list
)
1739 cookie
= nstat_tucookie_alloc_ref(inp
);
1740 if (cookie
== NULL
) {
1743 if (nstat_control_source_add(0, state
, &nstat_udp_provider
,
1745 nstat_tucookie_release(cookie
);
1751 lck_rw_done(udbinfo
.ipi_lock
);
1757 nstat_udp_remove_watcher(
1758 __unused nstat_control_state
*state
)
1760 OSDecrementAtomic(&nstat_udp_watchers
);
1763 __private_extern__
void
1767 struct nstat_tucookie
*cookie
;
1769 inp
->inp_start_timestamp
= mach_continuous_time();
1771 if (nstat_udp_watchers
== 0) {
1775 socket_lock(inp
->inp_socket
, 0);
1776 lck_mtx_lock(&nstat_mtx
);
1777 nstat_control_state
*state
;
1778 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1779 if ((state
->ncs_watching
& (1 << NSTAT_PROVIDER_UDP_KERNEL
)) != 0) {
1780 // this client is watching tcp
1781 // acquire a reference for it
1782 cookie
= nstat_tucookie_alloc_ref_locked(inp
);
1783 if (cookie
== NULL
) {
1786 // add the source, if that fails, release the reference
1787 if (nstat_control_source_add(0, state
,
1788 &nstat_udp_provider
, cookie
) != 0) {
1789 nstat_tucookie_release_locked(cookie
);
1794 lck_mtx_unlock(&nstat_mtx
);
1795 socket_unlock(inp
->inp_socket
, 0);
1799 nstat_udp_copy_descriptor(
1800 nstat_provider_cookie_t cookie
,
1804 if (len
< sizeof(nstat_udp_descriptor
)) {
1808 if (nstat_udp_gone(cookie
)) {
1812 struct nstat_tucookie
*tucookie
=
1813 (struct nstat_tucookie
*)cookie
;
1814 nstat_udp_descriptor
*desc
= (nstat_udp_descriptor
*)data
;
1815 struct inpcb
*inp
= tucookie
->inp
;
1817 bzero(desc
, sizeof(*desc
));
1819 if (tucookie
->cached
== false) {
1820 if (inp
->inp_vflag
& INP_IPV6
) {
1821 in6_ip6_to_sockaddr(&inp
->in6p_laddr
, inp
->inp_lport
,
1822 &desc
->local
.v6
, sizeof(desc
->local
.v6
));
1823 in6_ip6_to_sockaddr(&inp
->in6p_faddr
, inp
->inp_fport
,
1824 &desc
->remote
.v6
, sizeof(desc
->remote
.v6
));
1825 } else if (inp
->inp_vflag
& INP_IPV4
) {
1826 nstat_ip_to_sockaddr(&inp
->inp_laddr
, inp
->inp_lport
,
1827 &desc
->local
.v4
, sizeof(desc
->local
.v4
));
1828 nstat_ip_to_sockaddr(&inp
->inp_faddr
, inp
->inp_fport
,
1829 &desc
->remote
.v4
, sizeof(desc
->remote
.v4
));
1831 desc
->ifnet_properties
= nstat_inpcb_to_flags(inp
);
1833 if (inp
->inp_vflag
& INP_IPV6
) {
1834 memcpy(&desc
->local
.v6
, &tucookie
->local
.v6
,
1835 sizeof(desc
->local
.v6
));
1836 memcpy(&desc
->remote
.v6
, &tucookie
->remote
.v6
,
1837 sizeof(desc
->remote
.v6
));
1838 } else if (inp
->inp_vflag
& INP_IPV4
) {
1839 memcpy(&desc
->local
.v4
, &tucookie
->local
.v4
,
1840 sizeof(desc
->local
.v4
));
1841 memcpy(&desc
->remote
.v4
, &tucookie
->remote
.v4
,
1842 sizeof(desc
->remote
.v4
));
1844 desc
->ifnet_properties
= tucookie
->ifnet_properties
;
1847 if (inp
->inp_last_outifp
) {
1848 desc
->ifindex
= inp
->inp_last_outifp
->if_index
;
1850 desc
->ifindex
= tucookie
->if_index
;
1853 struct socket
*so
= inp
->inp_socket
;
1855 // TBD - take the socket lock around these to make sure
1857 desc
->upid
= so
->last_upid
;
1858 desc
->pid
= so
->last_pid
;
1859 proc_name(desc
->pid
, desc
->pname
, sizeof(desc
->pname
));
1860 if (desc
->pname
[0] == 0) {
1861 strlcpy(desc
->pname
, tucookie
->pname
,
1862 sizeof(desc
->pname
));
1864 desc
->pname
[sizeof(desc
->pname
) - 1] = 0;
1865 strlcpy(tucookie
->pname
, desc
->pname
,
1866 sizeof(tucookie
->pname
));
1868 memcpy(desc
->uuid
, so
->last_uuid
, sizeof(so
->last_uuid
));
1869 memcpy(desc
->vuuid
, so
->so_vuuid
, sizeof(so
->so_vuuid
));
1870 if (so
->so_flags
& SOF_DELEGATED
) {
1871 desc
->eupid
= so
->e_upid
;
1872 desc
->epid
= so
->e_pid
;
1873 memcpy(desc
->euuid
, so
->e_uuid
, sizeof(so
->e_uuid
));
1875 desc
->eupid
= desc
->upid
;
1876 desc
->epid
= desc
->pid
;
1877 memcpy(desc
->euuid
, desc
->uuid
, sizeof(desc
->uuid
));
1879 desc
->rcvbufsize
= so
->so_rcv
.sb_hiwat
;
1880 desc
->rcvbufused
= so
->so_rcv
.sb_cc
;
1881 desc
->traffic_class
= so
->so_traffic_class
;
1882 inp_get_activity_bitmap(inp
, &desc
->activity_bitmap
);
1883 desc
->start_timestamp
= inp
->inp_start_timestamp
;
1884 desc
->timestamp
= mach_continuous_time();
1891 nstat_udp_reporting_allowed(nstat_provider_cookie_t cookie
, nstat_provider_filter
*filter
)
1893 return nstat_tcpudp_reporting_allowed(cookie
, filter
, TRUE
);
1898 nstat_init_udp_provider(void)
1900 bzero(&nstat_udp_provider
, sizeof(nstat_udp_provider
));
1901 nstat_udp_provider
.nstat_provider_id
= NSTAT_PROVIDER_UDP_KERNEL
;
1902 nstat_udp_provider
.nstat_descriptor_length
= sizeof(nstat_udp_descriptor
);
1903 nstat_udp_provider
.nstat_lookup
= nstat_udp_lookup
;
1904 nstat_udp_provider
.nstat_gone
= nstat_udp_gone
;
1905 nstat_udp_provider
.nstat_counts
= nstat_udp_counts
;
1906 nstat_udp_provider
.nstat_watcher_add
= nstat_udp_add_watcher
;
1907 nstat_udp_provider
.nstat_watcher_remove
= nstat_udp_remove_watcher
;
1908 nstat_udp_provider
.nstat_copy_descriptor
= nstat_udp_copy_descriptor
;
1909 nstat_udp_provider
.nstat_release
= nstat_udp_release
;
1910 nstat_udp_provider
.nstat_reporting_allowed
= nstat_udp_reporting_allowed
;
1911 nstat_udp_provider
.next
= nstat_providers
;
1912 nstat_providers
= &nstat_udp_provider
;
1917 #pragma mark -- ifnet Provider --
1919 static nstat_provider nstat_ifnet_provider
;
1922 * We store a pointer to the ifnet and the original threshold
1923 * requested by the client.
1925 struct nstat_ifnet_cookie
{
1934 nstat_provider_cookie_t
*out_cookie
)
1936 const nstat_ifnet_add_param
*param
= (const nstat_ifnet_add_param
*)data
;
1938 boolean_t changed
= FALSE
;
1939 nstat_control_state
*state
;
1941 struct nstat_ifnet_cookie
*cookie
;
1943 if (length
< sizeof(*param
) || param
->threshold
< 1024 * 1024) {
1946 if (nstat_privcheck
!= 0) {
1947 errno_t result
= priv_check_cred(kauth_cred_get(),
1948 PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0);
1953 cookie
= OSMalloc(sizeof(*cookie
), nstat_malloc_tag
);
1954 if (cookie
== NULL
) {
1957 bzero(cookie
, sizeof(*cookie
));
1959 ifnet_head_lock_shared();
1960 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
1962 ifnet_lock_exclusive(ifp
);
1963 if (ifp
->if_index
== param
->ifindex
) {
1965 cookie
->threshold
= param
->threshold
;
1966 *out_cookie
= cookie
;
1967 if (!ifp
->if_data_threshold
||
1968 ifp
->if_data_threshold
> param
->threshold
) {
1970 ifp
->if_data_threshold
= param
->threshold
;
1972 ifnet_lock_done(ifp
);
1973 ifnet_reference(ifp
);
1976 ifnet_lock_done(ifp
);
1981 * When we change the threshold to something smaller, we notify
1982 * all of our clients with a description message.
1983 * We won't send a message to the client we are currently serving
1984 * because it has no `ifnet source' yet.
1987 lck_mtx_lock(&nstat_mtx
);
1988 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
1989 lck_mtx_lock(&state
->ncs_mtx
);
1990 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
1992 if (src
->provider
!= &nstat_ifnet_provider
) {
1995 nstat_control_send_description(state
, src
, 0, 0);
1997 lck_mtx_unlock(&state
->ncs_mtx
);
1999 lck_mtx_unlock(&nstat_mtx
);
2001 if (cookie
->ifp
== NULL
) {
2002 OSFree(cookie
, sizeof(*cookie
), nstat_malloc_tag
);
2005 return ifp
? 0 : EINVAL
;
2010 nstat_provider_cookie_t cookie
)
2013 struct nstat_ifnet_cookie
*ifcookie
=
2014 (struct nstat_ifnet_cookie
*)cookie
;
2016 ifnet_head_lock_shared();
2017 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
2019 if (ifp
== ifcookie
->ifp
) {
2030 nstat_provider_cookie_t cookie
,
2031 struct nstat_counts
*out_counts
,
2034 struct nstat_ifnet_cookie
*ifcookie
=
2035 (struct nstat_ifnet_cookie
*)cookie
;
2036 struct ifnet
*ifp
= ifcookie
->ifp
;
2042 // if the ifnet is gone, we should stop using it
2043 if (nstat_ifnet_gone(cookie
)) {
2050 bzero(out_counts
, sizeof(*out_counts
));
2051 out_counts
->nstat_rxpackets
= ifp
->if_ipackets
;
2052 out_counts
->nstat_rxbytes
= ifp
->if_ibytes
;
2053 out_counts
->nstat_txpackets
= ifp
->if_opackets
;
2054 out_counts
->nstat_txbytes
= ifp
->if_obytes
;
2055 out_counts
->nstat_cell_rxbytes
= out_counts
->nstat_cell_txbytes
= 0;
2060 nstat_ifnet_release(
2061 nstat_provider_cookie_t cookie
,
2062 __unused
int locked
)
2064 struct nstat_ifnet_cookie
*ifcookie
;
2066 nstat_control_state
*state
;
2068 uint64_t minthreshold
= UINT64_MAX
;
2071 * Find all the clients that requested a threshold
2072 * for this ifnet and re-calculate if_data_threshold.
2074 lck_mtx_lock(&nstat_mtx
);
2075 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
2076 lck_mtx_lock(&state
->ncs_mtx
);
2077 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
2079 /* Skip the provider we are about to detach. */
2080 if (src
->provider
!= &nstat_ifnet_provider
||
2081 src
->cookie
== cookie
) {
2084 ifcookie
= (struct nstat_ifnet_cookie
*)src
->cookie
;
2085 if (ifcookie
->threshold
< minthreshold
) {
2086 minthreshold
= ifcookie
->threshold
;
2089 lck_mtx_unlock(&state
->ncs_mtx
);
2091 lck_mtx_unlock(&nstat_mtx
);
2093 * Reset if_data_threshold or disable it.
2095 ifcookie
= (struct nstat_ifnet_cookie
*)cookie
;
2096 ifp
= ifcookie
->ifp
;
2097 if (ifnet_is_attached(ifp
, 1)) {
2098 ifnet_lock_exclusive(ifp
);
2099 if (minthreshold
== UINT64_MAX
) {
2100 ifp
->if_data_threshold
= 0;
2102 ifp
->if_data_threshold
= minthreshold
;
2104 ifnet_lock_done(ifp
);
2105 ifnet_decr_iorefcnt(ifp
);
2108 OSFree(ifcookie
, sizeof(*ifcookie
), nstat_malloc_tag
);
2112 nstat_ifnet_copy_link_status(
2114 struct nstat_ifnet_descriptor
*desc
)
2116 struct if_link_status
*ifsr
= ifp
->if_link_status
;
2117 nstat_ifnet_desc_link_status
*link_status
= &desc
->link_status
;
2119 link_status
->link_status_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_NONE
;
2124 lck_rw_lock_shared(&ifp
->if_link_status_lock
);
2126 if (ifp
->if_type
== IFT_CELLULAR
) {
2127 nstat_ifnet_desc_cellular_status
*cell_status
= &link_status
->u
.cellular
;
2128 struct if_cellular_status_v1
*if_cell_sr
=
2129 &ifsr
->ifsr_u
.ifsr_cell
.if_cell_u
.if_status_v1
;
2131 if (ifsr
->ifsr_version
!= IF_CELLULAR_STATUS_REPORT_VERSION_1
) {
2135 link_status
->link_status_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_CELLULAR
;
2137 if (if_cell_sr
->valid_bitmask
& IF_CELL_LINK_QUALITY_METRIC_VALID
) {
2138 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_LINK_QUALITY_METRIC_VALID
;
2139 cell_status
->link_quality_metric
= if_cell_sr
->link_quality_metric
;
2141 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_EFFECTIVE_BANDWIDTH_VALID
) {
2142 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_BANDWIDTH_VALID
;
2143 cell_status
->ul_effective_bandwidth
= if_cell_sr
->ul_effective_bandwidth
;
2145 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MAX_BANDWIDTH_VALID
) {
2146 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_MAX_BANDWIDTH_VALID
;
2147 cell_status
->ul_max_bandwidth
= if_cell_sr
->ul_max_bandwidth
;
2149 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MIN_LATENCY_VALID
) {
2150 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_MIN_LATENCY_VALID
;
2151 cell_status
->ul_min_latency
= if_cell_sr
->ul_min_latency
;
2153 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_EFFECTIVE_LATENCY_VALID
) {
2154 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_LATENCY_VALID
;
2155 cell_status
->ul_effective_latency
= if_cell_sr
->ul_effective_latency
;
2157 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MAX_LATENCY_VALID
) {
2158 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_MAX_LATENCY_VALID
;
2159 cell_status
->ul_max_latency
= if_cell_sr
->ul_max_latency
;
2161 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_RETXT_LEVEL_VALID
) {
2162 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_VALID
;
2163 if (if_cell_sr
->ul_retxt_level
== IF_CELL_UL_RETXT_LEVEL_NONE
) {
2164 cell_status
->ul_retxt_level
= NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_NONE
;
2165 } else if (if_cell_sr
->ul_retxt_level
== IF_CELL_UL_RETXT_LEVEL_LOW
) {
2166 cell_status
->ul_retxt_level
= NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_LOW
;
2167 } else if (if_cell_sr
->ul_retxt_level
== IF_CELL_UL_RETXT_LEVEL_MEDIUM
) {
2168 cell_status
->ul_retxt_level
= NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_MEDIUM
;
2169 } else if (if_cell_sr
->ul_retxt_level
== IF_CELL_UL_RETXT_LEVEL_HIGH
) {
2170 cell_status
->ul_retxt_level
= NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_HIGH
;
2172 cell_status
->valid_bitmask
&= ~NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_VALID
;
2175 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_BYTES_LOST_VALID
) {
2176 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_BYTES_LOST_VALID
;
2177 cell_status
->ul_bytes_lost
= if_cell_sr
->ul_bytes_lost
;
2179 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MIN_QUEUE_SIZE_VALID
) {
2180 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_MIN_QUEUE_SIZE_VALID
;
2181 cell_status
->ul_min_queue_size
= if_cell_sr
->ul_min_queue_size
;
2183 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_AVG_QUEUE_SIZE_VALID
) {
2184 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_AVG_QUEUE_SIZE_VALID
;
2185 cell_status
->ul_avg_queue_size
= if_cell_sr
->ul_avg_queue_size
;
2187 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MAX_QUEUE_SIZE_VALID
) {
2188 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_UL_MAX_QUEUE_SIZE_VALID
;
2189 cell_status
->ul_max_queue_size
= if_cell_sr
->ul_max_queue_size
;
2191 if (if_cell_sr
->valid_bitmask
& IF_CELL_DL_EFFECTIVE_BANDWIDTH_VALID
) {
2192 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_DL_EFFECTIVE_BANDWIDTH_VALID
;
2193 cell_status
->dl_effective_bandwidth
= if_cell_sr
->dl_effective_bandwidth
;
2195 if (if_cell_sr
->valid_bitmask
& IF_CELL_DL_MAX_BANDWIDTH_VALID
) {
2196 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_DL_MAX_BANDWIDTH_VALID
;
2197 cell_status
->dl_max_bandwidth
= if_cell_sr
->dl_max_bandwidth
;
2199 if (if_cell_sr
->valid_bitmask
& IF_CELL_CONFIG_INACTIVITY_TIME_VALID
) {
2200 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_CONFIG_INACTIVITY_TIME_VALID
;
2201 cell_status
->config_inactivity_time
= if_cell_sr
->config_inactivity_time
;
2203 if (if_cell_sr
->valid_bitmask
& IF_CELL_CONFIG_BACKOFF_TIME_VALID
) {
2204 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_CONFIG_BACKOFF_TIME_VALID
;
2205 cell_status
->config_backoff_time
= if_cell_sr
->config_backoff_time
;
2207 if (if_cell_sr
->valid_bitmask
& IF_CELL_UL_MSS_RECOMMENDED_VALID
) {
2208 cell_status
->valid_bitmask
|= NSTAT_IFNET_DESC_CELL_MSS_RECOMMENDED_VALID
;
2209 cell_status
->mss_recommended
= if_cell_sr
->mss_recommended
;
2211 } else if (ifp
->if_subfamily
== IFNET_SUBFAMILY_WIFI
) {
2212 nstat_ifnet_desc_wifi_status
*wifi_status
= &link_status
->u
.wifi
;
2213 struct if_wifi_status_v1
*if_wifi_sr
=
2214 &ifsr
->ifsr_u
.ifsr_wifi
.if_wifi_u
.if_status_v1
;
2216 if (ifsr
->ifsr_version
!= IF_WIFI_STATUS_REPORT_VERSION_1
) {
2220 link_status
->link_status_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_WIFI
;
2222 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_LINK_QUALITY_METRIC_VALID
) {
2223 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_LINK_QUALITY_METRIC_VALID
;
2224 wifi_status
->link_quality_metric
= if_wifi_sr
->link_quality_metric
;
2226 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID
) {
2227 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID
;
2228 wifi_status
->ul_effective_bandwidth
= if_wifi_sr
->ul_effective_bandwidth
;
2230 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_MAX_BANDWIDTH_VALID
) {
2231 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_MAX_BANDWIDTH_VALID
;
2232 wifi_status
->ul_max_bandwidth
= if_wifi_sr
->ul_max_bandwidth
;
2234 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_MIN_LATENCY_VALID
) {
2235 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_MIN_LATENCY_VALID
;
2236 wifi_status
->ul_min_latency
= if_wifi_sr
->ul_min_latency
;
2238 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_EFFECTIVE_LATENCY_VALID
) {
2239 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_LATENCY_VALID
;
2240 wifi_status
->ul_effective_latency
= if_wifi_sr
->ul_effective_latency
;
2242 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_MAX_LATENCY_VALID
) {
2243 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_MAX_LATENCY_VALID
;
2244 wifi_status
->ul_max_latency
= if_wifi_sr
->ul_max_latency
;
2246 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_RETXT_LEVEL_VALID
) {
2247 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_VALID
;
2248 if (if_wifi_sr
->ul_retxt_level
== IF_WIFI_UL_RETXT_LEVEL_NONE
) {
2249 wifi_status
->ul_retxt_level
= NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_NONE
;
2250 } else if (if_wifi_sr
->ul_retxt_level
== IF_WIFI_UL_RETXT_LEVEL_LOW
) {
2251 wifi_status
->ul_retxt_level
= NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_LOW
;
2252 } else if (if_wifi_sr
->ul_retxt_level
== IF_WIFI_UL_RETXT_LEVEL_MEDIUM
) {
2253 wifi_status
->ul_retxt_level
= NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_MEDIUM
;
2254 } else if (if_wifi_sr
->ul_retxt_level
== IF_WIFI_UL_RETXT_LEVEL_HIGH
) {
2255 wifi_status
->ul_retxt_level
= NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_HIGH
;
2257 wifi_status
->valid_bitmask
&= ~NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_VALID
;
2260 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_BYTES_LOST_VALID
) {
2261 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_BYTES_LOST_VALID
;
2262 wifi_status
->ul_bytes_lost
= if_wifi_sr
->ul_bytes_lost
;
2264 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_UL_ERROR_RATE_VALID
) {
2265 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_UL_ERROR_RATE_VALID
;
2266 wifi_status
->ul_error_rate
= if_wifi_sr
->ul_error_rate
;
2268 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID
) {
2269 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID
;
2270 wifi_status
->dl_effective_bandwidth
= if_wifi_sr
->dl_effective_bandwidth
;
2272 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_MAX_BANDWIDTH_VALID
) {
2273 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_MAX_BANDWIDTH_VALID
;
2274 wifi_status
->dl_max_bandwidth
= if_wifi_sr
->dl_max_bandwidth
;
2276 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_MIN_LATENCY_VALID
) {
2277 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_MIN_LATENCY_VALID
;
2278 wifi_status
->dl_min_latency
= if_wifi_sr
->dl_min_latency
;
2280 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_EFFECTIVE_LATENCY_VALID
) {
2281 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_LATENCY_VALID
;
2282 wifi_status
->dl_effective_latency
= if_wifi_sr
->dl_effective_latency
;
2284 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_MAX_LATENCY_VALID
) {
2285 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_MAX_LATENCY_VALID
;
2286 wifi_status
->dl_max_latency
= if_wifi_sr
->dl_max_latency
;
2288 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_DL_ERROR_RATE_VALID
) {
2289 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_DL_ERROR_RATE_VALID
;
2290 wifi_status
->dl_error_rate
= if_wifi_sr
->dl_error_rate
;
2292 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_CONFIG_FREQUENCY_VALID
) {
2293 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_VALID
;
2294 if (if_wifi_sr
->config_frequency
== IF_WIFI_CONFIG_FREQUENCY_2_4_GHZ
) {
2295 wifi_status
->config_frequency
= NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_2_4_GHZ
;
2296 } else if (if_wifi_sr
->config_frequency
== IF_WIFI_CONFIG_FREQUENCY_5_0_GHZ
) {
2297 wifi_status
->config_frequency
= NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_5_0_GHZ
;
2299 wifi_status
->valid_bitmask
&= ~NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_VALID
;
2302 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_CONFIG_MULTICAST_RATE_VALID
) {
2303 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_CONFIG_MULTICAST_RATE_VALID
;
2304 wifi_status
->config_multicast_rate
= if_wifi_sr
->config_multicast_rate
;
2306 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_CONFIG_SCAN_COUNT_VALID
) {
2307 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_COUNT_VALID
;
2308 wifi_status
->scan_count
= if_wifi_sr
->scan_count
;
2310 if (if_wifi_sr
->valid_bitmask
& IF_WIFI_CONFIG_SCAN_DURATION_VALID
) {
2311 wifi_status
->valid_bitmask
|= NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_DURATION_VALID
;
2312 wifi_status
->scan_duration
= if_wifi_sr
->scan_duration
;
2317 lck_rw_done(&ifp
->if_link_status_lock
);
2320 static u_int64_t nstat_ifnet_last_report_time
= 0;
2321 extern int tcp_report_stats_interval
;
2324 nstat_ifnet_compute_percentages(struct if_tcp_ecn_perf_stat
*ifst
)
2326 /* Retransmit percentage */
2327 if (ifst
->total_rxmitpkts
> 0 && ifst
->total_txpkts
> 0) {
2328 /* shift by 10 for precision */
2329 ifst
->rxmit_percent
=
2330 ((ifst
->total_rxmitpkts
<< 10) * 100) / ifst
->total_txpkts
;
2332 ifst
->rxmit_percent
= 0;
2335 /* Out-of-order percentage */
2336 if (ifst
->total_oopkts
> 0 && ifst
->total_rxpkts
> 0) {
2337 /* shift by 10 for precision */
2339 ((ifst
->total_oopkts
<< 10) * 100) / ifst
->total_rxpkts
;
2341 ifst
->oo_percent
= 0;
2344 /* Reorder percentage */
2345 if (ifst
->total_reorderpkts
> 0 &&
2346 (ifst
->total_txpkts
+ ifst
->total_rxpkts
) > 0) {
2347 /* shift by 10 for precision */
2348 ifst
->reorder_percent
=
2349 ((ifst
->total_reorderpkts
<< 10) * 100) /
2350 (ifst
->total_txpkts
+ ifst
->total_rxpkts
);
2352 ifst
->reorder_percent
= 0;
2357 nstat_ifnet_normalize_counter(struct if_tcp_ecn_stat
*if_st
)
2359 u_int64_t ecn_on_conn
, ecn_off_conn
;
2361 if (if_st
== NULL
) {
2364 ecn_on_conn
= if_st
->ecn_client_success
+
2365 if_st
->ecn_server_success
;
2366 ecn_off_conn
= if_st
->ecn_off_conn
+
2367 (if_st
->ecn_client_setup
- if_st
->ecn_client_success
) +
2368 (if_st
->ecn_server_setup
- if_st
->ecn_server_success
);
2371 * report sack episodes, rst_drop and rxmit_drop
2372 * as a ratio per connection, shift by 10 for precision
2374 if (ecn_on_conn
> 0) {
2375 if_st
->ecn_on
.sack_episodes
=
2376 (if_st
->ecn_on
.sack_episodes
<< 10) / ecn_on_conn
;
2377 if_st
->ecn_on
.rst_drop
=
2378 (if_st
->ecn_on
.rst_drop
<< 10) * 100 / ecn_on_conn
;
2379 if_st
->ecn_on
.rxmit_drop
=
2380 (if_st
->ecn_on
.rxmit_drop
<< 10) * 100 / ecn_on_conn
;
2382 /* set to zero, just in case */
2383 if_st
->ecn_on
.sack_episodes
= 0;
2384 if_st
->ecn_on
.rst_drop
= 0;
2385 if_st
->ecn_on
.rxmit_drop
= 0;
2388 if (ecn_off_conn
> 0) {
2389 if_st
->ecn_off
.sack_episodes
=
2390 (if_st
->ecn_off
.sack_episodes
<< 10) / ecn_off_conn
;
2391 if_st
->ecn_off
.rst_drop
=
2392 (if_st
->ecn_off
.rst_drop
<< 10) * 100 / ecn_off_conn
;
2393 if_st
->ecn_off
.rxmit_drop
=
2394 (if_st
->ecn_off
.rxmit_drop
<< 10) * 100 / ecn_off_conn
;
2396 if_st
->ecn_off
.sack_episodes
= 0;
2397 if_st
->ecn_off
.rst_drop
= 0;
2398 if_st
->ecn_off
.rxmit_drop
= 0;
2400 if_st
->ecn_total_conn
= ecn_off_conn
+ ecn_on_conn
;
2404 nstat_ifnet_report_ecn_stats(void)
2406 u_int64_t uptime
, last_report_time
;
2407 struct nstat_sysinfo_data data
;
2408 struct nstat_sysinfo_ifnet_ecn_stats
*st
;
2411 uptime
= net_uptime();
2413 if ((int)(uptime
- nstat_ifnet_last_report_time
) <
2414 tcp_report_stats_interval
) {
2418 last_report_time
= nstat_ifnet_last_report_time
;
2419 nstat_ifnet_last_report_time
= uptime
;
2420 data
.flags
= NSTAT_SYSINFO_IFNET_ECN_STATS
;
2421 st
= &data
.u
.ifnet_ecn_stats
;
2423 ifnet_head_lock_shared();
2424 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2425 if (ifp
->if_ipv4_stat
== NULL
|| ifp
->if_ipv6_stat
== NULL
) {
2429 if (!IF_FULLY_ATTACHED(ifp
)) {
2433 /* Limit reporting to Wifi, Ethernet and cellular. */
2434 if (!(IFNET_IS_ETHERNET(ifp
) || IFNET_IS_CELLULAR(ifp
))) {
2438 bzero(st
, sizeof(*st
));
2439 if (IFNET_IS_CELLULAR(ifp
)) {
2440 st
->ifnet_type
= NSTAT_IFNET_ECN_TYPE_CELLULAR
;
2441 } else if (IFNET_IS_WIFI(ifp
)) {
2442 st
->ifnet_type
= NSTAT_IFNET_ECN_TYPE_WIFI
;
2444 st
->ifnet_type
= NSTAT_IFNET_ECN_TYPE_ETHERNET
;
2446 data
.unsent_data_cnt
= ifp
->if_unsent_data_cnt
;
2447 /* skip if there was no update since last report */
2448 if (ifp
->if_ipv4_stat
->timestamp
<= 0 ||
2449 ifp
->if_ipv4_stat
->timestamp
< last_report_time
) {
2452 st
->ifnet_proto
= NSTAT_IFNET_ECN_PROTO_IPV4
;
2453 /* compute percentages using packet counts */
2454 nstat_ifnet_compute_percentages(&ifp
->if_ipv4_stat
->ecn_on
);
2455 nstat_ifnet_compute_percentages(&ifp
->if_ipv4_stat
->ecn_off
);
2456 nstat_ifnet_normalize_counter(ifp
->if_ipv4_stat
);
2457 bcopy(ifp
->if_ipv4_stat
, &st
->ecn_stat
,
2458 sizeof(st
->ecn_stat
));
2459 nstat_sysinfo_send_data(&data
);
2460 bzero(ifp
->if_ipv4_stat
, sizeof(*ifp
->if_ipv4_stat
));
2463 /* skip if there was no update since last report */
2464 if (ifp
->if_ipv6_stat
->timestamp
<= 0 ||
2465 ifp
->if_ipv6_stat
->timestamp
< last_report_time
) {
2468 st
->ifnet_proto
= NSTAT_IFNET_ECN_PROTO_IPV6
;
2470 /* compute percentages using packet counts */
2471 nstat_ifnet_compute_percentages(&ifp
->if_ipv6_stat
->ecn_on
);
2472 nstat_ifnet_compute_percentages(&ifp
->if_ipv6_stat
->ecn_off
);
2473 nstat_ifnet_normalize_counter(ifp
->if_ipv6_stat
);
2474 bcopy(ifp
->if_ipv6_stat
, &st
->ecn_stat
,
2475 sizeof(st
->ecn_stat
));
2476 nstat_sysinfo_send_data(&data
);
2478 /* Zero the stats in ifp */
2479 bzero(ifp
->if_ipv6_stat
, sizeof(*ifp
->if_ipv6_stat
));
2484 /* Some thresholds to determine Low Iternet mode */
2485 #define NSTAT_LIM_DL_MAX_BANDWIDTH_THRESHOLD 1000000 /* 1 Mbps */
2486 #define NSTAT_LIM_UL_MAX_BANDWIDTH_THRESHOLD 500000 /* 500 Kbps */
2487 #define NSTAT_LIM_UL_MIN_RTT_THRESHOLD 1000 /* 1 second */
2488 #define NSTAT_LIM_CONN_TIMEOUT_PERCENT_THRESHOLD (10 << 10) /* 10 percent connection timeouts */
2489 #define NSTAT_LIM_PACKET_LOSS_PERCENT_THRESHOLD (2 << 10) /* 2 percent packet loss rate */
2492 nstat_lim_activity_check(struct if_lim_perf_stat
*st
)
2494 /* check that the current activity is enough to report stats */
2495 if (st
->lim_total_txpkts
< nstat_lim_min_tx_pkts
||
2496 st
->lim_total_rxpkts
< nstat_lim_min_rx_pkts
||
2497 st
->lim_conn_attempts
== 0) {
2502 * Compute percentages if there was enough activity. Use
2503 * shift-left by 10 to preserve precision.
2505 st
->lim_packet_loss_percent
= ((st
->lim_total_retxpkts
<< 10) /
2506 st
->lim_total_txpkts
) * 100;
2508 st
->lim_packet_ooo_percent
= ((st
->lim_total_oopkts
<< 10) /
2509 st
->lim_total_rxpkts
) * 100;
2511 st
->lim_conn_timeout_percent
= ((st
->lim_conn_timeouts
<< 10) /
2512 st
->lim_conn_attempts
) * 100;
2515 * Is Low Internet detected? First order metrics are bandwidth
2516 * and RTT. If these metrics are below the minimum thresholds
2517 * defined then the network attachment can be classified as
2518 * having Low Internet capacity.
2520 * High connection timeout rate also indicates Low Internet
2523 if (st
->lim_dl_max_bandwidth
> 0 &&
2524 st
->lim_dl_max_bandwidth
<= NSTAT_LIM_DL_MAX_BANDWIDTH_THRESHOLD
) {
2525 st
->lim_dl_detected
= 1;
2528 if ((st
->lim_ul_max_bandwidth
> 0 &&
2529 st
->lim_ul_max_bandwidth
<= NSTAT_LIM_UL_MAX_BANDWIDTH_THRESHOLD
) ||
2530 st
->lim_rtt_min
>= NSTAT_LIM_UL_MIN_RTT_THRESHOLD
) {
2531 st
->lim_ul_detected
= 1;
2534 if (st
->lim_conn_attempts
> 20 &&
2535 st
->lim_conn_timeout_percent
>=
2536 NSTAT_LIM_CONN_TIMEOUT_PERCENT_THRESHOLD
) {
2537 st
->lim_ul_detected
= 1;
2540 * Second order metrics: If there was high packet loss even after
2541 * using delay based algorithms then we classify it as Low Internet
2544 if (st
->lim_bk_txpkts
>= nstat_lim_min_tx_pkts
&&
2545 st
->lim_packet_loss_percent
>=
2546 NSTAT_LIM_PACKET_LOSS_PERCENT_THRESHOLD
) {
2547 st
->lim_ul_detected
= 1;
2552 static u_int64_t nstat_lim_last_report_time
= 0;
2554 nstat_ifnet_report_lim_stats(void)
2557 struct nstat_sysinfo_data data
;
2558 struct nstat_sysinfo_lim_stats
*st
;
2562 uptime
= net_uptime();
2564 if ((u_int32_t
)(uptime
- nstat_lim_last_report_time
) <
2565 nstat_lim_interval
) {
2569 nstat_lim_last_report_time
= uptime
;
2570 data
.flags
= NSTAT_SYSINFO_LIM_STATS
;
2571 st
= &data
.u
.lim_stats
;
2572 data
.unsent_data_cnt
= 0;
2574 ifnet_head_lock_shared();
2575 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
2576 if (!IF_FULLY_ATTACHED(ifp
)) {
2580 /* Limit reporting to Wifi, Ethernet and cellular */
2581 if (!(IFNET_IS_ETHERNET(ifp
) || IFNET_IS_CELLULAR(ifp
))) {
2585 if (!nstat_lim_activity_check(&ifp
->if_lim_stat
)) {
2589 bzero(st
, sizeof(*st
));
2590 st
->ifnet_siglen
= sizeof(st
->ifnet_signature
);
2591 err
= ifnet_get_netsignature(ifp
, AF_INET
,
2592 (u_int8_t
*)&st
->ifnet_siglen
, NULL
,
2593 st
->ifnet_signature
);
2595 err
= ifnet_get_netsignature(ifp
, AF_INET6
,
2596 (u_int8_t
*)&st
->ifnet_siglen
, NULL
,
2597 st
->ifnet_signature
);
2602 ifnet_lock_shared(ifp
);
2603 if (IFNET_IS_CELLULAR(ifp
)) {
2604 st
->ifnet_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_CELLULAR
;
2605 } else if (IFNET_IS_WIFI(ifp
)) {
2606 st
->ifnet_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_WIFI
;
2608 st
->ifnet_type
= NSTAT_IFNET_DESC_LINK_STATUS_TYPE_ETHERNET
;
2610 bcopy(&ifp
->if_lim_stat
, &st
->lim_stat
,
2611 sizeof(st
->lim_stat
));
2613 /* Zero the stats in ifp */
2614 bzero(&ifp
->if_lim_stat
, sizeof(ifp
->if_lim_stat
));
2615 ifnet_lock_done(ifp
);
2616 nstat_sysinfo_send_data(&data
);
2622 nstat_ifnet_copy_descriptor(
2623 nstat_provider_cookie_t cookie
,
2627 nstat_ifnet_descriptor
*desc
= (nstat_ifnet_descriptor
*)data
;
2628 struct nstat_ifnet_cookie
*ifcookie
=
2629 (struct nstat_ifnet_cookie
*)cookie
;
2630 struct ifnet
*ifp
= ifcookie
->ifp
;
2632 if (len
< sizeof(nstat_ifnet_descriptor
)) {
2636 if (nstat_ifnet_gone(cookie
)) {
2640 bzero(desc
, sizeof(*desc
));
2641 ifnet_lock_shared(ifp
);
2642 strlcpy(desc
->name
, ifp
->if_xname
, sizeof(desc
->name
));
2643 desc
->ifindex
= ifp
->if_index
;
2644 desc
->threshold
= ifp
->if_data_threshold
;
2645 desc
->type
= ifp
->if_type
;
2646 if (ifp
->if_desc
.ifd_len
< sizeof(desc
->description
)) {
2647 memcpy(desc
->description
, ifp
->if_desc
.ifd_desc
,
2648 sizeof(desc
->description
));
2650 nstat_ifnet_copy_link_status(ifp
, desc
);
2651 ifnet_lock_done(ifp
);
2656 nstat_init_ifnet_provider(void)
2658 bzero(&nstat_ifnet_provider
, sizeof(nstat_ifnet_provider
));
2659 nstat_ifnet_provider
.nstat_provider_id
= NSTAT_PROVIDER_IFNET
;
2660 nstat_ifnet_provider
.nstat_descriptor_length
= sizeof(nstat_ifnet_descriptor
);
2661 nstat_ifnet_provider
.nstat_lookup
= nstat_ifnet_lookup
;
2662 nstat_ifnet_provider
.nstat_gone
= nstat_ifnet_gone
;
2663 nstat_ifnet_provider
.nstat_counts
= nstat_ifnet_counts
;
2664 nstat_ifnet_provider
.nstat_watcher_add
= NULL
;
2665 nstat_ifnet_provider
.nstat_watcher_remove
= NULL
;
2666 nstat_ifnet_provider
.nstat_copy_descriptor
= nstat_ifnet_copy_descriptor
;
2667 nstat_ifnet_provider
.nstat_release
= nstat_ifnet_release
;
2668 nstat_ifnet_provider
.next
= nstat_providers
;
2669 nstat_providers
= &nstat_ifnet_provider
;
2672 __private_extern__
void
2673 nstat_ifnet_threshold_reached(unsigned int ifindex
)
2675 nstat_control_state
*state
;
2678 struct nstat_ifnet_cookie
*ifcookie
;
2680 lck_mtx_lock(&nstat_mtx
);
2681 for (state
= nstat_controls
; state
; state
= state
->ncs_next
) {
2682 lck_mtx_lock(&state
->ncs_mtx
);
2683 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
2685 if (src
->provider
!= &nstat_ifnet_provider
) {
2688 ifcookie
= (struct nstat_ifnet_cookie
*)src
->cookie
;
2689 ifp
= ifcookie
->ifp
;
2690 if (ifp
->if_index
!= ifindex
) {
2693 nstat_control_send_counts(state
, src
, 0, 0, NULL
);
2695 lck_mtx_unlock(&state
->ncs_mtx
);
2697 lck_mtx_unlock(&nstat_mtx
);
2700 #pragma mark -- Sysinfo --
2702 nstat_set_keyval_scalar(nstat_sysinfo_keyval
*kv
, int key
, u_int32_t val
)
2704 kv
->nstat_sysinfo_key
= key
;
2705 kv
->nstat_sysinfo_flags
= NSTAT_SYSINFO_FLAG_SCALAR
;
2706 kv
->u
.nstat_sysinfo_scalar
= val
;
2707 kv
->nstat_sysinfo_valsize
= sizeof(kv
->u
.nstat_sysinfo_scalar
);
2711 nstat_set_keyval_string(nstat_sysinfo_keyval
*kv
, int key
, u_int8_t
*buf
,
2714 kv
->nstat_sysinfo_key
= key
;
2715 kv
->nstat_sysinfo_flags
= NSTAT_SYSINFO_FLAG_STRING
;
2716 kv
->nstat_sysinfo_valsize
= min(len
,
2717 NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE
);
2718 bcopy(buf
, kv
->u
.nstat_sysinfo_string
, kv
->nstat_sysinfo_valsize
);
2722 nstat_sysinfo_send_data_internal(
2723 nstat_control_state
*control
,
2724 nstat_sysinfo_data
*data
)
2726 nstat_msg_sysinfo_counts
*syscnt
= NULL
;
2727 size_t allocsize
= 0, countsize
= 0, nkeyvals
= 0, finalsize
= 0;
2728 nstat_sysinfo_keyval
*kv
;
2732 allocsize
= offsetof(nstat_msg_sysinfo_counts
, counts
);
2733 countsize
= offsetof(nstat_sysinfo_counts
, nstat_sysinfo_keyvals
);
2734 finalsize
= allocsize
;
2736 /* get number of key-vals for each kind of stat */
2737 switch (data
->flags
) {
2738 case NSTAT_SYSINFO_MBUF_STATS
:
2739 nkeyvals
= sizeof(struct nstat_sysinfo_mbuf_stats
) /
2742 case NSTAT_SYSINFO_TCP_STATS
:
2743 nkeyvals
= NSTAT_SYSINFO_TCP_STATS_COUNT
;
2745 case NSTAT_SYSINFO_IFNET_ECN_STATS
:
2746 nkeyvals
= (sizeof(struct if_tcp_ecn_stat
) /
2749 /* Two more keys for ifnet type and proto */
2752 /* One key for unsent data. */
2755 case NSTAT_SYSINFO_LIM_STATS
:
2756 nkeyvals
= NSTAT_LIM_STAT_KEYVAL_COUNT
;
2758 case NSTAT_SYSINFO_NET_API_STATS
:
2759 nkeyvals
= NSTAT_NET_API_STAT_KEYVAL_COUNT
;
2764 countsize
+= sizeof(nstat_sysinfo_keyval
) * nkeyvals
;
2765 allocsize
+= countsize
;
2767 syscnt
= OSMalloc(allocsize
, nstat_malloc_tag
);
2768 if (syscnt
== NULL
) {
2771 bzero(syscnt
, allocsize
);
2773 kv
= (nstat_sysinfo_keyval
*) &syscnt
->counts
.nstat_sysinfo_keyvals
;
2774 switch (data
->flags
) {
2775 case NSTAT_SYSINFO_MBUF_STATS
:
2777 nstat_set_keyval_scalar(&kv
[i
++],
2778 NSTAT_SYSINFO_KEY_MBUF_256B_TOTAL
,
2779 data
->u
.mb_stats
.total_256b
);
2780 nstat_set_keyval_scalar(&kv
[i
++],
2781 NSTAT_SYSINFO_KEY_MBUF_2KB_TOTAL
,
2782 data
->u
.mb_stats
.total_2kb
);
2783 nstat_set_keyval_scalar(&kv
[i
++],
2784 NSTAT_SYSINFO_KEY_MBUF_4KB_TOTAL
,
2785 data
->u
.mb_stats
.total_4kb
);
2786 nstat_set_keyval_scalar(&kv
[i
++],
2787 NSTAT_SYSINFO_MBUF_16KB_TOTAL
,
2788 data
->u
.mb_stats
.total_16kb
);
2789 nstat_set_keyval_scalar(&kv
[i
++],
2790 NSTAT_SYSINFO_KEY_SOCK_MBCNT
,
2791 data
->u
.mb_stats
.sbmb_total
);
2792 nstat_set_keyval_scalar(&kv
[i
++],
2793 NSTAT_SYSINFO_KEY_SOCK_ATMBLIMIT
,
2794 data
->u
.mb_stats
.sb_atmbuflimit
);
2795 nstat_set_keyval_scalar(&kv
[i
++],
2796 NSTAT_SYSINFO_MBUF_DRAIN_CNT
,
2797 data
->u
.mb_stats
.draincnt
);
2798 nstat_set_keyval_scalar(&kv
[i
++],
2799 NSTAT_SYSINFO_MBUF_MEM_RELEASED
,
2800 data
->u
.mb_stats
.memreleased
);
2801 nstat_set_keyval_scalar(&kv
[i
++],
2802 NSTAT_SYSINFO_KEY_SOCK_MBFLOOR
,
2803 data
->u
.mb_stats
.sbmb_floor
);
2804 VERIFY(i
== nkeyvals
);
2807 case NSTAT_SYSINFO_TCP_STATS
:
2809 nstat_set_keyval_scalar(&kv
[i
++],
2810 NSTAT_SYSINFO_KEY_IPV4_AVGRTT
,
2811 data
->u
.tcp_stats
.ipv4_avgrtt
);
2812 nstat_set_keyval_scalar(&kv
[i
++],
2813 NSTAT_SYSINFO_KEY_IPV6_AVGRTT
,
2814 data
->u
.tcp_stats
.ipv6_avgrtt
);
2815 nstat_set_keyval_scalar(&kv
[i
++],
2816 NSTAT_SYSINFO_KEY_SEND_PLR
,
2817 data
->u
.tcp_stats
.send_plr
);
2818 nstat_set_keyval_scalar(&kv
[i
++],
2819 NSTAT_SYSINFO_KEY_RECV_PLR
,
2820 data
->u
.tcp_stats
.recv_plr
);
2821 nstat_set_keyval_scalar(&kv
[i
++],
2822 NSTAT_SYSINFO_KEY_SEND_TLRTO
,
2823 data
->u
.tcp_stats
.send_tlrto_rate
);
2824 nstat_set_keyval_scalar(&kv
[i
++],
2825 NSTAT_SYSINFO_KEY_SEND_REORDERRATE
,
2826 data
->u
.tcp_stats
.send_reorder_rate
);
2827 nstat_set_keyval_scalar(&kv
[i
++],
2828 NSTAT_SYSINFO_CONNECTION_ATTEMPTS
,
2829 data
->u
.tcp_stats
.connection_attempts
);
2830 nstat_set_keyval_scalar(&kv
[i
++],
2831 NSTAT_SYSINFO_CONNECTION_ACCEPTS
,
2832 data
->u
.tcp_stats
.connection_accepts
);
2833 nstat_set_keyval_scalar(&kv
[i
++],
2834 NSTAT_SYSINFO_ECN_CLIENT_ENABLED
,
2835 data
->u
.tcp_stats
.ecn_client_enabled
);
2836 nstat_set_keyval_scalar(&kv
[i
++],
2837 NSTAT_SYSINFO_ECN_SERVER_ENABLED
,
2838 data
->u
.tcp_stats
.ecn_server_enabled
);
2839 nstat_set_keyval_scalar(&kv
[i
++],
2840 NSTAT_SYSINFO_ECN_CLIENT_SETUP
,
2841 data
->u
.tcp_stats
.ecn_client_setup
);
2842 nstat_set_keyval_scalar(&kv
[i
++],
2843 NSTAT_SYSINFO_ECN_SERVER_SETUP
,
2844 data
->u
.tcp_stats
.ecn_server_setup
);
2845 nstat_set_keyval_scalar(&kv
[i
++],
2846 NSTAT_SYSINFO_ECN_CLIENT_SUCCESS
,
2847 data
->u
.tcp_stats
.ecn_client_success
);
2848 nstat_set_keyval_scalar(&kv
[i
++],
2849 NSTAT_SYSINFO_ECN_SERVER_SUCCESS
,
2850 data
->u
.tcp_stats
.ecn_server_success
);
2851 nstat_set_keyval_scalar(&kv
[i
++],
2852 NSTAT_SYSINFO_ECN_NOT_SUPPORTED
,
2853 data
->u
.tcp_stats
.ecn_not_supported
);
2854 nstat_set_keyval_scalar(&kv
[i
++],
2855 NSTAT_SYSINFO_ECN_LOST_SYN
,
2856 data
->u
.tcp_stats
.ecn_lost_syn
);
2857 nstat_set_keyval_scalar(&kv
[i
++],
2858 NSTAT_SYSINFO_ECN_LOST_SYNACK
,
2859 data
->u
.tcp_stats
.ecn_lost_synack
);
2860 nstat_set_keyval_scalar(&kv
[i
++],
2861 NSTAT_SYSINFO_ECN_RECV_CE
,
2862 data
->u
.tcp_stats
.ecn_recv_ce
);
2863 nstat_set_keyval_scalar(&kv
[i
++],
2864 NSTAT_SYSINFO_ECN_RECV_ECE
,
2865 data
->u
.tcp_stats
.ecn_recv_ece
);
2866 nstat_set_keyval_scalar(&kv
[i
++],
2867 NSTAT_SYSINFO_ECN_SENT_ECE
,
2868 data
->u
.tcp_stats
.ecn_sent_ece
);
2869 nstat_set_keyval_scalar(&kv
[i
++],
2870 NSTAT_SYSINFO_ECN_CONN_RECV_CE
,
2871 data
->u
.tcp_stats
.ecn_conn_recv_ce
);
2872 nstat_set_keyval_scalar(&kv
[i
++],
2873 NSTAT_SYSINFO_ECN_CONN_RECV_ECE
,
2874 data
->u
.tcp_stats
.ecn_conn_recv_ece
);
2875 nstat_set_keyval_scalar(&kv
[i
++],
2876 NSTAT_SYSINFO_ECN_CONN_PLNOCE
,
2877 data
->u
.tcp_stats
.ecn_conn_plnoce
);
2878 nstat_set_keyval_scalar(&kv
[i
++],
2879 NSTAT_SYSINFO_ECN_CONN_PL_CE
,
2880 data
->u
.tcp_stats
.ecn_conn_pl_ce
);
2881 nstat_set_keyval_scalar(&kv
[i
++],
2882 NSTAT_SYSINFO_ECN_CONN_NOPL_CE
,
2883 data
->u
.tcp_stats
.ecn_conn_nopl_ce
);
2884 nstat_set_keyval_scalar(&kv
[i
++],
2885 NSTAT_SYSINFO_ECN_FALLBACK_SYNLOSS
,
2886 data
->u
.tcp_stats
.ecn_fallback_synloss
);
2887 nstat_set_keyval_scalar(&kv
[i
++],
2888 NSTAT_SYSINFO_ECN_FALLBACK_REORDER
,
2889 data
->u
.tcp_stats
.ecn_fallback_reorder
);
2890 nstat_set_keyval_scalar(&kv
[i
++],
2891 NSTAT_SYSINFO_ECN_FALLBACK_CE
,
2892 data
->u
.tcp_stats
.ecn_fallback_ce
);
2893 nstat_set_keyval_scalar(&kv
[i
++],
2894 NSTAT_SYSINFO_TFO_SYN_DATA_RCV
,
2895 data
->u
.tcp_stats
.tfo_syn_data_rcv
);
2896 nstat_set_keyval_scalar(&kv
[i
++],
2897 NSTAT_SYSINFO_TFO_COOKIE_REQ_RCV
,
2898 data
->u
.tcp_stats
.tfo_cookie_req_rcv
);
2899 nstat_set_keyval_scalar(&kv
[i
++],
2900 NSTAT_SYSINFO_TFO_COOKIE_SENT
,
2901 data
->u
.tcp_stats
.tfo_cookie_sent
);
2902 nstat_set_keyval_scalar(&kv
[i
++],
2903 NSTAT_SYSINFO_TFO_COOKIE_INVALID
,
2904 data
->u
.tcp_stats
.tfo_cookie_invalid
);
2905 nstat_set_keyval_scalar(&kv
[i
++],
2906 NSTAT_SYSINFO_TFO_COOKIE_REQ
,
2907 data
->u
.tcp_stats
.tfo_cookie_req
);
2908 nstat_set_keyval_scalar(&kv
[i
++],
2909 NSTAT_SYSINFO_TFO_COOKIE_RCV
,
2910 data
->u
.tcp_stats
.tfo_cookie_rcv
);
2911 nstat_set_keyval_scalar(&kv
[i
++],
2912 NSTAT_SYSINFO_TFO_SYN_DATA_SENT
,
2913 data
->u
.tcp_stats
.tfo_syn_data_sent
);
2914 nstat_set_keyval_scalar(&kv
[i
++],
2915 NSTAT_SYSINFO_TFO_SYN_DATA_ACKED
,
2916 data
->u
.tcp_stats
.tfo_syn_data_acked
);
2917 nstat_set_keyval_scalar(&kv
[i
++],
2918 NSTAT_SYSINFO_TFO_SYN_LOSS
,
2919 data
->u
.tcp_stats
.tfo_syn_loss
);
2920 nstat_set_keyval_scalar(&kv
[i
++],
2921 NSTAT_SYSINFO_TFO_BLACKHOLE
,
2922 data
->u
.tcp_stats
.tfo_blackhole
);
2923 nstat_set_keyval_scalar(&kv
[i
++],
2924 NSTAT_SYSINFO_TFO_COOKIE_WRONG
,
2925 data
->u
.tcp_stats
.tfo_cookie_wrong
);
2926 nstat_set_keyval_scalar(&kv
[i
++],
2927 NSTAT_SYSINFO_TFO_NO_COOKIE_RCV
,
2928 data
->u
.tcp_stats
.tfo_no_cookie_rcv
);
2929 nstat_set_keyval_scalar(&kv
[i
++],
2930 NSTAT_SYSINFO_TFO_HEURISTICS_DISABLE
,
2931 data
->u
.tcp_stats
.tfo_heuristics_disable
);
2932 nstat_set_keyval_scalar(&kv
[i
++],
2933 NSTAT_SYSINFO_TFO_SEND_BLACKHOLE
,
2934 data
->u
.tcp_stats
.tfo_sndblackhole
);
2935 nstat_set_keyval_scalar(&kv
[i
++],
2936 NSTAT_SYSINFO_MPTCP_HANDOVER_ATTEMPT
,
2937 data
->u
.tcp_stats
.mptcp_handover_attempt
);
2938 nstat_set_keyval_scalar(&kv
[i
++],
2939 NSTAT_SYSINFO_MPTCP_INTERACTIVE_ATTEMPT
,
2940 data
->u
.tcp_stats
.mptcp_interactive_attempt
);
2941 nstat_set_keyval_scalar(&kv
[i
++],
2942 NSTAT_SYSINFO_MPTCP_AGGREGATE_ATTEMPT
,
2943 data
->u
.tcp_stats
.mptcp_aggregate_attempt
);
2944 nstat_set_keyval_scalar(&kv
[i
++],
2945 NSTAT_SYSINFO_MPTCP_FP_HANDOVER_ATTEMPT
,
2946 data
->u
.tcp_stats
.mptcp_fp_handover_attempt
);
2947 nstat_set_keyval_scalar(&kv
[i
++],
2948 NSTAT_SYSINFO_MPTCP_FP_INTERACTIVE_ATTEMPT
,
2949 data
->u
.tcp_stats
.mptcp_fp_interactive_attempt
);
2950 nstat_set_keyval_scalar(&kv
[i
++],
2951 NSTAT_SYSINFO_MPTCP_FP_AGGREGATE_ATTEMPT
,
2952 data
->u
.tcp_stats
.mptcp_fp_aggregate_attempt
);
2953 nstat_set_keyval_scalar(&kv
[i
++],
2954 NSTAT_SYSINFO_MPTCP_HEURISTIC_FALLBACK
,
2955 data
->u
.tcp_stats
.mptcp_heuristic_fallback
);
2956 nstat_set_keyval_scalar(&kv
[i
++],
2957 NSTAT_SYSINFO_MPTCP_FP_HEURISTIC_FALLBACK
,
2958 data
->u
.tcp_stats
.mptcp_fp_heuristic_fallback
);
2959 nstat_set_keyval_scalar(&kv
[i
++],
2960 NSTAT_SYSINFO_MPTCP_HANDOVER_SUCCESS_WIFI
,
2961 data
->u
.tcp_stats
.mptcp_handover_success_wifi
);
2962 nstat_set_keyval_scalar(&kv
[i
++],
2963 NSTAT_SYSINFO_MPTCP_HANDOVER_SUCCESS_CELL
,
2964 data
->u
.tcp_stats
.mptcp_handover_success_cell
);
2965 nstat_set_keyval_scalar(&kv
[i
++],
2966 NSTAT_SYSINFO_MPTCP_INTERACTIVE_SUCCESS
,
2967 data
->u
.tcp_stats
.mptcp_interactive_success
);
2968 nstat_set_keyval_scalar(&kv
[i
++],
2969 NSTAT_SYSINFO_MPTCP_AGGREGATE_SUCCESS
,
2970 data
->u
.tcp_stats
.mptcp_aggregate_success
);
2971 nstat_set_keyval_scalar(&kv
[i
++],
2972 NSTAT_SYSINFO_MPTCP_FP_HANDOVER_SUCCESS_WIFI
,
2973 data
->u
.tcp_stats
.mptcp_fp_handover_success_wifi
);
2974 nstat_set_keyval_scalar(&kv
[i
++],
2975 NSTAT_SYSINFO_MPTCP_FP_HANDOVER_SUCCESS_CELL
,
2976 data
->u
.tcp_stats
.mptcp_fp_handover_success_cell
);
2977 nstat_set_keyval_scalar(&kv
[i
++],
2978 NSTAT_SYSINFO_MPTCP_FP_INTERACTIVE_SUCCESS
,
2979 data
->u
.tcp_stats
.mptcp_fp_interactive_success
);
2980 nstat_set_keyval_scalar(&kv
[i
++],
2981 NSTAT_SYSINFO_MPTCP_FP_AGGREGATE_SUCCESS
,
2982 data
->u
.tcp_stats
.mptcp_fp_aggregate_success
);
2983 nstat_set_keyval_scalar(&kv
[i
++],
2984 NSTAT_SYSINFO_MPTCP_HANDOVER_CELL_FROM_WIFI
,
2985 data
->u
.tcp_stats
.mptcp_handover_cell_from_wifi
);
2986 nstat_set_keyval_scalar(&kv
[i
++],
2987 NSTAT_SYSINFO_MPTCP_HANDOVER_WIFI_FROM_CELL
,
2988 data
->u
.tcp_stats
.mptcp_handover_wifi_from_cell
);
2989 nstat_set_keyval_scalar(&kv
[i
++],
2990 NSTAT_SYSINFO_MPTCP_INTERACTIVE_CELL_FROM_WIFI
,
2991 data
->u
.tcp_stats
.mptcp_interactive_cell_from_wifi
);
2992 nstat_set_keyval_scalar(&kv
[i
++],
2993 NSTAT_SYSINFO_MPTCP_HANDOVER_CELL_BYTES
,
2994 data
->u
.tcp_stats
.mptcp_handover_cell_bytes
);
2995 nstat_set_keyval_scalar(&kv
[i
++],
2996 NSTAT_SYSINFO_MPTCP_INTERACTIVE_CELL_BYTES
,
2997 data
->u
.tcp_stats
.mptcp_interactive_cell_bytes
);
2998 nstat_set_keyval_scalar(&kv
[i
++],
2999 NSTAT_SYSINFO_MPTCP_AGGREGATE_CELL_BYTES
,
3000 data
->u
.tcp_stats
.mptcp_aggregate_cell_bytes
);
3001 nstat_set_keyval_scalar(&kv
[i
++],
3002 NSTAT_SYSINFO_MPTCP_HANDOVER_ALL_BYTES
,
3003 data
->u
.tcp_stats
.mptcp_handover_all_bytes
);
3004 nstat_set_keyval_scalar(&kv
[i
++],
3005 NSTAT_SYSINFO_MPTCP_INTERACTIVE_ALL_BYTES
,
3006 data
->u
.tcp_stats
.mptcp_interactive_all_bytes
);
3007 nstat_set_keyval_scalar(&kv
[i
++],
3008 NSTAT_SYSINFO_MPTCP_AGGREGATE_ALL_BYTES
,
3009 data
->u
.tcp_stats
.mptcp_aggregate_all_bytes
);
3010 nstat_set_keyval_scalar(&kv
[i
++],
3011 NSTAT_SYSINFO_MPTCP_BACK_TO_WIFI
,
3012 data
->u
.tcp_stats
.mptcp_back_to_wifi
);
3013 nstat_set_keyval_scalar(&kv
[i
++],
3014 NSTAT_SYSINFO_MPTCP_WIFI_PROXY
,
3015 data
->u
.tcp_stats
.mptcp_wifi_proxy
);
3016 nstat_set_keyval_scalar(&kv
[i
++],
3017 NSTAT_SYSINFO_MPTCP_CELL_PROXY
,
3018 data
->u
.tcp_stats
.mptcp_cell_proxy
);
3019 nstat_set_keyval_scalar(&kv
[i
++],
3020 NSTAT_SYSINFO_MPTCP_TRIGGERED_CELL
,
3021 data
->u
.tcp_stats
.mptcp_triggered_cell
);
3022 VERIFY(i
== nkeyvals
);
3025 case NSTAT_SYSINFO_IFNET_ECN_STATS
:
3027 nstat_set_keyval_scalar(&kv
[i
++],
3028 NSTAT_SYSINFO_ECN_IFNET_TYPE
,
3029 data
->u
.ifnet_ecn_stats
.ifnet_type
);
3030 nstat_set_keyval_scalar(&kv
[i
++],
3031 NSTAT_SYSINFO_ECN_IFNET_PROTO
,
3032 data
->u
.ifnet_ecn_stats
.ifnet_proto
);
3033 nstat_set_keyval_scalar(&kv
[i
++],
3034 NSTAT_SYSINFO_ECN_IFNET_CLIENT_SETUP
,
3035 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_client_setup
);
3036 nstat_set_keyval_scalar(&kv
[i
++],
3037 NSTAT_SYSINFO_ECN_IFNET_SERVER_SETUP
,
3038 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_server_setup
);
3039 nstat_set_keyval_scalar(&kv
[i
++],
3040 NSTAT_SYSINFO_ECN_IFNET_CLIENT_SUCCESS
,
3041 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_client_success
);
3042 nstat_set_keyval_scalar(&kv
[i
++],
3043 NSTAT_SYSINFO_ECN_IFNET_SERVER_SUCCESS
,
3044 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_server_success
);
3045 nstat_set_keyval_scalar(&kv
[i
++],
3046 NSTAT_SYSINFO_ECN_IFNET_PEER_NOSUPPORT
,
3047 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_peer_nosupport
);
3048 nstat_set_keyval_scalar(&kv
[i
++],
3049 NSTAT_SYSINFO_ECN_IFNET_SYN_LOST
,
3050 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_syn_lost
);
3051 nstat_set_keyval_scalar(&kv
[i
++],
3052 NSTAT_SYSINFO_ECN_IFNET_SYNACK_LOST
,
3053 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_synack_lost
);
3054 nstat_set_keyval_scalar(&kv
[i
++],
3055 NSTAT_SYSINFO_ECN_IFNET_RECV_CE
,
3056 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_recv_ce
);
3057 nstat_set_keyval_scalar(&kv
[i
++],
3058 NSTAT_SYSINFO_ECN_IFNET_RECV_ECE
,
3059 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_recv_ece
);
3060 nstat_set_keyval_scalar(&kv
[i
++],
3061 NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_CE
,
3062 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_conn_recv_ce
);
3063 nstat_set_keyval_scalar(&kv
[i
++],
3064 NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_ECE
,
3065 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_conn_recv_ece
);
3066 nstat_set_keyval_scalar(&kv
[i
++],
3067 NSTAT_SYSINFO_ECN_IFNET_CONN_PLNOCE
,
3068 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_conn_plnoce
);
3069 nstat_set_keyval_scalar(&kv
[i
++],
3070 NSTAT_SYSINFO_ECN_IFNET_CONN_PLCE
,
3071 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_conn_plce
);
3072 nstat_set_keyval_scalar(&kv
[i
++],
3073 NSTAT_SYSINFO_ECN_IFNET_CONN_NOPLCE
,
3074 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_conn_noplce
);
3075 nstat_set_keyval_scalar(&kv
[i
++],
3076 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_SYNLOSS
,
3077 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_synloss
);
3078 nstat_set_keyval_scalar(&kv
[i
++],
3079 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_REORDER
,
3080 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_reorder
);
3081 nstat_set_keyval_scalar(&kv
[i
++],
3082 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_CE
,
3083 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_ce
);
3084 nstat_set_keyval_scalar(&kv
[i
++],
3085 NSTAT_SYSINFO_ECN_IFNET_ON_RTT_AVG
,
3086 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.rtt_avg
);
3087 nstat_set_keyval_scalar(&kv
[i
++],
3088 NSTAT_SYSINFO_ECN_IFNET_ON_RTT_VAR
,
3089 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.rtt_var
);
3090 nstat_set_keyval_scalar(&kv
[i
++],
3091 NSTAT_SYSINFO_ECN_IFNET_ON_OOPERCENT
,
3092 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.oo_percent
);
3093 nstat_set_keyval_scalar(&kv
[i
++],
3094 NSTAT_SYSINFO_ECN_IFNET_ON_SACK_EPISODE
,
3095 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.sack_episodes
);
3096 nstat_set_keyval_scalar(&kv
[i
++],
3097 NSTAT_SYSINFO_ECN_IFNET_ON_REORDER_PERCENT
,
3098 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.reorder_percent
);
3099 nstat_set_keyval_scalar(&kv
[i
++],
3100 NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_PERCENT
,
3101 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.rxmit_percent
);
3102 nstat_set_keyval_scalar(&kv
[i
++],
3103 NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_DROP
,
3104 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.rxmit_drop
);
3105 nstat_set_keyval_scalar(&kv
[i
++],
3106 NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_AVG
,
3107 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.rtt_avg
);
3108 nstat_set_keyval_scalar(&kv
[i
++],
3109 NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_VAR
,
3110 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.rtt_var
);
3111 nstat_set_keyval_scalar(&kv
[i
++],
3112 NSTAT_SYSINFO_ECN_IFNET_OFF_OOPERCENT
,
3113 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.oo_percent
);
3114 nstat_set_keyval_scalar(&kv
[i
++],
3115 NSTAT_SYSINFO_ECN_IFNET_OFF_SACK_EPISODE
,
3116 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.sack_episodes
);
3117 nstat_set_keyval_scalar(&kv
[i
++],
3118 NSTAT_SYSINFO_ECN_IFNET_OFF_REORDER_PERCENT
,
3119 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.reorder_percent
);
3120 nstat_set_keyval_scalar(&kv
[i
++],
3121 NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_PERCENT
,
3122 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.rxmit_percent
);
3123 nstat_set_keyval_scalar(&kv
[i
++],
3124 NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_DROP
,
3125 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.rxmit_drop
);
3126 nstat_set_keyval_scalar(&kv
[i
++],
3127 NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_TXPKTS
,
3128 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.total_txpkts
);
3129 nstat_set_keyval_scalar(&kv
[i
++],
3130 NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXMTPKTS
,
3131 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.total_rxmitpkts
);
3132 nstat_set_keyval_scalar(&kv
[i
++],
3133 NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXPKTS
,
3134 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.total_rxpkts
);
3135 nstat_set_keyval_scalar(&kv
[i
++],
3136 NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_OOPKTS
,
3137 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.total_oopkts
);
3138 nstat_set_keyval_scalar(&kv
[i
++],
3139 NSTAT_SYSINFO_ECN_IFNET_ON_DROP_RST
,
3140 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_on
.rst_drop
);
3141 nstat_set_keyval_scalar(&kv
[i
++],
3142 NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_TXPKTS
,
3143 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.total_txpkts
);
3144 nstat_set_keyval_scalar(&kv
[i
++],
3145 NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXMTPKTS
,
3146 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.total_rxmitpkts
);
3147 nstat_set_keyval_scalar(&kv
[i
++],
3148 NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXPKTS
,
3149 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.total_rxpkts
);
3150 nstat_set_keyval_scalar(&kv
[i
++],
3151 NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_OOPKTS
,
3152 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.total_oopkts
);
3153 nstat_set_keyval_scalar(&kv
[i
++],
3154 NSTAT_SYSINFO_ECN_IFNET_OFF_DROP_RST
,
3155 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_off
.rst_drop
);
3156 nstat_set_keyval_scalar(&kv
[i
++],
3157 NSTAT_SYSINFO_ECN_IFNET_TOTAL_CONN
,
3158 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_total_conn
);
3159 nstat_set_keyval_scalar(&kv
[i
++],
3160 NSTAT_SYSINFO_IFNET_UNSENT_DATA
,
3161 data
->unsent_data_cnt
);
3162 nstat_set_keyval_scalar(&kv
[i
++],
3163 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRST
,
3164 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_droprst
);
3165 nstat_set_keyval_scalar(&kv
[i
++],
3166 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRXMT
,
3167 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_droprxmt
);
3168 nstat_set_keyval_scalar(&kv
[i
++],
3169 NSTAT_SYSINFO_ECN_IFNET_FALLBACK_SYNRST
,
3170 data
->u
.ifnet_ecn_stats
.ecn_stat
.ecn_fallback_synrst
);
3173 case NSTAT_SYSINFO_LIM_STATS
:
3175 nstat_set_keyval_string(&kv
[i
++],
3176 NSTAT_SYSINFO_LIM_IFNET_SIGNATURE
,
3177 data
->u
.lim_stats
.ifnet_signature
,
3178 data
->u
.lim_stats
.ifnet_siglen
);
3179 nstat_set_keyval_scalar(&kv
[i
++],
3180 NSTAT_SYSINFO_LIM_IFNET_DL_MAX_BANDWIDTH
,
3181 data
->u
.lim_stats
.lim_stat
.lim_dl_max_bandwidth
);
3182 nstat_set_keyval_scalar(&kv
[i
++],
3183 NSTAT_SYSINFO_LIM_IFNET_UL_MAX_BANDWIDTH
,
3184 data
->u
.lim_stats
.lim_stat
.lim_ul_max_bandwidth
);
3185 nstat_set_keyval_scalar(&kv
[i
++],
3186 NSTAT_SYSINFO_LIM_IFNET_PACKET_LOSS_PERCENT
,
3187 data
->u
.lim_stats
.lim_stat
.lim_packet_loss_percent
);
3188 nstat_set_keyval_scalar(&kv
[i
++],
3189 NSTAT_SYSINFO_LIM_IFNET_PACKET_OOO_PERCENT
,
3190 data
->u
.lim_stats
.lim_stat
.lim_packet_ooo_percent
);
3191 nstat_set_keyval_scalar(&kv
[i
++],
3192 NSTAT_SYSINFO_LIM_IFNET_RTT_VARIANCE
,
3193 data
->u
.lim_stats
.lim_stat
.lim_rtt_variance
);
3194 nstat_set_keyval_scalar(&kv
[i
++],
3195 NSTAT_SYSINFO_LIM_IFNET_RTT_MIN
,
3196 data
->u
.lim_stats
.lim_stat
.lim_rtt_min
);
3197 nstat_set_keyval_scalar(&kv
[i
++],
3198 NSTAT_SYSINFO_LIM_IFNET_RTT_AVG
,
3199 data
->u
.lim_stats
.lim_stat
.lim_rtt_average
);
3200 nstat_set_keyval_scalar(&kv
[i
++],
3201 NSTAT_SYSINFO_LIM_IFNET_CONN_TIMEOUT_PERCENT
,
3202 data
->u
.lim_stats
.lim_stat
.lim_conn_timeout_percent
);
3203 nstat_set_keyval_scalar(&kv
[i
++],
3204 NSTAT_SYSINFO_LIM_IFNET_DL_DETECTED
,
3205 data
->u
.lim_stats
.lim_stat
.lim_dl_detected
);
3206 nstat_set_keyval_scalar(&kv
[i
++],
3207 NSTAT_SYSINFO_LIM_IFNET_UL_DETECTED
,
3208 data
->u
.lim_stats
.lim_stat
.lim_ul_detected
);
3209 nstat_set_keyval_scalar(&kv
[i
++],
3210 NSTAT_SYSINFO_LIM_IFNET_TYPE
,
3211 data
->u
.lim_stats
.ifnet_type
);
3214 case NSTAT_SYSINFO_NET_API_STATS
:
3216 nstat_set_keyval_scalar(&kv
[i
++],
3217 NSTAT_SYSINFO_API_IF_FLTR_ATTACH
,
3218 data
->u
.net_api_stats
.net_api_stats
.nas_iflt_attach_total
);
3219 nstat_set_keyval_scalar(&kv
[i
++],
3220 NSTAT_SYSINFO_API_IF_FLTR_ATTACH_OS
,
3221 data
->u
.net_api_stats
.net_api_stats
.nas_iflt_attach_os_total
);
3222 nstat_set_keyval_scalar(&kv
[i
++],
3223 NSTAT_SYSINFO_API_IP_FLTR_ADD
,
3224 data
->u
.net_api_stats
.net_api_stats
.nas_ipf_add_total
);
3225 nstat_set_keyval_scalar(&kv
[i
++],
3226 NSTAT_SYSINFO_API_IP_FLTR_ADD_OS
,
3227 data
->u
.net_api_stats
.net_api_stats
.nas_ipf_add_os_total
);
3228 nstat_set_keyval_scalar(&kv
[i
++],
3229 NSTAT_SYSINFO_API_SOCK_FLTR_ATTACH
,
3230 data
->u
.net_api_stats
.net_api_stats
.nas_sfltr_register_total
);
3231 nstat_set_keyval_scalar(&kv
[i
++],
3232 NSTAT_SYSINFO_API_SOCK_FLTR_ATTACH_OS
,
3233 data
->u
.net_api_stats
.net_api_stats
.nas_sfltr_register_os_total
);
3236 nstat_set_keyval_scalar(&kv
[i
++],
3237 NSTAT_SYSINFO_API_SOCK_ALLOC_TOTAL
,
3238 data
->u
.net_api_stats
.net_api_stats
.nas_socket_alloc_total
);
3239 nstat_set_keyval_scalar(&kv
[i
++],
3240 NSTAT_SYSINFO_API_SOCK_ALLOC_KERNEL
,
3241 data
->u
.net_api_stats
.net_api_stats
.nas_socket_in_kernel_total
);
3242 nstat_set_keyval_scalar(&kv
[i
++],
3243 NSTAT_SYSINFO_API_SOCK_ALLOC_KERNEL_OS
,
3244 data
->u
.net_api_stats
.net_api_stats
.nas_socket_in_kernel_os_total
);
3245 nstat_set_keyval_scalar(&kv
[i
++],
3246 NSTAT_SYSINFO_API_SOCK_NECP_CLIENTUUID
,
3247 data
->u
.net_api_stats
.net_api_stats
.nas_socket_necp_clientuuid_total
);
3249 nstat_set_keyval_scalar(&kv
[i
++],
3250 NSTAT_SYSINFO_API_SOCK_DOMAIN_LOCAL
,
3251 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_local_total
);
3252 nstat_set_keyval_scalar(&kv
[i
++],
3253 NSTAT_SYSINFO_API_SOCK_DOMAIN_ROUTE
,
3254 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_route_total
);
3255 nstat_set_keyval_scalar(&kv
[i
++],
3256 NSTAT_SYSINFO_API_SOCK_DOMAIN_INET
,
3257 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_inet_total
);
3258 nstat_set_keyval_scalar(&kv
[i
++],
3259 NSTAT_SYSINFO_API_SOCK_DOMAIN_INET6
,
3260 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_inet6_total
);
3261 nstat_set_keyval_scalar(&kv
[i
++],
3262 NSTAT_SYSINFO_API_SOCK_DOMAIN_SYSTEM
,
3263 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_system_total
);
3264 nstat_set_keyval_scalar(&kv
[i
++],
3265 NSTAT_SYSINFO_API_SOCK_DOMAIN_MULTIPATH
,
3266 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_multipath_total
);
3267 nstat_set_keyval_scalar(&kv
[i
++],
3268 NSTAT_SYSINFO_API_SOCK_DOMAIN_KEY
,
3269 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_key_total
);
3270 nstat_set_keyval_scalar(&kv
[i
++],
3271 NSTAT_SYSINFO_API_SOCK_DOMAIN_NDRV
,
3272 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_ndrv_total
);
3273 nstat_set_keyval_scalar(&kv
[i
++],
3274 NSTAT_SYSINFO_API_SOCK_DOMAIN_OTHER
,
3275 data
->u
.net_api_stats
.net_api_stats
.nas_socket_domain_other_total
);
3277 nstat_set_keyval_scalar(&kv
[i
++],
3278 NSTAT_SYSINFO_API_SOCK_INET_STREAM
,
3279 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet_stream_total
);
3280 nstat_set_keyval_scalar(&kv
[i
++],
3281 NSTAT_SYSINFO_API_SOCK_INET_DGRAM
,
3282 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet_dgram_total
);
3283 nstat_set_keyval_scalar(&kv
[i
++],
3284 NSTAT_SYSINFO_API_SOCK_INET_DGRAM_CONNECTED
,
3285 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet_dgram_connected
);
3286 nstat_set_keyval_scalar(&kv
[i
++],
3287 NSTAT_SYSINFO_API_SOCK_INET_DGRAM_DNS
,
3288 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet_dgram_dns
);
3289 nstat_set_keyval_scalar(&kv
[i
++],
3290 NSTAT_SYSINFO_API_SOCK_INET_DGRAM_NO_DATA
,
3291 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet_dgram_no_data
);
3293 nstat_set_keyval_scalar(&kv
[i
++],
3294 NSTAT_SYSINFO_API_SOCK_INET6_STREAM
,
3295 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet6_stream_total
);
3296 nstat_set_keyval_scalar(&kv
[i
++],
3297 NSTAT_SYSINFO_API_SOCK_INET6_DGRAM
,
3298 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet6_dgram_total
);
3299 nstat_set_keyval_scalar(&kv
[i
++],
3300 NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_CONNECTED
,
3301 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet6_dgram_connected
);
3302 nstat_set_keyval_scalar(&kv
[i
++],
3303 NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_DNS
,
3304 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet6_dgram_dns
);
3305 nstat_set_keyval_scalar(&kv
[i
++],
3306 NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_NO_DATA
,
3307 data
->u
.net_api_stats
.net_api_stats
.nas_socket_inet6_dgram_no_data
);
3309 nstat_set_keyval_scalar(&kv
[i
++],
3310 NSTAT_SYSINFO_API_SOCK_INET_MCAST_JOIN
,
3311 data
->u
.net_api_stats
.net_api_stats
.nas_socket_mcast_join_total
);
3312 nstat_set_keyval_scalar(&kv
[i
++],
3313 NSTAT_SYSINFO_API_SOCK_INET_MCAST_JOIN_OS
,
3314 data
->u
.net_api_stats
.net_api_stats
.nas_socket_mcast_join_os_total
);
3316 nstat_set_keyval_scalar(&kv
[i
++],
3317 NSTAT_SYSINFO_API_NEXUS_FLOW_INET_STREAM
,
3318 data
->u
.net_api_stats
.net_api_stats
.nas_nx_flow_inet_stream_total
);
3319 nstat_set_keyval_scalar(&kv
[i
++],
3320 NSTAT_SYSINFO_API_NEXUS_FLOW_INET_DATAGRAM
,
3321 data
->u
.net_api_stats
.net_api_stats
.nas_nx_flow_inet_dgram_total
);
3323 nstat_set_keyval_scalar(&kv
[i
++],
3324 NSTAT_SYSINFO_API_NEXUS_FLOW_INET6_STREAM
,
3325 data
->u
.net_api_stats
.net_api_stats
.nas_nx_flow_inet6_stream_total
);
3326 nstat_set_keyval_scalar(&kv
[i
++],
3327 NSTAT_SYSINFO_API_NEXUS_FLOW_INET6_DATAGRAM
,
3328 data
->u
.net_api_stats
.net_api_stats
.nas_nx_flow_inet6_dgram_total
);
3330 nstat_set_keyval_scalar(&kv
[i
++],
3331 NSTAT_SYSINFO_API_IFNET_ALLOC
,
3332 data
->u
.net_api_stats
.net_api_stats
.nas_ifnet_alloc_total
);
3333 nstat_set_keyval_scalar(&kv
[i
++],
3334 NSTAT_SYSINFO_API_IFNET_ALLOC_OS
,
3335 data
->u
.net_api_stats
.net_api_stats
.nas_ifnet_alloc_os_total
);
3337 nstat_set_keyval_scalar(&kv
[i
++],
3338 NSTAT_SYSINFO_API_PF_ADDRULE
,
3339 data
->u
.net_api_stats
.net_api_stats
.nas_pf_addrule_total
);
3340 nstat_set_keyval_scalar(&kv
[i
++],
3341 NSTAT_SYSINFO_API_PF_ADDRULE_OS
,
3342 data
->u
.net_api_stats
.net_api_stats
.nas_pf_addrule_os
);
3344 nstat_set_keyval_scalar(&kv
[i
++],
3345 NSTAT_SYSINFO_API_VMNET_START
,
3346 data
->u
.net_api_stats
.net_api_stats
.nas_vmnet_total
);
3349 nstat_set_keyval_scalar(&kv
[i
++],
3350 NSTAT_SYSINFO_API_REPORT_INTERVAL
,
3351 data
->u
.net_api_stats
.report_interval
);
3356 if (syscnt
!= NULL
) {
3357 VERIFY(i
> 0 && i
<= nkeyvals
);
3358 countsize
= offsetof(nstat_sysinfo_counts
,
3359 nstat_sysinfo_keyvals
) +
3360 sizeof(nstat_sysinfo_keyval
) * i
;
3361 finalsize
+= countsize
;
3362 syscnt
->hdr
.type
= NSTAT_MSG_TYPE_SYSINFO_COUNTS
;
3363 syscnt
->hdr
.length
= finalsize
;
3364 syscnt
->counts
.nstat_sysinfo_len
= countsize
;
3366 result
= ctl_enqueuedata(control
->ncs_kctl
,
3367 control
->ncs_unit
, syscnt
, finalsize
, CTL_DATA_EOR
);
3369 nstat_stats
.nstat_sysinfofailures
+= 1;
3371 OSFree(syscnt
, allocsize
, nstat_malloc_tag
);
3376 __private_extern__
void
3377 nstat_sysinfo_send_data(
3378 nstat_sysinfo_data
*data
)
3380 nstat_control_state
*control
;
3382 lck_mtx_lock(&nstat_mtx
);
3383 for (control
= nstat_controls
; control
; control
= control
->ncs_next
) {
3384 lck_mtx_lock(&control
->ncs_mtx
);
3385 if ((control
->ncs_flags
& NSTAT_FLAG_SYSINFO_SUBSCRIBED
) != 0) {
3386 nstat_sysinfo_send_data_internal(control
, data
);
3388 lck_mtx_unlock(&control
->ncs_mtx
);
3390 lck_mtx_unlock(&nstat_mtx
);
3394 nstat_sysinfo_generate_report(void)
3396 mbuf_report_peak_usage();
3398 nstat_ifnet_report_ecn_stats();
3399 nstat_ifnet_report_lim_stats();
3400 nstat_net_api_report_stats();
3403 #pragma mark -- net_api --
3405 static struct net_api_stats net_api_stats_before
;
3406 static u_int64_t net_api_stats_last_report_time
;
3409 nstat_net_api_report_stats(void)
3411 struct nstat_sysinfo_data data
;
3412 struct nstat_sysinfo_net_api_stats
*st
= &data
.u
.net_api_stats
;
3415 uptime
= net_uptime();
3417 if ((u_int32_t
)(uptime
- net_api_stats_last_report_time
) <
3418 net_api_stats_report_interval
) {
3422 st
->report_interval
= uptime
- net_api_stats_last_report_time
;
3423 net_api_stats_last_report_time
= uptime
;
3425 data
.flags
= NSTAT_SYSINFO_NET_API_STATS
;
3426 data
.unsent_data_cnt
= 0;
3429 * Some of the fields in the report are the current value and
3430 * other fields are the delta from the last report:
3431 * - Report difference for the per flow counters as they increase
3433 * - Report current value for other counters as they tend not to change
3436 #define STATCOPY(f) \
3437 (st->net_api_stats.f = net_api_stats.f)
3438 #define STATDIFF(f) \
3439 (st->net_api_stats.f = net_api_stats.f - net_api_stats_before.f)
3441 STATCOPY(nas_iflt_attach_count
);
3442 STATCOPY(nas_iflt_attach_total
);
3443 STATCOPY(nas_iflt_attach_os_total
);
3445 STATCOPY(nas_ipf_add_count
);
3446 STATCOPY(nas_ipf_add_total
);
3447 STATCOPY(nas_ipf_add_os_total
);
3449 STATCOPY(nas_sfltr_register_count
);
3450 STATCOPY(nas_sfltr_register_total
);
3451 STATCOPY(nas_sfltr_register_os_total
);
3453 STATDIFF(nas_socket_alloc_total
);
3454 STATDIFF(nas_socket_in_kernel_total
);
3455 STATDIFF(nas_socket_in_kernel_os_total
);
3456 STATDIFF(nas_socket_necp_clientuuid_total
);
3458 STATDIFF(nas_socket_domain_local_total
);
3459 STATDIFF(nas_socket_domain_route_total
);
3460 STATDIFF(nas_socket_domain_inet_total
);
3461 STATDIFF(nas_socket_domain_inet6_total
);
3462 STATDIFF(nas_socket_domain_system_total
);
3463 STATDIFF(nas_socket_domain_multipath_total
);
3464 STATDIFF(nas_socket_domain_key_total
);
3465 STATDIFF(nas_socket_domain_ndrv_total
);
3466 STATDIFF(nas_socket_domain_other_total
);
3468 STATDIFF(nas_socket_inet_stream_total
);
3469 STATDIFF(nas_socket_inet_dgram_total
);
3470 STATDIFF(nas_socket_inet_dgram_connected
);
3471 STATDIFF(nas_socket_inet_dgram_dns
);
3472 STATDIFF(nas_socket_inet_dgram_no_data
);
3474 STATDIFF(nas_socket_inet6_stream_total
);
3475 STATDIFF(nas_socket_inet6_dgram_total
);
3476 STATDIFF(nas_socket_inet6_dgram_connected
);
3477 STATDIFF(nas_socket_inet6_dgram_dns
);
3478 STATDIFF(nas_socket_inet6_dgram_no_data
);
3480 STATDIFF(nas_socket_mcast_join_total
);
3481 STATDIFF(nas_socket_mcast_join_os_total
);
3483 STATDIFF(nas_sock_inet6_stream_exthdr_in
);
3484 STATDIFF(nas_sock_inet6_stream_exthdr_out
);
3485 STATDIFF(nas_sock_inet6_dgram_exthdr_in
);
3486 STATDIFF(nas_sock_inet6_dgram_exthdr_out
);
3488 STATDIFF(nas_nx_flow_inet_stream_total
);
3489 STATDIFF(nas_nx_flow_inet_dgram_total
);
3491 STATDIFF(nas_nx_flow_inet6_stream_total
);
3492 STATDIFF(nas_nx_flow_inet6_dgram_total
);
3494 STATCOPY(nas_ifnet_alloc_count
);
3495 STATCOPY(nas_ifnet_alloc_total
);
3496 STATCOPY(nas_ifnet_alloc_os_count
);
3497 STATCOPY(nas_ifnet_alloc_os_total
);
3499 STATCOPY(nas_pf_addrule_total
);
3500 STATCOPY(nas_pf_addrule_os
);
3502 STATCOPY(nas_vmnet_total
);
3507 nstat_sysinfo_send_data(&data
);
3510 * Save a copy of the current fields so we can diff them the next time
3512 memcpy(&net_api_stats_before
, &net_api_stats
,
3513 sizeof(struct net_api_stats
));
3514 _CASSERT(sizeof(net_api_stats_before
) == sizeof(net_api_stats
));
3518 #pragma mark -- Kernel Control Socket --
3520 static kern_ctl_ref nstat_ctlref
= NULL
;
3521 static lck_grp_t
*nstat_lck_grp
= NULL
;
3523 static errno_t
nstat_control_connect(kern_ctl_ref kctl
, struct sockaddr_ctl
*sac
, void **uinfo
);
3524 static errno_t
nstat_control_disconnect(kern_ctl_ref kctl
, u_int32_t unit
, void *uinfo
);
3525 static errno_t
nstat_control_send(kern_ctl_ref kctl
, u_int32_t unit
, void *uinfo
, mbuf_t m
, int flags
);
3528 nstat_enqueue_success(
3530 nstat_control_state
*state
,
3533 nstat_msg_hdr success
;
3536 bzero(&success
, sizeof(success
));
3537 success
.context
= context
;
3538 success
.type
= NSTAT_MSG_TYPE_SUCCESS
;
3539 success
.length
= sizeof(success
);
3540 success
.flags
= flags
;
3541 result
= ctl_enqueuedata(state
->ncs_kctl
, state
->ncs_unit
, &success
,
3542 sizeof(success
), CTL_DATA_EOR
| CTL_DATA_CRIT
);
3544 if (nstat_debug
!= 0) {
3545 printf("%s: could not enqueue success message %d\n",
3548 nstat_stats
.nstat_successmsgfailures
+= 1;
3554 nstat_control_send_goodbye(
3555 nstat_control_state
*state
,
3561 if (nstat_control_reporting_allowed(state
, src
)) {
3562 if ((state
->ncs_flags
& NSTAT_FLAG_SUPPORTS_UPDATES
) != 0) {
3563 result
= nstat_control_send_update(state
, src
, 0, NSTAT_MSG_HDR_FLAG_CLOSING
, NULL
);
3566 if (nstat_debug
!= 0) {
3567 printf("%s - nstat_control_send_update() %d\n", __func__
, result
);
3571 // send one last counts notification
3572 result
= nstat_control_send_counts(state
, src
, 0, NSTAT_MSG_HDR_FLAG_CLOSING
, NULL
);
3575 if (nstat_debug
!= 0) {
3576 printf("%s - nstat_control_send_counts() %d\n", __func__
, result
);
3580 // send a last description
3581 result
= nstat_control_send_description(state
, src
, 0, NSTAT_MSG_HDR_FLAG_CLOSING
);
3584 if (nstat_debug
!= 0) {
3585 printf("%s - nstat_control_send_description() %d\n", __func__
, result
);
3591 // send the source removed notification
3592 result
= nstat_control_send_removed(state
, src
);
3593 if (result
!= 0 && nstat_debug
) {
3595 if (nstat_debug
!= 0) {
3596 printf("%s - nstat_control_send_removed() %d\n", __func__
, result
);
3601 nstat_stats
.nstat_control_send_goodbye_failures
++;
3609 nstat_flush_accumulated_msgs(
3610 nstat_control_state
*state
)
3613 if (state
->ncs_accumulated
!= NULL
&& mbuf_len(state
->ncs_accumulated
) > 0) {
3614 mbuf_pkthdr_setlen(state
->ncs_accumulated
, mbuf_len(state
->ncs_accumulated
));
3615 result
= ctl_enqueuembuf(state
->ncs_kctl
, state
->ncs_unit
, state
->ncs_accumulated
, CTL_DATA_EOR
);
3617 nstat_stats
.nstat_flush_accumulated_msgs_failures
++;
3618 if (nstat_debug
!= 0) {
3619 printf("%s - ctl_enqueuembuf failed: %d\n", __func__
, result
);
3621 mbuf_freem(state
->ncs_accumulated
);
3623 state
->ncs_accumulated
= NULL
;
3629 nstat_accumulate_msg(
3630 nstat_control_state
*state
,
3634 if (state
->ncs_accumulated
&& mbuf_trailingspace(state
->ncs_accumulated
) < length
) {
3635 // Will send the current mbuf
3636 nstat_flush_accumulated_msgs(state
);
3641 if (state
->ncs_accumulated
== NULL
) {
3642 unsigned int one
= 1;
3643 if (mbuf_allocpacket(MBUF_DONTWAIT
, NSTAT_MAX_MSG_SIZE
, &one
, &state
->ncs_accumulated
) != 0) {
3644 if (nstat_debug
!= 0) {
3645 printf("%s - mbuf_allocpacket failed\n", __func__
);
3649 mbuf_setlen(state
->ncs_accumulated
, 0);
3654 hdr
->length
= length
;
3655 result
= mbuf_copyback(state
->ncs_accumulated
, mbuf_len(state
->ncs_accumulated
),
3656 length
, hdr
, MBUF_DONTWAIT
);
3660 nstat_flush_accumulated_msgs(state
);
3661 if (nstat_debug
!= 0) {
3662 printf("%s - resorting to ctl_enqueuedata\n", __func__
);
3664 result
= ctl_enqueuedata(state
->ncs_kctl
, state
->ncs_unit
, hdr
, length
, CTL_DATA_EOR
);
3668 nstat_stats
.nstat_accumulate_msg_failures
++;
3676 __unused thread_call_param_t p0
,
3677 __unused thread_call_param_t p1
)
3679 nstat_control_state
*control
;
3680 nstat_src
*src
, *tmpsrc
;
3681 tailq_head_nstat_src dead_list
;
3682 TAILQ_INIT(&dead_list
);
3684 lck_mtx_lock(&nstat_mtx
);
3686 nstat_idle_time
= 0;
3688 for (control
= nstat_controls
; control
; control
= control
->ncs_next
) {
3689 lck_mtx_lock(&control
->ncs_mtx
);
3690 if (!(control
->ncs_flags
& NSTAT_FLAG_REQCOUNTS
)) {
3691 TAILQ_FOREACH_SAFE(src
, &control
->ncs_src_queue
, ns_control_link
, tmpsrc
)
3693 if (src
->provider
->nstat_gone(src
->cookie
)) {
3696 // Pull it off the list
3697 TAILQ_REMOVE(&control
->ncs_src_queue
, src
, ns_control_link
);
3699 result
= nstat_control_send_goodbye(control
, src
);
3701 // Put this on the list to release later
3702 TAILQ_INSERT_TAIL(&dead_list
, src
, ns_control_link
);
3706 control
->ncs_flags
&= ~NSTAT_FLAG_REQCOUNTS
;
3707 lck_mtx_unlock(&control
->ncs_mtx
);
3710 if (nstat_controls
) {
3711 clock_interval_to_deadline(60, NSEC_PER_SEC
, &nstat_idle_time
);
3712 thread_call_func_delayed((thread_call_func_t
)nstat_idle_check
, NULL
, nstat_idle_time
);
3715 lck_mtx_unlock(&nstat_mtx
);
3717 /* Generate any system level reports, if needed */
3718 nstat_sysinfo_generate_report();
3720 // Release the sources now that we aren't holding lots of locks
3721 while ((src
= TAILQ_FIRST(&dead_list
))) {
3722 TAILQ_REMOVE(&dead_list
, src
, ns_control_link
);
3723 nstat_control_cleanup_source(NULL
, src
, FALSE
);
3731 nstat_control_register(void)
3733 // Create our lock group first
3734 lck_grp_attr_t
*grp_attr
= lck_grp_attr_alloc_init();
3735 lck_grp_attr_setdefault(grp_attr
);
3736 nstat_lck_grp
= lck_grp_alloc_init("network statistics kctl", grp_attr
);
3737 lck_grp_attr_free(grp_attr
);
3739 lck_mtx_init(&nstat_mtx
, nstat_lck_grp
, NULL
);
3741 // Register the control
3742 struct kern_ctl_reg nstat_control
;
3743 bzero(&nstat_control
, sizeof(nstat_control
));
3744 strlcpy(nstat_control
.ctl_name
, NET_STAT_CONTROL_NAME
, sizeof(nstat_control
.ctl_name
));
3745 nstat_control
.ctl_flags
= CTL_FLAG_REG_EXTENDED
| CTL_FLAG_REG_CRIT
;
3746 nstat_control
.ctl_sendsize
= nstat_sendspace
;
3747 nstat_control
.ctl_recvsize
= nstat_recvspace
;
3748 nstat_control
.ctl_connect
= nstat_control_connect
;
3749 nstat_control
.ctl_disconnect
= nstat_control_disconnect
;
3750 nstat_control
.ctl_send
= nstat_control_send
;
3752 ctl_register(&nstat_control
, &nstat_ctlref
);
3756 nstat_control_cleanup_source(
3757 nstat_control_state
*state
,
3758 struct nstat_src
*src
,
3764 result
= nstat_control_send_removed(state
, src
);
3766 nstat_stats
.nstat_control_cleanup_source_failures
++;
3767 if (nstat_debug
!= 0) {
3768 printf("%s - nstat_control_send_removed() %d\n",
3773 // Cleanup the source if we found it.
3774 src
->provider
->nstat_release(src
->cookie
, locked
);
3775 OSFree(src
, sizeof(*src
), nstat_malloc_tag
);
3780 nstat_control_reporting_allowed(
3781 nstat_control_state
*state
,
3784 if (src
->provider
->nstat_reporting_allowed
== NULL
) {
3789 src
->provider
->nstat_reporting_allowed(src
->cookie
,
3790 &state
->ncs_provider_filters
[src
->provider
->nstat_provider_id
])
3796 nstat_control_connect(
3798 struct sockaddr_ctl
*sac
,
3801 nstat_control_state
*state
= OSMalloc(sizeof(*state
), nstat_malloc_tag
);
3802 if (state
== NULL
) {
3806 bzero(state
, sizeof(*state
));
3807 lck_mtx_init(&state
->ncs_mtx
, nstat_lck_grp
, NULL
);
3808 state
->ncs_kctl
= kctl
;
3809 state
->ncs_unit
= sac
->sc_unit
;
3810 state
->ncs_flags
= NSTAT_FLAG_REQCOUNTS
;
3813 lck_mtx_lock(&nstat_mtx
);
3814 state
->ncs_next
= nstat_controls
;
3815 nstat_controls
= state
;
3817 if (nstat_idle_time
== 0) {
3818 clock_interval_to_deadline(60, NSEC_PER_SEC
, &nstat_idle_time
);
3819 thread_call_func_delayed((thread_call_func_t
)nstat_idle_check
, NULL
, nstat_idle_time
);
3822 lck_mtx_unlock(&nstat_mtx
);
3828 nstat_control_disconnect(
3829 __unused kern_ctl_ref kctl
,
3830 __unused u_int32_t unit
,
3834 nstat_control_state
*state
= (nstat_control_state
*)uinfo
;
3835 tailq_head_nstat_src cleanup_list
;
3838 TAILQ_INIT(&cleanup_list
);
3840 // pull it out of the global list of states
3841 lck_mtx_lock(&nstat_mtx
);
3842 nstat_control_state
**statepp
;
3843 for (statepp
= &nstat_controls
; *statepp
; statepp
= &(*statepp
)->ncs_next
) {
3844 if (*statepp
== state
) {
3845 *statepp
= state
->ncs_next
;
3849 lck_mtx_unlock(&nstat_mtx
);
3851 lck_mtx_lock(&state
->ncs_mtx
);
3852 // Stop watching for sources
3853 nstat_provider
*provider
;
3854 watching
= state
->ncs_watching
;
3855 state
->ncs_watching
= 0;
3856 for (provider
= nstat_providers
; provider
&& watching
; provider
= provider
->next
) {
3857 if ((watching
& (1 << provider
->nstat_provider_id
)) != 0) {
3858 watching
&= ~(1 << provider
->nstat_provider_id
);
3859 provider
->nstat_watcher_remove(state
);
3863 // set cleanup flags
3864 state
->ncs_flags
|= NSTAT_FLAG_CLEANUP
;
3866 if (state
->ncs_accumulated
) {
3867 mbuf_freem(state
->ncs_accumulated
);
3868 state
->ncs_accumulated
= NULL
;
3871 // Copy out the list of sources
3872 TAILQ_CONCAT(&cleanup_list
, &state
->ncs_src_queue
, ns_control_link
);
3873 lck_mtx_unlock(&state
->ncs_mtx
);
3875 while ((src
= TAILQ_FIRST(&cleanup_list
))) {
3876 TAILQ_REMOVE(&cleanup_list
, src
, ns_control_link
);
3877 nstat_control_cleanup_source(NULL
, src
, FALSE
);
3880 lck_mtx_destroy(&state
->ncs_mtx
, nstat_lck_grp
);
3881 OSFree(state
, sizeof(*state
), nstat_malloc_tag
);
3886 static nstat_src_ref_t
3887 nstat_control_next_src_ref(
3888 nstat_control_state
*state
)
3890 return ++state
->ncs_next_srcref
;
3894 nstat_control_send_counts(
3895 nstat_control_state
*state
,
3897 unsigned long long context
,
3898 u_int16_t hdr_flags
,
3901 nstat_msg_src_counts counts
;
3904 /* Some providers may not have any counts to send */
3905 if (src
->provider
->nstat_counts
== NULL
) {
3909 bzero(&counts
, sizeof(counts
));
3910 counts
.hdr
.type
= NSTAT_MSG_TYPE_SRC_COUNTS
;
3911 counts
.hdr
.length
= sizeof(counts
);
3912 counts
.hdr
.flags
= hdr_flags
;
3913 counts
.hdr
.context
= context
;
3914 counts
.srcref
= src
->srcref
;
3915 counts
.event_flags
= 0;
3917 if (src
->provider
->nstat_counts(src
->cookie
, &counts
.counts
, gone
) == 0) {
3918 if ((src
->filter
& NSTAT_FILTER_NOZEROBYTES
) &&
3919 counts
.counts
.nstat_rxbytes
== 0 &&
3920 counts
.counts
.nstat_txbytes
== 0) {
3923 result
= ctl_enqueuedata(state
->ncs_kctl
,
3924 state
->ncs_unit
, &counts
, sizeof(counts
),
3927 nstat_stats
.nstat_sendcountfailures
+= 1;
3935 nstat_control_append_counts(
3936 nstat_control_state
*state
,
3940 /* Some providers may not have any counts to send */
3941 if (!src
->provider
->nstat_counts
) {
3945 nstat_msg_src_counts counts
;
3946 bzero(&counts
, sizeof(counts
));
3947 counts
.hdr
.type
= NSTAT_MSG_TYPE_SRC_COUNTS
;
3948 counts
.hdr
.length
= sizeof(counts
);
3949 counts
.srcref
= src
->srcref
;
3950 counts
.event_flags
= 0;
3953 result
= src
->provider
->nstat_counts(src
->cookie
, &counts
.counts
, gone
);
3958 if ((src
->filter
& NSTAT_FILTER_NOZEROBYTES
) == NSTAT_FILTER_NOZEROBYTES
&&
3959 counts
.counts
.nstat_rxbytes
== 0 && counts
.counts
.nstat_txbytes
== 0) {
3963 return nstat_accumulate_msg(state
, &counts
.hdr
, counts
.hdr
.length
);
3967 nstat_control_send_description(
3968 nstat_control_state
*state
,
3971 u_int16_t hdr_flags
)
3973 // Provider doesn't support getting the descriptor? Done.
3974 if (src
->provider
->nstat_descriptor_length
== 0 ||
3975 src
->provider
->nstat_copy_descriptor
== NULL
) {
3979 // Allocate storage for the descriptor message
3981 unsigned int one
= 1;
3982 u_int32_t size
= offsetof(nstat_msg_src_description
, data
) + src
->provider
->nstat_descriptor_length
;
3983 if (mbuf_allocpacket(MBUF_DONTWAIT
, size
, &one
, &msg
) != 0) {
3987 nstat_msg_src_description
*desc
= (nstat_msg_src_description
*)mbuf_data(msg
);
3989 mbuf_setlen(msg
, size
);
3990 mbuf_pkthdr_setlen(msg
, mbuf_len(msg
));
3992 // Query the provider for the provider specific bits
3993 errno_t result
= src
->provider
->nstat_copy_descriptor(src
->cookie
, desc
->data
, src
->provider
->nstat_descriptor_length
);
4000 desc
->hdr
.context
= context
;
4001 desc
->hdr
.type
= NSTAT_MSG_TYPE_SRC_DESC
;
4002 desc
->hdr
.length
= size
;
4003 desc
->hdr
.flags
= hdr_flags
;
4004 desc
->srcref
= src
->srcref
;
4005 desc
->event_flags
= 0;
4006 desc
->provider
= src
->provider
->nstat_provider_id
;
4008 result
= ctl_enqueuembuf(state
->ncs_kctl
, state
->ncs_unit
, msg
, CTL_DATA_EOR
);
4010 nstat_stats
.nstat_descriptionfailures
+= 1;
4018 nstat_control_append_description(
4019 nstat_control_state
*state
,
4022 size_t size
= offsetof(nstat_msg_src_description
, data
) + src
->provider
->nstat_descriptor_length
;
4023 if (size
> 512 || src
->provider
->nstat_descriptor_length
== 0 ||
4024 src
->provider
->nstat_copy_descriptor
== NULL
) {
4028 // Fill out a buffer on the stack, we will copy to the mbuf later
4029 u_int64_t buffer
[size
/ sizeof(u_int64_t
) + 1]; // u_int64_t to ensure alignment
4030 bzero(buffer
, size
);
4032 nstat_msg_src_description
*desc
= (nstat_msg_src_description
*)buffer
;
4033 desc
->hdr
.type
= NSTAT_MSG_TYPE_SRC_DESC
;
4034 desc
->hdr
.length
= size
;
4035 desc
->srcref
= src
->srcref
;
4036 desc
->event_flags
= 0;
4037 desc
->provider
= src
->provider
->nstat_provider_id
;
4040 // Fill in the description
4041 // Query the provider for the provider specific bits
4042 result
= src
->provider
->nstat_copy_descriptor(src
->cookie
, desc
->data
,
4043 src
->provider
->nstat_descriptor_length
);
4048 return nstat_accumulate_msg(state
, &desc
->hdr
, size
);
4052 nstat_control_send_update(
4053 nstat_control_state
*state
,
4056 u_int16_t hdr_flags
,
4059 // Provider doesn't support getting the descriptor or counts? Done.
4060 if ((src
->provider
->nstat_descriptor_length
== 0 ||
4061 src
->provider
->nstat_copy_descriptor
== NULL
) &&
4062 src
->provider
->nstat_counts
== NULL
) {
4066 // Allocate storage for the descriptor message
4068 unsigned int one
= 1;
4069 u_int32_t size
= offsetof(nstat_msg_src_update
, data
) +
4070 src
->provider
->nstat_descriptor_length
;
4071 if (mbuf_allocpacket(MBUF_DONTWAIT
, size
, &one
, &msg
) != 0) {
4075 nstat_msg_src_update
*desc
= (nstat_msg_src_update
*)mbuf_data(msg
);
4077 desc
->hdr
.context
= context
;
4078 desc
->hdr
.type
= NSTAT_MSG_TYPE_SRC_UPDATE
;
4079 desc
->hdr
.length
= size
;
4080 desc
->hdr
.flags
= hdr_flags
;
4081 desc
->srcref
= src
->srcref
;
4082 desc
->event_flags
= 0;
4083 desc
->provider
= src
->provider
->nstat_provider_id
;
4085 mbuf_setlen(msg
, size
);
4086 mbuf_pkthdr_setlen(msg
, mbuf_len(msg
));
4089 if (src
->provider
->nstat_descriptor_length
!= 0 && src
->provider
->nstat_copy_descriptor
) {
4090 // Query the provider for the provider specific bits
4091 result
= src
->provider
->nstat_copy_descriptor(src
->cookie
, desc
->data
,
4092 src
->provider
->nstat_descriptor_length
);
4099 if (src
->provider
->nstat_counts
) {
4100 result
= src
->provider
->nstat_counts(src
->cookie
, &desc
->counts
, gone
);
4102 if ((src
->filter
& NSTAT_FILTER_NOZEROBYTES
) == NSTAT_FILTER_NOZEROBYTES
&&
4103 desc
->counts
.nstat_rxbytes
== 0 && desc
->counts
.nstat_txbytes
== 0) {
4106 result
= ctl_enqueuembuf(state
->ncs_kctl
, state
->ncs_unit
, msg
, CTL_DATA_EOR
);
4112 nstat_stats
.nstat_srcupatefailures
+= 1;
4120 nstat_control_append_update(
4121 nstat_control_state
*state
,
4125 size_t size
= offsetof(nstat_msg_src_update
, data
) + src
->provider
->nstat_descriptor_length
;
4126 if (size
> 512 || ((src
->provider
->nstat_descriptor_length
== 0 ||
4127 src
->provider
->nstat_copy_descriptor
== NULL
) &&
4128 src
->provider
->nstat_counts
== NULL
)) {
4132 // Fill out a buffer on the stack, we will copy to the mbuf later
4133 u_int64_t buffer
[size
/ sizeof(u_int64_t
) + 1]; // u_int64_t to ensure alignment
4134 bzero(buffer
, size
);
4136 nstat_msg_src_update
*desc
= (nstat_msg_src_update
*)buffer
;
4137 desc
->hdr
.type
= NSTAT_MSG_TYPE_SRC_UPDATE
;
4138 desc
->hdr
.length
= size
;
4139 desc
->srcref
= src
->srcref
;
4140 desc
->event_flags
= 0;
4141 desc
->provider
= src
->provider
->nstat_provider_id
;
4144 // Fill in the description
4145 if (src
->provider
->nstat_descriptor_length
!= 0 && src
->provider
->nstat_copy_descriptor
) {
4146 // Query the provider for the provider specific bits
4147 result
= src
->provider
->nstat_copy_descriptor(src
->cookie
, desc
->data
,
4148 src
->provider
->nstat_descriptor_length
);
4150 nstat_stats
.nstat_copy_descriptor_failures
++;
4151 if (nstat_debug
!= 0) {
4152 printf("%s: src->provider->nstat_copy_descriptor: %d\n", __func__
, result
);
4158 if (src
->provider
->nstat_counts
) {
4159 result
= src
->provider
->nstat_counts(src
->cookie
, &desc
->counts
, gone
);
4161 nstat_stats
.nstat_provider_counts_failures
++;
4162 if (nstat_debug
!= 0) {
4163 printf("%s: src->provider->nstat_counts: %d\n", __func__
, result
);
4168 if ((src
->filter
& NSTAT_FILTER_NOZEROBYTES
) == NSTAT_FILTER_NOZEROBYTES
&&
4169 desc
->counts
.nstat_rxbytes
== 0 && desc
->counts
.nstat_txbytes
== 0) {
4174 return nstat_accumulate_msg(state
, &desc
->hdr
, size
);
4178 nstat_control_send_removed(
4179 nstat_control_state
*state
,
4182 nstat_msg_src_removed removed
;
4185 bzero(&removed
, sizeof(removed
));
4186 removed
.hdr
.type
= NSTAT_MSG_TYPE_SRC_REMOVED
;
4187 removed
.hdr
.length
= sizeof(removed
);
4188 removed
.hdr
.context
= 0;
4189 removed
.srcref
= src
->srcref
;
4190 result
= ctl_enqueuedata(state
->ncs_kctl
, state
->ncs_unit
, &removed
,
4191 sizeof(removed
), CTL_DATA_EOR
| CTL_DATA_CRIT
);
4193 nstat_stats
.nstat_msgremovedfailures
+= 1;
4200 nstat_control_handle_add_request(
4201 nstat_control_state
*state
,
4206 // Verify the header fits in the first mbuf
4207 if (mbuf_len(m
) < offsetof(nstat_msg_add_src_req
, param
)) {
4211 // Calculate the length of the parameter field
4212 int32_t paramlength
= mbuf_pkthdr_len(m
) - offsetof(nstat_msg_add_src_req
, param
);
4213 if (paramlength
< 0 || paramlength
> 2 * 1024) {
4217 nstat_provider
*provider
= NULL
;
4218 nstat_provider_cookie_t cookie
= NULL
;
4219 nstat_msg_add_src_req
*req
= mbuf_data(m
);
4220 if (mbuf_pkthdr_len(m
) > mbuf_len(m
)) {
4221 // parameter is too large, we need to make a contiguous copy
4222 void *data
= OSMalloc(paramlength
, nstat_malloc_tag
);
4227 result
= mbuf_copydata(m
, offsetof(nstat_msg_add_src_req
, param
), paramlength
, data
);
4229 result
= nstat_lookup_entry(req
->provider
, data
, paramlength
, &provider
, &cookie
);
4231 OSFree(data
, paramlength
, nstat_malloc_tag
);
4233 result
= nstat_lookup_entry(req
->provider
, (void*)&req
->param
, paramlength
, &provider
, &cookie
);
4240 result
= nstat_control_source_add(req
->hdr
.context
, state
, provider
, cookie
);
4242 provider
->nstat_release(cookie
, 0);
4249 nstat_set_provider_filter(
4250 nstat_control_state
*state
,
4251 nstat_msg_add_all_srcs
*req
)
4253 nstat_provider_id_t provider_id
= req
->provider
;
4255 u_int32_t prev_ncs_watching
= atomic_or_32_ov(&state
->ncs_watching
, (1 << provider_id
));
4257 if ((prev_ncs_watching
& (1 << provider_id
)) != 0) {
4261 state
->ncs_watching
|= (1 << provider_id
);
4262 state
->ncs_provider_filters
[provider_id
].npf_flags
= req
->filter
;
4263 state
->ncs_provider_filters
[provider_id
].npf_events
= req
->events
;
4264 state
->ncs_provider_filters
[provider_id
].npf_pid
= req
->target_pid
;
4265 uuid_copy(state
->ncs_provider_filters
[provider_id
].npf_uuid
, req
->target_uuid
);
4270 nstat_control_handle_add_all(
4271 nstat_control_state
*state
,
4276 // Verify the header fits in the first mbuf
4277 if (mbuf_len(m
) < sizeof(nstat_msg_add_all_srcs
)) {
4281 nstat_msg_add_all_srcs
*req
= mbuf_data(m
);
4282 if (req
->provider
> NSTAT_PROVIDER_LAST
) {
4286 nstat_provider
*provider
= nstat_find_provider_by_id(req
->provider
);
4291 if (provider
->nstat_watcher_add
== NULL
) {
4295 if (nstat_privcheck
!= 0) {
4296 result
= priv_check_cred(kauth_cred_get(),
4297 PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0);
4303 lck_mtx_lock(&state
->ncs_mtx
);
4304 if (req
->filter
& NSTAT_FILTER_SUPPRESS_SRC_ADDED
) {
4305 // Suppression of source messages implicitly requires the use of update messages
4306 state
->ncs_flags
|= NSTAT_FLAG_SUPPORTS_UPDATES
;
4308 lck_mtx_unlock(&state
->ncs_mtx
);
4310 // rdar://problem/30301300 Different providers require different synchronization
4311 // to ensure that a new entry does not get double counted due to being added prior
4312 // to all current provider entries being added. Hence pass the provider the details
4313 // in the original request for this to be applied atomically
4315 result
= provider
->nstat_watcher_add(state
, req
);
4318 nstat_enqueue_success(req
->hdr
.context
, state
, 0);
4325 nstat_control_source_add(
4327 nstat_control_state
*state
,
4328 nstat_provider
*provider
,
4329 nstat_provider_cookie_t cookie
)
4331 // Fill out source added message if appropriate
4333 nstat_src_ref_t
*srcrefp
= NULL
;
4335 u_int64_t provider_filter_flagss
=
4336 state
->ncs_provider_filters
[provider
->nstat_provider_id
].npf_flags
;
4337 boolean_t tell_user
=
4338 ((provider_filter_flagss
& NSTAT_FILTER_SUPPRESS_SRC_ADDED
) == 0);
4339 u_int32_t src_filter
=
4340 (provider_filter_flagss
& NSTAT_FILTER_PROVIDER_NOZEROBYTES
)
4341 ? NSTAT_FILTER_NOZEROBYTES
: 0;
4343 if (provider_filter_flagss
& NSTAT_FILTER_TCP_NO_EARLY_CLOSE
) {
4344 src_filter
|= NSTAT_FILTER_TCP_NO_EARLY_CLOSE
;
4348 unsigned int one
= 1;
4350 if (mbuf_allocpacket(MBUF_DONTWAIT
, sizeof(nstat_msg_src_added
),
4355 mbuf_setlen(msg
, sizeof(nstat_msg_src_added
));
4356 mbuf_pkthdr_setlen(msg
, mbuf_len(msg
));
4357 nstat_msg_src_added
*add
= mbuf_data(msg
);
4358 bzero(add
, sizeof(*add
));
4359 add
->hdr
.type
= NSTAT_MSG_TYPE_SRC_ADDED
;
4360 add
->hdr
.length
= mbuf_len(msg
);
4361 add
->hdr
.context
= context
;
4362 add
->provider
= provider
->nstat_provider_id
;
4363 srcrefp
= &add
->srcref
;
4366 // Allocate storage for the source
4367 nstat_src
*src
= OSMalloc(sizeof(*src
), nstat_malloc_tag
);
4375 // Fill in the source, including picking an unused source ref
4376 lck_mtx_lock(&state
->ncs_mtx
);
4378 src
->srcref
= nstat_control_next_src_ref(state
);
4380 *srcrefp
= src
->srcref
;
4383 if (state
->ncs_flags
& NSTAT_FLAG_CLEANUP
|| src
->srcref
== NSTAT_SRC_REF_INVALID
) {
4384 lck_mtx_unlock(&state
->ncs_mtx
);
4385 OSFree(src
, sizeof(*src
), nstat_malloc_tag
);
4391 src
->provider
= provider
;
4392 src
->cookie
= cookie
;
4393 src
->filter
= src_filter
;
4397 // send the source added message if appropriate
4398 errno_t result
= ctl_enqueuembuf(state
->ncs_kctl
, state
->ncs_unit
, msg
,
4401 nstat_stats
.nstat_srcaddedfailures
+= 1;
4402 lck_mtx_unlock(&state
->ncs_mtx
);
4403 OSFree(src
, sizeof(*src
), nstat_malloc_tag
);
4408 // Put the source in the list
4409 TAILQ_INSERT_HEAD(&state
->ncs_src_queue
, src
, ns_control_link
);
4410 src
->ns_control
= state
;
4412 lck_mtx_unlock(&state
->ncs_mtx
);
4418 nstat_control_handle_remove_request(
4419 nstat_control_state
*state
,
4422 nstat_src_ref_t srcref
= NSTAT_SRC_REF_INVALID
;
4425 if (mbuf_copydata(m
, offsetof(nstat_msg_rem_src_req
, srcref
), sizeof(srcref
), &srcref
) != 0) {
4429 lck_mtx_lock(&state
->ncs_mtx
);
4431 // Remove this source as we look for it
4432 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
4434 if (src
->srcref
== srcref
) {
4439 TAILQ_REMOVE(&state
->ncs_src_queue
, src
, ns_control_link
);
4442 lck_mtx_unlock(&state
->ncs_mtx
);
4445 nstat_control_cleanup_source(state
, src
, FALSE
);
4448 return src
? 0 : ENOENT
;
4452 nstat_control_handle_query_request(
4453 nstat_control_state
*state
,
4456 // TBD: handle this from another thread so we can enqueue a lot of data
4457 // As written, if a client requests query all, this function will be
4458 // called from their send of the request message. We will attempt to write
4459 // responses and succeed until the buffer fills up. Since the clients thread
4460 // is blocked on send, it won't be reading unless the client has two threads
4461 // using this socket, one for read and one for write. Two threads probably
4462 // won't work with this code anyhow since we don't have proper locking in
4464 tailq_head_nstat_src dead_list
;
4465 errno_t result
= ENOENT
;
4466 nstat_msg_query_src_req req
;
4468 if (mbuf_copydata(m
, 0, sizeof(req
), &req
) != 0) {
4472 const boolean_t all_srcs
= (req
.srcref
== NSTAT_SRC_REF_ALL
);
4473 TAILQ_INIT(&dead_list
);
4475 lck_mtx_lock(&state
->ncs_mtx
);
4478 state
->ncs_flags
|= NSTAT_FLAG_REQCOUNTS
;
4480 nstat_src
*src
, *tmpsrc
;
4481 u_int64_t src_count
= 0;
4482 boolean_t partial
= FALSE
;
4485 * Error handling policy and sequence number generation is folded into
4486 * nstat_control_begin_query.
4488 partial
= nstat_control_begin_query(state
, &req
.hdr
);
4491 TAILQ_FOREACH_SAFE(src
, &state
->ncs_src_queue
, ns_control_link
, tmpsrc
)
4495 // XXX ignore IFACE types?
4496 if (all_srcs
|| src
->srcref
== req
.srcref
) {
4497 if (nstat_control_reporting_allowed(state
, src
)
4498 && (!partial
|| !all_srcs
|| src
->seq
!= state
->ncs_seq
)) {
4500 (req
.hdr
.flags
& NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE
) != 0) {
4501 result
= nstat_control_append_counts(state
, src
, &gone
);
4503 result
= nstat_control_send_counts(state
, src
, req
.hdr
.context
, 0, &gone
);
4506 if (ENOMEM
== result
|| ENOBUFS
== result
) {
4508 * If the counts message failed to
4509 * enqueue then we should clear our flag so
4510 * that a client doesn't miss anything on
4511 * idle cleanup. We skip the "gone"
4512 * processing in the hope that we may
4513 * catch it another time.
4515 state
->ncs_flags
&= ~NSTAT_FLAG_REQCOUNTS
;
4520 * We skip over hard errors and
4523 src
->seq
= state
->ncs_seq
;
4530 // send one last descriptor message so client may see last state
4531 // If we can't send the notification now, it
4532 // will be sent in the idle cleanup.
4533 result
= nstat_control_send_description(state
, src
, 0, 0);
4535 nstat_stats
.nstat_control_send_description_failures
++;
4536 if (nstat_debug
!= 0) {
4537 printf("%s - nstat_control_send_description() %d\n", __func__
, result
);
4539 state
->ncs_flags
&= ~NSTAT_FLAG_REQCOUNTS
;
4543 // pull src out of the list
4544 TAILQ_REMOVE(&state
->ncs_src_queue
, src
, ns_control_link
);
4545 TAILQ_INSERT_TAIL(&dead_list
, src
, ns_control_link
);
4549 if (src_count
>= QUERY_CONTINUATION_SRC_COUNT
) {
4552 } else if (req
.srcref
== src
->srcref
) {
4557 nstat_flush_accumulated_msgs(state
);
4559 u_int16_t flags
= 0;
4560 if (req
.srcref
== NSTAT_SRC_REF_ALL
) {
4561 flags
= nstat_control_end_query(state
, src
, partial
);
4564 lck_mtx_unlock(&state
->ncs_mtx
);
4567 * If an error occurred enqueueing data, then allow the error to
4568 * propagate to nstat_control_send. This way, the error is sent to
4571 if (all_srcs
&& ENOMEM
!= result
&& ENOBUFS
!= result
) {
4572 nstat_enqueue_success(req
.hdr
.context
, state
, flags
);
4576 while ((src
= TAILQ_FIRST(&dead_list
))) {
4577 TAILQ_REMOVE(&dead_list
, src
, ns_control_link
);
4578 nstat_control_cleanup_source(state
, src
, FALSE
);
4585 nstat_control_handle_get_src_description(
4586 nstat_control_state
*state
,
4589 nstat_msg_get_src_description req
;
4590 errno_t result
= ENOENT
;
4593 if (mbuf_copydata(m
, 0, sizeof(req
), &req
) != 0) {
4597 lck_mtx_lock(&state
->ncs_mtx
);
4598 u_int64_t src_count
= 0;
4599 boolean_t partial
= FALSE
;
4600 const boolean_t all_srcs
= (req
.srcref
== NSTAT_SRC_REF_ALL
);
4603 * Error handling policy and sequence number generation is folded into
4604 * nstat_control_begin_query.
4606 partial
= nstat_control_begin_query(state
, &req
.hdr
);
4608 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
4610 if (all_srcs
|| src
->srcref
== req
.srcref
) {
4611 if (nstat_control_reporting_allowed(state
, src
)
4612 && (!all_srcs
|| !partial
|| src
->seq
!= state
->ncs_seq
)) {
4613 if ((req
.hdr
.flags
& NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE
) != 0 && all_srcs
) {
4614 result
= nstat_control_append_description(state
, src
);
4616 result
= nstat_control_send_description(state
, src
, req
.hdr
.context
, 0);
4619 if (ENOMEM
== result
|| ENOBUFS
== result
) {
4621 * If the description message failed to
4622 * enqueue then we give up for now.
4628 * Note, we skip over hard errors and
4631 src
->seq
= state
->ncs_seq
;
4633 if (src_count
>= QUERY_CONTINUATION_SRC_COUNT
) {
4644 nstat_flush_accumulated_msgs(state
);
4646 u_int16_t flags
= 0;
4647 if (req
.srcref
== NSTAT_SRC_REF_ALL
) {
4648 flags
= nstat_control_end_query(state
, src
, partial
);
4651 lck_mtx_unlock(&state
->ncs_mtx
);
4653 * If an error occurred enqueueing data, then allow the error to
4654 * propagate to nstat_control_send. This way, the error is sent to
4657 if (all_srcs
&& ENOMEM
!= result
&& ENOBUFS
!= result
) {
4658 nstat_enqueue_success(req
.hdr
.context
, state
, flags
);
4666 nstat_control_handle_set_filter(
4667 nstat_control_state
*state
,
4670 nstat_msg_set_filter req
;
4673 if (mbuf_copydata(m
, 0, sizeof(req
), &req
) != 0) {
4676 if (req
.srcref
== NSTAT_SRC_REF_ALL
||
4677 req
.srcref
== NSTAT_SRC_REF_INVALID
) {
4681 lck_mtx_lock(&state
->ncs_mtx
);
4682 TAILQ_FOREACH(src
, &state
->ncs_src_queue
, ns_control_link
)
4684 if (req
.srcref
== src
->srcref
) {
4685 src
->filter
= req
.filter
;
4689 lck_mtx_unlock(&state
->ncs_mtx
);
4699 nstat_control_state
*state
,
4704 struct nstat_msg_error err
;
4706 bzero(&err
, sizeof(err
));
4707 err
.hdr
.type
= NSTAT_MSG_TYPE_ERROR
;
4708 err
.hdr
.length
= sizeof(err
);
4709 err
.hdr
.context
= context
;
4712 result
= ctl_enqueuedata(state
->ncs_kctl
, state
->ncs_unit
, &err
,
4713 sizeof(err
), CTL_DATA_EOR
| CTL_DATA_CRIT
);
4715 nstat_stats
.nstat_msgerrorfailures
++;
4720 nstat_control_begin_query(
4721 nstat_control_state
*state
,
4722 const nstat_msg_hdr
*hdrp
)
4724 boolean_t partial
= FALSE
;
4726 if (hdrp
->flags
& NSTAT_MSG_HDR_FLAG_CONTINUATION
) {
4727 /* A partial query all has been requested. */
4730 if (state
->ncs_context
!= hdrp
->context
) {
4731 if (state
->ncs_context
!= 0) {
4732 nstat_send_error(state
, state
->ncs_context
, EAGAIN
);
4735 /* Initialize state for a partial query all. */
4736 state
->ncs_context
= hdrp
->context
;
4745 nstat_control_end_query(
4746 nstat_control_state
*state
,
4747 nstat_src
*last_src
,
4750 u_int16_t flags
= 0;
4752 if (last_src
== NULL
|| !partial
) {
4754 * We iterated through the entire srcs list or exited early
4755 * from the loop when a partial update was not requested (an
4756 * error occurred), so clear context to indicate internally
4757 * that the query is finished.
4759 state
->ncs_context
= 0;
4762 * Indicate to userlevel to make another partial request as
4763 * there are still sources left to be reported.
4765 flags
|= NSTAT_MSG_HDR_FLAG_CONTINUATION
;
4772 nstat_control_handle_get_update(
4773 nstat_control_state
*state
,
4776 nstat_msg_query_src_req req
;
4778 if (mbuf_copydata(m
, 0, sizeof(req
), &req
) != 0) {
4782 lck_mtx_lock(&state
->ncs_mtx
);
4784 state
->ncs_flags
|= NSTAT_FLAG_SUPPORTS_UPDATES
;
4786 errno_t result
= ENOENT
;
4787 nstat_src
*src
, *tmpsrc
;
4788 tailq_head_nstat_src dead_list
;
4789 u_int64_t src_count
= 0;
4790 boolean_t partial
= FALSE
;
4791 TAILQ_INIT(&dead_list
);
4794 * Error handling policy and sequence number generation is folded into
4795 * nstat_control_begin_query.
4797 partial
= nstat_control_begin_query(state
, &req
.hdr
);
4799 TAILQ_FOREACH_SAFE(src
, &state
->ncs_src_queue
, ns_control_link
, tmpsrc
)
4804 if (nstat_control_reporting_allowed(state
, src
)) {
4805 /* skip this source if it has the current state
4806 * sequence number as it's already been reported in
4807 * this query-all partial sequence. */
4808 if (req
.srcref
== NSTAT_SRC_REF_ALL
4809 && (FALSE
== partial
|| src
->seq
!= state
->ncs_seq
)) {
4810 result
= nstat_control_append_update(state
, src
, &gone
);
4811 if (ENOMEM
== result
|| ENOBUFS
== result
) {
4813 * If the update message failed to
4814 * enqueue then give up.
4820 * We skip over hard errors and
4823 src
->seq
= state
->ncs_seq
;
4826 } else if (src
->srcref
== req
.srcref
) {
4827 result
= nstat_control_send_update(state
, src
, req
.hdr
.context
, 0, &gone
);
4832 // pull src out of the list
4833 TAILQ_REMOVE(&state
->ncs_src_queue
, src
, ns_control_link
);
4834 TAILQ_INSERT_TAIL(&dead_list
, src
, ns_control_link
);
4837 if (req
.srcref
!= NSTAT_SRC_REF_ALL
&& req
.srcref
== src
->srcref
) {
4840 if (src_count
>= QUERY_CONTINUATION_SRC_COUNT
) {
4845 nstat_flush_accumulated_msgs(state
);
4848 u_int16_t flags
= 0;
4849 if (req
.srcref
== NSTAT_SRC_REF_ALL
) {
4850 flags
= nstat_control_end_query(state
, src
, partial
);
4853 lck_mtx_unlock(&state
->ncs_mtx
);
4855 * If an error occurred enqueueing data, then allow the error to
4856 * propagate to nstat_control_send. This way, the error is sent to
4859 if (req
.srcref
== NSTAT_SRC_REF_ALL
&& ENOMEM
!= result
&& ENOBUFS
!= result
) {
4860 nstat_enqueue_success(req
.hdr
.context
, state
, flags
);
4864 while ((src
= TAILQ_FIRST(&dead_list
))) {
4865 TAILQ_REMOVE(&dead_list
, src
, ns_control_link
);
4866 // release src and send notification
4867 nstat_control_cleanup_source(state
, src
, FALSE
);
4874 nstat_control_handle_subscribe_sysinfo(
4875 nstat_control_state
*state
)
4877 errno_t result
= priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0);
4883 lck_mtx_lock(&state
->ncs_mtx
);
4884 state
->ncs_flags
|= NSTAT_FLAG_SYSINFO_SUBSCRIBED
;
4885 lck_mtx_unlock(&state
->ncs_mtx
);
4898 nstat_control_state
*state
= (nstat_control_state
*)uinfo
;
4899 struct nstat_msg_hdr
*hdr
;
4900 struct nstat_msg_hdr storage
;
4903 if (mbuf_pkthdr_len(m
) < sizeof(*hdr
)) {
4904 // Is this the right thing to do?
4909 if (mbuf_len(m
) >= sizeof(*hdr
)) {
4912 mbuf_copydata(m
, 0, sizeof(storage
), &storage
);
4916 // Legacy clients may not set the length
4917 // Those clients are likely not setting the flags either
4918 // Fix everything up so old clients continue to work
4919 if (hdr
->length
!= mbuf_pkthdr_len(m
)) {
4921 hdr
->length
= mbuf_pkthdr_len(m
);
4922 if (hdr
== &storage
) {
4923 mbuf_copyback(m
, 0, sizeof(*hdr
), hdr
, MBUF_DONTWAIT
);
4927 switch (hdr
->type
) {
4928 case NSTAT_MSG_TYPE_ADD_SRC
:
4929 result
= nstat_control_handle_add_request(state
, m
);
4932 case NSTAT_MSG_TYPE_ADD_ALL_SRCS
:
4933 result
= nstat_control_handle_add_all(state
, m
);
4936 case NSTAT_MSG_TYPE_REM_SRC
:
4937 result
= nstat_control_handle_remove_request(state
, m
);
4940 case NSTAT_MSG_TYPE_QUERY_SRC
:
4941 result
= nstat_control_handle_query_request(state
, m
);
4944 case NSTAT_MSG_TYPE_GET_SRC_DESC
:
4945 result
= nstat_control_handle_get_src_description(state
, m
);
4948 case NSTAT_MSG_TYPE_SET_FILTER
:
4949 result
= nstat_control_handle_set_filter(state
, m
);
4952 case NSTAT_MSG_TYPE_GET_UPDATE
:
4953 result
= nstat_control_handle_get_update(state
, m
);
4956 case NSTAT_MSG_TYPE_SUBSCRIBE_SYSINFO
:
4957 result
= nstat_control_handle_subscribe_sysinfo(state
);
4966 struct nstat_msg_error err
;
4968 bzero(&err
, sizeof(err
));
4969 err
.hdr
.type
= NSTAT_MSG_TYPE_ERROR
;
4970 err
.hdr
.length
= sizeof(err
) + mbuf_pkthdr_len(m
);
4971 err
.hdr
.context
= hdr
->context
;
4974 if (mbuf_prepend(&m
, sizeof(err
), MBUF_DONTWAIT
) == 0 &&
4975 mbuf_copyback(m
, 0, sizeof(err
), &err
, MBUF_DONTWAIT
) == 0) {
4976 result
= ctl_enqueuembuf(kctl
, unit
, m
, CTL_DATA_EOR
| CTL_DATA_CRIT
);
4984 // Unable to prepend the error to the request - just send the error
4985 err
.hdr
.length
= sizeof(err
);
4986 result
= ctl_enqueuedata(kctl
, unit
, &err
, sizeof(err
),
4987 CTL_DATA_EOR
| CTL_DATA_CRIT
);
4989 nstat_stats
.nstat_msgerrorfailures
+= 1;
4992 nstat_stats
.nstat_handle_msg_failures
+= 1;
5004 tcp_progress_indicators_for_interface(unsigned int ifindex
, uint64_t recentflow_maxduration
, struct xtcpprogress_indicators
*indicators
)
5008 uint64_t min_recent_start_time
;
5010 min_recent_start_time
= mach_continuous_time() - recentflow_maxduration
;
5011 bzero(indicators
, sizeof(*indicators
));
5013 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
5015 * For progress indicators we don't need to special case TCP to collect time wait connections
5017 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
)
5019 struct tcpcb
*tp
= intotcpcb(inp
);
5020 if (tp
&& inp
->inp_last_outifp
&&
5021 inp
->inp_last_outifp
->if_index
== ifindex
&&
5022 inp
->inp_state
!= INPCB_STATE_DEAD
&&
5023 !(tp
->t_flags
& TF_LOCAL
)) {
5024 struct tcp_conn_status connstatus
;
5025 indicators
->xp_numflows
++;
5026 tcp_get_connectivity_status(tp
, &connstatus
);
5027 if (connstatus
.write_probe_failed
) {
5028 indicators
->xp_write_probe_fails
++;
5030 if (connstatus
.read_probe_failed
) {
5031 indicators
->xp_read_probe_fails
++;
5033 if (connstatus
.conn_probe_failed
) {
5034 indicators
->xp_conn_probe_fails
++;
5036 if (inp
->inp_start_timestamp
> min_recent_start_time
) {
5037 uint64_t flow_count
;
5039 indicators
->xp_recentflows
++;
5040 atomic_get_64(flow_count
, &inp
->inp_stat
->rxbytes
);
5041 indicators
->xp_recentflows_rxbytes
+= flow_count
;
5042 atomic_get_64(flow_count
, &inp
->inp_stat
->txbytes
);
5043 indicators
->xp_recentflows_txbytes
+= flow_count
;
5045 indicators
->xp_recentflows_rxooo
+= tp
->t_stat
.rxoutoforderbytes
;
5046 indicators
->xp_recentflows_rxdup
+= tp
->t_stat
.rxduplicatebytes
;
5047 indicators
->xp_recentflows_retx
+= tp
->t_stat
.txretransmitbytes
;
5048 if (tp
->snd_max
- tp
->snd_una
) {
5049 indicators
->xp_recentflows_unacked
++;
5054 lck_rw_done(tcbinfo
.ipi_lock
);
5060 __private_extern__
int
5061 ntstat_tcp_progress_indicators(struct sysctl_req
*req
)
5063 struct xtcpprogress_indicators indicators
= {};
5065 struct tcpprogressreq requested
;
5067 if (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0) != 0) {
5070 if (req
->newptr
== USER_ADDR_NULL
) {
5073 if (req
->newlen
< sizeof(req
)) {
5076 error
= SYSCTL_IN(req
, &requested
, sizeof(requested
));
5080 error
= tcp_progress_indicators_for_interface(requested
.ifindex
, requested
.recentflow_maxduration
, &indicators
);
5084 error
= SYSCTL_OUT(req
, &indicators
, sizeof(indicators
));