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