act_simple.c 2.3 KB

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