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