Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jiri Pirko | 0c6965d | 2014-11-05 20:51:51 +0100 | [diff] [blame] | 3 | * net/sched/act_police.c Input police filter |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 6 | * J Hadi Salim (action changes) |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/rtnetlink.h> |
| 16 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 19 | #include <net/netlink.h> |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 20 | #include <net/pkt_cls.h> |
Pieter Jansen van Vuuren | fa762da | 2019-05-04 04:46:21 -0700 | [diff] [blame] | 21 | #include <net/tc_act/tc_police.h> |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* Each policer is serialized by its individual spinlock */ |
| 24 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 25 | static unsigned int police_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 26 | static struct tc_action_ops act_police_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 27 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 28 | static int tcf_police_walker(struct net *net, struct sk_buff *skb, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 29 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 30 | const struct tc_action_ops *ops, |
| 31 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 33 | struct tc_action_net *tn = net_generic(net, police_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 35 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 38 | static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { |
| 39 | [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, |
| 40 | [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, |
| 41 | [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, |
| 42 | [TCA_POLICE_RESULT] = { .type = NLA_U32 }, |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 43 | [TCA_POLICE_RATE64] = { .type = NLA_U64 }, |
| 44 | [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 }, |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 47 | static int tcf_police_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 48 | struct nlattr *est, struct tc_action **a, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 49 | int ovr, int bind, bool rtnl_held, |
Vlad Buslov | abbb0d3 | 2019-10-30 16:09:05 +0200 | [diff] [blame] | 50 | struct tcf_proto *tp, u32 flags, |
Alexander Aring | 589dad6 | 2018-02-15 10:54:56 -0500 | [diff] [blame] | 51 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 53 | int ret = 0, tcfp_result = TC_ACT_OK, err, size; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 54 | struct nlattr *tb[TCA_POLICE_MAX + 1]; |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 55 | struct tcf_chain *goto_ch = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct tc_police *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 57 | struct tcf_police *police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 59 | struct tc_action_net *tn = net_generic(net, police_net_id); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 60 | struct tcf_police_params *new; |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 61 | bool exists = false; |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 62 | u32 index; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 63 | u64 rate64, prate64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 65 | if (nla == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return -EINVAL; |
| 67 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 68 | err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla, |
| 69 | police_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 70 | if (err < 0) |
| 71 | return err; |
| 72 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 73 | if (tb[TCA_POLICE_TBF] == NULL) |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 74 | return -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 75 | size = nla_len(tb[TCA_POLICE_TBF]); |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 76 | if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 79 | parm = nla_data(tb[TCA_POLICE_TBF]); |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 80 | index = parm->index; |
| 81 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 82 | if (err < 0) |
| 83 | return err; |
| 84 | exists = err; |
WANG Cong | 0852e45 | 2016-08-13 22:35:01 -0700 | [diff] [blame] | 85 | if (exists && bind) |
| 86 | return 0; |
| 87 | |
| 88 | if (!exists) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 89 | ret = tcf_idr_create(tn, index, NULL, a, |
Vlad Buslov | e382267 | 2019-10-30 16:09:06 +0200 | [diff] [blame] | 90 | &act_police_ops, bind, true, 0); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 91 | if (ret) { |
Dmytro Linkin | 7be8ef2 | 2019-08-01 13:02:51 +0000 | [diff] [blame] | 92 | tcf_idr_cleanup(tn, index); |
WANG Cong | a03e6fe | 2016-06-06 09:54:30 -0700 | [diff] [blame] | 93 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 94 | } |
WANG Cong | a03e6fe | 2016-06-06 09:54:30 -0700 | [diff] [blame] | 95 | ret = ACT_P_CREATED; |
Davide Caratti | 484afd1 | 2018-11-21 18:23:53 +0100 | [diff] [blame] | 96 | spin_lock_init(&(to_police(*a)->tcfp_lock)); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 97 | } else if (!ovr) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 98 | tcf_idr_release(*a, bind); |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 99 | return -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 101 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 102 | if (err < 0) |
| 103 | goto release_idr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 105 | police = to_police(*a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (parm->rate.rate) { |
| 107 | err = -ENOMEM; |
Alexander Aring | e9bc3fa | 2017-12-20 12:35:18 -0500 | [diff] [blame] | 108 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (R_tab == NULL) |
| 110 | goto failure; |
Stephen Hemminger | c1b5687 | 2008-11-25 21:14:06 -0800 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (parm->peakrate.rate) { |
| 113 | P_tab = qdisc_get_rtab(&parm->peakrate, |
Alexander Aring | e9bc3fa | 2017-12-20 12:35:18 -0500 | [diff] [blame] | 114 | tb[TCA_POLICE_PEAKRATE], NULL); |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 115 | if (P_tab == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | goto failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 119 | |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 120 | if (est) { |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 121 | err = gen_replace_estimator(&police->tcf_bstats, |
| 122 | police->common.cpu_bstats, |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 123 | &police->tcf_rate_est, |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 124 | &police->tcf_lock, |
| 125 | NULL, est); |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 126 | if (err) |
WANG Cong | 7403060 | 2017-06-13 13:36:24 -0700 | [diff] [blame] | 127 | goto failure; |
Jarek Poplawski | a883bf5 | 2009-03-04 17:38:10 -0800 | [diff] [blame] | 128 | } else if (tb[TCA_POLICE_AVRATE] && |
| 129 | (ret == ACT_P_CREATED || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 130 | !gen_estimator_active(&police->tcf_rate_est))) { |
Jarek Poplawski | a883bf5 | 2009-03-04 17:38:10 -0800 | [diff] [blame] | 131 | err = -EINVAL; |
WANG Cong | 7403060 | 2017-06-13 13:36:24 -0700 | [diff] [blame] | 132 | goto failure; |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 135 | if (tb[TCA_POLICE_RESULT]) { |
| 136 | tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); |
| 137 | if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) { |
| 138 | NL_SET_ERR_MSG(extack, |
| 139 | "goto chain not allowed on fallback"); |
| 140 | err = -EINVAL; |
| 141 | goto failure; |
| 142 | } |
| 143 | } |
| 144 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 145 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 146 | if (unlikely(!new)) { |
| 147 | err = -ENOMEM; |
| 148 | goto failure; |
| 149 | } |
| 150 | |
Stephen Hemminger | 71bcb09 | 2008-11-25 21:13:31 -0800 | [diff] [blame] | 151 | /* No failure allowed after this point */ |
Davide Caratti | fd6d433 | 2018-11-28 18:43:42 +0100 | [diff] [blame] | 152 | new->tcfp_result = tcfp_result; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 153 | new->tcfp_mtu = parm->mtu; |
| 154 | if (!new->tcfp_mtu) { |
| 155 | new->tcfp_mtu = ~0; |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 156 | if (R_tab) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 157 | new->tcfp_mtu = 255 << R_tab->rate.cell_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 159 | if (R_tab) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 160 | new->rate_present = true; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 161 | rate64 = tb[TCA_POLICE_RATE64] ? |
| 162 | nla_get_u64(tb[TCA_POLICE_RATE64]) : 0; |
| 163 | psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64); |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 164 | qdisc_put_rtab(R_tab); |
| 165 | } else { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 166 | new->rate_present = false; |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 167 | } |
| 168 | if (P_tab) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 169 | new->peak_present = true; |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 170 | prate64 = tb[TCA_POLICE_PEAKRATE64] ? |
| 171 | nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0; |
| 172 | psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64); |
Jiri Pirko | c6d14ff | 2013-02-12 00:12:07 +0000 | [diff] [blame] | 173 | qdisc_put_rtab(P_tab); |
| 174 | } else { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 175 | new->peak_present = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 178 | new->tcfp_burst = PSCHED_TICKS2NS(parm->burst); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 179 | if (new->peak_present) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 180 | new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak, |
| 181 | new->tcfp_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 183 | if (tb[TCA_POLICE_AVRATE]) |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 184 | new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 186 | spin_lock_bh(&police->tcf_lock); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 187 | spin_lock_bh(&police->tcfp_lock); |
| 188 | police->tcfp_t_c = ktime_get_ns(); |
| 189 | police->tcfp_toks = new->tcfp_burst; |
| 190 | if (new->peak_present) |
| 191 | police->tcfp_ptoks = new->tcfp_mtu_ptoks; |
| 192 | spin_unlock_bh(&police->tcfp_lock); |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 193 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Paul E. McKenney | 445d374 | 2019-09-23 16:09:18 -0700 | [diff] [blame] | 194 | new = rcu_replace_pointer(police->params, |
| 195 | new, |
| 196 | lockdep_is_held(&police->tcf_lock)); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 197 | spin_unlock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 199 | if (goto_ch) |
| 200 | tcf_chain_put_by_act(goto_ch); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 201 | if (new) |
| 202 | kfree_rcu(new, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return ret; |
| 205 | |
| 206 | failure: |
Yang Yingliang | 3b69a4c | 2013-12-17 15:29:16 +0800 | [diff] [blame] | 207 | qdisc_put_rtab(P_tab); |
| 208 | qdisc_put_rtab(R_tab); |
Davide Caratti | d6124d6 | 2019-03-20 15:00:08 +0100 | [diff] [blame] | 209 | if (goto_ch) |
| 210 | tcf_chain_put_by_act(goto_ch); |
| 211 | release_idr: |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 212 | tcf_idr_release(*a, bind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return err; |
| 214 | } |
| 215 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 216 | static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 217 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 219 | struct tcf_police *police = to_police(a); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 220 | struct tcf_police_params *p; |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 221 | s64 now, toks, ptoks = 0; |
| 222 | int ret; |
| 223 | |
| 224 | tcf_lastuse_update(&police->tcf_tm); |
| 225 | bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 227 | ret = READ_ONCE(police->tcf_action); |
| 228 | p = rcu_dereference_bh(police->params); |
| 229 | |
| 230 | if (p->tcfp_ewma_rate) { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 231 | struct gnet_stats_rate_est64 sample; |
| 232 | |
| 233 | if (!gen_estimator_read(&police->tcf_rate_est, &sample) || |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 234 | sample.bps >= p->tcfp_ewma_rate) |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 235 | goto inc_overlimits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 238 | if (qdisc_pkt_len(skb) <= p->tcfp_mtu) { |
| 239 | if (!p->rate_present) { |
| 240 | ret = p->tcfp_result; |
| 241 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Eric Dumazet | d2de875 | 2014-08-22 18:32:09 -0700 | [diff] [blame] | 244 | now = ktime_get_ns(); |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 245 | spin_lock_bh(&police->tcfp_lock); |
| 246 | toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 247 | if (p->peak_present) { |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 248 | ptoks = toks + police->tcfp_ptoks; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 249 | if (ptoks > p->tcfp_mtu_ptoks) |
| 250 | ptoks = p->tcfp_mtu_ptoks; |
| 251 | ptoks -= (s64)psched_l2t_ns(&p->peak, |
| 252 | qdisc_pkt_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 254 | toks += police->tcfp_toks; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 255 | if (toks > p->tcfp_burst) |
| 256 | toks = p->tcfp_burst; |
| 257 | toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if ((toks|ptoks) >= 0) { |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 259 | police->tcfp_t_c = now; |
| 260 | police->tcfp_toks = toks; |
| 261 | police->tcfp_ptoks = ptoks; |
| 262 | spin_unlock_bh(&police->tcfp_lock); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 263 | ret = p->tcfp_result; |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 264 | goto inc_drops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
Davide Caratti | f2cbd48 | 2018-11-20 22:18:44 +0100 | [diff] [blame] | 266 | spin_unlock_bh(&police->tcfp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 269 | inc_overlimits: |
| 270 | qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats)); |
| 271 | inc_drops: |
| 272 | if (ret == TC_ACT_SHOT) |
| 273 | qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats)); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 274 | end: |
Davide Caratti | 93be42f | 2018-09-13 19:29:12 +0200 | [diff] [blame] | 275 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 278 | static void tcf_police_cleanup(struct tc_action *a) |
| 279 | { |
| 280 | struct tcf_police *police = to_police(a); |
| 281 | struct tcf_police_params *p; |
| 282 | |
| 283 | p = rcu_dereference_protected(police->params, 1); |
| 284 | if (p) |
| 285 | kfree_rcu(p, rcu); |
| 286 | } |
| 287 | |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 288 | static void tcf_police_stats_update(struct tc_action *a, |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 289 | u64 bytes, u64 packets, u64 drops, |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 290 | u64 lastuse, bool hw) |
| 291 | { |
| 292 | struct tcf_police *police = to_police(a); |
| 293 | struct tcf_t *tm = &police->tcf_tm; |
| 294 | |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 295 | tcf_action_update_stats(a, bytes, packets, drops, hw); |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 296 | tm->lastuse = max_t(u64, tm->lastuse, lastuse); |
| 297 | } |
| 298 | |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 299 | static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 300 | int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 302 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 303 | struct tcf_police *police = to_police(a); |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 304 | struct tcf_police_params *p; |
Jeff Mahoney | 0f04cfd | 2010-08-31 13:21:42 +0000 | [diff] [blame] | 305 | struct tc_police opt = { |
| 306 | .index = police->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 307 | .refcnt = refcount_read(&police->tcf_refcnt) - ref, |
| 308 | .bindcnt = atomic_read(&police->tcf_bindcnt) - bind, |
Jeff Mahoney | 0f04cfd | 2010-08-31 13:21:42 +0000 | [diff] [blame] | 309 | }; |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 310 | struct tcf_t t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 312 | spin_lock_bh(&police->tcf_lock); |
| 313 | opt.action = police->tcf_action; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 314 | p = rcu_dereference_protected(police->params, |
| 315 | lockdep_is_held(&police->tcf_lock)); |
| 316 | opt.mtu = p->tcfp_mtu; |
| 317 | opt.burst = PSCHED_NS2TICKS(p->tcfp_burst); |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 318 | if (p->rate_present) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 319 | psched_ratecfg_getrate(&opt.rate, &p->rate); |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 320 | if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) && |
| 321 | nla_put_u64_64bit(skb, TCA_POLICE_RATE64, |
| 322 | police->params->rate.rate_bytes_ps, |
| 323 | TCA_POLICE_PAD)) |
| 324 | goto nla_put_failure; |
| 325 | } |
| 326 | if (p->peak_present) { |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 327 | psched_ratecfg_getrate(&opt.peakrate, &p->peak); |
David Dai | d1967e4 | 2019-09-04 10:03:43 -0500 | [diff] [blame] | 328 | if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) && |
| 329 | nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64, |
| 330 | police->params->peak.rate_bytes_ps, |
| 331 | TCA_POLICE_PAD)) |
| 332 | goto nla_put_failure; |
| 333 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 334 | if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) |
| 335 | goto nla_put_failure; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 336 | if (p->tcfp_result && |
| 337 | nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 338 | goto nla_put_failure; |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 339 | if (p->tcfp_ewma_rate && |
| 340 | nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 341 | goto nla_put_failure; |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 342 | |
Davide Caratti | 985fd98 | 2019-10-19 18:49:32 +0200 | [diff] [blame] | 343 | tcf_tm_dump(&t, &police->tcf_tm); |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 344 | if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) |
| 345 | goto nla_put_failure; |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 346 | spin_unlock_bh(&police->tcf_lock); |
Jamal Hadi Salim | 3d3ed18 | 2016-05-23 21:07:20 -0400 | [diff] [blame] | 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return skb->len; |
| 349 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 350 | nla_put_failure: |
Vlad Buslov | e329bc4 | 2018-08-10 20:51:55 +0300 | [diff] [blame] | 351 | spin_unlock_bh(&police->tcf_lock); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 352 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return -1; |
| 354 | } |
| 355 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 356 | static int tcf_police_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 357 | { |
| 358 | struct tc_action_net *tn = net_generic(net, police_net_id); |
| 359 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 360 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | MODULE_AUTHOR("Alexey Kuznetsov"); |
| 364 | MODULE_DESCRIPTION("Policing actions"); |
| 365 | MODULE_LICENSE("GPL"); |
| 366 | |
| 367 | static struct tc_action_ops act_police_ops = { |
| 368 | .kind = "police", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 369 | .id = TCA_ID_POLICE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | .owner = THIS_MODULE, |
Pieter Jansen van Vuuren | 12f02b6 | 2019-05-04 04:46:24 -0700 | [diff] [blame] | 371 | .stats_update = tcf_police_stats_update, |
Jamal Hadi Salim | 2ac06347 | 2018-08-12 09:34:56 -0400 | [diff] [blame] | 372 | .act = tcf_police_act, |
| 373 | .dump = tcf_police_dump, |
| 374 | .init = tcf_police_init, |
| 375 | .walk = tcf_police_walker, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 376 | .lookup = tcf_police_search, |
Davide Caratti | 2d550db | 2018-09-13 19:29:13 +0200 | [diff] [blame] | 377 | .cleanup = tcf_police_cleanup, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 378 | .size = sizeof(struct tcf_police), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | static __net_init int police_init_net(struct net *net) |
| 382 | { |
| 383 | struct tc_action_net *tn = net_generic(net, police_net_id); |
| 384 | |
Cong Wang | 981471b | 2019-08-25 10:01:32 -0700 | [diff] [blame] | 385 | return tc_action_net_init(net, tn, &act_police_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 388 | static void __net_exit police_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 389 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 390 | tc_action_net_exit(net_list, police_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static struct pernet_operations police_net_ops = { |
| 394 | .init = police_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 395 | .exit_batch = police_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 396 | .id = &police_net_id, |
| 397 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 400 | static int __init police_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 402 | return tcf_register_action(&act_police_ops, &police_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 405 | static void __exit police_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 407 | tcf_unregister_action(&act_police_ops, &police_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | module_init(police_init_module); |
| 411 | module_exit(police_cleanup_module); |