1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_malloc.h>
11 #include <rte_cycles.h>
12 #include <rte_prefetch.h>
13 #include <rte_branch_prediction.h>
15 #include <rte_bitmap.h>
16 #include <rte_reciprocal.h>
18 #include "rte_sched.h"
19 #include "rte_sched_common.h"
20 #include "rte_approx.h"
22 #ifdef __INTEL_COMPILER
23 #pragma warning(disable:2259) /* conversion may lose significant bits */
26 #ifndef RTE_SCHED_PORT_N_GRINDERS
27 #define RTE_SCHED_PORT_N_GRINDERS 8
30 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
31 #define RTE_SCHED_WRR_SHIFT 3
32 #define RTE_SCHED_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
33 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
34 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
35 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
37 /* Scaling for cycles_per_byte calculation
38 * Chosen so that minimum rate is 480 bit/sec
40 #define RTE_SCHED_TIME_SHIFT 8
42 struct rte_sched_pipe_profile {
43 /* Token bucket (TB) */
45 uint64_t tb_credits_per_period;
48 /* Pipe traffic classes */
50 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
53 /* Pipe best-effort traffic class queues */
54 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
57 struct rte_sched_pipe {
58 /* Token bucket (TB) */
59 uint64_t tb_time; /* time of last update */
62 /* Pipe profile and flags */
65 /* Traffic classes (TCs) */
66 uint64_t tc_time; /* time of next update */
67 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
69 /* Weighted Round Robin (WRR) */
70 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
72 /* TC oversubscription */
73 uint64_t tc_ov_credits;
74 uint8_t tc_ov_period_id;
75 } __rte_cache_aligned;
77 struct rte_sched_queue {
82 struct rte_sched_queue_extra {
83 struct rte_sched_queue_stats stats;
94 e_GRINDER_PREFETCH_PIPE = 0,
95 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
96 e_GRINDER_PREFETCH_MBUF,
100 struct rte_sched_subport_profile {
101 /* Token bucket (TB) */
103 uint64_t tb_credits_per_period;
106 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
110 struct rte_sched_grinder {
112 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
113 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
118 enum grinder_state state;
121 struct rte_sched_subport *subport;
122 struct rte_sched_subport_profile *subport_params;
123 struct rte_sched_pipe *pipe;
124 struct rte_sched_pipe_profile *pipe_params;
127 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
128 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
134 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
135 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
136 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
140 struct rte_mbuf *pkt;
143 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
144 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
145 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
148 struct rte_sched_subport {
149 /* Token bucket (TB) */
150 uint64_t tb_time; /* time of last update */
153 /* Traffic classes (TCs) */
154 uint64_t tc_time; /* time of next update */
155 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
157 /* TC oversubscription */
159 uint64_t tc_ov_wm_min;
160 uint64_t tc_ov_wm_max;
161 uint8_t tc_ov_period_id;
167 struct rte_sched_subport_stats stats __rte_cache_aligned;
169 /* subport profile */
172 uint32_t n_pipes_per_subport_enabled;
173 uint32_t n_pipe_profiles;
174 uint32_t n_max_pipe_profiles;
176 /* Pipe best-effort TC rate */
177 uint64_t pipe_tc_be_rate_max;
179 /* Pipe queues size */
180 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
182 #ifdef RTE_SCHED_CMAN
184 enum rte_sched_cman_mode cman;
188 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
189 struct rte_pie_config pie_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
193 /* Scheduling loop detection */
195 uint32_t pipe_exhaustion;
198 struct rte_bitmap *bmp;
199 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
202 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
203 uint32_t busy_grinders;
205 /* Queue base calculation */
206 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
209 struct rte_sched_pipe *pipe;
210 struct rte_sched_queue *queue;
211 struct rte_sched_queue_extra *queue_extra;
212 struct rte_sched_pipe_profile *pipe_profiles;
214 struct rte_mbuf **queue_array;
215 uint8_t memory[0] __rte_cache_aligned;
217 /* TC oversubscription activation */
219 } __rte_cache_aligned;
221 struct rte_sched_port {
222 /* User parameters */
223 uint32_t n_subports_per_port;
224 uint32_t n_pipes_per_subport;
225 uint32_t n_pipes_per_subport_log2;
226 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
227 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
228 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
229 uint32_t n_subport_profiles;
230 uint32_t n_max_subport_profiles;
233 uint32_t frame_overhead;
237 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cycles */
238 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
239 uint64_t time; /* Current NIC TX time measured in bytes */
240 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
241 uint64_t cycles_per_byte;
244 struct rte_mbuf **pkts_out;
248 /* Large data structures */
249 struct rte_sched_subport_profile *subport_profiles;
250 struct rte_sched_subport *subports[0] __rte_cache_aligned;
251 } __rte_cache_aligned;
253 enum rte_sched_subport_array {
254 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
255 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
256 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
257 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
258 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
259 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
260 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
263 static inline uint32_t
264 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
266 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
269 static inline struct rte_mbuf **
270 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
272 uint32_t pindex = qindex >> 4;
273 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
275 return (subport->queue_array + pindex *
276 subport->qsize_sum + subport->qsize_add[qpos]);
279 static inline uint16_t
280 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
281 struct rte_sched_subport *subport, uint32_t qindex)
283 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
285 return subport->qsize[tc];
288 static inline uint32_t
289 rte_sched_port_queues_per_port(struct rte_sched_port *port)
291 uint32_t n_queues = 0, i;
293 for (i = 0; i < port->n_subports_per_port; i++)
294 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
299 static inline uint16_t
300 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
302 uint16_t pipe_queue = port->pipe_queue[traffic_class];
307 static inline uint8_t
308 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
310 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
315 static inline uint8_t
316 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
318 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
324 pipe_profile_check(struct rte_sched_pipe_params *params,
325 uint64_t rate, uint16_t *qsize)
329 /* Pipe parameters */
330 if (params == NULL) {
332 "%s: Incorrect value for parameter params\n", __func__);
336 /* TB rate: non-zero, not greater than port rate */
337 if (params->tb_rate == 0 ||
338 params->tb_rate > rate) {
340 "%s: Incorrect value for tb rate\n", __func__);
344 /* TB size: non-zero */
345 if (params->tb_size == 0) {
347 "%s: Incorrect value for tb size\n", __func__);
351 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
352 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
353 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
354 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
355 params->tc_rate[i] > params->tb_rate))) {
357 "%s: Incorrect value for qsize or tc_rate\n", __func__);
362 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
363 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
365 "%s: Incorrect value for be traffic class rate\n", __func__);
369 /* TC period: non-zero */
370 if (params->tc_period == 0) {
372 "%s: Incorrect value for tc period\n", __func__);
376 /* Best effort tc oversubscription weight: non-zero */
377 if (params->tc_ov_weight == 0) {
379 "%s: Incorrect value for tc ov weight\n", __func__);
383 /* Queue WRR weights: non-zero */
384 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
385 if (params->wrr_weights[i] == 0) {
387 "%s: Incorrect value for wrr weight\n", __func__);
396 subport_profile_check(struct rte_sched_subport_profile_params *params,
401 /* Check user parameters */
402 if (params == NULL) {
403 RTE_LOG(ERR, SCHED, "%s: "
404 "Incorrect value for parameter params\n", __func__);
408 if (params->tb_rate == 0 || params->tb_rate > rate) {
409 RTE_LOG(ERR, SCHED, "%s: "
410 "Incorrect value for tb rate\n", __func__);
414 if (params->tb_size == 0) {
415 RTE_LOG(ERR, SCHED, "%s: "
416 "Incorrect value for tb size\n", __func__);
420 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
421 uint64_t tc_rate = params->tc_rate[i];
423 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
424 RTE_LOG(ERR, SCHED, "%s: "
425 "Incorrect value for tc rate\n", __func__);
430 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
431 RTE_LOG(ERR, SCHED, "%s: "
432 "Incorrect tc rate(best effort)\n", __func__);
436 if (params->tc_period == 0) {
437 RTE_LOG(ERR, SCHED, "%s: "
438 "Incorrect value for tc period\n", __func__);
446 rte_sched_port_check_params(struct rte_sched_port_params *params)
450 if (params == NULL) {
452 "%s: Incorrect value for parameter params\n", __func__);
457 if (params->socket < 0) {
459 "%s: Incorrect value for socket id\n", __func__);
464 if (params->rate == 0) {
466 "%s: Incorrect value for rate\n", __func__);
471 if (params->mtu == 0) {
473 "%s: Incorrect value for mtu\n", __func__);
477 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
478 if (params->n_subports_per_port == 0 ||
479 params->n_subports_per_port > 1u << 16 ||
480 !rte_is_power_of_2(params->n_subports_per_port)) {
482 "%s: Incorrect value for number of subports\n", __func__);
486 if (params->subport_profiles == NULL ||
487 params->n_subport_profiles == 0 ||
488 params->n_max_subport_profiles == 0 ||
489 params->n_subport_profiles > params->n_max_subport_profiles) {
491 "%s: Incorrect value for subport profiles\n", __func__);
495 for (i = 0; i < params->n_subport_profiles; i++) {
496 struct rte_sched_subport_profile_params *p =
497 params->subport_profiles + i;
500 status = subport_profile_check(p, params->rate);
503 "%s: subport profile check failed(%d)\n",
509 /* n_pipes_per_subport: non-zero, power of 2 */
510 if (params->n_pipes_per_subport == 0 ||
511 !rte_is_power_of_2(params->n_pipes_per_subport)) {
513 "%s: Incorrect value for maximum pipes number\n", __func__);
521 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
522 enum rte_sched_subport_array array)
524 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
525 uint32_t n_subport_pipe_queues =
526 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
528 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
529 uint32_t size_queue =
530 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
531 uint32_t size_queue_extra
532 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
533 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
534 sizeof(struct rte_sched_pipe_profile);
535 uint32_t size_bmp_array =
536 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
537 uint32_t size_per_pipe_queue_array, size_queue_array;
541 size_per_pipe_queue_array = 0;
542 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
543 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
544 size_per_pipe_queue_array +=
545 params->qsize[i] * sizeof(struct rte_mbuf *);
547 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
548 params->qsize[i] * sizeof(struct rte_mbuf *);
550 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
554 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
556 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
558 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
560 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
562 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
564 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
566 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
568 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
570 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
572 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
574 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
576 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
582 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
586 subport->qsize_add[0] = 0;
588 /* Strict priority traffic class */
589 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
590 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
592 /* Best-effort traffic class */
593 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
594 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
595 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
596 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
597 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
598 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
599 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
600 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
601 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
603 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
604 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
608 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
610 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
612 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
613 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
614 " Traffic classes: period = %"PRIu64",\n"
615 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
616 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
617 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
618 " Best-effort traffic class oversubscription: weight = %hhu\n"
619 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
624 p->tb_credits_per_period,
627 /* Traffic classes */
629 p->tc_credits_per_period[0],
630 p->tc_credits_per_period[1],
631 p->tc_credits_per_period[2],
632 p->tc_credits_per_period[3],
633 p->tc_credits_per_period[4],
634 p->tc_credits_per_period[5],
635 p->tc_credits_per_period[6],
636 p->tc_credits_per_period[7],
637 p->tc_credits_per_period[8],
638 p->tc_credits_per_period[9],
639 p->tc_credits_per_period[10],
640 p->tc_credits_per_period[11],
641 p->tc_credits_per_period[12],
643 /* Best-effort traffic class oversubscription */
647 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
651 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
653 struct rte_sched_subport_profile *p = port->subport_profiles + i;
655 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
656 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
658 "Traffic classes: period = %"PRIu64",\n"
659 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
660 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
661 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
666 p->tb_credits_per_period,
669 /* Traffic classes */
671 p->tc_credits_per_period[0],
672 p->tc_credits_per_period[1],
673 p->tc_credits_per_period[2],
674 p->tc_credits_per_period[3],
675 p->tc_credits_per_period[4],
676 p->tc_credits_per_period[5],
677 p->tc_credits_per_period[6],
678 p->tc_credits_per_period[7],
679 p->tc_credits_per_period[8],
680 p->tc_credits_per_period[9],
681 p->tc_credits_per_period[10],
682 p->tc_credits_per_period[11],
683 p->tc_credits_per_period[12]);
686 static inline uint64_t
687 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
689 uint64_t time = time_ms;
691 time = (time * rate) / 1000;
697 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
698 struct rte_sched_pipe_params *src,
699 struct rte_sched_pipe_profile *dst,
702 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
703 uint32_t lcd1, lcd2, lcd;
707 if (src->tb_rate == rate) {
708 dst->tb_credits_per_period = 1;
711 double tb_rate = (double) src->tb_rate
713 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
715 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
719 dst->tb_size = src->tb_size;
721 /* Traffic Classes */
722 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
725 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
726 if (subport->qsize[i])
727 dst->tc_credits_per_period[i]
728 = rte_sched_time_ms_to_bytes(src->tc_period,
731 dst->tc_ov_weight = src->tc_ov_weight;
734 wrr_cost[0] = src->wrr_weights[0];
735 wrr_cost[1] = src->wrr_weights[1];
736 wrr_cost[2] = src->wrr_weights[2];
737 wrr_cost[3] = src->wrr_weights[3];
739 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
740 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
741 lcd = rte_get_lcd(lcd1, lcd2);
743 wrr_cost[0] = lcd / wrr_cost[0];
744 wrr_cost[1] = lcd / wrr_cost[1];
745 wrr_cost[2] = lcd / wrr_cost[2];
746 wrr_cost[3] = lcd / wrr_cost[3];
748 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
749 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
750 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
751 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
755 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
756 struct rte_sched_subport_profile *dst,
762 if (src->tb_rate == rate) {
763 dst->tb_credits_per_period = 1;
766 double tb_rate = (double) src->tb_rate
768 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
770 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
774 dst->tb_size = src->tb_size;
776 /* Traffic Classes */
777 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
779 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
780 dst->tc_credits_per_period[i]
781 = rte_sched_time_ms_to_bytes(src->tc_period,
786 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
787 struct rte_sched_subport_params *params, uint64_t rate)
791 for (i = 0; i < subport->n_pipe_profiles; i++) {
792 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
793 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
795 rte_sched_pipe_profile_convert(subport, src, dst, rate);
796 rte_sched_port_log_pipe_profile(subport, i);
799 subport->pipe_tc_be_rate_max = 0;
800 for (i = 0; i < subport->n_pipe_profiles; i++) {
801 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
802 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
804 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
805 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
810 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
811 struct rte_sched_port_params *params,
816 for (i = 0; i < port->n_subport_profiles; i++) {
817 struct rte_sched_subport_profile_params *src
818 = params->subport_profiles + i;
819 struct rte_sched_subport_profile *dst
820 = port->subport_profiles + i;
822 rte_sched_subport_profile_convert(src, dst, rate);
823 rte_sched_port_log_subport_profile(port, i);
828 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
829 uint32_t n_max_pipes_per_subport,
834 /* Check user parameters */
835 if (params == NULL) {
837 "%s: Incorrect value for parameter params\n", __func__);
841 /* qsize: if non-zero, power of 2,
842 * no bigger than 32K (due to 16-bit read/write pointers)
844 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
845 uint16_t qsize = params->qsize[i];
847 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
849 "%s: Incorrect value for qsize\n", __func__);
854 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
855 RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
859 /* n_pipes_per_subport: non-zero, power of 2 */
860 if (params->n_pipes_per_subport_enabled == 0 ||
861 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
862 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
864 "%s: Incorrect value for pipes number\n", __func__);
868 /* pipe_profiles and n_pipe_profiles */
869 if (params->pipe_profiles == NULL ||
870 params->n_pipe_profiles == 0 ||
871 params->n_max_pipe_profiles == 0 ||
872 params->n_pipe_profiles > params->n_max_pipe_profiles) {
874 "%s: Incorrect value for pipe profiles\n", __func__);
878 for (i = 0; i < params->n_pipe_profiles; i++) {
879 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
882 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
885 "%s: Pipe profile check failed(%d)\n", __func__, status);
894 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
895 struct rte_sched_subport_params **subport_params)
897 uint32_t size0 = 0, size1 = 0, i;
900 status = rte_sched_port_check_params(port_params);
903 "%s: Port scheduler port params check failed (%d)\n",
909 for (i = 0; i < port_params->n_subports_per_port; i++) {
910 struct rte_sched_subport_params *sp = subport_params[i];
912 status = rte_sched_subport_check_params(sp,
913 port_params->n_pipes_per_subport,
917 "%s: Port scheduler subport params check failed (%d)\n",
924 size0 = sizeof(struct rte_sched_port);
926 for (i = 0; i < port_params->n_subports_per_port; i++) {
927 struct rte_sched_subport_params *sp = subport_params[i];
929 size1 += rte_sched_subport_get_array_base(sp,
930 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
933 return size0 + size1;
936 struct rte_sched_port *
937 rte_sched_port_config(struct rte_sched_port_params *params)
939 struct rte_sched_port *port = NULL;
940 uint32_t size0, size1, size2;
941 uint32_t cycles_per_byte;
945 status = rte_sched_port_check_params(params);
948 "%s: Port scheduler params check failed (%d)\n",
953 size0 = sizeof(struct rte_sched_port);
954 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
955 size2 = params->n_max_subport_profiles *
956 sizeof(struct rte_sched_subport_profile);
958 /* Allocate memory to store the data structures */
959 port = rte_zmalloc_socket("qos_params", size0 + size1,
960 RTE_CACHE_LINE_SIZE, params->socket);
962 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
967 /* Allocate memory to store the subport profile */
968 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
969 RTE_CACHE_LINE_SIZE, params->socket);
970 if (port->subport_profiles == NULL) {
971 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
976 /* User parameters */
977 port->n_subports_per_port = params->n_subports_per_port;
978 port->n_subport_profiles = params->n_subport_profiles;
979 port->n_max_subport_profiles = params->n_max_subport_profiles;
980 port->n_pipes_per_subport = params->n_pipes_per_subport;
981 port->n_pipes_per_subport_log2 =
982 __builtin_ctz(params->n_pipes_per_subport);
983 port->socket = params->socket;
985 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
986 port->pipe_queue[i] = i;
988 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
989 port->pipe_tc[i] = j;
991 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
995 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
996 port->tc_queue[i] = j;
998 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
1001 port->rate = params->rate;
1002 port->mtu = params->mtu + params->frame_overhead;
1003 port->frame_overhead = params->frame_overhead;
1006 port->time_cpu_cycles = rte_get_tsc_cycles();
1007 port->time_cpu_bytes = 0;
1010 /* Subport profile table */
1011 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1013 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1015 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1016 port->cycles_per_byte = cycles_per_byte;
1019 port->pkts_out = NULL;
1020 port->n_pkts_out = 0;
1021 port->subport_id = 0;
1027 rte_sched_subport_free(struct rte_sched_port *port,
1028 struct rte_sched_subport *subport)
1030 uint32_t n_subport_pipe_queues;
1033 if (subport == NULL)
1036 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1038 /* Free enqueued mbufs */
1039 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1040 struct rte_mbuf **mbufs =
1041 rte_sched_subport_pipe_qbase(subport, qindex);
1042 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1044 struct rte_sched_queue *queue = subport->queue + qindex;
1045 uint16_t qr = queue->qr & (qsize - 1);
1046 uint16_t qw = queue->qw & (qsize - 1);
1048 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1049 rte_pktmbuf_free(mbufs[qr]);
1057 rte_sched_port_free(struct rte_sched_port *port)
1061 /* Check user parameters */
1065 for (i = 0; i < port->n_subports_per_port; i++)
1066 rte_sched_subport_free(port, port->subports[i]);
1068 rte_free(port->subport_profiles);
1073 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1077 for (i = 0; i < n_subports; i++) {
1078 struct rte_sched_subport *subport = port->subports[i];
1080 rte_sched_subport_free(port, subport);
1083 rte_free(port->subport_profiles);
1087 #ifdef RTE_SCHED_CMAN
1089 rte_sched_red_config(struct rte_sched_port *port,
1090 struct rte_sched_subport *s,
1091 struct rte_sched_subport_params *params,
1092 uint32_t n_subports)
1096 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1100 for (j = 0; j < RTE_COLORS; j++) {
1101 /* if min/max are both zero, then RED is disabled */
1102 if ((params->cman_params->red_params[i][j].min_th |
1103 params->cman_params->red_params[i][j].max_th) == 0) {
1107 if (rte_red_config_init(&s->red_config[i][j],
1108 params->cman_params->red_params[i][j].wq_log2,
1109 params->cman_params->red_params[i][j].min_th,
1110 params->cman_params->red_params[i][j].max_th,
1111 params->cman_params->red_params[i][j].maxp_inv) != 0) {
1112 rte_sched_free_memory(port, n_subports);
1114 RTE_LOG(NOTICE, SCHED,
1115 "%s: RED configuration init fails\n", __func__);
1120 s->cman = RTE_SCHED_CMAN_RED;
1125 rte_sched_pie_config(struct rte_sched_port *port,
1126 struct rte_sched_subport *s,
1127 struct rte_sched_subport_params *params,
1128 uint32_t n_subports)
1132 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1133 if (params->cman_params->pie_params[i].tailq_th > params->qsize[i]) {
1134 RTE_LOG(NOTICE, SCHED,
1135 "%s: PIE tailq threshold incorrect\n", __func__);
1139 if (rte_pie_config_init(&s->pie_config[i],
1140 params->cman_params->pie_params[i].qdelay_ref,
1141 params->cman_params->pie_params[i].dp_update_interval,
1142 params->cman_params->pie_params[i].max_burst,
1143 params->cman_params->pie_params[i].tailq_th) != 0) {
1144 rte_sched_free_memory(port, n_subports);
1146 RTE_LOG(NOTICE, SCHED,
1147 "%s: PIE configuration init fails\n", __func__);
1151 s->cman = RTE_SCHED_CMAN_PIE;
1156 rte_sched_cman_config(struct rte_sched_port *port,
1157 struct rte_sched_subport *s,
1158 struct rte_sched_subport_params *params,
1159 uint32_t n_subports)
1161 if (params->cman_params->cman_mode == RTE_SCHED_CMAN_RED)
1162 return rte_sched_red_config(port, s, params, n_subports);
1164 else if (params->cman_params->cman_mode == RTE_SCHED_CMAN_PIE)
1165 return rte_sched_pie_config(port, s, params, n_subports);
1172 rte_sched_subport_tc_ov_config(struct rte_sched_port *port,
1173 uint32_t subport_id,
1176 struct rte_sched_subport *s;
1180 "%s: Incorrect value for parameter port\n", __func__);
1184 if (subport_id >= port->n_subports_per_port) {
1186 "%s: Incorrect value for parameter subport id\n", __func__);
1190 s = port->subports[subport_id];
1191 s->tc_ov_enabled = tc_ov_enable ? 1 : 0;
1197 rte_sched_subport_config(struct rte_sched_port *port,
1198 uint32_t subport_id,
1199 struct rte_sched_subport_params *params,
1200 uint32_t subport_profile_id)
1202 struct rte_sched_subport *s = NULL;
1203 uint32_t n_subports = subport_id;
1204 struct rte_sched_subport_profile *profile;
1205 uint32_t n_subport_pipe_queues, i;
1206 uint32_t size0, size1, bmp_mem_size;
1210 /* Check user parameters */
1213 "%s: Incorrect value for parameter port\n", __func__);
1217 if (subport_id >= port->n_subports_per_port) {
1219 "%s: Incorrect value for subport id\n", __func__);
1224 if (subport_profile_id >= port->n_max_subport_profiles) {
1225 RTE_LOG(ERR, SCHED, "%s: "
1226 "Number of subport profile exceeds the max limit\n",
1232 /** Memory is allocated only on first invocation of the api for a
1233 * given subport. Subsequent invocation on same subport will just
1234 * update subport bandwidth parameter.
1236 if (port->subports[subport_id] == NULL) {
1238 status = rte_sched_subport_check_params(params,
1239 port->n_pipes_per_subport,
1242 RTE_LOG(NOTICE, SCHED,
1243 "%s: Port scheduler params check failed (%d)\n",
1249 /* Determine the amount of memory to allocate */
1250 size0 = sizeof(struct rte_sched_subport);
1251 size1 = rte_sched_subport_get_array_base(params,
1252 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1254 /* Allocate memory to store the data structures */
1255 s = rte_zmalloc_socket("subport_params", size0 + size1,
1256 RTE_CACHE_LINE_SIZE, port->socket);
1259 "%s: Memory allocation fails\n", __func__);
1266 subport_profile_id = 0;
1269 port->subports[subport_id] = s;
1271 s->tb_time = port->time;
1273 /* compile time checks */
1274 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1275 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1276 (RTE_SCHED_PORT_N_GRINDERS - 1));
1278 /* User parameters */
1279 s->n_pipes_per_subport_enabled =
1280 params->n_pipes_per_subport_enabled;
1281 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1282 s->n_pipe_profiles = params->n_pipe_profiles;
1283 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1285 /* TC oversubscription is enabled by default */
1286 s->tc_ov_enabled = 1;
1288 #ifdef RTE_SCHED_CMAN
1289 if (params->cman_params != NULL) {
1290 s->cman_enabled = true;
1291 status = rte_sched_cman_config(port, s, params, n_subports);
1293 RTE_LOG(NOTICE, SCHED,
1294 "%s: CMAN configuration fails\n", __func__);
1298 s->cman_enabled = false;
1302 /* Scheduling loop detection */
1303 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1304 s->pipe_exhaustion = 0;
1307 s->busy_grinders = 0;
1309 /* Queue base calculation */
1310 rte_sched_subport_config_qsize(s);
1312 /* Large data structures */
1313 s->pipe = (struct rte_sched_pipe *)
1314 (s->memory + rte_sched_subport_get_array_base(params,
1315 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1316 s->queue = (struct rte_sched_queue *)
1317 (s->memory + rte_sched_subport_get_array_base(params,
1318 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1319 s->queue_extra = (struct rte_sched_queue_extra *)
1320 (s->memory + rte_sched_subport_get_array_base(params,
1321 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1322 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1323 (s->memory + rte_sched_subport_get_array_base(params,
1324 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1325 s->bmp_array = s->memory + rte_sched_subport_get_array_base(
1326 params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1327 s->queue_array = (struct rte_mbuf **)
1328 (s->memory + rte_sched_subport_get_array_base(params,
1329 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1331 /* Pipe profile table */
1332 rte_sched_subport_config_pipe_profile_table(s, params,
1336 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1337 bmp_mem_size = rte_bitmap_get_memory_footprint(
1338 n_subport_pipe_queues);
1339 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1341 if (s->bmp == NULL) {
1343 "%s: Subport bitmap init error\n", __func__);
1348 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1349 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1351 /* TC oversubscription */
1352 s->tc_ov_wm_min = port->mtu;
1353 s->tc_ov_period_id = 0;
1360 /* update subport parameters from subport profile table*/
1361 profile = port->subport_profiles + subport_profile_id;
1363 s = port->subports[subport_id];
1365 s->tb_credits = profile->tb_size / 2;
1367 s->tc_time = port->time + profile->tc_period;
1369 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1372 profile->tc_credits_per_period[i];
1374 profile->tc_credits_per_period[i] = 0;
1376 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1377 s->pipe_tc_be_rate_max);
1378 s->tc_ov_wm = s->tc_ov_wm_max;
1379 s->profile = subport_profile_id;
1383 rte_sched_port_log_subport_profile(port, subport_profile_id);
1388 rte_sched_free_memory(port, n_subports);
1394 rte_sched_pipe_config(struct rte_sched_port *port,
1395 uint32_t subport_id,
1397 int32_t pipe_profile)
1399 struct rte_sched_subport *s;
1400 struct rte_sched_subport_profile *sp;
1401 struct rte_sched_pipe *p;
1402 struct rte_sched_pipe_profile *params;
1403 uint32_t n_subports = subport_id + 1;
1404 uint32_t deactivate, profile, i;
1407 /* Check user parameters */
1408 profile = (uint32_t) pipe_profile;
1409 deactivate = (pipe_profile < 0);
1413 "%s: Incorrect value for parameter port\n", __func__);
1417 if (subport_id >= port->n_subports_per_port) {
1419 "%s: Incorrect value for parameter subport id\n", __func__);
1424 s = port->subports[subport_id];
1425 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1427 "%s: Incorrect value for parameter pipe id\n", __func__);
1432 if (!deactivate && profile >= s->n_pipe_profiles) {
1434 "%s: Incorrect value for parameter pipe profile\n", __func__);
1439 sp = port->subport_profiles + s->profile;
1440 /* Handle the case when pipe already has a valid configuration */
1441 p = s->pipe + pipe_id;
1443 params = s->pipe_profiles + p->profile;
1445 double subport_tc_be_rate =
1446 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1447 / (double) sp->tc_period;
1448 double pipe_tc_be_rate =
1449 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1450 / (double) params->tc_period;
1451 uint32_t tc_be_ov = s->tc_ov;
1453 /* Unplug pipe from its subport */
1454 s->tc_ov_n -= params->tc_ov_weight;
1455 s->tc_ov_rate -= pipe_tc_be_rate;
1456 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1458 if (s->tc_ov != tc_be_ov) {
1459 RTE_LOG(DEBUG, SCHED,
1460 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1461 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1464 /* Reset the pipe */
1465 memset(p, 0, sizeof(struct rte_sched_pipe));
1471 /* Apply the new pipe configuration */
1472 p->profile = profile;
1473 params = s->pipe_profiles + p->profile;
1475 /* Token Bucket (TB) */
1476 p->tb_time = port->time;
1477 p->tb_credits = params->tb_size / 2;
1479 /* Traffic Classes (TCs) */
1480 p->tc_time = port->time + params->tc_period;
1482 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1484 p->tc_credits[i] = params->tc_credits_per_period[i];
1487 /* Subport best effort tc oversubscription */
1488 double subport_tc_be_rate =
1489 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1490 / (double) sp->tc_period;
1491 double pipe_tc_be_rate =
1492 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1493 / (double) params->tc_period;
1494 uint32_t tc_be_ov = s->tc_ov;
1496 s->tc_ov_n += params->tc_ov_weight;
1497 s->tc_ov_rate += pipe_tc_be_rate;
1498 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1500 if (s->tc_ov != tc_be_ov) {
1501 RTE_LOG(DEBUG, SCHED,
1502 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1503 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1505 p->tc_ov_period_id = s->tc_ov_period_id;
1506 p->tc_ov_credits = s->tc_ov_wm;
1512 rte_sched_free_memory(port, n_subports);
1518 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1519 uint32_t subport_id,
1520 struct rte_sched_pipe_params *params,
1521 uint32_t *pipe_profile_id)
1523 struct rte_sched_subport *s;
1524 struct rte_sched_pipe_profile *pp;
1531 "%s: Incorrect value for parameter port\n", __func__);
1535 /* Subport id not exceeds the max limit */
1536 if (subport_id > port->n_subports_per_port) {
1538 "%s: Incorrect value for subport id\n", __func__);
1542 s = port->subports[subport_id];
1544 /* Pipe profiles exceeds the max limit */
1545 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1547 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1552 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1555 "%s: Pipe profile check failed(%d)\n", __func__, status);
1559 pp = &s->pipe_profiles[s->n_pipe_profiles];
1560 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1562 /* Pipe profile should not exists */
1563 for (i = 0; i < s->n_pipe_profiles; i++)
1564 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1566 "%s: Pipe profile exists\n", __func__);
1570 /* Pipe profile commit */
1571 *pipe_profile_id = s->n_pipe_profiles;
1572 s->n_pipe_profiles++;
1574 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1575 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1577 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1583 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1584 struct rte_sched_subport_profile_params *params,
1585 uint32_t *subport_profile_id)
1589 struct rte_sched_subport_profile *dst;
1593 RTE_LOG(ERR, SCHED, "%s: "
1594 "Incorrect value for parameter port\n", __func__);
1598 if (params == NULL) {
1599 RTE_LOG(ERR, SCHED, "%s: "
1600 "Incorrect value for parameter profile\n", __func__);
1604 if (subport_profile_id == NULL) {
1605 RTE_LOG(ERR, SCHED, "%s: "
1606 "Incorrect value for parameter subport_profile_id\n",
1611 dst = port->subport_profiles + port->n_subport_profiles;
1613 /* Subport profiles exceeds the max limit */
1614 if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1615 RTE_LOG(ERR, SCHED, "%s: "
1616 "Number of subport profiles exceeds the max limit\n",
1621 status = subport_profile_check(params, port->rate);
1624 "%s: subport profile check failed(%d)\n", __func__, status);
1628 rte_sched_subport_profile_convert(params, dst, port->rate);
1630 /* Subport profile should not exists */
1631 for (i = 0; i < port->n_subport_profiles; i++)
1632 if (memcmp(port->subport_profiles + i,
1633 dst, sizeof(*dst)) == 0) {
1635 "%s: subport profile exists\n", __func__);
1639 /* Subport profile commit */
1640 *subport_profile_id = port->n_subport_profiles;
1641 port->n_subport_profiles++;
1643 rte_sched_port_log_subport_profile(port, *subport_profile_id);
1648 static inline uint32_t
1649 rte_sched_port_qindex(struct rte_sched_port *port,
1652 uint32_t traffic_class,
1655 return ((subport & (port->n_subports_per_port - 1)) <<
1656 (port->n_pipes_per_subport_log2 + 4)) |
1658 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1659 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1660 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1664 rte_sched_port_pkt_write(struct rte_sched_port *port,
1665 struct rte_mbuf *pkt,
1666 uint32_t subport, uint32_t pipe,
1667 uint32_t traffic_class,
1668 uint32_t queue, enum rte_color color)
1671 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1673 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1677 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1678 const struct rte_mbuf *pkt,
1679 uint32_t *subport, uint32_t *pipe,
1680 uint32_t *traffic_class, uint32_t *queue)
1682 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1684 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1685 *pipe = (queue_id >> 4) &
1686 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1687 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1688 *queue = rte_sched_port_tc_queue(port, queue_id);
1692 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1694 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1698 rte_sched_subport_read_stats(struct rte_sched_port *port,
1699 uint32_t subport_id,
1700 struct rte_sched_subport_stats *stats,
1703 struct rte_sched_subport *s;
1705 /* Check user parameters */
1708 "%s: Incorrect value for parameter port\n", __func__);
1712 if (subport_id >= port->n_subports_per_port) {
1714 "%s: Incorrect value for subport id\n", __func__);
1718 if (stats == NULL) {
1720 "%s: Incorrect value for parameter stats\n", __func__);
1724 if (tc_ov == NULL) {
1726 "%s: Incorrect value for tc_ov\n", __func__);
1730 s = port->subports[subport_id];
1732 /* Copy subport stats and clear */
1733 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1734 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1736 /* Subport TC oversubscription status */
1743 rte_sched_queue_read_stats(struct rte_sched_port *port,
1745 struct rte_sched_queue_stats *stats,
1748 struct rte_sched_subport *s;
1749 struct rte_sched_queue *q;
1750 struct rte_sched_queue_extra *qe;
1751 uint32_t subport_id, subport_qmask, subport_qindex;
1753 /* Check user parameters */
1756 "%s: Incorrect value for parameter port\n", __func__);
1760 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1762 "%s: Incorrect value for queue id\n", __func__);
1766 if (stats == NULL) {
1768 "%s: Incorrect value for parameter stats\n", __func__);
1774 "%s: Incorrect value for parameter qlen\n", __func__);
1777 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1778 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1780 s = port->subports[subport_id];
1781 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1782 q = s->queue + subport_qindex;
1783 qe = s->queue_extra + subport_qindex;
1785 /* Copy queue stats and clear */
1786 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1787 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1790 *qlen = q->qw - q->qr;
1795 #ifdef RTE_SCHED_DEBUG
1798 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1801 struct rte_sched_queue *queue = subport->queue + qindex;
1803 return queue->qr == queue->qw;
1806 #endif /* RTE_SCHED_DEBUG */
1809 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1810 struct rte_sched_subport *subport,
1812 struct rte_mbuf *pkt)
1814 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1815 uint32_t pkt_len = pkt->pkt_len;
1817 subport->stats.n_pkts_tc[tc_index] += 1;
1818 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1822 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1823 struct rte_sched_subport *subport,
1825 struct rte_mbuf *pkt,
1826 __rte_unused uint32_t n_pkts_cman_dropped)
1828 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1829 uint32_t pkt_len = pkt->pkt_len;
1831 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1832 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1833 subport->stats.n_pkts_cman_dropped[tc_index] += n_pkts_cman_dropped;
1837 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1839 struct rte_mbuf *pkt)
1841 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1842 uint32_t pkt_len = pkt->pkt_len;
1844 qe->stats.n_pkts += 1;
1845 qe->stats.n_bytes += pkt_len;
1849 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1851 struct rte_mbuf *pkt,
1852 __rte_unused uint32_t n_pkts_cman_dropped)
1854 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1855 uint32_t pkt_len = pkt->pkt_len;
1857 qe->stats.n_pkts_dropped += 1;
1858 qe->stats.n_bytes_dropped += pkt_len;
1859 #ifdef RTE_SCHED_CMAN
1860 if (subport->cman_enabled)
1861 qe->stats.n_pkts_cman_dropped += n_pkts_cman_dropped;
1865 #ifdef RTE_SCHED_CMAN
1868 rte_sched_port_cman_drop(struct rte_sched_port *port,
1869 struct rte_sched_subport *subport,
1870 struct rte_mbuf *pkt,
1874 if (!subport->cman_enabled)
1877 struct rte_sched_queue_extra *qe;
1880 tc_index = rte_sched_port_pipe_tc(port, qindex);
1881 qe = subport->queue_extra + qindex;
1884 if (subport->cman == RTE_SCHED_CMAN_RED) {
1885 struct rte_red_config *red_cfg;
1886 struct rte_red *red;
1887 enum rte_color color;
1889 color = rte_sched_port_pkt_read_color(pkt);
1890 red_cfg = &subport->red_config[tc_index][color];
1892 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1897 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1901 struct rte_pie_config *pie_cfg = &subport->pie_config[tc_index];
1902 struct rte_pie *pie = &qe->pie;
1904 return rte_pie_enqueue(pie_cfg, pie, qlen, pkt->pkt_len, port->time_cpu_cycles);
1908 rte_sched_port_red_set_queue_empty_timestamp(struct rte_sched_port *port,
1909 struct rte_sched_subport *subport, uint32_t qindex)
1911 if (subport->cman_enabled) {
1912 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1913 if (subport->cman == RTE_SCHED_CMAN_RED) {
1914 struct rte_red *red = &qe->red;
1916 rte_red_mark_queue_empty(red, port->time);
1922 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport,
1923 uint32_t qindex, uint32_t pkt_len, uint64_t time) {
1924 if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_PIE) {
1925 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1926 struct rte_pie *pie = &qe->pie;
1928 /* Update queue length */
1930 pie->qlen_bytes -= pkt_len;
1932 rte_pie_dequeue(pie, pkt_len, time);
1938 static inline int rte_sched_port_cman_drop(struct rte_sched_port *port __rte_unused,
1939 struct rte_sched_subport *subport __rte_unused,
1940 struct rte_mbuf *pkt __rte_unused,
1941 uint32_t qindex __rte_unused,
1942 uint16_t qlen __rte_unused)
1947 #define rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex)
1950 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport __rte_unused,
1951 uint32_t qindex __rte_unused,
1952 uint32_t pkt_len __rte_unused,
1953 uint64_t time __rte_unused) {
1954 /* do-nothing when RTE_SCHED_CMAN not defined */
1957 #endif /* RTE_SCHED_CMAN */
1959 #ifdef RTE_SCHED_DEBUG
1962 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1969 rte_panic("Empty slab at position %u\n", bmp_pos);
1972 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1973 if (mask & bmp_slab) {
1974 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1975 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1982 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1986 #endif /* RTE_SCHED_DEBUG */
1988 static inline struct rte_sched_subport *
1989 rte_sched_port_subport(struct rte_sched_port *port,
1990 struct rte_mbuf *pkt)
1992 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1993 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1995 return port->subports[subport_id];
1998 static inline uint32_t
1999 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
2000 struct rte_mbuf *pkt, uint32_t subport_qmask)
2002 struct rte_sched_queue *q;
2003 struct rte_sched_queue_extra *qe;
2004 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
2005 uint32_t subport_queue_id = subport_qmask & qindex;
2007 q = subport->queue + subport_queue_id;
2009 qe = subport->queue_extra + subport_queue_id;
2012 return subport_queue_id;
2016 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
2017 struct rte_sched_subport *subport,
2019 struct rte_mbuf **qbase)
2021 struct rte_sched_queue *q;
2022 struct rte_mbuf **q_qw;
2025 q = subport->queue + qindex;
2026 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2027 q_qw = qbase + (q->qw & (qsize - 1));
2029 rte_prefetch0(q_qw);
2030 rte_bitmap_prefetch0(subport->bmp, qindex);
2034 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
2035 struct rte_sched_subport *subport,
2037 struct rte_mbuf **qbase,
2038 struct rte_mbuf *pkt)
2040 struct rte_sched_queue *q;
2044 q = subport->queue + qindex;
2045 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2046 qlen = q->qw - q->qr;
2048 /* Drop the packet (and update drop stats) when queue is full */
2049 if (unlikely(rte_sched_port_cman_drop(port, subport, pkt, qindex, qlen) ||
2051 rte_pktmbuf_free(pkt);
2052 rte_sched_port_update_subport_stats_on_drop(port, subport,
2053 qindex, pkt, qlen < qsize);
2054 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
2059 /* Enqueue packet */
2060 qbase[q->qw & (qsize - 1)] = pkt;
2063 /* Activate queue in the subport bitmap */
2064 rte_bitmap_set(subport->bmp, qindex);
2067 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2068 rte_sched_port_update_queue_stats(subport, qindex, pkt);
2075 * The enqueue function implements a 4-level pipeline with each stage
2076 * processing two different packets. The purpose of using a pipeline
2077 * is to hide the latency of prefetching the data structures. The
2078 * naming convention is presented in the diagram below:
2080 * p00 _______ p10 _______ p20 _______ p30 _______
2081 * ----->| |----->| |----->| |----->| |----->
2082 * | 0 | | 1 | | 2 | | 3 |
2083 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2088 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2091 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2092 *pkt30, *pkt31, *pkt_last;
2093 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2094 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2095 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2096 *subport20, *subport21, *subport30, *subport31, *subport_last;
2097 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2098 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2099 uint32_t subport_qmask;
2103 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2106 * Less then 6 input packets available, which is not enough to
2109 if (unlikely(n_pkts < 6)) {
2110 struct rte_sched_subport *subports[5];
2111 struct rte_mbuf **q_base[5];
2114 /* Prefetch the mbuf structure of each packet */
2115 for (i = 0; i < n_pkts; i++)
2116 rte_prefetch0(pkts[i]);
2118 /* Prefetch the subport structure for each packet */
2119 for (i = 0; i < n_pkts; i++)
2120 subports[i] = rte_sched_port_subport(port, pkts[i]);
2122 /* Prefetch the queue structure for each queue */
2123 for (i = 0; i < n_pkts; i++)
2124 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2125 pkts[i], subport_qmask);
2127 /* Prefetch the write pointer location of each queue */
2128 for (i = 0; i < n_pkts; i++) {
2129 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2130 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2134 /* Write each packet to its queue */
2135 for (i = 0; i < n_pkts; i++)
2136 result += rte_sched_port_enqueue_qwa(port, subports[i],
2137 q[i], q_base[i], pkts[i]);
2142 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2145 rte_prefetch0(pkt20);
2146 rte_prefetch0(pkt21);
2150 rte_prefetch0(pkt10);
2151 rte_prefetch0(pkt11);
2153 subport20 = rte_sched_port_subport(port, pkt20);
2154 subport21 = rte_sched_port_subport(port, pkt21);
2155 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2156 pkt20, subport_qmask);
2157 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2158 pkt21, subport_qmask);
2162 rte_prefetch0(pkt00);
2163 rte_prefetch0(pkt01);
2165 subport10 = rte_sched_port_subport(port, pkt10);
2166 subport11 = rte_sched_port_subport(port, pkt11);
2167 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2168 pkt10, subport_qmask);
2169 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2170 pkt11, subport_qmask);
2172 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2173 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2174 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2175 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2177 /* Run the pipeline */
2178 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2179 /* Propagate stage inputs */
2190 subport30 = subport20;
2191 subport31 = subport21;
2192 subport20 = subport10;
2193 subport21 = subport11;
2194 q30_base = q20_base;
2195 q31_base = q21_base;
2197 /* Stage 0: Get packets in */
2199 pkt01 = pkts[i + 1];
2200 rte_prefetch0(pkt00);
2201 rte_prefetch0(pkt01);
2203 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2204 subport10 = rte_sched_port_subport(port, pkt10);
2205 subport11 = rte_sched_port_subport(port, pkt11);
2206 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2207 pkt10, subport_qmask);
2208 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2209 pkt11, subport_qmask);
2211 /* Stage 2: Prefetch queue write location */
2212 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2213 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2214 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2215 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2217 /* Stage 3: Write packet to queue and activate queue */
2218 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2219 q30, q30_base, pkt30);
2220 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2221 q31, q31_base, pkt31);
2222 result += r30 + r31;
2226 * Drain the pipeline (exactly 6 packets).
2227 * Handle the last packet in the case
2228 * of an odd number of input packets.
2230 pkt_last = pkts[n_pkts - 1];
2231 rte_prefetch0(pkt_last);
2233 subport00 = rte_sched_port_subport(port, pkt00);
2234 subport01 = rte_sched_port_subport(port, pkt01);
2235 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2236 pkt00, subport_qmask);
2237 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2238 pkt01, subport_qmask);
2240 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2241 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2242 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2243 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2245 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2246 q20, q20_base, pkt20);
2247 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2248 q21, q21_base, pkt21);
2249 result += r20 + r21;
2251 subport_last = rte_sched_port_subport(port, pkt_last);
2252 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2253 pkt_last, subport_qmask);
2255 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2256 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2257 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2258 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2260 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2262 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2264 result += r10 + r11;
2266 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2267 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2268 q_last, q_last_base);
2270 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2272 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2274 result += r00 + r01;
2277 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2278 q_last, q_last_base, pkt_last);
2285 static inline uint64_t
2286 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2287 struct rte_sched_subport *subport, uint32_t pos)
2289 struct rte_sched_grinder *grinder = subport->grinder + pos;
2290 struct rte_sched_subport_profile *sp = grinder->subport_params;
2291 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2292 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2293 uint64_t tc_ov_wm = subport->tc_ov_wm;
2296 if (subport->tc_ov == 0)
2297 return subport->tc_ov_wm_max;
2299 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2300 tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2301 - subport->tc_credits[i];
2302 tc_consumption += tc_ov_consumption[i];
2305 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2306 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2307 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2309 tc_ov_consumption_max =
2310 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2313 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2314 (tc_ov_consumption_max - port->mtu)) {
2315 tc_ov_wm -= tc_ov_wm >> 7;
2316 if (tc_ov_wm < subport->tc_ov_wm_min)
2317 tc_ov_wm = subport->tc_ov_wm_min;
2322 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2323 if (tc_ov_wm > subport->tc_ov_wm_max)
2324 tc_ov_wm = subport->tc_ov_wm_max;
2330 grinder_credits_update(struct rte_sched_port *port,
2331 struct rte_sched_subport *subport, uint32_t pos)
2333 struct rte_sched_grinder *grinder = subport->grinder + pos;
2334 struct rte_sched_pipe *pipe = grinder->pipe;
2335 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2336 struct rte_sched_subport_profile *sp = grinder->subport_params;
2341 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2342 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2343 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2344 subport->tb_time += n_periods * sp->tb_period;
2347 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2348 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2349 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2350 pipe->tb_time += n_periods * params->tb_period;
2353 if (unlikely(port->time >= subport->tc_time)) {
2354 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2355 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2357 subport->tc_time = port->time + sp->tc_period;
2361 if (unlikely(port->time >= pipe->tc_time)) {
2362 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2363 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2364 pipe->tc_time = port->time + params->tc_period;
2369 grinder_credits_update_with_tc_ov(struct rte_sched_port *port,
2370 struct rte_sched_subport *subport, uint32_t pos)
2372 struct rte_sched_grinder *grinder = subport->grinder + pos;
2373 struct rte_sched_pipe *pipe = grinder->pipe;
2374 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2375 struct rte_sched_subport_profile *sp = grinder->subport_params;
2380 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2381 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2382 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2383 subport->tb_time += n_periods * sp->tb_period;
2386 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2387 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2388 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2389 pipe->tb_time += n_periods * params->tb_period;
2392 if (unlikely(port->time >= subport->tc_time)) {
2394 grinder_tc_ov_credits_update(port, subport, pos);
2396 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2397 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2399 subport->tc_time = port->time + sp->tc_period;
2400 subport->tc_ov_period_id++;
2404 if (unlikely(port->time >= pipe->tc_time)) {
2405 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2406 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2407 pipe->tc_time = port->time + params->tc_period;
2410 /* Pipe TCs - Oversubscription */
2411 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2412 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2414 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2419 grinder_credits_check(struct rte_sched_port *port,
2420 struct rte_sched_subport *subport, uint32_t pos)
2422 struct rte_sched_grinder *grinder = subport->grinder + pos;
2423 struct rte_sched_pipe *pipe = grinder->pipe;
2424 struct rte_mbuf *pkt = grinder->pkt;
2425 uint32_t tc_index = grinder->tc_index;
2426 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2427 uint64_t subport_tb_credits = subport->tb_credits;
2428 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2429 uint64_t pipe_tb_credits = pipe->tb_credits;
2430 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2433 /* Check pipe and subport credits */
2434 enough_credits = (pkt_len <= subport_tb_credits) &&
2435 (pkt_len <= subport_tc_credits) &&
2436 (pkt_len <= pipe_tb_credits) &&
2437 (pkt_len <= pipe_tc_credits);
2439 if (!enough_credits)
2442 /* Update pipe and subport credits */
2443 subport->tb_credits -= pkt_len;
2444 subport->tc_credits[tc_index] -= pkt_len;
2445 pipe->tb_credits -= pkt_len;
2446 pipe->tc_credits[tc_index] -= pkt_len;
2452 grinder_credits_check_with_tc_ov(struct rte_sched_port *port,
2453 struct rte_sched_subport *subport, uint32_t pos)
2455 struct rte_sched_grinder *grinder = subport->grinder + pos;
2456 struct rte_sched_pipe *pipe = grinder->pipe;
2457 struct rte_mbuf *pkt = grinder->pkt;
2458 uint32_t tc_index = grinder->tc_index;
2459 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2460 uint64_t subport_tb_credits = subport->tb_credits;
2461 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2462 uint64_t pipe_tb_credits = pipe->tb_credits;
2463 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2464 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2465 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2466 uint64_t pipe_tc_ov_credits;
2470 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2471 pipe_tc_ov_mask1[i] = ~0LLU;
2473 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2474 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2475 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2477 /* Check pipe and subport credits */
2478 enough_credits = (pkt_len <= subport_tb_credits) &&
2479 (pkt_len <= subport_tc_credits) &&
2480 (pkt_len <= pipe_tb_credits) &&
2481 (pkt_len <= pipe_tc_credits) &&
2482 (pkt_len <= pipe_tc_ov_credits);
2484 if (!enough_credits)
2487 /* Update pipe and subport credits */
2488 subport->tb_credits -= pkt_len;
2489 subport->tc_credits[tc_index] -= pkt_len;
2490 pipe->tb_credits -= pkt_len;
2491 pipe->tc_credits[tc_index] -= pkt_len;
2492 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2499 grinder_schedule(struct rte_sched_port *port,
2500 struct rte_sched_subport *subport, uint32_t pos)
2502 struct rte_sched_grinder *grinder = subport->grinder + pos;
2503 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2504 uint32_t qindex = grinder->qindex[grinder->qpos];
2505 struct rte_mbuf *pkt = grinder->pkt;
2506 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2507 uint32_t be_tc_active;
2509 if (subport->tc_ov_enabled) {
2510 if (!grinder_credits_check_with_tc_ov(port, subport, pos))
2513 if (!grinder_credits_check(port, subport, pos))
2517 /* Advance port time */
2518 port->time += pkt_len;
2521 port->pkts_out[port->n_pkts_out++] = pkt;
2524 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2525 grinder->wrr_tokens[grinder->qpos] +=
2526 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2528 if (queue->qr == queue->qw) {
2529 rte_bitmap_clear(subport->bmp, qindex);
2530 grinder->qmask &= ~(1 << grinder->qpos);
2532 grinder->wrr_mask[grinder->qpos] = 0;
2534 rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex);
2537 rte_sched_port_pie_dequeue(subport, qindex, pkt_len, port->time_cpu_cycles);
2539 /* Reset pipe loop detection */
2540 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2541 grinder->productive = 1;
2547 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2551 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2552 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2560 grinder_pcache_populate(struct rte_sched_subport *subport,
2561 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2563 struct rte_sched_grinder *grinder = subport->grinder + pos;
2566 grinder->pcache_w = 0;
2567 grinder->pcache_r = 0;
2569 w[0] = (uint16_t) bmp_slab;
2570 w[1] = (uint16_t) (bmp_slab >> 16);
2571 w[2] = (uint16_t) (bmp_slab >> 32);
2572 w[3] = (uint16_t) (bmp_slab >> 48);
2574 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2575 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2576 grinder->pcache_w += (w[0] != 0);
2578 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2579 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2580 grinder->pcache_w += (w[1] != 0);
2582 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2583 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2584 grinder->pcache_w += (w[2] != 0);
2586 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2587 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2588 grinder->pcache_w += (w[3] != 0);
2592 grinder_tccache_populate(struct rte_sched_subport *subport,
2593 uint32_t pos, uint32_t qindex, uint16_t qmask)
2595 struct rte_sched_grinder *grinder = subport->grinder + pos;
2598 grinder->tccache_w = 0;
2599 grinder->tccache_r = 0;
2601 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2602 b = (uint8_t) ((qmask >> i) & 0x1);
2603 grinder->tccache_qmask[grinder->tccache_w] = b;
2604 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2605 grinder->tccache_w += (b != 0);
2608 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2609 grinder->tccache_qmask[grinder->tccache_w] = b;
2610 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2611 RTE_SCHED_TRAFFIC_CLASS_BE;
2612 grinder->tccache_w += (b != 0);
2616 grinder_next_tc(struct rte_sched_port *port,
2617 struct rte_sched_subport *subport, uint32_t pos)
2619 struct rte_sched_grinder *grinder = subport->grinder + pos;
2620 struct rte_mbuf **qbase;
2624 if (grinder->tccache_r == grinder->tccache_w)
2627 qindex = grinder->tccache_qindex[grinder->tccache_r];
2628 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2629 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2631 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2632 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2633 grinder->qsize = qsize;
2635 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2636 grinder->queue[0] = subport->queue + qindex;
2637 grinder->qbase[0] = qbase;
2638 grinder->qindex[0] = qindex;
2639 grinder->tccache_r++;
2644 grinder->queue[0] = subport->queue + qindex;
2645 grinder->queue[1] = subport->queue + qindex + 1;
2646 grinder->queue[2] = subport->queue + qindex + 2;
2647 grinder->queue[3] = subport->queue + qindex + 3;
2649 grinder->qbase[0] = qbase;
2650 grinder->qbase[1] = qbase + qsize;
2651 grinder->qbase[2] = qbase + 2 * qsize;
2652 grinder->qbase[3] = qbase + 3 * qsize;
2654 grinder->qindex[0] = qindex;
2655 grinder->qindex[1] = qindex + 1;
2656 grinder->qindex[2] = qindex + 2;
2657 grinder->qindex[3] = qindex + 3;
2659 grinder->tccache_r++;
2664 grinder_next_pipe(struct rte_sched_port *port,
2665 struct rte_sched_subport *subport, uint32_t pos)
2667 struct rte_sched_grinder *grinder = subport->grinder + pos;
2668 uint32_t pipe_qindex;
2669 uint16_t pipe_qmask;
2671 if (grinder->pcache_r < grinder->pcache_w) {
2672 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2673 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2674 grinder->pcache_r++;
2676 uint64_t bmp_slab = 0;
2677 uint32_t bmp_pos = 0;
2679 /* Get another non-empty pipe group */
2680 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2683 #ifdef RTE_SCHED_DEBUG
2684 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2687 /* Return if pipe group already in one of the other grinders */
2688 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2689 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2692 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2694 /* Install new pipe group into grinder's pipe cache */
2695 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2697 pipe_qmask = grinder->pcache_qmask[0];
2698 pipe_qindex = grinder->pcache_qindex[0];
2699 grinder->pcache_r = 1;
2702 /* Install new pipe in the grinder */
2703 grinder->pindex = pipe_qindex >> 4;
2704 grinder->subport = subport;
2705 grinder->pipe = subport->pipe + grinder->pindex;
2706 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2707 grinder->productive = 0;
2709 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2710 grinder_next_tc(port, subport, pos);
2712 /* Check for pipe exhaustion */
2713 if (grinder->pindex == subport->pipe_loop) {
2714 subport->pipe_exhaustion = 1;
2715 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2723 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2725 struct rte_sched_grinder *grinder = subport->grinder + pos;
2726 struct rte_sched_pipe *pipe = grinder->pipe;
2727 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2728 uint32_t qmask = grinder->qmask;
2730 grinder->wrr_tokens[0] =
2731 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2732 grinder->wrr_tokens[1] =
2733 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2734 grinder->wrr_tokens[2] =
2735 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2736 grinder->wrr_tokens[3] =
2737 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2739 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2740 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2741 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2742 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2744 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2745 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2746 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2747 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2751 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2753 struct rte_sched_grinder *grinder = subport->grinder + pos;
2754 struct rte_sched_pipe *pipe = grinder->pipe;
2756 pipe->wrr_tokens[0] =
2757 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2758 RTE_SCHED_WRR_SHIFT;
2759 pipe->wrr_tokens[1] =
2760 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2761 RTE_SCHED_WRR_SHIFT;
2762 pipe->wrr_tokens[2] =
2763 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2764 RTE_SCHED_WRR_SHIFT;
2765 pipe->wrr_tokens[3] =
2766 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2767 RTE_SCHED_WRR_SHIFT;
2771 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2773 struct rte_sched_grinder *grinder = subport->grinder + pos;
2774 uint16_t wrr_tokens_min;
2776 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2777 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2778 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2779 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2781 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2782 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2784 grinder->wrr_tokens[0] -= wrr_tokens_min;
2785 grinder->wrr_tokens[1] -= wrr_tokens_min;
2786 grinder->wrr_tokens[2] -= wrr_tokens_min;
2787 grinder->wrr_tokens[3] -= wrr_tokens_min;
2791 #define grinder_evict(subport, pos)
2794 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2796 struct rte_sched_grinder *grinder = subport->grinder + pos;
2798 rte_prefetch0(grinder->pipe);
2799 rte_prefetch0(grinder->queue[0]);
2803 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2805 struct rte_sched_grinder *grinder = subport->grinder + pos;
2806 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2808 qsize = grinder->qsize;
2811 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2812 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2814 rte_prefetch0(grinder->qbase[0] + qr[0]);
2818 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2819 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2820 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2821 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2823 rte_prefetch0(grinder->qbase[0] + qr[0]);
2824 rte_prefetch0(grinder->qbase[1] + qr[1]);
2826 grinder_wrr_load(subport, pos);
2827 grinder_wrr(subport, pos);
2829 rte_prefetch0(grinder->qbase[2] + qr[2]);
2830 rte_prefetch0(grinder->qbase[3] + qr[3]);
2834 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2836 struct rte_sched_grinder *grinder = subport->grinder + pos;
2837 uint32_t qpos = grinder->qpos;
2838 struct rte_mbuf **qbase = grinder->qbase[qpos];
2839 uint16_t qsize = grinder->qsize;
2840 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2842 grinder->pkt = qbase[qr];
2843 rte_prefetch0(grinder->pkt);
2845 if (unlikely((qr & 0x7) == 7)) {
2846 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2848 rte_prefetch0(qbase + qr_next);
2852 static inline uint32_t
2853 grinder_handle(struct rte_sched_port *port,
2854 struct rte_sched_subport *subport, uint32_t pos)
2856 struct rte_sched_grinder *grinder = subport->grinder + pos;
2858 switch (grinder->state) {
2859 case e_GRINDER_PREFETCH_PIPE:
2861 if (grinder_next_pipe(port, subport, pos)) {
2862 grinder_prefetch_pipe(subport, pos);
2863 subport->busy_grinders++;
2865 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2872 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2874 struct rte_sched_pipe *pipe = grinder->pipe;
2876 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2877 grinder->subport_params = port->subport_profiles +
2880 grinder_prefetch_tc_queue_arrays(subport, pos);
2882 if (subport->tc_ov_enabled)
2883 grinder_credits_update_with_tc_ov(port, subport, pos);
2885 grinder_credits_update(port, subport, pos);
2887 grinder->state = e_GRINDER_PREFETCH_MBUF;
2891 case e_GRINDER_PREFETCH_MBUF:
2893 grinder_prefetch_mbuf(subport, pos);
2895 grinder->state = e_GRINDER_READ_MBUF;
2899 case e_GRINDER_READ_MBUF:
2901 uint32_t wrr_active, result = 0;
2903 result = grinder_schedule(port, subport, pos);
2905 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2907 /* Look for next packet within the same TC */
2908 if (result && grinder->qmask) {
2910 grinder_wrr(subport, pos);
2912 grinder_prefetch_mbuf(subport, pos);
2918 grinder_wrr_store(subport, pos);
2920 /* Look for another active TC within same pipe */
2921 if (grinder_next_tc(port, subport, pos)) {
2922 grinder_prefetch_tc_queue_arrays(subport, pos);
2924 grinder->state = e_GRINDER_PREFETCH_MBUF;
2928 if (grinder->productive == 0 &&
2929 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2930 subport->pipe_loop = grinder->pindex;
2932 grinder_evict(subport, pos);
2934 /* Look for another active pipe */
2935 if (grinder_next_pipe(port, subport, pos)) {
2936 grinder_prefetch_pipe(subport, pos);
2938 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2942 /* No active pipe found */
2943 subport->busy_grinders--;
2945 grinder->state = e_GRINDER_PREFETCH_PIPE;
2950 rte_panic("Algorithmic error (invalid state)\n");
2956 rte_sched_port_time_resync(struct rte_sched_port *port)
2958 uint64_t cycles = rte_get_tsc_cycles();
2959 uint64_t cycles_diff;
2960 uint64_t bytes_diff;
2963 if (cycles < port->time_cpu_cycles)
2964 port->time_cpu_cycles = 0;
2966 cycles_diff = cycles - port->time_cpu_cycles;
2967 /* Compute elapsed time in bytes */
2968 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2969 port->inv_cycles_per_byte);
2971 /* Advance port time */
2972 port->time_cpu_cycles +=
2973 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2974 port->time_cpu_bytes += bytes_diff;
2975 if (port->time < port->time_cpu_bytes)
2976 port->time = port->time_cpu_bytes;
2978 /* Reset pipe loop detection */
2979 for (i = 0; i < port->n_subports_per_port; i++)
2980 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2984 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2988 /* Check if any exception flag is set */
2989 exceptions = (second_pass && subport->busy_grinders == 0) ||
2990 (subport->pipe_exhaustion == 1);
2992 /* Clear exception flags */
2993 subport->pipe_exhaustion = 0;
2999 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
3001 struct rte_sched_subport *subport;
3002 uint32_t subport_id = port->subport_id;
3003 uint32_t i, n_subports = 0, count;
3005 port->pkts_out = pkts;
3006 port->n_pkts_out = 0;
3008 rte_sched_port_time_resync(port);
3010 /* Take each queue in the grinder one step further */
3011 for (i = 0, count = 0; ; i++) {
3012 subport = port->subports[subport_id];
3014 count += grinder_handle(port, subport,
3015 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
3017 if (count == n_pkts) {
3020 if (subport_id == port->n_subports_per_port)
3023 port->subport_id = subport_id;
3027 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
3033 if (subport_id == port->n_subports_per_port)
3036 if (n_subports == port->n_subports_per_port) {
3037 port->subport_id = subport_id;