act_api.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef __NET_ACT_API_H
  2. #define __NET_ACT_API_H
  3. /*
  4. * Public police action API for classifiers/qdiscs
  5. */
  6. #include <net/sch_generic.h>
  7. #include <net/pkt_sched.h>
  8. #include <net/net_namespace.h>
  9. #include <net/netns/generic.h>
  10. struct tcf_common {
  11. struct hlist_node tcfc_head;
  12. u32 tcfc_index;
  13. int tcfc_refcnt;
  14. int tcfc_bindcnt;
  15. u32 tcfc_capab;
  16. int tcfc_action;
  17. struct tcf_t tcfc_tm;
  18. struct gnet_stats_basic_packed tcfc_bstats;
  19. struct gnet_stats_queue tcfc_qstats;
  20. struct gnet_stats_rate_est64 tcfc_rate_est;
  21. spinlock_t tcfc_lock;
  22. struct rcu_head tcfc_rcu;
  23. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  24. struct gnet_stats_queue __percpu *cpu_qstats;
  25. };
  26. #define tcf_head common.tcfc_head
  27. #define tcf_index common.tcfc_index
  28. #define tcf_refcnt common.tcfc_refcnt
  29. #define tcf_bindcnt common.tcfc_bindcnt
  30. #define tcf_capab common.tcfc_capab
  31. #define tcf_action common.tcfc_action
  32. #define tcf_tm common.tcfc_tm
  33. #define tcf_bstats common.tcfc_bstats
  34. #define tcf_qstats common.tcfc_qstats
  35. #define tcf_rate_est common.tcfc_rate_est
  36. #define tcf_lock common.tcfc_lock
  37. #define tcf_rcu common.tcfc_rcu
  38. struct tcf_hashinfo {
  39. struct hlist_head *htab;
  40. unsigned int hmask;
  41. spinlock_t lock;
  42. u32 index;
  43. };
  44. static inline unsigned int tcf_hash(u32 index, unsigned int hmask)
  45. {
  46. return index & hmask;
  47. }
  48. static inline int tcf_hashinfo_init(struct tcf_hashinfo *hf, unsigned int mask)
  49. {
  50. int i;
  51. spin_lock_init(&hf->lock);
  52. hf->index = 0;
  53. hf->hmask = mask;
  54. hf->htab = kzalloc((mask + 1) * sizeof(struct hlist_head),
  55. GFP_KERNEL);
  56. if (!hf->htab)
  57. return -ENOMEM;
  58. for (i = 0; i < mask + 1; i++)
  59. INIT_HLIST_HEAD(&hf->htab[i]);
  60. return 0;
  61. }
  62. /* Update lastuse only if needed, to avoid dirtying a cache line.
  63. * We use a temp variable to avoid fetching jiffies twice.
  64. */
  65. static inline void tcf_lastuse_update(struct tcf_t *tm)
  66. {
  67. unsigned long now = jiffies;
  68. if (tm->lastuse != now)
  69. tm->lastuse = now;
  70. }
  71. struct tc_action {
  72. void *priv;
  73. const struct tc_action_ops *ops;
  74. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  75. __u32 order;
  76. struct list_head list;
  77. struct tcf_hashinfo *hinfo;
  78. };
  79. #ifdef CONFIG_NET_CLS_ACT
  80. #define ACT_P_CREATED 1
  81. #define ACT_P_DELETED 1
  82. struct tc_action_ops {
  83. struct list_head head;
  84. char kind[IFNAMSIZ];
  85. __u32 type; /* TBD to match kind */
  86. struct module *owner;
  87. int (*act)(struct sk_buff *, const struct tc_action *, struct tcf_result *);
  88. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  89. void (*cleanup)(struct tc_action *, int bind);
  90. int (*lookup)(struct net *, struct tc_action *, u32);
  91. int (*init)(struct net *net, struct nlattr *nla,
  92. struct nlattr *est, struct tc_action *act, int ovr,
  93. int bind);
  94. int (*walk)(struct net *, struct sk_buff *,
  95. struct netlink_callback *, int, struct tc_action *);
  96. };
  97. struct tc_action_net {
  98. struct tcf_hashinfo *hinfo;
  99. const struct tc_action_ops *ops;
  100. };
  101. static inline
  102. int tc_action_net_init(struct tc_action_net *tn, const struct tc_action_ops *ops,
  103. unsigned int mask)
  104. {
  105. int err = 0;
  106. tn->hinfo = kmalloc(sizeof(*tn->hinfo), GFP_KERNEL);
  107. if (!tn->hinfo)
  108. return -ENOMEM;
  109. tn->ops = ops;
  110. err = tcf_hashinfo_init(tn->hinfo, mask);
  111. if (err)
  112. kfree(tn->hinfo);
  113. return err;
  114. }
  115. void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
  116. struct tcf_hashinfo *hinfo);
  117. static inline void tc_action_net_exit(struct tc_action_net *tn)
  118. {
  119. tcf_hashinfo_destroy(tn->ops, tn->hinfo);
  120. }
  121. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  122. struct netlink_callback *cb, int type,
  123. struct tc_action *a);
  124. int tcf_hash_search(struct tc_action_net *tn, struct tc_action *a, u32 index);
  125. u32 tcf_hash_new_index(struct tc_action_net *tn);
  126. int tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action *a,
  127. int bind);
  128. int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  129. struct tc_action *a, int size, int bind, bool cpustats);
  130. void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est);
  131. void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a);
  132. int __tcf_hash_release(struct tc_action *a, bool bind, bool strict);
  133. static inline int tcf_hash_release(struct tc_action *a, bool bind)
  134. {
  135. return __tcf_hash_release(a, bind, false);
  136. }
  137. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  138. int tcf_unregister_action(struct tc_action_ops *a, struct pernet_operations *ops);
  139. int tcf_action_destroy(struct list_head *actions, int bind);
  140. int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
  141. struct tcf_result *res);
  142. int tcf_action_init(struct net *net, struct nlattr *nla,
  143. struct nlattr *est, char *n, int ovr,
  144. int bind, struct list_head *);
  145. struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
  146. struct nlattr *est, char *n, int ovr,
  147. int bind);
  148. int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
  149. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  150. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  151. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  152. #define tc_no_actions(_exts) \
  153. (list_empty(&(_exts)->actions))
  154. #define tc_for_each_action(_a, _exts) \
  155. list_for_each_entry(a, &(_exts)->actions, list)
  156. #else /* CONFIG_NET_CLS_ACT */
  157. #define tc_no_actions(_exts) true
  158. #define tc_for_each_action(_a, _exts) while (0)
  159. #endif /* CONFIG_NET_CLS_ACT */
  160. #endif