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