0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/types.h>
0009 #include <linux/kernel.h>
0010 #include <linux/string.h>
0011 #include <linux/errno.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/rtnetlink.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <net/netlink.h>
0017 #include <net/pkt_sched.h>
0018 #include <net/pkt_cls.h>
0019 #include <linux/tc_act/tc_gact.h>
0020 #include <net/tc_act/tc_gact.h>
0021
0022 static unsigned int gact_net_id;
0023 static struct tc_action_ops act_gact_ops;
0024
0025 #ifdef CONFIG_GACT_PROB
0026 static int gact_net_rand(struct tcf_gact *gact)
0027 {
0028 smp_rmb();
0029 if (prandom_u32() % gact->tcfg_pval)
0030 return gact->tcf_action;
0031 return gact->tcfg_paction;
0032 }
0033
0034 static int gact_determ(struct tcf_gact *gact)
0035 {
0036 u32 pack = atomic_inc_return(&gact->packets);
0037
0038 smp_rmb();
0039 if (pack % gact->tcfg_pval)
0040 return gact->tcf_action;
0041 return gact->tcfg_paction;
0042 }
0043
0044 typedef int (*g_rand)(struct tcf_gact *gact);
0045 static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
0046 #endif
0047
0048 static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
0049 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
0050 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
0051 };
0052
0053 static int tcf_gact_init(struct net *net, struct nlattr *nla,
0054 struct nlattr *est, struct tc_action **a,
0055 struct tcf_proto *tp, u32 flags,
0056 struct netlink_ext_ack *extack)
0057 {
0058 struct tc_action_net *tn = net_generic(net, gact_net_id);
0059 bool bind = flags & TCA_ACT_FLAGS_BIND;
0060 struct nlattr *tb[TCA_GACT_MAX + 1];
0061 struct tcf_chain *goto_ch = NULL;
0062 struct tc_gact *parm;
0063 struct tcf_gact *gact;
0064 int ret = 0;
0065 u32 index;
0066 int err;
0067 #ifdef CONFIG_GACT_PROB
0068 struct tc_gact_p *p_parm = NULL;
0069 #endif
0070
0071 if (nla == NULL)
0072 return -EINVAL;
0073
0074 err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
0075 NULL);
0076 if (err < 0)
0077 return err;
0078
0079 if (tb[TCA_GACT_PARMS] == NULL)
0080 return -EINVAL;
0081 parm = nla_data(tb[TCA_GACT_PARMS]);
0082 index = parm->index;
0083
0084 #ifndef CONFIG_GACT_PROB
0085 if (tb[TCA_GACT_PROB] != NULL)
0086 return -EOPNOTSUPP;
0087 #else
0088 if (tb[TCA_GACT_PROB]) {
0089 p_parm = nla_data(tb[TCA_GACT_PROB]);
0090 if (p_parm->ptype >= MAX_RAND)
0091 return -EINVAL;
0092 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
0093 NL_SET_ERR_MSG(extack,
0094 "goto chain not allowed on fallback");
0095 return -EINVAL;
0096 }
0097 }
0098 #endif
0099
0100 err = tcf_idr_check_alloc(tn, &index, a, bind);
0101 if (!err) {
0102 ret = tcf_idr_create_from_flags(tn, index, est, a,
0103 &act_gact_ops, bind, flags);
0104 if (ret) {
0105 tcf_idr_cleanup(tn, index);
0106 return ret;
0107 }
0108 ret = ACT_P_CREATED;
0109 } else if (err > 0) {
0110 if (bind)
0111 return 0;
0112 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
0113 tcf_idr_release(*a, bind);
0114 return -EEXIST;
0115 }
0116 } else {
0117 return err;
0118 }
0119
0120 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
0121 if (err < 0)
0122 goto release_idr;
0123 gact = to_gact(*a);
0124
0125 spin_lock_bh(&gact->tcf_lock);
0126 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
0127 #ifdef CONFIG_GACT_PROB
0128 if (p_parm) {
0129 gact->tcfg_paction = p_parm->paction;
0130 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
0131
0132
0133
0134 smp_wmb();
0135 gact->tcfg_ptype = p_parm->ptype;
0136 }
0137 #endif
0138 spin_unlock_bh(&gact->tcf_lock);
0139
0140 if (goto_ch)
0141 tcf_chain_put_by_act(goto_ch);
0142
0143 return ret;
0144 release_idr:
0145 tcf_idr_release(*a, bind);
0146 return err;
0147 }
0148
0149 static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
0150 struct tcf_result *res)
0151 {
0152 struct tcf_gact *gact = to_gact(a);
0153 int action = READ_ONCE(gact->tcf_action);
0154
0155 #ifdef CONFIG_GACT_PROB
0156 {
0157 u32 ptype = READ_ONCE(gact->tcfg_ptype);
0158
0159 if (ptype)
0160 action = gact_rand[ptype](gact);
0161 }
0162 #endif
0163 tcf_action_update_bstats(&gact->common, skb);
0164 if (action == TC_ACT_SHOT)
0165 tcf_action_inc_drop_qstats(&gact->common);
0166
0167 tcf_lastuse_update(&gact->tcf_tm);
0168
0169 return action;
0170 }
0171
0172 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
0173 u64 drops, u64 lastuse, bool hw)
0174 {
0175 struct tcf_gact *gact = to_gact(a);
0176 int action = READ_ONCE(gact->tcf_action);
0177 struct tcf_t *tm = &gact->tcf_tm;
0178
0179 tcf_action_update_stats(a, bytes, packets,
0180 action == TC_ACT_SHOT ? packets : drops, hw);
0181 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
0182 }
0183
0184 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
0185 int bind, int ref)
0186 {
0187 unsigned char *b = skb_tail_pointer(skb);
0188 struct tcf_gact *gact = to_gact(a);
0189 struct tc_gact opt = {
0190 .index = gact->tcf_index,
0191 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
0192 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
0193 };
0194 struct tcf_t t;
0195
0196 spin_lock_bh(&gact->tcf_lock);
0197 opt.action = gact->tcf_action;
0198 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
0199 goto nla_put_failure;
0200 #ifdef CONFIG_GACT_PROB
0201 if (gact->tcfg_ptype) {
0202 struct tc_gact_p p_opt = {
0203 .paction = gact->tcfg_paction,
0204 .pval = gact->tcfg_pval,
0205 .ptype = gact->tcfg_ptype,
0206 };
0207
0208 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
0209 goto nla_put_failure;
0210 }
0211 #endif
0212 tcf_tm_dump(&t, &gact->tcf_tm);
0213 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
0214 goto nla_put_failure;
0215 spin_unlock_bh(&gact->tcf_lock);
0216
0217 return skb->len;
0218
0219 nla_put_failure:
0220 spin_unlock_bh(&gact->tcf_lock);
0221 nlmsg_trim(skb, b);
0222 return -1;
0223 }
0224
0225 static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
0226 struct netlink_callback *cb, int type,
0227 const struct tc_action_ops *ops,
0228 struct netlink_ext_ack *extack)
0229 {
0230 struct tc_action_net *tn = net_generic(net, gact_net_id);
0231
0232 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
0233 }
0234
0235 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
0236 {
0237 struct tc_action_net *tn = net_generic(net, gact_net_id);
0238
0239 return tcf_idr_search(tn, a, index);
0240 }
0241
0242 static size_t tcf_gact_get_fill_size(const struct tc_action *act)
0243 {
0244 size_t sz = nla_total_size(sizeof(struct tc_gact));
0245
0246 #ifdef CONFIG_GACT_PROB
0247 if (to_gact(act)->tcfg_ptype)
0248
0249 sz += nla_total_size(sizeof(struct tc_gact_p));
0250 #endif
0251
0252 return sz;
0253 }
0254
0255 static int tcf_gact_offload_act_setup(struct tc_action *act, void *entry_data,
0256 u32 *index_inc, bool bind,
0257 struct netlink_ext_ack *extack)
0258 {
0259 if (bind) {
0260 struct flow_action_entry *entry = entry_data;
0261
0262 if (is_tcf_gact_ok(act)) {
0263 entry->id = FLOW_ACTION_ACCEPT;
0264 } else if (is_tcf_gact_shot(act)) {
0265 entry->id = FLOW_ACTION_DROP;
0266 } else if (is_tcf_gact_trap(act)) {
0267 entry->id = FLOW_ACTION_TRAP;
0268 } else if (is_tcf_gact_goto_chain(act)) {
0269 entry->id = FLOW_ACTION_GOTO;
0270 entry->chain_index = tcf_gact_goto_chain_index(act);
0271 } else if (is_tcf_gact_continue(act)) {
0272 NL_SET_ERR_MSG_MOD(extack, "Offload of \"continue\" action is not supported");
0273 return -EOPNOTSUPP;
0274 } else if (is_tcf_gact_reclassify(act)) {
0275 NL_SET_ERR_MSG_MOD(extack, "Offload of \"reclassify\" action is not supported");
0276 return -EOPNOTSUPP;
0277 } else if (is_tcf_gact_pipe(act)) {
0278 NL_SET_ERR_MSG_MOD(extack, "Offload of \"pipe\" action is not supported");
0279 return -EOPNOTSUPP;
0280 } else {
0281 NL_SET_ERR_MSG_MOD(extack, "Unsupported generic action offload");
0282 return -EOPNOTSUPP;
0283 }
0284 *index_inc = 1;
0285 } else {
0286 struct flow_offload_action *fl_action = entry_data;
0287
0288 if (is_tcf_gact_ok(act))
0289 fl_action->id = FLOW_ACTION_ACCEPT;
0290 else if (is_tcf_gact_shot(act))
0291 fl_action->id = FLOW_ACTION_DROP;
0292 else if (is_tcf_gact_trap(act))
0293 fl_action->id = FLOW_ACTION_TRAP;
0294 else if (is_tcf_gact_goto_chain(act))
0295 fl_action->id = FLOW_ACTION_GOTO;
0296 else
0297 return -EOPNOTSUPP;
0298 }
0299
0300 return 0;
0301 }
0302
0303 static struct tc_action_ops act_gact_ops = {
0304 .kind = "gact",
0305 .id = TCA_ID_GACT,
0306 .owner = THIS_MODULE,
0307 .act = tcf_gact_act,
0308 .stats_update = tcf_gact_stats_update,
0309 .dump = tcf_gact_dump,
0310 .init = tcf_gact_init,
0311 .walk = tcf_gact_walker,
0312 .lookup = tcf_gact_search,
0313 .get_fill_size = tcf_gact_get_fill_size,
0314 .offload_act_setup = tcf_gact_offload_act_setup,
0315 .size = sizeof(struct tcf_gact),
0316 };
0317
0318 static __net_init int gact_init_net(struct net *net)
0319 {
0320 struct tc_action_net *tn = net_generic(net, gact_net_id);
0321
0322 return tc_action_net_init(net, tn, &act_gact_ops);
0323 }
0324
0325 static void __net_exit gact_exit_net(struct list_head *net_list)
0326 {
0327 tc_action_net_exit(net_list, gact_net_id);
0328 }
0329
0330 static struct pernet_operations gact_net_ops = {
0331 .init = gact_init_net,
0332 .exit_batch = gact_exit_net,
0333 .id = &gact_net_id,
0334 .size = sizeof(struct tc_action_net),
0335 };
0336
0337 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
0338 MODULE_DESCRIPTION("Generic Classifier actions");
0339 MODULE_LICENSE("GPL");
0340
0341 static int __init gact_init_module(void)
0342 {
0343 #ifdef CONFIG_GACT_PROB
0344 pr_info("GACT probability on\n");
0345 #else
0346 pr_info("GACT probability NOT on\n");
0347 #endif
0348
0349 return tcf_register_action(&act_gact_ops, &gact_net_ops);
0350 }
0351
0352 static void __exit gact_cleanup_module(void)
0353 {
0354 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
0355 }
0356
0357 module_init(gact_init_module);
0358 module_exit(gact_cleanup_module);