act_api.h 6.3 KB

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