1 // SPDX-License-Identifier: GPL-2.0 2 3 /* net/sched/sch_taprio.c Time Aware Priority Scheduler 4 * 5 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> 6 * 7 */ 8 9 #include <linux/ethtool.h> 10 #include <linux/types.h> 11 #include <linux/slab.h> 12 #include <linux/kernel.h> 13 #include <linux/string.h> 14 #include <linux/list.h> 15 #include <linux/errno.h> 16 #include <linux/skbuff.h> 17 #include <linux/math64.h> 18 #include <linux/module.h> 19 #include <linux/spinlock.h> 20 #include <linux/rcupdate.h> 21 #include <net/netlink.h> 22 #include <net/pkt_sched.h> 23 #include <net/pkt_cls.h> 24 #include <net/sch_generic.h> 25 #include <net/sock.h> 26 #include <net/tcp.h> 27 28 static LIST_HEAD(taprio_list); 29 static DEFINE_SPINLOCK(taprio_list_lock); 30 31 #define TAPRIO_ALL_GATES_OPEN -1 32 33 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 34 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 35 #define TAPRIO_FLAGS_INVALID U32_MAX 36 37 struct sched_entry { 38 struct list_head list; 39 40 /* The instant that this entry "closes" and the next one 41 * should open, the qdisc will make some effort so that no 42 * packet leaves after this time. 43 */ 44 ktime_t close_time; 45 ktime_t next_txtime; 46 atomic_t budget; 47 int index; 48 u32 gate_mask; 49 u32 interval; 50 u8 command; 51 }; 52 53 struct sched_gate_list { 54 struct rcu_head rcu; 55 struct list_head entries; 56 size_t num_entries; 57 ktime_t cycle_close_time; 58 s64 cycle_time; 59 s64 cycle_time_extension; 60 s64 base_time; 61 }; 62 63 struct taprio_sched { 64 struct Qdisc **qdiscs; 65 struct Qdisc *root; 66 u32 flags; 67 enum tk_offsets tk_offset; 68 int clockid; 69 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 70 * speeds it's sub-nanoseconds per byte 71 */ 72 73 /* Protects the update side of the RCU protected current_entry */ 74 spinlock_t current_entry_lock; 75 struct sched_entry __rcu *current_entry; 76 struct sched_gate_list __rcu *oper_sched; 77 struct sched_gate_list __rcu *admin_sched; 78 struct hrtimer advance_timer; 79 struct list_head taprio_list; 80 struct sk_buff *(*dequeue)(struct Qdisc *sch); 81 struct sk_buff *(*peek)(struct Qdisc *sch); 82 u32 txtime_delay; 83 }; 84 85 struct __tc_taprio_qopt_offload { 86 refcount_t users; 87 struct tc_taprio_qopt_offload offload; 88 }; 89 90 static ktime_t sched_base_time(const struct sched_gate_list *sched) 91 { 92 if (!sched) 93 return KTIME_MAX; 94 95 return ns_to_ktime(sched->base_time); 96 } 97 98 static ktime_t taprio_get_time(struct taprio_sched *q) 99 { 100 ktime_t mono = ktime_get(); 101 102 switch (q->tk_offset) { 103 case TK_OFFS_MAX: 104 return mono; 105 default: 106 return ktime_mono_to_any(mono, q->tk_offset); 107 } 108 109 return KTIME_MAX; 110 } 111 112 static void taprio_free_sched_cb(struct rcu_head *head) 113 { 114 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 115 struct sched_entry *entry, *n; 116 117 if (!sched) 118 return; 119 120 list_for_each_entry_safe(entry, n, &sched->entries, list) { 121 list_del(&entry->list); 122 kfree(entry); 123 } 124 125 kfree(sched); 126 } 127 128 static void switch_schedules(struct taprio_sched *q, 129 struct sched_gate_list **admin, 130 struct sched_gate_list **oper) 131 { 132 rcu_assign_pointer(q->oper_sched, *admin); 133 rcu_assign_pointer(q->admin_sched, NULL); 134 135 if (*oper) 136 call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 137 138 *oper = *admin; 139 *admin = NULL; 140 } 141 142 /* Get how much time has been already elapsed in the current cycle. */ 143 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 144 { 145 ktime_t time_since_sched_start; 146 s32 time_elapsed; 147 148 time_since_sched_start = ktime_sub(time, sched->base_time); 149 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 150 151 return time_elapsed; 152 } 153 154 static ktime_t get_interval_end_time(struct sched_gate_list *sched, 155 struct sched_gate_list *admin, 156 struct sched_entry *entry, 157 ktime_t intv_start) 158 { 159 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 160 ktime_t intv_end, cycle_ext_end, cycle_end; 161 162 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 163 intv_end = ktime_add_ns(intv_start, entry->interval); 164 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 165 166 if (ktime_before(intv_end, cycle_end)) 167 return intv_end; 168 else if (admin && admin != sched && 169 ktime_after(admin->base_time, cycle_end) && 170 ktime_before(admin->base_time, cycle_ext_end)) 171 return admin->base_time; 172 else 173 return cycle_end; 174 } 175 176 static int length_to_duration(struct taprio_sched *q, int len) 177 { 178 return div_u64(len * atomic64_read(&q->picos_per_byte), 1000); 179 } 180 181 /* Returns the entry corresponding to next available interval. If 182 * validate_interval is set, it only validates whether the timestamp occurs 183 * when the gate corresponding to the skb's traffic class is open. 184 */ 185 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 186 struct Qdisc *sch, 187 struct sched_gate_list *sched, 188 struct sched_gate_list *admin, 189 ktime_t time, 190 ktime_t *interval_start, 191 ktime_t *interval_end, 192 bool validate_interval) 193 { 194 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 195 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 196 struct sched_entry *entry = NULL, *entry_found = NULL; 197 struct taprio_sched *q = qdisc_priv(sch); 198 struct net_device *dev = qdisc_dev(sch); 199 bool entry_available = false; 200 s32 cycle_elapsed; 201 int tc, n; 202 203 tc = netdev_get_prio_tc_map(dev, skb->priority); 204 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 205 206 *interval_start = 0; 207 *interval_end = 0; 208 209 if (!sched) 210 return NULL; 211 212 cycle = sched->cycle_time; 213 cycle_elapsed = get_cycle_time_elapsed(sched, time); 214 curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 215 cycle_end = ktime_add_ns(curr_intv_end, cycle); 216 217 list_for_each_entry(entry, &sched->entries, list) { 218 curr_intv_start = curr_intv_end; 219 curr_intv_end = get_interval_end_time(sched, admin, entry, 220 curr_intv_start); 221 222 if (ktime_after(curr_intv_start, cycle_end)) 223 break; 224 225 if (!(entry->gate_mask & BIT(tc)) || 226 packet_transmit_time > entry->interval) 227 continue; 228 229 txtime = entry->next_txtime; 230 231 if (ktime_before(txtime, time) || validate_interval) { 232 transmit_end_time = ktime_add_ns(time, packet_transmit_time); 233 if ((ktime_before(curr_intv_start, time) && 234 ktime_before(transmit_end_time, curr_intv_end)) || 235 (ktime_after(curr_intv_start, time) && !validate_interval)) { 236 entry_found = entry; 237 *interval_start = curr_intv_start; 238 *interval_end = curr_intv_end; 239 break; 240 } else if (!entry_available && !validate_interval) { 241 /* Here, we are just trying to find out the 242 * first available interval in the next cycle. 243 */ 244 entry_available = true; 245 entry_found = entry; 246 *interval_start = ktime_add_ns(curr_intv_start, cycle); 247 *interval_end = ktime_add_ns(curr_intv_end, cycle); 248 } 249 } else if (ktime_before(txtime, earliest_txtime) && 250 !entry_available) { 251 earliest_txtime = txtime; 252 entry_found = entry; 253 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 254 *interval_start = ktime_add(curr_intv_start, n * cycle); 255 *interval_end = ktime_add(curr_intv_end, n * cycle); 256 } 257 } 258 259 return entry_found; 260 } 261 262 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 263 { 264 struct taprio_sched *q = qdisc_priv(sch); 265 struct sched_gate_list *sched, *admin; 266 ktime_t interval_start, interval_end; 267 struct sched_entry *entry; 268 269 rcu_read_lock(); 270 sched = rcu_dereference(q->oper_sched); 271 admin = rcu_dereference(q->admin_sched); 272 273 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 274 &interval_start, &interval_end, true); 275 rcu_read_unlock(); 276 277 return entry; 278 } 279 280 static bool taprio_flags_valid(u32 flags) 281 { 282 /* Make sure no other flag bits are set. */ 283 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | 284 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 285 return false; 286 /* txtime-assist and full offload are mutually exclusive */ 287 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) && 288 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) 289 return false; 290 return true; 291 } 292 293 /* This returns the tstamp value set by TCP in terms of the set clock. */ 294 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 295 { 296 unsigned int offset = skb_network_offset(skb); 297 const struct ipv6hdr *ipv6h; 298 const struct iphdr *iph; 299 struct ipv6hdr _ipv6h; 300 301 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 302 if (!ipv6h) 303 return 0; 304 305 if (ipv6h->version == 4) { 306 iph = (struct iphdr *)ipv6h; 307 offset += iph->ihl * 4; 308 309 /* special-case 6in4 tunnelling, as that is a common way to get 310 * v6 connectivity in the home 311 */ 312 if (iph->protocol == IPPROTO_IPV6) { 313 ipv6h = skb_header_pointer(skb, offset, 314 sizeof(_ipv6h), &_ipv6h); 315 316 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 317 return 0; 318 } else if (iph->protocol != IPPROTO_TCP) { 319 return 0; 320 } 321 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 322 return 0; 323 } 324 325 return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset); 326 } 327 328 /* There are a few scenarios where we will have to modify the txtime from 329 * what is read from next_txtime in sched_entry. They are: 330 * 1. If txtime is in the past, 331 * a. The gate for the traffic class is currently open and packet can be 332 * transmitted before it closes, schedule the packet right away. 333 * b. If the gate corresponding to the traffic class is going to open later 334 * in the cycle, set the txtime of packet to the interval start. 335 * 2. If txtime is in the future, there are packets corresponding to the 336 * current traffic class waiting to be transmitted. So, the following 337 * possibilities exist: 338 * a. We can transmit the packet before the window containing the txtime 339 * closes. 340 * b. The window might close before the transmission can be completed 341 * successfully. So, schedule the packet in the next open window. 342 */ 343 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 344 { 345 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 346 struct taprio_sched *q = qdisc_priv(sch); 347 struct sched_gate_list *sched, *admin; 348 ktime_t minimum_time, now, txtime; 349 int len, packet_transmit_time; 350 struct sched_entry *entry; 351 bool sched_changed; 352 353 now = taprio_get_time(q); 354 minimum_time = ktime_add_ns(now, q->txtime_delay); 355 356 tcp_tstamp = get_tcp_tstamp(q, skb); 357 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 358 359 rcu_read_lock(); 360 admin = rcu_dereference(q->admin_sched); 361 sched = rcu_dereference(q->oper_sched); 362 if (admin && ktime_after(minimum_time, admin->base_time)) 363 switch_schedules(q, &admin, &sched); 364 365 /* Until the schedule starts, all the queues are open */ 366 if (!sched || ktime_before(minimum_time, sched->base_time)) { 367 txtime = minimum_time; 368 goto done; 369 } 370 371 len = qdisc_pkt_len(skb); 372 packet_transmit_time = length_to_duration(q, len); 373 374 do { 375 sched_changed = false; 376 377 entry = find_entry_to_transmit(skb, sch, sched, admin, 378 minimum_time, 379 &interval_start, &interval_end, 380 false); 381 if (!entry) { 382 txtime = 0; 383 goto done; 384 } 385 386 txtime = entry->next_txtime; 387 txtime = max_t(ktime_t, txtime, minimum_time); 388 txtime = max_t(ktime_t, txtime, interval_start); 389 390 if (admin && admin != sched && 391 ktime_after(txtime, admin->base_time)) { 392 sched = admin; 393 sched_changed = true; 394 continue; 395 } 396 397 transmit_end_time = ktime_add(txtime, packet_transmit_time); 398 minimum_time = transmit_end_time; 399 400 /* Update the txtime of current entry to the next time it's 401 * interval starts. 402 */ 403 if (ktime_after(transmit_end_time, interval_end)) 404 entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 405 } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 406 407 entry->next_txtime = transmit_end_time; 408 409 done: 410 rcu_read_unlock(); 411 return txtime; 412 } 413 414 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 415 struct sk_buff **to_free) 416 { 417 struct taprio_sched *q = qdisc_priv(sch); 418 struct Qdisc *child; 419 int queue; 420 421 queue = skb_get_queue_mapping(skb); 422 423 child = q->qdiscs[queue]; 424 if (unlikely(!child)) 425 return qdisc_drop(skb, sch, to_free); 426 427 if (skb->sk && sock_flag(skb->sk, SOCK_TXTIME)) { 428 if (!is_valid_interval(skb, sch)) 429 return qdisc_drop(skb, sch, to_free); 430 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 431 skb->tstamp = get_packet_txtime(skb, sch); 432 if (!skb->tstamp) 433 return qdisc_drop(skb, sch, to_free); 434 } 435 436 qdisc_qstats_backlog_inc(sch, skb); 437 sch->q.qlen++; 438 439 return qdisc_enqueue(skb, child, to_free); 440 } 441 442 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch) 443 { 444 struct taprio_sched *q = qdisc_priv(sch); 445 struct net_device *dev = qdisc_dev(sch); 446 struct sched_entry *entry; 447 struct sk_buff *skb; 448 u32 gate_mask; 449 int i; 450 451 rcu_read_lock(); 452 entry = rcu_dereference(q->current_entry); 453 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 454 rcu_read_unlock(); 455 456 if (!gate_mask) 457 return NULL; 458 459 for (i = 0; i < dev->num_tx_queues; i++) { 460 struct Qdisc *child = q->qdiscs[i]; 461 int prio; 462 u8 tc; 463 464 if (unlikely(!child)) 465 continue; 466 467 skb = child->ops->peek(child); 468 if (!skb) 469 continue; 470 471 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 472 return skb; 473 474 prio = skb->priority; 475 tc = netdev_get_prio_tc_map(dev, prio); 476 477 if (!(gate_mask & BIT(tc))) 478 continue; 479 480 return skb; 481 } 482 483 return NULL; 484 } 485 486 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch) 487 { 488 struct taprio_sched *q = qdisc_priv(sch); 489 struct net_device *dev = qdisc_dev(sch); 490 struct sk_buff *skb; 491 int i; 492 493 for (i = 0; i < dev->num_tx_queues; i++) { 494 struct Qdisc *child = q->qdiscs[i]; 495 496 if (unlikely(!child)) 497 continue; 498 499 skb = child->ops->peek(child); 500 if (!skb) 501 continue; 502 503 return skb; 504 } 505 506 return NULL; 507 } 508 509 static struct sk_buff *taprio_peek(struct Qdisc *sch) 510 { 511 struct taprio_sched *q = qdisc_priv(sch); 512 513 return q->peek(sch); 514 } 515 516 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry) 517 { 518 atomic_set(&entry->budget, 519 div64_u64((u64)entry->interval * 1000, 520 atomic64_read(&q->picos_per_byte))); 521 } 522 523 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch) 524 { 525 struct taprio_sched *q = qdisc_priv(sch); 526 struct net_device *dev = qdisc_dev(sch); 527 struct sk_buff *skb = NULL; 528 struct sched_entry *entry; 529 u32 gate_mask; 530 int i; 531 532 rcu_read_lock(); 533 entry = rcu_dereference(q->current_entry); 534 /* if there's no entry, it means that the schedule didn't 535 * start yet, so force all gates to be open, this is in 536 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 537 * "AdminGateSates" 538 */ 539 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 540 541 if (!gate_mask) 542 goto done; 543 544 for (i = 0; i < dev->num_tx_queues; i++) { 545 struct Qdisc *child = q->qdiscs[i]; 546 ktime_t guard; 547 int prio; 548 int len; 549 u8 tc; 550 551 if (unlikely(!child)) 552 continue; 553 554 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 555 skb = child->ops->dequeue(child); 556 if (!skb) 557 continue; 558 goto skb_found; 559 } 560 561 skb = child->ops->peek(child); 562 if (!skb) 563 continue; 564 565 prio = skb->priority; 566 tc = netdev_get_prio_tc_map(dev, prio); 567 568 if (!(gate_mask & BIT(tc))) { 569 skb = NULL; 570 continue; 571 } 572 573 len = qdisc_pkt_len(skb); 574 guard = ktime_add_ns(taprio_get_time(q), 575 length_to_duration(q, len)); 576 577 /* In the case that there's no gate entry, there's no 578 * guard band ... 579 */ 580 if (gate_mask != TAPRIO_ALL_GATES_OPEN && 581 ktime_after(guard, entry->close_time)) { 582 skb = NULL; 583 continue; 584 } 585 586 /* ... and no budget. */ 587 if (gate_mask != TAPRIO_ALL_GATES_OPEN && 588 atomic_sub_return(len, &entry->budget) < 0) { 589 skb = NULL; 590 continue; 591 } 592 593 skb = child->ops->dequeue(child); 594 if (unlikely(!skb)) 595 goto done; 596 597 skb_found: 598 qdisc_bstats_update(sch, skb); 599 qdisc_qstats_backlog_dec(sch, skb); 600 sch->q.qlen--; 601 602 goto done; 603 } 604 605 done: 606 rcu_read_unlock(); 607 608 return skb; 609 } 610 611 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch) 612 { 613 struct taprio_sched *q = qdisc_priv(sch); 614 struct net_device *dev = qdisc_dev(sch); 615 struct sk_buff *skb; 616 int i; 617 618 for (i = 0; i < dev->num_tx_queues; i++) { 619 struct Qdisc *child = q->qdiscs[i]; 620 621 if (unlikely(!child)) 622 continue; 623 624 skb = child->ops->dequeue(child); 625 if (unlikely(!skb)) 626 continue; 627 628 qdisc_bstats_update(sch, skb); 629 qdisc_qstats_backlog_dec(sch, skb); 630 sch->q.qlen--; 631 632 return skb; 633 } 634 635 return NULL; 636 } 637 638 static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 639 { 640 struct taprio_sched *q = qdisc_priv(sch); 641 642 return q->dequeue(sch); 643 } 644 645 static bool should_restart_cycle(const struct sched_gate_list *oper, 646 const struct sched_entry *entry) 647 { 648 if (list_is_last(&entry->list, &oper->entries)) 649 return true; 650 651 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0) 652 return true; 653 654 return false; 655 } 656 657 static bool should_change_schedules(const struct sched_gate_list *admin, 658 const struct sched_gate_list *oper, 659 ktime_t close_time) 660 { 661 ktime_t next_base_time, extension_time; 662 663 if (!admin) 664 return false; 665 666 next_base_time = sched_base_time(admin); 667 668 /* This is the simple case, the close_time would fall after 669 * the next schedule base_time. 670 */ 671 if (ktime_compare(next_base_time, close_time) <= 0) 672 return true; 673 674 /* This is the cycle_time_extension case, if the close_time 675 * plus the amount that can be extended would fall after the 676 * next schedule base_time, we can extend the current schedule 677 * for that amount. 678 */ 679 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension); 680 681 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 682 * how precisely the extension should be made. So after 683 * conformance testing, this logic may change. 684 */ 685 if (ktime_compare(next_base_time, extension_time) <= 0) 686 return true; 687 688 return false; 689 } 690 691 static enum hrtimer_restart advance_sched(struct hrtimer *timer) 692 { 693 struct taprio_sched *q = container_of(timer, struct taprio_sched, 694 advance_timer); 695 struct sched_gate_list *oper, *admin; 696 struct sched_entry *entry, *next; 697 struct Qdisc *sch = q->root; 698 ktime_t close_time; 699 700 spin_lock(&q->current_entry_lock); 701 entry = rcu_dereference_protected(q->current_entry, 702 lockdep_is_held(&q->current_entry_lock)); 703 oper = rcu_dereference_protected(q->oper_sched, 704 lockdep_is_held(&q->current_entry_lock)); 705 admin = rcu_dereference_protected(q->admin_sched, 706 lockdep_is_held(&q->current_entry_lock)); 707 708 if (!oper) 709 switch_schedules(q, &admin, &oper); 710 711 /* This can happen in two cases: 1. this is the very first run 712 * of this function (i.e. we weren't running any schedule 713 * previously); 2. The previous schedule just ended. The first 714 * entry of all schedules are pre-calculated during the 715 * schedule initialization. 716 */ 717 if (unlikely(!entry || entry->close_time == oper->base_time)) { 718 next = list_first_entry(&oper->entries, struct sched_entry, 719 list); 720 close_time = next->close_time; 721 goto first_run; 722 } 723 724 if (should_restart_cycle(oper, entry)) { 725 next = list_first_entry(&oper->entries, struct sched_entry, 726 list); 727 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time, 728 oper->cycle_time); 729 } else { 730 next = list_next_entry(entry, list); 731 } 732 733 close_time = ktime_add_ns(entry->close_time, next->interval); 734 close_time = min_t(ktime_t, close_time, oper->cycle_close_time); 735 736 if (should_change_schedules(admin, oper, close_time)) { 737 /* Set things so the next time this runs, the new 738 * schedule runs. 739 */ 740 close_time = sched_base_time(admin); 741 switch_schedules(q, &admin, &oper); 742 } 743 744 next->close_time = close_time; 745 taprio_set_budget(q, next); 746 747 first_run: 748 rcu_assign_pointer(q->current_entry, next); 749 spin_unlock(&q->current_entry_lock); 750 751 hrtimer_set_expires(&q->advance_timer, close_time); 752 753 rcu_read_lock(); 754 __netif_schedule(sch); 755 rcu_read_unlock(); 756 757 return HRTIMER_RESTART; 758 } 759 760 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 761 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 762 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 763 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 764 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 765 }; 766 767 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 768 [TCA_TAPRIO_ATTR_PRIOMAP] = { 769 .len = sizeof(struct tc_mqprio_qopt) 770 }, 771 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 772 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 773 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 774 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 775 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 }, 776 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 777 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 }, 778 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 }, 779 }; 780 781 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb, 782 struct sched_entry *entry, 783 struct netlink_ext_ack *extack) 784 { 785 int min_duration = length_to_duration(q, ETH_ZLEN); 786 u32 interval = 0; 787 788 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 789 entry->command = nla_get_u8( 790 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 791 792 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 793 entry->gate_mask = nla_get_u32( 794 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 795 796 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 797 interval = nla_get_u32( 798 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 799 800 /* The interval should allow at least the minimum ethernet 801 * frame to go out. 802 */ 803 if (interval < min_duration) { 804 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 805 return -EINVAL; 806 } 807 808 entry->interval = interval; 809 810 return 0; 811 } 812 813 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n, 814 struct sched_entry *entry, int index, 815 struct netlink_ext_ack *extack) 816 { 817 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 818 int err; 819 820 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 821 entry_policy, NULL); 822 if (err < 0) { 823 NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 824 return -EINVAL; 825 } 826 827 entry->index = index; 828 829 return fill_sched_entry(q, tb, entry, extack); 830 } 831 832 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list, 833 struct sched_gate_list *sched, 834 struct netlink_ext_ack *extack) 835 { 836 struct nlattr *n; 837 int err, rem; 838 int i = 0; 839 840 if (!list) 841 return -EINVAL; 842 843 nla_for_each_nested(n, list, rem) { 844 struct sched_entry *entry; 845 846 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 847 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 848 continue; 849 } 850 851 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 852 if (!entry) { 853 NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 854 return -ENOMEM; 855 } 856 857 err = parse_sched_entry(q, n, entry, i, extack); 858 if (err < 0) { 859 kfree(entry); 860 return err; 861 } 862 863 list_add_tail(&entry->list, &sched->entries); 864 i++; 865 } 866 867 sched->num_entries = i; 868 869 return i; 870 } 871 872 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb, 873 struct sched_gate_list *new, 874 struct netlink_ext_ack *extack) 875 { 876 int err = 0; 877 878 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 879 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 880 return -ENOTSUPP; 881 } 882 883 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 884 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 885 886 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 887 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 888 889 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 890 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 891 892 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 893 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], 894 new, extack); 895 if (err < 0) 896 return err; 897 898 if (!new->cycle_time) { 899 struct sched_entry *entry; 900 ktime_t cycle = 0; 901 902 list_for_each_entry(entry, &new->entries, list) 903 cycle = ktime_add_ns(cycle, entry->interval); 904 905 if (!cycle) { 906 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0"); 907 return -EINVAL; 908 } 909 910 new->cycle_time = cycle; 911 } 912 913 return 0; 914 } 915 916 static int taprio_parse_mqprio_opt(struct net_device *dev, 917 struct tc_mqprio_qopt *qopt, 918 struct netlink_ext_ack *extack, 919 u32 taprio_flags) 920 { 921 int i, j; 922 923 if (!qopt && !dev->num_tc) { 924 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 925 return -EINVAL; 926 } 927 928 /* If num_tc is already set, it means that the user already 929 * configured the mqprio part 930 */ 931 if (dev->num_tc) 932 return 0; 933 934 /* Verify num_tc is not out of max range */ 935 if (qopt->num_tc > TC_MAX_QUEUE) { 936 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range"); 937 return -EINVAL; 938 } 939 940 /* taprio imposes that traffic classes map 1:n to tx queues */ 941 if (qopt->num_tc > dev->num_tx_queues) { 942 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 943 return -EINVAL; 944 } 945 946 /* Verify priority mapping uses valid tcs */ 947 for (i = 0; i <= TC_BITMASK; i++) { 948 if (qopt->prio_tc_map[i] >= qopt->num_tc) { 949 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping"); 950 return -EINVAL; 951 } 952 } 953 954 for (i = 0; i < qopt->num_tc; i++) { 955 unsigned int last = qopt->offset[i] + qopt->count[i]; 956 957 /* Verify the queue count is in tx range being equal to the 958 * real_num_tx_queues indicates the last queue is in use. 959 */ 960 if (qopt->offset[i] >= dev->num_tx_queues || 961 !qopt->count[i] || 962 last > dev->real_num_tx_queues) { 963 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping"); 964 return -EINVAL; 965 } 966 967 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags)) 968 continue; 969 970 /* Verify that the offset and counts do not overlap */ 971 for (j = i + 1; j < qopt->num_tc; j++) { 972 if (last > qopt->offset[j]) { 973 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping"); 974 return -EINVAL; 975 } 976 } 977 } 978 979 return 0; 980 } 981 982 static int taprio_get_start_time(struct Qdisc *sch, 983 struct sched_gate_list *sched, 984 ktime_t *start) 985 { 986 struct taprio_sched *q = qdisc_priv(sch); 987 ktime_t now, base, cycle; 988 s64 n; 989 990 base = sched_base_time(sched); 991 now = taprio_get_time(q); 992 993 if (ktime_after(base, now)) { 994 *start = base; 995 return 0; 996 } 997 998 cycle = sched->cycle_time; 999 1000 /* The qdisc is expected to have at least one sched_entry. Moreover, 1001 * any entry must have 'interval' > 0. Thus if the cycle time is zero, 1002 * something went really wrong. In that case, we should warn about this 1003 * inconsistent state and return error. 1004 */ 1005 if (WARN_ON(!cycle)) 1006 return -EFAULT; 1007 1008 /* Schedule the start time for the beginning of the next 1009 * cycle. 1010 */ 1011 n = div64_s64(ktime_sub_ns(now, base), cycle); 1012 *start = ktime_add_ns(base, (n + 1) * cycle); 1013 return 0; 1014 } 1015 1016 static void setup_first_close_time(struct taprio_sched *q, 1017 struct sched_gate_list *sched, ktime_t base) 1018 { 1019 struct sched_entry *first; 1020 ktime_t cycle; 1021 1022 first = list_first_entry(&sched->entries, 1023 struct sched_entry, list); 1024 1025 cycle = sched->cycle_time; 1026 1027 /* FIXME: find a better place to do this */ 1028 sched->cycle_close_time = ktime_add_ns(base, cycle); 1029 1030 first->close_time = ktime_add_ns(base, first->interval); 1031 taprio_set_budget(q, first); 1032 rcu_assign_pointer(q->current_entry, NULL); 1033 } 1034 1035 static void taprio_start_sched(struct Qdisc *sch, 1036 ktime_t start, struct sched_gate_list *new) 1037 { 1038 struct taprio_sched *q = qdisc_priv(sch); 1039 ktime_t expires; 1040 1041 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1042 return; 1043 1044 expires = hrtimer_get_expires(&q->advance_timer); 1045 if (expires == 0) 1046 expires = KTIME_MAX; 1047 1048 /* If the new schedule starts before the next expiration, we 1049 * reprogram it to the earliest one, so we change the admin 1050 * schedule to the operational one at the right time. 1051 */ 1052 start = min_t(ktime_t, start, expires); 1053 1054 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 1055 } 1056 1057 static void taprio_set_picos_per_byte(struct net_device *dev, 1058 struct taprio_sched *q) 1059 { 1060 struct ethtool_link_ksettings ecmd; 1061 int speed = SPEED_10; 1062 int picos_per_byte; 1063 int err; 1064 1065 err = __ethtool_get_link_ksettings(dev, &ecmd); 1066 if (err < 0) 1067 goto skip; 1068 1069 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN) 1070 speed = ecmd.base.speed; 1071 1072 skip: 1073 picos_per_byte = (USEC_PER_SEC * 8) / speed; 1074 1075 atomic64_set(&q->picos_per_byte, picos_per_byte); 1076 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 1077 dev->name, (long long)atomic64_read(&q->picos_per_byte), 1078 ecmd.base.speed); 1079 } 1080 1081 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 1082 void *ptr) 1083 { 1084 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1085 struct net_device *qdev; 1086 struct taprio_sched *q; 1087 bool found = false; 1088 1089 ASSERT_RTNL(); 1090 1091 if (event != NETDEV_UP && event != NETDEV_CHANGE) 1092 return NOTIFY_DONE; 1093 1094 spin_lock(&taprio_list_lock); 1095 list_for_each_entry(q, &taprio_list, taprio_list) { 1096 qdev = qdisc_dev(q->root); 1097 if (qdev == dev) { 1098 found = true; 1099 break; 1100 } 1101 } 1102 spin_unlock(&taprio_list_lock); 1103 1104 if (found) 1105 taprio_set_picos_per_byte(dev, q); 1106 1107 return NOTIFY_DONE; 1108 } 1109 1110 static void setup_txtime(struct taprio_sched *q, 1111 struct sched_gate_list *sched, ktime_t base) 1112 { 1113 struct sched_entry *entry; 1114 u32 interval = 0; 1115 1116 list_for_each_entry(entry, &sched->entries, list) { 1117 entry->next_txtime = ktime_add_ns(base, interval); 1118 interval += entry->interval; 1119 } 1120 } 1121 1122 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries) 1123 { 1124 struct __tc_taprio_qopt_offload *__offload; 1125 1126 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries), 1127 GFP_KERNEL); 1128 if (!__offload) 1129 return NULL; 1130 1131 refcount_set(&__offload->users, 1); 1132 1133 return &__offload->offload; 1134 } 1135 1136 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload 1137 *offload) 1138 { 1139 struct __tc_taprio_qopt_offload *__offload; 1140 1141 __offload = container_of(offload, struct __tc_taprio_qopt_offload, 1142 offload); 1143 1144 refcount_inc(&__offload->users); 1145 1146 return offload; 1147 } 1148 EXPORT_SYMBOL_GPL(taprio_offload_get); 1149 1150 void taprio_offload_free(struct tc_taprio_qopt_offload *offload) 1151 { 1152 struct __tc_taprio_qopt_offload *__offload; 1153 1154 __offload = container_of(offload, struct __tc_taprio_qopt_offload, 1155 offload); 1156 1157 if (!refcount_dec_and_test(&__offload->users)) 1158 return; 1159 1160 kfree(__offload); 1161 } 1162 EXPORT_SYMBOL_GPL(taprio_offload_free); 1163 1164 /* The function will only serve to keep the pointers to the "oper" and "admin" 1165 * schedules valid in relation to their base times, so when calling dump() the 1166 * users looks at the right schedules. 1167 * When using full offload, the admin configuration is promoted to oper at the 1168 * base_time in the PHC time domain. But because the system time is not 1169 * necessarily in sync with that, we can't just trigger a hrtimer to call 1170 * switch_schedules at the right hardware time. 1171 * At the moment we call this by hand right away from taprio, but in the future 1172 * it will be useful to create a mechanism for drivers to notify taprio of the 1173 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump(). 1174 * This is left as TODO. 1175 */ 1176 static void taprio_offload_config_changed(struct taprio_sched *q) 1177 { 1178 struct sched_gate_list *oper, *admin; 1179 1180 spin_lock(&q->current_entry_lock); 1181 1182 oper = rcu_dereference_protected(q->oper_sched, 1183 lockdep_is_held(&q->current_entry_lock)); 1184 admin = rcu_dereference_protected(q->admin_sched, 1185 lockdep_is_held(&q->current_entry_lock)); 1186 1187 switch_schedules(q, &admin, &oper); 1188 1189 spin_unlock(&q->current_entry_lock); 1190 } 1191 1192 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask) 1193 { 1194 u32 i, queue_mask = 0; 1195 1196 for (i = 0; i < dev->num_tc; i++) { 1197 u32 offset, count; 1198 1199 if (!(tc_mask & BIT(i))) 1200 continue; 1201 1202 offset = dev->tc_to_txq[i].offset; 1203 count = dev->tc_to_txq[i].count; 1204 1205 queue_mask |= GENMASK(offset + count - 1, offset); 1206 } 1207 1208 return queue_mask; 1209 } 1210 1211 static void taprio_sched_to_offload(struct net_device *dev, 1212 struct sched_gate_list *sched, 1213 struct tc_taprio_qopt_offload *offload) 1214 { 1215 struct sched_entry *entry; 1216 int i = 0; 1217 1218 offload->base_time = sched->base_time; 1219 offload->cycle_time = sched->cycle_time; 1220 offload->cycle_time_extension = sched->cycle_time_extension; 1221 1222 list_for_each_entry(entry, &sched->entries, list) { 1223 struct tc_taprio_sched_entry *e = &offload->entries[i]; 1224 1225 e->command = entry->command; 1226 e->interval = entry->interval; 1227 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask); 1228 1229 i++; 1230 } 1231 1232 offload->num_entries = i; 1233 } 1234 1235 static int taprio_enable_offload(struct net_device *dev, 1236 struct taprio_sched *q, 1237 struct sched_gate_list *sched, 1238 struct netlink_ext_ack *extack) 1239 { 1240 const struct net_device_ops *ops = dev->netdev_ops; 1241 struct tc_taprio_qopt_offload *offload; 1242 int err = 0; 1243 1244 if (!ops->ndo_setup_tc) { 1245 NL_SET_ERR_MSG(extack, 1246 "Device does not support taprio offload"); 1247 return -EOPNOTSUPP; 1248 } 1249 1250 offload = taprio_offload_alloc(sched->num_entries); 1251 if (!offload) { 1252 NL_SET_ERR_MSG(extack, 1253 "Not enough memory for enabling offload mode"); 1254 return -ENOMEM; 1255 } 1256 offload->enable = 1; 1257 taprio_sched_to_offload(dev, sched, offload); 1258 1259 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 1260 if (err < 0) { 1261 NL_SET_ERR_MSG(extack, 1262 "Device failed to setup taprio offload"); 1263 goto done; 1264 } 1265 1266 done: 1267 taprio_offload_free(offload); 1268 1269 return err; 1270 } 1271 1272 static int taprio_disable_offload(struct net_device *dev, 1273 struct taprio_sched *q, 1274 struct netlink_ext_ack *extack) 1275 { 1276 const struct net_device_ops *ops = dev->netdev_ops; 1277 struct tc_taprio_qopt_offload *offload; 1278 int err; 1279 1280 if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) 1281 return 0; 1282 1283 if (!ops->ndo_setup_tc) 1284 return -EOPNOTSUPP; 1285 1286 offload = taprio_offload_alloc(0); 1287 if (!offload) { 1288 NL_SET_ERR_MSG(extack, 1289 "Not enough memory to disable offload mode"); 1290 return -ENOMEM; 1291 } 1292 offload->enable = 0; 1293 1294 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 1295 if (err < 0) { 1296 NL_SET_ERR_MSG(extack, 1297 "Device failed to disable offload"); 1298 goto out; 1299 } 1300 1301 out: 1302 taprio_offload_free(offload); 1303 1304 return err; 1305 } 1306 1307 /* If full offload is enabled, the only possible clockid is the net device's 1308 * PHC. For that reason, specifying a clockid through netlink is incorrect. 1309 * For txtime-assist, it is implicitly assumed that the device's PHC is kept 1310 * in sync with the specified clockid via a user space daemon such as phc2sys. 1311 * For both software taprio and txtime-assist, the clockid is used for the 1312 * hrtimer that advances the schedule and hence mandatory. 1313 */ 1314 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb, 1315 struct netlink_ext_ack *extack) 1316 { 1317 struct taprio_sched *q = qdisc_priv(sch); 1318 struct net_device *dev = qdisc_dev(sch); 1319 int err = -EINVAL; 1320 1321 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 1322 const struct ethtool_ops *ops = dev->ethtool_ops; 1323 struct ethtool_ts_info info = { 1324 .cmd = ETHTOOL_GET_TS_INFO, 1325 .phc_index = -1, 1326 }; 1327 1328 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1329 NL_SET_ERR_MSG(extack, 1330 "The 'clockid' cannot be specified for full offload"); 1331 goto out; 1332 } 1333 1334 if (ops && ops->get_ts_info) 1335 err = ops->get_ts_info(dev, &info); 1336 1337 if (err || info.phc_index < 0) { 1338 NL_SET_ERR_MSG(extack, 1339 "Device does not have a PTP clock"); 1340 err = -ENOTSUPP; 1341 goto out; 1342 } 1343 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1344 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 1345 1346 /* We only support static clockids and we don't allow 1347 * for it to be modified after the first init. 1348 */ 1349 if (clockid < 0 || 1350 (q->clockid != -1 && q->clockid != clockid)) { 1351 NL_SET_ERR_MSG(extack, 1352 "Changing the 'clockid' of a running schedule is not supported"); 1353 err = -ENOTSUPP; 1354 goto out; 1355 } 1356 1357 switch (clockid) { 1358 case CLOCK_REALTIME: 1359 q->tk_offset = TK_OFFS_REAL; 1360 break; 1361 case CLOCK_MONOTONIC: 1362 q->tk_offset = TK_OFFS_MAX; 1363 break; 1364 case CLOCK_BOOTTIME: 1365 q->tk_offset = TK_OFFS_BOOT; 1366 break; 1367 case CLOCK_TAI: 1368 q->tk_offset = TK_OFFS_TAI; 1369 break; 1370 default: 1371 NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 1372 err = -EINVAL; 1373 goto out; 1374 } 1375 1376 q->clockid = clockid; 1377 } else { 1378 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 1379 goto out; 1380 } 1381 1382 /* Everything went ok, return success. */ 1383 err = 0; 1384 1385 out: 1386 return err; 1387 } 1388 1389 static int taprio_mqprio_cmp(const struct net_device *dev, 1390 const struct tc_mqprio_qopt *mqprio) 1391 { 1392 int i; 1393 1394 if (!mqprio || mqprio->num_tc != dev->num_tc) 1395 return -1; 1396 1397 for (i = 0; i < mqprio->num_tc; i++) 1398 if (dev->tc_to_txq[i].count != mqprio->count[i] || 1399 dev->tc_to_txq[i].offset != mqprio->offset[i]) 1400 return -1; 1401 1402 for (i = 0; i <= TC_BITMASK; i++) 1403 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i]) 1404 return -1; 1405 1406 return 0; 1407 } 1408 1409 /* The semantics of the 'flags' argument in relation to 'change()' 1410 * requests, are interpreted following two rules (which are applied in 1411 * this order): (1) an omitted 'flags' argument is interpreted as 1412 * zero; (2) the 'flags' of a "running" taprio instance cannot be 1413 * changed. 1414 */ 1415 static int taprio_new_flags(const struct nlattr *attr, u32 old, 1416 struct netlink_ext_ack *extack) 1417 { 1418 u32 new = 0; 1419 1420 if (attr) 1421 new = nla_get_u32(attr); 1422 1423 if (old != TAPRIO_FLAGS_INVALID && old != new) { 1424 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported"); 1425 return -EOPNOTSUPP; 1426 } 1427 1428 if (!taprio_flags_valid(new)) { 1429 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid"); 1430 return -EINVAL; 1431 } 1432 1433 return new; 1434 } 1435 1436 static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 1437 struct netlink_ext_ack *extack) 1438 { 1439 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1440 struct sched_gate_list *oper, *admin, *new_admin; 1441 struct taprio_sched *q = qdisc_priv(sch); 1442 struct net_device *dev = qdisc_dev(sch); 1443 struct tc_mqprio_qopt *mqprio = NULL; 1444 unsigned long flags; 1445 ktime_t start; 1446 int i, err; 1447 1448 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 1449 taprio_policy, extack); 1450 if (err < 0) 1451 return err; 1452 1453 if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 1454 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 1455 1456 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS], 1457 q->flags, extack); 1458 if (err < 0) 1459 return err; 1460 1461 q->flags = err; 1462 1463 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags); 1464 if (err < 0) 1465 return err; 1466 1467 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1468 if (!new_admin) { 1469 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1470 return -ENOMEM; 1471 } 1472 INIT_LIST_HEAD(&new_admin->entries); 1473 1474 rcu_read_lock(); 1475 oper = rcu_dereference(q->oper_sched); 1476 admin = rcu_dereference(q->admin_sched); 1477 rcu_read_unlock(); 1478 1479 /* no changes - no new mqprio settings */ 1480 if (!taprio_mqprio_cmp(dev, mqprio)) 1481 mqprio = NULL; 1482 1483 if (mqprio && (oper || admin)) { 1484 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1485 err = -ENOTSUPP; 1486 goto free_sched; 1487 } 1488 1489 err = parse_taprio_schedule(q, tb, new_admin, extack); 1490 if (err < 0) 1491 goto free_sched; 1492 1493 if (new_admin->num_entries == 0) { 1494 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 1495 err = -EINVAL; 1496 goto free_sched; 1497 } 1498 1499 err = taprio_parse_clockid(sch, tb, extack); 1500 if (err < 0) 1501 goto free_sched; 1502 1503 taprio_set_picos_per_byte(dev, q); 1504 1505 if (mqprio) { 1506 netdev_set_num_tc(dev, mqprio->num_tc); 1507 for (i = 0; i < mqprio->num_tc; i++) 1508 netdev_set_tc_queue(dev, i, 1509 mqprio->count[i], 1510 mqprio->offset[i]); 1511 1512 /* Always use supplied priority mappings */ 1513 for (i = 0; i <= TC_BITMASK; i++) 1514 netdev_set_prio_tc_map(dev, i, 1515 mqprio->prio_tc_map[i]); 1516 } 1517 1518 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1519 err = taprio_enable_offload(dev, q, new_admin, extack); 1520 else 1521 err = taprio_disable_offload(dev, q, extack); 1522 if (err) 1523 goto free_sched; 1524 1525 /* Protects against enqueue()/dequeue() */ 1526 spin_lock_bh(qdisc_lock(sch)); 1527 1528 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 1529 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 1530 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 1531 err = -EINVAL; 1532 goto unlock; 1533 } 1534 1535 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 1536 } 1537 1538 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) && 1539 !FULL_OFFLOAD_IS_ENABLED(q->flags) && 1540 !hrtimer_active(&q->advance_timer)) { 1541 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS); 1542 q->advance_timer.function = advance_sched; 1543 } 1544 1545 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 1546 q->dequeue = taprio_dequeue_offload; 1547 q->peek = taprio_peek_offload; 1548 } else { 1549 /* Be sure to always keep the function pointers 1550 * in a consistent state. 1551 */ 1552 q->dequeue = taprio_dequeue_soft; 1553 q->peek = taprio_peek_soft; 1554 } 1555 1556 err = taprio_get_start_time(sch, new_admin, &start); 1557 if (err < 0) { 1558 NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1559 goto unlock; 1560 } 1561 1562 setup_txtime(q, new_admin, start); 1563 1564 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 1565 if (!oper) { 1566 rcu_assign_pointer(q->oper_sched, new_admin); 1567 err = 0; 1568 new_admin = NULL; 1569 goto unlock; 1570 } 1571 1572 rcu_assign_pointer(q->admin_sched, new_admin); 1573 if (admin) 1574 call_rcu(&admin->rcu, taprio_free_sched_cb); 1575 } else { 1576 setup_first_close_time(q, new_admin, start); 1577 1578 /* Protects against advance_sched() */ 1579 spin_lock_irqsave(&q->current_entry_lock, flags); 1580 1581 taprio_start_sched(sch, start, new_admin); 1582 1583 rcu_assign_pointer(q->admin_sched, new_admin); 1584 if (admin) 1585 call_rcu(&admin->rcu, taprio_free_sched_cb); 1586 1587 spin_unlock_irqrestore(&q->current_entry_lock, flags); 1588 1589 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1590 taprio_offload_config_changed(q); 1591 } 1592 1593 new_admin = NULL; 1594 err = 0; 1595 1596 unlock: 1597 spin_unlock_bh(qdisc_lock(sch)); 1598 1599 free_sched: 1600 if (new_admin) 1601 call_rcu(&new_admin->rcu, taprio_free_sched_cb); 1602 1603 return err; 1604 } 1605 1606 static void taprio_reset(struct Qdisc *sch) 1607 { 1608 struct taprio_sched *q = qdisc_priv(sch); 1609 struct net_device *dev = qdisc_dev(sch); 1610 int i; 1611 1612 hrtimer_cancel(&q->advance_timer); 1613 if (q->qdiscs) { 1614 for (i = 0; i < dev->num_tx_queues; i++) 1615 if (q->qdiscs[i]) 1616 qdisc_reset(q->qdiscs[i]); 1617 } 1618 sch->qstats.backlog = 0; 1619 sch->q.qlen = 0; 1620 } 1621 1622 static void taprio_destroy(struct Qdisc *sch) 1623 { 1624 struct taprio_sched *q = qdisc_priv(sch); 1625 struct net_device *dev = qdisc_dev(sch); 1626 unsigned int i; 1627 1628 spin_lock(&taprio_list_lock); 1629 list_del(&q->taprio_list); 1630 spin_unlock(&taprio_list_lock); 1631 1632 1633 taprio_disable_offload(dev, q, NULL); 1634 1635 if (q->qdiscs) { 1636 for (i = 0; i < dev->num_tx_queues; i++) 1637 qdisc_put(q->qdiscs[i]); 1638 1639 kfree(q->qdiscs); 1640 } 1641 q->qdiscs = NULL; 1642 1643 netdev_reset_tc(dev); 1644 1645 if (q->oper_sched) 1646 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb); 1647 1648 if (q->admin_sched) 1649 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb); 1650 } 1651 1652 static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 1653 struct netlink_ext_ack *extack) 1654 { 1655 struct taprio_sched *q = qdisc_priv(sch); 1656 struct net_device *dev = qdisc_dev(sch); 1657 int i; 1658 1659 spin_lock_init(&q->current_entry_lock); 1660 1661 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS); 1662 q->advance_timer.function = advance_sched; 1663 1664 q->dequeue = taprio_dequeue_soft; 1665 q->peek = taprio_peek_soft; 1666 1667 q->root = sch; 1668 1669 /* We only support static clockids. Use an invalid value as default 1670 * and get the valid one on taprio_change(). 1671 */ 1672 q->clockid = -1; 1673 q->flags = TAPRIO_FLAGS_INVALID; 1674 1675 spin_lock(&taprio_list_lock); 1676 list_add(&q->taprio_list, &taprio_list); 1677 spin_unlock(&taprio_list_lock); 1678 1679 if (sch->parent != TC_H_ROOT) 1680 return -EOPNOTSUPP; 1681 1682 if (!netif_is_multiqueue(dev)) 1683 return -EOPNOTSUPP; 1684 1685 /* pre-allocate qdisc, attachment can't fail */ 1686 q->qdiscs = kcalloc(dev->num_tx_queues, 1687 sizeof(q->qdiscs[0]), 1688 GFP_KERNEL); 1689 1690 if (!q->qdiscs) 1691 return -ENOMEM; 1692 1693 if (!opt) 1694 return -EINVAL; 1695 1696 for (i = 0; i < dev->num_tx_queues; i++) { 1697 struct netdev_queue *dev_queue; 1698 struct Qdisc *qdisc; 1699 1700 dev_queue = netdev_get_tx_queue(dev, i); 1701 qdisc = qdisc_create_dflt(dev_queue, 1702 &pfifo_qdisc_ops, 1703 TC_H_MAKE(TC_H_MAJ(sch->handle), 1704 TC_H_MIN(i + 1)), 1705 extack); 1706 if (!qdisc) 1707 return -ENOMEM; 1708 1709 if (i < dev->real_num_tx_queues) 1710 qdisc_hash_add(qdisc, false); 1711 1712 q->qdiscs[i] = qdisc; 1713 } 1714 1715 return taprio_change(sch, opt, extack); 1716 } 1717 1718 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 1719 unsigned long cl) 1720 { 1721 struct net_device *dev = qdisc_dev(sch); 1722 unsigned long ntx = cl - 1; 1723 1724 if (ntx >= dev->num_tx_queues) 1725 return NULL; 1726 1727 return netdev_get_tx_queue(dev, ntx); 1728 } 1729 1730 static int taprio_graft(struct Qdisc *sch, unsigned long cl, 1731 struct Qdisc *new, struct Qdisc **old, 1732 struct netlink_ext_ack *extack) 1733 { 1734 struct taprio_sched *q = qdisc_priv(sch); 1735 struct net_device *dev = qdisc_dev(sch); 1736 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 1737 1738 if (!dev_queue) 1739 return -EINVAL; 1740 1741 if (dev->flags & IFF_UP) 1742 dev_deactivate(dev); 1743 1744 *old = q->qdiscs[cl - 1]; 1745 q->qdiscs[cl - 1] = new; 1746 1747 if (new) 1748 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 1749 1750 if (dev->flags & IFF_UP) 1751 dev_activate(dev); 1752 1753 return 0; 1754 } 1755 1756 static int dump_entry(struct sk_buff *msg, 1757 const struct sched_entry *entry) 1758 { 1759 struct nlattr *item; 1760 1761 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 1762 if (!item) 1763 return -ENOSPC; 1764 1765 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 1766 goto nla_put_failure; 1767 1768 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 1769 goto nla_put_failure; 1770 1771 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 1772 entry->gate_mask)) 1773 goto nla_put_failure; 1774 1775 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 1776 entry->interval)) 1777 goto nla_put_failure; 1778 1779 return nla_nest_end(msg, item); 1780 1781 nla_put_failure: 1782 nla_nest_cancel(msg, item); 1783 return -1; 1784 } 1785 1786 static int dump_schedule(struct sk_buff *msg, 1787 const struct sched_gate_list *root) 1788 { 1789 struct nlattr *entry_list; 1790 struct sched_entry *entry; 1791 1792 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 1793 root->base_time, TCA_TAPRIO_PAD)) 1794 return -1; 1795 1796 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 1797 root->cycle_time, TCA_TAPRIO_PAD)) 1798 return -1; 1799 1800 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 1801 root->cycle_time_extension, TCA_TAPRIO_PAD)) 1802 return -1; 1803 1804 entry_list = nla_nest_start_noflag(msg, 1805 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 1806 if (!entry_list) 1807 goto error_nest; 1808 1809 list_for_each_entry(entry, &root->entries, list) { 1810 if (dump_entry(msg, entry) < 0) 1811 goto error_nest; 1812 } 1813 1814 nla_nest_end(msg, entry_list); 1815 return 0; 1816 1817 error_nest: 1818 nla_nest_cancel(msg, entry_list); 1819 return -1; 1820 } 1821 1822 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 1823 { 1824 struct taprio_sched *q = qdisc_priv(sch); 1825 struct net_device *dev = qdisc_dev(sch); 1826 struct sched_gate_list *oper, *admin; 1827 struct tc_mqprio_qopt opt = { 0 }; 1828 struct nlattr *nest, *sched_nest; 1829 unsigned int i; 1830 1831 rcu_read_lock(); 1832 oper = rcu_dereference(q->oper_sched); 1833 admin = rcu_dereference(q->admin_sched); 1834 1835 opt.num_tc = netdev_get_num_tc(dev); 1836 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map)); 1837 1838 for (i = 0; i < netdev_get_num_tc(dev); i++) { 1839 opt.count[i] = dev->tc_to_txq[i].count; 1840 opt.offset[i] = dev->tc_to_txq[i].offset; 1841 } 1842 1843 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 1844 if (!nest) 1845 goto start_error; 1846 1847 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 1848 goto options_error; 1849 1850 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) && 1851 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 1852 goto options_error; 1853 1854 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 1855 goto options_error; 1856 1857 if (q->txtime_delay && 1858 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 1859 goto options_error; 1860 1861 if (oper && dump_schedule(skb, oper)) 1862 goto options_error; 1863 1864 if (!admin) 1865 goto done; 1866 1867 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 1868 if (!sched_nest) 1869 goto options_error; 1870 1871 if (dump_schedule(skb, admin)) 1872 goto admin_error; 1873 1874 nla_nest_end(skb, sched_nest); 1875 1876 done: 1877 rcu_read_unlock(); 1878 1879 return nla_nest_end(skb, nest); 1880 1881 admin_error: 1882 nla_nest_cancel(skb, sched_nest); 1883 1884 options_error: 1885 nla_nest_cancel(skb, nest); 1886 1887 start_error: 1888 rcu_read_unlock(); 1889 return -ENOSPC; 1890 } 1891 1892 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 1893 { 1894 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 1895 1896 if (!dev_queue) 1897 return NULL; 1898 1899 return dev_queue->qdisc_sleeping; 1900 } 1901 1902 static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 1903 { 1904 unsigned int ntx = TC_H_MIN(classid); 1905 1906 if (!taprio_queue_get(sch, ntx)) 1907 return 0; 1908 return ntx; 1909 } 1910 1911 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 1912 struct sk_buff *skb, struct tcmsg *tcm) 1913 { 1914 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 1915 1916 tcm->tcm_parent = TC_H_ROOT; 1917 tcm->tcm_handle |= TC_H_MIN(cl); 1918 tcm->tcm_info = dev_queue->qdisc_sleeping->handle; 1919 1920 return 0; 1921 } 1922 1923 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 1924 struct gnet_dump *d) 1925 __releases(d->lock) 1926 __acquires(d->lock) 1927 { 1928 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 1929 1930 sch = dev_queue->qdisc_sleeping; 1931 if (gnet_stats_copy_basic(&sch->running, d, NULL, &sch->bstats) < 0 || 1932 qdisc_qstats_copy(d, sch) < 0) 1933 return -1; 1934 return 0; 1935 } 1936 1937 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 1938 { 1939 struct net_device *dev = qdisc_dev(sch); 1940 unsigned long ntx; 1941 1942 if (arg->stop) 1943 return; 1944 1945 arg->count = arg->skip; 1946 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 1947 if (arg->fn(sch, ntx + 1, arg) < 0) { 1948 arg->stop = 1; 1949 break; 1950 } 1951 arg->count++; 1952 } 1953 } 1954 1955 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 1956 struct tcmsg *tcm) 1957 { 1958 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 1959 } 1960 1961 static const struct Qdisc_class_ops taprio_class_ops = { 1962 .graft = taprio_graft, 1963 .leaf = taprio_leaf, 1964 .find = taprio_find, 1965 .walk = taprio_walk, 1966 .dump = taprio_dump_class, 1967 .dump_stats = taprio_dump_class_stats, 1968 .select_queue = taprio_select_queue, 1969 }; 1970 1971 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 1972 .cl_ops = &taprio_class_ops, 1973 .id = "taprio", 1974 .priv_size = sizeof(struct taprio_sched), 1975 .init = taprio_init, 1976 .change = taprio_change, 1977 .destroy = taprio_destroy, 1978 .reset = taprio_reset, 1979 .peek = taprio_peek, 1980 .dequeue = taprio_dequeue, 1981 .enqueue = taprio_enqueue, 1982 .dump = taprio_dump, 1983 .owner = THIS_MODULE, 1984 }; 1985 1986 static struct notifier_block taprio_device_notifier = { 1987 .notifier_call = taprio_dev_notifier, 1988 }; 1989 1990 static int __init taprio_module_init(void) 1991 { 1992 int err = register_netdevice_notifier(&taprio_device_notifier); 1993 1994 if (err) 1995 return err; 1996 1997 return register_qdisc(&taprio_qdisc_ops); 1998 } 1999 2000 static void __exit taprio_module_exit(void) 2001 { 2002 unregister_qdisc(&taprio_qdisc_ops); 2003 unregister_netdevice_notifier(&taprio_device_notifier); 2004 } 2005 2006 module_init(taprio_module_init); 2007 module_exit(taprio_module_exit); 2008 MODULE_LICENSE("GPL"); 2009