tc_police.h 4.6 KB

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