2 * Copyright (c) 2010-2013 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>
32 #include <net/if_var.h>
36 #pragma mark -- Common Data Structures --
38 #define __NSTAT_REVISION__ 4
40 typedef u_int32_t nstat_provider_id_t
;
41 typedef u_int32_t nstat_src_ref_t
;
43 typedef struct nstat_counts
46 u_int64_t nstat_rxpackets
__attribute__((aligned(8)));
47 u_int64_t nstat_rxbytes
__attribute__((aligned(8)));
48 u_int64_t nstat_txpackets
__attribute__((aligned(8)));
49 u_int64_t nstat_txbytes
__attribute__((aligned(8)));
51 u_int32_t nstat_rxduplicatebytes
;
52 u_int32_t nstat_rxoutoforderbytes
;
53 u_int32_t nstat_txretransmit
;
55 u_int32_t nstat_connectattempts
;
56 u_int32_t nstat_connectsuccesses
;
58 u_int32_t nstat_min_rtt
;
59 u_int32_t nstat_avg_rtt
;
60 u_int32_t nstat_var_rtt
;
62 u_int64_t nstat_cell_rxbytes
__attribute__((aligned(8)));
63 u_int64_t nstat_cell_txbytes
__attribute__((aligned(8)));
64 u_int64_t nstat_wifi_rxbytes
__attribute__((aligned(8)));
65 u_int64_t nstat_wifi_txbytes
__attribute__((aligned(8)));
68 #pragma mark -- Network Statistics Providers --
72 NSTAT_PROVIDER_ROUTE
= 1
73 ,NSTAT_PROVIDER_TCP
= 2
74 ,NSTAT_PROVIDER_UDP
= 3
75 ,NSTAT_PROVIDER_IFNET
= 4
78 typedef struct nstat_route_add_param
82 struct sockaddr_in v4
;
83 struct sockaddr_in6 v6
;
87 struct sockaddr_in v4
;
88 struct sockaddr_in6 v6
;
91 } nstat_route_add_param
;
93 typedef struct nstat_tcp_add_param
97 struct sockaddr_in v4
;
98 struct sockaddr_in6 v6
;
102 struct sockaddr_in v4
;
103 struct sockaddr_in6 v6
;
105 } nstat_tcp_add_param
;
107 typedef struct nstat_tcp_descriptor
111 struct sockaddr_in v4
;
112 struct sockaddr_in6 v6
;
117 struct sockaddr_in v4
;
118 struct sockaddr_in6 v6
;
125 u_int32_t sndbufsize
;
126 u_int32_t sndbufused
;
127 u_int32_t rcvbufsize
;
128 u_int32_t rcvbufused
;
132 u_int32_t traffic_class
;
142 } nstat_tcp_descriptor
;
144 typedef struct nstat_tcp_add_param nstat_udp_add_param
;
146 typedef struct nstat_udp_descriptor
150 struct sockaddr_in v4
;
151 struct sockaddr_in6 v6
;
156 struct sockaddr_in v4
;
157 struct sockaddr_in6 v6
;
162 u_int32_t rcvbufsize
;
163 u_int32_t rcvbufused
;
164 u_int32_t traffic_class
;
174 } nstat_udp_descriptor
;
176 typedef struct nstat_route_descriptor
180 u_int64_t gateway_id
;
184 struct sockaddr_in v4
;
185 struct sockaddr_in6 v6
;
191 struct sockaddr_in v4
;
192 struct sockaddr_in6 v6
;
198 struct sockaddr_in v4
;
199 struct sockaddr_in6 v6
;
206 } nstat_route_descriptor
;
208 typedef struct nstat_ifnet_add_param
212 } nstat_ifnet_add_param
;
215 #define IF_DESCSIZE 128
217 typedef struct nstat_ifnet_descriptor
219 char name
[IFNAMSIZ
+1];
223 char description
[IF_DESCSIZE
];
224 } nstat_ifnet_descriptor
;
226 #pragma mark -- Network Statistics User Client --
228 #define NET_STAT_CONTROL_NAME "com.apple.network.statistics"
232 // generic response messages
233 NSTAT_MSG_TYPE_SUCCESS
= 0
234 ,NSTAT_MSG_TYPE_ERROR
= 1
237 ,NSTAT_MSG_TYPE_ADD_SRC
= 1001
238 ,NSTAT_MSG_TYPE_ADD_ALL_SRCS
= 1002
239 ,NSTAT_MSG_TYPE_REM_SRC
= 1003
240 ,NSTAT_MSG_TYPE_QUERY_SRC
= 1004
241 ,NSTAT_MSG_TYPE_GET_SRC_DESC
= 1005
242 ,NSTAT_MSG_TYPE_SET_FILTER
= 1006
244 // Responses/Notfications
245 ,NSTAT_MSG_TYPE_SRC_ADDED
= 10001
246 ,NSTAT_MSG_TYPE_SRC_REMOVED
= 10002
247 ,NSTAT_MSG_TYPE_SRC_DESC
= 10003
248 ,NSTAT_MSG_TYPE_SRC_COUNTS
= 10004
253 NSTAT_SRC_REF_ALL
= 0xffffffff
254 ,NSTAT_SRC_REF_INVALID
= 0
259 NSTAT_FILTER_NOZEROBYTES
= 0x01,
262 typedef struct nstat_msg_hdr
266 u_int32_t pad
; // unused for now
269 typedef struct nstat_msg_error
272 u_int32_t error
; // errno error
275 typedef struct nstat_msg_add_src
278 nstat_provider_id_t provider
;
280 } nstat_msg_add_src_req
;
282 typedef struct nstat_msg_add_all_srcs
285 nstat_provider_id_t provider
;
286 } nstat_msg_add_all_srcs
;
288 typedef struct nstat_msg_src_added
291 nstat_provider_id_t provider
;
292 nstat_src_ref_t srcref
;
293 } nstat_msg_src_added
;
295 typedef struct nstat_msg_rem_src
298 nstat_src_ref_t srcref
;
299 } nstat_msg_rem_src_req
;
301 typedef struct nstat_msg_get_src_description
304 nstat_src_ref_t srcref
;
305 } nstat_msg_get_src_description
;
307 typedef struct nstat_msg_set_filter
310 nstat_src_ref_t srcref
;
312 } nstat_msg_set_filter
;
314 typedef struct nstat_msg_src_description
317 nstat_src_ref_t srcref
;
318 nstat_provider_id_t provider
;
320 } nstat_msg_src_description
;
322 typedef struct nstat_msg_query_src
325 nstat_src_ref_t srcref
;
326 } nstat_msg_query_src_req
;
328 typedef struct nstat_msg_src_counts
331 nstat_src_ref_t srcref
;
333 } nstat_msg_src_counts
;
335 typedef struct nstat_msg_src_removed
338 nstat_src_ref_t srcref
;
339 } nstat_msg_src_removed
;
345 #ifdef XNU_KERNEL_PRIVATE
346 #include <sys/mcache.h>
348 #pragma mark -- Generic Network Statistics Provider --
350 typedef void * nstat_provider_cookie_t
;
352 #pragma mark -- Route Statistics Gathering Functions --
357 NSTAT_TX_FLAG_RETRANSMIT
= 1
362 NSTAT_RX_FLAG_DUPLICATE
= 1,
363 NSTAT_RX_FLAG_OUT_OF_ORDER
= 2
366 // indicates whether or not collection of statistics is enabled
367 extern int nstat_collect
;
369 void nstat_init(void);
371 // Route collection routines
372 void nstat_route_connect_attempt(struct rtentry
*rte
);
373 void nstat_route_connect_success(struct rtentry
*rte
);
374 void nstat_route_tx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
375 void nstat_route_rx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
376 void nstat_route_rtt(struct rtentry
*rte
, u_int32_t rtt
, u_int32_t rtt_var
);
377 void nstat_route_detach(struct rtentry
*rte
);
381 void nstat_tcp_new_pcb(struct inpcb
*inp
);
382 void nstat_udp_new_pcb(struct inpcb
*inp
);
383 void nstat_route_new_entry(struct rtentry
*rt
);
384 void nstat_pcb_detach(struct inpcb
*inp
);
387 void nstat_ifnet_threshold_reached(unsigned int ifindex
);
389 // locked_add_64 uses atomic operations on 32bit so the 64bit
390 // value can be properly read. The values are only ever incremented
391 // while under the socket lock, so on 64bit we don't actually need
392 // atomic operations to increment.
393 #if defined(__LP64__)
394 #define locked_add_64(__addr, __count) do { \
395 *(__addr) += (__count); \
398 #define locked_add_64(__addr, __count) do { \
399 atomic_add_64((__addr), (__count)); \
403 #endif /* XNU_KERNEL_PRIVATE */
405 #endif /* __NTSTAT_H__ */