1/* 2 * Copyright (c) 2010-2011 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 <netinet/in.h> 31 32#ifdef PRIVATE 33#pragma pack(push, 4) 34#pragma mark -- Common Data Structures -- 35 36#define __NSTAT_REVISION__ 1 37 38typedef u_int32_t nstat_provider_id_t; 39typedef u_int32_t nstat_src_ref_t; 40 41typedef struct nstat_counts 42{ 43 /* Counters */ 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))); 48 49 u_int32_t nstat_rxduplicatebytes; 50 u_int32_t nstat_rxoutoforderbytes; 51 u_int32_t nstat_txretransmit; 52 53 u_int32_t nstat_connectattempts; 54 u_int32_t nstat_connectsuccesses; 55 56 u_int32_t nstat_min_rtt; 57 u_int32_t nstat_avg_rtt; 58 u_int32_t nstat_var_rtt; 59} nstat_counts; 60 61#pragma mark -- Network Statistics Providers -- 62 63enum 64{ 65 NSTAT_PROVIDER_ROUTE = 1 66 ,NSTAT_PROVIDER_TCP = 2 67 ,NSTAT_PROVIDER_UDP = 3 68}; 69 70typedef struct nstat_route_add_param 71{ 72 union 73 { 74 struct sockaddr_in v4; 75 struct sockaddr_in6 v6; 76 } dst; 77 union 78 { 79 struct sockaddr_in v4; 80 struct sockaddr_in6 v6; 81 } mask; 82 u_int32_t ifindex; 83} nstat_route_add_param; 84 85typedef struct nstat_tcp_add_param 86{ 87 union 88 { 89 struct sockaddr_in v4; 90 struct sockaddr_in6 v6; 91 } local; 92 union 93 { 94 struct sockaddr_in v4; 95 struct sockaddr_in6 v6; 96 } remote; 97} nstat_tcp_add_param; 98 99typedef struct nstat_tcp_descriptor 100{ 101 union 102 { 103 struct sockaddr_in v4; 104 struct sockaddr_in6 v6; 105 } local; 106 107 union 108 { 109 struct sockaddr_in v4; 110 struct sockaddr_in6 v6; 111 } remote; 112 113 u_int32_t ifindex; 114 115 u_int32_t state; 116 117 u_int32_t sndbufsize; 118 u_int32_t sndbufused; 119 u_int32_t rcvbufsize; 120 u_int32_t rcvbufused; 121 u_int32_t txunacked; 122 u_int32_t txwindow; 123 u_int32_t txcwindow; 124 u_int32_t traffic_class; 125 126 u_int64_t upid; 127 u_int32_t pid; 128 char pname[64]; 129} nstat_tcp_descriptor; 130 131typedef struct nstat_tcp_add_param nstat_udp_add_param; 132 133typedef struct nstat_udp_descriptor 134{ 135 union 136 { 137 struct sockaddr_in v4; 138 struct sockaddr_in6 v6; 139 } local; 140 141 union 142 { 143 struct sockaddr_in v4; 144 struct sockaddr_in6 v6; 145 } remote; 146 147 u_int32_t ifindex; 148 149 u_int32_t rcvbufsize; 150 u_int32_t rcvbufused; 151 u_int32_t traffic_class; 152 153 u_int64_t upid; 154 u_int32_t pid; 155 char pname[64]; 156} nstat_udp_descriptor; 157 158typedef struct nstat_route_descriptor 159{ 160 u_int64_t id; 161 u_int64_t parent_id; 162 u_int64_t gateway_id; 163 164 union 165 { 166 struct sockaddr_in v4; 167 struct sockaddr_in6 v6; 168 struct sockaddr sa; 169 } dst; 170 171 union 172 { 173 struct sockaddr_in v4; 174 struct sockaddr_in6 v6; 175 struct sockaddr sa; 176 } mask; 177 178 union 179 { 180 struct sockaddr_in v4; 181 struct sockaddr_in6 v6; 182 struct sockaddr sa; 183 } gateway; 184 185 u_int32_t ifindex; 186 u_int32_t flags; 187 188} nstat_route_descriptor; 189 190#pragma mark -- Network Statistics User Client -- 191 192#define NET_STAT_CONTROL_NAME "com.apple.network.statistics" 193 194enum 195{ 196 // generic response messages 197 NSTAT_MSG_TYPE_SUCCESS = 0 198 ,NSTAT_MSG_TYPE_ERROR = 1 199 200 // Requests 201 ,NSTAT_MSG_TYPE_ADD_SRC = 1001 202 ,NSTAT_MSG_TYPE_ADD_ALL_SRCS = 1002 203 ,NSTAT_MSG_TYPE_REM_SRC = 1003 204 ,NSTAT_MSG_TYPE_QUERY_SRC = 1004 205 ,NSTAT_MSG_TYPE_GET_SRC_DESC = 1005 206 207 // Responses/Notfications 208 ,NSTAT_MSG_TYPE_SRC_ADDED = 10001 209 ,NSTAT_MSG_TYPE_SRC_REMOVED = 10002 210 ,NSTAT_MSG_TYPE_SRC_DESC = 10003 211 ,NSTAT_MSG_TYPE_SRC_COUNTS = 10004 212}; 213 214enum 215{ 216 NSTAT_SRC_REF_ALL = 0xffffffff 217 ,NSTAT_SRC_REF_INVALID = 0 218}; 219 220typedef struct nstat_msg_hdr 221{ 222 u_int64_t context; 223 u_int32_t type; 224 u_int32_t pad; // unused for now 225} nstat_msg_hdr; 226 227typedef struct nstat_msg_error 228{ 229 nstat_msg_hdr hdr; 230 u_int32_t error; // errno error 231} nstat_msg_error; 232 233typedef struct nstat_msg_add_src 234{ 235 nstat_msg_hdr hdr; 236 nstat_provider_id_t provider; 237 u_int8_t param[]; 238} nstat_msg_add_src_req; 239 240typedef struct nstat_msg_add_all_srcs 241{ 242 nstat_msg_hdr hdr; 243 nstat_provider_id_t provider; 244} nstat_msg_add_all_srcs; 245 246typedef struct nstat_msg_src_added 247{ 248 nstat_msg_hdr hdr; 249 nstat_provider_id_t provider; 250 nstat_src_ref_t srcref; 251} nstat_msg_src_added; 252 253typedef struct nstat_msg_rem_src 254{ 255 nstat_msg_hdr hdr; 256 nstat_src_ref_t srcref; 257} nstat_msg_rem_src_req; 258 259typedef struct nstat_msg_get_src_description 260{ 261 nstat_msg_hdr hdr; 262 nstat_src_ref_t srcref; 263} nstat_msg_get_src_description; 264 265typedef struct nstat_msg_src_description 266{ 267 nstat_msg_hdr hdr; 268 nstat_src_ref_t srcref; 269 nstat_provider_id_t provider; 270 u_int8_t data[]; 271} nstat_msg_src_description; 272 273typedef struct nstat_msg_query_src 274{ 275 nstat_msg_hdr hdr; 276 nstat_src_ref_t srcref; 277} nstat_msg_query_src_req; 278 279typedef struct nstat_msg_src_counts 280{ 281 nstat_msg_hdr hdr; 282 nstat_src_ref_t srcref; 283 nstat_counts counts; 284} nstat_msg_src_counts; 285 286typedef struct nstat_msg_src_removed 287{ 288 nstat_msg_hdr hdr; 289 nstat_src_ref_t srcref; 290} nstat_msg_src_removed; 291 292#pragma pack(pop) 293 294#endif /* PRIVATE */ 295 296#ifdef XNU_KERNEL_PRIVATE 297#include <sys/mcache.h> 298 299#pragma mark -- Generic Network Statistics Provider -- 300 301typedef void * nstat_provider_cookie_t; 302 303#pragma mark -- Route Statistics Gathering Functions -- 304struct rtentry; 305 306enum 307{ 308 NSTAT_TX_FLAG_RETRANSMIT = 1 309}; 310 311enum 312{ 313 NSTAT_RX_FLAG_DUPLICATE = 1, 314 NSTAT_RX_FLAG_OUT_OF_ORDER = 2 315}; 316 317// indicates whether or not collection of statistics is enabled 318extern int nstat_collect; 319 320void nstat_init(void); 321 322// Route collection routines 323void nstat_route_connect_attempt(struct rtentry *rte); 324void nstat_route_connect_success(struct rtentry *rte); 325void nstat_route_tx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags); 326void nstat_route_rx(struct rtentry *rte, u_int32_t packets, u_int32_t bytes, u_int32_t flags); 327void nstat_route_rtt(struct rtentry *rte, u_int32_t rtt, u_int32_t rtt_var); 328void nstat_route_detach(struct rtentry *rte); 329 330// watcher support 331struct inpcb; 332void nstat_tcp_new_pcb(struct inpcb *inp); 333void nstat_udp_new_pcb(struct inpcb *inp); 334void nstat_route_new_entry(struct rtentry *rt); 335void nstat_pcb_detach(struct inpcb *inp); 336 337// locked_add_64 uses atomic operations on 32bit so the 64bit 338// value can be properly read. The values are only ever incremented 339// while under the socket lock, so on 64bit we don't actually need 340// atomic operations to increment. 341#if defined(__LP64__) 342#define locked_add_64(__addr, __count) do { \ 343 *(__addr) += (__count); \ 344} while (0) 345#else 346#define locked_add_64(__addr, __count) do { \ 347 atomic_add_64((__addr), (__count)); \ 348} while (0) 349#endif 350 351#endif /* XNU_KERNEL_PRIVATE */ 352 353#endif /* __NTSTAT_H__ */ 354