2 * Copyright (c) 2010-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <netinet/in.h>
34 #pragma mark -- Common Data Structures --
36 #define __NSTAT_REVISION__ 1
38 typedef u_int32_t nstat_provider_id_t
;
39 typedef u_int32_t nstat_src_ref_t
;
41 typedef struct nstat_counts
44 u_int64_t nstat_rxpackets
__attribute__((aligned(8)));
45 u_int64_t nstat_rxbytes
__attribute__((aligned(8)));
46 u_int64_t nstat_txpackets
__attribute__((aligned(8)));
47 u_int64_t nstat_txbytes
__attribute__((aligned(8)));
49 u_int32_t nstat_rxduplicatebytes
;
50 u_int32_t nstat_rxoutoforderbytes
;
51 u_int32_t nstat_txretransmit
;
53 u_int32_t nstat_connectattempts
;
54 u_int32_t nstat_connectsuccesses
;
56 u_int32_t nstat_min_rtt
;
57 u_int32_t nstat_avg_rtt
;
58 u_int32_t nstat_var_rtt
;
61 #pragma mark -- Network Statistics Providers --
65 NSTAT_PROVIDER_ROUTE
= 1
66 ,NSTAT_PROVIDER_TCP
= 2
67 ,NSTAT_PROVIDER_UDP
= 3
70 typedef struct nstat_route_add_param
74 struct sockaddr_in v4
;
75 struct sockaddr_in6 v6
;
79 struct sockaddr_in v4
;
80 struct sockaddr_in6 v6
;
83 } nstat_route_add_param
;
85 typedef struct nstat_tcp_add_param
89 struct sockaddr_in v4
;
90 struct sockaddr_in6 v6
;
94 struct sockaddr_in v4
;
95 struct sockaddr_in6 v6
;
97 } nstat_tcp_add_param
;
99 typedef struct nstat_tcp_descriptor
103 struct sockaddr_in v4
;
104 struct sockaddr_in6 v6
;
109 struct sockaddr_in v4
;
110 struct sockaddr_in6 v6
;
117 u_int32_t sndbufsize
;
118 u_int32_t sndbufused
;
119 u_int32_t rcvbufsize
;
120 u_int32_t rcvbufused
;
128 } nstat_tcp_descriptor
;
130 typedef struct nstat_tcp_add_param nstat_udp_add_param
;
132 typedef struct nstat_udp_descriptor
136 struct sockaddr_in v4
;
137 struct sockaddr_in6 v6
;
142 struct sockaddr_in v4
;
143 struct sockaddr_in6 v6
;
148 u_int32_t rcvbufsize
;
149 u_int32_t rcvbufused
;
154 } nstat_udp_descriptor
;
156 typedef struct nstat_route_descriptor
160 u_int64_t gateway_id
;
164 struct sockaddr_in v4
;
165 struct sockaddr_in6 v6
;
171 struct sockaddr_in v4
;
172 struct sockaddr_in6 v6
;
178 struct sockaddr_in v4
;
179 struct sockaddr_in6 v6
;
186 } nstat_route_descriptor
;
188 #pragma mark -- Network Statistics User Client --
190 #define NET_STAT_CONTROL_NAME "com.apple.network.statistics"
194 // generice respnse messages
195 NSTAT_MSG_TYPE_SUCCESS
= 0
196 ,NSTAT_MSG_TYPE_ERROR
= 1
199 ,NSTAT_MSG_TYPE_ADD_SRC
= 1001
200 ,NSTAT_MSG_TYPE_ADD_ALL_SRCS
= 1002
201 ,NSTAT_MSG_TYPE_REM_SRC
= 1003
202 ,NSTAT_MSG_TYPE_QUERY_SRC
= 1004
203 ,NSTAT_MSG_TYPE_GET_SRC_DESC
= 1005
205 // Responses/Notfications
206 ,NSTAT_MSG_TYPE_SRC_ADDED
= 10001
207 ,NSTAT_MSG_TYPE_SRC_REMOVED
= 10002
208 ,NSTAT_MSG_TYPE_SRC_DESC
= 10003
209 ,NSTAT_MSG_TYPE_SRC_COUNTS
= 10004
214 NSTAT_SRC_REF_ALL
= 0xffffffff
215 ,NSTAT_SRC_REF_INVALID
= 0
218 typedef struct nstat_msg_hdr
222 u_int32_t pad
; // unused for now
225 typedef struct nstat_msg_error
228 u_int32_t error
; // errno error
231 typedef struct nstat_msg_add_src
234 nstat_provider_id_t provider
;
236 } nstat_msg_add_src_req
;
238 typedef struct nstat_msg_add_all_srcs
241 nstat_provider_id_t provider
;
242 } nstat_msg_add_all_srcs
;
244 typedef struct nstat_msg_src_added
247 nstat_provider_id_t provider
;
248 nstat_src_ref_t srcref
;
249 } nstat_msg_src_added
;
251 typedef struct nstat_msg_rem_src
254 nstat_src_ref_t srcref
;
255 } nstat_msg_rem_src_req
;
257 typedef struct nstat_msg_get_src_description
260 nstat_src_ref_t srcref
;
261 } nstat_msg_get_src_description
;
263 typedef struct nstat_msg_src_description
266 nstat_src_ref_t srcref
;
267 nstat_provider_id_t provider
;
269 } nstat_msg_src_description
;
271 typedef struct nstat_msg_query_src
274 nstat_src_ref_t srcref
;
275 } nstat_msg_query_src_req
;
277 typedef struct nstat_msg_src_counts
280 nstat_src_ref_t srcref
;
282 } nstat_msg_src_counts
;
284 typedef struct nstat_msg_src_removed
287 nstat_src_ref_t srcref
;
288 } nstat_msg_src_removed
;
294 #ifdef XNU_KERNEL_PRIVATE
295 #include <sys/mcache.h>
297 #pragma mark -- Generic Network Statistics Provider --
299 typedef void * nstat_provider_cookie_t
;
301 #pragma mark -- Route Statistics Gathering Functions --
306 NSTAT_TX_FLAG_RETRANSMIT
= 1
311 NSTAT_RX_FLAG_DUPLICATE
= 1,
312 NSTAT_RX_FLAG_OUT_OF_ORDER
= 2
315 // indicates whether or not collection of statistics is enabled
316 extern int nstat_collect
;
318 // Route collection routines
319 void nstat_route_connect_attempt(struct rtentry
*rte
);
320 void nstat_route_connect_success(struct rtentry
*rte
);
321 void nstat_route_tx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
322 void nstat_route_rx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
323 void nstat_route_rtt(struct rtentry
*rte
, u_int32_t rtt
, u_int32_t rtt_var
);
324 void nstat_route_detach(struct rtentry
*rte
);
328 void nstat_tcp_new_pcb(struct inpcb
*inp
);
329 void nstat_udp_new_pcb(struct inpcb
*inp
);
330 void nstat_route_new_entry(struct rtentry
*rt
);
332 // locked_add_64 uses atomic operations on 32bit so the 64bit
333 // value can be properly read. The values are only ever incremented
334 // while under the socket lock, so on 64bit we don't actually need
335 // atomic operations to increment.
336 #if defined(__LP64__)
337 #define locked_add_64(__addr, __count) do { \
338 *(__addr) += (__count); \
341 #define locked_add_64(__addr, __count) do { \
342 atomic_add_64((__addr), (__count)); \
346 #endif /* XNU_KERNEL_PRIVATE */
348 #endif /* __NTSTAT_H__ */