1 /* $KAME: altq_hfsc.c,v 1.25 2004/04/17 10:54:48 kjc Exp $ */ 2 3 /* 4 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved. 5 * 6 * Permission to use, copy, modify, and distribute this software and 7 * its documentation is hereby granted (including for commercial or 8 * for-profit use), provided that both the copyright notice and this 9 * permission notice appear in all copies of the software, derivative 10 * works, or modified versions, and any portions thereof. 11 * 12 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF 13 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS 14 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 24 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 25 * DAMAGE. 26 * 27 * Carnegie Mellon encourages (but does not require) users of this 28 * software to return any improvements or extensions that they make, 29 * and to grant Carnegie Mellon the rights to redistribute these 30 * changes without encumbrance. 31 */ 32 /* 33 * H-FSC is described in Proceedings of SIGCOMM'97, 34 * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing, 35 * Real-Time and Priority Service" 36 * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng. 37 * 38 * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing. 39 * when a class has an upperlimit, the fit-time is computed from the 40 * upperlimit service curve. the link-sharing scheduler does not schedule 41 * a class whose fit-time exceeds the current time. 42 */ 43 44 #include "opt_altq.h" 45 #include "opt_inet.h" 46 #include "opt_inet6.h" 47 48 #ifdef ALTQ_HFSC /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */ 49 50 #include <sys/param.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <sys/systm.h> 55 #include <sys/errno.h> 56 #include <sys/queue.h> 57 #include <sys/thread.h> 58 59 #include <net/if.h> 60 #include <net/ifq_var.h> 61 #include <netinet/in.h> 62 63 #include <net/pf/pfvar.h> 64 #include <net/altq/altq.h> 65 #include <net/altq/altq_hfsc.h> 66 67 #include <sys/thread2.h> 68 69 /* 70 * function prototypes 71 */ 72 static int hfsc_clear_interface(struct hfsc_if *); 73 static int hfsc_request(struct ifaltq *, int, void *); 74 static void hfsc_purge(struct hfsc_if *); 75 static struct hfsc_class *hfsc_class_create(struct hfsc_if *, 76 struct service_curve *, 77 struct service_curve *, 78 struct service_curve *, 79 struct hfsc_class *, int, int, int); 80 static int hfsc_class_destroy(struct hfsc_class *); 81 static struct hfsc_class *hfsc_nextclass(struct hfsc_class *); 82 static int hfsc_enqueue(struct ifaltq *, struct mbuf *, 83 struct altq_pktattr *); 84 static struct mbuf *hfsc_dequeue(struct ifaltq *, struct mbuf *, int); 85 86 static int hfsc_addq(struct hfsc_class *, struct mbuf *); 87 static struct mbuf *hfsc_getq(struct hfsc_class *); 88 static struct mbuf *hfsc_pollq(struct hfsc_class *); 89 static void hfsc_purgeq(struct hfsc_class *); 90 91 static void update_cfmin(struct hfsc_class *); 92 static void set_active(struct hfsc_class *, int); 93 static void set_passive(struct hfsc_class *); 94 95 static void init_ed(struct hfsc_class *, int); 96 static void update_ed(struct hfsc_class *, int); 97 static void update_d(struct hfsc_class *, int); 98 static void init_vf(struct hfsc_class *, int); 99 static void update_vf(struct hfsc_class *, int, uint64_t); 100 static ellist_t *ellist_alloc(void); 101 static void ellist_destroy(ellist_t *); 102 static void ellist_insert(struct hfsc_class *); 103 static void ellist_remove(struct hfsc_class *); 104 static void ellist_update(struct hfsc_class *); 105 struct hfsc_class *ellist_get_mindl(ellist_t *, uint64_t); 106 static actlist_t *actlist_alloc(void); 107 static void actlist_destroy(actlist_t *); 108 static void actlist_insert(struct hfsc_class *); 109 static void actlist_remove(struct hfsc_class *); 110 static void actlist_update(struct hfsc_class *); 111 112 static struct hfsc_class *actlist_firstfit(struct hfsc_class *, uint64_t); 113 114 static __inline uint64_t seg_x2y(uint64_t, uint64_t); 115 static __inline uint64_t seg_y2x(uint64_t, uint64_t); 116 static __inline uint64_t m2sm(u_int); 117 static __inline uint64_t m2ism(u_int); 118 static __inline uint64_t d2dx(u_int); 119 static u_int sm2m(uint64_t); 120 static u_int dx2d(uint64_t); 121 122 static void sc2isc(struct service_curve *, struct internal_sc *); 123 static void rtsc_init(struct runtime_sc *, struct internal_sc *, 124 uint64_t, uint64_t); 125 static uint64_t rtsc_y2x(struct runtime_sc *, uint64_t); 126 static uint64_t rtsc_x2y(struct runtime_sc *, uint64_t); 127 static void rtsc_min(struct runtime_sc *, struct internal_sc *, 128 uint64_t, uint64_t); 129 130 static void get_class_stats(struct hfsc_classstats *, struct hfsc_class *); 131 static struct hfsc_class *clh_to_clp(struct hfsc_if *, uint32_t); 132 133 /* 134 * macros 135 */ 136 #define is_a_parent_class(cl) ((cl)->cl_children != NULL) 137 138 #define HT_INFINITY 0xffffffffffffffffLL /* infinite time value */ 139 140 int 141 hfsc_pfattach(struct pf_altq *a, struct ifaltq *ifq) 142 { 143 return altq_attach(ifq, ALTQT_HFSC, a->altq_disc, 144 hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL); 145 } 146 147 int 148 hfsc_add_altq(struct pf_altq *a) 149 { 150 struct hfsc_if *hif; 151 struct ifnet *ifp; 152 153 if ((ifp = ifunit(a->ifname)) == NULL) 154 return (EINVAL); 155 if (!ifq_is_ready(&ifp->if_snd)) 156 return (ENODEV); 157 158 hif = kmalloc(sizeof(struct hfsc_if), M_ALTQ, M_WAITOK | M_ZERO); 159 160 hif->hif_eligible = ellist_alloc(); 161 hif->hif_ifq = &ifp->if_snd; 162 ifq_purge(&ifp->if_snd); 163 164 /* keep the state in pf_altq */ 165 a->altq_disc = hif; 166 167 return (0); 168 } 169 170 int 171 hfsc_remove_altq(struct pf_altq *a) 172 { 173 struct hfsc_if *hif; 174 175 if ((hif = a->altq_disc) == NULL) 176 return (EINVAL); 177 a->altq_disc = NULL; 178 179 hfsc_clear_interface(hif); 180 hfsc_class_destroy(hif->hif_rootclass); 181 182 ellist_destroy(hif->hif_eligible); 183 184 kfree(hif, M_ALTQ); 185 186 return (0); 187 } 188 189 static int 190 hfsc_add_queue_locked(struct pf_altq *a, struct hfsc_if *hif) 191 { 192 struct hfsc_class *cl, *parent; 193 struct hfsc_opts *opts; 194 struct service_curve rtsc, lssc, ulsc; 195 196 KKASSERT(a->qid != 0); 197 198 opts = &a->pq_u.hfsc_opts; 199 200 if (a->parent_qid == HFSC_NULLCLASS_HANDLE && hif->hif_rootclass == NULL) 201 parent = NULL; 202 else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL) 203 return (EINVAL); 204 205 if (clh_to_clp(hif, a->qid) != NULL) 206 return (EBUSY); 207 208 rtsc.m1 = opts->rtsc_m1; 209 rtsc.d = opts->rtsc_d; 210 rtsc.m2 = opts->rtsc_m2; 211 lssc.m1 = opts->lssc_m1; 212 lssc.d = opts->lssc_d; 213 lssc.m2 = opts->lssc_m2; 214 ulsc.m1 = opts->ulsc_m1; 215 ulsc.d = opts->ulsc_d; 216 ulsc.m2 = opts->ulsc_m2; 217 218 cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc, parent, a->qlimit, 219 opts->flags, a->qid); 220 if (cl == NULL) 221 return (ENOMEM); 222 223 return (0); 224 } 225 226 int 227 hfsc_add_queue(struct pf_altq *a) 228 { 229 struct hfsc_if *hif; 230 struct ifaltq *ifq; 231 int error; 232 233 if (a->qid == 0) 234 return (EINVAL); 235 236 /* XXX not MP safe */ 237 if ((hif = a->altq_disc) == NULL) 238 return (EINVAL); 239 ifq = hif->hif_ifq; 240 241 ALTQ_LOCK(ifq); 242 error = hfsc_add_queue_locked(a, hif); 243 ALTQ_UNLOCK(ifq); 244 245 return error; 246 } 247 248 static int 249 hfsc_remove_queue_locked(struct pf_altq *a, struct hfsc_if *hif) 250 { 251 struct hfsc_class *cl; 252 253 if ((cl = clh_to_clp(hif, a->qid)) == NULL) 254 return (EINVAL); 255 256 return (hfsc_class_destroy(cl)); 257 } 258 259 int 260 hfsc_remove_queue(struct pf_altq *a) 261 { 262 struct hfsc_if *hif; 263 struct ifaltq *ifq; 264 int error; 265 266 /* XXX not MP safe */ 267 if ((hif = a->altq_disc) == NULL) 268 return (EINVAL); 269 ifq = hif->hif_ifq; 270 271 ALTQ_LOCK(ifq); 272 error = hfsc_remove_queue_locked(a, hif); 273 ALTQ_UNLOCK(ifq); 274 275 return error; 276 } 277 278 int 279 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 280 { 281 struct hfsc_if *hif; 282 struct hfsc_class *cl; 283 struct hfsc_classstats stats; 284 struct ifaltq *ifq; 285 int error = 0; 286 287 if (*nbytes < sizeof(stats)) 288 return (EINVAL); 289 290 /* XXX not MP safe */ 291 if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL) 292 return (EBADF); 293 ifq = hif->hif_ifq; 294 295 ALTQ_LOCK(ifq); 296 297 if ((cl = clh_to_clp(hif, a->qid)) == NULL) { 298 ALTQ_UNLOCK(ifq); 299 return (EINVAL); 300 } 301 302 get_class_stats(&stats, cl); 303 304 ALTQ_UNLOCK(ifq); 305 306 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 307 return (error); 308 *nbytes = sizeof(stats); 309 return (0); 310 } 311 312 /* 313 * bring the interface back to the initial state by discarding 314 * all the filters and classes except the root class. 315 */ 316 static int 317 hfsc_clear_interface(struct hfsc_if *hif) 318 { 319 struct hfsc_class *cl; 320 321 if (hif->hif_rootclass == NULL) 322 return (0); 323 324 325 /* clear out the classes */ 326 while ((cl = hif->hif_rootclass->cl_children) != NULL) { 327 /* 328 * remove the first leaf class found in the hierarchy 329 * then start over 330 */ 331 for (; cl != NULL; cl = hfsc_nextclass(cl)) { 332 if (!is_a_parent_class(cl)) { 333 hfsc_class_destroy(cl); 334 break; 335 } 336 } 337 } 338 339 return (0); 340 } 341 342 static int 343 hfsc_request(struct ifaltq *ifq, int req, void *arg) 344 { 345 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc; 346 347 crit_enter(); 348 switch (req) { 349 case ALTRQ_PURGE: 350 hfsc_purge(hif); 351 break; 352 } 353 crit_exit(); 354 return (0); 355 } 356 357 /* discard all the queued packets on the interface */ 358 static void 359 hfsc_purge(struct hfsc_if *hif) 360 { 361 struct hfsc_class *cl; 362 363 for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl)) { 364 if (!qempty(cl->cl_q)) 365 hfsc_purgeq(cl); 366 } 367 if (ifq_is_enabled(hif->hif_ifq)) 368 hif->hif_ifq->ifq_len = 0; 369 } 370 371 struct hfsc_class * 372 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc, 373 struct service_curve *fsc, struct service_curve *usc, 374 struct hfsc_class *parent, int qlimit, int flags, int qid) 375 { 376 struct hfsc_class *cl, *p; 377 int i; 378 379 if (hif->hif_classes >= HFSC_MAX_CLASSES) 380 return (NULL); 381 382 #ifndef ALTQ_RED 383 if (flags & HFCF_RED) { 384 #ifdef ALTQ_DEBUG 385 kprintf("hfsc_class_create: RED not configured for HFSC!\n"); 386 #endif 387 return (NULL); 388 } 389 #endif 390 391 cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO); 392 cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO); 393 cl->cl_actc = actlist_alloc(); 394 395 if (qlimit == 0) 396 qlimit = 50; /* use default */ 397 qlimit(cl->cl_q) = qlimit; 398 qtype(cl->cl_q) = Q_DROPTAIL; 399 qlen(cl->cl_q) = 0; 400 cl->cl_flags = flags; 401 #ifdef ALTQ_RED 402 if (flags & (HFCF_RED|HFCF_RIO)) { 403 int red_flags, red_pkttime; 404 u_int m2; 405 406 m2 = 0; 407 if (rsc != NULL && rsc->m2 > m2) 408 m2 = rsc->m2; 409 if (fsc != NULL && fsc->m2 > m2) 410 m2 = fsc->m2; 411 if (usc != NULL && usc->m2 > m2) 412 m2 = usc->m2; 413 414 red_flags = 0; 415 if (flags & HFCF_ECN) 416 red_flags |= REDF_ECN; 417 #ifdef ALTQ_RIO 418 if (flags & HFCF_CLEARDSCP) 419 red_flags |= RIOF_CLEARDSCP; 420 #endif 421 if (m2 < 8) 422 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */ 423 else 424 red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu 425 * 1000 * 1000 * 1000 / (m2 / 8); 426 if (flags & HFCF_RED) { 427 cl->cl_red = red_alloc(0, 0, 428 qlimit(cl->cl_q) * 10/100, 429 qlimit(cl->cl_q) * 30/100, 430 red_flags, red_pkttime); 431 if (cl->cl_red != NULL) 432 qtype(cl->cl_q) = Q_RED; 433 } 434 #ifdef ALTQ_RIO 435 else { 436 cl->cl_red = (red_t *)rio_alloc(0, NULL, 437 red_flags, red_pkttime); 438 if (cl->cl_red != NULL) 439 qtype(cl->cl_q) = Q_RIO; 440 } 441 #endif 442 } 443 #endif /* ALTQ_RED */ 444 445 if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) { 446 cl->cl_rsc = kmalloc(sizeof(*cl->cl_rsc), M_ALTQ, M_WAITOK); 447 sc2isc(rsc, cl->cl_rsc); 448 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0); 449 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0); 450 } 451 if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) { 452 cl->cl_fsc = kmalloc(sizeof(*cl->cl_fsc), M_ALTQ, M_WAITOK); 453 sc2isc(fsc, cl->cl_fsc); 454 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0); 455 } 456 if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) { 457 cl->cl_usc = kmalloc(sizeof(*cl->cl_usc), M_ALTQ, M_WAITOK); 458 sc2isc(usc, cl->cl_usc); 459 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0); 460 } 461 462 cl->cl_id = hif->hif_classid++; 463 cl->cl_handle = qid; 464 cl->cl_hif = hif; 465 cl->cl_parent = parent; 466 467 crit_enter(); 468 hif->hif_classes++; 469 470 /* 471 * find a free slot in the class table. if the slot matching 472 * the lower bits of qid is free, use this slot. otherwise, 473 * use the first free slot. 474 */ 475 i = qid % HFSC_MAX_CLASSES; 476 if (hif->hif_class_tbl[i] == NULL) 477 hif->hif_class_tbl[i] = cl; 478 else { 479 for (i = 0; i < HFSC_MAX_CLASSES; i++) { 480 if (hif->hif_class_tbl[i] == NULL) { 481 hif->hif_class_tbl[i] = cl; 482 break; 483 } 484 } 485 if (i == HFSC_MAX_CLASSES) { 486 crit_exit(); 487 goto err_ret; 488 } 489 } 490 491 if (flags & HFCF_DEFAULTCLASS) 492 hif->hif_defaultclass = cl; 493 494 if (parent == NULL) { 495 /* this is root class */ 496 hif->hif_rootclass = cl; 497 } else if (parent->cl_children == NULL) { 498 /* add this class to the children list of the parent */ 499 parent->cl_children = cl; 500 } else { 501 p = parent->cl_children; 502 while (p->cl_siblings != NULL) 503 p = p->cl_siblings; 504 p->cl_siblings = cl; 505 } 506 crit_exit(); 507 508 return (cl); 509 510 err_ret: 511 if (cl->cl_actc != NULL) 512 actlist_destroy(cl->cl_actc); 513 if (cl->cl_red != NULL) { 514 #ifdef ALTQ_RIO 515 if (q_is_rio(cl->cl_q)) 516 rio_destroy((rio_t *)cl->cl_red); 517 #endif 518 #ifdef ALTQ_RED 519 if (q_is_red(cl->cl_q)) 520 red_destroy(cl->cl_red); 521 #endif 522 } 523 if (cl->cl_fsc != NULL) 524 kfree(cl->cl_fsc, M_ALTQ); 525 if (cl->cl_rsc != NULL) 526 kfree(cl->cl_rsc, M_ALTQ); 527 if (cl->cl_usc != NULL) 528 kfree(cl->cl_usc, M_ALTQ); 529 if (cl->cl_q != NULL) 530 kfree(cl->cl_q, M_ALTQ); 531 kfree(cl, M_ALTQ); 532 return (NULL); 533 } 534 535 static int 536 hfsc_class_destroy(struct hfsc_class *cl) 537 { 538 struct hfsc_if *hif; 539 int i; 540 541 if (cl == NULL) 542 return (0); 543 hif = cl->cl_hif; 544 545 if (is_a_parent_class(cl)) 546 return (EBUSY); 547 548 crit_enter(); 549 550 if (!qempty(cl->cl_q)) 551 hfsc_purgeq(cl); 552 553 if (cl->cl_parent == NULL) { 554 /* this is root class */ 555 } else { 556 struct hfsc_class *p = cl->cl_parent->cl_children; 557 558 if (p == cl) { 559 cl->cl_parent->cl_children = cl->cl_siblings; 560 } else { 561 do { 562 if (p->cl_siblings == cl) { 563 p->cl_siblings = cl->cl_siblings; 564 break; 565 } 566 } while ((p = p->cl_siblings) != NULL); 567 } 568 KKASSERT(p != NULL); 569 } 570 571 for (i = 0; i < HFSC_MAX_CLASSES; i++) { 572 if (hif->hif_class_tbl[i] == cl) { 573 hif->hif_class_tbl[i] = NULL; 574 break; 575 } 576 } 577 578 hif->hif_classes--; 579 crit_exit(); 580 581 actlist_destroy(cl->cl_actc); 582 583 if (cl->cl_red != NULL) { 584 #ifdef ALTQ_RIO 585 if (q_is_rio(cl->cl_q)) 586 rio_destroy((rio_t *)cl->cl_red); 587 #endif 588 #ifdef ALTQ_RED 589 if (q_is_red(cl->cl_q)) 590 red_destroy(cl->cl_red); 591 #endif 592 } 593 594 if (cl == hif->hif_rootclass) 595 hif->hif_rootclass = NULL; 596 if (cl == hif->hif_defaultclass) 597 hif->hif_defaultclass = NULL; 598 if (cl == hif->hif_pollcache) 599 hif->hif_pollcache = NULL; 600 601 if (cl->cl_usc != NULL) 602 kfree(cl->cl_usc, M_ALTQ); 603 if (cl->cl_fsc != NULL) 604 kfree(cl->cl_fsc, M_ALTQ); 605 if (cl->cl_rsc != NULL) 606 kfree(cl->cl_rsc, M_ALTQ); 607 kfree(cl->cl_q, M_ALTQ); 608 kfree(cl, M_ALTQ); 609 610 return (0); 611 } 612 613 /* 614 * hfsc_nextclass returns the next class in the tree. 615 * usage: 616 * for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl)) 617 * do_something; 618 */ 619 static struct hfsc_class * 620 hfsc_nextclass(struct hfsc_class *cl) 621 { 622 if (cl->cl_children != NULL) { 623 cl = cl->cl_children; 624 } else if (cl->cl_siblings != NULL) { 625 cl = cl->cl_siblings; 626 } else { 627 while ((cl = cl->cl_parent) != NULL) { 628 if (cl->cl_siblings != NULL) { 629 cl = cl->cl_siblings; 630 break; 631 } 632 } 633 } 634 635 return (cl); 636 } 637 638 /* 639 * hfsc_enqueue is an enqueue function to be registered to 640 * (*altq_enqueue) in struct ifaltq. 641 */ 642 static int 643 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 644 { 645 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc; 646 struct hfsc_class *cl; 647 int len; 648 649 /* grab class set by classifier */ 650 if ((m->m_flags & M_PKTHDR) == 0) { 651 /* should not happen */ 652 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n"); 653 m_freem(m); 654 return (ENOBUFS); 655 } 656 crit_enter(); 657 if (m->m_pkthdr.fw_flags & PF_MBUF_STRUCTURE) 658 cl = clh_to_clp(hif, m->m_pkthdr.pf.qid); 659 else 660 cl = NULL; 661 if (cl == NULL || is_a_parent_class(cl)) { 662 cl = hif->hif_defaultclass; 663 if (cl == NULL) { 664 m_freem(m); 665 crit_exit(); 666 return (ENOBUFS); 667 } 668 } 669 cl->cl_pktattr = NULL; 670 len = m_pktlen(m); 671 if (hfsc_addq(cl, m) != 0) { 672 /* drop occurred. mbuf was freed in hfsc_addq. */ 673 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len); 674 crit_exit(); 675 return (ENOBUFS); 676 } 677 ifq->ifq_len++; 678 cl->cl_hif->hif_packets++; 679 680 /* successfully queued. */ 681 if (qlen(cl->cl_q) == 1) 682 set_active(cl, m_pktlen(m)); 683 crit_exit(); 684 return (0); 685 } 686 687 /* 688 * hfsc_dequeue is a dequeue function to be registered to 689 * (*altq_dequeue) in struct ifaltq. 690 * 691 * note: ALTDQ_POLL returns the next packet without removing the packet 692 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 693 * ALTDQ_REMOVE must return the same packet if called immediately 694 * after ALTDQ_POLL. 695 */ 696 static struct mbuf * 697 hfsc_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op) 698 { 699 struct hfsc_if *hif = (struct hfsc_if *)ifq->altq_disc; 700 struct hfsc_class *cl; 701 struct mbuf *m; 702 int len, next_len; 703 int realtime = 0; 704 uint64_t cur_time; 705 706 if (hif->hif_packets == 0) { 707 /* no packet in the tree */ 708 return (NULL); 709 } 710 711 crit_enter(); 712 cur_time = read_machclk(); 713 714 if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) { 715 cl = hif->hif_pollcache; 716 hif->hif_pollcache = NULL; 717 /* check if the class was scheduled by real-time criteria */ 718 if (cl->cl_rsc != NULL) 719 realtime = (cl->cl_e <= cur_time); 720 } else { 721 /* 722 * if there are eligible classes, use real-time criteria. 723 * find the class with the minimum deadline among 724 * the eligible classes. 725 */ 726 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time)) != NULL) { 727 realtime = 1; 728 } else { 729 #ifdef ALTQ_DEBUG 730 int fits = 0; 731 #endif 732 /* 733 * use link-sharing criteria 734 * get the class with the minimum vt in the hierarchy 735 */ 736 cl = hif->hif_rootclass; 737 while (is_a_parent_class(cl)) { 738 739 cl = actlist_firstfit(cl, cur_time); 740 if (cl == NULL) { 741 #ifdef ALTQ_DEBUG 742 if (fits > 0) 743 kprintf("%d fit but none found\n",fits); 744 #endif 745 m = NULL; 746 goto done; 747 } 748 /* 749 * update parent's cl_cvtmin. 750 * don't update if the new vt is smaller. 751 */ 752 if (cl->cl_parent->cl_cvtmin < cl->cl_vt) 753 cl->cl_parent->cl_cvtmin = cl->cl_vt; 754 #ifdef ALTQ_DEBUG 755 fits++; 756 #endif 757 } 758 } 759 760 if (op == ALTDQ_POLL) { 761 hif->hif_pollcache = cl; 762 m = hfsc_pollq(cl); 763 goto done; 764 } 765 } 766 767 m = hfsc_getq(cl); 768 if (m == NULL) 769 panic("hfsc_dequeue:"); 770 len = m_pktlen(m); 771 cl->cl_hif->hif_packets--; 772 ifq->ifq_len--; 773 PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len); 774 775 update_vf(cl, len, cur_time); 776 if (realtime) 777 cl->cl_cumul += len; 778 779 if (!qempty(cl->cl_q)) { 780 if (cl->cl_rsc != NULL) { 781 /* update ed */ 782 next_len = m_pktlen(qhead(cl->cl_q)); 783 784 if (realtime) 785 update_ed(cl, next_len); 786 else 787 update_d(cl, next_len); 788 } 789 } else { 790 /* the class becomes passive */ 791 set_passive(cl); 792 } 793 done: 794 crit_exit(); 795 KKASSERT(mpolled == NULL || m == mpolled); 796 return (m); 797 } 798 799 static int 800 hfsc_addq(struct hfsc_class *cl, struct mbuf *m) 801 { 802 803 #ifdef ALTQ_RIO 804 if (q_is_rio(cl->cl_q)) 805 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, 806 m, cl->cl_pktattr); 807 #endif 808 #ifdef ALTQ_RED 809 if (q_is_red(cl->cl_q)) 810 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr); 811 #endif 812 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) { 813 m_freem(m); 814 return (-1); 815 } 816 817 if (cl->cl_flags & HFCF_CLEARDSCP) 818 write_dsfield(m, cl->cl_pktattr, 0); 819 820 _addq(cl->cl_q, m); 821 822 return (0); 823 } 824 825 static struct mbuf * 826 hfsc_getq(struct hfsc_class *cl) 827 { 828 #ifdef ALTQ_RIO 829 if (q_is_rio(cl->cl_q)) 830 return rio_getq((rio_t *)cl->cl_red, cl->cl_q); 831 #endif 832 #ifdef ALTQ_RED 833 if (q_is_red(cl->cl_q)) 834 return red_getq(cl->cl_red, cl->cl_q); 835 #endif 836 return _getq(cl->cl_q); 837 } 838 839 static struct mbuf * 840 hfsc_pollq(struct hfsc_class *cl) 841 { 842 return qhead(cl->cl_q); 843 } 844 845 static void 846 hfsc_purgeq(struct hfsc_class *cl) 847 { 848 struct mbuf *m; 849 850 if (qempty(cl->cl_q)) 851 return; 852 853 while ((m = _getq(cl->cl_q)) != NULL) { 854 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m)); 855 m_freem(m); 856 cl->cl_hif->hif_packets--; 857 cl->cl_hif->hif_ifq->ifq_len--; 858 } 859 KKASSERT(qlen(cl->cl_q) == 0); 860 861 update_vf(cl, 0, 0); /* remove cl from the actlist */ 862 set_passive(cl); 863 } 864 865 static void 866 set_active(struct hfsc_class *cl, int len) 867 { 868 if (cl->cl_rsc != NULL) 869 init_ed(cl, len); 870 if (cl->cl_fsc != NULL) 871 init_vf(cl, len); 872 873 cl->cl_stats.period++; 874 } 875 876 static void 877 set_passive(struct hfsc_class *cl) 878 { 879 if (cl->cl_rsc != NULL) 880 ellist_remove(cl); 881 882 /* 883 * actlist is now handled in update_vf() so that update_vf(cl, 0, 0) 884 * needs to be called explicitly to remove a class from actlist 885 */ 886 } 887 888 static void 889 init_ed(struct hfsc_class *cl, int next_len) 890 { 891 uint64_t cur_time; 892 893 cur_time = read_machclk(); 894 895 /* update the deadline curve */ 896 rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul); 897 898 /* 899 * update the eligible curve. 900 * for concave, it is equal to the deadline curve. 901 * for convex, it is a linear curve with slope m2. 902 */ 903 cl->cl_eligible = cl->cl_deadline; 904 if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) { 905 cl->cl_eligible.dx = 0; 906 cl->cl_eligible.dy = 0; 907 } 908 909 /* compute e and d */ 910 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul); 911 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 912 913 ellist_insert(cl); 914 } 915 916 static void 917 update_ed(struct hfsc_class *cl, int next_len) 918 { 919 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul); 920 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 921 922 ellist_update(cl); 923 } 924 925 static void 926 update_d(struct hfsc_class *cl, int next_len) 927 { 928 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 929 } 930 931 static void 932 init_vf(struct hfsc_class *cl, int len) 933 { 934 struct hfsc_class *max_cl, *p; 935 uint64_t vt, f, cur_time; 936 int go_active; 937 938 cur_time = 0; 939 go_active = 1; 940 for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) { 941 if (go_active && cl->cl_nactive++ == 0) 942 go_active = 1; 943 else 944 go_active = 0; 945 946 if (go_active) { 947 max_cl = actlist_last(cl->cl_parent->cl_actc); 948 if (max_cl != NULL) { 949 /* 950 * set vt to the average of the min and max 951 * classes. if the parent's period didn't 952 * change, don't decrease vt of the class. 953 */ 954 vt = max_cl->cl_vt; 955 if (cl->cl_parent->cl_cvtmin != 0) 956 vt = (cl->cl_parent->cl_cvtmin + vt)/2; 957 958 if (cl->cl_parent->cl_vtperiod != 959 cl->cl_parentperiod || vt > cl->cl_vt) 960 cl->cl_vt = vt; 961 } else { 962 /* 963 * first child for a new parent backlog period. 964 * add parent's cvtmax to vtoff of children 965 * to make a new vt (vtoff + vt) larger than 966 * the vt in the last period for all children. 967 */ 968 vt = cl->cl_parent->cl_cvtmax; 969 for (p = cl->cl_parent->cl_children; p != NULL; 970 p = p->cl_siblings) 971 p->cl_vtoff += vt; 972 cl->cl_vt = 0; 973 cl->cl_parent->cl_cvtmax = 0; 974 cl->cl_parent->cl_cvtmin = 0; 975 } 976 cl->cl_initvt = cl->cl_vt; 977 978 /* update the virtual curve */ 979 vt = cl->cl_vt + cl->cl_vtoff; 980 rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total); 981 if (cl->cl_virtual.x == vt) { 982 cl->cl_virtual.x -= cl->cl_vtoff; 983 cl->cl_vtoff = 0; 984 } 985 cl->cl_vtadj = 0; 986 987 cl->cl_vtperiod++; /* increment vt period */ 988 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod; 989 if (cl->cl_parent->cl_nactive == 0) 990 cl->cl_parentperiod++; 991 cl->cl_f = 0; 992 993 actlist_insert(cl); 994 995 if (cl->cl_usc != NULL) { 996 /* class has upper limit curve */ 997 if (cur_time == 0) 998 cur_time = read_machclk(); 999 1000 /* update the ulimit curve */ 1001 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time, 1002 cl->cl_total); 1003 /* compute myf */ 1004 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit, 1005 cl->cl_total); 1006 cl->cl_myfadj = 0; 1007 } 1008 } 1009 1010 if (cl->cl_myf > cl->cl_cfmin) 1011 f = cl->cl_myf; 1012 else 1013 f = cl->cl_cfmin; 1014 if (f != cl->cl_f) { 1015 cl->cl_f = f; 1016 update_cfmin(cl->cl_parent); 1017 } 1018 } 1019 } 1020 1021 static void 1022 update_vf(struct hfsc_class *cl, int len, uint64_t cur_time) 1023 { 1024 uint64_t f, myf_bound, delta; 1025 int go_passive; 1026 1027 go_passive = qempty(cl->cl_q); 1028 1029 for (; cl->cl_parent != NULL; cl = cl->cl_parent) { 1030 cl->cl_total += len; 1031 1032 if (cl->cl_fsc == NULL || cl->cl_nactive == 0) 1033 continue; 1034 1035 if (go_passive && --cl->cl_nactive == 0) 1036 go_passive = 1; 1037 else 1038 go_passive = 0; 1039 1040 if (go_passive) { 1041 /* no more active child, going passive */ 1042 1043 /* update cvtmax of the parent class */ 1044 if (cl->cl_vt > cl->cl_parent->cl_cvtmax) 1045 cl->cl_parent->cl_cvtmax = cl->cl_vt; 1046 1047 /* remove this class from the vt list */ 1048 actlist_remove(cl); 1049 1050 update_cfmin(cl->cl_parent); 1051 1052 continue; 1053 } 1054 1055 /* 1056 * update vt and f 1057 */ 1058 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total) 1059 - cl->cl_vtoff + cl->cl_vtadj; 1060 1061 /* 1062 * if vt of the class is smaller than cvtmin, 1063 * the class was skipped in the past due to non-fit. 1064 * if so, we need to adjust vtadj. 1065 */ 1066 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) { 1067 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt; 1068 cl->cl_vt = cl->cl_parent->cl_cvtmin; 1069 } 1070 1071 /* update the vt list */ 1072 actlist_update(cl); 1073 1074 if (cl->cl_usc != NULL) { 1075 cl->cl_myf = cl->cl_myfadj 1076 + rtsc_y2x(&cl->cl_ulimit, cl->cl_total); 1077 1078 /* 1079 * if myf lags behind by more than one clock tick 1080 * from the current time, adjust myfadj to prevent 1081 * a rate-limited class from going greedy. 1082 * in a steady state under rate-limiting, myf 1083 * fluctuates within one clock tick. 1084 */ 1085 myf_bound = cur_time - machclk_per_tick; 1086 if (cl->cl_myf < myf_bound) { 1087 delta = cur_time - cl->cl_myf; 1088 cl->cl_myfadj += delta; 1089 cl->cl_myf += delta; 1090 } 1091 } 1092 1093 /* cl_f is max(cl_myf, cl_cfmin) */ 1094 if (cl->cl_myf > cl->cl_cfmin) 1095 f = cl->cl_myf; 1096 else 1097 f = cl->cl_cfmin; 1098 if (f != cl->cl_f) { 1099 cl->cl_f = f; 1100 update_cfmin(cl->cl_parent); 1101 } 1102 } 1103 } 1104 1105 static void 1106 update_cfmin(struct hfsc_class *cl) 1107 { 1108 struct hfsc_class *p; 1109 uint64_t cfmin; 1110 1111 if (TAILQ_EMPTY(cl->cl_actc)) { 1112 cl->cl_cfmin = 0; 1113 return; 1114 } 1115 cfmin = HT_INFINITY; 1116 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) { 1117 if (p->cl_f == 0) { 1118 cl->cl_cfmin = 0; 1119 return; 1120 } 1121 if (p->cl_f < cfmin) 1122 cfmin = p->cl_f; 1123 } 1124 cl->cl_cfmin = cfmin; 1125 } 1126 1127 /* 1128 * TAILQ based ellist and actlist implementation 1129 * (ion wanted to make a calendar queue based implementation) 1130 */ 1131 /* 1132 * eligible list holds backlogged classes being sorted by their eligible times. 1133 * there is one eligible list per interface. 1134 */ 1135 1136 static ellist_t * 1137 ellist_alloc(void) 1138 { 1139 ellist_t *head; 1140 1141 head = kmalloc(sizeof(ellist_t *), M_ALTQ, M_WAITOK); 1142 TAILQ_INIT(head); 1143 return (head); 1144 } 1145 1146 static void 1147 ellist_destroy(ellist_t *head) 1148 { 1149 kfree(head, M_ALTQ); 1150 } 1151 1152 static void 1153 ellist_insert(struct hfsc_class *cl) 1154 { 1155 struct hfsc_if *hif = cl->cl_hif; 1156 struct hfsc_class *p; 1157 1158 /* check the last entry first */ 1159 if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL || 1160 p->cl_e <= cl->cl_e) { 1161 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist); 1162 return; 1163 } 1164 1165 TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) { 1166 if (cl->cl_e < p->cl_e) { 1167 TAILQ_INSERT_BEFORE(p, cl, cl_ellist); 1168 return; 1169 } 1170 } 1171 KKASSERT(0); /* should not reach here */ 1172 } 1173 1174 static void 1175 ellist_remove(struct hfsc_class *cl) 1176 { 1177 struct hfsc_if *hif = cl->cl_hif; 1178 1179 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist); 1180 } 1181 1182 static void 1183 ellist_update(struct hfsc_class *cl) 1184 { 1185 struct hfsc_if *hif = cl->cl_hif; 1186 struct hfsc_class *p, *last; 1187 1188 /* 1189 * the eligible time of a class increases monotonically. 1190 * if the next entry has a larger eligible time, nothing to do. 1191 */ 1192 p = TAILQ_NEXT(cl, cl_ellist); 1193 if (p == NULL || cl->cl_e <= p->cl_e) 1194 return; 1195 1196 /* check the last entry */ 1197 last = TAILQ_LAST(hif->hif_eligible, _eligible); 1198 KKASSERT(last != NULL); 1199 if (last->cl_e <= cl->cl_e) { 1200 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist); 1201 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist); 1202 return; 1203 } 1204 1205 /* 1206 * the new position must be between the next entry 1207 * and the last entry 1208 */ 1209 while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) { 1210 if (cl->cl_e < p->cl_e) { 1211 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist); 1212 TAILQ_INSERT_BEFORE(p, cl, cl_ellist); 1213 return; 1214 } 1215 } 1216 KKASSERT(0); /* should not reach here */ 1217 } 1218 1219 /* find the class with the minimum deadline among the eligible classes */ 1220 struct hfsc_class * 1221 ellist_get_mindl(ellist_t *head, uint64_t cur_time) 1222 { 1223 struct hfsc_class *p, *cl = NULL; 1224 1225 TAILQ_FOREACH(p, head, cl_ellist) { 1226 if (p->cl_e > cur_time) 1227 break; 1228 if (cl == NULL || p->cl_d < cl->cl_d) 1229 cl = p; 1230 } 1231 return (cl); 1232 } 1233 1234 /* 1235 * active children list holds backlogged child classes being sorted 1236 * by their virtual time. 1237 * each intermediate class has one active children list. 1238 */ 1239 static actlist_t * 1240 actlist_alloc(void) 1241 { 1242 actlist_t *head; 1243 1244 head = kmalloc(sizeof(*head), M_ALTQ, M_WAITOK); 1245 TAILQ_INIT(head); 1246 return (head); 1247 } 1248 1249 static void 1250 actlist_destroy(actlist_t *head) 1251 { 1252 kfree(head, M_ALTQ); 1253 } 1254 static void 1255 actlist_insert(struct hfsc_class *cl) 1256 { 1257 struct hfsc_class *p; 1258 1259 /* check the last entry first */ 1260 if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL 1261 || p->cl_vt <= cl->cl_vt) { 1262 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist); 1263 return; 1264 } 1265 1266 TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) { 1267 if (cl->cl_vt < p->cl_vt) { 1268 TAILQ_INSERT_BEFORE(p, cl, cl_actlist); 1269 return; 1270 } 1271 } 1272 KKASSERT(0); /* should not reach here */ 1273 } 1274 1275 static void 1276 actlist_remove(struct hfsc_class *cl) 1277 { 1278 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist); 1279 } 1280 1281 static void 1282 actlist_update(struct hfsc_class *cl) 1283 { 1284 struct hfsc_class *p, *last; 1285 1286 /* 1287 * the virtual time of a class increases monotonically during its 1288 * backlogged period. 1289 * if the next entry has a larger virtual time, nothing to do. 1290 */ 1291 p = TAILQ_NEXT(cl, cl_actlist); 1292 if (p == NULL || cl->cl_vt < p->cl_vt) 1293 return; 1294 1295 /* check the last entry */ 1296 last = TAILQ_LAST(cl->cl_parent->cl_actc, _active); 1297 KKASSERT(last != NULL); 1298 if (last->cl_vt <= cl->cl_vt) { 1299 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist); 1300 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist); 1301 return; 1302 } 1303 1304 /* 1305 * the new position must be between the next entry 1306 * and the last entry 1307 */ 1308 while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) { 1309 if (cl->cl_vt < p->cl_vt) { 1310 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist); 1311 TAILQ_INSERT_BEFORE(p, cl, cl_actlist); 1312 return; 1313 } 1314 } 1315 KKASSERT(0); /* should not reach here */ 1316 } 1317 1318 static struct hfsc_class * 1319 actlist_firstfit(struct hfsc_class *cl, uint64_t cur_time) 1320 { 1321 struct hfsc_class *p; 1322 1323 TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) { 1324 if (p->cl_f <= cur_time) 1325 return (p); 1326 } 1327 return (NULL); 1328 } 1329 1330 /* 1331 * service curve support functions 1332 * 1333 * external service curve parameters 1334 * m: bits/sec 1335 * d: msec 1336 * internal service curve parameters 1337 * sm: (bytes/tsc_interval) << SM_SHIFT 1338 * ism: (tsc_count/byte) << ISM_SHIFT 1339 * dx: tsc_count 1340 * 1341 * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits. 1342 * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU 1343 * speed. SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective 1344 * digits in decimal using the following table. 1345 * 1346 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps 1347 * ----------+------------------------------------------------------- 1348 * bytes/nsec 12.5e-6 125e-6 1250e-6 12500e-6 125000e-6 1349 * sm(500MHz) 25.0e-6 250e-6 2500e-6 25000e-6 250000e-6 1350 * sm(200MHz) 62.5e-6 625e-6 6250e-6 62500e-6 625000e-6 1351 * 1352 * nsec/byte 80000 8000 800 80 8 1353 * ism(500MHz) 40000 4000 400 40 4 1354 * ism(200MHz) 16000 1600 160 16 1.6 1355 */ 1356 #define SM_SHIFT 24 1357 #define ISM_SHIFT 10 1358 1359 #define SM_MASK ((1LL << SM_SHIFT) - 1) 1360 #define ISM_MASK ((1LL << ISM_SHIFT) - 1) 1361 1362 static __inline uint64_t 1363 seg_x2y(uint64_t x, uint64_t sm) 1364 { 1365 uint64_t y; 1366 1367 /* 1368 * compute 1369 * y = x * sm >> SM_SHIFT 1370 * but divide it for the upper and lower bits to avoid overflow 1371 */ 1372 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT); 1373 return (y); 1374 } 1375 1376 static __inline uint64_t 1377 seg_y2x(uint64_t y, uint64_t ism) 1378 { 1379 uint64_t x; 1380 1381 if (y == 0) 1382 x = 0; 1383 else if (ism == HT_INFINITY) 1384 x = HT_INFINITY; 1385 else 1386 x = (y >> ISM_SHIFT) * ism + (((y & ISM_MASK) * ism) >> ISM_SHIFT); 1387 1388 return (x); 1389 } 1390 1391 static __inline uint64_t 1392 m2sm(u_int m) 1393 { 1394 uint64_t sm; 1395 1396 sm = ((uint64_t)m << SM_SHIFT) / 8 / machclk_freq; 1397 return (sm); 1398 } 1399 1400 static __inline uint64_t 1401 m2ism(u_int m) 1402 { 1403 uint64_t ism; 1404 1405 if (m == 0) 1406 ism = HT_INFINITY; 1407 else 1408 ism = ((uint64_t)machclk_freq << ISM_SHIFT) * 8 / m; 1409 return (ism); 1410 } 1411 1412 static __inline uint64_t 1413 d2dx(u_int d) 1414 { 1415 uint64_t dx; 1416 1417 dx = ((uint64_t)d * machclk_freq) / 1000; 1418 return (dx); 1419 } 1420 1421 static u_int 1422 sm2m(uint64_t sm) 1423 { 1424 uint64_t m; 1425 1426 m = (sm * 8 * machclk_freq) >> SM_SHIFT; 1427 return ((u_int)m); 1428 } 1429 1430 static u_int 1431 dx2d(uint64_t dx) 1432 { 1433 uint64_t d; 1434 1435 d = dx * 1000 / machclk_freq; 1436 return ((u_int)d); 1437 } 1438 1439 static void 1440 sc2isc(struct service_curve *sc, struct internal_sc *isc) 1441 { 1442 isc->sm1 = m2sm(sc->m1); 1443 isc->ism1 = m2ism(sc->m1); 1444 isc->dx = d2dx(sc->d); 1445 isc->dy = seg_x2y(isc->dx, isc->sm1); 1446 isc->sm2 = m2sm(sc->m2); 1447 isc->ism2 = m2ism(sc->m2); 1448 } 1449 1450 /* 1451 * initialize the runtime service curve with the given internal 1452 * service curve starting at (x, y). 1453 */ 1454 static void 1455 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y) 1456 { 1457 rtsc->x = x; 1458 rtsc->y = y; 1459 rtsc->sm1 = isc->sm1; 1460 rtsc->ism1 = isc->ism1; 1461 rtsc->dx = isc->dx; 1462 rtsc->dy = isc->dy; 1463 rtsc->sm2 = isc->sm2; 1464 rtsc->ism2 = isc->ism2; 1465 } 1466 1467 /* 1468 * calculate the y-projection of the runtime service curve by the 1469 * given x-projection value 1470 */ 1471 static uint64_t 1472 rtsc_y2x(struct runtime_sc *rtsc, uint64_t y) 1473 { 1474 uint64_t x; 1475 1476 if (y < rtsc->y) { 1477 x = rtsc->x; 1478 } else if (y <= rtsc->y + rtsc->dy) { 1479 /* x belongs to the 1st segment */ 1480 if (rtsc->dy == 0) 1481 x = rtsc->x + rtsc->dx; 1482 else 1483 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1); 1484 } else { 1485 /* x belongs to the 2nd segment */ 1486 x = rtsc->x + rtsc->dx 1487 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2); 1488 } 1489 return (x); 1490 } 1491 1492 static uint64_t 1493 rtsc_x2y(struct runtime_sc *rtsc, uint64_t x) 1494 { 1495 uint64_t y; 1496 1497 if (x <= rtsc->x) { 1498 y = rtsc->y; 1499 } else if (x <= rtsc->x + rtsc->dx) { 1500 /* y belongs to the 1st segment */ 1501 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1); 1502 } else 1503 /* y belongs to the 2nd segment */ 1504 y = rtsc->y + rtsc->dy 1505 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2); 1506 return (y); 1507 } 1508 1509 /* 1510 * update the runtime service curve by taking the minimum of the current 1511 * runtime service curve and the service curve starting at (x, y). 1512 */ 1513 static void 1514 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, uint64_t x, uint64_t y) 1515 { 1516 uint64_t y1, y2, dx, dy; 1517 1518 if (isc->sm1 <= isc->sm2) { 1519 /* service curve is convex */ 1520 y1 = rtsc_x2y(rtsc, x); 1521 if (y1 < y) 1522 /* the current rtsc is smaller */ 1523 return; 1524 rtsc->x = x; 1525 rtsc->y = y; 1526 return; 1527 } 1528 1529 /* 1530 * service curve is concave 1531 * compute the two y values of the current rtsc 1532 * y1: at x 1533 * y2: at (x + dx) 1534 */ 1535 y1 = rtsc_x2y(rtsc, x); 1536 if (y1 <= y) { 1537 /* rtsc is below isc, no change to rtsc */ 1538 return; 1539 } 1540 1541 y2 = rtsc_x2y(rtsc, x + isc->dx); 1542 if (y2 >= y + isc->dy) { 1543 /* rtsc is above isc, replace rtsc by isc */ 1544 rtsc->x = x; 1545 rtsc->y = y; 1546 rtsc->dx = isc->dx; 1547 rtsc->dy = isc->dy; 1548 return; 1549 } 1550 1551 /* 1552 * the two curves intersect 1553 * compute the offsets (dx, dy) using the reverse 1554 * function of seg_x2y() 1555 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y) 1556 */ 1557 dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2); 1558 /* 1559 * check if (x, y1) belongs to the 1st segment of rtsc. 1560 * if so, add the offset. 1561 */ 1562 if (rtsc->x + rtsc->dx > x) 1563 dx += rtsc->x + rtsc->dx - x; 1564 dy = seg_x2y(dx, isc->sm1); 1565 1566 rtsc->x = x; 1567 rtsc->y = y; 1568 rtsc->dx = dx; 1569 rtsc->dy = dy; 1570 } 1571 1572 static void 1573 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl) 1574 { 1575 sp->class_id = cl->cl_id; 1576 sp->class_handle = cl->cl_handle; 1577 1578 if (cl->cl_rsc != NULL) { 1579 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1); 1580 sp->rsc.d = dx2d(cl->cl_rsc->dx); 1581 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2); 1582 } else { 1583 sp->rsc.m1 = 0; 1584 sp->rsc.d = 0; 1585 sp->rsc.m2 = 0; 1586 } 1587 if (cl->cl_fsc != NULL) { 1588 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1); 1589 sp->fsc.d = dx2d(cl->cl_fsc->dx); 1590 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2); 1591 } else { 1592 sp->fsc.m1 = 0; 1593 sp->fsc.d = 0; 1594 sp->fsc.m2 = 0; 1595 } 1596 if (cl->cl_usc != NULL) { 1597 sp->usc.m1 = sm2m(cl->cl_usc->sm1); 1598 sp->usc.d = dx2d(cl->cl_usc->dx); 1599 sp->usc.m2 = sm2m(cl->cl_usc->sm2); 1600 } else { 1601 sp->usc.m1 = 0; 1602 sp->usc.d = 0; 1603 sp->usc.m2 = 0; 1604 } 1605 1606 sp->total = cl->cl_total; 1607 sp->cumul = cl->cl_cumul; 1608 1609 sp->d = cl->cl_d; 1610 sp->e = cl->cl_e; 1611 sp->vt = cl->cl_vt; 1612 sp->f = cl->cl_f; 1613 1614 sp->initvt = cl->cl_initvt; 1615 sp->vtperiod = cl->cl_vtperiod; 1616 sp->parentperiod = cl->cl_parentperiod; 1617 sp->nactive = cl->cl_nactive; 1618 sp->vtoff = cl->cl_vtoff; 1619 sp->cvtmax = cl->cl_cvtmax; 1620 sp->myf = cl->cl_myf; 1621 sp->cfmin = cl->cl_cfmin; 1622 sp->cvtmin = cl->cl_cvtmin; 1623 sp->myfadj = cl->cl_myfadj; 1624 sp->vtadj = cl->cl_vtadj; 1625 1626 sp->cur_time = read_machclk(); 1627 sp->machclk_freq = machclk_freq; 1628 1629 sp->qlength = qlen(cl->cl_q); 1630 sp->qlimit = qlimit(cl->cl_q); 1631 sp->xmit_cnt = cl->cl_stats.xmit_cnt; 1632 sp->drop_cnt = cl->cl_stats.drop_cnt; 1633 sp->period = cl->cl_stats.period; 1634 1635 sp->qtype = qtype(cl->cl_q); 1636 #ifdef ALTQ_RED 1637 if (q_is_red(cl->cl_q)) 1638 red_getstats(cl->cl_red, &sp->red[0]); 1639 #endif 1640 #ifdef ALTQ_RIO 1641 if (q_is_rio(cl->cl_q)) 1642 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]); 1643 #endif 1644 } 1645 1646 /* convert a class handle to the corresponding class pointer */ 1647 static struct hfsc_class * 1648 clh_to_clp(struct hfsc_if *hif, uint32_t chandle) 1649 { 1650 int i; 1651 struct hfsc_class *cl; 1652 1653 if (chandle == 0) 1654 return (NULL); 1655 /* 1656 * first, try optimistically the slot matching the lower bits of 1657 * the handle. if it fails, do the linear table search. 1658 */ 1659 i = chandle % HFSC_MAX_CLASSES; 1660 if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle) 1661 return (cl); 1662 for (i = 0; i < HFSC_MAX_CLASSES; i++) 1663 if ((cl = hif->hif_class_tbl[i]) != NULL && 1664 cl->cl_handle == chandle) 1665 return (cl); 1666 return (NULL); 1667 } 1668 1669 #endif /* ALTQ_HFSC */ 1670