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 #ifdef RTE_SCHED_VECTOR
30 #define SCHED_VECTOR_SSE4
31 #elif defined(__ARM_NEON)
32 #define SCHED_VECTOR_NEON
37 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
38 #define RTE_SCHED_WRR_SHIFT 3
39 #define RTE_SCHED_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
40 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
41 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
42 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
44 /* Scaling for cycles_per_byte calculation
45 * Chosen so that minimum rate is 480 bit/sec
47 #define RTE_SCHED_TIME_SHIFT 8
49 struct rte_sched_pipe_profile {
50 /* Token bucket (TB) */
52 uint64_t tb_credits_per_period;
55 /* Pipe traffic classes */
57 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
60 /* Pipe best-effort traffic class queues */
61 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
64 struct rte_sched_pipe {
65 /* Token bucket (TB) */
66 uint64_t tb_time; /* time of last update */
69 /* Pipe profile and flags */
72 /* Traffic classes (TCs) */
73 uint64_t tc_time; /* time of next update */
74 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
76 /* Weighted Round Robin (WRR) */
77 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
79 /* TC oversubscription */
80 uint64_t tc_ov_credits;
81 uint8_t tc_ov_period_id;
82 } __rte_cache_aligned;
84 struct rte_sched_queue {
89 struct rte_sched_queue_extra {
90 struct rte_sched_queue_stats stats;
101 e_GRINDER_PREFETCH_PIPE = 0,
102 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
103 e_GRINDER_PREFETCH_MBUF,
107 struct rte_sched_subport_profile {
108 /* Token bucket (TB) */
110 uint64_t tb_credits_per_period;
113 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
117 struct rte_sched_grinder {
119 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
120 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
125 enum grinder_state state;
128 struct rte_sched_subport *subport;
129 struct rte_sched_subport_profile *subport_params;
130 struct rte_sched_pipe *pipe;
131 struct rte_sched_pipe_profile *pipe_params;
134 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
135 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
141 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
142 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
143 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
147 struct rte_mbuf *pkt;
150 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
151 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
152 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
155 struct rte_sched_subport {
156 /* Token bucket (TB) */
157 uint64_t tb_time; /* time of last update */
160 /* Traffic classes (TCs) */
161 uint64_t tc_time; /* time of next update */
162 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
164 /* TC oversubscription */
166 uint64_t tc_ov_wm_min;
167 uint64_t tc_ov_wm_max;
168 uint8_t tc_ov_period_id;
174 struct rte_sched_subport_stats stats __rte_cache_aligned;
176 /* subport profile */
179 uint32_t n_pipes_per_subport_enabled;
180 uint32_t n_pipe_profiles;
181 uint32_t n_max_pipe_profiles;
183 /* Pipe best-effort TC rate */
184 uint64_t pipe_tc_be_rate_max;
186 /* Pipe queues size */
187 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
189 #ifdef RTE_SCHED_CMAN
191 enum rte_sched_cman_mode cman;
195 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
196 struct rte_pie_config pie_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
200 /* Scheduling loop detection */
202 uint32_t pipe_exhaustion;
205 struct rte_bitmap *bmp;
206 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
209 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
210 uint32_t busy_grinders;
212 /* Queue base calculation */
213 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
216 struct rte_sched_pipe *pipe;
217 struct rte_sched_queue *queue;
218 struct rte_sched_queue_extra *queue_extra;
219 struct rte_sched_pipe_profile *pipe_profiles;
221 struct rte_mbuf **queue_array;
222 uint8_t memory[0] __rte_cache_aligned;
223 } __rte_cache_aligned;
225 struct rte_sched_port {
226 /* User parameters */
227 uint32_t n_subports_per_port;
228 uint32_t n_pipes_per_subport;
229 uint32_t n_pipes_per_subport_log2;
230 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
231 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
232 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
233 uint32_t n_subport_profiles;
234 uint32_t n_max_subport_profiles;
237 uint32_t frame_overhead;
241 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cycles */
242 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
243 uint64_t time; /* Current NIC TX time measured in bytes */
244 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
245 uint64_t cycles_per_byte;
248 struct rte_mbuf **pkts_out;
252 /* Large data structures */
253 struct rte_sched_subport_profile *subport_profiles;
254 struct rte_sched_subport *subports[0] __rte_cache_aligned;
255 } __rte_cache_aligned;
257 enum rte_sched_subport_array {
258 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
259 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
260 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
261 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
262 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
263 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
264 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
267 static inline uint32_t
268 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
270 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
273 static inline struct rte_mbuf **
274 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
276 uint32_t pindex = qindex >> 4;
277 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
279 return (subport->queue_array + pindex *
280 subport->qsize_sum + subport->qsize_add[qpos]);
283 static inline uint16_t
284 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
285 struct rte_sched_subport *subport, uint32_t qindex)
287 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
289 return subport->qsize[tc];
292 static inline uint32_t
293 rte_sched_port_queues_per_port(struct rte_sched_port *port)
295 uint32_t n_queues = 0, i;
297 for (i = 0; i < port->n_subports_per_port; i++)
298 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
303 static inline uint16_t
304 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
306 uint16_t pipe_queue = port->pipe_queue[traffic_class];
311 static inline uint8_t
312 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
314 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
319 static inline uint8_t
320 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
322 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
328 pipe_profile_check(struct rte_sched_pipe_params *params,
329 uint64_t rate, uint16_t *qsize)
333 /* Pipe parameters */
334 if (params == NULL) {
336 "%s: Incorrect value for parameter params\n", __func__);
340 /* TB rate: non-zero, not greater than port rate */
341 if (params->tb_rate == 0 ||
342 params->tb_rate > rate) {
344 "%s: Incorrect value for tb rate\n", __func__);
348 /* TB size: non-zero */
349 if (params->tb_size == 0) {
351 "%s: Incorrect value for tb size\n", __func__);
355 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
356 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
357 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
358 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
359 params->tc_rate[i] > params->tb_rate))) {
361 "%s: Incorrect value for qsize or tc_rate\n", __func__);
366 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
367 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
369 "%s: Incorrect value for be traffic class rate\n", __func__);
373 /* TC period: non-zero */
374 if (params->tc_period == 0) {
376 "%s: Incorrect value for tc period\n", __func__);
380 /* Best effort tc oversubscription weight: non-zero */
381 if (params->tc_ov_weight == 0) {
383 "%s: Incorrect value for tc ov weight\n", __func__);
387 /* Queue WRR weights: non-zero */
388 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
389 if (params->wrr_weights[i] == 0) {
391 "%s: Incorrect value for wrr weight\n", __func__);
400 subport_profile_check(struct rte_sched_subport_profile_params *params,
405 /* Check user parameters */
406 if (params == NULL) {
407 RTE_LOG(ERR, SCHED, "%s: "
408 "Incorrect value for parameter params\n", __func__);
412 if (params->tb_rate == 0 || params->tb_rate > rate) {
413 RTE_LOG(ERR, SCHED, "%s: "
414 "Incorrect value for tb rate\n", __func__);
418 if (params->tb_size == 0) {
419 RTE_LOG(ERR, SCHED, "%s: "
420 "Incorrect value for tb size\n", __func__);
424 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
425 uint64_t tc_rate = params->tc_rate[i];
427 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
428 RTE_LOG(ERR, SCHED, "%s: "
429 "Incorrect value for tc rate\n", __func__);
434 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
435 RTE_LOG(ERR, SCHED, "%s: "
436 "Incorrect tc rate(best effort)\n", __func__);
440 if (params->tc_period == 0) {
441 RTE_LOG(ERR, SCHED, "%s: "
442 "Incorrect value for tc period\n", __func__);
450 rte_sched_port_check_params(struct rte_sched_port_params *params)
454 if (params == NULL) {
456 "%s: Incorrect value for parameter params\n", __func__);
461 if (params->socket < 0) {
463 "%s: Incorrect value for socket id\n", __func__);
468 if (params->rate == 0) {
470 "%s: Incorrect value for rate\n", __func__);
475 if (params->mtu == 0) {
477 "%s: Incorrect value for mtu\n", __func__);
481 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
482 if (params->n_subports_per_port == 0 ||
483 params->n_subports_per_port > 1u << 16 ||
484 !rte_is_power_of_2(params->n_subports_per_port)) {
486 "%s: Incorrect value for number of subports\n", __func__);
490 if (params->subport_profiles == NULL ||
491 params->n_subport_profiles == 0 ||
492 params->n_max_subport_profiles == 0 ||
493 params->n_subport_profiles > params->n_max_subport_profiles) {
495 "%s: Incorrect value for subport profiles\n", __func__);
499 for (i = 0; i < params->n_subport_profiles; i++) {
500 struct rte_sched_subport_profile_params *p =
501 params->subport_profiles + i;
504 status = subport_profile_check(p, params->rate);
507 "%s: subport profile check failed(%d)\n",
513 /* n_pipes_per_subport: non-zero, power of 2 */
514 if (params->n_pipes_per_subport == 0 ||
515 !rte_is_power_of_2(params->n_pipes_per_subport)) {
517 "%s: Incorrect value for maximum pipes number\n", __func__);
525 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
526 enum rte_sched_subport_array array)
528 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
529 uint32_t n_subport_pipe_queues =
530 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
532 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
533 uint32_t size_queue =
534 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
535 uint32_t size_queue_extra
536 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
537 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
538 sizeof(struct rte_sched_pipe_profile);
539 uint32_t size_bmp_array =
540 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
541 uint32_t size_per_pipe_queue_array, size_queue_array;
545 size_per_pipe_queue_array = 0;
546 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
547 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
548 size_per_pipe_queue_array +=
549 params->qsize[i] * sizeof(struct rte_mbuf *);
551 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
552 params->qsize[i] * sizeof(struct rte_mbuf *);
554 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
558 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
560 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
562 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
564 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
566 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
568 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
570 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
572 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
574 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
576 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
578 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
580 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
586 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
590 subport->qsize_add[0] = 0;
592 /* Strict priority traffic class */
593 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
594 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
596 /* Best-effort traffic class */
597 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
598 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
599 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
600 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
601 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
602 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
603 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
604 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
605 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
607 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
608 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
612 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
614 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
616 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
617 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
618 " Traffic classes: period = %"PRIu64",\n"
619 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
620 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
621 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
622 " Best-effort traffic class oversubscription: weight = %hhu\n"
623 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
628 p->tb_credits_per_period,
631 /* Traffic classes */
633 p->tc_credits_per_period[0],
634 p->tc_credits_per_period[1],
635 p->tc_credits_per_period[2],
636 p->tc_credits_per_period[3],
637 p->tc_credits_per_period[4],
638 p->tc_credits_per_period[5],
639 p->tc_credits_per_period[6],
640 p->tc_credits_per_period[7],
641 p->tc_credits_per_period[8],
642 p->tc_credits_per_period[9],
643 p->tc_credits_per_period[10],
644 p->tc_credits_per_period[11],
645 p->tc_credits_per_period[12],
647 /* Best-effort traffic class oversubscription */
651 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
655 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
657 struct rte_sched_subport_profile *p = port->subport_profiles + i;
659 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
660 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
662 "Traffic classes: period = %"PRIu64",\n"
663 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
664 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
665 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
670 p->tb_credits_per_period,
673 /* Traffic classes */
675 p->tc_credits_per_period[0],
676 p->tc_credits_per_period[1],
677 p->tc_credits_per_period[2],
678 p->tc_credits_per_period[3],
679 p->tc_credits_per_period[4],
680 p->tc_credits_per_period[5],
681 p->tc_credits_per_period[6],
682 p->tc_credits_per_period[7],
683 p->tc_credits_per_period[8],
684 p->tc_credits_per_period[9],
685 p->tc_credits_per_period[10],
686 p->tc_credits_per_period[11],
687 p->tc_credits_per_period[12]);
690 static inline uint64_t
691 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
693 uint64_t time = time_ms;
695 time = (time * rate) / 1000;
701 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
702 struct rte_sched_pipe_params *src,
703 struct rte_sched_pipe_profile *dst,
706 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
707 uint32_t lcd1, lcd2, lcd;
711 if (src->tb_rate == rate) {
712 dst->tb_credits_per_period = 1;
715 double tb_rate = (double) src->tb_rate
717 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
719 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
723 dst->tb_size = src->tb_size;
725 /* Traffic Classes */
726 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
729 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
730 if (subport->qsize[i])
731 dst->tc_credits_per_period[i]
732 = rte_sched_time_ms_to_bytes(src->tc_period,
735 dst->tc_ov_weight = src->tc_ov_weight;
738 wrr_cost[0] = src->wrr_weights[0];
739 wrr_cost[1] = src->wrr_weights[1];
740 wrr_cost[2] = src->wrr_weights[2];
741 wrr_cost[3] = src->wrr_weights[3];
743 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
744 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
745 lcd = rte_get_lcd(lcd1, lcd2);
747 wrr_cost[0] = lcd / wrr_cost[0];
748 wrr_cost[1] = lcd / wrr_cost[1];
749 wrr_cost[2] = lcd / wrr_cost[2];
750 wrr_cost[3] = lcd / wrr_cost[3];
752 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
753 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
754 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
755 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
759 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
760 struct rte_sched_subport_profile *dst,
766 if (src->tb_rate == rate) {
767 dst->tb_credits_per_period = 1;
770 double tb_rate = (double) src->tb_rate
772 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
774 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
778 dst->tb_size = src->tb_size;
780 /* Traffic Classes */
781 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
783 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
784 dst->tc_credits_per_period[i]
785 = rte_sched_time_ms_to_bytes(src->tc_period,
790 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
791 struct rte_sched_subport_params *params, uint64_t rate)
795 for (i = 0; i < subport->n_pipe_profiles; i++) {
796 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
797 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
799 rte_sched_pipe_profile_convert(subport, src, dst, rate);
800 rte_sched_port_log_pipe_profile(subport, i);
803 subport->pipe_tc_be_rate_max = 0;
804 for (i = 0; i < subport->n_pipe_profiles; i++) {
805 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
806 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
808 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
809 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
814 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
815 struct rte_sched_port_params *params,
820 for (i = 0; i < port->n_subport_profiles; i++) {
821 struct rte_sched_subport_profile_params *src
822 = params->subport_profiles + i;
823 struct rte_sched_subport_profile *dst
824 = port->subport_profiles + i;
826 rte_sched_subport_profile_convert(src, dst, rate);
827 rte_sched_port_log_subport_profile(port, i);
832 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
833 uint32_t n_max_pipes_per_subport,
838 /* Check user parameters */
839 if (params == NULL) {
841 "%s: Incorrect value for parameter params\n", __func__);
845 /* qsize: if non-zero, power of 2,
846 * no bigger than 32K (due to 16-bit read/write pointers)
848 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
849 uint16_t qsize = params->qsize[i];
851 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
853 "%s: Incorrect value for qsize\n", __func__);
858 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
859 RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
863 /* n_pipes_per_subport: non-zero, power of 2 */
864 if (params->n_pipes_per_subport_enabled == 0 ||
865 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
866 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
868 "%s: Incorrect value for pipes number\n", __func__);
872 /* pipe_profiles and n_pipe_profiles */
873 if (params->pipe_profiles == NULL ||
874 params->n_pipe_profiles == 0 ||
875 params->n_max_pipe_profiles == 0 ||
876 params->n_pipe_profiles > params->n_max_pipe_profiles) {
878 "%s: Incorrect value for pipe profiles\n", __func__);
882 for (i = 0; i < params->n_pipe_profiles; i++) {
883 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
886 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
889 "%s: Pipe profile check failed(%d)\n", __func__, status);
898 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
899 struct rte_sched_subport_params **subport_params)
901 uint32_t size0 = 0, size1 = 0, i;
904 status = rte_sched_port_check_params(port_params);
907 "%s: Port scheduler port params check failed (%d)\n",
913 for (i = 0; i < port_params->n_subports_per_port; i++) {
914 struct rte_sched_subport_params *sp = subport_params[i];
916 status = rte_sched_subport_check_params(sp,
917 port_params->n_pipes_per_subport,
921 "%s: Port scheduler subport params check failed (%d)\n",
928 size0 = sizeof(struct rte_sched_port);
930 for (i = 0; i < port_params->n_subports_per_port; i++) {
931 struct rte_sched_subport_params *sp = subport_params[i];
933 size1 += rte_sched_subport_get_array_base(sp,
934 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
937 return size0 + size1;
940 struct rte_sched_port *
941 rte_sched_port_config(struct rte_sched_port_params *params)
943 struct rte_sched_port *port = NULL;
944 uint32_t size0, size1, size2;
945 uint32_t cycles_per_byte;
949 status = rte_sched_port_check_params(params);
952 "%s: Port scheduler params check failed (%d)\n",
957 size0 = sizeof(struct rte_sched_port);
958 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
959 size2 = params->n_max_subport_profiles *
960 sizeof(struct rte_sched_subport_profile);
962 /* Allocate memory to store the data structures */
963 port = rte_zmalloc_socket("qos_params", size0 + size1,
964 RTE_CACHE_LINE_SIZE, params->socket);
966 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
971 /* Allocate memory to store the subport profile */
972 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
973 RTE_CACHE_LINE_SIZE, params->socket);
974 if (port->subport_profiles == NULL) {
975 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
980 /* User parameters */
981 port->n_subports_per_port = params->n_subports_per_port;
982 port->n_subport_profiles = params->n_subport_profiles;
983 port->n_max_subport_profiles = params->n_max_subport_profiles;
984 port->n_pipes_per_subport = params->n_pipes_per_subport;
985 port->n_pipes_per_subport_log2 =
986 __builtin_ctz(params->n_pipes_per_subport);
987 port->socket = params->socket;
989 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
990 port->pipe_queue[i] = i;
992 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
993 port->pipe_tc[i] = j;
995 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
999 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
1000 port->tc_queue[i] = j;
1002 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
1005 port->rate = params->rate;
1006 port->mtu = params->mtu + params->frame_overhead;
1007 port->frame_overhead = params->frame_overhead;
1010 port->time_cpu_cycles = rte_get_tsc_cycles();
1011 port->time_cpu_bytes = 0;
1014 /* Subport profile table */
1015 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1017 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1019 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1020 port->cycles_per_byte = cycles_per_byte;
1023 port->pkts_out = NULL;
1024 port->n_pkts_out = 0;
1025 port->subport_id = 0;
1031 rte_sched_subport_free(struct rte_sched_port *port,
1032 struct rte_sched_subport *subport)
1034 uint32_t n_subport_pipe_queues;
1037 if (subport == NULL)
1040 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1042 /* Free enqueued mbufs */
1043 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1044 struct rte_mbuf **mbufs =
1045 rte_sched_subport_pipe_qbase(subport, qindex);
1046 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1048 struct rte_sched_queue *queue = subport->queue + qindex;
1049 uint16_t qr = queue->qr & (qsize - 1);
1050 uint16_t qw = queue->qw & (qsize - 1);
1052 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1053 rte_pktmbuf_free(mbufs[qr]);
1061 rte_sched_port_free(struct rte_sched_port *port)
1065 /* Check user parameters */
1069 for (i = 0; i < port->n_subports_per_port; i++)
1070 rte_sched_subport_free(port, port->subports[i]);
1072 rte_free(port->subport_profiles);
1077 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1081 for (i = 0; i < n_subports; i++) {
1082 struct rte_sched_subport *subport = port->subports[i];
1084 rte_sched_subport_free(port, subport);
1087 rte_free(port->subport_profiles);
1091 #ifdef RTE_SCHED_CMAN
1093 rte_sched_red_config(struct rte_sched_port *port,
1094 struct rte_sched_subport *s,
1095 struct rte_sched_subport_params *params,
1096 uint32_t n_subports)
1100 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1104 for (j = 0; j < RTE_COLORS; j++) {
1105 /* if min/max are both zero, then RED is disabled */
1106 if ((params->cman_params->red_params[i][j].min_th |
1107 params->cman_params->red_params[i][j].max_th) == 0) {
1111 if (rte_red_config_init(&s->red_config[i][j],
1112 params->cman_params->red_params[i][j].wq_log2,
1113 params->cman_params->red_params[i][j].min_th,
1114 params->cman_params->red_params[i][j].max_th,
1115 params->cman_params->red_params[i][j].maxp_inv) != 0) {
1116 rte_sched_free_memory(port, n_subports);
1118 RTE_LOG(NOTICE, SCHED,
1119 "%s: RED configuration init fails\n", __func__);
1124 s->cman = RTE_SCHED_CMAN_RED;
1129 rte_sched_pie_config(struct rte_sched_port *port,
1130 struct rte_sched_subport *s,
1131 struct rte_sched_subport_params *params,
1132 uint32_t n_subports)
1136 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1137 if (params->cman_params->pie_params[i].tailq_th > params->qsize[i]) {
1138 RTE_LOG(NOTICE, SCHED,
1139 "%s: PIE tailq threshold incorrect\n", __func__);
1143 if (rte_pie_config_init(&s->pie_config[i],
1144 params->cman_params->pie_params[i].qdelay_ref,
1145 params->cman_params->pie_params[i].dp_update_interval,
1146 params->cman_params->pie_params[i].max_burst,
1147 params->cman_params->pie_params[i].tailq_th) != 0) {
1148 rte_sched_free_memory(port, n_subports);
1150 RTE_LOG(NOTICE, SCHED,
1151 "%s: PIE configuration init fails\n", __func__);
1155 s->cman = RTE_SCHED_CMAN_PIE;
1160 rte_sched_cman_config(struct rte_sched_port *port,
1161 struct rte_sched_subport *s,
1162 struct rte_sched_subport_params *params,
1163 uint32_t n_subports)
1165 if (params->cman_params->cman_mode == RTE_SCHED_CMAN_RED)
1166 return rte_sched_red_config(port, s, params, n_subports);
1168 else if (params->cman_params->cman_mode == RTE_SCHED_CMAN_PIE)
1169 return rte_sched_pie_config(port, s, params, n_subports);
1176 rte_sched_subport_config(struct rte_sched_port *port,
1177 uint32_t subport_id,
1178 struct rte_sched_subport_params *params,
1179 uint32_t subport_profile_id)
1181 struct rte_sched_subport *s = NULL;
1182 uint32_t n_subports = subport_id;
1183 struct rte_sched_subport_profile *profile;
1184 uint32_t n_subport_pipe_queues, i;
1185 uint32_t size0, size1, bmp_mem_size;
1189 /* Check user parameters */
1192 "%s: Incorrect value for parameter port\n", __func__);
1196 if (subport_id >= port->n_subports_per_port) {
1198 "%s: Incorrect value for subport id\n", __func__);
1203 if (subport_profile_id >= port->n_max_subport_profiles) {
1204 RTE_LOG(ERR, SCHED, "%s: "
1205 "Number of subport profile exceeds the max limit\n",
1211 /** Memory is allocated only on first invocation of the api for a
1212 * given subport. Subsequent invocation on same subport will just
1213 * update subport bandwidth parameter.
1215 if (port->subports[subport_id] == NULL) {
1217 status = rte_sched_subport_check_params(params,
1218 port->n_pipes_per_subport,
1221 RTE_LOG(NOTICE, SCHED,
1222 "%s: Port scheduler params check failed (%d)\n",
1228 /* Determine the amount of memory to allocate */
1229 size0 = sizeof(struct rte_sched_subport);
1230 size1 = rte_sched_subport_get_array_base(params,
1231 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1233 /* Allocate memory to store the data structures */
1234 s = rte_zmalloc_socket("subport_params", size0 + size1,
1235 RTE_CACHE_LINE_SIZE, port->socket);
1238 "%s: Memory allocation fails\n", __func__);
1245 subport_profile_id = 0;
1248 port->subports[subport_id] = s;
1250 s->tb_time = port->time;
1252 /* compile time checks */
1253 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1254 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1255 (RTE_SCHED_PORT_N_GRINDERS - 1));
1257 /* User parameters */
1258 s->n_pipes_per_subport_enabled =
1259 params->n_pipes_per_subport_enabled;
1260 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1261 s->n_pipe_profiles = params->n_pipe_profiles;
1262 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1264 #ifdef RTE_SCHED_CMAN
1265 if (params->cman_params != NULL) {
1266 s->cman_enabled = true;
1267 status = rte_sched_cman_config(port, s, params, n_subports);
1269 RTE_LOG(NOTICE, SCHED,
1270 "%s: CMAN configuration fails\n", __func__);
1274 s->cman_enabled = false;
1278 /* Scheduling loop detection */
1279 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1280 s->pipe_exhaustion = 0;
1283 s->busy_grinders = 0;
1285 /* Queue base calculation */
1286 rte_sched_subport_config_qsize(s);
1288 /* Large data structures */
1289 s->pipe = (struct rte_sched_pipe *)
1290 (s->memory + rte_sched_subport_get_array_base(params,
1291 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1292 s->queue = (struct rte_sched_queue *)
1293 (s->memory + rte_sched_subport_get_array_base(params,
1294 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1295 s->queue_extra = (struct rte_sched_queue_extra *)
1296 (s->memory + rte_sched_subport_get_array_base(params,
1297 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1298 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1299 (s->memory + rte_sched_subport_get_array_base(params,
1300 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1301 s->bmp_array = s->memory + rte_sched_subport_get_array_base(
1302 params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1303 s->queue_array = (struct rte_mbuf **)
1304 (s->memory + rte_sched_subport_get_array_base(params,
1305 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1307 /* Pipe profile table */
1308 rte_sched_subport_config_pipe_profile_table(s, params,
1312 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1313 bmp_mem_size = rte_bitmap_get_memory_footprint(
1314 n_subport_pipe_queues);
1315 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1317 if (s->bmp == NULL) {
1319 "%s: Subport bitmap init error\n", __func__);
1324 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1325 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1327 #ifdef RTE_SCHED_SUBPORT_TC_OV
1328 /* TC oversubscription */
1329 s->tc_ov_wm_min = port->mtu;
1330 s->tc_ov_period_id = 0;
1338 /* update subport parameters from subport profile table*/
1339 profile = port->subport_profiles + subport_profile_id;
1341 s = port->subports[subport_id];
1343 s->tb_credits = profile->tb_size / 2;
1345 s->tc_time = port->time + profile->tc_period;
1347 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1350 profile->tc_credits_per_period[i];
1352 profile->tc_credits_per_period[i] = 0;
1354 #ifdef RTE_SCHED_SUBPORT_TC_OV
1355 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1356 s->pipe_tc_be_rate_max);
1357 s->tc_ov_wm = s->tc_ov_wm_max;
1359 s->profile = subport_profile_id;
1363 rte_sched_port_log_subport_profile(port, subport_profile_id);
1368 rte_sched_free_memory(port, n_subports);
1374 rte_sched_pipe_config(struct rte_sched_port *port,
1375 uint32_t subport_id,
1377 int32_t pipe_profile)
1379 struct rte_sched_subport *s;
1380 struct rte_sched_subport_profile *sp;
1381 struct rte_sched_pipe *p;
1382 struct rte_sched_pipe_profile *params;
1383 uint32_t n_subports = subport_id + 1;
1384 uint32_t deactivate, profile, i;
1387 /* Check user parameters */
1388 profile = (uint32_t) pipe_profile;
1389 deactivate = (pipe_profile < 0);
1393 "%s: Incorrect value for parameter port\n", __func__);
1397 if (subport_id >= port->n_subports_per_port) {
1399 "%s: Incorrect value for parameter subport id\n", __func__);
1404 s = port->subports[subport_id];
1405 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1407 "%s: Incorrect value for parameter pipe id\n", __func__);
1412 if (!deactivate && profile >= s->n_pipe_profiles) {
1414 "%s: Incorrect value for parameter pipe profile\n", __func__);
1419 sp = port->subport_profiles + s->profile;
1420 /* Handle the case when pipe already has a valid configuration */
1421 p = s->pipe + pipe_id;
1423 params = s->pipe_profiles + p->profile;
1425 double subport_tc_be_rate =
1426 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1427 / (double) sp->tc_period;
1428 double pipe_tc_be_rate =
1429 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1430 / (double) params->tc_period;
1431 uint32_t tc_be_ov = s->tc_ov;
1433 /* Unplug pipe from its subport */
1434 s->tc_ov_n -= params->tc_ov_weight;
1435 s->tc_ov_rate -= pipe_tc_be_rate;
1436 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1438 if (s->tc_ov != tc_be_ov) {
1439 RTE_LOG(DEBUG, SCHED,
1440 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1441 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1444 /* Reset the pipe */
1445 memset(p, 0, sizeof(struct rte_sched_pipe));
1451 /* Apply the new pipe configuration */
1452 p->profile = profile;
1453 params = s->pipe_profiles + p->profile;
1455 /* Token Bucket (TB) */
1456 p->tb_time = port->time;
1457 p->tb_credits = params->tb_size / 2;
1459 /* Traffic Classes (TCs) */
1460 p->tc_time = port->time + params->tc_period;
1462 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1464 p->tc_credits[i] = params->tc_credits_per_period[i];
1467 /* Subport best effort tc oversubscription */
1468 double subport_tc_be_rate =
1469 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1470 / (double) sp->tc_period;
1471 double pipe_tc_be_rate =
1472 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1473 / (double) params->tc_period;
1474 uint32_t tc_be_ov = s->tc_ov;
1476 s->tc_ov_n += params->tc_ov_weight;
1477 s->tc_ov_rate += pipe_tc_be_rate;
1478 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1480 if (s->tc_ov != tc_be_ov) {
1481 RTE_LOG(DEBUG, SCHED,
1482 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1483 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1485 p->tc_ov_period_id = s->tc_ov_period_id;
1486 p->tc_ov_credits = s->tc_ov_wm;
1492 rte_sched_free_memory(port, n_subports);
1498 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1499 uint32_t subport_id,
1500 struct rte_sched_pipe_params *params,
1501 uint32_t *pipe_profile_id)
1503 struct rte_sched_subport *s;
1504 struct rte_sched_pipe_profile *pp;
1511 "%s: Incorrect value for parameter port\n", __func__);
1515 /* Subport id not exceeds the max limit */
1516 if (subport_id > port->n_subports_per_port) {
1518 "%s: Incorrect value for subport id\n", __func__);
1522 s = port->subports[subport_id];
1524 /* Pipe profiles exceeds the max limit */
1525 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1527 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1532 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1535 "%s: Pipe profile check failed(%d)\n", __func__, status);
1539 pp = &s->pipe_profiles[s->n_pipe_profiles];
1540 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1542 /* Pipe profile should not exists */
1543 for (i = 0; i < s->n_pipe_profiles; i++)
1544 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1546 "%s: Pipe profile exists\n", __func__);
1550 /* Pipe profile commit */
1551 *pipe_profile_id = s->n_pipe_profiles;
1552 s->n_pipe_profiles++;
1554 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1555 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1557 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1563 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1564 struct rte_sched_subport_profile_params *params,
1565 uint32_t *subport_profile_id)
1569 struct rte_sched_subport_profile *dst;
1573 RTE_LOG(ERR, SCHED, "%s: "
1574 "Incorrect value for parameter port\n", __func__);
1578 if (params == NULL) {
1579 RTE_LOG(ERR, SCHED, "%s: "
1580 "Incorrect value for parameter profile\n", __func__);
1584 if (subport_profile_id == NULL) {
1585 RTE_LOG(ERR, SCHED, "%s: "
1586 "Incorrect value for parameter subport_profile_id\n",
1591 dst = port->subport_profiles + port->n_subport_profiles;
1593 /* Subport profiles exceeds the max limit */
1594 if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1595 RTE_LOG(ERR, SCHED, "%s: "
1596 "Number of subport profiles exceeds the max limit\n",
1601 status = subport_profile_check(params, port->rate);
1604 "%s: subport profile check failed(%d)\n", __func__, status);
1608 rte_sched_subport_profile_convert(params, dst, port->rate);
1610 /* Subport profile should not exists */
1611 for (i = 0; i < port->n_subport_profiles; i++)
1612 if (memcmp(port->subport_profiles + i,
1613 dst, sizeof(*dst)) == 0) {
1615 "%s: subport profile exists\n", __func__);
1619 /* Subport profile commit */
1620 *subport_profile_id = port->n_subport_profiles;
1621 port->n_subport_profiles++;
1623 rte_sched_port_log_subport_profile(port, *subport_profile_id);
1628 static inline uint32_t
1629 rte_sched_port_qindex(struct rte_sched_port *port,
1632 uint32_t traffic_class,
1635 return ((subport & (port->n_subports_per_port - 1)) <<
1636 (port->n_pipes_per_subport_log2 + 4)) |
1638 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1639 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1640 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1644 rte_sched_port_pkt_write(struct rte_sched_port *port,
1645 struct rte_mbuf *pkt,
1646 uint32_t subport, uint32_t pipe,
1647 uint32_t traffic_class,
1648 uint32_t queue, enum rte_color color)
1651 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1653 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1657 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1658 const struct rte_mbuf *pkt,
1659 uint32_t *subport, uint32_t *pipe,
1660 uint32_t *traffic_class, uint32_t *queue)
1662 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1664 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1665 *pipe = (queue_id >> 4) &
1666 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1667 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1668 *queue = rte_sched_port_tc_queue(port, queue_id);
1672 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1674 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1678 rte_sched_subport_read_stats(struct rte_sched_port *port,
1679 uint32_t subport_id,
1680 struct rte_sched_subport_stats *stats,
1683 struct rte_sched_subport *s;
1685 /* Check user parameters */
1688 "%s: Incorrect value for parameter port\n", __func__);
1692 if (subport_id >= port->n_subports_per_port) {
1694 "%s: Incorrect value for subport id\n", __func__);
1698 if (stats == NULL) {
1700 "%s: Incorrect value for parameter stats\n", __func__);
1704 if (tc_ov == NULL) {
1706 "%s: Incorrect value for tc_ov\n", __func__);
1710 s = port->subports[subport_id];
1712 /* Copy subport stats and clear */
1713 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1714 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1716 /* Subport TC oversubscription status */
1723 rte_sched_queue_read_stats(struct rte_sched_port *port,
1725 struct rte_sched_queue_stats *stats,
1728 struct rte_sched_subport *s;
1729 struct rte_sched_queue *q;
1730 struct rte_sched_queue_extra *qe;
1731 uint32_t subport_id, subport_qmask, subport_qindex;
1733 /* Check user parameters */
1736 "%s: Incorrect value for parameter port\n", __func__);
1740 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1742 "%s: Incorrect value for queue id\n", __func__);
1746 if (stats == NULL) {
1748 "%s: Incorrect value for parameter stats\n", __func__);
1754 "%s: Incorrect value for parameter qlen\n", __func__);
1757 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1758 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1760 s = port->subports[subport_id];
1761 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1762 q = s->queue + subport_qindex;
1763 qe = s->queue_extra + subport_qindex;
1765 /* Copy queue stats and clear */
1766 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1767 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1770 *qlen = q->qw - q->qr;
1775 #ifdef RTE_SCHED_DEBUG
1778 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1781 struct rte_sched_queue *queue = subport->queue + qindex;
1783 return queue->qr == queue->qw;
1786 #endif /* RTE_SCHED_DEBUG */
1788 #ifdef RTE_SCHED_COLLECT_STATS
1791 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1792 struct rte_sched_subport *subport,
1794 struct rte_mbuf *pkt)
1796 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1797 uint32_t pkt_len = pkt->pkt_len;
1799 subport->stats.n_pkts_tc[tc_index] += 1;
1800 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1804 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1805 struct rte_sched_subport *subport,
1807 struct rte_mbuf *pkt,
1808 __rte_unused uint32_t n_pkts_cman_dropped)
1810 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1811 uint32_t pkt_len = pkt->pkt_len;
1813 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1814 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1815 subport->stats.n_pkts_cman_dropped[tc_index] += n_pkts_cman_dropped;
1819 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1821 struct rte_mbuf *pkt)
1823 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1824 uint32_t pkt_len = pkt->pkt_len;
1826 qe->stats.n_pkts += 1;
1827 qe->stats.n_bytes += pkt_len;
1831 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1833 struct rte_mbuf *pkt,
1834 __rte_unused uint32_t n_pkts_cman_dropped)
1836 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1837 uint32_t pkt_len = pkt->pkt_len;
1839 qe->stats.n_pkts_dropped += 1;
1840 qe->stats.n_bytes_dropped += pkt_len;
1841 #ifdef RTE_SCHED_CMAN
1842 if (subport->cman_enabled)
1843 qe->stats.n_pkts_cman_dropped += n_pkts_cman_dropped;
1847 #endif /* RTE_SCHED_COLLECT_STATS */
1849 #ifdef RTE_SCHED_CMAN
1852 rte_sched_port_cman_drop(struct rte_sched_port *port,
1853 struct rte_sched_subport *subport,
1854 struct rte_mbuf *pkt,
1858 if (!subport->cman_enabled)
1861 struct rte_sched_queue_extra *qe;
1864 tc_index = rte_sched_port_pipe_tc(port, qindex);
1865 qe = subport->queue_extra + qindex;
1868 if (subport->cman == RTE_SCHED_CMAN_RED) {
1869 struct rte_red_config *red_cfg;
1870 struct rte_red *red;
1871 enum rte_color color;
1873 color = rte_sched_port_pkt_read_color(pkt);
1874 red_cfg = &subport->red_config[tc_index][color];
1876 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1881 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1885 struct rte_pie_config *pie_cfg = &subport->pie_config[tc_index];
1886 struct rte_pie *pie = &qe->pie;
1888 return rte_pie_enqueue(pie_cfg, pie, qlen, pkt->pkt_len, port->time_cpu_cycles);
1892 rte_sched_port_red_set_queue_empty_timestamp(struct rte_sched_port *port,
1893 struct rte_sched_subport *subport, uint32_t qindex)
1895 if (subport->cman_enabled) {
1896 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1897 if (subport->cman == RTE_SCHED_CMAN_RED) {
1898 struct rte_red *red = &qe->red;
1900 rte_red_mark_queue_empty(red, port->time);
1906 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport,
1907 uint32_t qindex, uint32_t pkt_len, uint64_t time) {
1908 if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_PIE) {
1909 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1910 struct rte_pie *pie = &qe->pie;
1912 /* Update queue length */
1914 pie->qlen_bytes -= pkt_len;
1916 rte_pie_dequeue(pie, pkt_len, time);
1922 static inline int rte_sched_port_cman_drop(struct rte_sched_port *port __rte_unused,
1923 struct rte_sched_subport *subport __rte_unused,
1924 struct rte_mbuf *pkt __rte_unused,
1925 uint32_t qindex __rte_unused,
1926 uint16_t qlen __rte_unused)
1931 #define rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex)
1934 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport __rte_unused,
1935 uint32_t qindex __rte_unused,
1936 uint32_t pkt_len __rte_unused,
1937 uint64_t time __rte_unused) {
1938 /* do-nothing when RTE_SCHED_CMAN not defined */
1941 #endif /* RTE_SCHED_CMAN */
1943 #ifdef RTE_SCHED_DEBUG
1946 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1953 rte_panic("Empty slab at position %u\n", bmp_pos);
1956 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1957 if (mask & bmp_slab) {
1958 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1959 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1966 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1970 #endif /* RTE_SCHED_DEBUG */
1972 static inline struct rte_sched_subport *
1973 rte_sched_port_subport(struct rte_sched_port *port,
1974 struct rte_mbuf *pkt)
1976 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1977 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1979 return port->subports[subport_id];
1982 static inline uint32_t
1983 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1984 struct rte_mbuf *pkt, uint32_t subport_qmask)
1986 struct rte_sched_queue *q;
1987 #ifdef RTE_SCHED_COLLECT_STATS
1988 struct rte_sched_queue_extra *qe;
1990 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1991 uint32_t subport_queue_id = subport_qmask & qindex;
1993 q = subport->queue + subport_queue_id;
1995 #ifdef RTE_SCHED_COLLECT_STATS
1996 qe = subport->queue_extra + subport_queue_id;
2000 return subport_queue_id;
2004 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
2005 struct rte_sched_subport *subport,
2007 struct rte_mbuf **qbase)
2009 struct rte_sched_queue *q;
2010 struct rte_mbuf **q_qw;
2013 q = subport->queue + qindex;
2014 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2015 q_qw = qbase + (q->qw & (qsize - 1));
2017 rte_prefetch0(q_qw);
2018 rte_bitmap_prefetch0(subport->bmp, qindex);
2022 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
2023 struct rte_sched_subport *subport,
2025 struct rte_mbuf **qbase,
2026 struct rte_mbuf *pkt)
2028 struct rte_sched_queue *q;
2032 q = subport->queue + qindex;
2033 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2034 qlen = q->qw - q->qr;
2036 /* Drop the packet (and update drop stats) when queue is full */
2037 if (unlikely(rte_sched_port_cman_drop(port, subport, pkt, qindex, qlen) ||
2039 rte_pktmbuf_free(pkt);
2040 #ifdef RTE_SCHED_COLLECT_STATS
2041 rte_sched_port_update_subport_stats_on_drop(port, subport,
2042 qindex, pkt, qlen < qsize);
2043 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
2049 /* Enqueue packet */
2050 qbase[q->qw & (qsize - 1)] = pkt;
2053 /* Activate queue in the subport bitmap */
2054 rte_bitmap_set(subport->bmp, qindex);
2057 #ifdef RTE_SCHED_COLLECT_STATS
2058 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2059 rte_sched_port_update_queue_stats(subport, qindex, pkt);
2067 * The enqueue function implements a 4-level pipeline with each stage
2068 * processing two different packets. The purpose of using a pipeline
2069 * is to hide the latency of prefetching the data structures. The
2070 * naming convention is presented in the diagram below:
2072 * p00 _______ p10 _______ p20 _______ p30 _______
2073 * ----->| |----->| |----->| |----->| |----->
2074 * | 0 | | 1 | | 2 | | 3 |
2075 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2080 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2083 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2084 *pkt30, *pkt31, *pkt_last;
2085 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2086 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2087 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2088 *subport20, *subport21, *subport30, *subport31, *subport_last;
2089 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2090 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2091 uint32_t subport_qmask;
2095 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2098 * Less then 6 input packets available, which is not enough to
2101 if (unlikely(n_pkts < 6)) {
2102 struct rte_sched_subport *subports[5];
2103 struct rte_mbuf **q_base[5];
2106 /* Prefetch the mbuf structure of each packet */
2107 for (i = 0; i < n_pkts; i++)
2108 rte_prefetch0(pkts[i]);
2110 /* Prefetch the subport structure for each packet */
2111 for (i = 0; i < n_pkts; i++)
2112 subports[i] = rte_sched_port_subport(port, pkts[i]);
2114 /* Prefetch the queue structure for each queue */
2115 for (i = 0; i < n_pkts; i++)
2116 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2117 pkts[i], subport_qmask);
2119 /* Prefetch the write pointer location of each queue */
2120 for (i = 0; i < n_pkts; i++) {
2121 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2122 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2126 /* Write each packet to its queue */
2127 for (i = 0; i < n_pkts; i++)
2128 result += rte_sched_port_enqueue_qwa(port, subports[i],
2129 q[i], q_base[i], pkts[i]);
2134 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2137 rte_prefetch0(pkt20);
2138 rte_prefetch0(pkt21);
2142 rte_prefetch0(pkt10);
2143 rte_prefetch0(pkt11);
2145 subport20 = rte_sched_port_subport(port, pkt20);
2146 subport21 = rte_sched_port_subport(port, pkt21);
2147 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2148 pkt20, subport_qmask);
2149 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2150 pkt21, subport_qmask);
2154 rte_prefetch0(pkt00);
2155 rte_prefetch0(pkt01);
2157 subport10 = rte_sched_port_subport(port, pkt10);
2158 subport11 = rte_sched_port_subport(port, pkt11);
2159 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2160 pkt10, subport_qmask);
2161 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2162 pkt11, subport_qmask);
2164 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2165 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2166 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2167 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2169 /* Run the pipeline */
2170 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2171 /* Propagate stage inputs */
2182 subport30 = subport20;
2183 subport31 = subport21;
2184 subport20 = subport10;
2185 subport21 = subport11;
2186 q30_base = q20_base;
2187 q31_base = q21_base;
2189 /* Stage 0: Get packets in */
2191 pkt01 = pkts[i + 1];
2192 rte_prefetch0(pkt00);
2193 rte_prefetch0(pkt01);
2195 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2196 subport10 = rte_sched_port_subport(port, pkt10);
2197 subport11 = rte_sched_port_subport(port, pkt11);
2198 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2199 pkt10, subport_qmask);
2200 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2201 pkt11, subport_qmask);
2203 /* Stage 2: Prefetch queue write location */
2204 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2205 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2206 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2207 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2209 /* Stage 3: Write packet to queue and activate queue */
2210 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2211 q30, q30_base, pkt30);
2212 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2213 q31, q31_base, pkt31);
2214 result += r30 + r31;
2218 * Drain the pipeline (exactly 6 packets).
2219 * Handle the last packet in the case
2220 * of an odd number of input packets.
2222 pkt_last = pkts[n_pkts - 1];
2223 rte_prefetch0(pkt_last);
2225 subport00 = rte_sched_port_subport(port, pkt00);
2226 subport01 = rte_sched_port_subport(port, pkt01);
2227 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2228 pkt00, subport_qmask);
2229 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2230 pkt01, subport_qmask);
2232 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2233 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2234 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2235 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2237 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2238 q20, q20_base, pkt20);
2239 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2240 q21, q21_base, pkt21);
2241 result += r20 + r21;
2243 subport_last = rte_sched_port_subport(port, pkt_last);
2244 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2245 pkt_last, subport_qmask);
2247 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2248 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2249 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2250 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2252 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2254 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2256 result += r10 + r11;
2258 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2259 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2260 q_last, q_last_base);
2262 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2264 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2266 result += r00 + r01;
2269 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2270 q_last, q_last_base, pkt_last);
2277 #ifndef RTE_SCHED_SUBPORT_TC_OV
2280 grinder_credits_update(struct rte_sched_port *port,
2281 struct rte_sched_subport *subport, uint32_t pos)
2283 struct rte_sched_grinder *grinder = subport->grinder + pos;
2284 struct rte_sched_pipe *pipe = grinder->pipe;
2285 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2286 struct rte_sched_subport_profile *sp = grinder->subport_params;
2291 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2292 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2293 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2294 subport->tb_time += n_periods * sp->tb_period;
2297 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2298 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2299 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2300 pipe->tb_time += n_periods * params->tb_period;
2303 if (unlikely(port->time >= subport->tc_time)) {
2304 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2305 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2307 subport->tc_time = port->time + sp->tc_period;
2311 if (unlikely(port->time >= pipe->tc_time)) {
2312 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2313 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2315 pipe->tc_time = port->time + params->tc_period;
2321 static inline uint64_t
2322 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2323 struct rte_sched_subport *subport, uint32_t pos)
2325 struct rte_sched_grinder *grinder = subport->grinder + pos;
2326 struct rte_sched_subport_profile *sp = grinder->subport_params;
2327 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2328 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2329 uint64_t tc_ov_wm = subport->tc_ov_wm;
2332 if (subport->tc_ov == 0)
2333 return subport->tc_ov_wm_max;
2335 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2336 tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2337 - subport->tc_credits[i];
2338 tc_consumption += tc_ov_consumption[i];
2341 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2342 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2343 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2345 tc_ov_consumption_max =
2346 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2349 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2350 (tc_ov_consumption_max - port->mtu)) {
2351 tc_ov_wm -= tc_ov_wm >> 7;
2352 if (tc_ov_wm < subport->tc_ov_wm_min)
2353 tc_ov_wm = subport->tc_ov_wm_min;
2358 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2359 if (tc_ov_wm > subport->tc_ov_wm_max)
2360 tc_ov_wm = subport->tc_ov_wm_max;
2366 grinder_credits_update(struct rte_sched_port *port,
2367 struct rte_sched_subport *subport, uint32_t pos)
2369 struct rte_sched_grinder *grinder = subport->grinder + pos;
2370 struct rte_sched_pipe *pipe = grinder->pipe;
2371 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2372 struct rte_sched_subport_profile *sp = grinder->subport_params;
2377 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2378 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2379 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2380 subport->tb_time += n_periods * sp->tb_period;
2383 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2384 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2385 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2386 pipe->tb_time += n_periods * params->tb_period;
2389 if (unlikely(port->time >= subport->tc_time)) {
2391 grinder_tc_ov_credits_update(port, subport, pos);
2393 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2394 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2396 subport->tc_time = port->time + sp->tc_period;
2397 subport->tc_ov_period_id++;
2401 if (unlikely(port->time >= pipe->tc_time)) {
2402 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2403 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2404 pipe->tc_time = port->time + params->tc_period;
2407 /* Pipe TCs - Oversubscription */
2408 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2409 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2411 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2415 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2418 #ifndef RTE_SCHED_SUBPORT_TC_OV
2421 grinder_credits_check(struct rte_sched_port *port,
2422 struct rte_sched_subport *subport, uint32_t pos)
2424 struct rte_sched_grinder *grinder = subport->grinder + pos;
2425 struct rte_sched_pipe *pipe = grinder->pipe;
2426 struct rte_mbuf *pkt = grinder->pkt;
2427 uint32_t tc_index = grinder->tc_index;
2428 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2429 uint64_t subport_tb_credits = subport->tb_credits;
2430 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2431 uint64_t pipe_tb_credits = pipe->tb_credits;
2432 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2435 /* Check queue credits */
2436 enough_credits = (pkt_len <= subport_tb_credits) &&
2437 (pkt_len <= subport_tc_credits) &&
2438 (pkt_len <= pipe_tb_credits) &&
2439 (pkt_len <= pipe_tc_credits);
2441 if (!enough_credits)
2444 /* Update port credits */
2445 subport->tb_credits -= pkt_len;
2446 subport->tc_credits[tc_index] -= pkt_len;
2447 pipe->tb_credits -= pkt_len;
2448 pipe->tc_credits[tc_index] -= pkt_len;
2456 grinder_credits_check(struct rte_sched_port *port,
2457 struct rte_sched_subport *subport, uint32_t pos)
2459 struct rte_sched_grinder *grinder = subport->grinder + pos;
2460 struct rte_sched_pipe *pipe = grinder->pipe;
2461 struct rte_mbuf *pkt = grinder->pkt;
2462 uint32_t tc_index = grinder->tc_index;
2463 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2464 uint64_t subport_tb_credits = subport->tb_credits;
2465 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2466 uint64_t pipe_tb_credits = pipe->tb_credits;
2467 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2468 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2469 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2470 uint64_t pipe_tc_ov_credits;
2474 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2475 pipe_tc_ov_mask1[i] = ~0LLU;
2477 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2478 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2479 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2481 /* Check pipe and subport credits */
2482 enough_credits = (pkt_len <= subport_tb_credits) &&
2483 (pkt_len <= subport_tc_credits) &&
2484 (pkt_len <= pipe_tb_credits) &&
2485 (pkt_len <= pipe_tc_credits) &&
2486 (pkt_len <= pipe_tc_ov_credits);
2488 if (!enough_credits)
2491 /* Update pipe and subport credits */
2492 subport->tb_credits -= pkt_len;
2493 subport->tc_credits[tc_index] -= pkt_len;
2494 pipe->tb_credits -= pkt_len;
2495 pipe->tc_credits[tc_index] -= pkt_len;
2496 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2501 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2505 grinder_schedule(struct rte_sched_port *port,
2506 struct rte_sched_subport *subport, uint32_t pos)
2508 struct rte_sched_grinder *grinder = subport->grinder + pos;
2509 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2510 uint32_t qindex = grinder->qindex[grinder->qpos];
2511 struct rte_mbuf *pkt = grinder->pkt;
2512 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2513 uint32_t be_tc_active;
2515 if (!grinder_credits_check(port, subport, pos))
2518 /* Advance port time */
2519 port->time += pkt_len;
2522 port->pkts_out[port->n_pkts_out++] = pkt;
2525 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2526 grinder->wrr_tokens[grinder->qpos] +=
2527 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2529 if (queue->qr == queue->qw) {
2530 rte_bitmap_clear(subport->bmp, qindex);
2531 grinder->qmask &= ~(1 << grinder->qpos);
2533 grinder->wrr_mask[grinder->qpos] = 0;
2535 rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex);
2538 rte_sched_port_pie_dequeue(subport, qindex, pkt_len, port->time_cpu_cycles);
2540 /* Reset pipe loop detection */
2541 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2542 grinder->productive = 1;
2547 #ifdef SCHED_VECTOR_SSE4
2550 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2552 __m128i index = _mm_set1_epi32(base_pipe);
2553 __m128i pipes = _mm_load_si128((__m128i *)subport->grinder_base_bmp_pos);
2554 __m128i res = _mm_cmpeq_epi32(pipes, index);
2556 pipes = _mm_load_si128((__m128i *)(subport->grinder_base_bmp_pos + 4));
2557 pipes = _mm_cmpeq_epi32(pipes, index);
2558 res = _mm_or_si128(res, pipes);
2560 if (_mm_testz_si128(res, res))
2566 #elif defined(SCHED_VECTOR_NEON)
2569 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2571 uint32x4_t index, pipes;
2572 uint32_t *pos = (uint32_t *)subport->grinder_base_bmp_pos;
2574 index = vmovq_n_u32(base_pipe);
2575 pipes = vld1q_u32(pos);
2576 if (!vminvq_u32(veorq_u32(pipes, index)))
2579 pipes = vld1q_u32(pos + 4);
2580 if (!vminvq_u32(veorq_u32(pipes, index)))
2589 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2593 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2594 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2601 #endif /* RTE_SCHED_OPTIMIZATIONS */
2604 grinder_pcache_populate(struct rte_sched_subport *subport,
2605 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2607 struct rte_sched_grinder *grinder = subport->grinder + pos;
2610 grinder->pcache_w = 0;
2611 grinder->pcache_r = 0;
2613 w[0] = (uint16_t) bmp_slab;
2614 w[1] = (uint16_t) (bmp_slab >> 16);
2615 w[2] = (uint16_t) (bmp_slab >> 32);
2616 w[3] = (uint16_t) (bmp_slab >> 48);
2618 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2619 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2620 grinder->pcache_w += (w[0] != 0);
2622 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2623 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2624 grinder->pcache_w += (w[1] != 0);
2626 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2627 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2628 grinder->pcache_w += (w[2] != 0);
2630 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2631 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2632 grinder->pcache_w += (w[3] != 0);
2636 grinder_tccache_populate(struct rte_sched_subport *subport,
2637 uint32_t pos, uint32_t qindex, uint16_t qmask)
2639 struct rte_sched_grinder *grinder = subport->grinder + pos;
2642 grinder->tccache_w = 0;
2643 grinder->tccache_r = 0;
2645 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2646 b = (uint8_t) ((qmask >> i) & 0x1);
2647 grinder->tccache_qmask[grinder->tccache_w] = b;
2648 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2649 grinder->tccache_w += (b != 0);
2652 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2653 grinder->tccache_qmask[grinder->tccache_w] = b;
2654 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2655 RTE_SCHED_TRAFFIC_CLASS_BE;
2656 grinder->tccache_w += (b != 0);
2660 grinder_next_tc(struct rte_sched_port *port,
2661 struct rte_sched_subport *subport, uint32_t pos)
2663 struct rte_sched_grinder *grinder = subport->grinder + pos;
2664 struct rte_mbuf **qbase;
2668 if (grinder->tccache_r == grinder->tccache_w)
2671 qindex = grinder->tccache_qindex[grinder->tccache_r];
2672 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2673 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2675 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2676 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2677 grinder->qsize = qsize;
2679 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2680 grinder->queue[0] = subport->queue + qindex;
2681 grinder->qbase[0] = qbase;
2682 grinder->qindex[0] = qindex;
2683 grinder->tccache_r++;
2688 grinder->queue[0] = subport->queue + qindex;
2689 grinder->queue[1] = subport->queue + qindex + 1;
2690 grinder->queue[2] = subport->queue + qindex + 2;
2691 grinder->queue[3] = subport->queue + qindex + 3;
2693 grinder->qbase[0] = qbase;
2694 grinder->qbase[1] = qbase + qsize;
2695 grinder->qbase[2] = qbase + 2 * qsize;
2696 grinder->qbase[3] = qbase + 3 * qsize;
2698 grinder->qindex[0] = qindex;
2699 grinder->qindex[1] = qindex + 1;
2700 grinder->qindex[2] = qindex + 2;
2701 grinder->qindex[3] = qindex + 3;
2703 grinder->tccache_r++;
2708 grinder_next_pipe(struct rte_sched_port *port,
2709 struct rte_sched_subport *subport, uint32_t pos)
2711 struct rte_sched_grinder *grinder = subport->grinder + pos;
2712 uint32_t pipe_qindex;
2713 uint16_t pipe_qmask;
2715 if (grinder->pcache_r < grinder->pcache_w) {
2716 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2717 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2718 grinder->pcache_r++;
2720 uint64_t bmp_slab = 0;
2721 uint32_t bmp_pos = 0;
2723 /* Get another non-empty pipe group */
2724 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2727 #ifdef RTE_SCHED_DEBUG
2728 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2731 /* Return if pipe group already in one of the other grinders */
2732 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2733 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2736 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2738 /* Install new pipe group into grinder's pipe cache */
2739 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2741 pipe_qmask = grinder->pcache_qmask[0];
2742 pipe_qindex = grinder->pcache_qindex[0];
2743 grinder->pcache_r = 1;
2746 /* Install new pipe in the grinder */
2747 grinder->pindex = pipe_qindex >> 4;
2748 grinder->subport = subport;
2749 grinder->pipe = subport->pipe + grinder->pindex;
2750 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2751 grinder->productive = 0;
2753 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2754 grinder_next_tc(port, subport, pos);
2756 /* Check for pipe exhaustion */
2757 if (grinder->pindex == subport->pipe_loop) {
2758 subport->pipe_exhaustion = 1;
2759 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2767 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2769 struct rte_sched_grinder *grinder = subport->grinder + pos;
2770 struct rte_sched_pipe *pipe = grinder->pipe;
2771 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2772 uint32_t qmask = grinder->qmask;
2774 grinder->wrr_tokens[0] =
2775 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2776 grinder->wrr_tokens[1] =
2777 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2778 grinder->wrr_tokens[2] =
2779 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2780 grinder->wrr_tokens[3] =
2781 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2783 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2784 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2785 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2786 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2788 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2789 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2790 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2791 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2795 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2797 struct rte_sched_grinder *grinder = subport->grinder + pos;
2798 struct rte_sched_pipe *pipe = grinder->pipe;
2800 pipe->wrr_tokens[0] =
2801 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2802 RTE_SCHED_WRR_SHIFT;
2803 pipe->wrr_tokens[1] =
2804 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2805 RTE_SCHED_WRR_SHIFT;
2806 pipe->wrr_tokens[2] =
2807 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2808 RTE_SCHED_WRR_SHIFT;
2809 pipe->wrr_tokens[3] =
2810 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2811 RTE_SCHED_WRR_SHIFT;
2815 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2817 struct rte_sched_grinder *grinder = subport->grinder + pos;
2818 uint16_t wrr_tokens_min;
2820 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2821 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2822 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2823 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2825 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2826 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2828 grinder->wrr_tokens[0] -= wrr_tokens_min;
2829 grinder->wrr_tokens[1] -= wrr_tokens_min;
2830 grinder->wrr_tokens[2] -= wrr_tokens_min;
2831 grinder->wrr_tokens[3] -= wrr_tokens_min;
2835 #define grinder_evict(subport, pos)
2838 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2840 struct rte_sched_grinder *grinder = subport->grinder + pos;
2842 rte_prefetch0(grinder->pipe);
2843 rte_prefetch0(grinder->queue[0]);
2847 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2849 struct rte_sched_grinder *grinder = subport->grinder + pos;
2850 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2852 qsize = grinder->qsize;
2855 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2856 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2858 rte_prefetch0(grinder->qbase[0] + qr[0]);
2862 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2863 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2864 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2865 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2867 rte_prefetch0(grinder->qbase[0] + qr[0]);
2868 rte_prefetch0(grinder->qbase[1] + qr[1]);
2870 grinder_wrr_load(subport, pos);
2871 grinder_wrr(subport, pos);
2873 rte_prefetch0(grinder->qbase[2] + qr[2]);
2874 rte_prefetch0(grinder->qbase[3] + qr[3]);
2878 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2880 struct rte_sched_grinder *grinder = subport->grinder + pos;
2881 uint32_t qpos = grinder->qpos;
2882 struct rte_mbuf **qbase = grinder->qbase[qpos];
2883 uint16_t qsize = grinder->qsize;
2884 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2886 grinder->pkt = qbase[qr];
2887 rte_prefetch0(grinder->pkt);
2889 if (unlikely((qr & 0x7) == 7)) {
2890 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2892 rte_prefetch0(qbase + qr_next);
2896 static inline uint32_t
2897 grinder_handle(struct rte_sched_port *port,
2898 struct rte_sched_subport *subport, uint32_t pos)
2900 struct rte_sched_grinder *grinder = subport->grinder + pos;
2902 switch (grinder->state) {
2903 case e_GRINDER_PREFETCH_PIPE:
2905 if (grinder_next_pipe(port, subport, pos)) {
2906 grinder_prefetch_pipe(subport, pos);
2907 subport->busy_grinders++;
2909 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2916 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2918 struct rte_sched_pipe *pipe = grinder->pipe;
2920 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2921 grinder->subport_params = port->subport_profiles +
2924 grinder_prefetch_tc_queue_arrays(subport, pos);
2925 grinder_credits_update(port, subport, pos);
2927 grinder->state = e_GRINDER_PREFETCH_MBUF;
2931 case e_GRINDER_PREFETCH_MBUF:
2933 grinder_prefetch_mbuf(subport, pos);
2935 grinder->state = e_GRINDER_READ_MBUF;
2939 case e_GRINDER_READ_MBUF:
2941 uint32_t wrr_active, result = 0;
2943 result = grinder_schedule(port, subport, pos);
2945 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2947 /* Look for next packet within the same TC */
2948 if (result && grinder->qmask) {
2950 grinder_wrr(subport, pos);
2952 grinder_prefetch_mbuf(subport, pos);
2958 grinder_wrr_store(subport, pos);
2960 /* Look for another active TC within same pipe */
2961 if (grinder_next_tc(port, subport, pos)) {
2962 grinder_prefetch_tc_queue_arrays(subport, pos);
2964 grinder->state = e_GRINDER_PREFETCH_MBUF;
2968 if (grinder->productive == 0 &&
2969 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2970 subport->pipe_loop = grinder->pindex;
2972 grinder_evict(subport, pos);
2974 /* Look for another active pipe */
2975 if (grinder_next_pipe(port, subport, pos)) {
2976 grinder_prefetch_pipe(subport, pos);
2978 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2982 /* No active pipe found */
2983 subport->busy_grinders--;
2985 grinder->state = e_GRINDER_PREFETCH_PIPE;
2990 rte_panic("Algorithmic error (invalid state)\n");
2996 rte_sched_port_time_resync(struct rte_sched_port *port)
2998 uint64_t cycles = rte_get_tsc_cycles();
2999 uint64_t cycles_diff;
3000 uint64_t bytes_diff;
3003 if (cycles < port->time_cpu_cycles)
3004 port->time_cpu_cycles = 0;
3006 cycles_diff = cycles - port->time_cpu_cycles;
3007 /* Compute elapsed time in bytes */
3008 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
3009 port->inv_cycles_per_byte);
3011 /* Advance port time */
3012 port->time_cpu_cycles +=
3013 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
3014 port->time_cpu_bytes += bytes_diff;
3015 if (port->time < port->time_cpu_bytes)
3016 port->time = port->time_cpu_bytes;
3018 /* Reset pipe loop detection */
3019 for (i = 0; i < port->n_subports_per_port; i++)
3020 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
3024 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
3028 /* Check if any exception flag is set */
3029 exceptions = (second_pass && subport->busy_grinders == 0) ||
3030 (subport->pipe_exhaustion == 1);
3032 /* Clear exception flags */
3033 subport->pipe_exhaustion = 0;
3039 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
3041 struct rte_sched_subport *subport;
3042 uint32_t subport_id = port->subport_id;
3043 uint32_t i, n_subports = 0, count;
3045 port->pkts_out = pkts;
3046 port->n_pkts_out = 0;
3048 rte_sched_port_time_resync(port);
3050 /* Take each queue in the grinder one step further */
3051 for (i = 0, count = 0; ; i++) {
3052 subport = port->subports[subport_id];
3054 count += grinder_handle(port, subport,
3055 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
3057 if (count == n_pkts) {
3060 if (subport_id == port->n_subports_per_port)
3063 port->subport_id = subport_id;
3067 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
3073 if (subport_id == port->n_subports_per_port)
3076 if (n_subports == port->n_subports_per_port) {
3077 port->subport_id = subport_id;