1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_memory.h>
11 #include <rte_malloc.h>
12 #include <rte_cycles.h>
13 #include <rte_prefetch.h>
14 #include <rte_branch_prediction.h>
16 #include <rte_bitmap.h>
17 #include <rte_reciprocal.h>
19 #include "rte_sched.h"
20 #include "rte_sched_common.h"
21 #include "rte_approx.h"
23 #ifdef __INTEL_COMPILER
24 #pragma warning(disable:2259) /* conversion may lose significant bits */
27 #ifdef RTE_SCHED_VECTOR
31 #define SCHED_VECTOR_SSE4
32 #elif defined(__ARM_NEON)
33 #define SCHED_VECTOR_NEON
38 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
39 #define RTE_SCHED_WRR_SHIFT 3
40 #define RTE_SCHED_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
41 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
42 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
43 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
45 /* Scaling for cycles_per_byte calculation
46 * Chosen so that minimum rate is 480 bit/sec
48 #define RTE_SCHED_TIME_SHIFT 8
50 struct rte_sched_pipe_profile {
51 /* Token bucket (TB) */
53 uint64_t tb_credits_per_period;
56 /* Pipe traffic classes */
58 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
61 /* Pipe best-effort traffic class queues */
62 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
65 struct rte_sched_pipe {
66 /* Token bucket (TB) */
67 uint64_t tb_time; /* time of last update */
70 /* Pipe profile and flags */
73 /* Traffic classes (TCs) */
74 uint64_t tc_time; /* time of next update */
75 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
77 /* Weighted Round Robin (WRR) */
78 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
80 /* TC oversubscription */
81 uint64_t tc_ov_credits;
82 uint8_t tc_ov_period_id;
83 } __rte_cache_aligned;
85 struct rte_sched_queue {
90 struct rte_sched_queue_extra {
91 struct rte_sched_queue_stats stats;
98 e_GRINDER_PREFETCH_PIPE = 0,
99 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
100 e_GRINDER_PREFETCH_MBUF,
104 struct rte_sched_subport_profile {
105 /* Token bucket (TB) */
107 uint64_t tb_credits_per_period;
110 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
114 struct rte_sched_grinder {
116 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
117 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
122 enum grinder_state state;
125 struct rte_sched_subport *subport;
126 struct rte_sched_subport_profile *subport_params;
127 struct rte_sched_pipe *pipe;
128 struct rte_sched_pipe_profile *pipe_params;
131 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
132 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
138 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
139 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
140 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
144 struct rte_mbuf *pkt;
147 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
148 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
149 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
152 struct rte_sched_subport {
153 /* Token bucket (TB) */
154 uint64_t tb_time; /* time of last update */
157 /* Traffic classes (TCs) */
158 uint64_t tc_time; /* time of next update */
159 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
161 /* TC oversubscription */
163 uint64_t tc_ov_wm_min;
164 uint64_t tc_ov_wm_max;
165 uint8_t tc_ov_period_id;
171 struct rte_sched_subport_stats stats __rte_cache_aligned;
173 /* subport profile */
176 uint32_t n_pipes_per_subport_enabled;
177 uint32_t n_pipe_profiles;
178 uint32_t n_max_pipe_profiles;
180 /* Pipe best-effort TC rate */
181 uint64_t pipe_tc_be_rate_max;
183 /* Pipe queues size */
184 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
187 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
190 /* Scheduling loop detection */
192 uint32_t pipe_exhaustion;
195 struct rte_bitmap *bmp;
196 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
199 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
200 uint32_t busy_grinders;
202 /* Queue base calculation */
203 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
206 struct rte_sched_pipe *pipe;
207 struct rte_sched_queue *queue;
208 struct rte_sched_queue_extra *queue_extra;
209 struct rte_sched_pipe_profile *pipe_profiles;
211 struct rte_mbuf **queue_array;
212 uint8_t memory[0] __rte_cache_aligned;
213 } __rte_cache_aligned;
215 struct rte_sched_port {
216 /* User parameters */
217 uint32_t n_subports_per_port;
218 uint32_t n_pipes_per_subport;
219 uint32_t n_pipes_per_subport_log2;
220 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
221 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
222 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
223 uint32_t n_subport_profiles;
224 uint32_t n_max_subport_profiles;
227 uint32_t frame_overhead;
231 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
232 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
233 uint64_t time; /* Current NIC TX time measured in bytes */
234 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
235 uint64_t cycles_per_byte;
238 struct rte_mbuf **pkts_out;
242 /* Large data structures */
243 struct rte_sched_subport_profile *subport_profiles;
244 struct rte_sched_subport *subports[0] __rte_cache_aligned;
245 } __rte_cache_aligned;
247 enum rte_sched_subport_array {
248 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
249 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
250 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
251 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
252 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
253 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
254 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
257 static inline uint32_t
258 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
260 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
263 static inline struct rte_mbuf **
264 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
266 uint32_t pindex = qindex >> 4;
267 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
269 return (subport->queue_array + pindex *
270 subport->qsize_sum + subport->qsize_add[qpos]);
273 static inline uint16_t
274 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
275 struct rte_sched_subport *subport, uint32_t qindex)
277 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
279 return subport->qsize[tc];
282 static inline uint32_t
283 rte_sched_port_queues_per_port(struct rte_sched_port *port)
285 uint32_t n_queues = 0, i;
287 for (i = 0; i < port->n_subports_per_port; i++)
288 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
293 static inline uint16_t
294 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
296 uint16_t pipe_queue = port->pipe_queue[traffic_class];
301 static inline uint8_t
302 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
304 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
309 static inline uint8_t
310 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
312 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
318 pipe_profile_check(struct rte_sched_pipe_params *params,
319 uint64_t rate, uint16_t *qsize)
323 /* Pipe parameters */
324 if (params == NULL) {
326 "%s: Incorrect value for parameter params\n", __func__);
330 /* TB rate: non-zero, not greater than port rate */
331 if (params->tb_rate == 0 ||
332 params->tb_rate > rate) {
334 "%s: Incorrect value for tb rate\n", __func__);
338 /* TB size: non-zero */
339 if (params->tb_size == 0) {
341 "%s: Incorrect value for tb size\n", __func__);
345 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
346 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
347 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
348 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
349 params->tc_rate[i] > params->tb_rate))) {
351 "%s: Incorrect value for qsize or tc_rate\n", __func__);
356 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
357 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
359 "%s: Incorrect value for be traffic class rate\n", __func__);
363 /* TC period: non-zero */
364 if (params->tc_period == 0) {
366 "%s: Incorrect value for tc period\n", __func__);
370 /* Best effort tc oversubscription weight: non-zero */
371 if (params->tc_ov_weight == 0) {
373 "%s: Incorrect value for tc ov weight\n", __func__);
377 /* Queue WRR weights: non-zero */
378 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
379 if (params->wrr_weights[i] == 0) {
381 "%s: Incorrect value for wrr weight\n", __func__);
390 subport_profile_check(struct rte_sched_subport_profile_params *params,
395 /* Check user parameters */
396 if (params == NULL) {
397 RTE_LOG(ERR, SCHED, "%s: "
398 "Incorrect value for parameter params\n", __func__);
402 if (params->tb_rate == 0 || params->tb_rate > rate) {
403 RTE_LOG(ERR, SCHED, "%s: "
404 "Incorrect value for tb rate\n", __func__);
408 if (params->tb_size == 0) {
409 RTE_LOG(ERR, SCHED, "%s: "
410 "Incorrect value for tb size\n", __func__);
414 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
415 uint64_t tc_rate = params->tc_rate[i];
417 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
418 RTE_LOG(ERR, SCHED, "%s: "
419 "Incorrect value for tc rate\n", __func__);
424 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
425 RTE_LOG(ERR, SCHED, "%s: "
426 "Incorrect tc rate(best effort)\n", __func__);
430 if (params->tc_period == 0) {
431 RTE_LOG(ERR, SCHED, "%s: "
432 "Incorrect value for tc period\n", __func__);
440 rte_sched_port_check_params(struct rte_sched_port_params *params)
444 if (params == NULL) {
446 "%s: Incorrect value for parameter params\n", __func__);
451 if (params->socket < 0) {
453 "%s: Incorrect value for socket id\n", __func__);
458 if (params->rate == 0) {
460 "%s: Incorrect value for rate\n", __func__);
465 if (params->mtu == 0) {
467 "%s: Incorrect value for mtu\n", __func__);
471 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
472 if (params->n_subports_per_port == 0 ||
473 params->n_subports_per_port > 1u << 16 ||
474 !rte_is_power_of_2(params->n_subports_per_port)) {
476 "%s: Incorrect value for number of subports\n", __func__);
480 if (params->subport_profiles == NULL ||
481 params->n_subport_profiles == 0 ||
482 params->n_max_subport_profiles == 0 ||
483 params->n_subport_profiles > params->n_max_subport_profiles) {
485 "%s: Incorrect value for subport profiles\n", __func__);
489 for (i = 0; i < params->n_subport_profiles; i++) {
490 struct rte_sched_subport_profile_params *p =
491 params->subport_profiles + i;
494 status = subport_profile_check(p, params->rate);
497 "%s: subport profile check failed(%d)\n",
503 /* n_pipes_per_subport: non-zero, power of 2 */
504 if (params->n_pipes_per_subport == 0 ||
505 !rte_is_power_of_2(params->n_pipes_per_subport)) {
507 "%s: Incorrect value for maximum pipes number\n", __func__);
515 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
516 enum rte_sched_subport_array array)
518 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
519 uint32_t n_subport_pipe_queues =
520 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
522 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
523 uint32_t size_queue =
524 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
525 uint32_t size_queue_extra
526 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
527 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
528 sizeof(struct rte_sched_pipe_profile);
529 uint32_t size_bmp_array =
530 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
531 uint32_t size_per_pipe_queue_array, size_queue_array;
535 size_per_pipe_queue_array = 0;
536 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
537 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
538 size_per_pipe_queue_array +=
539 params->qsize[i] * sizeof(struct rte_mbuf *);
541 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
542 params->qsize[i] * sizeof(struct rte_mbuf *);
544 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
548 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
550 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
552 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
554 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
556 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
558 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
560 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
562 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
564 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
566 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
568 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
570 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
576 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
580 subport->qsize_add[0] = 0;
582 /* Strict prority traffic class */
583 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
584 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
586 /* Best-effort traffic class */
587 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
588 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
589 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
590 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
591 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
592 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
593 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
594 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
595 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
597 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
598 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
602 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
604 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
606 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
607 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
608 " Traffic classes: period = %"PRIu64",\n"
609 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
610 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
611 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
612 " Best-effort traffic class oversubscription: weight = %hhu\n"
613 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
618 p->tb_credits_per_period,
621 /* Traffic classes */
623 p->tc_credits_per_period[0],
624 p->tc_credits_per_period[1],
625 p->tc_credits_per_period[2],
626 p->tc_credits_per_period[3],
627 p->tc_credits_per_period[4],
628 p->tc_credits_per_period[5],
629 p->tc_credits_per_period[6],
630 p->tc_credits_per_period[7],
631 p->tc_credits_per_period[8],
632 p->tc_credits_per_period[9],
633 p->tc_credits_per_period[10],
634 p->tc_credits_per_period[11],
635 p->tc_credits_per_period[12],
637 /* Best-effort traffic class oversubscription */
641 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
645 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
647 struct rte_sched_subport_profile *p = port->subport_profiles + i;
649 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
650 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
652 "Traffic classes: period = %"PRIu64",\n"
653 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
654 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
655 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
660 p->tb_credits_per_period,
663 /* Traffic classes */
665 p->tc_credits_per_period[0],
666 p->tc_credits_per_period[1],
667 p->tc_credits_per_period[2],
668 p->tc_credits_per_period[3],
669 p->tc_credits_per_period[4],
670 p->tc_credits_per_period[5],
671 p->tc_credits_per_period[6],
672 p->tc_credits_per_period[7],
673 p->tc_credits_per_period[8],
674 p->tc_credits_per_period[9],
675 p->tc_credits_per_period[10],
676 p->tc_credits_per_period[11],
677 p->tc_credits_per_period[12]);
680 static inline uint64_t
681 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
683 uint64_t time = time_ms;
685 time = (time * rate) / 1000;
691 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
692 struct rte_sched_pipe_params *src,
693 struct rte_sched_pipe_profile *dst,
696 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
697 uint32_t lcd1, lcd2, lcd;
701 if (src->tb_rate == rate) {
702 dst->tb_credits_per_period = 1;
705 double tb_rate = (double) src->tb_rate
707 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
709 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
713 dst->tb_size = src->tb_size;
715 /* Traffic Classes */
716 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
719 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
720 if (subport->qsize[i])
721 dst->tc_credits_per_period[i]
722 = rte_sched_time_ms_to_bytes(src->tc_period,
725 dst->tc_ov_weight = src->tc_ov_weight;
728 wrr_cost[0] = src->wrr_weights[0];
729 wrr_cost[1] = src->wrr_weights[1];
730 wrr_cost[2] = src->wrr_weights[2];
731 wrr_cost[3] = src->wrr_weights[3];
733 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
734 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
735 lcd = rte_get_lcd(lcd1, lcd2);
737 wrr_cost[0] = lcd / wrr_cost[0];
738 wrr_cost[1] = lcd / wrr_cost[1];
739 wrr_cost[2] = lcd / wrr_cost[2];
740 wrr_cost[3] = lcd / wrr_cost[3];
742 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
743 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
744 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
745 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
749 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
750 struct rte_sched_subport_profile *dst,
756 if (src->tb_rate == rate) {
757 dst->tb_credits_per_period = 1;
760 double tb_rate = (double) src->tb_rate
762 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
764 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
768 dst->tb_size = src->tb_size;
770 /* Traffic Classes */
771 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
773 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
774 dst->tc_credits_per_period[i]
775 = rte_sched_time_ms_to_bytes(src->tc_period,
780 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
781 struct rte_sched_subport_params *params, uint64_t rate)
785 for (i = 0; i < subport->n_pipe_profiles; i++) {
786 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
787 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
789 rte_sched_pipe_profile_convert(subport, src, dst, rate);
790 rte_sched_port_log_pipe_profile(subport, i);
793 subport->pipe_tc_be_rate_max = 0;
794 for (i = 0; i < subport->n_pipe_profiles; i++) {
795 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
796 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
798 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
799 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
804 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
805 struct rte_sched_port_params *params,
810 for (i = 0; i < port->n_subport_profiles; i++) {
811 struct rte_sched_subport_profile_params *src
812 = params->subport_profiles + i;
813 struct rte_sched_subport_profile *dst
814 = port->subport_profiles + i;
816 rte_sched_subport_profile_convert(src, dst, rate);
817 rte_sched_port_log_subport_profile(port, i);
822 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
823 uint32_t n_max_pipes_per_subport,
828 /* Check user parameters */
829 if (params == NULL) {
831 "%s: Incorrect value for parameter params\n", __func__);
835 /* qsize: if non-zero, power of 2,
836 * no bigger than 32K (due to 16-bit read/write pointers)
838 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
839 uint16_t qsize = params->qsize[i];
841 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
843 "%s: Incorrect value for qsize\n", __func__);
848 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
849 RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
853 /* n_pipes_per_subport: non-zero, power of 2 */
854 if (params->n_pipes_per_subport_enabled == 0 ||
855 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
856 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
858 "%s: Incorrect value for pipes number\n", __func__);
862 /* pipe_profiles and n_pipe_profiles */
863 if (params->pipe_profiles == NULL ||
864 params->n_pipe_profiles == 0 ||
865 params->n_max_pipe_profiles == 0 ||
866 params->n_pipe_profiles > params->n_max_pipe_profiles) {
868 "%s: Incorrect value for pipe profiles\n", __func__);
872 for (i = 0; i < params->n_pipe_profiles; i++) {
873 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
876 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
879 "%s: Pipe profile check failed(%d)\n", __func__, status);
888 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
889 struct rte_sched_subport_params **subport_params)
891 uint32_t size0 = 0, size1 = 0, i;
894 status = rte_sched_port_check_params(port_params);
897 "%s: Port scheduler port params check failed (%d)\n",
903 for (i = 0; i < port_params->n_subports_per_port; i++) {
904 struct rte_sched_subport_params *sp = subport_params[i];
906 status = rte_sched_subport_check_params(sp,
907 port_params->n_pipes_per_subport,
911 "%s: Port scheduler subport params check failed (%d)\n",
918 size0 = sizeof(struct rte_sched_port);
920 for (i = 0; i < port_params->n_subports_per_port; i++) {
921 struct rte_sched_subport_params *sp = subport_params[i];
923 size1 += rte_sched_subport_get_array_base(sp,
924 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
927 return size0 + size1;
930 struct rte_sched_port *
931 rte_sched_port_config(struct rte_sched_port_params *params)
933 struct rte_sched_port *port = NULL;
934 uint32_t size0, size1, size2;
935 uint32_t cycles_per_byte;
939 status = rte_sched_port_check_params(params);
942 "%s: Port scheduler params check failed (%d)\n",
947 size0 = sizeof(struct rte_sched_port);
948 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
949 size2 = params->n_max_subport_profiles *
950 sizeof(struct rte_sched_subport_profile);
952 /* Allocate memory to store the data structures */
953 port = rte_zmalloc_socket("qos_params", size0 + size1,
954 RTE_CACHE_LINE_SIZE, params->socket);
956 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
961 /* Allocate memory to store the subport profile */
962 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
963 RTE_CACHE_LINE_SIZE, params->socket);
964 if (port->subport_profiles == NULL) {
965 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
970 /* User parameters */
971 port->n_subports_per_port = params->n_subports_per_port;
972 port->n_subport_profiles = params->n_subport_profiles;
973 port->n_max_subport_profiles = params->n_max_subport_profiles;
974 port->n_pipes_per_subport = params->n_pipes_per_subport;
975 port->n_pipes_per_subport_log2 =
976 __builtin_ctz(params->n_pipes_per_subport);
977 port->socket = params->socket;
979 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
980 port->pipe_queue[i] = i;
982 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
983 port->pipe_tc[i] = j;
985 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
989 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
990 port->tc_queue[i] = j;
992 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
995 port->rate = params->rate;
996 port->mtu = params->mtu + params->frame_overhead;
997 port->frame_overhead = params->frame_overhead;
1000 port->time_cpu_cycles = rte_get_tsc_cycles();
1001 port->time_cpu_bytes = 0;
1004 /* Subport profile table */
1005 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1007 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1009 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1010 port->cycles_per_byte = cycles_per_byte;
1013 port->pkts_out = NULL;
1014 port->n_pkts_out = 0;
1015 port->subport_id = 0;
1021 rte_sched_subport_free(struct rte_sched_port *port,
1022 struct rte_sched_subport *subport)
1024 uint32_t n_subport_pipe_queues;
1027 if (subport == NULL)
1030 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1032 /* Free enqueued mbufs */
1033 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1034 struct rte_mbuf **mbufs =
1035 rte_sched_subport_pipe_qbase(subport, qindex);
1036 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1038 struct rte_sched_queue *queue = subport->queue + qindex;
1039 uint16_t qr = queue->qr & (qsize - 1);
1040 uint16_t qw = queue->qw & (qsize - 1);
1042 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1043 rte_pktmbuf_free(mbufs[qr]);
1051 rte_sched_port_free(struct rte_sched_port *port)
1055 /* Check user parameters */
1059 for (i = 0; i < port->n_subports_per_port; i++)
1060 rte_sched_subport_free(port, port->subports[i]);
1062 rte_free(port->subport_profiles);
1067 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1071 for (i = 0; i < n_subports; i++) {
1072 struct rte_sched_subport *subport = port->subports[i];
1074 rte_sched_subport_free(port, subport);
1077 rte_free(port->subport_profiles);
1082 rte_sched_subport_config(struct rte_sched_port *port,
1083 uint32_t subport_id,
1084 struct rte_sched_subport_params *params,
1085 uint32_t subport_profile_id)
1087 struct rte_sched_subport *s = NULL;
1088 uint32_t n_subports = subport_id;
1089 struct rte_sched_subport_profile *profile;
1090 uint32_t n_subport_pipe_queues, i;
1091 uint32_t size0, size1, bmp_mem_size;
1095 /* Check user parameters */
1098 "%s: Incorrect value for parameter port\n", __func__);
1102 if (subport_id >= port->n_subports_per_port) {
1104 "%s: Incorrect value for subport id\n", __func__);
1109 if (subport_profile_id >= port->n_max_subport_profiles) {
1110 RTE_LOG(ERR, SCHED, "%s: "
1111 "Number of subport profile exceeds the max limit\n",
1117 /** Memory is allocated only on first invocation of the api for a
1118 * given subport. Subsequent invocation on same subport will just
1119 * update subport bandwidth parameter.
1121 if (port->subports[subport_id] == NULL) {
1123 status = rte_sched_subport_check_params(params,
1124 port->n_pipes_per_subport,
1127 RTE_LOG(NOTICE, SCHED,
1128 "%s: Port scheduler params check failed (%d)\n",
1134 /* Determine the amount of memory to allocate */
1135 size0 = sizeof(struct rte_sched_subport);
1136 size1 = rte_sched_subport_get_array_base(params,
1137 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1139 /* Allocate memory to store the data structures */
1140 s = rte_zmalloc_socket("subport_params", size0 + size1,
1141 RTE_CACHE_LINE_SIZE, port->socket);
1144 "%s: Memory allocation fails\n", __func__);
1151 subport_profile_id = 0;
1154 port->subports[subport_id] = s;
1156 s->tb_time = port->time;
1158 /* compile time checks */
1159 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1160 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1161 (RTE_SCHED_PORT_N_GRINDERS - 1));
1163 /* User parameters */
1164 s->n_pipes_per_subport_enabled =
1165 params->n_pipes_per_subport_enabled;
1166 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1167 s->n_pipe_profiles = params->n_pipe_profiles;
1168 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1170 #ifdef RTE_SCHED_RED
1171 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1174 for (j = 0; j < RTE_COLORS; j++) {
1175 /* if min/max are both zero, then RED is disabled */
1176 if ((params->red_params[i][j].min_th |
1177 params->red_params[i][j].max_th) == 0) {
1181 if (rte_red_config_init(&s->red_config[i][j],
1182 params->red_params[i][j].wq_log2,
1183 params->red_params[i][j].min_th,
1184 params->red_params[i][j].max_th,
1185 params->red_params[i][j].maxp_inv) != 0) {
1186 RTE_LOG(NOTICE, SCHED,
1187 "%s: RED configuration init fails\n",
1196 /* Scheduling loop detection */
1197 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1198 s->pipe_exhaustion = 0;
1201 s->busy_grinders = 0;
1203 /* Queue base calculation */
1204 rte_sched_subport_config_qsize(s);
1206 /* Large data structures */
1207 s->pipe = (struct rte_sched_pipe *)
1208 (s->memory + rte_sched_subport_get_array_base(params,
1209 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1210 s->queue = (struct rte_sched_queue *)
1211 (s->memory + rte_sched_subport_get_array_base(params,
1212 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1213 s->queue_extra = (struct rte_sched_queue_extra *)
1214 (s->memory + rte_sched_subport_get_array_base(params,
1215 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1216 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1217 (s->memory + rte_sched_subport_get_array_base(params,
1218 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1219 s->bmp_array = s->memory + rte_sched_subport_get_array_base(
1220 params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1221 s->queue_array = (struct rte_mbuf **)
1222 (s->memory + rte_sched_subport_get_array_base(params,
1223 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1225 /* Pipe profile table */
1226 rte_sched_subport_config_pipe_profile_table(s, params,
1230 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1231 bmp_mem_size = rte_bitmap_get_memory_footprint(
1232 n_subport_pipe_queues);
1233 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1235 if (s->bmp == NULL) {
1237 "%s: Subport bitmap init error\n", __func__);
1242 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1243 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1245 #ifdef RTE_SCHED_SUBPORT_TC_OV
1246 /* TC oversubscription */
1247 s->tc_ov_wm_min = port->mtu;
1248 s->tc_ov_period_id = 0;
1256 /* update subport parameters from subport profile table*/
1257 profile = port->subport_profiles + subport_profile_id;
1259 s = port->subports[subport_id];
1261 s->tb_credits = profile->tb_size / 2;
1263 s->tc_time = port->time + profile->tc_period;
1265 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1268 profile->tc_credits_per_period[i];
1270 profile->tc_credits_per_period[i] = 0;
1272 #ifdef RTE_SCHED_SUBPORT_TC_OV
1273 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1274 s->pipe_tc_be_rate_max);
1275 s->tc_ov_wm = s->tc_ov_wm_max;
1277 s->profile = subport_profile_id;
1281 rte_sched_port_log_subport_profile(port, subport_profile_id);
1286 rte_sched_free_memory(port, n_subports);
1292 rte_sched_pipe_config(struct rte_sched_port *port,
1293 uint32_t subport_id,
1295 int32_t pipe_profile)
1297 struct rte_sched_subport *s;
1298 struct rte_sched_subport_profile *sp;
1299 struct rte_sched_pipe *p;
1300 struct rte_sched_pipe_profile *params;
1301 uint32_t n_subports = subport_id + 1;
1302 uint32_t deactivate, profile, i;
1305 /* Check user parameters */
1306 profile = (uint32_t) pipe_profile;
1307 deactivate = (pipe_profile < 0);
1311 "%s: Incorrect value for parameter port\n", __func__);
1315 if (subport_id >= port->n_subports_per_port) {
1317 "%s: Incorrect value for parameter subport id\n", __func__);
1322 s = port->subports[subport_id];
1323 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1325 "%s: Incorrect value for parameter pipe id\n", __func__);
1330 if (!deactivate && profile >= s->n_pipe_profiles) {
1332 "%s: Incorrect value for parameter pipe profile\n", __func__);
1337 sp = port->subport_profiles + s->profile;
1338 /* Handle the case when pipe already has a valid configuration */
1339 p = s->pipe + pipe_id;
1341 params = s->pipe_profiles + p->profile;
1343 double subport_tc_be_rate =
1344 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1345 / (double) sp->tc_period;
1346 double pipe_tc_be_rate =
1347 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1348 / (double) params->tc_period;
1349 uint32_t tc_be_ov = s->tc_ov;
1351 /* Unplug pipe from its subport */
1352 s->tc_ov_n -= params->tc_ov_weight;
1353 s->tc_ov_rate -= pipe_tc_be_rate;
1354 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1356 if (s->tc_ov != tc_be_ov) {
1357 RTE_LOG(DEBUG, SCHED,
1358 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1359 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1362 /* Reset the pipe */
1363 memset(p, 0, sizeof(struct rte_sched_pipe));
1369 /* Apply the new pipe configuration */
1370 p->profile = profile;
1371 params = s->pipe_profiles + p->profile;
1373 /* Token Bucket (TB) */
1374 p->tb_time = port->time;
1375 p->tb_credits = params->tb_size / 2;
1377 /* Traffic Classes (TCs) */
1378 p->tc_time = port->time + params->tc_period;
1380 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1382 p->tc_credits[i] = params->tc_credits_per_period[i];
1385 /* Subport best effort tc oversubscription */
1386 double subport_tc_be_rate =
1387 (double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1388 / (double) sp->tc_period;
1389 double pipe_tc_be_rate =
1390 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1391 / (double) params->tc_period;
1392 uint32_t tc_be_ov = s->tc_ov;
1394 s->tc_ov_n += params->tc_ov_weight;
1395 s->tc_ov_rate += pipe_tc_be_rate;
1396 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1398 if (s->tc_ov != tc_be_ov) {
1399 RTE_LOG(DEBUG, SCHED,
1400 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1401 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1403 p->tc_ov_period_id = s->tc_ov_period_id;
1404 p->tc_ov_credits = s->tc_ov_wm;
1410 rte_sched_free_memory(port, n_subports);
1416 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1417 uint32_t subport_id,
1418 struct rte_sched_pipe_params *params,
1419 uint32_t *pipe_profile_id)
1421 struct rte_sched_subport *s;
1422 struct rte_sched_pipe_profile *pp;
1429 "%s: Incorrect value for parameter port\n", __func__);
1433 /* Subport id not exceeds the max limit */
1434 if (subport_id > port->n_subports_per_port) {
1436 "%s: Incorrect value for subport id\n", __func__);
1440 s = port->subports[subport_id];
1442 /* Pipe profiles exceeds the max limit */
1443 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1445 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1450 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1453 "%s: Pipe profile check failed(%d)\n", __func__, status);
1457 pp = &s->pipe_profiles[s->n_pipe_profiles];
1458 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1460 /* Pipe profile should not exists */
1461 for (i = 0; i < s->n_pipe_profiles; i++)
1462 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1464 "%s: Pipe profile exists\n", __func__);
1468 /* Pipe profile commit */
1469 *pipe_profile_id = s->n_pipe_profiles;
1470 s->n_pipe_profiles++;
1472 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1473 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1475 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1481 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1482 struct rte_sched_subport_profile_params *params,
1483 uint32_t *subport_profile_id)
1487 struct rte_sched_subport_profile *dst;
1491 RTE_LOG(ERR, SCHED, "%s: "
1492 "Incorrect value for parameter port\n", __func__);
1496 if (params == NULL) {
1497 RTE_LOG(ERR, SCHED, "%s: "
1498 "Incorrect value for parameter profile\n", __func__);
1502 if (subport_profile_id == NULL) {
1503 RTE_LOG(ERR, SCHED, "%s: "
1504 "Incorrect value for parameter subport_profile_id\n",
1509 dst = port->subport_profiles + port->n_subport_profiles;
1511 /* Subport profiles exceeds the max limit */
1512 if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1513 RTE_LOG(ERR, SCHED, "%s: "
1514 "Number of subport profiles exceeds the max limit\n",
1519 status = subport_profile_check(params, port->rate);
1522 "%s: subport profile check failed(%d)\n", __func__, status);
1526 rte_sched_subport_profile_convert(params, dst, port->rate);
1528 /* Subport profile should not exists */
1529 for (i = 0; i < port->n_subport_profiles; i++)
1530 if (memcmp(port->subport_profiles + i,
1531 dst, sizeof(*dst)) == 0) {
1533 "%s: subport profile exists\n", __func__);
1537 /* Subport profile commit */
1538 *subport_profile_id = port->n_subport_profiles;
1539 port->n_subport_profiles++;
1541 rte_sched_port_log_subport_profile(port, *subport_profile_id);
1546 static inline uint32_t
1547 rte_sched_port_qindex(struct rte_sched_port *port,
1550 uint32_t traffic_class,
1553 return ((subport & (port->n_subports_per_port - 1)) <<
1554 (port->n_pipes_per_subport_log2 + 4)) |
1556 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1557 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1558 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1562 rte_sched_port_pkt_write(struct rte_sched_port *port,
1563 struct rte_mbuf *pkt,
1564 uint32_t subport, uint32_t pipe,
1565 uint32_t traffic_class,
1566 uint32_t queue, enum rte_color color)
1569 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1571 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1575 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1576 const struct rte_mbuf *pkt,
1577 uint32_t *subport, uint32_t *pipe,
1578 uint32_t *traffic_class, uint32_t *queue)
1580 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1582 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1583 *pipe = (queue_id >> 4) &
1584 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1585 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1586 *queue = rte_sched_port_tc_queue(port, queue_id);
1590 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1592 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1596 rte_sched_subport_read_stats(struct rte_sched_port *port,
1597 uint32_t subport_id,
1598 struct rte_sched_subport_stats *stats,
1601 struct rte_sched_subport *s;
1603 /* Check user parameters */
1606 "%s: Incorrect value for parameter port\n", __func__);
1610 if (subport_id >= port->n_subports_per_port) {
1612 "%s: Incorrect value for subport id\n", __func__);
1616 if (stats == NULL) {
1618 "%s: Incorrect value for parameter stats\n", __func__);
1622 if (tc_ov == NULL) {
1624 "%s: Incorrect value for tc_ov\n", __func__);
1628 s = port->subports[subport_id];
1630 /* Copy subport stats and clear */
1631 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1632 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1634 /* Subport TC oversubscription status */
1641 rte_sched_queue_read_stats(struct rte_sched_port *port,
1643 struct rte_sched_queue_stats *stats,
1646 struct rte_sched_subport *s;
1647 struct rte_sched_queue *q;
1648 struct rte_sched_queue_extra *qe;
1649 uint32_t subport_id, subport_qmask, subport_qindex;
1651 /* Check user parameters */
1654 "%s: Incorrect value for parameter port\n", __func__);
1658 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1660 "%s: Incorrect value for queue id\n", __func__);
1664 if (stats == NULL) {
1666 "%s: Incorrect value for parameter stats\n", __func__);
1672 "%s: Incorrect value for parameter qlen\n", __func__);
1675 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1676 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1678 s = port->subports[subport_id];
1679 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1680 q = s->queue + subport_qindex;
1681 qe = s->queue_extra + subport_qindex;
1683 /* Copy queue stats and clear */
1684 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1685 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1688 *qlen = q->qw - q->qr;
1693 #ifdef RTE_SCHED_DEBUG
1696 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1699 struct rte_sched_queue *queue = subport->queue + qindex;
1701 return queue->qr == queue->qw;
1704 #endif /* RTE_SCHED_DEBUG */
1706 #ifdef RTE_SCHED_COLLECT_STATS
1709 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1710 struct rte_sched_subport *subport,
1712 struct rte_mbuf *pkt)
1714 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1715 uint32_t pkt_len = pkt->pkt_len;
1717 subport->stats.n_pkts_tc[tc_index] += 1;
1718 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1721 #ifdef RTE_SCHED_RED
1723 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1724 struct rte_sched_subport *subport,
1726 struct rte_mbuf *pkt,
1730 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1731 struct rte_sched_subport *subport,
1733 struct rte_mbuf *pkt,
1734 __rte_unused uint32_t red)
1737 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1738 uint32_t pkt_len = pkt->pkt_len;
1740 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1741 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1742 #ifdef RTE_SCHED_RED
1743 subport->stats.n_pkts_red_dropped[tc_index] += red;
1748 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1750 struct rte_mbuf *pkt)
1752 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1753 uint32_t pkt_len = pkt->pkt_len;
1755 qe->stats.n_pkts += 1;
1756 qe->stats.n_bytes += pkt_len;
1759 #ifdef RTE_SCHED_RED
1761 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1763 struct rte_mbuf *pkt,
1767 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1769 struct rte_mbuf *pkt,
1770 __rte_unused uint32_t red)
1773 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1774 uint32_t pkt_len = pkt->pkt_len;
1776 qe->stats.n_pkts_dropped += 1;
1777 qe->stats.n_bytes_dropped += pkt_len;
1778 #ifdef RTE_SCHED_RED
1779 qe->stats.n_pkts_red_dropped += red;
1783 #endif /* RTE_SCHED_COLLECT_STATS */
1785 #ifdef RTE_SCHED_RED
1788 rte_sched_port_red_drop(struct rte_sched_port *port,
1789 struct rte_sched_subport *subport,
1790 struct rte_mbuf *pkt,
1794 struct rte_sched_queue_extra *qe;
1795 struct rte_red_config *red_cfg;
1796 struct rte_red *red;
1798 enum rte_color color;
1800 tc_index = rte_sched_port_pipe_tc(port, qindex);
1801 color = rte_sched_port_pkt_read_color(pkt);
1802 red_cfg = &subport->red_config[tc_index][color];
1804 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1807 qe = subport->queue_extra + qindex;
1810 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1814 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port,
1815 struct rte_sched_subport *subport, uint32_t qindex)
1817 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1818 struct rte_red *red = &qe->red;
1820 rte_red_mark_queue_empty(red, port->time);
1825 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1826 struct rte_sched_subport *subport __rte_unused,
1827 struct rte_mbuf *pkt __rte_unused,
1828 uint32_t qindex __rte_unused,
1829 uint16_t qlen __rte_unused)
1834 #define rte_sched_port_set_queue_empty_timestamp(port, subport, qindex)
1836 #endif /* RTE_SCHED_RED */
1838 #ifdef RTE_SCHED_DEBUG
1841 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1848 rte_panic("Empty slab at position %u\n", bmp_pos);
1851 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1852 if (mask & bmp_slab) {
1853 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1854 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1861 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1865 #endif /* RTE_SCHED_DEBUG */
1867 static inline struct rte_sched_subport *
1868 rte_sched_port_subport(struct rte_sched_port *port,
1869 struct rte_mbuf *pkt)
1871 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1872 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1874 return port->subports[subport_id];
1877 static inline uint32_t
1878 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1879 struct rte_mbuf *pkt, uint32_t subport_qmask)
1881 struct rte_sched_queue *q;
1882 #ifdef RTE_SCHED_COLLECT_STATS
1883 struct rte_sched_queue_extra *qe;
1885 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1886 uint32_t subport_queue_id = subport_qmask & qindex;
1888 q = subport->queue + subport_queue_id;
1890 #ifdef RTE_SCHED_COLLECT_STATS
1891 qe = subport->queue_extra + subport_queue_id;
1895 return subport_queue_id;
1899 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1900 struct rte_sched_subport *subport,
1902 struct rte_mbuf **qbase)
1904 struct rte_sched_queue *q;
1905 struct rte_mbuf **q_qw;
1908 q = subport->queue + qindex;
1909 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1910 q_qw = qbase + (q->qw & (qsize - 1));
1912 rte_prefetch0(q_qw);
1913 rte_bitmap_prefetch0(subport->bmp, qindex);
1917 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1918 struct rte_sched_subport *subport,
1920 struct rte_mbuf **qbase,
1921 struct rte_mbuf *pkt)
1923 struct rte_sched_queue *q;
1927 q = subport->queue + qindex;
1928 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1929 qlen = q->qw - q->qr;
1931 /* Drop the packet (and update drop stats) when queue is full */
1932 if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1934 rte_pktmbuf_free(pkt);
1935 #ifdef RTE_SCHED_COLLECT_STATS
1936 rte_sched_port_update_subport_stats_on_drop(port, subport,
1937 qindex, pkt, qlen < qsize);
1938 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1944 /* Enqueue packet */
1945 qbase[q->qw & (qsize - 1)] = pkt;
1948 /* Activate queue in the subport bitmap */
1949 rte_bitmap_set(subport->bmp, qindex);
1952 #ifdef RTE_SCHED_COLLECT_STATS
1953 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
1954 rte_sched_port_update_queue_stats(subport, qindex, pkt);
1962 * The enqueue function implements a 4-level pipeline with each stage
1963 * processing two different packets. The purpose of using a pipeline
1964 * is to hide the latency of prefetching the data structures. The
1965 * naming convention is presented in the diagram below:
1967 * p00 _______ p10 _______ p20 _______ p30 _______
1968 * ----->| |----->| |----->| |----->| |----->
1969 * | 0 | | 1 | | 2 | | 3 |
1970 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1975 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1978 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1979 *pkt30, *pkt31, *pkt_last;
1980 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1981 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1982 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
1983 *subport20, *subport21, *subport30, *subport31, *subport_last;
1984 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1985 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1986 uint32_t subport_qmask;
1990 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
1993 * Less then 6 input packets available, which is not enough to
1996 if (unlikely(n_pkts < 6)) {
1997 struct rte_sched_subport *subports[5];
1998 struct rte_mbuf **q_base[5];
2001 /* Prefetch the mbuf structure of each packet */
2002 for (i = 0; i < n_pkts; i++)
2003 rte_prefetch0(pkts[i]);
2005 /* Prefetch the subport structure for each packet */
2006 for (i = 0; i < n_pkts; i++)
2007 subports[i] = rte_sched_port_subport(port, pkts[i]);
2009 /* Prefetch the queue structure for each queue */
2010 for (i = 0; i < n_pkts; i++)
2011 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2012 pkts[i], subport_qmask);
2014 /* Prefetch the write pointer location of each queue */
2015 for (i = 0; i < n_pkts; i++) {
2016 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2017 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2021 /* Write each packet to its queue */
2022 for (i = 0; i < n_pkts; i++)
2023 result += rte_sched_port_enqueue_qwa(port, subports[i],
2024 q[i], q_base[i], pkts[i]);
2029 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2032 rte_prefetch0(pkt20);
2033 rte_prefetch0(pkt21);
2037 rte_prefetch0(pkt10);
2038 rte_prefetch0(pkt11);
2040 subport20 = rte_sched_port_subport(port, pkt20);
2041 subport21 = rte_sched_port_subport(port, pkt21);
2042 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2043 pkt20, subport_qmask);
2044 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2045 pkt21, subport_qmask);
2049 rte_prefetch0(pkt00);
2050 rte_prefetch0(pkt01);
2052 subport10 = rte_sched_port_subport(port, pkt10);
2053 subport11 = rte_sched_port_subport(port, pkt11);
2054 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2055 pkt10, subport_qmask);
2056 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2057 pkt11, subport_qmask);
2059 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2060 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2061 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2062 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2064 /* Run the pipeline */
2065 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2066 /* Propagate stage inputs */
2077 subport30 = subport20;
2078 subport31 = subport21;
2079 subport20 = subport10;
2080 subport21 = subport11;
2081 q30_base = q20_base;
2082 q31_base = q21_base;
2084 /* Stage 0: Get packets in */
2086 pkt01 = pkts[i + 1];
2087 rte_prefetch0(pkt00);
2088 rte_prefetch0(pkt01);
2090 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2091 subport10 = rte_sched_port_subport(port, pkt10);
2092 subport11 = rte_sched_port_subport(port, pkt11);
2093 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2094 pkt10, subport_qmask);
2095 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2096 pkt11, subport_qmask);
2098 /* Stage 2: Prefetch queue write location */
2099 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2100 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2101 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2102 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2104 /* Stage 3: Write packet to queue and activate queue */
2105 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2106 q30, q30_base, pkt30);
2107 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2108 q31, q31_base, pkt31);
2109 result += r30 + r31;
2113 * Drain the pipeline (exactly 6 packets).
2114 * Handle the last packet in the case
2115 * of an odd number of input packets.
2117 pkt_last = pkts[n_pkts - 1];
2118 rte_prefetch0(pkt_last);
2120 subport00 = rte_sched_port_subport(port, pkt00);
2121 subport01 = rte_sched_port_subport(port, pkt01);
2122 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2123 pkt00, subport_qmask);
2124 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2125 pkt01, subport_qmask);
2127 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2128 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2129 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2130 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2132 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2133 q20, q20_base, pkt20);
2134 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2135 q21, q21_base, pkt21);
2136 result += r20 + r21;
2138 subport_last = rte_sched_port_subport(port, pkt_last);
2139 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2140 pkt_last, subport_qmask);
2142 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2143 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2144 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2145 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2147 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2149 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2151 result += r10 + r11;
2153 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2154 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2155 q_last, q_last_base);
2157 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2159 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2161 result += r00 + r01;
2164 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2165 q_last, q_last_base, pkt_last);
2172 #ifndef RTE_SCHED_SUBPORT_TC_OV
2175 grinder_credits_update(struct rte_sched_port *port,
2176 struct rte_sched_subport *subport, uint32_t pos)
2178 struct rte_sched_grinder *grinder = subport->grinder + pos;
2179 struct rte_sched_pipe *pipe = grinder->pipe;
2180 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2181 struct rte_sched_subport_profile *sp = grinder->subport_params;
2186 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2187 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2188 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2189 subport->tb_time += n_periods * sp->tb_period;
2192 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2193 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2194 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2195 pipe->tb_time += n_periods * params->tb_period;
2198 if (unlikely(port->time >= subport->tc_time)) {
2199 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2200 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2202 subport->tc_time = port->time + sp->tc_period;
2206 if (unlikely(port->time >= pipe->tc_time)) {
2207 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2208 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2210 pipe->tc_time = port->time + params->tc_period;
2216 static inline uint64_t
2217 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2218 struct rte_sched_subport *subport, uint32_t pos)
2220 struct rte_sched_grinder *grinder = subport->grinder + pos;
2221 struct rte_sched_subport_profile *sp = grinder->subport_params;
2222 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2223 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2224 uint64_t tc_ov_wm = subport->tc_ov_wm;
2227 if (subport->tc_ov == 0)
2228 return subport->tc_ov_wm_max;
2230 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2231 tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2232 - subport->tc_credits[i];
2233 tc_consumption += tc_ov_consumption[i];
2236 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2237 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2238 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2240 tc_ov_consumption_max =
2241 sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2244 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2245 (tc_ov_consumption_max - port->mtu)) {
2246 tc_ov_wm -= tc_ov_wm >> 7;
2247 if (tc_ov_wm < subport->tc_ov_wm_min)
2248 tc_ov_wm = subport->tc_ov_wm_min;
2253 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2254 if (tc_ov_wm > subport->tc_ov_wm_max)
2255 tc_ov_wm = subport->tc_ov_wm_max;
2261 grinder_credits_update(struct rte_sched_port *port,
2262 struct rte_sched_subport *subport, uint32_t pos)
2264 struct rte_sched_grinder *grinder = subport->grinder + pos;
2265 struct rte_sched_pipe *pipe = grinder->pipe;
2266 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2267 struct rte_sched_subport_profile *sp = grinder->subport_params;
2272 n_periods = (port->time - subport->tb_time) / sp->tb_period;
2273 subport->tb_credits += n_periods * sp->tb_credits_per_period;
2274 subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2275 subport->tb_time += n_periods * sp->tb_period;
2278 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2279 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2280 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2281 pipe->tb_time += n_periods * params->tb_period;
2284 if (unlikely(port->time >= subport->tc_time)) {
2286 grinder_tc_ov_credits_update(port, subport, pos);
2288 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2289 subport->tc_credits[i] = sp->tc_credits_per_period[i];
2291 subport->tc_time = port->time + sp->tc_period;
2292 subport->tc_ov_period_id++;
2296 if (unlikely(port->time >= pipe->tc_time)) {
2297 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2298 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2299 pipe->tc_time = port->time + params->tc_period;
2302 /* Pipe TCs - Oversubscription */
2303 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2304 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2306 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2310 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2313 #ifndef RTE_SCHED_SUBPORT_TC_OV
2316 grinder_credits_check(struct rte_sched_port *port,
2317 struct rte_sched_subport *subport, uint32_t pos)
2319 struct rte_sched_grinder *grinder = subport->grinder + pos;
2320 struct rte_sched_pipe *pipe = grinder->pipe;
2321 struct rte_mbuf *pkt = grinder->pkt;
2322 uint32_t tc_index = grinder->tc_index;
2323 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2324 uint64_t subport_tb_credits = subport->tb_credits;
2325 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2326 uint64_t pipe_tb_credits = pipe->tb_credits;
2327 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2330 /* Check queue credits */
2331 enough_credits = (pkt_len <= subport_tb_credits) &&
2332 (pkt_len <= subport_tc_credits) &&
2333 (pkt_len <= pipe_tb_credits) &&
2334 (pkt_len <= pipe_tc_credits);
2336 if (!enough_credits)
2339 /* Update port credits */
2340 subport->tb_credits -= pkt_len;
2341 subport->tc_credits[tc_index] -= pkt_len;
2342 pipe->tb_credits -= pkt_len;
2343 pipe->tc_credits[tc_index] -= pkt_len;
2351 grinder_credits_check(struct rte_sched_port *port,
2352 struct rte_sched_subport *subport, uint32_t pos)
2354 struct rte_sched_grinder *grinder = subport->grinder + pos;
2355 struct rte_sched_pipe *pipe = grinder->pipe;
2356 struct rte_mbuf *pkt = grinder->pkt;
2357 uint32_t tc_index = grinder->tc_index;
2358 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2359 uint64_t subport_tb_credits = subport->tb_credits;
2360 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2361 uint64_t pipe_tb_credits = pipe->tb_credits;
2362 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2363 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2364 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2365 uint64_t pipe_tc_ov_credits;
2369 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2370 pipe_tc_ov_mask1[i] = ~0LLU;
2372 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2373 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2374 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2376 /* Check pipe and subport credits */
2377 enough_credits = (pkt_len <= subport_tb_credits) &&
2378 (pkt_len <= subport_tc_credits) &&
2379 (pkt_len <= pipe_tb_credits) &&
2380 (pkt_len <= pipe_tc_credits) &&
2381 (pkt_len <= pipe_tc_ov_credits);
2383 if (!enough_credits)
2386 /* Update pipe and subport credits */
2387 subport->tb_credits -= pkt_len;
2388 subport->tc_credits[tc_index] -= pkt_len;
2389 pipe->tb_credits -= pkt_len;
2390 pipe->tc_credits[tc_index] -= pkt_len;
2391 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2396 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2400 grinder_schedule(struct rte_sched_port *port,
2401 struct rte_sched_subport *subport, uint32_t pos)
2403 struct rte_sched_grinder *grinder = subport->grinder + pos;
2404 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2405 struct rte_mbuf *pkt = grinder->pkt;
2406 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2407 uint32_t be_tc_active;
2409 if (!grinder_credits_check(port, subport, pos))
2412 /* Advance port time */
2413 port->time += pkt_len;
2416 port->pkts_out[port->n_pkts_out++] = pkt;
2419 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2420 grinder->wrr_tokens[grinder->qpos] +=
2421 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2423 if (queue->qr == queue->qw) {
2424 uint32_t qindex = grinder->qindex[grinder->qpos];
2426 rte_bitmap_clear(subport->bmp, qindex);
2427 grinder->qmask &= ~(1 << grinder->qpos);
2429 grinder->wrr_mask[grinder->qpos] = 0;
2430 rte_sched_port_set_queue_empty_timestamp(port, subport, qindex);
2433 /* Reset pipe loop detection */
2434 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2435 grinder->productive = 1;
2440 #ifdef SCHED_VECTOR_SSE4
2443 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2445 __m128i index = _mm_set1_epi32(base_pipe);
2446 __m128i pipes = _mm_load_si128((__m128i *)subport->grinder_base_bmp_pos);
2447 __m128i res = _mm_cmpeq_epi32(pipes, index);
2449 pipes = _mm_load_si128((__m128i *)(subport->grinder_base_bmp_pos + 4));
2450 pipes = _mm_cmpeq_epi32(pipes, index);
2451 res = _mm_or_si128(res, pipes);
2453 if (_mm_testz_si128(res, res))
2459 #elif defined(SCHED_VECTOR_NEON)
2462 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2464 uint32x4_t index, pipes;
2465 uint32_t *pos = (uint32_t *)subport->grinder_base_bmp_pos;
2467 index = vmovq_n_u32(base_pipe);
2468 pipes = vld1q_u32(pos);
2469 if (!vminvq_u32(veorq_u32(pipes, index)))
2472 pipes = vld1q_u32(pos + 4);
2473 if (!vminvq_u32(veorq_u32(pipes, index)))
2482 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2486 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2487 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2494 #endif /* RTE_SCHED_OPTIMIZATIONS */
2497 grinder_pcache_populate(struct rte_sched_subport *subport,
2498 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2500 struct rte_sched_grinder *grinder = subport->grinder + pos;
2503 grinder->pcache_w = 0;
2504 grinder->pcache_r = 0;
2506 w[0] = (uint16_t) bmp_slab;
2507 w[1] = (uint16_t) (bmp_slab >> 16);
2508 w[2] = (uint16_t) (bmp_slab >> 32);
2509 w[3] = (uint16_t) (bmp_slab >> 48);
2511 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2512 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2513 grinder->pcache_w += (w[0] != 0);
2515 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2516 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2517 grinder->pcache_w += (w[1] != 0);
2519 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2520 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2521 grinder->pcache_w += (w[2] != 0);
2523 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2524 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2525 grinder->pcache_w += (w[3] != 0);
2529 grinder_tccache_populate(struct rte_sched_subport *subport,
2530 uint32_t pos, uint32_t qindex, uint16_t qmask)
2532 struct rte_sched_grinder *grinder = subport->grinder + pos;
2535 grinder->tccache_w = 0;
2536 grinder->tccache_r = 0;
2538 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2539 b = (uint8_t) ((qmask >> i) & 0x1);
2540 grinder->tccache_qmask[grinder->tccache_w] = b;
2541 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2542 grinder->tccache_w += (b != 0);
2545 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2546 grinder->tccache_qmask[grinder->tccache_w] = b;
2547 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2548 RTE_SCHED_TRAFFIC_CLASS_BE;
2549 grinder->tccache_w += (b != 0);
2553 grinder_next_tc(struct rte_sched_port *port,
2554 struct rte_sched_subport *subport, uint32_t pos)
2556 struct rte_sched_grinder *grinder = subport->grinder + pos;
2557 struct rte_mbuf **qbase;
2561 if (grinder->tccache_r == grinder->tccache_w)
2564 qindex = grinder->tccache_qindex[grinder->tccache_r];
2565 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2566 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2568 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2569 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2570 grinder->qsize = qsize;
2572 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2573 grinder->queue[0] = subport->queue + qindex;
2574 grinder->qbase[0] = qbase;
2575 grinder->qindex[0] = qindex;
2576 grinder->tccache_r++;
2581 grinder->queue[0] = subport->queue + qindex;
2582 grinder->queue[1] = subport->queue + qindex + 1;
2583 grinder->queue[2] = subport->queue + qindex + 2;
2584 grinder->queue[3] = subport->queue + qindex + 3;
2586 grinder->qbase[0] = qbase;
2587 grinder->qbase[1] = qbase + qsize;
2588 grinder->qbase[2] = qbase + 2 * qsize;
2589 grinder->qbase[3] = qbase + 3 * qsize;
2591 grinder->qindex[0] = qindex;
2592 grinder->qindex[1] = qindex + 1;
2593 grinder->qindex[2] = qindex + 2;
2594 grinder->qindex[3] = qindex + 3;
2596 grinder->tccache_r++;
2601 grinder_next_pipe(struct rte_sched_port *port,
2602 struct rte_sched_subport *subport, uint32_t pos)
2604 struct rte_sched_grinder *grinder = subport->grinder + pos;
2605 uint32_t pipe_qindex;
2606 uint16_t pipe_qmask;
2608 if (grinder->pcache_r < grinder->pcache_w) {
2609 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2610 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2611 grinder->pcache_r++;
2613 uint64_t bmp_slab = 0;
2614 uint32_t bmp_pos = 0;
2616 /* Get another non-empty pipe group */
2617 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2620 #ifdef RTE_SCHED_DEBUG
2621 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2624 /* Return if pipe group already in one of the other grinders */
2625 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2626 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2629 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2631 /* Install new pipe group into grinder's pipe cache */
2632 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2634 pipe_qmask = grinder->pcache_qmask[0];
2635 pipe_qindex = grinder->pcache_qindex[0];
2636 grinder->pcache_r = 1;
2639 /* Install new pipe in the grinder */
2640 grinder->pindex = pipe_qindex >> 4;
2641 grinder->subport = subport;
2642 grinder->pipe = subport->pipe + grinder->pindex;
2643 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2644 grinder->productive = 0;
2646 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2647 grinder_next_tc(port, subport, pos);
2649 /* Check for pipe exhaustion */
2650 if (grinder->pindex == subport->pipe_loop) {
2651 subport->pipe_exhaustion = 1;
2652 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2660 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2662 struct rte_sched_grinder *grinder = subport->grinder + pos;
2663 struct rte_sched_pipe *pipe = grinder->pipe;
2664 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2665 uint32_t qmask = grinder->qmask;
2667 grinder->wrr_tokens[0] =
2668 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2669 grinder->wrr_tokens[1] =
2670 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2671 grinder->wrr_tokens[2] =
2672 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2673 grinder->wrr_tokens[3] =
2674 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2676 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2677 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2678 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2679 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2681 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2682 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2683 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2684 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2688 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2690 struct rte_sched_grinder *grinder = subport->grinder + pos;
2691 struct rte_sched_pipe *pipe = grinder->pipe;
2693 pipe->wrr_tokens[0] =
2694 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2695 RTE_SCHED_WRR_SHIFT;
2696 pipe->wrr_tokens[1] =
2697 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2698 RTE_SCHED_WRR_SHIFT;
2699 pipe->wrr_tokens[2] =
2700 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2701 RTE_SCHED_WRR_SHIFT;
2702 pipe->wrr_tokens[3] =
2703 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2704 RTE_SCHED_WRR_SHIFT;
2708 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2710 struct rte_sched_grinder *grinder = subport->grinder + pos;
2711 uint16_t wrr_tokens_min;
2713 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2714 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2715 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2716 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2718 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2719 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2721 grinder->wrr_tokens[0] -= wrr_tokens_min;
2722 grinder->wrr_tokens[1] -= wrr_tokens_min;
2723 grinder->wrr_tokens[2] -= wrr_tokens_min;
2724 grinder->wrr_tokens[3] -= wrr_tokens_min;
2728 #define grinder_evict(subport, pos)
2731 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2733 struct rte_sched_grinder *grinder = subport->grinder + pos;
2735 rte_prefetch0(grinder->pipe);
2736 rte_prefetch0(grinder->queue[0]);
2740 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2742 struct rte_sched_grinder *grinder = subport->grinder + pos;
2743 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2745 qsize = grinder->qsize;
2748 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2749 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2751 rte_prefetch0(grinder->qbase[0] + qr[0]);
2755 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2756 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2757 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2758 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2760 rte_prefetch0(grinder->qbase[0] + qr[0]);
2761 rte_prefetch0(grinder->qbase[1] + qr[1]);
2763 grinder_wrr_load(subport, pos);
2764 grinder_wrr(subport, pos);
2766 rte_prefetch0(grinder->qbase[2] + qr[2]);
2767 rte_prefetch0(grinder->qbase[3] + qr[3]);
2771 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2773 struct rte_sched_grinder *grinder = subport->grinder + pos;
2774 uint32_t qpos = grinder->qpos;
2775 struct rte_mbuf **qbase = grinder->qbase[qpos];
2776 uint16_t qsize = grinder->qsize;
2777 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2779 grinder->pkt = qbase[qr];
2780 rte_prefetch0(grinder->pkt);
2782 if (unlikely((qr & 0x7) == 7)) {
2783 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2785 rte_prefetch0(qbase + qr_next);
2789 static inline uint32_t
2790 grinder_handle(struct rte_sched_port *port,
2791 struct rte_sched_subport *subport, uint32_t pos)
2793 struct rte_sched_grinder *grinder = subport->grinder + pos;
2795 switch (grinder->state) {
2796 case e_GRINDER_PREFETCH_PIPE:
2798 if (grinder_next_pipe(port, subport, pos)) {
2799 grinder_prefetch_pipe(subport, pos);
2800 subport->busy_grinders++;
2802 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2809 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2811 struct rte_sched_pipe *pipe = grinder->pipe;
2813 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2814 grinder->subport_params = port->subport_profiles +
2817 grinder_prefetch_tc_queue_arrays(subport, pos);
2818 grinder_credits_update(port, subport, pos);
2820 grinder->state = e_GRINDER_PREFETCH_MBUF;
2824 case e_GRINDER_PREFETCH_MBUF:
2826 grinder_prefetch_mbuf(subport, pos);
2828 grinder->state = e_GRINDER_READ_MBUF;
2832 case e_GRINDER_READ_MBUF:
2834 uint32_t wrr_active, result = 0;
2836 result = grinder_schedule(port, subport, pos);
2838 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2840 /* Look for next packet within the same TC */
2841 if (result && grinder->qmask) {
2843 grinder_wrr(subport, pos);
2845 grinder_prefetch_mbuf(subport, pos);
2851 grinder_wrr_store(subport, pos);
2853 /* Look for another active TC within same pipe */
2854 if (grinder_next_tc(port, subport, pos)) {
2855 grinder_prefetch_tc_queue_arrays(subport, pos);
2857 grinder->state = e_GRINDER_PREFETCH_MBUF;
2861 if (grinder->productive == 0 &&
2862 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2863 subport->pipe_loop = grinder->pindex;
2865 grinder_evict(subport, pos);
2867 /* Look for another active pipe */
2868 if (grinder_next_pipe(port, subport, pos)) {
2869 grinder_prefetch_pipe(subport, pos);
2871 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2875 /* No active pipe found */
2876 subport->busy_grinders--;
2878 grinder->state = e_GRINDER_PREFETCH_PIPE;
2883 rte_panic("Algorithmic error (invalid state)\n");
2889 rte_sched_port_time_resync(struct rte_sched_port *port)
2891 uint64_t cycles = rte_get_tsc_cycles();
2892 uint64_t cycles_diff;
2893 uint64_t bytes_diff;
2896 if (cycles < port->time_cpu_cycles)
2897 port->time_cpu_cycles = 0;
2899 cycles_diff = cycles - port->time_cpu_cycles;
2900 /* Compute elapsed time in bytes */
2901 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2902 port->inv_cycles_per_byte);
2904 /* Advance port time */
2905 port->time_cpu_cycles +=
2906 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2907 port->time_cpu_bytes += bytes_diff;
2908 if (port->time < port->time_cpu_bytes)
2909 port->time = port->time_cpu_bytes;
2911 /* Reset pipe loop detection */
2912 for (i = 0; i < port->n_subports_per_port; i++)
2913 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2917 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2921 /* Check if any exception flag is set */
2922 exceptions = (second_pass && subport->busy_grinders == 0) ||
2923 (subport->pipe_exhaustion == 1);
2925 /* Clear exception flags */
2926 subport->pipe_exhaustion = 0;
2932 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2934 struct rte_sched_subport *subport;
2935 uint32_t subport_id = port->subport_id;
2936 uint32_t i, n_subports = 0, count;
2938 port->pkts_out = pkts;
2939 port->n_pkts_out = 0;
2941 rte_sched_port_time_resync(port);
2943 /* Take each queue in the grinder one step further */
2944 for (i = 0, count = 0; ; i++) {
2945 subport = port->subports[subport_id];
2947 count += grinder_handle(port, subport,
2948 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2950 if (count == n_pkts) {
2953 if (subport_id == port->n_subports_per_port)
2956 port->subport_id = subport_id;
2960 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2966 if (subport_id == port->n_subports_per_port)
2969 if (n_subports == port->n_subports_per_port) {
2970 port->subport_id = subport_id;