1 /* 2 * Copyright (c) 2003 Patrick McHardy, <kaber@trash.net> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * 2003-10-17 - Ported from altq 10 */ 11 /* 12 * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved. 13 * 14 * Permission to use, copy, modify, and distribute this software and 15 * its documentation is hereby granted (including for commercial or 16 * for-profit use), provided that both the copyright notice and this 17 * permission notice appear in all copies of the software, derivative 18 * works, or modified versions, and any portions thereof. 19 * 20 * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF 21 * WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON PROVIDES THIS 22 * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 33 * DAMAGE. 34 * 35 * Carnegie Mellon encourages (but does not require) users of this 36 * software to return any improvements or extensions that they make, 37 * and to grant Carnegie Mellon the rights to redistribute these 38 * changes without encumbrance. 39 */ 40 /* 41 * H-FSC is described in Proceedings of SIGCOMM'97, 42 * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing, 43 * Real-Time and Priority Service" 44 * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng. 45 * 46 * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing. 47 * when a class has an upperlimit, the fit-time is computed from the 48 * upperlimit service curve. the link-sharing scheduler does not schedule 49 * a class whose fit-time exceeds the current time. 50 */ 51 52 #include <linux/kernel.h> 53 #include <linux/module.h> 54 #include <linux/types.h> 55 #include <linux/errno.h> 56 #include <linux/compiler.h> 57 #include <linux/spinlock.h> 58 #include <linux/skbuff.h> 59 #include <linux/string.h> 60 #include <linux/slab.h> 61 #include <linux/list.h> 62 #include <linux/rbtree.h> 63 #include <linux/init.h> 64 #include <linux/rtnetlink.h> 65 #include <linux/pkt_sched.h> 66 #include <net/netlink.h> 67 #include <net/pkt_sched.h> 68 #include <net/pkt_cls.h> 69 #include <asm/div64.h> 70 71 /* 72 * kernel internal service curve representation: 73 * coordinates are given by 64 bit unsigned integers. 74 * x-axis: unit is clock count. 75 * y-axis: unit is byte. 76 * 77 * The service curve parameters are converted to the internal 78 * representation. The slope values are scaled to avoid overflow. 79 * the inverse slope values as well as the y-projection of the 1st 80 * segment are kept in order to avoid 64-bit divide operations 81 * that are expensive on 32-bit architectures. 82 */ 83 84 struct internal_sc { 85 u64 sm1; /* scaled slope of the 1st segment */ 86 u64 ism1; /* scaled inverse-slope of the 1st segment */ 87 u64 dx; /* the x-projection of the 1st segment */ 88 u64 dy; /* the y-projection of the 1st segment */ 89 u64 sm2; /* scaled slope of the 2nd segment */ 90 u64 ism2; /* scaled inverse-slope of the 2nd segment */ 91 }; 92 93 /* runtime service curve */ 94 struct runtime_sc { 95 u64 x; /* current starting position on x-axis */ 96 u64 y; /* current starting position on y-axis */ 97 u64 sm1; /* scaled slope of the 1st segment */ 98 u64 ism1; /* scaled inverse-slope of the 1st segment */ 99 u64 dx; /* the x-projection of the 1st segment */ 100 u64 dy; /* the y-projection of the 1st segment */ 101 u64 sm2; /* scaled slope of the 2nd segment */ 102 u64 ism2; /* scaled inverse-slope of the 2nd segment */ 103 }; 104 105 enum hfsc_class_flags { 106 HFSC_RSC = 0x1, 107 HFSC_FSC = 0x2, 108 HFSC_USC = 0x4 109 }; 110 111 struct hfsc_class { 112 struct Qdisc_class_common cl_common; 113 114 struct gnet_stats_basic_sync bstats; 115 struct gnet_stats_queue qstats; 116 struct net_rate_estimator __rcu *rate_est; 117 struct tcf_proto __rcu *filter_list; /* filter list */ 118 struct tcf_block *block; 119 unsigned int level; /* class level in hierarchy */ 120 121 struct hfsc_sched *sched; /* scheduler data */ 122 struct hfsc_class *cl_parent; /* parent class */ 123 struct list_head siblings; /* sibling classes */ 124 struct list_head children; /* child classes */ 125 struct Qdisc *qdisc; /* leaf qdisc */ 126 127 struct rb_node el_node; /* qdisc's eligible tree member */ 128 struct rb_root vt_tree; /* active children sorted by cl_vt */ 129 struct rb_node vt_node; /* parent's vt_tree member */ 130 struct rb_root cf_tree; /* active children sorted by cl_f */ 131 struct rb_node cf_node; /* parent's cf_heap member */ 132 133 u64 cl_total; /* total work in bytes */ 134 u64 cl_cumul; /* cumulative work in bytes done by 135 real-time criteria */ 136 137 u64 cl_d; /* deadline*/ 138 u64 cl_e; /* eligible time */ 139 u64 cl_vt; /* virtual time */ 140 u64 cl_f; /* time when this class will fit for 141 link-sharing, max(myf, cfmin) */ 142 u64 cl_myf; /* my fit-time (calculated from this 143 class's own upperlimit curve) */ 144 u64 cl_cfmin; /* earliest children's fit-time (used 145 with cl_myf to obtain cl_f) */ 146 u64 cl_cvtmin; /* minimal virtual time among the 147 children fit for link-sharing 148 (monotonic within a period) */ 149 u64 cl_vtadj; /* intra-period cumulative vt 150 adjustment */ 151 u64 cl_cvtoff; /* largest virtual time seen among 152 the children */ 153 154 struct internal_sc cl_rsc; /* internal real-time service curve */ 155 struct internal_sc cl_fsc; /* internal fair service curve */ 156 struct internal_sc cl_usc; /* internal upperlimit service curve */ 157 struct runtime_sc cl_deadline; /* deadline curve */ 158 struct runtime_sc cl_eligible; /* eligible curve */ 159 struct runtime_sc cl_virtual; /* virtual curve */ 160 struct runtime_sc cl_ulimit; /* upperlimit curve */ 161 162 u8 cl_flags; /* which curves are valid */ 163 u32 cl_vtperiod; /* vt period sequence number */ 164 u32 cl_parentperiod;/* parent's vt period sequence number*/ 165 u32 cl_nactive; /* number of active children */ 166 }; 167 168 struct hfsc_sched { 169 u16 defcls; /* default class id */ 170 struct hfsc_class root; /* root class */ 171 struct Qdisc_class_hash clhash; /* class hash */ 172 struct rb_root eligible; /* eligible tree */ 173 struct qdisc_watchdog watchdog; /* watchdog timer */ 174 }; 175 176 #define HT_INFINITY 0xffffffffffffffffULL /* infinite time value */ 177 178 179 /* 180 * eligible tree holds backlogged classes being sorted by their eligible times. 181 * there is one eligible tree per hfsc instance. 182 */ 183 184 static void 185 eltree_insert(struct hfsc_class *cl) 186 { 187 struct rb_node **p = &cl->sched->eligible.rb_node; 188 struct rb_node *parent = NULL; 189 struct hfsc_class *cl1; 190 191 while (*p != NULL) { 192 parent = *p; 193 cl1 = rb_entry(parent, struct hfsc_class, el_node); 194 if (cl->cl_e >= cl1->cl_e) 195 p = &parent->rb_right; 196 else 197 p = &parent->rb_left; 198 } 199 rb_link_node(&cl->el_node, parent, p); 200 rb_insert_color(&cl->el_node, &cl->sched->eligible); 201 } 202 203 static inline void 204 eltree_remove(struct hfsc_class *cl) 205 { 206 if (!RB_EMPTY_NODE(&cl->el_node)) { 207 rb_erase(&cl->el_node, &cl->sched->eligible); 208 RB_CLEAR_NODE(&cl->el_node); 209 } 210 } 211 212 static inline void 213 eltree_update(struct hfsc_class *cl) 214 { 215 eltree_remove(cl); 216 eltree_insert(cl); 217 } 218 219 /* find the class with the minimum deadline among the eligible classes */ 220 static inline struct hfsc_class * 221 eltree_get_mindl(struct hfsc_sched *q, u64 cur_time) 222 { 223 struct hfsc_class *p, *cl = NULL; 224 struct rb_node *n; 225 226 for (n = rb_first(&q->eligible); n != NULL; n = rb_next(n)) { 227 p = rb_entry(n, struct hfsc_class, el_node); 228 if (p->cl_e > cur_time) 229 break; 230 if (cl == NULL || p->cl_d < cl->cl_d) 231 cl = p; 232 } 233 return cl; 234 } 235 236 /* find the class with minimum eligible time among the eligible classes */ 237 static inline struct hfsc_class * 238 eltree_get_minel(struct hfsc_sched *q) 239 { 240 struct rb_node *n; 241 242 n = rb_first(&q->eligible); 243 if (n == NULL) 244 return NULL; 245 return rb_entry(n, struct hfsc_class, el_node); 246 } 247 248 /* 249 * vttree holds holds backlogged child classes being sorted by their virtual 250 * time. each intermediate class has one vttree. 251 */ 252 static void 253 vttree_insert(struct hfsc_class *cl) 254 { 255 struct rb_node **p = &cl->cl_parent->vt_tree.rb_node; 256 struct rb_node *parent = NULL; 257 struct hfsc_class *cl1; 258 259 while (*p != NULL) { 260 parent = *p; 261 cl1 = rb_entry(parent, struct hfsc_class, vt_node); 262 if (cl->cl_vt >= cl1->cl_vt) 263 p = &parent->rb_right; 264 else 265 p = &parent->rb_left; 266 } 267 rb_link_node(&cl->vt_node, parent, p); 268 rb_insert_color(&cl->vt_node, &cl->cl_parent->vt_tree); 269 } 270 271 static inline void 272 vttree_remove(struct hfsc_class *cl) 273 { 274 rb_erase(&cl->vt_node, &cl->cl_parent->vt_tree); 275 } 276 277 static inline void 278 vttree_update(struct hfsc_class *cl) 279 { 280 vttree_remove(cl); 281 vttree_insert(cl); 282 } 283 284 static inline struct hfsc_class * 285 vttree_firstfit(struct hfsc_class *cl, u64 cur_time) 286 { 287 struct hfsc_class *p; 288 struct rb_node *n; 289 290 for (n = rb_first(&cl->vt_tree); n != NULL; n = rb_next(n)) { 291 p = rb_entry(n, struct hfsc_class, vt_node); 292 if (p->cl_f <= cur_time) 293 return p; 294 } 295 return NULL; 296 } 297 298 /* 299 * get the leaf class with the minimum vt in the hierarchy 300 */ 301 static struct hfsc_class * 302 vttree_get_minvt(struct hfsc_class *cl, u64 cur_time) 303 { 304 /* if root-class's cfmin is bigger than cur_time nothing to do */ 305 if (cl->cl_cfmin > cur_time) 306 return NULL; 307 308 while (cl->level > 0) { 309 cl = vttree_firstfit(cl, cur_time); 310 if (cl == NULL) 311 return NULL; 312 /* 313 * update parent's cl_cvtmin. 314 */ 315 if (cl->cl_parent->cl_cvtmin < cl->cl_vt) 316 cl->cl_parent->cl_cvtmin = cl->cl_vt; 317 } 318 return cl; 319 } 320 321 static void 322 cftree_insert(struct hfsc_class *cl) 323 { 324 struct rb_node **p = &cl->cl_parent->cf_tree.rb_node; 325 struct rb_node *parent = NULL; 326 struct hfsc_class *cl1; 327 328 while (*p != NULL) { 329 parent = *p; 330 cl1 = rb_entry(parent, struct hfsc_class, cf_node); 331 if (cl->cl_f >= cl1->cl_f) 332 p = &parent->rb_right; 333 else 334 p = &parent->rb_left; 335 } 336 rb_link_node(&cl->cf_node, parent, p); 337 rb_insert_color(&cl->cf_node, &cl->cl_parent->cf_tree); 338 } 339 340 static inline void 341 cftree_remove(struct hfsc_class *cl) 342 { 343 rb_erase(&cl->cf_node, &cl->cl_parent->cf_tree); 344 } 345 346 static inline void 347 cftree_update(struct hfsc_class *cl) 348 { 349 cftree_remove(cl); 350 cftree_insert(cl); 351 } 352 353 /* 354 * service curve support functions 355 * 356 * external service curve parameters 357 * m: bps 358 * d: us 359 * internal service curve parameters 360 * sm: (bytes/psched_us) << SM_SHIFT 361 * ism: (psched_us/byte) << ISM_SHIFT 362 * dx: psched_us 363 * 364 * The clock source resolution with ktime and PSCHED_SHIFT 10 is 1.024us. 365 * 366 * sm and ism are scaled in order to keep effective digits. 367 * SM_SHIFT and ISM_SHIFT are selected to keep at least 4 effective 368 * digits in decimal using the following table. 369 * 370 * bits/sec 100Kbps 1Mbps 10Mbps 100Mbps 1Gbps 371 * ------------+------------------------------------------------------- 372 * bytes/1.024us 12.8e-3 128e-3 1280e-3 12800e-3 128000e-3 373 * 374 * 1.024us/byte 78.125 7.8125 0.78125 0.078125 0.0078125 375 * 376 * So, for PSCHED_SHIFT 10 we need: SM_SHIFT 20, ISM_SHIFT 18. 377 */ 378 #define SM_SHIFT (30 - PSCHED_SHIFT) 379 #define ISM_SHIFT (8 + PSCHED_SHIFT) 380 381 #define SM_MASK ((1ULL << SM_SHIFT) - 1) 382 #define ISM_MASK ((1ULL << ISM_SHIFT) - 1) 383 384 static inline u64 385 seg_x2y(u64 x, u64 sm) 386 { 387 u64 y; 388 389 /* 390 * compute 391 * y = x * sm >> SM_SHIFT 392 * but divide it for the upper and lower bits to avoid overflow 393 */ 394 y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT); 395 return y; 396 } 397 398 static inline u64 399 seg_y2x(u64 y, u64 ism) 400 { 401 u64 x; 402 403 if (y == 0) 404 x = 0; 405 else if (ism == HT_INFINITY) 406 x = HT_INFINITY; 407 else { 408 x = (y >> ISM_SHIFT) * ism 409 + (((y & ISM_MASK) * ism) >> ISM_SHIFT); 410 } 411 return x; 412 } 413 414 /* Convert m (bps) into sm (bytes/psched us) */ 415 static u64 416 m2sm(u32 m) 417 { 418 u64 sm; 419 420 sm = ((u64)m << SM_SHIFT); 421 sm += PSCHED_TICKS_PER_SEC - 1; 422 do_div(sm, PSCHED_TICKS_PER_SEC); 423 return sm; 424 } 425 426 /* convert m (bps) into ism (psched us/byte) */ 427 static u64 428 m2ism(u32 m) 429 { 430 u64 ism; 431 432 if (m == 0) 433 ism = HT_INFINITY; 434 else { 435 ism = ((u64)PSCHED_TICKS_PER_SEC << ISM_SHIFT); 436 ism += m - 1; 437 do_div(ism, m); 438 } 439 return ism; 440 } 441 442 /* convert d (us) into dx (psched us) */ 443 static u64 444 d2dx(u32 d) 445 { 446 u64 dx; 447 448 dx = ((u64)d * PSCHED_TICKS_PER_SEC); 449 dx += USEC_PER_SEC - 1; 450 do_div(dx, USEC_PER_SEC); 451 return dx; 452 } 453 454 /* convert sm (bytes/psched us) into m (bps) */ 455 static u32 456 sm2m(u64 sm) 457 { 458 u64 m; 459 460 m = (sm * PSCHED_TICKS_PER_SEC) >> SM_SHIFT; 461 return (u32)m; 462 } 463 464 /* convert dx (psched us) into d (us) */ 465 static u32 466 dx2d(u64 dx) 467 { 468 u64 d; 469 470 d = dx * USEC_PER_SEC; 471 do_div(d, PSCHED_TICKS_PER_SEC); 472 return (u32)d; 473 } 474 475 static void 476 sc2isc(struct tc_service_curve *sc, struct internal_sc *isc) 477 { 478 isc->sm1 = m2sm(sc->m1); 479 isc->ism1 = m2ism(sc->m1); 480 isc->dx = d2dx(sc->d); 481 isc->dy = seg_x2y(isc->dx, isc->sm1); 482 isc->sm2 = m2sm(sc->m2); 483 isc->ism2 = m2ism(sc->m2); 484 } 485 486 /* 487 * initialize the runtime service curve with the given internal 488 * service curve starting at (x, y). 489 */ 490 static void 491 rtsc_init(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y) 492 { 493 rtsc->x = x; 494 rtsc->y = y; 495 rtsc->sm1 = isc->sm1; 496 rtsc->ism1 = isc->ism1; 497 rtsc->dx = isc->dx; 498 rtsc->dy = isc->dy; 499 rtsc->sm2 = isc->sm2; 500 rtsc->ism2 = isc->ism2; 501 } 502 503 /* 504 * calculate the y-projection of the runtime service curve by the 505 * given x-projection value 506 */ 507 static u64 508 rtsc_y2x(struct runtime_sc *rtsc, u64 y) 509 { 510 u64 x; 511 512 if (y < rtsc->y) 513 x = rtsc->x; 514 else if (y <= rtsc->y + rtsc->dy) { 515 /* x belongs to the 1st segment */ 516 if (rtsc->dy == 0) 517 x = rtsc->x + rtsc->dx; 518 else 519 x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1); 520 } else { 521 /* x belongs to the 2nd segment */ 522 x = rtsc->x + rtsc->dx 523 + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2); 524 } 525 return x; 526 } 527 528 static u64 529 rtsc_x2y(struct runtime_sc *rtsc, u64 x) 530 { 531 u64 y; 532 533 if (x <= rtsc->x) 534 y = rtsc->y; 535 else if (x <= rtsc->x + rtsc->dx) 536 /* y belongs to the 1st segment */ 537 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1); 538 else 539 /* y belongs to the 2nd segment */ 540 y = rtsc->y + rtsc->dy 541 + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2); 542 return y; 543 } 544 545 /* 546 * update the runtime service curve by taking the minimum of the current 547 * runtime service curve and the service curve starting at (x, y). 548 */ 549 static void 550 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u64 x, u64 y) 551 { 552 u64 y1, y2, dx, dy; 553 u32 dsm; 554 555 if (isc->sm1 <= isc->sm2) { 556 /* service curve is convex */ 557 y1 = rtsc_x2y(rtsc, x); 558 if (y1 < y) 559 /* the current rtsc is smaller */ 560 return; 561 rtsc->x = x; 562 rtsc->y = y; 563 return; 564 } 565 566 /* 567 * service curve is concave 568 * compute the two y values of the current rtsc 569 * y1: at x 570 * y2: at (x + dx) 571 */ 572 y1 = rtsc_x2y(rtsc, x); 573 if (y1 <= y) { 574 /* rtsc is below isc, no change to rtsc */ 575 return; 576 } 577 578 y2 = rtsc_x2y(rtsc, x + isc->dx); 579 if (y2 >= y + isc->dy) { 580 /* rtsc is above isc, replace rtsc by isc */ 581 rtsc->x = x; 582 rtsc->y = y; 583 rtsc->dx = isc->dx; 584 rtsc->dy = isc->dy; 585 return; 586 } 587 588 /* 589 * the two curves intersect 590 * compute the offsets (dx, dy) using the reverse 591 * function of seg_x2y() 592 * seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y) 593 */ 594 dx = (y1 - y) << SM_SHIFT; 595 dsm = isc->sm1 - isc->sm2; 596 do_div(dx, dsm); 597 /* 598 * check if (x, y1) belongs to the 1st segment of rtsc. 599 * if so, add the offset. 600 */ 601 if (rtsc->x + rtsc->dx > x) 602 dx += rtsc->x + rtsc->dx - x; 603 dy = seg_x2y(dx, isc->sm1); 604 605 rtsc->x = x; 606 rtsc->y = y; 607 rtsc->dx = dx; 608 rtsc->dy = dy; 609 } 610 611 static void 612 init_ed(struct hfsc_class *cl, unsigned int next_len) 613 { 614 u64 cur_time = psched_get_time(); 615 616 /* update the deadline curve */ 617 rtsc_min(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul); 618 619 /* 620 * update the eligible curve. 621 * for concave, it is equal to the deadline curve. 622 * for convex, it is a linear curve with slope m2. 623 */ 624 cl->cl_eligible = cl->cl_deadline; 625 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) { 626 cl->cl_eligible.dx = 0; 627 cl->cl_eligible.dy = 0; 628 } 629 630 /* compute e and d */ 631 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul); 632 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 633 634 eltree_insert(cl); 635 } 636 637 static void 638 update_ed(struct hfsc_class *cl, unsigned int next_len) 639 { 640 cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul); 641 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 642 643 eltree_update(cl); 644 } 645 646 static inline void 647 update_d(struct hfsc_class *cl, unsigned int next_len) 648 { 649 cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len); 650 } 651 652 static inline void 653 update_cfmin(struct hfsc_class *cl) 654 { 655 struct rb_node *n = rb_first(&cl->cf_tree); 656 struct hfsc_class *p; 657 658 if (n == NULL) { 659 cl->cl_cfmin = 0; 660 return; 661 } 662 p = rb_entry(n, struct hfsc_class, cf_node); 663 cl->cl_cfmin = p->cl_f; 664 } 665 666 static void 667 init_vf(struct hfsc_class *cl, unsigned int len) 668 { 669 struct hfsc_class *max_cl; 670 struct rb_node *n; 671 u64 vt, f, cur_time; 672 int go_active; 673 674 cur_time = 0; 675 go_active = 1; 676 for (; cl->cl_parent != NULL; cl = cl->cl_parent) { 677 if (go_active && cl->cl_nactive++ == 0) 678 go_active = 1; 679 else 680 go_active = 0; 681 682 if (go_active) { 683 n = rb_last(&cl->cl_parent->vt_tree); 684 if (n != NULL) { 685 max_cl = rb_entry(n, struct hfsc_class, vt_node); 686 /* 687 * set vt to the average of the min and max 688 * classes. if the parent's period didn't 689 * change, don't decrease vt of the class. 690 */ 691 vt = max_cl->cl_vt; 692 if (cl->cl_parent->cl_cvtmin != 0) 693 vt = (cl->cl_parent->cl_cvtmin + vt)/2; 694 695 if (cl->cl_parent->cl_vtperiod != 696 cl->cl_parentperiod || vt > cl->cl_vt) 697 cl->cl_vt = vt; 698 } else { 699 /* 700 * first child for a new parent backlog period. 701 * initialize cl_vt to the highest value seen 702 * among the siblings. this is analogous to 703 * what cur_time would provide in realtime case. 704 */ 705 cl->cl_vt = cl->cl_parent->cl_cvtoff; 706 cl->cl_parent->cl_cvtmin = 0; 707 } 708 709 /* update the virtual curve */ 710 rtsc_min(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total); 711 cl->cl_vtadj = 0; 712 713 cl->cl_vtperiod++; /* increment vt period */ 714 cl->cl_parentperiod = cl->cl_parent->cl_vtperiod; 715 if (cl->cl_parent->cl_nactive == 0) 716 cl->cl_parentperiod++; 717 cl->cl_f = 0; 718 719 vttree_insert(cl); 720 cftree_insert(cl); 721 722 if (cl->cl_flags & HFSC_USC) { 723 /* class has upper limit curve */ 724 if (cur_time == 0) 725 cur_time = psched_get_time(); 726 727 /* update the ulimit curve */ 728 rtsc_min(&cl->cl_ulimit, &cl->cl_usc, cur_time, 729 cl->cl_total); 730 /* compute myf */ 731 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit, 732 cl->cl_total); 733 } 734 } 735 736 f = max(cl->cl_myf, cl->cl_cfmin); 737 if (f != cl->cl_f) { 738 cl->cl_f = f; 739 cftree_update(cl); 740 } 741 update_cfmin(cl->cl_parent); 742 } 743 } 744 745 static void 746 update_vf(struct hfsc_class *cl, unsigned int len, u64 cur_time) 747 { 748 u64 f; /* , myf_bound, delta; */ 749 int go_passive = 0; 750 751 if (cl->qdisc->q.qlen == 0 && cl->cl_flags & HFSC_FSC) 752 go_passive = 1; 753 754 for (; cl->cl_parent != NULL; cl = cl->cl_parent) { 755 cl->cl_total += len; 756 757 if (!(cl->cl_flags & HFSC_FSC) || cl->cl_nactive == 0) 758 continue; 759 760 if (go_passive && --cl->cl_nactive == 0) 761 go_passive = 1; 762 else 763 go_passive = 0; 764 765 /* update vt */ 766 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total) + cl->cl_vtadj; 767 768 /* 769 * if vt of the class is smaller than cvtmin, 770 * the class was skipped in the past due to non-fit. 771 * if so, we need to adjust vtadj. 772 */ 773 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) { 774 cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt; 775 cl->cl_vt = cl->cl_parent->cl_cvtmin; 776 } 777 778 if (go_passive) { 779 /* no more active child, going passive */ 780 781 /* update cvtoff of the parent class */ 782 if (cl->cl_vt > cl->cl_parent->cl_cvtoff) 783 cl->cl_parent->cl_cvtoff = cl->cl_vt; 784 785 /* remove this class from the vt tree */ 786 vttree_remove(cl); 787 788 cftree_remove(cl); 789 update_cfmin(cl->cl_parent); 790 791 continue; 792 } 793 794 /* update the vt tree */ 795 vttree_update(cl); 796 797 /* update f */ 798 if (cl->cl_flags & HFSC_USC) { 799 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit, cl->cl_total); 800 #if 0 801 cl->cl_myf = cl->cl_myfadj + rtsc_y2x(&cl->cl_ulimit, 802 cl->cl_total); 803 /* 804 * This code causes classes to stay way under their 805 * limit when multiple classes are used at gigabit 806 * speed. needs investigation. -kaber 807 */ 808 /* 809 * if myf lags behind by more than one clock tick 810 * from the current time, adjust myfadj to prevent 811 * a rate-limited class from going greedy. 812 * in a steady state under rate-limiting, myf 813 * fluctuates within one clock tick. 814 */ 815 myf_bound = cur_time - PSCHED_JIFFIE2US(1); 816 if (cl->cl_myf < myf_bound) { 817 delta = cur_time - cl->cl_myf; 818 cl->cl_myfadj += delta; 819 cl->cl_myf += delta; 820 } 821 #endif 822 } 823 824 f = max(cl->cl_myf, cl->cl_cfmin); 825 if (f != cl->cl_f) { 826 cl->cl_f = f; 827 cftree_update(cl); 828 update_cfmin(cl->cl_parent); 829 } 830 } 831 } 832 833 static unsigned int 834 qdisc_peek_len(struct Qdisc *sch) 835 { 836 struct sk_buff *skb; 837 unsigned int len; 838 839 skb = sch->ops->peek(sch); 840 if (unlikely(skb == NULL)) { 841 qdisc_warn_nonwc("qdisc_peek_len", sch); 842 return 0; 843 } 844 len = qdisc_pkt_len(skb); 845 846 return len; 847 } 848 849 static void 850 hfsc_adjust_levels(struct hfsc_class *cl) 851 { 852 struct hfsc_class *p; 853 unsigned int level; 854 855 do { 856 level = 0; 857 list_for_each_entry(p, &cl->children, siblings) { 858 if (p->level >= level) 859 level = p->level + 1; 860 } 861 cl->level = level; 862 } while ((cl = cl->cl_parent) != NULL); 863 } 864 865 static inline struct hfsc_class * 866 hfsc_find_class(u32 classid, struct Qdisc *sch) 867 { 868 struct hfsc_sched *q = qdisc_priv(sch); 869 struct Qdisc_class_common *clc; 870 871 clc = qdisc_class_find(&q->clhash, classid); 872 if (clc == NULL) 873 return NULL; 874 return container_of(clc, struct hfsc_class, cl_common); 875 } 876 877 static void 878 hfsc_change_rsc(struct hfsc_class *cl, struct tc_service_curve *rsc, 879 u64 cur_time) 880 { 881 sc2isc(rsc, &cl->cl_rsc); 882 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, cur_time, cl->cl_cumul); 883 cl->cl_eligible = cl->cl_deadline; 884 if (cl->cl_rsc.sm1 <= cl->cl_rsc.sm2) { 885 cl->cl_eligible.dx = 0; 886 cl->cl_eligible.dy = 0; 887 } 888 cl->cl_flags |= HFSC_RSC; 889 } 890 891 static void 892 hfsc_change_fsc(struct hfsc_class *cl, struct tc_service_curve *fsc) 893 { 894 sc2isc(fsc, &cl->cl_fsc); 895 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total); 896 cl->cl_flags |= HFSC_FSC; 897 } 898 899 static void 900 hfsc_change_usc(struct hfsc_class *cl, struct tc_service_curve *usc, 901 u64 cur_time) 902 { 903 sc2isc(usc, &cl->cl_usc); 904 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, cur_time, cl->cl_total); 905 cl->cl_flags |= HFSC_USC; 906 } 907 908 static void 909 hfsc_upgrade_rt(struct hfsc_class *cl) 910 { 911 cl->cl_fsc = cl->cl_rsc; 912 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, cl->cl_vt, cl->cl_total); 913 cl->cl_flags |= HFSC_FSC; 914 } 915 916 static const struct nla_policy hfsc_policy[TCA_HFSC_MAX + 1] = { 917 [TCA_HFSC_RSC] = { .len = sizeof(struct tc_service_curve) }, 918 [TCA_HFSC_FSC] = { .len = sizeof(struct tc_service_curve) }, 919 [TCA_HFSC_USC] = { .len = sizeof(struct tc_service_curve) }, 920 }; 921 922 static int 923 hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid, 924 struct nlattr **tca, unsigned long *arg, 925 struct netlink_ext_ack *extack) 926 { 927 struct hfsc_sched *q = qdisc_priv(sch); 928 struct hfsc_class *cl = (struct hfsc_class *)*arg; 929 struct hfsc_class *parent = NULL; 930 struct nlattr *opt = tca[TCA_OPTIONS]; 931 struct nlattr *tb[TCA_HFSC_MAX + 1]; 932 struct tc_service_curve *rsc = NULL, *fsc = NULL, *usc = NULL; 933 u64 cur_time; 934 int err; 935 936 if (opt == NULL) 937 return -EINVAL; 938 939 err = nla_parse_nested_deprecated(tb, TCA_HFSC_MAX, opt, hfsc_policy, 940 NULL); 941 if (err < 0) 942 return err; 943 944 if (tb[TCA_HFSC_RSC]) { 945 rsc = nla_data(tb[TCA_HFSC_RSC]); 946 if (rsc->m1 == 0 && rsc->m2 == 0) 947 rsc = NULL; 948 } 949 950 if (tb[TCA_HFSC_FSC]) { 951 fsc = nla_data(tb[TCA_HFSC_FSC]); 952 if (fsc->m1 == 0 && fsc->m2 == 0) 953 fsc = NULL; 954 } 955 956 if (tb[TCA_HFSC_USC]) { 957 usc = nla_data(tb[TCA_HFSC_USC]); 958 if (usc->m1 == 0 && usc->m2 == 0) 959 usc = NULL; 960 } 961 962 if (cl != NULL) { 963 int old_flags; 964 int len = 0; 965 966 if (parentid) { 967 if (cl->cl_parent && 968 cl->cl_parent->cl_common.classid != parentid) 969 return -EINVAL; 970 if (cl->cl_parent == NULL && parentid != TC_H_ROOT) 971 return -EINVAL; 972 } 973 cur_time = psched_get_time(); 974 975 if (tca[TCA_RATE]) { 976 err = gen_replace_estimator(&cl->bstats, NULL, 977 &cl->rate_est, 978 NULL, 979 true, 980 tca[TCA_RATE]); 981 if (err) 982 return err; 983 } 984 985 sch_tree_lock(sch); 986 old_flags = cl->cl_flags; 987 988 if (rsc != NULL) 989 hfsc_change_rsc(cl, rsc, cur_time); 990 if (fsc != NULL) 991 hfsc_change_fsc(cl, fsc); 992 if (usc != NULL) 993 hfsc_change_usc(cl, usc, cur_time); 994 995 if (cl->qdisc->q.qlen != 0) 996 len = qdisc_peek_len(cl->qdisc); 997 /* Check queue length again since some qdisc implementations 998 * (e.g., netem/codel) might empty the queue during the peek 999 * operation. 1000 */ 1001 if (cl->qdisc->q.qlen != 0) { 1002 if (cl->cl_flags & HFSC_RSC) { 1003 if (old_flags & HFSC_RSC) 1004 update_ed(cl, len); 1005 else 1006 init_ed(cl, len); 1007 } 1008 1009 if (cl->cl_flags & HFSC_FSC) { 1010 if (old_flags & HFSC_FSC) 1011 update_vf(cl, 0, cur_time); 1012 else 1013 init_vf(cl, len); 1014 } 1015 } 1016 sch_tree_unlock(sch); 1017 1018 return 0; 1019 } 1020 1021 if (parentid == TC_H_ROOT) 1022 return -EEXIST; 1023 1024 parent = &q->root; 1025 if (parentid) { 1026 parent = hfsc_find_class(parentid, sch); 1027 if (parent == NULL) 1028 return -ENOENT; 1029 } 1030 1031 if (classid == 0 || TC_H_MAJ(classid ^ sch->handle) != 0) 1032 return -EINVAL; 1033 if (hfsc_find_class(classid, sch)) 1034 return -EEXIST; 1035 1036 if (rsc == NULL && fsc == NULL) 1037 return -EINVAL; 1038 1039 cl = kzalloc(sizeof(struct hfsc_class), GFP_KERNEL); 1040 if (cl == NULL) 1041 return -ENOBUFS; 1042 1043 err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack); 1044 if (err) { 1045 kfree(cl); 1046 return err; 1047 } 1048 1049 if (tca[TCA_RATE]) { 1050 err = gen_new_estimator(&cl->bstats, NULL, &cl->rate_est, 1051 NULL, true, tca[TCA_RATE]); 1052 if (err) { 1053 tcf_block_put(cl->block); 1054 kfree(cl); 1055 return err; 1056 } 1057 } 1058 1059 if (rsc != NULL) 1060 hfsc_change_rsc(cl, rsc, 0); 1061 if (fsc != NULL) 1062 hfsc_change_fsc(cl, fsc); 1063 if (usc != NULL) 1064 hfsc_change_usc(cl, usc, 0); 1065 1066 cl->cl_common.classid = classid; 1067 cl->sched = q; 1068 cl->cl_parent = parent; 1069 cl->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 1070 classid, NULL); 1071 if (cl->qdisc == NULL) 1072 cl->qdisc = &noop_qdisc; 1073 else 1074 qdisc_hash_add(cl->qdisc, true); 1075 INIT_LIST_HEAD(&cl->children); 1076 cl->vt_tree = RB_ROOT; 1077 cl->cf_tree = RB_ROOT; 1078 1079 sch_tree_lock(sch); 1080 /* Check if the inner class is a misconfigured 'rt' */ 1081 if (!(parent->cl_flags & HFSC_FSC) && parent != &q->root) { 1082 NL_SET_ERR_MSG(extack, 1083 "Forced curve change on parent 'rt' to 'sc'"); 1084 hfsc_upgrade_rt(parent); 1085 } 1086 qdisc_class_hash_insert(&q->clhash, &cl->cl_common); 1087 list_add_tail(&cl->siblings, &parent->children); 1088 if (parent->level == 0) 1089 qdisc_purge_queue(parent->qdisc); 1090 hfsc_adjust_levels(parent); 1091 sch_tree_unlock(sch); 1092 1093 qdisc_class_hash_grow(sch, &q->clhash); 1094 1095 *arg = (unsigned long)cl; 1096 return 0; 1097 } 1098 1099 static void 1100 hfsc_destroy_class(struct Qdisc *sch, struct hfsc_class *cl) 1101 { 1102 struct hfsc_sched *q = qdisc_priv(sch); 1103 1104 tcf_block_put(cl->block); 1105 qdisc_put(cl->qdisc); 1106 gen_kill_estimator(&cl->rate_est); 1107 if (cl != &q->root) 1108 kfree(cl); 1109 } 1110 1111 static int 1112 hfsc_delete_class(struct Qdisc *sch, unsigned long arg, 1113 struct netlink_ext_ack *extack) 1114 { 1115 struct hfsc_sched *q = qdisc_priv(sch); 1116 struct hfsc_class *cl = (struct hfsc_class *)arg; 1117 1118 if (cl->level > 0 || qdisc_class_in_use(&cl->cl_common) || 1119 cl == &q->root) { 1120 NL_SET_ERR_MSG(extack, "HFSC class in use"); 1121 return -EBUSY; 1122 } 1123 1124 sch_tree_lock(sch); 1125 1126 list_del(&cl->siblings); 1127 hfsc_adjust_levels(cl->cl_parent); 1128 1129 qdisc_purge_queue(cl->qdisc); 1130 qdisc_class_hash_remove(&q->clhash, &cl->cl_common); 1131 1132 sch_tree_unlock(sch); 1133 1134 hfsc_destroy_class(sch, cl); 1135 return 0; 1136 } 1137 1138 static struct hfsc_class * 1139 hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) 1140 { 1141 struct hfsc_sched *q = qdisc_priv(sch); 1142 struct hfsc_class *head, *cl; 1143 struct tcf_result res; 1144 struct tcf_proto *tcf; 1145 int result; 1146 1147 if (TC_H_MAJ(skb->priority ^ sch->handle) == 0 && 1148 (cl = hfsc_find_class(skb->priority, sch)) != NULL) 1149 if (cl->level == 0) 1150 return cl; 1151 1152 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; 1153 head = &q->root; 1154 tcf = rcu_dereference_bh(q->root.filter_list); 1155 while (tcf && (result = tcf_classify(skb, NULL, tcf, &res, false)) >= 0) { 1156 #ifdef CONFIG_NET_CLS_ACT 1157 switch (result) { 1158 case TC_ACT_QUEUED: 1159 case TC_ACT_STOLEN: 1160 case TC_ACT_TRAP: 1161 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; 1162 fallthrough; 1163 case TC_ACT_SHOT: 1164 return NULL; 1165 } 1166 #endif 1167 cl = (struct hfsc_class *)res.class; 1168 if (!cl) { 1169 cl = hfsc_find_class(res.classid, sch); 1170 if (!cl) 1171 break; /* filter selected invalid classid */ 1172 if (cl->level >= head->level) 1173 break; /* filter may only point downwards */ 1174 } 1175 1176 if (cl->level == 0) 1177 return cl; /* hit leaf class */ 1178 1179 /* apply inner filter chain */ 1180 tcf = rcu_dereference_bh(cl->filter_list); 1181 head = cl; 1182 } 1183 1184 /* classification failed, try default class */ 1185 cl = hfsc_find_class(TC_H_MAKE(TC_H_MAJ(sch->handle), 1186 READ_ONCE(q->defcls)), sch); 1187 if (cl == NULL || cl->level > 0) 1188 return NULL; 1189 1190 return cl; 1191 } 1192 1193 static int 1194 hfsc_graft_class(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 1195 struct Qdisc **old, struct netlink_ext_ack *extack) 1196 { 1197 struct hfsc_class *cl = (struct hfsc_class *)arg; 1198 1199 if (cl->level > 0) 1200 return -EINVAL; 1201 if (new == NULL) { 1202 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 1203 cl->cl_common.classid, NULL); 1204 if (new == NULL) 1205 new = &noop_qdisc; 1206 } 1207 1208 *old = qdisc_replace(sch, new, &cl->qdisc); 1209 return 0; 1210 } 1211 1212 static struct Qdisc * 1213 hfsc_class_leaf(struct Qdisc *sch, unsigned long arg) 1214 { 1215 struct hfsc_class *cl = (struct hfsc_class *)arg; 1216 1217 if (cl->level == 0) 1218 return cl->qdisc; 1219 1220 return NULL; 1221 } 1222 1223 static void 1224 hfsc_qlen_notify(struct Qdisc *sch, unsigned long arg) 1225 { 1226 struct hfsc_class *cl = (struct hfsc_class *)arg; 1227 1228 /* vttree is now handled in update_vf() so that update_vf(cl, 0, 0) 1229 * needs to be called explicitly to remove a class from vttree. 1230 */ 1231 if (cl->cl_nactive) 1232 update_vf(cl, 0, 0); 1233 if (cl->cl_flags & HFSC_RSC) 1234 eltree_remove(cl); 1235 } 1236 1237 static unsigned long 1238 hfsc_search_class(struct Qdisc *sch, u32 classid) 1239 { 1240 return (unsigned long)hfsc_find_class(classid, sch); 1241 } 1242 1243 static unsigned long 1244 hfsc_bind_tcf(struct Qdisc *sch, unsigned long parent, u32 classid) 1245 { 1246 struct hfsc_class *p = (struct hfsc_class *)parent; 1247 struct hfsc_class *cl = hfsc_find_class(classid, sch); 1248 1249 if (cl != NULL) { 1250 if (p != NULL && p->level <= cl->level) 1251 return 0; 1252 qdisc_class_get(&cl->cl_common); 1253 } 1254 1255 return (unsigned long)cl; 1256 } 1257 1258 static void 1259 hfsc_unbind_tcf(struct Qdisc *sch, unsigned long arg) 1260 { 1261 struct hfsc_class *cl = (struct hfsc_class *)arg; 1262 1263 qdisc_class_put(&cl->cl_common); 1264 } 1265 1266 static struct tcf_block *hfsc_tcf_block(struct Qdisc *sch, unsigned long arg, 1267 struct netlink_ext_ack *extack) 1268 { 1269 struct hfsc_sched *q = qdisc_priv(sch); 1270 struct hfsc_class *cl = (struct hfsc_class *)arg; 1271 1272 if (cl == NULL) 1273 cl = &q->root; 1274 1275 return cl->block; 1276 } 1277 1278 static int 1279 hfsc_dump_sc(struct sk_buff *skb, int attr, struct internal_sc *sc) 1280 { 1281 struct tc_service_curve tsc; 1282 1283 tsc.m1 = sm2m(sc->sm1); 1284 tsc.d = dx2d(sc->dx); 1285 tsc.m2 = sm2m(sc->sm2); 1286 if (nla_put(skb, attr, sizeof(tsc), &tsc)) 1287 goto nla_put_failure; 1288 1289 return skb->len; 1290 1291 nla_put_failure: 1292 return -1; 1293 } 1294 1295 static int 1296 hfsc_dump_curves(struct sk_buff *skb, struct hfsc_class *cl) 1297 { 1298 if ((cl->cl_flags & HFSC_RSC) && 1299 (hfsc_dump_sc(skb, TCA_HFSC_RSC, &cl->cl_rsc) < 0)) 1300 goto nla_put_failure; 1301 1302 if ((cl->cl_flags & HFSC_FSC) && 1303 (hfsc_dump_sc(skb, TCA_HFSC_FSC, &cl->cl_fsc) < 0)) 1304 goto nla_put_failure; 1305 1306 if ((cl->cl_flags & HFSC_USC) && 1307 (hfsc_dump_sc(skb, TCA_HFSC_USC, &cl->cl_usc) < 0)) 1308 goto nla_put_failure; 1309 1310 return skb->len; 1311 1312 nla_put_failure: 1313 return -1; 1314 } 1315 1316 static int 1317 hfsc_dump_class(struct Qdisc *sch, unsigned long arg, struct sk_buff *skb, 1318 struct tcmsg *tcm) 1319 { 1320 struct hfsc_class *cl = (struct hfsc_class *)arg; 1321 struct nlattr *nest; 1322 1323 tcm->tcm_parent = cl->cl_parent ? cl->cl_parent->cl_common.classid : 1324 TC_H_ROOT; 1325 tcm->tcm_handle = cl->cl_common.classid; 1326 if (cl->level == 0) 1327 tcm->tcm_info = cl->qdisc->handle; 1328 1329 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 1330 if (nest == NULL) 1331 goto nla_put_failure; 1332 if (hfsc_dump_curves(skb, cl) < 0) 1333 goto nla_put_failure; 1334 return nla_nest_end(skb, nest); 1335 1336 nla_put_failure: 1337 nla_nest_cancel(skb, nest); 1338 return -EMSGSIZE; 1339 } 1340 1341 static int 1342 hfsc_dump_class_stats(struct Qdisc *sch, unsigned long arg, 1343 struct gnet_dump *d) 1344 { 1345 struct hfsc_class *cl = (struct hfsc_class *)arg; 1346 struct tc_hfsc_stats xstats; 1347 __u32 qlen; 1348 1349 qdisc_qstats_qlen_backlog(cl->qdisc, &qlen, &cl->qstats.backlog); 1350 xstats.level = cl->level; 1351 xstats.period = cl->cl_vtperiod; 1352 xstats.work = cl->cl_total; 1353 xstats.rtwork = cl->cl_cumul; 1354 1355 if (gnet_stats_copy_basic(d, NULL, &cl->bstats, true) < 0 || 1356 gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 || 1357 gnet_stats_copy_queue(d, NULL, &cl->qstats, qlen) < 0) 1358 return -1; 1359 1360 return gnet_stats_copy_app(d, &xstats, sizeof(xstats)); 1361 } 1362 1363 1364 1365 static void 1366 hfsc_walk(struct Qdisc *sch, struct qdisc_walker *arg) 1367 { 1368 struct hfsc_sched *q = qdisc_priv(sch); 1369 struct hfsc_class *cl; 1370 unsigned int i; 1371 1372 if (arg->stop) 1373 return; 1374 1375 for (i = 0; i < q->clhash.hashsize; i++) { 1376 hlist_for_each_entry(cl, &q->clhash.hash[i], 1377 cl_common.hnode) { 1378 if (!tc_qdisc_stats_dump(sch, (unsigned long)cl, arg)) 1379 return; 1380 } 1381 } 1382 } 1383 1384 static void 1385 hfsc_schedule_watchdog(struct Qdisc *sch) 1386 { 1387 struct hfsc_sched *q = qdisc_priv(sch); 1388 struct hfsc_class *cl; 1389 u64 next_time = 0; 1390 1391 cl = eltree_get_minel(q); 1392 if (cl) 1393 next_time = cl->cl_e; 1394 if (q->root.cl_cfmin != 0) { 1395 if (next_time == 0 || next_time > q->root.cl_cfmin) 1396 next_time = q->root.cl_cfmin; 1397 } 1398 if (next_time) 1399 qdisc_watchdog_schedule(&q->watchdog, next_time); 1400 } 1401 1402 static int 1403 hfsc_init_qdisc(struct Qdisc *sch, struct nlattr *opt, 1404 struct netlink_ext_ack *extack) 1405 { 1406 struct hfsc_sched *q = qdisc_priv(sch); 1407 struct tc_hfsc_qopt *qopt; 1408 int err; 1409 1410 qdisc_watchdog_init(&q->watchdog, sch); 1411 1412 if (!opt || nla_len(opt) < sizeof(*qopt)) 1413 return -EINVAL; 1414 qopt = nla_data(opt); 1415 1416 q->defcls = qopt->defcls; 1417 err = qdisc_class_hash_init(&q->clhash); 1418 if (err < 0) 1419 return err; 1420 q->eligible = RB_ROOT; 1421 1422 err = tcf_block_get(&q->root.block, &q->root.filter_list, sch, extack); 1423 if (err) 1424 return err; 1425 1426 gnet_stats_basic_sync_init(&q->root.bstats); 1427 q->root.cl_common.classid = sch->handle; 1428 q->root.sched = q; 1429 q->root.qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 1430 sch->handle, NULL); 1431 if (q->root.qdisc == NULL) 1432 q->root.qdisc = &noop_qdisc; 1433 else 1434 qdisc_hash_add(q->root.qdisc, true); 1435 INIT_LIST_HEAD(&q->root.children); 1436 q->root.vt_tree = RB_ROOT; 1437 q->root.cf_tree = RB_ROOT; 1438 1439 qdisc_class_hash_insert(&q->clhash, &q->root.cl_common); 1440 qdisc_class_hash_grow(sch, &q->clhash); 1441 1442 return 0; 1443 } 1444 1445 static int 1446 hfsc_change_qdisc(struct Qdisc *sch, struct nlattr *opt, 1447 struct netlink_ext_ack *extack) 1448 { 1449 struct hfsc_sched *q = qdisc_priv(sch); 1450 struct tc_hfsc_qopt *qopt; 1451 1452 if (nla_len(opt) < sizeof(*qopt)) 1453 return -EINVAL; 1454 qopt = nla_data(opt); 1455 1456 WRITE_ONCE(q->defcls, qopt->defcls); 1457 1458 return 0; 1459 } 1460 1461 static void 1462 hfsc_reset_class(struct hfsc_class *cl) 1463 { 1464 cl->cl_total = 0; 1465 cl->cl_cumul = 0; 1466 cl->cl_d = 0; 1467 cl->cl_e = 0; 1468 cl->cl_vt = 0; 1469 cl->cl_vtadj = 0; 1470 cl->cl_cvtmin = 0; 1471 cl->cl_cvtoff = 0; 1472 cl->cl_vtperiod = 0; 1473 cl->cl_parentperiod = 0; 1474 cl->cl_f = 0; 1475 cl->cl_myf = 0; 1476 cl->cl_cfmin = 0; 1477 cl->cl_nactive = 0; 1478 1479 cl->vt_tree = RB_ROOT; 1480 cl->cf_tree = RB_ROOT; 1481 qdisc_reset(cl->qdisc); 1482 1483 if (cl->cl_flags & HFSC_RSC) 1484 rtsc_init(&cl->cl_deadline, &cl->cl_rsc, 0, 0); 1485 if (cl->cl_flags & HFSC_FSC) 1486 rtsc_init(&cl->cl_virtual, &cl->cl_fsc, 0, 0); 1487 if (cl->cl_flags & HFSC_USC) 1488 rtsc_init(&cl->cl_ulimit, &cl->cl_usc, 0, 0); 1489 } 1490 1491 static void 1492 hfsc_reset_qdisc(struct Qdisc *sch) 1493 { 1494 struct hfsc_sched *q = qdisc_priv(sch); 1495 struct hfsc_class *cl; 1496 unsigned int i; 1497 1498 for (i = 0; i < q->clhash.hashsize; i++) { 1499 hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode) 1500 hfsc_reset_class(cl); 1501 } 1502 q->eligible = RB_ROOT; 1503 qdisc_watchdog_cancel(&q->watchdog); 1504 } 1505 1506 static void 1507 hfsc_destroy_qdisc(struct Qdisc *sch) 1508 { 1509 struct hfsc_sched *q = qdisc_priv(sch); 1510 struct hlist_node *next; 1511 struct hfsc_class *cl; 1512 unsigned int i; 1513 1514 for (i = 0; i < q->clhash.hashsize; i++) { 1515 hlist_for_each_entry(cl, &q->clhash.hash[i], cl_common.hnode) { 1516 tcf_block_put(cl->block); 1517 cl->block = NULL; 1518 } 1519 } 1520 for (i = 0; i < q->clhash.hashsize; i++) { 1521 hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i], 1522 cl_common.hnode) 1523 hfsc_destroy_class(sch, cl); 1524 } 1525 qdisc_class_hash_destroy(&q->clhash); 1526 qdisc_watchdog_cancel(&q->watchdog); 1527 } 1528 1529 static int 1530 hfsc_dump_qdisc(struct Qdisc *sch, struct sk_buff *skb) 1531 { 1532 struct hfsc_sched *q = qdisc_priv(sch); 1533 unsigned char *b = skb_tail_pointer(skb); 1534 struct tc_hfsc_qopt qopt; 1535 1536 qopt.defcls = READ_ONCE(q->defcls); 1537 if (nla_put(skb, TCA_OPTIONS, sizeof(qopt), &qopt)) 1538 goto nla_put_failure; 1539 return skb->len; 1540 1541 nla_put_failure: 1542 nlmsg_trim(skb, b); 1543 return -1; 1544 } 1545 1546 static int 1547 hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free) 1548 { 1549 unsigned int len = qdisc_pkt_len(skb); 1550 struct hfsc_class *cl; 1551 int err; 1552 bool first; 1553 1554 cl = hfsc_classify(skb, sch, &err); 1555 if (cl == NULL) { 1556 if (err & __NET_XMIT_BYPASS) 1557 qdisc_qstats_drop(sch); 1558 __qdisc_drop(skb, to_free); 1559 return err; 1560 } 1561 1562 first = !cl->qdisc->q.qlen; 1563 err = qdisc_enqueue(skb, cl->qdisc, to_free); 1564 if (unlikely(err != NET_XMIT_SUCCESS)) { 1565 if (net_xmit_drop_count(err)) { 1566 cl->qstats.drops++; 1567 qdisc_qstats_drop(sch); 1568 } 1569 return err; 1570 } 1571 1572 if (first && !cl->cl_nactive) { 1573 if (cl->cl_flags & HFSC_RSC) 1574 init_ed(cl, len); 1575 if (cl->cl_flags & HFSC_FSC) 1576 init_vf(cl, len); 1577 /* 1578 * If this is the first packet, isolate the head so an eventual 1579 * head drop before the first dequeue operation has no chance 1580 * to invalidate the deadline. 1581 */ 1582 if (cl->cl_flags & HFSC_RSC) 1583 cl->qdisc->ops->peek(cl->qdisc); 1584 1585 } 1586 1587 sch->qstats.backlog += len; 1588 sch->q.qlen++; 1589 1590 return NET_XMIT_SUCCESS; 1591 } 1592 1593 static struct sk_buff * 1594 hfsc_dequeue(struct Qdisc *sch) 1595 { 1596 struct hfsc_sched *q = qdisc_priv(sch); 1597 struct hfsc_class *cl; 1598 struct sk_buff *skb; 1599 u64 cur_time; 1600 unsigned int next_len; 1601 int realtime = 0; 1602 1603 if (sch->q.qlen == 0) 1604 return NULL; 1605 1606 cur_time = psched_get_time(); 1607 1608 /* 1609 * if there are eligible classes, use real-time criteria. 1610 * find the class with the minimum deadline among 1611 * the eligible classes. 1612 */ 1613 cl = eltree_get_mindl(q, cur_time); 1614 if (cl) { 1615 realtime = 1; 1616 } else { 1617 /* 1618 * use link-sharing criteria 1619 * get the class with the minimum vt in the hierarchy 1620 */ 1621 cl = vttree_get_minvt(&q->root, cur_time); 1622 if (cl == NULL) { 1623 qdisc_qstats_overlimit(sch); 1624 hfsc_schedule_watchdog(sch); 1625 return NULL; 1626 } 1627 } 1628 1629 skb = qdisc_dequeue_peeked(cl->qdisc); 1630 if (skb == NULL) { 1631 qdisc_warn_nonwc("HFSC", cl->qdisc); 1632 return NULL; 1633 } 1634 1635 bstats_update(&cl->bstats, skb); 1636 update_vf(cl, qdisc_pkt_len(skb), cur_time); 1637 if (realtime) 1638 cl->cl_cumul += qdisc_pkt_len(skb); 1639 1640 if (cl->cl_flags & HFSC_RSC) { 1641 if (cl->qdisc->q.qlen != 0) { 1642 /* update ed */ 1643 next_len = qdisc_peek_len(cl->qdisc); 1644 /* Check queue length again since some qdisc implementations 1645 * (e.g., netem/codel) might empty the queue during the peek 1646 * operation. 1647 */ 1648 if (cl->qdisc->q.qlen != 0) { 1649 if (realtime) 1650 update_ed(cl, next_len); 1651 else 1652 update_d(cl, next_len); 1653 } 1654 } else { 1655 /* the class becomes passive */ 1656 eltree_remove(cl); 1657 } 1658 } 1659 1660 qdisc_bstats_update(sch, skb); 1661 qdisc_qstats_backlog_dec(sch, skb); 1662 sch->q.qlen--; 1663 1664 return skb; 1665 } 1666 1667 static const struct Qdisc_class_ops hfsc_class_ops = { 1668 .change = hfsc_change_class, 1669 .delete = hfsc_delete_class, 1670 .graft = hfsc_graft_class, 1671 .leaf = hfsc_class_leaf, 1672 .qlen_notify = hfsc_qlen_notify, 1673 .find = hfsc_search_class, 1674 .bind_tcf = hfsc_bind_tcf, 1675 .unbind_tcf = hfsc_unbind_tcf, 1676 .tcf_block = hfsc_tcf_block, 1677 .dump = hfsc_dump_class, 1678 .dump_stats = hfsc_dump_class_stats, 1679 .walk = hfsc_walk 1680 }; 1681 1682 static struct Qdisc_ops hfsc_qdisc_ops __read_mostly = { 1683 .id = "hfsc", 1684 .init = hfsc_init_qdisc, 1685 .change = hfsc_change_qdisc, 1686 .reset = hfsc_reset_qdisc, 1687 .destroy = hfsc_destroy_qdisc, 1688 .dump = hfsc_dump_qdisc, 1689 .enqueue = hfsc_enqueue, 1690 .dequeue = hfsc_dequeue, 1691 .peek = qdisc_peek_dequeued, 1692 .cl_ops = &hfsc_class_ops, 1693 .priv_size = sizeof(struct hfsc_sched), 1694 .owner = THIS_MODULE 1695 }; 1696 MODULE_ALIAS_NET_SCH("hfsc"); 1697 1698 static int __init 1699 hfsc_init(void) 1700 { 1701 return register_qdisc(&hfsc_qdisc_ops); 1702 } 1703 1704 static void __exit 1705 hfsc_cleanup(void) 1706 { 1707 unregister_qdisc(&hfsc_qdisc_ops); 1708 } 1709 1710 MODULE_LICENSE("GPL"); 1711 MODULE_DESCRIPTION("Hierarchical Fair Service Curve scheduler"); 1712 module_init(hfsc_init); 1713 module_exit(hfsc_cleanup); 1714