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_pipe *pipe;
127 struct rte_sched_pipe_profile *pipe_params;
130 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
131 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
137 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
138 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
139 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
143 struct rte_mbuf *pkt;
146 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
147 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
148 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
151 struct rte_sched_subport {
152 /* Token bucket (TB) */
153 uint64_t tb_time; /* time of last update */
155 uint64_t tb_credits_per_period;
159 /* Traffic classes (TCs) */
160 uint64_t tc_time; /* time of next update */
161 uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
162 uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
165 /* TC oversubscription */
167 uint64_t tc_ov_wm_min;
168 uint64_t tc_ov_wm_max;
169 uint8_t tc_ov_period_id;
175 struct rte_sched_subport_stats stats __rte_cache_aligned;
178 uint32_t n_pipes_per_subport_enabled;
179 uint32_t n_pipe_profiles;
180 uint32_t n_max_pipe_profiles;
182 /* Pipe best-effort TC rate */
183 uint64_t pipe_tc_be_rate_max;
185 /* Pipe queues size */
186 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
189 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
192 /* Scheduling loop detection */
194 uint32_t pipe_exhaustion;
197 struct rte_bitmap *bmp;
198 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
201 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
202 uint32_t busy_grinders;
204 /* Queue base calculation */
205 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
208 struct rte_sched_pipe *pipe;
209 struct rte_sched_queue *queue;
210 struct rte_sched_queue_extra *queue_extra;
211 struct rte_sched_pipe_profile *pipe_profiles;
213 struct rte_mbuf **queue_array;
214 uint8_t memory[0] __rte_cache_aligned;
215 } __rte_cache_aligned;
217 struct rte_sched_port {
218 /* User parameters */
219 uint32_t n_subports_per_port;
220 uint32_t n_pipes_per_subport;
221 uint32_t n_pipes_per_subport_log2;
222 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
223 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
224 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
225 uint32_t n_subport_profiles;
226 uint32_t n_max_subport_profiles;
229 uint32_t frame_overhead;
233 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
234 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
235 uint64_t time; /* Current NIC TX time measured in bytes */
236 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
237 uint64_t cycles_per_byte;
240 struct rte_mbuf **pkts_out;
244 /* Large data structures */
245 struct rte_sched_subport_profile *subport_profiles;
246 struct rte_sched_subport *subports[0] __rte_cache_aligned;
247 } __rte_cache_aligned;
249 enum rte_sched_subport_array {
250 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
251 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
252 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
253 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
254 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
255 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
256 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
259 static inline uint32_t
260 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
262 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
265 static inline struct rte_mbuf **
266 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
268 uint32_t pindex = qindex >> 4;
269 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
271 return (subport->queue_array + pindex *
272 subport->qsize_sum + subport->qsize_add[qpos]);
275 static inline uint16_t
276 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
277 struct rte_sched_subport *subport, uint32_t qindex)
279 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
281 return subport->qsize[tc];
284 static inline uint32_t
285 rte_sched_port_queues_per_port(struct rte_sched_port *port)
287 uint32_t n_queues = 0, i;
289 for (i = 0; i < port->n_subports_per_port; i++)
290 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
295 static inline uint16_t
296 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
298 uint16_t pipe_queue = port->pipe_queue[traffic_class];
303 static inline uint8_t
304 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
306 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
311 static inline uint8_t
312 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
314 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
320 pipe_profile_check(struct rte_sched_pipe_params *params,
321 uint64_t rate, uint16_t *qsize)
325 /* Pipe parameters */
326 if (params == NULL) {
328 "%s: Incorrect value for parameter params\n", __func__);
332 /* TB rate: non-zero, not greater than port rate */
333 if (params->tb_rate == 0 ||
334 params->tb_rate > rate) {
336 "%s: Incorrect value for tb rate\n", __func__);
340 /* TB size: non-zero */
341 if (params->tb_size == 0) {
343 "%s: Incorrect value for tb size\n", __func__);
347 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
348 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
349 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
350 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
351 params->tc_rate[i] > params->tb_rate))) {
353 "%s: Incorrect value for qsize or tc_rate\n", __func__);
358 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
359 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
361 "%s: Incorrect value for be traffic class rate\n", __func__);
365 /* TC period: non-zero */
366 if (params->tc_period == 0) {
368 "%s: Incorrect value for tc period\n", __func__);
372 /* Best effort tc oversubscription weight: non-zero */
373 if (params->tc_ov_weight == 0) {
375 "%s: Incorrect value for tc ov weight\n", __func__);
379 /* Queue WRR weights: non-zero */
380 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
381 if (params->wrr_weights[i] == 0) {
383 "%s: Incorrect value for wrr weight\n", __func__);
392 subport_profile_check(struct rte_sched_subport_profile_params *params,
397 /* Check user parameters */
398 if (params == NULL) {
399 RTE_LOG(ERR, SCHED, "%s: "
400 "Incorrect value for parameter params\n", __func__);
404 if (params->tb_rate == 0 || params->tb_rate > rate) {
405 RTE_LOG(ERR, SCHED, "%s: "
406 "Incorrect value for tb rate\n", __func__);
410 if (params->tb_size == 0) {
411 RTE_LOG(ERR, SCHED, "%s: "
412 "Incorrect value for tb size\n", __func__);
416 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
417 uint64_t tc_rate = params->tc_rate[i];
419 if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
420 RTE_LOG(ERR, SCHED, "%s: "
421 "Incorrect value for tc rate\n", __func__);
426 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
427 RTE_LOG(ERR, SCHED, "%s: "
428 "Incorrect tc rate(best effort)\n", __func__);
432 if (params->tc_period == 0) {
433 RTE_LOG(ERR, SCHED, "%s: "
434 "Incorrect value for tc period\n", __func__);
442 rte_sched_port_check_params(struct rte_sched_port_params *params)
446 if (params == NULL) {
448 "%s: Incorrect value for parameter params\n", __func__);
453 if (params->socket < 0) {
455 "%s: Incorrect value for socket id\n", __func__);
460 if (params->rate == 0) {
462 "%s: Incorrect value for rate\n", __func__);
467 if (params->mtu == 0) {
469 "%s: Incorrect value for mtu\n", __func__);
473 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
474 if (params->n_subports_per_port == 0 ||
475 params->n_subports_per_port > 1u << 16 ||
476 !rte_is_power_of_2(params->n_subports_per_port)) {
478 "%s: Incorrect value for number of subports\n", __func__);
482 if (params->subport_profiles == NULL ||
483 params->n_subport_profiles == 0 ||
484 params->n_max_subport_profiles == 0 ||
485 params->n_subport_profiles > params->n_max_subport_profiles) {
487 "%s: Incorrect value for subport profiles\n", __func__);
491 for (i = 0; i < params->n_subport_profiles; i++) {
492 struct rte_sched_subport_profile_params *p =
493 params->subport_profiles + i;
496 status = subport_profile_check(p, params->rate);
499 "%s: subport profile check failed(%d)\n",
505 /* n_pipes_per_subport: non-zero, power of 2 */
506 if (params->n_pipes_per_subport == 0 ||
507 !rte_is_power_of_2(params->n_pipes_per_subport)) {
509 "%s: Incorrect value for maximum pipes number\n", __func__);
517 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
518 enum rte_sched_subport_array array)
520 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
521 uint32_t n_subport_pipe_queues =
522 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
524 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
525 uint32_t size_queue =
526 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
527 uint32_t size_queue_extra
528 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
529 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
530 sizeof(struct rte_sched_pipe_profile);
531 uint32_t size_bmp_array =
532 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
533 uint32_t size_per_pipe_queue_array, size_queue_array;
537 size_per_pipe_queue_array = 0;
538 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
539 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
540 size_per_pipe_queue_array +=
541 params->qsize[i] * sizeof(struct rte_mbuf *);
543 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
544 params->qsize[i] * sizeof(struct rte_mbuf *);
546 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
550 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
552 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
554 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
556 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
558 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
560 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
562 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
564 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
566 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
568 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
570 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
572 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
578 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
582 subport->qsize_add[0] = 0;
584 /* Strict prority traffic class */
585 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
586 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
588 /* Best-effort traffic class */
589 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
590 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
591 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
592 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
593 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
594 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
595 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
596 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
597 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
599 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
600 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
604 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
606 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
608 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
609 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
610 " Traffic classes: period = %"PRIu64",\n"
611 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
612 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
613 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
614 " Best-effort traffic class oversubscription: weight = %hhu\n"
615 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
620 p->tb_credits_per_period,
623 /* Traffic classes */
625 p->tc_credits_per_period[0],
626 p->tc_credits_per_period[1],
627 p->tc_credits_per_period[2],
628 p->tc_credits_per_period[3],
629 p->tc_credits_per_period[4],
630 p->tc_credits_per_period[5],
631 p->tc_credits_per_period[6],
632 p->tc_credits_per_period[7],
633 p->tc_credits_per_period[8],
634 p->tc_credits_per_period[9],
635 p->tc_credits_per_period[10],
636 p->tc_credits_per_period[11],
637 p->tc_credits_per_period[12],
639 /* Best-effort traffic class oversubscription */
643 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
647 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
649 struct rte_sched_subport_profile *p = port->subport_profiles + i;
651 RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
652 "Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
654 "Traffic classes: period = %"PRIu64",\n"
655 "credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
656 " %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
657 " %"PRIu64", %"PRIu64", %"PRIu64"]\n",
662 p->tb_credits_per_period,
665 /* Traffic classes */
667 p->tc_credits_per_period[0],
668 p->tc_credits_per_period[1],
669 p->tc_credits_per_period[2],
670 p->tc_credits_per_period[3],
671 p->tc_credits_per_period[4],
672 p->tc_credits_per_period[5],
673 p->tc_credits_per_period[6],
674 p->tc_credits_per_period[7],
675 p->tc_credits_per_period[8],
676 p->tc_credits_per_period[9],
677 p->tc_credits_per_period[10],
678 p->tc_credits_per_period[11],
679 p->tc_credits_per_period[12]);
682 static inline uint64_t
683 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
685 uint64_t time = time_ms;
687 time = (time * rate) / 1000;
693 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
694 struct rte_sched_pipe_params *src,
695 struct rte_sched_pipe_profile *dst,
698 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
699 uint32_t lcd1, lcd2, lcd;
703 if (src->tb_rate == rate) {
704 dst->tb_credits_per_period = 1;
707 double tb_rate = (double) src->tb_rate
709 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
711 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
715 dst->tb_size = src->tb_size;
717 /* Traffic Classes */
718 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
721 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
722 if (subport->qsize[i])
723 dst->tc_credits_per_period[i]
724 = rte_sched_time_ms_to_bytes(src->tc_period,
727 dst->tc_ov_weight = src->tc_ov_weight;
730 wrr_cost[0] = src->wrr_weights[0];
731 wrr_cost[1] = src->wrr_weights[1];
732 wrr_cost[2] = src->wrr_weights[2];
733 wrr_cost[3] = src->wrr_weights[3];
735 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
736 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
737 lcd = rte_get_lcd(lcd1, lcd2);
739 wrr_cost[0] = lcd / wrr_cost[0];
740 wrr_cost[1] = lcd / wrr_cost[1];
741 wrr_cost[2] = lcd / wrr_cost[2];
742 wrr_cost[3] = lcd / wrr_cost[3];
744 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
745 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
746 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
747 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
751 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
752 struct rte_sched_subport_profile *dst,
758 if (src->tb_rate == rate) {
759 dst->tb_credits_per_period = 1;
762 double tb_rate = (double) src->tb_rate
764 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
766 rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
770 dst->tb_size = src->tb_size;
772 /* Traffic Classes */
773 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
775 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
776 dst->tc_credits_per_period[i]
777 = rte_sched_time_ms_to_bytes(src->tc_period,
782 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
783 struct rte_sched_subport_params *params, uint64_t rate)
787 for (i = 0; i < subport->n_pipe_profiles; i++) {
788 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
789 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
791 rte_sched_pipe_profile_convert(subport, src, dst, rate);
792 rte_sched_port_log_pipe_profile(subport, i);
795 subport->pipe_tc_be_rate_max = 0;
796 for (i = 0; i < subport->n_pipe_profiles; i++) {
797 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
798 uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
800 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
801 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
806 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
807 struct rte_sched_port_params *params,
812 for (i = 0; i < port->n_subport_profiles; i++) {
813 struct rte_sched_subport_profile_params *src
814 = params->subport_profiles + i;
815 struct rte_sched_subport_profile *dst
816 = port->subport_profiles + i;
818 rte_sched_subport_profile_convert(src, dst, rate);
819 rte_sched_port_log_subport_profile(port, i);
824 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
825 uint32_t n_max_pipes_per_subport,
830 /* Check user parameters */
831 if (params == NULL) {
833 "%s: Incorrect value for parameter params\n", __func__);
837 if (params->tb_rate == 0 || params->tb_rate > rate) {
839 "%s: Incorrect value for tb rate\n", __func__);
843 if (params->tb_size == 0) {
845 "%s: Incorrect value for tb size\n", __func__);
849 /* qsize: if non-zero, power of 2,
850 * no bigger than 32K (due to 16-bit read/write pointers)
852 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
853 uint16_t qsize = params->qsize[i];
855 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
857 "%s: Incorrect value for qsize\n", __func__);
862 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
863 uint64_t tc_rate = params->tc_rate[i];
864 uint16_t qsize = params->qsize[i];
866 if ((qsize == 0 && tc_rate != 0) ||
867 (qsize != 0 && tc_rate == 0) ||
868 (tc_rate > params->tb_rate)) {
870 "%s: Incorrect value for tc rate\n", __func__);
875 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
876 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
878 "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
882 if (params->tc_period == 0) {
884 "%s: Incorrect value for tc period\n", __func__);
888 /* n_pipes_per_subport: non-zero, power of 2 */
889 if (params->n_pipes_per_subport_enabled == 0 ||
890 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
891 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
893 "%s: Incorrect value for pipes number\n", __func__);
897 /* pipe_profiles and n_pipe_profiles */
898 if (params->pipe_profiles == NULL ||
899 params->n_pipe_profiles == 0 ||
900 params->n_max_pipe_profiles == 0 ||
901 params->n_pipe_profiles > params->n_max_pipe_profiles) {
903 "%s: Incorrect value for pipe profiles\n", __func__);
907 for (i = 0; i < params->n_pipe_profiles; i++) {
908 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
911 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
914 "%s: Pipe profile check failed(%d)\n", __func__, status);
923 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
924 struct rte_sched_subport_params **subport_params)
926 uint32_t size0 = 0, size1 = 0, i;
929 status = rte_sched_port_check_params(port_params);
932 "%s: Port scheduler port params check failed (%d)\n",
938 for (i = 0; i < port_params->n_subports_per_port; i++) {
939 struct rte_sched_subport_params *sp = subport_params[i];
941 status = rte_sched_subport_check_params(sp,
942 port_params->n_pipes_per_subport,
946 "%s: Port scheduler subport params check failed (%d)\n",
953 size0 = sizeof(struct rte_sched_port);
955 for (i = 0; i < port_params->n_subports_per_port; i++) {
956 struct rte_sched_subport_params *sp = subport_params[i];
958 size1 += rte_sched_subport_get_array_base(sp,
959 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
962 return size0 + size1;
965 struct rte_sched_port *
966 rte_sched_port_config(struct rte_sched_port_params *params)
968 struct rte_sched_port *port = NULL;
969 uint32_t size0, size1, size2;
970 uint32_t cycles_per_byte;
974 status = rte_sched_port_check_params(params);
977 "%s: Port scheduler params check failed (%d)\n",
982 size0 = sizeof(struct rte_sched_port);
983 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
984 size2 = params->n_max_subport_profiles *
985 sizeof(struct rte_sched_subport_profile);
987 /* Allocate memory to store the data structures */
988 port = rte_zmalloc_socket("qos_params", size0 + size1,
989 RTE_CACHE_LINE_SIZE, params->socket);
991 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
996 /* Allocate memory to store the subport profile */
997 port->subport_profiles = rte_zmalloc_socket("subport_profile", size2,
998 RTE_CACHE_LINE_SIZE, params->socket);
1000 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
1005 /* User parameters */
1006 port->n_subports_per_port = params->n_subports_per_port;
1007 port->n_subport_profiles = params->n_subport_profiles;
1008 port->n_max_subport_profiles = params->n_max_subport_profiles;
1009 port->n_pipes_per_subport = params->n_pipes_per_subport;
1010 port->n_pipes_per_subport_log2 =
1011 __builtin_ctz(params->n_pipes_per_subport);
1012 port->socket = params->socket;
1014 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1015 port->pipe_queue[i] = i;
1017 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
1018 port->pipe_tc[i] = j;
1020 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
1024 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
1025 port->tc_queue[i] = j;
1027 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
1030 port->rate = params->rate;
1031 port->mtu = params->mtu + params->frame_overhead;
1032 port->frame_overhead = params->frame_overhead;
1035 port->time_cpu_cycles = rte_get_tsc_cycles();
1036 port->time_cpu_bytes = 0;
1039 /* Subport profile table */
1040 rte_sched_port_config_subport_profile_table(port, params, port->rate);
1042 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1044 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1045 port->cycles_per_byte = cycles_per_byte;
1048 port->pkts_out = NULL;
1049 port->n_pkts_out = 0;
1050 port->subport_id = 0;
1056 rte_sched_subport_free(struct rte_sched_port *port,
1057 struct rte_sched_subport *subport)
1059 uint32_t n_subport_pipe_queues;
1062 if (subport == NULL)
1065 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1067 /* Free enqueued mbufs */
1068 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1069 struct rte_mbuf **mbufs =
1070 rte_sched_subport_pipe_qbase(subport, qindex);
1071 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1073 struct rte_sched_queue *queue = subport->queue + qindex;
1074 uint16_t qr = queue->qr & (qsize - 1);
1075 uint16_t qw = queue->qw & (qsize - 1);
1077 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1078 rte_pktmbuf_free(mbufs[qr]);
1086 rte_sched_port_free(struct rte_sched_port *port)
1090 /* Check user parameters */
1094 for (i = 0; i < port->n_subports_per_port; i++)
1095 rte_sched_subport_free(port, port->subports[i]);
1097 rte_free(port->subport_profiles);
1102 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
1104 struct rte_sched_subport *s = port->subports[i];
1106 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
1107 " Token bucket: period = %"PRIu64", credits per period = %"PRIu64
1108 ", size = %"PRIu64"\n"
1109 " Traffic classes: period = %"PRIu64"\n"
1110 " credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
1111 ", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
1112 ", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
1113 " Best effort traffic class oversubscription: wm min = %"PRIu64
1114 ", wm max = %"PRIu64"\n",
1119 s->tb_credits_per_period,
1122 /* Traffic classes */
1124 s->tc_credits_per_period[0],
1125 s->tc_credits_per_period[1],
1126 s->tc_credits_per_period[2],
1127 s->tc_credits_per_period[3],
1128 s->tc_credits_per_period[4],
1129 s->tc_credits_per_period[5],
1130 s->tc_credits_per_period[6],
1131 s->tc_credits_per_period[7],
1132 s->tc_credits_per_period[8],
1133 s->tc_credits_per_period[9],
1134 s->tc_credits_per_period[10],
1135 s->tc_credits_per_period[11],
1136 s->tc_credits_per_period[12],
1138 /* Best effort traffic class oversubscription */
1144 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1148 for (i = 0; i < n_subports; i++) {
1149 struct rte_sched_subport *subport = port->subports[i];
1151 rte_sched_subport_free(port, subport);
1154 rte_free(port->subport_profiles);
1159 rte_sched_subport_config(struct rte_sched_port *port,
1160 uint32_t subport_id,
1161 struct rte_sched_subport_params *params)
1163 struct rte_sched_subport *s = NULL;
1164 uint32_t n_subports = subport_id;
1165 uint32_t n_subport_pipe_queues, i;
1166 uint32_t size0, size1, bmp_mem_size;
1169 /* Check user parameters */
1172 "%s: Incorrect value for parameter port\n", __func__);
1176 if (subport_id >= port->n_subports_per_port) {
1178 "%s: Incorrect value for subport id\n", __func__);
1180 rte_sched_free_memory(port, n_subports);
1184 status = rte_sched_subport_check_params(params,
1185 port->n_pipes_per_subport,
1188 RTE_LOG(NOTICE, SCHED,
1189 "%s: Port scheduler params check failed (%d)\n",
1192 rte_sched_free_memory(port, n_subports);
1196 /* Determine the amount of memory to allocate */
1197 size0 = sizeof(struct rte_sched_subport);
1198 size1 = rte_sched_subport_get_array_base(params,
1199 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1201 /* Allocate memory to store the data structures */
1202 s = rte_zmalloc_socket("subport_params", size0 + size1,
1203 RTE_CACHE_LINE_SIZE, port->socket);
1206 "%s: Memory allocation fails\n", __func__);
1208 rte_sched_free_memory(port, n_subports);
1215 port->subports[subport_id] = s;
1217 /* Token Bucket (TB) */
1218 if (params->tb_rate == port->rate) {
1219 s->tb_credits_per_period = 1;
1222 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1223 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1225 rte_approx_64(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1228 s->tb_size = params->tb_size;
1229 s->tb_time = port->time;
1230 s->tb_credits = s->tb_size / 2;
1232 /* Traffic Classes (TCs) */
1233 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1234 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1235 if (params->qsize[i])
1236 s->tc_credits_per_period[i]
1237 = rte_sched_time_ms_to_bytes(params->tc_period,
1238 params->tc_rate[i]);
1240 s->tc_time = port->time + s->tc_period;
1241 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1242 if (params->qsize[i])
1243 s->tc_credits[i] = s->tc_credits_per_period[i];
1245 /* compile time checks */
1246 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1247 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1248 (RTE_SCHED_PORT_N_GRINDERS - 1));
1250 /* User parameters */
1251 s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1252 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1253 s->n_pipe_profiles = params->n_pipe_profiles;
1254 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1256 #ifdef RTE_SCHED_RED
1257 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1260 for (j = 0; j < RTE_COLORS; j++) {
1261 /* if min/max are both zero, then RED is disabled */
1262 if ((params->red_params[i][j].min_th |
1263 params->red_params[i][j].max_th) == 0) {
1267 if (rte_red_config_init(&s->red_config[i][j],
1268 params->red_params[i][j].wq_log2,
1269 params->red_params[i][j].min_th,
1270 params->red_params[i][j].max_th,
1271 params->red_params[i][j].maxp_inv) != 0) {
1272 rte_sched_free_memory(port, n_subports);
1274 RTE_LOG(NOTICE, SCHED,
1275 "%s: RED configuration init fails\n", __func__);
1282 /* Scheduling loop detection */
1283 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1284 s->pipe_exhaustion = 0;
1287 s->busy_grinders = 0;
1289 /* Queue base calculation */
1290 rte_sched_subport_config_qsize(s);
1292 /* Large data structures */
1293 s->pipe = (struct rte_sched_pipe *)
1294 (s->memory + rte_sched_subport_get_array_base(params,
1295 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1296 s->queue = (struct rte_sched_queue *)
1297 (s->memory + rte_sched_subport_get_array_base(params,
1298 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1299 s->queue_extra = (struct rte_sched_queue_extra *)
1300 (s->memory + rte_sched_subport_get_array_base(params,
1301 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1302 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1303 (s->memory + rte_sched_subport_get_array_base(params,
1304 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1305 s->bmp_array = s->memory + rte_sched_subport_get_array_base(params,
1306 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1307 s->queue_array = (struct rte_mbuf **)
1308 (s->memory + rte_sched_subport_get_array_base(params,
1309 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1311 /* Pipe profile table */
1312 rte_sched_subport_config_pipe_profile_table(s, params, port->rate);
1315 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1316 bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1317 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1319 if (s->bmp == NULL) {
1321 "%s: Subport bitmap init error\n", __func__);
1323 rte_sched_free_memory(port, n_subports);
1327 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1328 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1330 #ifdef RTE_SCHED_SUBPORT_TC_OV
1331 /* TC oversubscription */
1332 s->tc_ov_wm_min = port->mtu;
1333 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1334 s->pipe_tc_be_rate_max);
1335 s->tc_ov_wm = s->tc_ov_wm_max;
1336 s->tc_ov_period_id = 0;
1342 rte_sched_port_log_subport_config(port, subport_id);
1348 rte_sched_pipe_config(struct rte_sched_port *port,
1349 uint32_t subport_id,
1351 int32_t pipe_profile)
1353 struct rte_sched_subport *s;
1354 struct rte_sched_pipe *p;
1355 struct rte_sched_pipe_profile *params;
1356 uint32_t n_subports = subport_id + 1;
1357 uint32_t deactivate, profile, i;
1359 /* Check user parameters */
1360 profile = (uint32_t) pipe_profile;
1361 deactivate = (pipe_profile < 0);
1365 "%s: Incorrect value for parameter port\n", __func__);
1369 if (subport_id >= port->n_subports_per_port) {
1371 "%s: Incorrect value for parameter subport id\n", __func__);
1373 rte_sched_free_memory(port, n_subports);
1377 s = port->subports[subport_id];
1378 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1380 "%s: Incorrect value for parameter pipe id\n", __func__);
1382 rte_sched_free_memory(port, n_subports);
1386 if (!deactivate && profile >= s->n_pipe_profiles) {
1388 "%s: Incorrect value for parameter pipe profile\n", __func__);
1390 rte_sched_free_memory(port, n_subports);
1394 /* Handle the case when pipe already has a valid configuration */
1395 p = s->pipe + pipe_id;
1397 params = s->pipe_profiles + p->profile;
1399 double subport_tc_be_rate =
1400 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1401 / (double) s->tc_period;
1402 double pipe_tc_be_rate =
1403 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1404 / (double) params->tc_period;
1405 uint32_t tc_be_ov = s->tc_ov;
1407 /* Unplug pipe from its subport */
1408 s->tc_ov_n -= params->tc_ov_weight;
1409 s->tc_ov_rate -= pipe_tc_be_rate;
1410 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1412 if (s->tc_ov != tc_be_ov) {
1413 RTE_LOG(DEBUG, SCHED,
1414 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1415 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1418 /* Reset the pipe */
1419 memset(p, 0, sizeof(struct rte_sched_pipe));
1425 /* Apply the new pipe configuration */
1426 p->profile = profile;
1427 params = s->pipe_profiles + p->profile;
1429 /* Token Bucket (TB) */
1430 p->tb_time = port->time;
1431 p->tb_credits = params->tb_size / 2;
1433 /* Traffic Classes (TCs) */
1434 p->tc_time = port->time + params->tc_period;
1436 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1438 p->tc_credits[i] = params->tc_credits_per_period[i];
1441 /* Subport best effort tc oversubscription */
1442 double subport_tc_be_rate =
1443 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1444 / (double) s->tc_period;
1445 double pipe_tc_be_rate =
1446 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1447 / (double) params->tc_period;
1448 uint32_t tc_be_ov = s->tc_ov;
1450 s->tc_ov_n += params->tc_ov_weight;
1451 s->tc_ov_rate += pipe_tc_be_rate;
1452 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1454 if (s->tc_ov != tc_be_ov) {
1455 RTE_LOG(DEBUG, SCHED,
1456 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1457 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1459 p->tc_ov_period_id = s->tc_ov_period_id;
1460 p->tc_ov_credits = s->tc_ov_wm;
1467 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1468 uint32_t subport_id,
1469 struct rte_sched_pipe_params *params,
1470 uint32_t *pipe_profile_id)
1472 struct rte_sched_subport *s;
1473 struct rte_sched_pipe_profile *pp;
1480 "%s: Incorrect value for parameter port\n", __func__);
1484 /* Subport id not exceeds the max limit */
1485 if (subport_id > port->n_subports_per_port) {
1487 "%s: Incorrect value for subport id\n", __func__);
1491 s = port->subports[subport_id];
1493 /* Pipe profiles exceeds the max limit */
1494 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1496 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1501 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1504 "%s: Pipe profile check failed(%d)\n", __func__, status);
1508 pp = &s->pipe_profiles[s->n_pipe_profiles];
1509 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1511 /* Pipe profile should not exists */
1512 for (i = 0; i < s->n_pipe_profiles; i++)
1513 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1515 "%s: Pipe profile exists\n", __func__);
1519 /* Pipe profile commit */
1520 *pipe_profile_id = s->n_pipe_profiles;
1521 s->n_pipe_profiles++;
1523 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1524 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1526 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1532 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1533 struct rte_sched_subport_profile_params *params,
1534 uint32_t *subport_profile_id)
1538 struct rte_sched_subport_profile *dst;
1542 RTE_LOG(ERR, SCHED, "%s: "
1543 "Incorrect value for parameter port\n", __func__);
1547 if (params == NULL) {
1548 RTE_LOG(ERR, SCHED, "%s: "
1549 "Incorrect value for parameter profile\n", __func__);
1553 if (subport_profile_id == NULL) {
1554 RTE_LOG(ERR, SCHED, "%s: "
1555 "Incorrect value for parameter subport_profile_id\n",
1560 dst = port->subport_profiles + port->n_subport_profiles;
1562 /* Subport profiles exceeds the max limit */
1563 if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1564 RTE_LOG(ERR, SCHED, "%s: "
1565 "Number of subport profiles exceeds the max limit\n",
1570 status = subport_profile_check(params, port->rate);
1573 "%s: subport profile check failed(%d)\n", __func__, status);
1577 rte_sched_subport_profile_convert(params, dst, port->rate);
1579 /* Subport profile should not exists */
1580 for (i = 0; i < port->n_subport_profiles; i++)
1581 if (memcmp(port->subport_profiles + i,
1582 dst, sizeof(*dst)) == 0) {
1584 "%s: subport profile exists\n", __func__);
1588 /* Subport profile commit */
1589 *subport_profile_id = port->n_subport_profiles;
1590 port->n_subport_profiles++;
1592 rte_sched_port_log_subport_profile(port, *subport_profile_id);
1597 static inline uint32_t
1598 rte_sched_port_qindex(struct rte_sched_port *port,
1601 uint32_t traffic_class,
1604 return ((subport & (port->n_subports_per_port - 1)) <<
1605 (port->n_pipes_per_subport_log2 + 4)) |
1607 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1608 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1609 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1613 rte_sched_port_pkt_write(struct rte_sched_port *port,
1614 struct rte_mbuf *pkt,
1615 uint32_t subport, uint32_t pipe,
1616 uint32_t traffic_class,
1617 uint32_t queue, enum rte_color color)
1620 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1622 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1626 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1627 const struct rte_mbuf *pkt,
1628 uint32_t *subport, uint32_t *pipe,
1629 uint32_t *traffic_class, uint32_t *queue)
1631 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1633 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1634 *pipe = (queue_id >> 4) &
1635 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1636 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1637 *queue = rte_sched_port_tc_queue(port, queue_id);
1641 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1643 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1647 rte_sched_subport_read_stats(struct rte_sched_port *port,
1648 uint32_t subport_id,
1649 struct rte_sched_subport_stats *stats,
1652 struct rte_sched_subport *s;
1654 /* Check user parameters */
1657 "%s: Incorrect value for parameter port\n", __func__);
1661 if (subport_id >= port->n_subports_per_port) {
1663 "%s: Incorrect value for subport id\n", __func__);
1667 if (stats == NULL) {
1669 "%s: Incorrect value for parameter stats\n", __func__);
1673 if (tc_ov == NULL) {
1675 "%s: Incorrect value for tc_ov\n", __func__);
1679 s = port->subports[subport_id];
1681 /* Copy subport stats and clear */
1682 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1683 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1685 /* Subport TC oversubscription status */
1692 rte_sched_queue_read_stats(struct rte_sched_port *port,
1694 struct rte_sched_queue_stats *stats,
1697 struct rte_sched_subport *s;
1698 struct rte_sched_queue *q;
1699 struct rte_sched_queue_extra *qe;
1700 uint32_t subport_id, subport_qmask, subport_qindex;
1702 /* Check user parameters */
1705 "%s: Incorrect value for parameter port\n", __func__);
1709 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1711 "%s: Incorrect value for queue id\n", __func__);
1715 if (stats == NULL) {
1717 "%s: Incorrect value for parameter stats\n", __func__);
1723 "%s: Incorrect value for parameter qlen\n", __func__);
1726 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1727 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1729 s = port->subports[subport_id];
1730 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1731 q = s->queue + subport_qindex;
1732 qe = s->queue_extra + subport_qindex;
1734 /* Copy queue stats and clear */
1735 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1736 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1739 *qlen = q->qw - q->qr;
1744 #ifdef RTE_SCHED_DEBUG
1747 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1750 struct rte_sched_queue *queue = subport->queue + qindex;
1752 return queue->qr == queue->qw;
1755 #endif /* RTE_SCHED_DEBUG */
1757 #ifdef RTE_SCHED_COLLECT_STATS
1760 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1761 struct rte_sched_subport *subport,
1763 struct rte_mbuf *pkt)
1765 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1766 uint32_t pkt_len = pkt->pkt_len;
1768 subport->stats.n_pkts_tc[tc_index] += 1;
1769 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1772 #ifdef RTE_SCHED_RED
1774 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1775 struct rte_sched_subport *subport,
1777 struct rte_mbuf *pkt,
1781 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1782 struct rte_sched_subport *subport,
1784 struct rte_mbuf *pkt,
1785 __rte_unused uint32_t red)
1788 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1789 uint32_t pkt_len = pkt->pkt_len;
1791 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1792 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1793 #ifdef RTE_SCHED_RED
1794 subport->stats.n_pkts_red_dropped[tc_index] += red;
1799 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1801 struct rte_mbuf *pkt)
1803 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1804 uint32_t pkt_len = pkt->pkt_len;
1806 qe->stats.n_pkts += 1;
1807 qe->stats.n_bytes += pkt_len;
1810 #ifdef RTE_SCHED_RED
1812 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1814 struct rte_mbuf *pkt,
1818 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1820 struct rte_mbuf *pkt,
1821 __rte_unused uint32_t red)
1824 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1825 uint32_t pkt_len = pkt->pkt_len;
1827 qe->stats.n_pkts_dropped += 1;
1828 qe->stats.n_bytes_dropped += pkt_len;
1829 #ifdef RTE_SCHED_RED
1830 qe->stats.n_pkts_red_dropped += red;
1834 #endif /* RTE_SCHED_COLLECT_STATS */
1836 #ifdef RTE_SCHED_RED
1839 rte_sched_port_red_drop(struct rte_sched_port *port,
1840 struct rte_sched_subport *subport,
1841 struct rte_mbuf *pkt,
1845 struct rte_sched_queue_extra *qe;
1846 struct rte_red_config *red_cfg;
1847 struct rte_red *red;
1849 enum rte_color color;
1851 tc_index = rte_sched_port_pipe_tc(port, qindex);
1852 color = rte_sched_port_pkt_read_color(pkt);
1853 red_cfg = &subport->red_config[tc_index][color];
1855 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1858 qe = subport->queue_extra + qindex;
1861 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1865 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port,
1866 struct rte_sched_subport *subport, uint32_t qindex)
1868 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1869 struct rte_red *red = &qe->red;
1871 rte_red_mark_queue_empty(red, port->time);
1876 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1877 struct rte_sched_subport *subport __rte_unused,
1878 struct rte_mbuf *pkt __rte_unused,
1879 uint32_t qindex __rte_unused,
1880 uint16_t qlen __rte_unused)
1885 #define rte_sched_port_set_queue_empty_timestamp(port, subport, qindex)
1887 #endif /* RTE_SCHED_RED */
1889 #ifdef RTE_SCHED_DEBUG
1892 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1899 rte_panic("Empty slab at position %u\n", bmp_pos);
1902 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1903 if (mask & bmp_slab) {
1904 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1905 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1912 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1916 #endif /* RTE_SCHED_DEBUG */
1918 static inline struct rte_sched_subport *
1919 rte_sched_port_subport(struct rte_sched_port *port,
1920 struct rte_mbuf *pkt)
1922 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1923 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1925 return port->subports[subport_id];
1928 static inline uint32_t
1929 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1930 struct rte_mbuf *pkt, uint32_t subport_qmask)
1932 struct rte_sched_queue *q;
1933 #ifdef RTE_SCHED_COLLECT_STATS
1934 struct rte_sched_queue_extra *qe;
1936 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1937 uint32_t subport_queue_id = subport_qmask & qindex;
1939 q = subport->queue + subport_queue_id;
1941 #ifdef RTE_SCHED_COLLECT_STATS
1942 qe = subport->queue_extra + subport_queue_id;
1946 return subport_queue_id;
1950 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1951 struct rte_sched_subport *subport,
1953 struct rte_mbuf **qbase)
1955 struct rte_sched_queue *q;
1956 struct rte_mbuf **q_qw;
1959 q = subport->queue + qindex;
1960 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1961 q_qw = qbase + (q->qw & (qsize - 1));
1963 rte_prefetch0(q_qw);
1964 rte_bitmap_prefetch0(subport->bmp, qindex);
1968 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1969 struct rte_sched_subport *subport,
1971 struct rte_mbuf **qbase,
1972 struct rte_mbuf *pkt)
1974 struct rte_sched_queue *q;
1978 q = subport->queue + qindex;
1979 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1980 qlen = q->qw - q->qr;
1982 /* Drop the packet (and update drop stats) when queue is full */
1983 if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1985 rte_pktmbuf_free(pkt);
1986 #ifdef RTE_SCHED_COLLECT_STATS
1987 rte_sched_port_update_subport_stats_on_drop(port, subport,
1988 qindex, pkt, qlen < qsize);
1989 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1995 /* Enqueue packet */
1996 qbase[q->qw & (qsize - 1)] = pkt;
1999 /* Activate queue in the subport bitmap */
2000 rte_bitmap_set(subport->bmp, qindex);
2003 #ifdef RTE_SCHED_COLLECT_STATS
2004 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2005 rte_sched_port_update_queue_stats(subport, qindex, pkt);
2013 * The enqueue function implements a 4-level pipeline with each stage
2014 * processing two different packets. The purpose of using a pipeline
2015 * is to hide the latency of prefetching the data structures. The
2016 * naming convention is presented in the diagram below:
2018 * p00 _______ p10 _______ p20 _______ p30 _______
2019 * ----->| |----->| |----->| |----->| |----->
2020 * | 0 | | 1 | | 2 | | 3 |
2021 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2026 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2029 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2030 *pkt30, *pkt31, *pkt_last;
2031 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2032 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2033 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2034 *subport20, *subport21, *subport30, *subport31, *subport_last;
2035 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2036 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2037 uint32_t subport_qmask;
2041 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2044 * Less then 6 input packets available, which is not enough to
2047 if (unlikely(n_pkts < 6)) {
2048 struct rte_sched_subport *subports[5];
2049 struct rte_mbuf **q_base[5];
2052 /* Prefetch the mbuf structure of each packet */
2053 for (i = 0; i < n_pkts; i++)
2054 rte_prefetch0(pkts[i]);
2056 /* Prefetch the subport structure for each packet */
2057 for (i = 0; i < n_pkts; i++)
2058 subports[i] = rte_sched_port_subport(port, pkts[i]);
2060 /* Prefetch the queue structure for each queue */
2061 for (i = 0; i < n_pkts; i++)
2062 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2063 pkts[i], subport_qmask);
2065 /* Prefetch the write pointer location of each queue */
2066 for (i = 0; i < n_pkts; i++) {
2067 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2068 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2072 /* Write each packet to its queue */
2073 for (i = 0; i < n_pkts; i++)
2074 result += rte_sched_port_enqueue_qwa(port, subports[i],
2075 q[i], q_base[i], pkts[i]);
2080 /* Feed the first 3 stages of the pipeline (6 packets needed) */
2083 rte_prefetch0(pkt20);
2084 rte_prefetch0(pkt21);
2088 rte_prefetch0(pkt10);
2089 rte_prefetch0(pkt11);
2091 subport20 = rte_sched_port_subport(port, pkt20);
2092 subport21 = rte_sched_port_subport(port, pkt21);
2093 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2094 pkt20, subport_qmask);
2095 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2096 pkt21, subport_qmask);
2100 rte_prefetch0(pkt00);
2101 rte_prefetch0(pkt01);
2103 subport10 = rte_sched_port_subport(port, pkt10);
2104 subport11 = rte_sched_port_subport(port, pkt11);
2105 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2106 pkt10, subport_qmask);
2107 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2108 pkt11, subport_qmask);
2110 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2111 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2112 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2113 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2115 /* Run the pipeline */
2116 for (i = 6; i < (n_pkts & (~1)); i += 2) {
2117 /* Propagate stage inputs */
2128 subport30 = subport20;
2129 subport31 = subport21;
2130 subport20 = subport10;
2131 subport21 = subport11;
2132 q30_base = q20_base;
2133 q31_base = q21_base;
2135 /* Stage 0: Get packets in */
2137 pkt01 = pkts[i + 1];
2138 rte_prefetch0(pkt00);
2139 rte_prefetch0(pkt01);
2141 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
2142 subport10 = rte_sched_port_subport(port, pkt10);
2143 subport11 = rte_sched_port_subport(port, pkt11);
2144 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2145 pkt10, subport_qmask);
2146 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2147 pkt11, subport_qmask);
2149 /* Stage 2: Prefetch queue write location */
2150 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2151 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2152 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2153 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2155 /* Stage 3: Write packet to queue and activate queue */
2156 r30 = rte_sched_port_enqueue_qwa(port, subport30,
2157 q30, q30_base, pkt30);
2158 r31 = rte_sched_port_enqueue_qwa(port, subport31,
2159 q31, q31_base, pkt31);
2160 result += r30 + r31;
2164 * Drain the pipeline (exactly 6 packets).
2165 * Handle the last packet in the case
2166 * of an odd number of input packets.
2168 pkt_last = pkts[n_pkts - 1];
2169 rte_prefetch0(pkt_last);
2171 subport00 = rte_sched_port_subport(port, pkt00);
2172 subport01 = rte_sched_port_subport(port, pkt01);
2173 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2174 pkt00, subport_qmask);
2175 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2176 pkt01, subport_qmask);
2178 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2179 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2180 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2181 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2183 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2184 q20, q20_base, pkt20);
2185 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2186 q21, q21_base, pkt21);
2187 result += r20 + r21;
2189 subport_last = rte_sched_port_subport(port, pkt_last);
2190 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2191 pkt_last, subport_qmask);
2193 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2194 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2195 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2196 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2198 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2200 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2202 result += r10 + r11;
2204 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2205 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2206 q_last, q_last_base);
2208 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2210 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2212 result += r00 + r01;
2215 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2216 q_last, q_last_base, pkt_last);
2223 #ifndef RTE_SCHED_SUBPORT_TC_OV
2226 grinder_credits_update(struct rte_sched_port *port,
2227 struct rte_sched_subport *subport, uint32_t pos)
2229 struct rte_sched_grinder *grinder = subport->grinder + pos;
2230 struct rte_sched_pipe *pipe = grinder->pipe;
2231 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2236 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2237 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2238 subport->tb_credits = RTE_MIN(subport->tb_credits, subport->tb_size);
2239 subport->tb_time += n_periods * subport->tb_period;
2242 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2243 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2244 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2245 pipe->tb_time += n_periods * params->tb_period;
2248 if (unlikely(port->time >= subport->tc_time)) {
2249 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2250 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2252 subport->tc_time = port->time + subport->tc_period;
2256 if (unlikely(port->time >= pipe->tc_time)) {
2257 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2258 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2260 pipe->tc_time = port->time + params->tc_period;
2266 static inline uint64_t
2267 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2268 struct rte_sched_subport *subport)
2270 uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2271 uint64_t tc_consumption = 0, tc_ov_consumption_max;
2272 uint64_t tc_ov_wm = subport->tc_ov_wm;
2275 if (subport->tc_ov == 0)
2276 return subport->tc_ov_wm_max;
2278 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2279 tc_ov_consumption[i] =
2280 subport->tc_credits_per_period[i] - subport->tc_credits[i];
2281 tc_consumption += tc_ov_consumption[i];
2284 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2285 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2286 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2288 tc_ov_consumption_max =
2289 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2292 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2293 (tc_ov_consumption_max - port->mtu)) {
2294 tc_ov_wm -= tc_ov_wm >> 7;
2295 if (tc_ov_wm < subport->tc_ov_wm_min)
2296 tc_ov_wm = subport->tc_ov_wm_min;
2301 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2302 if (tc_ov_wm > subport->tc_ov_wm_max)
2303 tc_ov_wm = subport->tc_ov_wm_max;
2309 grinder_credits_update(struct rte_sched_port *port,
2310 struct rte_sched_subport *subport, uint32_t pos)
2312 struct rte_sched_grinder *grinder = subport->grinder + pos;
2313 struct rte_sched_pipe *pipe = grinder->pipe;
2314 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2319 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2320 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2321 subport->tb_credits = RTE_MIN(subport->tb_credits, subport->tb_size);
2322 subport->tb_time += n_periods * subport->tb_period;
2325 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2326 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2327 pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2328 pipe->tb_time += n_periods * params->tb_period;
2331 if (unlikely(port->time >= subport->tc_time)) {
2332 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, subport);
2334 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2335 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2337 subport->tc_time = port->time + subport->tc_period;
2338 subport->tc_ov_period_id++;
2342 if (unlikely(port->time >= pipe->tc_time)) {
2343 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2344 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2345 pipe->tc_time = port->time + params->tc_period;
2348 /* Pipe TCs - Oversubscription */
2349 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2350 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2352 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2356 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2359 #ifndef RTE_SCHED_SUBPORT_TC_OV
2362 grinder_credits_check(struct rte_sched_port *port,
2363 struct rte_sched_subport *subport, uint32_t pos)
2365 struct rte_sched_grinder *grinder = subport->grinder + pos;
2366 struct rte_sched_pipe *pipe = grinder->pipe;
2367 struct rte_mbuf *pkt = grinder->pkt;
2368 uint32_t tc_index = grinder->tc_index;
2369 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2370 uint64_t subport_tb_credits = subport->tb_credits;
2371 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2372 uint64_t pipe_tb_credits = pipe->tb_credits;
2373 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2376 /* Check queue 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);
2382 if (!enough_credits)
2385 /* Update port credits */
2386 subport->tb_credits -= pkt_len;
2387 subport->tc_credits[tc_index] -= pkt_len;
2388 pipe->tb_credits -= pkt_len;
2389 pipe->tc_credits[tc_index] -= pkt_len;
2397 grinder_credits_check(struct rte_sched_port *port,
2398 struct rte_sched_subport *subport, uint32_t pos)
2400 struct rte_sched_grinder *grinder = subport->grinder + pos;
2401 struct rte_sched_pipe *pipe = grinder->pipe;
2402 struct rte_mbuf *pkt = grinder->pkt;
2403 uint32_t tc_index = grinder->tc_index;
2404 uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2405 uint64_t subport_tb_credits = subport->tb_credits;
2406 uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2407 uint64_t pipe_tb_credits = pipe->tb_credits;
2408 uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2409 uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2410 uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2411 uint64_t pipe_tc_ov_credits;
2415 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2416 pipe_tc_ov_mask1[i] = ~0LLU;
2418 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2419 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2420 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2422 /* Check pipe and subport credits */
2423 enough_credits = (pkt_len <= subport_tb_credits) &&
2424 (pkt_len <= subport_tc_credits) &&
2425 (pkt_len <= pipe_tb_credits) &&
2426 (pkt_len <= pipe_tc_credits) &&
2427 (pkt_len <= pipe_tc_ov_credits);
2429 if (!enough_credits)
2432 /* Update pipe and subport credits */
2433 subport->tb_credits -= pkt_len;
2434 subport->tc_credits[tc_index] -= pkt_len;
2435 pipe->tb_credits -= pkt_len;
2436 pipe->tc_credits[tc_index] -= pkt_len;
2437 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2442 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2446 grinder_schedule(struct rte_sched_port *port,
2447 struct rte_sched_subport *subport, uint32_t pos)
2449 struct rte_sched_grinder *grinder = subport->grinder + pos;
2450 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2451 struct rte_mbuf *pkt = grinder->pkt;
2452 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2453 uint32_t be_tc_active;
2455 if (!grinder_credits_check(port, subport, pos))
2458 /* Advance port time */
2459 port->time += pkt_len;
2462 port->pkts_out[port->n_pkts_out++] = pkt;
2465 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2466 grinder->wrr_tokens[grinder->qpos] +=
2467 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2469 if (queue->qr == queue->qw) {
2470 uint32_t qindex = grinder->qindex[grinder->qpos];
2472 rte_bitmap_clear(subport->bmp, qindex);
2473 grinder->qmask &= ~(1 << grinder->qpos);
2475 grinder->wrr_mask[grinder->qpos] = 0;
2476 rte_sched_port_set_queue_empty_timestamp(port, subport, qindex);
2479 /* Reset pipe loop detection */
2480 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2481 grinder->productive = 1;
2486 #ifdef SCHED_VECTOR_SSE4
2489 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2491 __m128i index = _mm_set1_epi32(base_pipe);
2492 __m128i pipes = _mm_load_si128((__m128i *)subport->grinder_base_bmp_pos);
2493 __m128i res = _mm_cmpeq_epi32(pipes, index);
2495 pipes = _mm_load_si128((__m128i *)(subport->grinder_base_bmp_pos + 4));
2496 pipes = _mm_cmpeq_epi32(pipes, index);
2497 res = _mm_or_si128(res, pipes);
2499 if (_mm_testz_si128(res, res))
2505 #elif defined(SCHED_VECTOR_NEON)
2508 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2510 uint32x4_t index, pipes;
2511 uint32_t *pos = (uint32_t *)subport->grinder_base_bmp_pos;
2513 index = vmovq_n_u32(base_pipe);
2514 pipes = vld1q_u32(pos);
2515 if (!vminvq_u32(veorq_u32(pipes, index)))
2518 pipes = vld1q_u32(pos + 4);
2519 if (!vminvq_u32(veorq_u32(pipes, index)))
2528 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2532 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2533 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2540 #endif /* RTE_SCHED_OPTIMIZATIONS */
2543 grinder_pcache_populate(struct rte_sched_subport *subport,
2544 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2546 struct rte_sched_grinder *grinder = subport->grinder + pos;
2549 grinder->pcache_w = 0;
2550 grinder->pcache_r = 0;
2552 w[0] = (uint16_t) bmp_slab;
2553 w[1] = (uint16_t) (bmp_slab >> 16);
2554 w[2] = (uint16_t) (bmp_slab >> 32);
2555 w[3] = (uint16_t) (bmp_slab >> 48);
2557 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2558 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2559 grinder->pcache_w += (w[0] != 0);
2561 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2562 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2563 grinder->pcache_w += (w[1] != 0);
2565 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2566 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2567 grinder->pcache_w += (w[2] != 0);
2569 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2570 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2571 grinder->pcache_w += (w[3] != 0);
2575 grinder_tccache_populate(struct rte_sched_subport *subport,
2576 uint32_t pos, uint32_t qindex, uint16_t qmask)
2578 struct rte_sched_grinder *grinder = subport->grinder + pos;
2581 grinder->tccache_w = 0;
2582 grinder->tccache_r = 0;
2584 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2585 b = (uint8_t) ((qmask >> i) & 0x1);
2586 grinder->tccache_qmask[grinder->tccache_w] = b;
2587 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2588 grinder->tccache_w += (b != 0);
2591 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2592 grinder->tccache_qmask[grinder->tccache_w] = b;
2593 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2594 RTE_SCHED_TRAFFIC_CLASS_BE;
2595 grinder->tccache_w += (b != 0);
2599 grinder_next_tc(struct rte_sched_port *port,
2600 struct rte_sched_subport *subport, uint32_t pos)
2602 struct rte_sched_grinder *grinder = subport->grinder + pos;
2603 struct rte_mbuf **qbase;
2607 if (grinder->tccache_r == grinder->tccache_w)
2610 qindex = grinder->tccache_qindex[grinder->tccache_r];
2611 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2612 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2614 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2615 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2616 grinder->qsize = qsize;
2618 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2619 grinder->queue[0] = subport->queue + qindex;
2620 grinder->qbase[0] = qbase;
2621 grinder->qindex[0] = qindex;
2622 grinder->tccache_r++;
2627 grinder->queue[0] = subport->queue + qindex;
2628 grinder->queue[1] = subport->queue + qindex + 1;
2629 grinder->queue[2] = subport->queue + qindex + 2;
2630 grinder->queue[3] = subport->queue + qindex + 3;
2632 grinder->qbase[0] = qbase;
2633 grinder->qbase[1] = qbase + qsize;
2634 grinder->qbase[2] = qbase + 2 * qsize;
2635 grinder->qbase[3] = qbase + 3 * qsize;
2637 grinder->qindex[0] = qindex;
2638 grinder->qindex[1] = qindex + 1;
2639 grinder->qindex[2] = qindex + 2;
2640 grinder->qindex[3] = qindex + 3;
2642 grinder->tccache_r++;
2647 grinder_next_pipe(struct rte_sched_port *port,
2648 struct rte_sched_subport *subport, uint32_t pos)
2650 struct rte_sched_grinder *grinder = subport->grinder + pos;
2651 uint32_t pipe_qindex;
2652 uint16_t pipe_qmask;
2654 if (grinder->pcache_r < grinder->pcache_w) {
2655 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2656 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2657 grinder->pcache_r++;
2659 uint64_t bmp_slab = 0;
2660 uint32_t bmp_pos = 0;
2662 /* Get another non-empty pipe group */
2663 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2666 #ifdef RTE_SCHED_DEBUG
2667 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2670 /* Return if pipe group already in one of the other grinders */
2671 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2672 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2675 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2677 /* Install new pipe group into grinder's pipe cache */
2678 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2680 pipe_qmask = grinder->pcache_qmask[0];
2681 pipe_qindex = grinder->pcache_qindex[0];
2682 grinder->pcache_r = 1;
2685 /* Install new pipe in the grinder */
2686 grinder->pindex = pipe_qindex >> 4;
2687 grinder->subport = subport;
2688 grinder->pipe = subport->pipe + grinder->pindex;
2689 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2690 grinder->productive = 0;
2692 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2693 grinder_next_tc(port, subport, pos);
2695 /* Check for pipe exhaustion */
2696 if (grinder->pindex == subport->pipe_loop) {
2697 subport->pipe_exhaustion = 1;
2698 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2706 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2708 struct rte_sched_grinder *grinder = subport->grinder + pos;
2709 struct rte_sched_pipe *pipe = grinder->pipe;
2710 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2711 uint32_t qmask = grinder->qmask;
2713 grinder->wrr_tokens[0] =
2714 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2715 grinder->wrr_tokens[1] =
2716 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2717 grinder->wrr_tokens[2] =
2718 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2719 grinder->wrr_tokens[3] =
2720 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2722 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2723 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2724 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2725 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2727 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2728 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2729 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2730 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2734 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2736 struct rte_sched_grinder *grinder = subport->grinder + pos;
2737 struct rte_sched_pipe *pipe = grinder->pipe;
2739 pipe->wrr_tokens[0] =
2740 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2741 RTE_SCHED_WRR_SHIFT;
2742 pipe->wrr_tokens[1] =
2743 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2744 RTE_SCHED_WRR_SHIFT;
2745 pipe->wrr_tokens[2] =
2746 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2747 RTE_SCHED_WRR_SHIFT;
2748 pipe->wrr_tokens[3] =
2749 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2750 RTE_SCHED_WRR_SHIFT;
2754 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2756 struct rte_sched_grinder *grinder = subport->grinder + pos;
2757 uint16_t wrr_tokens_min;
2759 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2760 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2761 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2762 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2764 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2765 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2767 grinder->wrr_tokens[0] -= wrr_tokens_min;
2768 grinder->wrr_tokens[1] -= wrr_tokens_min;
2769 grinder->wrr_tokens[2] -= wrr_tokens_min;
2770 grinder->wrr_tokens[3] -= wrr_tokens_min;
2774 #define grinder_evict(subport, pos)
2777 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2779 struct rte_sched_grinder *grinder = subport->grinder + pos;
2781 rte_prefetch0(grinder->pipe);
2782 rte_prefetch0(grinder->queue[0]);
2786 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2788 struct rte_sched_grinder *grinder = subport->grinder + pos;
2789 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2791 qsize = grinder->qsize;
2794 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2795 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2797 rte_prefetch0(grinder->qbase[0] + qr[0]);
2801 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2802 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2803 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2804 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2806 rte_prefetch0(grinder->qbase[0] + qr[0]);
2807 rte_prefetch0(grinder->qbase[1] + qr[1]);
2809 grinder_wrr_load(subport, pos);
2810 grinder_wrr(subport, pos);
2812 rte_prefetch0(grinder->qbase[2] + qr[2]);
2813 rte_prefetch0(grinder->qbase[3] + qr[3]);
2817 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2819 struct rte_sched_grinder *grinder = subport->grinder + pos;
2820 uint32_t qpos = grinder->qpos;
2821 struct rte_mbuf **qbase = grinder->qbase[qpos];
2822 uint16_t qsize = grinder->qsize;
2823 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2825 grinder->pkt = qbase[qr];
2826 rte_prefetch0(grinder->pkt);
2828 if (unlikely((qr & 0x7) == 7)) {
2829 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2831 rte_prefetch0(qbase + qr_next);
2835 static inline uint32_t
2836 grinder_handle(struct rte_sched_port *port,
2837 struct rte_sched_subport *subport, uint32_t pos)
2839 struct rte_sched_grinder *grinder = subport->grinder + pos;
2841 switch (grinder->state) {
2842 case e_GRINDER_PREFETCH_PIPE:
2844 if (grinder_next_pipe(port, subport, pos)) {
2845 grinder_prefetch_pipe(subport, pos);
2846 subport->busy_grinders++;
2848 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2855 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2857 struct rte_sched_pipe *pipe = grinder->pipe;
2859 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2860 grinder_prefetch_tc_queue_arrays(subport, pos);
2861 grinder_credits_update(port, subport, pos);
2863 grinder->state = e_GRINDER_PREFETCH_MBUF;
2867 case e_GRINDER_PREFETCH_MBUF:
2869 grinder_prefetch_mbuf(subport, pos);
2871 grinder->state = e_GRINDER_READ_MBUF;
2875 case e_GRINDER_READ_MBUF:
2877 uint32_t wrr_active, result = 0;
2879 result = grinder_schedule(port, subport, pos);
2881 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2883 /* Look for next packet within the same TC */
2884 if (result && grinder->qmask) {
2886 grinder_wrr(subport, pos);
2888 grinder_prefetch_mbuf(subport, pos);
2894 grinder_wrr_store(subport, pos);
2896 /* Look for another active TC within same pipe */
2897 if (grinder_next_tc(port, subport, pos)) {
2898 grinder_prefetch_tc_queue_arrays(subport, pos);
2900 grinder->state = e_GRINDER_PREFETCH_MBUF;
2904 if (grinder->productive == 0 &&
2905 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2906 subport->pipe_loop = grinder->pindex;
2908 grinder_evict(subport, pos);
2910 /* Look for another active pipe */
2911 if (grinder_next_pipe(port, subport, pos)) {
2912 grinder_prefetch_pipe(subport, pos);
2914 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2918 /* No active pipe found */
2919 subport->busy_grinders--;
2921 grinder->state = e_GRINDER_PREFETCH_PIPE;
2926 rte_panic("Algorithmic error (invalid state)\n");
2932 rte_sched_port_time_resync(struct rte_sched_port *port)
2934 uint64_t cycles = rte_get_tsc_cycles();
2935 uint64_t cycles_diff;
2936 uint64_t bytes_diff;
2939 if (cycles < port->time_cpu_cycles)
2940 port->time_cpu_cycles = 0;
2942 cycles_diff = cycles - port->time_cpu_cycles;
2943 /* Compute elapsed time in bytes */
2944 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2945 port->inv_cycles_per_byte);
2947 /* Advance port time */
2948 port->time_cpu_cycles +=
2949 (bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2950 port->time_cpu_bytes += bytes_diff;
2951 if (port->time < port->time_cpu_bytes)
2952 port->time = port->time_cpu_bytes;
2954 /* Reset pipe loop detection */
2955 for (i = 0; i < port->n_subports_per_port; i++)
2956 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2960 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2964 /* Check if any exception flag is set */
2965 exceptions = (second_pass && subport->busy_grinders == 0) ||
2966 (subport->pipe_exhaustion == 1);
2968 /* Clear exception flags */
2969 subport->pipe_exhaustion = 0;
2975 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2977 struct rte_sched_subport *subport;
2978 uint32_t subport_id = port->subport_id;
2979 uint32_t i, n_subports = 0, count;
2981 port->pkts_out = pkts;
2982 port->n_pkts_out = 0;
2984 rte_sched_port_time_resync(port);
2986 /* Take each queue in the grinder one step further */
2987 for (i = 0, count = 0; ; i++) {
2988 subport = port->subports[subport_id];
2990 count += grinder_handle(port, subport,
2991 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2993 if (count == n_pkts) {
2996 if (subport_id == port->n_subports_per_port)
2999 port->subport_id = subport_id;
3003 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
3009 if (subport_id == port->n_subports_per_port)
3012 if (n_subports == port->n_subports_per_port) {
3013 port->subport_id = subport_id;