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