act_api.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef __NET_ACT_API_H
  2. #define __NET_ACT_API_H
  3. /*
  4. * Public 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_hashinfo {
  11. struct hlist_head *htab;
  12. unsigned int hmask;
  13. spinlock_t lock;
  14. u32 index;
  15. };
  16. struct tc_action_ops;
  17. struct tc_action {
  18. const struct tc_action_ops *ops;
  19. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  20. __u32 order;
  21. struct list_head list;
  22. struct tcf_hashinfo *hinfo;
  23. struct hlist_node tcfa_head;
  24. u32 tcfa_index;
  25. int tcfa_refcnt;
  26. int tcfa_bindcnt;
  27. u32 tcfa_capab;
  28. int tcfa_action;
  29. struct tcf_t tcfa_tm;
  30. struct gnet_stats_basic_packed tcfa_bstats;
  31. struct gnet_stats_queue tcfa_qstats;
  32. struct gnet_stats_rate_est64 tcfa_rate_est;
  33. spinlock_t tcfa_lock;
  34. struct rcu_head tcfa_rcu;
  35. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  36. struct gnet_stats_queue __percpu *cpu_qstats;
  37. };
  38. #define tcf_act common.tcfa_act
  39. #define tcf_head common.tcfa_head
  40. #define tcf_index common.tcfa_index
  41. #define tcf_refcnt common.tcfa_refcnt
  42. #define tcf_bindcnt common.tcfa_bindcnt
  43. #define tcf_capab common.tcfa_capab
  44. #define tcf_action common.tcfa_action
  45. #define tcf_tm common.tcfa_tm
  46. #define tcf_bstats common.tcfa_bstats
  47. #define tcf_qstats common.tcfa_qstats
  48. #define tcf_rate_est common.tcfa_rate_est
  49. #define tcf_lock common.tcfa_lock
  50. #define tcf_rcu common.tcfa_rcu
  51. static inline unsigned int tcf_hash(u32 index, unsigned int hmask)
  52. {
  53. return index & hmask;
  54. }
  55. static inline int tcf_hashinfo_init(struct tcf_hashinfo *hf, unsigned int mask)
  56. {
  57. int i;
  58. spin_lock_init(&hf->lock);
  59. hf->index = 0;
  60. hf->hmask = mask;
  61. hf->htab = kzalloc((mask + 1) * sizeof(struct hlist_head),
  62. GFP_KERNEL);
  63. if (!hf->htab)
  64. return -ENOMEM;
  65. for (i = 0; i < mask + 1; i++)
  66. INIT_HLIST_HEAD(&hf->htab[i]);
  67. return 0;
  68. }
  69. /* Update lastuse only if needed, to avoid dirtying a cache line.
  70. * We use a temp variable to avoid fetching jiffies twice.
  71. */
  72. static inline void tcf_lastuse_update(struct tcf_t *tm)
  73. {
  74. unsigned long now = jiffies;
  75. if (tm->lastuse != now)
  76. tm->lastuse = now;
  77. if (unlikely(!tm->firstuse))
  78. tm->firstuse = now;
  79. }
  80. static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
  81. {
  82. dtm->install = jiffies_to_clock_t(jiffies - stm->install);
  83. dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
  84. dtm->firstuse = jiffies_to_clock_t(jiffies - stm->firstuse);
  85. dtm->expires = jiffies_to_clock_t(stm->expires);
  86. }
  87. #ifdef CONFIG_NET_CLS_ACT
  88. #define ACT_P_CREATED 1
  89. #define ACT_P_DELETED 1
  90. struct tc_action_ops {
  91. struct list_head head;
  92. char kind[IFNAMSIZ];
  93. __u32 type; /* TBD to match kind */
  94. size_t size;
  95. struct module *owner;
  96. int (*act)(struct sk_buff *, const struct tc_action *,
  97. struct tcf_result *);
  98. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  99. void (*cleanup)(struct tc_action *, int bind);
  100. int (*lookup)(struct net *, struct tc_action **, u32);
  101. int (*init)(struct net *net, struct nlattr *nla,
  102. struct nlattr *est, struct tc_action **act, int ovr,
  103. int bind);
  104. int (*walk)(struct net *, struct sk_buff *,
  105. struct netlink_callback *, int, const struct tc_action_ops *);
  106. void (*stats_update)(struct tc_action *, u64, u32, u64);
  107. };
  108. struct tc_action_net {
  109. struct tcf_hashinfo *hinfo;
  110. const struct tc_action_ops *ops;
  111. };
  112. static inline
  113. int tc_action_net_init(struct tc_action_net *tn,
  114. const struct tc_action_ops *ops, unsigned int mask)
  115. {
  116. int err = 0;
  117. tn->hinfo = kmalloc(sizeof(*tn->hinfo), GFP_KERNEL);
  118. if (!tn->hinfo)
  119. return -ENOMEM;
  120. tn->ops = ops;
  121. err = tcf_hashinfo_init(tn->hinfo, mask);
  122. if (err)
  123. kfree(tn->hinfo);
  124. return err;
  125. }
  126. void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
  127. struct tcf_hashinfo *hinfo);
  128. static inline void tc_action_net_exit(struct tc_action_net *tn)
  129. {
  130. tcf_hashinfo_destroy(tn->ops, tn->hinfo);
  131. kfree(tn->hinfo);
  132. }
  133. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  134. struct netlink_callback *cb, int type,
  135. const struct tc_action_ops *ops);
  136. int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
  137. u32 tcf_hash_new_index(struct tc_action_net *tn);
  138. bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  139. int bind);
  140. int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  141. struct tc_action **a, const struct tc_action_ops *ops, int bind,
  142. bool cpustats);
  143. void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est);
  144. void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a);
  145. int __tcf_hash_release(struct tc_action *a, bool bind, bool strict);
  146. static inline int tcf_hash_release(struct tc_action *a, bool bind)
  147. {
  148. return __tcf_hash_release(a, bind, false);
  149. }
  150. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  151. int tcf_unregister_action(struct tc_action_ops *a,
  152. struct pernet_operations *ops);
  153. int tcf_action_destroy(struct list_head *actions, int bind);
  154. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  155. int nr_actions, struct tcf_result *res);
  156. int tcf_action_init(struct net *net, struct nlattr *nla,
  157. struct nlattr *est, char *n, int ovr,
  158. int bind, struct list_head *);
  159. struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
  160. struct nlattr *est, char *n, int ovr,
  161. int bind);
  162. int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
  163. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  164. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  165. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  166. #endif /* CONFIG_NET_CLS_ACT */
  167. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  168. u64 packets, u64 lastuse)
  169. {
  170. #ifdef CONFIG_NET_CLS_ACT
  171. if (!a->ops->stats_update)
  172. return;
  173. a->ops->stats_update(a, bytes, packets, lastuse);
  174. #endif
  175. }
  176. #endif