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.
22 #include <vlib/vlib.h>
23 #include <vnet/vnet.h>
25 #include <vnet/policer/policer.h>
28 #define QOS_DEBUG_ERROR(msg, args...) \
29 vlib_log_err (vnet_policer_main.log_class, msg, ##args);
31 #define QOS_DEBUG_INFO(msg, args...) \
32 vlib_log_info (vnet_policer_main.log_class, msg, ##args);
35 #define MIN(x,y) (((x)<(y))?(x):(y))
39 #define MAX(x,y) (((x)>(y))?(x):(y))
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
102 #define IPE_RFC_RFC2697 0x00000000
103 #define IPE_RFC_RFC2698 0x00000001
104 #define IPE_RFC_RFC4115 0x00000002
105 #define IPE_RFC_MEF5CF1 0x00000003
107 /* End of constants copied from sse_ipe_desc_fmt.h */
109 /* Misc Policer specific definitions */
110 #define QOS_POLICER_FIXED_PKT_SIZE 256
112 #define QOS_POL_TICKS_PER_SEC 1000LL /* 1 tick = 1 ms */
115 * Default burst, in ms (byte format)
117 #define QOS_POL_DEF_BURST_BYTE 100
120 * Minimum burst needs to be such that the largest packet size is accommodated
122 #define QOS_POL_MIN_BURST_BYTE 9 * 1024
125 * Flag to indicate if AN is employed or not
126 * 1 - TRUE, 0 - FALSE
128 #define QOS_POL_ALLOW_NEGATIVE 1
130 /* Various Macros to take care of policer calculations */
132 #define QOS_POL_COMM_BKT_MAX (1 << IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK)
133 #define QOS_POL_EXTD_BKT_MAX (1 << IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK)
135 #define QOS_POL_RATE_EXP_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK)
136 #define QOS_POL_RATE_EXP_MAX ((1 << QOS_POL_RATE_EXP_SIZE) - 1)
137 #define QOS_POL_AVG_RATE_MANT_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK)
138 #define QOS_POL_AVG_RATE_MANT_MAX ((1 << QOS_POL_AVG_RATE_MANT_SIZE) - 1)
139 #define QOS_POL_AVG_RATE_MAX \
140 (QOS_POL_AVG_RATE_MANT_MAX << QOS_POL_RATE_EXP_MAX)
142 #define QOS_POL_PEAK_RATE_MANT_SIZE (IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK)
143 #define QOS_POL_PEAK_RATE_MANT_MAX ((1 << QOS_POL_PEAK_RATE_MANT_SIZE) - 1)
144 #define QOS_POL_PEAK_RATE_MAX \
145 (QOS_POL_PEAK_RATE_MANT_MAX << QOS_POL_RATE_EXP_MAX)
147 #define QOS_POL_COMM_BKT_LIMIT_MANT_SIZE \
148 (IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK)
149 #define QOS_POL_COMM_BKT_LIMIT_MANT_MAX \
150 ((1 << QOS_POL_COMM_BKT_LIMIT_MANT_SIZE) - 1)
151 #define QOS_POL_COMM_BKT_LIMIT_EXP_SIZE \
152 (IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK)
153 #define QOS_POL_COMM_BKT_LIMIT_EXP_MAX \
154 ((1 << QOS_POL_COMM_BKT_LIMIT_EXP_SIZE) - 1)
155 #define QOS_POL_COMM_BKT_LIMIT_MAX \
156 ((u64) QOS_POL_COMM_BKT_LIMIT_MANT_MAX \
157 << (u64) QOS_POL_COMM_BKT_LIMIT_EXP_MAX)
159 #define QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE \
160 (IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK)
161 #define QOS_POL_EXTD_BKT_LIMIT_MANT_MAX \
162 ((1 << QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE) - 1)
163 #define QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE \
164 (IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK)
165 #define QOS_POL_EXTD_BKT_LIMIT_EXP_MAX \
166 ((1 << QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE) - 1)
167 #define QOS_POL_EXT_BKT_LIMIT_MAX \
168 ((u64) QOS_POL_EXTD_BKT_LIMIT_MANT_MAX \
169 << (u64) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX)
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
178 * The code uses bytes per tick as oppose to Gigabits per second.
180 #define RATE256 (256114688000LL / 8LL / QOS_POL_TICKS_PER_SEC)
181 #define RATE128 (128057344000LL / 8LL / QOS_POL_TICKS_PER_SEC)
182 #define RATE64 (64028672000LL / 8LL / QOS_POL_TICKS_PER_SEC)
184 #define RATE_OVER256_UNIT 8LL
185 #define RATE_128TO256_UNIT 4LL
186 #define RATE_64TO128_UNIT 2LL
189 qos_pol_round (u64 numerator, u64 denominator, u64 *rounded_value,
190 qos_round_type_en round_type)
194 if (denominator == 0)
196 QOS_DEBUG_ERROR ("Illegal denominator");
202 case QOS_ROUND_TO_CLOSEST:
203 *rounded_value = ((numerator + (denominator >> 1)) / denominator);
206 case QOS_ROUND_TO_UP:
207 *rounded_value = (numerator / denominator);
208 if ((*rounded_value * denominator) < numerator)
214 case QOS_ROUND_TO_DOWN:
215 *rounded_value = (numerator / denominator);
218 case QOS_ROUND_INVALID:
220 QOS_DEBUG_ERROR ("Illegal round type");
228 pol_validate_cfg_params (qos_pol_cfg_params_st *cfg)
230 u64 numer, denom, rnd_value;
234 if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) &&
235 (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps))
237 QOS_DEBUG_ERROR ("CIR (%u kbps) is greater than PIR (%u kbps)",
238 cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps);
243 * convert rates to bytes-per-tick
245 numer = (u64) (cfg->rb.kbps.cir_kbps);
246 denom = (u64) (8 * QOS_POL_TICKS_PER_SEC) / 1000;
247 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
250 QOS_DEBUG_ERROR ("Unable to convert CIR to bytes/tick format");
253 cir_hw = (u32) rnd_value;
255 numer = (u64) (cfg->rb.kbps.eir_kbps);
256 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
259 QOS_DEBUG_ERROR ("Unable to convert EIR to bytes/tick format");
262 eir_hw = (u32) rnd_value;
264 if (cir_hw > QOS_POL_AVG_RATE_MAX)
266 QOS_DEBUG_ERROR ("hw cir (%u bytes/tick) is greater than the "
267 "max supported value (%u)",
268 cir_hw, QOS_POL_AVG_RATE_MAX);
272 if (eir_hw > QOS_POL_PEAK_RATE_MAX)
274 QOS_DEBUG_ERROR ("hw eir (%u bytes/tick) is greater than the "
275 "max supported value (%u). Capping it to the max. "
277 eir_hw, QOS_POL_PEAK_RATE_MAX);
281 * CIR = 0, with bc != 0 is not allowed
283 if ((cfg->rb.kbps.cir_kbps == 0) && cfg->rb.kbps.cb_bytes)
285 QOS_DEBUG_ERROR ("CIR = 0 with bc != 0");
289 if ((cfg->rb.kbps.eir_kbps == 0) &&
290 (cfg->rfc > QOS_POLICER_TYPE_1R3C_RFC_2697))
292 QOS_DEBUG_ERROR ("EIR = 0 for a 2R3C policer (rfc: %u)", cfg->rfc);
296 if (cfg->rb.kbps.eir_kbps && (cfg->rfc < QOS_POLICER_TYPE_2R3C_RFC_2698))
298 QOS_DEBUG_ERROR ("EIR: %u kbps for a 1-rate policer (rfc: %u)",
299 cfg->rb.kbps.eir_kbps, cfg->rfc);
303 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes)
305 QOS_DEBUG_ERROR ("For a 1R1B policer, EB burst cannot be > 0");
313 qos_convert_value_to_exp_mant_fmt (u64 value, u16 max_exp_value,
314 u16 max_mant_value, qos_round_type_en type,
322 * Select the lowest possible exp, and the largest possible mant
326 while (temp_exp <= max_exp_value)
328 if (temp_mant <= max_mant_value)
335 (void) qos_pol_round ((u64) value, (u64) (1 << temp_exp), &rnd_value,
337 temp_mant = rnd_value;
340 if (temp_exp > max_exp_value)
343 * CAP mant to its max value, and decrement exp
346 temp_mant = max_mant_value;
350 *mant = (u32) temp_mant;
352 QOS_DEBUG_INFO ("value: 0x%llx, mant: %u, exp: %u", value, *mant, *exp);
357 pol_convert_cfg_rates_to_hw (qos_pol_cfg_params_st *cfg,
358 qos_pol_hw_params_st *hw)
361 u32 cir_hw, eir_hw, hi_mant, hi_rate, cir_rnded, eir_rnded, eir_kbps;
362 u64 numer, denom, rnd_value;
366 * convert rates to bytes-per-tick (tick is 1ms)
367 * For rate conversion, the denominator is gonna be the same
369 denom = (u64) ((QOS_POL_TICKS_PER_SEC * 8) / 1000);
370 numer = (u64) (cfg->rb.kbps.cir_kbps);
371 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
374 QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d",
375 cfg->rb.kbps.cir_kbps, cfg->rnd_type);
378 cir_hw = (u32) rnd_value;
380 if (cfg->rb.kbps.cir_kbps && (cir_hw == 0))
383 * After rounding, cir_hw = 0. Bump it up
388 if (cfg->rfc == QOS_POLICER_TYPE_1R2C)
392 else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
394 eir_kbps = cfg->rb.kbps.cir_kbps;
396 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
398 eir_kbps = cfg->rb.kbps.eir_kbps - cfg->rb.kbps.cir_kbps;
402 eir_kbps = cfg->rb.kbps.eir_kbps;
405 numer = (u64) eir_kbps;
406 rc = qos_pol_round (numer, denom, &rnd_value, cfg->rnd_type);
409 QOS_DEBUG_ERROR ("Rounding error, rate: %d kbps, rounding_type: %d",
410 eir_kbps, cfg->rnd_type);
413 eir_hw = (u32) rnd_value;
415 if (eir_kbps && (eir_hw == 0))
418 * After rounding, eir_hw = 0. Bump it up
423 QOS_DEBUG_INFO ("cir_hw: %u bytes/tick, eir_hw: %u bytes/tick", cir_hw,
435 if ((cir_hw == 0) && (eir_hw == 0))
438 * Both the rates are 0. Use exp = 15, and set the RFC to 4115. Also
441 exp = (u8) QOS_POL_RATE_EXP_MAX;
443 hw->rfc = IPE_RFC_RFC4115;
444 hw->allow_negative = 0;
448 qos_convert_value_to_exp_mant_fmt (hi_rate, (u16) QOS_POL_RATE_EXP_MAX,
449 (u16) QOS_POL_AVG_RATE_MANT_MAX,
450 cfg->rnd_type, &exp, &hi_mant);
453 denom = (1ULL << exp);
454 if (hi_rate == eir_hw)
456 hw->peak_rate_man = (u16) hi_mant;
457 rc = qos_pol_round ((u64) cir_hw, denom, &rnd_value, cfg->rnd_type);
458 hw->avg_rate_man = (u16) rnd_value;
462 hw->avg_rate_man = (u16) hi_mant;
463 rc = qos_pol_round ((u64) eir_hw, denom, &rnd_value, cfg->rnd_type);
464 hw->peak_rate_man = (u16) rnd_value;
468 QOS_DEBUG_ERROR ("Rounding error");
473 if ((hw->avg_rate_man == 0) && (cfg->rb.kbps.cir_kbps))
476 * cir was reduced to 0 during rounding. Bump it up
478 hw->avg_rate_man = 1;
479 QOS_DEBUG_INFO ("CIR = 0 during rounding. Bump it up to %u "
481 (hw->avg_rate_man << hw->rate_exp));
484 if ((hw->peak_rate_man == 0) && eir_kbps)
487 * eir was reduced to 0 during rounding. Bump it up
489 hw->peak_rate_man = 1;
490 QOS_DEBUG_INFO ("EIR = 0 during rounding. Bump it up to %u "
492 (hw->peak_rate_man << hw->rate_exp));
495 cir_rnded = (hw->avg_rate_man << hw->rate_exp);
496 eir_rnded = (hw->peak_rate_man << hw->rate_exp);
498 QOS_DEBUG_INFO ("Configured(rounded) values, cir: %u "
499 "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
500 cfg->rb.kbps.cir_kbps, hw->avg_rate_man, hw->rate_exp,
503 QOS_DEBUG_INFO ("Configured(rounded) values, eir: %u "
504 "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
505 cfg->rb.kbps.eir_kbps, hw->peak_rate_man, hw->rate_exp,
516 * rate_hw - either the average rate or peak rate
517 * bkt_max - bit width in the current bucket or extended bucket
520 * u64 - maximum token bytes for the current or extended bucket
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.
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.
532 pol_get_bkt_max (u64 rate_hw, u64 bkt_max)
534 if (rate_hw <= RATE64)
536 return (bkt_max - 1);
538 else if (rate_hw <= RATE128)
540 return ((bkt_max * RATE_64TO128_UNIT) - RATE_64TO128_UNIT);
542 else if (rate_hw <= RATE256)
544 return ((bkt_max * RATE_128TO256_UNIT) - RATE_128TO256_UNIT);
546 /* rate must be over 256 */
547 return ((bkt_max * RATE_OVER256_UNIT) - RATE_OVER256_UNIT);
555 * rate_hw - either the average rate or peak rate
556 * byte_value - bytes for this token bucket
559 * u64 - unit value for the current or extended bucket field
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.
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.
570 pol_get_bkt_value (u64 rate_hw, u64 byte_value)
572 if (rate_hw <= RATE64)
576 else if (rate_hw <= RATE128)
578 return (byte_value / RATE_64TO128_UNIT);
580 else if (rate_hw <= RATE256)
582 return (byte_value / RATE_128TO256_UNIT);
584 /* rate must be over 256 */
585 return (byte_value / RATE_OVER256_UNIT);
589 pol_rnd_burst_byte_fmt (u64 cfg_burst, u16 max_exp_value, u16 max_mant_value,
590 u32 max_bkt_value, u32 rate_hw, u8 *exp, u32 *mant,
593 u64 bkt_max = max_bkt_value;
598 bkt_limit_max = ((u64) max_mant_value << (u64) max_exp_value);
599 bkt_max = pol_get_bkt_max (rate_hw, bkt_max);
600 bkt_max = MIN (bkt_max, bkt_limit_max);
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.
607 cfg_burst = (u64) rate_hw * (u64) QOS_POL_DEF_BURST_BYTE;
610 if (cfg_burst > bkt_max)
612 QOS_DEBUG_ERROR ("burst 0x%llx bytes is greater than the max. "
613 "supported value 0x%llx bytes. Capping it to the "
619 if (cfg_burst < QOS_POL_MIN_BURST_BYTE)
622 * Bump up the burst value ONLY if the cfg_burst is non-zero AND
623 * less than the min. supported value
625 QOS_DEBUG_INFO ("burst 0x%llx bytes is less than the min "
626 "supported value %u bytes. Rounding it up to "
628 cfg_burst, QOS_POL_MIN_BURST_BYTE);
629 cfg_burst = QOS_POL_MIN_BURST_BYTE;
632 qos_convert_value_to_exp_mant_fmt (cfg_burst, max_exp_value, max_mant_value,
633 QOS_ROUND_TO_DOWN, exp, mant);
635 /* Bucket value is based on rate. */
636 rnd_burst = ((u64) (*mant) << (u64) (*exp));
637 temp_bkt_value = pol_get_bkt_value (rate_hw, rnd_burst);
638 *bkt_value = (u32) temp_bkt_value;
642 pol_convert_cfg_burst_to_hw (qos_pol_cfg_params_st *cfg,
643 qos_pol_hw_params_st *hw)
646 u32 temp_mant, rate_hw;
651 * compute Committed Burst
653 QOS_DEBUG_INFO ("Compute commit burst ...");
654 rate_hw = (hw->avg_rate_man) << (hw->rate_exp);
655 pol_rnd_burst_byte_fmt (
656 cfg->rb.kbps.cb_bytes, (u16) QOS_POL_COMM_BKT_LIMIT_EXP_MAX,
657 (u16) QOS_POL_COMM_BKT_LIMIT_MANT_MAX, (u32) QOS_POL_COMM_BKT_MAX, rate_hw,
658 &temp_exp, &temp_mant, &bkt_value);
659 QOS_DEBUG_INFO ("Committed burst, burst_limit: 0x%llx mant : %u, "
660 "exp: %u, rnded: 0x%llx cb:%u bytes",
661 cfg->rb.kbps.cb_bytes, temp_mant, temp_exp,
662 ((u64) temp_mant << (u64) temp_exp), bkt_value);
664 hw->comm_bkt_limit_exp = temp_exp;
665 hw->comm_bkt_limit_man = (u8) temp_mant;
666 hw->comm_bkt = bkt_value;
669 * compute Exceed Burst
671 QOS_DEBUG_INFO ("Compute exceed burst ...");
673 if (cfg->rfc == QOS_POLICER_TYPE_1R2C)
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)
680 hw->extd_bkt_limit_exp = (u8) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX;
681 hw->extd_bkt_limit_man = 0;
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));
690 if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
692 eb_bytes = cfg->rb.kbps.cb_bytes + cfg->rb.kbps.eb_bytes;
694 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
696 eb_bytes = cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes;
700 eb_bytes = cfg->rb.kbps.eb_bytes;
703 rate_hw = (hw->peak_rate_man) << (hw->rate_exp);
704 pol_rnd_burst_byte_fmt (eb_bytes, (u16) QOS_POL_EXTD_BKT_LIMIT_EXP_MAX,
705 (u16) QOS_POL_EXTD_BKT_LIMIT_MANT_MAX,
706 (u32) QOS_POL_EXTD_BKT_MAX, rate_hw, &temp_exp,
707 &temp_mant, &bkt_value);
709 QOS_DEBUG_INFO ("Excess burst, burst_limit: 0x%llx mant: %u, "
710 "exp: %u, rnded: 0x%llx eb:%u bytes",
711 cfg->rb.kbps.eb_bytes, temp_mant, temp_exp,
712 ((u64) temp_mant << (u64) temp_exp), bkt_value);
714 hw->extd_bkt_limit_exp = (u8) temp_exp;
715 hw->extd_bkt_limit_man = (u8) temp_mant;
716 hw->extd_bkt = bkt_value;
723 * Input: configured parameter values in 'cfg'.
724 * Output: h/w programmable parameter values in 'hw'.
725 * Return: success or failure code.
728 pol_convert_cfg_to_hw_params (qos_pol_cfg_params_st *cfg,
729 qos_pol_hw_params_st *hw)
734 * clear the hw_params
736 clib_memset (hw, 0, sizeof (qos_pol_hw_params_st));
738 hw->allow_negative = QOS_POL_ALLOW_NEGATIVE;
740 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) ||
741 (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115))
743 hw->rfc = IPE_RFC_RFC4115;
745 else if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
747 hw->rfc = IPE_RFC_RFC2697;
749 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698)
751 hw->rfc = IPE_RFC_RFC2698;
753 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1)
755 hw->rfc = IPE_RFC_MEF5CF1;
759 QOS_DEBUG_ERROR ("Invalid RFC type %d\n", cfg->rfc);
763 rc = pol_convert_cfg_rates_to_hw (cfg, hw);
766 QOS_DEBUG_ERROR ("Unable to convert config rates to hw. Error: %d", rc);
770 rc = pol_convert_cfg_burst_to_hw (cfg, hw);
773 QOS_DEBUG_ERROR ("Unable to convert config burst to hw. Error: %d", rc);
781 qos_convert_pps_to_kbps (u32 rate_pps)
783 u64 numer, rnd_value = 0;
785 numer = (u64) ((u64) rate_pps * (u64) QOS_POLICER_FIXED_PKT_SIZE * 8LL);
786 (void) qos_pol_round (numer, 1000LL, &rnd_value, QOS_ROUND_TO_CLOSEST);
788 return ((u32) rnd_value);
792 qos_convert_burst_ms_to_bytes (u32 burst_ms, u32 rate_kbps)
794 u64 numer, rnd_value = 0;
796 numer = (u64) ((u64) burst_ms * (u64) rate_kbps);
798 (void) qos_pol_round (numer, 8LL, &rnd_value, QOS_ROUND_TO_CLOSEST);
800 return ((u32) rnd_value);
805 * Input: configured parameters in 'cfg'.
806 * Output: h/w parameters are returned in 'hw',
807 * Return: Status, success or failure code.
810 pol_compute_hw_params (qos_pol_cfg_params_st *cfg, qos_pol_hw_params_st *hw)
816 QOS_DEBUG_ERROR ("Illegal parameters");
821 * Validate the police config params being presented to RM
823 rc = pol_validate_cfg_params (cfg);
826 QOS_DEBUG_ERROR ("Config parameter validation failed. Error: %d", rc);
831 * first round configured values to h/w supported values. This func
832 * also determines whether 'tick' or 'byte' format
834 rc = pol_convert_cfg_to_hw_params (cfg, hw);
837 QOS_DEBUG_ERROR ("Unable to convert config params to hw params. "
847 * Return the number of hardware TSC timer ticks per second for the dataplane.
848 * This is approximately, but not exactly, the clock speed.
855 cpu_freq = os_cpu_clock_frequency ();
856 return (u64) cpu_freq;
860 * Convert rates into bytes_per_period and scale.
861 * Return 0 if ok or 1 if error.
864 compute_policer_params (u64 hz, /* CPU speed in clocks per second */
865 u64 cir_rate, /* in bytes per second */
866 u64 pir_rate, /* in bytes per second */
867 u32 *current_limit, /* in bytes, output may scale
870 u32 *extended_limit, /* in bytes, output may scale
873 u32 *cir_bytes_per_period, u32 *pir_bytes_per_period,
877 double internal_cir_bytes_per_period;
878 double internal_pir_bytes_per_period;
882 u32 __attribute__ ((unused)) orig_current_limit = *current_limit;
885 * Compute period. For 1Ghz-to-8Ghz CPUs, the period will be in
886 * the range of 16 to 116 usec.
888 period = ((double) hz) / ((double) POLICER_TICKS_PER_PERIOD);
890 /* Determine bytes per period for each rate */
891 internal_cir_bytes_per_period = (double) cir_rate / period;
892 internal_pir_bytes_per_period = (double) pir_rate / period;
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.
904 #define MAX_RATE_SHIFT 10
905 max = MAX (*current_limit, *extended_limit);
906 max = MAX (max, (u32) internal_cir_bytes_per_period << MAX_RATE_SHIFT);
907 max = MAX (max, (u32) internal_pir_bytes_per_period << MAX_RATE_SHIFT);
908 scale_shift = __builtin_clz (max);
910 scale_amount = 1 << scale_shift;
911 *scale = scale_shift;
913 /* Scale the limits */
914 *current_limit = *current_limit << scale_shift;
915 *extended_limit = *extended_limit << scale_shift;
917 /* Scale the rates */
918 internal_cir_bytes_per_period =
919 internal_cir_bytes_per_period * ((double) scale_amount);
920 internal_pir_bytes_per_period =
921 internal_pir_bytes_per_period * ((double) scale_amount);
924 * Make sure the new rates are reasonable
925 * Only needed for very low rates with large bursts
927 if (internal_cir_bytes_per_period < 1.0)
929 internal_cir_bytes_per_period = 1.0;
931 if (internal_pir_bytes_per_period < 1.0)
933 internal_pir_bytes_per_period = 1.0;
936 *cir_bytes_per_period = (u32) internal_cir_bytes_per_period;
937 *pir_bytes_per_period = (u32) internal_pir_bytes_per_period;
944 * Input: configured parameters in 'cfg'.
945 * Output: h/w parameters are returned in 'hw',
946 * Return: Status, success or failure code.
949 x86_pol_compute_hw_params (qos_pol_cfg_params_st *cfg, policer_t *hw)
951 const int BYTES_PER_KBIT = (1000 / 8);
957 QOS_DEBUG_ERROR ("Illegal parameters");
962 hw->last_update_time = 0;
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.
969 (cfg->rb.kbps.cb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.cb_bytes;
970 hw->current_limit = cap;
972 (cfg->rb.kbps.eb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.eb_bytes;
973 hw->extended_limit = cap;
975 if ((cfg->rb.kbps.cir_kbps == 0) && (cfg->rb.kbps.cb_bytes == 0)
976 && (cfg->rb.kbps.eb_bytes == 0))
978 /* This is a uninitialized, always-violate policer */
980 hw->cir_tokens_per_period = 0;
984 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) ||
985 (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697))
987 /* Single-rate policer */
990 if ((cfg->rfc == QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes)
992 QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R2C.");
996 if ((cfg->rb.kbps.cir_kbps == 0) ||
997 (cfg->rb.kbps.eir_kbps != 0) ||
998 ((cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0)))
1000 QOS_DEBUG_ERROR ("Policer parameter validation failed -- 1R.");
1004 if (compute_policer_params (hz,
1005 (u64) cfg->rb.kbps.cir_kbps *
1006 BYTES_PER_KBIT, 0, &hw->current_limit,
1007 &hw->extended_limit,
1008 &hw->cir_tokens_per_period,
1009 &hw->pir_tokens_per_period, &hw->scale))
1011 QOS_DEBUG_ERROR ("Policer parameter computation failed.");
1016 else if ((cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) ||
1017 (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115))
1019 /* Two-rate policer */
1020 if ((cfg->rb.kbps.cir_kbps == 0) || (cfg->rb.kbps.eir_kbps == 0)
1021 || (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps)
1022 || (cfg->rb.kbps.cb_bytes == 0) || (cfg->rb.kbps.eb_bytes == 0))
1024 QOS_DEBUG_ERROR ("Config parameter validation failed.");
1028 if (compute_policer_params (hz,
1029 (u64) cfg->rb.kbps.cir_kbps *
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))
1037 QOS_DEBUG_ERROR ("Policer parameter computation failed.");
1045 "Config parameter validation failed. RFC not supported");
1049 hw->current_bucket = hw->current_limit;
1050 hw->extended_bucket = hw->extended_limit;
1056 * Input: configured parameters in 'cfg'.
1057 * Output: physical structure is returned in 'phys',
1058 * Return: Status, success or failure code.
1061 pol_logical_2_physical (const qos_pol_cfg_params_st *cfg, policer_t *phys)
1064 qos_pol_cfg_params_st kbps_cfg;
1066 clib_memset (phys, 0, sizeof (policer_t));
1067 clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st));
1071 QOS_DEBUG_ERROR ("Illegal parameters");
1075 switch (cfg->rate_type)
1078 /* copy all the data into kbps_cfg */
1079 kbps_cfg.rb.kbps.cir_kbps = cfg->rb.kbps.cir_kbps;
1080 kbps_cfg.rb.kbps.eir_kbps = cfg->rb.kbps.eir_kbps;
1081 kbps_cfg.rb.kbps.cb_bytes = cfg->rb.kbps.cb_bytes;
1082 kbps_cfg.rb.kbps.eb_bytes = cfg->rb.kbps.eb_bytes;
1085 kbps_cfg.rb.kbps.cir_kbps =
1086 qos_convert_pps_to_kbps (cfg->rb.pps.cir_pps);
1087 kbps_cfg.rb.kbps.eir_kbps =
1088 qos_convert_pps_to_kbps (cfg->rb.pps.eir_pps);
1089 kbps_cfg.rb.kbps.cb_bytes = qos_convert_burst_ms_to_bytes (
1090 (u32) cfg->rb.pps.cb_ms, kbps_cfg.rb.kbps.cir_kbps);
1091 kbps_cfg.rb.kbps.eb_bytes = qos_convert_burst_ms_to_bytes (
1092 (u32) cfg->rb.pps.eb_ms, kbps_cfg.rb.kbps.eir_kbps);
1095 QOS_DEBUG_ERROR ("Illegal rate type");
1099 /* rate type is now converted to kbps */
1100 kbps_cfg.rate_type = QOS_RATE_KBPS;
1101 kbps_cfg.rnd_type = cfg->rnd_type;
1102 kbps_cfg.rfc = cfg->rfc;
1104 phys->action[POLICE_CONFORM] = cfg->conform_action.action_type;
1105 phys->mark_dscp[POLICE_CONFORM] = cfg->conform_action.dscp;
1106 phys->action[POLICE_EXCEED] = cfg->exceed_action.action_type;
1107 phys->mark_dscp[POLICE_EXCEED] = cfg->exceed_action.dscp;
1108 phys->action[POLICE_VIOLATE] = cfg->violate_action.action_type;
1109 phys->mark_dscp[POLICE_VIOLATE] = cfg->violate_action.dscp;
1111 phys->color_aware = cfg->color_aware;
1113 /* convert logical into hw params which involves qos calculations */
1114 rc = x86_pol_compute_hw_params (&kbps_cfg, phys);
1117 QOS_DEBUG_ERROR ("Unable to compute hw param. Error: %d", rc);
1121 /* for debugging purposes, the bucket token values can be overwritten */
1122 if (cfg->overwrite_bucket)
1124 phys->current_bucket = cfg->current_bucket;
1125 phys->extended_bucket = cfg->extended_bucket;
1132 qos_convert_pol_bucket_to_hw_fmt (policer_t *bkt, qos_pol_hw_params_st *hw_fmt)
1134 clib_memset (hw_fmt, 0, sizeof (qos_pol_hw_params_st));
1138 * Input: h/w programmable parameter values in 'hw'
1139 * Output: configured parameter values in 'cfg'
1140 * Return: Status, success or failure code.
1143 pol_convert_hw_to_cfg_params (qos_pol_hw_params_st *hw,
1144 qos_pol_cfg_params_st *cfg)
1148 if ((hw == NULL) || (cfg == NULL))
1153 if ((hw->rfc == IPE_RFC_RFC4115) &&
1154 (hw->peak_rate_man << hw->rate_exp) == 0 && !(hw->extd_bkt_limit_man))
1157 * For a 1R2C, we set EIR = 0, EB = 0
1159 cfg->rfc = QOS_POLICER_TYPE_1R2C;
1161 else if (hw->rfc == IPE_RFC_RFC2697)
1163 cfg->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697;
1165 else if (hw->rfc == IPE_RFC_RFC2698)
1167 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698;
1169 else if (hw->rfc == IPE_RFC_RFC4115)
1171 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115;
1173 else if (hw->rfc == IPE_RFC_MEF5CF1)
1175 cfg->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
1183 (((u64) hw->avg_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) /
1185 cfg->rb.kbps.cir_kbps = (u32) temp_rate;
1188 (((u64) hw->peak_rate_man << hw->rate_exp) * 8LL * QOS_POL_TICKS_PER_SEC) /
1190 cfg->rb.kbps.eir_kbps = (u32) temp_rate;
1192 cfg->rb.kbps.cb_bytes = ((u64) hw->comm_bkt_limit_man <<
1193 (u64) hw->comm_bkt_limit_exp);
1194 cfg->rb.kbps.eb_bytes = ((u64) hw->extd_bkt_limit_man <<
1195 (u64) hw->extd_bkt_limit_exp);
1197 if (cfg->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697)
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
1203 cfg->rb.kbps.eb_bytes = (cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes);
1204 cfg->rb.kbps.eir_kbps = 0;
1206 else if (cfg->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115)
1209 * For 4115 in the hardware is excess rate and burst, but EA provides
1210 * peak-rate, so adjust it to be eir
1212 cfg->rb.kbps.eir_kbps += cfg->rb.kbps.cir_kbps;
1213 cfg->rb.kbps.eb_bytes += cfg->rb.kbps.cb_bytes;
1215 /* h/w conversion to cfg is in kbps */
1216 cfg->rate_type = QOS_RATE_KBPS;
1217 cfg->overwrite_bucket = 0;
1218 cfg->current_bucket = hw->comm_bkt;
1219 cfg->extended_bucket = hw->extd_bkt;
1221 QOS_DEBUG_INFO ("configured params, cir: %u kbps, eir: %u kbps, cb "
1222 "burst: 0x%llx bytes, eb burst: 0x%llx bytes",
1223 cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps,
1224 cfg->rb.kbps.cb_bytes, cfg->rb.kbps.eb_bytes);
1230 qos_convert_kbps_to_pps (u32 rate_kbps)
1232 u64 numer, denom, rnd_value = 0;
1234 numer = (u64) ((u64) rate_kbps * 1000LL);
1235 denom = (u64) ((u64) QOS_POLICER_FIXED_PKT_SIZE * 8LL);
1237 (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST);
1239 return ((u32) rnd_value);
1243 qos_convert_burst_bytes_to_ms (u64 burst_bytes, u32 rate_kbps)
1245 u64 numer, denom, rnd_value = 0;
1247 numer = burst_bytes * 8LL;
1248 denom = (u64) rate_kbps;
1250 (void) qos_pol_round (numer, denom, &rnd_value, QOS_ROUND_TO_CLOSEST);
1252 return ((u32) rnd_value);
1256 * Input: physical structure in 'phys', rate_type in cfg
1257 * Output: configured parameters in 'cfg'.
1258 * Return: Status, success or failure code.
1261 pol_physical_2_logical (policer_t *phys, qos_pol_cfg_params_st *cfg)
1264 qos_pol_hw_params_st pol_hw;
1265 qos_pol_cfg_params_st kbps_cfg;
1267 clib_memset (&pol_hw, 0, sizeof (qos_pol_hw_params_st));
1268 clib_memset (&kbps_cfg, 0, sizeof (qos_pol_cfg_params_st));
1272 QOS_DEBUG_ERROR ("Illegal parameters");
1276 qos_convert_pol_bucket_to_hw_fmt (phys, &pol_hw);
1278 rc = pol_convert_hw_to_cfg_params (&pol_hw, &kbps_cfg);
1281 QOS_DEBUG_ERROR ("Unable to convert hw params to config params. "
1287 /* check what rate type is required */
1288 switch (cfg->rate_type)
1291 /* copy all the data into kbps_cfg */
1292 cfg->rb.kbps.cir_kbps = kbps_cfg.rb.kbps.cir_kbps;
1293 cfg->rb.kbps.eir_kbps = kbps_cfg.rb.kbps.eir_kbps;
1294 cfg->rb.kbps.cb_bytes = kbps_cfg.rb.kbps.cb_bytes;
1295 cfg->rb.kbps.eb_bytes = kbps_cfg.rb.kbps.eb_bytes;
1298 cfg->rb.pps.cir_pps =
1299 qos_convert_kbps_to_pps (kbps_cfg.rb.kbps.cir_kbps);
1300 cfg->rb.pps.eir_pps =
1301 qos_convert_kbps_to_pps (kbps_cfg.rb.kbps.eir_kbps);
1302 cfg->rb.pps.cb_ms = qos_convert_burst_bytes_to_ms (
1303 kbps_cfg.rb.kbps.cb_bytes, kbps_cfg.rb.kbps.cir_kbps);
1304 cfg->rb.pps.eb_ms = qos_convert_burst_bytes_to_ms (
1305 kbps_cfg.rb.kbps.eb_bytes, kbps_cfg.rb.kbps.eir_kbps);
1308 QOS_DEBUG_ERROR ("Illegal rate type");
1312 /* cfg->rate_type remains what it was */
1313 cfg->rnd_type = kbps_cfg.rnd_type;
1314 cfg->rfc = kbps_cfg.rfc;
1315 cfg->overwrite_bucket = kbps_cfg.overwrite_bucket;
1316 cfg->current_bucket = kbps_cfg.current_bucket;
1317 cfg->extended_bucket = kbps_cfg.extended_bucket;
1323 * fd.io coding-style-patch-verification: ON
1326 * eval: (c-set-style "gnu")