act_api.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/flow_offload.h>
  9. #include <net/sch_generic.h>
  10. #include <net/pkt_sched.h>
  11. #include <net/net_namespace.h>
  12. #include <net/netns/generic.h>
  13. struct tcf_idrinfo {
  14. struct mutex lock;
  15. struct idr action_idr;
  16. struct net *net;
  17. };
  18. struct tc_action_ops;
  19. struct tc_action {
  20. const struct tc_action_ops *ops;
  21. __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
  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_sync tcfa_bstats;
  29. struct gnet_stats_basic_sync tcfa_bstats_hw;
  30. struct gnet_stats_queue tcfa_qstats;
  31. struct net_rate_estimator __rcu *tcfa_rate_est;
  32. spinlock_t tcfa_lock;
  33. struct gnet_stats_basic_sync __percpu *cpu_bstats;
  34. struct gnet_stats_basic_sync __percpu *cpu_bstats_hw;
  35. struct gnet_stats_queue __percpu *cpu_qstats;
  36. struct tc_cookie __rcu *act_cookie;
  37. struct tcf_chain __rcu *goto_chain;
  38. u32 tcfa_flags;
  39. u8 hw_stats;
  40. u8 used_hw_stats;
  41. bool used_hw_stats_valid;
  42. u32 in_hw_count;
  43. };
  44. #define tcf_index common.tcfa_index
  45. #define tcf_refcnt common.tcfa_refcnt
  46. #define tcf_bindcnt common.tcfa_bindcnt
  47. #define tcf_action common.tcfa_action
  48. #define tcf_tm common.tcfa_tm
  49. #define tcf_bstats common.tcfa_bstats
  50. #define tcf_qstats common.tcfa_qstats
  51. #define tcf_rate_est common.tcfa_rate_est
  52. #define tcf_lock common.tcfa_lock
  53. #define TCA_ACT_HW_STATS_ANY (TCA_ACT_HW_STATS_IMMEDIATE | \
  54. TCA_ACT_HW_STATS_DELAYED)
  55. /* Reserve 16 bits for user-space. See TCA_ACT_FLAGS_NO_PERCPU_STATS. */
  56. #define TCA_ACT_FLAGS_USER_BITS 16
  57. #define TCA_ACT_FLAGS_USER_MASK 0xffff
  58. #define TCA_ACT_FLAGS_POLICE (1U << TCA_ACT_FLAGS_USER_BITS)
  59. #define TCA_ACT_FLAGS_BIND (1U << (TCA_ACT_FLAGS_USER_BITS + 1))
  60. #define TCA_ACT_FLAGS_REPLACE (1U << (TCA_ACT_FLAGS_USER_BITS + 2))
  61. #define TCA_ACT_FLAGS_NO_RTNL (1U << (TCA_ACT_FLAGS_USER_BITS + 3))
  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. if (unlikely(!tm->firstuse))
  71. tm->firstuse = now;
  72. }
  73. static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
  74. {
  75. dtm->install = jiffies_to_clock_t(jiffies - stm->install);
  76. dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
  77. dtm->firstuse = stm->firstuse ?
  78. jiffies_to_clock_t(jiffies - stm->firstuse) : 0;
  79. dtm->expires = jiffies_to_clock_t(stm->expires);
  80. }
  81. static inline enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
  82. {
  83. if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
  84. return FLOW_ACTION_HW_STATS_DONT_CARE;
  85. else if (!hw_stats)
  86. return FLOW_ACTION_HW_STATS_DISABLED;
  87. return hw_stats;
  88. }
  89. #ifdef CONFIG_NET_CLS_ACT
  90. #define ACT_P_CREATED 1
  91. #define ACT_P_DELETED 1
  92. typedef void (*tc_action_priv_destructor)(void *priv);
  93. struct tc_action_ops {
  94. struct list_head head;
  95. char kind[IFNAMSIZ];
  96. enum tca_id id; /* identifier should match kind */
  97. unsigned int net_id;
  98. size_t size;
  99. struct module *owner;
  100. int (*act)(struct sk_buff *, const struct tc_action *,
  101. struct tcf_result *); /* called under RCU BH lock*/
  102. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  103. void (*cleanup)(struct tc_action *);
  104. int (*lookup)(struct net *net, struct tc_action **a, u32 index);
  105. int (*init)(struct net *net, struct nlattr *nla,
  106. struct nlattr *est, struct tc_action **act,
  107. struct tcf_proto *tp,
  108. u32 flags, struct netlink_ext_ack *extack);
  109. int (*walk)(struct net *, struct sk_buff *,
  110. struct netlink_callback *, int,
  111. const struct tc_action_ops *,
  112. struct netlink_ext_ack *);
  113. void (*stats_update)(struct tc_action *, u64, u64, u64, u64, bool);
  114. size_t (*get_fill_size)(const struct tc_action *act);
  115. struct net_device *(*get_dev)(const struct tc_action *a,
  116. tc_action_priv_destructor *destructor);
  117. struct psample_group *
  118. (*get_psample_group)(const struct tc_action *a,
  119. tc_action_priv_destructor *destructor);
  120. int (*offload_act_setup)(struct tc_action *act, void *entry_data,
  121. u32 *index_inc, bool bind,
  122. struct netlink_ext_ack *extack);
  123. };
  124. struct tc_action_net {
  125. struct tcf_idrinfo *idrinfo;
  126. const struct tc_action_ops *ops;
  127. };
  128. static inline
  129. int tc_action_net_init(struct net *net, struct tc_action_net *tn,
  130. const struct tc_action_ops *ops)
  131. {
  132. int err = 0;
  133. tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
  134. if (!tn->idrinfo)
  135. return -ENOMEM;
  136. tn->ops = ops;
  137. tn->idrinfo->net = net;
  138. mutex_init(&tn->idrinfo->lock);
  139. idr_init(&tn->idrinfo->action_idr);
  140. return err;
  141. }
  142. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  143. struct tcf_idrinfo *idrinfo);
  144. static inline void tc_action_net_exit(struct list_head *net_list,
  145. unsigned int id)
  146. {
  147. struct net *net;
  148. rtnl_lock();
  149. list_for_each_entry(net, net_list, exit_list) {
  150. struct tc_action_net *tn = net_generic(net, id);
  151. tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
  152. kfree(tn->idrinfo);
  153. }
  154. rtnl_unlock();
  155. }
  156. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  157. struct netlink_callback *cb, int type,
  158. const struct tc_action_ops *ops,
  159. struct netlink_ext_ack *extack);
  160. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
  161. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  162. struct tc_action **a, const struct tc_action_ops *ops,
  163. int bind, bool cpustats, u32 flags);
  164. int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
  165. struct nlattr *est, struct tc_action **a,
  166. const struct tc_action_ops *ops, int bind,
  167. u32 flags);
  168. void tcf_idr_insert_many(struct tc_action *actions[]);
  169. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
  170. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  171. struct tc_action **a, int bind);
  172. int tcf_idr_release(struct tc_action *a, bool bind);
  173. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  174. int tcf_unregister_action(struct tc_action_ops *a,
  175. struct pernet_operations *ops);
  176. int tcf_action_destroy(struct tc_action *actions[], int bind);
  177. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  178. int nr_actions, struct tcf_result *res);
  179. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  180. struct nlattr *est,
  181. struct tc_action *actions[], int init_res[], size_t *attr_size,
  182. u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
  183. struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
  184. bool rtnl_held,
  185. struct netlink_ext_ack *extack);
  186. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  187. struct nlattr *nla, struct nlattr *est,
  188. struct tc_action_ops *a_o, int *init_res,
  189. u32 flags, struct netlink_ext_ack *extack);
  190. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind,
  191. int ref, bool terse);
  192. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  193. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  194. static inline void tcf_action_update_bstats(struct tc_action *a,
  195. struct sk_buff *skb)
  196. {
  197. if (likely(a->cpu_bstats)) {
  198. bstats_update(this_cpu_ptr(a->cpu_bstats), skb);
  199. return;
  200. }
  201. spin_lock(&a->tcfa_lock);
  202. bstats_update(&a->tcfa_bstats, skb);
  203. spin_unlock(&a->tcfa_lock);
  204. }
  205. static inline void tcf_action_inc_drop_qstats(struct tc_action *a)
  206. {
  207. if (likely(a->cpu_qstats)) {
  208. qstats_drop_inc(this_cpu_ptr(a->cpu_qstats));
  209. return;
  210. }
  211. spin_lock(&a->tcfa_lock);
  212. qstats_drop_inc(&a->tcfa_qstats);
  213. spin_unlock(&a->tcfa_lock);
  214. }
  215. static inline void tcf_action_inc_overlimit_qstats(struct tc_action *a)
  216. {
  217. if (likely(a->cpu_qstats)) {
  218. qstats_overlimit_inc(this_cpu_ptr(a->cpu_qstats));
  219. return;
  220. }
  221. spin_lock(&a->tcfa_lock);
  222. qstats_overlimit_inc(&a->tcfa_qstats);
  223. spin_unlock(&a->tcfa_lock);
  224. }
  225. void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
  226. u64 drops, bool hw);
  227. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  228. int tcf_action_update_hw_stats(struct tc_action *action);
  229. int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  230. void *cb_priv, bool add);
  231. int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
  232. struct tcf_chain **handle,
  233. struct netlink_ext_ack *newchain);
  234. struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
  235. struct tcf_chain *newchain);
  236. #ifdef CONFIG_INET
  237. DECLARE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
  238. #endif
  239. int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
  240. #else /* !CONFIG_NET_CLS_ACT */
  241. static inline int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  242. void *cb_priv, bool add) {
  243. return 0;
  244. }
  245. #endif /* CONFIG_NET_CLS_ACT */
  246. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  247. u64 packets, u64 drops,
  248. u64 lastuse, bool hw)
  249. {
  250. #ifdef CONFIG_NET_CLS_ACT
  251. if (!a->ops->stats_update)
  252. return;
  253. a->ops->stats_update(a, bytes, packets, drops, lastuse, hw);
  254. #endif
  255. }
  256. #endif