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