xref: /xnu-10002.1.13/bsd/net/ntstat.h (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1 /*
2  * Copyright (c) 2010-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #ifndef __NTSTAT_H__
29 #define __NTSTAT_H__
30 #include <stdbool.h>
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #include <net/if_var.h>
34 #include <net/net_api_stats.h>
35 #include <netinet/in_stat.h>
36 #include <netinet/tcp.h>
37 
38 #ifdef PRIVATE
39 #pragma mark -- Common Data Structures --
40 
41 #define __NSTAT_REVISION__      9
42 
43 typedef u_int32_t       nstat_provider_id_t;
44 typedef u_int64_t       nstat_src_ref_t;
45 typedef u_int64_t       nstat_event_flags_t;
46 
47 // The following event definitions are very provisional..
48 enum{
49 	NSTAT_EVENT_SRC_ADDED                    = 0x00000001
50 	, NSTAT_EVENT_SRC_REMOVED                = 0x00000002
51 	, NSTAT_EVENT_SRC_QUERIED                = 0x00000004
52 	, NSTAT_EVENT_SRC_QUERIED_ALL            = 0x00000008
53 	, NSTAT_EVENT_SRC_WILL_CHANGE_STATE      = 0x00000010
54 	, NSTAT_EVENT_SRC_DID_CHANGE_STATE       = 0x00000020
55 	, NSTAT_EVENT_SRC_WILL_CHANGE_OWNER      = 0x00000040
56 	, NSTAT_EVENT_SRC_DID_CHANGE_OWNER       = 0x00000080
57 	, NSTAT_EVENT_SRC_WILL_CHANGE_PROPERTY   = 0x00000100
58 	, NSTAT_EVENT_SRC_DID_CHANGE_PROPERTY    = 0x00000200
59 	, NSTAT_EVENT_SRC_ENTER_CELLFALLBACK     = 0x00000400
60 	, NSTAT_EVENT_SRC_EXIT_CELLFALLBACK      = 0x00000800
61 	, NSTAT_EVENT_SRC_FLOW_STATE_LISTEN      = 0x00001000
62 	, NSTAT_EVENT_SRC_FLOW_STATE_OUTBOUND    = 0x00002000
63 	, NSTAT_EVENT_SRC_FLOW_UUID_ASSIGNED     = 0x00004000
64 	, NSTAT_EVENT_SRC_FLOW_UUID_CHANGED      = 0x00008000
65 #if (DEBUG || DEVELOPMENT)
66 	, NSTAT_EVENT_SRC_RESERVED_1             = 0x00010000
67 	, NSTAT_EVENT_SRC_RESERVED_2             = 0x00020000
68 #endif /* (DEBUG || DEVELOPMENT) */
69 };
70 
71 typedef struct nstat_counts {
72 	/* Counters */
73 	u_int64_t       nstat_rxpackets __attribute__((aligned(sizeof(u_int64_t))));
74 	u_int64_t       nstat_rxbytes   __attribute__((aligned(sizeof(u_int64_t))));
75 	u_int64_t       nstat_txpackets __attribute__((aligned(sizeof(u_int64_t))));
76 	u_int64_t       nstat_txbytes   __attribute__((aligned(sizeof(u_int64_t))));
77 
78 	u_int64_t       nstat_cell_rxbytes      __attribute__((aligned(sizeof(u_int64_t))));
79 	u_int64_t       nstat_cell_txbytes      __attribute__((aligned(sizeof(u_int64_t))));
80 	u_int64_t       nstat_wifi_rxbytes      __attribute__((aligned(sizeof(u_int64_t))));
81 	u_int64_t       nstat_wifi_txbytes      __attribute__((aligned(sizeof(u_int64_t))));
82 	u_int64_t       nstat_wired_rxbytes     __attribute__((aligned(sizeof(u_int64_t))));
83 	u_int64_t       nstat_wired_txbytes     __attribute__((aligned(sizeof(u_int64_t))));
84 
85 	u_int32_t       nstat_rxduplicatebytes;
86 	u_int32_t       nstat_rxoutoforderbytes;
87 	u_int32_t       nstat_txretransmit;
88 
89 	u_int32_t       nstat_connectattempts;
90 	u_int32_t       nstat_connectsuccesses;
91 
92 	u_int32_t       nstat_min_rtt;
93 	u_int32_t       nstat_avg_rtt;
94 	u_int32_t       nstat_var_rtt;
95 } nstat_counts;
96 
97 #define NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE     24
98 typedef struct nstat_sysinfo_keyval {
99 	u_int32_t       nstat_sysinfo_key;
100 	u_int32_t       nstat_sysinfo_flags;
101 	union {
102 		int64_t nstat_sysinfo_scalar;
103 		double  nstat_sysinfo_distribution;
104 		u_int8_t nstat_sysinfo_string[NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE];
105 	} u;
106 	u_int32_t       nstat_sysinfo_valsize;
107 	u_int8_t        reserved[4];
108 }  nstat_sysinfo_keyval;
109 
110 #define NSTAT_SYSINFO_FLAG_SCALAR       0x0001
111 #define NSTAT_SYSINFO_FLAG_DISTRIBUTION 0x0002
112 #define NSTAT_SYSINFO_FLAG_STRING       0x0004
113 
114 #define NSTAT_MAX_MSG_SIZE      4096
115 
116 typedef struct nstat_sysinfo_counts {
117 	/* Counters */
118 	u_int32_t       nstat_sysinfo_len;
119 	u_int32_t       pad;
120 	u_int8_t        nstat_sysinfo_keyvals[];
121 }  nstat_sysinfo_counts;
122 
123 enum{
124 	NSTAT_SYSINFO_KEY_MBUF_256B_TOTAL        = 1
125 	, NSTAT_SYSINFO_KEY_MBUF_2KB_TOTAL       = 2
126 	, NSTAT_SYSINFO_KEY_MBUF_4KB_TOTAL       = 3
127 	, NSTAT_SYSINFO_KEY_SOCK_MBCNT           = 4
128 	, NSTAT_SYSINFO_KEY_SOCK_ATMBLIMIT       = 5
129 	, NSTAT_SYSINFO_KEY_IPV4_AVGRTT          = 6
130 	, NSTAT_SYSINFO_KEY_IPV6_AVGRTT          = 7
131 	, NSTAT_SYSINFO_KEY_SEND_PLR             = 8
132 	, NSTAT_SYSINFO_KEY_RECV_PLR             = 9
133 	, NSTAT_SYSINFO_KEY_SEND_TLRTO           = 10
134 	, NSTAT_SYSINFO_KEY_SEND_REORDERRATE     = 11
135 	, NSTAT_SYSINFO_CONNECTION_ATTEMPTS      = 12
136 	, NSTAT_SYSINFO_CONNECTION_ACCEPTS       = 13
137 	, NSTAT_SYSINFO_ECN_CLIENT_SETUP         = 14
138 	, NSTAT_SYSINFO_ECN_SERVER_SETUP         = 15
139 	, NSTAT_SYSINFO_ECN_CLIENT_SUCCESS       = 16
140 	, NSTAT_SYSINFO_ECN_SERVER_SUCCESS       = 17
141 	, NSTAT_SYSINFO_ECN_NOT_SUPPORTED        = 18
142 	, NSTAT_SYSINFO_ECN_LOST_SYN             = 19
143 	, NSTAT_SYSINFO_ECN_LOST_SYNACK          = 20
144 	, NSTAT_SYSINFO_ECN_RECV_CE              = 21
145 	, NSTAT_SYSINFO_ECN_RECV_ECE             = 22
146 	, NSTAT_SYSINFO_ECN_SENT_ECE             = 23
147 	, NSTAT_SYSINFO_ECN_CONN_RECV_CE         = 24
148 	, NSTAT_SYSINFO_ECN_CONN_PLNOCE          = 25
149 	, NSTAT_SYSINFO_ECN_CONN_PL_CE           = 26
150 	, NSTAT_SYSINFO_ECN_CONN_NOPL_CE         = 27
151 	, NSTAT_SYSINFO_MBUF_16KB_TOTAL          = 28
152 	, NSTAT_SYSINFO_ECN_CLIENT_ENABLED       = 29
153 	, NSTAT_SYSINFO_ECN_SERVER_ENABLED       = 30
154 	, NSTAT_SYSINFO_ECN_CONN_RECV_ECE        = 31
155 	, NSTAT_SYSINFO_MBUF_MEM_RELEASED        = 32
156 	, NSTAT_SYSINFO_MBUF_DRAIN_CNT           = 33
157 	, NSTAT_SYSINFO_TFO_SYN_DATA_RCV         = 34
158 	, NSTAT_SYSINFO_TFO_COOKIE_REQ_RCV       = 35
159 	, NSTAT_SYSINFO_TFO_COOKIE_SENT          = 36
160 	, NSTAT_SYSINFO_TFO_COOKIE_INVALID       = 37
161 	, NSTAT_SYSINFO_TFO_COOKIE_REQ           = 38
162 	, NSTAT_SYSINFO_TFO_COOKIE_RCV           = 39
163 	, NSTAT_SYSINFO_TFO_SYN_DATA_SENT        = 40
164 	, NSTAT_SYSINFO_TFO_SYN_DATA_ACKED       = 41
165 	, NSTAT_SYSINFO_TFO_SYN_LOSS             = 42
166 	, NSTAT_SYSINFO_TFO_BLACKHOLE            = 43
167 	, NSTAT_SYSINFO_ECN_FALLBACK_SYNLOSS     = 44
168 	, NSTAT_SYSINFO_ECN_FALLBACK_REORDER     = 45
169 	, NSTAT_SYSINFO_ECN_FALLBACK_CE          = 46
170 	, NSTAT_SYSINFO_ECN_IFNET_TYPE           = 47
171 	, NSTAT_SYSINFO_ECN_IFNET_PROTO          = 48
172 	, NSTAT_SYSINFO_ECN_IFNET_CLIENT_SETUP   = 49
173 	, NSTAT_SYSINFO_ECN_IFNET_SERVER_SETUP   = 50
174 	, NSTAT_SYSINFO_ECN_IFNET_CLIENT_SUCCESS = 51
175 	, NSTAT_SYSINFO_ECN_IFNET_SERVER_SUCCESS = 52
176 	, NSTAT_SYSINFO_ECN_IFNET_PEER_NOSUPPORT = 53
177 	, NSTAT_SYSINFO_ECN_IFNET_SYN_LOST       = 54
178 	, NSTAT_SYSINFO_ECN_IFNET_SYNACK_LOST    = 55
179 	, NSTAT_SYSINFO_ECN_IFNET_RECV_CE        = 56
180 	, NSTAT_SYSINFO_ECN_IFNET_RECV_ECE       = 57
181 	, NSTAT_SYSINFO_ECN_IFNET_SENT_ECE       = 58
182 	, NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_CE   = 59
183 	, NSTAT_SYSINFO_ECN_IFNET_CONN_RECV_ECE  = 60
184 	, NSTAT_SYSINFO_ECN_IFNET_CONN_PLNOCE    = 61
185 	, NSTAT_SYSINFO_ECN_IFNET_CONN_PLCE      = 62
186 	, NSTAT_SYSINFO_ECN_IFNET_CONN_NOPLCE    = 63
187 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_SYNLOSS = 64
188 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_REORDER = 65
189 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_CE    = 66
190 	, NSTAT_SYSINFO_ECN_IFNET_ON_RTT_AVG     = 67
191 	, NSTAT_SYSINFO_ECN_IFNET_ON_RTT_VAR     = 68
192 	, NSTAT_SYSINFO_ECN_IFNET_ON_OOPERCENT   = 69
193 	, NSTAT_SYSINFO_ECN_IFNET_ON_SACK_EPISODE = 70
194 	, NSTAT_SYSINFO_ECN_IFNET_ON_REORDER_PERCENT = 71
195 	, NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_PERCENT = 72
196 	, NSTAT_SYSINFO_ECN_IFNET_ON_RXMIT_DROP  = 73
197 	, NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_AVG    = 74
198 	, NSTAT_SYSINFO_ECN_IFNET_OFF_RTT_VAR    = 75
199 	, NSTAT_SYSINFO_ECN_IFNET_OFF_OOPERCENT  = 76
200 	, NSTAT_SYSINFO_ECN_IFNET_OFF_SACK_EPISODE = 77
201 	, NSTAT_SYSINFO_ECN_IFNET_OFF_REORDER_PERCENT = 78
202 	, NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_PERCENT = 79
203 	, NSTAT_SYSINFO_ECN_IFNET_OFF_RXMIT_DROP = 80
204 	, NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_TXPKTS = 81
205 	, NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXMTPKTS = 82
206 	, NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_RXPKTS = 83
207 	, NSTAT_SYSINFO_ECN_IFNET_ON_TOTAL_OOPKTS = 84
208 	, NSTAT_SYSINFO_ECN_IFNET_ON_DROP_RST = 85
209 	, NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_TXPKTS = 86
210 	, NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXMTPKTS = 87
211 	, NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_RXPKTS = 88
212 	, NSTAT_SYSINFO_ECN_IFNET_OFF_TOTAL_OOPKTS = 89
213 	, NSTAT_SYSINFO_ECN_IFNET_OFF_DROP_RST = 90
214 	, NSTAT_SYSINFO_ECN_IFNET_TOTAL_CONN = 91
215 	, NSTAT_SYSINFO_TFO_COOKIE_WRONG = 92
216 	, NSTAT_SYSINFO_TFO_NO_COOKIE_RCV = 93
217 	, NSTAT_SYSINFO_TFO_HEURISTICS_DISABLE = 94
218 	, NSTAT_SYSINFO_TFO_SEND_BLACKHOLE = 95
219 	, NSTAT_SYSINFO_KEY_SOCK_MBFLOOR = 96
220 	, NSTAT_SYSINFO_IFNET_UNSENT_DATA = 97
221 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRST = 98
222 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_DROPRXMT = 99
223 	, NSTAT_SYSINFO_LIM_IFNET_SIGNATURE = 100
224 	, NSTAT_SYSINFO_LIM_IFNET_DL_MAX_BANDWIDTH = 101
225 	, NSTAT_SYSINFO_LIM_IFNET_UL_MAX_BANDWIDTH = 102
226 	, NSTAT_SYSINFO_LIM_IFNET_PACKET_LOSS_PERCENT = 103
227 	, NSTAT_SYSINFO_LIM_IFNET_PACKET_OOO_PERCENT = 104
228 	, NSTAT_SYSINFO_LIM_IFNET_RTT_VARIANCE = 105
229 	, NSTAT_SYSINFO_LIM_IFNET_RTT_MIN = 106
230 	, NSTAT_SYSINFO_LIM_IFNET_RTT_AVG = 107
231 	, NSTAT_SYSINFO_LIM_IFNET_CONN_TIMEOUT_PERCENT = 108
232 	, NSTAT_SYSINFO_LIM_IFNET_DL_DETECTED = 109
233 	, NSTAT_SYSINFO_LIM_IFNET_UL_DETECTED = 110
234 	, NSTAT_SYSINFO_LIM_IFNET_TYPE = 111
235 
236 	, NSTAT_SYSINFO_API_IF_FLTR_ATTACH = 112
237 	, NSTAT_SYSINFO_API_IF_FLTR_ATTACH_OS = 113
238 	, NSTAT_SYSINFO_API_IP_FLTR_ADD = 114
239 	, NSTAT_SYSINFO_API_IP_FLTR_ADD_OS = 115
240 	, NSTAT_SYSINFO_API_SOCK_FLTR_ATTACH = 116
241 	, NSTAT_SYSINFO_API_SOCK_FLTR_ATTACH_OS = 117
242 
243 	, NSTAT_SYSINFO_API_SOCK_ALLOC_TOTAL = 118
244 	, NSTAT_SYSINFO_API_SOCK_ALLOC_KERNEL = 119
245 	, NSTAT_SYSINFO_API_SOCK_ALLOC_KERNEL_OS = 120
246 	, NSTAT_SYSINFO_API_SOCK_NECP_CLIENTUUID = 121
247 
248 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_LOCAL = 122
249 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_ROUTE = 123
250 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_INET = 124
251 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_INET6 = 125
252 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_SYSTEM = 126
253 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_MULTIPATH = 127
254 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_KEY = 128
255 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_NDRV = 129
256 	, NSTAT_SYSINFO_API_SOCK_DOMAIN_OTHER = 130
257 
258 	, NSTAT_SYSINFO_API_SOCK_INET_STREAM= 131
259 	, NSTAT_SYSINFO_API_SOCK_INET_DGRAM = 132
260 	, NSTAT_SYSINFO_API_SOCK_INET_DGRAM_CONNECTED = 133
261 	, NSTAT_SYSINFO_API_SOCK_INET_DGRAM_DNS = 134
262 	, NSTAT_SYSINFO_API_SOCK_INET_DGRAM_NO_DATA = 135
263 
264 	, NSTAT_SYSINFO_API_SOCK_INET6_STREAM= 136
265 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM = 137
266 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_CONNECTED = 138
267 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_DNS = 139
268 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_NO_DATA = 140
269 
270 	, NSTAT_SYSINFO_API_SOCK_INET_MCAST_JOIN = 141
271 	, NSTAT_SYSINFO_API_SOCK_INET_MCAST_JOIN_OS = 142
272 
273 	, NSTAT_SYSINFO_API_SOCK_INET6_STREAM_EXTHDR_IN = 143
274 	, NSTAT_SYSINFO_API_SOCK_INET6_STREAM_EXTHDR_OUT = 144
275 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_EXTHDR_IN = 145
276 	, NSTAT_SYSINFO_API_SOCK_INET6_DGRAM_EXTHDR_OUT = 146
277 
278 	, NSTAT_SYSINFO_API_NEXUS_FLOW_INET_STREAM = 147
279 	, NSTAT_SYSINFO_API_NEXUS_FLOW_INET_DATAGRAM = 148
280 
281 	, NSTAT_SYSINFO_API_NEXUS_FLOW_INET6_STREAM = 149
282 	, NSTAT_SYSINFO_API_NEXUS_FLOW_INET6_DATAGRAM = 150
283 
284 	, NSTAT_SYSINFO_API_IFNET_ALLOC = 151
285 	, NSTAT_SYSINFO_API_IFNET_ALLOC_OS = 152
286 
287 	, NSTAT_SYSINFO_API_PF_ADDRULE = 153
288 	, NSTAT_SYSINFO_API_PF_ADDRULE_OS = 154
289 
290 	, NSTAT_SYSINFO_API_VMNET_START = 155
291 
292 	, NSTAT_SYSINFO_API_IF_NETAGENT_ENABLED = 156
293 
294 	, NSTAT_SYSINFO_API_REPORT_INTERVAL = 157
295 
296 	, NSTAT_SYSINFO_MPTCP_HANDOVER_ATTEMPT = 158
297 	, NSTAT_SYSINFO_MPTCP_INTERACTIVE_ATTEMPT = 159
298 	, NSTAT_SYSINFO_MPTCP_AGGREGATE_ATTEMPT = 160
299 	, NSTAT_SYSINFO_MPTCP_FP_HANDOVER_ATTEMPT = 161 /* _FP_ stands for first-party */
300 	, NSTAT_SYSINFO_MPTCP_FP_INTERACTIVE_ATTEMPT = 162
301 	, NSTAT_SYSINFO_MPTCP_FP_AGGREGATE_ATTEMPT = 163
302 	, NSTAT_SYSINFO_MPTCP_HEURISTIC_FALLBACK = 164
303 	, NSTAT_SYSINFO_MPTCP_FP_HEURISTIC_FALLBACK = 165
304 	, NSTAT_SYSINFO_MPTCP_HANDOVER_SUCCESS_WIFI = 166
305 	, NSTAT_SYSINFO_MPTCP_HANDOVER_SUCCESS_CELL = 167
306 	, NSTAT_SYSINFO_MPTCP_INTERACTIVE_SUCCESS = 168
307 	, NSTAT_SYSINFO_MPTCP_AGGREGATE_SUCCESS = 169
308 	, NSTAT_SYSINFO_MPTCP_FP_HANDOVER_SUCCESS_WIFI = 170
309 	, NSTAT_SYSINFO_MPTCP_FP_HANDOVER_SUCCESS_CELL = 171
310 	, NSTAT_SYSINFO_MPTCP_FP_INTERACTIVE_SUCCESS = 172
311 	, NSTAT_SYSINFO_MPTCP_FP_AGGREGATE_SUCCESS = 173
312 	, NSTAT_SYSINFO_MPTCP_HANDOVER_CELL_FROM_WIFI = 174
313 	, NSTAT_SYSINFO_MPTCP_HANDOVER_WIFI_FROM_CELL = 175
314 	, NSTAT_SYSINFO_MPTCP_INTERACTIVE_CELL_FROM_WIFI = 176
315 	, NSTAT_SYSINFO_MPTCP_HANDOVER_CELL_BYTES = 177
316 	, NSTAT_SYSINFO_MPTCP_INTERACTIVE_CELL_BYTES = 178
317 	, NSTAT_SYSINFO_MPTCP_AGGREGATE_CELL_BYTES = 179
318 	, NSTAT_SYSINFO_MPTCP_HANDOVER_ALL_BYTES = 180
319 	, NSTAT_SYSINFO_MPTCP_INTERACTIVE_ALL_BYTES = 181
320 	, NSTAT_SYSINFO_MPTCP_AGGREGATE_ALL_BYTES = 182
321 	, NSTAT_SYSINFO_MPTCP_BACK_TO_WIFI = 183
322 	, NSTAT_SYSINFO_MPTCP_WIFI_PROXY = 184
323 	, NSTAT_SYSINFO_MPTCP_CELL_PROXY = 185
324 	, NSTAT_SYSINFO_ECN_IFNET_FALLBACK_SYNRST = 186
325 	, NSTAT_SYSINFO_MPTCP_TRIGGERED_CELL = 187
326 
327 // NSTAT_SYSINFO_ENUM_VERSION must be updated any time a value is added
328 #define NSTAT_SYSINFO_ENUM_VERSION      20180416
329 };
330 
331 #define NSTAT_SYSINFO_API_FIRST NSTAT_SYSINFO_API_IF_FLTR_ATTACH
332 #define NSTAT_SYSINFO_API_LAST  NSTAT_SYSINFO_API_REPORT_INTERVAL
333 
334 #pragma mark -- Network Statistics Providers --
335 
336 
337 // Interface properties
338 
339 #define NSTAT_IFNET_IS_UNKNOWN_TYPE      0x0001
340 #define NSTAT_IFNET_IS_LOOPBACK          0x0002
341 #define NSTAT_IFNET_IS_CELLULAR          0x0004
342 #define NSTAT_IFNET_IS_WIFI              0x0008
343 #define NSTAT_IFNET_IS_WIRED             0x0010
344 #define NSTAT_IFNET_IS_AWDL              0x0020
345 #define NSTAT_IFNET_IS_EXPENSIVE         0x0040
346 #define NSTAT_IFNET_IS_VPN               0x0080 /* Reserved, currently unused */
347 #define NSTAT_IFNET_VIA_CELLFALLBACK     0x0100
348 #define NSTAT_IFNET_IS_COMPANIONLINK     0x0200
349 #define NSTAT_IFNET_IS_CONSTRAINED       0x0400
350 // The following local and non-local flags are set only if fully known
351 // They are mutually exclusive but there is no guarantee that one or the other will be set
352 #define NSTAT_IFNET_IS_LOCAL             0x0800
353 #define NSTAT_IFNET_IS_NON_LOCAL         0x1000
354 // Temporary properties of use for bringing up userland providers
355 #define NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE      0x2000
356 #define NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE 0x4000
357 #define NSTAT_IFNET_IS_LLW               0x8000
358 
359 // Note that many usages of interface properties are constrained to be within a 16 bit field.
360 // The following may be used if the properties are in a wider field, and is shorthand for WiFi and not AWDL or LLW..
361 #define NSTAT_IFNET_IS_WIFI_INFRA        0x10000
362 
363 // Not interface properties, but used for filtering in similar fashion
364 #define NSTAT_NECP_CONN_HAS_NET_ACCESS   0x01000000
365 
366 // Not interface properties but conveniently handled in the same flags word
367 #define NSTAT_SOURCE_IS_LISTENER         0x02000000
368 #define NSTAT_SOURCE_IS_INBOUND          0x04000000
369 #define NSTAT_SOURCE_IS_OUTBOUND         0x08000000
370 
371 
372 typedef enum {
373 	NSTAT_PROVIDER_NONE           = 0
374 	, NSTAT_PROVIDER_ROUTE        = 1
375 	, NSTAT_PROVIDER_TCP_KERNEL   = 2
376 	, NSTAT_PROVIDER_TCP_USERLAND = 3
377 	, NSTAT_PROVIDER_UDP_KERNEL   = 4
378 	, NSTAT_PROVIDER_UDP_USERLAND = 5
379 	, NSTAT_PROVIDER_IFNET        = 6
380 	, NSTAT_PROVIDER_SYSINFO      = 7
381 	, NSTAT_PROVIDER_QUIC_USERLAND = 8
382 	, NSTAT_PROVIDER_CONN_USERLAND = 9
383 	, NSTAT_PROVIDER_UDP_SUBFLOW   = 10
384 } nstat_provider_type_t;
385 #define NSTAT_PROVIDER_LAST NSTAT_PROVIDER_UDP_SUBFLOW
386 #define NSTAT_PROVIDER_COUNT (NSTAT_PROVIDER_LAST+1)
387 
388 typedef struct nstat_route_add_param {
389 	union{
390 		struct sockaddr_in      v4;
391 		struct sockaddr_in6     v6;
392 	} dst;
393 	union{
394 		struct sockaddr_in      v4;
395 		struct sockaddr_in6     v6;
396 	} mask;
397 	u_int32_t       ifindex;
398 } nstat_route_add_param;
399 
400 typedef struct nstat_tcp_add_param {
401 	union{
402 		struct sockaddr_in      v4;
403 		struct sockaddr_in6     v6;
404 	} local;
405 	union{
406 		struct sockaddr_in      v4;
407 		struct sockaddr_in6     v6;
408 	} remote;
409 } nstat_tcp_add_param;
410 
411 #define NSTAT_MAX_DOMAIN_NAME_LENGTH           256 /* As per RFC 2181 for full domain name */
412 #define NSTAT_MAX_DOMAIN_OWNER_LENGTH          256
413 #define NSTAT_MAX_DOMAIN_TRACKER_CONTEXT       256
414 #define NSTAT_MAX_DOMAIN_ATTR_BUNDLE_ID        256
415 
416 typedef struct nstat_domain_info {
417 	char            domain_name[NSTAT_MAX_DOMAIN_NAME_LENGTH];
418 	char            domain_owner[NSTAT_MAX_DOMAIN_OWNER_LENGTH];
419 	char            domain_tracker_ctxt[NSTAT_MAX_DOMAIN_TRACKER_CONTEXT];
420 	char            domain_attributed_bundle_id[NSTAT_MAX_DOMAIN_ATTR_BUNDLE_ID];
421 	union{
422 		struct sockaddr_in      v4;
423 		struct sockaddr_in6     v6;
424 	} remote;
425 	bool            is_tracker;
426 	bool            is_non_app_initiated;
427 	bool            is_silent;
428 	uint8_t         reserved[1];
429 } nstat_domain_info __attribute__((aligned(sizeof(u_int64_t))));
430 
431 typedef struct nstat_tcp_descriptor {
432 	u_int64_t       upid __attribute__((aligned(sizeof(u_int64_t))));
433 	u_int64_t       eupid __attribute__((aligned(sizeof(u_int64_t))));
434 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
435 	u_int64_t       timestamp __attribute__((aligned(sizeof(u_int64_t))));
436 
437 	u_int64_t       rx_transfer_size __attribute__((aligned(sizeof(u_int64_t))));
438 	u_int64_t       tx_transfer_size __attribute__((aligned(sizeof(u_int64_t))));
439 
440 	activity_bitmap_t activity_bitmap;
441 
442 	u_int32_t       ifindex;
443 	u_int32_t       state;
444 
445 	u_int32_t       sndbufsize;
446 	u_int32_t       sndbufused;
447 	u_int32_t       rcvbufsize;
448 	u_int32_t       rcvbufused;
449 	u_int32_t       txunacked;
450 	u_int32_t       txwindow;
451 	u_int32_t       txcwindow;
452 	u_int32_t       traffic_class;
453 	u_int32_t       traffic_mgt_flags;
454 
455 	u_int32_t       pid;
456 	u_int32_t       epid;
457 
458 	union{
459 		struct sockaddr_in      v4;
460 		struct sockaddr_in6     v6;
461 	} local;
462 
463 	union{
464 		struct sockaddr_in      v4;
465 		struct sockaddr_in6     v6;
466 	} remote;
467 
468 	char            cc_algo[16];
469 	char            pname[64];
470 
471 	uuid_t          uuid;
472 	uuid_t          euuid;
473 	uuid_t          vuuid;
474 	uuid_t          fuuid;
475 	uid_t           persona_id;
476 	uid_t           uid;
477 	union {
478 		struct tcp_conn_status connstatus;
479 		// On armv7k, tcp_conn_status is 1 byte instead of 4
480 		uint8_t                                 __pad_connstatus[4];
481 	};
482 	uint32_t        ifnet_properties        __attribute__((aligned(4)));
483 	uint8_t         fallback_mode;
484 	uint8_t         reserved[3];
485 } nstat_tcp_descriptor;
486 
487 typedef struct nstat_tcp_add_param      nstat_udp_add_param;
488 
489 typedef struct nstat_udp_descriptor {
490 	u_int64_t       upid __attribute__((aligned(sizeof(u_int64_t))));
491 	u_int64_t       eupid __attribute__((aligned(sizeof(u_int64_t))));
492 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
493 	u_int64_t       timestamp __attribute__((aligned(sizeof(u_int64_t))));
494 
495 	activity_bitmap_t activity_bitmap;
496 
497 	union{
498 		struct sockaddr_in      v4;
499 		struct sockaddr_in6     v6;
500 	} local;
501 
502 	union{
503 		struct sockaddr_in      v4;
504 		struct sockaddr_in6     v6;
505 	} remote;
506 
507 	u_int32_t       ifindex;
508 
509 	u_int32_t       rcvbufsize;
510 	u_int32_t       rcvbufused;
511 	u_int32_t       traffic_class;
512 
513 	u_int32_t       pid;
514 	char            pname[64];
515 	u_int32_t       epid;
516 
517 	uuid_t          uuid;
518 	uuid_t          euuid;
519 	uuid_t          vuuid;
520 	uuid_t          fuuid;
521 	uid_t           persona_id;
522 	uid_t           uid;
523 	uint32_t        ifnet_properties;
524 	uint8_t         fallback_mode;
525 	uint8_t         reserved[3];
526 } nstat_udp_descriptor;
527 
528 /*
529  * XXX For now just typedef'ing TCP Nstat descriptor to nstat_quic_descriptor
530  * as for now they report very similar data.
531  * Later when we extend the QUIC descriptor we can just declare its own
532  * descriptor struct.
533  */
534 typedef struct nstat_tcp_add_param      nstat_quic_add_param;
535 typedef struct nstat_tcp_descriptor     nstat_quic_descriptor;
536 
537 typedef struct nstat_connection_descriptor {
538 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
539 	u_int64_t       timestamp;
540 	u_int64_t       upid;
541 	u_int64_t       eupid;
542 
543 	u_int32_t       pid;
544 	u_int32_t       epid;
545 	u_int32_t       ifnet_properties;
546 	char            pname[64];
547 	uuid_t          uuid;   /* UUID of the app */
548 	uuid_t          euuid;  /* Effective UUID */
549 	uuid_t          cuuid;  /* Connection UUID */
550 	uuid_t          puuid;  /* Parent UUID */
551 	uuid_t          fuuid;  /* Flow UUID */
552 	uid_t           persona_id;
553 	uid_t           uid;
554 	uint8_t         reserved[4];
555 } nstat_connection_descriptor;
556 
557 typedef struct nstat_route_descriptor {
558 	u_int64_t       id __attribute__((aligned(sizeof(u_int64_t))));
559 	u_int64_t       parent_id __attribute__((aligned(sizeof(u_int64_t))));
560 	u_int64_t       gateway_id __attribute__((aligned(sizeof(u_int64_t))));
561 
562 	union{
563 		struct sockaddr_in      v4;
564 		struct sockaddr_in6     v6;
565 		struct sockaddr         sa;
566 	} dst;
567 
568 	union{
569 		struct sockaddr_in      v4;
570 		struct sockaddr_in6     v6;
571 		struct sockaddr         sa;
572 	} mask;
573 
574 	union{
575 		struct sockaddr_in      v4;
576 		struct sockaddr_in6     v6;
577 		struct sockaddr         sa;
578 	} gateway;
579 
580 	u_int32_t       ifindex;
581 	u_int32_t       flags;
582 
583 	u_int8_t        reserved[4];
584 } nstat_route_descriptor;
585 
586 typedef struct nstat_ifnet_add_param {
587 	u_int64_t       threshold __attribute__((aligned(sizeof(u_int64_t))));
588 	u_int32_t       ifindex;
589 
590 	u_int8_t        reserved[4];
591 } nstat_ifnet_add_param;
592 
593 typedef struct nstat_ifnet_desc_cellular_status {
594 	u_int32_t valid_bitmask; /* indicates which fields are valid */
595 #define NSTAT_IFNET_DESC_CELL_LINK_QUALITY_METRIC_VALID         0x1
596 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_BANDWIDTH_VALID      0x2
597 #define NSTAT_IFNET_DESC_CELL_UL_MAX_BANDWIDTH_VALID            0x4
598 #define NSTAT_IFNET_DESC_CELL_UL_MIN_LATENCY_VALID              0x8
599 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_LATENCY_VALID        0x10
600 #define NSTAT_IFNET_DESC_CELL_UL_MAX_LATENCY_VALID              0x20
601 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_VALID              0x40
602 #define NSTAT_IFNET_DESC_CELL_UL_BYTES_LOST_VALID               0x80
603 #define NSTAT_IFNET_DESC_CELL_UL_MIN_QUEUE_SIZE_VALID           0x100
604 #define NSTAT_IFNET_DESC_CELL_UL_AVG_QUEUE_SIZE_VALID           0x200
605 #define NSTAT_IFNET_DESC_CELL_UL_MAX_QUEUE_SIZE_VALID           0x400
606 #define NSTAT_IFNET_DESC_CELL_DL_EFFECTIVE_BANDWIDTH_VALID      0x800
607 #define NSTAT_IFNET_DESC_CELL_DL_MAX_BANDWIDTH_VALID            0x1000
608 #define NSTAT_IFNET_DESC_CELL_CONFIG_INACTIVITY_TIME_VALID      0x2000
609 #define NSTAT_IFNET_DESC_CELL_CONFIG_BACKOFF_TIME_VALID         0x4000
610 #define NSTAT_IFNET_DESC_CELL_MSS_RECOMMENDED_VALID             0x8000
611 	u_int32_t link_quality_metric;
612 	u_int32_t ul_effective_bandwidth; /* Measured uplink bandwidth based on
613 	                                   *  current activity (bps) */
614 	u_int32_t ul_max_bandwidth; /* Maximum supported uplink bandwidth
615 	                             *  (bps) */
616 	u_int32_t ul_min_latency; /* min expected uplink latency for first hop
617 	                           *  (ms) */
618 	u_int32_t ul_effective_latency; /* current expected uplink latency for
619 	                                 *  first hop (ms) */
620 	u_int32_t ul_max_latency; /* max expected uplink latency first hop
621 	                           *  (ms) */
622 	u_int32_t ul_retxt_level; /* Retransmission metric */
623 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_NONE       1
624 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_LOW        2
625 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_MEDIUM     3
626 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_HIGH       4
627 
628 	u_int32_t ul_bytes_lost; /* % of total bytes lost on uplink in Q10
629 	                          *  format */
630 	u_int32_t ul_min_queue_size; /* minimum bytes in queue */
631 	u_int32_t ul_avg_queue_size; /* average bytes in queue */
632 	u_int32_t ul_max_queue_size; /* maximum bytes in queue */
633 	u_int32_t dl_effective_bandwidth; /* Measured downlink bandwidth based
634 	                                   *  on current activity (bps) */
635 	u_int32_t dl_max_bandwidth; /* Maximum supported downlink bandwidth
636 	                             *  (bps) */
637 	u_int32_t config_inactivity_time; /* ms */
638 	u_int32_t config_backoff_time; /* new connections backoff time in ms */
639 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_NONE   0x0
640 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_MEDIUM 0x1
641 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_LOW    0x2
642 	u_int16_t mss_recommended; /* recommended MSS */
643 	u_int8_t        reserved[2];
644 } nstat_ifnet_desc_cellular_status;
645 
646 typedef struct nstat_ifnet_desc_wifi_status {
647 	u_int32_t valid_bitmask;
648 #define NSTAT_IFNET_DESC_WIFI_LINK_QUALITY_METRIC_VALID         0x1
649 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID      0x2
650 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_BANDWIDTH_VALID            0x4
651 #define NSTAT_IFNET_DESC_WIFI_UL_MIN_LATENCY_VALID              0x8
652 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_LATENCY_VALID        0x10
653 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_LATENCY_VALID              0x20
654 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_VALID              0x40
655 #define NSTAT_IFNET_DESC_WIFI_UL_ERROR_RATE_VALID               0x80
656 #define NSTAT_IFNET_DESC_WIFI_UL_BYTES_LOST_VALID               0x100
657 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID      0x200
658 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_BANDWIDTH_VALID            0x400
659 #define NSTAT_IFNET_DESC_WIFI_DL_MIN_LATENCY_VALID              0x800
660 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_LATENCY_VALID        0x1000
661 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_LATENCY_VALID              0x2000
662 #define NSTAT_IFNET_DESC_WIFI_DL_ERROR_RATE_VALID               0x4000
663 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_VALID            0x8000
664 #define NSTAT_IFNET_DESC_WIFI_CONFIG_MULTICAST_RATE_VALID       0x10000
665 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_COUNT_VALID           0x20000
666 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_DURATION_VALID        0x40000
667 	u_int32_t link_quality_metric; /* link quality metric */
668 	u_int32_t ul_effective_bandwidth; /* Measured uplink bandwidth based on
669 	                                   *  current activity (bps) */
670 	u_int32_t ul_max_bandwidth; /* Maximum supported uplink bandwidth
671 	                             *  (bps) */
672 	u_int32_t ul_min_latency; /* min expected uplink latency for first hop
673 	                           *  (ms) */
674 	u_int32_t ul_effective_latency; /* current expected uplink latency for
675 	                                 *  first hop (ms) */
676 	u_int32_t ul_max_latency; /* max expected uplink latency for first hop
677 	                           *  (ms) */
678 	u_int32_t ul_retxt_level; /* Retransmission metric */
679 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_NONE       1
680 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_LOW        2
681 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_MEDIUM     3
682 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_HIGH       4
683 
684 	u_int32_t ul_bytes_lost; /* % of total bytes lost on uplink in Q10
685 	                          *  format */
686 	u_int32_t ul_error_rate; /* % of bytes dropped on uplink after many
687 	                          *  retransmissions in Q10 format */
688 	u_int32_t dl_effective_bandwidth; /* Measured downlink bandwidth based
689 	                                   *  on current activity (bps) */
690 	u_int32_t dl_max_bandwidth; /* Maximum supported downlink bandwidth
691 	                             *  (bps) */
692 	/*
693 	 * The download latency values indicate the time AP may have to wait
694 	 * for the  driver to receive the packet. These values give the range
695 	 * of expected latency mainly due to co-existence events and channel
696 	 * hopping where the interface becomes unavailable.
697 	 */
698 	u_int32_t dl_min_latency; /* min expected latency for first hop in ms */
699 	u_int32_t dl_effective_latency; /* current expected latency for first
700 	                                 *  hop in ms */
701 	u_int32_t dl_max_latency; /* max expected latency for first hop in ms */
702 	u_int32_t dl_error_rate; /* % of CRC or other errors in Q10 format */
703 	u_int32_t config_frequency; /* 2.4 or 5 GHz */
704 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_2_4_GHZ  1
705 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_5_0_GHZ  2
706 	u_int32_t config_multicast_rate; /* bps */
707 	u_int32_t scan_count; /* scan count during the previous period */
708 	u_int32_t scan_duration; /* scan duration in ms */
709 } nstat_ifnet_desc_wifi_status;
710 
711 enum{
712 	NSTAT_IFNET_DESC_LINK_STATUS_TYPE_NONE = 0
713 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_CELLULAR = 1
714 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_WIFI = 2
715 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_ETHERNET = 3
716 };
717 
718 typedef struct nstat_ifnet_desc_link_status {
719 	u_int32_t       link_status_type;
720 	union {
721 		nstat_ifnet_desc_cellular_status        cellular;
722 		nstat_ifnet_desc_wifi_status            wifi;
723 	} u;
724 } nstat_ifnet_desc_link_status;
725 
726 #ifndef IF_DESCSIZE
727 #define IF_DESCSIZE 128
728 #endif
729 typedef struct nstat_ifnet_descriptor {
730 	u_int64_t                       threshold __attribute__((aligned(sizeof(u_int64_t))));
731 	u_int32_t                       ifindex;
732 	nstat_ifnet_desc_link_status    link_status;
733 	unsigned int            type;
734 	char                            description[IF_DESCSIZE];
735 	char                            name[IFNAMSIZ + 1];
736 	u_int8_t                        reserved[3];
737 } nstat_ifnet_descriptor;
738 
739 typedef struct nstat_sysinfo_descriptor {
740 	u_int32_t       flags;
741 } nstat_sysinfo_descriptor;
742 
743 typedef struct nstat_sysinfo_add_param {
744 	/* To indicate which system level information should be collected */
745 	u_int32_t       flags;
746 } nstat_sysinfo_add_param;
747 
748 #define NSTAT_SYSINFO_MBUF_STATS        0x0001
749 #define NSTAT_SYSINFO_TCP_STATS         0x0002
750 #define NSTAT_SYSINFO_IFNET_ECN_STATS   0x0003
751 #define NSTAT_SYSINFO_LIM_STATS         0x0004  /* Low Internet mode stats */
752 #define NSTAT_SYSINFO_NET_API_STATS     0x0005  /* API and KPI stats */
753 
754 #pragma mark -- Network Statistics User Client --
755 
756 #define NET_STAT_CONTROL_NAME   "com.apple.network.statistics"
757 
758 enum{
759 	// generic response messages
760 	NSTAT_MSG_TYPE_SUCCESS               = 0
761 	, NSTAT_MSG_TYPE_ERROR               = 1
762 
763 	    // Requests
764 	, NSTAT_MSG_TYPE_ADD_SRC             = 1001
765 	, NSTAT_MSG_TYPE_ADD_ALL_SRCS        = 1002
766 	, NSTAT_MSG_TYPE_REM_SRC             = 1003
767 	, NSTAT_MSG_TYPE_QUERY_SRC           = 1004
768 	, NSTAT_MSG_TYPE_GET_SRC_DESC        = 1005
769 	, NSTAT_MSG_TYPE_SET_FILTER          = 1006 // Obsolete
770 	, NSTAT_MSG_TYPE_GET_UPDATE          = 1007
771 	, NSTAT_MSG_TYPE_SUBSCRIBE_SYSINFO   = 1008
772 
773 	    // Responses/Notfications
774 	, NSTAT_MSG_TYPE_SRC_ADDED           = 10001
775 	, NSTAT_MSG_TYPE_SRC_REMOVED         = 10002
776 	, NSTAT_MSG_TYPE_SRC_DESC            = 10003
777 	, NSTAT_MSG_TYPE_SRC_COUNTS          = 10004
778 	, NSTAT_MSG_TYPE_SYSINFO_COUNTS      = 10005
779 	, NSTAT_MSG_TYPE_SRC_UPDATE          = 10006
780 	, NSTAT_MSG_TYPE_SRC_EXTENDED_UPDATE = 10007
781 };
782 
783 enum{
784 	NSTAT_SRC_REF_ALL       = 0xffffffffffffffffULL
785 	, NSTAT_SRC_REF_INVALID  = 0
786 };
787 
788 /* Source-level filters */
789 enum{
790 	NSTAT_FILTER_NOZEROBYTES             = 0x00000001
791 };
792 
793 
794 /* Types of extended update information, used in setting initial filters as well as to identify returned extensions */
795 /* A contiguous range currently limited 1..31 due to being passed as the top 32 bits of filter */
796 enum{
797 	NSTAT_EXTENDED_UPDATE_TYPE_UNKNOWN              = 0
798 	, NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN             = 1
799 	, NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV           = 2
800 	, NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV  = 3
801 	, NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN    = 4
802 	, NSTAT_EXTENDED_UPDATE_TYPE_FUUID              = 5
803 };
804 
805 #define NSTAT_EXTENDED_UPDATE_TYPE_MIN  NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN
806 #define NSTAT_EXTENDED_UPDATE_TYPE_MAX  NSTAT_EXTENDED_UPDATE_TYPE_FUUID
807 
808 
809 #define NSTAT_EXTENDED_UPDATE_FLAG_MASK    0x00ffffffull    /* Maximum of 24 extension types allowed due to restrictions on specifying via filter flags */
810 
811 #define NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT   40  /* With extensions expediently passed as the top 24 bits of filters supplied by client, this shift is for extraction */
812 
813 /* Provider-level filters */
814 #define NSTAT_FILTER_ACCEPT_UNKNOWN          0x0000000000000001ull
815 #define NSTAT_FILTER_ACCEPT_LOOPBACK         0x0000000000000002ull
816 #define NSTAT_FILTER_ACCEPT_CELLULAR         0x0000000000000004ull
817 #define NSTAT_FILTER_ACCEPT_WIFI             0x0000000000000008ull
818 #define NSTAT_FILTER_ACCEPT_WIRED            0x0000000000000010ull
819 #define NSTAT_FILTER_ACCEPT_AWDL             0x0000000000000020ull
820 #define NSTAT_FILTER_ACCEPT_EXPENSIVE        0x0000000000000040ull
821 #define NSTAT_FILTER_ACCEPT_CELLFALLBACK     0x0000000000000100ull
822 #define NSTAT_FILTER_ACCEPT_COMPANIONLINK    0x0000000000000200ull
823 #define NSTAT_FILTER_ACCEPT_IS_CONSTRAINED   0x0000000000000400ull
824 #define NSTAT_FILTER_ACCEPT_IS_LOCAL         0x0000000000000800ull
825 #define NSTAT_FILTER_ACCEPT_IS_NON_LOCAL     0x0000000000001000ull
826 #define NSTAT_FILTER_ACCEPT_ROUTE_VAL_ERR    0x0000000000002000ull
827 #define NSTAT_FILTER_ACCEPT_FLOWSWITCH_ERR   0x0000000000004000ull
828 #define NSTAT_FILTER_ACCEPT_WIFI_LLW         0x0000000000008000ull
829 #define NSTAT_FILTER_ACCEPT_WIFI_INFRA       0x0000000000010000ull
830 #define NSTAT_FILTER_IFNET_FLAGS             0x000000000001FFFFull
831 
832 #define NSTAT_FILTER_UDP_INTERFACE_ATTACH    0x0000000000020000ull
833 #define NSTAT_FILTER_UDP_FLAGS               0x0000000000020000ull
834 
835 #define NSTAT_FILTER_TCP_INTERFACE_ATTACH    0x0000000000040000ull
836 #define NSTAT_FILTER_TCP_NO_EARLY_CLOSE      0x0000000000080000ull
837 #define NSTAT_FILTER_TCP_FLAGS               0x00000000000C0000ull
838 
839 #define NSTAT_FILTER_SUPPRESS_SRC_ADDED      0x0000000000100000ull
840 #define NSTAT_FILTER_REQUIRE_SRC_ADDED       0x0000000000200000ull
841 #define NSTAT_FILTER_PROVIDER_NOZEROBYTES    0x0000000000400000ull
842 
843 #define NSTAT_FILTER_CONN_HAS_NET_ACCESS     0x0000000001000000ull
844 #define NSTAT_FILTER_CONN_FLAGS              0x0000000001000000ull
845 
846 /* In this context, boring == no change from previous report */
847 #define NSTAT_FILTER_SUPPRESS_BORING_CLOSE   0x0000000010000000ull  /* No final update, only NSTAT_MSG_TYPE_SRC_REMOVED */
848 #define NSTAT_FILTER_SUPPRESS_BORING_POLL    0x0000000020000000ull  /* Only for poll-all, not poll specific source */
849 #define NSTAT_FILTER_SUPPRESS_BORING_FLAGS   (NSTAT_FILTER_SUPPRESS_BORING_CLOSE|NSTAT_FILTER_SUPPRESS_BORING_POLL)
850 
851 #define NSTAT_FILTER_SPECIFIC_USER_BY_PID    0x0000000100000000ull
852 #define NSTAT_FILTER_SPECIFIC_USER_BY_EPID   0x0000000200000000ull
853 #define NSTAT_FILTER_SPECIFIC_USER_BY_UUID   0x0000000400000000ull
854 #define NSTAT_FILTER_SPECIFIC_USER_BY_EUUID  0x0000000800000000ull
855 #define NSTAT_FILTER_SPECIFIC_USER           0x0000000F00000000ull
856 
857 #define NSTAT_FILTER_INITIAL_PROPERTIES      0x0000001000000000ull  /* For providers that give "properties" on open, apply the filter to the properties */
858                                                                     /* and permanently discard unless the filter allows through */
859 #define NSTAT_FILTER_FLAGS_RESERVED          0x000000E000000000ul
860 
861 #define NSTAT_FILTER_IFNET_AND_CONN_FLAGS    (NSTAT_FILTER_IFNET_FLAGS|NSTAT_FILTER_CONN_FLAGS)
862 
863 #define NSTAT_EXTENSION_FILTER_DOMAIN_INFO              (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
864 #define NSTAT_EXTENSION_FILTER_NECP_TLV                 (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
865 #define NSTAT_EXTENSION_FILTER_ORIGINAL_NECP_TLV        (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
866 #define NSTAT_EXTENSION_FILTER_ORIGINAL_DOMAIN_INFO     (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
867 #define NSTAT_EXTENSION_FILTER_MASK                     (NSTAT_EXTENDED_UPDATE_FLAG_MASK << NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT)
868 
869 enum{
870 	NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE   = 1 << 0,
871 	NSTAT_MSG_HDR_FLAG_CONTINUATION         = 1 << 1,
872 	NSTAT_MSG_HDR_FLAG_CLOSING              = 1 << 2,
873 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_DROP    = 1 << 3,
874 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_FILTER  = 1 << 4,
875 };
876 
877 typedef struct nstat_msg_hdr {
878 	u_int64_t       context __attribute__((aligned(sizeof(u_int64_t))));
879 	u_int32_t       type;
880 	u_int16_t       length;
881 	u_int16_t       flags;
882 } nstat_msg_hdr;
883 
884 #define MAX_NSTAT_MSG_HDR_LENGTH    65532
885 
886 typedef struct nstat_msg_error {
887 	nstat_msg_hdr   hdr;
888 	u_int32_t               error;  // errno error
889 	u_int8_t                reserved[4];
890 } nstat_msg_error;
891 
892 #define NSTAT_ADD_SRC_FIELDS            \
893 	nstat_msg_hdr		hdr;            \
894 	nstat_provider_id_t	provider;       \
895 	u_int8_t			reserved[4]     \
896 
897 typedef struct nstat_msg_add_src {
898 	NSTAT_ADD_SRC_FIELDS;
899 	u_int8_t        param[];
900 } nstat_msg_add_src_req;
901 
902 typedef struct nstat_msg_add_src_header {
903 	NSTAT_ADD_SRC_FIELDS;
904 } nstat_msg_add_src_header;
905 
906 typedef struct nstat_msg_add_src_convenient {
907 	nstat_msg_add_src_header        hdr;
908 	union {
909 		nstat_route_add_param   route;
910 		nstat_tcp_add_param             tcp;
911 		nstat_udp_add_param             udp;
912 		nstat_ifnet_add_param   ifnet;
913 		nstat_sysinfo_add_param sysinfo;
914 	};
915 } nstat_msg_add_src_convenient;
916 
917 #undef NSTAT_ADD_SRC_FIELDS
918 
919 typedef struct nstat_msg_add_all_srcs {
920 	nstat_msg_hdr           hdr;
921 	u_int64_t               filter __attribute__((aligned(sizeof(u_int64_t))));
922 	nstat_event_flags_t     events __attribute__((aligned(sizeof(u_int64_t))));
923 	nstat_provider_id_t     provider;
924 	pid_t                   target_pid;
925 	uuid_t                  target_uuid;
926 } nstat_msg_add_all_srcs;
927 
928 typedef struct nstat_msg_src_added {
929 	nstat_msg_hdr           hdr;
930 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
931 	nstat_provider_id_t     provider;
932 	u_int8_t                reserved[4];
933 } nstat_msg_src_added;
934 
935 typedef struct nstat_msg_rem_src {
936 	nstat_msg_hdr           hdr;
937 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
938 } nstat_msg_rem_src_req;
939 
940 typedef struct nstat_msg_get_src_description {
941 	nstat_msg_hdr           hdr;
942 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
943 } nstat_msg_get_src_description;
944 
945 typedef struct nstat_msg_set_filter {
946 	nstat_msg_hdr           hdr;
947 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
948 	u_int32_t               filter;
949 	u_int8_t                reserved[4];
950 } nstat_msg_set_filter;
951 
952 #define NSTAT_SRC_DESCRIPTION_FIELDS                                                                                            \
953 	nstat_msg_hdr		hdr;                                                                                                            \
954 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
955 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
956 	nstat_provider_id_t	provider;                                                                                                       \
957 	u_int8_t			reserved[4]
958 
959 typedef struct nstat_msg_src_description {
960 	NSTAT_SRC_DESCRIPTION_FIELDS;
961 	u_int8_t        data[];
962 } nstat_msg_src_description;
963 
964 typedef struct nstat_msg_src_description_header {
965 	NSTAT_SRC_DESCRIPTION_FIELDS;
966 } nstat_msg_src_description_header;
967 
968 typedef struct nstat_msg_src_description_convenient {
969 	nstat_msg_src_description_header    hdr;
970 	union {
971 		nstat_tcp_descriptor            tcp;
972 		nstat_udp_descriptor            udp;
973 		nstat_route_descriptor          route;
974 		nstat_ifnet_descriptor          ifnet;
975 		nstat_sysinfo_descriptor        sysinfo;
976 		nstat_quic_descriptor           quic;
977 	};
978 } nstat_msg_src_description_convenient;
979 
980 #undef NSTAT_SRC_DESCRIPTION_FIELDS
981 
982 typedef struct nstat_msg_query_src {
983 	nstat_msg_hdr           hdr;
984 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
985 } nstat_msg_query_src_req;
986 
987 typedef struct nstat_msg_src_counts {
988 	nstat_msg_hdr           hdr;
989 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
990 	nstat_event_flags_t     event_flags __attribute__((aligned(sizeof(u_int64_t))));
991 	nstat_counts            counts;
992 } nstat_msg_src_counts;
993 
994 #define NSTAT_SRC_UPDATE_FIELDS                                                                                                         \
995 	nstat_msg_hdr		hdr;                                                                                                            \
996 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
997 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
998 	nstat_counts		counts;                                                                                                         \
999 	nstat_provider_id_t	provider;                                                                                                       \
1000 	u_int8_t			reserved[4]
1001 
1002 typedef struct nstat_msg_src_update {
1003 	NSTAT_SRC_UPDATE_FIELDS;
1004 	u_int8_t        data[];
1005 } nstat_msg_src_update;
1006 
1007 typedef struct nstat_msg_src_update_hdr {
1008 	NSTAT_SRC_UPDATE_FIELDS;
1009 } nstat_msg_src_update_hdr;
1010 
1011 typedef struct nstat_msg_src_update_tcp {
1012 	NSTAT_SRC_UPDATE_FIELDS;
1013 	nstat_tcp_descriptor            tcp_desc;
1014 } nstat_msg_src_update_tcp;
1015 
1016 typedef struct nstat_msg_src_update_udp {
1017 	NSTAT_SRC_UPDATE_FIELDS;
1018 	nstat_udp_descriptor            udp_desc;
1019 } nstat_msg_src_update_udp;
1020 
1021 typedef struct nstat_msg_src_update_quic {
1022 	NSTAT_SRC_UPDATE_FIELDS;
1023 	nstat_quic_descriptor           quic_desc;
1024 } nstat_msg_src_update_quic;
1025 
1026 typedef struct nstat_msg_src_update_conn {
1027 	NSTAT_SRC_UPDATE_FIELDS;
1028 	nstat_connection_descriptor     conn_desc;
1029 } nstat_msg_src_update_conn;
1030 
1031 
1032 typedef struct nstat_msg_src_update_convenient {
1033 	nstat_msg_src_update_hdr                hdr;
1034 	union {
1035 		nstat_tcp_descriptor            tcp;
1036 		nstat_udp_descriptor            udp;
1037 		nstat_route_descriptor          route;
1038 		nstat_ifnet_descriptor          ifnet;
1039 		nstat_sysinfo_descriptor        sysinfo;
1040 		nstat_quic_descriptor           quic;
1041 		nstat_connection_descriptor     conn;
1042 	};
1043 } nstat_msg_src_update_convenient;
1044 
1045 typedef struct nstat_msg_src_extended_item_hdr {
1046 	u_int32_t       type;
1047 	u_int32_t       length;
1048 } nstat_msg_src_extended_item_hdr __attribute__((aligned(sizeof(u_int64_t))));;
1049 
1050 typedef struct nstat_msg_src_extended_item {
1051 	nstat_msg_src_extended_item_hdr         hdr;
1052 	u_int8_t                                data[];
1053 } nstat_msg_src_extended_item;
1054 
1055 typedef struct nstat_msg_src_extended_tcp_update {
1056 	nstat_msg_src_update_hdr                hdr;
1057 	nstat_tcp_descriptor                    tcp;
1058 	nstat_msg_src_extended_item_hdr         extension_hdr;
1059 	u_int8_t                                data[];
1060 } nstat_msg_src_extended_tcp_update;
1061 
1062 typedef struct nstat_msg_src_extended_udp_update {
1063 	nstat_msg_src_update_hdr                hdr;
1064 	nstat_udp_descriptor                    udp;
1065 	nstat_msg_src_extended_item_hdr         extension_hdr;
1066 	u_int8_t                                data[];
1067 } nstat_msg_src_extended_udp_update;
1068 
1069 typedef struct nstat_msg_src_extended_quic_update {
1070 	nstat_msg_src_update_hdr                hdr;
1071 	nstat_quic_descriptor                   quic;
1072 	nstat_msg_src_extended_item_hdr         extension_hdr;
1073 	u_int8_t                                data[];
1074 } nstat_msg_src_extended_quic_update;
1075 
1076 typedef struct nstat_msg_src_extended_conn_update {
1077 	nstat_msg_src_update_hdr                hdr;
1078 	nstat_connection_descriptor             conn;
1079 	nstat_msg_src_extended_item_hdr         extension_hdr;
1080 	u_int8_t                                data[];
1081 } nstat_msg_src_extended_conn_update;
1082 
1083 /* While the only type of extended update is for domain information, we can fully define the structure */
1084 typedef struct nstat_msg_src_tcp_update_domain_extension {
1085 	nstat_msg_src_update_hdr                hdr;
1086 	nstat_tcp_descriptor                    tcp;
1087 	nstat_msg_src_extended_item_hdr         extension_hdr;
1088 	nstat_domain_info                       domain_info;
1089 } nstat_msg_src_tcp_update_domain_extension;
1090 
1091 typedef struct nstat_msg_src_udp_update_domain_extension {
1092 	nstat_msg_src_update_hdr                hdr;
1093 	nstat_udp_descriptor                    udp;
1094 	nstat_msg_src_extended_item_hdr         extension_hdr;
1095 	nstat_domain_info                       domain_info;
1096 } nstat_msg_src_udp_update_domain_extension;
1097 
1098 typedef struct nstat_msg_src_quic_update_domain_extension {
1099 	nstat_msg_src_update_hdr                hdr;
1100 	nstat_quic_descriptor                   quic;
1101 	nstat_msg_src_extended_item_hdr         extension_hdr;
1102 	nstat_domain_info                       domain_info;
1103 } nstat_msg_src_quic_update_domain_extension;
1104 
1105 typedef struct nstat_msg_src_update_domain_extension_convenient {
1106 	nstat_msg_src_tcp_update_domain_extension       tcp;
1107 	nstat_msg_src_udp_update_domain_extension       udp;
1108 	nstat_msg_src_quic_update_domain_extension      quic;
1109 } nstat_msg_src_update_domain_extension_convenient;
1110 
1111 #undef NSTAT_SRC_UPDATE_FIELDS
1112 
1113 typedef struct nstat_msg_src_removed {
1114 	nstat_msg_hdr           hdr;
1115 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1116 } nstat_msg_src_removed;
1117 
1118 typedef struct nstat_msg_sysinfo_counts {
1119 	nstat_msg_hdr           hdr;
1120 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1121 	nstat_sysinfo_counts    counts;
1122 }  nstat_msg_sysinfo_counts;
1123 
1124 #pragma mark -- Statitiscs about Network Statistics --
1125 
1126 struct nstat_stats {
1127 	u_int32_t nstat_successmsgfailures;
1128 	u_int32_t nstat_sendcountfailures;
1129 	u_int32_t nstat_sysinfofailures;
1130 	u_int32_t nstat_srcupatefailures;
1131 	u_int32_t nstat_descriptionfailures;
1132 	u_int32_t nstat_msgremovedfailures;
1133 	u_int32_t nstat_srcaddedfailures;
1134 	u_int32_t nstat_msgerrorfailures;
1135 	u_int32_t nstat_copy_descriptor_failures;
1136 	u_int32_t nstat_provider_counts_failures;
1137 	u_int32_t nstat_control_send_description_failures;
1138 	u_int32_t nstat_control_send_goodbye_failures;
1139 	u_int32_t nstat_flush_accumulated_msgs_failures;
1140 	u_int32_t nstat_accumulate_msg_failures;
1141 	u_int32_t nstat_control_cleanup_source_failures;
1142 	u_int32_t nstat_handle_msg_failures;
1143 };
1144 
1145 #endif /* PRIVATE */
1146 
1147 #ifdef XNU_KERNEL_PRIVATE
1148 #include <sys/mcache.h>
1149 
1150 #if (DEBUG || DEVELOPMENT)
1151 extern int nstat_test_privacy_transparency;
1152 #endif /* (DEBUG || DEVELOPMENT) */
1153 
1154 #pragma mark -- System Information Internal Support --
1155 
1156 typedef struct nstat_sysinfo_mbuf_stats {
1157 	u_int32_t       total_256b;     /* Peak usage, 256B pool */
1158 	u_int32_t       total_2kb;      /* Peak usage, 2KB pool */
1159 	u_int32_t       total_4kb;      /* Peak usage, 4KB pool */
1160 	u_int32_t       total_16kb;     /* Peak usage, 16KB pool */
1161 	u_int32_t       sbmb_total;     /* Total mbufs in sock buffer pool */
1162 	u_int32_t       sb_atmbuflimit; /* Memory limit reached for socket buffer autoscaling */
1163 	u_int32_t       draincnt;       /* Number of times mbuf pool has been drained under memory pressure */
1164 	u_int32_t       memreleased;    /* Memory (bytes) released from mbuf pool to VM */
1165 	u_int32_t       sbmb_floor;     /* Lowest mbufs in sock buffer pool */
1166 } nstat_sysinfo_mbuf_stats;
1167 
1168 typedef struct nstat_sysinfo_tcp_stats {
1169 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1170 	u_int32_t       ipv4_avgrtt;    /* Average RTT for IPv4 */
1171 	u_int32_t       ipv6_avgrtt;    /* Average RTT for IPv6 */
1172 	u_int32_t       send_plr;       /* Average uplink packet loss rate */
1173 	u_int32_t       recv_plr;       /* Average downlink packet loss rate */
1174 	u_int32_t       send_tlrto_rate; /* Average rxt timeout after tail loss */
1175 	u_int32_t       send_reorder_rate; /* Average packet reordering rate */
1176 	u_int32_t       connection_attempts; /* TCP client connection attempts */
1177 	u_int32_t       connection_accepts; /* TCP server connection accepts */
1178 	u_int32_t       ecn_client_enabled; /* Global setting for ECN client side */
1179 	u_int32_t       ecn_server_enabled; /* Global setting for ECN server side */
1180 	u_int32_t       ecn_client_setup; /* Attempts to setup TCP client connection with ECN */
1181 	u_int32_t       ecn_server_setup; /* Attempts to setup TCP server connection with ECN */
1182 	u_int32_t       ecn_client_success; /* Number of successful negotiations of ECN for a client connection */
1183 	u_int32_t       ecn_server_success; /* Number of successful negotiations of ECN for a server connection */
1184 	u_int32_t       ecn_not_supported; /* Number of falbacks to Non-ECN, no support from peer */
1185 	u_int32_t       ecn_lost_syn;   /* Number of SYNs lost with ECN bits */
1186 	u_int32_t       ecn_lost_synack; /* Number of SYN-ACKs lost with ECN bits */
1187 	u_int32_t       ecn_recv_ce;    /* Number of CEs received from network */
1188 	u_int32_t       ecn_recv_ece;   /* Number of ECEs received from receiver */
1189 	u_int32_t       ecn_sent_ece;   /* Number of ECEs sent in response to CE */
1190 	u_int32_t       ecn_conn_recv_ce; /* Number of connections using ECN received CE at least once */
1191 	u_int32_t       ecn_conn_recv_ece; /* Number of connections using ECN received ECE at least once */
1192 	u_int32_t       ecn_conn_plnoce; /* Number of connections using ECN seen packet loss but never received CE */
1193 	u_int32_t       ecn_conn_pl_ce; /* Number of connections using ECN seen packet loss and CE */
1194 	u_int32_t       ecn_conn_nopl_ce; /* Number of connections using ECN with no packet loss but received CE */
1195 	u_int32_t       ecn_fallback_synloss; /* Number of times we did fall back due to SYN-Loss */
1196 	u_int32_t       ecn_fallback_reorder; /* Number of times we fallback because we detected the PAWS-issue */
1197 	u_int32_t       ecn_fallback_ce; /* Number of times we fallback because we received too many CEs */
1198 	u_int32_t       tfo_syn_data_rcv;       /* Number of SYN+data received with valid cookie */
1199 	u_int32_t       tfo_cookie_req_rcv;/* Number of TFO cookie-requests received */
1200 	u_int32_t       tfo_cookie_sent;        /* Number of TFO-cookies offered to the client */
1201 	u_int32_t       tfo_cookie_invalid;/* Number of invalid TFO-cookies received */
1202 	u_int32_t       tfo_cookie_req; /* Number of SYNs with cookie request received*/
1203 	u_int32_t       tfo_cookie_rcv; /* Number of SYN/ACKs with Cookie received */
1204 	u_int32_t       tfo_syn_data_sent;      /* Number of SYNs+data+cookie sent */
1205 	u_int32_t       tfo_syn_data_acked;/* Number of times our SYN+data has been acknowledged */
1206 	u_int32_t       tfo_syn_loss;   /* Number of times SYN+TFO has been lost and we fallback */
1207 	u_int32_t       tfo_blackhole;  /* Number of times SYN+TFO has been lost and we fallback */
1208 	u_int32_t       tfo_cookie_wrong;       /* TFO-cookie we sent was wrong */
1209 	u_int32_t       tfo_no_cookie_rcv;      /* We asked for a cookie but didn't get one */
1210 	u_int32_t       tfo_heuristics_disable; /* TFO got disabled due to heuristics */
1211 	u_int32_t       tfo_sndblackhole;       /* TFO got blackholed in the sending direction */
1212 	u_int32_t       mptcp_handover_attempt; /* Total number of MPTCP-attempts using handover mode */
1213 	u_int32_t       mptcp_interactive_attempt;      /* Total number of MPTCP-attempts using interactive mode */
1214 	u_int32_t       mptcp_aggregate_attempt;        /* Total number of MPTCP-attempts using aggregate mode */
1215 	u_int32_t       mptcp_fp_handover_attempt; /* Same as previous three but only for first-party apps */
1216 	u_int32_t       mptcp_fp_interactive_attempt;
1217 	u_int32_t       mptcp_fp_aggregate_attempt;
1218 	u_int32_t       mptcp_heuristic_fallback;       /* Total number of MPTCP-connections that fell back due to heuristics */
1219 	u_int32_t       mptcp_fp_heuristic_fallback;    /* Same as previous but for first-party apps */
1220 	u_int32_t       mptcp_handover_success_wifi;    /* Total number of successfull handover-mode connections that *started* on WiFi */
1221 	u_int32_t       mptcp_handover_success_cell;    /* Total number of successfull handover-mode connections that *started* on Cell */
1222 	u_int32_t       mptcp_interactive_success;              /* Total number of interactive-mode connections that negotiated MPTCP */
1223 	u_int32_t       mptcp_aggregate_success;                /* Same as previous but for aggregate */
1224 	u_int32_t       mptcp_fp_handover_success_wifi; /* Same as previous four, but for first-party apps */
1225 	u_int32_t       mptcp_fp_handover_success_cell;
1226 	u_int32_t       mptcp_fp_interactive_success;
1227 	u_int32_t       mptcp_fp_aggregate_success;
1228 	u_int32_t       mptcp_handover_cell_from_wifi;  /* Total number of connections that use cell in handover-mode (coming from WiFi) */
1229 	u_int32_t       mptcp_handover_wifi_from_cell;  /* Total number of connections that use WiFi in handover-mode (coming from cell) */
1230 	u_int32_t       mptcp_interactive_cell_from_wifi;       /* Total number of connections that use cell in interactive mode (coming from WiFi) */
1231 	u_int32_t       mptcp_back_to_wifi;     /* Total number of connections that succeed to move traffic away from cell (when starting on cell) */
1232 	u_int64_t       mptcp_handover_cell_bytes;              /* Total number of bytes sent on cell in handover-mode (on new subflows, ignoring initial one) */
1233 	u_int64_t       mptcp_interactive_cell_bytes;   /* Same as previous but for interactive */
1234 	u_int64_t       mptcp_aggregate_cell_bytes;
1235 	u_int64_t       mptcp_handover_all_bytes;               /* Total number of bytes sent in handover */
1236 	u_int64_t       mptcp_interactive_all_bytes;
1237 	u_int64_t       mptcp_aggregate_all_bytes;
1238 	u_int32_t       mptcp_wifi_proxy;               /* Total number of new subflows that fell back to regular TCP on cell */
1239 	u_int32_t       mptcp_cell_proxy;               /* Total number of new subflows that fell back to regular TCP on WiFi */
1240 	u_int32_t       mptcp_triggered_cell;           /* Total number of times an MPTCP-connection triggered cell bringup */
1241 	u_int32_t       _padding;
1242 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1243 } nstat_sysinfo_tcp_stats;
1244 #define NSTAT_SYSINFO_TCP_STATS_COUNT   71
1245 
1246 enum {
1247 	NSTAT_IFNET_ECN_PROTO_IPV4 = 1
1248 	, NSTAT_IFNET_ECN_PROTO_IPV6
1249 };
1250 
1251 enum {
1252 	NSTAT_IFNET_ECN_TYPE_CELLULAR = 1
1253 	, NSTAT_IFNET_ECN_TYPE_WIFI
1254 	, NSTAT_IFNET_ECN_TYPE_ETHERNET
1255 };
1256 
1257 typedef struct nstat_sysinfo_ifnet_ecn_stats {
1258 	u_int32_t               ifnet_proto;
1259 	u_int32_t               ifnet_type;
1260 	struct if_tcp_ecn_stat  ecn_stat;
1261 } nstat_sysinfo_ifnet_ecn_stats;
1262 
1263 /* Total number of Low Internet stats that will be reported */
1264 #define NSTAT_LIM_STAT_KEYVAL_COUNT     12
1265 typedef struct nstat_sysinfo_lim_stats {
1266 	u_int8_t                ifnet_signature[NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE];
1267 	u_int32_t               ifnet_siglen;
1268 	u_int32_t               ifnet_type;
1269 	struct if_lim_perf_stat lim_stat;
1270 } nstat_sysinfo_lim_stats;
1271 
1272 #define NSTAT_NET_API_STAT_KEYVAL_COUNT (NSTAT_SYSINFO_API_LAST - NSTAT_SYSINFO_API_FIRST + 1)
1273 typedef struct nstat_sysinfo_net_api_stats {
1274 	u_int32_t               report_interval;
1275 	u_int32_t               _padding;
1276 	struct net_api_stats    net_api_stats;
1277 } nstat_sysinfo_net_api_stats;
1278 
1279 typedef struct nstat_sysinfo_data {
1280 	uint32_t                flags;
1281 	uint32_t                unsent_data_cnt; /* Before sleeping */
1282 	union {
1283 		nstat_sysinfo_mbuf_stats mb_stats;
1284 		nstat_sysinfo_tcp_stats tcp_stats;
1285 		nstat_sysinfo_ifnet_ecn_stats ifnet_ecn_stats;
1286 		nstat_sysinfo_lim_stats lim_stats;
1287 		nstat_sysinfo_net_api_stats net_api_stats;
1288 	} u;
1289 } nstat_sysinfo_data;
1290 
1291 #pragma mark -- Generic Network Statistics Provider --
1292 
1293 typedef void *  nstat_provider_cookie_t;
1294 
1295 #pragma mark -- Route Statistics Gathering Functions --
1296 struct rtentry;
1297 
1298 enum{
1299 	NSTAT_TX_FLAG_RETRANSMIT        = 1
1300 };
1301 
1302 enum{
1303 	NSTAT_RX_FLAG_DUPLICATE         = 1,
1304 	NSTAT_RX_FLAG_OUT_OF_ORDER      = 2
1305 };
1306 
1307 // indicates whether or not collection of statistics is enabled
1308 extern int      nstat_collect;
1309 
1310 void nstat_init(void);
1311 
1312 // Route collection routines
1313 void nstat_route_connect_attempt(struct rtentry *rte);
1314 void nstat_route_connect_success(struct rtentry *rte);
1315 void nstat_route_tx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1316 void nstat_route_rx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1317 void nstat_route_rtt(struct rtentry *rte, u_int32_t rtt, u_int32_t rtt_var);
1318 void nstat_route_update(struct rtentry *rte, uint32_t connect_attempts, uint32_t connect_successes,
1319     uint32_t rx_packets, uint32_t rx_bytes, uint32_t rx_duplicatebytes, uint32_t rx_outoforderbytes,
1320     uint32_t tx_packets, uint32_t tx_bytes, uint32_t tx_retransmit,
1321     uint32_t rtt, uint32_t rtt_var);
1322 struct nstat_counts* nstat_route_attach(struct rtentry  *rte);
1323 void nstat_route_detach(struct rtentry *rte);
1324 
1325 // watcher support
1326 struct inpcb;
1327 void nstat_tcp_new_pcb(struct inpcb *inp);
1328 void nstat_udp_new_pcb(struct inpcb *inp);
1329 void nstat_route_new_entry(struct rtentry *rt);
1330 void nstat_pcb_detach(struct inpcb *inp);
1331 void nstat_pcb_event(struct inpcb *inp, u_int64_t event);
1332 void nstat_pcb_cache(struct inpcb *inp);
1333 void nstat_pcb_invalidate_cache(struct inpcb *inp);
1334 
1335 
1336 void nstat_ifnet_threshold_reached(unsigned int ifindex);
1337 
1338 void nstat_sysinfo_send_data(struct nstat_sysinfo_data *);
1339 
1340 int ntstat_tcp_progress_enable(struct sysctl_req *req);
1341 int ntstat_tcp_progress_indicators(struct sysctl_req *req);
1342 
1343 #if SKYWALK
1344 
1345 // Userland stats reporting
1346 
1347 // Each side, NetworkStatistics and the kernel provider for userland,
1348 // pass opaque references.
1349 typedef void *userland_stats_provider_context;
1350 typedef void *nstat_userland_context;
1351 
1352 typedef struct nstat_progress_digest {
1353 	u_int64_t       rxbytes;
1354 	u_int64_t       txbytes;
1355 	u_int32_t       rxduplicatebytes;
1356 	u_int32_t       rxoutoforderbytes;
1357 	u_int32_t       txretransmit;
1358 	u_int32_t       ifindex;
1359 	u_int32_t       state;
1360 	u_int32_t       txunacked;
1361 	u_int32_t       txwindow;
1362 	union {
1363 		struct tcp_conn_status connstatus;
1364 		// On armv7k, tcp_conn_status is 1 byte instead of 4
1365 		uint8_t                                 __pad_connstatus[4];
1366 	};
1367 } nstat_progress_digest;
1368 
1369 // When things have been set up, Netstats can request a refresh of its data.
1370 typedef bool (userland_stats_request_vals_fn)(userland_stats_provider_context *ctx,
1371     u_int16_t *ifflagsp,
1372     nstat_progress_digest *digestp,
1373     nstat_counts *countsp,
1374     void *metadatap);
1375 
1376 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1377 // The return value is the amount of space currently required for the extension
1378 typedef size_t (userland_stats_request_extension_fn)(userland_stats_provider_context *ctx,
1379     int requested_extension,    /* The extension to be returned */
1380     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1381     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1382 
1383 // Things get started with a call to netstats to say that there’s a new connection:
1384 nstat_userland_context ntstat_userland_stats_open(userland_stats_provider_context *ctx,
1385     int provider_id,
1386     u_int64_t properties,
1387     userland_stats_request_vals_fn req_fn,
1388     userland_stats_request_extension_fn req_extension_fn);
1389 
1390 void ntstat_userland_stats_close(nstat_userland_context nstat_ctx);
1391 
1392 
1393 void ntstat_userland_stats_event(nstat_userland_context nstat_ctx, uint64_t event);
1394 
1395 void nstats_userland_stats_defunct_for_process(int pid);
1396 
1397 errno_t nstat_userland_mark_rnf_override(uuid_t fuuid, bool rnf_override);
1398 
1399 
1400 // Servicing a sysctl for information of TCP or UDP flows
1401 int ntstat_userland_count(short proto);
1402 int nstat_userland_get_snapshot(short proto, void **snapshotp, int *countp);
1403 int nstat_userland_list_snapshot(short proto, struct sysctl_req *req, void *userlandsnapshot, int nuserland);
1404 void nstat_userland_release_snapshot(void *snapshot, int nuserland);
1405 #if NTSTAT_SUPPORTS_STANDALONE_SYSCTL
1406 int ntstat_userland_list_n(short proto, struct sysctl_req *req);
1407 #endif
1408 #endif /* SKYWALK */
1409 
1410 // Utilities for userland stats reporting
1411 
1412 // Original form for backwards compatibility, should be replaced in due course
1413 u_int16_t nstat_ifnet_to_flags(struct ifnet *ifp);
1414 
1415 // Preferred form returns a 32 bit quantity, for easier handling of NSTAT_IFNET_IS_WIFI_INFRA and future expansion
1416 u_int32_t nstat_ifnet_to_flags_extended(struct ifnet *ifp);
1417 
1418 
1419 // Generic external provider reporting
1420 
1421 // Each side passes opaque references.
1422 typedef void *nstat_provider_context;   /* This is quoted to the external provider */
1423 typedef void *nstat_context;            /* This is quoted by the external provider when calling nstat */
1424 
1425 // After nstat_provider_stats_open() has been called (and potentially while the open is still executing), netstats can request a refresh of its data
1426 // The various return pointer parameters may be null if the item is not required
1427 // The return code is true for success
1428 typedef bool (nstat_provider_request_vals_fn)(nstat_provider_context ctx,
1429     u_int32_t *ifflagsp,    /* Flags for being on cell/wifi etc, used for filtering */
1430     nstat_counts *countsp,  /* Counts to be filled in */
1431     void *metadatap);       /* A descriptor for the particular provider */
1432 
1433 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1434 // The return value is the amount of space currently required for the extension
1435 typedef size_t (nstat_provider_request_extensions_fn)(nstat_provider_context ctx,
1436     int requested_extension,    /* The extension to be returned */
1437     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1438     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1439 
1440 // Things get started with a call to netstats to say that there’s a new item to become a netstats source
1441 nstat_context nstat_provider_stats_open(nstat_provider_context ctx,
1442     int provider_id,
1443     u_int64_t properties,                   /* The bottom 32 bits can be used as per the interface / connection flags ifflagsp */
1444     nstat_provider_request_vals_fn req_fn,
1445     nstat_provider_request_extensions_fn req_extensions_fn);
1446 
1447 // Note that when the source is closed, netstats will make one last call on the request functions to retrieve final values
1448 void nstat_provider_stats_close(nstat_context nstat_ctx);
1449 
1450 // Events that cause a significant change may be reported via a flags word
1451 void nstat_provider_stats_event(nstat_context nstat_ctx, uint64_t event);
1452 
1453 
1454 // locked_add_64 uses atomic operations on 32bit so the 64bit
1455 // value can be properly read. The values are only ever incremented
1456 // while under the socket lock, so on 64bit we don't actually need
1457 // atomic operations to increment.
1458 #if defined(__LP64__)
1459 #define locked_add_64(__addr, __count) do { \
1460 	*(__addr) += (__count); \
1461 } while (0)
1462 #else
1463 #define locked_add_64(__addr, __count) do { \
1464 	os_atomic_add((__addr), (__count), relaxed); \
1465 } while (0)
1466 #endif
1467 
1468 #endif /* XNU_KERNEL_PRIVATE */
1469 
1470 #endif /* __NTSTAT_H__ */
1471