2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* net/sched/sch_tbf.c Token Bucket Filter queue.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
* Dmitry Torokhov <dtor@mail.ru> - allow attaching inner qdiscs -
|
|
|
|
* original idea by Martin Devera
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/skbuff.h>
|
2007-07-03 13:49:07 +08:00
|
|
|
#include <net/netlink.h>
|
2013-02-12 08:12:05 +08:00
|
|
|
#include <net/sch_generic.h>
|
2020-01-24 21:23:06 +08:00
|
|
|
#include <net/pkt_cls.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/pkt_sched.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* Simple Token Bucket Filter.
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
SOURCE.
|
|
|
|
-------
|
|
|
|
|
|
|
|
None.
|
|
|
|
|
|
|
|
Description.
|
|
|
|
------------
|
|
|
|
|
|
|
|
A data flow obeys TBF with rate R and depth B, if for any
|
|
|
|
time interval t_i...t_f the number of transmitted bits
|
|
|
|
does not exceed B + R*(t_f-t_i).
|
|
|
|
|
|
|
|
Packetized version of this definition:
|
|
|
|
The sequence of packets of sizes s_i served at moments t_i
|
|
|
|
obeys TBF, if for any i<=k:
|
|
|
|
|
|
|
|
s_i+....+s_k <= B + R*(t_k - t_i)
|
|
|
|
|
|
|
|
Algorithm.
|
|
|
|
----------
|
|
|
|
|
|
|
|
Let N(t_i) be B/R initially and N(t) grow continuously with time as:
|
|
|
|
|
|
|
|
N(t+delta) = min{B/R, N(t) + delta}
|
|
|
|
|
|
|
|
If the first packet in queue has length S, it may be
|
|
|
|
transmitted only at the time t_* when S/R <= N(t_*),
|
|
|
|
and in this case N(t) jumps:
|
|
|
|
|
|
|
|
N(t_* + 0) = N(t_* - 0) - S/R.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Actually, QoS requires two TBF to be applied to a data stream.
|
|
|
|
One of them controls steady state burst size, another
|
|
|
|
one with rate P (peak rate) and depth M (equal to link MTU)
|
|
|
|
limits bursts at a smaller time scale.
|
|
|
|
|
|
|
|
It is easy to see that P>R, and B>M. If P is infinity, this double
|
|
|
|
TBF is equivalent to a single one.
|
|
|
|
|
|
|
|
When TBF works in reshaping mode, latency is estimated as:
|
|
|
|
|
|
|
|
lat = max ((L-B)/R, (L-M)/P)
|
|
|
|
|
|
|
|
|
|
|
|
NOTES.
|
|
|
|
------
|
|
|
|
|
|
|
|
If TBF throttles, it starts a watchdog timer, which will wake it up
|
|
|
|
when it is ready to transmit.
|
|
|
|
Note that the minimal timer resolution is 1/HZ.
|
|
|
|
If no new packets arrive during this period,
|
|
|
|
or if the device is not awaken by EOI for some previous packet,
|
|
|
|
TBF can stop its activity for 1/HZ.
|
|
|
|
|
|
|
|
|
|
|
|
This means, that with depth B, the maximal rate is
|
|
|
|
|
|
|
|
R_crit = B*HZ
|
|
|
|
|
|
|
|
F.e. for 10Mbit ethernet and HZ=100 the minimal allowed B is ~10Kbytes.
|
|
|
|
|
|
|
|
Note that the peak rate TBF is much more tough: with MTU 1500
|
|
|
|
P_crit = 150Kbytes/sec. So, if you need greater peak
|
|
|
|
rates, use alpha with HZ=1000 :-)
|
|
|
|
|
|
|
|
With classful TBF, limit is just kept for backwards compatibility.
|
|
|
|
It is passed to the default bfifo qdisc - if the inner qdisc is
|
|
|
|
changed the limit is not effective anymore.
|
|
|
|
*/
|
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
struct tbf_sched_data {
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Parameters */
|
|
|
|
u32 limit; /* Maximal length of backlog: bytes */
|
2014-03-02 16:30:26 +08:00
|
|
|
u32 max_size;
|
2013-02-12 08:12:05 +08:00
|
|
|
s64 buffer; /* Token bucket depth/rate: MUST BE >= MTU/B */
|
|
|
|
s64 mtu;
|
|
|
|
struct psched_ratecfg rate;
|
|
|
|
struct psched_ratecfg peak;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Variables */
|
2013-02-12 08:12:05 +08:00
|
|
|
s64 tokens; /* Current number of B tokens */
|
|
|
|
s64 ptokens; /* Current number of P tokens */
|
|
|
|
s64 t_c; /* Time check-point */
|
2005-04-17 06:20:36 +08:00
|
|
|
struct Qdisc *qdisc; /* Inner qdisc, default - bfifo queue */
|
2007-03-16 16:20:07 +08:00
|
|
|
struct qdisc_watchdog watchdog; /* Watchdog timer */
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2013-05-21 16:16:46 +08:00
|
|
|
|
2013-12-10 14:59:27 +08:00
|
|
|
/* Time to Length, convert time in ns to length in bytes
|
|
|
|
* to determinate how many bytes can be sent in given time.
|
|
|
|
*/
|
|
|
|
static u64 psched_ns_t2l(const struct psched_ratecfg *r,
|
|
|
|
u64 time_in_ns)
|
|
|
|
{
|
|
|
|
/* The formula is :
|
|
|
|
* len = (time_in_ns * r->rate_bytes_ps) / NSEC_PER_SEC
|
|
|
|
*/
|
|
|
|
u64 len = time_in_ns * r->rate_bytes_ps;
|
|
|
|
|
|
|
|
do_div(len, NSEC_PER_SEC);
|
|
|
|
|
2013-12-12 10:57:22 +08:00
|
|
|
if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) {
|
|
|
|
do_div(len, 53);
|
|
|
|
len = len * 48;
|
|
|
|
}
|
2013-12-10 14:59:27 +08:00
|
|
|
|
|
|
|
if (len > r->overhead)
|
|
|
|
len -= r->overhead;
|
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:23:06 +08:00
|
|
|
static void tbf_offload_change(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct net_device *dev = qdisc_dev(sch);
|
|
|
|
struct tc_tbf_qopt_offload qopt;
|
|
|
|
|
|
|
|
if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qopt.command = TC_TBF_REPLACE;
|
|
|
|
qopt.handle = sch->handle;
|
|
|
|
qopt.parent = sch->parent;
|
|
|
|
qopt.replace_params.rate = q->rate;
|
|
|
|
qopt.replace_params.max_size = q->max_size;
|
|
|
|
qopt.replace_params.qstats = &sch->qstats;
|
|
|
|
|
|
|
|
dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TBF, &qopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tbf_offload_destroy(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct net_device *dev = qdisc_dev(sch);
|
|
|
|
struct tc_tbf_qopt_offload qopt;
|
|
|
|
|
|
|
|
if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qopt.command = TC_TBF_DESTROY;
|
|
|
|
qopt.handle = sch->handle;
|
|
|
|
qopt.parent = sch->parent;
|
|
|
|
dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_TBF, &qopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tbf_offload_dump(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct tc_tbf_qopt_offload qopt;
|
|
|
|
|
|
|
|
qopt.command = TC_TBF_STATS;
|
|
|
|
qopt.handle = sch->handle;
|
|
|
|
qopt.parent = sch->parent;
|
|
|
|
qopt.stats.bstats = &sch->bstats;
|
|
|
|
qopt.stats.qstats = &sch->qstats;
|
|
|
|
|
|
|
|
return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_TBF, &qopt);
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:07:04 +08:00
|
|
|
static void tbf_offload_graft(struct Qdisc *sch, struct Qdisc *new,
|
|
|
|
struct Qdisc *old, struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
struct tc_tbf_qopt_offload graft_offload = {
|
|
|
|
.handle = sch->handle,
|
|
|
|
.parent = sch->parent,
|
|
|
|
.child_handle = new->handle,
|
|
|
|
.command = TC_TBF_GRAFT,
|
|
|
|
};
|
|
|
|
|
|
|
|
qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, old,
|
|
|
|
TC_SETUP_QDISC_TBF, &graft_offload, extack);
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:16:46 +08:00
|
|
|
/* GSO packet is too big, segment it so that tbf can transmit
|
|
|
|
* each segment in time
|
|
|
|
*/
|
2016-06-22 14:16:49 +08:00
|
|
|
static int tbf_segment(struct sk_buff *skb, struct Qdisc *sch,
|
|
|
|
struct sk_buff **to_free)
|
2013-05-21 16:16:46 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct sk_buff *segs, *nskb;
|
|
|
|
netdev_features_t features = netif_skb_features(skb);
|
2016-02-26 06:55:01 +08:00
|
|
|
unsigned int len = 0, prev_len = qdisc_pkt_len(skb);
|
2013-05-21 16:16:46 +08:00
|
|
|
int ret, nb;
|
|
|
|
|
|
|
|
segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
|
|
|
|
|
|
|
|
if (IS_ERR_OR_NULL(segs))
|
2016-06-22 14:16:49 +08:00
|
|
|
return qdisc_drop(skb, sch, to_free);
|
2013-05-21 16:16:46 +08:00
|
|
|
|
|
|
|
nb = 0;
|
2020-01-14 07:42:30 +08:00
|
|
|
skb_list_walk_safe(segs, segs, nskb) {
|
2018-07-30 11:42:53 +08:00
|
|
|
skb_mark_not_on_list(segs);
|
2013-11-24 04:59:20 +08:00
|
|
|
qdisc_skb_cb(segs)->pkt_len = segs->len;
|
2016-02-26 06:55:01 +08:00
|
|
|
len += segs->len;
|
2016-06-22 14:16:49 +08:00
|
|
|
ret = qdisc_enqueue(segs, q->qdisc, to_free);
|
2013-05-21 16:16:46 +08:00
|
|
|
if (ret != NET_XMIT_SUCCESS) {
|
|
|
|
if (net_xmit_drop_count(ret))
|
2014-09-29 02:53:29 +08:00
|
|
|
qdisc_qstats_drop(sch);
|
2013-05-21 16:16:46 +08:00
|
|
|
} else {
|
|
|
|
nb++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sch->q.qlen += nb;
|
|
|
|
if (nb > 1)
|
2016-02-26 06:55:01 +08:00
|
|
|
qdisc_tree_reduce_backlog(sch, 1 - nb, prev_len - len);
|
2013-05-21 16:16:46 +08:00
|
|
|
consume_skb(skb);
|
|
|
|
return nb > 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
|
|
|
|
}
|
|
|
|
|
2016-06-22 14:16:49 +08:00
|
|
|
static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch,
|
|
|
|
struct sk_buff **to_free)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
2019-01-10 00:09:42 +08:00
|
|
|
unsigned int len = qdisc_pkt_len(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
int ret;
|
|
|
|
|
2013-05-21 16:16:46 +08:00
|
|
|
if (qdisc_pkt_len(skb) > q->max_size) {
|
2018-03-01 14:13:38 +08:00
|
|
|
if (skb_is_gso(skb) &&
|
|
|
|
skb_gso_validate_mac_len(skb, q->max_size))
|
2016-06-22 14:16:49 +08:00
|
|
|
return tbf_segment(skb, sch, to_free);
|
|
|
|
return qdisc_drop(skb, sch, to_free);
|
2013-05-21 16:16:46 +08:00
|
|
|
}
|
2016-06-22 14:16:49 +08:00
|
|
|
ret = qdisc_enqueue(skb, q->qdisc, to_free);
|
2010-08-10 16:45:40 +08:00
|
|
|
if (ret != NET_XMIT_SUCCESS) {
|
2008-08-05 13:31:03 +08:00
|
|
|
if (net_xmit_drop_count(ret))
|
2014-09-29 02:53:29 +08:00
|
|
|
qdisc_qstats_drop(sch);
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-10 00:09:42 +08:00
|
|
|
sch->qstats.backlog += len;
|
2005-04-17 06:20:36 +08:00
|
|
|
sch->q.qlen++;
|
2010-08-10 16:45:40 +08:00
|
|
|
return NET_XMIT_SUCCESS;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-02 16:30:26 +08:00
|
|
|
static bool tbf_peak_present(const struct tbf_sched_data *q)
|
|
|
|
{
|
|
|
|
return q->peak.rate_bytes_ps;
|
|
|
|
}
|
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
static struct sk_buff *tbf_dequeue(struct Qdisc *sch)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2008-10-31 15:46:19 +08:00
|
|
|
skb = q->qdisc->ops->peek(q->qdisc);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (skb) {
|
2013-02-12 08:12:05 +08:00
|
|
|
s64 now;
|
|
|
|
s64 toks;
|
|
|
|
s64 ptoks = 0;
|
2008-07-20 15:08:27 +08:00
|
|
|
unsigned int len = qdisc_pkt_len(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-08-23 09:32:09 +08:00
|
|
|
now = ktime_get_ns();
|
2013-02-12 08:12:05 +08:00
|
|
|
toks = min_t(s64, now - q->t_c, q->buffer);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-03-02 16:30:26 +08:00
|
|
|
if (tbf_peak_present(q)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ptoks = toks + q->ptokens;
|
2013-02-12 08:12:05 +08:00
|
|
|
if (ptoks > q->mtu)
|
2005-04-17 06:20:36 +08:00
|
|
|
ptoks = q->mtu;
|
2013-02-12 08:12:05 +08:00
|
|
|
ptoks -= (s64) psched_l2t_ns(&q->peak, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
toks += q->tokens;
|
2013-02-12 08:12:05 +08:00
|
|
|
if (toks > q->buffer)
|
2005-04-17 06:20:36 +08:00
|
|
|
toks = q->buffer;
|
2013-02-12 08:12:05 +08:00
|
|
|
toks -= (s64) psched_l2t_ns(&q->rate, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if ((toks|ptoks) >= 0) {
|
2008-10-31 15:47:01 +08:00
|
|
|
skb = qdisc_dequeue_peeked(q->qdisc);
|
2008-10-31 15:46:19 +08:00
|
|
|
if (unlikely(!skb))
|
|
|
|
return NULL;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
q->t_c = now;
|
|
|
|
q->tokens = toks;
|
|
|
|
q->ptokens = ptoks;
|
2016-06-02 07:15:19 +08:00
|
|
|
qdisc_qstats_backlog_dec(sch, skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
sch->q.qlen--;
|
2011-01-21 15:31:33 +08:00
|
|
|
qdisc_bstats_update(sch, skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2013-02-12 08:12:05 +08:00
|
|
|
qdisc_watchdog_schedule_ns(&q->watchdog,
|
2016-06-11 07:41:39 +08:00
|
|
|
now + max_t(long, -toks, -ptoks));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Maybe we have a shorter packet in the queue,
|
|
|
|
which can be sent now. It sounds cool,
|
|
|
|
but, however, this is wrong in principle.
|
|
|
|
We MUST NOT reorder packets under these circumstances.
|
|
|
|
|
|
|
|
Really, if we split the flow into independent
|
|
|
|
subflows, it would be a very good solution.
|
|
|
|
This is the main idea of all FQ algorithms
|
|
|
|
(cf. CSZ, HPFQ, HFSC)
|
|
|
|
*/
|
|
|
|
|
2014-09-29 02:53:29 +08:00
|
|
|
qdisc_qstats_overlimit(sch);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
static void tbf_reset(struct Qdisc *sch)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
qdisc_reset(q->qdisc);
|
2016-06-02 07:15:19 +08:00
|
|
|
sch->qstats.backlog = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
sch->q.qlen = 0;
|
2014-08-23 09:32:09 +08:00
|
|
|
q->t_c = ktime_get_ns();
|
2005-04-17 06:20:36 +08:00
|
|
|
q->tokens = q->buffer;
|
|
|
|
q->ptokens = q->mtu;
|
2007-03-16 16:20:07 +08:00
|
|
|
qdisc_watchdog_cancel(&q->watchdog);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-01-24 12:35:39 +08:00
|
|
|
static const struct nla_policy tbf_policy[TCA_TBF_MAX + 1] = {
|
|
|
|
[TCA_TBF_PARMS] = { .len = sizeof(struct tc_tbf_qopt) },
|
|
|
|
[TCA_TBF_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
|
|
|
|
[TCA_TBF_PTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
|
2013-11-08 10:23:34 +08:00
|
|
|
[TCA_TBF_RATE64] = { .type = NLA_U64 },
|
|
|
|
[TCA_TBF_PRATE64] = { .type = NLA_U64 },
|
2013-12-20 09:24:47 +08:00
|
|
|
[TCA_TBF_BURST] = { .type = NLA_U32 },
|
|
|
|
[TCA_TBF_PBURST] = { .type = NLA_U32 },
|
2008-01-24 12:35:39 +08:00
|
|
|
};
|
|
|
|
|
2017-12-21 01:35:14 +08:00
|
|
|
static int tbf_change(struct Qdisc *sch, struct nlattr *opt,
|
|
|
|
struct netlink_ext_ack *extack)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-01-24 12:33:32 +08:00
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
2013-11-08 10:23:34 +08:00
|
|
|
struct nlattr *tb[TCA_TBF_MAX + 1];
|
2005-04-17 06:20:36 +08:00
|
|
|
struct tc_tbf_qopt *qopt;
|
|
|
|
struct Qdisc *child = NULL;
|
2013-12-10 14:59:27 +08:00
|
|
|
struct psched_ratecfg rate;
|
|
|
|
struct psched_ratecfg peak;
|
|
|
|
u64 max_size;
|
|
|
|
s64 buffer, mtu;
|
2013-11-08 10:23:34 +08:00
|
|
|
u64 rate64 = 0, prate64 = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 20:07:28 +08:00
|
|
|
err = nla_parse_nested_deprecated(tb, TCA_TBF_MAX, opt, tbf_policy,
|
|
|
|
NULL);
|
2008-01-24 12:33:32 +08:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
2008-01-24 12:35:39 +08:00
|
|
|
if (tb[TCA_TBF_PARMS] == NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
qopt = nla_data(tb[TCA_TBF_PARMS]);
|
2013-12-10 14:59:27 +08:00
|
|
|
if (qopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
|
|
|
|
qdisc_put_rtab(qdisc_get_rtab(&qopt->rate,
|
2017-12-21 01:35:18 +08:00
|
|
|
tb[TCA_TBF_RTAB],
|
|
|
|
NULL));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-12-10 14:59:27 +08:00
|
|
|
if (qopt->peakrate.linklayer == TC_LINKLAYER_UNAWARE)
|
|
|
|
qdisc_put_rtab(qdisc_get_rtab(&qopt->peakrate,
|
2017-12-21 01:35:18 +08:00
|
|
|
tb[TCA_TBF_PTAB],
|
|
|
|
NULL));
|
2013-11-24 04:59:20 +08:00
|
|
|
|
2013-12-10 14:59:27 +08:00
|
|
|
buffer = min_t(u64, PSCHED_TICKS2NS(qopt->buffer), ~0U);
|
|
|
|
mtu = min_t(u64, PSCHED_TICKS2NS(qopt->mtu), ~0U);
|
|
|
|
|
|
|
|
if (tb[TCA_TBF_RATE64])
|
|
|
|
rate64 = nla_get_u64(tb[TCA_TBF_RATE64]);
|
|
|
|
psched_ratecfg_precompute(&rate, &qopt->rate, rate64);
|
|
|
|
|
2013-12-20 09:24:47 +08:00
|
|
|
if (tb[TCA_TBF_BURST]) {
|
|
|
|
max_size = nla_get_u32(tb[TCA_TBF_BURST]);
|
|
|
|
buffer = psched_l2t_ns(&rate, max_size);
|
|
|
|
} else {
|
|
|
|
max_size = min_t(u64, psched_ns_t2l(&rate, buffer), ~0U);
|
|
|
|
}
|
2013-12-10 14:59:27 +08:00
|
|
|
|
|
|
|
if (qopt->peakrate.rate) {
|
|
|
|
if (tb[TCA_TBF_PRATE64])
|
|
|
|
prate64 = nla_get_u64(tb[TCA_TBF_PRATE64]);
|
|
|
|
psched_ratecfg_precompute(&peak, &qopt->peakrate, prate64);
|
|
|
|
if (peak.rate_bytes_ps <= rate.rate_bytes_ps) {
|
|
|
|
pr_warn_ratelimited("sch_tbf: peakrate %llu is lower than or equals to rate %llu !\n",
|
2013-12-20 09:24:47 +08:00
|
|
|
peak.rate_bytes_ps, rate.rate_bytes_ps);
|
2013-12-10 14:59:27 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:24:47 +08:00
|
|
|
if (tb[TCA_TBF_PBURST]) {
|
|
|
|
u32 pburst = nla_get_u32(tb[TCA_TBF_PBURST]);
|
|
|
|
max_size = min_t(u32, max_size, pburst);
|
|
|
|
mtu = psched_l2t_ns(&peak, pburst);
|
|
|
|
} else {
|
|
|
|
max_size = min_t(u64, max_size, psched_ns_t2l(&peak, mtu));
|
|
|
|
}
|
2014-03-02 16:30:26 +08:00
|
|
|
} else {
|
|
|
|
memset(&peak, 0, sizeof(peak));
|
2013-12-10 14:59:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (max_size < psched_mtu(qdisc_dev(sch)))
|
|
|
|
pr_warn_ratelimited("sch_tbf: burst %llu is lower than device %s mtu (%u) !\n",
|
|
|
|
max_size, qdisc_dev(sch)->name,
|
|
|
|
psched_mtu(qdisc_dev(sch)));
|
|
|
|
|
|
|
|
if (!max_size) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-02-26 20:43:42 +08:00
|
|
|
if (q->qdisc != &noop_qdisc) {
|
|
|
|
err = fifo_set_limit(q->qdisc, qopt->limit);
|
|
|
|
if (err)
|
|
|
|
goto done;
|
|
|
|
} else if (qopt->limit > 0) {
|
2017-12-21 01:35:21 +08:00
|
|
|
child = fifo_create_dflt(sch, &bfifo_qdisc_ops, qopt->limit,
|
|
|
|
extack);
|
2014-02-26 20:43:42 +08:00
|
|
|
if (IS_ERR(child)) {
|
|
|
|
err = PTR_ERR(child);
|
|
|
|
goto done;
|
|
|
|
}
|
2018-05-18 20:51:44 +08:00
|
|
|
|
|
|
|
/* child is fifo, no need to check for noop_qdisc */
|
|
|
|
qdisc_hash_add(child, true);
|
2014-02-26 20:43:42 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
sch_tree_lock(sch);
|
2006-11-30 09:36:20 +08:00
|
|
|
if (child) {
|
2019-03-28 23:53:13 +08:00
|
|
|
qdisc_tree_flush_backlog(q->qdisc);
|
2018-09-25 00:22:50 +08:00
|
|
|
qdisc_put(q->qdisc);
|
2008-11-20 20:11:36 +08:00
|
|
|
q->qdisc = child;
|
2006-11-30 09:36:20 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
q->limit = qopt->limit;
|
2013-12-20 09:24:47 +08:00
|
|
|
if (tb[TCA_TBF_PBURST])
|
|
|
|
q->mtu = mtu;
|
|
|
|
else
|
|
|
|
q->mtu = PSCHED_TICKS2NS(qopt->mtu);
|
2005-04-17 06:20:36 +08:00
|
|
|
q->max_size = max_size;
|
2013-12-20 09:24:47 +08:00
|
|
|
if (tb[TCA_TBF_BURST])
|
|
|
|
q->buffer = buffer;
|
|
|
|
else
|
|
|
|
q->buffer = PSCHED_TICKS2NS(qopt->buffer);
|
2005-04-17 06:20:36 +08:00
|
|
|
q->tokens = q->buffer;
|
|
|
|
q->ptokens = q->mtu;
|
2008-11-20 20:11:36 +08:00
|
|
|
|
2013-12-10 14:59:27 +08:00
|
|
|
memcpy(&q->rate, &rate, sizeof(struct psched_ratecfg));
|
2014-03-02 16:30:26 +08:00
|
|
|
memcpy(&q->peak, &peak, sizeof(struct psched_ratecfg));
|
2008-11-20 20:11:36 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
sch_tree_unlock(sch);
|
|
|
|
err = 0;
|
2020-01-24 21:23:06 +08:00
|
|
|
|
|
|
|
tbf_offload_change(sch);
|
2005-04-17 06:20:36 +08:00
|
|
|
done:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:35:13 +08:00
|
|
|
static int tbf_init(struct Qdisc *sch, struct nlattr *opt,
|
|
|
|
struct netlink_ext_ack *extack)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
2017-08-30 17:49:05 +08:00
|
|
|
qdisc_watchdog_init(&q->watchdog, sch);
|
|
|
|
q->qdisc = &noop_qdisc;
|
|
|
|
|
2017-12-21 01:35:11 +08:00
|
|
|
if (!opt)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2014-08-23 09:32:09 +08:00
|
|
|
q->t_c = ktime_get_ns();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-12-21 01:35:14 +08:00
|
|
|
return tbf_change(sch, opt, extack);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tbf_destroy(struct Qdisc *sch)
|
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
2007-03-16 16:20:07 +08:00
|
|
|
qdisc_watchdog_cancel(&q->watchdog);
|
2020-01-24 21:23:06 +08:00
|
|
|
tbf_offload_destroy(sch);
|
2018-09-25 00:22:50 +08:00
|
|
|
qdisc_put(q->qdisc);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tbf_dump(struct Qdisc *sch, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
2008-01-24 12:34:11 +08:00
|
|
|
struct nlattr *nest;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct tc_tbf_qopt opt;
|
2020-01-24 21:23:06 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = tbf_offload_dump(sch);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
|
2008-01-24 12:34:11 +08:00
|
|
|
if (nest == NULL)
|
|
|
|
goto nla_put_failure;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
opt.limit = q->limit;
|
2013-06-02 21:55:05 +08:00
|
|
|
psched_ratecfg_getrate(&opt.rate, &q->rate);
|
2014-03-02 16:30:26 +08:00
|
|
|
if (tbf_peak_present(q))
|
2013-06-02 21:55:05 +08:00
|
|
|
psched_ratecfg_getrate(&opt.peakrate, &q->peak);
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
|
|
|
memset(&opt.peakrate, 0, sizeof(opt.peakrate));
|
2013-02-12 08:12:05 +08:00
|
|
|
opt.mtu = PSCHED_NS2TICKS(q->mtu);
|
|
|
|
opt.buffer = PSCHED_NS2TICKS(q->buffer);
|
2012-03-29 17:11:39 +08:00
|
|
|
if (nla_put(skb, TCA_TBF_PARMS, sizeof(opt), &opt))
|
|
|
|
goto nla_put_failure;
|
2013-11-08 10:23:34 +08:00
|
|
|
if (q->rate.rate_bytes_ps >= (1ULL << 32) &&
|
2016-04-25 16:25:15 +08:00
|
|
|
nla_put_u64_64bit(skb, TCA_TBF_RATE64, q->rate.rate_bytes_ps,
|
|
|
|
TCA_TBF_PAD))
|
2013-11-08 10:23:34 +08:00
|
|
|
goto nla_put_failure;
|
2014-03-02 16:30:26 +08:00
|
|
|
if (tbf_peak_present(q) &&
|
2013-11-08 10:23:34 +08:00
|
|
|
q->peak.rate_bytes_ps >= (1ULL << 32) &&
|
2016-04-25 16:25:15 +08:00
|
|
|
nla_put_u64_64bit(skb, TCA_TBF_PRATE64, q->peak.rate_bytes_ps,
|
|
|
|
TCA_TBF_PAD))
|
2013-11-08 10:23:34 +08:00
|
|
|
goto nla_put_failure;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-03-12 10:20:32 +08:00
|
|
|
return nla_nest_end(skb, nest);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-23 14:11:17 +08:00
|
|
|
nla_put_failure:
|
2008-01-24 12:34:11 +08:00
|
|
|
nla_nest_cancel(skb, nest);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tbf_dump_class(struct Qdisc *sch, unsigned long cl,
|
|
|
|
struct sk_buff *skb, struct tcmsg *tcm)
|
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
tcm->tcm_handle |= TC_H_MIN(1);
|
|
|
|
tcm->tcm_info = q->qdisc->handle;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tbf_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
|
2017-12-21 01:35:17 +08:00
|
|
|
struct Qdisc **old, struct netlink_ext_ack *extack)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
|
|
|
|
if (new == NULL)
|
|
|
|
new = &noop_qdisc;
|
|
|
|
|
2016-02-26 06:55:00 +08:00
|
|
|
*old = qdisc_replace(sch, new, &q->qdisc);
|
2021-10-19 16:07:04 +08:00
|
|
|
|
|
|
|
tbf_offload_graft(sch, new, *old, extack);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Qdisc *tbf_leaf(struct Qdisc *sch, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct tbf_sched_data *q = qdisc_priv(sch);
|
|
|
|
return q->qdisc;
|
|
|
|
}
|
|
|
|
|
net_sched: remove tc class reference counting
For TC classes, their ->get() and ->put() are always paired, and the
reference counting is completely useless, because:
1) For class modification and dumping paths, we already hold RTNL lock,
so all of these ->get(),->change(),->put() are atomic.
2) For filter bindiing/unbinding, we use other reference counter than
this one, and they should have RTNL lock too.
3) For ->qlen_notify(), it is special because it is called on ->enqueue()
path, but we already hold qdisc tree lock there, and we hold this
tree lock when graft or delete the class too, so it should not be gone
or changed until we release the tree lock.
Therefore, this patch removes ->get() and ->put(), but:
1) Adds a new ->find() to find the pointer to a class by classid, no
refcnt.
2) Move the original class destroy upon the last refcnt into ->delete(),
right after releasing tree lock. This is fine because the class is
already removed from hash when holding the lock.
For those who also use ->put() as ->unbind(), just rename them to reflect
this change.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Acked-by: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-25 07:51:29 +08:00
|
|
|
static unsigned long tbf_find(struct Qdisc *sch, u32 classid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tbf_walk(struct Qdisc *sch, struct qdisc_walker *walker)
|
|
|
|
{
|
|
|
|
if (!walker->stop) {
|
|
|
|
if (walker->count >= walker->skip)
|
|
|
|
if (walker->fn(sch, 1, walker) < 0) {
|
|
|
|
walker->stop = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
walker->count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
static const struct Qdisc_class_ops tbf_class_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.graft = tbf_graft,
|
|
|
|
.leaf = tbf_leaf,
|
net_sched: remove tc class reference counting
For TC classes, their ->get() and ->put() are always paired, and the
reference counting is completely useless, because:
1) For class modification and dumping paths, we already hold RTNL lock,
so all of these ->get(),->change(),->put() are atomic.
2) For filter bindiing/unbinding, we use other reference counter than
this one, and they should have RTNL lock too.
3) For ->qlen_notify(), it is special because it is called on ->enqueue()
path, but we already hold qdisc tree lock there, and we hold this
tree lock when graft or delete the class too, so it should not be gone
or changed until we release the tree lock.
Therefore, this patch removes ->get() and ->put(), but:
1) Adds a new ->find() to find the pointer to a class by classid, no
refcnt.
2) Move the original class destroy upon the last refcnt into ->delete(),
right after releasing tree lock. This is fine because the class is
already removed from hash when holding the lock.
For those who also use ->put() as ->unbind(), just rename them to reflect
this change.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Acked-by: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-25 07:51:29 +08:00
|
|
|
.find = tbf_find,
|
2005-04-17 06:20:36 +08:00
|
|
|
.walk = tbf_walk,
|
|
|
|
.dump = tbf_dump_class,
|
|
|
|
};
|
|
|
|
|
2007-11-14 17:44:41 +08:00
|
|
|
static struct Qdisc_ops tbf_qdisc_ops __read_mostly = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.next = NULL,
|
|
|
|
.cl_ops = &tbf_class_ops,
|
|
|
|
.id = "tbf",
|
|
|
|
.priv_size = sizeof(struct tbf_sched_data),
|
|
|
|
.enqueue = tbf_enqueue,
|
|
|
|
.dequeue = tbf_dequeue,
|
2008-10-31 15:47:01 +08:00
|
|
|
.peek = qdisc_peek_dequeued,
|
2005-04-17 06:20:36 +08:00
|
|
|
.init = tbf_init,
|
|
|
|
.reset = tbf_reset,
|
|
|
|
.destroy = tbf_destroy,
|
|
|
|
.change = tbf_change,
|
|
|
|
.dump = tbf_dump,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init tbf_module_init(void)
|
|
|
|
{
|
|
|
|
return register_qdisc(&tbf_qdisc_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit tbf_module_exit(void)
|
|
|
|
{
|
|
|
|
unregister_qdisc(&tbf_qdisc_ops);
|
|
|
|
}
|
|
|
|
module_init(tbf_module_init)
|
|
|
|
module_exit(tbf_module_exit)
|
|
|
|
MODULE_LICENSE("GPL");
|