forked from Minki/linux
d6124d6ba6
the following script: # tc qdisc add dev crash0 clsact # tc filter add dev crash0 egress matchall \ > action police rate 3mbit burst 250k pass index 90 # tc actions replace action police \ > rate 3mbit burst 250k goto chain 42 index 90 cookie c1a0c1a0 # tc actions show action police rate 3mbit burst had the following output: Error: Failed to init TC action chain. We have an error talking to the kernel total acts 1 action order 0: police 0x5a rate 3Mbit burst 250Kb mtu 2Kb action goto chain 42 overhead 0b ref 2 bind 1 cookie c1a0c1a0 Then, when crash0 starts transmitting more than 3Mbit/s, the following kernel crash is observed: BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 #PF error: [normal kernel read fault] PGD 800000007a779067 P4D 800000007a779067 PUD 2ad96067 PMD 0 Oops: 0000 [#1] SMP PTI CPU: 3 PID: 5032 Comm: netperf Not tainted 5.0.0-rc4.gotochain_crash+ #533 Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 RIP: 0010:tcf_action_exec+0xb8/0x100 Code: 00 00 00 20 74 1d 83 f8 03 75 09 49 83 c4 08 4d 39 ec 75 bc 48 83 c4 10 5b 5d 41 5c 41 5d 41 5e 41 5f c3 49 8b 97 a8 00 00 00 <48> 8b 12 48 89 55 00 48 83 c4 10 5b 5d 41 5c 41 5d 41 5e 41 5f c3 RSP: 0018:ffffb0e04064fa60 EFLAGS: 00010246 RAX: 000000002000002a RBX: ffff93bb3322cce0 RCX: 0000000000000005 RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff93bb3322cce0 RBP: ffffb0e04064fb00 R08: 0000000000000022 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000001 R12: ffff93bb3beed300 R13: ffff93bb3beed308 R14: 0000000000000001 R15: ffff93bb3b64d000 FS: 00007f0bc6be5740(0000) GS:ffff93bb3db80000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000000 CR3: 00000000746a8001 CR4: 00000000001606e0 Call Trace: tcf_classify+0x58/0x120 __dev_queue_xmit+0x40a/0x890 ? ipt_do_table+0x31c/0x420 [ip_tables] ? ip_finish_output2+0x16f/0x430 ip_finish_output2+0x16f/0x430 ? ip_output+0x69/0xe0 ip_output+0x69/0xe0 ? ip_forward_options+0x1a0/0x1a0 __tcp_transmit_skb+0x563/0xa40 tcp_write_xmit+0x243/0xfa0 __tcp_push_pending_frames+0x32/0xf0 tcp_sendmsg_locked+0x404/0xd30 tcp_sendmsg+0x27/0x40 sock_sendmsg+0x36/0x40 __sys_sendto+0x10e/0x140 ? __sys_connect+0x87/0xf0 ? syscall_trace_enter+0x1df/0x2e0 ? __audit_syscall_exit+0x216/0x260 __x64_sys_sendto+0x24/0x30 do_syscall_64+0x5b/0x180 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7f0bc5ffbafd Code: 89 01 48 83 c8 ff c3 66 0f 1f 44 00 00 8b 05 ae c4 2c 00 85 c0 75 2d 45 31 c9 45 31 c0 4c 63 d1 48 63 ff b8 2c 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 01 c3 48 8b 15 63 63 2c 00 f7 d8 64 89 02 48 RSP: 002b:00007fffef94b7f8 EFLAGS: 00000246 ORIG_RAX: 000000000000002c RAX: ffffffffffffffda RBX: 0000000000004000 RCX: 00007f0bc5ffbafd RDX: 0000000000004000 RSI: 00000000017e5420 RDI: 0000000000000004 RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000004 R13: 00000000017e51d0 R14: 0000000000000010 R15: 0000000000000006 Modules linked in: act_police veth ip6table_filter ip6_tables iptable_filter binfmt_misc ext4 snd_hda_codec_generic mbcache crct10dif_pclmul jbd2 crc32_pclmul ghash_clmulni_intel snd_hda_intel snd_hda_codec snd_hwdep snd_hda_core snd_seq snd_seq_device snd_pcm aesni_intel crypto_simd cryptd glue_helper snd_timer snd joydev pcspkr virtio_balloon soundcore i2c_piix4 nfsd auth_rpcgss nfs_acl lockd grace sunrpc ip_tables xfs ata_generic pata_acpi qxl drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm drm virtio_blk virtio_net virtio_console net_failover failover crc32c_intel ata_piix libata serio_raw virtio_pci virtio_ring virtio floppy dm_mirror dm_region_hash dm_log dm_mod CR2: 0000000000000000 Validating the control action within tcf_police_init() proved to fix the above issue. A TDC selftest is added to verify the correct behavior. Fixes:db50514f9a
("net: sched: add termination action to allow goto chain") Fixes:97763dc0f4
("net_sched: reject unknown tcfa_action values") Signed-off-by: Davide Caratti <dcaratti@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
422 lines
11 KiB
C
422 lines
11 KiB
C
/*
|
|
* net/sched/act_police.c Input police filter
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
* J Hadi Salim (action changes)
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/types.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/string.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/rtnetlink.h>
|
|
#include <linux/init.h>
|
|
#include <linux/slab.h>
|
|
#include <net/act_api.h>
|
|
#include <net/netlink.h>
|
|
#include <net/pkt_cls.h>
|
|
|
|
struct tcf_police_params {
|
|
int tcfp_result;
|
|
u32 tcfp_ewma_rate;
|
|
s64 tcfp_burst;
|
|
u32 tcfp_mtu;
|
|
s64 tcfp_mtu_ptoks;
|
|
struct psched_ratecfg rate;
|
|
bool rate_present;
|
|
struct psched_ratecfg peak;
|
|
bool peak_present;
|
|
struct rcu_head rcu;
|
|
};
|
|
|
|
struct tcf_police {
|
|
struct tc_action common;
|
|
struct tcf_police_params __rcu *params;
|
|
|
|
spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
|
|
s64 tcfp_toks;
|
|
s64 tcfp_ptoks;
|
|
s64 tcfp_t_c;
|
|
};
|
|
|
|
#define to_police(pc) ((struct tcf_police *)pc)
|
|
|
|
/* old policer structure from before tc actions */
|
|
struct tc_police_compat {
|
|
u32 index;
|
|
int action;
|
|
u32 limit;
|
|
u32 burst;
|
|
u32 mtu;
|
|
struct tc_ratespec rate;
|
|
struct tc_ratespec peakrate;
|
|
};
|
|
|
|
/* Each policer is serialized by its individual spinlock */
|
|
|
|
static unsigned int police_net_id;
|
|
static struct tc_action_ops act_police_ops;
|
|
|
|
static int tcf_police_walker(struct net *net, struct sk_buff *skb,
|
|
struct netlink_callback *cb, int type,
|
|
const struct tc_action_ops *ops,
|
|
struct netlink_ext_ack *extack)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, police_net_id);
|
|
|
|
return tcf_generic_walker(tn, skb, cb, type, ops, extack);
|
|
}
|
|
|
|
static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
|
|
[TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
|
|
[TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
|
|
[TCA_POLICE_AVRATE] = { .type = NLA_U32 },
|
|
[TCA_POLICE_RESULT] = { .type = NLA_U32 },
|
|
};
|
|
|
|
static int tcf_police_init(struct net *net, struct nlattr *nla,
|
|
struct nlattr *est, struct tc_action **a,
|
|
int ovr, int bind, bool rtnl_held,
|
|
struct tcf_proto *tp,
|
|
struct netlink_ext_ack *extack)
|
|
{
|
|
int ret = 0, tcfp_result = TC_ACT_OK, err, size;
|
|
struct nlattr *tb[TCA_POLICE_MAX + 1];
|
|
struct tcf_chain *goto_ch = NULL;
|
|
struct tc_police *parm;
|
|
struct tcf_police *police;
|
|
struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
|
|
struct tc_action_net *tn = net_generic(net, police_net_id);
|
|
struct tcf_police_params *new;
|
|
bool exists = false;
|
|
|
|
if (nla == NULL)
|
|
return -EINVAL;
|
|
|
|
err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
if (tb[TCA_POLICE_TBF] == NULL)
|
|
return -EINVAL;
|
|
size = nla_len(tb[TCA_POLICE_TBF]);
|
|
if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
|
|
return -EINVAL;
|
|
|
|
parm = nla_data(tb[TCA_POLICE_TBF]);
|
|
err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
|
|
if (err < 0)
|
|
return err;
|
|
exists = err;
|
|
if (exists && bind)
|
|
return 0;
|
|
|
|
if (!exists) {
|
|
ret = tcf_idr_create(tn, parm->index, NULL, a,
|
|
&act_police_ops, bind, true);
|
|
if (ret) {
|
|
tcf_idr_cleanup(tn, parm->index);
|
|
return ret;
|
|
}
|
|
ret = ACT_P_CREATED;
|
|
spin_lock_init(&(to_police(*a)->tcfp_lock));
|
|
} else if (!ovr) {
|
|
tcf_idr_release(*a, bind);
|
|
return -EEXIST;
|
|
}
|
|
err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
|
|
if (err < 0)
|
|
goto release_idr;
|
|
|
|
police = to_police(*a);
|
|
if (parm->rate.rate) {
|
|
err = -ENOMEM;
|
|
R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
|
|
if (R_tab == NULL)
|
|
goto failure;
|
|
|
|
if (parm->peakrate.rate) {
|
|
P_tab = qdisc_get_rtab(&parm->peakrate,
|
|
tb[TCA_POLICE_PEAKRATE], NULL);
|
|
if (P_tab == NULL)
|
|
goto failure;
|
|
}
|
|
}
|
|
|
|
if (est) {
|
|
err = gen_replace_estimator(&police->tcf_bstats,
|
|
police->common.cpu_bstats,
|
|
&police->tcf_rate_est,
|
|
&police->tcf_lock,
|
|
NULL, est);
|
|
if (err)
|
|
goto failure;
|
|
} else if (tb[TCA_POLICE_AVRATE] &&
|
|
(ret == ACT_P_CREATED ||
|
|
!gen_estimator_active(&police->tcf_rate_est))) {
|
|
err = -EINVAL;
|
|
goto failure;
|
|
}
|
|
|
|
if (tb[TCA_POLICE_RESULT]) {
|
|
tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
|
|
if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
|
|
NL_SET_ERR_MSG(extack,
|
|
"goto chain not allowed on fallback");
|
|
err = -EINVAL;
|
|
goto failure;
|
|
}
|
|
}
|
|
|
|
new = kzalloc(sizeof(*new), GFP_KERNEL);
|
|
if (unlikely(!new)) {
|
|
err = -ENOMEM;
|
|
goto failure;
|
|
}
|
|
|
|
/* No failure allowed after this point */
|
|
new->tcfp_result = tcfp_result;
|
|
new->tcfp_mtu = parm->mtu;
|
|
if (!new->tcfp_mtu) {
|
|
new->tcfp_mtu = ~0;
|
|
if (R_tab)
|
|
new->tcfp_mtu = 255 << R_tab->rate.cell_log;
|
|
}
|
|
if (R_tab) {
|
|
new->rate_present = true;
|
|
psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
|
|
qdisc_put_rtab(R_tab);
|
|
} else {
|
|
new->rate_present = false;
|
|
}
|
|
if (P_tab) {
|
|
new->peak_present = true;
|
|
psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
|
|
qdisc_put_rtab(P_tab);
|
|
} else {
|
|
new->peak_present = false;
|
|
}
|
|
|
|
new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
|
|
if (new->peak_present)
|
|
new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
|
|
new->tcfp_mtu);
|
|
|
|
if (tb[TCA_POLICE_AVRATE])
|
|
new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
|
|
|
|
spin_lock_bh(&police->tcf_lock);
|
|
spin_lock_bh(&police->tcfp_lock);
|
|
police->tcfp_t_c = ktime_get_ns();
|
|
police->tcfp_toks = new->tcfp_burst;
|
|
if (new->peak_present)
|
|
police->tcfp_ptoks = new->tcfp_mtu_ptoks;
|
|
spin_unlock_bh(&police->tcfp_lock);
|
|
goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
|
|
rcu_swap_protected(police->params,
|
|
new,
|
|
lockdep_is_held(&police->tcf_lock));
|
|
spin_unlock_bh(&police->tcf_lock);
|
|
|
|
if (goto_ch)
|
|
tcf_chain_put_by_act(goto_ch);
|
|
if (new)
|
|
kfree_rcu(new, rcu);
|
|
|
|
if (ret == ACT_P_CREATED)
|
|
tcf_idr_insert(tn, *a);
|
|
return ret;
|
|
|
|
failure:
|
|
qdisc_put_rtab(P_tab);
|
|
qdisc_put_rtab(R_tab);
|
|
if (goto_ch)
|
|
tcf_chain_put_by_act(goto_ch);
|
|
release_idr:
|
|
tcf_idr_release(*a, bind);
|
|
return err;
|
|
}
|
|
|
|
static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
|
|
struct tcf_result *res)
|
|
{
|
|
struct tcf_police *police = to_police(a);
|
|
struct tcf_police_params *p;
|
|
s64 now, toks, ptoks = 0;
|
|
int ret;
|
|
|
|
tcf_lastuse_update(&police->tcf_tm);
|
|
bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
|
|
|
|
ret = READ_ONCE(police->tcf_action);
|
|
p = rcu_dereference_bh(police->params);
|
|
|
|
if (p->tcfp_ewma_rate) {
|
|
struct gnet_stats_rate_est64 sample;
|
|
|
|
if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
|
|
sample.bps >= p->tcfp_ewma_rate)
|
|
goto inc_overlimits;
|
|
}
|
|
|
|
if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
|
|
if (!p->rate_present) {
|
|
ret = p->tcfp_result;
|
|
goto end;
|
|
}
|
|
|
|
now = ktime_get_ns();
|
|
spin_lock_bh(&police->tcfp_lock);
|
|
toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
|
|
if (p->peak_present) {
|
|
ptoks = toks + police->tcfp_ptoks;
|
|
if (ptoks > p->tcfp_mtu_ptoks)
|
|
ptoks = p->tcfp_mtu_ptoks;
|
|
ptoks -= (s64)psched_l2t_ns(&p->peak,
|
|
qdisc_pkt_len(skb));
|
|
}
|
|
toks += police->tcfp_toks;
|
|
if (toks > p->tcfp_burst)
|
|
toks = p->tcfp_burst;
|
|
toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
|
|
if ((toks|ptoks) >= 0) {
|
|
police->tcfp_t_c = now;
|
|
police->tcfp_toks = toks;
|
|
police->tcfp_ptoks = ptoks;
|
|
spin_unlock_bh(&police->tcfp_lock);
|
|
ret = p->tcfp_result;
|
|
goto inc_drops;
|
|
}
|
|
spin_unlock_bh(&police->tcfp_lock);
|
|
}
|
|
|
|
inc_overlimits:
|
|
qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
|
|
inc_drops:
|
|
if (ret == TC_ACT_SHOT)
|
|
qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
|
|
end:
|
|
return ret;
|
|
}
|
|
|
|
static void tcf_police_cleanup(struct tc_action *a)
|
|
{
|
|
struct tcf_police *police = to_police(a);
|
|
struct tcf_police_params *p;
|
|
|
|
p = rcu_dereference_protected(police->params, 1);
|
|
if (p)
|
|
kfree_rcu(p, rcu);
|
|
}
|
|
|
|
static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
|
|
int bind, int ref)
|
|
{
|
|
unsigned char *b = skb_tail_pointer(skb);
|
|
struct tcf_police *police = to_police(a);
|
|
struct tcf_police_params *p;
|
|
struct tc_police opt = {
|
|
.index = police->tcf_index,
|
|
.refcnt = refcount_read(&police->tcf_refcnt) - ref,
|
|
.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
|
|
};
|
|
struct tcf_t t;
|
|
|
|
spin_lock_bh(&police->tcf_lock);
|
|
opt.action = police->tcf_action;
|
|
p = rcu_dereference_protected(police->params,
|
|
lockdep_is_held(&police->tcf_lock));
|
|
opt.mtu = p->tcfp_mtu;
|
|
opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
|
|
if (p->rate_present)
|
|
psched_ratecfg_getrate(&opt.rate, &p->rate);
|
|
if (p->peak_present)
|
|
psched_ratecfg_getrate(&opt.peakrate, &p->peak);
|
|
if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
|
|
goto nla_put_failure;
|
|
if (p->tcfp_result &&
|
|
nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
|
|
goto nla_put_failure;
|
|
if (p->tcfp_ewma_rate &&
|
|
nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
|
|
goto nla_put_failure;
|
|
|
|
t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
|
|
t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
|
|
t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
|
|
t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
|
|
if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
|
|
goto nla_put_failure;
|
|
spin_unlock_bh(&police->tcf_lock);
|
|
|
|
return skb->len;
|
|
|
|
nla_put_failure:
|
|
spin_unlock_bh(&police->tcf_lock);
|
|
nlmsg_trim(skb, b);
|
|
return -1;
|
|
}
|
|
|
|
static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, police_net_id);
|
|
|
|
return tcf_idr_search(tn, a, index);
|
|
}
|
|
|
|
MODULE_AUTHOR("Alexey Kuznetsov");
|
|
MODULE_DESCRIPTION("Policing actions");
|
|
MODULE_LICENSE("GPL");
|
|
|
|
static struct tc_action_ops act_police_ops = {
|
|
.kind = "police",
|
|
.id = TCA_ID_POLICE,
|
|
.owner = THIS_MODULE,
|
|
.act = tcf_police_act,
|
|
.dump = tcf_police_dump,
|
|
.init = tcf_police_init,
|
|
.walk = tcf_police_walker,
|
|
.lookup = tcf_police_search,
|
|
.cleanup = tcf_police_cleanup,
|
|
.size = sizeof(struct tcf_police),
|
|
};
|
|
|
|
static __net_init int police_init_net(struct net *net)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, police_net_id);
|
|
|
|
return tc_action_net_init(tn, &act_police_ops);
|
|
}
|
|
|
|
static void __net_exit police_exit_net(struct list_head *net_list)
|
|
{
|
|
tc_action_net_exit(net_list, police_net_id);
|
|
}
|
|
|
|
static struct pernet_operations police_net_ops = {
|
|
.init = police_init_net,
|
|
.exit_batch = police_exit_net,
|
|
.id = &police_net_id,
|
|
.size = sizeof(struct tc_action_net),
|
|
};
|
|
|
|
static int __init police_init_module(void)
|
|
{
|
|
return tcf_register_action(&act_police_ops, &police_net_ops);
|
|
}
|
|
|
|
static void __exit police_cleanup_module(void)
|
|
{
|
|
tcf_unregister_action(&act_police_ops, &police_net_ops);
|
|
}
|
|
|
|
module_init(police_init_module);
|
|
module_exit(police_cleanup_module);
|