Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/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 <asm/uaccess.h> |
| 14 | #include <asm/system.h> |
| 15 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/socket.h> |
| 21 | #include <linux/sockios.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/rtnetlink.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/proc_fs.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 31 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/sock.h> |
| 33 | #include <net/pkt_sched.h> |
| 34 | #include <linux/tc_act/tc_gact.h> |
| 35 | #include <net/tc_act/tc_gact.h> |
| 36 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 37 | #define GACT_TAB_MASK 15 |
| 38 | static struct tcf_common *tcf_gact_ht[GACT_TAB_MASK + 1]; |
| 39 | static u32 gact_idx_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static DEFINE_RWLOCK(gact_lock); |
| 41 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 42 | static struct tcf_hashinfo gact_hash_info = { |
| 43 | .htab = tcf_gact_ht, |
| 44 | .hmask = GACT_TAB_MASK, |
| 45 | .lock = &gact_lock, |
| 46 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #ifdef CONFIG_GACT_PROB |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 49 | static int gact_net_rand(struct tcf_gact *gact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Kim Nordlund | a163148 | 2006-12-01 20:21:44 -0800 | [diff] [blame] | 51 | if (!gact->tcfg_pval || net_random() % gact->tcfg_pval) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 52 | return gact->tcf_action; |
| 53 | return gact->tcfg_paction; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 56 | static int gact_determ(struct tcf_gact *gact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Kim Nordlund | a163148 | 2006-12-01 20:21:44 -0800 | [diff] [blame] | 58 | if (!gact->tcfg_pval || gact->tcf_bstats.packets % gact->tcfg_pval) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 59 | return gact->tcf_action; |
| 60 | return gact->tcfg_paction; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 63 | typedef int (*g_rand)(struct tcf_gact *gact); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static g_rand gact_rand[MAX_RAND]= { NULL, gact_net_rand, gact_determ }; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 65 | #endif /* CONFIG_GACT_PROB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | static int tcf_gact_init(struct rtattr *rta, struct rtattr *est, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 68 | struct tc_action *a, int ovr, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | struct rtattr *tb[TCA_GACT_MAX]; |
| 71 | struct tc_gact *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 72 | struct tcf_gact *gact; |
| 73 | struct tcf_common *pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int ret = 0; |
| 75 | |
| 76 | if (rta == NULL || rtattr_parse_nested(tb, TCA_GACT_MAX, rta) < 0) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | if (tb[TCA_GACT_PARMS - 1] == NULL || |
| 80 | RTA_PAYLOAD(tb[TCA_GACT_PARMS - 1]) < sizeof(*parm)) |
| 81 | return -EINVAL; |
| 82 | parm = RTA_DATA(tb[TCA_GACT_PARMS - 1]); |
| 83 | |
| 84 | if (tb[TCA_GACT_PROB-1] != NULL) |
| 85 | #ifdef CONFIG_GACT_PROB |
| 86 | if (RTA_PAYLOAD(tb[TCA_GACT_PROB-1]) < sizeof(struct tc_gact_p)) |
| 87 | return -EINVAL; |
| 88 | #else |
| 89 | return -EOPNOTSUPP; |
| 90 | #endif |
| 91 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 92 | pc = tcf_hash_check(parm->index, a, bind, &gact_hash_info); |
| 93 | if (!pc) { |
| 94 | pc = tcf_hash_create(parm->index, est, a, sizeof(*gact), |
| 95 | bind, &gact_idx_gen, &gact_hash_info); |
| 96 | if (unlikely(!pc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return -ENOMEM; |
| 98 | ret = ACT_P_CREATED; |
| 99 | } else { |
| 100 | if (!ovr) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 101 | tcf_hash_release(pc, bind, &gact_hash_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return -EEXIST; |
| 103 | } |
| 104 | } |
| 105 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 106 | gact = to_gact(pc); |
| 107 | |
| 108 | spin_lock_bh(&gact->tcf_lock); |
| 109 | gact->tcf_action = parm->action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #ifdef CONFIG_GACT_PROB |
| 111 | if (tb[TCA_GACT_PROB-1] != NULL) { |
| 112 | struct tc_gact_p *p_parm = RTA_DATA(tb[TCA_GACT_PROB-1]); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 113 | gact->tcfg_paction = p_parm->paction; |
| 114 | gact->tcfg_pval = p_parm->pval; |
| 115 | gact->tcfg_ptype = p_parm->ptype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 118 | spin_unlock_bh(&gact->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (ret == ACT_P_CREATED) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 120 | tcf_hash_insert(pc, &gact_hash_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return ret; |
| 122 | } |
| 123 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 124 | static int tcf_gact_cleanup(struct tc_action *a, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 126 | struct tcf_gact *gact = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 128 | if (gact) |
| 129 | return tcf_hash_release(&gact->common, bind, &gact_hash_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 133 | static int tcf_gact(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 135 | struct tcf_gact *gact = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int action = TC_ACT_SHOT; |
| 137 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 138 | spin_lock(&gact->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | #ifdef CONFIG_GACT_PROB |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 140 | if (gact->tcfg_ptype && gact_rand[gact->tcfg_ptype] != NULL) |
| 141 | action = gact_rand[gact->tcfg_ptype](gact); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | else |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 143 | action = gact->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #else |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 145 | action = gact->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 147 | gact->tcf_bstats.bytes += skb->len; |
| 148 | gact->tcf_bstats.packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (action == TC_ACT_SHOT) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 150 | gact->tcf_qstats.drops++; |
| 151 | gact->tcf_tm.lastuse = jiffies; |
| 152 | spin_unlock(&gact->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | return action; |
| 155 | } |
| 156 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 157 | static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 159 | unsigned char *b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | struct tc_gact opt; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 161 | struct tcf_gact *gact = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | struct tcf_t t; |
| 163 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 164 | opt.index = gact->tcf_index; |
| 165 | opt.refcnt = gact->tcf_refcnt - ref; |
| 166 | opt.bindcnt = gact->tcf_bindcnt - bind; |
| 167 | opt.action = gact->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | RTA_PUT(skb, TCA_GACT_PARMS, sizeof(opt), &opt); |
| 169 | #ifdef CONFIG_GACT_PROB |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 170 | if (gact->tcfg_ptype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | struct tc_gact_p p_opt; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 172 | p_opt.paction = gact->tcfg_paction; |
| 173 | p_opt.pval = gact->tcfg_pval; |
| 174 | p_opt.ptype = gact->tcfg_ptype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | RTA_PUT(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt); |
| 176 | } |
| 177 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 178 | t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install); |
| 179 | t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse); |
| 180 | t.expires = jiffies_to_clock_t(gact->tcf_tm.expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | RTA_PUT(skb, TCA_GACT_TM, sizeof(t), &t); |
| 182 | return skb->len; |
| 183 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 184 | rtattr_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 185 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | static struct tc_action_ops act_gact_ops = { |
| 190 | .kind = "gact", |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 191 | .hinfo = &gact_hash_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .type = TCA_ACT_GACT, |
| 193 | .capab = TCA_CAP_NONE, |
| 194 | .owner = THIS_MODULE, |
| 195 | .act = tcf_gact, |
| 196 | .dump = tcf_gact_dump, |
| 197 | .cleanup = tcf_gact_cleanup, |
| 198 | .lookup = tcf_hash_search, |
| 199 | .init = tcf_gact_init, |
| 200 | .walk = tcf_generic_walker |
| 201 | }; |
| 202 | |
| 203 | MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); |
| 204 | MODULE_DESCRIPTION("Generic Classifier actions"); |
| 205 | MODULE_LICENSE("GPL"); |
| 206 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 207 | static int __init gact_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | #ifdef CONFIG_GACT_PROB |
| 210 | printk("GACT probability on\n"); |
| 211 | #else |
| 212 | printk("GACT probability NOT on\n"); |
| 213 | #endif |
| 214 | return tcf_register_action(&act_gact_ops); |
| 215 | } |
| 216 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 217 | static void __exit gact_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | tcf_unregister_action(&act_gact_ops); |
| 220 | } |
| 221 | |
| 222 | module_init(gact_init_module); |
| 223 | module_exit(gact_cleanup_module); |