act_api.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_ACT_API_H
  3. #define __NET_ACT_API_H
  4. /*
  5. * Public action API for classifiers/qdiscs
  6. */
  7. #include <linux/refcount.h>
  8. #include <net/sch_generic.h>
  9. #include <net/pkt_sched.h>
  10. #include <net/net_namespace.h>
  11. #include <net/netns/generic.h>
  12. struct tcf_idrinfo {
  13. spinlock_t lock;
  14. struct idr action_idr;
  15. struct net *net;
  16. };
  17. struct tc_action_ops;
  18. struct tc_action {
  19. const struct tc_action_ops *ops;
  20. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  21. __u32 order;
  22. struct tcf_idrinfo *idrinfo;
  23. u32 tcfa_index;
  24. refcount_t tcfa_refcnt;
  25. atomic_t tcfa_bindcnt;
  26. int tcfa_action;
  27. struct tcf_t tcfa_tm;
  28. struct gnet_stats_basic_packed tcfa_bstats;
  29. struct gnet_stats_queue tcfa_qstats;
  30. struct net_rate_estimator __rcu *tcfa_rate_est;
  31. spinlock_t tcfa_lock;
  32. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  33. struct gnet_stats_queue __percpu *cpu_qstats;
  34. struct tc_cookie __rcu *act_cookie;
  35. struct tcf_chain *goto_chain;
  36. };
  37. #define tcf_index common.tcfa_index
  38. #define tcf_refcnt common.tcfa_refcnt
  39. #define tcf_bindcnt common.tcfa_bindcnt
  40. #define tcf_action common.tcfa_action
  41. #define tcf_tm common.tcfa_tm
  42. #define tcf_bstats common.tcfa_bstats
  43. #define tcf_qstats common.tcfa_qstats
  44. #define tcf_rate_est common.tcfa_rate_est
  45. #define tcf_lock common.tcfa_lock
  46. /* Update lastuse only if needed, to avoid dirtying a cache line.
  47. * We use a temp variable to avoid fetching jiffies twice.
  48. */
  49. static inline void tcf_lastuse_update(struct tcf_t *tm)
  50. {
  51. unsigned long now = jiffies;
  52. if (tm->lastuse != now)
  53. tm->lastuse = now;
  54. if (unlikely(!tm->firstuse))
  55. tm->firstuse = now;
  56. }
  57. static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
  58. {
  59. dtm->install = jiffies_to_clock_t(jiffies - stm->install);
  60. dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
  61. dtm->firstuse = stm->firstuse ?
  62. jiffies_to_clock_t(jiffies - stm->firstuse) : 0;
  63. dtm->expires = jiffies_to_clock_t(stm->expires);
  64. }
  65. #ifdef CONFIG_NET_CLS_ACT
  66. #define ACT_P_CREATED 1
  67. #define ACT_P_DELETED 1
  68. struct tc_action_ops {
  69. struct list_head head;
  70. char kind[IFNAMSIZ];
  71. __u32 type; /* TBD to match kind */
  72. size_t size;
  73. struct module *owner;
  74. int (*act)(struct sk_buff *, const struct tc_action *,
  75. struct tcf_result *); /* called under RCU BH lock*/
  76. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  77. void (*cleanup)(struct tc_action *);
  78. int (*lookup)(struct net *net, struct tc_action **a, u32 index,
  79. struct netlink_ext_ack *extack);
  80. int (*init)(struct net *net, struct nlattr *nla,
  81. struct nlattr *est, struct tc_action **act, int ovr,
  82. int bind, bool rtnl_held,
  83. struct netlink_ext_ack *extack);
  84. int (*walk)(struct net *, struct sk_buff *,
  85. struct netlink_callback *, int,
  86. const struct tc_action_ops *,
  87. struct netlink_ext_ack *);
  88. void (*stats_update)(struct tc_action *, u64, u32, u64);
  89. size_t (*get_fill_size)(const struct tc_action *act);
  90. struct net_device *(*get_dev)(const struct tc_action *a);
  91. void (*put_dev)(struct net_device *dev);
  92. };
  93. struct tc_action_net {
  94. struct tcf_idrinfo *idrinfo;
  95. const struct tc_action_ops *ops;
  96. };
  97. static inline
  98. int tc_action_net_init(struct net *net, struct tc_action_net *tn,
  99. const struct tc_action_ops *ops)
  100. {
  101. int err = 0;
  102. tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
  103. if (!tn->idrinfo)
  104. return -ENOMEM;
  105. tn->ops = ops;
  106. tn->idrinfo->net = net;
  107. spin_lock_init(&tn->idrinfo->lock);
  108. idr_init(&tn->idrinfo->action_idr);
  109. return err;
  110. }
  111. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  112. struct tcf_idrinfo *idrinfo);
  113. static inline void tc_action_net_exit(struct list_head *net_list,
  114. unsigned int id)
  115. {
  116. struct net *net;
  117. rtnl_lock();
  118. list_for_each_entry(net, net_list, exit_list) {
  119. struct tc_action_net *tn = net_generic(net, id);
  120. tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
  121. kfree(tn->idrinfo);
  122. }
  123. rtnl_unlock();
  124. }
  125. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  126. struct netlink_callback *cb, int type,
  127. const struct tc_action_ops *ops,
  128. struct netlink_ext_ack *extack);
  129. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
  130. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  131. struct tc_action **a, const struct tc_action_ops *ops,
  132. int bind, bool cpustats);
  133. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
  134. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
  135. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  136. struct tc_action **a, int bind);
  137. int __tcf_idr_release(struct tc_action *a, bool bind, bool strict);
  138. static inline int tcf_idr_release(struct tc_action *a, bool bind)
  139. {
  140. return __tcf_idr_release(a, bind, false);
  141. }
  142. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  143. int tcf_unregister_action(struct tc_action_ops *a,
  144. struct pernet_operations *ops);
  145. int tcf_action_destroy(struct tc_action *actions[], int bind);
  146. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  147. int nr_actions, struct tcf_result *res);
  148. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  149. struct nlattr *est, char *name, int ovr, int bind,
  150. struct tc_action *actions[], size_t *attr_size,
  151. bool rtnl_held, struct netlink_ext_ack *extack);
  152. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  153. struct nlattr *nla, struct nlattr *est,
  154. char *name, int ovr, int bind,
  155. bool rtnl_held,
  156. struct netlink_ext_ack *extack);
  157. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind,
  158. int ref);
  159. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  160. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  161. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  162. #endif /* CONFIG_NET_CLS_ACT */
  163. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  164. u64 packets, u64 lastuse)
  165. {
  166. #ifdef CONFIG_NET_CLS_ACT
  167. if (!a->ops->stats_update)
  168. return;
  169. a->ops->stats_update(a, bytes, packets, lastuse);
  170. #endif
  171. }
  172. #ifdef CONFIG_NET_CLS_ACT
  173. int tc_setup_cb_egdev_register(const struct net_device *dev,
  174. tc_setup_cb_t *cb, void *cb_priv);
  175. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  176. tc_setup_cb_t *cb, void *cb_priv);
  177. int tc_setup_cb_egdev_call(const struct net_device *dev,
  178. enum tc_setup_type type, void *type_data,
  179. bool err_stop);
  180. #else
  181. static inline
  182. int tc_setup_cb_egdev_register(const struct net_device *dev,
  183. tc_setup_cb_t *cb, void *cb_priv)
  184. {
  185. return 0;
  186. }
  187. static inline
  188. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  189. tc_setup_cb_t *cb, void *cb_priv)
  190. {
  191. }
  192. static inline
  193. int tc_setup_cb_egdev_call(const struct net_device *dev,
  194. enum tc_setup_type type, void *type_data,
  195. bool err_stop)
  196. {
  197. return 0;
  198. }
  199. #endif
  200. #endif