act_api.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 *user_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. #define TCA_ACT_FLAGS_AT_INGRESS (1U << (TCA_ACT_FLAGS_USER_BITS + 4))
  63. /* Update lastuse only if needed, to avoid dirtying a cache line.
  64. * We use a temp variable to avoid fetching jiffies twice.
  65. */
  66. static inline void tcf_lastuse_update(struct tcf_t *tm)
  67. {
  68. unsigned long now = jiffies;
  69. if (tm->lastuse != now)
  70. tm->lastuse = now;
  71. if (unlikely(!tm->firstuse))
  72. tm->firstuse = now;
  73. }
  74. static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
  75. {
  76. dtm->install = jiffies_to_clock_t(jiffies - stm->install);
  77. dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
  78. dtm->firstuse = stm->firstuse ?
  79. jiffies_to_clock_t(jiffies - stm->firstuse) : 0;
  80. dtm->expires = jiffies_to_clock_t(stm->expires);
  81. }
  82. static inline enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
  83. {
  84. if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
  85. return FLOW_ACTION_HW_STATS_DONT_CARE;
  86. else if (!hw_stats)
  87. return FLOW_ACTION_HW_STATS_DISABLED;
  88. return hw_stats;
  89. }
  90. typedef void (*tc_action_priv_destructor)(void *priv);
  91. struct tc_action_ops {
  92. struct list_head head;
  93. char kind[IFNAMSIZ];
  94. enum tca_id id; /* identifier should match kind */
  95. unsigned int net_id;
  96. size_t size;
  97. struct module *owner;
  98. int (*act)(struct sk_buff *, const struct tc_action *,
  99. struct tcf_result *); /* called under RCU BH lock*/
  100. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  101. void (*cleanup)(struct tc_action *);
  102. int (*lookup)(struct net *net, struct tc_action **a, u32 index);
  103. int (*init)(struct net *net, struct nlattr *nla,
  104. struct nlattr *est, struct tc_action **act,
  105. struct tcf_proto *tp,
  106. u32 flags, struct netlink_ext_ack *extack);
  107. int (*walk)(struct net *, struct sk_buff *,
  108. struct netlink_callback *, int,
  109. const struct tc_action_ops *,
  110. struct netlink_ext_ack *);
  111. void (*stats_update)(struct tc_action *, u64, u64, u64, u64, bool);
  112. size_t (*get_fill_size)(const struct tc_action *act);
  113. struct net_device *(*get_dev)(const struct tc_action *a,
  114. tc_action_priv_destructor *destructor);
  115. struct psample_group *
  116. (*get_psample_group)(const struct tc_action *a,
  117. tc_action_priv_destructor *destructor);
  118. int (*offload_act_setup)(struct tc_action *act, void *entry_data,
  119. u32 *index_inc, bool bind,
  120. struct netlink_ext_ack *extack);
  121. };
  122. #ifdef CONFIG_NET_CLS_ACT
  123. #define ACT_P_BOUND 0
  124. #define ACT_P_CREATED 1
  125. #define ACT_P_DELETED 1
  126. struct tc_action_net {
  127. struct tcf_idrinfo *idrinfo;
  128. const struct tc_action_ops *ops;
  129. };
  130. static inline
  131. int tc_action_net_init(struct net *net, struct tc_action_net *tn,
  132. const struct tc_action_ops *ops)
  133. {
  134. int err = 0;
  135. tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
  136. if (!tn->idrinfo)
  137. return -ENOMEM;
  138. tn->ops = ops;
  139. tn->idrinfo->net = net;
  140. mutex_init(&tn->idrinfo->lock);
  141. idr_init(&tn->idrinfo->action_idr);
  142. return err;
  143. }
  144. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  145. struct tcf_idrinfo *idrinfo);
  146. static inline void tc_action_net_exit(struct list_head *net_list,
  147. unsigned int id)
  148. {
  149. struct net *net;
  150. rtnl_lock();
  151. list_for_each_entry(net, net_list, exit_list) {
  152. struct tc_action_net *tn = net_generic(net, id);
  153. tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
  154. kfree(tn->idrinfo);
  155. }
  156. rtnl_unlock();
  157. }
  158. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  159. struct netlink_callback *cb, int type,
  160. const struct tc_action_ops *ops,
  161. struct netlink_ext_ack *extack);
  162. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
  163. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  164. struct tc_action **a, const struct tc_action_ops *ops,
  165. int bind, bool cpustats, u32 flags);
  166. int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
  167. struct nlattr *est, struct tc_action **a,
  168. const struct tc_action_ops *ops, int bind,
  169. u32 flags);
  170. void tcf_idr_insert_many(struct tc_action *actions[], int init_res[]);
  171. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
  172. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  173. struct tc_action **a, int bind);
  174. int tcf_idr_release(struct tc_action *a, bool bind);
  175. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  176. int tcf_unregister_action(struct tc_action_ops *a,
  177. struct pernet_operations *ops);
  178. #define NET_ACT_ALIAS_PREFIX "net-act-"
  179. #define MODULE_ALIAS_NET_ACT(kind) MODULE_ALIAS(NET_ACT_ALIAS_PREFIX kind)
  180. int tcf_action_destroy(struct tc_action *actions[], int bind);
  181. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  182. int nr_actions, struct tcf_result *res);
  183. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  184. struct nlattr *est,
  185. struct tc_action *actions[], int init_res[], size_t *attr_size,
  186. u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
  187. struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags,
  188. struct netlink_ext_ack *extack);
  189. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  190. struct nlattr *nla, struct nlattr *est,
  191. struct tc_action_ops *a_o, int *init_res,
  192. u32 flags, struct netlink_ext_ack *extack);
  193. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind,
  194. int ref, bool terse);
  195. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  196. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  197. static inline void tcf_action_update_bstats(struct tc_action *a,
  198. struct sk_buff *skb)
  199. {
  200. if (likely(a->cpu_bstats)) {
  201. bstats_update(this_cpu_ptr(a->cpu_bstats), skb);
  202. return;
  203. }
  204. spin_lock(&a->tcfa_lock);
  205. bstats_update(&a->tcfa_bstats, skb);
  206. spin_unlock(&a->tcfa_lock);
  207. }
  208. static inline void tcf_action_inc_drop_qstats(struct tc_action *a)
  209. {
  210. if (likely(a->cpu_qstats)) {
  211. qstats_drop_inc(this_cpu_ptr(a->cpu_qstats));
  212. return;
  213. }
  214. spin_lock(&a->tcfa_lock);
  215. qstats_drop_inc(&a->tcfa_qstats);
  216. spin_unlock(&a->tcfa_lock);
  217. }
  218. static inline void tcf_action_inc_overlimit_qstats(struct tc_action *a)
  219. {
  220. if (likely(a->cpu_qstats)) {
  221. qstats_overlimit_inc(this_cpu_ptr(a->cpu_qstats));
  222. return;
  223. }
  224. spin_lock(&a->tcfa_lock);
  225. qstats_overlimit_inc(&a->tcfa_qstats);
  226. spin_unlock(&a->tcfa_lock);
  227. }
  228. void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
  229. u64 drops, bool hw);
  230. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  231. int tcf_action_update_hw_stats(struct tc_action *action);
  232. int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  233. void *cb_priv, bool add);
  234. int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
  235. struct tcf_chain **handle,
  236. struct netlink_ext_ack *newchain);
  237. struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
  238. struct tcf_chain *newchain);
  239. #ifdef CONFIG_INET
  240. DECLARE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
  241. #endif
  242. int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
  243. #else /* !CONFIG_NET_CLS_ACT */
  244. static inline int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  245. void *cb_priv, bool add) {
  246. return 0;
  247. }
  248. #endif /* CONFIG_NET_CLS_ACT */
  249. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  250. u64 packets, u64 drops,
  251. u64 lastuse, bool hw)
  252. {
  253. #ifdef CONFIG_NET_CLS_ACT
  254. if (!a->ops->stats_update)
  255. return;
  256. a->ops->stats_update(a, bytes, packets, drops, lastuse, hw);
  257. #endif
  258. }
  259. #endif