1585d763aSVinicius Costa Gomes /* 2585d763aSVinicius Costa Gomes * net/sched/sch_cbs.c Credit Based Shaper 3585d763aSVinicius Costa Gomes * 4585d763aSVinicius Costa Gomes * This program is free software; you can redistribute it and/or 5585d763aSVinicius Costa Gomes * modify it under the terms of the GNU General Public License 6585d763aSVinicius Costa Gomes * as published by the Free Software Foundation; either version 7585d763aSVinicius Costa Gomes * 2 of the License, or (at your option) any later version. 8585d763aSVinicius Costa Gomes * 9585d763aSVinicius Costa Gomes * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com> 10585d763aSVinicius Costa Gomes * 11585d763aSVinicius Costa Gomes */ 12585d763aSVinicius Costa Gomes 13585d763aSVinicius Costa Gomes /* Credit Based Shaper (CBS) 14585d763aSVinicius Costa Gomes * ========================= 15585d763aSVinicius Costa Gomes * 16585d763aSVinicius Costa Gomes * This is a simple rate-limiting shaper aimed at TSN applications on 17585d763aSVinicius Costa Gomes * systems with known traffic workloads. 18585d763aSVinicius Costa Gomes * 19585d763aSVinicius Costa Gomes * Its algorithm is defined by the IEEE 802.1Q-2014 Specification, 20585d763aSVinicius Costa Gomes * Section 8.6.8.2, and explained in more detail in the Annex L of the 21585d763aSVinicius Costa Gomes * same specification. 22585d763aSVinicius Costa Gomes * 23585d763aSVinicius Costa Gomes * There are four tunables to be considered: 24585d763aSVinicius Costa Gomes * 25585d763aSVinicius Costa Gomes * 'idleslope': Idleslope is the rate of credits that is 26585d763aSVinicius Costa Gomes * accumulated (in kilobits per second) when there is at least 27585d763aSVinicius Costa Gomes * one packet waiting for transmission. Packets are transmitted 28585d763aSVinicius Costa Gomes * when the current value of credits is equal or greater than 29585d763aSVinicius Costa Gomes * zero. When there is no packet to be transmitted the amount of 30585d763aSVinicius Costa Gomes * credits is set to zero. This is the main tunable of the CBS 31585d763aSVinicius Costa Gomes * algorithm. 32585d763aSVinicius Costa Gomes * 33585d763aSVinicius Costa Gomes * 'sendslope': 34585d763aSVinicius Costa Gomes * Sendslope is the rate of credits that is depleted (it should be a 35585d763aSVinicius Costa Gomes * negative number of kilobits per second) when a transmission is 36585d763aSVinicius Costa Gomes * ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section 37585d763aSVinicius Costa Gomes * 8.6.8.2 item g): 38585d763aSVinicius Costa Gomes * 39585d763aSVinicius Costa Gomes * sendslope = idleslope - port_transmit_rate 40585d763aSVinicius Costa Gomes * 41585d763aSVinicius Costa Gomes * 'hicredit': Hicredit defines the maximum amount of credits (in 42585d763aSVinicius Costa Gomes * bytes) that can be accumulated. Hicredit depends on the 43585d763aSVinicius Costa Gomes * characteristics of interfering traffic, 44585d763aSVinicius Costa Gomes * 'max_interference_size' is the maximum size of any burst of 45585d763aSVinicius Costa Gomes * traffic that can delay the transmission of a frame that is 46585d763aSVinicius Costa Gomes * available for transmission for this traffic class, (IEEE 47585d763aSVinicius Costa Gomes * 802.1Q-2014 Annex L, Equation L-3): 48585d763aSVinicius Costa Gomes * 49585d763aSVinicius Costa Gomes * hicredit = max_interference_size * (idleslope / port_transmit_rate) 50585d763aSVinicius Costa Gomes * 51585d763aSVinicius Costa Gomes * 'locredit': Locredit is the minimum amount of credits that can 52585d763aSVinicius Costa Gomes * be reached. It is a function of the traffic flowing through 53585d763aSVinicius Costa Gomes * this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2): 54585d763aSVinicius Costa Gomes * 55585d763aSVinicius Costa Gomes * locredit = max_frame_size * (sendslope / port_transmit_rate) 56585d763aSVinicius Costa Gomes */ 57585d763aSVinicius Costa Gomes 58585d763aSVinicius Costa Gomes #include <linux/module.h> 59585d763aSVinicius Costa Gomes #include <linux/types.h> 60585d763aSVinicius Costa Gomes #include <linux/kernel.h> 61585d763aSVinicius Costa Gomes #include <linux/string.h> 62585d763aSVinicius Costa Gomes #include <linux/errno.h> 63585d763aSVinicius Costa Gomes #include <linux/skbuff.h> 64e0a7683dSLeandro Dorileo #include <net/netevent.h> 65585d763aSVinicius Costa Gomes #include <net/netlink.h> 66585d763aSVinicius Costa Gomes #include <net/sch_generic.h> 67585d763aSVinicius Costa Gomes #include <net/pkt_sched.h> 68585d763aSVinicius Costa Gomes 69e0a7683dSLeandro Dorileo static LIST_HEAD(cbs_list); 70e0a7683dSLeandro Dorileo static DEFINE_SPINLOCK(cbs_list_lock); 71e0a7683dSLeandro Dorileo 72585d763aSVinicius Costa Gomes #define BYTES_PER_KBIT (1000LL / 8) 73585d763aSVinicius Costa Gomes 74585d763aSVinicius Costa Gomes struct cbs_sched_data { 753d0bd028SVinicius Costa Gomes bool offload; 763d0bd028SVinicius Costa Gomes int queue; 77e0a7683dSLeandro Dorileo atomic64_t port_rate; /* in bytes/s */ 78585d763aSVinicius Costa Gomes s64 last; /* timestamp in ns */ 79585d763aSVinicius Costa Gomes s64 credits; /* in bytes */ 80585d763aSVinicius Costa Gomes s32 locredit; /* in bytes */ 81585d763aSVinicius Costa Gomes s32 hicredit; /* in bytes */ 82585d763aSVinicius Costa Gomes s64 sendslope; /* in bytes/s */ 83585d763aSVinicius Costa Gomes s64 idleslope; /* in bytes/s */ 84585d763aSVinicius Costa Gomes struct qdisc_watchdog watchdog; 85990e35ecSVinicius Costa Gomes int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch, 86990e35ecSVinicius Costa Gomes struct sk_buff **to_free); 87585d763aSVinicius Costa Gomes struct sk_buff *(*dequeue)(struct Qdisc *sch); 88990e35ecSVinicius Costa Gomes struct Qdisc *qdisc; 89e0a7683dSLeandro Dorileo struct list_head cbs_list; 90585d763aSVinicius Costa Gomes }; 91585d763aSVinicius Costa Gomes 92990e35ecSVinicius Costa Gomes static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch, 93990e35ecSVinicius Costa Gomes struct Qdisc *child, 94990e35ecSVinicius Costa Gomes struct sk_buff **to_free) 953d0bd028SVinicius Costa Gomes { 96f6bab199SToke Høiland-Jørgensen unsigned int len = qdisc_pkt_len(skb); 97990e35ecSVinicius Costa Gomes int err; 98990e35ecSVinicius Costa Gomes 99990e35ecSVinicius Costa Gomes err = child->ops->enqueue(skb, child, to_free); 100990e35ecSVinicius Costa Gomes if (err != NET_XMIT_SUCCESS) 101990e35ecSVinicius Costa Gomes return err; 102990e35ecSVinicius Costa Gomes 103f6bab199SToke Høiland-Jørgensen sch->qstats.backlog += len; 104990e35ecSVinicius Costa Gomes sch->q.qlen++; 105990e35ecSVinicius Costa Gomes 106990e35ecSVinicius Costa Gomes return NET_XMIT_SUCCESS; 1073d0bd028SVinicius Costa Gomes } 1083d0bd028SVinicius Costa Gomes 109990e35ecSVinicius Costa Gomes static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch, 110990e35ecSVinicius Costa Gomes struct sk_buff **to_free) 111585d763aSVinicius Costa Gomes { 112585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 113990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc; 114990e35ecSVinicius Costa Gomes 115990e35ecSVinicius Costa Gomes return cbs_child_enqueue(skb, sch, qdisc, to_free); 116990e35ecSVinicius Costa Gomes } 117990e35ecSVinicius Costa Gomes 118990e35ecSVinicius Costa Gomes static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch, 119990e35ecSVinicius Costa Gomes struct sk_buff **to_free) 120990e35ecSVinicius Costa Gomes { 121990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 122990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc; 123585d763aSVinicius Costa Gomes 124585d763aSVinicius Costa Gomes if (sch->q.qlen == 0 && q->credits > 0) { 125585d763aSVinicius Costa Gomes /* We need to stop accumulating credits when there's 126585d763aSVinicius Costa Gomes * no enqueued packets and q->credits is positive. 127585d763aSVinicius Costa Gomes */ 128585d763aSVinicius Costa Gomes q->credits = 0; 129585d763aSVinicius Costa Gomes q->last = ktime_get_ns(); 130585d763aSVinicius Costa Gomes } 131585d763aSVinicius Costa Gomes 132990e35ecSVinicius Costa Gomes return cbs_child_enqueue(skb, sch, qdisc, to_free); 133585d763aSVinicius Costa Gomes } 134585d763aSVinicius Costa Gomes 135585d763aSVinicius Costa Gomes static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch, 136585d763aSVinicius Costa Gomes struct sk_buff **to_free) 137585d763aSVinicius Costa Gomes { 138585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 139585d763aSVinicius Costa Gomes 140990e35ecSVinicius Costa Gomes return q->enqueue(skb, sch, to_free); 141585d763aSVinicius Costa Gomes } 142585d763aSVinicius Costa Gomes 143585d763aSVinicius Costa Gomes /* timediff is in ns, slope is in bytes/s */ 144585d763aSVinicius Costa Gomes static s64 timediff_to_credits(s64 timediff, s64 slope) 145585d763aSVinicius Costa Gomes { 146585d763aSVinicius Costa Gomes return div64_s64(timediff * slope, NSEC_PER_SEC); 147585d763aSVinicius Costa Gomes } 148585d763aSVinicius Costa Gomes 149585d763aSVinicius Costa Gomes static s64 delay_from_credits(s64 credits, s64 slope) 150585d763aSVinicius Costa Gomes { 151585d763aSVinicius Costa Gomes if (unlikely(slope == 0)) 152585d763aSVinicius Costa Gomes return S64_MAX; 153585d763aSVinicius Costa Gomes 154585d763aSVinicius Costa Gomes return div64_s64(-credits * NSEC_PER_SEC, slope); 155585d763aSVinicius Costa Gomes } 156585d763aSVinicius Costa Gomes 157585d763aSVinicius Costa Gomes static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate) 158585d763aSVinicius Costa Gomes { 159585d763aSVinicius Costa Gomes if (unlikely(port_rate == 0)) 160585d763aSVinicius Costa Gomes return S64_MAX; 161585d763aSVinicius Costa Gomes 162585d763aSVinicius Costa Gomes return div64_s64(len * slope, port_rate); 163585d763aSVinicius Costa Gomes } 164585d763aSVinicius Costa Gomes 165990e35ecSVinicius Costa Gomes static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child) 166990e35ecSVinicius Costa Gomes { 167990e35ecSVinicius Costa Gomes struct sk_buff *skb; 168990e35ecSVinicius Costa Gomes 169990e35ecSVinicius Costa Gomes skb = child->ops->dequeue(child); 170990e35ecSVinicius Costa Gomes if (!skb) 171990e35ecSVinicius Costa Gomes return NULL; 172990e35ecSVinicius Costa Gomes 173990e35ecSVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb); 174990e35ecSVinicius Costa Gomes qdisc_bstats_update(sch, skb); 175990e35ecSVinicius Costa Gomes sch->q.qlen--; 176990e35ecSVinicius Costa Gomes 177990e35ecSVinicius Costa Gomes return skb; 178990e35ecSVinicius Costa Gomes } 179990e35ecSVinicius Costa Gomes 180585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch) 181585d763aSVinicius Costa Gomes { 182585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 183990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc; 184585d763aSVinicius Costa Gomes s64 now = ktime_get_ns(); 185585d763aSVinicius Costa Gomes struct sk_buff *skb; 186585d763aSVinicius Costa Gomes s64 credits; 187585d763aSVinicius Costa Gomes int len; 188585d763aSVinicius Costa Gomes 189e0a7683dSLeandro Dorileo if (atomic64_read(&q->port_rate) == -1) { 190e0a7683dSLeandro Dorileo WARN_ONCE(1, "cbs: dequeue() called with unknown port rate."); 191e0a7683dSLeandro Dorileo return NULL; 192e0a7683dSLeandro Dorileo } 193e0a7683dSLeandro Dorileo 194585d763aSVinicius Costa Gomes if (q->credits < 0) { 195585d763aSVinicius Costa Gomes credits = timediff_to_credits(now - q->last, q->idleslope); 196585d763aSVinicius Costa Gomes 197585d763aSVinicius Costa Gomes credits = q->credits + credits; 198585d763aSVinicius Costa Gomes q->credits = min_t(s64, credits, q->hicredit); 199585d763aSVinicius Costa Gomes 200585d763aSVinicius Costa Gomes if (q->credits < 0) { 201585d763aSVinicius Costa Gomes s64 delay; 202585d763aSVinicius Costa Gomes 203585d763aSVinicius Costa Gomes delay = delay_from_credits(q->credits, q->idleslope); 204585d763aSVinicius Costa Gomes qdisc_watchdog_schedule_ns(&q->watchdog, now + delay); 205585d763aSVinicius Costa Gomes 206585d763aSVinicius Costa Gomes q->last = now; 207585d763aSVinicius Costa Gomes 208585d763aSVinicius Costa Gomes return NULL; 209585d763aSVinicius Costa Gomes } 210585d763aSVinicius Costa Gomes } 211990e35ecSVinicius Costa Gomes skb = cbs_child_dequeue(sch, qdisc); 212585d763aSVinicius Costa Gomes if (!skb) 213585d763aSVinicius Costa Gomes return NULL; 214585d763aSVinicius Costa Gomes 215585d763aSVinicius Costa Gomes len = qdisc_pkt_len(skb); 216585d763aSVinicius Costa Gomes 217585d763aSVinicius Costa Gomes /* As sendslope is a negative number, this will decrease the 218585d763aSVinicius Costa Gomes * amount of q->credits. 219585d763aSVinicius Costa Gomes */ 220e0a7683dSLeandro Dorileo credits = credits_from_len(len, q->sendslope, 221e0a7683dSLeandro Dorileo atomic64_read(&q->port_rate)); 222585d763aSVinicius Costa Gomes credits += q->credits; 223585d763aSVinicius Costa Gomes 224585d763aSVinicius Costa Gomes q->credits = max_t(s64, credits, q->locredit); 225585d763aSVinicius Costa Gomes q->last = now; 226585d763aSVinicius Costa Gomes 227585d763aSVinicius Costa Gomes return skb; 228585d763aSVinicius Costa Gomes } 229585d763aSVinicius Costa Gomes 2303d0bd028SVinicius Costa Gomes static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch) 2313d0bd028SVinicius Costa Gomes { 232990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 233990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc; 234990e35ecSVinicius Costa Gomes 235990e35ecSVinicius Costa Gomes return cbs_child_dequeue(sch, qdisc); 2363d0bd028SVinicius Costa Gomes } 2373d0bd028SVinicius Costa Gomes 238585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue(struct Qdisc *sch) 239585d763aSVinicius Costa Gomes { 240585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 241585d763aSVinicius Costa Gomes 242585d763aSVinicius Costa Gomes return q->dequeue(sch); 243585d763aSVinicius Costa Gomes } 244585d763aSVinicius Costa Gomes 245585d763aSVinicius Costa Gomes static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = { 246585d763aSVinicius Costa Gomes [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) }, 247585d763aSVinicius Costa Gomes }; 248585d763aSVinicius Costa Gomes 2493d0bd028SVinicius Costa Gomes static void cbs_disable_offload(struct net_device *dev, 2503d0bd028SVinicius Costa Gomes struct cbs_sched_data *q) 2513d0bd028SVinicius Costa Gomes { 2523d0bd028SVinicius Costa Gomes struct tc_cbs_qopt_offload cbs = { }; 2533d0bd028SVinicius Costa Gomes const struct net_device_ops *ops; 2543d0bd028SVinicius Costa Gomes int err; 2553d0bd028SVinicius Costa Gomes 2563d0bd028SVinicius Costa Gomes if (!q->offload) 2573d0bd028SVinicius Costa Gomes return; 2583d0bd028SVinicius Costa Gomes 2593d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_soft; 2603d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_soft; 2613d0bd028SVinicius Costa Gomes 2623d0bd028SVinicius Costa Gomes ops = dev->netdev_ops; 2633d0bd028SVinicius Costa Gomes if (!ops->ndo_setup_tc) 2643d0bd028SVinicius Costa Gomes return; 2653d0bd028SVinicius Costa Gomes 2663d0bd028SVinicius Costa Gomes cbs.queue = q->queue; 2673d0bd028SVinicius Costa Gomes cbs.enable = 0; 2683d0bd028SVinicius Costa Gomes 2698521db4cSNogah Frankel err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs); 2703d0bd028SVinicius Costa Gomes if (err < 0) 2713d0bd028SVinicius Costa Gomes pr_warn("Couldn't disable CBS offload for queue %d\n", 2723d0bd028SVinicius Costa Gomes cbs.queue); 2733d0bd028SVinicius Costa Gomes } 2743d0bd028SVinicius Costa Gomes 2753d0bd028SVinicius Costa Gomes static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q, 276710fb396SAlexander Aring const struct tc_cbs_qopt *opt, 277710fb396SAlexander Aring struct netlink_ext_ack *extack) 2783d0bd028SVinicius Costa Gomes { 2793d0bd028SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops; 2803d0bd028SVinicius Costa Gomes struct tc_cbs_qopt_offload cbs = { }; 2813d0bd028SVinicius Costa Gomes int err; 2823d0bd028SVinicius Costa Gomes 283710fb396SAlexander Aring if (!ops->ndo_setup_tc) { 284710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload"); 2853d0bd028SVinicius Costa Gomes return -EOPNOTSUPP; 286710fb396SAlexander Aring } 2873d0bd028SVinicius Costa Gomes 2883d0bd028SVinicius Costa Gomes cbs.queue = q->queue; 2893d0bd028SVinicius Costa Gomes 2903d0bd028SVinicius Costa Gomes cbs.enable = 1; 2913d0bd028SVinicius Costa Gomes cbs.hicredit = opt->hicredit; 2923d0bd028SVinicius Costa Gomes cbs.locredit = opt->locredit; 2933d0bd028SVinicius Costa Gomes cbs.idleslope = opt->idleslope; 2943d0bd028SVinicius Costa Gomes cbs.sendslope = opt->sendslope; 2953d0bd028SVinicius Costa Gomes 2968521db4cSNogah Frankel err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs); 297710fb396SAlexander Aring if (err < 0) { 298710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload"); 2993d0bd028SVinicius Costa Gomes return err; 300710fb396SAlexander Aring } 3013d0bd028SVinicius Costa Gomes 3023d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_offload; 3033d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_offload; 3043d0bd028SVinicius Costa Gomes 3053d0bd028SVinicius Costa Gomes return 0; 3063d0bd028SVinicius Costa Gomes } 3073d0bd028SVinicius Costa Gomes 308e0a7683dSLeandro Dorileo static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q) 309e0a7683dSLeandro Dorileo { 310e0a7683dSLeandro Dorileo struct ethtool_link_ksettings ecmd; 311e0a7683dSLeandro Dorileo int port_rate = -1; 312e0a7683dSLeandro Dorileo 313e0a7683dSLeandro Dorileo if (!__ethtool_get_link_ksettings(dev, &ecmd) && 314e0a7683dSLeandro Dorileo ecmd.base.speed != SPEED_UNKNOWN) 315e0a7683dSLeandro Dorileo port_rate = ecmd.base.speed * 1000 * BYTES_PER_KBIT; 316e0a7683dSLeandro Dorileo 317e0a7683dSLeandro Dorileo atomic64_set(&q->port_rate, port_rate); 318e0a7683dSLeandro Dorileo netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n", 319e0a7683dSLeandro Dorileo dev->name, (long long)atomic64_read(&q->port_rate), 320e0a7683dSLeandro Dorileo ecmd.base.speed); 321e0a7683dSLeandro Dorileo } 322e0a7683dSLeandro Dorileo 323e0a7683dSLeandro Dorileo static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event, 324e0a7683dSLeandro Dorileo void *ptr) 325e0a7683dSLeandro Dorileo { 326e0a7683dSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr); 327e0a7683dSLeandro Dorileo struct cbs_sched_data *q; 328e0a7683dSLeandro Dorileo struct net_device *qdev; 329e0a7683dSLeandro Dorileo bool found = false; 330e0a7683dSLeandro Dorileo 331e0a7683dSLeandro Dorileo ASSERT_RTNL(); 332e0a7683dSLeandro Dorileo 333e0a7683dSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE) 334e0a7683dSLeandro Dorileo return NOTIFY_DONE; 335e0a7683dSLeandro Dorileo 336e0a7683dSLeandro Dorileo spin_lock(&cbs_list_lock); 337e0a7683dSLeandro Dorileo list_for_each_entry(q, &cbs_list, cbs_list) { 338e0a7683dSLeandro Dorileo qdev = qdisc_dev(q->qdisc); 339e0a7683dSLeandro Dorileo if (qdev == dev) { 340e0a7683dSLeandro Dorileo found = true; 341e0a7683dSLeandro Dorileo break; 342e0a7683dSLeandro Dorileo } 343e0a7683dSLeandro Dorileo } 344e0a7683dSLeandro Dorileo spin_unlock(&cbs_list_lock); 345e0a7683dSLeandro Dorileo 346e0a7683dSLeandro Dorileo if (found) 347e0a7683dSLeandro Dorileo cbs_set_port_rate(dev, q); 348e0a7683dSLeandro Dorileo 349e0a7683dSLeandro Dorileo return NOTIFY_DONE; 350e0a7683dSLeandro Dorileo } 351e0a7683dSLeandro Dorileo 3522030721cSAlexander Aring static int cbs_change(struct Qdisc *sch, struct nlattr *opt, 3532030721cSAlexander Aring struct netlink_ext_ack *extack) 354585d763aSVinicius Costa Gomes { 355585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 356585d763aSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 357585d763aSVinicius Costa Gomes struct nlattr *tb[TCA_CBS_MAX + 1]; 358585d763aSVinicius Costa Gomes struct tc_cbs_qopt *qopt; 359585d763aSVinicius Costa Gomes int err; 360585d763aSVinicius Costa Gomes 361*8cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy, 362*8cb08174SJohannes Berg extack); 363585d763aSVinicius Costa Gomes if (err < 0) 364585d763aSVinicius Costa Gomes return err; 365585d763aSVinicius Costa Gomes 366710fb396SAlexander Aring if (!tb[TCA_CBS_PARMS]) { 367710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory"); 368585d763aSVinicius Costa Gomes return -EINVAL; 369710fb396SAlexander Aring } 370585d763aSVinicius Costa Gomes 371585d763aSVinicius Costa Gomes qopt = nla_data(tb[TCA_CBS_PARMS]); 372585d763aSVinicius Costa Gomes 3733d0bd028SVinicius Costa Gomes if (!qopt->offload) { 374e0a7683dSLeandro Dorileo cbs_set_port_rate(dev, q); 3753d0bd028SVinicius Costa Gomes cbs_disable_offload(dev, q); 3763d0bd028SVinicius Costa Gomes } else { 377710fb396SAlexander Aring err = cbs_enable_offload(dev, q, qopt, extack); 3783d0bd028SVinicius Costa Gomes if (err < 0) 3793d0bd028SVinicius Costa Gomes return err; 3803d0bd028SVinicius Costa Gomes } 381585d763aSVinicius Costa Gomes 3823d0bd028SVinicius Costa Gomes /* Everything went OK, save the parameters used. */ 383585d763aSVinicius Costa Gomes q->hicredit = qopt->hicredit; 384585d763aSVinicius Costa Gomes q->locredit = qopt->locredit; 385585d763aSVinicius Costa Gomes q->idleslope = qopt->idleslope * BYTES_PER_KBIT; 386585d763aSVinicius Costa Gomes q->sendslope = qopt->sendslope * BYTES_PER_KBIT; 3873d0bd028SVinicius Costa Gomes q->offload = qopt->offload; 388585d763aSVinicius Costa Gomes 389585d763aSVinicius Costa Gomes return 0; 390585d763aSVinicius Costa Gomes } 391585d763aSVinicius Costa Gomes 392e63d7dfdSAlexander Aring static int cbs_init(struct Qdisc *sch, struct nlattr *opt, 393e63d7dfdSAlexander Aring struct netlink_ext_ack *extack) 394585d763aSVinicius Costa Gomes { 395585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 3963d0bd028SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 397e0a7683dSLeandro Dorileo int err; 398585d763aSVinicius Costa Gomes 399710fb396SAlexander Aring if (!opt) { 400710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory"); 401585d763aSVinicius Costa Gomes return -EINVAL; 402710fb396SAlexander Aring } 403585d763aSVinicius Costa Gomes 404990e35ecSVinicius Costa Gomes q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 405990e35ecSVinicius Costa Gomes sch->handle, extack); 406990e35ecSVinicius Costa Gomes if (!q->qdisc) 407990e35ecSVinicius Costa Gomes return -ENOMEM; 408990e35ecSVinicius Costa Gomes 409990e35ecSVinicius Costa Gomes qdisc_hash_add(q->qdisc, false); 410990e35ecSVinicius Costa Gomes 4113d0bd028SVinicius Costa Gomes q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0); 4123d0bd028SVinicius Costa Gomes 4133d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_soft; 4143d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_soft; 4153d0bd028SVinicius Costa Gomes 416585d763aSVinicius Costa Gomes qdisc_watchdog_init(&q->watchdog, sch); 417585d763aSVinicius Costa Gomes 418e0a7683dSLeandro Dorileo err = cbs_change(sch, opt, extack); 419e0a7683dSLeandro Dorileo if (err) 420e0a7683dSLeandro Dorileo return err; 421e0a7683dSLeandro Dorileo 422e0a7683dSLeandro Dorileo if (!q->offload) { 423e0a7683dSLeandro Dorileo spin_lock(&cbs_list_lock); 424e0a7683dSLeandro Dorileo list_add(&q->cbs_list, &cbs_list); 425e0a7683dSLeandro Dorileo spin_unlock(&cbs_list_lock); 426e0a7683dSLeandro Dorileo } 427e0a7683dSLeandro Dorileo 428e0a7683dSLeandro Dorileo return 0; 429585d763aSVinicius Costa Gomes } 430585d763aSVinicius Costa Gomes 431585d763aSVinicius Costa Gomes static void cbs_destroy(struct Qdisc *sch) 432585d763aSVinicius Costa Gomes { 433585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 4343d0bd028SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch); 435585d763aSVinicius Costa Gomes 436e0a7683dSLeandro Dorileo spin_lock(&cbs_list_lock); 437e0a7683dSLeandro Dorileo list_del(&q->cbs_list); 438e0a7683dSLeandro Dorileo spin_unlock(&cbs_list_lock); 4393d0bd028SVinicius Costa Gomes 440e0a7683dSLeandro Dorileo qdisc_watchdog_cancel(&q->watchdog); 4413d0bd028SVinicius Costa Gomes cbs_disable_offload(dev, q); 442990e35ecSVinicius Costa Gomes 443990e35ecSVinicius Costa Gomes if (q->qdisc) 44486bd446bSVlad Buslov qdisc_put(q->qdisc); 445585d763aSVinicius Costa Gomes } 446585d763aSVinicius Costa Gomes 447585d763aSVinicius Costa Gomes static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb) 448585d763aSVinicius Costa Gomes { 449585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 450585d763aSVinicius Costa Gomes struct tc_cbs_qopt opt = { }; 451585d763aSVinicius Costa Gomes struct nlattr *nest; 452585d763aSVinicius Costa Gomes 453ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 454585d763aSVinicius Costa Gomes if (!nest) 455585d763aSVinicius Costa Gomes goto nla_put_failure; 456585d763aSVinicius Costa Gomes 457585d763aSVinicius Costa Gomes opt.hicredit = q->hicredit; 458585d763aSVinicius Costa Gomes opt.locredit = q->locredit; 459585d763aSVinicius Costa Gomes opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT); 460585d763aSVinicius Costa Gomes opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT); 4613d0bd028SVinicius Costa Gomes opt.offload = q->offload; 462585d763aSVinicius Costa Gomes 463585d763aSVinicius Costa Gomes if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt)) 464585d763aSVinicius Costa Gomes goto nla_put_failure; 465585d763aSVinicius Costa Gomes 466585d763aSVinicius Costa Gomes return nla_nest_end(skb, nest); 467585d763aSVinicius Costa Gomes 468585d763aSVinicius Costa Gomes nla_put_failure: 469585d763aSVinicius Costa Gomes nla_nest_cancel(skb, nest); 470585d763aSVinicius Costa Gomes return -1; 471585d763aSVinicius Costa Gomes } 472585d763aSVinicius Costa Gomes 473990e35ecSVinicius Costa Gomes static int cbs_dump_class(struct Qdisc *sch, unsigned long cl, 474990e35ecSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm) 475990e35ecSVinicius Costa Gomes { 476990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 477990e35ecSVinicius Costa Gomes 478990e35ecSVinicius Costa Gomes if (cl != 1 || !q->qdisc) /* only one class */ 479990e35ecSVinicius Costa Gomes return -ENOENT; 480990e35ecSVinicius Costa Gomes 481990e35ecSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(1); 482990e35ecSVinicius Costa Gomes tcm->tcm_info = q->qdisc->handle; 483990e35ecSVinicius Costa Gomes 484990e35ecSVinicius Costa Gomes return 0; 485990e35ecSVinicius Costa Gomes } 486990e35ecSVinicius Costa Gomes 487990e35ecSVinicius Costa Gomes static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 488990e35ecSVinicius Costa Gomes struct Qdisc **old, struct netlink_ext_ack *extack) 489990e35ecSVinicius Costa Gomes { 490990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 491990e35ecSVinicius Costa Gomes 492990e35ecSVinicius Costa Gomes if (!new) { 493990e35ecSVinicius Costa Gomes new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, 494990e35ecSVinicius Costa Gomes sch->handle, NULL); 495990e35ecSVinicius Costa Gomes if (!new) 496990e35ecSVinicius Costa Gomes new = &noop_qdisc; 497990e35ecSVinicius Costa Gomes } 498990e35ecSVinicius Costa Gomes 499990e35ecSVinicius Costa Gomes *old = qdisc_replace(sch, new, &q->qdisc); 500990e35ecSVinicius Costa Gomes return 0; 501990e35ecSVinicius Costa Gomes } 502990e35ecSVinicius Costa Gomes 503990e35ecSVinicius Costa Gomes static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg) 504990e35ecSVinicius Costa Gomes { 505990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch); 506990e35ecSVinicius Costa Gomes 507990e35ecSVinicius Costa Gomes return q->qdisc; 508990e35ecSVinicius Costa Gomes } 509990e35ecSVinicius Costa Gomes 510990e35ecSVinicius Costa Gomes static unsigned long cbs_find(struct Qdisc *sch, u32 classid) 511990e35ecSVinicius Costa Gomes { 512990e35ecSVinicius Costa Gomes return 1; 513990e35ecSVinicius Costa Gomes } 514990e35ecSVinicius Costa Gomes 515990e35ecSVinicius Costa Gomes static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker) 516990e35ecSVinicius Costa Gomes { 517990e35ecSVinicius Costa Gomes if (!walker->stop) { 518990e35ecSVinicius Costa Gomes if (walker->count >= walker->skip) { 519990e35ecSVinicius Costa Gomes if (walker->fn(sch, 1, walker) < 0) { 520990e35ecSVinicius Costa Gomes walker->stop = 1; 521990e35ecSVinicius Costa Gomes return; 522990e35ecSVinicius Costa Gomes } 523990e35ecSVinicius Costa Gomes } 524990e35ecSVinicius Costa Gomes walker->count++; 525990e35ecSVinicius Costa Gomes } 526990e35ecSVinicius Costa Gomes } 527990e35ecSVinicius Costa Gomes 528990e35ecSVinicius Costa Gomes static const struct Qdisc_class_ops cbs_class_ops = { 529990e35ecSVinicius Costa Gomes .graft = cbs_graft, 530990e35ecSVinicius Costa Gomes .leaf = cbs_leaf, 531990e35ecSVinicius Costa Gomes .find = cbs_find, 532990e35ecSVinicius Costa Gomes .walk = cbs_walk, 533990e35ecSVinicius Costa Gomes .dump = cbs_dump_class, 534990e35ecSVinicius Costa Gomes }; 535990e35ecSVinicius Costa Gomes 536585d763aSVinicius Costa Gomes static struct Qdisc_ops cbs_qdisc_ops __read_mostly = { 537585d763aSVinicius Costa Gomes .id = "cbs", 538990e35ecSVinicius Costa Gomes .cl_ops = &cbs_class_ops, 539585d763aSVinicius Costa Gomes .priv_size = sizeof(struct cbs_sched_data), 540585d763aSVinicius Costa Gomes .enqueue = cbs_enqueue, 541585d763aSVinicius Costa Gomes .dequeue = cbs_dequeue, 542585d763aSVinicius Costa Gomes .peek = qdisc_peek_dequeued, 543585d763aSVinicius Costa Gomes .init = cbs_init, 544585d763aSVinicius Costa Gomes .reset = qdisc_reset_queue, 545585d763aSVinicius Costa Gomes .destroy = cbs_destroy, 546585d763aSVinicius Costa Gomes .change = cbs_change, 547585d763aSVinicius Costa Gomes .dump = cbs_dump, 548585d763aSVinicius Costa Gomes .owner = THIS_MODULE, 549585d763aSVinicius Costa Gomes }; 550585d763aSVinicius Costa Gomes 551e0a7683dSLeandro Dorileo static struct notifier_block cbs_device_notifier = { 552e0a7683dSLeandro Dorileo .notifier_call = cbs_dev_notifier, 553e0a7683dSLeandro Dorileo }; 554e0a7683dSLeandro Dorileo 555585d763aSVinicius Costa Gomes static int __init cbs_module_init(void) 556585d763aSVinicius Costa Gomes { 557e0a7683dSLeandro Dorileo int err = register_netdevice_notifier(&cbs_device_notifier); 558e0a7683dSLeandro Dorileo 559e0a7683dSLeandro Dorileo if (err) 560e0a7683dSLeandro Dorileo return err; 561e0a7683dSLeandro Dorileo 562585d763aSVinicius Costa Gomes return register_qdisc(&cbs_qdisc_ops); 563585d763aSVinicius Costa Gomes } 564585d763aSVinicius Costa Gomes 565585d763aSVinicius Costa Gomes static void __exit cbs_module_exit(void) 566585d763aSVinicius Costa Gomes { 567585d763aSVinicius Costa Gomes unregister_qdisc(&cbs_qdisc_ops); 568e0a7683dSLeandro Dorileo unregister_netdevice_notifier(&cbs_device_notifier); 569585d763aSVinicius Costa Gomes } 570585d763aSVinicius Costa Gomes module_init(cbs_module_init) 571585d763aSVinicius Costa Gomes module_exit(cbs_module_exit) 572585d763aSVinicius Costa Gomes MODULE_LICENSE("GPL"); 573