1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/sch_fq.c Fair Queue Packet Scheduler (per flow pacing) 4 * 5 * Copyright (C) 2013-2023 Eric Dumazet <edumazet@google.com> 6 * 7 * Meant to be mostly used for locally generated traffic : 8 * Fast classification depends on skb->sk being set before reaching us. 9 * If not, (router workload), we use rxhash as fallback, with 32 bits wide hash. 10 * All packets belonging to a socket are considered as a 'flow'. 11 * 12 * Flows are dynamically allocated and stored in a hash table of RB trees 13 * They are also part of one Round Robin 'queues' (new or old flows) 14 * 15 * Burst avoidance (aka pacing) capability : 16 * 17 * Transport (eg TCP) can set in sk->sk_pacing_rate a rate, enqueue a 18 * bunch of packets, and this packet scheduler adds delay between 19 * packets to respect rate limitation. 20 * 21 * enqueue() : 22 * - lookup one RB tree (out of 1024 or more) to find the flow. 23 * If non existent flow, create it, add it to the tree. 24 * Add skb to the per flow list of skb (fifo). 25 * - Use a special fifo for high prio packets 26 * 27 * dequeue() : serves flows in Round Robin 28 * Note : When a flow becomes empty, we do not immediately remove it from 29 * rb trees, for performance reasons (its expected to send additional packets, 30 * or SLAB cache will reuse socket for another flow) 31 */ 32 33 #include <linux/module.h> 34 #include <linux/types.h> 35 #include <linux/kernel.h> 36 #include <linux/jiffies.h> 37 #include <linux/string.h> 38 #include <linux/in.h> 39 #include <linux/errno.h> 40 #include <linux/init.h> 41 #include <linux/skbuff.h> 42 #include <linux/slab.h> 43 #include <linux/rbtree.h> 44 #include <linux/hash.h> 45 #include <linux/prefetch.h> 46 #include <linux/vmalloc.h> 47 #include <net/netlink.h> 48 #include <net/pkt_sched.h> 49 #include <net/sock.h> 50 #include <net/tcp_states.h> 51 #include <net/tcp.h> 52 53 struct fq_skb_cb { 54 u64 time_to_send; 55 u8 band; 56 }; 57 58 static inline struct fq_skb_cb *fq_skb_cb(struct sk_buff *skb) 59 { 60 qdisc_cb_private_validate(skb, sizeof(struct fq_skb_cb)); 61 return (struct fq_skb_cb *)qdisc_skb_cb(skb)->data; 62 } 63 64 /* 65 * Per flow structure, dynamically allocated. 66 * If packets have monotically increasing time_to_send, they are placed in O(1) 67 * in linear list (head,tail), otherwise are placed in a rbtree (t_root). 68 */ 69 struct fq_flow { 70 /* First cache line : used in fq_gc(), fq_enqueue(), fq_dequeue() */ 71 struct rb_root t_root; 72 struct sk_buff *head; /* list of skbs for this flow : first skb */ 73 union { 74 struct sk_buff *tail; /* last skb in the list */ 75 unsigned long age; /* (jiffies | 1UL) when flow was emptied, for gc */ 76 }; 77 union { 78 struct rb_node fq_node; /* anchor in fq_root[] trees */ 79 /* Following field is only used for q->internal, 80 * because q->internal is not hashed in fq_root[] 81 */ 82 u64 stat_fastpath_packets; 83 }; 84 struct sock *sk; 85 u32 socket_hash; /* sk_hash */ 86 int qlen; /* number of packets in flow queue */ 87 88 /* Second cache line */ 89 int credit; 90 int band; 91 struct fq_flow *next; /* next pointer in RR lists */ 92 93 struct rb_node rate_node; /* anchor in q->delayed tree */ 94 u64 time_next_packet; 95 }; 96 97 struct fq_flow_head { 98 struct fq_flow *first; 99 struct fq_flow *last; 100 }; 101 102 struct fq_perband_flows { 103 struct fq_flow_head new_flows; 104 struct fq_flow_head old_flows; 105 int credit; 106 int quantum; /* based on band nr : 576KB, 192KB, 64KB */ 107 }; 108 109 #define FQ_PRIO2BAND_CRUMB_SIZE ((TC_PRIO_MAX + 1) >> 2) 110 111 struct fq_sched_data { 112 /* Read mostly cache line */ 113 114 u64 offload_horizon; 115 u32 quantum; 116 u32 initial_quantum; 117 u32 flow_refill_delay; 118 u32 flow_plimit; /* max packets per flow */ 119 unsigned long flow_max_rate; /* optional max rate per flow */ 120 u64 ce_threshold; 121 u64 horizon; /* horizon in ns */ 122 u32 orphan_mask; /* mask for orphaned skb */ 123 u32 low_rate_threshold; 124 struct rb_root *fq_root; 125 u8 rate_enable; 126 u8 fq_trees_log; 127 u8 horizon_drop; 128 u8 prio2band[FQ_PRIO2BAND_CRUMB_SIZE]; 129 u32 timer_slack; /* hrtimer slack in ns */ 130 131 /* Read/Write fields. */ 132 133 unsigned int band_nr; /* band being serviced in fq_dequeue() */ 134 135 struct fq_perband_flows band_flows[FQ_BANDS]; 136 137 struct fq_flow internal; /* fastpath queue. */ 138 struct rb_root delayed; /* for rate limited flows */ 139 u64 time_next_delayed_flow; 140 unsigned long unthrottle_latency_ns; 141 142 u32 band_pkt_count[FQ_BANDS]; 143 u32 flows; 144 u32 inactive_flows; /* Flows with no packet to send. */ 145 u32 throttled_flows; 146 147 u64 stat_throttled; 148 struct qdisc_watchdog watchdog; 149 u64 stat_gc_flows; 150 151 /* Seldom used fields. */ 152 153 u64 stat_band_drops[FQ_BANDS]; 154 u64 stat_ce_mark; 155 u64 stat_horizon_drops; 156 u64 stat_horizon_caps; 157 u64 stat_flows_plimit; 158 u64 stat_pkts_too_long; 159 u64 stat_allocation_errors; 160 }; 161 162 /* return the i-th 2-bit value ("crumb") */ 163 static u8 fq_prio2band(const u8 *prio2band, unsigned int prio) 164 { 165 return (READ_ONCE(prio2band[prio / 4]) >> (2 * (prio & 0x3))) & 0x3; 166 } 167 168 /* 169 * f->tail and f->age share the same location. 170 * We can use the low order bit to differentiate if this location points 171 * to a sk_buff or contains a jiffies value, if we force this value to be odd. 172 * This assumes f->tail low order bit must be 0 since alignof(struct sk_buff) >= 2 173 */ 174 static void fq_flow_set_detached(struct fq_flow *f) 175 { 176 f->age = jiffies | 1UL; 177 } 178 179 static bool fq_flow_is_detached(const struct fq_flow *f) 180 { 181 return !!(f->age & 1UL); 182 } 183 184 /* special value to mark a throttled flow (not on old/new list) */ 185 static struct fq_flow throttled; 186 187 static bool fq_flow_is_throttled(const struct fq_flow *f) 188 { 189 return f->next == &throttled; 190 } 191 192 enum new_flow { 193 NEW_FLOW, 194 OLD_FLOW 195 }; 196 197 static void fq_flow_add_tail(struct fq_sched_data *q, struct fq_flow *flow, 198 enum new_flow list_sel) 199 { 200 struct fq_perband_flows *pband = &q->band_flows[flow->band]; 201 struct fq_flow_head *head = (list_sel == NEW_FLOW) ? 202 &pband->new_flows : 203 &pband->old_flows; 204 205 if (head->first) 206 head->last->next = flow; 207 else 208 head->first = flow; 209 head->last = flow; 210 flow->next = NULL; 211 } 212 213 static void fq_flow_unset_throttled(struct fq_sched_data *q, struct fq_flow *f) 214 { 215 rb_erase(&f->rate_node, &q->delayed); 216 q->throttled_flows--; 217 fq_flow_add_tail(q, f, OLD_FLOW); 218 } 219 220 static void fq_flow_rb_insert(struct fq_sched_data *q, struct fq_flow *f) 221 { 222 struct rb_node **p = &q->delayed.rb_node, *parent = NULL; 223 224 while (*p) { 225 struct fq_flow *aux; 226 227 parent = *p; 228 aux = rb_entry(parent, struct fq_flow, rate_node); 229 if (f->time_next_packet >= aux->time_next_packet) 230 p = &parent->rb_right; 231 else 232 p = &parent->rb_left; 233 } 234 rb_link_node(&f->rate_node, parent, p); 235 rb_insert_color(&f->rate_node, &q->delayed); 236 237 if (q->time_next_delayed_flow > f->time_next_packet) 238 q->time_next_delayed_flow = f->time_next_packet; 239 } 240 241 static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f) 242 { 243 fq_flow_rb_insert(q, f); 244 q->throttled_flows++; 245 q->stat_throttled++; 246 f->next = &throttled; 247 } 248 249 static struct kmem_cache *fq_flow_cachep __read_mostly; 250 251 252 #define FQ_GC_AGE (3*HZ) 253 254 static bool fq_gc_candidate(const struct fq_flow *f) 255 { 256 return fq_flow_is_detached(f) && 257 time_after(jiffies, f->age + FQ_GC_AGE); 258 } 259 260 static void fq_gc(struct fq_sched_data *q, 261 struct rb_root *root, 262 struct sock *sk) 263 { 264 struct fq_flow *f, *tofree = NULL; 265 struct rb_node **p, *parent; 266 int fcnt; 267 268 p = &root->rb_node; 269 parent = NULL; 270 while (*p) { 271 parent = *p; 272 273 f = rb_entry(parent, struct fq_flow, fq_node); 274 if (f->sk == sk) 275 break; 276 277 if (fq_gc_candidate(f)) { 278 f->next = tofree; 279 tofree = f; 280 } 281 282 if (f->sk > sk) 283 p = &parent->rb_right; 284 else 285 p = &parent->rb_left; 286 } 287 288 if (!tofree) 289 return; 290 291 fcnt = 0; 292 while (tofree) { 293 f = tofree; 294 tofree = f->next; 295 rb_erase(&f->fq_node, root); 296 kmem_cache_free(fq_flow_cachep, f); 297 fcnt++; 298 } 299 q->flows -= fcnt; 300 q->inactive_flows -= fcnt; 301 q->stat_gc_flows += fcnt; 302 } 303 304 /* Fast path can be used if : 305 * 1) Packet tstamp is in the past, or within the pacing offload horizon. 306 * 2) FQ qlen == 0 OR 307 * (no flow is currently eligible for transmit, 308 * AND fast path queue has less than 8 packets) 309 * 3) No SO_MAX_PACING_RATE on the socket (if any). 310 * 4) No @maxrate attribute on this qdisc, 311 * 312 * FQ can not use generic TCQ_F_CAN_BYPASS infrastructure. 313 */ 314 static bool fq_fastpath_check(const struct Qdisc *sch, struct sk_buff *skb, 315 u64 now) 316 { 317 const struct fq_sched_data *q = qdisc_priv(sch); 318 const struct sock *sk; 319 320 if (fq_skb_cb(skb)->time_to_send > now + q->offload_horizon) 321 return false; 322 323 if (sch->q.qlen != 0) { 324 /* Even if some packets are stored in this qdisc, 325 * we can still enable fast path if all of them are 326 * scheduled in the future (ie no flows are eligible) 327 * or in the fast path queue. 328 */ 329 if (q->flows != q->inactive_flows + q->throttled_flows) 330 return false; 331 332 /* Do not allow fast path queue to explode, we want Fair Queue mode 333 * under pressure. 334 */ 335 if (q->internal.qlen >= 8) 336 return false; 337 338 /* Ordering invariants fall apart if some delayed flows 339 * are ready but we haven't serviced them, yet. 340 */ 341 if (q->time_next_delayed_flow <= now + q->offload_horizon) 342 return false; 343 } 344 345 sk = skb->sk; 346 if (sk && sk_fullsock(sk) && !sk_is_tcp(sk) && 347 sk->sk_max_pacing_rate != ~0UL) 348 return false; 349 350 if (q->flow_max_rate != ~0UL) 351 return false; 352 353 return true; 354 } 355 356 static struct fq_flow *fq_classify(struct Qdisc *sch, struct sk_buff *skb, 357 u64 now) 358 { 359 struct fq_sched_data *q = qdisc_priv(sch); 360 struct rb_node **p, *parent; 361 struct sock *sk = skb->sk; 362 struct rb_root *root; 363 struct fq_flow *f; 364 365 /* SYNACK messages are attached to a TCP_NEW_SYN_RECV request socket 366 * or a listener (SYNCOOKIE mode) 367 * 1) request sockets are not full blown, 368 * they do not contain sk_pacing_rate 369 * 2) They are not part of a 'flow' yet 370 * 3) We do not want to rate limit them (eg SYNFLOOD attack), 371 * especially if the listener set SO_MAX_PACING_RATE 372 * 4) We pretend they are orphaned 373 * TCP can also associate TIME_WAIT sockets with RST or ACK packets. 374 */ 375 if (!sk || sk_listener_or_tw(sk)) { 376 unsigned long hash = skb_get_hash(skb) & q->orphan_mask; 377 378 /* By forcing low order bit to 1, we make sure to not 379 * collide with a local flow (socket pointers are word aligned) 380 */ 381 sk = (struct sock *)((hash << 1) | 1UL); 382 skb_orphan(skb); 383 } else if (sk->sk_state == TCP_CLOSE) { 384 unsigned long hash = skb_get_hash(skb) & q->orphan_mask; 385 /* 386 * Sockets in TCP_CLOSE are non connected. 387 * Typical use case is UDP sockets, they can send packets 388 * with sendto() to many different destinations. 389 * We probably could use a generic bit advertising 390 * non connected sockets, instead of sk_state == TCP_CLOSE, 391 * if we care enough. 392 */ 393 sk = (struct sock *)((hash << 1) | 1UL); 394 } 395 396 if (fq_fastpath_check(sch, skb, now)) { 397 q->internal.stat_fastpath_packets++; 398 if (skb->sk == sk && q->rate_enable && 399 READ_ONCE(sk->sk_pacing_status) != SK_PACING_FQ) 400 smp_store_release(&sk->sk_pacing_status, 401 SK_PACING_FQ); 402 return &q->internal; 403 } 404 405 root = &q->fq_root[hash_ptr(sk, q->fq_trees_log)]; 406 407 fq_gc(q, root, sk); 408 409 p = &root->rb_node; 410 parent = NULL; 411 while (*p) { 412 parent = *p; 413 414 f = rb_entry(parent, struct fq_flow, fq_node); 415 if (f->sk == sk) { 416 /* socket might have been reallocated, so check 417 * if its sk_hash is the same. 418 * It not, we need to refill credit with 419 * initial quantum 420 */ 421 if (unlikely(skb->sk == sk && 422 f->socket_hash != sk->sk_hash)) { 423 f->credit = q->initial_quantum; 424 f->socket_hash = sk->sk_hash; 425 if (q->rate_enable) 426 smp_store_release(&sk->sk_pacing_status, 427 SK_PACING_FQ); 428 if (fq_flow_is_throttled(f)) 429 fq_flow_unset_throttled(q, f); 430 f->time_next_packet = 0ULL; 431 } 432 return f; 433 } 434 if (f->sk > sk) 435 p = &parent->rb_right; 436 else 437 p = &parent->rb_left; 438 } 439 440 f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN); 441 if (unlikely(!f)) { 442 q->stat_allocation_errors++; 443 return &q->internal; 444 } 445 /* f->t_root is already zeroed after kmem_cache_zalloc() */ 446 447 fq_flow_set_detached(f); 448 f->sk = sk; 449 if (skb->sk == sk) { 450 f->socket_hash = sk->sk_hash; 451 if (q->rate_enable) 452 smp_store_release(&sk->sk_pacing_status, 453 SK_PACING_FQ); 454 } 455 f->credit = q->initial_quantum; 456 457 rb_link_node(&f->fq_node, parent, p); 458 rb_insert_color(&f->fq_node, root); 459 460 q->flows++; 461 q->inactive_flows++; 462 return f; 463 } 464 465 static struct sk_buff *fq_peek(struct fq_flow *flow) 466 { 467 struct sk_buff *skb = skb_rb_first(&flow->t_root); 468 struct sk_buff *head = flow->head; 469 470 if (!skb) 471 return head; 472 473 if (!head) 474 return skb; 475 476 if (fq_skb_cb(skb)->time_to_send < fq_skb_cb(head)->time_to_send) 477 return skb; 478 return head; 479 } 480 481 static void fq_erase_head(struct Qdisc *sch, struct fq_flow *flow, 482 struct sk_buff *skb) 483 { 484 if (skb == flow->head) { 485 struct sk_buff *next = skb->next; 486 487 prefetch(next); 488 flow->head = next; 489 } else { 490 rb_erase(&skb->rbnode, &flow->t_root); 491 skb->dev = qdisc_dev(sch); 492 } 493 } 494 495 /* Remove one skb from flow queue. 496 * This skb must be the return value of prior fq_peek(). 497 */ 498 static void fq_dequeue_skb(struct Qdisc *sch, struct fq_flow *flow, 499 struct sk_buff *skb) 500 { 501 fq_erase_head(sch, flow, skb); 502 skb_mark_not_on_list(skb); 503 qdisc_qstats_backlog_dec(sch, skb); 504 qdisc_qlen_dec(sch); 505 qdisc_bstats_update(sch, skb); 506 } 507 508 static void flow_queue_add(struct fq_flow *flow, struct sk_buff *skb) 509 { 510 struct rb_node **p, *parent; 511 struct sk_buff *head, *aux; 512 513 head = flow->head; 514 if (!head || 515 fq_skb_cb(skb)->time_to_send >= fq_skb_cb(flow->tail)->time_to_send) { 516 if (!head) 517 flow->head = skb; 518 else 519 flow->tail->next = skb; 520 flow->tail = skb; 521 skb->next = NULL; 522 return; 523 } 524 525 p = &flow->t_root.rb_node; 526 parent = NULL; 527 528 while (*p) { 529 parent = *p; 530 aux = rb_to_skb(parent); 531 if (fq_skb_cb(skb)->time_to_send >= fq_skb_cb(aux)->time_to_send) 532 p = &parent->rb_right; 533 else 534 p = &parent->rb_left; 535 } 536 rb_link_node(&skb->rbnode, parent, p); 537 rb_insert_color(&skb->rbnode, &flow->t_root); 538 } 539 540 static bool fq_packet_beyond_horizon(ktime_t time_to_send, 541 const struct fq_sched_data *q, u64 now) 542 { 543 return unlikely((s64)time_to_send > (s64)(now + q->horizon)); 544 } 545 546 static void fq_flow_adjust_timer(struct fq_sched_data *q, struct fq_flow *flow, 547 u64 time_to_send, u64 now) 548 { 549 if (time_to_send <= now) { 550 fq_flow_unset_throttled(q, flow); 551 if (q->time_next_delayed_flow == flow->time_next_packet) { 552 struct rb_node *p = rb_first(&q->delayed); 553 554 q->time_next_delayed_flow = p ? rb_entry(p, struct fq_flow, rate_node)->time_next_packet : ~0ULL; 555 } 556 flow->time_next_packet = time_to_send; 557 } else { 558 rb_erase(&flow->rate_node, &q->delayed); 559 flow->time_next_packet = time_to_send; 560 fq_flow_rb_insert(q, flow); 561 } 562 } 563 564 static ktime_t fq_skb_tstamp_to_mono(struct sk_buff *skb) 565 { 566 const ktime_t mono_max = NSEC_PER_SEC * TIME_UPTIME_SEC_MAX; 567 568 if (likely(skb->tstamp_type == SKB_CLOCK_MONOTONIC)) 569 return max(skb->tstamp, 1); 570 571 if (skb->tstamp_type == SKB_CLOCK_TAI) 572 return max(ktime_sub(skb->tstamp, ktime_mono_to_any(0, TK_OFFS_TAI)), 1); 573 574 if (likely(skb->tstamp > mono_max)) 575 return max(ktime_sub(skb->tstamp, ktime_mono_to_real(0)), 1); 576 577 /* Handle BPF programs setting skb->stamp but not tstamp_type */ 578 net_warn_ratelimited("fq: likely mono tstamp with tstamp_type 0\n"); 579 580 skb->tstamp_type = SKB_CLOCK_MONOTONIC; 581 return max(skb->tstamp, 1); 582 } 583 584 static void fq_mono_to_skb_tstamp(struct sk_buff *skb, ktime_t time_to_send) 585 { 586 if (skb->tstamp_type == SKB_CLOCK_MONOTONIC) 587 skb->tstamp = time_to_send; 588 else if (skb->tstamp_type == SKB_CLOCK_REALTIME) 589 skb->tstamp = ktime_mono_to_real(time_to_send); 590 else 591 skb->tstamp = ktime_mono_to_any(time_to_send, TK_OFFS_TAI); 592 } 593 594 static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch, 595 struct sk_buff **to_free) 596 { 597 struct fq_sched_data *q = qdisc_priv(sch); 598 struct fq_flow *f; 599 u64 now; 600 u8 band; 601 602 band = fq_prio2band(q->prio2band, skb->priority & TC_PRIO_MAX); 603 if (unlikely(q->band_pkt_count[band] >= sch->limit)) { 604 q->stat_band_drops[band]++; 605 return qdisc_drop_reason(skb, sch, to_free, QDISC_DROP_BAND_LIMIT); 606 } 607 608 now = ktime_get_ns(); 609 if (!skb->tstamp) { 610 fq_skb_cb(skb)->time_to_send = now; 611 } else { 612 ktime_t time_to_send = fq_skb_tstamp_to_mono(skb); 613 614 /* Check if packet timestamp is too far in the future. */ 615 if (fq_packet_beyond_horizon(time_to_send, q, now)) { 616 if (q->horizon_drop) { 617 q->stat_horizon_drops++; 618 return qdisc_drop_reason(skb, sch, to_free, 619 QDISC_DROP_HORIZON_LIMIT); 620 } 621 q->stat_horizon_caps++; 622 time_to_send = now + q->horizon; 623 fq_mono_to_skb_tstamp(skb, time_to_send); 624 } 625 fq_skb_cb(skb)->time_to_send = (u64)time_to_send; 626 } 627 628 f = fq_classify(sch, skb, now); 629 630 if (f != &q->internal) { 631 if (unlikely(f->qlen >= q->flow_plimit)) { 632 q->stat_flows_plimit++; 633 return qdisc_drop_reason(skb, sch, to_free, 634 QDISC_DROP_FLOW_LIMIT); 635 } 636 637 if (fq_flow_is_detached(f)) { 638 fq_flow_add_tail(q, f, NEW_FLOW); 639 if (time_after(jiffies, f->age + q->flow_refill_delay)) 640 f->credit = max_t(u32, f->credit, q->quantum); 641 } 642 643 f->band = band; 644 q->band_pkt_count[band]++; 645 fq_skb_cb(skb)->band = band; 646 if (f->qlen == 0) 647 q->inactive_flows--; 648 } 649 650 f->qlen++; 651 /* Note: this overwrites f->age */ 652 flow_queue_add(f, skb); 653 654 if (fq_skb_cb(skb)->time_to_send < f->time_next_packet && skb->tstamp && 655 fq_flow_is_throttled(f) && q->flow_max_rate == ~0UL) 656 fq_flow_adjust_timer(q, f, fq_skb_cb(skb)->time_to_send, now); 657 658 qdisc_qstats_backlog_inc(sch, skb); 659 qdisc_qlen_inc(sch); 660 661 return NET_XMIT_SUCCESS; 662 } 663 664 static void fq_check_throttled(struct fq_sched_data *q, u64 now) 665 { 666 unsigned long sample; 667 struct rb_node *p; 668 669 if (q->time_next_delayed_flow > now + q->offload_horizon) 670 return; 671 672 /* Update unthrottle latency EWMA. 673 * This is cheap and can help diagnosing timer/latency problems. 674 */ 675 sample = (unsigned long)(now - q->time_next_delayed_flow); 676 if ((long)sample > 0) { 677 q->unthrottle_latency_ns -= q->unthrottle_latency_ns >> 3; 678 q->unthrottle_latency_ns += sample >> 3; 679 } 680 now += q->offload_horizon; 681 682 q->time_next_delayed_flow = ~0ULL; 683 while ((p = rb_first(&q->delayed)) != NULL) { 684 struct fq_flow *f = rb_entry(p, struct fq_flow, rate_node); 685 686 if (f->time_next_packet > now) { 687 q->time_next_delayed_flow = f->time_next_packet; 688 break; 689 } 690 fq_flow_unset_throttled(q, f); 691 } 692 } 693 694 static struct fq_flow_head *fq_pband_head_select(struct fq_perband_flows *pband) 695 { 696 if (pband->credit <= 0) 697 return NULL; 698 699 if (pband->new_flows.first) 700 return &pband->new_flows; 701 702 return pband->old_flows.first ? &pband->old_flows : NULL; 703 } 704 705 static struct sk_buff *fq_dequeue(struct Qdisc *sch) 706 { 707 struct fq_sched_data *q = qdisc_priv(sch); 708 struct fq_perband_flows *pband; 709 struct fq_flow_head *head; 710 struct sk_buff *skb; 711 struct fq_flow *f; 712 unsigned long rate; 713 int retry; 714 u32 plen; 715 u64 now; 716 717 if (!sch->q.qlen) 718 return NULL; 719 720 skb = fq_peek(&q->internal); 721 if (skb) { 722 q->internal.qlen--; 723 fq_dequeue_skb(sch, &q->internal, skb); 724 goto out; 725 } 726 727 now = ktime_get_ns(); 728 fq_check_throttled(q, now); 729 retry = 0; 730 pband = &q->band_flows[q->band_nr]; 731 begin: 732 head = fq_pband_head_select(pband); 733 if (!head) { 734 while (++retry <= FQ_BANDS) { 735 if (++q->band_nr == FQ_BANDS) 736 q->band_nr = 0; 737 pband = &q->band_flows[q->band_nr]; 738 pband->credit = min(pband->credit + pband->quantum, 739 pband->quantum); 740 if (pband->credit > 0) 741 goto begin; 742 retry = 0; 743 } 744 if (q->time_next_delayed_flow != ~0ULL) 745 qdisc_watchdog_schedule_range_ns(&q->watchdog, 746 q->time_next_delayed_flow, 747 q->timer_slack); 748 return NULL; 749 } 750 f = head->first; 751 retry = 0; 752 if (f->credit <= 0) { 753 f->credit += q->quantum; 754 head->first = f->next; 755 fq_flow_add_tail(q, f, OLD_FLOW); 756 goto begin; 757 } 758 759 skb = fq_peek(f); 760 if (skb) { 761 u64 time_next_packet = max_t(u64, fq_skb_cb(skb)->time_to_send, 762 f->time_next_packet); 763 764 if (now + q->offload_horizon < time_next_packet) { 765 head->first = f->next; 766 f->time_next_packet = time_next_packet; 767 fq_flow_set_throttled(q, f); 768 goto begin; 769 } 770 prefetch(&skb->end); 771 fq_dequeue_skb(sch, f, skb); 772 if (unlikely((s64)(now - time_next_packet - q->ce_threshold) > 0)) { 773 INET_ECN_set_ce(skb); 774 q->stat_ce_mark++; 775 } 776 if (--f->qlen == 0) 777 q->inactive_flows++; 778 q->band_pkt_count[fq_skb_cb(skb)->band]--; 779 } else { 780 head->first = f->next; 781 /* force a pass through old_flows to prevent starvation */ 782 if (head == &pband->new_flows) { 783 fq_flow_add_tail(q, f, OLD_FLOW); 784 } else { 785 fq_flow_set_detached(f); 786 } 787 goto begin; 788 } 789 plen = qdisc_pkt_len(skb); 790 f->credit -= plen; 791 pband->credit -= plen; 792 793 if (!q->rate_enable) 794 goto out; 795 796 rate = q->flow_max_rate; 797 798 /* If EDT time was provided for this skb, we need to 799 * update f->time_next_packet only if this qdisc enforces 800 * a flow max rate. 801 */ 802 if (!skb->tstamp) { 803 if (skb->sk) 804 rate = min(READ_ONCE(skb->sk->sk_pacing_rate), rate); 805 806 if (rate <= q->low_rate_threshold) { 807 f->credit = 0; 808 } else { 809 plen = max(plen, q->quantum); 810 if (f->credit > 0) 811 goto out; 812 } 813 } 814 if (rate != ~0UL) { 815 u64 len = (u64)plen * NSEC_PER_SEC; 816 817 if (likely(rate)) 818 len = div64_ul(len, rate); 819 /* Since socket rate can change later, 820 * clamp the delay to 1 second. 821 * Really, providers of too big packets should be fixed ! 822 */ 823 if (unlikely(len > NSEC_PER_SEC)) { 824 len = NSEC_PER_SEC; 825 q->stat_pkts_too_long++; 826 } 827 /* Account for schedule/timers drifts. 828 * f->time_next_packet was set when prior packet was sent, 829 * and current time (@now) can be too late by tens of us. 830 */ 831 if (f->time_next_packet) { 832 s64 drift = now - f->time_next_packet; 833 834 if (drift > 0) 835 len -= min_t(u64, len / 2, drift); 836 } 837 f->time_next_packet = now + len; 838 } 839 out: 840 return skb; 841 } 842 843 static void fq_flow_purge(struct fq_flow *flow) 844 { 845 struct rb_node *p = rb_first(&flow->t_root); 846 847 while (p) { 848 struct sk_buff *skb = rb_to_skb(p); 849 850 p = rb_next(p); 851 rb_erase(&skb->rbnode, &flow->t_root); 852 rtnl_kfree_skbs(skb, skb); 853 } 854 rtnl_kfree_skbs(flow->head, flow->tail); 855 flow->head = NULL; 856 flow->qlen = 0; 857 } 858 859 static void fq_reset(struct Qdisc *sch) 860 { 861 struct fq_sched_data *q = qdisc_priv(sch); 862 struct rb_root *root; 863 struct rb_node *p; 864 struct fq_flow *f; 865 unsigned int idx; 866 867 WRITE_ONCE(sch->q.qlen, 0); 868 WRITE_ONCE(sch->qstats.backlog, 0); 869 870 fq_flow_purge(&q->internal); 871 872 if (!q->fq_root) 873 return; 874 875 for (idx = 0; idx < (1U << q->fq_trees_log); idx++) { 876 root = &q->fq_root[idx]; 877 while ((p = rb_first(root)) != NULL) { 878 f = rb_entry(p, struct fq_flow, fq_node); 879 rb_erase(p, root); 880 881 fq_flow_purge(f); 882 883 kmem_cache_free(fq_flow_cachep, f); 884 } 885 } 886 for (idx = 0; idx < FQ_BANDS; idx++) { 887 q->band_flows[idx].new_flows.first = NULL; 888 q->band_flows[idx].old_flows.first = NULL; 889 q->band_pkt_count[idx] = 0; 890 } 891 q->delayed = RB_ROOT; 892 q->flows = 0; 893 q->inactive_flows = 0; 894 q->throttled_flows = 0; 895 } 896 897 static void fq_rehash(struct fq_sched_data *q, 898 struct rb_root *old_array, u32 old_log, 899 struct rb_root *new_array, u32 new_log) 900 { 901 struct rb_node *op, **np, *parent; 902 struct rb_root *oroot, *nroot; 903 struct fq_flow *of, *nf; 904 int fcnt = 0; 905 u32 idx; 906 907 for (idx = 0; idx < (1U << old_log); idx++) { 908 oroot = &old_array[idx]; 909 while ((op = rb_first(oroot)) != NULL) { 910 rb_erase(op, oroot); 911 of = rb_entry(op, struct fq_flow, fq_node); 912 if (fq_gc_candidate(of)) { 913 fcnt++; 914 kmem_cache_free(fq_flow_cachep, of); 915 continue; 916 } 917 nroot = &new_array[hash_ptr(of->sk, new_log)]; 918 919 np = &nroot->rb_node; 920 parent = NULL; 921 while (*np) { 922 parent = *np; 923 924 nf = rb_entry(parent, struct fq_flow, fq_node); 925 BUG_ON(nf->sk == of->sk); 926 927 if (nf->sk > of->sk) 928 np = &parent->rb_right; 929 else 930 np = &parent->rb_left; 931 } 932 933 rb_link_node(&of->fq_node, parent, np); 934 rb_insert_color(&of->fq_node, nroot); 935 } 936 } 937 q->flows -= fcnt; 938 q->inactive_flows -= fcnt; 939 q->stat_gc_flows += fcnt; 940 } 941 942 static void fq_free(void *addr) 943 { 944 kvfree(addr); 945 } 946 947 static int fq_resize(struct Qdisc *sch, u32 log) 948 { 949 struct fq_sched_data *q = qdisc_priv(sch); 950 struct rb_root *array; 951 void *old_fq_root; 952 u32 idx; 953 954 if (q->fq_root && log == q->fq_trees_log) 955 return 0; 956 957 /* If XPS was setup, we can allocate memory on right NUMA node */ 958 array = kvmalloc_node(sizeof(struct rb_root) << log, GFP_KERNEL | __GFP_RETRY_MAYFAIL, 959 netdev_queue_numa_node_read(sch->dev_queue)); 960 if (!array) 961 return -ENOMEM; 962 963 for (idx = 0; idx < (1U << log); idx++) 964 array[idx] = RB_ROOT; 965 966 sch_tree_lock(sch); 967 968 old_fq_root = q->fq_root; 969 if (old_fq_root) 970 fq_rehash(q, old_fq_root, q->fq_trees_log, array, log); 971 972 q->fq_root = array; 973 WRITE_ONCE(q->fq_trees_log, log); 974 975 sch_tree_unlock(sch); 976 977 fq_free(old_fq_root); 978 979 return 0; 980 } 981 982 static const struct netlink_range_validation iq_range = { 983 .max = INT_MAX, 984 }; 985 986 static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = { 987 [TCA_FQ_UNSPEC] = { .strict_start_type = TCA_FQ_TIMER_SLACK }, 988 989 [TCA_FQ_PLIMIT] = { .type = NLA_U32 }, 990 [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 }, 991 [TCA_FQ_QUANTUM] = { .type = NLA_U32 }, 992 [TCA_FQ_INITIAL_QUANTUM] = NLA_POLICY_FULL_RANGE(NLA_U32, &iq_range), 993 [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 }, 994 [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 }, 995 [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 }, 996 [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 }, 997 [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 }, 998 [TCA_FQ_ORPHAN_MASK] = { .type = NLA_U32 }, 999 [TCA_FQ_LOW_RATE_THRESHOLD] = { .type = NLA_U32 }, 1000 [TCA_FQ_CE_THRESHOLD] = { .type = NLA_U32 }, 1001 [TCA_FQ_TIMER_SLACK] = { .type = NLA_U32 }, 1002 [TCA_FQ_HORIZON] = { .type = NLA_U32 }, 1003 [TCA_FQ_HORIZON_DROP] = { .type = NLA_U8 }, 1004 [TCA_FQ_PRIOMAP] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_prio_qopt)), 1005 [TCA_FQ_WEIGHTS] = NLA_POLICY_EXACT_LEN(FQ_BANDS * sizeof(s32)), 1006 [TCA_FQ_OFFLOAD_HORIZON] = { .type = NLA_U32 }, 1007 }; 1008 1009 /* compress a u8 array with all elems <= 3 to an array of 2-bit fields */ 1010 static void fq_prio2band_compress_crumb(const u8 *in, u8 *out) 1011 { 1012 const int num_elems = TC_PRIO_MAX + 1; 1013 u8 tmp[FQ_PRIO2BAND_CRUMB_SIZE]; 1014 int i; 1015 1016 memset(tmp, 0, sizeof(tmp)); 1017 for (i = 0; i < num_elems; i++) 1018 tmp[i / 4] |= in[i] << (2 * (i & 0x3)); 1019 1020 for (i = 0; i < FQ_PRIO2BAND_CRUMB_SIZE; i++) 1021 WRITE_ONCE(out[i], tmp[i]); 1022 } 1023 1024 static void fq_prio2band_decompress_crumb(const u8 *in, u8 *out) 1025 { 1026 const int num_elems = TC_PRIO_MAX + 1; 1027 int i; 1028 1029 for (i = 0; i < num_elems; i++) 1030 out[i] = fq_prio2band(in, i); 1031 } 1032 1033 static int fq_load_weights(struct fq_sched_data *q, 1034 const struct nlattr *attr, 1035 struct netlink_ext_ack *extack) 1036 { 1037 s32 *weights = nla_data(attr); 1038 int i; 1039 1040 for (i = 0; i < FQ_BANDS; i++) { 1041 if (weights[i] < FQ_MIN_WEIGHT) { 1042 NL_SET_ERR_MSG_FMT_MOD(extack, "Weight %d less that minimum allowed %d", 1043 weights[i], FQ_MIN_WEIGHT); 1044 return -EINVAL; 1045 } 1046 } 1047 for (i = 0; i < FQ_BANDS; i++) 1048 WRITE_ONCE(q->band_flows[i].quantum, weights[i]); 1049 return 0; 1050 } 1051 1052 static int fq_load_priomap(struct fq_sched_data *q, 1053 const struct nlattr *attr, 1054 struct netlink_ext_ack *extack) 1055 { 1056 const struct tc_prio_qopt *map = nla_data(attr); 1057 int i; 1058 1059 if (map->bands != FQ_BANDS) { 1060 NL_SET_ERR_MSG_MOD(extack, "FQ only supports 3 bands"); 1061 return -EINVAL; 1062 } 1063 for (i = 0; i < TC_PRIO_MAX + 1; i++) { 1064 if (map->priomap[i] >= FQ_BANDS) { 1065 NL_SET_ERR_MSG_FMT_MOD(extack, "FQ priomap field %d maps to a too high band %d", 1066 i, map->priomap[i]); 1067 return -EINVAL; 1068 } 1069 } 1070 fq_prio2band_compress_crumb(map->priomap, q->prio2band); 1071 return 0; 1072 } 1073 1074 static int fq_change(struct Qdisc *sch, struct nlattr *opt, 1075 struct netlink_ext_ack *extack) 1076 { 1077 unsigned int dropped_pkts = 0, dropped_bytes = 0; 1078 struct fq_sched_data *q = qdisc_priv(sch); 1079 struct nlattr *tb[TCA_FQ_MAX + 1]; 1080 u32 fq_log; 1081 int err; 1082 1083 err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy, 1084 NULL); 1085 if (err < 0) 1086 return err; 1087 1088 sch_tree_lock(sch); 1089 1090 fq_log = q->fq_trees_log; 1091 1092 if (tb[TCA_FQ_BUCKETS_LOG]) { 1093 u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]); 1094 1095 if (nval >= 1 && nval <= ilog2(256*1024)) 1096 fq_log = nval; 1097 else 1098 err = -EINVAL; 1099 } 1100 if (tb[TCA_FQ_PLIMIT]) 1101 WRITE_ONCE(sch->limit, 1102 nla_get_u32(tb[TCA_FQ_PLIMIT])); 1103 1104 if (tb[TCA_FQ_FLOW_PLIMIT]) 1105 WRITE_ONCE(q->flow_plimit, 1106 nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT])); 1107 1108 if (tb[TCA_FQ_QUANTUM]) { 1109 u32 quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]); 1110 1111 if (quantum > 0 && quantum <= (1 << 20)) { 1112 WRITE_ONCE(q->quantum, quantum); 1113 } else { 1114 NL_SET_ERR_MSG_MOD(extack, "invalid quantum"); 1115 err = -EINVAL; 1116 } 1117 } 1118 1119 if (tb[TCA_FQ_INITIAL_QUANTUM]) 1120 WRITE_ONCE(q->initial_quantum, 1121 nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM])); 1122 1123 if (tb[TCA_FQ_FLOW_DEFAULT_RATE]) 1124 pr_warn_ratelimited("sch_fq: defrate %u ignored.\n", 1125 nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE])); 1126 1127 if (tb[TCA_FQ_FLOW_MAX_RATE]) { 1128 u32 rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]); 1129 1130 WRITE_ONCE(q->flow_max_rate, 1131 (rate == ~0U) ? ~0UL : rate); 1132 } 1133 if (tb[TCA_FQ_LOW_RATE_THRESHOLD]) 1134 WRITE_ONCE(q->low_rate_threshold, 1135 nla_get_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD])); 1136 1137 if (tb[TCA_FQ_RATE_ENABLE]) { 1138 u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]); 1139 1140 if (enable <= 1) 1141 WRITE_ONCE(q->rate_enable, 1142 enable); 1143 else 1144 err = -EINVAL; 1145 } 1146 1147 if (tb[TCA_FQ_FLOW_REFILL_DELAY]) { 1148 u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ; 1149 1150 WRITE_ONCE(q->flow_refill_delay, 1151 usecs_to_jiffies(usecs_delay)); 1152 } 1153 1154 if (!err && tb[TCA_FQ_PRIOMAP]) 1155 err = fq_load_priomap(q, tb[TCA_FQ_PRIOMAP], extack); 1156 1157 if (!err && tb[TCA_FQ_WEIGHTS]) 1158 err = fq_load_weights(q, tb[TCA_FQ_WEIGHTS], extack); 1159 1160 if (tb[TCA_FQ_ORPHAN_MASK]) 1161 WRITE_ONCE(q->orphan_mask, 1162 nla_get_u32(tb[TCA_FQ_ORPHAN_MASK])); 1163 1164 if (tb[TCA_FQ_CE_THRESHOLD]) 1165 WRITE_ONCE(q->ce_threshold, 1166 (u64)NSEC_PER_USEC * 1167 nla_get_u32(tb[TCA_FQ_CE_THRESHOLD])); 1168 1169 if (tb[TCA_FQ_TIMER_SLACK]) 1170 WRITE_ONCE(q->timer_slack, 1171 nla_get_u32(tb[TCA_FQ_TIMER_SLACK])); 1172 1173 if (tb[TCA_FQ_HORIZON]) 1174 WRITE_ONCE(q->horizon, 1175 (u64)NSEC_PER_USEC * 1176 nla_get_u32(tb[TCA_FQ_HORIZON])); 1177 1178 if (tb[TCA_FQ_HORIZON_DROP]) 1179 WRITE_ONCE(q->horizon_drop, 1180 nla_get_u8(tb[TCA_FQ_HORIZON_DROP])); 1181 1182 if (tb[TCA_FQ_OFFLOAD_HORIZON]) { 1183 u64 offload_horizon = (u64)NSEC_PER_USEC * 1184 nla_get_u32(tb[TCA_FQ_OFFLOAD_HORIZON]); 1185 1186 if (offload_horizon <= qdisc_dev(sch)->max_pacing_offload_horizon) { 1187 WRITE_ONCE(q->offload_horizon, offload_horizon); 1188 } else { 1189 NL_SET_ERR_MSG_MOD(extack, "invalid offload_horizon"); 1190 err = -EINVAL; 1191 } 1192 } 1193 if (!err) { 1194 1195 sch_tree_unlock(sch); 1196 err = fq_resize(sch, fq_log); 1197 sch_tree_lock(sch); 1198 } 1199 1200 while (sch->q.qlen > sch->limit) { 1201 struct sk_buff *skb = qdisc_dequeue_internal(sch, false); 1202 1203 if (!skb) 1204 break; 1205 1206 dropped_pkts++; 1207 dropped_bytes += qdisc_pkt_len(skb); 1208 rtnl_kfree_skbs(skb, skb); 1209 } 1210 qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes); 1211 1212 sch_tree_unlock(sch); 1213 return err; 1214 } 1215 1216 static void fq_destroy(struct Qdisc *sch) 1217 { 1218 struct fq_sched_data *q = qdisc_priv(sch); 1219 1220 fq_reset(sch); 1221 fq_free(q->fq_root); 1222 qdisc_watchdog_cancel(&q->watchdog); 1223 } 1224 1225 static int fq_init(struct Qdisc *sch, struct nlattr *opt, 1226 struct netlink_ext_ack *extack) 1227 { 1228 struct fq_sched_data *q = qdisc_priv(sch); 1229 u32 mtu; 1230 int i, err; 1231 1232 sch->limit = 10000; 1233 q->flow_plimit = 100; 1234 mtu = clamp_t(u32, psched_mtu(qdisc_dev(sch)), 1, 1 << 20); 1235 q->quantum = min_t(u32, 2 * mtu, 1 << 20); 1236 q->initial_quantum = min_t(u32, 10 * mtu, 1 << 20); 1237 q->flow_refill_delay = msecs_to_jiffies(40); 1238 q->flow_max_rate = ~0UL; 1239 q->time_next_delayed_flow = ~0ULL; 1240 q->rate_enable = 1; 1241 for (i = 0; i < FQ_BANDS; i++) { 1242 q->band_flows[i].new_flows.first = NULL; 1243 q->band_flows[i].old_flows.first = NULL; 1244 } 1245 q->band_flows[0].quantum = 9 << 16; 1246 q->band_flows[1].quantum = 3 << 16; 1247 q->band_flows[2].quantum = 1 << 16; 1248 q->delayed = RB_ROOT; 1249 q->fq_root = NULL; 1250 q->fq_trees_log = ilog2(1024); 1251 q->orphan_mask = 1024 - 1; 1252 q->low_rate_threshold = 550000 / 8; 1253 1254 q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */ 1255 1256 q->horizon = 10ULL * NSEC_PER_SEC; /* 10 seconds */ 1257 q->horizon_drop = 1; /* by default, drop packets beyond horizon */ 1258 1259 /* Default ce_threshold of 4294 seconds */ 1260 q->ce_threshold = (u64)NSEC_PER_USEC * ~0U; 1261 1262 fq_prio2band_compress_crumb(sch_default_prio2band, q->prio2band); 1263 qdisc_watchdog_init_clockid(&q->watchdog, sch, CLOCK_MONOTONIC); 1264 1265 if (opt) 1266 err = fq_change(sch, opt, extack); 1267 else 1268 err = fq_resize(sch, q->fq_trees_log); 1269 1270 return err; 1271 } 1272 1273 static int fq_dump(struct Qdisc *sch, struct sk_buff *skb) 1274 { 1275 struct fq_sched_data *q = qdisc_priv(sch); 1276 struct tc_prio_qopt prio = { 1277 .bands = FQ_BANDS, 1278 }; 1279 struct nlattr *opts; 1280 u64 offload_horizon; 1281 u64 ce_threshold; 1282 s32 weights[3]; 1283 u64 horizon; 1284 1285 opts = nla_nest_start_noflag(skb, TCA_OPTIONS); 1286 if (opts == NULL) 1287 goto nla_put_failure; 1288 1289 /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */ 1290 1291 ce_threshold = READ_ONCE(q->ce_threshold); 1292 do_div(ce_threshold, NSEC_PER_USEC); 1293 1294 horizon = READ_ONCE(q->horizon); 1295 do_div(horizon, NSEC_PER_USEC); 1296 1297 offload_horizon = READ_ONCE(q->offload_horizon); 1298 do_div(offload_horizon, NSEC_PER_USEC); 1299 1300 if (nla_put_u32(skb, TCA_FQ_PLIMIT, 1301 READ_ONCE(sch->limit)) || 1302 nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, 1303 READ_ONCE(q->flow_plimit)) || 1304 nla_put_u32(skb, TCA_FQ_QUANTUM, 1305 READ_ONCE(q->quantum)) || 1306 nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, 1307 READ_ONCE(q->initial_quantum)) || 1308 nla_put_u32(skb, TCA_FQ_RATE_ENABLE, 1309 READ_ONCE(q->rate_enable)) || 1310 nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE, 1311 min_t(unsigned long, 1312 READ_ONCE(q->flow_max_rate), ~0U)) || 1313 nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY, 1314 jiffies_to_usecs(READ_ONCE(q->flow_refill_delay))) || 1315 nla_put_u32(skb, TCA_FQ_ORPHAN_MASK, 1316 READ_ONCE(q->orphan_mask)) || 1317 nla_put_u32(skb, TCA_FQ_LOW_RATE_THRESHOLD, 1318 READ_ONCE(q->low_rate_threshold)) || 1319 nla_put_u32(skb, TCA_FQ_CE_THRESHOLD, (u32)ce_threshold) || 1320 nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, 1321 READ_ONCE(q->fq_trees_log)) || 1322 nla_put_u32(skb, TCA_FQ_TIMER_SLACK, 1323 READ_ONCE(q->timer_slack)) || 1324 nla_put_u32(skb, TCA_FQ_HORIZON, (u32)horizon) || 1325 nla_put_u32(skb, TCA_FQ_OFFLOAD_HORIZON, (u32)offload_horizon) || 1326 nla_put_u8(skb, TCA_FQ_HORIZON_DROP, 1327 READ_ONCE(q->horizon_drop))) 1328 goto nla_put_failure; 1329 1330 fq_prio2band_decompress_crumb(q->prio2band, prio.priomap); 1331 if (nla_put(skb, TCA_FQ_PRIOMAP, sizeof(prio), &prio)) 1332 goto nla_put_failure; 1333 1334 weights[0] = READ_ONCE(q->band_flows[0].quantum); 1335 weights[1] = READ_ONCE(q->band_flows[1].quantum); 1336 weights[2] = READ_ONCE(q->band_flows[2].quantum); 1337 if (nla_put(skb, TCA_FQ_WEIGHTS, sizeof(weights), &weights)) 1338 goto nla_put_failure; 1339 1340 return nla_nest_end(skb, opts); 1341 1342 nla_put_failure: 1343 return -1; 1344 } 1345 1346 static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d) 1347 { 1348 struct fq_sched_data *q = qdisc_priv(sch); 1349 struct tc_fq_qd_stats st; 1350 int i; 1351 1352 st.pad = 0; 1353 1354 sch_tree_lock(sch); 1355 1356 st.gc_flows = q->stat_gc_flows; 1357 st.highprio_packets = 0; 1358 st.fastpath_packets = q->internal.stat_fastpath_packets; 1359 st.tcp_retrans = 0; 1360 st.throttled = q->stat_throttled; 1361 st.flows_plimit = q->stat_flows_plimit; 1362 st.pkts_too_long = q->stat_pkts_too_long; 1363 st.allocation_errors = q->stat_allocation_errors; 1364 st.time_next_delayed_flow = q->time_next_delayed_flow + q->timer_slack - 1365 ktime_get_ns(); 1366 st.flows = q->flows; 1367 st.inactive_flows = q->inactive_flows; 1368 st.throttled_flows = q->throttled_flows; 1369 st.unthrottle_latency_ns = min_t(unsigned long, 1370 q->unthrottle_latency_ns, ~0U); 1371 st.ce_mark = q->stat_ce_mark; 1372 st.horizon_drops = q->stat_horizon_drops; 1373 st.horizon_caps = q->stat_horizon_caps; 1374 for (i = 0; i < FQ_BANDS; i++) { 1375 st.band_drops[i] = q->stat_band_drops[i]; 1376 st.band_pkt_count[i] = q->band_pkt_count[i]; 1377 } 1378 sch_tree_unlock(sch); 1379 1380 return gnet_stats_copy_app(d, &st, sizeof(st)); 1381 } 1382 1383 static struct Qdisc_ops fq_qdisc_ops __read_mostly = { 1384 .id = "fq", 1385 .priv_size = sizeof(struct fq_sched_data), 1386 1387 .enqueue = fq_enqueue, 1388 .dequeue = fq_dequeue, 1389 .peek = qdisc_peek_dequeued, 1390 .init = fq_init, 1391 .reset = fq_reset, 1392 .destroy = fq_destroy, 1393 .change = fq_change, 1394 .dump = fq_dump, 1395 .dump_stats = fq_dump_stats, 1396 .owner = THIS_MODULE, 1397 }; 1398 MODULE_ALIAS_NET_SCH("fq"); 1399 1400 static int __init fq_module_init(void) 1401 { 1402 int ret; 1403 1404 fq_flow_cachep = kmem_cache_create("fq_flow_cache", 1405 sizeof(struct fq_flow), 1406 0, SLAB_HWCACHE_ALIGN, NULL); 1407 if (!fq_flow_cachep) 1408 return -ENOMEM; 1409 1410 ret = register_qdisc(&fq_qdisc_ops); 1411 if (ret) 1412 kmem_cache_destroy(fq_flow_cachep); 1413 return ret; 1414 } 1415 1416 static void __exit fq_module_exit(void) 1417 { 1418 unregister_qdisc(&fq_qdisc_ops); 1419 kmem_cache_destroy(fq_flow_cachep); 1420 } 1421 1422 module_init(fq_module_init) 1423 module_exit(fq_module_exit) 1424 MODULE_AUTHOR("Eric Dumazet"); 1425 MODULE_LICENSE("GPL"); 1426 MODULE_DESCRIPTION("Fair Queue Packet Scheduler"); 1427