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/ethtool_netlink.h> 11 #include <linux/types.h> 12 #include <linux/slab.h> 13 #include <linux/kernel.h> 14 #include <linux/string.h> 15 #include <linux/list.h> 16 #include <linux/errno.h> 17 #include <linux/skbuff.h> 18 #include <linux/math64.h> 19 #include <linux/module.h> 20 #include <linux/spinlock.h> 21 #include <linux/rcupdate.h> 22 #include <linux/time.h> 23 #include <net/gso.h> 24 #include <net/netlink.h> 25 #include <net/pkt_sched.h> 26 #include <net/pkt_cls.h> 27 #include <net/sch_generic.h> 28 #include <net/sock.h> 29 #include <net/tcp.h> 30 31 #define TAPRIO_STAT_NOT_SET (~0ULL) 32 33 #include "sch_mqprio_lib.h" 34 35 static LIST_HEAD(taprio_list); 36 static struct static_key_false taprio_have_broken_mqprio; 37 static struct static_key_false taprio_have_working_mqprio; 38 39 #define TAPRIO_ALL_GATES_OPEN -1 40 41 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) 42 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 43 #define TAPRIO_SUPPORTED_FLAGS \ 44 (TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST | TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD) 45 #define TAPRIO_FLAGS_INVALID U32_MAX 46 /* Minimum value for picos_per_byte to ensure non-zero duration 47 * for minimum-sized Ethernet frames (ETH_ZLEN = 60). 48 * 60 * 17 > PSEC_PER_NSEC (1000) 49 */ 50 #define TAPRIO_PICOS_PER_BYTE_MIN 17 51 52 struct sched_entry { 53 /* Durations between this GCL entry and the GCL entry where the 54 * respective traffic class gate closes 55 */ 56 u64 gate_duration[TC_MAX_QUEUE]; 57 atomic_t budget[TC_MAX_QUEUE]; 58 /* The qdisc makes some effort so that no packet leaves 59 * after this time 60 */ 61 ktime_t gate_close_time[TC_MAX_QUEUE]; 62 struct list_head list; 63 /* Used to calculate when to advance the schedule */ 64 ktime_t end_time; 65 ktime_t next_txtime; 66 int index; 67 u32 gate_mask; 68 u32 interval; 69 u8 command; 70 }; 71 72 struct sched_gate_list { 73 /* Longest non-zero contiguous gate durations per traffic class, 74 * or 0 if a traffic class gate never opens during the schedule. 75 */ 76 u64 max_open_gate_duration[TC_MAX_QUEUE]; 77 u32 max_frm_len[TC_MAX_QUEUE]; /* for the fast path */ 78 u32 max_sdu[TC_MAX_QUEUE]; /* for dump */ 79 struct rcu_head rcu; 80 struct list_head entries; 81 size_t num_entries; 82 ktime_t cycle_end_time; 83 s64 cycle_time; 84 s64 cycle_time_extension; 85 s64 base_time; 86 }; 87 88 struct taprio_sched { 89 struct Qdisc **qdiscs; 90 struct Qdisc *root; 91 u32 flags; 92 enum tk_offsets tk_offset; 93 int clockid; 94 bool offloaded; 95 bool detected_mqprio; 96 bool broken_mqprio; 97 atomic64_t picos_per_byte; /* Using picoseconds because for 10Gbps+ 98 * speeds it's sub-nanoseconds per byte 99 */ 100 101 /* Protects the update side of the RCU protected current_entry */ 102 spinlock_t current_entry_lock; 103 struct sched_entry __rcu *current_entry; 104 struct sched_gate_list __rcu *oper_sched; 105 struct sched_gate_list __rcu *admin_sched; 106 struct hrtimer advance_timer; 107 struct list_head taprio_list; 108 int cur_txq[TC_MAX_QUEUE]; 109 u32 max_sdu[TC_MAX_QUEUE]; /* save info from the user */ 110 u32 fp[TC_QOPT_MAX_QUEUE]; /* only for dump and offloading */ 111 u32 txtime_delay; 112 }; 113 114 struct __tc_taprio_qopt_offload { 115 refcount_t users; 116 struct tc_taprio_qopt_offload offload; 117 }; 118 119 static void taprio_calculate_gate_durations(struct taprio_sched *q, 120 struct sched_gate_list *sched) 121 { 122 struct net_device *dev = qdisc_dev(q->root); 123 int num_tc = netdev_get_num_tc(dev); 124 struct sched_entry *entry, *cur; 125 int tc; 126 127 list_for_each_entry(entry, &sched->entries, list) { 128 u32 gates_still_open = entry->gate_mask; 129 130 /* For each traffic class, calculate each open gate duration, 131 * starting at this schedule entry and ending at the schedule 132 * entry containing a gate close event for that TC. 133 */ 134 cur = entry; 135 136 do { 137 if (!gates_still_open) 138 break; 139 140 for (tc = 0; tc < num_tc; tc++) { 141 if (!(gates_still_open & BIT(tc))) 142 continue; 143 144 if (cur->gate_mask & BIT(tc)) 145 entry->gate_duration[tc] += cur->interval; 146 else 147 gates_still_open &= ~BIT(tc); 148 } 149 150 cur = list_next_entry_circular(cur, &sched->entries, list); 151 } while (cur != entry); 152 153 /* Keep track of the maximum gate duration for each traffic 154 * class, taking care to not confuse a traffic class which is 155 * temporarily closed with one that is always closed. 156 */ 157 for (tc = 0; tc < num_tc; tc++) 158 if (entry->gate_duration[tc] && 159 sched->max_open_gate_duration[tc] < entry->gate_duration[tc]) 160 sched->max_open_gate_duration[tc] = entry->gate_duration[tc]; 161 } 162 } 163 164 static bool taprio_entry_allows_tx(ktime_t skb_end_time, 165 struct sched_entry *entry, int tc) 166 { 167 return ktime_before(skb_end_time, entry->gate_close_time[tc]); 168 } 169 170 static ktime_t sched_base_time(const struct sched_gate_list *sched) 171 { 172 if (!sched) 173 return KTIME_MAX; 174 175 return ns_to_ktime(sched->base_time); 176 } 177 178 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono) 179 { 180 /* This pairs with WRITE_ONCE() in taprio_parse_clockid() */ 181 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset); 182 183 switch (tk_offset) { 184 case TK_OFFS_MAX: 185 return mono; 186 default: 187 return ktime_mono_to_any(mono, tk_offset); 188 } 189 } 190 191 static ktime_t taprio_get_time(const struct taprio_sched *q) 192 { 193 return taprio_mono_to_any(q, ktime_get()); 194 } 195 196 static void taprio_free_sched_cb(struct rcu_head *head) 197 { 198 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu); 199 struct sched_entry *entry, *n; 200 201 list_for_each_entry_safe(entry, n, &sched->entries, list) { 202 list_del(&entry->list); 203 kfree(entry); 204 } 205 206 kfree(sched); 207 } 208 209 static void switch_schedules(struct taprio_sched *q, 210 struct sched_gate_list **admin, 211 struct sched_gate_list **oper) 212 { 213 rcu_assign_pointer(q->oper_sched, *admin); 214 rcu_assign_pointer(q->admin_sched, NULL); 215 216 if (*oper) 217 call_rcu(&(*oper)->rcu, taprio_free_sched_cb); 218 219 *oper = *admin; 220 *admin = NULL; 221 } 222 223 /* Get how much time has been already elapsed in the current cycle. */ 224 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time) 225 { 226 ktime_t time_since_sched_start; 227 s32 time_elapsed; 228 229 time_since_sched_start = ktime_sub(time, sched->base_time); 230 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed); 231 232 return time_elapsed; 233 } 234 235 static ktime_t get_interval_end_time(struct sched_gate_list *sched, 236 struct sched_gate_list *admin, 237 struct sched_entry *entry, 238 ktime_t intv_start) 239 { 240 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start); 241 ktime_t intv_end, cycle_ext_end, cycle_end; 242 243 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed); 244 intv_end = ktime_add_ns(intv_start, entry->interval); 245 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension); 246 247 if (ktime_before(intv_end, cycle_end)) 248 return intv_end; 249 else if (admin && admin != sched && 250 ktime_after(admin->base_time, cycle_end) && 251 ktime_before(admin->base_time, cycle_ext_end)) 252 return admin->base_time; 253 else 254 return cycle_end; 255 } 256 257 static int length_to_duration(struct taprio_sched *q, int len) 258 { 259 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC); 260 } 261 262 static int duration_to_length(struct taprio_sched *q, u64 duration) 263 { 264 return div_u64(duration * PSEC_PER_NSEC, atomic64_read(&q->picos_per_byte)); 265 } 266 267 /* Sets sched->max_sdu[] and sched->max_frm_len[] to the minimum between the 268 * q->max_sdu[] requested by the user and the max_sdu dynamically determined by 269 * the maximum open gate durations at the given link speed. 270 */ 271 static void taprio_update_queue_max_sdu(struct taprio_sched *q, 272 struct sched_gate_list *sched, 273 struct qdisc_size_table *stab) 274 { 275 struct net_device *dev = qdisc_dev(q->root); 276 int num_tc = netdev_get_num_tc(dev); 277 u32 max_sdu_from_user; 278 u32 max_sdu_dynamic; 279 u32 max_sdu; 280 int tc; 281 282 for (tc = 0; tc < num_tc; tc++) { 283 max_sdu_from_user = q->max_sdu[tc] ?: U32_MAX; 284 285 /* TC gate never closes => keep the queueMaxSDU 286 * selected by the user 287 */ 288 if (sched->max_open_gate_duration[tc] == sched->cycle_time) { 289 max_sdu_dynamic = U32_MAX; 290 } else { 291 u32 max_frm_len; 292 293 max_frm_len = duration_to_length(q, sched->max_open_gate_duration[tc]); 294 /* Compensate for L1 overhead from size table, 295 * but don't let the frame size go negative 296 */ 297 if (stab) { 298 max_frm_len -= stab->szopts.overhead; 299 max_frm_len = max_t(int, max_frm_len, 300 dev->hard_header_len + 1); 301 } 302 max_sdu_dynamic = max_frm_len - dev->hard_header_len; 303 if (max_sdu_dynamic > dev->max_mtu) 304 max_sdu_dynamic = U32_MAX; 305 } 306 307 max_sdu = min(max_sdu_dynamic, max_sdu_from_user); 308 309 if (max_sdu != U32_MAX) { 310 sched->max_frm_len[tc] = max_sdu + dev->hard_header_len; 311 sched->max_sdu[tc] = max_sdu; 312 } else { 313 sched->max_frm_len[tc] = U32_MAX; /* never oversized */ 314 sched->max_sdu[tc] = 0; 315 } 316 } 317 } 318 319 /* Returns the entry corresponding to next available interval. If 320 * validate_interval is set, it only validates whether the timestamp occurs 321 * when the gate corresponding to the skb's traffic class is open. 322 */ 323 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb, 324 struct Qdisc *sch, 325 struct sched_gate_list *sched, 326 struct sched_gate_list *admin, 327 ktime_t time, 328 ktime_t *interval_start, 329 ktime_t *interval_end, 330 bool validate_interval) 331 { 332 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time; 333 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time; 334 struct sched_entry *entry = NULL, *entry_found = NULL; 335 struct taprio_sched *q = qdisc_priv(sch); 336 struct net_device *dev = qdisc_dev(sch); 337 bool entry_available = false; 338 s32 cycle_elapsed; 339 int tc, n; 340 341 tc = netdev_get_prio_tc_map(dev, skb->priority); 342 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb)); 343 344 *interval_start = 0; 345 *interval_end = 0; 346 347 if (!sched) 348 return NULL; 349 350 cycle = sched->cycle_time; 351 cycle_elapsed = get_cycle_time_elapsed(sched, time); 352 curr_intv_end = ktime_sub_ns(time, cycle_elapsed); 353 cycle_end = ktime_add_ns(curr_intv_end, cycle); 354 355 list_for_each_entry(entry, &sched->entries, list) { 356 curr_intv_start = curr_intv_end; 357 curr_intv_end = get_interval_end_time(sched, admin, entry, 358 curr_intv_start); 359 360 if (ktime_after(curr_intv_start, cycle_end)) 361 break; 362 363 if (!(entry->gate_mask & BIT(tc)) || 364 packet_transmit_time > entry->interval) 365 continue; 366 367 txtime = entry->next_txtime; 368 369 if (ktime_before(txtime, time) || validate_interval) { 370 transmit_end_time = ktime_add_ns(time, packet_transmit_time); 371 if ((ktime_before(curr_intv_start, time) && 372 ktime_before(transmit_end_time, curr_intv_end)) || 373 (ktime_after(curr_intv_start, time) && !validate_interval)) { 374 entry_found = entry; 375 *interval_start = curr_intv_start; 376 *interval_end = curr_intv_end; 377 break; 378 } else if (!entry_available && !validate_interval) { 379 /* Here, we are just trying to find out the 380 * first available interval in the next cycle. 381 */ 382 entry_available = true; 383 entry_found = entry; 384 *interval_start = ktime_add_ns(curr_intv_start, cycle); 385 *interval_end = ktime_add_ns(curr_intv_end, cycle); 386 } 387 } else if (ktime_before(txtime, earliest_txtime) && 388 !entry_available) { 389 earliest_txtime = txtime; 390 entry_found = entry; 391 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle); 392 *interval_start = ktime_add(curr_intv_start, n * cycle); 393 *interval_end = ktime_add(curr_intv_end, n * cycle); 394 } 395 } 396 397 return entry_found; 398 } 399 400 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch) 401 { 402 struct taprio_sched *q = qdisc_priv(sch); 403 struct sched_gate_list *sched, *admin; 404 ktime_t interval_start, interval_end; 405 struct sched_entry *entry; 406 407 rcu_read_lock(); 408 sched = rcu_dereference(q->oper_sched); 409 admin = rcu_dereference(q->admin_sched); 410 411 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp, 412 &interval_start, &interval_end, true); 413 rcu_read_unlock(); 414 415 return entry; 416 } 417 418 /* This returns the tstamp value set by TCP in terms of the set clock. */ 419 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb) 420 { 421 unsigned int offset = skb_network_offset(skb); 422 const struct ipv6hdr *ipv6h; 423 const struct iphdr *iph; 424 struct ipv6hdr _ipv6h; 425 426 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 427 if (!ipv6h) 428 return 0; 429 430 if (ipv6h->version == 4) { 431 iph = (struct iphdr *)ipv6h; 432 offset += iph->ihl * 4; 433 434 /* special-case 6in4 tunnelling, as that is a common way to get 435 * v6 connectivity in the home 436 */ 437 if (iph->protocol == IPPROTO_IPV6) { 438 ipv6h = skb_header_pointer(skb, offset, 439 sizeof(_ipv6h), &_ipv6h); 440 441 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP) 442 return 0; 443 } else if (iph->protocol != IPPROTO_TCP) { 444 return 0; 445 } 446 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) { 447 return 0; 448 } 449 450 return taprio_mono_to_any(q, skb->skb_mstamp_ns); 451 } 452 453 /* There are a few scenarios where we will have to modify the txtime from 454 * what is read from next_txtime in sched_entry. They are: 455 * 1. If txtime is in the past, 456 * a. The gate for the traffic class is currently open and packet can be 457 * transmitted before it closes, schedule the packet right away. 458 * b. If the gate corresponding to the traffic class is going to open later 459 * in the cycle, set the txtime of packet to the interval start. 460 * 2. If txtime is in the future, there are packets corresponding to the 461 * current traffic class waiting to be transmitted. So, the following 462 * possibilities exist: 463 * a. We can transmit the packet before the window containing the txtime 464 * closes. 465 * b. The window might close before the transmission can be completed 466 * successfully. So, schedule the packet in the next open window. 467 */ 468 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch) 469 { 470 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp; 471 struct taprio_sched *q = qdisc_priv(sch); 472 struct sched_gate_list *sched, *admin; 473 ktime_t minimum_time, now, txtime; 474 int len, packet_transmit_time; 475 struct sched_entry *entry; 476 bool sched_changed; 477 478 now = taprio_get_time(q); 479 minimum_time = ktime_add_ns(now, q->txtime_delay); 480 481 tcp_tstamp = get_tcp_tstamp(q, skb); 482 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp); 483 484 rcu_read_lock(); 485 admin = rcu_dereference(q->admin_sched); 486 sched = rcu_dereference(q->oper_sched); 487 if (admin && ktime_after(minimum_time, admin->base_time)) 488 switch_schedules(q, &admin, &sched); 489 490 /* Until the schedule starts, all the queues are open */ 491 if (!sched || ktime_before(minimum_time, sched->base_time)) { 492 txtime = minimum_time; 493 goto done; 494 } 495 496 len = qdisc_pkt_len(skb); 497 packet_transmit_time = length_to_duration(q, len); 498 499 do { 500 sched_changed = false; 501 502 entry = find_entry_to_transmit(skb, sch, sched, admin, 503 minimum_time, 504 &interval_start, &interval_end, 505 false); 506 if (!entry) { 507 txtime = 0; 508 goto done; 509 } 510 511 txtime = entry->next_txtime; 512 txtime = max_t(ktime_t, txtime, minimum_time); 513 txtime = max_t(ktime_t, txtime, interval_start); 514 515 if (admin && admin != sched && 516 ktime_after(txtime, admin->base_time)) { 517 sched = admin; 518 sched_changed = true; 519 continue; 520 } 521 522 transmit_end_time = ktime_add(txtime, packet_transmit_time); 523 minimum_time = transmit_end_time; 524 525 /* Update the txtime of current entry to the next time it's 526 * interval starts. 527 */ 528 if (ktime_after(transmit_end_time, interval_end)) 529 entry->next_txtime = ktime_add(interval_start, sched->cycle_time); 530 } while (sched_changed || ktime_after(transmit_end_time, interval_end)); 531 532 entry->next_txtime = transmit_end_time; 533 534 done: 535 rcu_read_unlock(); 536 return txtime; 537 } 538 539 /* Devices with full offload are expected to honor this in hardware */ 540 static bool taprio_skb_exceeds_queue_max_sdu(struct Qdisc *sch, 541 struct sk_buff *skb) 542 { 543 struct taprio_sched *q = qdisc_priv(sch); 544 struct net_device *dev = qdisc_dev(sch); 545 struct sched_gate_list *sched; 546 int prio = skb->priority; 547 bool exceeds = false; 548 u8 tc; 549 550 tc = netdev_get_prio_tc_map(dev, prio); 551 552 rcu_read_lock(); 553 sched = rcu_dereference(q->oper_sched); 554 if (sched && skb->len > sched->max_frm_len[tc]) 555 exceeds = true; 556 rcu_read_unlock(); 557 558 return exceeds; 559 } 560 561 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch, 562 struct Qdisc *child, struct sk_buff **to_free) 563 { 564 struct taprio_sched *q = qdisc_priv(sch); 565 566 /* sk_flags are only safe to use on full sockets. */ 567 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) { 568 if (!is_valid_interval(skb, sch)) 569 return qdisc_drop(skb, sch, to_free); 570 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 571 skb->tstamp = get_packet_txtime(skb, sch); 572 if (!skb->tstamp) 573 return qdisc_drop(skb, sch, to_free); 574 } 575 576 qdisc_qstats_backlog_inc(sch, skb); 577 sch->q.qlen++; 578 579 return qdisc_enqueue(skb, child, to_free); 580 } 581 582 static int taprio_enqueue_segmented(struct sk_buff *skb, struct Qdisc *sch, 583 struct Qdisc *child, 584 struct sk_buff **to_free) 585 { 586 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb); 587 netdev_features_t features = netif_skb_features(skb); 588 struct sk_buff *segs, *nskb; 589 int ret; 590 591 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK); 592 if (IS_ERR_OR_NULL(segs)) 593 return qdisc_drop(skb, sch, to_free); 594 595 skb_list_walk_safe(segs, segs, nskb) { 596 skb_mark_not_on_list(segs); 597 qdisc_skb_cb(segs)->pkt_len = segs->len; 598 slen += segs->len; 599 600 /* FIXME: we should be segmenting to a smaller size 601 * rather than dropping these 602 */ 603 if (taprio_skb_exceeds_queue_max_sdu(sch, segs)) 604 ret = qdisc_drop(segs, sch, to_free); 605 else 606 ret = taprio_enqueue_one(segs, sch, child, to_free); 607 608 if (ret != NET_XMIT_SUCCESS) { 609 if (net_xmit_drop_count(ret)) 610 qdisc_qstats_drop(sch); 611 } else { 612 numsegs++; 613 } 614 } 615 616 if (numsegs > 1) 617 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen); 618 consume_skb(skb); 619 620 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP; 621 } 622 623 /* Will not be called in the full offload case, since the TX queues are 624 * attached to the Qdisc created using qdisc_create_dflt() 625 */ 626 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch, 627 struct sk_buff **to_free) 628 { 629 struct taprio_sched *q = qdisc_priv(sch); 630 struct Qdisc *child; 631 int queue; 632 633 queue = skb_get_queue_mapping(skb); 634 635 child = q->qdiscs[queue]; 636 if (unlikely(!child)) 637 return qdisc_drop(skb, sch, to_free); 638 639 if (taprio_skb_exceeds_queue_max_sdu(sch, skb)) { 640 /* Large packets might not be transmitted when the transmission 641 * duration exceeds any configured interval. Therefore, segment 642 * the skb into smaller chunks. Drivers with full offload are 643 * expected to handle this in hardware. 644 */ 645 if (skb_is_gso(skb)) 646 return taprio_enqueue_segmented(skb, sch, child, 647 to_free); 648 649 return qdisc_drop(skb, sch, to_free); 650 } 651 652 return taprio_enqueue_one(skb, sch, child, to_free); 653 } 654 655 static struct sk_buff *taprio_peek(struct Qdisc *sch) 656 { 657 WARN_ONCE(1, "taprio only supports operating as root qdisc, peek() not implemented"); 658 return NULL; 659 } 660 661 static void taprio_set_budgets(struct taprio_sched *q, 662 struct sched_gate_list *sched, 663 struct sched_entry *entry) 664 { 665 struct net_device *dev = qdisc_dev(q->root); 666 int num_tc = netdev_get_num_tc(dev); 667 int tc, budget; 668 669 for (tc = 0; tc < num_tc; tc++) { 670 /* Traffic classes which never close have infinite budget */ 671 if (entry->gate_duration[tc] == sched->cycle_time) 672 budget = INT_MAX; 673 else 674 budget = div64_u64((u64)entry->gate_duration[tc] * PSEC_PER_NSEC, 675 atomic64_read(&q->picos_per_byte)); 676 677 atomic_set(&entry->budget[tc], budget); 678 } 679 } 680 681 /* When an skb is sent, it consumes from the budget of all traffic classes */ 682 static int taprio_update_budgets(struct sched_entry *entry, size_t len, 683 int tc_consumed, int num_tc) 684 { 685 int tc, budget, new_budget = 0; 686 687 for (tc = 0; tc < num_tc; tc++) { 688 budget = atomic_read(&entry->budget[tc]); 689 /* Don't consume from infinite budget */ 690 if (budget == INT_MAX) { 691 if (tc == tc_consumed) 692 new_budget = budget; 693 continue; 694 } 695 696 if (tc == tc_consumed) 697 new_budget = atomic_sub_return(len, &entry->budget[tc]); 698 else 699 atomic_sub(len, &entry->budget[tc]); 700 } 701 702 return new_budget; 703 } 704 705 static struct sk_buff *taprio_dequeue_from_txq(struct Qdisc *sch, int txq, 706 struct sched_entry *entry, 707 u32 gate_mask) 708 { 709 struct taprio_sched *q = qdisc_priv(sch); 710 struct net_device *dev = qdisc_dev(sch); 711 struct Qdisc *child = q->qdiscs[txq]; 712 int num_tc = netdev_get_num_tc(dev); 713 struct sk_buff *skb; 714 ktime_t guard; 715 int prio; 716 int len; 717 u8 tc; 718 719 if (unlikely(!child)) 720 return NULL; 721 722 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) 723 goto skip_peek_checks; 724 725 skb = child->ops->peek(child); 726 if (!skb) 727 return NULL; 728 729 prio = skb->priority; 730 tc = netdev_get_prio_tc_map(dev, prio); 731 732 if (!(gate_mask & BIT(tc))) 733 return NULL; 734 735 len = qdisc_pkt_len(skb); 736 guard = ktime_add_ns(taprio_get_time(q), length_to_duration(q, len)); 737 738 /* In the case that there's no gate entry, there's no 739 * guard band ... 740 */ 741 if (gate_mask != TAPRIO_ALL_GATES_OPEN && 742 !taprio_entry_allows_tx(guard, entry, tc)) 743 return NULL; 744 745 /* ... and no budget. */ 746 if (gate_mask != TAPRIO_ALL_GATES_OPEN && 747 taprio_update_budgets(entry, len, tc, num_tc) < 0) 748 return NULL; 749 750 skip_peek_checks: 751 skb = child->ops->dequeue(child); 752 if (unlikely(!skb)) 753 return NULL; 754 755 qdisc_bstats_update(sch, skb); 756 qdisc_qstats_backlog_dec(sch, skb); 757 sch->q.qlen--; 758 759 return skb; 760 } 761 762 static void taprio_next_tc_txq(struct net_device *dev, int tc, int *txq) 763 { 764 int offset = dev->tc_to_txq[tc].offset; 765 int count = dev->tc_to_txq[tc].count; 766 767 (*txq)++; 768 if (*txq == offset + count) 769 *txq = offset; 770 } 771 772 /* Prioritize higher traffic classes, and select among TXQs belonging to the 773 * same TC using round robin 774 */ 775 static struct sk_buff *taprio_dequeue_tc_priority(struct Qdisc *sch, 776 struct sched_entry *entry, 777 u32 gate_mask) 778 { 779 struct taprio_sched *q = qdisc_priv(sch); 780 struct net_device *dev = qdisc_dev(sch); 781 int num_tc = netdev_get_num_tc(dev); 782 struct sk_buff *skb; 783 int tc; 784 785 for (tc = num_tc - 1; tc >= 0; tc--) { 786 int first_txq = q->cur_txq[tc]; 787 788 if (!(gate_mask & BIT(tc))) 789 continue; 790 791 do { 792 skb = taprio_dequeue_from_txq(sch, q->cur_txq[tc], 793 entry, gate_mask); 794 795 taprio_next_tc_txq(dev, tc, &q->cur_txq[tc]); 796 797 if (q->cur_txq[tc] >= dev->num_tx_queues) 798 q->cur_txq[tc] = first_txq; 799 800 if (skb) 801 return skb; 802 } while (q->cur_txq[tc] != first_txq); 803 } 804 805 return NULL; 806 } 807 808 /* Broken way of prioritizing smaller TXQ indices and ignoring the traffic 809 * class other than to determine whether the gate is open or not 810 */ 811 static struct sk_buff *taprio_dequeue_txq_priority(struct Qdisc *sch, 812 struct sched_entry *entry, 813 u32 gate_mask) 814 { 815 struct net_device *dev = qdisc_dev(sch); 816 struct sk_buff *skb; 817 int i; 818 819 for (i = 0; i < dev->num_tx_queues; i++) { 820 skb = taprio_dequeue_from_txq(sch, i, entry, gate_mask); 821 if (skb) 822 return skb; 823 } 824 825 return NULL; 826 } 827 828 /* Will not be called in the full offload case, since the TX queues are 829 * attached to the Qdisc created using qdisc_create_dflt() 830 */ 831 static struct sk_buff *taprio_dequeue(struct Qdisc *sch) 832 { 833 struct taprio_sched *q = qdisc_priv(sch); 834 struct sk_buff *skb = NULL; 835 struct sched_entry *entry; 836 u32 gate_mask; 837 838 rcu_read_lock(); 839 entry = rcu_dereference(q->current_entry); 840 /* if there's no entry, it means that the schedule didn't 841 * start yet, so force all gates to be open, this is in 842 * accordance to IEEE 802.1Qbv-2015 Section 8.6.9.4.5 843 * "AdminGateStates" 844 */ 845 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN; 846 if (!gate_mask) 847 goto done; 848 849 if (static_branch_unlikely(&taprio_have_broken_mqprio) && 850 !static_branch_likely(&taprio_have_working_mqprio)) { 851 /* Single NIC kind which is broken */ 852 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask); 853 } else if (static_branch_likely(&taprio_have_working_mqprio) && 854 !static_branch_unlikely(&taprio_have_broken_mqprio)) { 855 /* Single NIC kind which prioritizes properly */ 856 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask); 857 } else { 858 /* Mixed NIC kinds present in system, need dynamic testing */ 859 if (q->broken_mqprio) 860 skb = taprio_dequeue_txq_priority(sch, entry, gate_mask); 861 else 862 skb = taprio_dequeue_tc_priority(sch, entry, gate_mask); 863 } 864 865 done: 866 rcu_read_unlock(); 867 868 return skb; 869 } 870 871 static bool should_restart_cycle(const struct sched_gate_list *oper, 872 const struct sched_entry *entry) 873 { 874 if (list_is_last(&entry->list, &oper->entries)) 875 return true; 876 877 if (ktime_compare(entry->end_time, oper->cycle_end_time) == 0) 878 return true; 879 880 return false; 881 } 882 883 static bool should_change_schedules(const struct sched_gate_list *admin, 884 const struct sched_gate_list *oper, 885 ktime_t end_time) 886 { 887 ktime_t next_base_time, extension_time; 888 889 if (!admin) 890 return false; 891 892 next_base_time = sched_base_time(admin); 893 894 /* This is the simple case, the end_time would fall after 895 * the next schedule base_time. 896 */ 897 if (ktime_compare(next_base_time, end_time) <= 0) 898 return true; 899 900 /* This is the cycle_time_extension case, if the end_time 901 * plus the amount that can be extended would fall after the 902 * next schedule base_time, we can extend the current schedule 903 * for that amount. 904 */ 905 extension_time = ktime_add_ns(end_time, oper->cycle_time_extension); 906 907 /* FIXME: the IEEE 802.1Q-2018 Specification isn't clear about 908 * how precisely the extension should be made. So after 909 * conformance testing, this logic may change. 910 */ 911 if (ktime_compare(next_base_time, extension_time) <= 0) 912 return true; 913 914 return false; 915 } 916 917 static enum hrtimer_restart advance_sched(struct hrtimer *timer) 918 { 919 struct taprio_sched *q = container_of(timer, struct taprio_sched, 920 advance_timer); 921 struct net_device *dev = qdisc_dev(q->root); 922 struct sched_gate_list *oper, *admin; 923 int num_tc = netdev_get_num_tc(dev); 924 struct sched_entry *entry, *next; 925 struct Qdisc *sch = q->root; 926 ktime_t end_time; 927 int tc; 928 929 spin_lock(&q->current_entry_lock); 930 entry = rcu_dereference_protected(q->current_entry, 931 lockdep_is_held(&q->current_entry_lock)); 932 oper = rcu_dereference_protected(q->oper_sched, 933 lockdep_is_held(&q->current_entry_lock)); 934 admin = rcu_dereference_protected(q->admin_sched, 935 lockdep_is_held(&q->current_entry_lock)); 936 937 if (!oper) 938 switch_schedules(q, &admin, &oper); 939 940 /* This can happen in two cases: 1. this is the very first run 941 * of this function (i.e. we weren't running any schedule 942 * previously); 2. The previous schedule just ended. The first 943 * entry of all schedules are pre-calculated during the 944 * schedule initialization. 945 */ 946 if (unlikely(!entry || entry->end_time == oper->base_time)) { 947 next = list_first_entry(&oper->entries, struct sched_entry, 948 list); 949 end_time = next->end_time; 950 goto first_run; 951 } 952 953 if (should_restart_cycle(oper, entry)) { 954 next = list_first_entry(&oper->entries, struct sched_entry, 955 list); 956 oper->cycle_end_time = ktime_add_ns(oper->cycle_end_time, 957 oper->cycle_time); 958 } else { 959 next = list_next_entry(entry, list); 960 } 961 962 end_time = ktime_add_ns(entry->end_time, next->interval); 963 end_time = min_t(ktime_t, end_time, oper->cycle_end_time); 964 965 for (tc = 0; tc < num_tc; tc++) { 966 if (next->gate_duration[tc] == oper->cycle_time) 967 next->gate_close_time[tc] = KTIME_MAX; 968 else 969 next->gate_close_time[tc] = ktime_add_ns(entry->end_time, 970 next->gate_duration[tc]); 971 } 972 973 if (should_change_schedules(admin, oper, end_time)) { 974 /* Set things so the next time this runs, the new 975 * schedule runs. 976 */ 977 end_time = sched_base_time(admin); 978 switch_schedules(q, &admin, &oper); 979 } 980 981 next->end_time = end_time; 982 taprio_set_budgets(q, oper, next); 983 984 first_run: 985 rcu_assign_pointer(q->current_entry, next); 986 spin_unlock(&q->current_entry_lock); 987 988 hrtimer_set_expires(&q->advance_timer, end_time); 989 990 rcu_read_lock(); 991 __netif_schedule(sch); 992 rcu_read_unlock(); 993 994 return HRTIMER_RESTART; 995 } 996 997 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { 998 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 }, 999 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 }, 1000 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 }, 1001 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 }, 1002 }; 1003 1004 static const struct nla_policy taprio_tc_policy[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { 1005 [TCA_TAPRIO_TC_ENTRY_INDEX] = NLA_POLICY_MAX(NLA_U32, 1006 TC_QOPT_MAX_QUEUE - 1), 1007 [TCA_TAPRIO_TC_ENTRY_MAX_SDU] = { .type = NLA_U32 }, 1008 [TCA_TAPRIO_TC_ENTRY_FP] = NLA_POLICY_RANGE(NLA_U32, 1009 TC_FP_EXPRESS, 1010 TC_FP_PREEMPTIBLE), 1011 }; 1012 1013 static const struct netlink_range_validation_signed taprio_cycle_time_range = { 1014 .min = 0, 1015 .max = INT_MAX, 1016 }; 1017 1018 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = { 1019 [TCA_TAPRIO_ATTR_PRIOMAP] = { 1020 .len = sizeof(struct tc_mqprio_qopt) 1021 }, 1022 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED }, 1023 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 }, 1024 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED }, 1025 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 }, 1026 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = 1027 NLA_POLICY_FULL_RANGE_SIGNED(NLA_S64, &taprio_cycle_time_range), 1028 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 }, 1029 [TCA_TAPRIO_ATTR_FLAGS] = 1030 NLA_POLICY_MASK(NLA_U32, TAPRIO_SUPPORTED_FLAGS), 1031 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 }, 1032 [TCA_TAPRIO_ATTR_TC_ENTRY] = { .type = NLA_NESTED }, 1033 }; 1034 1035 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb, 1036 struct sched_entry *entry, 1037 struct netlink_ext_ack *extack) 1038 { 1039 int min_duration = length_to_duration(q, ETH_ZLEN); 1040 u32 interval = 0; 1041 1042 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD]) 1043 entry->command = nla_get_u8( 1044 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]); 1045 1046 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]) 1047 entry->gate_mask = nla_get_u32( 1048 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]); 1049 1050 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]) 1051 interval = nla_get_u32( 1052 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]); 1053 1054 /* The interval should allow at least the minimum ethernet 1055 * frame to go out. 1056 */ 1057 if (interval < min_duration) { 1058 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry"); 1059 return -EINVAL; 1060 } 1061 1062 entry->interval = interval; 1063 1064 return 0; 1065 } 1066 1067 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n, 1068 struct sched_entry *entry, int index, 1069 struct netlink_ext_ack *extack) 1070 { 1071 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { }; 1072 int err; 1073 1074 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n, 1075 entry_policy, NULL); 1076 if (err < 0) { 1077 NL_SET_ERR_MSG(extack, "Could not parse nested entry"); 1078 return -EINVAL; 1079 } 1080 1081 entry->index = index; 1082 1083 return fill_sched_entry(q, tb, entry, extack); 1084 } 1085 1086 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list, 1087 struct sched_gate_list *sched, 1088 struct netlink_ext_ack *extack) 1089 { 1090 struct nlattr *n; 1091 int err, rem; 1092 int i = 0; 1093 1094 if (!list) 1095 return -EINVAL; 1096 1097 nla_for_each_nested(n, list, rem) { 1098 struct sched_entry *entry; 1099 1100 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) { 1101 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'"); 1102 continue; 1103 } 1104 1105 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 1106 if (!entry) { 1107 NL_SET_ERR_MSG(extack, "Not enough memory for entry"); 1108 return -ENOMEM; 1109 } 1110 1111 err = parse_sched_entry(q, n, entry, i, extack); 1112 if (err < 0) { 1113 kfree(entry); 1114 return err; 1115 } 1116 1117 list_add_tail(&entry->list, &sched->entries); 1118 i++; 1119 } 1120 1121 sched->num_entries = i; 1122 1123 return i; 1124 } 1125 1126 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb, 1127 struct sched_gate_list *new, 1128 struct netlink_ext_ack *extack) 1129 { 1130 int err = 0; 1131 1132 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) { 1133 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported"); 1134 return -ENOTSUPP; 1135 } 1136 1137 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]) 1138 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]); 1139 1140 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]) 1141 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]); 1142 1143 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]) 1144 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]); 1145 1146 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST]) 1147 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST], 1148 new, extack); 1149 if (err < 0) 1150 return err; 1151 1152 if (!new->cycle_time) { 1153 struct sched_entry *entry; 1154 ktime_t cycle = 0; 1155 1156 list_for_each_entry(entry, &new->entries, list) 1157 cycle = ktime_add_ns(cycle, entry->interval); 1158 1159 if (cycle < 0 || cycle > INT_MAX) { 1160 NL_SET_ERR_MSG(extack, "'cycle_time' is too big"); 1161 return -EINVAL; 1162 } 1163 1164 new->cycle_time = cycle; 1165 } 1166 1167 if (new->cycle_time < new->num_entries * length_to_duration(q, ETH_ZLEN)) { 1168 NL_SET_ERR_MSG(extack, "'cycle_time' is too small"); 1169 return -EINVAL; 1170 } 1171 1172 taprio_calculate_gate_durations(q, new); 1173 1174 return 0; 1175 } 1176 1177 static int taprio_parse_mqprio_opt(struct net_device *dev, 1178 struct tc_mqprio_qopt *qopt, 1179 struct netlink_ext_ack *extack, 1180 u32 taprio_flags) 1181 { 1182 bool allow_overlapping_txqs = TXTIME_ASSIST_IS_ENABLED(taprio_flags); 1183 1184 if (!qopt) { 1185 if (!dev->num_tc) { 1186 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary"); 1187 return -EINVAL; 1188 } 1189 return 0; 1190 } 1191 1192 /* taprio imposes that traffic classes map 1:n to tx queues */ 1193 if (qopt->num_tc > dev->num_tx_queues) { 1194 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues"); 1195 return -EINVAL; 1196 } 1197 1198 /* For some reason, in txtime-assist mode, we allow TXQ ranges for 1199 * different TCs to overlap, and just validate the TXQ ranges. 1200 */ 1201 return mqprio_validate_qopt(dev, qopt, true, allow_overlapping_txqs, 1202 extack); 1203 } 1204 1205 static int taprio_get_start_time(struct Qdisc *sch, 1206 struct sched_gate_list *sched, 1207 ktime_t *start) 1208 { 1209 struct taprio_sched *q = qdisc_priv(sch); 1210 ktime_t now, base, cycle; 1211 s64 n; 1212 1213 base = sched_base_time(sched); 1214 now = taprio_get_time(q); 1215 1216 if (ktime_after(base, now)) { 1217 *start = base; 1218 return 0; 1219 } 1220 1221 cycle = sched->cycle_time; 1222 1223 /* The qdisc is expected to have at least one sched_entry. Moreover, 1224 * any entry must have 'interval' > 0. Thus if the cycle time is zero, 1225 * something went really wrong. In that case, we should warn about this 1226 * inconsistent state and return error. 1227 */ 1228 if (WARN_ON(!cycle)) 1229 return -EFAULT; 1230 1231 /* Schedule the start time for the beginning of the next 1232 * cycle. 1233 */ 1234 n = div64_s64(ktime_sub_ns(now, base), cycle); 1235 *start = ktime_add_ns(base, (n + 1) * cycle); 1236 return 0; 1237 } 1238 1239 static void setup_first_end_time(struct taprio_sched *q, 1240 struct sched_gate_list *sched, ktime_t base) 1241 { 1242 struct net_device *dev = qdisc_dev(q->root); 1243 int num_tc = netdev_get_num_tc(dev); 1244 struct sched_entry *first; 1245 ktime_t cycle; 1246 int tc; 1247 1248 first = list_first_entry(&sched->entries, 1249 struct sched_entry, list); 1250 1251 cycle = sched->cycle_time; 1252 1253 /* FIXME: find a better place to do this */ 1254 sched->cycle_end_time = ktime_add_ns(base, cycle); 1255 1256 first->end_time = ktime_add_ns(base, first->interval); 1257 taprio_set_budgets(q, sched, first); 1258 1259 for (tc = 0; tc < num_tc; tc++) { 1260 if (first->gate_duration[tc] == sched->cycle_time) 1261 first->gate_close_time[tc] = KTIME_MAX; 1262 else 1263 first->gate_close_time[tc] = ktime_add_ns(base, first->gate_duration[tc]); 1264 } 1265 1266 rcu_assign_pointer(q->current_entry, NULL); 1267 } 1268 1269 static void taprio_start_sched(struct Qdisc *sch, 1270 ktime_t start, struct sched_gate_list *new) 1271 { 1272 struct taprio_sched *q = qdisc_priv(sch); 1273 ktime_t expires; 1274 1275 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1276 return; 1277 1278 expires = hrtimer_get_expires(&q->advance_timer); 1279 if (expires == 0) 1280 expires = KTIME_MAX; 1281 1282 /* If the new schedule starts before the next expiration, we 1283 * reprogram it to the earliest one, so we change the admin 1284 * schedule to the operational one at the right time. 1285 */ 1286 start = min_t(ktime_t, start, expires); 1287 1288 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS); 1289 } 1290 1291 static void taprio_set_picos_per_byte(struct net_device *dev, 1292 struct taprio_sched *q, 1293 struct netlink_ext_ack *extack) 1294 { 1295 struct ethtool_link_ksettings ecmd; 1296 int speed = SPEED_10; 1297 int picos_per_byte; 1298 int err; 1299 1300 err = __ethtool_get_link_ksettings(dev, &ecmd); 1301 if (err < 0) 1302 goto skip; 1303 1304 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN) 1305 speed = ecmd.base.speed; 1306 1307 skip: 1308 picos_per_byte = (USEC_PER_SEC * 8) / speed; 1309 if (picos_per_byte < TAPRIO_PICOS_PER_BYTE_MIN) { 1310 if (!extack) 1311 pr_warn("Link speed %d is too high. Schedule may be inaccurate.\n", 1312 speed); 1313 NL_SET_ERR_MSG_FMT_MOD(extack, 1314 "Link speed %d is too high. Schedule may be inaccurate.", 1315 speed); 1316 picos_per_byte = TAPRIO_PICOS_PER_BYTE_MIN; 1317 } 1318 1319 atomic64_set(&q->picos_per_byte, picos_per_byte); 1320 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n", 1321 dev->name, (long long)atomic64_read(&q->picos_per_byte), 1322 ecmd.base.speed); 1323 } 1324 1325 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event, 1326 void *ptr) 1327 { 1328 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1329 struct sched_gate_list *oper, *admin; 1330 struct qdisc_size_table *stab; 1331 struct taprio_sched *q; 1332 1333 ASSERT_RTNL(); 1334 1335 if (event != NETDEV_UP && event != NETDEV_CHANGE) 1336 return NOTIFY_DONE; 1337 1338 list_for_each_entry(q, &taprio_list, taprio_list) { 1339 if (dev != qdisc_dev(q->root)) 1340 continue; 1341 1342 taprio_set_picos_per_byte(dev, q, NULL); 1343 1344 stab = rtnl_dereference(q->root->stab); 1345 1346 rcu_read_lock(); 1347 oper = rcu_dereference(q->oper_sched); 1348 if (oper) 1349 taprio_update_queue_max_sdu(q, oper, stab); 1350 1351 admin = rcu_dereference(q->admin_sched); 1352 if (admin) 1353 taprio_update_queue_max_sdu(q, admin, stab); 1354 rcu_read_unlock(); 1355 1356 break; 1357 } 1358 1359 return NOTIFY_DONE; 1360 } 1361 1362 static void setup_txtime(struct taprio_sched *q, 1363 struct sched_gate_list *sched, ktime_t base) 1364 { 1365 struct sched_entry *entry; 1366 u64 interval = 0; 1367 1368 list_for_each_entry(entry, &sched->entries, list) { 1369 entry->next_txtime = ktime_add_ns(base, interval); 1370 interval += entry->interval; 1371 } 1372 } 1373 1374 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries) 1375 { 1376 struct __tc_taprio_qopt_offload *__offload; 1377 1378 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries), 1379 GFP_KERNEL); 1380 if (!__offload) 1381 return NULL; 1382 1383 refcount_set(&__offload->users, 1); 1384 1385 return &__offload->offload; 1386 } 1387 1388 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload 1389 *offload) 1390 { 1391 struct __tc_taprio_qopt_offload *__offload; 1392 1393 __offload = container_of(offload, struct __tc_taprio_qopt_offload, 1394 offload); 1395 1396 refcount_inc(&__offload->users); 1397 1398 return offload; 1399 } 1400 EXPORT_SYMBOL_GPL(taprio_offload_get); 1401 1402 void taprio_offload_free(struct tc_taprio_qopt_offload *offload) 1403 { 1404 struct __tc_taprio_qopt_offload *__offload; 1405 1406 __offload = container_of(offload, struct __tc_taprio_qopt_offload, 1407 offload); 1408 1409 if (!refcount_dec_and_test(&__offload->users)) 1410 return; 1411 1412 kfree(__offload); 1413 } 1414 EXPORT_SYMBOL_GPL(taprio_offload_free); 1415 1416 /* The function will only serve to keep the pointers to the "oper" and "admin" 1417 * schedules valid in relation to their base times, so when calling dump() the 1418 * users looks at the right schedules. 1419 * When using full offload, the admin configuration is promoted to oper at the 1420 * base_time in the PHC time domain. But because the system time is not 1421 * necessarily in sync with that, we can't just trigger a hrtimer to call 1422 * switch_schedules at the right hardware time. 1423 * At the moment we call this by hand right away from taprio, but in the future 1424 * it will be useful to create a mechanism for drivers to notify taprio of the 1425 * offload state (PENDING, ACTIVE, INACTIVE) so it can be visible in dump(). 1426 * This is left as TODO. 1427 */ 1428 static void taprio_offload_config_changed(struct taprio_sched *q) 1429 { 1430 struct sched_gate_list *oper, *admin; 1431 1432 oper = rtnl_dereference(q->oper_sched); 1433 admin = rtnl_dereference(q->admin_sched); 1434 1435 switch_schedules(q, &admin, &oper); 1436 } 1437 1438 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask) 1439 { 1440 u32 i, queue_mask = 0; 1441 1442 for (i = 0; i < dev->num_tc; i++) { 1443 u32 offset, count; 1444 1445 if (!(tc_mask & BIT(i))) 1446 continue; 1447 1448 offset = dev->tc_to_txq[i].offset; 1449 count = dev->tc_to_txq[i].count; 1450 1451 queue_mask |= GENMASK(offset + count - 1, offset); 1452 } 1453 1454 return queue_mask; 1455 } 1456 1457 static void taprio_sched_to_offload(struct net_device *dev, 1458 struct sched_gate_list *sched, 1459 struct tc_taprio_qopt_offload *offload, 1460 const struct tc_taprio_caps *caps) 1461 { 1462 struct sched_entry *entry; 1463 int i = 0; 1464 1465 offload->base_time = sched->base_time; 1466 offload->cycle_time = sched->cycle_time; 1467 offload->cycle_time_extension = sched->cycle_time_extension; 1468 1469 list_for_each_entry(entry, &sched->entries, list) { 1470 struct tc_taprio_sched_entry *e = &offload->entries[i]; 1471 1472 e->command = entry->command; 1473 e->interval = entry->interval; 1474 if (caps->gate_mask_per_txq) 1475 e->gate_mask = tc_map_to_queue_mask(dev, 1476 entry->gate_mask); 1477 else 1478 e->gate_mask = entry->gate_mask; 1479 1480 i++; 1481 } 1482 1483 offload->num_entries = i; 1484 } 1485 1486 static void taprio_detect_broken_mqprio(struct taprio_sched *q) 1487 { 1488 struct net_device *dev = qdisc_dev(q->root); 1489 struct tc_taprio_caps caps; 1490 1491 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO, 1492 &caps, sizeof(caps)); 1493 1494 q->broken_mqprio = caps.broken_mqprio; 1495 if (q->broken_mqprio) 1496 static_branch_inc(&taprio_have_broken_mqprio); 1497 else 1498 static_branch_inc(&taprio_have_working_mqprio); 1499 1500 q->detected_mqprio = true; 1501 } 1502 1503 static void taprio_cleanup_broken_mqprio(struct taprio_sched *q) 1504 { 1505 if (!q->detected_mqprio) 1506 return; 1507 1508 if (q->broken_mqprio) 1509 static_branch_dec(&taprio_have_broken_mqprio); 1510 else 1511 static_branch_dec(&taprio_have_working_mqprio); 1512 } 1513 1514 static int taprio_enable_offload(struct net_device *dev, 1515 struct taprio_sched *q, 1516 struct sched_gate_list *sched, 1517 struct netlink_ext_ack *extack) 1518 { 1519 const struct net_device_ops *ops = dev->netdev_ops; 1520 struct tc_taprio_qopt_offload *offload; 1521 struct tc_taprio_caps caps; 1522 int tc, err = 0; 1523 1524 if (!ops->ndo_setup_tc) { 1525 NL_SET_ERR_MSG(extack, 1526 "Device does not support taprio offload"); 1527 return -EOPNOTSUPP; 1528 } 1529 1530 qdisc_offload_query_caps(dev, TC_SETUP_QDISC_TAPRIO, 1531 &caps, sizeof(caps)); 1532 1533 if (!caps.supports_queue_max_sdu) { 1534 for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 1535 if (q->max_sdu[tc]) { 1536 NL_SET_ERR_MSG_MOD(extack, 1537 "Device does not handle queueMaxSDU"); 1538 return -EOPNOTSUPP; 1539 } 1540 } 1541 } 1542 1543 offload = taprio_offload_alloc(sched->num_entries); 1544 if (!offload) { 1545 NL_SET_ERR_MSG(extack, 1546 "Not enough memory for enabling offload mode"); 1547 return -ENOMEM; 1548 } 1549 offload->cmd = TAPRIO_CMD_REPLACE; 1550 offload->extack = extack; 1551 mqprio_qopt_reconstruct(dev, &offload->mqprio.qopt); 1552 offload->mqprio.extack = extack; 1553 taprio_sched_to_offload(dev, sched, offload, &caps); 1554 mqprio_fp_to_offload(q->fp, &offload->mqprio); 1555 1556 for (tc = 0; tc < TC_MAX_QUEUE; tc++) 1557 offload->max_sdu[tc] = q->max_sdu[tc]; 1558 1559 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 1560 if (err < 0) { 1561 NL_SET_ERR_MSG_WEAK(extack, 1562 "Device failed to setup taprio offload"); 1563 goto done; 1564 } 1565 1566 q->offloaded = true; 1567 1568 done: 1569 /* The offload structure may linger around via a reference taken by the 1570 * device driver, so clear up the netlink extack pointer so that the 1571 * driver isn't tempted to dereference data which stopped being valid 1572 */ 1573 offload->extack = NULL; 1574 offload->mqprio.extack = NULL; 1575 taprio_offload_free(offload); 1576 1577 return err; 1578 } 1579 1580 static int taprio_disable_offload(struct net_device *dev, 1581 struct taprio_sched *q, 1582 struct netlink_ext_ack *extack) 1583 { 1584 const struct net_device_ops *ops = dev->netdev_ops; 1585 struct tc_taprio_qopt_offload *offload; 1586 int err; 1587 1588 if (!q->offloaded) 1589 return 0; 1590 1591 offload = taprio_offload_alloc(0); 1592 if (!offload) { 1593 NL_SET_ERR_MSG(extack, 1594 "Not enough memory to disable offload mode"); 1595 return -ENOMEM; 1596 } 1597 offload->cmd = TAPRIO_CMD_DESTROY; 1598 1599 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 1600 if (err < 0) { 1601 NL_SET_ERR_MSG(extack, 1602 "Device failed to disable offload"); 1603 goto out; 1604 } 1605 1606 q->offloaded = false; 1607 1608 out: 1609 taprio_offload_free(offload); 1610 1611 return err; 1612 } 1613 1614 /* If full offload is enabled, the only possible clockid is the net device's 1615 * PHC. For that reason, specifying a clockid through netlink is incorrect. 1616 * For txtime-assist, it is implicitly assumed that the device's PHC is kept 1617 * in sync with the specified clockid via a user space daemon such as phc2sys. 1618 * For both software taprio and txtime-assist, the clockid is used for the 1619 * hrtimer that advances the schedule and hence mandatory. 1620 */ 1621 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb, 1622 struct netlink_ext_ack *extack) 1623 { 1624 struct taprio_sched *q = qdisc_priv(sch); 1625 struct net_device *dev = qdisc_dev(sch); 1626 int err = -EINVAL; 1627 1628 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 1629 const struct ethtool_ops *ops = dev->ethtool_ops; 1630 struct kernel_ethtool_ts_info info = { 1631 .cmd = ETHTOOL_GET_TS_INFO, 1632 .phc_index = -1, 1633 }; 1634 1635 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1636 NL_SET_ERR_MSG(extack, 1637 "The 'clockid' cannot be specified for full offload"); 1638 goto out; 1639 } 1640 1641 if (ops && ops->get_ts_info) 1642 err = ops->get_ts_info(dev, &info); 1643 1644 if (err || info.phc_index < 0) { 1645 NL_SET_ERR_MSG(extack, 1646 "Device does not have a PTP clock"); 1647 err = -ENOTSUPP; 1648 goto out; 1649 } 1650 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) { 1651 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]); 1652 enum tk_offsets tk_offset; 1653 1654 /* We only support static clockids and we don't allow 1655 * for it to be modified after the first init. 1656 */ 1657 if (clockid < 0 || 1658 (q->clockid != -1 && q->clockid != clockid)) { 1659 NL_SET_ERR_MSG(extack, 1660 "Changing the 'clockid' of a running schedule is not supported"); 1661 err = -ENOTSUPP; 1662 goto out; 1663 } 1664 1665 switch (clockid) { 1666 case CLOCK_REALTIME: 1667 tk_offset = TK_OFFS_REAL; 1668 break; 1669 case CLOCK_MONOTONIC: 1670 tk_offset = TK_OFFS_MAX; 1671 break; 1672 case CLOCK_BOOTTIME: 1673 tk_offset = TK_OFFS_BOOT; 1674 break; 1675 case CLOCK_TAI: 1676 tk_offset = TK_OFFS_TAI; 1677 break; 1678 default: 1679 NL_SET_ERR_MSG(extack, "Invalid 'clockid'"); 1680 err = -EINVAL; 1681 goto out; 1682 } 1683 /* This pairs with READ_ONCE() in taprio_mono_to_any */ 1684 WRITE_ONCE(q->tk_offset, tk_offset); 1685 1686 q->clockid = clockid; 1687 } else { 1688 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory"); 1689 goto out; 1690 } 1691 1692 /* Everything went ok, return success. */ 1693 err = 0; 1694 1695 out: 1696 return err; 1697 } 1698 1699 static int taprio_parse_tc_entry(struct Qdisc *sch, 1700 struct nlattr *opt, 1701 u32 max_sdu[TC_QOPT_MAX_QUEUE], 1702 u32 fp[TC_QOPT_MAX_QUEUE], 1703 unsigned long *seen_tcs, 1704 struct netlink_ext_ack *extack) 1705 { 1706 struct nlattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1] = { }; 1707 struct net_device *dev = qdisc_dev(sch); 1708 int err, tc; 1709 u32 val; 1710 1711 err = nla_parse_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, opt, 1712 taprio_tc_policy, extack); 1713 if (err < 0) 1714 return err; 1715 1716 if (NL_REQ_ATTR_CHECK(extack, opt, tb, TCA_TAPRIO_TC_ENTRY_INDEX)) { 1717 NL_SET_ERR_MSG_MOD(extack, "TC entry index missing"); 1718 return -EINVAL; 1719 } 1720 1721 tc = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]); 1722 if (*seen_tcs & BIT(tc)) { 1723 NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_TC_ENTRY_INDEX], 1724 "Duplicate tc entry"); 1725 return -EINVAL; 1726 } 1727 1728 *seen_tcs |= BIT(tc); 1729 1730 if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) { 1731 val = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]); 1732 if (val > dev->max_mtu) { 1733 NL_SET_ERR_MSG_MOD(extack, "TC max SDU exceeds device max MTU"); 1734 return -ERANGE; 1735 } 1736 1737 max_sdu[tc] = val; 1738 } 1739 1740 if (tb[TCA_TAPRIO_TC_ENTRY_FP]) 1741 fp[tc] = nla_get_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]); 1742 1743 return 0; 1744 } 1745 1746 static int taprio_parse_tc_entries(struct Qdisc *sch, 1747 struct nlattr *opt, 1748 struct netlink_ext_ack *extack) 1749 { 1750 struct taprio_sched *q = qdisc_priv(sch); 1751 struct net_device *dev = qdisc_dev(sch); 1752 u32 max_sdu[TC_QOPT_MAX_QUEUE]; 1753 bool have_preemption = false; 1754 unsigned long seen_tcs = 0; 1755 u32 fp[TC_QOPT_MAX_QUEUE]; 1756 struct nlattr *n; 1757 int tc, rem; 1758 int err = 0; 1759 1760 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 1761 max_sdu[tc] = q->max_sdu[tc]; 1762 fp[tc] = q->fp[tc]; 1763 } 1764 1765 nla_for_each_nested_type(n, TCA_TAPRIO_ATTR_TC_ENTRY, opt, rem) { 1766 err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs, 1767 extack); 1768 if (err) 1769 return err; 1770 } 1771 1772 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) { 1773 q->max_sdu[tc] = max_sdu[tc]; 1774 q->fp[tc] = fp[tc]; 1775 if (fp[tc] != TC_FP_EXPRESS) 1776 have_preemption = true; 1777 } 1778 1779 if (have_preemption) { 1780 if (!FULL_OFFLOAD_IS_ENABLED(q->flags)) { 1781 NL_SET_ERR_MSG(extack, 1782 "Preemption only supported with full offload"); 1783 return -EOPNOTSUPP; 1784 } 1785 1786 if (!ethtool_dev_mm_supported(dev)) { 1787 NL_SET_ERR_MSG(extack, 1788 "Device does not support preemption"); 1789 return -EOPNOTSUPP; 1790 } 1791 } 1792 1793 return err; 1794 } 1795 1796 static int taprio_mqprio_cmp(const struct net_device *dev, 1797 const struct tc_mqprio_qopt *mqprio) 1798 { 1799 int i; 1800 1801 if (!mqprio || mqprio->num_tc != dev->num_tc) 1802 return -1; 1803 1804 for (i = 0; i < mqprio->num_tc; i++) 1805 if (dev->tc_to_txq[i].count != mqprio->count[i] || 1806 dev->tc_to_txq[i].offset != mqprio->offset[i]) 1807 return -1; 1808 1809 for (i = 0; i <= TC_BITMASK; i++) 1810 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i]) 1811 return -1; 1812 1813 return 0; 1814 } 1815 1816 static int taprio_change(struct Qdisc *sch, struct nlattr *opt, 1817 struct netlink_ext_ack *extack) 1818 { 1819 struct qdisc_size_table *stab = rtnl_dereference(sch->stab); 1820 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { }; 1821 struct sched_gate_list *oper, *admin, *new_admin; 1822 struct taprio_sched *q = qdisc_priv(sch); 1823 struct net_device *dev = qdisc_dev(sch); 1824 struct tc_mqprio_qopt *mqprio = NULL; 1825 unsigned long flags; 1826 u32 taprio_flags; 1827 ktime_t start; 1828 int i, err; 1829 1830 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt, 1831 taprio_policy, extack); 1832 if (err < 0) 1833 return err; 1834 1835 if (tb[TCA_TAPRIO_ATTR_PRIOMAP]) 1836 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]); 1837 1838 /* The semantics of the 'flags' argument in relation to 'change()' 1839 * requests, are interpreted following two rules (which are applied in 1840 * this order): (1) an omitted 'flags' argument is interpreted as 1841 * zero; (2) the 'flags' of a "running" taprio instance cannot be 1842 * changed. 1843 */ 1844 taprio_flags = nla_get_u32_default(tb[TCA_TAPRIO_ATTR_FLAGS], 0); 1845 1846 /* txtime-assist and full offload are mutually exclusive */ 1847 if ((taprio_flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) && 1848 (taprio_flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)) { 1849 NL_SET_ERR_MSG_ATTR(extack, tb[TCA_TAPRIO_ATTR_FLAGS], 1850 "TXTIME_ASSIST and FULL_OFFLOAD are mutually exclusive"); 1851 return -EINVAL; 1852 } 1853 1854 if (q->flags != TAPRIO_FLAGS_INVALID && q->flags != taprio_flags) { 1855 NL_SET_ERR_MSG_MOD(extack, 1856 "Changing 'flags' of a running schedule is not supported"); 1857 return -EOPNOTSUPP; 1858 } 1859 q->flags = taprio_flags; 1860 1861 /* Needed for length_to_duration() during netlink attribute parsing */ 1862 taprio_set_picos_per_byte(dev, q, extack); 1863 1864 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags); 1865 if (err < 0) 1866 return err; 1867 1868 err = taprio_parse_tc_entries(sch, opt, extack); 1869 if (err) 1870 return err; 1871 1872 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL); 1873 if (!new_admin) { 1874 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule"); 1875 return -ENOMEM; 1876 } 1877 INIT_LIST_HEAD(&new_admin->entries); 1878 1879 oper = rtnl_dereference(q->oper_sched); 1880 admin = rtnl_dereference(q->admin_sched); 1881 1882 /* no changes - no new mqprio settings */ 1883 if (!taprio_mqprio_cmp(dev, mqprio)) 1884 mqprio = NULL; 1885 1886 if (mqprio && (oper || admin)) { 1887 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported"); 1888 err = -ENOTSUPP; 1889 goto free_sched; 1890 } 1891 1892 if (mqprio) { 1893 err = netdev_set_num_tc(dev, mqprio->num_tc); 1894 if (err) 1895 goto free_sched; 1896 for (i = 0; i < mqprio->num_tc; i++) { 1897 netdev_set_tc_queue(dev, i, 1898 mqprio->count[i], 1899 mqprio->offset[i]); 1900 q->cur_txq[i] = mqprio->offset[i]; 1901 } 1902 1903 /* Always use supplied priority mappings */ 1904 for (i = 0; i <= TC_BITMASK; i++) 1905 netdev_set_prio_tc_map(dev, i, 1906 mqprio->prio_tc_map[i]); 1907 } 1908 1909 err = parse_taprio_schedule(q, tb, new_admin, extack); 1910 if (err < 0) 1911 goto free_sched; 1912 1913 if (new_admin->num_entries == 0) { 1914 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule"); 1915 err = -EINVAL; 1916 goto free_sched; 1917 } 1918 1919 err = taprio_parse_clockid(sch, tb, extack); 1920 if (err < 0) 1921 goto free_sched; 1922 1923 taprio_update_queue_max_sdu(q, new_admin, stab); 1924 1925 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1926 err = taprio_enable_offload(dev, q, new_admin, extack); 1927 else 1928 err = taprio_disable_offload(dev, q, extack); 1929 if (err) 1930 goto free_sched; 1931 1932 /* Protects against enqueue()/dequeue() */ 1933 spin_lock_bh(qdisc_lock(sch)); 1934 1935 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) { 1936 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) { 1937 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled"); 1938 err = -EINVAL; 1939 goto unlock; 1940 } 1941 1942 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]); 1943 } 1944 1945 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) && 1946 !FULL_OFFLOAD_IS_ENABLED(q->flags) && 1947 !hrtimer_active(&q->advance_timer)) { 1948 hrtimer_setup(&q->advance_timer, advance_sched, q->clockid, HRTIMER_MODE_ABS); 1949 } 1950 1951 err = taprio_get_start_time(sch, new_admin, &start); 1952 if (err < 0) { 1953 NL_SET_ERR_MSG(extack, "Internal error: failed get start time"); 1954 goto unlock; 1955 } 1956 1957 setup_txtime(q, new_admin, start); 1958 1959 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) { 1960 if (!oper) { 1961 rcu_assign_pointer(q->oper_sched, new_admin); 1962 err = 0; 1963 new_admin = NULL; 1964 goto unlock; 1965 } 1966 1967 /* Not going to race against advance_sched(), but still */ 1968 admin = rcu_replace_pointer(q->admin_sched, new_admin, 1969 lockdep_rtnl_is_held()); 1970 if (admin) 1971 call_rcu(&admin->rcu, taprio_free_sched_cb); 1972 } else { 1973 setup_first_end_time(q, new_admin, start); 1974 1975 /* Protects against advance_sched() */ 1976 spin_lock_irqsave(&q->current_entry_lock, flags); 1977 1978 taprio_start_sched(sch, start, new_admin); 1979 1980 admin = rcu_replace_pointer(q->admin_sched, new_admin, 1981 lockdep_rtnl_is_held()); 1982 if (admin) 1983 call_rcu(&admin->rcu, taprio_free_sched_cb); 1984 1985 spin_unlock_irqrestore(&q->current_entry_lock, flags); 1986 1987 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) 1988 taprio_offload_config_changed(q); 1989 } 1990 1991 new_admin = NULL; 1992 err = 0; 1993 1994 if (!stab) 1995 NL_SET_ERR_MSG_MOD(extack, 1996 "Size table not specified, frame length estimations may be inaccurate"); 1997 1998 unlock: 1999 spin_unlock_bh(qdisc_lock(sch)); 2000 2001 free_sched: 2002 if (new_admin) 2003 call_rcu(&new_admin->rcu, taprio_free_sched_cb); 2004 2005 return err; 2006 } 2007 2008 static void taprio_reset(struct Qdisc *sch) 2009 { 2010 struct taprio_sched *q = qdisc_priv(sch); 2011 struct net_device *dev = qdisc_dev(sch); 2012 int i; 2013 2014 hrtimer_cancel(&q->advance_timer); 2015 2016 if (q->qdiscs) { 2017 for (i = 0; i < dev->num_tx_queues; i++) 2018 if (q->qdiscs[i]) 2019 qdisc_reset(q->qdiscs[i]); 2020 } 2021 } 2022 2023 static void taprio_destroy(struct Qdisc *sch) 2024 { 2025 struct taprio_sched *q = qdisc_priv(sch); 2026 struct net_device *dev = qdisc_dev(sch); 2027 struct sched_gate_list *oper, *admin; 2028 unsigned int i; 2029 2030 list_del(&q->taprio_list); 2031 2032 /* Note that taprio_reset() might not be called if an error 2033 * happens in qdisc_create(), after taprio_init() has been called. 2034 */ 2035 hrtimer_cancel(&q->advance_timer); 2036 qdisc_synchronize(sch); 2037 2038 taprio_disable_offload(dev, q, NULL); 2039 2040 if (q->qdiscs) { 2041 for (i = 0; i < dev->num_tx_queues; i++) 2042 qdisc_put(q->qdiscs[i]); 2043 2044 kfree(q->qdiscs); 2045 } 2046 q->qdiscs = NULL; 2047 2048 netdev_reset_tc(dev); 2049 2050 oper = rtnl_dereference(q->oper_sched); 2051 admin = rtnl_dereference(q->admin_sched); 2052 2053 if (oper) 2054 call_rcu(&oper->rcu, taprio_free_sched_cb); 2055 2056 if (admin) 2057 call_rcu(&admin->rcu, taprio_free_sched_cb); 2058 2059 taprio_cleanup_broken_mqprio(q); 2060 } 2061 2062 static int taprio_init(struct Qdisc *sch, struct nlattr *opt, 2063 struct netlink_ext_ack *extack) 2064 { 2065 struct taprio_sched *q = qdisc_priv(sch); 2066 struct net_device *dev = qdisc_dev(sch); 2067 int i, tc; 2068 2069 spin_lock_init(&q->current_entry_lock); 2070 2071 hrtimer_setup(&q->advance_timer, advance_sched, CLOCK_TAI, HRTIMER_MODE_ABS); 2072 2073 q->root = sch; 2074 2075 /* We only support static clockids. Use an invalid value as default 2076 * and get the valid one on taprio_change(). 2077 */ 2078 q->clockid = -1; 2079 q->flags = TAPRIO_FLAGS_INVALID; 2080 2081 list_add(&q->taprio_list, &taprio_list); 2082 2083 if (sch->parent != TC_H_ROOT) { 2084 NL_SET_ERR_MSG_MOD(extack, "Can only be attached as root qdisc"); 2085 return -EOPNOTSUPP; 2086 } 2087 2088 if (!netif_is_multiqueue(dev)) { 2089 NL_SET_ERR_MSG_MOD(extack, "Multi-queue device is required"); 2090 return -EOPNOTSUPP; 2091 } 2092 2093 q->qdiscs = kcalloc(dev->num_tx_queues, sizeof(q->qdiscs[0]), 2094 GFP_KERNEL); 2095 if (!q->qdiscs) 2096 return -ENOMEM; 2097 2098 if (!opt) 2099 return -EINVAL; 2100 2101 for (i = 0; i < dev->num_tx_queues; i++) { 2102 struct netdev_queue *dev_queue; 2103 struct Qdisc *qdisc; 2104 2105 dev_queue = netdev_get_tx_queue(dev, i); 2106 qdisc = qdisc_create_dflt(dev_queue, 2107 &pfifo_qdisc_ops, 2108 TC_H_MAKE(TC_H_MAJ(sch->handle), 2109 TC_H_MIN(i + 1)), 2110 extack); 2111 if (!qdisc) 2112 return -ENOMEM; 2113 2114 if (i < dev->real_num_tx_queues) 2115 qdisc_hash_add(qdisc, false); 2116 2117 q->qdiscs[i] = qdisc; 2118 } 2119 2120 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) 2121 q->fp[tc] = TC_FP_EXPRESS; 2122 2123 taprio_detect_broken_mqprio(q); 2124 2125 return taprio_change(sch, opt, extack); 2126 } 2127 2128 static void taprio_attach(struct Qdisc *sch) 2129 { 2130 struct taprio_sched *q = qdisc_priv(sch); 2131 struct net_device *dev = qdisc_dev(sch); 2132 unsigned int ntx; 2133 2134 /* Attach underlying qdisc */ 2135 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) { 2136 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, ntx); 2137 struct Qdisc *old, *dev_queue_qdisc; 2138 2139 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 2140 struct Qdisc *qdisc = q->qdiscs[ntx]; 2141 2142 /* In offload mode, the root taprio qdisc is bypassed 2143 * and the netdev TX queues see the children directly 2144 */ 2145 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 2146 dev_queue_qdisc = qdisc; 2147 } else { 2148 /* In software mode, attach the root taprio qdisc 2149 * to all netdev TX queues, so that dev_qdisc_enqueue() 2150 * goes through taprio_enqueue(). 2151 */ 2152 dev_queue_qdisc = sch; 2153 } 2154 old = dev_graft_qdisc(dev_queue, dev_queue_qdisc); 2155 /* The qdisc's refcount requires to be elevated once 2156 * for each netdev TX queue it is grafted onto 2157 */ 2158 qdisc_refcount_inc(dev_queue_qdisc); 2159 if (old) 2160 qdisc_put(old); 2161 } 2162 } 2163 2164 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch, 2165 unsigned long cl) 2166 { 2167 struct net_device *dev = qdisc_dev(sch); 2168 unsigned long ntx = cl - 1; 2169 2170 if (ntx >= dev->num_tx_queues) 2171 return NULL; 2172 2173 return netdev_get_tx_queue(dev, ntx); 2174 } 2175 2176 static int taprio_graft(struct Qdisc *sch, unsigned long cl, 2177 struct Qdisc *new, struct Qdisc **old, 2178 struct netlink_ext_ack *extack) 2179 { 2180 struct taprio_sched *q = qdisc_priv(sch); 2181 struct net_device *dev = qdisc_dev(sch); 2182 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl); 2183 2184 if (!dev_queue) 2185 return -EINVAL; 2186 2187 if (dev->flags & IFF_UP) 2188 dev_deactivate(dev); 2189 2190 /* In offload mode, the child Qdisc is directly attached to the netdev 2191 * TX queue, and thus, we need to keep its refcount elevated in order 2192 * to counteract qdisc_graft()'s call to qdisc_put() once per TX queue. 2193 * However, save the reference to the new qdisc in the private array in 2194 * both software and offload cases, to have an up-to-date reference to 2195 * our children. 2196 */ 2197 *old = q->qdiscs[cl - 1]; 2198 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) { 2199 WARN_ON_ONCE(dev_graft_qdisc(dev_queue, new) != *old); 2200 if (new) 2201 qdisc_refcount_inc(new); 2202 if (*old) 2203 qdisc_put(*old); 2204 } 2205 2206 q->qdiscs[cl - 1] = new; 2207 if (new) 2208 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 2209 2210 if (dev->flags & IFF_UP) 2211 dev_activate(dev); 2212 2213 return 0; 2214 } 2215 2216 static int dump_entry(struct sk_buff *msg, 2217 const struct sched_entry *entry) 2218 { 2219 struct nlattr *item; 2220 2221 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY); 2222 if (!item) 2223 return -ENOSPC; 2224 2225 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index)) 2226 goto nla_put_failure; 2227 2228 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command)) 2229 goto nla_put_failure; 2230 2231 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, 2232 entry->gate_mask)) 2233 goto nla_put_failure; 2234 2235 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, 2236 entry->interval)) 2237 goto nla_put_failure; 2238 2239 return nla_nest_end(msg, item); 2240 2241 nla_put_failure: 2242 nla_nest_cancel(msg, item); 2243 return -1; 2244 } 2245 2246 static int dump_schedule(struct sk_buff *msg, 2247 const struct sched_gate_list *root) 2248 { 2249 struct nlattr *entry_list; 2250 struct sched_entry *entry; 2251 2252 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, 2253 root->base_time, TCA_TAPRIO_PAD)) 2254 return -1; 2255 2256 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME, 2257 root->cycle_time, TCA_TAPRIO_PAD)) 2258 return -1; 2259 2260 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION, 2261 root->cycle_time_extension, TCA_TAPRIO_PAD)) 2262 return -1; 2263 2264 entry_list = nla_nest_start_noflag(msg, 2265 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST); 2266 if (!entry_list) 2267 goto error_nest; 2268 2269 list_for_each_entry(entry, &root->entries, list) { 2270 if (dump_entry(msg, entry) < 0) 2271 goto error_nest; 2272 } 2273 2274 nla_nest_end(msg, entry_list); 2275 return 0; 2276 2277 error_nest: 2278 nla_nest_cancel(msg, entry_list); 2279 return -1; 2280 } 2281 2282 static int taprio_dump_tc_entries(struct sk_buff *skb, 2283 struct taprio_sched *q, 2284 struct sched_gate_list *sched) 2285 { 2286 struct nlattr *n; 2287 int tc; 2288 2289 for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 2290 n = nla_nest_start(skb, TCA_TAPRIO_ATTR_TC_ENTRY); 2291 if (!n) 2292 return -EMSGSIZE; 2293 2294 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_INDEX, tc)) 2295 goto nla_put_failure; 2296 2297 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_MAX_SDU, 2298 sched->max_sdu[tc])) 2299 goto nla_put_failure; 2300 2301 if (nla_put_u32(skb, TCA_TAPRIO_TC_ENTRY_FP, q->fp[tc])) 2302 goto nla_put_failure; 2303 2304 nla_nest_end(skb, n); 2305 } 2306 2307 return 0; 2308 2309 nla_put_failure: 2310 nla_nest_cancel(skb, n); 2311 return -EMSGSIZE; 2312 } 2313 2314 static int taprio_put_stat(struct sk_buff *skb, u64 val, u16 attrtype) 2315 { 2316 if (val == TAPRIO_STAT_NOT_SET) 2317 return 0; 2318 if (nla_put_u64_64bit(skb, attrtype, val, TCA_TAPRIO_OFFLOAD_STATS_PAD)) 2319 return -EMSGSIZE; 2320 return 0; 2321 } 2322 2323 static int taprio_dump_xstats(struct Qdisc *sch, struct gnet_dump *d, 2324 struct tc_taprio_qopt_offload *offload, 2325 struct tc_taprio_qopt_stats *stats) 2326 { 2327 struct net_device *dev = qdisc_dev(sch); 2328 const struct net_device_ops *ops; 2329 struct sk_buff *skb = d->skb; 2330 struct nlattr *xstats; 2331 int err; 2332 2333 ops = qdisc_dev(sch)->netdev_ops; 2334 2335 /* FIXME I could use qdisc_offload_dump_helper(), but that messes 2336 * with sch->flags depending on whether the device reports taprio 2337 * stats, and I'm not sure whether that's a good idea, considering 2338 * that stats are optional to the offload itself 2339 */ 2340 if (!ops->ndo_setup_tc) 2341 return 0; 2342 2343 memset(stats, 0xff, sizeof(*stats)); 2344 2345 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload); 2346 if (err == -EOPNOTSUPP) 2347 return 0; 2348 if (err) 2349 return err; 2350 2351 xstats = nla_nest_start(skb, TCA_STATS_APP); 2352 if (!xstats) 2353 goto err; 2354 2355 if (taprio_put_stat(skb, stats->window_drops, 2356 TCA_TAPRIO_OFFLOAD_STATS_WINDOW_DROPS) || 2357 taprio_put_stat(skb, stats->tx_overruns, 2358 TCA_TAPRIO_OFFLOAD_STATS_TX_OVERRUNS)) 2359 goto err_cancel; 2360 2361 nla_nest_end(skb, xstats); 2362 2363 return 0; 2364 2365 err_cancel: 2366 nla_nest_cancel(skb, xstats); 2367 err: 2368 return -EMSGSIZE; 2369 } 2370 2371 static int taprio_dump_stats(struct Qdisc *sch, struct gnet_dump *d) 2372 { 2373 struct tc_taprio_qopt_offload offload = { 2374 .cmd = TAPRIO_CMD_STATS, 2375 }; 2376 2377 return taprio_dump_xstats(sch, d, &offload, &offload.stats); 2378 } 2379 2380 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb) 2381 { 2382 struct taprio_sched *q = qdisc_priv(sch); 2383 struct net_device *dev = qdisc_dev(sch); 2384 struct sched_gate_list *oper, *admin; 2385 struct tc_mqprio_qopt opt = { 0 }; 2386 struct nlattr *nest, *sched_nest; 2387 2388 mqprio_qopt_reconstruct(dev, &opt); 2389 2390 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 2391 if (!nest) 2392 goto start_error; 2393 2394 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt)) 2395 goto options_error; 2396 2397 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) && 2398 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid)) 2399 goto options_error; 2400 2401 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags)) 2402 goto options_error; 2403 2404 if (q->txtime_delay && 2405 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay)) 2406 goto options_error; 2407 2408 rcu_read_lock(); 2409 2410 oper = rtnl_dereference(q->oper_sched); 2411 admin = rtnl_dereference(q->admin_sched); 2412 2413 if (oper && taprio_dump_tc_entries(skb, q, oper)) 2414 goto options_error_rcu; 2415 2416 if (oper && dump_schedule(skb, oper)) 2417 goto options_error_rcu; 2418 2419 if (!admin) 2420 goto done; 2421 2422 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED); 2423 if (!sched_nest) 2424 goto options_error_rcu; 2425 2426 if (dump_schedule(skb, admin)) 2427 goto admin_error; 2428 2429 nla_nest_end(skb, sched_nest); 2430 2431 done: 2432 rcu_read_unlock(); 2433 return nla_nest_end(skb, nest); 2434 2435 admin_error: 2436 nla_nest_cancel(skb, sched_nest); 2437 2438 options_error_rcu: 2439 rcu_read_unlock(); 2440 2441 options_error: 2442 nla_nest_cancel(skb, nest); 2443 2444 start_error: 2445 return -ENOSPC; 2446 } 2447 2448 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl) 2449 { 2450 struct taprio_sched *q = qdisc_priv(sch); 2451 struct net_device *dev = qdisc_dev(sch); 2452 unsigned int ntx = cl - 1; 2453 2454 if (ntx >= dev->num_tx_queues) 2455 return NULL; 2456 2457 return q->qdiscs[ntx]; 2458 } 2459 2460 static unsigned long taprio_find(struct Qdisc *sch, u32 classid) 2461 { 2462 unsigned int ntx = TC_H_MIN(classid); 2463 2464 if (!taprio_queue_get(sch, ntx)) 2465 return 0; 2466 return ntx; 2467 } 2468 2469 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl, 2470 struct sk_buff *skb, struct tcmsg *tcm) 2471 { 2472 struct Qdisc *child = taprio_leaf(sch, cl); 2473 2474 tcm->tcm_parent = TC_H_ROOT; 2475 tcm->tcm_handle |= TC_H_MIN(cl); 2476 tcm->tcm_info = child->handle; 2477 2478 return 0; 2479 } 2480 2481 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl, 2482 struct gnet_dump *d) 2483 __releases(d->lock) 2484 __acquires(d->lock) 2485 { 2486 struct Qdisc *child = taprio_leaf(sch, cl); 2487 struct tc_taprio_qopt_offload offload = { 2488 .cmd = TAPRIO_CMD_QUEUE_STATS, 2489 .queue_stats = { 2490 .queue = cl - 1, 2491 }, 2492 }; 2493 2494 if (gnet_stats_copy_basic(d, NULL, &child->bstats, true) < 0 || 2495 qdisc_qstats_copy(d, child) < 0) 2496 return -1; 2497 2498 return taprio_dump_xstats(sch, d, &offload, &offload.queue_stats.stats); 2499 } 2500 2501 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 2502 { 2503 struct net_device *dev = qdisc_dev(sch); 2504 unsigned long ntx; 2505 2506 if (arg->stop) 2507 return; 2508 2509 arg->count = arg->skip; 2510 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) { 2511 if (!tc_qdisc_stats_dump(sch, ntx + 1, arg)) 2512 break; 2513 } 2514 } 2515 2516 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch, 2517 struct tcmsg *tcm) 2518 { 2519 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent)); 2520 } 2521 2522 static const struct Qdisc_class_ops taprio_class_ops = { 2523 .graft = taprio_graft, 2524 .leaf = taprio_leaf, 2525 .find = taprio_find, 2526 .walk = taprio_walk, 2527 .dump = taprio_dump_class, 2528 .dump_stats = taprio_dump_class_stats, 2529 .select_queue = taprio_select_queue, 2530 }; 2531 2532 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = { 2533 .cl_ops = &taprio_class_ops, 2534 .id = "taprio", 2535 .priv_size = sizeof(struct taprio_sched), 2536 .init = taprio_init, 2537 .change = taprio_change, 2538 .destroy = taprio_destroy, 2539 .reset = taprio_reset, 2540 .attach = taprio_attach, 2541 .peek = taprio_peek, 2542 .dequeue = taprio_dequeue, 2543 .enqueue = taprio_enqueue, 2544 .dump = taprio_dump, 2545 .dump_stats = taprio_dump_stats, 2546 .owner = THIS_MODULE, 2547 }; 2548 MODULE_ALIAS_NET_SCH("taprio"); 2549 2550 static struct notifier_block taprio_device_notifier = { 2551 .notifier_call = taprio_dev_notifier, 2552 }; 2553 2554 static int __init taprio_module_init(void) 2555 { 2556 int err = register_netdevice_notifier(&taprio_device_notifier); 2557 2558 if (err) 2559 return err; 2560 2561 return register_qdisc(&taprio_qdisc_ops); 2562 } 2563 2564 static void __exit taprio_module_exit(void) 2565 { 2566 unregister_qdisc(&taprio_qdisc_ops); 2567 unregister_netdevice_notifier(&taprio_device_notifier); 2568 } 2569 2570 module_init(taprio_module_init); 2571 module_exit(taprio_module_exit); 2572 MODULE_LICENSE("GPL"); 2573 MODULE_DESCRIPTION("Time Aware Priority qdisc"); 2574