1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_malloc.h>
11 #include <rte_cycles.h>
12 #include <rte_prefetch.h>
13 #include <rte_branch_prediction.h>
15 #include <rte_bitmap.h>
16 #include <rte_reciprocal.h>
18 #include "rte_sched.h"
19 #include "rte_sched_common.h"
20 #include "rte_approx.h"
22 #ifdef __INTEL_COMPILER
23 #pragma warning(disable:2259) /* conversion may lose significant bits */
26 #ifndef RTE_SCHED_PORT_N_GRINDERS
27 #define RTE_SCHED_PORT_N_GRINDERS 8
30 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
31 #define RTE_SCHED_WRR_SHIFT 3
32 #define RTE_SCHED_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
33 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
34 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
35 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
37 /* Scaling for cycles_per_byte calculation
38 * Chosen so that minimum rate is 480 bit/sec
40 #define RTE_SCHED_TIME_SHIFT 8
42 struct rte_sched_pipe_profile {
43 /* Token bucket (TB) */
45 uint64_t tb_credits_per_period;
48 /* Pipe traffic classes */
50 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
53 /* Pipe best-effort traffic class queues */
54 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
57 struct rte_sched_pipe {
58 /* Token bucket (TB) */
59 uint64_t tb_time; /* time of last update */
62 /* Pipe profile and flags */
65 /* Traffic classes (TCs) */
66 uint64_t tc_time; /* time of next update */
67 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
69 /* Weighted Round Robin (WRR) */
70 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
72 /* TC oversubscription */
73 uint64_t tc_ov_credits;
74 uint8_t tc_ov_period_id;
75 } __rte_cache_aligned;
77 struct rte_sched_queue {
82 struct rte_sched_queue_extra {
83 struct rte_sched_queue_stats stats;
94 e_GRINDER_PREFETCH_PIPE = 0,
95 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
96 e_GRINDER_PREFETCH_MBUF,
100 struct rte_sched_subport_profile {
101 /* Token bucket (TB) */
103 uint64_t tb_credits_per_period;
106 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
110 struct rte_sched_grinder {
112 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
113 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
118 enum grinder_state state;
121 struct rte_sched_subport *subport;
122 struct rte_sched_subport_profile *subport_params;
123 struct rte_sched_pipe *pipe;
124 struct rte_sched_pipe_profile *pipe_params;
127 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
128 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
134 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
135 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
136 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
140 struct rte_mbuf *pkt;
143 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
144 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
145 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
148 struct rte_sched_subport {
149 /* Token bucket (TB) */
150 uint64_t tb_time; /* time of last update */
153 /* Traffic classes (TCs) */
154 uint64_t tc_time; /* time of next update */
155 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
157 /* TC oversubscription */
159 uint64_t tc_ov_wm_min;
160 uint64_t tc_ov_wm_max;
161 uint8_t tc_ov_period_id;
167 struct rte_sched_subport_stats stats __rte_cache_aligned;
169 /* subport profile */
172 uint32_t n_pipes_per_subport_enabled;
173 uint32_t n_pipe_profiles;
174 uint32_t n_max_pipe_profiles;
176 /* Pipe best-effort TC rate */
177 uint64_t pipe_tc_be_rate_max;
179 /* Pipe queues size */
180 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
182 #ifdef RTE_SCHED_CMAN
184 enum rte_sched_cman_mode cman;
188 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
189 struct rte_pie_config pie_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
193 /* Scheduling loop detection */
195 uint32_t pipe_exhaustion;
198 struct rte_bitmap *bmp;
199 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
202 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
203 uint32_t busy_grinders;
205 /* Queue base calculation */
206 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
209 struct rte_sched_pipe *pipe;
210 struct rte_sched_queue *queue;
211 struct rte_sched_queue_extra *queue_extra;
212 struct rte_sched_pipe_profile *pipe_profiles;
214 struct rte_mbuf **queue_array;
215 uint8_t memory[0] __rte_cache_aligned;
216 } __rte_cache_aligned;
218 struct rte_sched_port {
219 /* User parameters */
220 uint32_t n_subports_per_port;
221 uint32_t n_pipes_per_subport;
222 uint32_t n_pipes_per_subport_log2;
223 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
224 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
225 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
226 uint32_t n_subport_profiles;
227 uint32_t n_max_subport_profiles;
230 uint32_t frame_overhead;
234 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cycles */
235 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
236 uint64_t time; /* Current NIC TX time measured in bytes */
237 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
238 uint64_t cycles_per_byte;
241 struct rte_mbuf **pkts_out;
245 /* Large data structures */
246 struct rte_sched_subport_profile *subport_profiles;
247 struct rte_sched_subport *subports[0] __rte_cache_aligned;
248 } __rte_cache_aligned;
250 enum rte_sched_subport_array {
251 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
252 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
253 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
254 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
255 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
256 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
257 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
260 static inline uint32_t
261 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
263 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
266 static inline struct rte_mbuf **
267 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
269 uint32_t pindex = qindex >> 4;
270 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
272 return (subport->queue_array + pindex *
273 subport->qsize_sum + subport->qsize_add[qpos]);
276 static inline uint16_t
277 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
278 struct rte_sched_subport *subport, uint32_t qindex)
280 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
282 return subport->qsize[tc];
285 static inline uint32_t
286 rte_sched_port_queues_per_port(struct rte_sched_port *port)
288 uint32_t n_queues = 0, i;
290 for (i = 0; i < port->n_subports_per_port; i++)
291 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
296 static inline uint16_t
297 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
299 uint16_t pipe_queue = port->pipe_queue[traffic_class];
304 static inline uint8_t
305 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
307 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
312 static inline uint8_t
313 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
315 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
321 pipe_profile_check(struct rte_sched_pipe_params *params,
322 uint64_t rate, uint16_t *qsize)
326 /* Pipe parameters */
327 if (params == NULL) {
329 "%s: Incorrect value for parameter params\n", __func__);
333 /* TB rate: non-zero, not greater than port rate */
334 if (params->tb_rate == 0 ||
335 params->tb_rate > rate) {
337 "%s: Incorrect value for tb rate\n", __func__);
341 /* TB size: non-zero */
342 if (params->tb_size == 0) {
344 "%s: Incorrect value for tb size\n", __func__);
348 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
349 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
350 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
351 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
352 params->tc_rate[i] > params->tb_rate))) {
354 "%s: Incorrect value for qsize or tc_rate\n", __func__);
359 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
360 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
362 "%s: Incorrect value for be traffic class rate\n", __func__);
366 /* TC period: non-zero */
367 if (params->tc_period == 0) {
369 "%s: Incorrect value for tc period\n", __func__);
373 /* Best effort tc oversubscription weight: non-zero */
374 if (params->tc_ov_weight == 0) {
376 "%s: Incorrect value for tc ov weight\n", __func__);
380 /* Queue WRR weights: non-zero */
381 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
382 if (params->wrr_weights[i] == 0) {
384 "%s: Incorrect value for wrr weight\n", __func__);
393 subport_profile_check(struct rte_sched_subport_profile_params *params,
398 /* Check user parameters */
399 if (params == NULL) {
400 RTE_LOG(ERR, SCHED, "%s: "
401 "Incorrect value for parameter params\n", __func__);
405 if (params->tb_rate == 0 || params->tb_rate > rate) {
406 RTE_LOG(ERR, SCHED, "%s: "
407 "Incorrect value for tb rate\n", __func__);
411 if (params->tb_size == 0) {
412 RTE_LOG(ERR, SCHED, "%s: "
413 "Incorrect value for tb size\n", __func__);
417 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
418 uint64_t tc_rate = params->tc_rate[i];
420 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
421 RTE_LOG(ERR, SCHED, "%s: "
422 "Incorrect value for tc rate\n", __func__);
427 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
428 RTE_LOG(ERR, SCHED, "%s: "
429 "Incorrect tc rate(best effort)\n", __func__);
433 if (params->tc_period == 0) {
434 RTE_LOG(ERR, SCHED, "%s: "
435 "Incorrect value for tc period\n", __func__);
443 rte_sched_port_check_params(struct rte_sched_port_params *params)
447 if (params == NULL) {
449 "%s: Incorrect value for parameter params\n", __func__);
454 if (params->socket < 0) {
456 "%s: Incorrect value for socket id\n", __func__);
461 if (params->rate == 0) {
463 "%s: Incorrect value for rate\n", __func__);
468 if (params->mtu == 0) {
470 "%s: Incorrect value for mtu\n", __func__);
474 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
475 if (params->n_subports_per_port == 0 ||
476 params->n_subports_per_port > 1u << 16 ||
477 !rte_is_power_of_2(params->n_subports_per_port)) {
479 "%s: Incorrect value for number of subports\n", __func__);
483 if (params->subport_profiles == NULL ||
484 params->n_subport_profiles == 0 ||
485 params->n_max_subport_profiles == 0 ||
486 params->n_subport_profiles > params->n_max_subport_profiles) {
488 "%s: Incorrect value for subport profiles\n", __func__);
492 for (i = 0; i < params->n_subport_profiles; i++) {
493 struct rte_sched_subport_profile_params *p =
494 params->subport_profiles + i;
497 status = subport_profile_check(p, params->rate);
500 "%s: subport profile check failed(%d)\n",
506 /* n_pipes_per_subport: non-zero, power of 2 */
507 if (params->n_pipes_per_subport == 0 ||
508 !rte_is_power_of_2(params->n_pipes_per_subport)) {
510 "%s: Incorrect value for maximum pipes number\n", __func__);
518 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
519 enum rte_sched_subport_array array)
521 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
522 uint32_t n_subport_pipe_queues =
523 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
525 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
526 uint32_t size_queue =
527 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
528 uint32_t size_queue_extra
529 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
530 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
531 sizeof(struct rte_sched_pipe_profile);
532 uint32_t size_bmp_array =
533 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
534 uint32_t size_per_pipe_queue_array, size_queue_array;
538 size_per_pipe_queue_array = 0;
539 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
540 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
541 size_per_pipe_queue_array +=
542 params->qsize[i] * sizeof(struct rte_mbuf *);
544 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
545 params->qsize[i] * sizeof(struct rte_mbuf *);
547 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
551 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
553 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
555 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
557 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
559 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
561 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
563 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
565 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
567 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
569 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
571 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
573 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
579 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
583 subport->qsize_add[0] = 0;
585 /* Strict priority traffic class */
586 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
587 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
589 /* Best-effort traffic class */
590 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
591 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
592 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
593 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
594 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
595 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
596 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
597 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
598 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
600 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
601 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
605 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
607 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
609 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
610 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
611 " Traffic classes: period = %"PRIu64",\n"
612 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
613 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
614 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
615 " Best-effort traffic class oversubscription: weight = %hhu\n"
616 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
621 p->tb_credits_per_period,
624 /* Traffic classes */
626 p->tc_credits_per_period[0],
627 p->tc_credits_per_period[1],
628 p->tc_credits_per_period[2],
629 p->tc_credits_per_period[3],
630 p->tc_credits_per_period[4],
631 p->tc_credits_per_period[5],
632 p->tc_credits_per_period[6],
633 p->tc_credits_per_period[7],
634 p->tc_credits_per_period[8],
635 p->tc_credits_per_period[9],
636 p->tc_credits_per_period[10],
637 p->tc_credits_per_period[11],
638 p->tc_credits_per_period[12],
640 /* Best-effort traffic class oversubscription */
644 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
648 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
650 struct rte_sched_subport_profile *p = port->subport_profiles + i;
652 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
653 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
655 "Traffic classes: period = %"PRIu64",\n"
656 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
657 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
658 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
663 p->tb_credits_per_period,
666 /* Traffic classes */
668 p->tc_credits_per_period[0],
669 p->tc_credits_per_period[1],
670 p->tc_credits_per_period[2],
671 p->tc_credits_per_period[3],
672 p->tc_credits_per_period[4],
673 p->tc_credits_per_period[5],
674 p->tc_credits_per_period[6],
675 p->tc_credits_per_period[7],
676 p->tc_credits_per_period[8],
677 p->tc_credits_per_period[9],
678 p->tc_credits_per_period[10],
679 p->tc_credits_per_period[11],
680 p->tc_credits_per_period[12]);
683 static inline uint64_t
684 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
686 uint64_t time = time_ms;
688 time = (time * rate) / 1000;
694 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
695 struct rte_sched_pipe_params *src,
696 struct rte_sched_pipe_profile *dst,
699 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
700 uint32_t lcd1, lcd2, lcd;
704 if (src->tb_rate == rate) {
705 dst->tb_credits_per_period = 1;
708 double tb_rate = (double) src->tb_rate
710 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
712 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
716 dst->tb_size = src->tb_size;
718 /* Traffic Classes */
719 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
722 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
723 if (subport->qsize[i])
724 dst->tc_credits_per_period[i]
725 = rte_sched_time_ms_to_bytes(src->tc_period,
728 dst->tc_ov_weight = src->tc_ov_weight;
731 wrr_cost[0] = src->wrr_weights[0];
732 wrr_cost[1] = src->wrr_weights[1];
733 wrr_cost[2] = src->wrr_weights[2];
734 wrr_cost[3] = src->wrr_weights[3];
736 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
737 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
738 lcd = rte_get_lcd(lcd1, lcd2);
740 wrr_cost[0] = lcd / wrr_cost[0];
741 wrr_cost[1] = lcd / wrr_cost[1];
742 wrr_cost[2] = lcd / wrr_cost[2];
743 wrr_cost[3] = lcd / wrr_cost[3];
745 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
746 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
747 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
748 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
752 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
753 struct rte_sched_subport_profile *dst,
759 if (src->tb_rate == rate) {
760 dst->tb_credits_per_period = 1;
763 double tb_rate = (double) src->tb_rate
765 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
767 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
771 dst->tb_size = src->tb_size;
773 /* Traffic Classes */
774 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
776 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
777 dst->tc_credits_per_period[i]
778 = rte_sched_time_ms_to_bytes(src->tc_period,
783 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
784 struct rte_sched_subport_params *params, uint64_t rate)
788 for (i = 0; i < subport->n_pipe_profiles; i++) {
789 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
790 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
792 rte_sched_pipe_profile_convert(subport, src, dst, rate);
793 rte_sched_port_log_pipe_profile(subport, i);
796 subport->pipe_tc_be_rate_max = 0;
797 for (i = 0; i < subport->n_pipe_profiles; i++) {
798 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
799 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
801 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
802 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
807 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
808 struct rte_sched_port_params *params,
813 for (i = 0; i < port->n_subport_profiles; i++) {
814 struct rte_sched_subport_profile_params *src
815 = params->subport_profiles + i;
816 struct rte_sched_subport_profile *dst
817 = port->subport_profiles + i;
819 rte_sched_subport_profile_convert(src, dst, rate);
820 rte_sched_port_log_subport_profile(port, i);
825 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
826 uint32_t n_max_pipes_per_subport,
831 /* Check user parameters */
832 if (params == NULL) {
834 "%s: Incorrect value for parameter params\n", __func__);
838 /* qsize: if non-zero, power of 2,
839 * no bigger than 32K (due to 16-bit read/write pointers)
841 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
842 uint16_t qsize = params->qsize[i];
844 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
846 "%s: Incorrect value for qsize\n", __func__);
851 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
852 RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
856 /* n_pipes_per_subport: non-zero, power of 2 */
857 if (params->n_pipes_per_subport_enabled == 0 ||
858 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
859 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
861 "%s: Incorrect value for pipes number\n", __func__);
865 /* pipe_profiles and n_pipe_profiles */
866 if (params->pipe_profiles == NULL ||
867 params->n_pipe_profiles == 0 ||
868 params->n_max_pipe_profiles == 0 ||
869 params->n_pipe_profiles > params->n_max_pipe_profiles) {
871 "%s: Incorrect value for pipe profiles\n", __func__);
875 for (i = 0; i < params->n_pipe_profiles; i++) {
876 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
879 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
882 "%s: Pipe profile check failed(%d)\n", __func__, status);
891 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
892 struct rte_sched_subport_params **subport_params)
894 uint32_t size0 = 0, size1 = 0, i;
897 status = rte_sched_port_check_params(port_params);
900 "%s: Port scheduler port params check failed (%d)\n",
906 for (i = 0; i < port_params->n_subports_per_port; i++) {
907 struct rte_sched_subport_params *sp = subport_params[i];
909 status = rte_sched_subport_check_params(sp,
910 port_params->n_pipes_per_subport,
914 "%s: Port scheduler subport params check failed (%d)\n",
921 size0 = sizeof(struct rte_sched_port);
923 for (i = 0; i < port_params->n_subports_per_port; i++) {
924 struct rte_sched_subport_params *sp = subport_params[i];
926 size1 += rte_sched_subport_get_array_base(sp,
927 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
930 return size0 + size1;
933 struct rte_sched_port *
934 rte_sched_port_config(struct rte_sched_port_params *params)
936 struct rte_sched_port *port = NULL;
937 uint32_t size0, size1, size2;
938 uint32_t cycles_per_byte;
942 status = rte_sched_port_check_params(params);
945 "%s: Port scheduler params check failed (%d)\n",
950 size0 = sizeof(struct rte_sched_port);
951 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
952 size2 = params->n_max_subport_profiles *
953 sizeof(struct rte_sched_subport_profile);
955 /* Allocate memory to store the data structures */
956 port = rte_zmalloc_socket("qos_params", size0 + size1,
957 RTE_CACHE_LINE_SIZE, params->socket);
959 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
964 /* Allocate memory to store the subport profile */
965 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
966 RTE_CACHE_LINE_SIZE, params->socket);
967 if (port->subport_profiles == NULL) {
968 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
973 /* User parameters */
974 port->n_subports_per_port = params->n_subports_per_port;
975 port->n_subport_profiles = params->n_subport_profiles;
976 port->n_max_subport_profiles = params->n_max_subport_profiles;
977 port->n_pipes_per_subport = params->n_pipes_per_subport;
978 port->n_pipes_per_subport_log2 =
979 __builtin_ctz(params->n_pipes_per_subport);
980 port->socket = params->socket;
982 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
983 port->pipe_queue[i] = i;
985 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
986 port->pipe_tc[i] = j;
988 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
992 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
993 port->tc_queue[i] = j;
995 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
998 port->rate = params->rate;
999 port->mtu = params->mtu + params->frame_overhead;
1000 port->frame_overhead = params->frame_overhead;
1003 port->time_cpu_cycles = rte_get_tsc_cycles();
1004 port->time_cpu_bytes = 0;
1007 /* Subport profile table */
1008 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1010 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1012 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1013 port->cycles_per_byte = cycles_per_byte;
1016 port->pkts_out = NULL;
1017 port->n_pkts_out = 0;
1018 port->subport_id = 0;
1024 rte_sched_subport_free(struct rte_sched_port *port,
1025 struct rte_sched_subport *subport)
1027 uint32_t n_subport_pipe_queues;
1030 if (subport == NULL)
1033 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1035 /* Free enqueued mbufs */
1036 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1037 struct rte_mbuf **mbufs =
1038 rte_sched_subport_pipe_qbase(subport, qindex);
1039 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1041 struct rte_sched_queue *queue = subport->queue + qindex;
1042 uint16_t qr = queue->qr & (qsize - 1);
1043 uint16_t qw = queue->qw & (qsize - 1);
1045 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1046 rte_pktmbuf_free(mbufs[qr]);
1054 rte_sched_port_free(struct rte_sched_port *port)
1058 /* Check user parameters */
1062 for (i = 0; i < port->n_subports_per_port; i++)
1063 rte_sched_subport_free(port, port->subports[i]);
1065 rte_free(port->subport_profiles);
1070 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1074 for (i = 0; i < n_subports; i++) {
1075 struct rte_sched_subport *subport = port->subports[i];
1077 rte_sched_subport_free(port, subport);
1080 rte_free(port->subport_profiles);
1084 #ifdef RTE_SCHED_CMAN
1086 rte_sched_red_config(struct rte_sched_port *port,
1087 struct rte_sched_subport *s,
1088 struct rte_sched_subport_params *params,
1089 uint32_t n_subports)
1093 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1097 for (j = 0; j < RTE_COLORS; j++) {
1098 /* if min/max are both zero, then RED is disabled */
1099 if ((params->cman_params->red_params[i][j].min_th |
1100 params->cman_params->red_params[i][j].max_th) == 0) {
1104 if (rte_red_config_init(&s->red_config[i][j],
1105 params->cman_params->red_params[i][j].wq_log2,
1106 params->cman_params->red_params[i][j].min_th,
1107 params->cman_params->red_params[i][j].max_th,
1108 params->cman_params->red_params[i][j].maxp_inv) != 0) {
1109 rte_sched_free_memory(port, n_subports);
1111 RTE_LOG(NOTICE, SCHED,
1112 "%s: RED configuration init fails\n", __func__);
1117 s->cman = RTE_SCHED_CMAN_RED;
1122 rte_sched_pie_config(struct rte_sched_port *port,
1123 struct rte_sched_subport *s,
1124 struct rte_sched_subport_params *params,
1125 uint32_t n_subports)
1129 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1130 if (params->cman_params->pie_params[i].tailq_th > params->qsize[i]) {
1131 RTE_LOG(NOTICE, SCHED,
1132 "%s: PIE tailq threshold incorrect\n", __func__);
1136 if (rte_pie_config_init(&s->pie_config[i],
1137 params->cman_params->pie_params[i].qdelay_ref,
1138 params->cman_params->pie_params[i].dp_update_interval,
1139 params->cman_params->pie_params[i].max_burst,
1140 params->cman_params->pie_params[i].tailq_th) != 0) {
1141 rte_sched_free_memory(port, n_subports);
1143 RTE_LOG(NOTICE, SCHED,
1144 "%s: PIE configuration init fails\n", __func__);
1148 s->cman = RTE_SCHED_CMAN_PIE;
1153 rte_sched_cman_config(struct rte_sched_port *port,
1154 struct rte_sched_subport *s,
1155 struct rte_sched_subport_params *params,
1156 uint32_t n_subports)
1158 if (params->cman_params->cman_mode == RTE_SCHED_CMAN_RED)
1159 return rte_sched_red_config(port, s, params, n_subports);
1161 else if (params->cman_params->cman_mode == RTE_SCHED_CMAN_PIE)
1162 return rte_sched_pie_config(port, s, params, n_subports);
1169 rte_sched_subport_config(struct rte_sched_port *port,
1170 uint32_t subport_id,
1171 struct rte_sched_subport_params *params,
1172 uint32_t subport_profile_id)
1174 struct rte_sched_subport *s = NULL;
1175 uint32_t n_subports = subport_id;
1176 struct rte_sched_subport_profile *profile;
1177 uint32_t n_subport_pipe_queues, i;
1178 uint32_t size0, size1, bmp_mem_size;
1182 /* Check user parameters */
1185 "%s: Incorrect value for parameter port\n", __func__);
1189 if (subport_id >= port->n_subports_per_port) {
1191 "%s: Incorrect value for subport id\n", __func__);
1196 if (subport_profile_id >= port->n_max_subport_profiles) {
1197 RTE_LOG(ERR, SCHED, "%s: "
1198 "Number of subport profile exceeds the max limit\n",
1204 /** Memory is allocated only on first invocation of the api for a
1205 * given subport. Subsequent invocation on same subport will just
1206 * update subport bandwidth parameter.
1208 if (port->subports[subport_id] == NULL) {
1210 status = rte_sched_subport_check_params(params,
1211 port->n_pipes_per_subport,
1214 RTE_LOG(NOTICE, SCHED,
1215 "%s: Port scheduler params check failed (%d)\n",
1221 /* Determine the amount of memory to allocate */
1222 size0 = sizeof(struct rte_sched_subport);
1223 size1 = rte_sched_subport_get_array_base(params,
1224 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1226 /* Allocate memory to store the data structures */
1227 s = rte_zmalloc_socket("subport_params", size0 + size1,
1228 RTE_CACHE_LINE_SIZE, port->socket);
1231 "%s: Memory allocation fails\n", __func__);
1238 subport_profile_id = 0;
1241 port->subports[subport_id] = s;
1243 s->tb_time = port->time;
1245 /* compile time checks */
1246 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1247 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1248 (RTE_SCHED_PORT_N_GRINDERS - 1));
1250 /* User parameters */
1251 s->n_pipes_per_subport_enabled =
1252 params->n_pipes_per_subport_enabled;
1253 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1254 s->n_pipe_profiles = params->n_pipe_profiles;
1255 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1257 #ifdef RTE_SCHED_CMAN
1258 if (params->cman_params != NULL) {
1259 s->cman_enabled = true;
1260 status = rte_sched_cman_config(port, s, params, n_subports);
1262 RTE_LOG(NOTICE, SCHED,
1263 "%s: CMAN configuration fails\n", __func__);
1267 s->cman_enabled = false;
1271 /* Scheduling loop detection */
1272 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1273 s->pipe_exhaustion = 0;
1276 s->busy_grinders = 0;
1278 /* Queue base calculation */
1279 rte_sched_subport_config_qsize(s);
1281 /* Large data structures */
1282 s->pipe = (struct rte_sched_pipe *)
1283 (s->memory + rte_sched_subport_get_array_base(params,
1284 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1285 s->queue = (struct rte_sched_queue *)
1286 (s->memory + rte_sched_subport_get_array_base(params,
1287 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1288 s->queue_extra = (struct rte_sched_queue_extra *)
1289 (s->memory + rte_sched_subport_get_array_base(params,
1290 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1291 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1292 (s->memory + rte_sched_subport_get_array_base(params,
1293 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1294 s->bmp_array = s->memory + rte_sched_subport_get_array_base(
1295 params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1296 s->queue_array = (struct rte_mbuf **)
1297 (s->memory + rte_sched_subport_get_array_base(params,
1298 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1300 /* Pipe profile table */
1301 rte_sched_subport_config_pipe_profile_table(s, params,
1305 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1306 bmp_mem_size = rte_bitmap_get_memory_footprint(
1307 n_subport_pipe_queues);
1308 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1310 if (s->bmp == NULL) {
1312 "%s: Subport bitmap init error\n", __func__);
1317 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1318 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1320 /* TC oversubscription */
1321 s->tc_ov_wm_min = port->mtu;
1322 s->tc_ov_period_id = 0;
1329 /* update subport parameters from subport profile table*/
1330 profile = port->subport_profiles + subport_profile_id;
1332 s = port->subports[subport_id];
1334 s->tb_credits = profile->tb_size / 2;
1336 s->tc_time = port->time + profile->tc_period;
1338 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1341 profile->tc_credits_per_period[i];
1343 profile->tc_credits_per_period[i] = 0;
1345 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1346 s->pipe_tc_be_rate_max);
1347 s->tc_ov_wm = s->tc_ov_wm_max;
1348 s->profile = subport_profile_id;
1352 rte_sched_port_log_subport_profile(port, subport_profile_id);
1357 rte_sched_free_memory(port, n_subports);
1363 rte_sched_pipe_config(struct rte_sched_port *port,
1364 uint32_t subport_id,
1366 int32_t pipe_profile)
1368 struct rte_sched_subport *s;
1369 struct rte_sched_subport_profile *sp;
1370 struct rte_sched_pipe *p;
1371 struct rte_sched_pipe_profile *params;
1372 uint32_t n_subports = subport_id + 1;
1373 uint32_t deactivate, profile, i;
1376 /* Check user parameters */
1377 profile = (uint32_t) pipe_profile;
1378 deactivate = (pipe_profile < 0);
1382 "%s: Incorrect value for parameter port\n", __func__);
1386 if (subport_id >= port->n_subports_per_port) {
1388 "%s: Incorrect value for parameter subport id\n", __func__);
1393 s = port->subports[subport_id];
1394 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1396 "%s: Incorrect value for parameter pipe id\n", __func__);
1401 if (!deactivate && profile >= s->n_pipe_profiles) {
1403 "%s: Incorrect value for parameter pipe profile\n", __func__);
1408 sp = port->subport_profiles + s->profile;
1409 /* Handle the case when pipe already has a valid configuration */
1410 p = s->pipe + pipe_id;
1412 params = s->pipe_profiles + p->profile;
1414 double subport_tc_be_rate =
1415 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1416 / (double) sp->tc_period;
1417 double pipe_tc_be_rate =
1418 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1419 / (double) params->tc_period;
1420 uint32_t tc_be_ov = s->tc_ov;
1422 /* Unplug pipe from its subport */
1423 s->tc_ov_n -= params->tc_ov_weight;
1424 s->tc_ov_rate -= pipe_tc_be_rate;
1425 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1427 if (s->tc_ov != tc_be_ov) {
1428 RTE_LOG(DEBUG, SCHED,
1429 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1430 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1433 /* Reset the pipe */
1434 memset(p, 0, sizeof(struct rte_sched_pipe));
1440 /* Apply the new pipe configuration */
1441 p->profile = profile;
1442 params = s->pipe_profiles + p->profile;
1444 /* Token Bucket (TB) */
1445 p->tb_time = port->time;
1446 p->tb_credits = params->tb_size / 2;
1448 /* Traffic Classes (TCs) */
1449 p->tc_time = port->time + params->tc_period;
1451 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1453 p->tc_credits[i] = params->tc_credits_per_period[i];
1456 /* Subport best effort tc oversubscription */
1457 double subport_tc_be_rate =
1458 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1459 / (double) sp->tc_period;
1460 double pipe_tc_be_rate =
1461 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1462 / (double) params->tc_period;
1463 uint32_t tc_be_ov = s->tc_ov;
1465 s->tc_ov_n += params->tc_ov_weight;
1466 s->tc_ov_rate += pipe_tc_be_rate;
1467 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1469 if (s->tc_ov != tc_be_ov) {
1470 RTE_LOG(DEBUG, SCHED,
1471 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1472 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1474 p->tc_ov_period_id = s->tc_ov_period_id;
1475 p->tc_ov_credits = s->tc_ov_wm;
1481 rte_sched_free_memory(port, n_subports);
1487 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1488 uint32_t subport_id,
1489 struct rte_sched_pipe_params *params,
1490 uint32_t *pipe_profile_id)
1492 struct rte_sched_subport *s;
1493 struct rte_sched_pipe_profile *pp;
1500 "%s: Incorrect value for parameter port\n", __func__);
1504 /* Subport id not exceeds the max limit */
1505 if (subport_id > port->n_subports_per_port) {
1507 "%s: Incorrect value for subport id\n", __func__);
1511 s = port->subports[subport_id];
1513 /* Pipe profiles exceeds the max limit */
1514 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1516 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1521 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1524 "%s: Pipe profile check failed(%d)\n", __func__, status);
1528 pp = &s->pipe_profiles[s->n_pipe_profiles];
1529 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1531 /* Pipe profile should not exists */
1532 for (i = 0; i < s->n_pipe_profiles; i++)
1533 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1535 "%s: Pipe profile exists\n", __func__);
1539 /* Pipe profile commit */
1540 *pipe_profile_id = s->n_pipe_profiles;
1541 s->n_pipe_profiles++;
1543 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1544 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1546 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1552 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1553 struct rte_sched_subport_profile_params *params,
1554 uint32_t *subport_profile_id)
1558 struct rte_sched_subport_profile *dst;
1562 RTE_LOG(ERR, SCHED, "%s: "
1563 "Incorrect value for parameter port\n", __func__);
1567 if (params == NULL) {
1568 RTE_LOG(ERR, SCHED, "%s: "
1569 "Incorrect value for parameter profile\n", __func__);
1573 if (subport_profile_id == NULL) {
1574 RTE_LOG(ERR, SCHED, "%s: "
1575 "Incorrect value for parameter subport_profile_id\n",
1580 dst = port->subport_profiles + port->n_subport_profiles;
1582 /* Subport profiles exceeds the max limit */
1583 if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1584 RTE_LOG(ERR, SCHED, "%s: "
1585 "Number of subport profiles exceeds the max limit\n",
1590 status = subport_profile_check(params, port->rate);
1593 "%s: subport profile check failed(%d)\n", __func__, status);
1597 rte_sched_subport_profile_convert(params, dst, port->rate);
1599 /* Subport profile should not exists */
1600 for (i = 0; i < port->n_subport_profiles; i++)
1601 if (memcmp(port->subport_profiles + i,
1602 dst, sizeof(*dst)) == 0) {
1604 "%s: subport profile exists\n", __func__);
1608 /* Subport profile commit */
1609 *subport_profile_id = port->n_subport_profiles;
1610 port->n_subport_profiles++;
1612 rte_sched_port_log_subport_profile(port, *subport_profile_id);
1617 static inline uint32_t
1618 rte_sched_port_qindex(struct rte_sched_port *port,
1621 uint32_t traffic_class,
1624 return ((subport & (port->n_subports_per_port - 1)) <<
1625 (port->n_pipes_per_subport_log2 + 4)) |
1627 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1628 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1629 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1633 rte_sched_port_pkt_write(struct rte_sched_port *port,
1634 struct rte_mbuf *pkt,
1635 uint32_t subport, uint32_t pipe,
1636 uint32_t traffic_class,
1637 uint32_t queue, enum rte_color color)
1640 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1642 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1646 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1647 const struct rte_mbuf *pkt,
1648 uint32_t *subport, uint32_t *pipe,
1649 uint32_t *traffic_class, uint32_t *queue)
1651 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1653 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1654 *pipe = (queue_id >> 4) &
1655 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1656 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1657 *queue = rte_sched_port_tc_queue(port, queue_id);
1661 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1663 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1667 rte_sched_subport_read_stats(struct rte_sched_port *port,
1668 uint32_t subport_id,
1669 struct rte_sched_subport_stats *stats,
1672 struct rte_sched_subport *s;
1674 /* Check user parameters */
1677 "%s: Incorrect value for parameter port\n", __func__);
1681 if (subport_id >= port->n_subports_per_port) {
1683 "%s: Incorrect value for subport id\n", __func__);
1687 if (stats == NULL) {
1689 "%s: Incorrect value for parameter stats\n", __func__);
1693 if (tc_ov == NULL) {
1695 "%s: Incorrect value for tc_ov\n", __func__);
1699 s = port->subports[subport_id];
1701 /* Copy subport stats and clear */
1702 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1703 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1705 /* Subport TC oversubscription status */
1712 rte_sched_queue_read_stats(struct rte_sched_port *port,
1714 struct rte_sched_queue_stats *stats,
1717 struct rte_sched_subport *s;
1718 struct rte_sched_queue *q;
1719 struct rte_sched_queue_extra *qe;
1720 uint32_t subport_id, subport_qmask, subport_qindex;
1722 /* Check user parameters */
1725 "%s: Incorrect value for parameter port\n", __func__);
1729 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1731 "%s: Incorrect value for queue id\n", __func__);
1735 if (stats == NULL) {
1737 "%s: Incorrect value for parameter stats\n", __func__);
1743 "%s: Incorrect value for parameter qlen\n", __func__);
1746 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1747 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1749 s = port->subports[subport_id];
1750 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1751 q = s->queue + subport_qindex;
1752 qe = s->queue_extra + subport_qindex;
1754 /* Copy queue stats and clear */
1755 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1756 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1759 *qlen = q->qw - q->qr;
1764 #ifdef RTE_SCHED_DEBUG
1767 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1770 struct rte_sched_queue *queue = subport->queue + qindex;
1772 return queue->qr == queue->qw;
1775 #endif /* RTE_SCHED_DEBUG */
1778 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1779 struct rte_sched_subport *subport,
1781 struct rte_mbuf *pkt)
1783 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1784 uint32_t pkt_len = pkt->pkt_len;
1786 subport->stats.n_pkts_tc[tc_index] += 1;
1787 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1791 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1792 struct rte_sched_subport *subport,
1794 struct rte_mbuf *pkt,
1795 __rte_unused uint32_t n_pkts_cman_dropped)
1797 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1798 uint32_t pkt_len = pkt->pkt_len;
1800 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1801 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1802 subport->stats.n_pkts_cman_dropped[tc_index] += n_pkts_cman_dropped;
1806 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1808 struct rte_mbuf *pkt)
1810 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1811 uint32_t pkt_len = pkt->pkt_len;
1813 qe->stats.n_pkts += 1;
1814 qe->stats.n_bytes += pkt_len;
1818 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1820 struct rte_mbuf *pkt,
1821 __rte_unused uint32_t n_pkts_cman_dropped)
1823 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1824 uint32_t pkt_len = pkt->pkt_len;
1826 qe->stats.n_pkts_dropped += 1;
1827 qe->stats.n_bytes_dropped += pkt_len;
1828 #ifdef RTE_SCHED_CMAN
1829 if (subport->cman_enabled)
1830 qe->stats.n_pkts_cman_dropped += n_pkts_cman_dropped;
1834 #ifdef RTE_SCHED_CMAN
1837 rte_sched_port_cman_drop(struct rte_sched_port *port,
1838 struct rte_sched_subport *subport,
1839 struct rte_mbuf *pkt,
1843 if (!subport->cman_enabled)
1846 struct rte_sched_queue_extra *qe;
1849 tc_index = rte_sched_port_pipe_tc(port, qindex);
1850 qe = subport->queue_extra + qindex;
1853 if (subport->cman == RTE_SCHED_CMAN_RED) {
1854 struct rte_red_config *red_cfg;
1855 struct rte_red *red;
1856 enum rte_color color;
1858 color = rte_sched_port_pkt_read_color(pkt);
1859 red_cfg = &subport->red_config[tc_index][color];
1861 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1866 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1870 struct rte_pie_config *pie_cfg = &subport->pie_config[tc_index];
1871 struct rte_pie *pie = &qe->pie;
1873 return rte_pie_enqueue(pie_cfg, pie, qlen, pkt->pkt_len, port->time_cpu_cycles);
1877 rte_sched_port_red_set_queue_empty_timestamp(struct rte_sched_port *port,
1878 struct rte_sched_subport *subport, uint32_t qindex)
1880 if (subport->cman_enabled) {
1881 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1882 if (subport->cman == RTE_SCHED_CMAN_RED) {
1883 struct rte_red *red = &qe->red;
1885 rte_red_mark_queue_empty(red, port->time);
1891 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport,
1892 uint32_t qindex, uint32_t pkt_len, uint64_t time) {
1893 if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_PIE) {
1894 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1895 struct rte_pie *pie = &qe->pie;
1897 /* Update queue length */
1899 pie->qlen_bytes -= pkt_len;
1901 rte_pie_dequeue(pie, pkt_len, time);
1907 static inline int rte_sched_port_cman_drop(struct rte_sched_port *port __rte_unused,
1908 struct rte_sched_subport *subport __rte_unused,
1909 struct rte_mbuf *pkt __rte_unused,
1910 uint32_t qindex __rte_unused,
1911 uint16_t qlen __rte_unused)
1916 #define rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex)
1919 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport __rte_unused,
1920 uint32_t qindex __rte_unused,
1921 uint32_t pkt_len __rte_unused,
1922 uint64_t time __rte_unused) {
1923 /* do-nothing when RTE_SCHED_CMAN not defined */
1926 #endif /* RTE_SCHED_CMAN */
1928 #ifdef RTE_SCHED_DEBUG
1931 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1938 rte_panic("Empty slab at position %u\n", bmp_pos);
1941 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1942 if (mask & bmp_slab) {
1943 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1944 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1951 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1955 #endif /* RTE_SCHED_DEBUG */
1957 static inline struct rte_sched_subport *
1958 rte_sched_port_subport(struct rte_sched_port *port,
1959 struct rte_mbuf *pkt)
1961 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1962 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1964 return port->subports[subport_id];
1967 static inline uint32_t
1968 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1969 struct rte_mbuf *pkt, uint32_t subport_qmask)
1971 struct rte_sched_queue *q;
1972 struct rte_sched_queue_extra *qe;
1973 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1974 uint32_t subport_queue_id = subport_qmask & qindex;
1976 q = subport->queue + subport_queue_id;
1978 qe = subport->queue_extra + subport_queue_id;
1981 return subport_queue_id;
1985 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1986 struct rte_sched_subport *subport,
1988 struct rte_mbuf **qbase)
1990 struct rte_sched_queue *q;
1991 struct rte_mbuf **q_qw;
1994 q = subport->queue + qindex;
1995 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1996 q_qw = qbase + (q->qw & (qsize - 1));
1998 rte_prefetch0(q_qw);
1999 rte_bitmap_prefetch0(subport->bmp, qindex);
2003 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
2004 struct rte_sched_subport *subport,
2006 struct rte_mbuf **qbase,
2007 struct rte_mbuf *pkt)
2009 struct rte_sched_queue *q;
2013 q = subport->queue + qindex;
2014 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2015 qlen = q->qw - q->qr;
2017 /* Drop the packet (and update drop stats) when queue is full */
2018 if (unlikely(rte_sched_port_cman_drop(port, subport, pkt, qindex, qlen) ||
2020 rte_pktmbuf_free(pkt);
2021 rte_sched_port_update_subport_stats_on_drop(port, subport,
2022 qindex, pkt, qlen < qsize);
2023 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
2028 /* Enqueue packet */
2029 qbase[q->qw & (qsize - 1)] = pkt;
2032 /* Activate queue in the subport bitmap */
2033 rte_bitmap_set(subport->bmp, qindex);
2036 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2037 rte_sched_port_update_queue_stats(subport, qindex, pkt);
2044 * The enqueue function implements a 4-level pipeline with each stage
2045 * processing two different packets. The purpose of using a pipeline
2046 * is to hide the latency of prefetching the data structures. The
2047 * naming convention is presented in the diagram below:
2049 * p00 _______ p10 _______ p20 _______ p30 _______
2050 * ----->| |----->| |----->| |----->| |----->
2051 * | 0 | | 1 | | 2 | | 3 |
2052 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2057 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2060 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2061 *pkt30, *pkt31, *pkt_last;
2062 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2063 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2064 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2065 *subport20, *subport21, *subport30, *subport31, *subport_last;
2066 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2067 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2068 uint32_t subport_qmask;
2072 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2075 * Less then 6 input packets available, which is not enough to
2078 if (unlikely(n_pkts < 6)) {
2079 struct rte_sched_subport *subports[5];
2080 struct rte_mbuf **q_base[5];
2083 /* Prefetch the mbuf structure of each packet */
2084 for (i = 0; i < n_pkts; i++)
2085 rte_prefetch0(pkts[i]);
2087 /* Prefetch the subport structure for each packet */
2088 for (i = 0; i < n_pkts; i++)
2089 subports[i] = rte_sched_port_subport(port, pkts[i]);
2091 /* Prefetch the queue structure for each queue */
2092 for (i = 0; i < n_pkts; i++)
2093 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2094 pkts[i], subport_qmask);
2096 /* Prefetch the write pointer location of each queue */
2097 for (i = 0; i < n_pkts; i++) {
2098 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2099 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2103 /* Write each packet to its queue */
2104 for (i = 0; i < n_pkts; i++)
2105 result += rte_sched_port_enqueue_qwa(port, subports[i],
2106 q[i], q_base[i], pkts[i]);
2111 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2114 rte_prefetch0(pkt20);
2115 rte_prefetch0(pkt21);
2119 rte_prefetch0(pkt10);
2120 rte_prefetch0(pkt11);
2122 subport20 = rte_sched_port_subport(port, pkt20);
2123 subport21 = rte_sched_port_subport(port, pkt21);
2124 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2125 pkt20, subport_qmask);
2126 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2127 pkt21, subport_qmask);
2131 rte_prefetch0(pkt00);
2132 rte_prefetch0(pkt01);
2134 subport10 = rte_sched_port_subport(port, pkt10);
2135 subport11 = rte_sched_port_subport(port, pkt11);
2136 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2137 pkt10, subport_qmask);
2138 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2139 pkt11, subport_qmask);
2141 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2142 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2143 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2144 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2146 /* Run the pipeline */
2147 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2148 /* Propagate stage inputs */
2159 subport30 = subport20;
2160 subport31 = subport21;
2161 subport20 = subport10;
2162 subport21 = subport11;
2163 q30_base = q20_base;
2164 q31_base = q21_base;
2166 /* Stage 0: Get packets in */
2168 pkt01 = pkts[i + 1];
2169 rte_prefetch0(pkt00);
2170 rte_prefetch0(pkt01);
2172 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2173 subport10 = rte_sched_port_subport(port, pkt10);
2174 subport11 = rte_sched_port_subport(port, pkt11);
2175 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2176 pkt10, subport_qmask);
2177 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2178 pkt11, subport_qmask);
2180 /* Stage 2: Prefetch queue write location */
2181 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2182 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2183 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2184 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2186 /* Stage 3: Write packet to queue and activate queue */
2187 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2188 q30, q30_base, pkt30);
2189 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2190 q31, q31_base, pkt31);
2191 result += r30 + r31;
2195 * Drain the pipeline (exactly 6 packets).
2196 * Handle the last packet in the case
2197 * of an odd number of input packets.
2199 pkt_last = pkts[n_pkts - 1];
2200 rte_prefetch0(pkt_last);
2202 subport00 = rte_sched_port_subport(port, pkt00);
2203 subport01 = rte_sched_port_subport(port, pkt01);
2204 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2205 pkt00, subport_qmask);
2206 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2207 pkt01, subport_qmask);
2209 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2210 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2211 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2212 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2214 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2215 q20, q20_base, pkt20);
2216 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2217 q21, q21_base, pkt21);
2218 result += r20 + r21;
2220 subport_last = rte_sched_port_subport(port, pkt_last);
2221 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2222 pkt_last, subport_qmask);
2224 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2225 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2226 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2227 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2229 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2231 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2233 result += r10 + r11;
2235 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2236 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2237 q_last, q_last_base);
2239 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2241 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2243 result += r00 + r01;
2246 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2247 q_last, q_last_base, pkt_last);
2254 static inline uint64_t
2255 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2256 struct rte_sched_subport *subport, uint32_t pos)
2258 struct rte_sched_grinder *grinder = subport->grinder + pos;
2259 struct rte_sched_subport_profile *sp = grinder->subport_params;
2260 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2261 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2262 uint64_t tc_ov_wm = subport->tc_ov_wm;
2265 if (subport->tc_ov == 0)
2266 return subport->tc_ov_wm_max;
2268 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2269 tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2270 - subport->tc_credits[i];
2271 tc_consumption += tc_ov_consumption[i];
2274 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2275 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2276 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2278 tc_ov_consumption_max =
2279 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2282 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2283 (tc_ov_consumption_max - port->mtu)) {
2284 tc_ov_wm -= tc_ov_wm >> 7;
2285 if (tc_ov_wm < subport->tc_ov_wm_min)
2286 tc_ov_wm = subport->tc_ov_wm_min;
2291 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2292 if (tc_ov_wm > subport->tc_ov_wm_max)
2293 tc_ov_wm = subport->tc_ov_wm_max;
2299 grinder_credits_update(struct rte_sched_port *port,
2300 struct rte_sched_subport *subport, uint32_t pos)
2302 struct rte_sched_grinder *grinder = subport->grinder + pos;
2303 struct rte_sched_pipe *pipe = grinder->pipe;
2304 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2305 struct rte_sched_subport_profile *sp = grinder->subport_params;
2310 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2311 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2312 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2313 subport->tb_time += n_periods * sp->tb_period;
2316 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2317 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2318 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2319 pipe->tb_time += n_periods * params->tb_period;
2322 if (unlikely(port->time >= subport->tc_time)) {
2324 grinder_tc_ov_credits_update(port, subport, pos);
2326 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2327 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2329 subport->tc_time = port->time + sp->tc_period;
2330 subport->tc_ov_period_id++;
2334 if (unlikely(port->time >= pipe->tc_time)) {
2335 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2336 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2337 pipe->tc_time = port->time + params->tc_period;
2340 /* Pipe TCs - Oversubscription */
2341 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2342 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2344 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2349 grinder_credits_check(struct rte_sched_port *port,
2350 struct rte_sched_subport *subport, uint32_t pos)
2352 struct rte_sched_grinder *grinder = subport->grinder + pos;
2353 struct rte_sched_pipe *pipe = grinder->pipe;
2354 struct rte_mbuf *pkt = grinder->pkt;
2355 uint32_t tc_index = grinder->tc_index;
2356 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2357 uint64_t subport_tb_credits = subport->tb_credits;
2358 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2359 uint64_t pipe_tb_credits = pipe->tb_credits;
2360 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2361 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2362 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2363 uint64_t pipe_tc_ov_credits;
2367 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2368 pipe_tc_ov_mask1[i] = ~0LLU;
2370 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2371 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2372 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2374 /* Check pipe and subport credits */
2375 enough_credits = (pkt_len <= subport_tb_credits) &&
2376 (pkt_len <= subport_tc_credits) &&
2377 (pkt_len <= pipe_tb_credits) &&
2378 (pkt_len <= pipe_tc_credits) &&
2379 (pkt_len <= pipe_tc_ov_credits);
2381 if (!enough_credits)
2384 /* Update pipe and subport credits */
2385 subport->tb_credits -= pkt_len;
2386 subport->tc_credits[tc_index] -= pkt_len;
2387 pipe->tb_credits -= pkt_len;
2388 pipe->tc_credits[tc_index] -= pkt_len;
2389 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2396 grinder_schedule(struct rte_sched_port *port,
2397 struct rte_sched_subport *subport, uint32_t pos)
2399 struct rte_sched_grinder *grinder = subport->grinder + pos;
2400 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2401 uint32_t qindex = grinder->qindex[grinder->qpos];
2402 struct rte_mbuf *pkt = grinder->pkt;
2403 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2404 uint32_t be_tc_active;
2406 if (!grinder_credits_check(port, subport, pos))
2409 /* Advance port time */
2410 port->time += pkt_len;
2413 port->pkts_out[port->n_pkts_out++] = pkt;
2416 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2417 grinder->wrr_tokens[grinder->qpos] +=
2418 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2420 if (queue->qr == queue->qw) {
2421 rte_bitmap_clear(subport->bmp, qindex);
2422 grinder->qmask &= ~(1 << grinder->qpos);
2424 grinder->wrr_mask[grinder->qpos] = 0;
2426 rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex);
2429 rte_sched_port_pie_dequeue(subport, qindex, pkt_len, port->time_cpu_cycles);
2431 /* Reset pipe loop detection */
2432 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2433 grinder->productive = 1;
2439 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2443 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2444 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2452 grinder_pcache_populate(struct rte_sched_subport *subport,
2453 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2455 struct rte_sched_grinder *grinder = subport->grinder + pos;
2458 grinder->pcache_w = 0;
2459 grinder->pcache_r = 0;
2461 w[0] = (uint16_t) bmp_slab;
2462 w[1] = (uint16_t) (bmp_slab >> 16);
2463 w[2] = (uint16_t) (bmp_slab >> 32);
2464 w[3] = (uint16_t) (bmp_slab >> 48);
2466 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2467 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2468 grinder->pcache_w += (w[0] != 0);
2470 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2471 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2472 grinder->pcache_w += (w[1] != 0);
2474 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2475 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2476 grinder->pcache_w += (w[2] != 0);
2478 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2479 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2480 grinder->pcache_w += (w[3] != 0);
2484 grinder_tccache_populate(struct rte_sched_subport *subport,
2485 uint32_t pos, uint32_t qindex, uint16_t qmask)
2487 struct rte_sched_grinder *grinder = subport->grinder + pos;
2490 grinder->tccache_w = 0;
2491 grinder->tccache_r = 0;
2493 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2494 b = (uint8_t) ((qmask >> i) & 0x1);
2495 grinder->tccache_qmask[grinder->tccache_w] = b;
2496 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2497 grinder->tccache_w += (b != 0);
2500 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2501 grinder->tccache_qmask[grinder->tccache_w] = b;
2502 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2503 RTE_SCHED_TRAFFIC_CLASS_BE;
2504 grinder->tccache_w += (b != 0);
2508 grinder_next_tc(struct rte_sched_port *port,
2509 struct rte_sched_subport *subport, uint32_t pos)
2511 struct rte_sched_grinder *grinder = subport->grinder + pos;
2512 struct rte_mbuf **qbase;
2516 if (grinder->tccache_r == grinder->tccache_w)
2519 qindex = grinder->tccache_qindex[grinder->tccache_r];
2520 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2521 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2523 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2524 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2525 grinder->qsize = qsize;
2527 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2528 grinder->queue[0] = subport->queue + qindex;
2529 grinder->qbase[0] = qbase;
2530 grinder->qindex[0] = qindex;
2531 grinder->tccache_r++;
2536 grinder->queue[0] = subport->queue + qindex;
2537 grinder->queue[1] = subport->queue + qindex + 1;
2538 grinder->queue[2] = subport->queue + qindex + 2;
2539 grinder->queue[3] = subport->queue + qindex + 3;
2541 grinder->qbase[0] = qbase;
2542 grinder->qbase[1] = qbase + qsize;
2543 grinder->qbase[2] = qbase + 2 * qsize;
2544 grinder->qbase[3] = qbase + 3 * qsize;
2546 grinder->qindex[0] = qindex;
2547 grinder->qindex[1] = qindex + 1;
2548 grinder->qindex[2] = qindex + 2;
2549 grinder->qindex[3] = qindex + 3;
2551 grinder->tccache_r++;
2556 grinder_next_pipe(struct rte_sched_port *port,
2557 struct rte_sched_subport *subport, uint32_t pos)
2559 struct rte_sched_grinder *grinder = subport->grinder + pos;
2560 uint32_t pipe_qindex;
2561 uint16_t pipe_qmask;
2563 if (grinder->pcache_r < grinder->pcache_w) {
2564 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2565 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2566 grinder->pcache_r++;
2568 uint64_t bmp_slab = 0;
2569 uint32_t bmp_pos = 0;
2571 /* Get another non-empty pipe group */
2572 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2575 #ifdef RTE_SCHED_DEBUG
2576 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2579 /* Return if pipe group already in one of the other grinders */
2580 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2581 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2584 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2586 /* Install new pipe group into grinder's pipe cache */
2587 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2589 pipe_qmask = grinder->pcache_qmask[0];
2590 pipe_qindex = grinder->pcache_qindex[0];
2591 grinder->pcache_r = 1;
2594 /* Install new pipe in the grinder */
2595 grinder->pindex = pipe_qindex >> 4;
2596 grinder->subport = subport;
2597 grinder->pipe = subport->pipe + grinder->pindex;
2598 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2599 grinder->productive = 0;
2601 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2602 grinder_next_tc(port, subport, pos);
2604 /* Check for pipe exhaustion */
2605 if (grinder->pindex == subport->pipe_loop) {
2606 subport->pipe_exhaustion = 1;
2607 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2615 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2617 struct rte_sched_grinder *grinder = subport->grinder + pos;
2618 struct rte_sched_pipe *pipe = grinder->pipe;
2619 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2620 uint32_t qmask = grinder->qmask;
2622 grinder->wrr_tokens[0] =
2623 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2624 grinder->wrr_tokens[1] =
2625 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2626 grinder->wrr_tokens[2] =
2627 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2628 grinder->wrr_tokens[3] =
2629 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2631 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2632 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2633 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2634 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2636 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2637 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2638 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2639 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2643 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2645 struct rte_sched_grinder *grinder = subport->grinder + pos;
2646 struct rte_sched_pipe *pipe = grinder->pipe;
2648 pipe->wrr_tokens[0] =
2649 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2650 RTE_SCHED_WRR_SHIFT;
2651 pipe->wrr_tokens[1] =
2652 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2653 RTE_SCHED_WRR_SHIFT;
2654 pipe->wrr_tokens[2] =
2655 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2656 RTE_SCHED_WRR_SHIFT;
2657 pipe->wrr_tokens[3] =
2658 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2659 RTE_SCHED_WRR_SHIFT;
2663 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2665 struct rte_sched_grinder *grinder = subport->grinder + pos;
2666 uint16_t wrr_tokens_min;
2668 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2669 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2670 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2671 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2673 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2674 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2676 grinder->wrr_tokens[0] -= wrr_tokens_min;
2677 grinder->wrr_tokens[1] -= wrr_tokens_min;
2678 grinder->wrr_tokens[2] -= wrr_tokens_min;
2679 grinder->wrr_tokens[3] -= wrr_tokens_min;
2683 #define grinder_evict(subport, pos)
2686 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2688 struct rte_sched_grinder *grinder = subport->grinder + pos;
2690 rte_prefetch0(grinder->pipe);
2691 rte_prefetch0(grinder->queue[0]);
2695 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2697 struct rte_sched_grinder *grinder = subport->grinder + pos;
2698 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2700 qsize = grinder->qsize;
2703 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2704 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2706 rte_prefetch0(grinder->qbase[0] + qr[0]);
2710 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2711 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2712 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2713 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2715 rte_prefetch0(grinder->qbase[0] + qr[0]);
2716 rte_prefetch0(grinder->qbase[1] + qr[1]);
2718 grinder_wrr_load(subport, pos);
2719 grinder_wrr(subport, pos);
2721 rte_prefetch0(grinder->qbase[2] + qr[2]);
2722 rte_prefetch0(grinder->qbase[3] + qr[3]);
2726 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2728 struct rte_sched_grinder *grinder = subport->grinder + pos;
2729 uint32_t qpos = grinder->qpos;
2730 struct rte_mbuf **qbase = grinder->qbase[qpos];
2731 uint16_t qsize = grinder->qsize;
2732 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2734 grinder->pkt = qbase[qr];
2735 rte_prefetch0(grinder->pkt);
2737 if (unlikely((qr & 0x7) == 7)) {
2738 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2740 rte_prefetch0(qbase + qr_next);
2744 static inline uint32_t
2745 grinder_handle(struct rte_sched_port *port,
2746 struct rte_sched_subport *subport, uint32_t pos)
2748 struct rte_sched_grinder *grinder = subport->grinder + pos;
2750 switch (grinder->state) {
2751 case e_GRINDER_PREFETCH_PIPE:
2753 if (grinder_next_pipe(port, subport, pos)) {
2754 grinder_prefetch_pipe(subport, pos);
2755 subport->busy_grinders++;
2757 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2764 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2766 struct rte_sched_pipe *pipe = grinder->pipe;
2768 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2769 grinder->subport_params = port->subport_profiles +
2772 grinder_prefetch_tc_queue_arrays(subport, pos);
2773 grinder_credits_update(port, subport, pos);
2775 grinder->state = e_GRINDER_PREFETCH_MBUF;
2779 case e_GRINDER_PREFETCH_MBUF:
2781 grinder_prefetch_mbuf(subport, pos);
2783 grinder->state = e_GRINDER_READ_MBUF;
2787 case e_GRINDER_READ_MBUF:
2789 uint32_t wrr_active, result = 0;
2791 result = grinder_schedule(port, subport, pos);
2793 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2795 /* Look for next packet within the same TC */
2796 if (result && grinder->qmask) {
2798 grinder_wrr(subport, pos);
2800 grinder_prefetch_mbuf(subport, pos);
2806 grinder_wrr_store(subport, pos);
2808 /* Look for another active TC within same pipe */
2809 if (grinder_next_tc(port, subport, pos)) {
2810 grinder_prefetch_tc_queue_arrays(subport, pos);
2812 grinder->state = e_GRINDER_PREFETCH_MBUF;
2816 if (grinder->productive == 0 &&
2817 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2818 subport->pipe_loop = grinder->pindex;
2820 grinder_evict(subport, pos);
2822 /* Look for another active pipe */
2823 if (grinder_next_pipe(port, subport, pos)) {
2824 grinder_prefetch_pipe(subport, pos);
2826 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2830 /* No active pipe found */
2831 subport->busy_grinders--;
2833 grinder->state = e_GRINDER_PREFETCH_PIPE;
2838 rte_panic("Algorithmic error (invalid state)\n");
2844 rte_sched_port_time_resync(struct rte_sched_port *port)
2846 uint64_t cycles = rte_get_tsc_cycles();
2847 uint64_t cycles_diff;
2848 uint64_t bytes_diff;
2851 if (cycles < port->time_cpu_cycles)
2852 port->time_cpu_cycles = 0;
2854 cycles_diff = cycles - port->time_cpu_cycles;
2855 /* Compute elapsed time in bytes */
2856 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2857 port->inv_cycles_per_byte);
2859 /* Advance port time */
2860 port->time_cpu_cycles +=
2861 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2862 port->time_cpu_bytes += bytes_diff;
2863 if (port->time < port->time_cpu_bytes)
2864 port->time = port->time_cpu_bytes;
2866 /* Reset pipe loop detection */
2867 for (i = 0; i < port->n_subports_per_port; i++)
2868 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2872 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2876 /* Check if any exception flag is set */
2877 exceptions = (second_pass && subport->busy_grinders == 0) ||
2878 (subport->pipe_exhaustion == 1);
2880 /* Clear exception flags */
2881 subport->pipe_exhaustion = 0;
2887 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2889 struct rte_sched_subport *subport;
2890 uint32_t subport_id = port->subport_id;
2891 uint32_t i, n_subports = 0, count;
2893 port->pkts_out = pkts;
2894 port->n_pkts_out = 0;
2896 rte_sched_port_time_resync(port);
2898 /* Take each queue in the grinder one step further */
2899 for (i = 0, count = 0; ; i++) {
2900 subport = port->subports[subport_id];
2902 count += grinder_handle(port, subport,
2903 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2905 if (count == n_pkts) {
2908 if (subport_id == port->n_subports_per_port)
2911 port->subport_id = subport_id;
2915 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2921 if (subport_id == port->n_subports_per_port)
2924 if (n_subports == port->n_subports_per_port) {
2925 port->subport_id = subport_id;