act_simple.c 5.1 KB

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