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 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
27 #define RTE_SCHED_WRR_SHIFT 3
28 #define RTE_SCHED_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
29 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
30 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
31 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
33 /* Scaling for cycles_per_byte calculation
34 * Chosen so that minimum rate is 480 bit/sec
36 #define RTE_SCHED_TIME_SHIFT 8
38 struct rte_sched_pipe_profile {
39 /* Token bucket (TB) */
41 uint64_t tb_credits_per_period;
44 /* Pipe traffic classes */
46 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
49 /* Pipe best-effort traffic class queues */
50 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
53 struct rte_sched_pipe {
54 /* Token bucket (TB) */
55 uint64_t tb_time; /* time of last update */
58 /* Pipe profile and flags */
61 /* Traffic classes (TCs) */
62 uint64_t tc_time; /* time of next update */
63 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
65 /* Weighted Round Robin (WRR) */
66 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
68 /* TC oversubscription */
69 uint64_t tc_ov_credits;
70 uint8_t tc_ov_period_id;
71 } __rte_cache_aligned;
73 struct rte_sched_queue {
78 struct rte_sched_queue_extra {
79 struct rte_sched_queue_stats stats;
90 e_GRINDER_PREFETCH_PIPE = 0,
91 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
92 e_GRINDER_PREFETCH_MBUF,
96 struct rte_sched_subport_profile {
97 /* Token bucket (TB) */
99 uint64_t tb_credits_per_period;
102 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
106 struct rte_sched_grinder {
108 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
109 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
114 enum grinder_state state;
117 struct rte_sched_subport *subport;
118 struct rte_sched_subport_profile *subport_params;
119 struct rte_sched_pipe *pipe;
120 struct rte_sched_pipe_profile *pipe_params;
123 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
124 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
130 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
131 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
132 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
136 struct rte_mbuf *pkt;
139 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
140 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
141 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
144 struct rte_sched_subport {
145 /* Token bucket (TB) */
146 uint64_t tb_time; /* time of last update */
149 /* Traffic classes (TCs) */
150 uint64_t tc_time; /* time of next update */
151 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
153 /* TC oversubscription */
155 uint64_t tc_ov_wm_min;
156 uint64_t tc_ov_wm_max;
157 uint8_t tc_ov_period_id;
163 struct rte_sched_subport_stats stats __rte_cache_aligned;
165 /* subport profile */
168 uint32_t n_pipes_per_subport_enabled;
169 uint32_t n_pipe_profiles;
170 uint32_t n_max_pipe_profiles;
172 /* Pipe best-effort TC rate */
173 uint64_t pipe_tc_be_rate_max;
175 /* Pipe queues size */
176 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
178 #ifdef RTE_SCHED_CMAN
180 enum rte_sched_cman_mode cman;
184 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
185 struct rte_pie_config pie_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
189 /* Scheduling loop detection */
191 uint32_t pipe_exhaustion;
194 struct rte_bitmap *bmp;
195 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
198 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
199 uint32_t busy_grinders;
201 /* Queue base calculation */
202 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
205 struct rte_sched_pipe *pipe;
206 struct rte_sched_queue *queue;
207 struct rte_sched_queue_extra *queue_extra;
208 struct rte_sched_pipe_profile *pipe_profiles;
210 struct rte_mbuf **queue_array;
211 uint8_t memory[0] __rte_cache_aligned;
212 } __rte_cache_aligned;
214 struct rte_sched_port {
215 /* User parameters */
216 uint32_t n_subports_per_port;
217 uint32_t n_pipes_per_subport;
218 uint32_t n_pipes_per_subport_log2;
219 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
220 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
221 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
222 uint32_t n_subport_profiles;
223 uint32_t n_max_subport_profiles;
226 uint32_t frame_overhead;
230 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cycles */
231 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
232 uint64_t time; /* Current NIC TX time measured in bytes */
233 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
234 uint64_t cycles_per_byte;
237 struct rte_mbuf **pkts_out;
241 /* Large data structures */
242 struct rte_sched_subport_profile *subport_profiles;
243 struct rte_sched_subport *subports[0] __rte_cache_aligned;
244 } __rte_cache_aligned;
246 enum rte_sched_subport_array {
247 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
248 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
249 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
250 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
251 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
252 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
253 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
256 static inline uint32_t
257 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
259 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
262 static inline struct rte_mbuf **
263 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
265 uint32_t pindex = qindex >> 4;
266 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
268 return (subport->queue_array + pindex *
269 subport->qsize_sum + subport->qsize_add[qpos]);
272 static inline uint16_t
273 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
274 struct rte_sched_subport *subport, uint32_t qindex)
276 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
278 return subport->qsize[tc];
281 static inline uint32_t
282 rte_sched_port_queues_per_port(struct rte_sched_port *port)
284 uint32_t n_queues = 0, i;
286 for (i = 0; i < port->n_subports_per_port; i++)
287 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
292 static inline uint16_t
293 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
295 uint16_t pipe_queue = port->pipe_queue[traffic_class];
300 static inline uint8_t
301 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
303 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
308 static inline uint8_t
309 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
311 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
317 pipe_profile_check(struct rte_sched_pipe_params *params,
318 uint64_t rate, uint16_t *qsize)
322 /* Pipe parameters */
323 if (params == NULL) {
325 "%s: Incorrect value for parameter params\n", __func__);
329 /* TB rate: non-zero, not greater than port rate */
330 if (params->tb_rate == 0 ||
331 params->tb_rate > rate) {
333 "%s: Incorrect value for tb rate\n", __func__);
337 /* TB size: non-zero */
338 if (params->tb_size == 0) {
340 "%s: Incorrect value for tb size\n", __func__);
344 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
345 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
346 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
347 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
348 params->tc_rate[i] > params->tb_rate))) {
350 "%s: Incorrect value for qsize or tc_rate\n", __func__);
355 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
356 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
358 "%s: Incorrect value for be traffic class rate\n", __func__);
362 /* TC period: non-zero */
363 if (params->tc_period == 0) {
365 "%s: Incorrect value for tc period\n", __func__);
369 /* Best effort tc oversubscription weight: non-zero */
370 if (params->tc_ov_weight == 0) {
372 "%s: Incorrect value for tc ov weight\n", __func__);
376 /* Queue WRR weights: non-zero */
377 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
378 if (params->wrr_weights[i] == 0) {
380 "%s: Incorrect value for wrr weight\n", __func__);
389 subport_profile_check(struct rte_sched_subport_profile_params *params,
394 /* Check user parameters */
395 if (params == NULL) {
396 RTE_LOG(ERR, SCHED, "%s: "
397 "Incorrect value for parameter params\n", __func__);
401 if (params->tb_rate == 0 || params->tb_rate > rate) {
402 RTE_LOG(ERR, SCHED, "%s: "
403 "Incorrect value for tb rate\n", __func__);
407 if (params->tb_size == 0) {
408 RTE_LOG(ERR, SCHED, "%s: "
409 "Incorrect value for tb size\n", __func__);
413 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
414 uint64_t tc_rate = params->tc_rate[i];
416 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
417 RTE_LOG(ERR, SCHED, "%s: "
418 "Incorrect value for tc rate\n", __func__);
423 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
424 RTE_LOG(ERR, SCHED, "%s: "
425 "Incorrect tc rate(best effort)\n", __func__);
429 if (params->tc_period == 0) {
430 RTE_LOG(ERR, SCHED, "%s: "
431 "Incorrect value for tc period\n", __func__);
439 rte_sched_port_check_params(struct rte_sched_port_params *params)
443 if (params == NULL) {
445 "%s: Incorrect value for parameter params\n", __func__);
450 if (params->socket < 0) {
452 "%s: Incorrect value for socket id\n", __func__);
457 if (params->rate == 0) {
459 "%s: Incorrect value for rate\n", __func__);
464 if (params->mtu == 0) {
466 "%s: Incorrect value for mtu\n", __func__);
470 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
471 if (params->n_subports_per_port == 0 ||
472 params->n_subports_per_port > 1u << 16 ||
473 !rte_is_power_of_2(params->n_subports_per_port)) {
475 "%s: Incorrect value for number of subports\n", __func__);
479 if (params->subport_profiles == NULL ||
480 params->n_subport_profiles == 0 ||
481 params->n_max_subport_profiles == 0 ||
482 params->n_subport_profiles > params->n_max_subport_profiles) {
484 "%s: Incorrect value for subport profiles\n", __func__);
488 for (i = 0; i < params->n_subport_profiles; i++) {
489 struct rte_sched_subport_profile_params *p =
490 params->subport_profiles + i;
493 status = subport_profile_check(p, params->rate);
496 "%s: subport profile check failed(%d)\n",
502 /* n_pipes_per_subport: non-zero, power of 2 */
503 if (params->n_pipes_per_subport == 0 ||
504 !rte_is_power_of_2(params->n_pipes_per_subport)) {
506 "%s: Incorrect value for maximum pipes number\n", __func__);
514 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
515 enum rte_sched_subport_array array)
517 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
518 uint32_t n_subport_pipe_queues =
519 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
521 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
522 uint32_t size_queue =
523 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
524 uint32_t size_queue_extra
525 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
526 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
527 sizeof(struct rte_sched_pipe_profile);
528 uint32_t size_bmp_array =
529 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
530 uint32_t size_per_pipe_queue_array, size_queue_array;
534 size_per_pipe_queue_array = 0;
535 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
536 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
537 size_per_pipe_queue_array +=
538 params->qsize[i] * sizeof(struct rte_mbuf *);
540 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
541 params->qsize[i] * sizeof(struct rte_mbuf *);
543 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
547 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
549 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
551 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
553 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
555 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
557 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
559 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
561 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
563 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
565 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
567 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
569 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
575 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
579 subport->qsize_add[0] = 0;
581 /* Strict priority traffic class */
582 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
583 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
585 /* Best-effort traffic class */
586 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
587 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
588 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
589 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
590 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
591 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
592 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
593 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
594 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
596 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
597 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
601 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
603 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
605 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
606 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
607 " Traffic classes: period = %"PRIu64",\n"
608 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
609 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
610 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
611 " Best-effort traffic class oversubscription: weight = %hhu\n"
612 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
617 p->tb_credits_per_period,
620 /* Traffic classes */
622 p->tc_credits_per_period[0],
623 p->tc_credits_per_period[1],
624 p->tc_credits_per_period[2],
625 p->tc_credits_per_period[3],
626 p->tc_credits_per_period[4],
627 p->tc_credits_per_period[5],
628 p->tc_credits_per_period[6],
629 p->tc_credits_per_period[7],
630 p->tc_credits_per_period[8],
631 p->tc_credits_per_period[9],
632 p->tc_credits_per_period[10],
633 p->tc_credits_per_period[11],
634 p->tc_credits_per_period[12],
636 /* Best-effort traffic class oversubscription */
640 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
644 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
646 struct rte_sched_subport_profile *p = port->subport_profiles + i;
648 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
649 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
651 "Traffic classes: period = %"PRIu64",\n"
652 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
653 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
654 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
659 p->tb_credits_per_period,
662 /* Traffic classes */
664 p->tc_credits_per_period[0],
665 p->tc_credits_per_period[1],
666 p->tc_credits_per_period[2],
667 p->tc_credits_per_period[3],
668 p->tc_credits_per_period[4],
669 p->tc_credits_per_period[5],
670 p->tc_credits_per_period[6],
671 p->tc_credits_per_period[7],
672 p->tc_credits_per_period[8],
673 p->tc_credits_per_period[9],
674 p->tc_credits_per_period[10],
675 p->tc_credits_per_period[11],
676 p->tc_credits_per_period[12]);
679 static inline uint64_t
680 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
682 uint64_t time = time_ms;
684 time = (time * rate) / 1000;
690 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
691 struct rte_sched_pipe_params *src,
692 struct rte_sched_pipe_profile *dst,
695 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
696 uint32_t lcd1, lcd2, lcd;
700 if (src->tb_rate == rate) {
701 dst->tb_credits_per_period = 1;
704 double tb_rate = (double) src->tb_rate
706 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
708 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
712 dst->tb_size = src->tb_size;
714 /* Traffic Classes */
715 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
718 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
719 if (subport->qsize[i])
720 dst->tc_credits_per_period[i]
721 = rte_sched_time_ms_to_bytes(src->tc_period,
724 dst->tc_ov_weight = src->tc_ov_weight;
727 wrr_cost[0] = src->wrr_weights[0];
728 wrr_cost[1] = src->wrr_weights[1];
729 wrr_cost[2] = src->wrr_weights[2];
730 wrr_cost[3] = src->wrr_weights[3];
732 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
733 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
734 lcd = rte_get_lcd(lcd1, lcd2);
736 wrr_cost[0] = lcd / wrr_cost[0];
737 wrr_cost[1] = lcd / wrr_cost[1];
738 wrr_cost[2] = lcd / wrr_cost[2];
739 wrr_cost[3] = lcd / wrr_cost[3];
741 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
742 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
743 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
744 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
748 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
749 struct rte_sched_subport_profile *dst,
755 if (src->tb_rate == rate) {
756 dst->tb_credits_per_period = 1;
759 double tb_rate = (double) src->tb_rate
761 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
763 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
767 dst->tb_size = src->tb_size;
769 /* Traffic Classes */
770 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
772 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
773 dst->tc_credits_per_period[i]
774 = rte_sched_time_ms_to_bytes(src->tc_period,
779 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
780 struct rte_sched_subport_params *params, uint64_t rate)
784 for (i = 0; i < subport->n_pipe_profiles; i++) {
785 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
786 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
788 rte_sched_pipe_profile_convert(subport, src, dst, rate);
789 rte_sched_port_log_pipe_profile(subport, i);
792 subport->pipe_tc_be_rate_max = 0;
793 for (i = 0; i < subport->n_pipe_profiles; i++) {
794 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
795 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
797 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
798 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
803 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
804 struct rte_sched_port_params *params,
809 for (i = 0; i < port->n_subport_profiles; i++) {
810 struct rte_sched_subport_profile_params *src
811 = params->subport_profiles + i;
812 struct rte_sched_subport_profile *dst
813 = port->subport_profiles + i;
815 rte_sched_subport_profile_convert(src, dst, rate);
816 rte_sched_port_log_subport_profile(port, i);
821 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
822 uint32_t n_max_pipes_per_subport,
827 /* Check user parameters */
828 if (params == NULL) {
830 "%s: Incorrect value for parameter params\n", __func__);
834 /* qsize: if non-zero, power of 2,
835 * no bigger than 32K (due to 16-bit read/write pointers)
837 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
838 uint16_t qsize = params->qsize[i];
840 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
842 "%s: Incorrect value for qsize\n", __func__);
847 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
848 RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
852 /* n_pipes_per_subport: non-zero, power of 2 */
853 if (params->n_pipes_per_subport_enabled == 0 ||
854 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
855 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
857 "%s: Incorrect value for pipes number\n", __func__);
861 /* pipe_profiles and n_pipe_profiles */
862 if (params->pipe_profiles == NULL ||
863 params->n_pipe_profiles == 0 ||
864 params->n_max_pipe_profiles == 0 ||
865 params->n_pipe_profiles > params->n_max_pipe_profiles) {
867 "%s: Incorrect value for pipe profiles\n", __func__);
871 for (i = 0; i < params->n_pipe_profiles; i++) {
872 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
875 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
878 "%s: Pipe profile check failed(%d)\n", __func__, status);
887 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
888 struct rte_sched_subport_params **subport_params)
890 uint32_t size0 = 0, size1 = 0, i;
893 status = rte_sched_port_check_params(port_params);
896 "%s: Port scheduler port params check failed (%d)\n",
902 for (i = 0; i < port_params->n_subports_per_port; i++) {
903 struct rte_sched_subport_params *sp = subport_params[i];
905 status = rte_sched_subport_check_params(sp,
906 port_params->n_pipes_per_subport,
910 "%s: Port scheduler subport params check failed (%d)\n",
917 size0 = sizeof(struct rte_sched_port);
919 for (i = 0; i < port_params->n_subports_per_port; i++) {
920 struct rte_sched_subport_params *sp = subport_params[i];
922 size1 += rte_sched_subport_get_array_base(sp,
923 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
926 return size0 + size1;
929 struct rte_sched_port *
930 rte_sched_port_config(struct rte_sched_port_params *params)
932 struct rte_sched_port *port = NULL;
933 uint32_t size0, size1, size2;
934 uint32_t cycles_per_byte;
938 status = rte_sched_port_check_params(params);
941 "%s: Port scheduler params check failed (%d)\n",
946 size0 = sizeof(struct rte_sched_port);
947 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
948 size2 = params->n_max_subport_profiles *
949 sizeof(struct rte_sched_subport_profile);
951 /* Allocate memory to store the data structures */
952 port = rte_zmalloc_socket("qos_params", size0 + size1,
953 RTE_CACHE_LINE_SIZE, params->socket);
955 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
960 /* Allocate memory to store the subport profile */
961 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
962 RTE_CACHE_LINE_SIZE, params->socket);
963 if (port->subport_profiles == NULL) {
964 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
969 /* User parameters */
970 port->n_subports_per_port = params->n_subports_per_port;
971 port->n_subport_profiles = params->n_subport_profiles;
972 port->n_max_subport_profiles = params->n_max_subport_profiles;
973 port->n_pipes_per_subport = params->n_pipes_per_subport;
974 port->n_pipes_per_subport_log2 =
975 __builtin_ctz(params->n_pipes_per_subport);
976 port->socket = params->socket;
978 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
979 port->pipe_queue[i] = i;
981 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
982 port->pipe_tc[i] = j;
984 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
988 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
989 port->tc_queue[i] = j;
991 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
994 port->rate = params->rate;
995 port->mtu = params->mtu + params->frame_overhead;
996 port->frame_overhead = params->frame_overhead;
999 port->time_cpu_cycles = rte_get_tsc_cycles();
1000 port->time_cpu_bytes = 0;
1003 /* Subport profile table */
1004 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1006 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1008 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1009 port->cycles_per_byte = cycles_per_byte;
1012 port->pkts_out = NULL;
1013 port->n_pkts_out = 0;
1014 port->subport_id = 0;
1020 rte_sched_subport_free(struct rte_sched_port *port,
1021 struct rte_sched_subport *subport)
1023 uint32_t n_subport_pipe_queues;
1026 if (subport == NULL)
1029 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1031 /* Free enqueued mbufs */
1032 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1033 struct rte_mbuf **mbufs =
1034 rte_sched_subport_pipe_qbase(subport, qindex);
1035 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1037 struct rte_sched_queue *queue = subport->queue + qindex;
1038 uint16_t qr = queue->qr & (qsize - 1);
1039 uint16_t qw = queue->qw & (qsize - 1);
1041 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1042 rte_pktmbuf_free(mbufs[qr]);
1050 rte_sched_port_free(struct rte_sched_port *port)
1054 /* Check user parameters */
1058 for (i = 0; i < port->n_subports_per_port; i++)
1059 rte_sched_subport_free(port, port->subports[i]);
1061 rte_free(port->subport_profiles);
1066 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1070 for (i = 0; i < n_subports; i++) {
1071 struct rte_sched_subport *subport = port->subports[i];
1073 rte_sched_subport_free(port, subport);
1076 rte_free(port->subport_profiles);
1080 #ifdef RTE_SCHED_CMAN
1082 rte_sched_red_config(struct rte_sched_port *port,
1083 struct rte_sched_subport *s,
1084 struct rte_sched_subport_params *params,
1085 uint32_t n_subports)
1089 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1093 for (j = 0; j < RTE_COLORS; j++) {
1094 /* if min/max are both zero, then RED is disabled */
1095 if ((params->cman_params->red_params[i][j].min_th |
1096 params->cman_params->red_params[i][j].max_th) == 0) {
1100 if (rte_red_config_init(&s->red_config[i][j],
1101 params->cman_params->red_params[i][j].wq_log2,
1102 params->cman_params->red_params[i][j].min_th,
1103 params->cman_params->red_params[i][j].max_th,
1104 params->cman_params->red_params[i][j].maxp_inv) != 0) {
1105 rte_sched_free_memory(port, n_subports);
1107 RTE_LOG(NOTICE, SCHED,
1108 "%s: RED configuration init fails\n", __func__);
1113 s->cman = RTE_SCHED_CMAN_RED;
1118 rte_sched_pie_config(struct rte_sched_port *port,
1119 struct rte_sched_subport *s,
1120 struct rte_sched_subport_params *params,
1121 uint32_t n_subports)
1125 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1126 if (params->cman_params->pie_params[i].tailq_th > params->qsize[i]) {
1127 RTE_LOG(NOTICE, SCHED,
1128 "%s: PIE tailq threshold incorrect\n", __func__);
1132 if (rte_pie_config_init(&s->pie_config[i],
1133 params->cman_params->pie_params[i].qdelay_ref,
1134 params->cman_params->pie_params[i].dp_update_interval,
1135 params->cman_params->pie_params[i].max_burst,
1136 params->cman_params->pie_params[i].tailq_th) != 0) {
1137 rte_sched_free_memory(port, n_subports);
1139 RTE_LOG(NOTICE, SCHED,
1140 "%s: PIE configuration init fails\n", __func__);
1144 s->cman = RTE_SCHED_CMAN_PIE;
1149 rte_sched_cman_config(struct rte_sched_port *port,
1150 struct rte_sched_subport *s,
1151 struct rte_sched_subport_params *params,
1152 uint32_t n_subports)
1154 if (params->cman_params->cman_mode == RTE_SCHED_CMAN_RED)
1155 return rte_sched_red_config(port, s, params, n_subports);
1157 else if (params->cman_params->cman_mode == RTE_SCHED_CMAN_PIE)
1158 return rte_sched_pie_config(port, s, params, n_subports);
1165 rte_sched_subport_config(struct rte_sched_port *port,
1166 uint32_t subport_id,
1167 struct rte_sched_subport_params *params,
1168 uint32_t subport_profile_id)
1170 struct rte_sched_subport *s = NULL;
1171 uint32_t n_subports = subport_id;
1172 struct rte_sched_subport_profile *profile;
1173 uint32_t n_subport_pipe_queues, i;
1174 uint32_t size0, size1, bmp_mem_size;
1178 /* Check user parameters */
1181 "%s: Incorrect value for parameter port\n", __func__);
1185 if (subport_id >= port->n_subports_per_port) {
1187 "%s: Incorrect value for subport id\n", __func__);
1192 if (subport_profile_id >= port->n_max_subport_profiles) {
1193 RTE_LOG(ERR, SCHED, "%s: "
1194 "Number of subport profile exceeds the max limit\n",
1200 /** Memory is allocated only on first invocation of the api for a
1201 * given subport. Subsequent invocation on same subport will just
1202 * update subport bandwidth parameter.
1204 if (port->subports[subport_id] == NULL) {
1206 status = rte_sched_subport_check_params(params,
1207 port->n_pipes_per_subport,
1210 RTE_LOG(NOTICE, SCHED,
1211 "%s: Port scheduler params check failed (%d)\n",
1217 /* Determine the amount of memory to allocate */
1218 size0 = sizeof(struct rte_sched_subport);
1219 size1 = rte_sched_subport_get_array_base(params,
1220 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1222 /* Allocate memory to store the data structures */
1223 s = rte_zmalloc_socket("subport_params", size0 + size1,
1224 RTE_CACHE_LINE_SIZE, port->socket);
1227 "%s: Memory allocation fails\n", __func__);
1234 subport_profile_id = 0;
1237 port->subports[subport_id] = s;
1239 s->tb_time = port->time;
1241 /* compile time checks */
1242 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1243 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1244 (RTE_SCHED_PORT_N_GRINDERS - 1));
1246 /* User parameters */
1247 s->n_pipes_per_subport_enabled =
1248 params->n_pipes_per_subport_enabled;
1249 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1250 s->n_pipe_profiles = params->n_pipe_profiles;
1251 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1253 #ifdef RTE_SCHED_CMAN
1254 if (params->cman_params != NULL) {
1255 s->cman_enabled = true;
1256 status = rte_sched_cman_config(port, s, params, n_subports);
1258 RTE_LOG(NOTICE, SCHED,
1259 "%s: CMAN configuration fails\n", __func__);
1263 s->cman_enabled = false;
1267 /* Scheduling loop detection */
1268 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1269 s->pipe_exhaustion = 0;
1272 s->busy_grinders = 0;
1274 /* Queue base calculation */
1275 rte_sched_subport_config_qsize(s);
1277 /* Large data structures */
1278 s->pipe = (struct rte_sched_pipe *)
1279 (s->memory + rte_sched_subport_get_array_base(params,
1280 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1281 s->queue = (struct rte_sched_queue *)
1282 (s->memory + rte_sched_subport_get_array_base(params,
1283 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1284 s->queue_extra = (struct rte_sched_queue_extra *)
1285 (s->memory + rte_sched_subport_get_array_base(params,
1286 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1287 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1288 (s->memory + rte_sched_subport_get_array_base(params,
1289 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1290 s->bmp_array = s->memory + rte_sched_subport_get_array_base(
1291 params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1292 s->queue_array = (struct rte_mbuf **)
1293 (s->memory + rte_sched_subport_get_array_base(params,
1294 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1296 /* Pipe profile table */
1297 rte_sched_subport_config_pipe_profile_table(s, params,
1301 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1302 bmp_mem_size = rte_bitmap_get_memory_footprint(
1303 n_subport_pipe_queues);
1304 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1306 if (s->bmp == NULL) {
1308 "%s: Subport bitmap init error\n", __func__);
1313 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1314 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1316 #ifdef RTE_SCHED_SUBPORT_TC_OV
1317 /* TC oversubscription */
1318 s->tc_ov_wm_min = port->mtu;
1319 s->tc_ov_period_id = 0;
1327 /* update subport parameters from subport profile table*/
1328 profile = port->subport_profiles + subport_profile_id;
1330 s = port->subports[subport_id];
1332 s->tb_credits = profile->tb_size / 2;
1334 s->tc_time = port->time + profile->tc_period;
1336 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1339 profile->tc_credits_per_period[i];
1341 profile->tc_credits_per_period[i] = 0;
1343 #ifdef RTE_SCHED_SUBPORT_TC_OV
1344 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1345 s->pipe_tc_be_rate_max);
1346 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 */
1777 #ifdef RTE_SCHED_COLLECT_STATS
1780 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1781 struct rte_sched_subport *subport,
1783 struct rte_mbuf *pkt)
1785 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1786 uint32_t pkt_len = pkt->pkt_len;
1788 subport->stats.n_pkts_tc[tc_index] += 1;
1789 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1793 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1794 struct rte_sched_subport *subport,
1796 struct rte_mbuf *pkt,
1797 __rte_unused uint32_t n_pkts_cman_dropped)
1799 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1800 uint32_t pkt_len = pkt->pkt_len;
1802 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1803 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1804 subport->stats.n_pkts_cman_dropped[tc_index] += n_pkts_cman_dropped;
1808 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1810 struct rte_mbuf *pkt)
1812 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1813 uint32_t pkt_len = pkt->pkt_len;
1815 qe->stats.n_pkts += 1;
1816 qe->stats.n_bytes += pkt_len;
1820 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1822 struct rte_mbuf *pkt,
1823 __rte_unused uint32_t n_pkts_cman_dropped)
1825 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1826 uint32_t pkt_len = pkt->pkt_len;
1828 qe->stats.n_pkts_dropped += 1;
1829 qe->stats.n_bytes_dropped += pkt_len;
1830 #ifdef RTE_SCHED_CMAN
1831 if (subport->cman_enabled)
1832 qe->stats.n_pkts_cman_dropped += n_pkts_cman_dropped;
1836 #endif /* RTE_SCHED_COLLECT_STATS */
1838 #ifdef RTE_SCHED_CMAN
1841 rte_sched_port_cman_drop(struct rte_sched_port *port,
1842 struct rte_sched_subport *subport,
1843 struct rte_mbuf *pkt,
1847 if (!subport->cman_enabled)
1850 struct rte_sched_queue_extra *qe;
1853 tc_index = rte_sched_port_pipe_tc(port, qindex);
1854 qe = subport->queue_extra + qindex;
1857 if (subport->cman == RTE_SCHED_CMAN_RED) {
1858 struct rte_red_config *red_cfg;
1859 struct rte_red *red;
1860 enum rte_color color;
1862 color = rte_sched_port_pkt_read_color(pkt);
1863 red_cfg = &subport->red_config[tc_index][color];
1865 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1870 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1874 struct rte_pie_config *pie_cfg = &subport->pie_config[tc_index];
1875 struct rte_pie *pie = &qe->pie;
1877 return rte_pie_enqueue(pie_cfg, pie, qlen, pkt->pkt_len, port->time_cpu_cycles);
1881 rte_sched_port_red_set_queue_empty_timestamp(struct rte_sched_port *port,
1882 struct rte_sched_subport *subport, uint32_t qindex)
1884 if (subport->cman_enabled) {
1885 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1886 if (subport->cman == RTE_SCHED_CMAN_RED) {
1887 struct rte_red *red = &qe->red;
1889 rte_red_mark_queue_empty(red, port->time);
1895 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport,
1896 uint32_t qindex, uint32_t pkt_len, uint64_t time) {
1897 if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_PIE) {
1898 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1899 struct rte_pie *pie = &qe->pie;
1901 /* Update queue length */
1903 pie->qlen_bytes -= pkt_len;
1905 rte_pie_dequeue(pie, pkt_len, time);
1911 static inline int rte_sched_port_cman_drop(struct rte_sched_port *port __rte_unused,
1912 struct rte_sched_subport *subport __rte_unused,
1913 struct rte_mbuf *pkt __rte_unused,
1914 uint32_t qindex __rte_unused,
1915 uint16_t qlen __rte_unused)
1920 #define rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex)
1923 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport __rte_unused,
1924 uint32_t qindex __rte_unused,
1925 uint32_t pkt_len __rte_unused,
1926 uint64_t time __rte_unused) {
1927 /* do-nothing when RTE_SCHED_CMAN not defined */
1930 #endif /* RTE_SCHED_CMAN */
1932 #ifdef RTE_SCHED_DEBUG
1935 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1942 rte_panic("Empty slab at position %u\n", bmp_pos);
1945 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1946 if (mask & bmp_slab) {
1947 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1948 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1955 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1959 #endif /* RTE_SCHED_DEBUG */
1961 static inline struct rte_sched_subport *
1962 rte_sched_port_subport(struct rte_sched_port *port,
1963 struct rte_mbuf *pkt)
1965 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1966 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1968 return port->subports[subport_id];
1971 static inline uint32_t
1972 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1973 struct rte_mbuf *pkt, uint32_t subport_qmask)
1975 struct rte_sched_queue *q;
1976 #ifdef RTE_SCHED_COLLECT_STATS
1977 struct rte_sched_queue_extra *qe;
1979 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1980 uint32_t subport_queue_id = subport_qmask & qindex;
1982 q = subport->queue + subport_queue_id;
1984 #ifdef RTE_SCHED_COLLECT_STATS
1985 qe = subport->queue_extra + subport_queue_id;
1989 return subport_queue_id;
1993 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1994 struct rte_sched_subport *subport,
1996 struct rte_mbuf **qbase)
1998 struct rte_sched_queue *q;
1999 struct rte_mbuf **q_qw;
2002 q = subport->queue + qindex;
2003 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2004 q_qw = qbase + (q->qw & (qsize - 1));
2006 rte_prefetch0(q_qw);
2007 rte_bitmap_prefetch0(subport->bmp, qindex);
2011 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
2012 struct rte_sched_subport *subport,
2014 struct rte_mbuf **qbase,
2015 struct rte_mbuf *pkt)
2017 struct rte_sched_queue *q;
2021 q = subport->queue + qindex;
2022 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2023 qlen = q->qw - q->qr;
2025 /* Drop the packet (and update drop stats) when queue is full */
2026 if (unlikely(rte_sched_port_cman_drop(port, subport, pkt, qindex, qlen) ||
2028 rte_pktmbuf_free(pkt);
2029 #ifdef RTE_SCHED_COLLECT_STATS
2030 rte_sched_port_update_subport_stats_on_drop(port, subport,
2031 qindex, pkt, qlen < qsize);
2032 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
2038 /* Enqueue packet */
2039 qbase[q->qw & (qsize - 1)] = pkt;
2042 /* Activate queue in the subport bitmap */
2043 rte_bitmap_set(subport->bmp, qindex);
2046 #ifdef RTE_SCHED_COLLECT_STATS
2047 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2048 rte_sched_port_update_queue_stats(subport, qindex, pkt);
2056 * The enqueue function implements a 4-level pipeline with each stage
2057 * processing two different packets. The purpose of using a pipeline
2058 * is to hide the latency of prefetching the data structures. The
2059 * naming convention is presented in the diagram below:
2061 * p00 _______ p10 _______ p20 _______ p30 _______
2062 * ----->| |----->| |----->| |----->| |----->
2063 * | 0 | | 1 | | 2 | | 3 |
2064 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2069 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2072 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2073 *pkt30, *pkt31, *pkt_last;
2074 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2075 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2076 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2077 *subport20, *subport21, *subport30, *subport31, *subport_last;
2078 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2079 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2080 uint32_t subport_qmask;
2084 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2087 * Less then 6 input packets available, which is not enough to
2090 if (unlikely(n_pkts < 6)) {
2091 struct rte_sched_subport *subports[5];
2092 struct rte_mbuf **q_base[5];
2095 /* Prefetch the mbuf structure of each packet */
2096 for (i = 0; i < n_pkts; i++)
2097 rte_prefetch0(pkts[i]);
2099 /* Prefetch the subport structure for each packet */
2100 for (i = 0; i < n_pkts; i++)
2101 subports[i] = rte_sched_port_subport(port, pkts[i]);
2103 /* Prefetch the queue structure for each queue */
2104 for (i = 0; i < n_pkts; i++)
2105 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2106 pkts[i], subport_qmask);
2108 /* Prefetch the write pointer location of each queue */
2109 for (i = 0; i < n_pkts; i++) {
2110 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2111 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2115 /* Write each packet to its queue */
2116 for (i = 0; i < n_pkts; i++)
2117 result += rte_sched_port_enqueue_qwa(port, subports[i],
2118 q[i], q_base[i], pkts[i]);
2123 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2126 rte_prefetch0(pkt20);
2127 rte_prefetch0(pkt21);
2131 rte_prefetch0(pkt10);
2132 rte_prefetch0(pkt11);
2134 subport20 = rte_sched_port_subport(port, pkt20);
2135 subport21 = rte_sched_port_subport(port, pkt21);
2136 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2137 pkt20, subport_qmask);
2138 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2139 pkt21, subport_qmask);
2143 rte_prefetch0(pkt00);
2144 rte_prefetch0(pkt01);
2146 subport10 = rte_sched_port_subport(port, pkt10);
2147 subport11 = rte_sched_port_subport(port, pkt11);
2148 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2149 pkt10, subport_qmask);
2150 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2151 pkt11, subport_qmask);
2153 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2154 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2155 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2156 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2158 /* Run the pipeline */
2159 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2160 /* Propagate stage inputs */
2171 subport30 = subport20;
2172 subport31 = subport21;
2173 subport20 = subport10;
2174 subport21 = subport11;
2175 q30_base = q20_base;
2176 q31_base = q21_base;
2178 /* Stage 0: Get packets in */
2180 pkt01 = pkts[i + 1];
2181 rte_prefetch0(pkt00);
2182 rte_prefetch0(pkt01);
2184 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2185 subport10 = rte_sched_port_subport(port, pkt10);
2186 subport11 = rte_sched_port_subport(port, pkt11);
2187 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2188 pkt10, subport_qmask);
2189 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2190 pkt11, subport_qmask);
2192 /* Stage 2: Prefetch queue write location */
2193 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2194 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2195 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2196 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2198 /* Stage 3: Write packet to queue and activate queue */
2199 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2200 q30, q30_base, pkt30);
2201 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2202 q31, q31_base, pkt31);
2203 result += r30 + r31;
2207 * Drain the pipeline (exactly 6 packets).
2208 * Handle the last packet in the case
2209 * of an odd number of input packets.
2211 pkt_last = pkts[n_pkts - 1];
2212 rte_prefetch0(pkt_last);
2214 subport00 = rte_sched_port_subport(port, pkt00);
2215 subport01 = rte_sched_port_subport(port, pkt01);
2216 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2217 pkt00, subport_qmask);
2218 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2219 pkt01, subport_qmask);
2221 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2222 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2223 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2224 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2226 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2227 q20, q20_base, pkt20);
2228 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2229 q21, q21_base, pkt21);
2230 result += r20 + r21;
2232 subport_last = rte_sched_port_subport(port, pkt_last);
2233 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2234 pkt_last, subport_qmask);
2236 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2237 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2238 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2239 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2241 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2243 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2245 result += r10 + r11;
2247 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2248 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2249 q_last, q_last_base);
2251 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2253 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2255 result += r00 + r01;
2258 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2259 q_last, q_last_base, pkt_last);
2266 #ifndef RTE_SCHED_SUBPORT_TC_OV
2269 grinder_credits_update(struct rte_sched_port *port,
2270 struct rte_sched_subport *subport, uint32_t pos)
2272 struct rte_sched_grinder *grinder = subport->grinder + pos;
2273 struct rte_sched_pipe *pipe = grinder->pipe;
2274 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2275 struct rte_sched_subport_profile *sp = grinder->subport_params;
2280 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2281 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2282 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2283 subport->tb_time += n_periods * sp->tb_period;
2286 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2287 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2288 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2289 pipe->tb_time += n_periods * params->tb_period;
2292 if (unlikely(port->time >= subport->tc_time)) {
2293 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2294 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2296 subport->tc_time = port->time + sp->tc_period;
2300 if (unlikely(port->time >= pipe->tc_time)) {
2301 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2302 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2304 pipe->tc_time = port->time + params->tc_period;
2310 static inline uint64_t
2311 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2312 struct rte_sched_subport *subport, uint32_t pos)
2314 struct rte_sched_grinder *grinder = subport->grinder + pos;
2315 struct rte_sched_subport_profile *sp = grinder->subport_params;
2316 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2317 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2318 uint64_t tc_ov_wm = subport->tc_ov_wm;
2321 if (subport->tc_ov == 0)
2322 return subport->tc_ov_wm_max;
2324 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2325 tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2326 - subport->tc_credits[i];
2327 tc_consumption += tc_ov_consumption[i];
2330 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2331 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2332 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2334 tc_ov_consumption_max =
2335 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2338 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2339 (tc_ov_consumption_max - port->mtu)) {
2340 tc_ov_wm -= tc_ov_wm >> 7;
2341 if (tc_ov_wm < subport->tc_ov_wm_min)
2342 tc_ov_wm = subport->tc_ov_wm_min;
2347 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2348 if (tc_ov_wm > subport->tc_ov_wm_max)
2349 tc_ov_wm = subport->tc_ov_wm_max;
2355 grinder_credits_update(struct rte_sched_port *port,
2356 struct rte_sched_subport *subport, uint32_t pos)
2358 struct rte_sched_grinder *grinder = subport->grinder + pos;
2359 struct rte_sched_pipe *pipe = grinder->pipe;
2360 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2361 struct rte_sched_subport_profile *sp = grinder->subport_params;
2366 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2367 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2368 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2369 subport->tb_time += n_periods * sp->tb_period;
2372 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2373 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2374 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2375 pipe->tb_time += n_periods * params->tb_period;
2378 if (unlikely(port->time >= subport->tc_time)) {
2380 grinder_tc_ov_credits_update(port, subport, pos);
2382 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2383 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2385 subport->tc_time = port->time + sp->tc_period;
2386 subport->tc_ov_period_id++;
2390 if (unlikely(port->time >= pipe->tc_time)) {
2391 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2392 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2393 pipe->tc_time = port->time + params->tc_period;
2396 /* Pipe TCs - Oversubscription */
2397 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2398 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2400 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2404 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2407 #ifndef RTE_SCHED_SUBPORT_TC_OV
2410 grinder_credits_check(struct rte_sched_port *port,
2411 struct rte_sched_subport *subport, uint32_t pos)
2413 struct rte_sched_grinder *grinder = subport->grinder + pos;
2414 struct rte_sched_pipe *pipe = grinder->pipe;
2415 struct rte_mbuf *pkt = grinder->pkt;
2416 uint32_t tc_index = grinder->tc_index;
2417 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2418 uint64_t subport_tb_credits = subport->tb_credits;
2419 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2420 uint64_t pipe_tb_credits = pipe->tb_credits;
2421 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2424 /* Check queue credits */
2425 enough_credits = (pkt_len <= subport_tb_credits) &&
2426 (pkt_len <= subport_tc_credits) &&
2427 (pkt_len <= pipe_tb_credits) &&
2428 (pkt_len <= pipe_tc_credits);
2430 if (!enough_credits)
2433 /* Update port credits */
2434 subport->tb_credits -= pkt_len;
2435 subport->tc_credits[tc_index] -= pkt_len;
2436 pipe->tb_credits -= pkt_len;
2437 pipe->tc_credits[tc_index] -= pkt_len;
2445 grinder_credits_check(struct rte_sched_port *port,
2446 struct rte_sched_subport *subport, uint32_t pos)
2448 struct rte_sched_grinder *grinder = subport->grinder + pos;
2449 struct rte_sched_pipe *pipe = grinder->pipe;
2450 struct rte_mbuf *pkt = grinder->pkt;
2451 uint32_t tc_index = grinder->tc_index;
2452 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2453 uint64_t subport_tb_credits = subport->tb_credits;
2454 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2455 uint64_t pipe_tb_credits = pipe->tb_credits;
2456 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2457 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2458 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2459 uint64_t pipe_tc_ov_credits;
2463 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2464 pipe_tc_ov_mask1[i] = ~0LLU;
2466 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2467 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2468 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2470 /* Check pipe and subport credits */
2471 enough_credits = (pkt_len <= subport_tb_credits) &&
2472 (pkt_len <= subport_tc_credits) &&
2473 (pkt_len <= pipe_tb_credits) &&
2474 (pkt_len <= pipe_tc_credits) &&
2475 (pkt_len <= pipe_tc_ov_credits);
2477 if (!enough_credits)
2480 /* Update pipe and subport credits */
2481 subport->tb_credits -= pkt_len;
2482 subport->tc_credits[tc_index] -= pkt_len;
2483 pipe->tb_credits -= pkt_len;
2484 pipe->tc_credits[tc_index] -= pkt_len;
2485 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2490 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2494 grinder_schedule(struct rte_sched_port *port,
2495 struct rte_sched_subport *subport, uint32_t pos)
2497 struct rte_sched_grinder *grinder = subport->grinder + pos;
2498 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2499 uint32_t qindex = grinder->qindex[grinder->qpos];
2500 struct rte_mbuf *pkt = grinder->pkt;
2501 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2502 uint32_t be_tc_active;
2504 if (!grinder_credits_check(port, subport, pos))
2507 /* Advance port time */
2508 port->time += pkt_len;
2511 port->pkts_out[port->n_pkts_out++] = pkt;
2514 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2515 grinder->wrr_tokens[grinder->qpos] +=
2516 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2518 if (queue->qr == queue->qw) {
2519 rte_bitmap_clear(subport->bmp, qindex);
2520 grinder->qmask &= ~(1 << grinder->qpos);
2522 grinder->wrr_mask[grinder->qpos] = 0;
2524 rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex);
2527 rte_sched_port_pie_dequeue(subport, qindex, pkt_len, port->time_cpu_cycles);
2529 /* Reset pipe loop detection */
2530 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2531 grinder->productive = 1;
2537 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2541 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2542 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2550 grinder_pcache_populate(struct rte_sched_subport *subport,
2551 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2553 struct rte_sched_grinder *grinder = subport->grinder + pos;
2556 grinder->pcache_w = 0;
2557 grinder->pcache_r = 0;
2559 w[0] = (uint16_t) bmp_slab;
2560 w[1] = (uint16_t) (bmp_slab >> 16);
2561 w[2] = (uint16_t) (bmp_slab >> 32);
2562 w[3] = (uint16_t) (bmp_slab >> 48);
2564 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2565 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2566 grinder->pcache_w += (w[0] != 0);
2568 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2569 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2570 grinder->pcache_w += (w[1] != 0);
2572 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2573 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2574 grinder->pcache_w += (w[2] != 0);
2576 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2577 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2578 grinder->pcache_w += (w[3] != 0);
2582 grinder_tccache_populate(struct rte_sched_subport *subport,
2583 uint32_t pos, uint32_t qindex, uint16_t qmask)
2585 struct rte_sched_grinder *grinder = subport->grinder + pos;
2588 grinder->tccache_w = 0;
2589 grinder->tccache_r = 0;
2591 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2592 b = (uint8_t) ((qmask >> i) & 0x1);
2593 grinder->tccache_qmask[grinder->tccache_w] = b;
2594 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2595 grinder->tccache_w += (b != 0);
2598 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2599 grinder->tccache_qmask[grinder->tccache_w] = b;
2600 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2601 RTE_SCHED_TRAFFIC_CLASS_BE;
2602 grinder->tccache_w += (b != 0);
2606 grinder_next_tc(struct rte_sched_port *port,
2607 struct rte_sched_subport *subport, uint32_t pos)
2609 struct rte_sched_grinder *grinder = subport->grinder + pos;
2610 struct rte_mbuf **qbase;
2614 if (grinder->tccache_r == grinder->tccache_w)
2617 qindex = grinder->tccache_qindex[grinder->tccache_r];
2618 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2619 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2621 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2622 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2623 grinder->qsize = qsize;
2625 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2626 grinder->queue[0] = subport->queue + qindex;
2627 grinder->qbase[0] = qbase;
2628 grinder->qindex[0] = qindex;
2629 grinder->tccache_r++;
2634 grinder->queue[0] = subport->queue + qindex;
2635 grinder->queue[1] = subport->queue + qindex + 1;
2636 grinder->queue[2] = subport->queue + qindex + 2;
2637 grinder->queue[3] = subport->queue + qindex + 3;
2639 grinder->qbase[0] = qbase;
2640 grinder->qbase[1] = qbase + qsize;
2641 grinder->qbase[2] = qbase + 2 * qsize;
2642 grinder->qbase[3] = qbase + 3 * qsize;
2644 grinder->qindex[0] = qindex;
2645 grinder->qindex[1] = qindex + 1;
2646 grinder->qindex[2] = qindex + 2;
2647 grinder->qindex[3] = qindex + 3;
2649 grinder->tccache_r++;
2654 grinder_next_pipe(struct rte_sched_port *port,
2655 struct rte_sched_subport *subport, uint32_t pos)
2657 struct rte_sched_grinder *grinder = subport->grinder + pos;
2658 uint32_t pipe_qindex;
2659 uint16_t pipe_qmask;
2661 if (grinder->pcache_r < grinder->pcache_w) {
2662 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2663 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2664 grinder->pcache_r++;
2666 uint64_t bmp_slab = 0;
2667 uint32_t bmp_pos = 0;
2669 /* Get another non-empty pipe group */
2670 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2673 #ifdef RTE_SCHED_DEBUG
2674 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2677 /* Return if pipe group already in one of the other grinders */
2678 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2679 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2682 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2684 /* Install new pipe group into grinder's pipe cache */
2685 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2687 pipe_qmask = grinder->pcache_qmask[0];
2688 pipe_qindex = grinder->pcache_qindex[0];
2689 grinder->pcache_r = 1;
2692 /* Install new pipe in the grinder */
2693 grinder->pindex = pipe_qindex >> 4;
2694 grinder->subport = subport;
2695 grinder->pipe = subport->pipe + grinder->pindex;
2696 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2697 grinder->productive = 0;
2699 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2700 grinder_next_tc(port, subport, pos);
2702 /* Check for pipe exhaustion */
2703 if (grinder->pindex == subport->pipe_loop) {
2704 subport->pipe_exhaustion = 1;
2705 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2713 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2715 struct rte_sched_grinder *grinder = subport->grinder + pos;
2716 struct rte_sched_pipe *pipe = grinder->pipe;
2717 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2718 uint32_t qmask = grinder->qmask;
2720 grinder->wrr_tokens[0] =
2721 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2722 grinder->wrr_tokens[1] =
2723 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2724 grinder->wrr_tokens[2] =
2725 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2726 grinder->wrr_tokens[3] =
2727 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2729 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2730 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2731 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2732 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2734 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2735 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2736 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2737 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2741 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2743 struct rte_sched_grinder *grinder = subport->grinder + pos;
2744 struct rte_sched_pipe *pipe = grinder->pipe;
2746 pipe->wrr_tokens[0] =
2747 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2748 RTE_SCHED_WRR_SHIFT;
2749 pipe->wrr_tokens[1] =
2750 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2751 RTE_SCHED_WRR_SHIFT;
2752 pipe->wrr_tokens[2] =
2753 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2754 RTE_SCHED_WRR_SHIFT;
2755 pipe->wrr_tokens[3] =
2756 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2757 RTE_SCHED_WRR_SHIFT;
2761 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2763 struct rte_sched_grinder *grinder = subport->grinder + pos;
2764 uint16_t wrr_tokens_min;
2766 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2767 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2768 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2769 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2771 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2772 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2774 grinder->wrr_tokens[0] -= wrr_tokens_min;
2775 grinder->wrr_tokens[1] -= wrr_tokens_min;
2776 grinder->wrr_tokens[2] -= wrr_tokens_min;
2777 grinder->wrr_tokens[3] -= wrr_tokens_min;
2781 #define grinder_evict(subport, pos)
2784 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2786 struct rte_sched_grinder *grinder = subport->grinder + pos;
2788 rte_prefetch0(grinder->pipe);
2789 rte_prefetch0(grinder->queue[0]);
2793 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2795 struct rte_sched_grinder *grinder = subport->grinder + pos;
2796 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2798 qsize = grinder->qsize;
2801 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2802 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2804 rte_prefetch0(grinder->qbase[0] + qr[0]);
2808 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2809 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2810 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2811 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2813 rte_prefetch0(grinder->qbase[0] + qr[0]);
2814 rte_prefetch0(grinder->qbase[1] + qr[1]);
2816 grinder_wrr_load(subport, pos);
2817 grinder_wrr(subport, pos);
2819 rte_prefetch0(grinder->qbase[2] + qr[2]);
2820 rte_prefetch0(grinder->qbase[3] + qr[3]);
2824 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2826 struct rte_sched_grinder *grinder = subport->grinder + pos;
2827 uint32_t qpos = grinder->qpos;
2828 struct rte_mbuf **qbase = grinder->qbase[qpos];
2829 uint16_t qsize = grinder->qsize;
2830 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2832 grinder->pkt = qbase[qr];
2833 rte_prefetch0(grinder->pkt);
2835 if (unlikely((qr & 0x7) == 7)) {
2836 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2838 rte_prefetch0(qbase + qr_next);
2842 static inline uint32_t
2843 grinder_handle(struct rte_sched_port *port,
2844 struct rte_sched_subport *subport, uint32_t pos)
2846 struct rte_sched_grinder *grinder = subport->grinder + pos;
2848 switch (grinder->state) {
2849 case e_GRINDER_PREFETCH_PIPE:
2851 if (grinder_next_pipe(port, subport, pos)) {
2852 grinder_prefetch_pipe(subport, pos);
2853 subport->busy_grinders++;
2855 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2862 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2864 struct rte_sched_pipe *pipe = grinder->pipe;
2866 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2867 grinder->subport_params = port->subport_profiles +
2870 grinder_prefetch_tc_queue_arrays(subport, pos);
2871 grinder_credits_update(port, subport, pos);
2873 grinder->state = e_GRINDER_PREFETCH_MBUF;
2877 case e_GRINDER_PREFETCH_MBUF:
2879 grinder_prefetch_mbuf(subport, pos);
2881 grinder->state = e_GRINDER_READ_MBUF;
2885 case e_GRINDER_READ_MBUF:
2887 uint32_t wrr_active, result = 0;
2889 result = grinder_schedule(port, subport, pos);
2891 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2893 /* Look for next packet within the same TC */
2894 if (result && grinder->qmask) {
2896 grinder_wrr(subport, pos);
2898 grinder_prefetch_mbuf(subport, pos);
2904 grinder_wrr_store(subport, pos);
2906 /* Look for another active TC within same pipe */
2907 if (grinder_next_tc(port, subport, pos)) {
2908 grinder_prefetch_tc_queue_arrays(subport, pos);
2910 grinder->state = e_GRINDER_PREFETCH_MBUF;
2914 if (grinder->productive == 0 &&
2915 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2916 subport->pipe_loop = grinder->pindex;
2918 grinder_evict(subport, pos);
2920 /* Look for another active pipe */
2921 if (grinder_next_pipe(port, subport, pos)) {
2922 grinder_prefetch_pipe(subport, pos);
2924 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2928 /* No active pipe found */
2929 subport->busy_grinders--;
2931 grinder->state = e_GRINDER_PREFETCH_PIPE;
2936 rte_panic("Algorithmic error (invalid state)\n");
2942 rte_sched_port_time_resync(struct rte_sched_port *port)
2944 uint64_t cycles = rte_get_tsc_cycles();
2945 uint64_t cycles_diff;
2946 uint64_t bytes_diff;
2949 if (cycles < port->time_cpu_cycles)
2950 port->time_cpu_cycles = 0;
2952 cycles_diff = cycles - port->time_cpu_cycles;
2953 /* Compute elapsed time in bytes */
2954 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2955 port->inv_cycles_per_byte);
2957 /* Advance port time */
2958 port->time_cpu_cycles +=
2959 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2960 port->time_cpu_bytes += bytes_diff;
2961 if (port->time < port->time_cpu_bytes)
2962 port->time = port->time_cpu_bytes;
2964 /* Reset pipe loop detection */
2965 for (i = 0; i < port->n_subports_per_port; i++)
2966 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2970 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2974 /* Check if any exception flag is set */
2975 exceptions = (second_pass && subport->busy_grinders == 0) ||
2976 (subport->pipe_exhaustion == 1);
2978 /* Clear exception flags */
2979 subport->pipe_exhaustion = 0;
2985 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2987 struct rte_sched_subport *subport;
2988 uint32_t subport_id = port->subport_id;
2989 uint32_t i, n_subports = 0, count;
2991 port->pkts_out = pkts;
2992 port->n_pkts_out = 0;
2994 rte_sched_port_time_resync(port);
2996 /* Take each queue in the grinder one step further */
2997 for (i = 0, count = 0; ; i++) {
2998 subport = port->subports[subport_id];
3000 count += grinder_handle(port, subport,
3001 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
3003 if (count == n_pkts) {
3006 if (subport_id == port->n_subports_per_port)
3009 port->subport_id = subport_id;
3013 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
3019 if (subport_id == port->n_subports_per_port)
3022 if (n_subports == port->n_subports_per_port) {
3023 port->subport_id = subport_id;