act_gact.c 5.3 KB

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