act_simple.c 5.3 KB

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