]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
fe8ab488 | 2 | * Copyright (c) 2000-2014 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1992,7 NeXT Computer, Inc. | |
30 | * | |
31 | * Unix data structure initialization. | |
91447636 | 32 | * |
1c79356b A |
33 | */ |
34 | ||
35 | #include <mach/mach_types.h> | |
36 | ||
f427ee49 | 37 | #include <kern/startup.h> |
1c79356b A |
38 | #include <vm/vm_kern.h> |
39 | #include <mach/vm_prot.h> | |
40 | ||
41 | #include <sys/param.h> | |
91447636 | 42 | #include <sys/buf_internal.h> |
b0d623f7 A |
43 | #include <sys/file_internal.h> |
44 | #include <sys/proc_internal.h> | |
6d2010ae | 45 | #include <sys/mcache.h> |
1c79356b A |
46 | #include <sys/mbuf.h> |
47 | #include <sys/systm.h> | |
48 | #include <sys/tty.h> | |
0c530ab8 A |
49 | #include <sys/vnode.h> |
50 | #include <sys/sysctl.h> | |
b0d623f7 | 51 | #include <machine/cons.h> |
c910b4d9 | 52 | #include <pexpert/pexpert.h> |
b0d623f7 | 53 | #include <sys/socketvar.h> |
6d2010ae | 54 | #include <pexpert/pexpert.h> |
3e170ce0 | 55 | #include <netinet/tcp_var.h> |
1c79356b | 56 | |
6d2010ae | 57 | extern uint32_t kern_maxvnodes; |
91447636 A |
58 | extern vm_map_t mb_map; |
59 | ||
f427ee49 | 60 | #if INET |
b0d623f7 A |
61 | extern uint32_t tcp_sendspace; |
62 | extern uint32_t tcp_recvspace; | |
2d21ac55 | 63 | #endif |
91447636 | 64 | |
39236c6e | 65 | void bsd_bufferinit(void); |
1c79356b | 66 | |
0a7de745 | 67 | unsigned int bsd_mbuf_cluster_reserve(boolean_t *); |
6d2010ae | 68 | void bsd_scale_setup(int); |
b0d623f7 | 69 | void bsd_exec_setup(int); |
0c530ab8 | 70 | |
1c79356b A |
71 | /* |
72 | * Declare these as initialized data so we can patch them. | |
73 | */ | |
74 | ||
0a7de745 | 75 | #ifdef NBUF |
0c530ab8 | 76 | int max_nbuf_headers = NBUF; |
6d2010ae | 77 | int niobuf_headers = (NBUF / 2) + 2048; |
0a7de745 A |
78 | int nbuf_hashelements = NBUF; |
79 | int nbuf_headers = NBUF; | |
1c79356b | 80 | #else |
0c530ab8 | 81 | int max_nbuf_headers = 0; |
2d21ac55 | 82 | int niobuf_headers = 0; |
0a7de745 A |
83 | int nbuf_hashelements = 0; |
84 | int nbuf_headers = 0; | |
6601e61a | 85 | #endif |
4452a7af | 86 | |
0a7de745 A |
87 | SYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RD | CTLFLAG_LOCKED, &nbuf_headers, 0, ""); |
88 | SYSCTL_INT(_kern, OID_AUTO, maxnbuf, CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &max_nbuf_headers, 0, ""); | |
0c530ab8 A |
89 | |
90 | __private_extern__ int customnbuf = 0; | |
91447636 | 91 | int ncl = 0; |
39236c6e | 92 | |
f427ee49 A |
93 | /* Indicates a server boot when set */ |
94 | TUNABLE(int, serverperfmode, "serverperfmode", 0); | |
95 | ||
39236c6e | 96 | #if SOCKETS |
c910b4d9 | 97 | static unsigned int mbuf_poolsz; |
39236c6e | 98 | #endif |
91447636 A |
99 | |
100 | vm_map_t buffer_map; | |
101 | vm_map_t bufferhdr_map; | |
b0d623f7 | 102 | static int vnodes_sized = 0; |
91447636 | 103 | |
39236c6e | 104 | extern void bsd_startupearly(void); |
1c79356b A |
105 | |
106 | void | |
91447636 | 107 | bsd_startupearly(void) |
1c79356b | 108 | { |
91447636 A |
109 | vm_offset_t firstaddr; |
110 | vm_size_t size; | |
111 | kern_return_t ret; | |
1c79356b | 112 | |
0c530ab8 | 113 | /* clip the number of buf headers upto 16k */ |
0a7de745 | 114 | if (max_nbuf_headers == 0) { |
f427ee49 | 115 | max_nbuf_headers = (int)atop_kernel(sane_size / 50); /* Get 2% of ram, but no more than we can map */ |
0a7de745 | 116 | } |
f427ee49 | 117 | if ((customnbuf == 0) && ((unsigned int)max_nbuf_headers > 16384)) { |
0c530ab8 | 118 | max_nbuf_headers = 16384; |
0a7de745 A |
119 | } |
120 | if (max_nbuf_headers < CONFIG_MIN_NBUF) { | |
2d21ac55 | 121 | max_nbuf_headers = CONFIG_MIN_NBUF; |
0a7de745 | 122 | } |
0c530ab8 A |
123 | |
124 | /* clip the number of hash elements to 200000 */ | |
0a7de745 | 125 | if ((customnbuf == 0) && nbuf_hashelements == 0) { |
f427ee49 A |
126 | nbuf_hashelements = (int)atop_kernel(sane_size / 50); |
127 | if ((unsigned int)nbuf_hashelements > 200000) { | |
0c530ab8 | 128 | nbuf_hashelements = 200000; |
0a7de745 A |
129 | } |
130 | } else { | |
0c530ab8 | 131 | nbuf_hashelements = max_nbuf_headers; |
0a7de745 | 132 | } |
1c79356b | 133 | |
6d2010ae | 134 | if (niobuf_headers == 0) { |
0a7de745 | 135 | if (max_nbuf_headers < 4096) { |
6d2010ae | 136 | niobuf_headers = max_nbuf_headers; |
0a7de745 | 137 | } else { |
6d2010ae | 138 | niobuf_headers = (max_nbuf_headers / 2) + 2048; |
0a7de745 | 139 | } |
6d2010ae | 140 | } |
0a7de745 | 141 | if (niobuf_headers < CONFIG_MIN_NIOBUF) { |
2d21ac55 | 142 | niobuf_headers = CONFIG_MIN_NIOBUF; |
0a7de745 | 143 | } |
1c79356b | 144 | |
2d21ac55 | 145 | size = (max_nbuf_headers + niobuf_headers) * sizeof(struct buf); |
91447636 | 146 | size = round_page(size); |
1c79356b A |
147 | |
148 | ret = kmem_suballoc(kernel_map, | |
0a7de745 A |
149 | &firstaddr, |
150 | size, | |
151 | FALSE, | |
152 | VM_FLAGS_ANYWHERE, | |
153 | VM_MAP_KERNEL_FLAGS_NONE, | |
154 | VM_KERN_MEMORY_FILE, | |
155 | &bufferhdr_map); | |
156 | ||
157 | if (ret != KERN_SUCCESS) { | |
1c79356b | 158 | panic("Failed to create bufferhdr_map"); |
0a7de745 | 159 | } |
91447636 | 160 | |
1c79356b | 161 | ret = kernel_memory_allocate(bufferhdr_map, |
0a7de745 A |
162 | &firstaddr, |
163 | size, | |
164 | 0, | |
165 | KMA_HERE | KMA_KOBJECT, | |
166 | VM_KERN_MEMORY_FILE); | |
1c79356b | 167 | |
0a7de745 | 168 | if (ret != KERN_SUCCESS) { |
1c79356b | 169 | panic("Failed to allocate bufferhdr_map"); |
0a7de745 | 170 | } |
1c79356b | 171 | |
2d21ac55 A |
172 | buf_headers = (struct buf *) firstaddr; |
173 | bzero(buf_headers, size); | |
1c79356b | 174 | |
2d21ac55 | 175 | #if SOCKETS |
0c530ab8 | 176 | { |
0a7de745 | 177 | static const unsigned int maxspace = 128 * 1024; |
91447636 | 178 | int scale; |
1c79356b | 179 | |
0b4c1975 | 180 | nmbclusters = bsd_mbuf_cluster_reserve(NULL) / MCLBYTES; |
0c530ab8 | 181 | |
f427ee49 | 182 | #if INET |
1c79356b A |
183 | if ((scale = nmbclusters / NMBCLUSTERS) > 1) { |
184 | tcp_sendspace *= scale; | |
185 | tcp_recvspace *= scale; | |
186 | ||
0a7de745 | 187 | if (tcp_sendspace > maxspace) { |
593a1d5f | 188 | tcp_sendspace = maxspace; |
0a7de745 A |
189 | } |
190 | if (tcp_recvspace > maxspace) { | |
593a1d5f | 191 | tcp_recvspace = maxspace; |
0a7de745 | 192 | } |
1c79356b | 193 | } |
f427ee49 | 194 | #endif /* INET */ |
1c79356b | 195 | } |
2d21ac55 | 196 | #endif /* SOCKETS */ |
0c530ab8 | 197 | |
b0d623f7 | 198 | if (vnodes_sized == 0) { |
6d2010ae A |
199 | if (!PE_get_default("kern.maxvnodes", &desiredvnodes, sizeof(desiredvnodes))) { |
200 | /* | |
0a7de745 A |
201 | * Size vnodes based on memory |
202 | * Number vnodes is (memsize/64k) + 1024 | |
6d2010ae | 203 | * This is the calculation that is used by launchd in tiger |
0a7de745 | 204 | * we are clipping the max based on 16G |
6d2010ae A |
205 | * ie ((16*1024*1024*1024)/(64 *1024)) + 1024 = 263168; |
206 | * CONFIG_VNODES is set to 263168 for "medium" configurations (the default) | |
0a7de745 | 207 | * but can be smaller or larger. |
6d2010ae | 208 | */ |
f427ee49 | 209 | desiredvnodes = (int)(sane_size / 65536) + 1024; |
6d2010ae | 210 | #ifdef CONFIG_VNODES |
0a7de745 A |
211 | if (desiredvnodes > CONFIG_VNODES) { |
212 | desiredvnodes = CONFIG_VNODES; | |
213 | } | |
6d2010ae A |
214 | #endif |
215 | } | |
216 | vnodes_sized = 1; | |
b0d623f7 | 217 | } |
1c79356b A |
218 | } |
219 | ||
220 | void | |
91447636 | 221 | bsd_bufferinit(void) |
1c79356b | 222 | { |
39236c6e | 223 | #if SOCKETS |
91447636 | 224 | kern_return_t ret; |
39236c6e | 225 | #endif |
b0d623f7 A |
226 | /* |
227 | * Note: Console device initialized in kminit() from bsd_autoconf() | |
228 | * prior to call to us in bsd_init(). | |
229 | */ | |
1c79356b A |
230 | |
231 | bsd_startupearly(); | |
232 | ||
2d21ac55 | 233 | #if SOCKETS |
91447636 | 234 | ret = kmem_suballoc(kernel_map, |
0a7de745 A |
235 | (vm_offset_t *) &mbutl, |
236 | (vm_size_t) (nmbclusters * MCLBYTES), | |
237 | FALSE, | |
238 | VM_FLAGS_ANYWHERE, | |
239 | VM_MAP_KERNEL_FLAGS_NONE, | |
240 | VM_KERN_MEMORY_MBUF, | |
241 | &mb_map); | |
242 | ||
243 | if (ret != KERN_SUCCESS) { | |
1c79356b | 244 | panic("Failed to allocate mb_map\n"); |
0a7de745 | 245 | } |
2d21ac55 | 246 | #endif /* SOCKETS */ |
0b4e3aa0 | 247 | |
91447636 A |
248 | /* |
249 | * Set up buffers, so they can be used to read disk labels. | |
250 | */ | |
251 | bufinit(); | |
0b4e3aa0 | 252 | } |
0c530ab8 | 253 | |
b0d623f7 A |
254 | /* 512 MB (K32) or 2 GB (K64) hard limit on size of the mbuf pool */ |
255 | #if !defined(__LP64__) | |
0a7de745 | 256 | #define MAX_MBUF_POOL (512 << MBSHIFT) |
b0d623f7 | 257 | #else |
0a7de745 | 258 | #define MAX_MBUF_POOL (2ULL << GBSHIFT) |
b0d623f7 | 259 | #endif /* !__LP64__ */ |
0a7de745 | 260 | #define MAX_NCL (MAX_MBUF_POOL >> MCLSHIFT) |
2d21ac55 | 261 | |
39236c6e | 262 | #if SOCKETS |
0c530ab8 A |
263 | /* |
264 | * this has been broken out into a separate routine that | |
265 | * can be called from the x86 early vm initialization to | |
266 | * determine how much lo memory to reserve on systems with | |
267 | * DMA hardware that can't fully address all of the physical | |
268 | * memory that is present. | |
269 | */ | |
b0d623f7 | 270 | unsigned int |
0b4c1975 | 271 | bsd_mbuf_cluster_reserve(boolean_t *overridden) |
0c530ab8 | 272 | { |
b0d623f7 | 273 | int mbuf_pool = 0; |
0b4c1975 | 274 | static boolean_t was_overridden = FALSE; |
b0d623f7 | 275 | |
c910b4d9 | 276 | /* If called more than once, return the previously calculated size */ |
0a7de745 | 277 | if (mbuf_poolsz != 0) { |
b0d623f7 | 278 | goto done; |
0a7de745 | 279 | } |
c910b4d9 | 280 | |
b0d623f7 A |
281 | /* |
282 | * Some of these are parsed in parse_bsd_args(), but for x86 we get | |
283 | * here early from i386_vm_init() and so we parse them now, in order | |
284 | * to correctly compute the size of the low-memory VM pool. It is | |
285 | * redundant but rather harmless. | |
286 | */ | |
0a7de745 A |
287 | (void) PE_parse_boot_argn("ncl", &ncl, sizeof(ncl)); |
288 | (void) PE_parse_boot_argn("mbuf_pool", &mbuf_pool, sizeof(mbuf_pool)); | |
b0d623f7 A |
289 | |
290 | /* | |
291 | * Convert "mbuf_pool" from MB to # of 2KB clusters; it is | |
292 | * equivalent to "ncl", except that it uses different unit. | |
293 | */ | |
0a7de745 | 294 | if (mbuf_pool != 0) { |
b0d623f7 | 295 | ncl = (mbuf_pool << MBSHIFT) >> MCLSHIFT; |
0a7de745 | 296 | } |
0c530ab8 | 297 | |
0a7de745 A |
298 | if (sane_size > (64 * 1024 * 1024) || ncl != 0) { |
299 | if (ncl || serverperfmode) { | |
0b4c1975 | 300 | was_overridden = TRUE; |
0a7de745 | 301 | } |
0b4c1975 | 302 | |
0a7de745 | 303 | if ((nmbclusters = ncl) == 0) { |
b0d623f7 | 304 | /* Auto-configure the mbuf pool size */ |
f427ee49 | 305 | nmbclusters = mbuf_default_ncl(mem_actual); |
b0d623f7 A |
306 | } else { |
307 | /* Make sure it's not odd in case ncl is manually set */ | |
0a7de745 | 308 | if (nmbclusters & 0x1) { |
b0d623f7 | 309 | --nmbclusters; |
0a7de745 | 310 | } |
b0d623f7 A |
311 | |
312 | /* And obey the upper limit */ | |
0a7de745 | 313 | if (nmbclusters > MAX_NCL) { |
b0d623f7 | 314 | nmbclusters = MAX_NCL; |
0a7de745 | 315 | } |
0c530ab8 | 316 | } |
6d2010ae | 317 | |
3e170ce0 | 318 | /* Round it down to nearest multiple of PAGE_SIZE */ |
f427ee49 | 319 | nmbclusters = (unsigned int)P2ROUNDDOWN(nmbclusters, NCLPG); |
c910b4d9 A |
320 | } |
321 | mbuf_poolsz = nmbclusters << MCLSHIFT; | |
322 | done: | |
0a7de745 | 323 | if (overridden) { |
0b4c1975 | 324 | *overridden = was_overridden; |
0a7de745 | 325 | } |
0b4c1975 | 326 | |
0a7de745 | 327 | return mbuf_poolsz; |
0c530ab8 | 328 | } |
39236c6e A |
329 | #endif |
330 | ||
b0d623f7 A |
331 | #if defined(__LP64__) |
332 | extern int tcp_tcbhashsize; | |
333 | extern int max_cached_sock_count; | |
0a7de745 | 334 | #endif |
b0d623f7 A |
335 | |
336 | ||
337 | void | |
6d2010ae | 338 | bsd_scale_setup(int scale) |
b0d623f7 A |
339 | { |
340 | #if defined(__LP64__) | |
6d2010ae A |
341 | if ((scale > 0) && (serverperfmode == 0)) { |
342 | maxproc *= scale; | |
343 | maxprocperuid = (maxproc * 2) / 3; | |
5ba3f43e A |
344 | if (scale > 2) { |
345 | maxfiles *= scale; | |
0a7de745 | 346 | maxfilesperproc = maxfiles / 2; |
5ba3f43e | 347 | } |
6d2010ae A |
348 | } |
349 | /* Apply server scaling rules */ | |
0a7de745 | 350 | if ((scale > 0) && (serverperfmode != 0)) { |
b0d623f7 A |
351 | maxproc = 2500 * scale; |
352 | hard_maxproc = maxproc; | |
353 | /* no fp usage */ | |
0a7de745 | 354 | maxprocperuid = (maxproc * 3) / 4; |
b0d623f7 | 355 | maxfiles = (150000 * scale); |
0a7de745 | 356 | maxfilesperproc = maxfiles / 2; |
b0d623f7 A |
357 | desiredvnodes = maxfiles; |
358 | vnodes_sized = 1; | |
3e170ce0 | 359 | tcp_tfo_backlog = 100 * scale; |
b0d623f7 | 360 | if (scale > 4) { |
fe8ab488 | 361 | /* clip somaxconn at 32G level */ |
b0d623f7 | 362 | somaxconn = 2048; |
3e170ce0 A |
363 | /* |
364 | * For scale > 4 (> 32G), clip | |
fe8ab488 A |
365 | * tcp_tcbhashsize to 32K |
366 | */ | |
0a7de745 | 367 | tcp_tcbhashsize = 32 * 1024; |
3e170ce0 | 368 | |
b0d623f7 A |
369 | if (scale > 7) { |
370 | /* clip at 64G level */ | |
b0d623f7 A |
371 | max_cached_sock_count = 165000; |
372 | } else { | |
0a7de745 | 373 | max_cached_sock_count = 60000 + ((scale - 1) * 15000); |
b0d623f7 A |
374 | } |
375 | } else { | |
0a7de745 A |
376 | somaxconn = 512 * scale; |
377 | tcp_tcbhashsize = 4 * 1024 * scale; | |
378 | max_cached_sock_count = 60000 + ((scale - 1) * 15000); | |
b0d623f7 A |
379 | } |
380 | } | |
39037602 | 381 | |
0a7de745 | 382 | if (maxproc > hard_maxproc) { |
39037602 A |
383 | hard_maxproc = maxproc; |
384 | } | |
b0d623f7 A |
385 | #endif |
386 | bsd_exec_setup(scale); | |
387 | } |