linux/net/sched/act_gact.c
<<
>>
Prefs
   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
  27static unsigned int gact_net_id;
  28static struct tc_action_ops act_gact_ops;
  29
  30#ifdef CONFIG_GACT_PROB
  31static 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
  39static 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
  49typedef int (*g_rand)(struct tcf_gact *gact);
  50static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
  51#endif /* CONFIG_GACT_PROB */
  52
  53static 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
  58static 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, u32 flags,
  62                         struct netlink_ext_ack *extack)
  63{
  64        struct tc_action_net *tn = net_generic(net, gact_net_id);
  65        struct nlattr *tb[TCA_GACT_MAX + 1];
  66        struct tcf_chain *goto_ch = NULL;
  67        struct tc_gact *parm;
  68        struct tcf_gact *gact;
  69        int ret = 0;
  70        u32 index;
  71        int err;
  72#ifdef CONFIG_GACT_PROB
  73        struct tc_gact_p *p_parm = NULL;
  74#endif
  75
  76        if (nla == NULL)
  77                return -EINVAL;
  78
  79        err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
  80                                          NULL);
  81        if (err < 0)
  82                return err;
  83
  84        if (tb[TCA_GACT_PARMS] == NULL)
  85                return -EINVAL;
  86        parm = nla_data(tb[TCA_GACT_PARMS]);
  87        index = parm->index;
  88
  89#ifndef CONFIG_GACT_PROB
  90        if (tb[TCA_GACT_PROB] != NULL)
  91                return -EOPNOTSUPP;
  92#else
  93        if (tb[TCA_GACT_PROB]) {
  94                p_parm = nla_data(tb[TCA_GACT_PROB]);
  95                if (p_parm->ptype >= MAX_RAND)
  96                        return -EINVAL;
  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 0;
 117                if (!ovr) {
 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;
 149release_idr:
 150        tcf_idr_release(*a, bind);
 151        return err;
 152}
 153
 154static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
 155                        struct tcf_result *res)
 156{
 157        struct tcf_gact *gact = to_gact(a);
 158        int action = READ_ONCE(gact->tcf_action);
 159
 160#ifdef CONFIG_GACT_PROB
 161        {
 162        u32 ptype = READ_ONCE(gact->tcfg_ptype);
 163
 164        if (ptype)
 165                action = gact_rand[ptype](gact);
 166        }
 167#endif
 168        tcf_action_update_bstats(&gact->common, skb);
 169        if (action == TC_ACT_SHOT)
 170                tcf_action_inc_drop_qstats(&gact->common);
 171
 172        tcf_lastuse_update(&gact->tcf_tm);
 173
 174        return action;
 175}
 176
 177static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
 178                                  u64 drops, u64 lastuse, bool hw)
 179{
 180        struct tcf_gact *gact = to_gact(a);
 181        int action = READ_ONCE(gact->tcf_action);
 182        struct tcf_t *tm = &gact->tcf_tm;
 183
 184        tcf_action_update_stats(a, bytes, packets,
 185                                action == TC_ACT_SHOT ? packets : drops, hw);
 186        tm->lastuse = max_t(u64, tm->lastuse, lastuse);
 187}
 188
 189static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
 190                         int bind, int ref)
 191{
 192        unsigned char *b = skb_tail_pointer(skb);
 193        struct tcf_gact *gact = to_gact(a);
 194        struct tc_gact opt = {
 195                .index   = gact->tcf_index,
 196                .refcnt  = refcount_read(&gact->tcf_refcnt) - ref,
 197                .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
 198        };
 199        struct tcf_t t;
 200
 201        spin_lock_bh(&gact->tcf_lock);
 202        opt.action = gact->tcf_action;
 203        if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
 204                goto nla_put_failure;
 205#ifdef CONFIG_GACT_PROB
 206        if (gact->tcfg_ptype) {
 207                struct tc_gact_p p_opt = {
 208                        .paction = gact->tcfg_paction,
 209                        .pval    = gact->tcfg_pval,
 210                        .ptype   = gact->tcfg_ptype,
 211                };
 212
 213                if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
 214                        goto nla_put_failure;
 215        }
 216#endif
 217        tcf_tm_dump(&t, &gact->tcf_tm);
 218        if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
 219                goto nla_put_failure;
 220        spin_unlock_bh(&gact->tcf_lock);
 221
 222        return skb->len;
 223
 224nla_put_failure:
 225        spin_unlock_bh(&gact->tcf_lock);
 226        nlmsg_trim(skb, b);
 227        return -1;
 228}
 229
 230static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
 231                           struct netlink_callback *cb, int type,
 232                           const struct tc_action_ops *ops,
 233                           struct netlink_ext_ack *extack)
 234{
 235        struct tc_action_net *tn = net_generic(net, gact_net_id);
 236
 237        return tcf_generic_walker(tn, skb, cb, type, ops, extack);
 238}
 239
 240static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
 241{
 242        struct tc_action_net *tn = net_generic(net, gact_net_id);
 243
 244        return tcf_idr_search(tn, a, index);
 245}
 246
 247static size_t tcf_gact_get_fill_size(const struct tc_action *act)
 248{
 249        size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
 250
 251#ifdef CONFIG_GACT_PROB
 252        if (to_gact(act)->tcfg_ptype)
 253                /* TCA_GACT_PROB */
 254                sz += nla_total_size(sizeof(struct tc_gact_p));
 255#endif
 256
 257        return sz;
 258}
 259
 260static struct tc_action_ops act_gact_ops = {
 261        .kind           =       "gact",
 262        .id             =       TCA_ID_GACT,
 263        .owner          =       THIS_MODULE,
 264        .act            =       tcf_gact_act,
 265        .stats_update   =       tcf_gact_stats_update,
 266        .dump           =       tcf_gact_dump,
 267        .init           =       tcf_gact_init,
 268        .walk           =       tcf_gact_walker,
 269        .lookup         =       tcf_gact_search,
 270        .get_fill_size  =       tcf_gact_get_fill_size,
 271        .size           =       sizeof(struct tcf_gact),
 272};
 273
 274static __net_init int gact_init_net(struct net *net)
 275{
 276        struct tc_action_net *tn = net_generic(net, gact_net_id);
 277
 278        return tc_action_net_init(net, tn, &act_gact_ops);
 279}
 280
 281static void __net_exit gact_exit_net(struct list_head *net_list)
 282{
 283        tc_action_net_exit(net_list, gact_net_id);
 284}
 285
 286static struct pernet_operations gact_net_ops = {
 287        .init = gact_init_net,
 288        .exit_batch = gact_exit_net,
 289        .id   = &gact_net_id,
 290        .size = sizeof(struct tc_action_net),
 291};
 292
 293MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
 294MODULE_DESCRIPTION("Generic Classifier actions");
 295MODULE_LICENSE("GPL");
 296
 297static int __init gact_init_module(void)
 298{
 299#ifdef CONFIG_GACT_PROB
 300        pr_info("GACT probability on\n");
 301#else
 302        pr_info("GACT probability NOT on\n");
 303#endif
 304
 305        return tcf_register_action(&act_gact_ops, &gact_net_ops);
 306}
 307
 308static void __exit gact_cleanup_module(void)
 309{
 310        tcf_unregister_action(&act_gact_ops, &gact_net_ops);
 311}
 312
 313module_init(gact_init_module);
 314module_exit(gact_cleanup_module);
 315