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