act_simple.c 4.6 KB

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