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