0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/ethtool.h>
0010 #include <linux/types.h>
0011 #include <linux/slab.h>
0012 #include <linux/kernel.h>
0013 #include <linux/string.h>
0014 #include <linux/list.h>
0015 #include <linux/errno.h>
0016 #include <linux/skbuff.h>
0017 #include <linux/math64.h>
0018 #include <linux/module.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/rcupdate.h>
0021 #include <linux/time.h>
0022 #include <net/netlink.h>
0023 #include <net/pkt_sched.h>
0024 #include <net/pkt_cls.h>
0025 #include <net/sch_generic.h>
0026 #include <net/sock.h>
0027 #include <net/tcp.h>
0028
0029 static LIST_HEAD(taprio_list);
0030 static DEFINE_SPINLOCK(taprio_list_lock);
0031
0032 #define TAPRIO_ALL_GATES_OPEN -1
0033
0034 #define TXTIME_ASSIST_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST)
0035 #define FULL_OFFLOAD_IS_ENABLED(flags) ((flags) & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD)
0036 #define TAPRIO_FLAGS_INVALID U32_MAX
0037
0038 struct sched_entry {
0039 struct list_head list;
0040
0041
0042
0043
0044
0045 ktime_t close_time;
0046 ktime_t next_txtime;
0047 atomic_t budget;
0048 int index;
0049 u32 gate_mask;
0050 u32 interval;
0051 u8 command;
0052 };
0053
0054 struct sched_gate_list {
0055 struct rcu_head rcu;
0056 struct list_head entries;
0057 size_t num_entries;
0058 ktime_t cycle_close_time;
0059 s64 cycle_time;
0060 s64 cycle_time_extension;
0061 s64 base_time;
0062 };
0063
0064 struct taprio_sched {
0065 struct Qdisc **qdiscs;
0066 struct Qdisc *root;
0067 u32 flags;
0068 enum tk_offsets tk_offset;
0069 int clockid;
0070 bool offloaded;
0071 atomic64_t picos_per_byte;
0072
0073
0074
0075
0076 spinlock_t current_entry_lock;
0077 struct sched_entry __rcu *current_entry;
0078 struct sched_gate_list __rcu *oper_sched;
0079 struct sched_gate_list __rcu *admin_sched;
0080 struct hrtimer advance_timer;
0081 struct list_head taprio_list;
0082 struct sk_buff *(*dequeue)(struct Qdisc *sch);
0083 struct sk_buff *(*peek)(struct Qdisc *sch);
0084 u32 txtime_delay;
0085 };
0086
0087 struct __tc_taprio_qopt_offload {
0088 refcount_t users;
0089 struct tc_taprio_qopt_offload offload;
0090 };
0091
0092 static ktime_t sched_base_time(const struct sched_gate_list *sched)
0093 {
0094 if (!sched)
0095 return KTIME_MAX;
0096
0097 return ns_to_ktime(sched->base_time);
0098 }
0099
0100 static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
0101 {
0102
0103 enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
0104
0105 switch (tk_offset) {
0106 case TK_OFFS_MAX:
0107 return mono;
0108 default:
0109 return ktime_mono_to_any(mono, tk_offset);
0110 }
0111 }
0112
0113 static ktime_t taprio_get_time(const struct taprio_sched *q)
0114 {
0115 return taprio_mono_to_any(q, ktime_get());
0116 }
0117
0118 static void taprio_free_sched_cb(struct rcu_head *head)
0119 {
0120 struct sched_gate_list *sched = container_of(head, struct sched_gate_list, rcu);
0121 struct sched_entry *entry, *n;
0122
0123 list_for_each_entry_safe(entry, n, &sched->entries, list) {
0124 list_del(&entry->list);
0125 kfree(entry);
0126 }
0127
0128 kfree(sched);
0129 }
0130
0131 static void switch_schedules(struct taprio_sched *q,
0132 struct sched_gate_list **admin,
0133 struct sched_gate_list **oper)
0134 {
0135 rcu_assign_pointer(q->oper_sched, *admin);
0136 rcu_assign_pointer(q->admin_sched, NULL);
0137
0138 if (*oper)
0139 call_rcu(&(*oper)->rcu, taprio_free_sched_cb);
0140
0141 *oper = *admin;
0142 *admin = NULL;
0143 }
0144
0145
0146 static s32 get_cycle_time_elapsed(struct sched_gate_list *sched, ktime_t time)
0147 {
0148 ktime_t time_since_sched_start;
0149 s32 time_elapsed;
0150
0151 time_since_sched_start = ktime_sub(time, sched->base_time);
0152 div_s64_rem(time_since_sched_start, sched->cycle_time, &time_elapsed);
0153
0154 return time_elapsed;
0155 }
0156
0157 static ktime_t get_interval_end_time(struct sched_gate_list *sched,
0158 struct sched_gate_list *admin,
0159 struct sched_entry *entry,
0160 ktime_t intv_start)
0161 {
0162 s32 cycle_elapsed = get_cycle_time_elapsed(sched, intv_start);
0163 ktime_t intv_end, cycle_ext_end, cycle_end;
0164
0165 cycle_end = ktime_add_ns(intv_start, sched->cycle_time - cycle_elapsed);
0166 intv_end = ktime_add_ns(intv_start, entry->interval);
0167 cycle_ext_end = ktime_add(cycle_end, sched->cycle_time_extension);
0168
0169 if (ktime_before(intv_end, cycle_end))
0170 return intv_end;
0171 else if (admin && admin != sched &&
0172 ktime_after(admin->base_time, cycle_end) &&
0173 ktime_before(admin->base_time, cycle_ext_end))
0174 return admin->base_time;
0175 else
0176 return cycle_end;
0177 }
0178
0179 static int length_to_duration(struct taprio_sched *q, int len)
0180 {
0181 return div_u64(len * atomic64_read(&q->picos_per_byte), PSEC_PER_NSEC);
0182 }
0183
0184
0185
0186
0187
0188 static struct sched_entry *find_entry_to_transmit(struct sk_buff *skb,
0189 struct Qdisc *sch,
0190 struct sched_gate_list *sched,
0191 struct sched_gate_list *admin,
0192 ktime_t time,
0193 ktime_t *interval_start,
0194 ktime_t *interval_end,
0195 bool validate_interval)
0196 {
0197 ktime_t curr_intv_start, curr_intv_end, cycle_end, packet_transmit_time;
0198 ktime_t earliest_txtime = KTIME_MAX, txtime, cycle, transmit_end_time;
0199 struct sched_entry *entry = NULL, *entry_found = NULL;
0200 struct taprio_sched *q = qdisc_priv(sch);
0201 struct net_device *dev = qdisc_dev(sch);
0202 bool entry_available = false;
0203 s32 cycle_elapsed;
0204 int tc, n;
0205
0206 tc = netdev_get_prio_tc_map(dev, skb->priority);
0207 packet_transmit_time = length_to_duration(q, qdisc_pkt_len(skb));
0208
0209 *interval_start = 0;
0210 *interval_end = 0;
0211
0212 if (!sched)
0213 return NULL;
0214
0215 cycle = sched->cycle_time;
0216 cycle_elapsed = get_cycle_time_elapsed(sched, time);
0217 curr_intv_end = ktime_sub_ns(time, cycle_elapsed);
0218 cycle_end = ktime_add_ns(curr_intv_end, cycle);
0219
0220 list_for_each_entry(entry, &sched->entries, list) {
0221 curr_intv_start = curr_intv_end;
0222 curr_intv_end = get_interval_end_time(sched, admin, entry,
0223 curr_intv_start);
0224
0225 if (ktime_after(curr_intv_start, cycle_end))
0226 break;
0227
0228 if (!(entry->gate_mask & BIT(tc)) ||
0229 packet_transmit_time > entry->interval)
0230 continue;
0231
0232 txtime = entry->next_txtime;
0233
0234 if (ktime_before(txtime, time) || validate_interval) {
0235 transmit_end_time = ktime_add_ns(time, packet_transmit_time);
0236 if ((ktime_before(curr_intv_start, time) &&
0237 ktime_before(transmit_end_time, curr_intv_end)) ||
0238 (ktime_after(curr_intv_start, time) && !validate_interval)) {
0239 entry_found = entry;
0240 *interval_start = curr_intv_start;
0241 *interval_end = curr_intv_end;
0242 break;
0243 } else if (!entry_available && !validate_interval) {
0244
0245
0246
0247 entry_available = true;
0248 entry_found = entry;
0249 *interval_start = ktime_add_ns(curr_intv_start, cycle);
0250 *interval_end = ktime_add_ns(curr_intv_end, cycle);
0251 }
0252 } else if (ktime_before(txtime, earliest_txtime) &&
0253 !entry_available) {
0254 earliest_txtime = txtime;
0255 entry_found = entry;
0256 n = div_s64(ktime_sub(txtime, curr_intv_start), cycle);
0257 *interval_start = ktime_add(curr_intv_start, n * cycle);
0258 *interval_end = ktime_add(curr_intv_end, n * cycle);
0259 }
0260 }
0261
0262 return entry_found;
0263 }
0264
0265 static bool is_valid_interval(struct sk_buff *skb, struct Qdisc *sch)
0266 {
0267 struct taprio_sched *q = qdisc_priv(sch);
0268 struct sched_gate_list *sched, *admin;
0269 ktime_t interval_start, interval_end;
0270 struct sched_entry *entry;
0271
0272 rcu_read_lock();
0273 sched = rcu_dereference(q->oper_sched);
0274 admin = rcu_dereference(q->admin_sched);
0275
0276 entry = find_entry_to_transmit(skb, sch, sched, admin, skb->tstamp,
0277 &interval_start, &interval_end, true);
0278 rcu_read_unlock();
0279
0280 return entry;
0281 }
0282
0283 static bool taprio_flags_valid(u32 flags)
0284 {
0285
0286 if (flags & ~(TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST |
0287 TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
0288 return false;
0289
0290 if ((flags & TCA_TAPRIO_ATTR_FLAG_TXTIME_ASSIST) &&
0291 (flags & TCA_TAPRIO_ATTR_FLAG_FULL_OFFLOAD))
0292 return false;
0293 return true;
0294 }
0295
0296
0297 static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
0298 {
0299 unsigned int offset = skb_network_offset(skb);
0300 const struct ipv6hdr *ipv6h;
0301 const struct iphdr *iph;
0302 struct ipv6hdr _ipv6h;
0303
0304 ipv6h = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
0305 if (!ipv6h)
0306 return 0;
0307
0308 if (ipv6h->version == 4) {
0309 iph = (struct iphdr *)ipv6h;
0310 offset += iph->ihl * 4;
0311
0312
0313
0314
0315 if (iph->protocol == IPPROTO_IPV6) {
0316 ipv6h = skb_header_pointer(skb, offset,
0317 sizeof(_ipv6h), &_ipv6h);
0318
0319 if (!ipv6h || ipv6h->nexthdr != IPPROTO_TCP)
0320 return 0;
0321 } else if (iph->protocol != IPPROTO_TCP) {
0322 return 0;
0323 }
0324 } else if (ipv6h->version == 6 && ipv6h->nexthdr != IPPROTO_TCP) {
0325 return 0;
0326 }
0327
0328 return taprio_mono_to_any(q, skb->skb_mstamp_ns);
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 static long get_packet_txtime(struct sk_buff *skb, struct Qdisc *sch)
0347 {
0348 ktime_t transmit_end_time, interval_end, interval_start, tcp_tstamp;
0349 struct taprio_sched *q = qdisc_priv(sch);
0350 struct sched_gate_list *sched, *admin;
0351 ktime_t minimum_time, now, txtime;
0352 int len, packet_transmit_time;
0353 struct sched_entry *entry;
0354 bool sched_changed;
0355
0356 now = taprio_get_time(q);
0357 minimum_time = ktime_add_ns(now, q->txtime_delay);
0358
0359 tcp_tstamp = get_tcp_tstamp(q, skb);
0360 minimum_time = max_t(ktime_t, minimum_time, tcp_tstamp);
0361
0362 rcu_read_lock();
0363 admin = rcu_dereference(q->admin_sched);
0364 sched = rcu_dereference(q->oper_sched);
0365 if (admin && ktime_after(minimum_time, admin->base_time))
0366 switch_schedules(q, &admin, &sched);
0367
0368
0369 if (!sched || ktime_before(minimum_time, sched->base_time)) {
0370 txtime = minimum_time;
0371 goto done;
0372 }
0373
0374 len = qdisc_pkt_len(skb);
0375 packet_transmit_time = length_to_duration(q, len);
0376
0377 do {
0378 sched_changed = false;
0379
0380 entry = find_entry_to_transmit(skb, sch, sched, admin,
0381 minimum_time,
0382 &interval_start, &interval_end,
0383 false);
0384 if (!entry) {
0385 txtime = 0;
0386 goto done;
0387 }
0388
0389 txtime = entry->next_txtime;
0390 txtime = max_t(ktime_t, txtime, minimum_time);
0391 txtime = max_t(ktime_t, txtime, interval_start);
0392
0393 if (admin && admin != sched &&
0394 ktime_after(txtime, admin->base_time)) {
0395 sched = admin;
0396 sched_changed = true;
0397 continue;
0398 }
0399
0400 transmit_end_time = ktime_add(txtime, packet_transmit_time);
0401 minimum_time = transmit_end_time;
0402
0403
0404
0405
0406 if (ktime_after(transmit_end_time, interval_end))
0407 entry->next_txtime = ktime_add(interval_start, sched->cycle_time);
0408 } while (sched_changed || ktime_after(transmit_end_time, interval_end));
0409
0410 entry->next_txtime = transmit_end_time;
0411
0412 done:
0413 rcu_read_unlock();
0414 return txtime;
0415 }
0416
0417 static int taprio_enqueue_one(struct sk_buff *skb, struct Qdisc *sch,
0418 struct Qdisc *child, struct sk_buff **to_free)
0419 {
0420 struct taprio_sched *q = qdisc_priv(sch);
0421
0422
0423 if (skb->sk && sk_fullsock(skb->sk) && sock_flag(skb->sk, SOCK_TXTIME)) {
0424 if (!is_valid_interval(skb, sch))
0425 return qdisc_drop(skb, sch, to_free);
0426 } else if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
0427 skb->tstamp = get_packet_txtime(skb, sch);
0428 if (!skb->tstamp)
0429 return qdisc_drop(skb, sch, to_free);
0430 }
0431
0432 qdisc_qstats_backlog_inc(sch, skb);
0433 sch->q.qlen++;
0434
0435 return qdisc_enqueue(skb, child, to_free);
0436 }
0437
0438 static int taprio_enqueue(struct sk_buff *skb, struct Qdisc *sch,
0439 struct sk_buff **to_free)
0440 {
0441 struct taprio_sched *q = qdisc_priv(sch);
0442 struct Qdisc *child;
0443 int queue;
0444
0445 if (unlikely(FULL_OFFLOAD_IS_ENABLED(q->flags))) {
0446 WARN_ONCE(1, "Trying to enqueue skb into the root of a taprio qdisc configured with full offload\n");
0447 return qdisc_drop(skb, sch, to_free);
0448 }
0449
0450 queue = skb_get_queue_mapping(skb);
0451
0452 child = q->qdiscs[queue];
0453 if (unlikely(!child))
0454 return qdisc_drop(skb, sch, to_free);
0455
0456
0457
0458
0459
0460
0461 if (skb_is_gso(skb) && !FULL_OFFLOAD_IS_ENABLED(q->flags)) {
0462 unsigned int slen = 0, numsegs = 0, len = qdisc_pkt_len(skb);
0463 netdev_features_t features = netif_skb_features(skb);
0464 struct sk_buff *segs, *nskb;
0465 int ret;
0466
0467 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
0468 if (IS_ERR_OR_NULL(segs))
0469 return qdisc_drop(skb, sch, to_free);
0470
0471 skb_list_walk_safe(segs, segs, nskb) {
0472 skb_mark_not_on_list(segs);
0473 qdisc_skb_cb(segs)->pkt_len = segs->len;
0474 slen += segs->len;
0475
0476 ret = taprio_enqueue_one(segs, sch, child, to_free);
0477 if (ret != NET_XMIT_SUCCESS) {
0478 if (net_xmit_drop_count(ret))
0479 qdisc_qstats_drop(sch);
0480 } else {
0481 numsegs++;
0482 }
0483 }
0484
0485 if (numsegs > 1)
0486 qdisc_tree_reduce_backlog(sch, 1 - numsegs, len - slen);
0487 consume_skb(skb);
0488
0489 return numsegs > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
0490 }
0491
0492 return taprio_enqueue_one(skb, sch, child, to_free);
0493 }
0494
0495 static struct sk_buff *taprio_peek_soft(struct Qdisc *sch)
0496 {
0497 struct taprio_sched *q = qdisc_priv(sch);
0498 struct net_device *dev = qdisc_dev(sch);
0499 struct sched_entry *entry;
0500 struct sk_buff *skb;
0501 u32 gate_mask;
0502 int i;
0503
0504 rcu_read_lock();
0505 entry = rcu_dereference(q->current_entry);
0506 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
0507 rcu_read_unlock();
0508
0509 if (!gate_mask)
0510 return NULL;
0511
0512 for (i = 0; i < dev->num_tx_queues; i++) {
0513 struct Qdisc *child = q->qdiscs[i];
0514 int prio;
0515 u8 tc;
0516
0517 if (unlikely(!child))
0518 continue;
0519
0520 skb = child->ops->peek(child);
0521 if (!skb)
0522 continue;
0523
0524 if (TXTIME_ASSIST_IS_ENABLED(q->flags))
0525 return skb;
0526
0527 prio = skb->priority;
0528 tc = netdev_get_prio_tc_map(dev, prio);
0529
0530 if (!(gate_mask & BIT(tc)))
0531 continue;
0532
0533 return skb;
0534 }
0535
0536 return NULL;
0537 }
0538
0539 static struct sk_buff *taprio_peek_offload(struct Qdisc *sch)
0540 {
0541 WARN_ONCE(1, "Trying to peek into the root of a taprio qdisc configured with full offload\n");
0542
0543 return NULL;
0544 }
0545
0546 static struct sk_buff *taprio_peek(struct Qdisc *sch)
0547 {
0548 struct taprio_sched *q = qdisc_priv(sch);
0549
0550 return q->peek(sch);
0551 }
0552
0553 static void taprio_set_budget(struct taprio_sched *q, struct sched_entry *entry)
0554 {
0555 atomic_set(&entry->budget,
0556 div64_u64((u64)entry->interval * PSEC_PER_NSEC,
0557 atomic64_read(&q->picos_per_byte)));
0558 }
0559
0560 static struct sk_buff *taprio_dequeue_soft(struct Qdisc *sch)
0561 {
0562 struct taprio_sched *q = qdisc_priv(sch);
0563 struct net_device *dev = qdisc_dev(sch);
0564 struct sk_buff *skb = NULL;
0565 struct sched_entry *entry;
0566 u32 gate_mask;
0567 int i;
0568
0569 rcu_read_lock();
0570 entry = rcu_dereference(q->current_entry);
0571
0572
0573
0574
0575
0576 gate_mask = entry ? entry->gate_mask : TAPRIO_ALL_GATES_OPEN;
0577
0578 if (!gate_mask)
0579 goto done;
0580
0581 for (i = 0; i < dev->num_tx_queues; i++) {
0582 struct Qdisc *child = q->qdiscs[i];
0583 ktime_t guard;
0584 int prio;
0585 int len;
0586 u8 tc;
0587
0588 if (unlikely(!child))
0589 continue;
0590
0591 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
0592 skb = child->ops->dequeue(child);
0593 if (!skb)
0594 continue;
0595 goto skb_found;
0596 }
0597
0598 skb = child->ops->peek(child);
0599 if (!skb)
0600 continue;
0601
0602 prio = skb->priority;
0603 tc = netdev_get_prio_tc_map(dev, prio);
0604
0605 if (!(gate_mask & BIT(tc))) {
0606 skb = NULL;
0607 continue;
0608 }
0609
0610 len = qdisc_pkt_len(skb);
0611 guard = ktime_add_ns(taprio_get_time(q),
0612 length_to_duration(q, len));
0613
0614
0615
0616
0617 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
0618 ktime_after(guard, entry->close_time)) {
0619 skb = NULL;
0620 continue;
0621 }
0622
0623
0624 if (gate_mask != TAPRIO_ALL_GATES_OPEN &&
0625 atomic_sub_return(len, &entry->budget) < 0) {
0626 skb = NULL;
0627 continue;
0628 }
0629
0630 skb = child->ops->dequeue(child);
0631 if (unlikely(!skb))
0632 goto done;
0633
0634 skb_found:
0635 qdisc_bstats_update(sch, skb);
0636 qdisc_qstats_backlog_dec(sch, skb);
0637 sch->q.qlen--;
0638
0639 goto done;
0640 }
0641
0642 done:
0643 rcu_read_unlock();
0644
0645 return skb;
0646 }
0647
0648 static struct sk_buff *taprio_dequeue_offload(struct Qdisc *sch)
0649 {
0650 WARN_ONCE(1, "Trying to dequeue from the root of a taprio qdisc configured with full offload\n");
0651
0652 return NULL;
0653 }
0654
0655 static struct sk_buff *taprio_dequeue(struct Qdisc *sch)
0656 {
0657 struct taprio_sched *q = qdisc_priv(sch);
0658
0659 return q->dequeue(sch);
0660 }
0661
0662 static bool should_restart_cycle(const struct sched_gate_list *oper,
0663 const struct sched_entry *entry)
0664 {
0665 if (list_is_last(&entry->list, &oper->entries))
0666 return true;
0667
0668 if (ktime_compare(entry->close_time, oper->cycle_close_time) == 0)
0669 return true;
0670
0671 return false;
0672 }
0673
0674 static bool should_change_schedules(const struct sched_gate_list *admin,
0675 const struct sched_gate_list *oper,
0676 ktime_t close_time)
0677 {
0678 ktime_t next_base_time, extension_time;
0679
0680 if (!admin)
0681 return false;
0682
0683 next_base_time = sched_base_time(admin);
0684
0685
0686
0687
0688 if (ktime_compare(next_base_time, close_time) <= 0)
0689 return true;
0690
0691
0692
0693
0694
0695
0696 extension_time = ktime_add_ns(close_time, oper->cycle_time_extension);
0697
0698
0699
0700
0701
0702 if (ktime_compare(next_base_time, extension_time) <= 0)
0703 return true;
0704
0705 return false;
0706 }
0707
0708 static enum hrtimer_restart advance_sched(struct hrtimer *timer)
0709 {
0710 struct taprio_sched *q = container_of(timer, struct taprio_sched,
0711 advance_timer);
0712 struct sched_gate_list *oper, *admin;
0713 struct sched_entry *entry, *next;
0714 struct Qdisc *sch = q->root;
0715 ktime_t close_time;
0716
0717 spin_lock(&q->current_entry_lock);
0718 entry = rcu_dereference_protected(q->current_entry,
0719 lockdep_is_held(&q->current_entry_lock));
0720 oper = rcu_dereference_protected(q->oper_sched,
0721 lockdep_is_held(&q->current_entry_lock));
0722 admin = rcu_dereference_protected(q->admin_sched,
0723 lockdep_is_held(&q->current_entry_lock));
0724
0725 if (!oper)
0726 switch_schedules(q, &admin, &oper);
0727
0728
0729
0730
0731
0732
0733
0734 if (unlikely(!entry || entry->close_time == oper->base_time)) {
0735 next = list_first_entry(&oper->entries, struct sched_entry,
0736 list);
0737 close_time = next->close_time;
0738 goto first_run;
0739 }
0740
0741 if (should_restart_cycle(oper, entry)) {
0742 next = list_first_entry(&oper->entries, struct sched_entry,
0743 list);
0744 oper->cycle_close_time = ktime_add_ns(oper->cycle_close_time,
0745 oper->cycle_time);
0746 } else {
0747 next = list_next_entry(entry, list);
0748 }
0749
0750 close_time = ktime_add_ns(entry->close_time, next->interval);
0751 close_time = min_t(ktime_t, close_time, oper->cycle_close_time);
0752
0753 if (should_change_schedules(admin, oper, close_time)) {
0754
0755
0756
0757 close_time = sched_base_time(admin);
0758 switch_schedules(q, &admin, &oper);
0759 }
0760
0761 next->close_time = close_time;
0762 taprio_set_budget(q, next);
0763
0764 first_run:
0765 rcu_assign_pointer(q->current_entry, next);
0766 spin_unlock(&q->current_entry_lock);
0767
0768 hrtimer_set_expires(&q->advance_timer, close_time);
0769
0770 rcu_read_lock();
0771 __netif_schedule(sch);
0772 rcu_read_unlock();
0773
0774 return HRTIMER_RESTART;
0775 }
0776
0777 static const struct nla_policy entry_policy[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = {
0778 [TCA_TAPRIO_SCHED_ENTRY_INDEX] = { .type = NLA_U32 },
0779 [TCA_TAPRIO_SCHED_ENTRY_CMD] = { .type = NLA_U8 },
0780 [TCA_TAPRIO_SCHED_ENTRY_GATE_MASK] = { .type = NLA_U32 },
0781 [TCA_TAPRIO_SCHED_ENTRY_INTERVAL] = { .type = NLA_U32 },
0782 };
0783
0784 static const struct nla_policy taprio_policy[TCA_TAPRIO_ATTR_MAX + 1] = {
0785 [TCA_TAPRIO_ATTR_PRIOMAP] = {
0786 .len = sizeof(struct tc_mqprio_qopt)
0787 },
0788 [TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST] = { .type = NLA_NESTED },
0789 [TCA_TAPRIO_ATTR_SCHED_BASE_TIME] = { .type = NLA_S64 },
0790 [TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY] = { .type = NLA_NESTED },
0791 [TCA_TAPRIO_ATTR_SCHED_CLOCKID] = { .type = NLA_S32 },
0792 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME] = { .type = NLA_S64 },
0793 [TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION] = { .type = NLA_S64 },
0794 [TCA_TAPRIO_ATTR_FLAGS] = { .type = NLA_U32 },
0795 [TCA_TAPRIO_ATTR_TXTIME_DELAY] = { .type = NLA_U32 },
0796 };
0797
0798 static int fill_sched_entry(struct taprio_sched *q, struct nlattr **tb,
0799 struct sched_entry *entry,
0800 struct netlink_ext_ack *extack)
0801 {
0802 int min_duration = length_to_duration(q, ETH_ZLEN);
0803 u32 interval = 0;
0804
0805 if (tb[TCA_TAPRIO_SCHED_ENTRY_CMD])
0806 entry->command = nla_get_u8(
0807 tb[TCA_TAPRIO_SCHED_ENTRY_CMD]);
0808
0809 if (tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK])
0810 entry->gate_mask = nla_get_u32(
0811 tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK]);
0812
0813 if (tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL])
0814 interval = nla_get_u32(
0815 tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL]);
0816
0817
0818
0819
0820 if (interval < min_duration) {
0821 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
0822 return -EINVAL;
0823 }
0824
0825 entry->interval = interval;
0826
0827 return 0;
0828 }
0829
0830 static int parse_sched_entry(struct taprio_sched *q, struct nlattr *n,
0831 struct sched_entry *entry, int index,
0832 struct netlink_ext_ack *extack)
0833 {
0834 struct nlattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1] = { };
0835 int err;
0836
0837 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, n,
0838 entry_policy, NULL);
0839 if (err < 0) {
0840 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
0841 return -EINVAL;
0842 }
0843
0844 entry->index = index;
0845
0846 return fill_sched_entry(q, tb, entry, extack);
0847 }
0848
0849 static int parse_sched_list(struct taprio_sched *q, struct nlattr *list,
0850 struct sched_gate_list *sched,
0851 struct netlink_ext_ack *extack)
0852 {
0853 struct nlattr *n;
0854 int err, rem;
0855 int i = 0;
0856
0857 if (!list)
0858 return -EINVAL;
0859
0860 nla_for_each_nested(n, list, rem) {
0861 struct sched_entry *entry;
0862
0863 if (nla_type(n) != TCA_TAPRIO_SCHED_ENTRY) {
0864 NL_SET_ERR_MSG(extack, "Attribute is not of type 'entry'");
0865 continue;
0866 }
0867
0868 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0869 if (!entry) {
0870 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
0871 return -ENOMEM;
0872 }
0873
0874 err = parse_sched_entry(q, n, entry, i, extack);
0875 if (err < 0) {
0876 kfree(entry);
0877 return err;
0878 }
0879
0880 list_add_tail(&entry->list, &sched->entries);
0881 i++;
0882 }
0883
0884 sched->num_entries = i;
0885
0886 return i;
0887 }
0888
0889 static int parse_taprio_schedule(struct taprio_sched *q, struct nlattr **tb,
0890 struct sched_gate_list *new,
0891 struct netlink_ext_ack *extack)
0892 {
0893 int err = 0;
0894
0895 if (tb[TCA_TAPRIO_ATTR_SCHED_SINGLE_ENTRY]) {
0896 NL_SET_ERR_MSG(extack, "Adding a single entry is not supported");
0897 return -ENOTSUPP;
0898 }
0899
0900 if (tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME])
0901 new->base_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME]);
0902
0903 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION])
0904 new->cycle_time_extension = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION]);
0905
0906 if (tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME])
0907 new->cycle_time = nla_get_s64(tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME]);
0908
0909 if (tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST])
0910 err = parse_sched_list(q, tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST],
0911 new, extack);
0912 if (err < 0)
0913 return err;
0914
0915 if (!new->cycle_time) {
0916 struct sched_entry *entry;
0917 ktime_t cycle = 0;
0918
0919 list_for_each_entry(entry, &new->entries, list)
0920 cycle = ktime_add_ns(cycle, entry->interval);
0921
0922 if (!cycle) {
0923 NL_SET_ERR_MSG(extack, "'cycle_time' can never be 0");
0924 return -EINVAL;
0925 }
0926
0927 new->cycle_time = cycle;
0928 }
0929
0930 return 0;
0931 }
0932
0933 static int taprio_parse_mqprio_opt(struct net_device *dev,
0934 struct tc_mqprio_qopt *qopt,
0935 struct netlink_ext_ack *extack,
0936 u32 taprio_flags)
0937 {
0938 int i, j;
0939
0940 if (!qopt && !dev->num_tc) {
0941 NL_SET_ERR_MSG(extack, "'mqprio' configuration is necessary");
0942 return -EINVAL;
0943 }
0944
0945
0946
0947
0948 if (dev->num_tc)
0949 return 0;
0950
0951
0952 if (qopt->num_tc > TC_MAX_QUEUE) {
0953 NL_SET_ERR_MSG(extack, "Number of traffic classes is outside valid range");
0954 return -EINVAL;
0955 }
0956
0957
0958 if (qopt->num_tc > dev->num_tx_queues) {
0959 NL_SET_ERR_MSG(extack, "Number of traffic classes is greater than number of HW queues");
0960 return -EINVAL;
0961 }
0962
0963
0964 for (i = 0; i <= TC_BITMASK; i++) {
0965 if (qopt->prio_tc_map[i] >= qopt->num_tc) {
0966 NL_SET_ERR_MSG(extack, "Invalid traffic class in priority to traffic class mapping");
0967 return -EINVAL;
0968 }
0969 }
0970
0971 for (i = 0; i < qopt->num_tc; i++) {
0972 unsigned int last = qopt->offset[i] + qopt->count[i];
0973
0974
0975
0976
0977 if (qopt->offset[i] >= dev->num_tx_queues ||
0978 !qopt->count[i] ||
0979 last > dev->real_num_tx_queues) {
0980 NL_SET_ERR_MSG(extack, "Invalid queue in traffic class to queue mapping");
0981 return -EINVAL;
0982 }
0983
0984 if (TXTIME_ASSIST_IS_ENABLED(taprio_flags))
0985 continue;
0986
0987
0988 for (j = i + 1; j < qopt->num_tc; j++) {
0989 if (last > qopt->offset[j]) {
0990 NL_SET_ERR_MSG(extack, "Detected overlap in the traffic class to queue mapping");
0991 return -EINVAL;
0992 }
0993 }
0994 }
0995
0996 return 0;
0997 }
0998
0999 static int taprio_get_start_time(struct Qdisc *sch,
1000 struct sched_gate_list *sched,
1001 ktime_t *start)
1002 {
1003 struct taprio_sched *q = qdisc_priv(sch);
1004 ktime_t now, base, cycle;
1005 s64 n;
1006
1007 base = sched_base_time(sched);
1008 now = taprio_get_time(q);
1009
1010 if (ktime_after(base, now)) {
1011 *start = base;
1012 return 0;
1013 }
1014
1015 cycle = sched->cycle_time;
1016
1017
1018
1019
1020
1021
1022 if (WARN_ON(!cycle))
1023 return -EFAULT;
1024
1025
1026
1027
1028 n = div64_s64(ktime_sub_ns(now, base), cycle);
1029 *start = ktime_add_ns(base, (n + 1) * cycle);
1030 return 0;
1031 }
1032
1033 static void setup_first_close_time(struct taprio_sched *q,
1034 struct sched_gate_list *sched, ktime_t base)
1035 {
1036 struct sched_entry *first;
1037 ktime_t cycle;
1038
1039 first = list_first_entry(&sched->entries,
1040 struct sched_entry, list);
1041
1042 cycle = sched->cycle_time;
1043
1044
1045 sched->cycle_close_time = ktime_add_ns(base, cycle);
1046
1047 first->close_time = ktime_add_ns(base, first->interval);
1048 taprio_set_budget(q, first);
1049 rcu_assign_pointer(q->current_entry, NULL);
1050 }
1051
1052 static void taprio_start_sched(struct Qdisc *sch,
1053 ktime_t start, struct sched_gate_list *new)
1054 {
1055 struct taprio_sched *q = qdisc_priv(sch);
1056 ktime_t expires;
1057
1058 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1059 return;
1060
1061 expires = hrtimer_get_expires(&q->advance_timer);
1062 if (expires == 0)
1063 expires = KTIME_MAX;
1064
1065
1066
1067
1068
1069 start = min_t(ktime_t, start, expires);
1070
1071 hrtimer_start(&q->advance_timer, start, HRTIMER_MODE_ABS);
1072 }
1073
1074 static void taprio_set_picos_per_byte(struct net_device *dev,
1075 struct taprio_sched *q)
1076 {
1077 struct ethtool_link_ksettings ecmd;
1078 int speed = SPEED_10;
1079 int picos_per_byte;
1080 int err;
1081
1082 err = __ethtool_get_link_ksettings(dev, &ecmd);
1083 if (err < 0)
1084 goto skip;
1085
1086 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
1087 speed = ecmd.base.speed;
1088
1089 skip:
1090 picos_per_byte = (USEC_PER_SEC * 8) / speed;
1091
1092 atomic64_set(&q->picos_per_byte, picos_per_byte);
1093 netdev_dbg(dev, "taprio: set %s's picos_per_byte to: %lld, linkspeed: %d\n",
1094 dev->name, (long long)atomic64_read(&q->picos_per_byte),
1095 ecmd.base.speed);
1096 }
1097
1098 static int taprio_dev_notifier(struct notifier_block *nb, unsigned long event,
1099 void *ptr)
1100 {
1101 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1102 struct net_device *qdev;
1103 struct taprio_sched *q;
1104 bool found = false;
1105
1106 ASSERT_RTNL();
1107
1108 if (event != NETDEV_UP && event != NETDEV_CHANGE)
1109 return NOTIFY_DONE;
1110
1111 spin_lock(&taprio_list_lock);
1112 list_for_each_entry(q, &taprio_list, taprio_list) {
1113 qdev = qdisc_dev(q->root);
1114 if (qdev == dev) {
1115 found = true;
1116 break;
1117 }
1118 }
1119 spin_unlock(&taprio_list_lock);
1120
1121 if (found)
1122 taprio_set_picos_per_byte(dev, q);
1123
1124 return NOTIFY_DONE;
1125 }
1126
1127 static void setup_txtime(struct taprio_sched *q,
1128 struct sched_gate_list *sched, ktime_t base)
1129 {
1130 struct sched_entry *entry;
1131 u32 interval = 0;
1132
1133 list_for_each_entry(entry, &sched->entries, list) {
1134 entry->next_txtime = ktime_add_ns(base, interval);
1135 interval += entry->interval;
1136 }
1137 }
1138
1139 static struct tc_taprio_qopt_offload *taprio_offload_alloc(int num_entries)
1140 {
1141 struct __tc_taprio_qopt_offload *__offload;
1142
1143 __offload = kzalloc(struct_size(__offload, offload.entries, num_entries),
1144 GFP_KERNEL);
1145 if (!__offload)
1146 return NULL;
1147
1148 refcount_set(&__offload->users, 1);
1149
1150 return &__offload->offload;
1151 }
1152
1153 struct tc_taprio_qopt_offload *taprio_offload_get(struct tc_taprio_qopt_offload
1154 *offload)
1155 {
1156 struct __tc_taprio_qopt_offload *__offload;
1157
1158 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1159 offload);
1160
1161 refcount_inc(&__offload->users);
1162
1163 return offload;
1164 }
1165 EXPORT_SYMBOL_GPL(taprio_offload_get);
1166
1167 void taprio_offload_free(struct tc_taprio_qopt_offload *offload)
1168 {
1169 struct __tc_taprio_qopt_offload *__offload;
1170
1171 __offload = container_of(offload, struct __tc_taprio_qopt_offload,
1172 offload);
1173
1174 if (!refcount_dec_and_test(&__offload->users))
1175 return;
1176
1177 kfree(__offload);
1178 }
1179 EXPORT_SYMBOL_GPL(taprio_offload_free);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 static void taprio_offload_config_changed(struct taprio_sched *q)
1194 {
1195 struct sched_gate_list *oper, *admin;
1196
1197 spin_lock(&q->current_entry_lock);
1198
1199 oper = rcu_dereference_protected(q->oper_sched,
1200 lockdep_is_held(&q->current_entry_lock));
1201 admin = rcu_dereference_protected(q->admin_sched,
1202 lockdep_is_held(&q->current_entry_lock));
1203
1204 switch_schedules(q, &admin, &oper);
1205
1206 spin_unlock(&q->current_entry_lock);
1207 }
1208
1209 static u32 tc_map_to_queue_mask(struct net_device *dev, u32 tc_mask)
1210 {
1211 u32 i, queue_mask = 0;
1212
1213 for (i = 0; i < dev->num_tc; i++) {
1214 u32 offset, count;
1215
1216 if (!(tc_mask & BIT(i)))
1217 continue;
1218
1219 offset = dev->tc_to_txq[i].offset;
1220 count = dev->tc_to_txq[i].count;
1221
1222 queue_mask |= GENMASK(offset + count - 1, offset);
1223 }
1224
1225 return queue_mask;
1226 }
1227
1228 static void taprio_sched_to_offload(struct net_device *dev,
1229 struct sched_gate_list *sched,
1230 struct tc_taprio_qopt_offload *offload)
1231 {
1232 struct sched_entry *entry;
1233 int i = 0;
1234
1235 offload->base_time = sched->base_time;
1236 offload->cycle_time = sched->cycle_time;
1237 offload->cycle_time_extension = sched->cycle_time_extension;
1238
1239 list_for_each_entry(entry, &sched->entries, list) {
1240 struct tc_taprio_sched_entry *e = &offload->entries[i];
1241
1242 e->command = entry->command;
1243 e->interval = entry->interval;
1244 e->gate_mask = tc_map_to_queue_mask(dev, entry->gate_mask);
1245
1246 i++;
1247 }
1248
1249 offload->num_entries = i;
1250 }
1251
1252 static int taprio_enable_offload(struct net_device *dev,
1253 struct taprio_sched *q,
1254 struct sched_gate_list *sched,
1255 struct netlink_ext_ack *extack)
1256 {
1257 const struct net_device_ops *ops = dev->netdev_ops;
1258 struct tc_taprio_qopt_offload *offload;
1259 int err = 0;
1260
1261 if (!ops->ndo_setup_tc) {
1262 NL_SET_ERR_MSG(extack,
1263 "Device does not support taprio offload");
1264 return -EOPNOTSUPP;
1265 }
1266
1267 offload = taprio_offload_alloc(sched->num_entries);
1268 if (!offload) {
1269 NL_SET_ERR_MSG(extack,
1270 "Not enough memory for enabling offload mode");
1271 return -ENOMEM;
1272 }
1273 offload->enable = 1;
1274 taprio_sched_to_offload(dev, sched, offload);
1275
1276 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1277 if (err < 0) {
1278 NL_SET_ERR_MSG(extack,
1279 "Device failed to setup taprio offload");
1280 goto done;
1281 }
1282
1283 q->offloaded = true;
1284
1285 done:
1286 taprio_offload_free(offload);
1287
1288 return err;
1289 }
1290
1291 static int taprio_disable_offload(struct net_device *dev,
1292 struct taprio_sched *q,
1293 struct netlink_ext_ack *extack)
1294 {
1295 const struct net_device_ops *ops = dev->netdev_ops;
1296 struct tc_taprio_qopt_offload *offload;
1297 int err;
1298
1299 if (!q->offloaded)
1300 return 0;
1301
1302 offload = taprio_offload_alloc(0);
1303 if (!offload) {
1304 NL_SET_ERR_MSG(extack,
1305 "Not enough memory to disable offload mode");
1306 return -ENOMEM;
1307 }
1308 offload->enable = 0;
1309
1310 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TAPRIO, offload);
1311 if (err < 0) {
1312 NL_SET_ERR_MSG(extack,
1313 "Device failed to disable offload");
1314 goto out;
1315 }
1316
1317 q->offloaded = false;
1318
1319 out:
1320 taprio_offload_free(offload);
1321
1322 return err;
1323 }
1324
1325
1326
1327
1328
1329
1330
1331
1332 static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
1333 struct netlink_ext_ack *extack)
1334 {
1335 struct taprio_sched *q = qdisc_priv(sch);
1336 struct net_device *dev = qdisc_dev(sch);
1337 int err = -EINVAL;
1338
1339 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1340 const struct ethtool_ops *ops = dev->ethtool_ops;
1341 struct ethtool_ts_info info = {
1342 .cmd = ETHTOOL_GET_TS_INFO,
1343 .phc_index = -1,
1344 };
1345
1346 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1347 NL_SET_ERR_MSG(extack,
1348 "The 'clockid' cannot be specified for full offload");
1349 goto out;
1350 }
1351
1352 if (ops && ops->get_ts_info)
1353 err = ops->get_ts_info(dev, &info);
1354
1355 if (err || info.phc_index < 0) {
1356 NL_SET_ERR_MSG(extack,
1357 "Device does not have a PTP clock");
1358 err = -ENOTSUPP;
1359 goto out;
1360 }
1361 } else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
1362 int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1363 enum tk_offsets tk_offset;
1364
1365
1366
1367
1368 if (clockid < 0 ||
1369 (q->clockid != -1 && q->clockid != clockid)) {
1370 NL_SET_ERR_MSG(extack,
1371 "Changing the 'clockid' of a running schedule is not supported");
1372 err = -ENOTSUPP;
1373 goto out;
1374 }
1375
1376 switch (clockid) {
1377 case CLOCK_REALTIME:
1378 tk_offset = TK_OFFS_REAL;
1379 break;
1380 case CLOCK_MONOTONIC:
1381 tk_offset = TK_OFFS_MAX;
1382 break;
1383 case CLOCK_BOOTTIME:
1384 tk_offset = TK_OFFS_BOOT;
1385 break;
1386 case CLOCK_TAI:
1387 tk_offset = TK_OFFS_TAI;
1388 break;
1389 default:
1390 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
1391 err = -EINVAL;
1392 goto out;
1393 }
1394
1395 WRITE_ONCE(q->tk_offset, tk_offset);
1396
1397 q->clockid = clockid;
1398 } else {
1399 NL_SET_ERR_MSG(extack, "Specifying a 'clockid' is mandatory");
1400 goto out;
1401 }
1402
1403
1404 err = 0;
1405
1406 out:
1407 return err;
1408 }
1409
1410 static int taprio_mqprio_cmp(const struct net_device *dev,
1411 const struct tc_mqprio_qopt *mqprio)
1412 {
1413 int i;
1414
1415 if (!mqprio || mqprio->num_tc != dev->num_tc)
1416 return -1;
1417
1418 for (i = 0; i < mqprio->num_tc; i++)
1419 if (dev->tc_to_txq[i].count != mqprio->count[i] ||
1420 dev->tc_to_txq[i].offset != mqprio->offset[i])
1421 return -1;
1422
1423 for (i = 0; i <= TC_BITMASK; i++)
1424 if (dev->prio_tc_map[i] != mqprio->prio_tc_map[i])
1425 return -1;
1426
1427 return 0;
1428 }
1429
1430
1431
1432
1433
1434
1435
1436 static int taprio_new_flags(const struct nlattr *attr, u32 old,
1437 struct netlink_ext_ack *extack)
1438 {
1439 u32 new = 0;
1440
1441 if (attr)
1442 new = nla_get_u32(attr);
1443
1444 if (old != TAPRIO_FLAGS_INVALID && old != new) {
1445 NL_SET_ERR_MSG_MOD(extack, "Changing 'flags' of a running schedule is not supported");
1446 return -EOPNOTSUPP;
1447 }
1448
1449 if (!taprio_flags_valid(new)) {
1450 NL_SET_ERR_MSG_MOD(extack, "Specified 'flags' are not valid");
1451 return -EINVAL;
1452 }
1453
1454 return new;
1455 }
1456
1457 static int taprio_change(struct Qdisc *sch, struct nlattr *opt,
1458 struct netlink_ext_ack *extack)
1459 {
1460 struct nlattr *tb[TCA_TAPRIO_ATTR_MAX + 1] = { };
1461 struct sched_gate_list *oper, *admin, *new_admin;
1462 struct taprio_sched *q = qdisc_priv(sch);
1463 struct net_device *dev = qdisc_dev(sch);
1464 struct tc_mqprio_qopt *mqprio = NULL;
1465 unsigned long flags;
1466 ktime_t start;
1467 int i, err;
1468
1469 err = nla_parse_nested_deprecated(tb, TCA_TAPRIO_ATTR_MAX, opt,
1470 taprio_policy, extack);
1471 if (err < 0)
1472 return err;
1473
1474 if (tb[TCA_TAPRIO_ATTR_PRIOMAP])
1475 mqprio = nla_data(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
1476
1477 err = taprio_new_flags(tb[TCA_TAPRIO_ATTR_FLAGS],
1478 q->flags, extack);
1479 if (err < 0)
1480 return err;
1481
1482 q->flags = err;
1483
1484 err = taprio_parse_mqprio_opt(dev, mqprio, extack, q->flags);
1485 if (err < 0)
1486 return err;
1487
1488 new_admin = kzalloc(sizeof(*new_admin), GFP_KERNEL);
1489 if (!new_admin) {
1490 NL_SET_ERR_MSG(extack, "Not enough memory for a new schedule");
1491 return -ENOMEM;
1492 }
1493 INIT_LIST_HEAD(&new_admin->entries);
1494
1495 rcu_read_lock();
1496 oper = rcu_dereference(q->oper_sched);
1497 admin = rcu_dereference(q->admin_sched);
1498 rcu_read_unlock();
1499
1500
1501 if (!taprio_mqprio_cmp(dev, mqprio))
1502 mqprio = NULL;
1503
1504 if (mqprio && (oper || admin)) {
1505 NL_SET_ERR_MSG(extack, "Changing the traffic mapping of a running schedule is not supported");
1506 err = -ENOTSUPP;
1507 goto free_sched;
1508 }
1509
1510 err = parse_taprio_schedule(q, tb, new_admin, extack);
1511 if (err < 0)
1512 goto free_sched;
1513
1514 if (new_admin->num_entries == 0) {
1515 NL_SET_ERR_MSG(extack, "There should be at least one entry in the schedule");
1516 err = -EINVAL;
1517 goto free_sched;
1518 }
1519
1520 err = taprio_parse_clockid(sch, tb, extack);
1521 if (err < 0)
1522 goto free_sched;
1523
1524 taprio_set_picos_per_byte(dev, q);
1525
1526 if (mqprio) {
1527 err = netdev_set_num_tc(dev, mqprio->num_tc);
1528 if (err)
1529 goto free_sched;
1530 for (i = 0; i < mqprio->num_tc; i++)
1531 netdev_set_tc_queue(dev, i,
1532 mqprio->count[i],
1533 mqprio->offset[i]);
1534
1535
1536 for (i = 0; i <= TC_BITMASK; i++)
1537 netdev_set_prio_tc_map(dev, i,
1538 mqprio->prio_tc_map[i]);
1539 }
1540
1541 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1542 err = taprio_enable_offload(dev, q, new_admin, extack);
1543 else
1544 err = taprio_disable_offload(dev, q, extack);
1545 if (err)
1546 goto free_sched;
1547
1548
1549 spin_lock_bh(qdisc_lock(sch));
1550
1551 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
1552 if (!TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1553 NL_SET_ERR_MSG_MOD(extack, "txtime-delay can only be set when txtime-assist mode is enabled");
1554 err = -EINVAL;
1555 goto unlock;
1556 }
1557
1558 q->txtime_delay = nla_get_u32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
1559 }
1560
1561 if (!TXTIME_ASSIST_IS_ENABLED(q->flags) &&
1562 !FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1563 !hrtimer_active(&q->advance_timer)) {
1564 hrtimer_init(&q->advance_timer, q->clockid, HRTIMER_MODE_ABS);
1565 q->advance_timer.function = advance_sched;
1566 }
1567
1568 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1569 q->dequeue = taprio_dequeue_offload;
1570 q->peek = taprio_peek_offload;
1571 } else {
1572
1573
1574
1575 q->dequeue = taprio_dequeue_soft;
1576 q->peek = taprio_peek_soft;
1577 }
1578
1579 err = taprio_get_start_time(sch, new_admin, &start);
1580 if (err < 0) {
1581 NL_SET_ERR_MSG(extack, "Internal error: failed get start time");
1582 goto unlock;
1583 }
1584
1585 setup_txtime(q, new_admin, start);
1586
1587 if (TXTIME_ASSIST_IS_ENABLED(q->flags)) {
1588 if (!oper) {
1589 rcu_assign_pointer(q->oper_sched, new_admin);
1590 err = 0;
1591 new_admin = NULL;
1592 goto unlock;
1593 }
1594
1595 rcu_assign_pointer(q->admin_sched, new_admin);
1596 if (admin)
1597 call_rcu(&admin->rcu, taprio_free_sched_cb);
1598 } else {
1599 setup_first_close_time(q, new_admin, start);
1600
1601
1602 spin_lock_irqsave(&q->current_entry_lock, flags);
1603
1604 taprio_start_sched(sch, start, new_admin);
1605
1606 rcu_assign_pointer(q->admin_sched, new_admin);
1607 if (admin)
1608 call_rcu(&admin->rcu, taprio_free_sched_cb);
1609
1610 spin_unlock_irqrestore(&q->current_entry_lock, flags);
1611
1612 if (FULL_OFFLOAD_IS_ENABLED(q->flags))
1613 taprio_offload_config_changed(q);
1614 }
1615
1616 new_admin = NULL;
1617 err = 0;
1618
1619 unlock:
1620 spin_unlock_bh(qdisc_lock(sch));
1621
1622 free_sched:
1623 if (new_admin)
1624 call_rcu(&new_admin->rcu, taprio_free_sched_cb);
1625
1626 return err;
1627 }
1628
1629 static void taprio_reset(struct Qdisc *sch)
1630 {
1631 struct taprio_sched *q = qdisc_priv(sch);
1632 struct net_device *dev = qdisc_dev(sch);
1633 int i;
1634
1635 hrtimer_cancel(&q->advance_timer);
1636 if (q->qdiscs) {
1637 for (i = 0; i < dev->num_tx_queues; i++)
1638 if (q->qdiscs[i])
1639 qdisc_reset(q->qdiscs[i]);
1640 }
1641 sch->qstats.backlog = 0;
1642 sch->q.qlen = 0;
1643 }
1644
1645 static void taprio_destroy(struct Qdisc *sch)
1646 {
1647 struct taprio_sched *q = qdisc_priv(sch);
1648 struct net_device *dev = qdisc_dev(sch);
1649 unsigned int i;
1650
1651 spin_lock(&taprio_list_lock);
1652 list_del(&q->taprio_list);
1653 spin_unlock(&taprio_list_lock);
1654
1655
1656
1657
1658 hrtimer_cancel(&q->advance_timer);
1659
1660 taprio_disable_offload(dev, q, NULL);
1661
1662 if (q->qdiscs) {
1663 for (i = 0; i < dev->num_tx_queues; i++)
1664 qdisc_put(q->qdiscs[i]);
1665
1666 kfree(q->qdiscs);
1667 }
1668 q->qdiscs = NULL;
1669
1670 netdev_reset_tc(dev);
1671
1672 if (q->oper_sched)
1673 call_rcu(&q->oper_sched->rcu, taprio_free_sched_cb);
1674
1675 if (q->admin_sched)
1676 call_rcu(&q->admin_sched->rcu, taprio_free_sched_cb);
1677 }
1678
1679 static int taprio_init(struct Qdisc *sch, struct nlattr *opt,
1680 struct netlink_ext_ack *extack)
1681 {
1682 struct taprio_sched *q = qdisc_priv(sch);
1683 struct net_device *dev = qdisc_dev(sch);
1684 int i;
1685
1686 spin_lock_init(&q->current_entry_lock);
1687
1688 hrtimer_init(&q->advance_timer, CLOCK_TAI, HRTIMER_MODE_ABS);
1689 q->advance_timer.function = advance_sched;
1690
1691 q->dequeue = taprio_dequeue_soft;
1692 q->peek = taprio_peek_soft;
1693
1694 q->root = sch;
1695
1696
1697
1698
1699 q->clockid = -1;
1700 q->flags = TAPRIO_FLAGS_INVALID;
1701
1702 spin_lock(&taprio_list_lock);
1703 list_add(&q->taprio_list, &taprio_list);
1704 spin_unlock(&taprio_list_lock);
1705
1706 if (sch->parent != TC_H_ROOT)
1707 return -EOPNOTSUPP;
1708
1709 if (!netif_is_multiqueue(dev))
1710 return -EOPNOTSUPP;
1711
1712
1713 q->qdiscs = kcalloc(dev->num_tx_queues,
1714 sizeof(q->qdiscs[0]),
1715 GFP_KERNEL);
1716
1717 if (!q->qdiscs)
1718 return -ENOMEM;
1719
1720 if (!opt)
1721 return -EINVAL;
1722
1723 for (i = 0; i < dev->num_tx_queues; i++) {
1724 struct netdev_queue *dev_queue;
1725 struct Qdisc *qdisc;
1726
1727 dev_queue = netdev_get_tx_queue(dev, i);
1728 qdisc = qdisc_create_dflt(dev_queue,
1729 &pfifo_qdisc_ops,
1730 TC_H_MAKE(TC_H_MAJ(sch->handle),
1731 TC_H_MIN(i + 1)),
1732 extack);
1733 if (!qdisc)
1734 return -ENOMEM;
1735
1736 if (i < dev->real_num_tx_queues)
1737 qdisc_hash_add(qdisc, false);
1738
1739 q->qdiscs[i] = qdisc;
1740 }
1741
1742 return taprio_change(sch, opt, extack);
1743 }
1744
1745 static void taprio_attach(struct Qdisc *sch)
1746 {
1747 struct taprio_sched *q = qdisc_priv(sch);
1748 struct net_device *dev = qdisc_dev(sch);
1749 unsigned int ntx;
1750
1751
1752 for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
1753 struct Qdisc *qdisc = q->qdiscs[ntx];
1754 struct Qdisc *old;
1755
1756 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1757 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1758 old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
1759 } else {
1760 old = dev_graft_qdisc(qdisc->dev_queue, sch);
1761 qdisc_refcount_inc(sch);
1762 }
1763 if (old)
1764 qdisc_put(old);
1765 }
1766
1767
1768 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1769 kfree(q->qdiscs);
1770 q->qdiscs = NULL;
1771 }
1772 }
1773
1774 static struct netdev_queue *taprio_queue_get(struct Qdisc *sch,
1775 unsigned long cl)
1776 {
1777 struct net_device *dev = qdisc_dev(sch);
1778 unsigned long ntx = cl - 1;
1779
1780 if (ntx >= dev->num_tx_queues)
1781 return NULL;
1782
1783 return netdev_get_tx_queue(dev, ntx);
1784 }
1785
1786 static int taprio_graft(struct Qdisc *sch, unsigned long cl,
1787 struct Qdisc *new, struct Qdisc **old,
1788 struct netlink_ext_ack *extack)
1789 {
1790 struct taprio_sched *q = qdisc_priv(sch);
1791 struct net_device *dev = qdisc_dev(sch);
1792 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1793
1794 if (!dev_queue)
1795 return -EINVAL;
1796
1797 if (dev->flags & IFF_UP)
1798 dev_deactivate(dev);
1799
1800 if (FULL_OFFLOAD_IS_ENABLED(q->flags)) {
1801 *old = dev_graft_qdisc(dev_queue, new);
1802 } else {
1803 *old = q->qdiscs[cl - 1];
1804 q->qdiscs[cl - 1] = new;
1805 }
1806
1807 if (new)
1808 new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1809
1810 if (dev->flags & IFF_UP)
1811 dev_activate(dev);
1812
1813 return 0;
1814 }
1815
1816 static int dump_entry(struct sk_buff *msg,
1817 const struct sched_entry *entry)
1818 {
1819 struct nlattr *item;
1820
1821 item = nla_nest_start_noflag(msg, TCA_TAPRIO_SCHED_ENTRY);
1822 if (!item)
1823 return -ENOSPC;
1824
1825 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INDEX, entry->index))
1826 goto nla_put_failure;
1827
1828 if (nla_put_u8(msg, TCA_TAPRIO_SCHED_ENTRY_CMD, entry->command))
1829 goto nla_put_failure;
1830
1831 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK,
1832 entry->gate_mask))
1833 goto nla_put_failure;
1834
1835 if (nla_put_u32(msg, TCA_TAPRIO_SCHED_ENTRY_INTERVAL,
1836 entry->interval))
1837 goto nla_put_failure;
1838
1839 return nla_nest_end(msg, item);
1840
1841 nla_put_failure:
1842 nla_nest_cancel(msg, item);
1843 return -1;
1844 }
1845
1846 static int dump_schedule(struct sk_buff *msg,
1847 const struct sched_gate_list *root)
1848 {
1849 struct nlattr *entry_list;
1850 struct sched_entry *entry;
1851
1852 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_BASE_TIME,
1853 root->base_time, TCA_TAPRIO_PAD))
1854 return -1;
1855
1856 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
1857 root->cycle_time, TCA_TAPRIO_PAD))
1858 return -1;
1859
1860 if (nla_put_s64(msg, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
1861 root->cycle_time_extension, TCA_TAPRIO_PAD))
1862 return -1;
1863
1864 entry_list = nla_nest_start_noflag(msg,
1865 TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST);
1866 if (!entry_list)
1867 goto error_nest;
1868
1869 list_for_each_entry(entry, &root->entries, list) {
1870 if (dump_entry(msg, entry) < 0)
1871 goto error_nest;
1872 }
1873
1874 nla_nest_end(msg, entry_list);
1875 return 0;
1876
1877 error_nest:
1878 nla_nest_cancel(msg, entry_list);
1879 return -1;
1880 }
1881
1882 static int taprio_dump(struct Qdisc *sch, struct sk_buff *skb)
1883 {
1884 struct taprio_sched *q = qdisc_priv(sch);
1885 struct net_device *dev = qdisc_dev(sch);
1886 struct sched_gate_list *oper, *admin;
1887 struct tc_mqprio_qopt opt = { 0 };
1888 struct nlattr *nest, *sched_nest;
1889 unsigned int i;
1890
1891 rcu_read_lock();
1892 oper = rcu_dereference(q->oper_sched);
1893 admin = rcu_dereference(q->admin_sched);
1894
1895 opt.num_tc = netdev_get_num_tc(dev);
1896 memcpy(opt.prio_tc_map, dev->prio_tc_map, sizeof(opt.prio_tc_map));
1897
1898 for (i = 0; i < netdev_get_num_tc(dev); i++) {
1899 opt.count[i] = dev->tc_to_txq[i].count;
1900 opt.offset[i] = dev->tc_to_txq[i].offset;
1901 }
1902
1903 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1904 if (!nest)
1905 goto start_error;
1906
1907 if (nla_put(skb, TCA_TAPRIO_ATTR_PRIOMAP, sizeof(opt), &opt))
1908 goto options_error;
1909
1910 if (!FULL_OFFLOAD_IS_ENABLED(q->flags) &&
1911 nla_put_s32(skb, TCA_TAPRIO_ATTR_SCHED_CLOCKID, q->clockid))
1912 goto options_error;
1913
1914 if (q->flags && nla_put_u32(skb, TCA_TAPRIO_ATTR_FLAGS, q->flags))
1915 goto options_error;
1916
1917 if (q->txtime_delay &&
1918 nla_put_u32(skb, TCA_TAPRIO_ATTR_TXTIME_DELAY, q->txtime_delay))
1919 goto options_error;
1920
1921 if (oper && dump_schedule(skb, oper))
1922 goto options_error;
1923
1924 if (!admin)
1925 goto done;
1926
1927 sched_nest = nla_nest_start_noflag(skb, TCA_TAPRIO_ATTR_ADMIN_SCHED);
1928 if (!sched_nest)
1929 goto options_error;
1930
1931 if (dump_schedule(skb, admin))
1932 goto admin_error;
1933
1934 nla_nest_end(skb, sched_nest);
1935
1936 done:
1937 rcu_read_unlock();
1938
1939 return nla_nest_end(skb, nest);
1940
1941 admin_error:
1942 nla_nest_cancel(skb, sched_nest);
1943
1944 options_error:
1945 nla_nest_cancel(skb, nest);
1946
1947 start_error:
1948 rcu_read_unlock();
1949 return -ENOSPC;
1950 }
1951
1952 static struct Qdisc *taprio_leaf(struct Qdisc *sch, unsigned long cl)
1953 {
1954 struct taprio_sched *q = qdisc_priv(sch);
1955 struct net_device *dev = qdisc_dev(sch);
1956 unsigned int ntx = cl - 1;
1957
1958 if (ntx >= dev->num_tx_queues)
1959 return NULL;
1960
1961 return q->qdiscs[ntx];
1962 }
1963
1964 static unsigned long taprio_find(struct Qdisc *sch, u32 classid)
1965 {
1966 unsigned int ntx = TC_H_MIN(classid);
1967
1968 if (!taprio_queue_get(sch, ntx))
1969 return 0;
1970 return ntx;
1971 }
1972
1973 static int taprio_dump_class(struct Qdisc *sch, unsigned long cl,
1974 struct sk_buff *skb, struct tcmsg *tcm)
1975 {
1976 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1977
1978 tcm->tcm_parent = TC_H_ROOT;
1979 tcm->tcm_handle |= TC_H_MIN(cl);
1980 tcm->tcm_info = dev_queue->qdisc_sleeping->handle;
1981
1982 return 0;
1983 }
1984
1985 static int taprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
1986 struct gnet_dump *d)
1987 __releases(d->lock)
1988 __acquires(d->lock)
1989 {
1990 struct netdev_queue *dev_queue = taprio_queue_get(sch, cl);
1991
1992 sch = dev_queue->qdisc_sleeping;
1993 if (gnet_stats_copy_basic(d, NULL, &sch->bstats, true) < 0 ||
1994 qdisc_qstats_copy(d, sch) < 0)
1995 return -1;
1996 return 0;
1997 }
1998
1999 static void taprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
2000 {
2001 struct net_device *dev = qdisc_dev(sch);
2002 unsigned long ntx;
2003
2004 if (arg->stop)
2005 return;
2006
2007 arg->count = arg->skip;
2008 for (ntx = arg->skip; ntx < dev->num_tx_queues; ntx++) {
2009 if (arg->fn(sch, ntx + 1, arg) < 0) {
2010 arg->stop = 1;
2011 break;
2012 }
2013 arg->count++;
2014 }
2015 }
2016
2017 static struct netdev_queue *taprio_select_queue(struct Qdisc *sch,
2018 struct tcmsg *tcm)
2019 {
2020 return taprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
2021 }
2022
2023 static const struct Qdisc_class_ops taprio_class_ops = {
2024 .graft = taprio_graft,
2025 .leaf = taprio_leaf,
2026 .find = taprio_find,
2027 .walk = taprio_walk,
2028 .dump = taprio_dump_class,
2029 .dump_stats = taprio_dump_class_stats,
2030 .select_queue = taprio_select_queue,
2031 };
2032
2033 static struct Qdisc_ops taprio_qdisc_ops __read_mostly = {
2034 .cl_ops = &taprio_class_ops,
2035 .id = "taprio",
2036 .priv_size = sizeof(struct taprio_sched),
2037 .init = taprio_init,
2038 .change = taprio_change,
2039 .destroy = taprio_destroy,
2040 .reset = taprio_reset,
2041 .attach = taprio_attach,
2042 .peek = taprio_peek,
2043 .dequeue = taprio_dequeue,
2044 .enqueue = taprio_enqueue,
2045 .dump = taprio_dump,
2046 .owner = THIS_MODULE,
2047 };
2048
2049 static struct notifier_block taprio_device_notifier = {
2050 .notifier_call = taprio_dev_notifier,
2051 };
2052
2053 static int __init taprio_module_init(void)
2054 {
2055 int err = register_netdevice_notifier(&taprio_device_notifier);
2056
2057 if (err)
2058 return err;
2059
2060 return register_qdisc(&taprio_qdisc_ops);
2061 }
2062
2063 static void __exit taprio_module_exit(void)
2064 {
2065 unregister_qdisc(&taprio_qdisc_ops);
2066 unregister_netdevice_notifier(&taprio_device_notifier);
2067 }
2068
2069 module_init(taprio_module_init);
2070 module_exit(taprio_module_exit);
2071 MODULE_LICENSE("GPL");