2 * Copyright (c) 2010-2014 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__ 6
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)));
66 u_int64_t nstat_wired_rxbytes
__attribute__((aligned(8)));
67 u_int64_t nstat_wired_txbytes
__attribute__((aligned(8)));
70 typedef struct nstat_sysinfo_keyval
72 u_int32_t nstat_sysinfo_key
;
73 u_int32_t nstat_sysinfo_flags
;
75 int64_t nstat_sysinfo_scalar
;
76 double nstat_sysinfo_distribution
;
78 } nstat_sysinfo_keyval
;
80 #define NSTAT_SYSINFO_FLAG_SCALAR 0x0001
81 #define NSTAT_SYSINFO_FLAG_DISTRIBUTION 0x0002
83 typedef struct nstat_sysinfo_counts
86 u_int32_t nstat_sysinfo_len
;
88 u_int8_t nstat_sysinfo_keyvals
[];
89 } nstat_sysinfo_counts
;
93 NSTAT_SYSINFO_KEY_MBUF_256B_TOTAL
= 1
94 ,NSTAT_SYSINFO_KEY_MBUF_2KB_TOTAL
= 2
95 ,NSTAT_SYSINFO_KEY_MBUF_4KB_TOTAL
= 3
96 ,NSTAT_SYSINFO_KEY_SOCK_MBCNT
= 4
97 ,NSTAT_SYSINFO_KEY_SOCK_ATMBLIMIT
= 5
98 ,NSTAT_SYSINFO_KEY_IPV4_AVGRTT
= 6
99 ,NSTAT_SYSINFO_KEY_IPV6_AVGRTT
= 7
100 ,NSTAT_SYSINFO_KEY_SEND_PLR
= 8
101 ,NSTAT_SYSINFO_KEY_RECV_PLR
= 9
102 ,NSTAT_SYSINFO_KEY_SEND_TLRTO
= 10
103 ,NSTAT_SYSINFO_KEY_SEND_REORDERRATE
= 11
107 #pragma mark -- Network Statistics Providers --
111 NSTAT_PROVIDER_ROUTE
= 1
112 ,NSTAT_PROVIDER_TCP
= 2
113 ,NSTAT_PROVIDER_UDP
= 3
114 ,NSTAT_PROVIDER_IFNET
= 4
115 ,NSTAT_PROVIDER_SYSINFO
= 5
118 typedef struct nstat_route_add_param
122 struct sockaddr_in v4
;
123 struct sockaddr_in6 v6
;
127 struct sockaddr_in v4
;
128 struct sockaddr_in6 v6
;
131 } nstat_route_add_param
;
133 typedef struct nstat_tcp_add_param
137 struct sockaddr_in v4
;
138 struct sockaddr_in6 v6
;
142 struct sockaddr_in v4
;
143 struct sockaddr_in6 v6
;
145 } nstat_tcp_add_param
;
147 typedef struct nstat_tcp_descriptor
151 struct sockaddr_in v4
;
152 struct sockaddr_in6 v6
;
157 struct sockaddr_in v4
;
158 struct sockaddr_in6 v6
;
165 u_int32_t sndbufsize
;
166 u_int32_t sndbufused
;
167 u_int32_t rcvbufsize
;
168 u_int32_t rcvbufused
;
172 u_int32_t traffic_class
;
173 u_int32_t traffic_mgt_flags
;
185 } nstat_tcp_descriptor
;
187 typedef struct nstat_tcp_add_param nstat_udp_add_param
;
189 typedef struct nstat_udp_descriptor
193 struct sockaddr_in v4
;
194 struct sockaddr_in6 v6
;
199 struct sockaddr_in v4
;
200 struct sockaddr_in6 v6
;
205 u_int32_t rcvbufsize
;
206 u_int32_t rcvbufused
;
207 u_int32_t traffic_class
;
218 } nstat_udp_descriptor
;
220 typedef struct nstat_route_descriptor
224 u_int64_t gateway_id
;
228 struct sockaddr_in v4
;
229 struct sockaddr_in6 v6
;
235 struct sockaddr_in v4
;
236 struct sockaddr_in6 v6
;
242 struct sockaddr_in v4
;
243 struct sockaddr_in6 v6
;
250 } nstat_route_descriptor
;
252 typedef struct nstat_ifnet_add_param
256 } nstat_ifnet_add_param
;
259 #define IF_DESCSIZE 128
261 typedef struct nstat_ifnet_descriptor
263 char name
[IFNAMSIZ
+1];
267 char description
[IF_DESCSIZE
];
268 } nstat_ifnet_descriptor
;
270 typedef struct nstat_sysinfo_descriptor
273 } nstat_sysinfo_descriptor
;
275 typedef struct nstat_sysinfo_add_param
277 /* To indicate which system level information should be collected */
279 } nstat_sysinfo_add_param
;
281 #define NSTAT_SYSINFO_MBUF_STATS 0x0001
282 #define NSTAT_SYSINFO_TCP_STATS 0x0002
284 #pragma mark -- Network Statistics User Client --
286 #define NET_STAT_CONTROL_NAME "com.apple.network.statistics"
290 // generic response messages
291 NSTAT_MSG_TYPE_SUCCESS
= 0
292 ,NSTAT_MSG_TYPE_ERROR
= 1
295 ,NSTAT_MSG_TYPE_ADD_SRC
= 1001
296 ,NSTAT_MSG_TYPE_ADD_ALL_SRCS
= 1002
297 ,NSTAT_MSG_TYPE_REM_SRC
= 1003
298 ,NSTAT_MSG_TYPE_QUERY_SRC
= 1004
299 ,NSTAT_MSG_TYPE_GET_SRC_DESC
= 1005
300 ,NSTAT_MSG_TYPE_SET_FILTER
= 1006
302 // Responses/Notfications
303 ,NSTAT_MSG_TYPE_SRC_ADDED
= 10001
304 ,NSTAT_MSG_TYPE_SRC_REMOVED
= 10002
305 ,NSTAT_MSG_TYPE_SRC_DESC
= 10003
306 ,NSTAT_MSG_TYPE_SRC_COUNTS
= 10004
307 ,NSTAT_MSG_TYPE_SYSINFO_COUNTS
= 10005
312 NSTAT_SRC_REF_ALL
= 0xffffffff
313 ,NSTAT_SRC_REF_INVALID
= 0
318 NSTAT_FILTER_NOZEROBYTES
= 0x01,
321 typedef struct nstat_msg_hdr
325 u_int32_t pad
; // unused for now
328 typedef struct nstat_msg_error
331 u_int32_t error
; // errno error
334 typedef struct nstat_msg_add_src
337 nstat_provider_id_t provider
;
339 } nstat_msg_add_src_req
;
341 typedef struct nstat_msg_add_all_srcs
344 nstat_provider_id_t provider
;
345 } nstat_msg_add_all_srcs
;
347 typedef struct nstat_msg_src_added
350 nstat_provider_id_t provider
;
351 nstat_src_ref_t srcref
;
352 } nstat_msg_src_added
;
354 typedef struct nstat_msg_rem_src
357 nstat_src_ref_t srcref
;
358 } nstat_msg_rem_src_req
;
360 typedef struct nstat_msg_get_src_description
363 nstat_src_ref_t srcref
;
364 } nstat_msg_get_src_description
;
366 typedef struct nstat_msg_set_filter
369 nstat_src_ref_t srcref
;
371 } nstat_msg_set_filter
;
373 typedef struct nstat_msg_src_description
376 nstat_src_ref_t srcref
;
377 nstat_provider_id_t provider
;
379 } nstat_msg_src_description
;
381 typedef struct nstat_msg_query_src
384 nstat_src_ref_t srcref
;
385 } nstat_msg_query_src_req
;
387 typedef struct nstat_msg_src_counts
390 nstat_src_ref_t srcref
;
392 } nstat_msg_src_counts
;
394 typedef struct nstat_msg_src_removed
397 nstat_src_ref_t srcref
;
398 } nstat_msg_src_removed
;
400 typedef struct nstat_msg_sysinfo_counts
403 nstat_src_ref_t srcref
;
404 nstat_sysinfo_counts counts
;
405 } nstat_msg_sysinfo_counts
;
407 typedef struct nstat_sysinfo_mbuf_stats
409 u_int32_t total_256b
;
412 u_int32_t sbmb_total
;
413 u_int32_t sb_atmbuflimit
;
415 u_int32_t memreleased
;
416 } nstat_sysinfo_mbuf_stats
;
418 typedef struct nstat_sysinfo_tcp_stats
420 u_int32_t ipv4_avgrtt
;
421 u_int32_t ipv6_avgrtt
;
424 u_int32_t send_tlrto_rate
;
425 u_int32_t send_reorder_rate
;
426 } nstat_sysinfo_tcp_stats
;
428 typedef struct nstat_sysinfo_data
432 nstat_sysinfo_mbuf_stats mb_stats
;
433 nstat_sysinfo_tcp_stats tcp_stats
;
435 } nstat_sysinfo_data
;
441 #ifdef XNU_KERNEL_PRIVATE
442 #include <sys/mcache.h>
444 #pragma mark -- Generic Network Statistics Provider --
446 typedef void * nstat_provider_cookie_t
;
448 #pragma mark -- Route Statistics Gathering Functions --
453 NSTAT_TX_FLAG_RETRANSMIT
= 1
458 NSTAT_RX_FLAG_DUPLICATE
= 1,
459 NSTAT_RX_FLAG_OUT_OF_ORDER
= 2
462 // indicates whether or not collection of statistics is enabled
463 extern int nstat_collect
;
465 void nstat_init(void);
467 // Route collection routines
468 void nstat_route_connect_attempt(struct rtentry
*rte
);
469 void nstat_route_connect_success(struct rtentry
*rte
);
470 void nstat_route_tx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
471 void nstat_route_rx(struct rtentry
*rte
, u_int32_t packets
, u_int32_t bytes
, u_int32_t flags
);
472 void nstat_route_rtt(struct rtentry
*rte
, u_int32_t rtt
, u_int32_t rtt_var
);
473 void nstat_route_detach(struct rtentry
*rte
);
477 void nstat_tcp_new_pcb(struct inpcb
*inp
);
478 void nstat_udp_new_pcb(struct inpcb
*inp
);
479 void nstat_route_new_entry(struct rtentry
*rt
);
480 void nstat_pcb_detach(struct inpcb
*inp
);
481 void nstat_pcb_cache(struct inpcb
*inp
);
482 void nstat_pcb_invalidate_cache(struct inpcb
*inp
);
485 void nstat_ifnet_threshold_reached(unsigned int ifindex
);
487 void nstat_sysinfo_send_data(struct nstat_sysinfo_data
*);
489 // locked_add_64 uses atomic operations on 32bit so the 64bit
490 // value can be properly read. The values are only ever incremented
491 // while under the socket lock, so on 64bit we don't actually need
492 // atomic operations to increment.
493 #if defined(__LP64__)
494 #define locked_add_64(__addr, __count) do { \
495 *(__addr) += (__count); \
498 #define locked_add_64(__addr, __count) do { \
499 atomic_add_64((__addr), (__count)); \
503 #endif /* XNU_KERNEL_PRIVATE */
505 #endif /* __NTSTAT_H__ */