Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #include <string.h> |
| 16 | #include <stddef.h> |
| 17 | #include <stdio.h> |
| 18 | #include <assert.h> |
| 19 | #include <math.h> |
Damjan Marion | e936bbe | 2016-02-25 23:17:38 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include <vlib/vlib.h> |
| 23 | #include <vnet/vnet.h> |
| 24 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 25 | #include <vnet/policer/policer.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
| 27 | /* debugs */ |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 28 | #define QOS_DEBUG_ERROR(msg, args...) \ |
| 29 | vlib_log_err (vnet_policer_main.log_class, msg, ##args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 31 | #define QOS_DEBUG_INFO(msg, args...) \ |
| 32 | vlib_log_info (vnet_policer_main.log_class, msg, ##args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | |
| 34 | #ifndef MIN |
| 35 | #define MIN(x,y) (((x)<(y))?(x):(y)) |
| 36 | #endif |
| 37 | |
| 38 | #ifndef MAX |
| 39 | #define MAX(x,y) (((x)>(y))?(x):(y)) |
| 40 | #endif |
| 41 | |
| 42 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_OFFSET 0 |
| 43 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_MASK 8 |
| 44 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_SHIFT 24 |
| 45 | |
| 46 | #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_OFFSET 2 |
| 47 | #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_MASK 2 |
| 48 | #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_SHIFT 10 |
| 49 | |
| 50 | #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_OFFSET 3 |
| 51 | #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_MASK 2 |
| 52 | #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_SHIFT 0 |
| 53 | |
| 54 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_OFFSET 4 |
| 55 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_MASK 32 |
| 56 | #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_SHIFT 0 |
| 57 | |
| 58 | #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_OFFSET 8 |
| 59 | #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_MASK 2 |
| 60 | #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_SHIFT 30 |
| 61 | |
| 62 | #define IPE_POLICER_FULL_WRITE_REQUEST_AN_OFFSET 8 |
| 63 | #define IPE_POLICER_FULL_WRITE_REQUEST_AN_MASK 1 |
| 64 | #define IPE_POLICER_FULL_WRITE_REQUEST_AN_SHIFT 29 |
| 65 | |
| 66 | #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_OFFSET 8 |
| 67 | #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK 4 |
| 68 | #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_SHIFT 22 |
| 69 | |
| 70 | #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_OFFSET 9 |
| 71 | #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK 11 |
| 72 | #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_SHIFT 11 |
| 73 | |
| 74 | #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_OFFSET 10 |
| 75 | #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK 11 |
| 76 | #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_SHIFT 0 |
| 77 | |
| 78 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_OFFSET 12 |
| 79 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK 5 |
| 80 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_SHIFT 27 |
| 81 | |
| 82 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_OFFSET 12 |
| 83 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK 7 |
| 84 | #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_SHIFT 20 |
| 85 | |
| 86 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_OFFSET 13 |
| 87 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK 5 |
| 88 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_SHIFT 15 |
| 89 | |
| 90 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_OFFSET 14 |
| 91 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK 7 |
| 92 | #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_SHIFT 8 |
| 93 | |
| 94 | #define IPE_POLICER_FULL_WRITE_REQUEST_CB_OFFSET 16 |
| 95 | #define IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK 31 |
| 96 | #define IPE_POLICER_FULL_WRITE_REQUEST_CB_SHIFT 0 |
| 97 | |
| 98 | #define IPE_POLICER_FULL_WRITE_REQUEST_EB_OFFSET 20 |
| 99 | #define IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK 31 |
| 100 | #define IPE_POLICER_FULL_WRITE_REQUEST_EB_SHIFT 0 |
| 101 | |
| 102 | #define IPE_RFC_RFC2697 0x00000000 |
| 103 | #define IPE_RFC_RFC2698 0x00000001 |
| 104 | #define IPE_RFC_RFC4115 0x00000002 |
| 105 | #define IPE_RFC_MEF5CF1 0x00000003 |
| 106 | |
| 107 | /* End of constants copied from sse_ipe_desc_fmt.h */ |
| 108 | |
| 109 | /* Misc Policer specific definitions */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 110 | #define QOS_POLICER_FIXED_PKT_SIZE 256 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 112 | #define QOS_POL_TICKS_PER_SEC 1000LL /* 1 tick = 1 ms */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Default burst, in ms (byte format) |
| 116 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 117 | #define QOS_POL_DEF_BURST_BYTE 100 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 120 | * Minimum burst needs to be such that the largest packet size is accommodated |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 122 | #define QOS_POL_MIN_BURST_BYTE 9 * 1024 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * Flag to indicate if AN is employed or not |
| 126 | * 1 - TRUE, 0 - FALSE |
| 127 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 128 | #define QOS_POL_ALLOW_NEGATIVE 1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 130 | /* Various Macros to take care of policer calculations */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 132 | #define QOS_POL_COMM_BKT_MAX (1 << IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK) |
| 133 | #define QOS_POL_EXTD_BKT_MAX (1 << IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 135 | #define QOS_POL_RATE_EXP_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK) |
| 136 | #define QOS_POL_RATE_EXP_MAX ((1 << QOS_POL_RATE_EXP_SIZE) - 1) |
| 137 | #define QOS_POL_AVG_RATE_MANT_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK) |
| 138 | #define QOS_POL_AVG_RATE_MANT_MAX ((1 << QOS_POL_AVG_RATE_MANT_SIZE) - 1) |
| 139 | #define QOS_POL_AVG_RATE_MAX \ |
| 140 | (QOS_POL_AVG_RATE_MANT_MAX << QOS_POL_RATE_EXP_MAX) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 142 | #define QOS_POL_PEAK_RATE_MANT_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK) |
| 143 | #define QOS_POL_PEAK_RATE_MANT_MAX ((1 << QOS_POL_PEAK_RATE_MANT_SIZE) - 1) |
| 144 | #define QOS_POL_PEAK_RATE_MAX \ |
| 145 | (QOS_POL_PEAK_RATE_MANT_MAX << QOS_POL_RATE_EXP_MAX) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 147 | #define QOS_POL_COMM_BKT_LIMIT_MANT_SIZE \ |
| 148 | (IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK) |
| 149 | #define QOS_POL_COMM_BKT_LIMIT_MANT_MAX \ |
| 150 | ((1 << QOS_POL_COMM_BKT_LIMIT_MANT_SIZE) - 1) |
| 151 | #define QOS_POL_COMM_BKT_LIMIT_EXP_SIZE \ |
| 152 | (IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK) |
| 153 | #define QOS_POL_COMM_BKT_LIMIT_EXP_MAX \ |
| 154 | ((1 << QOS_POL_COMM_BKT_LIMIT_EXP_SIZE) - 1) |
| 155 | #define QOS_POL_COMM_BKT_LIMIT_MAX \ |
| 156 | ((u64) QOS_POL_COMM_BKT_LIMIT_MANT_MAX \ |
| 157 | << (u64) QOS_POL_COMM_BKT_LIMIT_EXP_MAX) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 159 | #define QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE \ |
| 160 | (IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK) |
| 161 | #define QOS_POL_EXTD_BKT_LIMIT_MANT_MAX \ |
| 162 | ((1 << QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE) - 1) |
| 163 | #define QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE \ |
| 164 | (IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK) |
| 165 | #define QOS_POL_EXTD_BKT_LIMIT_EXP_MAX \ |
| 166 | ((1 << QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE) - 1) |
| 167 | #define QOS_POL_EXT_BKT_LIMIT_MAX \ |
| 168 | ((u64) QOS_POL_EXTD_BKT_LIMIT_MANT_MAX \ |
| 169 | << (u64) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Rates determine the units of the bucket |
| 173 | * 256.114688 Gbps < Rate 8 byte units |
| 174 | * 128.057344 Gbps < Rate <= 256.114688 Gbps 4 byte units |
| 175 | * 64.028672 Gbps < Rate <= 128.057344 Gbps 2 byte units |
| 176 | * Rate <= 64.028672 Gbps 1 byte units |
| 177 | * |
| 178 | * The code uses bytes per tick as oppose to Gigabits per second. |
| 179 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 180 | #define RATE256 (256114688000LL / 8LL / QOS_POL_TICKS_PER_SEC) |
| 181 | #define RATE128 (128057344000LL / 8LL / QOS_POL_TICKS_PER_SEC) |
| 182 | #define RATE64 (64028672000LL / 8LL / QOS_POL_TICKS_PER_SEC) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | |
| 184 | #define RATE_OVER256_UNIT 8LL |
| 185 | #define RATE_128TO256_UNIT 4LL |
| 186 | #define RATE_64TO128_UNIT 2LL |
| 187 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 188 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 189 | qos_pol_round (u64 numerator, u64 denominator, u64 *rounded_value, |
| 190 | qos_round_type_en round_type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 192 | int rc = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 194 | if (denominator == 0) |
| 195 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 196 | QOS_DEBUG_ERROR ("Illegal denominator"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 197 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 200 | switch (round_type) |
| 201 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 202 | case QOS_ROUND_TO_CLOSEST: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 203 | *rounded_value = ((numerator + (denominator >> 1)) / denominator); |
| 204 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 206 | case QOS_ROUND_TO_UP: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 207 | *rounded_value = (numerator / denominator); |
| 208 | if ((*rounded_value * denominator) < numerator) |
| 209 | { |
| 210 | *rounded_value += 1; |
| 211 | } |
| 212 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 214 | case QOS_ROUND_TO_DOWN: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 215 | *rounded_value = (numerator / denominator); |
| 216 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 218 | case QOS_ROUND_INVALID: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | default: |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 220 | QOS_DEBUG_ERROR ("Illegal round type"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 221 | rc = EINVAL; |
| 222 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 224 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 227 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 228 | pol_validate_cfg_params (qos_pol_cfg_params_st *cfg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 230 | u64 numer, denom, rnd_value; |
| 231 | u32 cir_hw, eir_hw; |
| 232 | int rc = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 234 | if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) && |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 235 | (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps)) |
| 236 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 237 | QOS_DEBUG_ERROR ("CIR (%u kbps) is greater than PIR (%u kbps)", |
| 238 | cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 239 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 242 | /* |
| 243 | * convert rates to bytes-per-tick |
| 244 | */ |
| 245 | numer = (u64) (cfg->rb.kbps.cir_kbps); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 246 | denom = (u64) (8 * QOS_POL_TICKS_PER_SEC) / 1000; |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 247 | rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 248 | if (rc != 0) |
| 249 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 250 | QOS_DEBUG_ERROR ("Unable to convert CIR to bytes/tick format"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 251 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 253 | cir_hw = (u32) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 255 | numer = (u64) (cfg->rb.kbps.eir_kbps); |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 256 | rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 257 | if (rc != 0) |
| 258 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 259 | QOS_DEBUG_ERROR ("Unable to convert EIR to bytes/tick format"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 260 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 262 | eir_hw = (u32) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 264 | if (cir_hw > QOS_POL_AVG_RATE_MAX) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 265 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 266 | QOS_DEBUG_ERROR ("hw cir (%u bytes/tick) is greater than the " |
| 267 | "max supported value (%u)", |
| 268 | cir_hw, QOS_POL_AVG_RATE_MAX); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 269 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 272 | if (eir_hw > QOS_POL_PEAK_RATE_MAX) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 273 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 274 | QOS_DEBUG_ERROR ("hw eir (%u bytes/tick) is greater than the " |
| 275 | "max supported value (%u). Capping it to the max. " |
| 276 | "supported value", |
| 277 | eir_hw, QOS_POL_PEAK_RATE_MAX); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 278 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 280 | /* |
| 281 | * CIR = 0, with bc != 0 is not allowed |
| 282 | */ |
| 283 | if ((cfg->rb.kbps.cir_kbps == 0) && cfg->rb.kbps.cb_bytes) |
| 284 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 285 | QOS_DEBUG_ERROR ("CIR = 0 with bc != 0"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 286 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 289 | if ((cfg->rb.kbps.eir_kbps == 0) && |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 290 | (cfg->rfc > QOS_POLICER_TYPE_1R3C_RFC_2697)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 291 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 292 | QOS_DEBUG_ERROR ("EIR = 0 for a 2R3C policer (rfc: %u)", cfg->rfc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 293 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 296 | if (cfg->rb.kbps.eir_kbps && (cfg->rfc < QOS_POLICER_TYPE_2R3C_RFC_2698)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 297 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 298 | QOS_DEBUG_ERROR ("EIR: %u kbps for a 1-rate policer (rfc: %u)", |
| 299 | cfg->rb.kbps.eir_kbps, cfg->rfc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 300 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 303 | if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 304 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 305 | QOS_DEBUG_ERROR ("For a 1R1B policer, EB burst cannot be > 0"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 306 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 309 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 313 | qos_convert_value_to_exp_mant_fmt (u64 value, u16 max_exp_value, |
| 314 | u16 max_mant_value, qos_round_type_en type, |
| 315 | u8 *exp, u32 *mant) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 317 | u64 rnd_value; |
| 318 | u64 temp_mant; |
| 319 | u8 temp_exp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 321 | /* |
| 322 | * Select the lowest possible exp, and the largest possible mant |
| 323 | */ |
| 324 | temp_exp = 0; |
| 325 | temp_mant = value; |
| 326 | while (temp_exp <= max_exp_value) |
| 327 | { |
| 328 | if (temp_mant <= max_mant_value) |
| 329 | { |
| 330 | break; |
| 331 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 333 | temp_exp++; |
| 334 | rnd_value = 0; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 335 | (void) qos_pol_round ((u64) value, (u64) (1 << temp_exp), &rnd_value, |
| 336 | type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 337 | temp_mant = rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 340 | if (temp_exp > max_exp_value) |
| 341 | { |
| 342 | /* |
| 343 | * CAP mant to its max value, and decrement exp |
| 344 | */ |
| 345 | temp_exp--; |
| 346 | temp_mant = max_mant_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 349 | *exp = temp_exp; |
| 350 | *mant = (u32) temp_mant; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 352 | QOS_DEBUG_INFO ("value: 0x%llx, mant: %u, exp: %u", value, *mant, *exp); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 353 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 356 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 357 | pol_convert_cfg_rates_to_hw (qos_pol_cfg_params_st *cfg, |
| 358 | qos_pol_hw_params_st *hw) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 360 | int rc = 0; |
| 361 | u32 cir_hw, eir_hw, hi_mant, hi_rate, cir_rnded, eir_rnded, eir_kbps; |
| 362 | u64 numer, denom, rnd_value; |
| 363 | u8 exp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 365 | /* |
| 366 | * convert rates to bytes-per-tick (tick is 1ms) |
| 367 | * For rate conversion, the denominator is gonna be the same |
| 368 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 369 | denom = (u64) ((QOS_POL_TICKS_PER_SEC * 8) / 1000); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 370 | numer = (u64) (cfg->rb.kbps.cir_kbps); |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 371 | rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 372 | if (rc != 0) |
| 373 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 374 | QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d", |
| 375 | cfg->rb.kbps.cir_kbps, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 376 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 378 | cir_hw = (u32) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 380 | if (cfg->rb.kbps.cir_kbps && (cir_hw == 0)) |
| 381 | { |
| 382 | /* |
| 383 | * After rounding, cir_hw = 0. Bump it up |
| 384 | */ |
| 385 | cir_hw = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 388 | if (cfg->rfc == QOS_POLICER_TYPE_1R2C) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 389 | { |
| 390 | eir_kbps = 0; |
| 391 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 392 | else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 393 | { |
| 394 | eir_kbps = cfg->rb.kbps.cir_kbps; |
| 395 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 396 | else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 397 | { |
| 398 | eir_kbps = cfg->rb.kbps.eir_kbps - cfg->rb.kbps.cir_kbps; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | eir_kbps = cfg->rb.kbps.eir_kbps; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 405 | numer = (u64) eir_kbps; |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 406 | rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 407 | if (rc != 0) |
| 408 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 409 | QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d", |
| 410 | eir_kbps, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 411 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 413 | eir_hw = (u32) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 415 | if (eir_kbps && (eir_hw == 0)) |
| 416 | { |
| 417 | /* |
| 418 | * After rounding, eir_hw = 0. Bump it up |
| 419 | */ |
| 420 | eir_hw = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 423 | QOS_DEBUG_INFO ("cir_hw: %u bytes/tick, eir_hw: %u bytes/tick", cir_hw, |
| 424 | eir_hw); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 426 | if (cir_hw > eir_hw) |
| 427 | { |
| 428 | hi_rate = cir_hw; |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | hi_rate = eir_hw; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 435 | if ((cir_hw == 0) && (eir_hw == 0)) |
| 436 | { |
| 437 | /* |
| 438 | * Both the rates are 0. Use exp = 15, and set the RFC to 4115. Also |
| 439 | * set AN = 0 |
| 440 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 441 | exp = (u8) QOS_POL_RATE_EXP_MAX; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 442 | hi_mant = 0; |
| 443 | hw->rfc = IPE_RFC_RFC4115; |
| 444 | hw->allow_negative = 0; |
| 445 | } |
| 446 | else |
| 447 | { |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 448 | qos_convert_value_to_exp_mant_fmt (hi_rate, (u16) QOS_POL_RATE_EXP_MAX, |
| 449 | (u16) QOS_POL_AVG_RATE_MANT_MAX, |
| 450 | cfg->rnd_type, &exp, &hi_mant); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 453 | denom = (1ULL << exp); |
| 454 | if (hi_rate == eir_hw) |
| 455 | { |
| 456 | hw->peak_rate_man = (u16) hi_mant; |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 457 | rc = qos_pol_round ((u64) cir_hw, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 458 | hw->avg_rate_man = (u16) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 459 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 460 | else |
| 461 | { |
| 462 | hw->avg_rate_man = (u16) hi_mant; |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 463 | rc = qos_pol_round ((u64) eir_hw, denom, &rnd_value, cfg->rnd_type); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 464 | hw->peak_rate_man = (u16) rnd_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 466 | if (rc != 0) |
| 467 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 468 | QOS_DEBUG_ERROR ("Rounding error"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 469 | return (rc); |
| 470 | } |
| 471 | hw->rate_exp = exp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 473 | if ((hw->avg_rate_man == 0) && (cfg->rb.kbps.cir_kbps)) |
| 474 | { |
| 475 | /* |
| 476 | * cir was reduced to 0 during rounding. Bump it up |
| 477 | */ |
| 478 | hw->avg_rate_man = 1; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 479 | QOS_DEBUG_INFO ("CIR = 0 during rounding. Bump it up to %u " |
| 480 | "bytes/tick", |
| 481 | (hw->avg_rate_man << hw->rate_exp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 484 | if ((hw->peak_rate_man == 0) && eir_kbps) |
| 485 | { |
| 486 | /* |
| 487 | * eir was reduced to 0 during rounding. Bump it up |
| 488 | */ |
| 489 | hw->peak_rate_man = 1; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 490 | QOS_DEBUG_INFO ("EIR = 0 during rounding. Bump it up to %u " |
| 491 | "bytes/tick", |
| 492 | (hw->peak_rate_man << hw->rate_exp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 495 | cir_rnded = (hw->avg_rate_man << hw->rate_exp); |
| 496 | eir_rnded = (hw->peak_rate_man << hw->rate_exp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 498 | QOS_DEBUG_INFO ("Configured(rounded) values, cir: %u " |
| 499 | "kbps (mant: %u, exp: %u, rate: %u bytes/tick)", |
| 500 | cfg->rb.kbps.cir_kbps, hw->avg_rate_man, hw->rate_exp, |
| 501 | cir_rnded); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 503 | QOS_DEBUG_INFO ("Configured(rounded) values, eir: %u " |
| 504 | "kbps (mant: %u, exp: %u, rate: %u bytes/tick)", |
| 505 | cfg->rb.kbps.eir_kbps, hw->peak_rate_man, hw->rate_exp, |
| 506 | eir_rnded); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 508 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /***** |
| 512 | * NAME |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 513 | * pol_get_bkt_max |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | * |
| 515 | * PARAMETERS |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 516 | * rate_hw - either the average rate or peak rate |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | * bkt_max - bit width in the current bucket or extended bucket |
| 518 | * |
| 519 | * RETURNS |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 520 | * u64 - maximum token bytes for the current or extended bucket |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | * |
| 522 | * DESCRIPTION |
| 523 | * The current bucket or extended bucket fields are in units of either |
| 524 | * 1,2,4,8 bytes based on the average or peak rate respective to current |
| 525 | * or extended bucket. |
| 526 | * |
| 527 | * To get the actual maximum number of bytes that can be stored in the |
| 528 | * field, the value must be multiplied by the units of either 1,2,4,8 |
| 529 | * bytes based on the rate. |
| 530 | *****/ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 531 | u64 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 532 | pol_get_bkt_max (u64 rate_hw, u64 bkt_max) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 534 | if (rate_hw <= RATE64) |
| 535 | { |
| 536 | return (bkt_max - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 538 | else if (rate_hw <= RATE128) |
| 539 | { |
| 540 | return ((bkt_max * RATE_64TO128_UNIT) - RATE_64TO128_UNIT); |
| 541 | } |
| 542 | else if (rate_hw <= RATE256) |
| 543 | { |
| 544 | return ((bkt_max * RATE_128TO256_UNIT) - RATE_128TO256_UNIT); |
| 545 | } |
| 546 | /* rate must be over 256 */ |
| 547 | return ((bkt_max * RATE_OVER256_UNIT) - RATE_OVER256_UNIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /***** |
| 551 | * NAME |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 552 | * pol_get_bkt_value |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | * |
| 554 | * PARAMETERS |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 555 | * rate_hw - either the average rate or peak rate |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | * byte_value - bytes for this token bucket |
| 557 | * |
| 558 | * RETURNS |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 559 | * u64 - unit value for the current or extended bucket field |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 560 | * |
| 561 | * DESCRIPTION |
| 562 | * The current bucket or extended bucket fields are in units of either |
| 563 | * 1,2,4,8 bytes based on the average or peak rate respective to current |
| 564 | * or extended bucket. |
| 565 | * |
| 566 | * To get the units that can be stored in the field, the byte value must |
| 567 | * be divided by the units of either 1,2,4,8 bytes based on the rate. |
| 568 | *****/ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 569 | u64 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 570 | pol_get_bkt_value (u64 rate_hw, u64 byte_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 572 | if (rate_hw <= RATE64) |
| 573 | { |
| 574 | return (byte_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 576 | else if (rate_hw <= RATE128) |
| 577 | { |
| 578 | return (byte_value / RATE_64TO128_UNIT); |
| 579 | } |
| 580 | else if (rate_hw <= RATE256) |
| 581 | { |
| 582 | return (byte_value / RATE_128TO256_UNIT); |
| 583 | } |
| 584 | /* rate must be over 256 */ |
| 585 | return (byte_value / RATE_OVER256_UNIT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static void |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 589 | pol_rnd_burst_byte_fmt (u64 cfg_burst, u16 max_exp_value, u16 max_mant_value, |
| 590 | u32 max_bkt_value, u32 rate_hw, u8 *exp, u32 *mant, |
| 591 | u32 *bkt_value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 593 | u64 bkt_max = max_bkt_value; |
| 594 | u64 bkt_limit_max; |
| 595 | u64 rnd_burst; |
| 596 | u64 temp_bkt_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 598 | bkt_limit_max = ((u64) max_mant_value << (u64) max_exp_value); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 599 | bkt_max = pol_get_bkt_max (rate_hw, bkt_max); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 600 | bkt_max = MIN (bkt_max, bkt_limit_max); |
| 601 | if (!cfg_burst) |
| 602 | { |
| 603 | /* |
| 604 | * If configured burst = 0, compute the burst to be 100ms at a given |
| 605 | * rate. Note that for rate_hw = 0, exp = mant = 0. |
| 606 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 607 | cfg_burst = (u64) rate_hw * (u64) QOS_POL_DEF_BURST_BYTE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 610 | if (cfg_burst > bkt_max) |
| 611 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 612 | QOS_DEBUG_ERROR ("burst 0x%llx bytes is greater than the max. " |
| 613 | "supported value 0x%llx bytes. Capping it to the " |
| 614 | "max", |
| 615 | cfg_burst, bkt_max); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 616 | cfg_burst = bkt_max; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 619 | if (cfg_burst < QOS_POL_MIN_BURST_BYTE) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 620 | { |
| 621 | /* |
| 622 | * Bump up the burst value ONLY if the cfg_burst is non-zero AND |
| 623 | * less than the min. supported value |
| 624 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 625 | QOS_DEBUG_INFO ("burst 0x%llx bytes is less than the min " |
| 626 | "supported value %u bytes. Rounding it up to " |
| 627 | "the min", |
| 628 | cfg_burst, QOS_POL_MIN_BURST_BYTE); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 629 | cfg_burst = QOS_POL_MIN_BURST_BYTE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 632 | qos_convert_value_to_exp_mant_fmt (cfg_burst, max_exp_value, max_mant_value, |
| 633 | QOS_ROUND_TO_DOWN, exp, mant); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 634 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 635 | /* Bucket value is based on rate. */ |
| 636 | rnd_burst = ((u64) (*mant) << (u64) (*exp)); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 637 | temp_bkt_value = pol_get_bkt_value (rate_hw, rnd_burst); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 638 | *bkt_value = (u32) temp_bkt_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 641 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 642 | pol_convert_cfg_burst_to_hw (qos_pol_cfg_params_st *cfg, |
| 643 | qos_pol_hw_params_st *hw) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 645 | u8 temp_exp; |
| 646 | u32 temp_mant, rate_hw; |
| 647 | u64 eb_bytes; |
| 648 | u32 bkt_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 650 | /* |
| 651 | * compute Committed Burst |
| 652 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 653 | QOS_DEBUG_INFO ("Compute commit burst ..."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 654 | rate_hw = (hw->avg_rate_man) << (hw->rate_exp); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 655 | pol_rnd_burst_byte_fmt ( |
| 656 | cfg->rb.kbps.cb_bytes, (u16) QOS_POL_COMM_BKT_LIMIT_EXP_MAX, |
| 657 | (u16) QOS_POL_COMM_BKT_LIMIT_MANT_MAX, (u32) QOS_POL_COMM_BKT_MAX, rate_hw, |
| 658 | &temp_exp, &temp_mant, &bkt_value); |
| 659 | QOS_DEBUG_INFO ("Committed burst, burst_limit: 0x%llx mant : %u, " |
| 660 | "exp: %u, rnded: 0x%llx cb:%u bytes", |
| 661 | cfg->rb.kbps.cb_bytes, temp_mant, temp_exp, |
| 662 | ((u64) temp_mant << (u64) temp_exp), bkt_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 664 | hw->comm_bkt_limit_exp = temp_exp; |
| 665 | hw->comm_bkt_limit_man = (u8) temp_mant; |
| 666 | hw->comm_bkt = bkt_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 668 | /* |
| 669 | * compute Exceed Burst |
| 670 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 671 | QOS_DEBUG_INFO ("Compute exceed burst ..."); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 673 | if (cfg->rfc == QOS_POLICER_TYPE_1R2C) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 674 | { |
| 675 | /* |
| 676 | * For 1R2C, hw uses 2R3C (RFC-4115). As such, the Exceed Bucket |
| 677 | * params are set to 0. Recommendation is to use EB_exp = max_exp (=15) |
| 678 | * and EB_mant = 0 |
| 679 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 680 | hw->extd_bkt_limit_exp = (u8) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 681 | hw->extd_bkt_limit_man = 0; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 682 | QOS_DEBUG_INFO ( |
| 683 | "Excess burst, burst: 0x%llx mant: %u, " |
| 684 | "exp: %u, rnded: 0x%llx bytes", |
| 685 | cfg->rb.kbps.eb_bytes, hw->extd_bkt_limit_man, hw->extd_bkt_limit_exp, |
| 686 | ((u64) hw->extd_bkt_limit_man << (u64) hw->extd_bkt_limit_exp)); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 687 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 690 | if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 691 | { |
| 692 | eb_bytes = cfg->rb.kbps.cb_bytes + cfg->rb.kbps.eb_bytes; |
| 693 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 694 | else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 695 | { |
| 696 | eb_bytes = cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes; |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | eb_bytes = cfg->rb.kbps.eb_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 703 | rate_hw = (hw->peak_rate_man) << (hw->rate_exp); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 704 | pol_rnd_burst_byte_fmt (eb_bytes, (u16) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX, |
| 705 | (u16) QOS_POL_EXTD_BKT_LIMIT_MANT_MAX, |
| 706 | (u32) QOS_POL_EXTD_BKT_MAX, rate_hw, &temp_exp, |
| 707 | &temp_mant, &bkt_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 708 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 709 | QOS_DEBUG_INFO ("Excess burst, burst_limit: 0x%llx mant: %u, " |
| 710 | "exp: %u, rnded: 0x%llx eb:%u bytes", |
| 711 | cfg->rb.kbps.eb_bytes, temp_mant, temp_exp, |
| 712 | ((u64) temp_mant << (u64) temp_exp), bkt_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 713 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 714 | hw->extd_bkt_limit_exp = (u8) temp_exp; |
| 715 | hw->extd_bkt_limit_man = (u8) temp_mant; |
| 716 | hw->extd_bkt = bkt_value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 718 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
| 722 | /* |
| 723 | * Input: configured parameter values in 'cfg'. |
| 724 | * Output: h/w programmable parameter values in 'hw'. |
| 725 | * Return: success or failure code. |
| 726 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 727 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 728 | pol_convert_cfg_to_hw_params (qos_pol_cfg_params_st *cfg, |
| 729 | qos_pol_hw_params_st *hw) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 731 | int rc = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 733 | /* |
| 734 | * clear the hw_params |
| 735 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 736 | clib_memset (hw, 0, sizeof (qos_pol_hw_params_st)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 738 | hw->allow_negative = QOS_POL_ALLOW_NEGATIVE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 740 | if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) || |
| 741 | (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 742 | { |
| 743 | hw->rfc = IPE_RFC_RFC4115; |
| 744 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 745 | else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 746 | { |
| 747 | hw->rfc = IPE_RFC_RFC2697; |
| 748 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 749 | else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 750 | { |
| 751 | hw->rfc = IPE_RFC_RFC2698; |
| 752 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 753 | else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 754 | { |
| 755 | hw->rfc = IPE_RFC_MEF5CF1; |
| 756 | } |
| 757 | else |
| 758 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 759 | QOS_DEBUG_ERROR ("Invalid RFC type %d\n", cfg->rfc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 760 | return (EINVAL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 763 | rc = pol_convert_cfg_rates_to_hw (cfg, hw); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 764 | if (rc != 0) |
| 765 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 766 | QOS_DEBUG_ERROR ("Unable to convert config rates to hw. Error: %d", rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 767 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 770 | rc = pol_convert_cfg_burst_to_hw (cfg, hw); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 771 | if (rc != 0) |
| 772 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 773 | QOS_DEBUG_ERROR ("Unable to convert config burst to hw. Error: %d", rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 774 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 777 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 780 | u32 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 781 | qos_convert_pps_to_kbps (u32 rate_pps) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 783 | u64 numer, rnd_value = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 785 | numer = (u64) ((u64) rate_pps * (u64) QOS_POLICER_FIXED_PKT_SIZE * 8LL); |
| 786 | (void) qos_pol_round (numer, 1000LL, &rnd_value, QOS_ROUND_TO_CLOSEST); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 788 | return ((u32) rnd_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 791 | u32 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 792 | qos_convert_burst_ms_to_bytes (u32 burst_ms, u32 rate_kbps) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 793 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 794 | u64 numer, rnd_value = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 795 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 796 | numer = (u64) ((u64) burst_ms * (u64) rate_kbps); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 797 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 798 | (void) qos_pol_round (numer, 8LL, &rnd_value, QOS_ROUND_TO_CLOSEST); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 800 | return ((u32) rnd_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | |
| 804 | /* |
| 805 | * Input: configured parameters in 'cfg'. |
| 806 | * Output: h/w parameters are returned in 'hw', |
| 807 | * Return: Status, success or failure code. |
| 808 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 809 | int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 810 | pol_compute_hw_params (qos_pol_cfg_params_st *cfg, qos_pol_hw_params_st *hw) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 811 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 812 | int rc = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 814 | if (!cfg || !hw) |
| 815 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 816 | QOS_DEBUG_ERROR ("Illegal parameters"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 817 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 820 | /* |
| 821 | * Validate the police config params being presented to RM |
| 822 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 823 | rc = pol_validate_cfg_params (cfg); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 824 | if (rc != 0) |
| 825 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 826 | QOS_DEBUG_ERROR ("Config parameter validation failed. Error: %d", rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 827 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 830 | /* |
| 831 | * first round configured values to h/w supported values. This func |
| 832 | * also determines whether 'tick' or 'byte' format |
| 833 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 834 | rc = pol_convert_cfg_to_hw_params (cfg, hw); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 835 | if (rc != 0) |
| 836 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 837 | QOS_DEBUG_ERROR ("Unable to convert config params to hw params. " |
| 838 | "Error: %d", |
| 839 | rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 840 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 843 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 846 | /* |
| 847 | * Return the number of hardware TSC timer ticks per second for the dataplane. |
| 848 | * This is approximately, but not exactly, the clock speed. |
| 849 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 850 | static u64 |
| 851 | get_tsc_hz (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 852 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 853 | f64 cpu_freq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 854 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 855 | cpu_freq = os_cpu_clock_frequency (); |
| 856 | return (u64) cpu_freq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Convert rates into bytes_per_period and scale. |
| 861 | * Return 0 if ok or 1 if error. |
| 862 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 863 | static int |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 864 | compute_policer_params (u64 hz, /* CPU speed in clocks per second */ |
| 865 | u64 cir_rate, /* in bytes per second */ |
| 866 | u64 pir_rate, /* in bytes per second */ |
| 867 | u32 *current_limit, /* in bytes, output may scale |
| 868 | * the input |
| 869 | */ |
| 870 | u32 *extended_limit, /* in bytes, output may scale |
| 871 | * the input |
| 872 | */ |
| 873 | u32 *cir_bytes_per_period, u32 *pir_bytes_per_period, |
| 874 | u32 *scale) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 875 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 876 | double period; |
| 877 | double internal_cir_bytes_per_period; |
| 878 | double internal_pir_bytes_per_period; |
| 879 | u32 max; |
| 880 | u32 scale_shift; |
| 881 | u32 scale_amount; |
| 882 | u32 __attribute__ ((unused)) orig_current_limit = *current_limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 883 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 884 | /* |
| 885 | * Compute period. For 1Ghz-to-8Ghz CPUs, the period will be in |
| 886 | * the range of 16 to 116 usec. |
| 887 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 888 | period = ((double) hz) / ((double) POLICER_TICKS_PER_PERIOD); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 890 | /* Determine bytes per period for each rate */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 891 | internal_cir_bytes_per_period = (double) cir_rate / period; |
| 892 | internal_pir_bytes_per_period = (double) pir_rate / period; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 893 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 894 | /* |
| 895 | * Scale if possible. Scaling helps rate accuracy, but is constrained |
| 896 | * by the scaled rates and limits fitting in 32-bits. |
| 897 | * In addition, we need to insure the scaled rate is no larger than |
| 898 | * 2^22 tokens per period. This allows the dataplane to ignore overflow |
| 899 | * in the tokens-per-period multiplication since it could only |
| 900 | * happen if the policer were idle for more than a year. |
| 901 | * This is not really a constraint because 100Gbps at 1Ghz is only |
| 902 | * 1.6M tokens per period. |
| 903 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | #define MAX_RATE_SHIFT 10 |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 905 | max = MAX (*current_limit, *extended_limit); |
| 906 | max = MAX (max, (u32) internal_cir_bytes_per_period << MAX_RATE_SHIFT); |
| 907 | max = MAX (max, (u32) internal_pir_bytes_per_period << MAX_RATE_SHIFT); |
| 908 | scale_shift = __builtin_clz (max); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 909 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 910 | scale_amount = 1 << scale_shift; |
| 911 | *scale = scale_shift; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 913 | /* Scale the limits */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 914 | *current_limit = *current_limit << scale_shift; |
| 915 | *extended_limit = *extended_limit << scale_shift; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 917 | /* Scale the rates */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 918 | internal_cir_bytes_per_period = |
| 919 | internal_cir_bytes_per_period * ((double) scale_amount); |
| 920 | internal_pir_bytes_per_period = |
| 921 | internal_pir_bytes_per_period * ((double) scale_amount); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 922 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 923 | /* |
| 924 | * Make sure the new rates are reasonable |
| 925 | * Only needed for very low rates with large bursts |
| 926 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 927 | if (internal_cir_bytes_per_period < 1.0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 928 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 929 | internal_cir_bytes_per_period = 1.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 930 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 931 | if (internal_pir_bytes_per_period < 1.0) |
| 932 | { |
| 933 | internal_pir_bytes_per_period = 1.0; |
| 934 | } |
| 935 | |
| 936 | *cir_bytes_per_period = (u32) internal_cir_bytes_per_period; |
| 937 | *pir_bytes_per_period = (u32) internal_pir_bytes_per_period; |
| 938 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 939 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | |
| 943 | /* |
| 944 | * Input: configured parameters in 'cfg'. |
| 945 | * Output: h/w parameters are returned in 'hw', |
| 946 | * Return: Status, success or failure code. |
| 947 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 948 | int |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 949 | x86_pol_compute_hw_params (qos_pol_cfg_params_st *cfg, policer_t *hw) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 950 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 951 | const int BYTES_PER_KBIT = (1000 / 8); |
| 952 | u64 hz; |
| 953 | u32 cap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 955 | if (!cfg || !hw) |
| 956 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 957 | QOS_DEBUG_ERROR ("Illegal parameters"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 958 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 961 | hz = get_tsc_hz (); |
| 962 | hw->last_update_time = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 963 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 964 | /* |
| 965 | * Cap the bursts to 32-bits. This allows up to almost one second of |
| 966 | * burst on a 40GE interface, which should be fine for x86. |
| 967 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 968 | cap = |
| 969 | (cfg->rb.kbps.cb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.cb_bytes; |
| 970 | hw->current_limit = cap; |
| 971 | cap = |
| 972 | (cfg->rb.kbps.eb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.eb_bytes; |
| 973 | hw->extended_limit = cap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 975 | if ((cfg->rb.kbps.cir_kbps == 0) && (cfg->rb.kbps.cb_bytes == 0) |
| 976 | && (cfg->rb.kbps.eb_bytes == 0)) |
| 977 | { |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 978 | /* This is a uninitialized, always-violate policer */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 979 | hw->single_rate = 1; |
| 980 | hw->cir_tokens_per_period = 0; |
| 981 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 984 | if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) || |
| 985 | (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 986 | { |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 987 | /* Single-rate policer */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 988 | hw->single_rate = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 989 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 990 | if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 991 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 992 | QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R2C."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 993 | return (-1); |
| 994 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 996 | if ((cfg->rb.kbps.cir_kbps == 0) || |
| 997 | (cfg->rb.kbps.eir_kbps != 0) || |
| 998 | ((cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0))) |
| 999 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1000 | QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1001 | return (-1); |
| 1002 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1003 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1004 | if (compute_policer_params (hz, |
| 1005 | (u64) cfg->rb.kbps.cir_kbps * |
| 1006 | BYTES_PER_KBIT, 0, &hw->current_limit, |
| 1007 | &hw->extended_limit, |
| 1008 | &hw->cir_tokens_per_period, |
| 1009 | &hw->pir_tokens_per_period, &hw->scale)) |
| 1010 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1011 | QOS_DEBUG_ERROR ("Policer parameter computation failed."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1012 | return (-1); |
| 1013 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1015 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1016 | else if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) || |
| 1017 | (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1018 | { |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 1019 | /* Two-rate policer */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1020 | if ((cfg->rb.kbps.cir_kbps == 0) || (cfg->rb.kbps.eir_kbps == 0) |
| 1021 | || (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps) |
| 1022 | || (cfg->rb.kbps.cb_bytes == 0) || (cfg->rb.kbps.eb_bytes == 0)) |
| 1023 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1024 | QOS_DEBUG_ERROR ("Config parameter validation failed."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1025 | return (-1); |
| 1026 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1028 | if (compute_policer_params (hz, |
| 1029 | (u64) cfg->rb.kbps.cir_kbps * |
| 1030 | BYTES_PER_KBIT, |
| 1031 | (u64) cfg->rb.kbps.eir_kbps * |
| 1032 | BYTES_PER_KBIT, &hw->current_limit, |
| 1033 | &hw->extended_limit, |
| 1034 | &hw->cir_tokens_per_period, |
| 1035 | &hw->pir_tokens_per_period, &hw->scale)) |
| 1036 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1037 | QOS_DEBUG_ERROR ("Policer parameter computation failed."); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1038 | return (-1); |
| 1039 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1040 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1041 | } |
| 1042 | else |
| 1043 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1044 | QOS_DEBUG_ERROR ( |
| 1045 | "Config parameter validation failed. RFC not supported"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1046 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1049 | hw->current_bucket = hw->current_limit; |
| 1050 | hw->extended_bucket = hw->extended_limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1051 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1052 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1053 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1054 | |
| 1055 | /* |
| 1056 | * Input: configured parameters in 'cfg'. |
| 1057 | * Output: physical structure is returned in 'phys', |
| 1058 | * Return: Status, success or failure code. |
| 1059 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1060 | int |
Maxime Peim | 2d1a62b | 2023-01-06 11:57:38 +0000 | [diff] [blame] | 1061 | pol_logical_2_physical (const qos_pol_cfg_params_st *cfg, policer_t *phys) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1062 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1063 | int rc; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1064 | qos_pol_cfg_params_st kbps_cfg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1065 | |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 1066 | clib_memset (phys, 0, sizeof (policer_t)); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1067 | clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1068 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1069 | if (!cfg) |
| 1070 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1071 | QOS_DEBUG_ERROR ("Illegal parameters"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1072 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1075 | switch (cfg->rate_type) |
| 1076 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1077 | case QOS_RATE_KBPS: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1078 | /* copy all the data into kbps_cfg */ |
| 1079 | kbps_cfg.rb.kbps.cir_kbps = cfg->rb.kbps.cir_kbps; |
| 1080 | kbps_cfg.rb.kbps.eir_kbps = cfg->rb.kbps.eir_kbps; |
| 1081 | kbps_cfg.rb.kbps.cb_bytes = cfg->rb.kbps.cb_bytes; |
| 1082 | kbps_cfg.rb.kbps.eb_bytes = cfg->rb.kbps.eb_bytes; |
| 1083 | break; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1084 | case QOS_RATE_PPS: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1085 | kbps_cfg.rb.kbps.cir_kbps = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1086 | qos_convert_pps_to_kbps (cfg->rb.pps.cir_pps); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1087 | kbps_cfg.rb.kbps.eir_kbps = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1088 | qos_convert_pps_to_kbps (cfg->rb.pps.eir_pps); |
| 1089 | kbps_cfg.rb.kbps.cb_bytes = qos_convert_burst_ms_to_bytes ( |
| 1090 | (u32) cfg->rb.pps.cb_ms, kbps_cfg.rb.kbps.cir_kbps); |
| 1091 | kbps_cfg.rb.kbps.eb_bytes = qos_convert_burst_ms_to_bytes ( |
| 1092 | (u32) cfg->rb.pps.eb_ms, kbps_cfg.rb.kbps.eir_kbps); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1093 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1094 | default: |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1095 | QOS_DEBUG_ERROR ("Illegal rate type"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1096 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1099 | /* rate type is now converted to kbps */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1100 | kbps_cfg.rate_type = QOS_RATE_KBPS; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1101 | kbps_cfg.rnd_type = cfg->rnd_type; |
| 1102 | kbps_cfg.rfc = cfg->rfc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1103 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1104 | phys->action[POLICE_CONFORM] = cfg->conform_action.action_type; |
| 1105 | phys->mark_dscp[POLICE_CONFORM] = cfg->conform_action.dscp; |
| 1106 | phys->action[POLICE_EXCEED] = cfg->exceed_action.action_type; |
| 1107 | phys->mark_dscp[POLICE_EXCEED] = cfg->exceed_action.dscp; |
| 1108 | phys->action[POLICE_VIOLATE] = cfg->violate_action.action_type; |
| 1109 | phys->mark_dscp[POLICE_VIOLATE] = cfg->violate_action.dscp; |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 1110 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1111 | phys->color_aware = cfg->color_aware; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1112 | |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 1113 | /* convert logical into hw params which involves qos calculations */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1114 | rc = x86_pol_compute_hw_params (&kbps_cfg, phys); |
| 1115 | if (rc == -1) |
| 1116 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1117 | QOS_DEBUG_ERROR ("Unable to compute hw param. Error: %d", rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1118 | return (rc); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1121 | /* for debugging purposes, the bucket token values can be overwritten */ |
| 1122 | if (cfg->overwrite_bucket) |
| 1123 | { |
| 1124 | phys->current_bucket = cfg->current_bucket; |
| 1125 | phys->extended_bucket = cfg->extended_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1128 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1131 | static void |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 1132 | qos_convert_pol_bucket_to_hw_fmt (policer_t *bkt, qos_pol_hw_params_st *hw_fmt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1133 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1134 | clib_memset (hw_fmt, 0, sizeof (qos_pol_hw_params_st)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Input: h/w programmable parameter values in 'hw' |
| 1139 | * Output: configured parameter values in 'cfg' |
| 1140 | * Return: Status, success or failure code. |
| 1141 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1142 | static int |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1143 | pol_convert_hw_to_cfg_params (qos_pol_hw_params_st *hw, |
| 1144 | qos_pol_cfg_params_st *cfg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1145 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1146 | u64 temp_rate; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1147 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1148 | if ((hw == NULL) || (cfg == NULL)) |
| 1149 | { |
| 1150 | return EINVAL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1153 | if ((hw->rfc == IPE_RFC_RFC4115) && |
Marco Varlese | 8c55b37 | 2017-05-31 14:00:37 +0200 | [diff] [blame] | 1154 | (hw->peak_rate_man << hw->rate_exp) == 0 && !(hw->extd_bkt_limit_man)) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1155 | { |
| 1156 | /* |
| 1157 | * For a 1R2C, we set EIR = 0, EB = 0 |
| 1158 | */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1159 | cfg->rfc = QOS_POLICER_TYPE_1R2C; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1160 | } |
| 1161 | else if (hw->rfc == IPE_RFC_RFC2697) |
| 1162 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1163 | cfg->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1164 | } |
| 1165 | else if (hw->rfc == IPE_RFC_RFC2698) |
| 1166 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1167 | cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1168 | } |
| 1169 | else if (hw->rfc == IPE_RFC_RFC4115) |
| 1170 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1171 | cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1172 | } |
| 1173 | else if (hw->rfc == IPE_RFC_MEF5CF1) |
| 1174 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1175 | cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1176 | } |
| 1177 | else |
| 1178 | { |
| 1179 | return EINVAL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1182 | temp_rate = |
| 1183 | (((u64) hw->avg_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) / |
| 1184 | 1000; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1185 | cfg->rb.kbps.cir_kbps = (u32) temp_rate; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1186 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1187 | temp_rate = |
| 1188 | (((u64) hw->peak_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) / |
| 1189 | 1000; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1190 | cfg->rb.kbps.eir_kbps = (u32) temp_rate; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1191 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1192 | cfg->rb.kbps.cb_bytes = ((u64) hw->comm_bkt_limit_man << |
| 1193 | (u64) hw->comm_bkt_limit_exp); |
| 1194 | cfg->rb.kbps.eb_bytes = ((u64) hw->extd_bkt_limit_man << |
| 1195 | (u64) hw->extd_bkt_limit_exp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1196 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1197 | if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1198 | { |
| 1199 | /* |
| 1200 | * For 1R3C in the hardware, EB = sum(CB, EB). Also, EIR = CIR. Restore |
| 1201 | * values such that the configured params don't reflect this adjustment |
| 1202 | */ |
| 1203 | cfg->rb.kbps.eb_bytes = (cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes); |
| 1204 | cfg->rb.kbps.eir_kbps = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | } |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1206 | else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1207 | { |
| 1208 | /* |
| 1209 | * For 4115 in the hardware is excess rate and burst, but EA provides |
| 1210 | * peak-rate, so adjust it to be eir |
| 1211 | */ |
| 1212 | cfg->rb.kbps.eir_kbps += cfg->rb.kbps.cir_kbps; |
| 1213 | cfg->rb.kbps.eb_bytes += cfg->rb.kbps.cb_bytes; |
| 1214 | } |
| 1215 | /* h/w conversion to cfg is in kbps */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1216 | cfg->rate_type = QOS_RATE_KBPS; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1217 | cfg->overwrite_bucket = 0; |
| 1218 | cfg->current_bucket = hw->comm_bkt; |
| 1219 | cfg->extended_bucket = hw->extd_bkt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1220 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1221 | QOS_DEBUG_INFO ("configured params, cir: %u kbps, eir: %u kbps, cb " |
| 1222 | "burst: 0x%llx bytes, eb burst: 0x%llx bytes", |
| 1223 | cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps, |
| 1224 | cfg->rb.kbps.cb_bytes, cfg->rb.kbps.eb_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1225 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1226 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1229 | u32 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1230 | qos_convert_kbps_to_pps (u32 rate_kbps) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1231 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1232 | u64 numer, denom, rnd_value = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1233 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1234 | numer = (u64) ((u64) rate_kbps * 1000LL); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1235 | denom = (u64) ((u64) QOS_POLICER_FIXED_PKT_SIZE * 8LL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1236 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1237 | (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1238 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1239 | return ((u32) rnd_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1242 | u32 |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1243 | qos_convert_burst_bytes_to_ms (u64 burst_bytes, u32 rate_kbps) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1244 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1245 | u64 numer, denom, rnd_value = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1246 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1247 | numer = burst_bytes * 8LL; |
| 1248 | denom = (u64) rate_kbps; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1249 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1250 | (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1251 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1252 | return ((u32) rnd_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Input: physical structure in 'phys', rate_type in cfg |
| 1257 | * Output: configured parameters in 'cfg'. |
| 1258 | * Return: Status, success or failure code. |
| 1259 | */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1260 | int |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 1261 | pol_physical_2_logical (policer_t *phys, qos_pol_cfg_params_st *cfg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1263 | int rc; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1264 | qos_pol_hw_params_st pol_hw; |
| 1265 | qos_pol_cfg_params_st kbps_cfg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1266 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1267 | clib_memset (&pol_hw, 0, sizeof (qos_pol_hw_params_st)); |
| 1268 | clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1269 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1270 | if (!phys) |
| 1271 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1272 | QOS_DEBUG_ERROR ("Illegal parameters"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1273 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1276 | qos_convert_pol_bucket_to_hw_fmt (phys, &pol_hw); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1277 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1278 | rc = pol_convert_hw_to_cfg_params (&pol_hw, &kbps_cfg); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1279 | if (rc != 0) |
| 1280 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1281 | QOS_DEBUG_ERROR ("Unable to convert hw params to config params. " |
| 1282 | "Error: %d", |
| 1283 | rc); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1284 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1287 | /* check what rate type is required */ |
| 1288 | switch (cfg->rate_type) |
| 1289 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1290 | case QOS_RATE_KBPS: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1291 | /* copy all the data into kbps_cfg */ |
| 1292 | cfg->rb.kbps.cir_kbps = kbps_cfg.rb.kbps.cir_kbps; |
| 1293 | cfg->rb.kbps.eir_kbps = kbps_cfg.rb.kbps.eir_kbps; |
| 1294 | cfg->rb.kbps.cb_bytes = kbps_cfg.rb.kbps.cb_bytes; |
| 1295 | cfg->rb.kbps.eb_bytes = kbps_cfg.rb.kbps.eb_bytes; |
| 1296 | break; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1297 | case QOS_RATE_PPS: |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1298 | cfg->rb.pps.cir_pps = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1299 | qos_convert_kbps_to_pps (kbps_cfg.rb.kbps.cir_kbps); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1300 | cfg->rb.pps.eir_pps = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1301 | qos_convert_kbps_to_pps (kbps_cfg.rb.kbps.eir_kbps); |
| 1302 | cfg->rb.pps.cb_ms = qos_convert_burst_bytes_to_ms ( |
| 1303 | kbps_cfg.rb.kbps.cb_bytes, kbps_cfg.rb.kbps.cir_kbps); |
| 1304 | cfg->rb.pps.eb_ms = qos_convert_burst_bytes_to_ms ( |
| 1305 | kbps_cfg.rb.kbps.eb_bytes, kbps_cfg.rb.kbps.eir_kbps); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1306 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1307 | default: |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 1308 | QOS_DEBUG_ERROR ("Illegal rate type"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1309 | return (-1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1312 | /* cfg->rate_type remains what it was */ |
| 1313 | cfg->rnd_type = kbps_cfg.rnd_type; |
| 1314 | cfg->rfc = kbps_cfg.rfc; |
| 1315 | cfg->overwrite_bucket = kbps_cfg.overwrite_bucket; |
| 1316 | cfg->current_bucket = kbps_cfg.current_bucket; |
| 1317 | cfg->extended_bucket = kbps_cfg.extended_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1318 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1319 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1320 | } |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * fd.io coding-style-patch-verification: ON |
| 1324 | * |
| 1325 | * Local Variables: |
| 1326 | * eval: (c-set-style "gnu") |
| 1327 | * End: |
| 1328 | */ |