act_api.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <net/sch_generic.h>
  8. #include <net/pkt_sched.h>
  9. #include <net/net_namespace.h>
  10. #include <net/netns/generic.h>
  11. struct tcf_idrinfo {
  12. spinlock_t lock;
  13. struct idr action_idr;
  14. struct net *net;
  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_idrinfo *idrinfo;
  23. u32 tcfa_index;
  24. int tcfa_refcnt;
  25. int tcfa_bindcnt;
  26. u32 tcfa_capab;
  27. int tcfa_action;
  28. struct tcf_t tcfa_tm;
  29. struct gnet_stats_basic_packed tcfa_bstats;
  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_cpu __percpu *cpu_bstats;
  34. struct gnet_stats_queue __percpu *cpu_qstats;
  35. struct tc_cookie *act_cookie;
  36. struct tcf_chain *goto_chain;
  37. };
  38. #define tcf_index common.tcfa_index
  39. #define tcf_refcnt common.tcfa_refcnt
  40. #define tcf_bindcnt common.tcfa_bindcnt
  41. #define tcf_capab common.tcfa_capab
  42. #define tcf_action common.tcfa_action
  43. #define tcf_tm common.tcfa_tm
  44. #define tcf_bstats common.tcfa_bstats
  45. #define tcf_qstats common.tcfa_qstats
  46. #define tcf_rate_est common.tcfa_rate_est
  47. #define tcf_lock common.tcfa_lock
  48. /* Update lastuse only if needed, to avoid dirtying a cache line.
  49. * We use a temp variable to avoid fetching jiffies twice.
  50. */
  51. static inline void tcf_lastuse_update(struct tcf_t *tm)
  52. {
  53. unsigned long now = jiffies;
  54. if (tm->lastuse != now)
  55. tm->lastuse = now;
  56. if (unlikely(!tm->firstuse))
  57. tm->firstuse = now;
  58. }
  59. static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
  60. {
  61. dtm->install = jiffies_to_clock_t(jiffies - stm->install);
  62. dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
  63. dtm->firstuse = stm->firstuse ?
  64. jiffies_to_clock_t(jiffies - stm->firstuse) : 0;
  65. dtm->expires = jiffies_to_clock_t(stm->expires);
  66. }
  67. #ifdef CONFIG_NET_CLS_ACT
  68. #define ACT_P_CREATED 1
  69. #define ACT_P_DELETED 1
  70. struct tc_action_ops {
  71. struct list_head head;
  72. char kind[IFNAMSIZ];
  73. __u32 type; /* TBD to match kind */
  74. size_t size;
  75. struct module *owner;
  76. int (*act)(struct sk_buff *, const struct tc_action *,
  77. struct tcf_result *);
  78. int (*dump)(struct sk_buff *, struct tc_action *, int, int);
  79. void (*cleanup)(struct tc_action *, int bind);
  80. int (*lookup)(struct net *, struct tc_action **, u32);
  81. int (*init)(struct net *net, struct nlattr *nla,
  82. struct nlattr *est, struct tc_action **act, int ovr,
  83. int bind);
  84. int (*walk)(struct net *, struct sk_buff *,
  85. struct netlink_callback *, int, const struct tc_action_ops *);
  86. void (*stats_update)(struct tc_action *, u64, u32, u64);
  87. int (*get_dev)(const struct tc_action *a, struct net *net,
  88. struct net_device **mirred_dev);
  89. };
  90. struct tc_action_net {
  91. struct tcf_idrinfo *idrinfo;
  92. const struct tc_action_ops *ops;
  93. };
  94. static inline
  95. int tc_action_net_init(struct net *net, struct tc_action_net *tn,
  96. const struct tc_action_ops *ops)
  97. {
  98. int err = 0;
  99. tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
  100. if (!tn->idrinfo)
  101. return -ENOMEM;
  102. tn->ops = ops;
  103. tn->idrinfo->net = net;
  104. spin_lock_init(&tn->idrinfo->lock);
  105. idr_init(&tn->idrinfo->action_idr);
  106. return err;
  107. }
  108. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  109. struct tcf_idrinfo *idrinfo);
  110. static inline void tc_action_net_exit(struct tc_action_net *tn)
  111. {
  112. rtnl_lock();
  113. tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
  114. rtnl_unlock();
  115. kfree(tn->idrinfo);
  116. }
  117. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  118. struct netlink_callback *cb, int type,
  119. const struct tc_action_ops *ops);
  120. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
  121. bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  122. int bind);
  123. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  124. struct tc_action **a, const struct tc_action_ops *ops,
  125. int bind, bool cpustats);
  126. void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est);
  127. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
  128. int __tcf_idr_release(struct tc_action *a, bool bind, bool strict);
  129. static inline int tcf_idr_release(struct tc_action *a, bool bind)
  130. {
  131. return __tcf_idr_release(a, bind, false);
  132. }
  133. int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
  134. int tcf_unregister_action(struct tc_action_ops *a,
  135. struct pernet_operations *ops);
  136. int tcf_action_destroy(struct list_head *actions, int bind);
  137. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  138. int nr_actions, struct tcf_result *res);
  139. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  140. struct nlattr *est, char *name, int ovr, int bind,
  141. struct list_head *actions);
  142. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  143. struct nlattr *nla, struct nlattr *est,
  144. char *name, int ovr, int bind);
  145. int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
  146. int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
  147. int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
  148. int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
  149. #endif /* CONFIG_NET_CLS_ACT */
  150. static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
  151. u64 packets, u64 lastuse)
  152. {
  153. #ifdef CONFIG_NET_CLS_ACT
  154. if (!a->ops->stats_update)
  155. return;
  156. a->ops->stats_update(a, bytes, packets, lastuse);
  157. #endif
  158. }
  159. #endif