1 /*- 2 * Copyright (c) 2001-2002 Luigi Rizzo 3 * 4 * Supported by: the Xorp Project (www.xorp.org) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_route.h" 32 #include "opt_device_polling.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/kthread.h> 38 #include <sys/proc.h> 39 #include <sys/resourcevar.h> 40 #include <sys/socket.h> /* needed by net/if.h */ 41 #include <sys/sockio.h> 42 #include <sys/sysctl.h> 43 #include <sys/syslog.h> 44 #include <sys/vimage.h> 45 46 #include <net/if.h> /* for IFF_* flags */ 47 #include <net/netisr.h> /* for NETISR_POLL */ 48 #include <net/route.h> 49 #include <net/vnet.h> 50 51 static void netisr_poll(void); /* the two netisr handlers */ 52 static void netisr_pollmore(void); 53 static int poll_switch(SYSCTL_HANDLER_ARGS); 54 55 void hardclock_device_poll(void); /* hook from hardclock */ 56 57 static struct mtx poll_mtx; 58 59 /* 60 * Polling support for [network] device drivers. 61 * 62 * Drivers which support this feature can register with the 63 * polling code. 64 * 65 * If registration is successful, the driver must disable interrupts, 66 * and further I/O is performed through the handler, which is invoked 67 * (at least once per clock tick) with 3 arguments: the "arg" passed at 68 * register time (a struct ifnet pointer), a command, and a "count" limit. 69 * 70 * The command can be one of the following: 71 * POLL_ONLY: quick move of "count" packets from input/output queues. 72 * POLL_AND_CHECK_STATUS: as above, plus check status registers or do 73 * other more expensive operations. This command is issued periodically 74 * but less frequently than POLL_ONLY. 75 * 76 * The count limit specifies how much work the handler can do during the 77 * call -- typically this is the number of packets to be received, or 78 * transmitted, etc. (drivers are free to interpret this number, as long 79 * as the max time spent in the function grows roughly linearly with the 80 * count). 81 * 82 * Polling is enabled and disabled via setting IFCAP_POLLING flag on 83 * the interface. The driver ioctl handler should register interface 84 * with polling and disable interrupts, if registration was successful. 85 * 86 * A second variable controls the sharing of CPU between polling/kernel 87 * network processing, and other activities (typically userlevel tasks): 88 * kern.polling.user_frac (between 0 and 100, default 50) sets the share 89 * of CPU allocated to user tasks. CPU is allocated proportionally to the 90 * shares, by dynamically adjusting the "count" (poll_burst). 91 * 92 * Other parameters can should be left to their default values. 93 * The following constraints hold 94 * 95 * 1 <= poll_each_burst <= poll_burst <= poll_burst_max 96 * 0 <= poll_each_burst 97 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX 98 */ 99 100 #define MIN_POLL_BURST_MAX 10 101 #define MAX_POLL_BURST_MAX 1000 102 103 static uint32_t poll_burst = 5; 104 static uint32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ 105 static uint32_t poll_each_burst = 5; 106 107 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, 108 "Device polling parameters"); 109 110 SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RD, 111 &poll_burst, 0, "Current polling burst size"); 112 113 static int poll_burst_max_sysctl(SYSCTL_HANDLER_ARGS) 114 { 115 uint32_t val = poll_burst_max; 116 int error; 117 118 error = sysctl_handle_int(oidp, &val, 0, req); 119 if (error || !req->newptr ) 120 return (error); 121 if (val < MIN_POLL_BURST_MAX || val > MAX_POLL_BURST_MAX) 122 return (EINVAL); 123 124 mtx_lock(&poll_mtx); 125 poll_burst_max = val; 126 if (poll_burst > poll_burst_max) 127 poll_burst = poll_burst_max; 128 if (poll_each_burst > poll_burst_max) 129 poll_each_burst = MIN_POLL_BURST_MAX; 130 mtx_unlock(&poll_mtx); 131 132 return (0); 133 } 134 SYSCTL_PROC(_kern_polling, OID_AUTO, burst_max, CTLTYPE_UINT | CTLFLAG_RW, 135 0, sizeof(uint32_t), poll_burst_max_sysctl, "I", "Max Polling burst size"); 136 137 static int poll_each_burst_sysctl(SYSCTL_HANDLER_ARGS) 138 { 139 uint32_t val = poll_each_burst; 140 int error; 141 142 error = sysctl_handle_int(oidp, &val, 0, req); 143 if (error || !req->newptr ) 144 return (error); 145 if (val < 1) 146 return (EINVAL); 147 148 mtx_lock(&poll_mtx); 149 if (val > poll_burst_max) { 150 mtx_unlock(&poll_mtx); 151 return (EINVAL); 152 } 153 poll_each_burst = val; 154 mtx_unlock(&poll_mtx); 155 156 return (0); 157 } 158 SYSCTL_PROC(_kern_polling, OID_AUTO, each_burst, CTLTYPE_UINT | CTLFLAG_RW, 159 0, sizeof(uint32_t), poll_each_burst_sysctl, "I", 160 "Max size of each burst"); 161 162 static uint32_t poll_in_idle_loop=0; /* do we poll in idle loop ? */ 163 SYSCTL_UINT(_kern_polling, OID_AUTO, idle_poll, CTLFLAG_RW, 164 &poll_in_idle_loop, 0, "Enable device polling in idle loop"); 165 166 static uint32_t user_frac = 50; 167 static int user_frac_sysctl(SYSCTL_HANDLER_ARGS) 168 { 169 uint32_t val = user_frac; 170 int error; 171 172 error = sysctl_handle_int(oidp, &val, 0, req); 173 if (error || !req->newptr ) 174 return (error); 175 if (val < 0 || val > 99) 176 return (EINVAL); 177 178 mtx_lock(&poll_mtx); 179 user_frac = val; 180 mtx_unlock(&poll_mtx); 181 182 return (0); 183 } 184 SYSCTL_PROC(_kern_polling, OID_AUTO, user_frac, CTLTYPE_UINT | CTLFLAG_RW, 185 0, sizeof(uint32_t), user_frac_sysctl, "I", 186 "Desired user fraction of cpu time"); 187 188 static uint32_t reg_frac_count = 0; 189 static uint32_t reg_frac = 20 ; 190 static int reg_frac_sysctl(SYSCTL_HANDLER_ARGS) 191 { 192 uint32_t val = reg_frac; 193 int error; 194 195 error = sysctl_handle_int(oidp, &val, 0, req); 196 if (error || !req->newptr ) 197 return (error); 198 if (val < 1 || val > hz) 199 return (EINVAL); 200 201 mtx_lock(&poll_mtx); 202 reg_frac = val; 203 if (reg_frac_count >= reg_frac) 204 reg_frac_count = 0; 205 mtx_unlock(&poll_mtx); 206 207 return (0); 208 } 209 SYSCTL_PROC(_kern_polling, OID_AUTO, reg_frac, CTLTYPE_UINT | CTLFLAG_RW, 210 0, sizeof(uint32_t), reg_frac_sysctl, "I", 211 "Every this many cycles check registers"); 212 213 static uint32_t short_ticks; 214 SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RD, 215 &short_ticks, 0, "Hardclock ticks shorter than they should be"); 216 217 static uint32_t lost_polls; 218 SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RD, 219 &lost_polls, 0, "How many times we would have lost a poll tick"); 220 221 static uint32_t pending_polls; 222 SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RD, 223 &pending_polls, 0, "Do we need to poll again"); 224 225 static int residual_burst = 0; 226 SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RD, 227 &residual_burst, 0, "# of residual cycles in burst"); 228 229 static uint32_t poll_handlers; /* next free entry in pr[]. */ 230 SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, 231 &poll_handlers, 0, "Number of registered poll handlers"); 232 233 static int polling = 0; 234 SYSCTL_PROC(_kern_polling, OID_AUTO, enable, CTLTYPE_UINT | CTLFLAG_RW, 235 0, sizeof(int), poll_switch, "I", "Switch polling for all interfaces"); 236 237 static uint32_t phase; 238 SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RD, 239 &phase, 0, "Polling phase"); 240 241 static uint32_t suspect; 242 SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RD, 243 &suspect, 0, "suspect event"); 244 245 static uint32_t stalled; 246 SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RD, 247 &stalled, 0, "potential stalls"); 248 249 static uint32_t idlepoll_sleeping; /* idlepoll is sleeping */ 250 SYSCTL_UINT(_kern_polling, OID_AUTO, idlepoll_sleeping, CTLFLAG_RD, 251 &idlepoll_sleeping, 0, "idlepoll is sleeping"); 252 253 254 #define POLL_LIST_LEN 128 255 struct pollrec { 256 poll_handler_t *handler; 257 struct ifnet *ifp; 258 }; 259 260 static struct pollrec pr[POLL_LIST_LEN]; 261 262 static void 263 init_device_poll(void) 264 { 265 266 mtx_init(&poll_mtx, "polling", NULL, MTX_DEF); 267 netisr_register(NETISR_POLL, (netisr_t *)netisr_poll, NULL, 0); 268 netisr_register(NETISR_POLLMORE, (netisr_t *)netisr_pollmore, NULL, 0); 269 } 270 SYSINIT(device_poll, SI_SUB_CLOCKS, SI_ORDER_MIDDLE, init_device_poll, NULL); 271 272 273 /* 274 * Hook from hardclock. Tries to schedule a netisr, but keeps track 275 * of lost ticks due to the previous handler taking too long. 276 * Normally, this should not happen, because polling handler should 277 * run for a short time. However, in some cases (e.g. when there are 278 * changes in link status etc.) the drivers take a very long time 279 * (even in the order of milliseconds) to reset and reconfigure the 280 * device, causing apparent lost polls. 281 * 282 * The first part of the code is just for debugging purposes, and tries 283 * to count how often hardclock ticks are shorter than they should, 284 * meaning either stray interrupts or delayed events. 285 */ 286 void 287 hardclock_device_poll(void) 288 { 289 static struct timeval prev_t, t; 290 int delta; 291 292 if (poll_handlers == 0) 293 return; 294 295 microuptime(&t); 296 delta = (t.tv_usec - prev_t.tv_usec) + 297 (t.tv_sec - prev_t.tv_sec)*1000000; 298 if (delta * hz < 500000) 299 short_ticks++; 300 else 301 prev_t = t; 302 303 if (pending_polls > 100) { 304 /* 305 * Too much, assume it has stalled (not always true 306 * see comment above). 307 */ 308 stalled++; 309 pending_polls = 0; 310 phase = 0; 311 } 312 313 if (phase <= 2) { 314 if (phase != 0) 315 suspect++; 316 phase = 1; 317 schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 318 phase = 2; 319 } 320 if (pending_polls++ > 0) 321 lost_polls++; 322 } 323 324 /* 325 * ether_poll is called from the idle loop. 326 */ 327 static void 328 ether_poll(int count) 329 { 330 int i; 331 332 mtx_lock(&poll_mtx); 333 334 if (count > poll_each_burst) 335 count = poll_each_burst; 336 337 for (i = 0 ; i < poll_handlers ; i++) 338 pr[i].handler(pr[i].ifp, POLL_ONLY, count); 339 340 mtx_unlock(&poll_mtx); 341 } 342 343 /* 344 * netisr_pollmore is called after other netisr's, possibly scheduling 345 * another NETISR_POLL call, or adapting the burst size for the next cycle. 346 * 347 * It is very bad to fetch large bursts of packets from a single card at once, 348 * because the burst could take a long time to be completely processed, or 349 * could saturate the intermediate queue (ipintrq or similar) leading to 350 * losses or unfairness. To reduce the problem, and also to account better for 351 * time spent in network-related processing, we split the burst in smaller 352 * chunks of fixed size, giving control to the other netisr's between chunks. 353 * This helps in improving the fairness, reducing livelock (because we 354 * emulate more closely the "process to completion" that we have with 355 * fastforwarding) and accounting for the work performed in low level 356 * handling and forwarding. 357 */ 358 359 static struct timeval poll_start_t; 360 361 void 362 netisr_pollmore() 363 { 364 struct timeval t; 365 int kern_load; 366 367 mtx_lock(&poll_mtx); 368 phase = 5; 369 if (residual_burst > 0) { 370 schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 371 mtx_unlock(&poll_mtx); 372 /* will run immediately on return, followed by netisrs */ 373 return; 374 } 375 /* here we can account time spent in netisr's in this tick */ 376 microuptime(&t); 377 kern_load = (t.tv_usec - poll_start_t.tv_usec) + 378 (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ 379 kern_load = (kern_load * hz) / 10000; /* 0..100 */ 380 if (kern_load > (100 - user_frac)) { /* try decrease ticks */ 381 if (poll_burst > 1) 382 poll_burst--; 383 } else { 384 if (poll_burst < poll_burst_max) 385 poll_burst++; 386 } 387 388 pending_polls--; 389 if (pending_polls == 0) /* we are done */ 390 phase = 0; 391 else { 392 /* 393 * Last cycle was long and caused us to miss one or more 394 * hardclock ticks. Restart processing again, but slightly 395 * reduce the burst size to prevent that this happens again. 396 */ 397 poll_burst -= (poll_burst / 8); 398 if (poll_burst < 1) 399 poll_burst = 1; 400 schednetisrbits(1 << NETISR_POLL | 1 << NETISR_POLLMORE); 401 phase = 6; 402 } 403 mtx_unlock(&poll_mtx); 404 } 405 406 /* 407 * netisr_poll is scheduled by schednetisr when appropriate, typically once 408 * per tick. 409 */ 410 static void 411 netisr_poll(void) 412 { 413 int i, cycles; 414 enum poll_cmd arg = POLL_ONLY; 415 416 mtx_lock(&poll_mtx); 417 phase = 3; 418 if (residual_burst == 0) { /* first call in this tick */ 419 microuptime(&poll_start_t); 420 if (++reg_frac_count == reg_frac) { 421 arg = POLL_AND_CHECK_STATUS; 422 reg_frac_count = 0; 423 } 424 425 residual_burst = poll_burst; 426 } 427 cycles = (residual_burst < poll_each_burst) ? 428 residual_burst : poll_each_burst; 429 residual_burst -= cycles; 430 431 for (i = 0 ; i < poll_handlers ; i++) 432 pr[i].handler(pr[i].ifp, arg, cycles); 433 434 phase = 4; 435 mtx_unlock(&poll_mtx); 436 } 437 438 /* 439 * Try to register routine for polling. Returns 0 if successful 440 * (and polling should be enabled), error code otherwise. 441 * A device is not supposed to register itself multiple times. 442 * 443 * This is called from within the *_ioctl() functions. 444 */ 445 int 446 ether_poll_register(poll_handler_t *h, struct ifnet *ifp) 447 { 448 int i; 449 450 KASSERT(h != NULL, ("%s: handler is NULL", __func__)); 451 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 452 453 mtx_lock(&poll_mtx); 454 if (poll_handlers >= POLL_LIST_LEN) { 455 /* 456 * List full, cannot register more entries. 457 * This should never happen; if it does, it is probably a 458 * broken driver trying to register multiple times. Checking 459 * this at runtime is expensive, and won't solve the problem 460 * anyways, so just report a few times and then give up. 461 */ 462 static int verbose = 10 ; 463 if (verbose >0) { 464 log(LOG_ERR, "poll handlers list full, " 465 "maybe a broken driver ?\n"); 466 verbose--; 467 } 468 mtx_unlock(&poll_mtx); 469 return (ENOMEM); /* no polling for you */ 470 } 471 472 for (i = 0 ; i < poll_handlers ; i++) 473 if (pr[i].ifp == ifp && pr[i].handler != NULL) { 474 mtx_unlock(&poll_mtx); 475 log(LOG_DEBUG, "ether_poll_register: %s: handler" 476 " already registered\n", ifp->if_xname); 477 return (EEXIST); 478 } 479 480 pr[poll_handlers].handler = h; 481 pr[poll_handlers].ifp = ifp; 482 poll_handlers++; 483 mtx_unlock(&poll_mtx); 484 if (idlepoll_sleeping) 485 wakeup(&idlepoll_sleeping); 486 return (0); 487 } 488 489 /* 490 * Remove interface from the polling list. Called from *_ioctl(), too. 491 */ 492 int 493 ether_poll_deregister(struct ifnet *ifp) 494 { 495 int i; 496 497 KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 498 499 mtx_lock(&poll_mtx); 500 501 for (i = 0 ; i < poll_handlers ; i++) 502 if (pr[i].ifp == ifp) /* found it */ 503 break; 504 if (i == poll_handlers) { 505 log(LOG_DEBUG, "ether_poll_deregister: %s: not found!\n", 506 ifp->if_xname); 507 mtx_unlock(&poll_mtx); 508 return (ENOENT); 509 } 510 poll_handlers--; 511 if (i < poll_handlers) { /* Last entry replaces this one. */ 512 pr[i].handler = pr[poll_handlers].handler; 513 pr[i].ifp = pr[poll_handlers].ifp; 514 } 515 mtx_unlock(&poll_mtx); 516 return (0); 517 } 518 519 /* 520 * Legacy interface for turning polling on all interfaces at one time. 521 */ 522 static int 523 poll_switch(SYSCTL_HANDLER_ARGS) 524 { 525 INIT_VNET_NET(curvnet); 526 struct ifnet *ifp; 527 int error; 528 int val = polling; 529 530 error = sysctl_handle_int(oidp, &val, 0, req); 531 if (error || !req->newptr ) 532 return (error); 533 534 if (val == polling) 535 return (0); 536 537 if (val < 0 || val > 1) 538 return (EINVAL); 539 540 polling = val; 541 542 IFNET_RLOCK(); 543 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 544 if (ifp->if_capabilities & IFCAP_POLLING) { 545 struct ifreq ifr; 546 547 if (val == 1) 548 ifr.ifr_reqcap = 549 ifp->if_capenable | IFCAP_POLLING; 550 else 551 ifr.ifr_reqcap = 552 ifp->if_capenable & ~IFCAP_POLLING; 553 (void) (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); 554 } 555 } 556 IFNET_RUNLOCK(); 557 558 log(LOG_ERR, "kern.polling.enable is deprecated. Use ifconfig(8)"); 559 560 return (0); 561 } 562 563 static void 564 poll_idle(void) 565 { 566 struct thread *td = curthread; 567 struct rtprio rtp; 568 569 rtp.prio = RTP_PRIO_MAX; /* lowest priority */ 570 rtp.type = RTP_PRIO_IDLE; 571 PROC_SLOCK(td->td_proc); 572 rtp_to_pri(&rtp, td); 573 PROC_SUNLOCK(td->td_proc); 574 575 for (;;) { 576 if (poll_in_idle_loop && poll_handlers > 0) { 577 idlepoll_sleeping = 0; 578 ether_poll(poll_each_burst); 579 thread_lock(td); 580 mi_switch(SW_VOL, NULL); 581 thread_unlock(td); 582 } else { 583 idlepoll_sleeping = 1; 584 tsleep(&idlepoll_sleeping, 0, "pollid", hz * 3); 585 } 586 } 587 } 588 589 static struct proc *idlepoll; 590 static struct kproc_desc idlepoll_kp = { 591 "idlepoll", 592 poll_idle, 593 &idlepoll 594 }; 595 SYSINIT(idlepoll, SI_SUB_KTHREAD_VM, SI_ORDER_ANY, kproc_start, 596 &idlepoll_kp); 597