blob: bffd208716db07604cd1db51661bb599d15dcd93 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Marione936bbe2016-02-25 23:17:38 +010020#include <stdint.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
22#include <vlib/vlib.h>
23#include <vnet/vnet.h>
24
Brian Russellbbccdc52021-02-10 18:34:48 +000025#include <vnet/policer/policer.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
27/* debugs */
Brian Russellbbccdc52021-02-10 18:34:48 +000028#define QOS_DEBUG_ERROR(msg, args...) \
29 vlib_log_err (vnet_policer_main.log_class, msg, ##args);
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Brian Russellbbccdc52021-02-10 18:34:48 +000031#define QOS_DEBUG_INFO(msg, args...) \
32 vlib_log_info (vnet_policer_main.log_class, msg, ##args);
Ed Warnickecb9cada2015-12-08 15:45:58 -070033
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 Russellc5299ff2021-02-09 10:16:58 +0000110#define QOS_POLICER_FIXED_PKT_SIZE 256
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111
Brian Russellc5299ff2021-02-09 10:16:58 +0000112#define QOS_POL_TICKS_PER_SEC 1000LL /* 1 tick = 1 ms */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
114/*
115 * Default burst, in ms (byte format)
116 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000117#define QOS_POL_DEF_BURST_BYTE 100
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
119/*
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700120 * Minimum burst needs to be such that the largest packet size is accommodated
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000122#define QOS_POL_MIN_BURST_BYTE 9 * 1024
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
124/*
125 * Flag to indicate if AN is employed or not
126 * 1 - TRUE, 0 - FALSE
127 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000128#define QOS_POL_ALLOW_NEGATIVE 1
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Brian Russellbbccdc52021-02-10 18:34:48 +0000130/* Various Macros to take care of policer calculations */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Brian Russellc5299ff2021-02-09 10:16:58 +0000132#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 Warnickecb9cada2015-12-08 15:45:58 -0700134
Brian Russellc5299ff2021-02-09 10:16:58 +0000135#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 Warnickecb9cada2015-12-08 15:45:58 -0700141
Brian Russellc5299ff2021-02-09 10:16:58 +0000142#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 Warnickecb9cada2015-12-08 15:45:58 -0700146
Brian Russellc5299ff2021-02-09 10:16:58 +0000147#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 Warnickecb9cada2015-12-08 15:45:58 -0700158
Brian Russellc5299ff2021-02-09 10:16:58 +0000159#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 Warnickecb9cada2015-12-08 15:45:58 -0700170
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 Russellc5299ff2021-02-09 10:16:58 +0000180#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 Warnickecb9cada2015-12-08 15:45:58 -0700183
184#define RATE_OVER256_UNIT 8LL
185#define RATE_128TO256_UNIT 4LL
186#define RATE_64TO128_UNIT 2LL
187
Damjan Marion3891cd82016-10-27 10:27:00 +0200188static int
Brian Russellc5299ff2021-02-09 10:16:58 +0000189qos_pol_round (u64 numerator, u64 denominator, u64 *rounded_value,
190 qos_round_type_en round_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191{
Damjan Marion3891cd82016-10-27 10:27:00 +0200192 int rc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Damjan Marion3891cd82016-10-27 10:27:00 +0200194 if (denominator == 0)
195 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000196 QOS_DEBUG_ERROR ("Illegal denominator");
Damjan Marion3891cd82016-10-27 10:27:00 +0200197 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 }
199
Damjan Marion3891cd82016-10-27 10:27:00 +0200200 switch (round_type)
201 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000202 case QOS_ROUND_TO_CLOSEST:
Damjan Marion3891cd82016-10-27 10:27:00 +0200203 *rounded_value = ((numerator + (denominator >> 1)) / denominator);
204 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Brian Russellc5299ff2021-02-09 10:16:58 +0000206 case QOS_ROUND_TO_UP:
Damjan Marion3891cd82016-10-27 10:27:00 +0200207 *rounded_value = (numerator / denominator);
208 if ((*rounded_value * denominator) < numerator)
209 {
210 *rounded_value += 1;
211 }
212 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Brian Russellc5299ff2021-02-09 10:16:58 +0000214 case QOS_ROUND_TO_DOWN:
Damjan Marion3891cd82016-10-27 10:27:00 +0200215 *rounded_value = (numerator / denominator);
216 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
Brian Russellc5299ff2021-02-09 10:16:58 +0000218 case QOS_ROUND_INVALID:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 default:
Brian Russellc5299ff2021-02-09 10:16:58 +0000220 QOS_DEBUG_ERROR ("Illegal round type");
Damjan Marion3891cd82016-10-27 10:27:00 +0200221 rc = EINVAL;
222 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200224 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225}
226
Damjan Marion3891cd82016-10-27 10:27:00 +0200227static int
Brian Russellc5299ff2021-02-09 10:16:58 +0000228pol_validate_cfg_params (qos_pol_cfg_params_st *cfg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229{
Damjan Marion3891cd82016-10-27 10:27:00 +0200230 u64 numer, denom, rnd_value;
231 u32 cir_hw, eir_hw;
232 int rc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Brian Russellc5299ff2021-02-09 10:16:58 +0000234 if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) &&
Damjan Marion3891cd82016-10-27 10:27:00 +0200235 (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps))
236 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000237 QOS_DEBUG_ERROR ("CIR (%u kbps) is greater than PIR (%u kbps)",
238 cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps);
Damjan Marion3891cd82016-10-27 10:27:00 +0200239 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 }
241
Damjan Marion3891cd82016-10-27 10:27:00 +0200242 /*
243 * convert rates to bytes-per-tick
244 */
245 numer = (u64) (cfg->rb.kbps.cir_kbps);
Brian Russellc5299ff2021-02-09 10:16:58 +0000246 denom = (u64) (8 * QOS_POL_TICKS_PER_SEC) / 1000;
Brian Russellb2aae752021-02-09 11:36:31 +0000247 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200248 if (rc != 0)
249 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000250 QOS_DEBUG_ERROR ("Unable to convert CIR to bytes/tick format");
Damjan Marion3891cd82016-10-27 10:27:00 +0200251 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200253 cir_hw = (u32) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Damjan Marion3891cd82016-10-27 10:27:00 +0200255 numer = (u64) (cfg->rb.kbps.eir_kbps);
Brian Russellb2aae752021-02-09 11:36:31 +0000256 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200257 if (rc != 0)
258 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000259 QOS_DEBUG_ERROR ("Unable to convert EIR to bytes/tick format");
Damjan Marion3891cd82016-10-27 10:27:00 +0200260 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200262 eir_hw = (u32) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Brian Russellc5299ff2021-02-09 10:16:58 +0000264 if (cir_hw > QOS_POL_AVG_RATE_MAX)
Damjan Marion3891cd82016-10-27 10:27:00 +0200265 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000266 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 Marion3891cd82016-10-27 10:27:00 +0200269 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 }
271
Brian Russellc5299ff2021-02-09 10:16:58 +0000272 if (eir_hw > QOS_POL_PEAK_RATE_MAX)
Damjan Marion3891cd82016-10-27 10:27:00 +0200273 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000274 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 Marion3891cd82016-10-27 10:27:00 +0200278 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200280 /*
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 Russellc5299ff2021-02-09 10:16:58 +0000285 QOS_DEBUG_ERROR ("CIR = 0 with bc != 0");
Damjan Marion3891cd82016-10-27 10:27:00 +0200286 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 }
288
Damjan Marion3891cd82016-10-27 10:27:00 +0200289 if ((cfg->rb.kbps.eir_kbps == 0) &&
Brian Russellc5299ff2021-02-09 10:16:58 +0000290 (cfg->rfc > QOS_POLICER_TYPE_1R3C_RFC_2697))
Damjan Marion3891cd82016-10-27 10:27:00 +0200291 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000292 QOS_DEBUG_ERROR ("EIR = 0 for a 2R3C policer (rfc: %u)", cfg->rfc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200293 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 }
295
Brian Russellc5299ff2021-02-09 10:16:58 +0000296 if (cfg->rb.kbps.eir_kbps && (cfg->rfc < QOS_POLICER_TYPE_2R3C_RFC_2698))
Damjan Marion3891cd82016-10-27 10:27:00 +0200297 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000298 QOS_DEBUG_ERROR ("EIR: %u kbps for a 1-rate policer (rfc: %u)",
299 cfg->rb.kbps.eir_kbps, cfg->rfc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200300 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 }
302
Brian Russellc5299ff2021-02-09 10:16:58 +0000303 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes)
Damjan Marion3891cd82016-10-27 10:27:00 +0200304 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000305 QOS_DEBUG_ERROR ("For a 1R1B policer, EB burst cannot be > 0");
Damjan Marion3891cd82016-10-27 10:27:00 +0200306 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 }
308
Damjan Marion3891cd82016-10-27 10:27:00 +0200309 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310}
311
312static void
Brian Russellc5299ff2021-02-09 10:16:58 +0000313qos_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 Warnickecb9cada2015-12-08 15:45:58 -0700316{
Damjan Marion3891cd82016-10-27 10:27:00 +0200317 u64 rnd_value;
318 u64 temp_mant;
319 u8 temp_exp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Damjan Marion3891cd82016-10-27 10:27:00 +0200321 /*
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 Warnickecb9cada2015-12-08 15:45:58 -0700332
Damjan Marion3891cd82016-10-27 10:27:00 +0200333 temp_exp++;
334 rnd_value = 0;
Brian Russellc5299ff2021-02-09 10:16:58 +0000335 (void) qos_pol_round ((u64) value, (u64) (1 << temp_exp), &rnd_value,
336 type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200337 temp_mant = rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 }
339
Damjan Marion3891cd82016-10-27 10:27:00 +0200340 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 Warnickecb9cada2015-12-08 15:45:58 -0700347 }
348
Damjan Marion3891cd82016-10-27 10:27:00 +0200349 *exp = temp_exp;
350 *mant = (u32) temp_mant;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
Brian Russellc5299ff2021-02-09 10:16:58 +0000352 QOS_DEBUG_INFO ("value: 0x%llx, mant: %u, exp: %u", value, *mant, *exp);
Damjan Marion3891cd82016-10-27 10:27:00 +0200353 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354}
355
Damjan Marion3891cd82016-10-27 10:27:00 +0200356static int
Brian Russellc5299ff2021-02-09 10:16:58 +0000357pol_convert_cfg_rates_to_hw (qos_pol_cfg_params_st *cfg,
358 qos_pol_hw_params_st *hw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359{
Damjan Marion3891cd82016-10-27 10:27:00 +0200360 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 Warnickecb9cada2015-12-08 15:45:58 -0700364
Damjan Marion3891cd82016-10-27 10:27:00 +0200365 /*
366 * convert rates to bytes-per-tick (tick is 1ms)
367 * For rate conversion, the denominator is gonna be the same
368 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000369 denom = (u64) ((QOS_POL_TICKS_PER_SEC * 8) / 1000);
Damjan Marion3891cd82016-10-27 10:27:00 +0200370 numer = (u64) (cfg->rb.kbps.cir_kbps);
Brian Russellb2aae752021-02-09 11:36:31 +0000371 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200372 if (rc != 0)
373 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000374 QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d",
375 cfg->rb.kbps.cir_kbps, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200376 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200378 cir_hw = (u32) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Damjan Marion3891cd82016-10-27 10:27:00 +0200380 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 Warnickecb9cada2015-12-08 15:45:58 -0700386 }
387
Brian Russellc5299ff2021-02-09 10:16:58 +0000388 if (cfg->rfc == QOS_POLICER_TYPE_1R2C)
Damjan Marion3891cd82016-10-27 10:27:00 +0200389 {
390 eir_kbps = 0;
391 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000392 else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
Damjan Marion3891cd82016-10-27 10:27:00 +0200393 {
394 eir_kbps = cfg->rb.kbps.cir_kbps;
395 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000396 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
Damjan Marion3891cd82016-10-27 10:27:00 +0200397 {
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 Warnickecb9cada2015-12-08 15:45:58 -0700403 }
404
Damjan Marion3891cd82016-10-27 10:27:00 +0200405 numer = (u64) eir_kbps;
Brian Russellb2aae752021-02-09 11:36:31 +0000406 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200407 if (rc != 0)
408 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000409 QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d",
410 eir_kbps, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200411 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200413 eir_hw = (u32) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Damjan Marion3891cd82016-10-27 10:27:00 +0200415 if (eir_kbps && (eir_hw == 0))
416 {
417 /*
418 * After rounding, eir_hw = 0. Bump it up
419 */
420 eir_hw = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 }
422
Brian Russellc5299ff2021-02-09 10:16:58 +0000423 QOS_DEBUG_INFO ("cir_hw: %u bytes/tick, eir_hw: %u bytes/tick", cir_hw,
424 eir_hw);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
Damjan Marion3891cd82016-10-27 10:27:00 +0200426 if (cir_hw > eir_hw)
427 {
428 hi_rate = cir_hw;
429 }
430 else
431 {
432 hi_rate = eir_hw;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 }
434
Damjan Marion3891cd82016-10-27 10:27:00 +0200435 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 Russellc5299ff2021-02-09 10:16:58 +0000441 exp = (u8) QOS_POL_RATE_EXP_MAX;
Damjan Marion3891cd82016-10-27 10:27:00 +0200442 hi_mant = 0;
443 hw->rfc = IPE_RFC_RFC4115;
444 hw->allow_negative = 0;
445 }
446 else
447 {
Brian Russellb2aae752021-02-09 11:36:31 +0000448 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 Warnickecb9cada2015-12-08 15:45:58 -0700451 }
452
Damjan Marion3891cd82016-10-27 10:27:00 +0200453 denom = (1ULL << exp);
454 if (hi_rate == eir_hw)
455 {
456 hw->peak_rate_man = (u16) hi_mant;
Brian Russellb2aae752021-02-09 11:36:31 +0000457 rc = qos_pol_round ((u64) cir_hw, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200458 hw->avg_rate_man = (u16) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200460 else
461 {
462 hw->avg_rate_man = (u16) hi_mant;
Brian Russellb2aae752021-02-09 11:36:31 +0000463 rc = qos_pol_round ((u64) eir_hw, denom, &rnd_value, cfg->rnd_type);
Damjan Marion3891cd82016-10-27 10:27:00 +0200464 hw->peak_rate_man = (u16) rnd_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200466 if (rc != 0)
467 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000468 QOS_DEBUG_ERROR ("Rounding error");
Damjan Marion3891cd82016-10-27 10:27:00 +0200469 return (rc);
470 }
471 hw->rate_exp = exp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Damjan Marion3891cd82016-10-27 10:27:00 +0200473 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 Russellc5299ff2021-02-09 10:16:58 +0000479 QOS_DEBUG_INFO ("CIR = 0 during rounding. Bump it up to %u "
480 "bytes/tick",
481 (hw->avg_rate_man << hw->rate_exp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 }
483
Damjan Marion3891cd82016-10-27 10:27:00 +0200484 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 Russellc5299ff2021-02-09 10:16:58 +0000490 QOS_DEBUG_INFO ("EIR = 0 during rounding. Bump it up to %u "
491 "bytes/tick",
492 (hw->peak_rate_man << hw->rate_exp));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 }
494
Damjan Marion3891cd82016-10-27 10:27:00 +0200495 cir_rnded = (hw->avg_rate_man << hw->rate_exp);
496 eir_rnded = (hw->peak_rate_man << hw->rate_exp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Brian Russellc5299ff2021-02-09 10:16:58 +0000498 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 Warnickecb9cada2015-12-08 15:45:58 -0700502
Brian Russellc5299ff2021-02-09 10:16:58 +0000503 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 Warnickecb9cada2015-12-08 15:45:58 -0700507
Damjan Marion3891cd82016-10-27 10:27:00 +0200508 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509}
510
511/*****
512 * NAME
Brian Russellc5299ff2021-02-09 10:16:58 +0000513 * pol_get_bkt_max
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 *
515 * PARAMETERS
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700516 * rate_hw - either the average rate or peak rate
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 * bkt_max - bit width in the current bucket or extended bucket
518 *
519 * RETURNS
Damjan Marion3891cd82016-10-27 10:27:00 +0200520 * u64 - maximum token bytes for the current or extended bucket
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 *
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 Marion3891cd82016-10-27 10:27:00 +0200531u64
Brian Russellc5299ff2021-02-09 10:16:58 +0000532pol_get_bkt_max (u64 rate_hw, u64 bkt_max)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533{
Damjan Marion3891cd82016-10-27 10:27:00 +0200534 if (rate_hw <= RATE64)
535 {
536 return (bkt_max - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200538 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 Warnickecb9cada2015-12-08 15:45:58 -0700548}
549
550/*****
551 * NAME
Brian Russellc5299ff2021-02-09 10:16:58 +0000552 * pol_get_bkt_value
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 *
554 * PARAMETERS
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700555 * rate_hw - either the average rate or peak rate
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 * byte_value - bytes for this token bucket
557 *
558 * RETURNS
Damjan Marion3891cd82016-10-27 10:27:00 +0200559 * u64 - unit value for the current or extended bucket field
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 *
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 Marion3891cd82016-10-27 10:27:00 +0200569u64
Brian Russellc5299ff2021-02-09 10:16:58 +0000570pol_get_bkt_value (u64 rate_hw, u64 byte_value)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571{
Damjan Marion3891cd82016-10-27 10:27:00 +0200572 if (rate_hw <= RATE64)
573 {
574 return (byte_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200576 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 Warnickecb9cada2015-12-08 15:45:58 -0700586}
587
588static void
Brian Russellc5299ff2021-02-09 10:16:58 +0000589pol_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 Warnickecb9cada2015-12-08 15:45:58 -0700592{
Damjan Marion3891cd82016-10-27 10:27:00 +0200593 u64 bkt_max = max_bkt_value;
594 u64 bkt_limit_max;
595 u64 rnd_burst;
596 u64 temp_bkt_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Damjan Marion3891cd82016-10-27 10:27:00 +0200598 bkt_limit_max = ((u64) max_mant_value << (u64) max_exp_value);
Brian Russellc5299ff2021-02-09 10:16:58 +0000599 bkt_max = pol_get_bkt_max (rate_hw, bkt_max);
Damjan Marion3891cd82016-10-27 10:27:00 +0200600 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 Russellc5299ff2021-02-09 10:16:58 +0000607 cfg_burst = (u64) rate_hw * (u64) QOS_POL_DEF_BURST_BYTE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 }
609
Damjan Marion3891cd82016-10-27 10:27:00 +0200610 if (cfg_burst > bkt_max)
611 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000612 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 Marion3891cd82016-10-27 10:27:00 +0200616 cfg_burst = bkt_max;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 }
618
Brian Russellc5299ff2021-02-09 10:16:58 +0000619 if (cfg_burst < QOS_POL_MIN_BURST_BYTE)
Damjan Marion3891cd82016-10-27 10:27:00 +0200620 {
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 Russellc5299ff2021-02-09 10:16:58 +0000625 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 Russellc5299ff2021-02-09 10:16:58 +0000629 cfg_burst = QOS_POL_MIN_BURST_BYTE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 }
631
Brian Russellc5299ff2021-02-09 10:16:58 +0000632 qos_convert_value_to_exp_mant_fmt (cfg_burst, max_exp_value, max_mant_value,
633 QOS_ROUND_TO_DOWN, exp, mant);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634
Damjan Marion3891cd82016-10-27 10:27:00 +0200635 /* Bucket value is based on rate. */
636 rnd_burst = ((u64) (*mant) << (u64) (*exp));
Brian Russellc5299ff2021-02-09 10:16:58 +0000637 temp_bkt_value = pol_get_bkt_value (rate_hw, rnd_burst);
Damjan Marion3891cd82016-10-27 10:27:00 +0200638 *bkt_value = (u32) temp_bkt_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639}
640
Damjan Marion3891cd82016-10-27 10:27:00 +0200641static int
Brian Russellc5299ff2021-02-09 10:16:58 +0000642pol_convert_cfg_burst_to_hw (qos_pol_cfg_params_st *cfg,
643 qos_pol_hw_params_st *hw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644{
Damjan Marion3891cd82016-10-27 10:27:00 +0200645 u8 temp_exp;
646 u32 temp_mant, rate_hw;
647 u64 eb_bytes;
648 u32 bkt_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Damjan Marion3891cd82016-10-27 10:27:00 +0200650 /*
651 * compute Committed Burst
652 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000653 QOS_DEBUG_INFO ("Compute commit burst ...");
Damjan Marion3891cd82016-10-27 10:27:00 +0200654 rate_hw = (hw->avg_rate_man) << (hw->rate_exp);
Brian Russellc5299ff2021-02-09 10:16:58 +0000655 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 Warnickecb9cada2015-12-08 15:45:58 -0700663
Damjan Marion3891cd82016-10-27 10:27:00 +0200664 hw->comm_bkt_limit_exp = temp_exp;
665 hw->comm_bkt_limit_man = (u8) temp_mant;
666 hw->comm_bkt = bkt_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Damjan Marion3891cd82016-10-27 10:27:00 +0200668 /*
669 * compute Exceed Burst
670 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000671 QOS_DEBUG_INFO ("Compute exceed burst ...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Brian Russellc5299ff2021-02-09 10:16:58 +0000673 if (cfg->rfc == QOS_POLICER_TYPE_1R2C)
Damjan Marion3891cd82016-10-27 10:27:00 +0200674 {
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 Russellc5299ff2021-02-09 10:16:58 +0000680 hw->extd_bkt_limit_exp = (u8) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX;
Damjan Marion3891cd82016-10-27 10:27:00 +0200681 hw->extd_bkt_limit_man = 0;
Brian Russellc5299ff2021-02-09 10:16:58 +0000682 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 Marion3891cd82016-10-27 10:27:00 +0200687 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 }
689
Brian Russellc5299ff2021-02-09 10:16:58 +0000690 if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
Damjan Marion3891cd82016-10-27 10:27:00 +0200691 {
692 eb_bytes = cfg->rb.kbps.cb_bytes + cfg->rb.kbps.eb_bytes;
693 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000694 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
Damjan Marion3891cd82016-10-27 10:27:00 +0200695 {
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 Warnickecb9cada2015-12-08 15:45:58 -0700701 }
702
Damjan Marion3891cd82016-10-27 10:27:00 +0200703 rate_hw = (hw->peak_rate_man) << (hw->rate_exp);
Brian Russellc5299ff2021-02-09 10:16:58 +0000704 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 Warnickecb9cada2015-12-08 15:45:58 -0700708
Brian Russellc5299ff2021-02-09 10:16:58 +0000709 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 Warnickecb9cada2015-12-08 15:45:58 -0700713
Damjan Marion3891cd82016-10-27 10:27:00 +0200714 hw->extd_bkt_limit_exp = (u8) temp_exp;
715 hw->extd_bkt_limit_man = (u8) temp_mant;
716 hw->extd_bkt = bkt_value;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
Damjan Marion3891cd82016-10-27 10:27:00 +0200718 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719}
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 Marion3891cd82016-10-27 10:27:00 +0200727static int
Brian Russellc5299ff2021-02-09 10:16:58 +0000728pol_convert_cfg_to_hw_params (qos_pol_cfg_params_st *cfg,
729 qos_pol_hw_params_st *hw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730{
Damjan Marion3891cd82016-10-27 10:27:00 +0200731 int rc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732
Damjan Marion3891cd82016-10-27 10:27:00 +0200733 /*
734 * clear the hw_params
735 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000736 clib_memset (hw, 0, sizeof (qos_pol_hw_params_st));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Brian Russellc5299ff2021-02-09 10:16:58 +0000738 hw->allow_negative = QOS_POL_ALLOW_NEGATIVE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
Brian Russellc5299ff2021-02-09 10:16:58 +0000740 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) ||
741 (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115))
Damjan Marion3891cd82016-10-27 10:27:00 +0200742 {
743 hw->rfc = IPE_RFC_RFC4115;
744 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000745 else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
Damjan Marion3891cd82016-10-27 10:27:00 +0200746 {
747 hw->rfc = IPE_RFC_RFC2697;
748 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000749 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
Damjan Marion3891cd82016-10-27 10:27:00 +0200750 {
751 hw->rfc = IPE_RFC_RFC2698;
752 }
Brian Russellc5299ff2021-02-09 10:16:58 +0000753 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
Damjan Marion3891cd82016-10-27 10:27:00 +0200754 {
755 hw->rfc = IPE_RFC_MEF5CF1;
756 }
757 else
758 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000759 QOS_DEBUG_ERROR ("Invalid RFC type %d\n", cfg->rfc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200760 return (EINVAL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 }
762
Brian Russellc5299ff2021-02-09 10:16:58 +0000763 rc = pol_convert_cfg_rates_to_hw (cfg, hw);
Damjan Marion3891cd82016-10-27 10:27:00 +0200764 if (rc != 0)
765 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000766 QOS_DEBUG_ERROR ("Unable to convert config rates to hw. Error: %d", rc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200767 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 }
769
Brian Russellc5299ff2021-02-09 10:16:58 +0000770 rc = pol_convert_cfg_burst_to_hw (cfg, hw);
Damjan Marion3891cd82016-10-27 10:27:00 +0200771 if (rc != 0)
772 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000773 QOS_DEBUG_ERROR ("Unable to convert config burst to hw. Error: %d", rc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200774 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775 }
776
Damjan Marion3891cd82016-10-27 10:27:00 +0200777 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778}
779
Damjan Marion3891cd82016-10-27 10:27:00 +0200780u32
Brian Russellc5299ff2021-02-09 10:16:58 +0000781qos_convert_pps_to_kbps (u32 rate_pps)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782{
Damjan Marion3891cd82016-10-27 10:27:00 +0200783 u64 numer, rnd_value = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
Brian Russellc5299ff2021-02-09 10:16:58 +0000785 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 Warnickecb9cada2015-12-08 15:45:58 -0700787
Damjan Marion3891cd82016-10-27 10:27:00 +0200788 return ((u32) rnd_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789}
790
Damjan Marion3891cd82016-10-27 10:27:00 +0200791u32
Brian Russellc5299ff2021-02-09 10:16:58 +0000792qos_convert_burst_ms_to_bytes (u32 burst_ms, u32 rate_kbps)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793{
Damjan Marion3891cd82016-10-27 10:27:00 +0200794 u64 numer, rnd_value = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Damjan Marion3891cd82016-10-27 10:27:00 +0200796 numer = (u64) ((u64) burst_ms * (u64) rate_kbps);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
Brian Russellc5299ff2021-02-09 10:16:58 +0000798 (void) qos_pol_round (numer, 8LL, &rnd_value, QOS_ROUND_TO_CLOSEST);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Damjan Marion3891cd82016-10-27 10:27:00 +0200800 return ((u32) rnd_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801}
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 Marion3891cd82016-10-27 10:27:00 +0200809int
Brian Russellc5299ff2021-02-09 10:16:58 +0000810pol_compute_hw_params (qos_pol_cfg_params_st *cfg, qos_pol_hw_params_st *hw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811{
Damjan Marion3891cd82016-10-27 10:27:00 +0200812 int rc = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813
Damjan Marion3891cd82016-10-27 10:27:00 +0200814 if (!cfg || !hw)
815 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000816 QOS_DEBUG_ERROR ("Illegal parameters");
Damjan Marion3891cd82016-10-27 10:27:00 +0200817 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 }
819
Damjan Marion3891cd82016-10-27 10:27:00 +0200820 /*
821 * Validate the police config params being presented to RM
822 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000823 rc = pol_validate_cfg_params (cfg);
Damjan Marion3891cd82016-10-27 10:27:00 +0200824 if (rc != 0)
825 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000826 QOS_DEBUG_ERROR ("Config parameter validation failed. Error: %d", rc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200827 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 }
829
Damjan Marion3891cd82016-10-27 10:27:00 +0200830 /*
831 * first round configured values to h/w supported values. This func
832 * also determines whether 'tick' or 'byte' format
833 */
Brian Russellc5299ff2021-02-09 10:16:58 +0000834 rc = pol_convert_cfg_to_hw_params (cfg, hw);
Damjan Marion3891cd82016-10-27 10:27:00 +0200835 if (rc != 0)
836 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000837 QOS_DEBUG_ERROR ("Unable to convert config params to hw params. "
838 "Error: %d",
839 rc);
Damjan Marion3891cd82016-10-27 10:27:00 +0200840 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 }
842
Damjan Marion3891cd82016-10-27 10:27:00 +0200843 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844}
845
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846/*
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 Marion3891cd82016-10-27 10:27:00 +0200850static u64
851get_tsc_hz (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852{
Damjan Marion3891cd82016-10-27 10:27:00 +0200853 f64 cpu_freq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
Damjan Marion3891cd82016-10-27 10:27:00 +0200855 cpu_freq = os_cpu_clock_frequency ();
856 return (u64) cpu_freq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857}
858
859/*
860 * Convert rates into bytes_per_period and scale.
861 * Return 0 if ok or 1 if error.
862 */
Damjan Marion3891cd82016-10-27 10:27:00 +0200863static int
Brian Russellbbccdc52021-02-10 18:34:48 +0000864compute_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 Warnickecb9cada2015-12-08 15:45:58 -0700875{
Damjan Marion3891cd82016-10-27 10:27:00 +0200876 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 Warnickecb9cada2015-12-08 15:45:58 -0700883
Brian Russellbbccdc52021-02-10 18:34:48 +0000884 /*
885 * Compute period. For 1Ghz-to-8Ghz CPUs, the period will be in
886 * the range of 16 to 116 usec.
887 */
Damjan Marion3891cd82016-10-27 10:27:00 +0200888 period = ((double) hz) / ((double) POLICER_TICKS_PER_PERIOD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889
Brian Russellbbccdc52021-02-10 18:34:48 +0000890 /* Determine bytes per period for each rate */
Damjan Marion3891cd82016-10-27 10:27:00 +0200891 internal_cir_bytes_per_period = (double) cir_rate / period;
892 internal_pir_bytes_per_period = (double) pir_rate / period;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Brian Russellbbccdc52021-02-10 18:34:48 +0000894 /*
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 Warnickecb9cada2015-12-08 15:45:58 -0700904#define MAX_RATE_SHIFT 10
Damjan Marion3891cd82016-10-27 10:27:00 +0200905 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 Warnickecb9cada2015-12-08 15:45:58 -0700909
Damjan Marion3891cd82016-10-27 10:27:00 +0200910 scale_amount = 1 << scale_shift;
911 *scale = scale_shift;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Brian Russellbbccdc52021-02-10 18:34:48 +0000913 /* Scale the limits */
Damjan Marion3891cd82016-10-27 10:27:00 +0200914 *current_limit = *current_limit << scale_shift;
915 *extended_limit = *extended_limit << scale_shift;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916
Brian Russellbbccdc52021-02-10 18:34:48 +0000917 /* Scale the rates */
Damjan Marion3891cd82016-10-27 10:27:00 +0200918 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 Warnickecb9cada2015-12-08 15:45:58 -0700922
Brian Russellbbccdc52021-02-10 18:34:48 +0000923 /*
924 * Make sure the new rates are reasonable
925 * Only needed for very low rates with large bursts
926 */
Damjan Marion3891cd82016-10-27 10:27:00 +0200927 if (internal_cir_bytes_per_period < 1.0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 {
Damjan Marion3891cd82016-10-27 10:27:00 +0200929 internal_cir_bytes_per_period = 1.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 }
Damjan Marion3891cd82016-10-27 10:27:00 +0200931 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 Russellbbccdc52021-02-10 18:34:48 +0000939 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940}
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 Marion3891cd82016-10-27 10:27:00 +0200948int
Brian Russell54be0cc2021-02-15 11:49:42 +0000949x86_pol_compute_hw_params (qos_pol_cfg_params_st *cfg, policer_t *hw)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950{
Damjan Marion3891cd82016-10-27 10:27:00 +0200951 const int BYTES_PER_KBIT = (1000 / 8);
952 u64 hz;
953 u32 cap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
Damjan Marion3891cd82016-10-27 10:27:00 +0200955 if (!cfg || !hw)
956 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000957 QOS_DEBUG_ERROR ("Illegal parameters");
Damjan Marion3891cd82016-10-27 10:27:00 +0200958 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 }
960
Damjan Marion3891cd82016-10-27 10:27:00 +0200961 hz = get_tsc_hz ();
962 hw->last_update_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
Brian Russellbbccdc52021-02-10 18:34:48 +0000964 /*
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 Marion3891cd82016-10-27 10:27:00 +0200968 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 Warnickecb9cada2015-12-08 15:45:58 -0700974
Damjan Marion3891cd82016-10-27 10:27:00 +0200975 if ((cfg->rb.kbps.cir_kbps == 0) && (cfg->rb.kbps.cb_bytes == 0)
976 && (cfg->rb.kbps.eb_bytes == 0))
977 {
Brian Russellbbccdc52021-02-10 18:34:48 +0000978 /* This is a uninitialized, always-violate policer */
Damjan Marion3891cd82016-10-27 10:27:00 +0200979 hw->single_rate = 1;
980 hw->cir_tokens_per_period = 0;
981 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982 }
983
Brian Russellc5299ff2021-02-09 10:16:58 +0000984 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) ||
985 (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697))
Damjan Marion3891cd82016-10-27 10:27:00 +0200986 {
Brian Russellbbccdc52021-02-10 18:34:48 +0000987 /* Single-rate policer */
Damjan Marion3891cd82016-10-27 10:27:00 +0200988 hw->single_rate = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989
Brian Russellc5299ff2021-02-09 10:16:58 +0000990 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes)
Damjan Marion3891cd82016-10-27 10:27:00 +0200991 {
Brian Russellc5299ff2021-02-09 10:16:58 +0000992 QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R2C.");
Damjan Marion3891cd82016-10-27 10:27:00 +0200993 return (-1);
994 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995
Damjan Marion3891cd82016-10-27 10:27:00 +0200996 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 Russellc5299ff2021-02-09 10:16:58 +00001000 QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R.");
Damjan Marion3891cd82016-10-27 10:27:00 +02001001 return (-1);
1002 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
Damjan Marion3891cd82016-10-27 10:27:00 +02001004 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 Russellc5299ff2021-02-09 10:16:58 +00001011 QOS_DEBUG_ERROR ("Policer parameter computation failed.");
Damjan Marion3891cd82016-10-27 10:27:00 +02001012 return (-1);
1013 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014
Damjan Marion3891cd82016-10-27 10:27:00 +02001015 }
Brian Russellc5299ff2021-02-09 10:16:58 +00001016 else if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) ||
1017 (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115))
Damjan Marion3891cd82016-10-27 10:27:00 +02001018 {
Brian Russellbbccdc52021-02-10 18:34:48 +00001019 /* Two-rate policer */
Damjan Marion3891cd82016-10-27 10:27:00 +02001020 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 Russellc5299ff2021-02-09 10:16:58 +00001024 QOS_DEBUG_ERROR ("Config parameter validation failed.");
Damjan Marion3891cd82016-10-27 10:27:00 +02001025 return (-1);
1026 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027
Damjan Marion3891cd82016-10-27 10:27:00 +02001028 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 Russellc5299ff2021-02-09 10:16:58 +00001037 QOS_DEBUG_ERROR ("Policer parameter computation failed.");
Damjan Marion3891cd82016-10-27 10:27:00 +02001038 return (-1);
1039 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Damjan Marion3891cd82016-10-27 10:27:00 +02001041 }
1042 else
1043 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001044 QOS_DEBUG_ERROR (
1045 "Config parameter validation failed. RFC not supported");
Damjan Marion3891cd82016-10-27 10:27:00 +02001046 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047 }
1048
Damjan Marion3891cd82016-10-27 10:27:00 +02001049 hw->current_bucket = hw->current_limit;
1050 hw->extended_bucket = hw->extended_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
Damjan Marion3891cd82016-10-27 10:27:00 +02001052 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
1055/*
1056 * Input: configured parameters in 'cfg'.
1057 * Output: physical structure is returned in 'phys',
1058 * Return: Status, success or failure code.
1059 */
Damjan Marion3891cd82016-10-27 10:27:00 +02001060int
Maxime Peim2d1a62b2023-01-06 11:57:38 +00001061pol_logical_2_physical (const qos_pol_cfg_params_st *cfg, policer_t *phys)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062{
Damjan Marion3891cd82016-10-27 10:27:00 +02001063 int rc;
Brian Russellc5299ff2021-02-09 10:16:58 +00001064 qos_pol_cfg_params_st kbps_cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065
Brian Russell54be0cc2021-02-15 11:49:42 +00001066 clib_memset (phys, 0, sizeof (policer_t));
Brian Russellc5299ff2021-02-09 10:16:58 +00001067 clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068
Damjan Marion3891cd82016-10-27 10:27:00 +02001069 if (!cfg)
1070 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001071 QOS_DEBUG_ERROR ("Illegal parameters");
Damjan Marion3891cd82016-10-27 10:27:00 +02001072 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073 }
1074
Damjan Marion3891cd82016-10-27 10:27:00 +02001075 switch (cfg->rate_type)
1076 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001077 case QOS_RATE_KBPS:
Damjan Marion3891cd82016-10-27 10:27:00 +02001078 /* 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 Russellc5299ff2021-02-09 10:16:58 +00001084 case QOS_RATE_PPS:
Damjan Marion3891cd82016-10-27 10:27:00 +02001085 kbps_cfg.rb.kbps.cir_kbps =
Brian Russellc5299ff2021-02-09 10:16:58 +00001086 qos_convert_pps_to_kbps (cfg->rb.pps.cir_pps);
Damjan Marion3891cd82016-10-27 10:27:00 +02001087 kbps_cfg.rb.kbps.eir_kbps =
Brian Russellc5299ff2021-02-09 10:16:58 +00001088 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 Marion3891cd82016-10-27 10:27:00 +02001093 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 default:
Brian Russellc5299ff2021-02-09 10:16:58 +00001095 QOS_DEBUG_ERROR ("Illegal rate type");
Damjan Marion3891cd82016-10-27 10:27:00 +02001096 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 }
1098
Damjan Marion3891cd82016-10-27 10:27:00 +02001099 /* rate type is now converted to kbps */
Brian Russellc5299ff2021-02-09 10:16:58 +00001100 kbps_cfg.rate_type = QOS_RATE_KBPS;
Damjan Marion3891cd82016-10-27 10:27:00 +02001101 kbps_cfg.rnd_type = cfg->rnd_type;
1102 kbps_cfg.rfc = cfg->rfc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103
Damjan Marion3891cd82016-10-27 10:27:00 +02001104 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 Fabian4ac74c92016-05-31 07:33:29 -07001110
Damjan Marion3891cd82016-10-27 10:27:00 +02001111 phys->color_aware = cfg->color_aware;
Matus Fabian70e6a8d2016-06-20 08:10:42 -07001112
Brian Russellbbccdc52021-02-10 18:34:48 +00001113 /* convert logical into hw params which involves qos calculations */
Damjan Marion3891cd82016-10-27 10:27:00 +02001114 rc = x86_pol_compute_hw_params (&kbps_cfg, phys);
1115 if (rc == -1)
1116 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001117 QOS_DEBUG_ERROR ("Unable to compute hw param. Error: %d", rc);
Damjan Marion3891cd82016-10-27 10:27:00 +02001118 return (rc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 }
1120
Damjan Marion3891cd82016-10-27 10:27:00 +02001121 /* 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 Warnickecb9cada2015-12-08 15:45:58 -07001126 }
1127
Damjan Marion3891cd82016-10-27 10:27:00 +02001128 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129}
1130
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131static void
Brian Russell54be0cc2021-02-15 11:49:42 +00001132qos_convert_pol_bucket_to_hw_fmt (policer_t *bkt, qos_pol_hw_params_st *hw_fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133{
Brian Russellc5299ff2021-02-09 10:16:58 +00001134 clib_memset (hw_fmt, 0, sizeof (qos_pol_hw_params_st));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135}
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 Marion3891cd82016-10-27 10:27:00 +02001142static int
Brian Russellc5299ff2021-02-09 10:16:58 +00001143pol_convert_hw_to_cfg_params (qos_pol_hw_params_st *hw,
1144 qos_pol_cfg_params_st *cfg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145{
Damjan Marion3891cd82016-10-27 10:27:00 +02001146 u64 temp_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
Damjan Marion3891cd82016-10-27 10:27:00 +02001148 if ((hw == NULL) || (cfg == NULL))
1149 {
1150 return EINVAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151 }
1152
Damjan Marion3891cd82016-10-27 10:27:00 +02001153 if ((hw->rfc == IPE_RFC_RFC4115) &&
Marco Varlese8c55b372017-05-31 14:00:37 +02001154 (hw->peak_rate_man << hw->rate_exp) == 0 && !(hw->extd_bkt_limit_man))
Damjan Marion3891cd82016-10-27 10:27:00 +02001155 {
1156 /*
1157 * For a 1R2C, we set EIR = 0, EB = 0
1158 */
Brian Russellc5299ff2021-02-09 10:16:58 +00001159 cfg->rfc = QOS_POLICER_TYPE_1R2C;
Damjan Marion3891cd82016-10-27 10:27:00 +02001160 }
1161 else if (hw->rfc == IPE_RFC_RFC2697)
1162 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001163 cfg->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
Damjan Marion3891cd82016-10-27 10:27:00 +02001164 }
1165 else if (hw->rfc == IPE_RFC_RFC2698)
1166 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001167 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
Damjan Marion3891cd82016-10-27 10:27:00 +02001168 }
1169 else if (hw->rfc == IPE_RFC_RFC4115)
1170 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001171 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
Damjan Marion3891cd82016-10-27 10:27:00 +02001172 }
1173 else if (hw->rfc == IPE_RFC_MEF5CF1)
1174 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001175 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
Damjan Marion3891cd82016-10-27 10:27:00 +02001176 }
1177 else
1178 {
1179 return EINVAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180 }
1181
Brian Russellc5299ff2021-02-09 10:16:58 +00001182 temp_rate =
1183 (((u64) hw->avg_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) /
1184 1000;
Damjan Marion3891cd82016-10-27 10:27:00 +02001185 cfg->rb.kbps.cir_kbps = (u32) temp_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
Brian Russellc5299ff2021-02-09 10:16:58 +00001187 temp_rate =
1188 (((u64) hw->peak_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) /
1189 1000;
Damjan Marion3891cd82016-10-27 10:27:00 +02001190 cfg->rb.kbps.eir_kbps = (u32) temp_rate;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Damjan Marion3891cd82016-10-27 10:27:00 +02001192 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 Warnickecb9cada2015-12-08 15:45:58 -07001196
Brian Russellc5299ff2021-02-09 10:16:58 +00001197 if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
Damjan Marion3891cd82016-10-27 10:27:00 +02001198 {
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 Warnickecb9cada2015-12-08 15:45:58 -07001205 }
Brian Russellc5299ff2021-02-09 10:16:58 +00001206 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
Damjan Marion3891cd82016-10-27 10:27:00 +02001207 {
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 Russellc5299ff2021-02-09 10:16:58 +00001216 cfg->rate_type = QOS_RATE_KBPS;
Damjan Marion3891cd82016-10-27 10:27:00 +02001217 cfg->overwrite_bucket = 0;
1218 cfg->current_bucket = hw->comm_bkt;
1219 cfg->extended_bucket = hw->extd_bkt;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
Brian Russellc5299ff2021-02-09 10:16:58 +00001221 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 Warnickecb9cada2015-12-08 15:45:58 -07001225
Damjan Marion3891cd82016-10-27 10:27:00 +02001226 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227}
1228
Damjan Marion3891cd82016-10-27 10:27:00 +02001229u32
Brian Russellc5299ff2021-02-09 10:16:58 +00001230qos_convert_kbps_to_pps (u32 rate_kbps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231{
Damjan Marion3891cd82016-10-27 10:27:00 +02001232 u64 numer, denom, rnd_value = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
Damjan Marion3891cd82016-10-27 10:27:00 +02001234 numer = (u64) ((u64) rate_kbps * 1000LL);
Brian Russellc5299ff2021-02-09 10:16:58 +00001235 denom = (u64) ((u64) QOS_POLICER_FIXED_PKT_SIZE * 8LL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236
Brian Russellc5299ff2021-02-09 10:16:58 +00001237 (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238
Damjan Marion3891cd82016-10-27 10:27:00 +02001239 return ((u32) rnd_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240}
1241
Damjan Marion3891cd82016-10-27 10:27:00 +02001242u32
Brian Russellc5299ff2021-02-09 10:16:58 +00001243qos_convert_burst_bytes_to_ms (u64 burst_bytes, u32 rate_kbps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244{
Damjan Marion3891cd82016-10-27 10:27:00 +02001245 u64 numer, denom, rnd_value = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246
Damjan Marion3891cd82016-10-27 10:27:00 +02001247 numer = burst_bytes * 8LL;
1248 denom = (u64) rate_kbps;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249
Brian Russellc5299ff2021-02-09 10:16:58 +00001250 (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251
Damjan Marion3891cd82016-10-27 10:27:00 +02001252 return ((u32) rnd_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253}
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 Marion3891cd82016-10-27 10:27:00 +02001260int
Brian Russell54be0cc2021-02-15 11:49:42 +00001261pol_physical_2_logical (policer_t *phys, qos_pol_cfg_params_st *cfg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262{
Damjan Marion3891cd82016-10-27 10:27:00 +02001263 int rc;
Brian Russellc5299ff2021-02-09 10:16:58 +00001264 qos_pol_hw_params_st pol_hw;
1265 qos_pol_cfg_params_st kbps_cfg;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
Brian Russellc5299ff2021-02-09 10:16:58 +00001267 clib_memset (&pol_hw, 0, sizeof (qos_pol_hw_params_st));
1268 clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269
Damjan Marion3891cd82016-10-27 10:27:00 +02001270 if (!phys)
1271 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001272 QOS_DEBUG_ERROR ("Illegal parameters");
Damjan Marion3891cd82016-10-27 10:27:00 +02001273 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274 }
1275
Brian Russellc5299ff2021-02-09 10:16:58 +00001276 qos_convert_pol_bucket_to_hw_fmt (phys, &pol_hw);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001277
Brian Russellc5299ff2021-02-09 10:16:58 +00001278 rc = pol_convert_hw_to_cfg_params (&pol_hw, &kbps_cfg);
Damjan Marion3891cd82016-10-27 10:27:00 +02001279 if (rc != 0)
1280 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001281 QOS_DEBUG_ERROR ("Unable to convert hw params to config params. "
1282 "Error: %d",
1283 rc);
Damjan Marion3891cd82016-10-27 10:27:00 +02001284 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285 }
1286
Damjan Marion3891cd82016-10-27 10:27:00 +02001287 /* check what rate type is required */
1288 switch (cfg->rate_type)
1289 {
Brian Russellc5299ff2021-02-09 10:16:58 +00001290 case QOS_RATE_KBPS:
Damjan Marion3891cd82016-10-27 10:27:00 +02001291 /* 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 Russellc5299ff2021-02-09 10:16:58 +00001297 case QOS_RATE_PPS:
Damjan Marion3891cd82016-10-27 10:27:00 +02001298 cfg->rb.pps.cir_pps =
Brian Russellc5299ff2021-02-09 10:16:58 +00001299 qos_convert_kbps_to_pps (kbps_cfg.rb.kbps.cir_kbps);
Damjan Marion3891cd82016-10-27 10:27:00 +02001300 cfg->rb.pps.eir_pps =
Brian Russellc5299ff2021-02-09 10:16:58 +00001301 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 Marion3891cd82016-10-27 10:27:00 +02001306 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307 default:
Brian Russellc5299ff2021-02-09 10:16:58 +00001308 QOS_DEBUG_ERROR ("Illegal rate type");
Damjan Marion3891cd82016-10-27 10:27:00 +02001309 return (-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001310 }
1311
Damjan Marion3891cd82016-10-27 10:27:00 +02001312 /* 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 Warnickecb9cada2015-12-08 15:45:58 -07001318
Damjan Marion3891cd82016-10-27 10:27:00 +02001319 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320}
Damjan Marion3891cd82016-10-27 10:27:00 +02001321
1322/*
1323 * fd.io coding-style-patch-verification: ON
1324 *
1325 * Local Variables:
1326 * eval: (c-set-style "gnu")
1327 * End:
1328 */