act_simple.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/netdevice.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/pkt_sched.h>
  19. #define TCA_ACT_SIMP 22
  20. /* XXX: Hide all these common elements under some macro
  21. * probably
  22. */
  23. #include <linux/tc_act/tc_defact.h>
  24. #include <net/tc_act/tc_defact.h>
  25. /* use generic hash table with 8 buckets */
  26. #define MY_TAB_SIZE 8
  27. #define MY_TAB_MASK (MY_TAB_SIZE - 1)
  28. static u32 idx_gen;
  29. static struct tcf_defact *tcf_simp_ht[MY_TAB_SIZE];
  30. static DEFINE_RWLOCK(simp_lock);
  31. /* override the defaults */
  32. #define tcf_st tcf_defact
  33. #define tc_st tc_defact
  34. #define tcf_t_lock simp_lock
  35. #define tcf_ht tcf_simp_ht
  36. #define CONFIG_NET_ACT_INIT 1
  37. #include <net/pkt_act.h>
  38. #include <net/act_generic.h>
  39. static int tcf_simp(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  40. {
  41. struct tcf_defact *p = PRIV(a, defact);
  42. spin_lock(&p->lock);
  43. p->tm.lastuse = jiffies;
  44. p->bstats.bytes += skb->len;
  45. p->bstats.packets++;
  46. /* print policy string followed by _ then packet count
  47. * Example if this was the 3rd packet and the string was "hello"
  48. * then it would look like "hello_3" (without quotes)
  49. **/
  50. printk("simple: %s_%d\n", (char *)p->defdata, p->bstats.packets);
  51. spin_unlock(&p->lock);
  52. return p->action;
  53. }
  54. static struct tc_action_ops act_simp_ops = {
  55. .kind = "simple",
  56. .type = TCA_ACT_SIMP,
  57. .capab = TCA_CAP_NONE,
  58. .owner = THIS_MODULE,
  59. .act = tcf_simp,
  60. tca_use_default_ops
  61. };
  62. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  63. MODULE_DESCRIPTION("Simple example action");
  64. MODULE_LICENSE("GPL");
  65. static int __init simp_init_module(void)
  66. {
  67. int ret = tcf_register_action(&act_simp_ops);
  68. if (!ret)
  69. printk("Simple TC action Loaded\n");
  70. return ret;
  71. }
  72. static void __exit simp_cleanup_module(void)
  73. {
  74. tcf_unregister_action(&act_simp_ops);
  75. }
  76. module_init(simp_init_module);
  77. module_exit(simp_cleanup_module);