tc_wrapper.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_TC_WRAPPER_H
  3. #define __NET_TC_WRAPPER_H
  4. #include <net/pkt_cls.h>
  5. #if IS_ENABLED(CONFIG_MITIGATION_RETPOLINE)
  6. #include <linux/cpufeature.h>
  7. #include <linux/static_key.h>
  8. #include <linux/indirect_call_wrapper.h>
  9. #define TC_INDIRECT_SCOPE
  10. extern struct static_key_false tc_skip_wrapper;
  11. /* TC Actions */
  12. #ifdef CONFIG_NET_CLS_ACT
  13. #define TC_INDIRECT_ACTION_DECLARE(fname) \
  14. INDIRECT_CALLABLE_DECLARE(int fname(struct sk_buff *skb, \
  15. const struct tc_action *a, \
  16. struct tcf_result *res))
  17. TC_INDIRECT_ACTION_DECLARE(tcf_bpf_act);
  18. TC_INDIRECT_ACTION_DECLARE(tcf_connmark_act);
  19. TC_INDIRECT_ACTION_DECLARE(tcf_csum_act);
  20. TC_INDIRECT_ACTION_DECLARE(tcf_ct_act);
  21. TC_INDIRECT_ACTION_DECLARE(tcf_ctinfo_act);
  22. TC_INDIRECT_ACTION_DECLARE(tcf_gact_act);
  23. TC_INDIRECT_ACTION_DECLARE(tcf_gate_act);
  24. TC_INDIRECT_ACTION_DECLARE(tcf_ife_act);
  25. TC_INDIRECT_ACTION_DECLARE(tcf_ipt_act);
  26. TC_INDIRECT_ACTION_DECLARE(tcf_mirred_act);
  27. TC_INDIRECT_ACTION_DECLARE(tcf_mpls_act);
  28. TC_INDIRECT_ACTION_DECLARE(tcf_nat_act);
  29. TC_INDIRECT_ACTION_DECLARE(tcf_pedit_act);
  30. TC_INDIRECT_ACTION_DECLARE(tcf_police_act);
  31. TC_INDIRECT_ACTION_DECLARE(tcf_sample_act);
  32. TC_INDIRECT_ACTION_DECLARE(tcf_simp_act);
  33. TC_INDIRECT_ACTION_DECLARE(tcf_skbedit_act);
  34. TC_INDIRECT_ACTION_DECLARE(tcf_skbmod_act);
  35. TC_INDIRECT_ACTION_DECLARE(tcf_vlan_act);
  36. TC_INDIRECT_ACTION_DECLARE(tunnel_key_act);
  37. static inline int tc_act(struct sk_buff *skb, const struct tc_action *a,
  38. struct tcf_result *res)
  39. {
  40. if (static_branch_likely(&tc_skip_wrapper))
  41. goto skip;
  42. #if IS_BUILTIN(CONFIG_NET_ACT_GACT)
  43. if (a->ops->act == tcf_gact_act)
  44. return tcf_gact_act(skb, a, res);
  45. #endif
  46. #if IS_BUILTIN(CONFIG_NET_ACT_MIRRED)
  47. if (a->ops->act == tcf_mirred_act)
  48. return tcf_mirred_act(skb, a, res);
  49. #endif
  50. #if IS_BUILTIN(CONFIG_NET_ACT_PEDIT)
  51. if (a->ops->act == tcf_pedit_act)
  52. return tcf_pedit_act(skb, a, res);
  53. #endif
  54. #if IS_BUILTIN(CONFIG_NET_ACT_SKBEDIT)
  55. if (a->ops->act == tcf_skbedit_act)
  56. return tcf_skbedit_act(skb, a, res);
  57. #endif
  58. #if IS_BUILTIN(CONFIG_NET_ACT_SKBMOD)
  59. if (a->ops->act == tcf_skbmod_act)
  60. return tcf_skbmod_act(skb, a, res);
  61. #endif
  62. #if IS_BUILTIN(CONFIG_NET_ACT_POLICE)
  63. if (a->ops->act == tcf_police_act)
  64. return tcf_police_act(skb, a, res);
  65. #endif
  66. #if IS_BUILTIN(CONFIG_NET_ACT_BPF)
  67. if (a->ops->act == tcf_bpf_act)
  68. return tcf_bpf_act(skb, a, res);
  69. #endif
  70. #if IS_BUILTIN(CONFIG_NET_ACT_CONNMARK)
  71. if (a->ops->act == tcf_connmark_act)
  72. return tcf_connmark_act(skb, a, res);
  73. #endif
  74. #if IS_BUILTIN(CONFIG_NET_ACT_CSUM)
  75. if (a->ops->act == tcf_csum_act)
  76. return tcf_csum_act(skb, a, res);
  77. #endif
  78. #if IS_BUILTIN(CONFIG_NET_ACT_CT)
  79. if (a->ops->act == tcf_ct_act)
  80. return tcf_ct_act(skb, a, res);
  81. #endif
  82. #if IS_BUILTIN(CONFIG_NET_ACT_CTINFO)
  83. if (a->ops->act == tcf_ctinfo_act)
  84. return tcf_ctinfo_act(skb, a, res);
  85. #endif
  86. #if IS_BUILTIN(CONFIG_NET_ACT_GATE)
  87. if (a->ops->act == tcf_gate_act)
  88. return tcf_gate_act(skb, a, res);
  89. #endif
  90. #if IS_BUILTIN(CONFIG_NET_ACT_MPLS)
  91. if (a->ops->act == tcf_mpls_act)
  92. return tcf_mpls_act(skb, a, res);
  93. #endif
  94. #if IS_BUILTIN(CONFIG_NET_ACT_NAT)
  95. if (a->ops->act == tcf_nat_act)
  96. return tcf_nat_act(skb, a, res);
  97. #endif
  98. #if IS_BUILTIN(CONFIG_NET_ACT_TUNNEL_KEY)
  99. if (a->ops->act == tunnel_key_act)
  100. return tunnel_key_act(skb, a, res);
  101. #endif
  102. #if IS_BUILTIN(CONFIG_NET_ACT_VLAN)
  103. if (a->ops->act == tcf_vlan_act)
  104. return tcf_vlan_act(skb, a, res);
  105. #endif
  106. #if IS_BUILTIN(CONFIG_NET_ACT_IFE)
  107. if (a->ops->act == tcf_ife_act)
  108. return tcf_ife_act(skb, a, res);
  109. #endif
  110. #if IS_BUILTIN(CONFIG_NET_ACT_SIMP)
  111. if (a->ops->act == tcf_simp_act)
  112. return tcf_simp_act(skb, a, res);
  113. #endif
  114. #if IS_BUILTIN(CONFIG_NET_ACT_SAMPLE)
  115. if (a->ops->act == tcf_sample_act)
  116. return tcf_sample_act(skb, a, res);
  117. #endif
  118. skip:
  119. return a->ops->act(skb, a, res);
  120. }
  121. #endif /* CONFIG_NET_CLS_ACT */
  122. /* TC Filters */
  123. #ifdef CONFIG_NET_CLS
  124. #define TC_INDIRECT_FILTER_DECLARE(fname) \
  125. INDIRECT_CALLABLE_DECLARE(int fname(struct sk_buff *skb, \
  126. const struct tcf_proto *tp, \
  127. struct tcf_result *res))
  128. TC_INDIRECT_FILTER_DECLARE(basic_classify);
  129. TC_INDIRECT_FILTER_DECLARE(cls_bpf_classify);
  130. TC_INDIRECT_FILTER_DECLARE(cls_cgroup_classify);
  131. TC_INDIRECT_FILTER_DECLARE(fl_classify);
  132. TC_INDIRECT_FILTER_DECLARE(flow_classify);
  133. TC_INDIRECT_FILTER_DECLARE(fw_classify);
  134. TC_INDIRECT_FILTER_DECLARE(mall_classify);
  135. TC_INDIRECT_FILTER_DECLARE(route4_classify);
  136. TC_INDIRECT_FILTER_DECLARE(u32_classify);
  137. static inline int tc_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  138. struct tcf_result *res)
  139. {
  140. if (static_branch_likely(&tc_skip_wrapper))
  141. goto skip;
  142. #if IS_BUILTIN(CONFIG_NET_CLS_BPF)
  143. if (tp->classify == cls_bpf_classify)
  144. return cls_bpf_classify(skb, tp, res);
  145. #endif
  146. #if IS_BUILTIN(CONFIG_NET_CLS_U32)
  147. if (tp->classify == u32_classify)
  148. return u32_classify(skb, tp, res);
  149. #endif
  150. #if IS_BUILTIN(CONFIG_NET_CLS_FLOWER)
  151. if (tp->classify == fl_classify)
  152. return fl_classify(skb, tp, res);
  153. #endif
  154. #if IS_BUILTIN(CONFIG_NET_CLS_FW)
  155. if (tp->classify == fw_classify)
  156. return fw_classify(skb, tp, res);
  157. #endif
  158. #if IS_BUILTIN(CONFIG_NET_CLS_MATCHALL)
  159. if (tp->classify == mall_classify)
  160. return mall_classify(skb, tp, res);
  161. #endif
  162. #if IS_BUILTIN(CONFIG_NET_CLS_BASIC)
  163. if (tp->classify == basic_classify)
  164. return basic_classify(skb, tp, res);
  165. #endif
  166. #if IS_BUILTIN(CONFIG_NET_CLS_CGROUP)
  167. if (tp->classify == cls_cgroup_classify)
  168. return cls_cgroup_classify(skb, tp, res);
  169. #endif
  170. #if IS_BUILTIN(CONFIG_NET_CLS_FLOW)
  171. if (tp->classify == flow_classify)
  172. return flow_classify(skb, tp, res);
  173. #endif
  174. #if IS_BUILTIN(CONFIG_NET_CLS_ROUTE4)
  175. if (tp->classify == route4_classify)
  176. return route4_classify(skb, tp, res);
  177. #endif
  178. skip:
  179. return tp->classify(skb, tp, res);
  180. }
  181. #endif /* CONFIG_NET_CLS */
  182. static inline void tc_wrapper_init(void)
  183. {
  184. #ifdef CONFIG_X86
  185. if (!cpu_feature_enabled(X86_FEATURE_RETPOLINE))
  186. static_branch_enable(&tc_skip_wrapper);
  187. #endif
  188. }
  189. #else
  190. #define TC_INDIRECT_SCOPE static
  191. static inline int tc_act(struct sk_buff *skb, const struct tc_action *a,
  192. struct tcf_result *res)
  193. {
  194. return a->ops->act(skb, a, res);
  195. }
  196. static inline int tc_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  197. struct tcf_result *res)
  198. {
  199. return tp->classify(skb, tp, res);
  200. }
  201. static inline void tc_wrapper_init(void)
  202. {
  203. }
  204. #endif
  205. #endif /* __NET_TC_WRAPPER_H */