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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
24 #include <vnet/policer/xlate.h>
25 #include <vnet/policer/police.h>
30 #define SSE2_QOS_DEBUG_ERROR(msg, args...) \
31 fformat(stderr, msg "\n", ##args);
33 #define SSE2_QOS_DEBUG_INFO(msg, args...) \
34 fformat(stderr, msg "\n", ##args);
37 #define SSE2_QOS_TR_ERR(TpParms...)
41 #define SSE2_QOS_TR_INFO(TpParms...)
44 #define MIN(x,y) (((x)<(y))?(x):(y))
48 #define MAX(x,y) (((x)>(y))?(x):(y))
51 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_OFFSET 0
52 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_MASK 8
53 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_SHIFT 24
55 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_OFFSET 2
56 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_MASK 2
57 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_SHIFT 10
59 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_OFFSET 3
60 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_MASK 2
61 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_SHIFT 0
63 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_OFFSET 4
64 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_MASK 32
65 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_SHIFT 0
67 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_OFFSET 8
68 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_MASK 2
69 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_SHIFT 30
71 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_OFFSET 8
72 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_MASK 1
73 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_SHIFT 29
75 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_OFFSET 8
76 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK 4
77 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_SHIFT 22
79 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_OFFSET 9
80 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK 11
81 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_SHIFT 11
83 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_OFFSET 10
84 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK 11
85 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_SHIFT 0
87 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_OFFSET 12
88 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK 5
89 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_SHIFT 27
91 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_OFFSET 12
92 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK 7
93 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_SHIFT 20
95 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_OFFSET 13
96 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK 5
97 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_SHIFT 15
99 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_OFFSET 14
100 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK 7
101 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_SHIFT 8
103 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_OFFSET 16
104 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK 31
105 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_SHIFT 0
107 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_OFFSET 20
108 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK 31
109 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_SHIFT 0
111 #define IPE_RFC_RFC2697 0x00000000
112 #define IPE_RFC_RFC2698 0x00000001
113 #define IPE_RFC_RFC4115 0x00000002
114 #define IPE_RFC_MEF5CF1 0x00000003
116 /* End of constants copied from sse_ipe_desc_fmt.h */
118 /* Misc Policer specific definitions */
119 #define SSE2_QOS_POLICER_FIXED_PKT_SIZE 256
121 // TODO check what can be provided by hw macro based on ASIC
122 #define SSE2_QOS_POL_TICKS_PER_SEC 1000LL /* 1 tick = 1 ms */
125 * Default burst, in ms (byte format)
127 #define SSE2_QOS_POL_DEF_BURST_BYTE 100
130 * Minimum burst needs to be such that the largest packet size is accomodated
132 // Do we need to get it from some lib?
133 #define SSE2_QOS_POL_MIN_BURST_BYTE 9*1024
137 * Flag to indicate if AN is employed or not
138 * 1 - TRUE, 0 - FALSE
140 #define SSE2_QOS_POL_ALLOW_NEGATIVE 1
142 // Various Macros to take care of policer calculations
144 #define SSE2_QOS_POL_COMM_BKT_MAX \
145 (1<<IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK)
146 #define SSE2_QOS_POL_EXTD_BKT_MAX \
147 (1<<IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK)
149 #define SSE2_QOS_POL_RATE_EXP_SIZE \
150 (IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK)
151 #define SSE2_QOS_POL_RATE_EXP_MAX ((1<<SSE2_QOS_POL_RATE_EXP_SIZE) - 1)
152 #define SSE2_QOS_POL_AVG_RATE_MANT_SIZE \
153 (IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK)
154 #define SSE2_QOS_POL_AVG_RATE_MANT_MAX \
155 ((1<< SSE2_QOS_POL_AVG_RATE_MANT_SIZE) - 1)
156 #define SSE2_QOS_POL_AVG_RATE_MAX \
157 (SSE2_QOS_POL_AVG_RATE_MANT_MAX << \
158 SSE2_QOS_POL_RATE_EXP_MAX)
160 #define SSE2_QOS_POL_PEAK_RATE_MANT_SIZE \
161 (IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK)
162 #define SSE2_QOS_POL_PEAK_RATE_MANT_MAX \
163 ((1<<SSE2_QOS_POL_PEAK_RATE_MANT_SIZE) - 1)
164 #define SSE2_QOS_POL_PEAK_RATE_MAX \
165 (SSE2_QOS_POL_PEAK_RATE_MANT_MAX << \
166 SSE2_QOS_POL_RATE_EXP_MAX)
168 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_SIZE \
169 (IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK)
170 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX \
171 ((1<<SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_SIZE) - 1)
172 #define SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_SIZE \
173 (IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK)
174 #define SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX \
175 ((1<<SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_SIZE) - 1)
176 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MAX \
177 ((uint64_t)SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX << \
178 (uint64_t)SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX)
180 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE \
181 (IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK)
182 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX \
183 ((1<<SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE) - 1)
184 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE \
185 (IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK)
186 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX \
187 ((1<<SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE) - 1)
188 #define SSE2_QOS_POL_EXT_BKT_LIMIT_MAX \
189 ((uint64_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX << \
190 (uint64_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX)
193 * Rates determine the units of the bucket
194 * 256.114688 Gbps < Rate 8 byte units
195 * 128.057344 Gbps < Rate <= 256.114688 Gbps 4 byte units
196 * 64.028672 Gbps < Rate <= 128.057344 Gbps 2 byte units
197 * Rate <= 64.028672 Gbps 1 byte units
199 * The code uses bytes per tick as oppose to Gigabits per second.
201 #define RATE256 (256114688000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
202 #define RATE128 (128057344000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
203 #define RATE64 ( 64028672000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
205 #define RATE_OVER256_UNIT 8LL
206 #define RATE_128TO256_UNIT 4LL
207 #define RATE_64TO128_UNIT 2LL
210 sse2_qos_pol_round (uint64_t numerator,
211 uint64_t denominator,
212 uint64_t *rounded_value,
213 sse2_qos_round_type_en round_type)
217 if (denominator == 0) {
218 SSE2_QOS_DEBUG_ERROR("Illegal denominator");
219 SSE2_QOS_TR_ERR(SSE2_QOSRM_TP_ERR_59);
223 switch (round_type) {
224 case SSE2_QOS_ROUND_TO_CLOSEST:
225 *rounded_value = ((numerator + (denominator >> 1)) / denominator);
228 case SSE2_QOS_ROUND_TO_UP:
229 *rounded_value = (numerator / denominator);
230 if ((*rounded_value * denominator) < numerator) {
235 case SSE2_QOS_ROUND_TO_DOWN:
236 *rounded_value = (numerator / denominator);
239 case SSE2_QOS_ROUND_INVALID:
241 SSE2_QOS_DEBUG_ERROR("Illegal round type");
242 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_60, round_type);
251 sse2_pol_validate_cfg_params (sse2_qos_pol_cfg_params_st *cfg)
253 uint64_t numer, denom, rnd_value;
254 uint32_t cir_hw, eir_hw;
257 if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) &&
258 (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps)) {
259 SSE2_QOS_DEBUG_ERROR("CIR (%u kbps) is greater than PIR (%u kbps)",
260 cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps);
261 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_39, cfg->rb.kbps.cir_kbps,
262 cfg->rb.kbps.eir_kbps);
267 * convert rates to bytes-per-tick
269 numer = (uint64_t)(cfg->rb.kbps.cir_kbps);
270 denom = (uint64_t)(8 * SSE2_QOS_POL_TICKS_PER_SEC) / 1000;
271 rc = sse2_qos_pol_round(numer, denom, &rnd_value,
272 (sse2_qos_round_type_en) cfg->rnd_type);
273 if (CERR_IS_NOTOK(rc)) {
274 SSE2_QOS_DEBUG_ERROR("Unable to convert CIR to bytes/tick format");
278 cir_hw = (uint32_t)rnd_value;
280 numer = (uint64_t)(cfg->rb.kbps.eir_kbps);
281 rc = sse2_qos_pol_round(numer, denom, &rnd_value,
282 (sse2_qos_round_type_en) cfg->rnd_type);
283 if (CERR_IS_NOTOK(rc)) {
284 SSE2_QOS_DEBUG_ERROR("Unable to convert EIR to bytes/tick format");
288 eir_hw = (uint32_t)rnd_value;
290 if (cir_hw > SSE2_QOS_POL_AVG_RATE_MAX) {
291 SSE2_QOS_DEBUG_ERROR("hw cir (%u bytes/tick) is greater than the "
292 "max supported value (%u)", cir_hw,
293 SSE2_QOS_POL_AVG_RATE_MAX);
294 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_84, cir_hw,
295 SSE2_QOS_POL_AVG_RATE_MAX);
299 if (eir_hw > SSE2_QOS_POL_PEAK_RATE_MAX) {
300 SSE2_QOS_DEBUG_ERROR("hw eir (%u bytes/tick) is greater than the "
301 "max supported value (%u). Capping it to the max. "
302 "supported value", eir_hw, SSE2_QOS_POL_PEAK_RATE_MAX);
303 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_85,
304 eir_hw, SSE2_QOS_POL_PEAK_RATE_MAX);
308 * CIR = 0, with bc != 0 is not allowed
310 if ((cfg->rb.kbps.cir_kbps == 0) && cfg->rb.kbps.cb_bytes) {
311 SSE2_QOS_DEBUG_ERROR("CIR = 0 with bc != 0");
312 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_55);
316 if ((cfg->rb.kbps.eir_kbps == 0) &&
317 (cfg->rfc > SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)) {
318 SSE2_QOS_DEBUG_ERROR("EIR = 0 for a 2R3C policer (rfc: %u)", cfg->rfc);
319 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_23, cfg->rb.kbps.eir_kbps, cfg->rfc);
323 if (cfg->rb.kbps.eir_kbps &&
324 (cfg->rfc < SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)) {
325 SSE2_QOS_DEBUG_ERROR("EIR: %u kbps for a 1-rate policer (rfc: %u)",
326 cfg->rb.kbps.eir_kbps, cfg->rfc);
327 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_23, cfg->rb.kbps.eir_kbps, cfg->rfc);
331 if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) {
332 SSE2_QOS_DEBUG_ERROR("For a 1R1B policer, EB burst cannot be > 0");
333 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_56);
341 sse2_qos_convert_value_to_exp_mant_fmt (uint64_t value,
342 uint16_t max_exp_value,
343 uint16_t max_mant_value,
344 sse2_qos_round_type_en type,
353 * Select the lowest possible exp, and the largest possible mant
357 while (temp_exp <= max_exp_value) {
358 if (temp_mant <= max_mant_value) {
364 (void)sse2_qos_pol_round((uint64_t)value, (uint64_t)(1 << temp_exp),
366 temp_mant = rnd_value;
369 if (temp_exp > max_exp_value) {
371 * CAP mant to its max value, and decrement exp
374 temp_mant = max_mant_value;
378 *mant = (uint32_t)temp_mant;
380 SSE2_QOS_DEBUG_INFO("value: 0x%llx, mant: %u, exp: %u", value, *mant, *exp);
385 sse2_pol_convert_cfg_rates_to_hw (sse2_qos_pol_cfg_params_st *cfg,
386 sse2_qos_pol_hw_params_st *hw)
389 uint32_t cir_hw, eir_hw, hi_mant, hi_rate, cir_rnded, eir_rnded, eir_kbps;
390 uint64_t numer, denom, rnd_value;
394 * convert rates to bytes-per-tick (tick is 1ms)
395 * For rate conversion, the denominator is gonna be the same
397 denom = (uint64_t)((SSE2_QOS_POL_TICKS_PER_SEC * 8) / 1000);
398 numer = (uint64_t)(cfg->rb.kbps.cir_kbps);
399 rc = sse2_qos_pol_round(numer, denom, &rnd_value,
400 (sse2_qos_round_type_en) cfg->rnd_type);
401 if (CERR_IS_NOTOK(rc)) {
402 SSE2_QOS_DEBUG_ERROR("Rounding error, rate: %d kbps, rounding_type: %d",
403 cfg->rb.kbps.cir_kbps, cfg->rnd_type);
407 cir_hw = (uint32_t)rnd_value;
409 if (cfg->rb.kbps.cir_kbps && (cir_hw == 0)) {
411 * After rounding, cir_hw = 0. Bump it up
416 if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) {
418 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
419 eir_kbps = cfg->rb.kbps.cir_kbps;
420 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
421 eir_kbps = cfg->rb.kbps.eir_kbps - cfg->rb.kbps.cir_kbps;
423 eir_kbps = cfg->rb.kbps.eir_kbps;
426 numer = (uint64_t)eir_kbps;
427 rc = sse2_qos_pol_round(numer, denom, &rnd_value,
428 (sse2_qos_round_type_en) cfg->rnd_type);
429 if (CERR_IS_NOTOK(rc)) {
430 SSE2_QOS_DEBUG_ERROR("Rounding error, rate: %d kbps, rounding_type: %d",
431 eir_kbps, cfg->rnd_type);
435 eir_hw = (uint32_t)rnd_value;
437 if (eir_kbps && (eir_hw == 0)) {
439 * After rounding, eir_hw = 0. Bump it up
444 SSE2_QOS_DEBUG_INFO("cir_hw: %u bytes/tick, eir_hw: %u bytes/tick", cir_hw,
447 if (cir_hw > eir_hw) {
453 if ((cir_hw == 0) && (eir_hw == 0)) {
455 * Both the rates are 0. Use exp = 15, and set the RFC to 4115. Also
458 exp = (uint8_t)SSE2_QOS_POL_RATE_EXP_MAX;
460 hw->rfc = IPE_RFC_RFC4115;
461 hw->allow_negative = 0;
463 sse2_qos_convert_value_to_exp_mant_fmt(hi_rate,
464 (uint16_t)SSE2_QOS_POL_RATE_EXP_MAX,
465 (uint16_t)SSE2_QOS_POL_AVG_RATE_MANT_MAX,
466 (sse2_qos_round_type_en) cfg->rnd_type,
471 if (hi_rate == eir_hw) {
472 hw->peak_rate_man = (uint16_t)hi_mant;
473 rc = sse2_qos_pol_round((uint64_t)cir_hw, denom, &rnd_value,
474 (sse2_qos_round_type_en) cfg->rnd_type);
475 hw->avg_rate_man = (uint16_t)rnd_value;
477 hw->avg_rate_man = (uint16_t)hi_mant;
478 rc = sse2_qos_pol_round((uint64_t)eir_hw, denom, &rnd_value,
479 (sse2_qos_round_type_en) cfg->rnd_type);
480 hw->peak_rate_man = (uint16_t)rnd_value;
482 if (CERR_IS_NOTOK(rc)) {
483 SSE2_QOS_DEBUG_ERROR("Rounding error");
489 if ((hw->avg_rate_man == 0) && (cfg->rb.kbps.cir_kbps)) {
491 * cir was reduced to 0 during rounding. Bump it up
493 hw->avg_rate_man = 1;
494 SSE2_QOS_DEBUG_INFO("CIR = 0 during rounding. Bump it up to %u "
495 "bytes/tick", (hw->avg_rate_man << hw->rate_exp));
498 if ((hw->peak_rate_man == 0) && eir_kbps) {
500 * eir was reduced to 0 during rounding. Bump it up
502 hw->peak_rate_man = 1;
503 SSE2_QOS_DEBUG_INFO("EIR = 0 during rounding. Bump it up to %u "
504 "bytes/tick", (hw->peak_rate_man << hw->rate_exp));
507 cir_rnded = (hw->avg_rate_man << hw->rate_exp);
508 eir_rnded = (hw->peak_rate_man << hw->rate_exp);
510 SSE2_QOS_DEBUG_INFO("Configured(rounded) values, cir: %u "
511 "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
512 cfg->rb.kbps.cir_kbps, hw->avg_rate_man,
513 hw->rate_exp, cir_rnded);
515 SSE2_QOS_DEBUG_INFO("Configured(rounded) values, eir: %u "
516 "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
517 cfg->rb.kbps.eir_kbps, hw->peak_rate_man,
518 hw->rate_exp, eir_rnded);
525 * sse2_pol_get_bkt_max
528 * rate_hw - either the averate rate or peak rate
529 * bkt_max - bit width in the current bucket or extended bucket
532 * uint64_t - maximum token bytes for the current or extended bucket
535 * The current bucket or extended bucket fields are in units of either
536 * 1,2,4,8 bytes based on the average or peak rate respective to current
537 * or extended bucket.
539 * To get the actual maximum number of bytes that can be stored in the
540 * field, the value must be multiplied by the units of either 1,2,4,8
541 * bytes based on the rate.
544 sse2_pol_get_bkt_max (uint64_t rate_hw, uint64_t bkt_max)
546 if (rate_hw <= RATE64) {
547 return (bkt_max - 1);
548 } else if (rate_hw <= RATE128) {
549 return ((bkt_max * RATE_64TO128_UNIT) - RATE_64TO128_UNIT);
550 } else if (rate_hw <= RATE256) {
551 return ((bkt_max * RATE_128TO256_UNIT) - RATE_128TO256_UNIT);
553 /* rate must be over 256 */
554 return ((bkt_max * RATE_OVER256_UNIT) - RATE_OVER256_UNIT);
559 * sse2_pol_get_bkt_value
562 * rate_hw - either the averate rate or peak rate
563 * byte_value - bytes for this token bucket
566 * uint64_t - unit value for the current or extended bucket field
569 * The current bucket or extended bucket fields are in units of either
570 * 1,2,4,8 bytes based on the average or peak rate respective to current
571 * or extended bucket.
573 * To get the units that can be stored in the field, the byte value must
574 * be divided by the units of either 1,2,4,8 bytes based on the rate.
577 sse2_pol_get_bkt_value (uint64_t rate_hw, uint64_t byte_value)
579 if (rate_hw <= RATE64) {
581 } else if (rate_hw <= RATE128) {
582 return (byte_value / RATE_64TO128_UNIT);
583 } else if (rate_hw <= RATE256) {
584 return (byte_value / RATE_128TO256_UNIT);
586 /* rate must be over 256 */
587 return (byte_value / RATE_OVER256_UNIT);
591 sse2_pol_rnd_burst_byte_fmt (uint64_t cfg_burst,
592 uint16_t max_exp_value,
593 uint16_t max_mant_value,
594 uint32_t max_bkt_value,
600 uint64_t bkt_max=max_bkt_value;
601 uint64_t bkt_limit_max;
603 uint64_t temp_bkt_value;
605 bkt_limit_max = ((uint64_t)max_mant_value<<(uint64_t)max_exp_value);
606 bkt_max = sse2_pol_get_bkt_max(rate_hw, bkt_max);
607 bkt_max=MIN(bkt_max, bkt_limit_max);
610 * If configured burst = 0, compute the burst to be 100ms at a given
611 * rate. Note that for rate_hw = 0, exp = mant = 0.
613 cfg_burst = (uint64_t)rate_hw * (uint64_t)SSE2_QOS_POL_DEF_BURST_BYTE;
616 if (cfg_burst > bkt_max) {
617 SSE2_QOS_DEBUG_ERROR("burst 0x%llx bytes is greater than the max. "
618 "supported value 0x%llx bytes. Capping it to the "
619 "max", cfg_burst, bkt_max);
620 SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_38,
621 (uint)cfg_burst, (uint)bkt_max);
625 if (cfg_burst < SSE2_QOS_POL_MIN_BURST_BYTE) {
627 * Bump up the burst value ONLY if the cfg_burst is non-zero AND
628 * less than the min. supported value
630 SSE2_QOS_DEBUG_INFO("burst 0x%llx bytes is less than the min "
631 "supported value %u bytes. Rounding it up to "
632 "the min", cfg_burst, SSE2_QOS_POL_MIN_BURST_BYTE);
633 SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_39, (uint)cfg_burst,
634 SSE2_QOS_POL_MIN_BURST_BYTE);
635 cfg_burst = SSE2_QOS_POL_MIN_BURST_BYTE;
638 sse2_qos_convert_value_to_exp_mant_fmt(cfg_burst,
641 SSE2_QOS_ROUND_TO_DOWN,
645 /* Bucket value is based on rate. */
646 rnd_burst = ((uint64_t)(*mant) << (uint64_t)(*exp));
647 temp_bkt_value = sse2_pol_get_bkt_value(rate_hw, rnd_burst);
648 *bkt_value = (uint32_t)temp_bkt_value;
652 sse2_pol_convert_cfg_burst_to_hw (sse2_qos_pol_cfg_params_st *cfg,
653 sse2_qos_pol_hw_params_st *hw)
656 uint32_t temp_mant, rate_hw;
661 * compute Committed Burst
663 SSE2_QOS_DEBUG_INFO("Compute commit burst ...");
664 rate_hw = (hw->avg_rate_man) << (hw->rate_exp);
665 sse2_pol_rnd_burst_byte_fmt(cfg->rb.kbps.cb_bytes,
666 (uint16_t)SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX,
667 (uint16_t)SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX,
668 (uint32_t)SSE2_QOS_POL_COMM_BKT_MAX,
669 rate_hw, &temp_exp, &temp_mant, &bkt_value);
670 SSE2_QOS_DEBUG_INFO("Committed burst, burst_limit: 0x%llx mant : %u, "
671 "exp: %u, rnded: 0x%llx cb:%u bytes",
672 cfg->rb.kbps.cb_bytes, temp_mant, temp_exp,
673 ((uint64_t)temp_mant << (uint64_t)temp_exp), bkt_value);
675 hw->comm_bkt_limit_exp = temp_exp;
676 hw->comm_bkt_limit_man = (uint8_t)temp_mant;
677 hw->comm_bkt = bkt_value;
680 * compute Exceed Burst
682 SSE2_QOS_DEBUG_INFO("Compute exceed burst ...");
684 if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) {
686 * For 1R2C, hw uses 2R3C (RFC-4115). As such, the Exceed Bucket
687 * params are set to 0. Recommendation is to use EB_exp = max_exp (=15)
690 hw->extd_bkt_limit_exp = (uint8_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX;
691 hw->extd_bkt_limit_man = 0;
692 SSE2_QOS_DEBUG_INFO("Excess burst, burst: 0x%llx mant: %u, "
693 "exp: %u, rnded: 0x%llx bytes",
694 cfg->rb.kbps.eb_bytes, hw->extd_bkt_limit_man,
695 hw->extd_bkt_limit_exp,
696 ((uint64_t)hw->extd_bkt_limit_man <<
697 (uint64_t)hw->extd_bkt_limit_exp));
698 SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_20, (uint)cfg->rb.kbps.eb_bytes,
699 hw->extd_bkt_limit_man, hw->extd_bkt_limit_exp);
703 if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
704 eb_bytes = cfg->rb.kbps.cb_bytes + cfg->rb.kbps.eb_bytes;
705 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
706 eb_bytes = cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes;
708 eb_bytes = cfg->rb.kbps.eb_bytes;
711 rate_hw = (hw->peak_rate_man) << (hw->rate_exp);
712 sse2_pol_rnd_burst_byte_fmt(eb_bytes,
713 (uint16_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX,
714 (uint16_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX,
715 (uint32_t)SSE2_QOS_POL_EXTD_BKT_MAX,
716 rate_hw, &temp_exp, &temp_mant, &bkt_value);
718 SSE2_QOS_DEBUG_INFO("Excess burst, burst_limit: 0x%llx mant: %u, "
719 "exp: %u, rnded: 0x%llx eb:%u bytes",
720 cfg->rb.kbps.eb_bytes, temp_mant, temp_exp,
721 ((uint64_t)temp_mant << (uint64_t)temp_exp), bkt_value);
723 hw->extd_bkt_limit_exp = (uint8_t)temp_exp;
724 hw->extd_bkt_limit_man = (uint8_t)temp_mant;
725 hw->extd_bkt = bkt_value;
732 * Input: configured parameter values in 'cfg'.
733 * Output: h/w programmable parameter values in 'hw'.
734 * Return: success or failure code.
737 sse2_pol_convert_cfg_to_hw_params (sse2_qos_pol_cfg_params_st *cfg,
738 sse2_qos_pol_hw_params_st *hw)
743 * clear the hw_params
745 memset(hw, 0, sizeof(sse2_qos_pol_hw_params_st));
747 hw->allow_negative = SSE2_QOS_POL_ALLOW_NEGATIVE;
749 if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) ||
750 (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)) {
751 hw->rfc = IPE_RFC_RFC4115;
752 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
753 hw->rfc = IPE_RFC_RFC2697;
754 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) {
755 hw->rfc = IPE_RFC_RFC2698;
756 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1) {
757 hw->rfc = IPE_RFC_MEF5CF1;
759 SSE2_QOS_DEBUG_ERROR("Invalid RFC type %d\n", cfg->rfc);
760 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_61, cfg->rfc);
764 rc = sse2_pol_convert_cfg_rates_to_hw(cfg, hw);
765 if (CERR_IS_NOTOK(rc)) {
766 SSE2_QOS_DEBUG_ERROR("Unable to convert config rates to hw. Error: %d",
772 rc = sse2_pol_convert_cfg_burst_to_hw(cfg, hw);
773 if (CERR_IS_NOTOK(rc)) {
774 SSE2_QOS_DEBUG_ERROR("Unable to convert config burst to hw. Error: %d",
785 sse2_qos_convert_pps_to_kbps (uint32_t rate_pps)
787 // sse2_qos_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
788 // SSE2_QOS_SHIP_CNT_POL_CONV_PPS_TO_KBPS);
790 uint64_t numer, rnd_value = 0;
792 numer = (uint64_t)((uint64_t)rate_pps *
793 (uint64_t)SSE2_QOS_POLICER_FIXED_PKT_SIZE * 8LL);
794 (void)sse2_qos_pol_round(numer, 1000LL, &rnd_value,
795 SSE2_QOS_ROUND_TO_CLOSEST);
797 return ((uint32_t)rnd_value);
801 sse2_qos_convert_burst_ms_to_bytes (uint32_t burst_ms,
804 uint64_t numer, rnd_value = 0;
806 //sse2_qos_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
807 // SSE2_QOS_SHIP_CNT_POL_CONV_BURST_MS_TO_BYTES);
809 numer = (uint64_t)((uint64_t)burst_ms * (uint64_t)rate_kbps);
811 (void)sse2_qos_pol_round(numer, 8LL, &rnd_value,
812 SSE2_QOS_ROUND_TO_CLOSEST);
814 return ((uint32_t)rnd_value);
819 * Input: configured parameters in 'cfg'.
820 * Output: h/w parameters are returned in 'hw',
821 * Return: Status, success or failure code.
824 sse2_pol_compute_hw_params (sse2_qos_pol_cfg_params_st *cfg,
825 sse2_qos_pol_hw_params_st *hw)
830 SSE2_QOS_DEBUG_ERROR("Illegal parameters");
835 * Validate the police config params being presented to RM
837 rc = sse2_pol_validate_cfg_params(cfg);
838 if (CERR_IS_NOTOK(rc)) {
839 SSE2_QOS_DEBUG_ERROR("Config parameter validation failed. Error: %d",
846 * first round configured values to h/w supported values. This func
847 * also determines whether 'tick' or 'byte' format
849 rc = sse2_pol_convert_cfg_to_hw_params(cfg, hw);
850 if (CERR_IS_NOTOK(rc)) {
851 SSE2_QOS_DEBUG_ERROR("Unable to convert config params to hw params. "
853 SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_53, rc);
861 #if defined (INTERNAL_SS) || defined (X86)
863 // For initializing the x86 policer format
866 * Return the number of hardware TSC timer ticks per second for the dataplane.
867 * This is approximately, but not exactly, the clock speed.
869 static uint64_t get_tsc_hz(void)
873 cpu_freq = os_cpu_clock_frequency();
874 return (uint64_t) cpu_freq;
878 * Convert rates into bytes_per_period and scale.
879 * Return 0 if ok or 1 if error.
882 compute_policer_params (uint64_t hz, // CPU speed in clocks per second
883 uint64_t cir_rate, // in bytes per second
884 uint64_t pir_rate, // in bytes per second
885 uint32_t *current_limit, // in bytes, output may scale the input
886 uint32_t *extended_limit, // in bytes, output may scale the input
887 uint32_t *cir_bytes_per_period,
888 uint32_t *pir_bytes_per_period,
892 double internal_cir_bytes_per_period;
893 double internal_pir_bytes_per_period;
895 uint32_t scale_shift;
896 uint32_t scale_amount;
897 uint32_t orig_current_limit = *current_limit;
899 // Compute period. For 1Ghz-to-8Ghz CPUs, the period will be in
900 // the range of 16 to 116 usec.
901 period = ((double) hz) / ((double) POLICER_TICKS_PER_PERIOD);
903 // Determine bytes per period for each rate
904 internal_cir_bytes_per_period = (double)cir_rate / period;
905 internal_pir_bytes_per_period = (double)pir_rate / period;
907 // Scale if possible. Scaling helps rate accuracy, but is contrained
908 // by the scaled rates and limits fitting in 32-bits.
909 // In addition, we need to insure the scaled rate is no larger than
910 // 2^22 tokens per period. This allows the dataplane to ignore overflow
911 // in the tokens-per-period multiplication since it could only
912 // happen if the policer were idle for more than a year.
913 // This is not really a constraint because 100Gbps at 1Ghz is only
914 // 1.6M tokens per period.
915 #define MAX_RATE_SHIFT 10
916 max = MAX(*current_limit, *extended_limit);
917 max = MAX(max, (uint32_t)internal_cir_bytes_per_period << MAX_RATE_SHIFT);
918 max = MAX(max, (uint32_t)internal_pir_bytes_per_period << MAX_RATE_SHIFT);
919 scale_shift = __builtin_clz(max);
921 scale_amount = 1 << scale_shift;
922 *scale = scale_shift;
925 *current_limit = *current_limit << scale_shift;
926 *extended_limit = *extended_limit << scale_shift;
929 internal_cir_bytes_per_period = internal_cir_bytes_per_period * ((double)scale_amount);
930 internal_pir_bytes_per_period = internal_pir_bytes_per_period * ((double)scale_amount);
932 // Make sure the new rates are reasonable
933 // Only needed for very low rates with large bursts
934 if (internal_cir_bytes_per_period < 1.0) {
935 internal_cir_bytes_per_period = 1.0;
937 if (internal_pir_bytes_per_period < 1.0) {
938 internal_pir_bytes_per_period = 1.0;
941 *cir_bytes_per_period = (uint32_t)internal_cir_bytes_per_period;
942 *pir_bytes_per_period = (uint32_t)internal_pir_bytes_per_period;
944 // #define PRINT_X86_POLICE_PARAMS
945 #ifdef PRINT_X86_POLICE_PARAMS
947 uint64_t effective_BPS;
949 // This value actually slightly conservative because it doesn't take into account
950 // the partial period at the end of a second. This really matters only for very low
952 effective_BPS = (((uint64_t) (*cir_bytes_per_period * (uint64_t)period)) >> *scale );
954 printf("hz=%llu, cir_rate=%llu, limit=%u => "
955 "periods-per-sec=%d usec-per-period=%d => "
956 "scale=%d cir_BPP=%u, scaled_limit=%u => "
957 "effective BPS=%llu, accuracy=%f\n",
959 (unsigned long long)hz,
960 (unsigned long long)cir_rate,
963 (uint32_t)(period), // periods per second
964 (uint32_t)(1000.0 * 1000.0 / period), // in usec
966 *cir_bytes_per_period,
969 (unsigned long long)effective_BPS,
970 (double)cir_rate / (double)effective_BPS);
973 orig_current_limit = orig_current_limit; // Make compiler happy
981 * Input: configured parameters in 'cfg'.
982 * Output: h/w parameters are returned in 'hw',
983 * Return: Status, success or failure code.
986 x86_pol_compute_hw_params (sse2_qos_pol_cfg_params_st *cfg,
987 policer_read_response_type_st *hw)
989 const int BYTES_PER_KBIT = (1000 / 8);
994 SSE2_QOS_DEBUG_ERROR("Illegal parameters");
999 hw->last_update_time = 0;
1001 // Cap the bursts to 32-bits. This allows up to almost one second of
1002 // burst on a 40GE interface, which should be fine for x86.
1003 cap = (cfg->rb.kbps.cb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.cb_bytes;
1004 hw->current_limit = cap;
1005 cap = (cfg->rb.kbps.eb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.eb_bytes;
1006 hw->extended_limit = cap;
1008 if ((cfg->rb.kbps.cir_kbps == 0) && (cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0)) {
1009 // This is a uninitialized, always-violate policer
1010 hw->single_rate = 1;
1011 hw->cir_tokens_per_period = 0;
1015 if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) ||
1016 (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)) {
1017 // Single-rate policer
1019 hw->single_rate = 1;
1021 if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) {
1022 SSE2_QOS_DEBUG_ERROR("Policer parameter validation failed -- 1R2C.");
1026 if ((cfg->rb.kbps.cir_kbps == 0) ||
1027 (cfg->rb.kbps.eir_kbps != 0) ||
1028 ((cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0))) {
1029 SSE2_QOS_DEBUG_ERROR("Policer parameter validation failed -- 1R.");
1033 if (compute_policer_params(hz,
1034 (uint64_t)cfg->rb.kbps.cir_kbps * BYTES_PER_KBIT,
1037 &hw->extended_limit,
1038 &hw->cir_tokens_per_period,
1039 &hw->pir_tokens_per_period,
1041 SSE2_QOS_DEBUG_ERROR("Policer parameter computation failed.");
1045 } else if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) ||
1046 (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)) {
1049 if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
1050 hw->color_aware = 1;
1053 if ((cfg->rb.kbps.cir_kbps == 0) || (cfg->rb.kbps.eir_kbps == 0) || (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps) ||
1054 (cfg->rb.kbps.cb_bytes == 0) || (cfg->rb.kbps.eb_bytes == 0)) {
1055 SSE2_QOS_DEBUG_ERROR("Config parameter validation failed.");
1059 if (compute_policer_params(hz,
1060 (uint64_t)cfg->rb.kbps.cir_kbps * BYTES_PER_KBIT,
1061 (uint64_t)cfg->rb.kbps.eir_kbps * BYTES_PER_KBIT,
1063 &hw->extended_limit,
1064 &hw->cir_tokens_per_period,
1065 &hw->pir_tokens_per_period,
1067 SSE2_QOS_DEBUG_ERROR("Policer parameter computation failed.");
1072 SSE2_QOS_DEBUG_ERROR("Config parameter validation failed. RFC not supported");
1076 hw->current_bucket = hw->current_limit;
1077 hw->extended_bucket = hw->extended_limit;
1085 * Input: configured parameters in 'cfg'.
1086 * Output: physical structure is returned in 'phys',
1087 * Return: Status, success or failure code.
1090 sse2_pol_logical_2_physical (sse2_qos_pol_cfg_params_st *cfg,
1091 policer_read_response_type_st *phys)
1094 sse2_qos_pol_hw_params_st pol_hw;
1095 sse2_qos_pol_cfg_params_st kbps_cfg;
1097 memset(phys, 0, sizeof(policer_read_response_type_st));
1098 memset(&kbps_cfg, 0, sizeof(sse2_qos_pol_cfg_params_st));
1101 SSE2_QOS_DEBUG_ERROR("Illegal parameters");
1105 switch (cfg->rate_type) {
1106 case SSE2_QOS_RATE_KBPS:
1107 /* copy all the data into kbps_cfg */
1108 kbps_cfg.rb.kbps.cir_kbps = cfg->rb.kbps.cir_kbps;
1109 kbps_cfg.rb.kbps.eir_kbps = cfg->rb.kbps.eir_kbps;
1110 kbps_cfg.rb.kbps.cb_bytes = cfg->rb.kbps.cb_bytes;
1111 kbps_cfg.rb.kbps.eb_bytes = cfg->rb.kbps.eb_bytes;
1113 case SSE2_QOS_RATE_PPS:
1114 kbps_cfg.rb.kbps.cir_kbps =
1115 sse2_qos_convert_pps_to_kbps(cfg->rb.pps.cir_pps);
1116 kbps_cfg.rb.kbps.eir_kbps =
1117 sse2_qos_convert_pps_to_kbps(cfg->rb.pps.eir_pps);
1118 kbps_cfg.rb.kbps.cb_bytes = sse2_qos_convert_burst_ms_to_bytes(
1119 (uint32_t) cfg->rb.pps.cb_ms, kbps_cfg.rb.kbps.cir_kbps);
1120 kbps_cfg.rb.kbps.eb_bytes = sse2_qos_convert_burst_ms_to_bytes(
1121 (uint32_t) cfg->rb.pps.eb_ms, kbps_cfg.rb.kbps.eir_kbps);
1124 SSE2_QOS_DEBUG_ERROR("Illegal rate type");
1128 /* rate type is now converted to kbps */
1129 kbps_cfg.rate_type = SSE2_QOS_RATE_KBPS;
1130 kbps_cfg.rnd_type = cfg->rnd_type;
1131 kbps_cfg.rfc = cfg->rfc;
1133 #if !defined (INTERNAL_SS) && !defined (X86)
1134 // convert logical into hw params which involves qos calculations
1135 rc = sse2_pol_compute_hw_params(&kbps_cfg, &pol_hw);
1137 SSE2_QOS_DEBUG_ERROR("Unable to compute hw param. Error: %d", rc);
1141 // convert hw params into the physical
1142 phys->rfc = pol_hw.rfc;
1143 phys->an = pol_hw.allow_negative;
1144 phys->rexp = pol_hw.rate_exp;
1145 phys->arm = pol_hw.avg_rate_man;
1146 phys->prm = pol_hw.peak_rate_man;
1147 phys->cble = pol_hw.comm_bkt_limit_exp;
1148 phys->cblm = pol_hw.comm_bkt_limit_man;
1149 phys->eble = pol_hw.extd_bkt_limit_exp;
1150 phys->eblm = pol_hw.extd_bkt_limit_man;
1151 phys->cb = pol_hw.comm_bkt;
1152 phys->eb = pol_hw.extd_bkt;
1154 /* for debugging purposes, the bucket token values can be overwritten */
1155 if (cfg->overwrite_bucket) {
1156 phys->cb = cfg->current_bucket;
1157 phys->eb = cfg->extended_bucket;
1160 // convert logical into hw params which involves qos calculations
1161 rc = x86_pol_compute_hw_params(&kbps_cfg, phys);
1163 SSE2_QOS_DEBUG_ERROR("Unable to compute hw param. Error: %d", rc);
1167 /* for debugging purposes, the bucket token values can be overwritten */
1168 if (cfg->overwrite_bucket) {
1169 phys->current_bucket = cfg->current_bucket;
1170 phys->extended_bucket = cfg->extended_bucket;
1173 // Touch to avoid compiler warning for X86
1174 pol_hw.allow_negative = pol_hw.allow_negative;
1176 #endif // if !defined (INTERNAL_SS) && !defined (X86)
1183 sse2_qos_convert_pol_bucket_to_hw_fmt (
1184 policer_read_response_type_st *bkt,
1185 sse2_qos_pol_hw_params_st *hw_fmt)
1187 memset(hw_fmt, 0, sizeof(sse2_qos_pol_hw_params_st));
1188 #if !defined (INTERNAL_SS) && !defined (X86)
1189 hw_fmt->rfc = (uint8_t)bkt->rfc;
1190 hw_fmt->allow_negative = (uint8_t)bkt->an;
1191 hw_fmt->rate_exp = (uint8_t)bkt->rexp;
1192 hw_fmt->avg_rate_man = (uint16_t)bkt->arm;
1193 hw_fmt->peak_rate_man = (uint16_t)bkt->prm;
1194 hw_fmt->comm_bkt_limit_man = (uint8_t)bkt->cblm;
1195 hw_fmt->comm_bkt_limit_exp = (uint8_t)bkt->cble;
1196 hw_fmt->extd_bkt_limit_man = (uint8_t)bkt->eblm;
1197 hw_fmt->extd_bkt_limit_exp = (uint8_t)bkt->eble;
1198 hw_fmt->extd_bkt = bkt->eb;
1199 hw_fmt->comm_bkt = bkt->cb;
1200 #endif // if !defined (INTERNAL_SS) && !defined (X86)
1204 * Input: h/w programmable parameter values in 'hw'
1205 * Output: configured parameter values in 'cfg'
1206 * Return: Status, success or failure code.
1209 sse2_pol_convert_hw_to_cfg_params (sse2_qos_pol_hw_params_st *hw,
1210 sse2_qos_pol_cfg_params_st *cfg)
1214 if ((hw == NULL) || (cfg == NULL)) {
1218 if ((hw->rfc == IPE_RFC_RFC4115) &&
1219 !(hw->peak_rate_man << hw->rate_exp) &&
1220 !(hw->extd_bkt_limit_man)) {
1222 * For a 1R2C, we set EIR = 0, EB = 0
1224 cfg->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
1225 } else if (hw->rfc == IPE_RFC_RFC2697) {
1226 cfg->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
1227 } else if (hw->rfc == IPE_RFC_RFC2698) {
1228 cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
1229 } else if (hw->rfc == IPE_RFC_RFC4115) {
1230 cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
1231 } else if (hw->rfc == IPE_RFC_MEF5CF1) {
1232 cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
1237 temp_rate = ((hw->avg_rate_man << hw->rate_exp) * 8LL *
1238 SSE2_QOS_POL_TICKS_PER_SEC)/1000;
1239 cfg->rb.kbps.cir_kbps = (uint32_t)temp_rate;
1241 temp_rate = ((hw->peak_rate_man << hw->rate_exp) * 8LL *
1242 SSE2_QOS_POL_TICKS_PER_SEC)/1000;
1243 cfg->rb.kbps.eir_kbps = (uint32_t)temp_rate;
1245 cfg->rb.kbps.cb_bytes = ((uint64_t)hw->comm_bkt_limit_man <<
1246 (uint64_t)hw->comm_bkt_limit_exp);
1247 cfg->rb.kbps.eb_bytes = ((uint64_t)hw->extd_bkt_limit_man <<
1248 (uint64_t)hw->extd_bkt_limit_exp);
1250 if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
1252 * For 1R3C in the hardware, EB = sum(CB, EB). Also, EIR = CIR. Restore
1253 * values such that the configured params don't reflect this adjustment
1255 cfg->rb.kbps.eb_bytes = (cfg->rb.kbps.eb_bytes -
1256 cfg->rb.kbps.cb_bytes);
1257 cfg->rb.kbps.eir_kbps = 0;
1258 } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
1260 * For 4115 in the hardware is excess rate and burst, but EA provides
1261 * peak-rate, so adjust it to be eir
1263 cfg->rb.kbps.eir_kbps += cfg->rb.kbps.cir_kbps;
1264 cfg->rb.kbps.eb_bytes += cfg->rb.kbps.cb_bytes;
1266 /* h/w conversion to cfg is in kbps */
1267 cfg->rate_type = SSE2_QOS_RATE_KBPS;
1268 cfg->overwrite_bucket = 0;
1269 cfg->current_bucket = hw->comm_bkt;
1270 cfg->extended_bucket = hw->extd_bkt;
1272 SSE2_QOS_DEBUG_INFO("configured params, cir: %u kbps, eir: %u kbps, cb "
1273 "burst: 0x%llx bytes, eb burst: 0x%llx bytes",
1274 cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps,
1275 cfg->rb.kbps.cb_bytes, cfg->rb.kbps.eb_bytes);
1276 SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_22, cfg->rb.kbps.cir_kbps,
1277 cfg->rb.kbps.eir_kbps,
1278 (uint)cfg->rb.kbps.cb_bytes, (uint)cfg->rb.kbps.eb_bytes);
1284 sse2_qos_convert_kbps_to_pps (uint32_t rate_kbps)
1286 uint64_t numer, denom, rnd_value = 0;
1288 // sse_qosrm_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
1289 // SSE2_QOS_SHIP_CNT_POL_CONV_KBPS_TO_PPS);
1291 numer = (uint64_t)((uint64_t)rate_kbps * 1000LL);
1292 denom = (uint64_t)((uint64_t)SSE2_QOS_POLICER_FIXED_PKT_SIZE * 8LL);
1294 (void)sse2_qos_pol_round(numer, denom, &rnd_value,
1295 SSE2_QOS_ROUND_TO_CLOSEST);
1297 return((uint32_t)rnd_value);
1301 sse2_qos_convert_burst_bytes_to_ms (uint64_t burst_bytes,
1304 uint64_t numer, denom, rnd_value = 0;
1306 //sse_qosrm_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
1307 // SSE2_QOS_SHIP_CNT_POL_CONV_BYTES_TO_BURST_MS);
1309 numer = burst_bytes * 8LL;
1310 denom = (uint64_t)rate_kbps;
1312 (void)sse2_qos_pol_round(numer, denom, &rnd_value,
1313 SSE2_QOS_ROUND_TO_CLOSEST);
1315 return((uint32_t)rnd_value);
1319 * Input: physical structure in 'phys', rate_type in cfg
1320 * Output: configured parameters in 'cfg'.
1321 * Return: Status, success or failure code.
1324 sse2_pol_physical_2_logical (policer_read_response_type_st *phys,
1325 sse2_qos_pol_cfg_params_st *cfg)
1328 sse2_qos_pol_hw_params_st pol_hw;
1329 sse2_qos_pol_cfg_params_st kbps_cfg;
1331 memset(&pol_hw, 0, sizeof(sse2_qos_pol_hw_params_st));
1332 memset(&kbps_cfg, 0, sizeof(sse2_qos_pol_cfg_params_st));
1335 SSE2_QOS_DEBUG_ERROR("Illegal parameters");
1339 sse2_qos_convert_pol_bucket_to_hw_fmt (phys, &pol_hw);
1341 rc = sse2_pol_convert_hw_to_cfg_params(&pol_hw, &kbps_cfg);
1342 if (CERR_IS_NOTOK(rc)) {
1343 SSE2_QOS_DEBUG_ERROR("Unable to convert hw params to config params. "
1348 /* check what rate type is required */
1349 switch (cfg->rate_type) {
1350 case SSE2_QOS_RATE_KBPS:
1351 /* copy all the data into kbps_cfg */
1352 cfg->rb.kbps.cir_kbps = kbps_cfg.rb.kbps.cir_kbps;
1353 cfg->rb.kbps.eir_kbps = kbps_cfg.rb.kbps.eir_kbps;
1354 cfg->rb.kbps.cb_bytes = kbps_cfg.rb.kbps.cb_bytes;
1355 cfg->rb.kbps.eb_bytes = kbps_cfg.rb.kbps.eb_bytes;
1357 case SSE2_QOS_RATE_PPS:
1358 cfg->rb.pps.cir_pps =
1359 sse2_qos_convert_kbps_to_pps(kbps_cfg.rb.kbps.cir_kbps);
1360 cfg->rb.pps.eir_pps =
1361 sse2_qos_convert_kbps_to_pps(kbps_cfg.rb.kbps.eir_kbps);
1362 cfg->rb.pps.cb_ms = sse2_qos_convert_burst_bytes_to_ms(
1363 kbps_cfg.rb.kbps.cb_bytes, kbps_cfg.rb.kbps.cir_kbps);
1364 cfg->rb.pps.eb_ms = sse2_qos_convert_burst_bytes_to_ms(
1365 kbps_cfg.rb.kbps.eb_bytes, kbps_cfg.rb.kbps.eir_kbps);
1368 SSE2_QOS_DEBUG_ERROR("Illegal rate type");
1372 /* cfg->rate_type remains what it was */
1373 cfg->rnd_type = kbps_cfg.rnd_type;
1374 cfg->rfc = kbps_cfg.rfc;
1375 cfg->overwrite_bucket = kbps_cfg.overwrite_bucket;
1376 cfg->current_bucket = kbps_cfg.current_bucket;
1377 cfg->extended_bucket = kbps_cfg.extended_bucket;