act_simple.c 5.2 KB

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