act_simple.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * net/sched/simp.c Simple example of an action
  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. * Authors: Jamal Hadi Salim (2005)
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/netlink.h>
  18. #include <net/pkt_sched.h>
  19. #define TCA_ACT_SIMP 22
  20. #include <linux/tc_act/tc_defact.h>
  21. #include <net/tc_act/tc_defact.h>
  22. #define SIMP_TAB_MASK 7
  23. static struct tcf_common *tcf_simp_ht[SIMP_TAB_MASK + 1];
  24. static u32 simp_idx_gen;
  25. static DEFINE_RWLOCK(simp_lock);
  26. static struct tcf_hashinfo simp_hash_info = {
  27. .htab = tcf_simp_ht,
  28. .hmask = SIMP_TAB_MASK,
  29. .lock = &simp_lock,
  30. };
  31. static int tcf_simp(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  32. {
  33. struct tcf_defact *d = a->priv;
  34. spin_lock(&d->tcf_lock);
  35. d->tcf_tm.lastuse = jiffies;
  36. d->tcf_bstats.bytes += skb->len;
  37. d->tcf_bstats.packets++;
  38. /* print policy string followed by _ then packet count
  39. * Example if this was the 3rd packet and the string was "hello"
  40. * then it would look like "hello_3" (without quotes)
  41. **/
  42. printk("simple: %s_%d\n",
  43. (char *)d->tcfd_defdata, d->tcf_bstats.packets);
  44. spin_unlock(&d->tcf_lock);
  45. return d->tcf_action;
  46. }
  47. static int tcf_simp_release(struct tcf_defact *d, int bind)
  48. {
  49. int ret = 0;
  50. if (d) {
  51. if (bind)
  52. d->tcf_bindcnt--;
  53. d->tcf_refcnt--;
  54. if (d->tcf_bindcnt <= 0 && d->tcf_refcnt <= 0) {
  55. kfree(d->tcfd_defdata);
  56. tcf_hash_destroy(&d->common, &simp_hash_info);
  57. ret = 1;
  58. }
  59. }
  60. return ret;
  61. }
  62. static int alloc_defdata(struct tcf_defact *d, u32 datalen, void *defdata)
  63. {
  64. d->tcfd_defdata = kmemdup(defdata, datalen, GFP_KERNEL);
  65. if (unlikely(!d->tcfd_defdata))
  66. return -ENOMEM;
  67. d->tcfd_datalen = datalen;
  68. return 0;
  69. }
  70. static int realloc_defdata(struct tcf_defact *d, u32 datalen, void *defdata)
  71. {
  72. kfree(d->tcfd_defdata);
  73. return alloc_defdata(d, datalen, defdata);
  74. }
  75. static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
  76. [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) },
  77. };
  78. static int tcf_simp_init(struct nlattr *nla, struct nlattr *est,
  79. struct tc_action *a, int ovr, int bind)
  80. {
  81. struct nlattr *tb[TCA_DEF_MAX + 1];
  82. struct tc_defact *parm;
  83. struct tcf_defact *d;
  84. struct tcf_common *pc;
  85. void *defdata;
  86. u32 datalen = 0;
  87. int ret = 0, err;
  88. if (nla == NULL)
  89. return -EINVAL;
  90. err = nla_parse_nested(tb, TCA_DEF_MAX, nla, NULL);
  91. if (err < 0)
  92. return err;
  93. if (tb[TCA_DEF_PARMS] == NULL)
  94. return -EINVAL;
  95. parm = nla_data(tb[TCA_DEF_PARMS]);
  96. defdata = nla_data(tb[TCA_DEF_DATA]);
  97. if (defdata == NULL)
  98. return -EINVAL;
  99. datalen = nla_len(tb[TCA_DEF_DATA]);
  100. if (datalen == 0)
  101. return -EINVAL;
  102. pc = tcf_hash_check(parm->index, a, bind, &simp_hash_info);
  103. if (!pc) {
  104. pc = tcf_hash_create(parm->index, est, a, sizeof(*d), bind,
  105. &simp_idx_gen, &simp_hash_info);
  106. if (unlikely(!pc))
  107. return -ENOMEM;
  108. d = to_defact(pc);
  109. ret = alloc_defdata(d, datalen, defdata);
  110. if (ret < 0) {
  111. kfree(pc);
  112. return ret;
  113. }
  114. ret = ACT_P_CREATED;
  115. } else {
  116. d = to_defact(pc);
  117. if (!ovr) {
  118. tcf_simp_release(d, bind);
  119. return -EEXIST;
  120. }
  121. realloc_defdata(d, datalen, defdata);
  122. }
  123. spin_lock_bh(&d->tcf_lock);
  124. d->tcf_action = parm->action;
  125. spin_unlock_bh(&d->tcf_lock);
  126. if (ret == ACT_P_CREATED)
  127. tcf_hash_insert(pc, &simp_hash_info);
  128. return ret;
  129. }
  130. static inline int tcf_simp_cleanup(struct tc_action *a, int bind)
  131. {
  132. struct tcf_defact *d = a->priv;
  133. if (d)
  134. return tcf_simp_release(d, bind);
  135. return 0;
  136. }
  137. static inline int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a,
  138. int bind, int ref)
  139. {
  140. unsigned char *b = skb_tail_pointer(skb);
  141. struct tcf_defact *d = a->priv;
  142. struct tc_defact opt;
  143. struct tcf_t t;
  144. opt.index = d->tcf_index;
  145. opt.refcnt = d->tcf_refcnt - ref;
  146. opt.bindcnt = d->tcf_bindcnt - bind;
  147. opt.action = d->tcf_action;
  148. NLA_PUT(skb, TCA_DEF_PARMS, sizeof(opt), &opt);
  149. NLA_PUT(skb, TCA_DEF_DATA, d->tcfd_datalen, d->tcfd_defdata);
  150. t.install = jiffies_to_clock_t(jiffies - d->tcf_tm.install);
  151. t.lastuse = jiffies_to_clock_t(jiffies - d->tcf_tm.lastuse);
  152. t.expires = jiffies_to_clock_t(d->tcf_tm.expires);
  153. NLA_PUT(skb, TCA_DEF_TM, sizeof(t), &t);
  154. return skb->len;
  155. nla_put_failure:
  156. nlmsg_trim(skb, b);
  157. return -1;
  158. }
  159. static struct tc_action_ops act_simp_ops = {
  160. .kind = "simple",
  161. .hinfo = &simp_hash_info,
  162. .type = TCA_ACT_SIMP,
  163. .capab = TCA_CAP_NONE,
  164. .owner = THIS_MODULE,
  165. .act = tcf_simp,
  166. .dump = tcf_simp_dump,
  167. .cleanup = tcf_simp_cleanup,
  168. .init = tcf_simp_init,
  169. .walk = tcf_generic_walker,
  170. };
  171. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  172. MODULE_DESCRIPTION("Simple example action");
  173. MODULE_LICENSE("GPL");
  174. static int __init simp_init_module(void)
  175. {
  176. int ret = tcf_register_action(&act_simp_ops);
  177. if (!ret)
  178. printk("Simple TC action Loaded\n");
  179. return ret;
  180. }
  181. static void __exit simp_cleanup_module(void)
  182. {
  183. tcf_unregister_action(&act_simp_ops);
  184. }
  185. module_init(simp_init_module);
  186. module_exit(simp_cleanup_module);