2 * Copyright (c) 2010-2016 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@
30 #include <netinet/in.h>
32 #include <net/if_var.h>
33 #include <netinet/tcp.h>
37 #pragma mark -- Common Data Structures --
39 #define __NSTAT_REVISION__ 8
41 typedef u_int32_t nstat_provider_id_t
;
42 typedef u_int64_t nstat_src_ref_t
;
43 typedef u_int64_t nstat_event_flags_t
;
45 // The following event definitions are very provisional..
48 NSTAT_EVENT_SRC_ADDED
= 0x00000001
49 ,NSTAT_EVENT_SRC_REMOVED
= 0x00000002
50 ,NSTAT_EVENT_SRC_QUERIED
= 0x00000004
51 ,NSTAT_EVENT_SRC_QUERIED_ALL
= 0x00000008
52 ,NSTAT_EVENT_SRC_WILL_CHANGE_STATE
= 0x00000010
53 ,NSTAT_EVENT_SRC_DID_CHANGE_STATE
= 0x00000020
54 ,NSTAT_EVENT_SRC_WILL_CHANGE_OWNER
= 0x00000040
55 ,NSTAT_EVENT_SRC_DID_CHANGE_OWNER
= 0x00000080
56 ,NSTAT_EVENT_SRC_WILL_CHANGE_PROPERTY
= 0x00000100
57 ,NSTAT_EVENT_SRC_DID_CHANGE_PROPERTY
= 0x00000200
60 typedef struct nstat_counts
63 u_int64_t nstat_rxpackets
__attribute__((aligned(8)));
64 u_int64_t nstat_rxbytes
__attribute__((aligned(8)));
65 u_int64_t nstat_txpackets
__attribute__((aligned(8)));
66 u_int64_t nstat_txbytes
__attribute__((aligned(8)));
68 u_int32_t nstat_rxduplicatebytes
;
69 u_int32_t nstat_rxoutoforderbytes
;
70 u_int32_t nstat_txretransmit
;
72 u_int32_t nstat_connectattempts
;
73 u_int32_t nstat_connectsuccesses
;
75 u_int32_t nstat_min_rtt
;
76 u_int32_t nstat_avg_rtt
;
77 u_int32_t nstat_var_rtt
;
79 u_int64_t nstat_cell_rxbytes
__attribute__((aligned(8)));
80 u_int64_t nstat_cell_txbytes
__attribute__((aligned(8)));
81 u_int64_t nstat_wifi_rxbytes
__attribute__((aligned(8)));
82 u_int64_t nstat_wifi_txbytes
__attribute__((aligned(8)));
83 u_int64_t nstat_wired_rxbytes
__attribute__((aligned(8)));
84 u_int64_t nstat_wired_txbytes
__attribute__((aligned(8)));
87 typedef struct nstat_sysinfo_keyval
89 u_int32_t nstat_sysinfo_key
;
90 u_int32_t nstat_sysinfo_flags
;
92 int64_t nstat_sysinfo_scalar
;
93 double nstat_sysinfo_distribution
;
95 } __attribute__((packed
)) nstat_sysinfo_keyval
;
97 #define NSTAT_SYSINFO_FLAG_SCALAR 0x0001
98 #define NSTAT_SYSINFO_FLAG_DISTRIBUTION 0x0002
100 #define NSTAT_MAX_MSG_SIZE 4096
102 typedef struct nstat_sysinfo_counts
105 u_int32_t nstat_sysinfo_len
;
107 u_int8_t nstat_sysinfo_keyvals
[];
108 } __attribute__((packed
)) nstat_sysinfo_counts
;
112 NSTAT_SYSINFO_KEY_MBUF_256B_TOTAL
= 1
113 ,NSTAT_SYSINFO_KEY_MBUF_2KB_TOTAL
= 2
114 ,NSTAT_SYSINFO_KEY_MBUF_4KB_TOTAL
= 3
115 ,NSTAT_SYSINFO_KEY_SOCK_MBCNT
= 4
116 ,NSTAT_SYSINFO_KEY_SOCK_ATMBLIMIT
= 5
117 ,NSTAT_SYSINFO_KEY_IPV4_AVGRTT
= 6
118 ,NSTAT_SYSINFO_KEY_IPV6_AVGRTT
= 7
119 ,NSTAT_SYSINFO_KEY_SEND_PLR
= 8
120 ,NSTAT_SYSINFO_KEY_RECV_PLR
= 9
121 ,NSTAT_SYSINFO_KEY_SEND_TLRTO
= 10
122 ,NSTAT_SYSINFO_KEY_SEND_REORDERRATE
= 11
123 ,NSTAT_SYSINFO_CONNECTION_ATTEMPTS
= 12
124 ,NSTAT_SYSINFO_CONNECTION_ACCEPTS
= 13
125 ,NSTAT_SYSINFO_ECN_CLIENT_SETUP
= 14
126 ,NSTAT_SYSINFO_ECN_SERVER_SETUP
= 15
127 ,NSTAT_SYSINFO_ECN_CLIENT_SUCCESS
= 16
128 ,NSTAT_SYSINFO_ECN_SERVER_SUCCESS
= 17
129 ,NSTAT_SYSINFO_ECN_NOT_SUPPORTED
= 18
130 ,NSTAT_SYSINFO_ECN_LOST_SYN
= 19
131 ,NSTAT_SYSINFO_ECN_LOST_SYNACK
= 20
132 ,NSTAT_SYSINFO_ECN_RECV_CE
= 21
133 ,NSTAT_SYSINFO_ECN_RECV_ECE
= 22
134 ,NSTAT_SYSINFO_ECN_SENT_ECE
= 23
135 ,NSTAT_SYSINFO_ECN_CONN_RECV_CE
= 24
136 ,NSTAT_SYSINFO_ECN_CONN_PLNOCE
= 25
137 ,NSTAT_SYSINFO_ECN_CONN_PL_CE
= 26
138 ,NSTAT_SYSINFO_ECN_CONN_NOPL_CE
= 27
139 ,NSTAT_SYSINFO_MBUF_16KB_TOTAL
= 28
140 ,NSTAT_SYSINFO_ECN_CLIENT_ENABLED
= 29
141 ,NSTAT_SYSINFO_ECN_SERVER_ENABLED
= 30
142 ,NSTAT_SYSINFO_ECN_CONN_RECV_ECE
= 31
143 ,NSTAT_SYSINFO_MBUF_MEM_RELEASED
= 32
144 ,NSTAT_SYSINFO_MBUF_DRAIN_CNT
= 33
145 ,NSTAT_SYSINFO_TFO_SYN_DATA_RCV
= 34
146 ,NSTAT_SYSINFO_TFO_COOKIE_REQ_RCV
= 35
147 ,NSTAT_SYSINFO_TFO_COOKIE_SENT
= 36
148 ,NSTAT_SYSINFO_TFO_COOKIE_INVALID
= 37
149 ,NSTAT_SYSINFO_TFO_COOKIE_REQ
= 38
150 ,NSTAT_SYSINFO_TFO_COOKIE_RCV
= 39
151 ,NSTAT_SYSINFO_TFO_SYN_DATA_SENT
= 40
152 ,NSTAT_SYSINFO_TFO_SYN_DATA_ACKED
= 41
153 ,NSTAT_SYSINFO_TFO_SYN_LOSS
= 42
154 ,NSTAT_SYSINFO_TFO_BLACKHOLE
= 43
155 ,NSTAT_SYSINFO_ECN_FALLBACK_SYNLOSS
= 44
156 ,NSTAT_SYSINFO_ECN_FALLBACK_REORDER
= 45
157 ,NSTAT_SYSINFO_ECN_FALLBACK_CE
= 46
158 ,NSTAT_SYSINFO_ECN_IFNET_TYPE
= 47
159 ,NSTAT_SYSINFO_ECN_IFNET_PROTO
= 48
160 ,NSTAT_SYSINFO_ECN_IFNET_CLIENT_SETUP
= 49
161 ,NSTAT_SYSINFO_ECN_IFNET_SERVER_SETUP
= 50
162 ,NSTAT_SYSINFO_ECN_IFNET_CLIENT_SUCCESS
= 51
163 ,NSTAT_SYSINFO_ECN_IFNET_SERVER_SUCCESS
= 52
164 ,NSTAT_SYSINFO_ECN_IFNET_PEER_NOSUPPORT
= 53
165 ,NSTAT_SYSINFO_ECN_IFNET_SYN_LOST
= 54
166 ,NSTAT_SYSINFO_ECN_IFNET_SYNACK_LOST
= 55
167 ,NSTAT_SYSINFO_ECN_IFNET_RECV_CE
= 56
168 ,NSTAT_SYSINFO_ECN_IFNET_RECV_ECE
= 57
169 ,NSTAT_SYSINFO_ECN_IFNET_SENT_ECE
= 58
170 ,NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_CE
= 59
171 ,NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_ECE
= 60
172 ,NSTAT_SYSINFO_ECN_IFNET_CONN_PLNOCE
= 61
173 ,NSTAT_SYSINFO_ECN_IFNET_CONN_PLCE
= 62
174 ,NSTAT_SYSINFO_ECN_IFNET_CONN_NOPLCE
= 63
175 ,NSTAT_SYSINFO_ECN_IFNET_FALLBACK_SYNLOSS
= 64
176 ,NSTAT_SYSINFO_ECN_IFNET_FALLBACK_REORDER
= 65
177 ,NSTAT_SYSINFO_ECN_IFNET_FALLBACK_CE
= 66
178 ,NSTAT_SYSINFO_ECN_IFNET_ON_RTT_AVG
= 67
179 ,NSTAT_SYSINFO_ECN_IFNET_ON_RTT_VAR
= 68
180 ,NSTAT_SYSINFO_ECN_IFNET_ON_OOPERCENT
= 69
181 ,NSTAT_SYSINFO_ECN_IFNET_ON_SACK_EPISODE
= 70
182 ,NSTAT_SYSINFO_ECN_IFNET_ON_REORDER_PERCENT
= 71
183 ,NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_PERCENT
= 72
184 ,NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_DROP
= 73
185 ,NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_AVG
= 74
186 ,NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_VAR
= 75
187 ,NSTAT_SYSINFO_ECN_IFNET_OFF_OOPERCENT
= 76
188 ,NSTAT_SYSINFO_ECN_IFNET_OFF_SACK_EPISODE
= 77
189 ,NSTAT_SYSINFO_ECN_IFNET_OFF_REORDER_PERCENT
= 78
190 ,NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_PERCENT
= 79
191 ,NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_DROP
= 80
192 ,NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_TXPKTS
= 81
193 ,NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXMTPKTS
= 82
194 ,NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXPKTS
= 83
195 ,NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_OOPKTS
= 84
196 ,NSTAT_SYSINFO_ECN_IFNET_ON_DROP_RST
= 85
197 ,NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_TXPKTS
= 86
198 ,NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXMTPKTS
= 87
199 ,NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXPKTS
= 88
200 ,NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_OOPKTS
= 89
201 ,NSTAT_SYSINFO_ECN_IFNET_OFF_DROP_RST
= 90
202 ,NSTAT_SYSINFO_ECN_IFNET_TOTAL_CONN
= 91
203 ,NSTAT_SYSINFO_TFO_COOKIE_WRONG
= 92
204 ,NSTAT_SYSINFO_TFO_NO_COOKIE_RCV
= 93
205 ,NSTAT_SYSINFO_TFO_HEURISTICS_DISABLE
= 94
206 ,NSTAT_SYSINFO_TFO_SEND_BLACKHOLE
= 95
207 ,NSTAT_SYSINFO_KEY_SOCK_MBFLOOR
= 96
208 ,NSTAT_SYSINFO_IFNET_UNSENT_DATA
= 97
209 ,NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRST
= 98
210 ,NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRXMT
= 99
211 // NSTAT_SYSINFO_ENUM_VERSION must be updated any time a value is added
212 #define NSTAT_SYSINFO_ENUM_VERSION 20160715
215 #pragma mark -- Network Statistics Providers --
218 // Interface properties
220 #define NSTAT_IFNET_IS_UNKNOWN_TYPE 0x01
221 #define NSTAT_IFNET_IS_LOOPBACK 0x02
222 #define NSTAT_IFNET_IS_CELLULAR 0x04
223 #define NSTAT_IFNET_IS_WIFI 0x08
224 #define NSTAT_IFNET_IS_WIRED 0x10
225 #define NSTAT_IFNET_IS_AWDL 0x20
226 #define NSTAT_IFNET_IS_EXPENSIVE 0x40
227 #define NSTAT_IFNET_IS_VPN 0x80
228 #define NSTAT_IFNET_VIA_CELLFALLBACK 0x100
233 NSTAT_PROVIDER_NONE
= 0
234 ,NSTAT_PROVIDER_ROUTE
= 1
235 ,NSTAT_PROVIDER_TCP_KERNEL
= 2
236 ,NSTAT_PROVIDER_TCP_USERLAND
= 3
237 ,NSTAT_PROVIDER_UDP_KERNEL
= 4
238 ,NSTAT_PROVIDER_UDP_USERLAND
= 5
239 ,NSTAT_PROVIDER_IFNET
= 6
240 ,NSTAT_PROVIDER_SYSINFO
= 7
242 #define NSTAT_PROVIDER_LAST NSTAT_PROVIDER_SYSINFO
243 #define NSTAT_PROVIDER_COUNT (NSTAT_PROVIDER_LAST+1)
245 typedef struct nstat_route_add_param
249 struct sockaddr_in v4
;
250 struct sockaddr_in6 v6
;
254 struct sockaddr_in v4
;
255 struct sockaddr_in6 v6
;
258 } nstat_route_add_param
;
260 typedef struct nstat_tcp_add_param
264 struct sockaddr_in v4
;
265 struct sockaddr_in6 v6
;
269 struct sockaddr_in v4
;
270 struct sockaddr_in6 v6
;
272 } nstat_tcp_add_param
;
274 typedef struct nstat_tcp_descriptor
278 struct sockaddr_in v4
;
279 struct sockaddr_in6 v6
;
284 struct sockaddr_in v4
;
285 struct sockaddr_in6 v6
;
292 u_int32_t sndbufsize
;
293 u_int32_t sndbufused
;
294 u_int32_t rcvbufsize
;
295 u_int32_t rcvbufused
;
299 u_int32_t traffic_class
;
300 u_int32_t traffic_mgt_flags
;
312 struct tcp_conn_status connstatus
;
313 uint16_t ifnet_properties
__attribute__((aligned(4)));
314 } nstat_tcp_descriptor
;
316 typedef struct nstat_tcp_add_param nstat_udp_add_param
;
318 typedef struct nstat_udp_descriptor
322 struct sockaddr_in v4
;
323 struct sockaddr_in6 v6
;
328 struct sockaddr_in v4
;
329 struct sockaddr_in6 v6
;
334 u_int32_t rcvbufsize
;
335 u_int32_t rcvbufused
;
336 u_int32_t traffic_class
;
347 uint16_t ifnet_properties
;
348 } nstat_udp_descriptor
;
350 typedef struct nstat_route_descriptor
354 u_int64_t gateway_id
;
358 struct sockaddr_in v4
;
359 struct sockaddr_in6 v6
;
365 struct sockaddr_in v4
;
366 struct sockaddr_in6 v6
;
372 struct sockaddr_in v4
;
373 struct sockaddr_in6 v6
;
380 } nstat_route_descriptor
;
382 typedef struct nstat_ifnet_add_param
386 } nstat_ifnet_add_param
;
388 typedef struct nstat_ifnet_desc_cellular_status
390 u_int32_t valid_bitmask
; /* indicates which fields are valid */
391 #define NSTAT_IFNET_DESC_CELL_LINK_QUALITY_METRIC_VALID 0x1
392 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_BANDWIDTH_VALID 0x2
393 #define NSTAT_IFNET_DESC_CELL_UL_MAX_BANDWIDTH_VALID 0x4
394 #define NSTAT_IFNET_DESC_CELL_UL_MIN_LATENCY_VALID 0x8
395 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_LATENCY_VALID 0x10
396 #define NSTAT_IFNET_DESC_CELL_UL_MAX_LATENCY_VALID 0x20
397 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_VALID 0x40
398 #define NSTAT_IFNET_DESC_CELL_UL_BYTES_LOST_VALID 0x80
399 #define NSTAT_IFNET_DESC_CELL_UL_MIN_QUEUE_SIZE_VALID 0x100
400 #define NSTAT_IFNET_DESC_CELL_UL_AVG_QUEUE_SIZE_VALID 0x200
401 #define NSTAT_IFNET_DESC_CELL_UL_MAX_QUEUE_SIZE_VALID 0x400
402 #define NSTAT_IFNET_DESC_CELL_DL_EFFECTIVE_BANDWIDTH_VALID 0x800
403 #define NSTAT_IFNET_DESC_CELL_DL_MAX_BANDWIDTH_VALID 0x1000
404 #define NSTAT_IFNET_DESC_CELL_CONFIG_INACTIVITY_TIME_VALID 0x2000
405 #define NSTAT_IFNET_DESC_CELL_CONFIG_BACKOFF_TIME_VALID 0x4000
406 #define NSTAT_IFNET_DESC_CELL_MSS_RECOMMENDED_VALID 0x8000
407 u_int32_t link_quality_metric
;
408 u_int32_t ul_effective_bandwidth
; /* Measured uplink bandwidth based on
409 current activity (bps) */
410 u_int32_t ul_max_bandwidth
; /* Maximum supported uplink bandwidth
412 u_int32_t ul_min_latency
; /* min expected uplink latency for first hop
414 u_int32_t ul_effective_latency
; /* current expected uplink latency for
416 u_int32_t ul_max_latency
; /* max expected uplink latency first hop
418 u_int32_t ul_retxt_level
; /* Retransmission metric */
419 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_NONE 1
420 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_LOW 2
421 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_MEDIUM 3
422 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_HIGH 4
424 u_int32_t ul_bytes_lost
; /* % of total bytes lost on uplink in Q10
426 u_int32_t ul_min_queue_size
; /* minimum bytes in queue */
427 u_int32_t ul_avg_queue_size
; /* average bytes in queue */
428 u_int32_t ul_max_queue_size
; /* maximum bytes in queue */
429 u_int32_t dl_effective_bandwidth
; /* Measured downlink bandwidth based
430 on current activity (bps) */
431 u_int32_t dl_max_bandwidth
; /* Maximum supported downlink bandwidth
433 u_int32_t config_inactivity_time
; /* ms */
434 u_int32_t config_backoff_time
; /* new connections backoff time in ms */
435 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_NONE 0x0
436 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_MEDIUM 0x1
437 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_LOW 0x2
438 u_int16_t mss_recommended
; /* recommended MSS */
439 } nstat_ifnet_desc_cellular_status
;
441 typedef struct nstat_ifnet_desc_wifi_status
{
442 u_int32_t valid_bitmask
;
443 #define NSTAT_IFNET_DESC_WIFI_LINK_QUALITY_METRIC_VALID 0x1
444 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID 0x2
445 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_BANDWIDTH_VALID 0x4
446 #define NSTAT_IFNET_DESC_WIFI_UL_MIN_LATENCY_VALID 0x8
447 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_LATENCY_VALID 0x10
448 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_LATENCY_VALID 0x20
449 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_VALID 0x40
450 #define NSTAT_IFNET_DESC_WIFI_UL_ERROR_RATE_VALID 0x80
451 #define NSTAT_IFNET_DESC_WIFI_UL_BYTES_LOST_VALID 0x100
452 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID 0x200
453 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_BANDWIDTH_VALID 0x400
454 #define NSTAT_IFNET_DESC_WIFI_DL_MIN_LATENCY_VALID 0x800
455 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_LATENCY_VALID 0x1000
456 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_LATENCY_VALID 0x2000
457 #define NSTAT_IFNET_DESC_WIFI_DL_ERROR_RATE_VALID 0x4000
458 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_VALID 0x8000
459 #define NSTAT_IFNET_DESC_WIFI_CONFIG_MULTICAST_RATE_VALID 0x10000
460 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_COUNT_VALID 0x20000
461 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_DURATION_VALID 0x40000
462 u_int32_t link_quality_metric
; /* link quality metric */
463 u_int32_t ul_effective_bandwidth
; /* Measured uplink bandwidth based on
464 current activity (bps) */
465 u_int32_t ul_max_bandwidth
; /* Maximum supported uplink bandwidth
467 u_int32_t ul_min_latency
; /* min expected uplink latency for first hop
469 u_int32_t ul_effective_latency
; /* current expected uplink latency for
471 u_int32_t ul_max_latency
; /* max expected uplink latency for first hop
473 u_int32_t ul_retxt_level
; /* Retransmission metric */
474 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_NONE 1
475 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_LOW 2
476 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_MEDIUM 3
477 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_HIGH 4
479 u_int32_t ul_bytes_lost
; /* % of total bytes lost on uplink in Q10
481 u_int32_t ul_error_rate
; /* % of bytes dropped on uplink after many
482 retransmissions in Q10 format */
483 u_int32_t dl_effective_bandwidth
; /* Measured downlink bandwidth based
484 on current activity (bps) */
485 u_int32_t dl_max_bandwidth
; /* Maximum supported downlink bandwidth
488 * The download latency values indicate the time AP may have to wait
489 * for the driver to receive the packet. These values give the range
490 * of expected latency mainly due to co-existence events and channel
491 * hopping where the interface becomes unavailable.
493 u_int32_t dl_min_latency
; /* min expected latency for first hop in ms */
494 u_int32_t dl_effective_latency
; /* current expected latency for first
496 u_int32_t dl_max_latency
; /* max expected latency for first hop in ms */
497 u_int32_t dl_error_rate
; /* % of CRC or other errors in Q10 format */
498 u_int32_t config_frequency
; /* 2.4 or 5 GHz */
499 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_2_4_GHZ 1
500 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_5_0_GHZ 2
501 u_int32_t config_multicast_rate
; /* bps */
502 u_int32_t scan_count
; /* scan count during the previous period */
503 u_int32_t scan_duration
; /* scan duration in ms */
504 } nstat_ifnet_desc_wifi_status
;
508 NSTAT_IFNET_DESC_LINK_STATUS_TYPE_NONE
= 0
509 ,NSTAT_IFNET_DESC_LINK_STATUS_TYPE_CELLULAR
= 1
510 ,NSTAT_IFNET_DESC_LINK_STATUS_TYPE_WIFI
= 2
513 typedef struct nstat_ifnet_desc_link_status
515 u_int32_t link_status_type
;
517 nstat_ifnet_desc_cellular_status cellular
;
518 nstat_ifnet_desc_wifi_status wifi
;
520 } nstat_ifnet_desc_link_status
;
523 #define IF_DESCSIZE 128
525 typedef struct nstat_ifnet_descriptor
527 char name
[IFNAMSIZ
+1];
531 char description
[IF_DESCSIZE
];
532 nstat_ifnet_desc_link_status link_status
;
533 } nstat_ifnet_descriptor
;
535 typedef struct nstat_sysinfo_descriptor
538 } nstat_sysinfo_descriptor
;
540 typedef struct nstat_sysinfo_add_param
542 /* To indicate which system level information should be collected */
544 } nstat_sysinfo_add_param
;
546 #define NSTAT_SYSINFO_MBUF_STATS 0x0001
547 #define NSTAT_SYSINFO_TCP_STATS 0x0002
548 #define NSTAT_SYSINFO_IFNET_ECN_STATS 0x0003
550 #pragma mark -- Network Statistics User Client --
552 #define NET_STAT_CONTROL_NAME "com.apple.network.statistics"
556 // generic response messages
557 NSTAT_MSG_TYPE_SUCCESS
= 0
558 ,NSTAT_MSG_TYPE_ERROR
= 1
561 ,NSTAT_MSG_TYPE_ADD_SRC
= 1001
562 ,NSTAT_MSG_TYPE_ADD_ALL_SRCS
= 1002
563 ,NSTAT_MSG_TYPE_REM_SRC
= 1003
564 ,NSTAT_MSG_TYPE_QUERY_SRC
= 1004
565 ,NSTAT_MSG_TYPE_GET_SRC_DESC
= 1005
566 ,NSTAT_MSG_TYPE_SET_FILTER
= 1006
567 ,NSTAT_MSG_TYPE_GET_UPDATE
= 1007
568 ,NSTAT_MSG_TYPE_SUBSCRIBE_SYSINFO
= 1008
570 // Responses/Notfications
571 ,NSTAT_MSG_TYPE_SRC_ADDED
= 10001
572 ,NSTAT_MSG_TYPE_SRC_REMOVED
= 10002
573 ,NSTAT_MSG_TYPE_SRC_DESC
= 10003
574 ,NSTAT_MSG_TYPE_SRC_COUNTS
= 10004
575 ,NSTAT_MSG_TYPE_SYSINFO_COUNTS
= 10005
576 ,NSTAT_MSG_TYPE_SRC_UPDATE
= 10006
581 NSTAT_SRC_REF_ALL
= 0xffffffffffffffffULL
582 ,NSTAT_SRC_REF_INVALID
= 0
585 /* Source-level filters */
588 NSTAT_FILTER_NOZEROBYTES
= 0x00000001
591 /* Provider-level filters */
594 NSTAT_FILTER_ACCEPT_UNKNOWN
= 0x00000001
595 ,NSTAT_FILTER_ACCEPT_LOOPBACK
= 0x00000002
596 ,NSTAT_FILTER_ACCEPT_CELLULAR
= 0x00000004
597 ,NSTAT_FILTER_ACCEPT_WIFI
= 0x00000008
598 ,NSTAT_FILTER_ACCEPT_WIRED
= 0x00000010
599 ,NSTAT_FILTER_ACCEPT_AWDL
= 0x00000020
600 ,NSTAT_FILTER_ACCEPT_EXPENSIVE
= 0x00000040
601 ,NSTAT_FILTER_ACCEPT_CELLFALLBACK
= 0x00000100
602 ,NSTAT_FILTER_IFNET_FLAGS
= 0x00000FFF
604 ,NSTAT_FILTER_TCP_NO_LISTENER
= 0x00001000
605 ,NSTAT_FILTER_TCP_ONLY_LISTENER
= 0x00002000
606 ,NSTAT_FILTER_TCP_INTERFACE_ATTACH
= 0x00004000
607 ,NSTAT_FILTER_TCP_FLAGS
= 0x0000F000
609 ,NSTAT_FILTER_UDP_INTERFACE_ATTACH
= 0x00010000
610 ,NSTAT_FILTER_UDP_FLAGS
= 0x000F0000
612 ,NSTAT_FILTER_SUPPRESS_SRC_ADDED
= 0x00100000
613 ,NSTAT_FILTER_REQUIRE_SRC_ADDED
= 0x00200000
614 ,NSTAT_FILTER_PROVIDER_NOZEROBYTES
= 0x00400000
616 ,NSTAT_FILTER_SPECIFIC_USER_BY_PID
= 0x01000000
617 ,NSTAT_FILTER_SPECIFIC_USER_BY_EPID
= 0x02000000
618 ,NSTAT_FILTER_SPECIFIC_USER_BY_UUID
= 0x04000000
619 ,NSTAT_FILTER_SPECIFIC_USER_BY_EUUID
= 0x08000000
620 ,NSTAT_FILTER_SPECIFIC_USER
= 0x0F000000
625 NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE
= 1 << 0,
626 NSTAT_MSG_HDR_FLAG_CONTINUATION
= 1 << 1,
627 NSTAT_MSG_HDR_FLAG_CLOSING
= 1 << 2,
630 typedef struct nstat_msg_hdr
638 typedef struct nstat_msg_error
641 u_int32_t error
; // errno error
644 typedef struct nstat_msg_add_src
647 nstat_provider_id_t provider
;
649 } nstat_msg_add_src_req
;
651 typedef struct nstat_msg_add_all_srcs
654 nstat_provider_id_t provider
;
656 nstat_event_flags_t events
;
659 } nstat_msg_add_all_srcs
;
661 typedef struct nstat_msg_src_added
664 nstat_provider_id_t provider
;
665 nstat_src_ref_t srcref
;
666 } nstat_msg_src_added
;
668 typedef struct nstat_msg_rem_src
671 nstat_src_ref_t srcref
;
672 } nstat_msg_rem_src_req
;
674 typedef struct nstat_msg_get_src_description
677 nstat_src_ref_t srcref
;
678 } nstat_msg_get_src_description
;
680 typedef struct nstat_msg_set_filter
683 nstat_src_ref_t srcref
;
685 } nstat_msg_set_filter
;
687 typedef struct nstat_msg_src_description
690 nstat_src_ref_t srcref
;
691 nstat_event_flags_t event_flags
;
692 nstat_provider_id_t provider
;
694 } nstat_msg_src_description
;
696 typedef struct nstat_msg_query_src
699 nstat_src_ref_t srcref
;
700 } nstat_msg_query_src_req
;
702 typedef struct nstat_msg_src_counts
705 nstat_src_ref_t srcref
;
706 nstat_event_flags_t event_flags
;
708 } nstat_msg_src_counts
;
710 typedef struct nstat_msg_src_update
713 nstat_src_ref_t srcref
;
714 nstat_event_flags_t event_flags
;
716 nstat_provider_id_t provider
;
718 } nstat_msg_src_update
;
720 typedef struct nstat_msg_src_removed
723 nstat_src_ref_t srcref
;
724 } nstat_msg_src_removed
;
726 typedef struct nstat_msg_sysinfo_counts
729 nstat_src_ref_t srcref
;
730 nstat_sysinfo_counts counts
;
731 } __attribute__((packed
)) nstat_msg_sysinfo_counts
;
735 #pragma mark -- Statitiscs about Network Statistics --
738 u_int32_t nstat_successmsgfailures
;
739 u_int32_t nstat_sendcountfailures
;
740 u_int32_t nstat_sysinfofailures
;
741 u_int32_t nstat_srcupatefailures
;
742 u_int32_t nstat_descriptionfailures
;
743 u_int32_t nstat_msgremovedfailures
;
744 u_int32_t nstat_srcaddedfailures
;
745 u_int32_t nstat_msgerrorfailures
;
746 u_int32_t nstat_copy_descriptor_failures
;
747 u_int32_t nstat_provider_counts_failures
;
748 u_int32_t nstat_control_send_description_failures
;
749 u_int32_t nstat_control_send_goodbye_failures
;
750 u_int32_t nstat_flush_accumulated_msgs_failures
;
751 u_int32_t nstat_accumulate_msg_failures
;
752 u_int32_t nstat_control_cleanup_source_failures
;
753 u_int32_t nstat_handle_msg_failures
;
758 #ifdef XNU_KERNEL_PRIVATE
759 #include <sys/mcache.h>
761 #pragma mark -- System Information Internal Support --
763 typedef struct nstat_sysinfo_mbuf_stats
765 u_int32_t total_256b
; /* Peak usage, 256B pool */
766 u_int32_t total_2kb
; /* Peak usage, 2KB pool */
767 u_int32_t total_4kb
; /* Peak usage, 4KB pool */
768 u_int32_t total_16kb
; /* Peak usage, 16KB pool */
769 u_int32_t sbmb_total
; /* Total mbufs in sock buffer pool */
770 u_int32_t sb_atmbuflimit
; /* Memory limit reached for socket buffer autoscaling */
771 u_int32_t draincnt
; /* Number of times mbuf pool has been drained under memory pressure */
772 u_int32_t memreleased
; /* Memory (bytes) released from mbuf pool to VM */
773 u_int32_t sbmb_floor
; /* Lowest mbufs in sock buffer pool */
774 } nstat_sysinfo_mbuf_stats
;
776 typedef struct nstat_sysinfo_tcp_stats
778 u_int32_t ipv4_avgrtt
; /* Average RTT for IPv4 */
779 u_int32_t ipv6_avgrtt
; /* Average RTT for IPv6 */
780 u_int32_t send_plr
; /* Average uplink packet loss rate */
781 u_int32_t recv_plr
; /* Average downlink packet loss rate */
782 u_int32_t send_tlrto_rate
; /* Average rxt timeout after tail loss */
783 u_int32_t send_reorder_rate
; /* Average packet reordering rate */
784 u_int32_t connection_attempts
; /* TCP client connection attempts */
785 u_int32_t connection_accepts
; /* TCP server connection accepts */
786 u_int32_t ecn_client_enabled
; /* Global setting for ECN client side */
787 u_int32_t ecn_server_enabled
; /* Global setting for ECN server side */
788 u_int32_t ecn_client_setup
; /* Attempts to setup TCP client connection with ECN */
789 u_int32_t ecn_server_setup
; /* Attempts to setup TCP server connection with ECN */
790 u_int32_t ecn_client_success
; /* Number of successful negotiations of ECN for a client connection */
791 u_int32_t ecn_server_success
; /* Number of successful negotiations of ECN for a server connection */
792 u_int32_t ecn_not_supported
; /* Number of falbacks to Non-ECN, no support from peer */
793 u_int32_t ecn_lost_syn
; /* Number of SYNs lost with ECN bits */
794 u_int32_t ecn_lost_synack
; /* Number of SYN-ACKs lost with ECN bits */
795 u_int32_t ecn_recv_ce
; /* Number of CEs received from network */
796 u_int32_t ecn_recv_ece
; /* Number of ECEs received from receiver */
797 u_int32_t ecn_sent_ece
; /* Number of ECEs sent in response to CE */
798 u_int32_t ecn_conn_recv_ce
; /* Number of connections using ECN received CE at least once */
799 u_int32_t ecn_conn_recv_ece
; /* Number of connections using ECN received ECE at least once */
800 u_int32_t ecn_conn_plnoce
; /* Number of connections using ECN seen packet loss but never received CE */
801 u_int32_t ecn_conn_pl_ce
; /* Number of connections using ECN seen packet loss and CE */
802 u_int32_t ecn_conn_nopl_ce
; /* Number of connections using ECN with no packet loss but received CE */
803 u_int32_t ecn_fallback_synloss
; /* Number of times we did fall back due to SYN-Loss */
804 u_int32_t ecn_fallback_reorder
; /* Number of times we fallback because we detected the PAWS-issue */
805 u_int32_t ecn_fallback_ce
; /* Number of times we fallback because we received too many CEs */
806 u_int32_t tfo_syn_data_rcv
; /* Number of SYN+data received with valid cookie */
807 u_int32_t tfo_cookie_req_rcv
;/* Number of TFO cookie-requests received */
808 u_int32_t tfo_cookie_sent
; /* Number of TFO-cookies offered to the client */
809 u_int32_t tfo_cookie_invalid
;/* Number of invalid TFO-cookies received */
810 u_int32_t tfo_cookie_req
; /* Number of SYNs with cookie request received*/
811 u_int32_t tfo_cookie_rcv
; /* Number of SYN/ACKs with Cookie received */
812 u_int32_t tfo_syn_data_sent
; /* Number of SYNs+data+cookie sent */
813 u_int32_t tfo_syn_data_acked
;/* Number of times our SYN+data has been acknowledged */
814 u_int32_t tfo_syn_loss
; /* Number of times SYN+TFO has been lost and we fallback */
815 u_int32_t tfo_blackhole
; /* Number of times SYN+TFO has been lost and we fallback */
816 u_int32_t tfo_cookie_wrong
; /* TFO-cookie we sent was wrong */
817 u_int32_t tfo_no_cookie_rcv
; /* We asked for a cookie but didn't get one */
818 u_int32_t tfo_heuristics_disable
; /* TFO got disabled due to heuristics */
819 u_int32_t tfo_sndblackhole
; /* TFO got blackholed in the sending direction */
820 } nstat_sysinfo_tcp_stats
;
823 NSTAT_IFNET_ECN_PROTO_IPV4
= 1
824 ,NSTAT_IFNET_ECN_PROTO_IPV6
828 NSTAT_IFNET_ECN_TYPE_CELLULAR
= 1
829 ,NSTAT_IFNET_ECN_TYPE_WIFI
830 ,NSTAT_IFNET_ECN_TYPE_ETHERNET
833 typedef struct nstat_sysinfo_ifnet_ecn_stats
{
834 u_int32_t ifnet_proto
;
835 u_int32_t ifnet_type
;
836 struct if_tcp_ecn_stat ecn_stat
;
837 } nstat_sysinfo_ifnet_ecn_stats
;
839 typedef struct nstat_sysinfo_data
843 nstat_sysinfo_mbuf_stats mb_stats
;
844 nstat_sysinfo_tcp_stats tcp_stats
;
845 nstat_sysinfo_ifnet_ecn_stats ifnet_ecn_stats
;
847 uint32_t unsent_data_cnt
; /* Before sleeping */
848 } nstat_sysinfo_data
;
850 #pragma mark -- Generic Network Statistics Provider --
852 typedef void * nstat_provider_cookie_t
;
854 #pragma mark -- Route Statistics Gathering Functions --
859 NSTAT_TX_FLAG_RETRANSMIT
= 1
864 NSTAT_RX_FLAG_DUPLICATE
= 1,
865 NSTAT_RX_FLAG_OUT_OF_ORDER
= 2
868 // indicates whether or not collection of statistics is enabled
869 extern int nstat_collect
;
871 void nstat_init(void);
873 // Route collection routines
874 void nstat_route_connect_attempt(struct rtentry
*rte
);
875 void nstat_route_connect_success(struct rtentry
*rte
);
876 void nstat_route_tx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
877 void nstat_route_rx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
878 void nstat_route_rtt(struct rtentry
*rte
, u_int32_t rtt
, u_int32_t rtt_var
);
879 void nstat_route_detach(struct rtentry
*rte
);
883 void nstat_tcp_new_pcb(struct inpcb
*inp
);
884 void nstat_udp_new_pcb(struct inpcb
*inp
);
885 void nstat_route_new_entry(struct rtentry
*rt
);
886 void nstat_pcb_detach(struct inpcb
*inp
);
887 void nstat_pcb_cache(struct inpcb
*inp
);
888 void nstat_pcb_invalidate_cache(struct inpcb
*inp
);
891 void nstat_ifnet_threshold_reached(unsigned int ifindex
);
893 void nstat_sysinfo_send_data(struct nstat_sysinfo_data
*);
895 // Userland stats reporting
897 // Each side, NetworkStatistics and the kernel provider for userland,
898 // pass opaque references.
899 typedef void *userland_stats_provider_context
;
900 typedef void *nstat_userland_context
;
902 // When things have been set up, Netstats can request a refresh of its data.
903 typedef bool (userland_stats_request_vals_fn
)(userland_stats_provider_context
*ctx
,
904 nstat_counts
*countsp
,
907 // Things get started with a call to netstats to say that there’s a new connection:
908 nstat_userland_context
ntstat_userland_stats_open(userland_stats_provider_context
*ctx
,
910 u_int64_t properties
,
911 userland_stats_request_vals_fn req_fn
);
913 void ntstat_userland_stats_close(nstat_userland_context nstat_ctx
);
916 // There may be other occasions where the stats have changed and NECP should push the new values.
917 // This is provisional, ahead of full implementation.
920 USERLAND_STATS_WILL_UPDATE
,
921 USERLAND_STATS_DID_UPDATE
922 } userland_stats_event_t
;
924 void ntstat_userland_stats_event(nstat_userland_context nstat_ctx
, userland_stats_event_t event
);
927 // locked_add_64 uses atomic operations on 32bit so the 64bit
928 // value can be properly read. The values are only ever incremented
929 // while under the socket lock, so on 64bit we don't actually need
930 // atomic operations to increment.
931 #if defined(__LP64__)
932 #define locked_add_64(__addr, __count) do { \
933 *(__addr) += (__count); \
936 #define locked_add_64(__addr, __count) do { \
937 atomic_add_64((__addr), (__count)); \
941 #endif /* XNU_KERNEL_PRIVATE */
943 #endif /* __NTSTAT_H__ */