simple.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 **pskb, struct tc_action *a)
  41. {
  42. struct sk_buff *skb = *pskb;
  43. struct tcf_defact *p = PRIV(a, defact);
  44. spin_lock(&p->lock);
  45. p->tm.lastuse = jiffies;
  46. p->bstats.bytes += skb->len;
  47. p->bstats.packets++;
  48. /* print policy string followed by _ then packet count
  49. * Example if this was the 3rd packet and the string was "hello"
  50. * then it would look like "hello_3" (without quotes)
  51. **/
  52. printk("simple: %s_%d\n", (char *)p->defdata, p->bstats.packets);
  53. spin_unlock(&p->lock);
  54. return p->action;
  55. }
  56. static struct tc_action_ops act_simp_ops = {
  57. .kind = "simple",
  58. .type = TCA_ACT_SIMP,
  59. .capab = TCA_CAP_NONE,
  60. .owner = THIS_MODULE,
  61. .act = tcf_simp,
  62. tca_use_default_ops
  63. };
  64. MODULE_AUTHOR("Jamal Hadi Salim(2005)");
  65. MODULE_DESCRIPTION("Simple example action");
  66. MODULE_LICENSE("GPL");
  67. static int __init simp_init_module(void)
  68. {
  69. int ret = tcf_register_action(&act_simp_ops);
  70. if (!ret)
  71. printk("Simple TC action Loaded\n");
  72. return ret;
  73. }
  74. static void __exit simp_cleanup_module(void)
  75. {
  76. tcf_unregister_action(&act_simp_ops);
  77. }
  78. module_init(simp_init_module);
  79. module_exit(simp_cleanup_module);