tc_police.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_TC_POLICE_H
  3. #define __NET_TC_POLICE_H
  4. #include <net/act_api.h>
  5. struct tcf_police_params {
  6. int tcfp_result;
  7. u32 tcfp_ewma_rate;
  8. s64 tcfp_burst;
  9. u32 tcfp_mtu;
  10. s64 tcfp_mtu_ptoks;
  11. s64 tcfp_pkt_burst;
  12. struct psched_ratecfg rate;
  13. bool rate_present;
  14. struct psched_ratecfg peak;
  15. bool peak_present;
  16. struct psched_pktrate ppsrate;
  17. bool pps_present;
  18. struct rcu_head rcu;
  19. };
  20. struct tcf_police {
  21. struct tc_action common;
  22. struct tcf_police_params __rcu *params;
  23. spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
  24. s64 tcfp_toks;
  25. s64 tcfp_ptoks;
  26. s64 tcfp_pkttoks;
  27. s64 tcfp_t_c;
  28. };
  29. #define to_police(pc) ((struct tcf_police *)pc)
  30. /* old policer structure from before tc actions */
  31. struct tc_police_compat {
  32. u32 index;
  33. int action;
  34. u32 limit;
  35. u32 burst;
  36. u32 mtu;
  37. struct tc_ratespec rate;
  38. struct tc_ratespec peakrate;
  39. };
  40. static inline bool is_tcf_police(const struct tc_action *act)
  41. {
  42. #ifdef CONFIG_NET_CLS_ACT
  43. if (act->ops && act->ops->id == TCA_ID_POLICE)
  44. return true;
  45. #endif
  46. return false;
  47. }
  48. static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act)
  49. {
  50. struct tcf_police *police = to_police(act);
  51. struct tcf_police_params *params;
  52. params = rcu_dereference_protected(police->params,
  53. lockdep_is_held(&police->tcf_lock));
  54. return params->rate.rate_bytes_ps;
  55. }
  56. static inline u32 tcf_police_burst(const struct tc_action *act)
  57. {
  58. struct tcf_police *police = to_police(act);
  59. struct tcf_police_params *params;
  60. u32 burst;
  61. params = rcu_dereference_protected(police->params,
  62. lockdep_is_held(&police->tcf_lock));
  63. /*
  64. * "rate" bytes "burst" nanoseconds
  65. * ------------ * -------------------
  66. * 1 second 2^6 ticks
  67. *
  68. * ------------------------------------
  69. * NSEC_PER_SEC nanoseconds
  70. * ------------------------
  71. * 2^6 ticks
  72. *
  73. * "rate" bytes "burst" nanoseconds 2^6 ticks
  74. * = ------------ * ------------------- * ------------------------
  75. * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds
  76. *
  77. * "rate" * "burst"
  78. * = ---------------- bytes/nanosecond
  79. * NSEC_PER_SEC^2
  80. *
  81. *
  82. * "rate" * "burst"
  83. * = ---------------- bytes/second
  84. * NSEC_PER_SEC
  85. */
  86. burst = div_u64(params->tcfp_burst * params->rate.rate_bytes_ps,
  87. NSEC_PER_SEC);
  88. return burst;
  89. }
  90. static inline u64 tcf_police_rate_pkt_ps(const struct tc_action *act)
  91. {
  92. struct tcf_police *police = to_police(act);
  93. struct tcf_police_params *params;
  94. params = rcu_dereference_protected(police->params,
  95. lockdep_is_held(&police->tcf_lock));
  96. return params->ppsrate.rate_pkts_ps;
  97. }
  98. static inline u32 tcf_police_burst_pkt(const struct tc_action *act)
  99. {
  100. struct tcf_police *police = to_police(act);
  101. struct tcf_police_params *params;
  102. u32 burst;
  103. params = rcu_dereference_protected(police->params,
  104. lockdep_is_held(&police->tcf_lock));
  105. /*
  106. * "rate" pkts "burst" nanoseconds
  107. * ------------ * -------------------
  108. * 1 second 2^6 ticks
  109. *
  110. * ------------------------------------
  111. * NSEC_PER_SEC nanoseconds
  112. * ------------------------
  113. * 2^6 ticks
  114. *
  115. * "rate" pkts "burst" nanoseconds 2^6 ticks
  116. * = ------------ * ------------------- * ------------------------
  117. * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds
  118. *
  119. * "rate" * "burst"
  120. * = ---------------- pkts/nanosecond
  121. * NSEC_PER_SEC^2
  122. *
  123. *
  124. * "rate" * "burst"
  125. * = ---------------- pkts/second
  126. * NSEC_PER_SEC
  127. */
  128. burst = div_u64(params->tcfp_pkt_burst * params->ppsrate.rate_pkts_ps,
  129. NSEC_PER_SEC);
  130. return burst;
  131. }
  132. static inline u32 tcf_police_tcfp_mtu(const struct tc_action *act)
  133. {
  134. struct tcf_police *police = to_police(act);
  135. struct tcf_police_params *params;
  136. params = rcu_dereference_protected(police->params,
  137. lockdep_is_held(&police->tcf_lock));
  138. return params->tcfp_mtu;
  139. }
  140. static inline u64 tcf_police_peakrate_bytes_ps(const struct tc_action *act)
  141. {
  142. struct tcf_police *police = to_police(act);
  143. struct tcf_police_params *params;
  144. params = rcu_dereference_protected(police->params,
  145. lockdep_is_held(&police->tcf_lock));
  146. return params->peak.rate_bytes_ps;
  147. }
  148. static inline u32 tcf_police_tcfp_ewma_rate(const struct tc_action *act)
  149. {
  150. struct tcf_police *police = to_police(act);
  151. struct tcf_police_params *params;
  152. params = rcu_dereference_protected(police->params,
  153. lockdep_is_held(&police->tcf_lock));
  154. return params->tcfp_ewma_rate;
  155. }
  156. static inline u16 tcf_police_rate_overhead(const struct tc_action *act)
  157. {
  158. struct tcf_police *police = to_police(act);
  159. struct tcf_police_params *params;
  160. params = rcu_dereference_protected(police->params,
  161. lockdep_is_held(&police->tcf_lock));
  162. return params->rate.overhead;
  163. }
  164. #endif /* __NET_TC_POLICE_H */