| 1 | /* |
| 2 | * Copyright (c) 2017-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 | |
| 29 | #ifndef __NET_API_STATS__ |
| 30 | #define __NET_API_STATS__ |
| 31 | |
| 32 | #ifdef PRIVATE |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #define NAS_HAS_FLTR_OS_COUNTS 1 |
| 36 | |
| 37 | /* |
| 38 | * net_api_stats counts the usage of the networking APIs |
| 39 | * |
| 40 | * Note: we are using signed 64 bit values to detect and prevent wrap around |
| 41 | */ |
| 42 | struct net_api_stats { |
| 43 | /* |
| 44 | * Interface Filters |
| 45 | */ |
| 46 | int64_t nas_iflt_attach_count; // Currently attached |
| 47 | int64_t nas_iflt_attach_os_count; |
| 48 | int64_t nas_iflt_attach_total; // Total number of attachments |
| 49 | int64_t nas_iflt_attach_os_total; |
| 50 | |
| 51 | /* |
| 52 | * IP Filters |
| 53 | */ |
| 54 | int64_t nas_ipf_add_count; // Currently attached |
| 55 | int64_t nas_ipf_add_os_count; |
| 56 | int64_t nas_ipf_add_total; // Total number of attachments |
| 57 | int64_t nas_ipf_add_os_total; |
| 58 | |
| 59 | /* |
| 60 | * Socket Filters |
| 61 | */ |
| 62 | int64_t nas_sfltr_register_count; // Currently attached |
| 63 | int64_t nas_sfltr_register_os_count; |
| 64 | int64_t nas_sfltr_register_total; // Total number of attachments |
| 65 | int64_t nas_sfltr_register_os_total; |
| 66 | |
| 67 | /* |
| 68 | * Sockets |
| 69 | */ |
| 70 | int64_t nas_socket_alloc_total; |
| 71 | int64_t nas_socket_in_kernel_total; |
| 72 | int64_t nas_socket_in_kernel_os_total; |
| 73 | int64_t nas_socket_necp_clientuuid_total; |
| 74 | |
| 75 | /* |
| 76 | * Sockets per protocol domains |
| 77 | */ |
| 78 | int64_t nas_socket_domain_local_total; |
| 79 | int64_t nas_socket_domain_route_total; |
| 80 | int64_t nas_socket_domain_inet_total; |
| 81 | int64_t nas_socket_domain_inet6_total; |
| 82 | int64_t nas_socket_domain_system_total; |
| 83 | int64_t nas_socket_domain_multipath_total; |
| 84 | int64_t nas_socket_domain_key_total; |
| 85 | int64_t nas_socket_domain_ndrv_total; |
| 86 | int64_t nas_socket_domain_other_total; |
| 87 | |
| 88 | /* |
| 89 | * Sockets per domain and type |
| 90 | */ |
| 91 | int64_t nas_socket_inet_stream_total; |
| 92 | int64_t nas_socket_inet_dgram_total; |
| 93 | int64_t nas_socket_inet_dgram_connected; |
| 94 | int64_t nas_socket_inet_dgram_dns; // port 53 |
| 95 | int64_t nas_socket_inet_dgram_no_data; // typically for interface ioctl |
| 96 | |
| 97 | int64_t nas_socket_inet6_stream_total; |
| 98 | int64_t nas_socket_inet6_dgram_total; |
| 99 | int64_t nas_socket_inet6_dgram_connected; |
| 100 | int64_t nas_socket_inet6_dgram_dns; // port 53 |
| 101 | int64_t nas_socket_inet6_dgram_no_data; // typically for interface ioctl |
| 102 | |
| 103 | /* |
| 104 | * Multicast join |
| 105 | */ |
| 106 | int64_t nas_socket_mcast_join_total; |
| 107 | int64_t nas_socket_mcast_join_os_total; |
| 108 | |
| 109 | /* |
| 110 | * IPv6 Extension Header Socket API |
| 111 | */ |
| 112 | int64_t nas_sock_inet6_stream_exthdr_in; |
| 113 | int64_t nas_sock_inet6_stream_exthdr_out; |
| 114 | int64_t nas_sock_inet6_dgram_exthdr_in; |
| 115 | int64_t nas_sock_inet6_dgram_exthdr_out; |
| 116 | |
| 117 | /* |
| 118 | * Nexus flows |
| 119 | */ |
| 120 | int64_t nas_nx_flow_inet_stream_total; |
| 121 | int64_t nas_nx_flow_inet_dgram_total; |
| 122 | |
| 123 | int64_t nas_nx_flow_inet6_stream_total; |
| 124 | int64_t nas_nx_flow_inet6_dgram_total; |
| 125 | |
| 126 | /* |
| 127 | * Interfaces |
| 128 | */ |
| 129 | int64_t nas_ifnet_alloc_count; |
| 130 | int64_t nas_ifnet_alloc_total; |
| 131 | int64_t nas_ifnet_alloc_os_count; |
| 132 | int64_t nas_ifnet_alloc_os_total; |
| 133 | |
| 134 | /* |
| 135 | * PF |
| 136 | */ |
| 137 | int64_t nas_pf_addrule_total; |
| 138 | int64_t nas_pf_addrule_os; |
| 139 | |
| 140 | /* |
| 141 | * vmnet API |
| 142 | */ |
| 143 | int64_t nas_vmnet_total; |
| 144 | }; |
| 145 | |
| 146 | #ifdef XNU_KERNEL_PRIVATE |
| 147 | extern struct net_api_stats net_api_stats; |
| 148 | |
| 149 | /* |
| 150 | * Increment up to the max value of int64_t |
| 151 | */ |
| 152 | #define INC_ATOMIC_INT64_LIM(counter) { \ |
| 153 | int64_t val; \ |
| 154 | do { \ |
| 155 | val = counter; \ |
| 156 | if (val >= INT64_MAX) { \ |
| 157 | break; \ |
| 158 | } \ |
| 159 | } while (!OSCompareAndSwap64(val, val + 1, &(counter))); \ |
| 160 | } |
| 161 | #endif /* XNU_KERNEL_PRIVATE */ |
| 162 | |
| 163 | #endif /* PRIVATE */ |
| 164 | |
| 165 | #endif /* __NET_API_STATS__ */ |
| 166 | |