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