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