1 /* 2 * net/sched/act_gact.c Generic actions 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 * copyright Jamal Hadi Salim (2002-4) 10 * 11 */ 12 13 #include <linux/types.h> 14 #include <linux/kernel.h> 15 #include <linux/string.h> 16 #include <linux/errno.h> 17 #include <linux/skbuff.h> 18 #include <linux/rtnetlink.h> 19 #include <linux/module.h> 20 #include <linux/init.h> 21 #include <net/netlink.h> 22 #include <net/pkt_sched.h> 23 #include <net/pkt_cls.h> 24 #include <linux/tc_act/tc_gact.h> 25 #include <net/tc_act/tc_gact.h> 26 27 static unsigned int gact_net_id; 28 static struct tc_action_ops act_gact_ops; 29 30 #ifdef CONFIG_GACT_PROB 31 static int gact_net_rand(struct tcf_gact *gact) 32 { 33 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ 34 if (prandom_u32() % gact->tcfg_pval) 35 return gact->tcf_action; 36 return gact->tcfg_paction; 37 } 38 39 static int gact_determ(struct tcf_gact *gact) 40 { 41 u32 pack = atomic_inc_return(&gact->packets); 42 43 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ 44 if (pack % gact->tcfg_pval) 45 return gact->tcf_action; 46 return gact->tcfg_paction; 47 } 48 49 typedef int (*g_rand)(struct tcf_gact *gact); 50 static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ }; 51 #endif /* CONFIG_GACT_PROB */ 52 53 static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = { 54 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) }, 55 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) }, 56 }; 57 58 static int tcf_gact_init(struct net *net, struct nlattr *nla, 59 struct nlattr *est, struct tc_action **a, 60 int ovr, int bind, bool rtnl_held, 61 struct tcf_proto *tp, struct netlink_ext_ack *extack) 62 { 63 struct tc_action_net *tn = net_generic(net, gact_net_id); 64 struct nlattr *tb[TCA_GACT_MAX + 1]; 65 struct tcf_chain *goto_ch = NULL; 66 struct tc_gact *parm; 67 struct tcf_gact *gact; 68 int ret = 0; 69 int err; 70 #ifdef CONFIG_GACT_PROB 71 struct tc_gact_p *p_parm = NULL; 72 #endif 73 74 if (nla == NULL) 75 return -EINVAL; 76 77 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy, NULL); 78 if (err < 0) 79 return err; 80 81 if (tb[TCA_GACT_PARMS] == NULL) 82 return -EINVAL; 83 parm = nla_data(tb[TCA_GACT_PARMS]); 84 85 #ifndef CONFIG_GACT_PROB 86 if (tb[TCA_GACT_PROB] != NULL) 87 return -EOPNOTSUPP; 88 #else 89 if (tb[TCA_GACT_PROB]) { 90 p_parm = nla_data(tb[TCA_GACT_PROB]); 91 if (p_parm->ptype >= MAX_RAND) 92 return -EINVAL; 93 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) { 94 NL_SET_ERR_MSG(extack, 95 "goto chain not allowed on fallback"); 96 return -EINVAL; 97 } 98 } 99 #endif 100 101 err = tcf_idr_check_alloc(tn, &parm->index, a, bind); 102 if (!err) { 103 ret = tcf_idr_create(tn, parm->index, est, a, 104 &act_gact_ops, bind, true); 105 if (ret) { 106 tcf_idr_cleanup(tn, parm->index); 107 return ret; 108 } 109 ret = ACT_P_CREATED; 110 } else if (err > 0) { 111 if (bind)/* dont override defaults */ 112 return 0; 113 if (!ovr) { 114 tcf_idr_release(*a, bind); 115 return -EEXIST; 116 } 117 } else { 118 return err; 119 } 120 121 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 122 if (err < 0) 123 goto release_idr; 124 gact = to_gact(*a); 125 126 spin_lock_bh(&gact->tcf_lock); 127 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 128 #ifdef CONFIG_GACT_PROB 129 if (p_parm) { 130 gact->tcfg_paction = p_parm->paction; 131 gact->tcfg_pval = max_t(u16, 1, p_parm->pval); 132 /* Make sure tcfg_pval is written before tcfg_ptype 133 * coupled with smp_rmb() in gact_net_rand() & gact_determ() 134 */ 135 smp_wmb(); 136 gact->tcfg_ptype = p_parm->ptype; 137 } 138 #endif 139 spin_unlock_bh(&gact->tcf_lock); 140 141 if (goto_ch) 142 tcf_chain_put_by_act(goto_ch); 143 144 if (ret == ACT_P_CREATED) 145 tcf_idr_insert(tn, *a); 146 return ret; 147 release_idr: 148 tcf_idr_release(*a, bind); 149 return err; 150 } 151 152 static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a, 153 struct tcf_result *res) 154 { 155 struct tcf_gact *gact = to_gact(a); 156 int action = READ_ONCE(gact->tcf_action); 157 158 #ifdef CONFIG_GACT_PROB 159 { 160 u32 ptype = READ_ONCE(gact->tcfg_ptype); 161 162 if (ptype) 163 action = gact_rand[ptype](gact); 164 } 165 #endif 166 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb); 167 if (action == TC_ACT_SHOT) 168 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats)); 169 170 tcf_lastuse_update(&gact->tcf_tm); 171 172 return action; 173 } 174 175 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets, 176 u64 lastuse, bool hw) 177 { 178 struct tcf_gact *gact = to_gact(a); 179 int action = READ_ONCE(gact->tcf_action); 180 struct tcf_t *tm = &gact->tcf_tm; 181 182 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes, 183 packets); 184 if (action == TC_ACT_SHOT) 185 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets; 186 187 if (hw) 188 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats_hw), 189 bytes, packets); 190 191 tm->lastuse = max_t(u64, tm->lastuse, lastuse); 192 } 193 194 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, 195 int bind, int ref) 196 { 197 unsigned char *b = skb_tail_pointer(skb); 198 struct tcf_gact *gact = to_gact(a); 199 struct tc_gact opt = { 200 .index = gact->tcf_index, 201 .refcnt = refcount_read(&gact->tcf_refcnt) - ref, 202 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind, 203 }; 204 struct tcf_t t; 205 206 spin_lock_bh(&gact->tcf_lock); 207 opt.action = gact->tcf_action; 208 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt)) 209 goto nla_put_failure; 210 #ifdef CONFIG_GACT_PROB 211 if (gact->tcfg_ptype) { 212 struct tc_gact_p p_opt = { 213 .paction = gact->tcfg_paction, 214 .pval = gact->tcfg_pval, 215 .ptype = gact->tcfg_ptype, 216 }; 217 218 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt)) 219 goto nla_put_failure; 220 } 221 #endif 222 tcf_tm_dump(&t, &gact->tcf_tm); 223 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD)) 224 goto nla_put_failure; 225 spin_unlock_bh(&gact->tcf_lock); 226 227 return skb->len; 228 229 nla_put_failure: 230 spin_unlock_bh(&gact->tcf_lock); 231 nlmsg_trim(skb, b); 232 return -1; 233 } 234 235 static int tcf_gact_walker(struct net *net, struct sk_buff *skb, 236 struct netlink_callback *cb, int type, 237 const struct tc_action_ops *ops, 238 struct netlink_ext_ack *extack) 239 { 240 struct tc_action_net *tn = net_generic(net, gact_net_id); 241 242 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 243 } 244 245 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index) 246 { 247 struct tc_action_net *tn = net_generic(net, gact_net_id); 248 249 return tcf_idr_search(tn, a, index); 250 } 251 252 static size_t tcf_gact_get_fill_size(const struct tc_action *act) 253 { 254 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */ 255 256 #ifdef CONFIG_GACT_PROB 257 if (to_gact(act)->tcfg_ptype) 258 /* TCA_GACT_PROB */ 259 sz += nla_total_size(sizeof(struct tc_gact_p)); 260 #endif 261 262 return sz; 263 } 264 265 static struct tc_action_ops act_gact_ops = { 266 .kind = "gact", 267 .id = TCA_ID_GACT, 268 .owner = THIS_MODULE, 269 .act = tcf_gact_act, 270 .stats_update = tcf_gact_stats_update, 271 .dump = tcf_gact_dump, 272 .init = tcf_gact_init, 273 .walk = tcf_gact_walker, 274 .lookup = tcf_gact_search, 275 .get_fill_size = tcf_gact_get_fill_size, 276 .size = sizeof(struct tcf_gact), 277 }; 278 279 static __net_init int gact_init_net(struct net *net) 280 { 281 struct tc_action_net *tn = net_generic(net, gact_net_id); 282 283 return tc_action_net_init(tn, &act_gact_ops); 284 } 285 286 static void __net_exit gact_exit_net(struct list_head *net_list) 287 { 288 tc_action_net_exit(net_list, gact_net_id); 289 } 290 291 static struct pernet_operations gact_net_ops = { 292 .init = gact_init_net, 293 .exit_batch = gact_exit_net, 294 .id = &gact_net_id, 295 .size = sizeof(struct tc_action_net), 296 }; 297 298 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); 299 MODULE_DESCRIPTION("Generic Classifier actions"); 300 MODULE_LICENSE("GPL"); 301 302 static int __init gact_init_module(void) 303 { 304 #ifdef CONFIG_GACT_PROB 305 pr_info("GACT probability on\n"); 306 #else 307 pr_info("GACT probability NOT on\n"); 308 #endif 309 310 return tcf_register_action(&act_gact_ops, &gact_net_ops); 311 } 312 313 static void __exit gact_cleanup_module(void) 314 { 315 tcf_unregister_action(&act_gact_ops, &gact_net_ops); 316 } 317 318 module_init(gact_init_module); 319 module_exit(gact_cleanup_module); 320