act_api.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. void (*stats_update)(struct tc_action *, u64, u32, u64);
  97. };
  98. struct tc_action_net {
  99. struct tcf_hashinfo *hinfo;
  100. const struct tc_action_ops *ops;
  101. };
  102. static inline
  103. int tc_action_net_init(struct tc_action_net *tn, const struct tc_action_ops *ops,
  104. unsigned int mask)
  105. {
  106. int err = 0;
  107. tn->hinfo = kmalloc(sizeof(*tn->hinfo), GFP_KERNEL);
  108. if (!tn->hinfo)
  109. return -ENOMEM;
  110. tn->ops = ops;
  111. err = tcf_hashinfo_init(tn->hinfo, mask);
  112. if (err)
  113. kfree(tn->hinfo);
  114. return err;
  115. }
  116. void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
  117. struct tcf_hashinfo *hinfo);
  118. static inline void tc_action_net_exit(struct tc_action_net *tn)
  119. {
  120. tcf_hashinfo_destroy(tn->ops, tn->hinfo);
  121. kfree(tn->hinfo);
  122. }
  123. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  124. struct netlink_callback *cb, int type,
  125. struct tc_action *a);
  126. int tcf_hash_search(struct tc_action_net *tn, struct tc_action *a, u32 index);
  127. u32 tcf_hash_new_index(struct tc_action_net *tn);
  128. int tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action *a,
  129. int bind);
  130. int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  131. struct tc_action *a, int size, int bind, bool cpustats);
  132. void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est);
  133. void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a);
  134. int __tcf_hash_release(struct tc_action *a, bool bind, bool strict);
  135. static inline int tcf_hash_release(struct tc_action *a, bool bind)
  136. {
  137. return __tcf_hash_release(a, bind, false);
  138. }
  139. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  140. int tcf_unregister_action(struct tc_action_ops *a, struct pernet_operations *ops);
  141. int tcf_action_destroy(struct list_head *actions, int bind);
  142. int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
  143. struct tcf_result *res);
  144. int tcf_action_init(struct net *net, struct nlattr *nla,
  145. struct nlattr *est, char *n, int ovr,
  146. int bind, struct list_head *);
  147. struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
  148. struct nlattr *est, char *n, int ovr,
  149. int bind);
  150. int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
  151. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  152. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  153. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  154. #define tc_no_actions(_exts) \
  155. (list_empty(&(_exts)->actions))
  156. #define tc_for_each_action(_a, _exts) \
  157. list_for_each_entry(a, &(_exts)->actions, list)
  158. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  159. u64 packets, u64 lastuse)
  160. {
  161. if (!a->ops->stats_update)
  162. return;
  163. a->ops->stats_update(a, bytes, packets, lastuse);
  164. }
  165. #else /* CONFIG_NET_CLS_ACT */
  166. #define tc_no_actions(_exts) true
  167. #define tc_for_each_action(_a, _exts) while (0)
  168. #define tcf_action_stats_update(a, bytes, packets, lastuse)
  169. #endif /* CONFIG_NET_CLS_ACT */
  170. #endif