xref: /xnu-8020.140.41/bsd/net/ntstat.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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 };
790 
791 #define NSTAT_EXTENDED_UPDATE_TYPE_MIN  NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN
792 #define NSTAT_EXTENDED_UPDATE_TYPE_MAX  NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN
793 
794 
795 #define NSTAT_EXTENDED_UPDATE_FLAG_MASK    0x00ffffffull    /* Maximum of 24 extension types allowed due to restrictions on specifying via filter flags */
796 
797 #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 */
798 
799 /* Provider-level filters */
800 #define NSTAT_FILTER_ACCEPT_UNKNOWN          0x0000000000000001ull
801 #define NSTAT_FILTER_ACCEPT_LOOPBACK         0x0000000000000002ull
802 #define NSTAT_FILTER_ACCEPT_CELLULAR         0x0000000000000004ull
803 #define NSTAT_FILTER_ACCEPT_WIFI             0x0000000000000008ull
804 #define NSTAT_FILTER_ACCEPT_WIRED            0x0000000000000010ull
805 #define NSTAT_FILTER_ACCEPT_AWDL             0x0000000000000020ull
806 #define NSTAT_FILTER_ACCEPT_EXPENSIVE        0x0000000000000040ull
807 #define NSTAT_FILTER_ACCEPT_CELLFALLBACK     0x0000000000000100ull
808 #define NSTAT_FILTER_ACCEPT_COMPANIONLINK    0x0000000000000200ull
809 #define NSTAT_FILTER_ACCEPT_IS_CONSTRAINED   0x0000000000000400ull
810 #define NSTAT_FILTER_ACCEPT_IS_LOCAL         0x0000000000000800ull
811 #define NSTAT_FILTER_ACCEPT_IS_NON_LOCAL     0x0000000000001000ull
812 #define NSTAT_FILTER_ACCEPT_ROUTE_VAL_ERR    0x0000000000002000ull
813 #define NSTAT_FILTER_ACCEPT_FLOWSWITCH_ERR   0x0000000000004000ull
814 #define NSTAT_FILTER_ACCEPT_WIFI_LLW         0x0000000000008000ull
815 #define NSTAT_FILTER_ACCEPT_WIFI_INFRA       0x0000000000010000ull
816 #define NSTAT_FILTER_IFNET_FLAGS             0x000000000001FFFFull
817 
818 #define NSTAT_FILTER_UDP_INTERFACE_ATTACH    0x0000000000020000ull
819 #define NSTAT_FILTER_UDP_FLAGS               0x0000000000020000ull
820 
821 #define NSTAT_FILTER_TCP_INTERFACE_ATTACH    0x0000000000040000ull
822 #define NSTAT_FILTER_TCP_NO_EARLY_CLOSE      0x0000000000080000ull
823 #define NSTAT_FILTER_TCP_FLAGS               0x00000000000C0000ull
824 
825 #define NSTAT_FILTER_SUPPRESS_SRC_ADDED      0x0000000000100000ull
826 #define NSTAT_FILTER_REQUIRE_SRC_ADDED       0x0000000000200000ull
827 #define NSTAT_FILTER_PROVIDER_NOZEROBYTES    0x0000000000400000ull
828 
829 #define NSTAT_FILTER_CONN_HAS_NET_ACCESS     0x0000000001000000ull
830 #define NSTAT_FILTER_CONN_FLAGS              0x0000000001000000ull
831 
832 /* In this context, boring == no change from previous report */
833 #define NSTAT_FILTER_SUPPRESS_BORING_CLOSE   0x0000000010000000ull  /* No final update, only NSTAT_MSG_TYPE_SRC_REMOVED */
834 #define NSTAT_FILTER_SUPPRESS_BORING_POLL    0x0000000020000000ull  /* Only for poll-all, not poll specific source */
835 #define NSTAT_FILTER_SUPPRESS_BORING_FLAGS   (NSTAT_FILTER_SUPPRESS_BORING_CLOSE|NSTAT_FILTER_SUPPRESS_BORING_POLL)
836 
837 #define NSTAT_FILTER_SPECIFIC_USER_BY_PID    0x0000000100000000ull
838 #define NSTAT_FILTER_SPECIFIC_USER_BY_EPID   0x0000000200000000ull
839 #define NSTAT_FILTER_SPECIFIC_USER_BY_UUID   0x0000000400000000ull
840 #define NSTAT_FILTER_SPECIFIC_USER_BY_EUUID  0x0000000800000000ull
841 #define NSTAT_FILTER_SPECIFIC_USER           0x0000000F00000000ull
842 
843 #define NSTAT_FILTER_INITIAL_PROPERTIES      0x0000001000000000ull  /* For providers that give "properties" on open, apply the filter to the properties */
844                                                                     /* and permanently discard unless the filter allows through */
845 #define NSTAT_FILTER_FLAGS_RESERVED          0x000000E000000000ul
846 
847 #define NSTAT_FILTER_IFNET_AND_CONN_FLAGS    (NSTAT_FILTER_IFNET_FLAGS|NSTAT_FILTER_CONN_FLAGS)
848 
849 #define NSTAT_EXTENSION_FILTER_DOMAIN_INFO              (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
850 #define NSTAT_EXTENSION_FILTER_NECP_TLV                 (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
851 #define NSTAT_EXTENSION_FILTER_ORIGINAL_NECP_TLV        (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
852 #define NSTAT_EXTENSION_FILTER_ORIGINAL_DOMAIN_INFO     (1ull << (NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN + NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT))
853 #define NSTAT_EXTENSION_FILTER_MASK                     (NSTAT_EXTENDED_UPDATE_FLAG_MASK << NSTAT_FILTER_ALLOWED_EXTENSIONS_SHIFT)
854 
855 enum{
856 	NSTAT_MSG_HDR_FLAG_SUPPORTS_AGGREGATE   = 1 << 0,
857 	NSTAT_MSG_HDR_FLAG_CONTINUATION         = 1 << 1,
858 	NSTAT_MSG_HDR_FLAG_CLOSING              = 1 << 2,
859 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_DROP    = 1 << 3,
860 	NSTAT_MSG_HDR_FLAG_CLOSED_AFTER_FILTER  = 1 << 4,
861 };
862 
863 typedef struct nstat_msg_hdr {
864 	u_int64_t       context __attribute__((aligned(sizeof(u_int64_t))));
865 	u_int32_t       type;
866 	u_int16_t       length;
867 	u_int16_t       flags;
868 } nstat_msg_hdr;
869 
870 #define MAX_NSTAT_MSG_HDR_LENGTH    65532
871 
872 typedef struct nstat_msg_error {
873 	nstat_msg_hdr   hdr;
874 	u_int32_t               error;  // errno error
875 	u_int8_t                reserved[4];
876 } nstat_msg_error;
877 
878 #define NSTAT_ADD_SRC_FIELDS            \
879 	nstat_msg_hdr		hdr;            \
880 	nstat_provider_id_t	provider;       \
881 	u_int8_t			reserved[4]     \
882 
883 typedef struct nstat_msg_add_src {
884 	NSTAT_ADD_SRC_FIELDS;
885 	u_int8_t        param[];
886 } nstat_msg_add_src_req;
887 
888 typedef struct nstat_msg_add_src_header {
889 	NSTAT_ADD_SRC_FIELDS;
890 } nstat_msg_add_src_header;
891 
892 typedef struct nstat_msg_add_src_convenient {
893 	nstat_msg_add_src_header        hdr;
894 	union {
895 		nstat_route_add_param   route;
896 		nstat_tcp_add_param             tcp;
897 		nstat_udp_add_param             udp;
898 		nstat_ifnet_add_param   ifnet;
899 		nstat_sysinfo_add_param sysinfo;
900 	};
901 } nstat_msg_add_src_convenient;
902 
903 #undef NSTAT_ADD_SRC_FIELDS
904 
905 typedef struct nstat_msg_add_all_srcs {
906 	nstat_msg_hdr           hdr;
907 	u_int64_t               filter __attribute__((aligned(sizeof(u_int64_t))));
908 	nstat_event_flags_t     events __attribute__((aligned(sizeof(u_int64_t))));
909 	nstat_provider_id_t     provider;
910 	pid_t                   target_pid;
911 	uuid_t                  target_uuid;
912 } nstat_msg_add_all_srcs;
913 
914 typedef struct nstat_msg_src_added {
915 	nstat_msg_hdr           hdr;
916 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
917 	nstat_provider_id_t     provider;
918 	u_int8_t                reserved[4];
919 } nstat_msg_src_added;
920 
921 typedef struct nstat_msg_rem_src {
922 	nstat_msg_hdr           hdr;
923 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
924 } nstat_msg_rem_src_req;
925 
926 typedef struct nstat_msg_get_src_description {
927 	nstat_msg_hdr           hdr;
928 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
929 } nstat_msg_get_src_description;
930 
931 typedef struct nstat_msg_set_filter {
932 	nstat_msg_hdr           hdr;
933 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
934 	u_int32_t               filter;
935 	u_int8_t                reserved[4];
936 } nstat_msg_set_filter;
937 
938 #define NSTAT_SRC_DESCRIPTION_FIELDS                                                                                            \
939 	nstat_msg_hdr		hdr;                                                                                                            \
940 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
941 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
942 	nstat_provider_id_t	provider;                                                                                                       \
943 	u_int8_t			reserved[4]
944 
945 typedef struct nstat_msg_src_description {
946 	NSTAT_SRC_DESCRIPTION_FIELDS;
947 	u_int8_t        data[];
948 } nstat_msg_src_description;
949 
950 typedef struct nstat_msg_src_description_header {
951 	NSTAT_SRC_DESCRIPTION_FIELDS;
952 } nstat_msg_src_description_header;
953 
954 typedef struct nstat_msg_src_description_convenient {
955 	nstat_msg_src_description_header    hdr;
956 	union {
957 		nstat_tcp_descriptor            tcp;
958 		nstat_udp_descriptor            udp;
959 		nstat_route_descriptor          route;
960 		nstat_ifnet_descriptor          ifnet;
961 		nstat_sysinfo_descriptor        sysinfo;
962 		nstat_quic_descriptor           quic;
963 	};
964 } nstat_msg_src_description_convenient;
965 
966 #undef NSTAT_SRC_DESCRIPTION_FIELDS
967 
968 typedef struct nstat_msg_query_src {
969 	nstat_msg_hdr           hdr;
970 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
971 } nstat_msg_query_src_req;
972 
973 typedef struct nstat_msg_src_counts {
974 	nstat_msg_hdr           hdr;
975 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
976 	nstat_event_flags_t     event_flags __attribute__((aligned(sizeof(u_int64_t))));
977 	nstat_counts            counts;
978 } nstat_msg_src_counts;
979 
980 #define NSTAT_SRC_UPDATE_FIELDS                                                                                                         \
981 	nstat_msg_hdr		hdr;                                                                                                            \
982 	nstat_src_ref_t		srcref __attribute__((aligned(sizeof(u_int64_t))));                     \
983 	nstat_event_flags_t	event_flags __attribute__((aligned(sizeof(u_int64_t))));        \
984 	nstat_counts		counts;                                                                                                         \
985 	nstat_provider_id_t	provider;                                                                                                       \
986 	u_int8_t			reserved[4]
987 
988 typedef struct nstat_msg_src_update {
989 	NSTAT_SRC_UPDATE_FIELDS;
990 	u_int8_t        data[];
991 } nstat_msg_src_update;
992 
993 typedef struct nstat_msg_src_update_hdr {
994 	NSTAT_SRC_UPDATE_FIELDS;
995 } nstat_msg_src_update_hdr;
996 
997 typedef struct nstat_msg_src_update_tcp {
998 	NSTAT_SRC_UPDATE_FIELDS;
999 	nstat_tcp_descriptor            tcp_desc;
1000 } nstat_msg_src_update_tcp;
1001 
1002 typedef struct nstat_msg_src_update_udp {
1003 	NSTAT_SRC_UPDATE_FIELDS;
1004 	nstat_udp_descriptor            udp_desc;
1005 } nstat_msg_src_update_udp;
1006 
1007 typedef struct nstat_msg_src_update_quic {
1008 	NSTAT_SRC_UPDATE_FIELDS;
1009 	nstat_quic_descriptor           quic_desc;
1010 } nstat_msg_src_update_quic;
1011 
1012 typedef struct nstat_msg_src_update_conn {
1013 	NSTAT_SRC_UPDATE_FIELDS;
1014 	nstat_connection_descriptor     conn_desc;
1015 } nstat_msg_src_update_conn;
1016 
1017 
1018 typedef struct nstat_msg_src_update_convenient {
1019 	nstat_msg_src_update_hdr                hdr;
1020 	union {
1021 		nstat_tcp_descriptor            tcp;
1022 		nstat_udp_descriptor            udp;
1023 		nstat_route_descriptor          route;
1024 		nstat_ifnet_descriptor          ifnet;
1025 		nstat_sysinfo_descriptor        sysinfo;
1026 		nstat_quic_descriptor           quic;
1027 		nstat_connection_descriptor     conn;
1028 	};
1029 } nstat_msg_src_update_convenient;
1030 
1031 typedef struct nstat_msg_src_extended_item_hdr {
1032 	u_int32_t       type;
1033 	u_int32_t       length;
1034 } nstat_msg_src_extended_item_hdr __attribute__((aligned(sizeof(u_int64_t))));;
1035 
1036 typedef struct nstat_msg_src_extended_item {
1037 	nstat_msg_src_extended_item_hdr         hdr;
1038 	u_int8_t                                data[];
1039 } nstat_msg_src_extended_item;
1040 
1041 typedef struct nstat_msg_src_extended_tcp_update {
1042 	nstat_msg_src_update_hdr                hdr;
1043 	nstat_tcp_descriptor                    tcp;
1044 	nstat_msg_src_extended_item_hdr         extension_hdr;
1045 	u_int8_t                                data[];
1046 } nstat_msg_src_extended_tcp_update;
1047 
1048 typedef struct nstat_msg_src_extended_udp_update {
1049 	nstat_msg_src_update_hdr                hdr;
1050 	nstat_udp_descriptor                    udp;
1051 	nstat_msg_src_extended_item_hdr         extension_hdr;
1052 	u_int8_t                                data[];
1053 } nstat_msg_src_extended_udp_update;
1054 
1055 typedef struct nstat_msg_src_extended_quic_update {
1056 	nstat_msg_src_update_hdr                hdr;
1057 	nstat_quic_descriptor                   quic;
1058 	nstat_msg_src_extended_item_hdr         extension_hdr;
1059 	u_int8_t                                data[];
1060 } nstat_msg_src_extended_quic_update;
1061 
1062 typedef struct nstat_msg_src_extended_conn_update {
1063 	nstat_msg_src_update_hdr                hdr;
1064 	nstat_connection_descriptor             conn;
1065 	nstat_msg_src_extended_item_hdr         extension_hdr;
1066 	u_int8_t                                data[];
1067 } nstat_msg_src_extended_conn_update;
1068 
1069 /* While the only type of extended update is for domain information, we can fully define the structure */
1070 typedef struct nstat_msg_src_tcp_update_domain_extension {
1071 	nstat_msg_src_update_hdr                hdr;
1072 	nstat_tcp_descriptor                    tcp;
1073 	nstat_msg_src_extended_item_hdr         extension_hdr;
1074 	nstat_domain_info                       domain_info;
1075 } nstat_msg_src_tcp_update_domain_extension;
1076 
1077 typedef struct nstat_msg_src_udp_update_domain_extension {
1078 	nstat_msg_src_update_hdr                hdr;
1079 	nstat_udp_descriptor                    udp;
1080 	nstat_msg_src_extended_item_hdr         extension_hdr;
1081 	nstat_domain_info                       domain_info;
1082 } nstat_msg_src_udp_update_domain_extension;
1083 
1084 typedef struct nstat_msg_src_quic_update_domain_extension {
1085 	nstat_msg_src_update_hdr                hdr;
1086 	nstat_quic_descriptor                   quic;
1087 	nstat_msg_src_extended_item_hdr         extension_hdr;
1088 	nstat_domain_info                       domain_info;
1089 } nstat_msg_src_quic_update_domain_extension;
1090 
1091 typedef struct nstat_msg_src_update_domain_extension_convenient {
1092 	nstat_msg_src_tcp_update_domain_extension       tcp;
1093 	nstat_msg_src_udp_update_domain_extension       udp;
1094 	nstat_msg_src_quic_update_domain_extension      quic;
1095 } nstat_msg_src_update_domain_extension_convenient;
1096 
1097 #undef NSTAT_SRC_UPDATE_FIELDS
1098 
1099 typedef struct nstat_msg_src_removed {
1100 	nstat_msg_hdr           hdr;
1101 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1102 } nstat_msg_src_removed;
1103 
1104 typedef struct nstat_msg_sysinfo_counts {
1105 	nstat_msg_hdr           hdr;
1106 	nstat_src_ref_t         srcref __attribute__((aligned(sizeof(u_int64_t))));
1107 	nstat_sysinfo_counts    counts;
1108 }  nstat_msg_sysinfo_counts;
1109 
1110 #pragma mark -- Statitiscs about Network Statistics --
1111 
1112 struct nstat_stats {
1113 	u_int32_t nstat_successmsgfailures;
1114 	u_int32_t nstat_sendcountfailures;
1115 	u_int32_t nstat_sysinfofailures;
1116 	u_int32_t nstat_srcupatefailures;
1117 	u_int32_t nstat_descriptionfailures;
1118 	u_int32_t nstat_msgremovedfailures;
1119 	u_int32_t nstat_srcaddedfailures;
1120 	u_int32_t nstat_msgerrorfailures;
1121 	u_int32_t nstat_copy_descriptor_failures;
1122 	u_int32_t nstat_provider_counts_failures;
1123 	u_int32_t nstat_control_send_description_failures;
1124 	u_int32_t nstat_control_send_goodbye_failures;
1125 	u_int32_t nstat_flush_accumulated_msgs_failures;
1126 	u_int32_t nstat_accumulate_msg_failures;
1127 	u_int32_t nstat_control_cleanup_source_failures;
1128 	u_int32_t nstat_handle_msg_failures;
1129 };
1130 
1131 #endif /* PRIVATE */
1132 
1133 #ifdef XNU_KERNEL_PRIVATE
1134 #include <sys/mcache.h>
1135 
1136 #if (DEBUG || DEVELOPMENT)
1137 extern int nstat_test_privacy_transparency;
1138 #endif /* (DEBUG || DEVELOPMENT) */
1139 
1140 #pragma mark -- System Information Internal Support --
1141 
1142 typedef struct nstat_sysinfo_mbuf_stats {
1143 	u_int32_t       total_256b;     /* Peak usage, 256B pool */
1144 	u_int32_t       total_2kb;      /* Peak usage, 2KB pool */
1145 	u_int32_t       total_4kb;      /* Peak usage, 4KB pool */
1146 	u_int32_t       total_16kb;     /* Peak usage, 16KB pool */
1147 	u_int32_t       sbmb_total;     /* Total mbufs in sock buffer pool */
1148 	u_int32_t       sb_atmbuflimit; /* Memory limit reached for socket buffer autoscaling */
1149 	u_int32_t       draincnt;       /* Number of times mbuf pool has been drained under memory pressure */
1150 	u_int32_t       memreleased;    /* Memory (bytes) released from mbuf pool to VM */
1151 	u_int32_t       sbmb_floor;     /* Lowest mbufs in sock buffer pool */
1152 } nstat_sysinfo_mbuf_stats;
1153 
1154 typedef struct nstat_sysinfo_tcp_stats {
1155 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1156 	u_int32_t       ipv4_avgrtt;    /* Average RTT for IPv4 */
1157 	u_int32_t       ipv6_avgrtt;    /* Average RTT for IPv6 */
1158 	u_int32_t       send_plr;       /* Average uplink packet loss rate */
1159 	u_int32_t       recv_plr;       /* Average downlink packet loss rate */
1160 	u_int32_t       send_tlrto_rate; /* Average rxt timeout after tail loss */
1161 	u_int32_t       send_reorder_rate; /* Average packet reordering rate */
1162 	u_int32_t       connection_attempts; /* TCP client connection attempts */
1163 	u_int32_t       connection_accepts; /* TCP server connection accepts */
1164 	u_int32_t       ecn_client_enabled; /* Global setting for ECN client side */
1165 	u_int32_t       ecn_server_enabled; /* Global setting for ECN server side */
1166 	u_int32_t       ecn_client_setup; /* Attempts to setup TCP client connection with ECN */
1167 	u_int32_t       ecn_server_setup; /* Attempts to setup TCP server connection with ECN */
1168 	u_int32_t       ecn_client_success; /* Number of successful negotiations of ECN for a client connection */
1169 	u_int32_t       ecn_server_success; /* Number of successful negotiations of ECN for a server connection */
1170 	u_int32_t       ecn_not_supported; /* Number of falbacks to Non-ECN, no support from peer */
1171 	u_int32_t       ecn_lost_syn;   /* Number of SYNs lost with ECN bits */
1172 	u_int32_t       ecn_lost_synack; /* Number of SYN-ACKs lost with ECN bits */
1173 	u_int32_t       ecn_recv_ce;    /* Number of CEs received from network */
1174 	u_int32_t       ecn_recv_ece;   /* Number of ECEs received from receiver */
1175 	u_int32_t       ecn_sent_ece;   /* Number of ECEs sent in response to CE */
1176 	u_int32_t       ecn_conn_recv_ce; /* Number of connections using ECN received CE at least once */
1177 	u_int32_t       ecn_conn_recv_ece; /* Number of connections using ECN received ECE at least once */
1178 	u_int32_t       ecn_conn_plnoce; /* Number of connections using ECN seen packet loss but never received CE */
1179 	u_int32_t       ecn_conn_pl_ce; /* Number of connections using ECN seen packet loss and CE */
1180 	u_int32_t       ecn_conn_nopl_ce; /* Number of connections using ECN with no packet loss but received CE */
1181 	u_int32_t       ecn_fallback_synloss; /* Number of times we did fall back due to SYN-Loss */
1182 	u_int32_t       ecn_fallback_reorder; /* Number of times we fallback because we detected the PAWS-issue */
1183 	u_int32_t       ecn_fallback_ce; /* Number of times we fallback because we received too many CEs */
1184 	u_int32_t       tfo_syn_data_rcv;       /* Number of SYN+data received with valid cookie */
1185 	u_int32_t       tfo_cookie_req_rcv;/* Number of TFO cookie-requests received */
1186 	u_int32_t       tfo_cookie_sent;        /* Number of TFO-cookies offered to the client */
1187 	u_int32_t       tfo_cookie_invalid;/* Number of invalid TFO-cookies received */
1188 	u_int32_t       tfo_cookie_req; /* Number of SYNs with cookie request received*/
1189 	u_int32_t       tfo_cookie_rcv; /* Number of SYN/ACKs with Cookie received */
1190 	u_int32_t       tfo_syn_data_sent;      /* Number of SYNs+data+cookie sent */
1191 	u_int32_t       tfo_syn_data_acked;/* Number of times our SYN+data has been acknowledged */
1192 	u_int32_t       tfo_syn_loss;   /* Number of times SYN+TFO has been lost and we fallback */
1193 	u_int32_t       tfo_blackhole;  /* Number of times SYN+TFO has been lost and we fallback */
1194 	u_int32_t       tfo_cookie_wrong;       /* TFO-cookie we sent was wrong */
1195 	u_int32_t       tfo_no_cookie_rcv;      /* We asked for a cookie but didn't get one */
1196 	u_int32_t       tfo_heuristics_disable; /* TFO got disabled due to heuristics */
1197 	u_int32_t       tfo_sndblackhole;       /* TFO got blackholed in the sending direction */
1198 	u_int32_t       mptcp_handover_attempt; /* Total number of MPTCP-attempts using handover mode */
1199 	u_int32_t       mptcp_interactive_attempt;      /* Total number of MPTCP-attempts using interactive mode */
1200 	u_int32_t       mptcp_aggregate_attempt;        /* Total number of MPTCP-attempts using aggregate mode */
1201 	u_int32_t       mptcp_fp_handover_attempt; /* Same as previous three but only for first-party apps */
1202 	u_int32_t       mptcp_fp_interactive_attempt;
1203 	u_int32_t       mptcp_fp_aggregate_attempt;
1204 	u_int32_t       mptcp_heuristic_fallback;       /* Total number of MPTCP-connections that fell back due to heuristics */
1205 	u_int32_t       mptcp_fp_heuristic_fallback;    /* Same as previous but for first-party apps */
1206 	u_int32_t       mptcp_handover_success_wifi;    /* Total number of successfull handover-mode connections that *started* on WiFi */
1207 	u_int32_t       mptcp_handover_success_cell;    /* Total number of successfull handover-mode connections that *started* on Cell */
1208 	u_int32_t       mptcp_interactive_success;              /* Total number of interactive-mode connections that negotiated MPTCP */
1209 	u_int32_t       mptcp_aggregate_success;                /* Same as previous but for aggregate */
1210 	u_int32_t       mptcp_fp_handover_success_wifi; /* Same as previous four, but for first-party apps */
1211 	u_int32_t       mptcp_fp_handover_success_cell;
1212 	u_int32_t       mptcp_fp_interactive_success;
1213 	u_int32_t       mptcp_fp_aggregate_success;
1214 	u_int32_t       mptcp_handover_cell_from_wifi;  /* Total number of connections that use cell in handover-mode (coming from WiFi) */
1215 	u_int32_t       mptcp_handover_wifi_from_cell;  /* Total number of connections that use WiFi in handover-mode (coming from cell) */
1216 	u_int32_t       mptcp_interactive_cell_from_wifi;       /* Total number of connections that use cell in interactive mode (coming from WiFi) */
1217 	u_int32_t       mptcp_back_to_wifi;     /* Total number of connections that succeed to move traffic away from cell (when starting on cell) */
1218 	u_int64_t       mptcp_handover_cell_bytes;              /* Total number of bytes sent on cell in handover-mode (on new subflows, ignoring initial one) */
1219 	u_int64_t       mptcp_interactive_cell_bytes;   /* Same as previous but for interactive */
1220 	u_int64_t       mptcp_aggregate_cell_bytes;
1221 	u_int64_t       mptcp_handover_all_bytes;               /* Total number of bytes sent in handover */
1222 	u_int64_t       mptcp_interactive_all_bytes;
1223 	u_int64_t       mptcp_aggregate_all_bytes;
1224 	u_int32_t       mptcp_wifi_proxy;               /* Total number of new subflows that fell back to regular TCP on cell */
1225 	u_int32_t       mptcp_cell_proxy;               /* Total number of new subflows that fell back to regular TCP on WiFi */
1226 	u_int32_t       mptcp_triggered_cell;           /* Total number of times an MPTCP-connection triggered cell bringup */
1227 	u_int32_t       _padding;
1228 	/* When adding/removing here, also adjust NSTAT_SYSINFO_TCP_STATS_COUNT */
1229 } nstat_sysinfo_tcp_stats;
1230 #define NSTAT_SYSINFO_TCP_STATS_COUNT   71
1231 
1232 enum {
1233 	NSTAT_IFNET_ECN_PROTO_IPV4 = 1
1234 	, NSTAT_IFNET_ECN_PROTO_IPV6
1235 };
1236 
1237 enum {
1238 	NSTAT_IFNET_ECN_TYPE_CELLULAR = 1
1239 	, NSTAT_IFNET_ECN_TYPE_WIFI
1240 	, NSTAT_IFNET_ECN_TYPE_ETHERNET
1241 };
1242 
1243 typedef struct nstat_sysinfo_ifnet_ecn_stats {
1244 	u_int32_t               ifnet_proto;
1245 	u_int32_t               ifnet_type;
1246 	struct if_tcp_ecn_stat  ecn_stat;
1247 } nstat_sysinfo_ifnet_ecn_stats;
1248 
1249 /* Total number of Low Internet stats that will be reported */
1250 #define NSTAT_LIM_STAT_KEYVAL_COUNT     12
1251 typedef struct nstat_sysinfo_lim_stats {
1252 	u_int8_t                ifnet_signature[NSTAT_SYSINFO_KEYVAL_STRING_MAXSIZE];
1253 	u_int32_t               ifnet_siglen;
1254 	u_int32_t               ifnet_type;
1255 	struct if_lim_perf_stat lim_stat;
1256 } nstat_sysinfo_lim_stats;
1257 
1258 #define NSTAT_NET_API_STAT_KEYVAL_COUNT (NSTAT_SYSINFO_API_LAST - NSTAT_SYSINFO_API_FIRST + 1)
1259 typedef struct nstat_sysinfo_net_api_stats {
1260 	u_int32_t               report_interval;
1261 	u_int32_t               _padding;
1262 	struct net_api_stats    net_api_stats;
1263 } nstat_sysinfo_net_api_stats;
1264 
1265 typedef struct nstat_sysinfo_data {
1266 	uint32_t                flags;
1267 	uint32_t                unsent_data_cnt; /* Before sleeping */
1268 	union {
1269 		nstat_sysinfo_mbuf_stats mb_stats;
1270 		nstat_sysinfo_tcp_stats tcp_stats;
1271 		nstat_sysinfo_ifnet_ecn_stats ifnet_ecn_stats;
1272 		nstat_sysinfo_lim_stats lim_stats;
1273 		nstat_sysinfo_net_api_stats net_api_stats;
1274 	} u;
1275 } nstat_sysinfo_data;
1276 
1277 #pragma mark -- Generic Network Statistics Provider --
1278 
1279 typedef void *  nstat_provider_cookie_t;
1280 
1281 #pragma mark -- Route Statistics Gathering Functions --
1282 struct rtentry;
1283 
1284 enum{
1285 	NSTAT_TX_FLAG_RETRANSMIT        = 1
1286 };
1287 
1288 enum{
1289 	NSTAT_RX_FLAG_DUPLICATE         = 1,
1290 	NSTAT_RX_FLAG_OUT_OF_ORDER      = 2
1291 };
1292 
1293 // indicates whether or not collection of statistics is enabled
1294 extern int      nstat_collect;
1295 
1296 void nstat_init(void);
1297 
1298 // Route collection routines
1299 void nstat_route_connect_attempt(struct rtentry *rte);
1300 void nstat_route_connect_success(struct rtentry *rte);
1301 void nstat_route_tx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1302 void nstat_route_rx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags);
1303 void nstat_route_rtt(struct rtentry *rte, u_int32_t rtt, u_int32_t rtt_var);
1304 void nstat_route_update(struct rtentry *rte, uint32_t connect_attempts, uint32_t connect_successes,
1305     uint32_t rx_packets, uint32_t rx_bytes, uint32_t rx_duplicatebytes, uint32_t rx_outoforderbytes,
1306     uint32_t tx_packets, uint32_t tx_bytes, uint32_t tx_retransmit,
1307     uint32_t rtt, uint32_t rtt_var);
1308 struct nstat_counts* nstat_route_attach(struct rtentry  *rte);
1309 void nstat_route_detach(struct rtentry *rte);
1310 
1311 // watcher support
1312 struct inpcb;
1313 void nstat_tcp_new_pcb(struct inpcb *inp);
1314 void nstat_udp_new_pcb(struct inpcb *inp);
1315 void nstat_route_new_entry(struct rtentry *rt);
1316 void nstat_pcb_detach(struct inpcb *inp);
1317 void nstat_pcb_event(struct inpcb *inp, u_int64_t event);
1318 void nstat_pcb_cache(struct inpcb *inp);
1319 void nstat_pcb_invalidate_cache(struct inpcb *inp);
1320 
1321 
1322 void nstat_ifnet_threshold_reached(unsigned int ifindex);
1323 
1324 void nstat_sysinfo_send_data(struct nstat_sysinfo_data *);
1325 
1326 int ntstat_tcp_progress_enable(struct sysctl_req *req);
1327 int ntstat_tcp_progress_indicators(struct sysctl_req *req);
1328 
1329 #if SKYWALK
1330 
1331 // Userland stats reporting
1332 
1333 // Each side, NetworkStatistics and the kernel provider for userland,
1334 // pass opaque references.
1335 typedef void *userland_stats_provider_context;
1336 typedef void *nstat_userland_context;
1337 
1338 typedef struct nstat_progress_digest {
1339 	u_int64_t       rxbytes;
1340 	u_int64_t       txbytes;
1341 	u_int32_t       rxduplicatebytes;
1342 	u_int32_t       rxoutoforderbytes;
1343 	u_int32_t       txretransmit;
1344 	u_int32_t       ifindex;
1345 	u_int32_t       state;
1346 	u_int32_t       txunacked;
1347 	u_int32_t       txwindow;
1348 	union {
1349 		struct tcp_conn_status connstatus;
1350 		// On armv7k, tcp_conn_status is 1 byte instead of 4
1351 		uint8_t                                 __pad_connstatus[4];
1352 	};
1353 } nstat_progress_digest;
1354 
1355 // When things have been set up, Netstats can request a refresh of its data.
1356 typedef bool (userland_stats_request_vals_fn)(userland_stats_provider_context *ctx,
1357     u_int16_t *ifflagsp,
1358     nstat_progress_digest *digestp,
1359     nstat_counts *countsp,
1360     void *metadatap);
1361 
1362 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1363 // The return value is the amount of space currently required for the extension
1364 typedef size_t (userland_stats_request_extension_fn)(userland_stats_provider_context *ctx,
1365     int requested_extension,    /* The extension to be returned */
1366     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1367     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1368 
1369 // Things get started with a call to netstats to say that there’s a new connection:
1370 nstat_userland_context ntstat_userland_stats_open(userland_stats_provider_context *ctx,
1371     int provider_id,
1372     u_int64_t properties,
1373     userland_stats_request_vals_fn req_fn,
1374     userland_stats_request_extension_fn req_extension_fn);
1375 
1376 void ntstat_userland_stats_close(nstat_userland_context nstat_ctx);
1377 
1378 
1379 void ntstat_userland_stats_event(nstat_userland_context nstat_ctx, uint64_t event);
1380 
1381 void nstats_userland_stats_defunct_for_process(int pid);
1382 
1383 // Servicing a sysctl for information of TCP or UDP flows
1384 int ntstat_userland_count(short proto);
1385 int nstat_userland_get_snapshot(short proto, void **snapshotp, int *countp);
1386 int nstat_userland_list_snapshot(short proto, struct sysctl_req *req, void *userlandsnapshot, int nuserland);
1387 void nstat_userland_release_snapshot(void *snapshot, int nuserland);
1388 #if NTSTAT_SUPPORTS_STANDALONE_SYSCTL
1389 int ntstat_userland_list_n(short proto, struct sysctl_req *req);
1390 #endif
1391 #endif /* SKYWALK */
1392 
1393 // Utilities for userland stats reporting
1394 
1395 // Original form for backwards compatibility, should be replaced in due course
1396 u_int16_t nstat_ifnet_to_flags(struct ifnet *ifp);
1397 
1398 // Preferred form returns a 32 bit quantity, for easier handling of NSTAT_IFNET_IS_WIFI_INFRA and future expansion
1399 u_int32_t nstat_ifnet_to_flags_extended(struct ifnet *ifp);
1400 
1401 
1402 // Generic external provider reporting
1403 
1404 // Each side passes opaque references.
1405 typedef void *nstat_provider_context;   /* This is quoted to the external provider */
1406 typedef void *nstat_context;            /* This is quoted by the external provider when calling nstat */
1407 
1408 // After nstat_provider_stats_open() has been called (and potentially while the open is still executing), netstats can request a refresh of its data
1409 // The various return pointer parameters may be null if the item is not required
1410 // The return code is true for success
1411 typedef bool (nstat_provider_request_vals_fn)(nstat_provider_context ctx,
1412     u_int32_t *ifflagsp,    /* Flags for being on cell/wifi etc, used for filtering */
1413     nstat_counts *countsp,  /* Counts to be filled in */
1414     void *metadatap);       /* A descriptor for the particular provider */
1415 
1416 // Netstats can also request "extension" items, specified by the allowed_extensions flag
1417 // The return value is the amount of space currently required for the extension
1418 typedef size_t (nstat_provider_request_extensions_fn)(nstat_provider_context ctx,
1419     int requested_extension,    /* The extension to be returned */
1420     void *buf,                  /* If not NULL, the address for the extension to be returned in */
1421     size_t buf_size);           /* The size of the buffer space, typically matching the return from a previous call with null buffer pointer */
1422 
1423 // Things get started with a call to netstats to say that there’s a new item to become a netstats source
1424 nstat_context nstat_provider_stats_open(nstat_provider_context ctx,
1425     int provider_id,
1426     u_int64_t properties,                   /* The bottom 32 bits can be used as per the interface / connection flags ifflagsp */
1427     nstat_provider_request_vals_fn req_fn,
1428     nstat_provider_request_extensions_fn req_extensions_fn);
1429 
1430 // Note that when the source is closed, netstats will make one last call on the request functions to retrieve final values
1431 void nstat_provider_stats_close(nstat_context nstat_ctx);
1432 
1433 // Events that cause a significant change may be reported via a flags word
1434 void nstat_provider_stats_event(nstat_context nstat_ctx, uint64_t event);
1435 
1436 
1437 // locked_add_64 uses atomic operations on 32bit so the 64bit
1438 // value can be properly read. The values are only ever incremented
1439 // while under the socket lock, so on 64bit we don't actually need
1440 // atomic operations to increment.
1441 #if defined(__LP64__)
1442 #define locked_add_64(__addr, __count) do { \
1443 	*(__addr) += (__count); \
1444 } while (0)
1445 #else
1446 #define locked_add_64(__addr, __count) do { \
1447 	atomic_add_64((__addr), (__count)); \
1448 } while (0)
1449 #endif
1450 
1451 #endif /* XNU_KERNEL_PRIVATE */
1452 
1453 #endif /* __NTSTAT_H__ */
1454