xref: /xnu-10063.101.15/bsd/net/ntstat.h (revision 94d3b452840153a99b38a3a9659680b2a006908e)
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 	, NSTAT_EVENT_SRC_ATTRIBUTION_CHANGE     = 0x00010000
66 #if (DEBUG || DEVELOPMENT)
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 #define NSTAT_IFNET_IS_WIFI_INFRA        0x10000
359 
360 // Not interface properties, but used for filtering in similar fashion
361 #define NSTAT_NECP_CONN_HAS_NET_ACCESS   0x01000000
362 
363 // Not interface properties but conveniently handled in the same flags word
364 #define NSTAT_SOURCE_IS_LISTENER         0x02000000
365 #define NSTAT_SOURCE_IS_INBOUND          0x04000000
366 #define NSTAT_SOURCE_IS_OUTBOUND         0x08000000
367 
368 
369 typedef enum {
370 	NSTAT_PROVIDER_NONE           = 0
371 	, NSTAT_PROVIDER_ROUTE        = 1
372 	, NSTAT_PROVIDER_TCP_KERNEL   = 2
373 	, NSTAT_PROVIDER_TCP_USERLAND = 3
374 	, NSTAT_PROVIDER_UDP_KERNEL   = 4
375 	, NSTAT_PROVIDER_UDP_USERLAND = 5
376 	, NSTAT_PROVIDER_IFNET        = 6
377 	, NSTAT_PROVIDER_SYSINFO      = 7
378 	, NSTAT_PROVIDER_QUIC_USERLAND = 8
379 	, NSTAT_PROVIDER_CONN_USERLAND = 9
380 	, NSTAT_PROVIDER_UDP_SUBFLOW   = 10
381 } nstat_provider_type_t;
382 #define NSTAT_PROVIDER_LAST NSTAT_PROVIDER_UDP_SUBFLOW
383 #define NSTAT_PROVIDER_COUNT (NSTAT_PROVIDER_LAST+1)
384 
385 typedef struct nstat_route_add_param {
386 	union{
387 		struct sockaddr_in      v4;
388 		struct sockaddr_in6     v6;
389 	} dst;
390 	union{
391 		struct sockaddr_in      v4;
392 		struct sockaddr_in6     v6;
393 	} mask;
394 	u_int32_t       ifindex;
395 } nstat_route_add_param;
396 
397 typedef struct nstat_tcp_add_param {
398 	union{
399 		struct sockaddr_in      v4;
400 		struct sockaddr_in6     v6;
401 	} local;
402 	union{
403 		struct sockaddr_in      v4;
404 		struct sockaddr_in6     v6;
405 	} remote;
406 } nstat_tcp_add_param;
407 
408 #define NSTAT_MAX_DOMAIN_NAME_LENGTH           256 /* As per RFC 2181 for full domain name */
409 #define NSTAT_MAX_DOMAIN_OWNER_LENGTH          256
410 #define NSTAT_MAX_DOMAIN_TRACKER_CONTEXT       256
411 #define NSTAT_MAX_DOMAIN_ATTR_BUNDLE_ID        256
412 
413 typedef struct nstat_domain_info {
414 	char            domain_name[NSTAT_MAX_DOMAIN_NAME_LENGTH];
415 	char            domain_owner[NSTAT_MAX_DOMAIN_OWNER_LENGTH];
416 	char            domain_tracker_ctxt[NSTAT_MAX_DOMAIN_TRACKER_CONTEXT];
417 	char            domain_attributed_bundle_id[NSTAT_MAX_DOMAIN_ATTR_BUNDLE_ID];
418 	union{
419 		struct sockaddr_in      v4;
420 		struct sockaddr_in6     v6;
421 	} remote;
422 	bool            is_tracker;
423 	bool            is_non_app_initiated;
424 	bool            is_silent;
425 	uint8_t         reserved[1];
426 } nstat_domain_info __attribute__((aligned(sizeof(u_int64_t))));
427 
428 typedef struct nstat_tcp_descriptor {
429 	u_int64_t       upid __attribute__((aligned(sizeof(u_int64_t))));
430 	u_int64_t       eupid __attribute__((aligned(sizeof(u_int64_t))));
431 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
432 	u_int64_t       timestamp __attribute__((aligned(sizeof(u_int64_t))));
433 
434 	u_int64_t       rx_transfer_size __attribute__((aligned(sizeof(u_int64_t))));
435 	u_int64_t       tx_transfer_size __attribute__((aligned(sizeof(u_int64_t))));
436 
437 	activity_bitmap_t activity_bitmap;
438 
439 	u_int32_t       ifindex;
440 	u_int32_t       state;
441 
442 	u_int32_t       sndbufsize;
443 	u_int32_t       sndbufused;
444 	u_int32_t       rcvbufsize;
445 	u_int32_t       rcvbufused;
446 	u_int32_t       txunacked;
447 	u_int32_t       txwindow;
448 	u_int32_t       txcwindow;
449 	u_int32_t       traffic_class;
450 	u_int32_t       traffic_mgt_flags;
451 
452 	u_int32_t       pid;
453 	u_int32_t       epid;
454 
455 	union{
456 		struct sockaddr_in      v4;
457 		struct sockaddr_in6     v6;
458 	} local;
459 
460 	union{
461 		struct sockaddr_in      v4;
462 		struct sockaddr_in6     v6;
463 	} remote;
464 
465 	char            cc_algo[16];
466 	char            pname[64];
467 
468 	uuid_t          uuid;
469 	uuid_t          euuid;
470 	uuid_t          vuuid;
471 	uuid_t          fuuid;
472 	uid_t           persona_id;
473 	uid_t           uid;
474 	union {
475 		struct tcp_conn_status connstatus;
476 		// On armv7k, tcp_conn_status is 1 byte instead of 4
477 		uint8_t                                 __pad_connstatus[4];
478 	};
479 	uint32_t        ifnet_properties        __attribute__((aligned(4)));
480 	uint8_t         fallback_mode;
481 	uint8_t         reserved[3];
482 } nstat_tcp_descriptor;
483 
484 typedef struct nstat_tcp_add_param      nstat_udp_add_param;
485 
486 typedef struct nstat_udp_descriptor {
487 	u_int64_t       upid __attribute__((aligned(sizeof(u_int64_t))));
488 	u_int64_t       eupid __attribute__((aligned(sizeof(u_int64_t))));
489 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
490 	u_int64_t       timestamp __attribute__((aligned(sizeof(u_int64_t))));
491 
492 	activity_bitmap_t activity_bitmap;
493 
494 	union{
495 		struct sockaddr_in      v4;
496 		struct sockaddr_in6     v6;
497 	} local;
498 
499 	union{
500 		struct sockaddr_in      v4;
501 		struct sockaddr_in6     v6;
502 	} remote;
503 
504 	u_int32_t       ifindex;
505 
506 	u_int32_t       rcvbufsize;
507 	u_int32_t       rcvbufused;
508 	u_int32_t       traffic_class;
509 
510 	u_int32_t       pid;
511 	char            pname[64];
512 	u_int32_t       epid;
513 
514 	uuid_t          uuid;
515 	uuid_t          euuid;
516 	uuid_t          vuuid;
517 	uuid_t          fuuid;
518 	uid_t           persona_id;
519 	uid_t           uid;
520 	uint32_t        ifnet_properties;
521 	uint8_t         fallback_mode;
522 	uint8_t         reserved[3];
523 } nstat_udp_descriptor;
524 
525 /*
526  * XXX For now just typedef'ing TCP Nstat descriptor to nstat_quic_descriptor
527  * as for now they report very similar data.
528  * Later when we extend the QUIC descriptor we can just declare its own
529  * descriptor struct.
530  */
531 typedef struct nstat_tcp_add_param      nstat_quic_add_param;
532 typedef struct nstat_tcp_descriptor     nstat_quic_descriptor;
533 
534 typedef struct nstat_connection_descriptor {
535 	u_int64_t       start_timestamp __attribute__((aligned(sizeof(u_int64_t))));
536 	u_int64_t       timestamp;
537 	u_int64_t       upid;
538 	u_int64_t       eupid;
539 
540 	u_int32_t       pid;
541 	u_int32_t       epid;
542 	u_int32_t       ifnet_properties;
543 	char            pname[64];
544 	uuid_t          uuid;   /* UUID of the app */
545 	uuid_t          euuid;  /* Effective UUID */
546 	uuid_t          cuuid;  /* Connection UUID */
547 	uuid_t          puuid;  /* Parent UUID */
548 	uuid_t          fuuid;  /* Flow UUID */
549 	uid_t           persona_id;
550 	uid_t           uid;
551 	uint8_t         reserved[4];
552 } nstat_connection_descriptor;
553 
554 typedef struct nstat_route_descriptor {
555 	u_int64_t       id __attribute__((aligned(sizeof(u_int64_t))));
556 	u_int64_t       parent_id __attribute__((aligned(sizeof(u_int64_t))));
557 	u_int64_t       gateway_id __attribute__((aligned(sizeof(u_int64_t))));
558 
559 	union{
560 		struct sockaddr_in      v4;
561 		struct sockaddr_in6     v6;
562 		struct sockaddr         sa;
563 	} dst;
564 
565 	union{
566 		struct sockaddr_in      v4;
567 		struct sockaddr_in6     v6;
568 		struct sockaddr         sa;
569 	} mask;
570 
571 	union{
572 		struct sockaddr_in      v4;
573 		struct sockaddr_in6     v6;
574 		struct sockaddr         sa;
575 	} gateway;
576 
577 	u_int32_t       ifindex;
578 	u_int32_t       flags;
579 
580 	u_int8_t        reserved[4];
581 } nstat_route_descriptor;
582 
583 typedef struct nstat_ifnet_add_param {
584 	u_int64_t       threshold __attribute__((aligned(sizeof(u_int64_t))));
585 	u_int32_t       ifindex;
586 
587 	u_int8_t        reserved[4];
588 } nstat_ifnet_add_param;
589 
590 typedef struct nstat_ifnet_desc_cellular_status {
591 	u_int32_t valid_bitmask; /* indicates which fields are valid */
592 #define NSTAT_IFNET_DESC_CELL_LINK_QUALITY_METRIC_VALID         0x1
593 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_BANDWIDTH_VALID      0x2
594 #define NSTAT_IFNET_DESC_CELL_UL_MAX_BANDWIDTH_VALID            0x4
595 #define NSTAT_IFNET_DESC_CELL_UL_MIN_LATENCY_VALID              0x8
596 #define NSTAT_IFNET_DESC_CELL_UL_EFFECTIVE_LATENCY_VALID        0x10
597 #define NSTAT_IFNET_DESC_CELL_UL_MAX_LATENCY_VALID              0x20
598 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_VALID              0x40
599 #define NSTAT_IFNET_DESC_CELL_UL_BYTES_LOST_VALID               0x80
600 #define NSTAT_IFNET_DESC_CELL_UL_MIN_QUEUE_SIZE_VALID           0x100
601 #define NSTAT_IFNET_DESC_CELL_UL_AVG_QUEUE_SIZE_VALID           0x200
602 #define NSTAT_IFNET_DESC_CELL_UL_MAX_QUEUE_SIZE_VALID           0x400
603 #define NSTAT_IFNET_DESC_CELL_DL_EFFECTIVE_BANDWIDTH_VALID      0x800
604 #define NSTAT_IFNET_DESC_CELL_DL_MAX_BANDWIDTH_VALID            0x1000
605 #define NSTAT_IFNET_DESC_CELL_CONFIG_INACTIVITY_TIME_VALID      0x2000
606 #define NSTAT_IFNET_DESC_CELL_CONFIG_BACKOFF_TIME_VALID         0x4000
607 #define NSTAT_IFNET_DESC_CELL_MSS_RECOMMENDED_VALID             0x8000
608 	u_int32_t link_quality_metric;
609 	u_int32_t ul_effective_bandwidth; /* Measured uplink bandwidth based on
610 	                                   *  current activity (bps) */
611 	u_int32_t ul_max_bandwidth; /* Maximum supported uplink bandwidth
612 	                             *  (bps) */
613 	u_int32_t ul_min_latency; /* min expected uplink latency for first hop
614 	                           *  (ms) */
615 	u_int32_t ul_effective_latency; /* current expected uplink latency for
616 	                                 *  first hop (ms) */
617 	u_int32_t ul_max_latency; /* max expected uplink latency first hop
618 	                           *  (ms) */
619 	u_int32_t ul_retxt_level; /* Retransmission metric */
620 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_NONE       1
621 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_LOW        2
622 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_MEDIUM     3
623 #define NSTAT_IFNET_DESC_CELL_UL_RETXT_LEVEL_HIGH       4
624 
625 	u_int32_t ul_bytes_lost; /* % of total bytes lost on uplink in Q10
626 	                          *  format */
627 	u_int32_t ul_min_queue_size; /* minimum bytes in queue */
628 	u_int32_t ul_avg_queue_size; /* average bytes in queue */
629 	u_int32_t ul_max_queue_size; /* maximum bytes in queue */
630 	u_int32_t dl_effective_bandwidth; /* Measured downlink bandwidth based
631 	                                   *  on current activity (bps) */
632 	u_int32_t dl_max_bandwidth; /* Maximum supported downlink bandwidth
633 	                             *  (bps) */
634 	u_int32_t config_inactivity_time; /* ms */
635 	u_int32_t config_backoff_time; /* new connections backoff time in ms */
636 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_NONE   0x0
637 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_MEDIUM 0x1
638 #define NSTAT_IFNET_DESC_MSS_RECOMMENDED_LOW    0x2
639 	u_int16_t mss_recommended; /* recommended MSS */
640 	u_int8_t        reserved[2];
641 } nstat_ifnet_desc_cellular_status;
642 
643 typedef struct nstat_ifnet_desc_wifi_status {
644 	u_int32_t valid_bitmask;
645 #define NSTAT_IFNET_DESC_WIFI_LINK_QUALITY_METRIC_VALID         0x1
646 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_BANDWIDTH_VALID      0x2
647 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_BANDWIDTH_VALID            0x4
648 #define NSTAT_IFNET_DESC_WIFI_UL_MIN_LATENCY_VALID              0x8
649 #define NSTAT_IFNET_DESC_WIFI_UL_EFFECTIVE_LATENCY_VALID        0x10
650 #define NSTAT_IFNET_DESC_WIFI_UL_MAX_LATENCY_VALID              0x20
651 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_VALID              0x40
652 #define NSTAT_IFNET_DESC_WIFI_UL_ERROR_RATE_VALID               0x80
653 #define NSTAT_IFNET_DESC_WIFI_UL_BYTES_LOST_VALID               0x100
654 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_BANDWIDTH_VALID      0x200
655 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_BANDWIDTH_VALID            0x400
656 #define NSTAT_IFNET_DESC_WIFI_DL_MIN_LATENCY_VALID              0x800
657 #define NSTAT_IFNET_DESC_WIFI_DL_EFFECTIVE_LATENCY_VALID        0x1000
658 #define NSTAT_IFNET_DESC_WIFI_DL_MAX_LATENCY_VALID              0x2000
659 #define NSTAT_IFNET_DESC_WIFI_DL_ERROR_RATE_VALID               0x4000
660 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_VALID            0x8000
661 #define NSTAT_IFNET_DESC_WIFI_CONFIG_MULTICAST_RATE_VALID       0x10000
662 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_COUNT_VALID           0x20000
663 #define NSTAT_IFNET_DESC_WIFI_CONFIG_SCAN_DURATION_VALID        0x40000
664 	u_int32_t link_quality_metric; /* link quality metric */
665 	u_int32_t ul_effective_bandwidth; /* Measured uplink bandwidth based on
666 	                                   *  current activity (bps) */
667 	u_int32_t ul_max_bandwidth; /* Maximum supported uplink bandwidth
668 	                             *  (bps) */
669 	u_int32_t ul_min_latency; /* min expected uplink latency for first hop
670 	                           *  (ms) */
671 	u_int32_t ul_effective_latency; /* current expected uplink latency for
672 	                                 *  first hop (ms) */
673 	u_int32_t ul_max_latency; /* max expected uplink latency for first hop
674 	                           *  (ms) */
675 	u_int32_t ul_retxt_level; /* Retransmission metric */
676 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_NONE       1
677 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_LOW        2
678 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_MEDIUM     3
679 #define NSTAT_IFNET_DESC_WIFI_UL_RETXT_LEVEL_HIGH       4
680 
681 	u_int32_t ul_bytes_lost; /* % of total bytes lost on uplink in Q10
682 	                          *  format */
683 	u_int32_t ul_error_rate; /* % of bytes dropped on uplink after many
684 	                          *  retransmissions in Q10 format */
685 	u_int32_t dl_effective_bandwidth; /* Measured downlink bandwidth based
686 	                                   *  on current activity (bps) */
687 	u_int32_t dl_max_bandwidth; /* Maximum supported downlink bandwidth
688 	                             *  (bps) */
689 	/*
690 	 * The download latency values indicate the time AP may have to wait
691 	 * for the  driver to receive the packet. These values give the range
692 	 * of expected latency mainly due to co-existence events and channel
693 	 * hopping where the interface becomes unavailable.
694 	 */
695 	u_int32_t dl_min_latency; /* min expected latency for first hop in ms */
696 	u_int32_t dl_effective_latency; /* current expected latency for first
697 	                                 *  hop in ms */
698 	u_int32_t dl_max_latency; /* max expected latency for first hop in ms */
699 	u_int32_t dl_error_rate; /* % of CRC or other errors in Q10 format */
700 	u_int32_t config_frequency; /* 2.4 or 5 GHz */
701 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_2_4_GHZ  1
702 #define NSTAT_IFNET_DESC_WIFI_CONFIG_FREQUENCY_5_0_GHZ  2
703 	u_int32_t config_multicast_rate; /* bps */
704 	u_int32_t scan_count; /* scan count during the previous period */
705 	u_int32_t scan_duration; /* scan duration in ms */
706 } nstat_ifnet_desc_wifi_status;
707 
708 enum{
709 	NSTAT_IFNET_DESC_LINK_STATUS_TYPE_NONE = 0
710 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_CELLULAR = 1
711 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_WIFI = 2
712 	, NSTAT_IFNET_DESC_LINK_STATUS_TYPE_ETHERNET = 3
713 };
714 
715 typedef struct nstat_ifnet_desc_link_status {
716 	u_int32_t       link_status_type;
717 	union {
718 		nstat_ifnet_desc_cellular_status        cellular;
719 		nstat_ifnet_desc_wifi_status            wifi;
720 	} u;
721 } nstat_ifnet_desc_link_status;
722 
723 #ifndef IF_DESCSIZE
724 #define IF_DESCSIZE 128
725 #endif
726 typedef struct nstat_ifnet_descriptor {
727 	u_int64_t                       threshold __attribute__((aligned(sizeof(u_int64_t))));
728 	u_int32_t                       ifindex;
729 	nstat_ifnet_desc_link_status    link_status;
730 	unsigned int            type;
731 	char                            description[IF_DESCSIZE];
732 	char                            name[IFNAMSIZ + 1];
733 	u_int8_t                        reserved[3];
734 } nstat_ifnet_descriptor;
735 
736 typedef struct nstat_sysinfo_descriptor {
737 	u_int32_t       flags;
738 } nstat_sysinfo_descriptor;
739 
740 typedef struct nstat_sysinfo_add_param {
741 	/* To indicate which system level information should be collected */
742 	u_int32_t       flags;
743 } nstat_sysinfo_add_param;
744 
745 #define NSTAT_SYSINFO_MBUF_STATS        0x0001
746 #define NSTAT_SYSINFO_TCP_STATS         0x0002
747 #define NSTAT_SYSINFO_IFNET_ECN_STATS   0x0003
748 #define NSTAT_SYSINFO_LIM_STATS         0x0004  /* Low Internet mode stats */
749 #define NSTAT_SYSINFO_NET_API_STATS     0x0005  /* API and KPI stats */
750 
751 #pragma mark -- Network Statistics User Client --
752 
753 #define NET_STAT_CONTROL_NAME   "com.apple.network.statistics"
754 
755 enum{
756 	// generic response messages
757 	NSTAT_MSG_TYPE_SUCCESS               = 0
758 	, NSTAT_MSG_TYPE_ERROR               = 1
759 
760 	    // Requests
761 	, NSTAT_MSG_TYPE_ADD_SRC             = 1001
762 	, NSTAT_MSG_TYPE_ADD_ALL_SRCS        = 1002
763 	, NSTAT_MSG_TYPE_REM_SRC             = 1003
764 	, NSTAT_MSG_TYPE_QUERY_SRC           = 1004
765 	, NSTAT_MSG_TYPE_GET_SRC_DESC        = 1005
766 	, NSTAT_MSG_TYPE_SET_FILTER          = 1006 // Obsolete
767 	, NSTAT_MSG_TYPE_GET_UPDATE          = 1007
768 	, NSTAT_MSG_TYPE_SUBSCRIBE_SYSINFO   = 1008
769 
770 	    // Responses/Notfications
771 	, NSTAT_MSG_TYPE_SRC_ADDED           = 10001
772 	, NSTAT_MSG_TYPE_SRC_REMOVED         = 10002
773 	, NSTAT_MSG_TYPE_SRC_DESC            = 10003
774 	, NSTAT_MSG_TYPE_SRC_COUNTS          = 10004
775 	, NSTAT_MSG_TYPE_SYSINFO_COUNTS      = 10005
776 	, NSTAT_MSG_TYPE_SRC_UPDATE          = 10006
777 	, NSTAT_MSG_TYPE_SRC_EXTENDED_UPDATE = 10007
778 };
779 
780 enum{
781 	NSTAT_SRC_REF_ALL       = 0xffffffffffffffffULL
782 	, NSTAT_SRC_REF_INVALID  = 0
783 };
784 
785 /* Source-level filters */
786 enum{
787 	NSTAT_FILTER_NOZEROBYTES             = 0x00000001
788 };
789 
790 
791 /* Types of extended update information, used in setting initial filters as well as to identify returned extensions */
792 /* A contiguous range currently limited 1..31 due to being passed as the top 32 bits of filter */
793 enum{
794 	NSTAT_EXTENDED_UPDATE_TYPE_UNKNOWN              = 0
795 	, NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN             = 1
796 	, NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV           = 2
797 	, NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV  = 3
798 	, NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN    = 4
799 	, NSTAT_EXTENDED_UPDATE_TYPE_FUUID              = 5
800 };
801 
802 #define NSTAT_EXTENDED_UPDATE_TYPE_MIN  NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN
803 #define NSTAT_EXTENDED_UPDATE_TYPE_MAX  NSTAT_EXTENDED_UPDATE_TYPE_FUUID
804 
805 
806 #define NSTAT_EXTENDED_UPDATE_FLAG_MASK    0x00ffffffull    /* Maximum of 24 extension types allowed due to restrictions on specifying via filter flags */
807 
808 #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 */
809 
810 /* Provider-level filters */
811 #define NSTAT_FILTER_ACCEPT_UNKNOWN          0x0000000000000001ull
812 #define NSTAT_FILTER_ACCEPT_LOOPBACK         0x0000000000000002ull
813 #define NSTAT_FILTER_ACCEPT_CELLULAR         0x0000000000000004ull
814 #define NSTAT_FILTER_ACCEPT_WIFI             0x0000000000000008ull
815 #define NSTAT_FILTER_ACCEPT_WIRED            0x0000000000000010ull
816 #define NSTAT_FILTER_ACCEPT_AWDL             0x0000000000000020ull
817 #define NSTAT_FILTER_ACCEPT_EXPENSIVE        0x0000000000000040ull
818 #define NSTAT_FILTER_ACCEPT_CELLFALLBACK     0x0000000000000100ull
819 #define NSTAT_FILTER_ACCEPT_COMPANIONLINK    0x0000000000000200ull
820 #define NSTAT_FILTER_ACCEPT_IS_CONSTRAINED   0x0000000000000400ull
821 #define NSTAT_FILTER_ACCEPT_IS_LOCAL         0x0000000000000800ull
822 #define NSTAT_FILTER_ACCEPT_IS_NON_LOCAL     0x0000000000001000ull
823 #define NSTAT_FILTER_ACCEPT_ROUTE_VAL_ERR    0x0000000000002000ull
824 #define NSTAT_FILTER_ACCEPT_FLOWSWITCH_ERR   0x0000000000004000ull
825 #define NSTAT_FILTER_ACCEPT_WIFI_LLW         0x0000000000008000ull
826 #define NSTAT_FILTER_ACCEPT_WIFI_INFRA       0x0000000000010000ull
827 #define NSTAT_FILTER_IFNET_FLAGS             0x000000000001FFFFull
828 
829 #define NSTAT_FILTER_UDP_INTERFACE_ATTACH    0x0000000000020000ull
830 #define NSTAT_FILTER_UDP_FLAGS               0x0000000000020000ull
831 
832 #define NSTAT_FILTER_TCP_INTERFACE_ATTACH    0x0000000000040000ull
833 #define NSTAT_FILTER_TCP_NO_EARLY_CLOSE      0x0000000000080000ull
834 #define NSTAT_FILTER_TCP_FLAGS               0x00000000000C0000ull
835 
836 #define NSTAT_FILTER_SUPPRESS_SRC_ADDED      0x0000000000100000ull
837 #define NSTAT_FILTER_REQUIRE_SRC_ADDED       0x0000000000200000ull
838 #define NSTAT_FILTER_PROVIDER_NOZEROBYTES    0x0000000000400000ull
839 
840 #define NSTAT_FILTER_CONN_HAS_NET_ACCESS     0x0000000001000000ull
841 #define NSTAT_FILTER_CONN_FLAGS              0x0000000001000000ull
842 
843 /* In this context, boring == no change from previous report */
844 #define NSTAT_FILTER_SUPPRESS_BORING_CLOSE   0x0000000010000000ull  /* No final update, only NSTAT_MSG_TYPE_SRC_REMOVED */
845 #define NSTAT_FILTER_SUPPRESS_BORING_POLL    0x0000000020000000ull  /* Only for poll-all, not poll specific source */
846 #define NSTAT_FILTER_SUPPRESS_BORING_FLAGS   (NSTAT_FILTER_SUPPRESS_BORING_CLOSE|NSTAT_FILTER_SUPPRESS_BORING_POLL)
847 
848 #define NSTAT_FILTER_SPECIFIC_USER_BY_PID    0x0000000100000000ull
849 #define NSTAT_FILTER_SPECIFIC_USER_BY_EPID   0x0000000200000000ull
850 #define NSTAT_FILTER_SPECIFIC_USER_BY_UUID   0x0000000400000000ull
851 #define NSTAT_FILTER_SPECIFIC_USER_BY_EUUID  0x0000000800000000ull
852 #define NSTAT_FILTER_SPECIFIC_USER           0x0000000F00000000ull
853 
854 #define NSTAT_FILTER_INITIAL_PROPERTIES      0x0000001000000000ull  /* For providers that give "properties" on open, apply the filter to the properties */
855                                                                     /* and permanently discard unless the filter allows through */
856 #define NSTAT_FILTER_FLAGS_RESERVED          0x000000E000000000ul
857 
858 #define NSTAT_FILTER_IFNET_AND_CONN_FLAGS    (NSTAT_FILTER_IFNET_FLAGS|NSTAT_FILTER_CONN_FLAGS)
859 
860 #define NSTAT_EXTENSION_FILTER_DOMAIN_INFO              (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
861 #define NSTAT_EXTENSION_FILTER_NECP_TLV                 (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
862 #define NSTAT_EXTENSION_FILTER_ORIGINAL_NECP_TLV        (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
863 #define NSTAT_EXTENSION_FILTER_ORIGINAL_DOMAIN_INFO     (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
864 #define NSTAT_EXTENSION_FILTER_MASK                     (NSTAT_EXTENDED_UPDATE_FLAG_MASK << NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT)
865 
866 enum{
867 	NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE   = 1 << 0,
868 	NSTAT_MSG_HDR_FLAG_CONTINUATION         = 1 << 1,
869 	NSTAT_MSG_HDR_FLAG_CLOSING              = 1 << 2,
870 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_DROP    = 1 << 3,
871 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_FILTER  = 1 << 4,
872 };
873 
874 typedef struct nstat_msg_hdr {
875 	u_int64_t       context __attribute__((aligned(sizeof(u_int64_t))));
876 	u_int32_t       type;
877 	u_int16_t       length;
878 	u_int16_t       flags;
879 } nstat_msg_hdr;
880 
881 #define MAX_NSTAT_MSG_HDR_LENGTH    65532
882 
883 typedef struct nstat_msg_error {
884 	nstat_msg_hdr   hdr;
885 	u_int32_t               error;  // errno error
886 	u_int8_t                reserved[4];
887 } nstat_msg_error;
888 
889 #define NSTAT_ADD_SRC_FIELDS            \
890 	nstat_msg_hdr		hdr;            \
891 	nstat_provider_id_t	provider;       \
892 	u_int8_t			reserved[4]     \
893 
894 typedef struct nstat_msg_add_src {
895 	NSTAT_ADD_SRC_FIELDS;
896 	u_int8_t        param[];
897 } nstat_msg_add_src_req;
898 
899 typedef struct nstat_msg_add_src_header {
900 	NSTAT_ADD_SRC_FIELDS;
901 } nstat_msg_add_src_header;
902 
903 typedef struct nstat_msg_add_src_convenient {
904 	nstat_msg_add_src_header        hdr;
905 	union {
906 		nstat_route_add_param   route;
907 		nstat_tcp_add_param             tcp;
908 		nstat_udp_add_param             udp;
909 		nstat_ifnet_add_param   ifnet;
910 		nstat_sysinfo_add_param sysinfo;
911 	};
912 } nstat_msg_add_src_convenient;
913 
914 #undef NSTAT_ADD_SRC_FIELDS
915 
916 typedef struct nstat_msg_add_all_srcs {
917 	nstat_msg_hdr           hdr;
918 	u_int64_t               filter __attribute__((aligned(sizeof(u_int64_t))));
919 	nstat_event_flags_t     events __attribute__((aligned(sizeof(u_int64_t))));
920 	nstat_provider_id_t     provider;
921 	pid_t                   target_pid;
922 	uuid_t                  target_uuid;
923 } nstat_msg_add_all_srcs;
924 
925 typedef struct nstat_msg_src_added {
926 	nstat_msg_hdr           hdr;
927 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
928 	nstat_provider_id_t     provider;
929 	u_int8_t                reserved[4];
930 } nstat_msg_src_added;
931 
932 typedef struct nstat_msg_rem_src {
933 	nstat_msg_hdr           hdr;
934 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
935 } nstat_msg_rem_src_req;
936 
937 typedef struct nstat_msg_get_src_description {
938 	nstat_msg_hdr           hdr;
939 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
940 } nstat_msg_get_src_description;
941 
942 typedef struct nstat_msg_set_filter {
943 	nstat_msg_hdr           hdr;
944 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
945 	u_int32_t               filter;
946 	u_int8_t                reserved[4];
947 } nstat_msg_set_filter;
948 
949 #define NSTAT_SRC_DESCRIPTION_FIELDS                                                                                            \
950 	nstat_msg_hdr		hdr;                                                                                                            \
951 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
952 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
953 	nstat_provider_id_t	provider;                                                                                                       \
954 	u_int8_t			reserved[4]
955 
956 typedef struct nstat_msg_src_description {
957 	NSTAT_SRC_DESCRIPTION_FIELDS;
958 	u_int8_t        data[];
959 } nstat_msg_src_description;
960 
961 typedef struct nstat_msg_src_description_header {
962 	NSTAT_SRC_DESCRIPTION_FIELDS;
963 } nstat_msg_src_description_header;
964 
965 typedef struct nstat_msg_src_description_convenient {
966 	nstat_msg_src_description_header    hdr;
967 	union {
968 		nstat_tcp_descriptor            tcp;
969 		nstat_udp_descriptor            udp;
970 		nstat_route_descriptor          route;
971 		nstat_ifnet_descriptor          ifnet;
972 		nstat_sysinfo_descriptor        sysinfo;
973 		nstat_quic_descriptor           quic;
974 	};
975 } nstat_msg_src_description_convenient;
976 
977 #undef NSTAT_SRC_DESCRIPTION_FIELDS
978 
979 typedef struct nstat_msg_query_src {
980 	nstat_msg_hdr           hdr;
981 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
982 } nstat_msg_query_src_req;
983 
984 typedef struct nstat_msg_src_counts {
985 	nstat_msg_hdr           hdr;
986 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
987 	nstat_event_flags_t     event_flags __attribute__((aligned(sizeof(u_int64_t))));
988 	nstat_counts            counts;
989 } nstat_msg_src_counts;
990 
991 #define NSTAT_SRC_UPDATE_FIELDS                                                                                                         \
992 	nstat_msg_hdr		hdr;                                                                                                            \
993 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
994 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
995 	nstat_counts		counts;                                                                                                         \
996 	nstat_provider_id_t	provider;                                                                                                       \
997 	u_int8_t			reserved[4]
998 
999 typedef struct nstat_msg_src_update {
1000 	NSTAT_SRC_UPDATE_FIELDS;
1001 	u_int8_t        data[];
1002 } nstat_msg_src_update;
1003 
1004 typedef struct nstat_msg_src_update_hdr {
1005 	NSTAT_SRC_UPDATE_FIELDS;
1006 } nstat_msg_src_update_hdr;
1007 
1008 typedef struct nstat_msg_src_update_tcp {
1009 	NSTAT_SRC_UPDATE_FIELDS;
1010 	nstat_tcp_descriptor            tcp_desc;
1011 } nstat_msg_src_update_tcp;
1012 
1013 typedef struct nstat_msg_src_update_udp {
1014 	NSTAT_SRC_UPDATE_FIELDS;
1015 	nstat_udp_descriptor            udp_desc;
1016 } nstat_msg_src_update_udp;
1017 
1018 typedef struct nstat_msg_src_update_quic {
1019 	NSTAT_SRC_UPDATE_FIELDS;
1020 	nstat_quic_descriptor           quic_desc;
1021 } nstat_msg_src_update_quic;
1022 
1023 typedef struct nstat_msg_src_update_conn {
1024 	NSTAT_SRC_UPDATE_FIELDS;
1025 	nstat_connection_descriptor     conn_desc;
1026 } nstat_msg_src_update_conn;
1027 
1028 
1029 typedef struct nstat_msg_src_update_convenient {
1030 	nstat_msg_src_update_hdr                hdr;
1031 	union {
1032 		nstat_tcp_descriptor            tcp;
1033 		nstat_udp_descriptor            udp;
1034 		nstat_route_descriptor          route;
1035 		nstat_ifnet_descriptor          ifnet;
1036 		nstat_sysinfo_descriptor        sysinfo;
1037 		nstat_quic_descriptor           quic;
1038 		nstat_connection_descriptor     conn;
1039 	};
1040 } nstat_msg_src_update_convenient;
1041 
1042 typedef struct nstat_msg_src_extended_item_hdr {
1043 	u_int32_t       type;
1044 	u_int32_t       length;
1045 } nstat_msg_src_extended_item_hdr __attribute__((aligned(sizeof(u_int64_t))));;
1046 
1047 typedef struct nstat_msg_src_extended_item {
1048 	nstat_msg_src_extended_item_hdr         hdr;
1049 	u_int8_t                                data[];
1050 } nstat_msg_src_extended_item;
1051 
1052 typedef struct nstat_msg_src_extended_tcp_update {
1053 	nstat_msg_src_update_hdr                hdr;
1054 	nstat_tcp_descriptor                    tcp;
1055 	nstat_msg_src_extended_item_hdr         extension_hdr;
1056 	u_int8_t                                data[];
1057 } nstat_msg_src_extended_tcp_update;
1058 
1059 typedef struct nstat_msg_src_extended_udp_update {
1060 	nstat_msg_src_update_hdr                hdr;
1061 	nstat_udp_descriptor                    udp;
1062 	nstat_msg_src_extended_item_hdr         extension_hdr;
1063 	u_int8_t                                data[];
1064 } nstat_msg_src_extended_udp_update;
1065 
1066 typedef struct nstat_msg_src_extended_quic_update {
1067 	nstat_msg_src_update_hdr                hdr;
1068 	nstat_quic_descriptor                   quic;
1069 	nstat_msg_src_extended_item_hdr         extension_hdr;
1070 	u_int8_t                                data[];
1071 } nstat_msg_src_extended_quic_update;
1072 
1073 typedef struct nstat_msg_src_extended_conn_update {
1074 	nstat_msg_src_update_hdr                hdr;
1075 	nstat_connection_descriptor             conn;
1076 	nstat_msg_src_extended_item_hdr         extension_hdr;
1077 	u_int8_t                                data[];
1078 } nstat_msg_src_extended_conn_update;
1079 
1080 /* While the only type of extended update is for domain information, we can fully define the structure */
1081 typedef struct nstat_msg_src_tcp_update_domain_extension {
1082 	nstat_msg_src_update_hdr                hdr;
1083 	nstat_tcp_descriptor                    tcp;
1084 	nstat_msg_src_extended_item_hdr         extension_hdr;
1085 	nstat_domain_info                       domain_info;
1086 } nstat_msg_src_tcp_update_domain_extension;
1087 
1088 typedef struct nstat_msg_src_udp_update_domain_extension {
1089 	nstat_msg_src_update_hdr                hdr;
1090 	nstat_udp_descriptor                    udp;
1091 	nstat_msg_src_extended_item_hdr         extension_hdr;
1092 	nstat_domain_info                       domain_info;
1093 } nstat_msg_src_udp_update_domain_extension;
1094 
1095 typedef struct nstat_msg_src_quic_update_domain_extension {
1096 	nstat_msg_src_update_hdr                hdr;
1097 	nstat_quic_descriptor                   quic;
1098 	nstat_msg_src_extended_item_hdr         extension_hdr;
1099 	nstat_domain_info                       domain_info;
1100 } nstat_msg_src_quic_update_domain_extension;
1101 
1102 typedef struct nstat_msg_src_update_domain_extension_convenient {
1103 	nstat_msg_src_tcp_update_domain_extension       tcp;
1104 	nstat_msg_src_udp_update_domain_extension       udp;
1105 	nstat_msg_src_quic_update_domain_extension      quic;
1106 } nstat_msg_src_update_domain_extension_convenient;
1107 
1108 #undef NSTAT_SRC_UPDATE_FIELDS
1109 
1110 typedef struct nstat_msg_src_removed {
1111 	nstat_msg_hdr           hdr;
1112 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1113 } nstat_msg_src_removed;
1114 
1115 typedef struct nstat_msg_sysinfo_counts {
1116 	nstat_msg_hdr           hdr;
1117 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1118 	nstat_sysinfo_counts    counts;
1119 }  nstat_msg_sysinfo_counts;
1120 
1121 #pragma mark -- Statitiscs about Network Statistics --
1122 
1123 struct nstat_stats {
1124 	u_int32_t nstat_successmsgfailures;
1125 	u_int32_t nstat_sendcountfailures;
1126 	u_int32_t nstat_sysinfofailures;
1127 	u_int32_t nstat_srcupatefailures;
1128 	u_int32_t nstat_descriptionfailures;
1129 	u_int32_t nstat_msgremovedfailures;
1130 	u_int32_t nstat_srcaddedfailures;
1131 	u_int32_t nstat_msgerrorfailures;
1132 	u_int32_t nstat_copy_descriptor_failures;
1133 	u_int32_t nstat_provider_counts_failures;
1134 	u_int32_t nstat_control_send_description_failures;
1135 	u_int32_t nstat_control_send_goodbye_failures;
1136 	u_int32_t nstat_flush_accumulated_msgs_failures;
1137 	u_int32_t nstat_accumulate_msg_failures;
1138 	u_int32_t nstat_control_cleanup_source_failures;
1139 	u_int32_t nstat_handle_msg_failures;
1140 };
1141 
1142 #endif /* PRIVATE */
1143 
1144 #ifdef XNU_KERNEL_PRIVATE
1145 #include <sys/mcache.h>
1146 
1147 #if (DEBUG || DEVELOPMENT)
1148 extern int nstat_test_privacy_transparency;
1149 #endif /* (DEBUG || DEVELOPMENT) */
1150 
1151 #pragma mark -- System Information Internal Support --
1152 
1153 typedef struct nstat_sysinfo_tcp_stats {
1154 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1155 	u_int32_t       ipv4_avgrtt;    /* Average RTT for IPv4 */
1156 	u_int32_t       ipv6_avgrtt;    /* Average RTT for IPv6 */
1157 	u_int32_t       send_plr;       /* Average uplink packet loss rate */
1158 	u_int32_t       recv_plr;       /* Average downlink packet loss rate */
1159 	u_int32_t       send_tlrto_rate; /* Average rxt timeout after tail loss */
1160 	u_int32_t       send_reorder_rate; /* Average packet reordering rate */
1161 	u_int32_t       connection_attempts; /* TCP client connection attempts */
1162 	u_int32_t       connection_accepts; /* TCP server connection accepts */
1163 	u_int32_t       ecn_client_enabled; /* Global setting for ECN client side */
1164 	u_int32_t       ecn_server_enabled; /* Global setting for ECN server side */
1165 	u_int32_t       ecn_client_setup; /* Attempts to setup TCP client connection with ECN */
1166 	u_int32_t       ecn_server_setup; /* Attempts to setup TCP server connection with ECN */
1167 	u_int32_t       ecn_client_success; /* Number of successful negotiations of ECN for a client connection */
1168 	u_int32_t       ecn_server_success; /* Number of successful negotiations of ECN for a server connection */
1169 	u_int32_t       ecn_not_supported; /* Number of falbacks to Non-ECN, no support from peer */
1170 	u_int32_t       ecn_lost_syn;   /* Number of SYNs lost with ECN bits */
1171 	u_int32_t       ecn_lost_synack; /* Number of SYN-ACKs lost with ECN bits */
1172 	u_int32_t       ecn_recv_ce;    /* Number of CEs received from network */
1173 	u_int32_t       ecn_recv_ece;   /* Number of ECEs received from receiver */
1174 	u_int32_t       ecn_sent_ece;   /* Number of ECEs sent in response to CE */
1175 	u_int32_t       ecn_conn_recv_ce; /* Number of connections using ECN received CE at least once */
1176 	u_int32_t       ecn_conn_recv_ece; /* Number of connections using ECN received ECE at least once */
1177 	u_int32_t       ecn_conn_plnoce; /* Number of connections using ECN seen packet loss but never received CE */
1178 	u_int32_t       ecn_conn_pl_ce; /* Number of connections using ECN seen packet loss and CE */
1179 	u_int32_t       ecn_conn_nopl_ce; /* Number of connections using ECN with no packet loss but received CE */
1180 	u_int32_t       ecn_fallback_synloss; /* Number of times we did fall back due to SYN-Loss */
1181 	u_int32_t       ecn_fallback_reorder; /* Number of times we fallback because we detected the PAWS-issue */
1182 	u_int32_t       ecn_fallback_ce; /* Number of times we fallback because we received too many CEs */
1183 	u_int32_t       tfo_syn_data_rcv;       /* Number of SYN+data received with valid cookie */
1184 	u_int32_t       tfo_cookie_req_rcv;/* Number of TFO cookie-requests received */
1185 	u_int32_t       tfo_cookie_sent;        /* Number of TFO-cookies offered to the client */
1186 	u_int32_t       tfo_cookie_invalid;/* Number of invalid TFO-cookies received */
1187 	u_int32_t       tfo_cookie_req; /* Number of SYNs with cookie request received*/
1188 	u_int32_t       tfo_cookie_rcv; /* Number of SYN/ACKs with Cookie received */
1189 	u_int32_t       tfo_syn_data_sent;      /* Number of SYNs+data+cookie sent */
1190 	u_int32_t       tfo_syn_data_acked;/* Number of times our SYN+data has been acknowledged */
1191 	u_int32_t       tfo_syn_loss;   /* Number of times SYN+TFO has been lost and we fallback */
1192 	u_int32_t       tfo_blackhole;  /* Number of times SYN+TFO has been lost and we fallback */
1193 	u_int32_t       tfo_cookie_wrong;       /* TFO-cookie we sent was wrong */
1194 	u_int32_t       tfo_no_cookie_rcv;      /* We asked for a cookie but didn't get one */
1195 	u_int32_t       tfo_heuristics_disable; /* TFO got disabled due to heuristics */
1196 	u_int32_t       tfo_sndblackhole;       /* TFO got blackholed in the sending direction */
1197 	u_int32_t       mptcp_handover_attempt; /* Total number of MPTCP-attempts using handover mode */
1198 	u_int32_t       mptcp_interactive_attempt;      /* Total number of MPTCP-attempts using interactive mode */
1199 	u_int32_t       mptcp_aggregate_attempt;        /* Total number of MPTCP-attempts using aggregate mode */
1200 	u_int32_t       mptcp_fp_handover_attempt; /* Same as previous three but only for first-party apps */
1201 	u_int32_t       mptcp_fp_interactive_attempt;
1202 	u_int32_t       mptcp_fp_aggregate_attempt;
1203 	u_int32_t       mptcp_heuristic_fallback;       /* Total number of MPTCP-connections that fell back due to heuristics */
1204 	u_int32_t       mptcp_fp_heuristic_fallback;    /* Same as previous but for first-party apps */
1205 	u_int32_t       mptcp_handover_success_wifi;    /* Total number of successfull handover-mode connections that *started* on WiFi */
1206 	u_int32_t       mptcp_handover_success_cell;    /* Total number of successfull handover-mode connections that *started* on Cell */
1207 	u_int32_t       mptcp_interactive_success;              /* Total number of interactive-mode connections that negotiated MPTCP */
1208 	u_int32_t       mptcp_aggregate_success;                /* Same as previous but for aggregate */
1209 	u_int32_t       mptcp_fp_handover_success_wifi; /* Same as previous four, but for first-party apps */
1210 	u_int32_t       mptcp_fp_handover_success_cell;
1211 	u_int32_t       mptcp_fp_interactive_success;
1212 	u_int32_t       mptcp_fp_aggregate_success;
1213 	u_int32_t       mptcp_handover_cell_from_wifi;  /* Total number of connections that use cell in handover-mode (coming from WiFi) */
1214 	u_int32_t       mptcp_handover_wifi_from_cell;  /* Total number of connections that use WiFi in handover-mode (coming from cell) */
1215 	u_int32_t       mptcp_interactive_cell_from_wifi;       /* Total number of connections that use cell in interactive mode (coming from WiFi) */
1216 	u_int32_t       mptcp_back_to_wifi;     /* Total number of connections that succeed to move traffic away from cell (when starting on cell) */
1217 	u_int64_t       mptcp_handover_cell_bytes;              /* Total number of bytes sent on cell in handover-mode (on new subflows, ignoring initial one) */
1218 	u_int64_t       mptcp_interactive_cell_bytes;   /* Same as previous but for interactive */
1219 	u_int64_t       mptcp_aggregate_cell_bytes;
1220 	u_int64_t       mptcp_handover_all_bytes;               /* Total number of bytes sent in handover */
1221 	u_int64_t       mptcp_interactive_all_bytes;
1222 	u_int64_t       mptcp_aggregate_all_bytes;
1223 	u_int32_t       mptcp_wifi_proxy;               /* Total number of new subflows that fell back to regular TCP on cell */
1224 	u_int32_t       mptcp_cell_proxy;               /* Total number of new subflows that fell back to regular TCP on WiFi */
1225 	u_int32_t       mptcp_triggered_cell;           /* Total number of times an MPTCP-connection triggered cell bringup */
1226 	u_int32_t       _padding;
1227 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1228 } nstat_sysinfo_tcp_stats;
1229 #define NSTAT_SYSINFO_TCP_STATS_COUNT   71
1230 
1231 enum {
1232 	NSTAT_IFNET_ECN_PROTO_IPV4 = 1
1233 	, NSTAT_IFNET_ECN_PROTO_IPV6
1234 };
1235 
1236 enum {
1237 	NSTAT_IFNET_ECN_TYPE_CELLULAR = 1
1238 	, NSTAT_IFNET_ECN_TYPE_WIFI
1239 	, NSTAT_IFNET_ECN_TYPE_ETHERNET
1240 };
1241 
1242 typedef struct nstat_sysinfo_ifnet_ecn_stats {
1243 	u_int32_t               ifnet_proto;
1244 	u_int32_t               ifnet_type;
1245 	struct if_tcp_ecn_stat  ecn_stat;
1246 } nstat_sysinfo_ifnet_ecn_stats;
1247 
1248 /* Total number of Low Internet stats that will be reported */
1249 #define NSTAT_LIM_STAT_KEYVAL_COUNT     12
1250 typedef struct nstat_sysinfo_lim_stats {
1251 	u_int8_t                ifnet_signature[NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE];
1252 	u_int32_t               ifnet_siglen;
1253 	u_int32_t               ifnet_type;
1254 	struct if_lim_perf_stat lim_stat;
1255 } nstat_sysinfo_lim_stats;
1256 
1257 #define NSTAT_NET_API_STAT_KEYVAL_COUNT (NSTAT_SYSINFO_API_LAST - NSTAT_SYSINFO_API_FIRST + 1)
1258 typedef struct nstat_sysinfo_net_api_stats {
1259 	u_int32_t               report_interval;
1260 	u_int32_t               _padding;
1261 	struct net_api_stats    net_api_stats;
1262 } nstat_sysinfo_net_api_stats;
1263 
1264 typedef struct nstat_sysinfo_data {
1265 	uint32_t                flags;
1266 	uint32_t                unsent_data_cnt; /* Before sleeping */
1267 	union {
1268 		nstat_sysinfo_tcp_stats tcp_stats;
1269 		nstat_sysinfo_ifnet_ecn_stats ifnet_ecn_stats;
1270 		nstat_sysinfo_lim_stats lim_stats;
1271 		nstat_sysinfo_net_api_stats net_api_stats;
1272 	} u;
1273 } nstat_sysinfo_data;
1274 
1275 #pragma mark -- Generic Network Statistics Provider --
1276 
1277 typedef void *  nstat_provider_cookie_t;
1278 
1279 #pragma mark -- Route Statistics Gathering Functions --
1280 struct rtentry;
1281 
1282 enum{
1283 	NSTAT_TX_FLAG_RETRANSMIT        = 1
1284 };
1285 
1286 enum{
1287 	NSTAT_RX_FLAG_DUPLICATE         = 1,
1288 	NSTAT_RX_FLAG_OUT_OF_ORDER      = 2
1289 };
1290 
1291 // indicates whether or not collection of statistics is enabled
1292 extern int      nstat_collect;
1293 
1294 void nstat_init(void);
1295 
1296 // Route collection routines
1297 void nstat_route_connect_attempt(struct rtentry *rte);
1298 void nstat_route_connect_success(struct rtentry *rte);
1299 void nstat_route_tx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1300 void nstat_route_rx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1301 void nstat_route_rtt(struct rtentry *rte, u_int32_t rtt, u_int32_t rtt_var);
1302 void nstat_route_update(struct rtentry *rte, uint32_t connect_attempts, uint32_t connect_successes,
1303     uint32_t rx_packets, uint32_t rx_bytes, uint32_t rx_duplicatebytes, uint32_t rx_outoforderbytes,
1304     uint32_t tx_packets, uint32_t tx_bytes, uint32_t tx_retransmit,
1305     uint32_t rtt, uint32_t rtt_var);
1306 struct nstat_counts* nstat_route_attach(struct rtentry  *rte);
1307 void nstat_route_detach(struct rtentry *rte);
1308 
1309 // watcher support
1310 struct inpcb;
1311 void nstat_tcp_new_pcb(struct inpcb *inp);
1312 void nstat_udp_new_pcb(struct inpcb *inp);
1313 void nstat_route_new_entry(struct rtentry *rt);
1314 void nstat_pcb_detach(struct inpcb *inp);
1315 void nstat_pcb_event(struct inpcb *inp, u_int64_t event);
1316 void nstat_pcb_cache(struct inpcb *inp);
1317 void nstat_pcb_invalidate_cache(struct inpcb *inp);
1318 
1319 
1320 void nstat_ifnet_threshold_reached(unsigned int ifindex);
1321 
1322 void nstat_sysinfo_send_data(struct nstat_sysinfo_data *);
1323 
1324 int ntstat_tcp_progress_enable(struct sysctl_req *req);
1325 int ntstat_tcp_progress_indicators(struct sysctl_req *req);
1326 
1327 #if SKYWALK
1328 
1329 // Userland stats reporting
1330 
1331 // Each side, NetworkStatistics and the kernel provider for userland,
1332 // pass opaque references.
1333 typedef void *userland_stats_provider_context;
1334 typedef void *nstat_userland_context;
1335 
1336 typedef struct nstat_progress_digest {
1337 	u_int64_t       rxbytes;
1338 	u_int64_t       txbytes;
1339 	u_int32_t       rxduplicatebytes;
1340 	u_int32_t       rxoutoforderbytes;
1341 	u_int32_t       txretransmit;
1342 	u_int32_t       ifindex;
1343 	u_int32_t       state;
1344 	u_int32_t       txunacked;
1345 	u_int32_t       txwindow;
1346 	union {
1347 		struct tcp_conn_status connstatus;
1348 		// On armv7k, tcp_conn_status is 1 byte instead of 4
1349 		uint8_t                                 __pad_connstatus[4];
1350 	};
1351 } nstat_progress_digest;
1352 
1353 // When things have been set up, Netstats can request a refresh of its data.
1354 typedef bool (userland_stats_request_vals_fn)(userland_stats_provider_context *ctx,
1355     u_int32_t *ifflagsp,
1356     nstat_progress_digest *digestp,
1357     nstat_counts *countsp,
1358     void *metadatap);
1359 
1360 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1361 // The return value is the amount of space currently required for the extension
1362 typedef size_t (userland_stats_request_extension_fn)(userland_stats_provider_context *ctx,
1363     int requested_extension,    /* The extension to be returned */
1364     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1365     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1366 
1367 // Things get started with a call to netstats to say that there’s a new connection:
1368 nstat_userland_context ntstat_userland_stats_open(userland_stats_provider_context *ctx,
1369     int provider_id,
1370     u_int64_t properties,
1371     userland_stats_request_vals_fn req_fn,
1372     userland_stats_request_extension_fn req_extension_fn);
1373 
1374 void ntstat_userland_stats_close(nstat_userland_context nstat_ctx);
1375 
1376 
1377 void ntstat_userland_stats_event(nstat_userland_context nstat_ctx, uint64_t event);
1378 
1379 void nstats_userland_stats_defunct_for_process(int pid);
1380 
1381 errno_t nstat_userland_mark_rnf_override(uuid_t fuuid, bool rnf_override);
1382 
1383 
1384 // Servicing a sysctl for information of TCP or UDP flows
1385 int ntstat_userland_count(short proto);
1386 int nstat_userland_get_snapshot(short proto, void **snapshotp, int *countp);
1387 int nstat_userland_list_snapshot(short proto, struct sysctl_req *req, void *userlandsnapshot, int nuserland);
1388 void nstat_userland_release_snapshot(void *snapshot, int nuserland);
1389 #if NTSTAT_SUPPORTS_STANDALONE_SYSCTL
1390 int ntstat_userland_list_n(short proto, struct sysctl_req *req);
1391 #endif
1392 #endif /* SKYWALK */
1393 
1394 // Utilities for userland stats reporting
1395 
1396 u_int32_t nstat_ifnet_to_flags(struct ifnet *ifp);
1397 
1398 // Generic external provider reporting
1399 
1400 // Each side passes opaque references.
1401 typedef void *nstat_provider_context;   /* This is quoted to the external provider */
1402 typedef void *nstat_context;            /* This is quoted by the external provider when calling nstat */
1403 
1404 // After nstat_provider_stats_open() has been called (and potentially while the open is still executing), netstats can request a refresh of its data
1405 // The various return pointer parameters may be null if the item is not required
1406 // The return code is true for success
1407 typedef bool (nstat_provider_request_vals_fn)(nstat_provider_context ctx,
1408     u_int32_t *ifflagsp,    /* Flags for being on cell/wifi etc, used for filtering */
1409     nstat_counts *countsp,  /* Counts to be filled in */
1410     void *metadatap);       /* A descriptor for the particular provider */
1411 
1412 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1413 // The return value is the amount of space currently required for the extension
1414 typedef size_t (nstat_provider_request_extensions_fn)(nstat_provider_context ctx,
1415     int requested_extension,    /* The extension to be returned */
1416     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1417     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1418 
1419 // Things get started with a call to netstats to say that there’s a new item to become a netstats source
1420 nstat_context nstat_provider_stats_open(nstat_provider_context ctx,
1421     int provider_id,
1422     u_int64_t properties,                   /* The bottom 32 bits can be used as per the interface / connection flags ifflagsp */
1423     nstat_provider_request_vals_fn req_fn,
1424     nstat_provider_request_extensions_fn req_extensions_fn);
1425 
1426 // Note that when the source is closed, netstats will make one last call on the request functions to retrieve final values
1427 void nstat_provider_stats_close(nstat_context nstat_ctx);
1428 
1429 // Events that cause a significant change may be reported via a flags word
1430 void nstat_provider_stats_event(nstat_context nstat_ctx, uint64_t event);
1431 
1432 
1433 // locked_add_64 uses atomic operations on 32bit so the 64bit
1434 // value can be properly read. The values are only ever incremented
1435 // while under the socket lock, so on 64bit we don't actually need
1436 // atomic operations to increment.
1437 #if defined(__LP64__)
1438 #define locked_add_64(__addr, __count) do { \
1439 	*(__addr) += (__count); \
1440 } while (0)
1441 #else
1442 #define locked_add_64(__addr, __count) do { \
1443 	os_atomic_add((__addr), (__count), relaxed); \
1444 } while (0)
1445 #endif
1446 
1447 #endif /* XNU_KERNEL_PRIVATE */
1448 
1449 #endif /* __NTSTAT_H__ */
1450